Skip to content

Commit d9f5aa0

Browse files
committed
[Mangling] Add a mangling for macro declarations.
Although macro declarations never produce any symbols in the compiled binary, they can be referenced via USR, so introduce a mangling for them.
1 parent af6b30a commit d9f5aa0

File tree

8 files changed

+24
-0
lines changed

8 files changed

+24
-0
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ Entities
362362
entity-spec ::= decl-name label-list? type 'v' ACCESSOR // variable
363363
entity-spec ::= decl-name type 'fp' // generic type parameter
364364
entity-spec ::= decl-name type 'fo' // enum element (currently not used)
365+
entity-spec ::= decl-name label-list? type generic-signature? 'fm' // macro
365366
entity-spec ::= identifier 'Qa' // associated type declaration
366367

367368
ACCESSOR ::= 'm' // materializeForSet

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ NODE(KeyPathHashThunkHelper)
147147
NODE(LazyProtocolWitnessTableAccessor)
148148
NODE(LazyProtocolWitnessTableCacheVariable)
149149
NODE(LocalDeclName)
150+
NODE(Macro)
150151
CONTEXT_NODE(MaterializeForSet)
151152
NODE(MergedFunction)
152153
NODE(Metatype)

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,6 +3339,8 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {
33393339
return appendAccessorEntity("p", storageDecl, decl->isStatic());
33403340
if (isa<GenericTypeParamDecl>(decl))
33413341
return appendEntity(decl, "fp", decl->isStatic());
3342+
if (auto macro = dyn_cast<MacroDecl>(decl))
3343+
return appendEntity(decl, "fm", false);
33423344

33433345
assert(isa<AbstractFunctionDecl>(decl) || isa<EnumElementDecl>(decl));
33443346

lib/Demangling/Demangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3545,6 +3545,7 @@ NodePointer Demangler::demangleFunctionEntity() {
35453545
case 'U': Args = TypeAndIndex; Kind = Node::Kind::ExplicitClosure; break;
35463546
case 'u': Args = TypeAndIndex; Kind = Node::Kind::ImplicitClosure; break;
35473547
case 'A': Args = Index; Kind = Node::Kind::DefaultArgumentInitializer; break;
3548+
case 'm': return demangleEntity(Node::Kind::Macro);
35483549
case 'p': return demangleEntity(Node::Kind::GenericTypeParamDecl);
35493550
case 'P':
35503551
Args = None;

lib/Demangling/NodePrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ class NodePrinter {
438438
case Node::Kind::LazyProtocolWitnessTableAccessor:
439439
case Node::Kind::LazyProtocolWitnessTableCacheVariable:
440440
case Node::Kind::LocalDeclName:
441+
case Node::Kind::Macro:
441442
case Node::Kind::MaterializeForSet:
442443
case Node::Kind::MergedFunction:
443444
case Node::Kind::Metaclass:
@@ -1327,6 +1328,11 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
13271328
return printEntity(
13281329
Node, depth, asPrefixContext, TypePrinting::FunctionStyle,
13291330
/*hasName*/ false, /*ExtraName*/ "", /*ExtraIndex*/ -1, "subscript");
1331+
case Node::Kind::Macro:
1332+
return printEntity(Node, depth, asPrefixContext,
1333+
Node->getNumChildren() == 3? TypePrinting::WithColon
1334+
: TypePrinting::FunctionStyle,
1335+
/*hasName*/ true);
13301336
case Node::Kind::GenericTypeParamDecl:
13311337
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
13321338
/*hasName*/ true);

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,11 @@ ManglingError Remangler::mangleSubscript(Node *node, EntityContext &ctx,
10581058
return ManglingError::Success;
10591059
}
10601060

1061+
ManglingError Remangler::mangleMacro(Node *node, unsigned depth) {
1062+
Buffer << "fm";
1063+
return mangleChildNodes(node, depth + 1);
1064+
}
1065+
10611066
ManglingError Remangler::mangleAccessor(Node *storageNode,
10621067
StringRef accessorCode,
10631068
EntityContext &ctx, unsigned depth) {

lib/Demangling/Remangler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,12 @@ ManglingError Remangler::mangleSubscript(Node *node, unsigned depth) {
28592859
return mangleAbstractStorage(node, "p", depth + 1);
28602860
}
28612861

2862+
ManglingError Remangler::mangleMacro(Node *node, unsigned depth) {
2863+
RETURN_IF_ERROR(mangleChildNodes(node, depth + 1));
2864+
Buffer << "fm";
2865+
return ManglingError::Success;
2866+
}
2867+
28622868
ManglingError Remangler::mangleSuffix(Node *node, unsigned depth) {
28632869
// Just add the suffix back on.
28642870
Buffer << node->getText();

test/Demangle/Inputs/manglings.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,5 @@ $sxIeghHr_xs5Error_pIegHrzo_s8SendableRzs5NeverORs_r0_lTRTY_ ---> {T:} (0) suspe
441441
$sxIeghHr_xs5Error_pIegHrzo_s8SendableRzs5NeverORs_r0_lTRTQ12_ ---> {T:} (13) await resume partial function for reabstraction thunk helper <A, B where A: Swift.Sendable, B == Swift.Never> from @escaping @callee_guaranteed @Sendable @async () -> (@out A) to @escaping @callee_guaranteed @async () -> (@out A, @error @owned Swift.Error)
442442
$s7Library3fooyyFTwS ---> #_hasSymbol query for Library.foo() -> ()
443443
$s7Library5KlassCTwS ---> #_hasSymbol query for Library.Klass
444+
$s14swift_ide_test14myColorLiteral3red5green4blue5alphaAA0E0VSf_S3ftcfm ---> swift_ide_test.myColorLiteral(red: Swift.Float, green: Swift.Float, blue: Swift.Float, alpha: Swift.Float) -> swift_ide_test.Color
445+
$s14swift_ide_test10myFilenamexfm ---> swift_ide_test.myFilename : A

0 commit comments

Comments
 (0)