-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Should import Cpp inline be part of the declaration list rather than the preamble? #6830
Description
Summary of issue:
The toolchain currently allows import Cpp inline .... While the leads have informally agreed that we like this functionality and there's not been a lot of pushback on the syntax, it's not gone through the proposal process yet. We currently treat it as an import directive, so it goes at the start of the file with other imports (in any order), not in the declaration list of the file.
I think we should reconsider that: instead of inline C++ being part of the import sequence, it should be a declaration, it should appear after all imports, and it should be allowed to be interleaved with Carbon declarations.
Details:
We generally want our imports to be order-independent. For Carbon imports, they essentially are already. C++ imports are not -- later imported headers can see earlier ones -- but we don't necessarily intend for that to be the case, and I think we should be open to modeling C++ imports as instead building and importing a C++ header unit. But inline C++ code isn't like that -- we want it to be order-dependent, and to be able to name both C++ and Carbon entities introduced by earlier imports.
Also, inline C++ has turned out to be extremely useful for invoking C++ file-scope macros, such as ABSL_FLAG. When these can be expanded at the start of the file, this works great, but some of these macros are doing things like registering classes with some kind of global registry, and those macros would ideally be expanded either at the end of the file or immediately after the class definition.
I think we should answer three questions before proceeding with a proposal here:
- Do we want an inline C++ facility? [Suggested answer: yes]
- Where should it be usable? [Suggested answer: as a top-level declaration]
- What syntax should we use? [If it doesn't live with the other import directives, perhaps
importis the wrong choice?]
Any other information that you want to share?
Possible alternative syntaxes:
Today's syntax
import Cpp inline '''
// your c++ code here
''';Using inline as a member of Cpp (note that this would be a top-level declaration with no introducer!)
Cpp.inline '''
// your c++ code here
''';Using inline as a keyword
inline Cpp '''
// your c++ code here
''';Same but with package as well
inline package Cpp '''
// your c++ code here
''';Using extern (note this would be extern not followed by an introducer!)
extern Cpp '''
// your c++ code here
''';Using extern with package
extern package Cpp '''
// your c++ code here
''';It might also be worth considering how to support file-scope inline asm at the same time, as it may be reasonable to use a similar syntax there.