mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Introduce and use #externalMacro for externally-defined macros.
Align the grammar of macro declarations with SE-0382, so that macro
definitions are parsed as an expression. External macro definitions
are referenced via a referenced to the macro `#externalMacro`. Define
that macro in the standard library, and recognize uses of it as the
definition of other macros to use externally-defined macros. For
example, this means that the "stringify" macro used in a lot of
examples is now defined as something like this:
@expression macro stringify<T>(_ value: T) -> (T, String) =
#externalMacro(module: "MyMacros", type: "StringifyMacro")
We still parse the old "A.B" syntax for two reasons. First, it's
helpful to anyone who has existing code using the prior syntax, so they
get a warning + Fix-It to rewrite to the new syntax. Second, we use it
to define builtin macros like `externalMacro` itself, which looks like this:
@expression
public macro externalMacro<T>(module: String, type: String) -> T =
Builtin.ExternalMacro
This uses the same virtual `Builtin` module as other library builtins,
and we can expand it to handle other builtin macro implementations
(such as #line) over time.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include "swift/AST/Initializer.h"
|
||||
#include "swift/AST/LazyResolver.h"
|
||||
#include "swift/AST/LinkLibrary.h"
|
||||
#include "swift/AST/MacroDefinition.h"
|
||||
#include "swift/AST/ParameterList.h"
|
||||
#include "swift/AST/Pattern.h"
|
||||
#include "swift/AST/PrettyStackTrace.h"
|
||||
@@ -4324,6 +4325,32 @@ public:
|
||||
|
||||
Type resultType = macro->getResultInterfaceType();
|
||||
|
||||
uint8_t builtinID = 0;
|
||||
IdentifierID externalModuleNameID = 0;
|
||||
IdentifierID externalMacroTypeNameID = 0;
|
||||
auto def = macro->getDefinition();
|
||||
switch (def.kind) {
|
||||
case MacroDefinition::Kind::Invalid:
|
||||
case MacroDefinition::Kind::Undefined:
|
||||
break;
|
||||
|
||||
case MacroDefinition::Kind::External: {
|
||||
auto external = def.getExternalMacro();
|
||||
externalModuleNameID = S.addDeclBaseNameRef(external.moduleName);
|
||||
externalMacroTypeNameID = S.addDeclBaseNameRef(external.macroTypeName);
|
||||
break;
|
||||
}
|
||||
|
||||
case MacroDefinition::Kind::Builtin: {
|
||||
switch (def.getBuiltinKind()) {
|
||||
case BuiltinMacroKind::ExternalMacro:
|
||||
builtinID = 1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned abbrCode = S.DeclTypeAbbrCodes[MacroLayout::Code];
|
||||
MacroLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
|
||||
contextID.getOpaqueValue(),
|
||||
@@ -4334,8 +4361,9 @@ public:
|
||||
S.addTypeRef(resultType),
|
||||
rawAccessLevel,
|
||||
macro->getName().getArgumentNames().size(),
|
||||
S.addDeclBaseNameRef(macro->externalModuleName),
|
||||
S.addDeclBaseNameRef(macro->externalMacroTypeName),
|
||||
builtinID,
|
||||
externalModuleNameID,
|
||||
externalMacroTypeNameID,
|
||||
nameComponentsAndDependencies);
|
||||
|
||||
writeGenericParams(macro->getGenericParams());
|
||||
|
||||
Reference in New Issue
Block a user