Skip to content

Commit 0f31305

Browse files
committed
[Macros] Use the resolved macro for evaluation.
Don't look up the macro name again when doing expansion; that's already been done when we found the macro definition.
1 parent cb338dd commit 0f31305

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private func allocateUTF8String(
8585
@_cdecl("swift_ASTGen_evaluateMacro")
8686
@usableFromInline
8787
func evaluateMacro(
88+
macroPtr: UnsafeMutablePointer<UInt8>,
8889
sourceFilePtr: UnsafePointer<UInt8>,
8990
sourceLocationPtr: UnsafePointer<UInt8>?,
9091
expandedSourcePointer: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
@@ -117,13 +118,12 @@ func evaluateMacro(
117118
}
118119

119120
guard let parentSyntax = token.parent,
120-
parentSyntax.is(MacroExpansionExprSyntax.self)
121+
let parentExpansion = parentSyntax.as(MacroExpansionExprSyntax.self)
121122
else {
122123
print("not on a macro expansion node: \(token.recursiveDescription)")
123124
return -1
124125
}
125126

126-
let macroSystem = MacroSystem.exampleSystem
127127
let converter = SourceLocationConverter(
128128
file: sourceFile.pointee.fileName, tree: sf
129129
)
@@ -132,10 +132,15 @@ func evaluateMacro(
132132
sourceLocationConverter: converter
133133
)
134134

135-
let evaluatedSyntax = parentSyntax.evaluateMacro(
136-
with: macroSystem, context: context
137-
) { error in
138-
/* TODO: Report errors */
135+
let evaluatedSyntax: ExprSyntax = macroPtr.withMemoryRebound(to: ExportedMacro.self, capacity: 1) { macro in
136+
guard let exprMacro = macro.pointee.macro as? ExpressionMacro.Type else {
137+
print("not an expression macro")
138+
return ExprSyntax(parentExpansion)
139+
}
140+
141+
let expansion = exprMacro.apply(parentExpansion, in: context)
142+
// FIXME: Produce diagnostics.
143+
return expansion.rewritten
139144
}
140145

141146
var evaluatedSyntaxStr = evaluatedSyntax.withoutTrivia().description

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern "C" void *swift_ASTGen_lookupMacro(const char *macroName);
4040
extern "C" void swift_ASTGen_destroyMacro(void *macro);
4141

4242
extern "C" ptrdiff_t swift_ASTGen_evaluateMacro(
43-
void *sourceFile, const void *sourceLocation,
43+
void *macro, void *sourceFile, const void *sourceLocation,
4444
const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength);
4545

4646
/// Produce the mangled name for the nominal type descriptor of a type
@@ -177,10 +177,10 @@ Expr *swift::expandMacroExpr(
177177
if (!astGenSourceFile)
178178
return nullptr;
179179

180-
// FIXME: Tell ASTGen which macro to use.
181180
const char *evaluatedSourceAddress;
182181
ptrdiff_t evaluatedSourceLength;
183182
swift_ASTGen_evaluateMacro(
183+
macroDef.getAsBuiltin(),
184184
astGenSourceFile, expr->getStartLoc().getOpaquePointerValue(),
185185
&evaluatedSourceAddress, &evaluatedSourceLength);
186186
if (!evaluatedSourceAddress)
@@ -206,8 +206,7 @@ Expr *swift::expandMacroExpr(
206206
/*range*/ Lexer::getCharSourceRangeFromSourceRange(
207207
sourceMgr, expr->getSourceRange()), ctx, pluginDiags);
208208
for (auto &diag : pluginDiags) {
209-
// FIXME: Switch to DeclName in the diagnostics.
210-
auto loc = sourceMgr.getLocForOffset(*bufferID, diag.position);
209+
\ auto loc = sourceMgr.getLocForOffset(*bufferID, diag.position);
211210
Diag<DeclName, StringRef> diagID;
212211
switch (diag.severity) {
213212
case CompilerPlugin::DiagnosticSeverity::Note:

0 commit comments

Comments
 (0)