Skip to content

Commit 36613dc

Browse files
committed
[Macros] Simplify the macro signature context with a generic typealias.
The macro signature context was a (possibly generic) struct declaration containing a typealias. We don't need the struct itself, because the typealias can be generic as well. This eliminates an extra, annoying hop through name lookup.
1 parent f98842b commit 36613dc

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,19 @@ public func getMacroEvaluationContext(
128128
)
129129
let sourceFile = Parser.parse(source: evaluatedSource)
130130

131-
// Dig out the top-level struct declaration. That's all we'll
131+
// Dig out the top-level typealias declaration. That's all we'll
132132
// parse.
133-
guard let structDecl = sourceFile.statements.first(
134-
where: { item in item.item.is(StructDeclSyntax.self)
135-
})?.item.as(StructDeclSyntax.self) else {
133+
guard let typealiasDecl = sourceFile.statements.first(
134+
where: { item in item.item.is(TypealiasDeclSyntax.self)
135+
})?.item.as(TypealiasDeclSyntax.self) else {
136136
return nil
137137
}
138138

139139
// Parse and ASTGen that top-level declaration.
140140
// FIXME: we need to emit diagnostics from this.
141141
return ASTGenVisitor(
142142
ctx: context, base: sourceFilePtr, declContext: declContext
143-
).visit(structDecl).rawValue
143+
).visit(typealiasDecl).rawValue
144144
}
145145

146146
@_cdecl("swift_ASTGen_evaluateMacro")

lib/Sema/TypeCheckMacros.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ swift_ASTGen_getMacroTypeSignature(void *macro,
5959
/// The macro signature is a user-defined generic signature and return
6060
/// type that serves as the "interface type" of references to the macro. The
6161
/// current implementation takes those pieces of syntax from the macro itself,
62-
/// then inserts them into a Swift struct that looks like
62+
/// then inserts them into a Swift typealias that looks like
6363
///
6464
/// \code
65-
/// struct __MacroEvaluationContext\(macro.genericSignature) {
66-
/// typealias SignatureType = \(macro.signature)
67-
/// }
65+
/// typealias __MacroEvaluationContext\(macro.genericSignature ?? "") =
66+
/// \(macro.signature)
6867
/// \endcode
6968
///
7069
/// So that we can use all of Swift's native name lookup and type resolution
@@ -78,10 +77,9 @@ getMacroSignatureContextBuffer(
7877
) {
7978
std::string source;
8079
llvm::raw_string_ostream out(source);
81-
out << "struct __MacroEvaluationContext"
82-
<< (genericSignature ? *genericSignature : "") << " {\n"
83-
<< " typealias SignatureType = " << typeSignature << "\n"
84-
<< "}";
80+
out << "typealias __MacroEvaluationContext"
81+
<< (genericSignature ? *genericSignature : "")
82+
<< " = " << typeSignature << "\n";
8583
auto len = source.length();
8684
auto *buffer = (char *)malloc(len + 1);
8785
memcpy(buffer, source.data(), len + 1);
@@ -132,16 +130,12 @@ getMacroSignature(
132130
if (!decl)
133131
return None;
134132

135-
auto structDecl = cast<StructDecl>(decl);
136-
137133
// Make sure imports are resolved in this file.
138134
performImportResolution(*macroSourceFile);
139135

140-
// Dig the SignatureType typealias out of the struct and return that type.
141-
auto sigName = ctx.getIdentifier("SignatureType");
142-
auto *signature = cast<TypeAliasDecl>(structDecl->lookupDirect(sigName).front());
136+
auto typealias = cast<TypeAliasDecl>(decl);
143137
return std::make_pair(
144-
signature->getGenericSignature(), signature->getUnderlyingType());
138+
typealias->getGenericSignature(), typealias->getUnderlyingType());
145139
}
146140

147141
/// Create a macro.

0 commit comments

Comments
 (0)