Skip to content

Commit 0af3523

Browse files
authored
Merge pull request #63322 from DougGregor/macro-expansion-unique-name-mangling
2 parents 4409c2f + 6905bc0 commit 0af3523

File tree

14 files changed

+121
-27
lines changed

14 files changed

+121
-27
lines changed

docs/ABI/Mangling.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ Entities
395395

396396
macro-discriminator-list ::= macro-discriminator-list? 'fM' macro-expansion-operator INDEX
397397

398-
macro-expansion-operator ::= 'f' // freestanding macro
399-
macro-expansion-operator ::= 'u' // uniquely-named entity
398+
macro-expansion-operator ::= identifier 'f' // freestanding macro
399+
macro-expansion-operator ::= identifier 'u' // uniquely-named entity
400400

401401
file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration
402402

include/swift/Demangling/DemangleNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ NODE(ExistentialMetatype)
9191
CONTEXT_NODE(ExplicitClosure)
9292
CONTEXT_NODE(Extension)
9393
NODE(FieldOffset)
94+
NODE(FreestandingMacroExpansion)
9495
NODE(FullTypeMetadata)
9596
CONTEXT_NODE(Function)
9697
NODE(FunctionSignatureSpecialization)
@@ -148,7 +149,7 @@ NODE(LazyProtocolWitnessTableAccessor)
148149
NODE(LazyProtocolWitnessTableCacheVariable)
149150
NODE(LocalDeclName)
150151
NODE(Macro)
151-
NODE(MacroExpansion)
152+
NODE(MacroExpansionUniqueName)
152153
CONTEXT_NODE(MaterializeForSet)
153154
NODE(MergedFunction)
154155
NODE(Metatype)

include/swift/Parse/Parser.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ class Parser {
196196

197197
bool allowTopLevelCode() const;
198198

199+
bool isInMacroExpansion(SourceLoc loc) const;
200+
199201
const std::vector<Token> &getSplitTokens() const { return SplitTokens; }
200202

201203
void markSplitToken(tok Kind, StringRef Txt);
@@ -576,7 +578,8 @@ class Parser {
576578
return;
577579

578580
if (tok.getText().size() == 1 || Context.LangOpts.EnableDollarIdentifiers ||
579-
isInSILMode() || L->isSwiftInterface())
581+
isInSILMode() || L->isSwiftInterface() ||
582+
isInMacroExpansion(tok.getLoc()))
580583
return;
581584

582585
diagnose(tok.getLoc(), diag::dollar_identifier_decl,

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ fileprivate struct ThrownErrorDiagnostic: DiagnosticMessage {
106106
}
107107
}
108108

109-
@_cdecl("swift_ASTGen_evaluateMacro")
109+
@_cdecl("swift_ASTGen_expandFreestandingMacro")
110110
@usableFromInline
111-
func evaluateMacro(
111+
func expandFreestandingMacro(
112112
diagEnginePtr: UnsafeMutablePointer<UInt8>,
113113
macroPtr: UnsafeRawPointer,
114+
discriminatorText: UnsafePointer<UInt8>,
115+
discriminatorTextLength: Int,
114116
sourceFilePtr: UnsafeRawPointer,
115117
sourceLocationPtr: UnsafePointer<UInt8>?,
116118
expandedSourcePointer: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
@@ -144,7 +146,13 @@ func evaluateMacro(
144146
let sourceManager = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
145147
sourceManager.insert(sourceFilePtr)
146148

147-
let context = sourceManager.createMacroExpansionContext()
149+
let discriminatorBuffer = UnsafeBufferPointer(
150+
start: discriminatorText, count: discriminatorTextLength
151+
)
152+
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)
153+
let context = sourceManager.createMacroExpansionContext(
154+
discriminator: discriminator
155+
)
148156

149157
guard let parentSyntax = token.parent else {
150158
print("not on a macro expansion node: \(token.recursiveDescription)")

lib/ASTGen/Sources/ASTGen/SourceManager.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension SourceManager {
145145
}
146146

147147
/// Create a new macro expansion context
148-
func createMacroExpansionContext() -> BasicMacroExpansionContext {
148+
func createMacroExpansionContext(discriminator: String = "") -> BasicMacroExpansionContext {
149149
// Collect the set of source files for this context.
150150
var sourceFiles: [SourceFileSyntax : BasicMacroExpansionContext.KnownSourceFile] = [:]
151151
for (syntax, exported) in exportedSourceFilesBySyntax {
@@ -155,6 +155,8 @@ extension SourceManager {
155155
)
156156
}
157157

158-
return BasicMacroExpansionContext(sourceFiles: sourceFiles)
158+
return BasicMacroExpansionContext(
159+
expansionDiscriminator: discriminator, sourceFiles: sourceFiles
160+
)
159161
}
160162
}

lib/Demangling/Demangler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3899,15 +3899,21 @@ NodePointer Demangler::demangleMacroExpansion() {
38993899
Node::Kind kind;
39003900
switch (nextChar()) {
39013901
case 'f':
3902-
kind = Node::Kind::MacroExpansion;
3902+
kind = Node::Kind::FreestandingMacroExpansion;
3903+
break;
3904+
3905+
case 'u':
3906+
kind = Node::Kind::MacroExpansionUniqueName;
39033907
break;
39043908

39053909
default:
39063910
return nullptr;
39073911
}
39083912

39093913
NodePointer name = popNode(Node::Kind::Identifier);
3910-
NodePointer context = popContext();
3914+
NodePointer context = popNode(Node::Kind::FreestandingMacroExpansion);
3915+
if (!context)
3916+
context = popContext();
39113917
NodePointer discriminator = demangleIndexAsNode();
39123918
return createWithChildren(kind, context, name, discriminator);
39133919
}

lib/Demangling/NodePrinter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ class NodePrinter {
382382
case Node::Kind::Extension:
383383
case Node::Kind::EnumCase:
384384
case Node::Kind::FieldOffset:
385+
case Node::Kind::FreestandingMacroExpansion:
385386
case Node::Kind::FullObjCResilientClassStub:
386387
case Node::Kind::FullTypeMetadata:
387388
case Node::Kind::Function:
@@ -439,7 +440,7 @@ class NodePrinter {
439440
case Node::Kind::LazyProtocolWitnessTableCacheVariable:
440441
case Node::Kind::LocalDeclName:
441442
case Node::Kind::Macro:
442-
case Node::Kind::MacroExpansion:
443+
case Node::Kind::MacroExpansionUniqueName:
443444
case Node::Kind::MaterializeForSet:
444445
case Node::Kind::MergedFunction:
445446
case Node::Kind::Metaclass:
@@ -1337,9 +1338,13 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
13371338
Node->getNumChildren() == 3? TypePrinting::WithColon
13381339
: TypePrinting::FunctionStyle,
13391340
/*hasName*/ true);
1340-
case Node::Kind::MacroExpansion:
1341+
case Node::Kind::FreestandingMacroExpansion:
13411342
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
1342-
/*hasName*/true, "macro expansion #",
1343+
/*hasName*/true, "freestanding macro expansion #",
1344+
(int)Node->getChild(2)->getIndex() + 1);
1345+
case Node::Kind::MacroExpansionUniqueName:
1346+
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
1347+
/*hasName*/true, "unique name #",
13431348
(int)Node->getChild(2)->getIndex() + 1);
13441349
case Node::Kind::GenericTypeParamDecl:
13451350
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,

lib/Demangling/OldRemangler.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,20 @@ ManglingError Remangler::mangleMacro(Node *node, unsigned depth) {
10631063
return mangleChildNodes(node, depth + 1);
10641064
}
10651065

1066-
ManglingError Remangler::mangleMacroExpansion(Node *node, unsigned depth) {
1066+
ManglingError Remangler::mangleFreestandingMacroExpansion(
1067+
Node *node, unsigned depth) {
10671068
Buffer << "fMf";
10681069
RETURN_IF_ERROR(mangleIndex(node, depth + 1));
10691070
return mangleChildNodes(node, depth + 1);
10701071
}
10711072

1073+
ManglingError Remangler::mangleMacroExpansionUniqueName(
1074+
Node *node, unsigned depth) {
1075+
Buffer << "fMu";
1076+
RETURN_IF_ERROR(mangleIndex(node, depth + 1));
1077+
return mangleChildNodes(node, depth + 1);
1078+
}
1079+
10721080
ManglingError Remangler::mangleAccessor(Node *storageNode,
10731081
StringRef accessorCode,
10741082
EntityContext &ctx, unsigned depth) {

lib/Demangling/Remangler.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2872,13 +2872,22 @@ ManglingError Remangler::mangleMacro(Node *node, unsigned depth) {
28722872
return ManglingError::Success;
28732873
}
28742874

2875-
ManglingError Remangler::mangleMacroExpansion(Node *node, unsigned depth) {
2875+
ManglingError Remangler::mangleFreestandingMacroExpansion(
2876+
Node *node, unsigned depth) {
28762877
RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1));
28772878
RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1));
28782879
Buffer << "fMf";
28792880
return mangleChildNode(node, 2, depth + 1);
28802881
}
28812882

2883+
ManglingError Remangler::mangleMacroExpansionUniqueName(
2884+
Node *node, unsigned depth) {
2885+
RETURN_IF_ERROR(mangleChildNode(node, 0, depth + 1));
2886+
RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1));
2887+
Buffer << "fMu";
2888+
return mangleChildNode(node, 2, depth + 1);
2889+
}
2890+
28822891
ManglingError Remangler::mangleSuffix(Node *node, unsigned depth) {
28832892
// Just add the suffix back on.
28842893
Buffer << node->getText();

lib/Parse/Parser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,18 @@ bool Parser::allowTopLevelCode() const {
520520
return SF.isScriptMode();
521521
}
522522

523+
bool Parser::isInMacroExpansion(SourceLoc loc) const {
524+
if (loc.isInvalid())
525+
return false;
526+
527+
auto bufferID = SourceMgr.findBufferContainingLoc(loc);
528+
auto generatedSourceInfo = SourceMgr.getGeneratedSourceInfo(bufferID);
529+
if (!generatedSourceInfo)
530+
return false;
531+
532+
return true;
533+
}
534+
523535
const Token &Parser::peekToken() {
524536
return L->peekNextToken();
525537
}

0 commit comments

Comments
 (0)