Skip to content

Commit bbb5c7a

Browse files
committed
[Macros] Allow enums and classes/actors to be macro definitions, too
1 parent 222bc03 commit bbb5c7a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ extern "C" ptrdiff_t swift_ASTGen_evaluateMacro(
4545
/// Produce the mangled name for the nominal type descriptor of a type
4646
/// referenced by its module and type name.
4747
static std::string mangledNameForTypeMetadataAccessor(
48-
StringRef moduleName, StringRef typeName) {
48+
StringRef moduleName, StringRef typeName, Node::Kind typeKind) {
4949
using namespace Demangle;
5050

5151
// kind=Global
5252
// kind=NominalTypeDescriptor
5353
// kind=Type
54-
// kind=Structure
54+
// kind=Structure|Enum|Class
5555
// kind=Module, text=moduleName
5656
// kind=Identifier, text=typeName
5757
Demangle::Demangler D;
@@ -64,7 +64,7 @@ static std::string mangledNameForTypeMetadataAccessor(
6464
{
6565
auto *module = D.createNode(Node::Kind::Module, moduleName);
6666
auto *identifier = D.createNode(Node::Kind::Identifier, typeName);
67-
auto *structNode = D.createNode(Node::Kind::Structure);
67+
auto *structNode = D.createNode(typeKind);
6868
structNode->addChild(module, D);
6969
structNode->addChild(identifier, D);
7070
type->addChild(structNode, D);
@@ -82,9 +82,22 @@ static std::string mangledNameForTypeMetadataAccessor(
8282
/// Look for macro's type metadata given its external module and type name.
8383
static void const *lookupMacroTypeMetadataByExternalName(
8484
ASTContext &ctx, StringRef moduleName, StringRef typeName) {
85-
// Look up the type metadata accessor.
86-
auto symbolName = mangledNameForTypeMetadataAccessor(moduleName, typeName);
87-
auto accessorAddr = ctx.getAddressOfSymbol(symbolName.c_str());
85+
// Look up the type metadata accessor as a struct, enum, or class.
86+
const Demangle::Node::Kind typeKinds[] = {
87+
Demangle::Node::Kind::Structure,
88+
Demangle::Node::Kind::Enum,
89+
Demangle::Node::Kind::Class
90+
};
91+
92+
void *accessorAddr = nullptr;
93+
for (auto typeKind : typeKinds) {
94+
auto symbolName = mangledNameForTypeMetadataAccessor(
95+
moduleName, typeName, typeKind);
96+
accessorAddr = ctx.getAddressOfSymbol(symbolName.c_str());
97+
if (accessorAddr)
98+
break;
99+
}
100+
88101
if (!accessorAddr)
89102
return nullptr;
90103

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension SimpleDiagnosticMessage: FixItMessage {
6767
var fixItID: MessageID { diagnosticID }
6868
}
6969

70-
public struct AddBlocker: ExpressionMacro {
70+
public enum AddBlocker: ExpressionMacro {
7171
public static func expansion(
7272
of node: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
7373
) -> ExprSyntax {
@@ -155,7 +155,7 @@ public struct AddBlocker: ExpressionMacro {
155155
}
156156
}
157157

158-
public struct RecursiveMacro: ExpressionMacro {
158+
public class RecursiveMacro: ExpressionMacro {
159159
public static func expansion(
160160
of macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
161161
) -> ExprSyntax {

0 commit comments

Comments
 (0)