Skip to content

Commit 4ae434e

Browse files
committed
[Macros] Add a mangling for macro expansions
These aren't ABI, but are useful to provide consistent names to refer to macro expansions, e.g., in buffer names.
1 parent 72ddbeb commit 4ae434e

File tree

16 files changed

+149
-42
lines changed

16 files changed

+149
-42
lines changed

docs/ABI/Mangling.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Entities
365365
entity-spec ::= decl-name type 'fp' // generic type parameter
366366
entity-spec ::= decl-name type 'fo' // enum element (currently not used)
367367
entity-spec ::= decl-name label-list? type generic-signature? 'fm' // macro
368+
entity-spec ::= context macro-discriminator-list // macro expansion
368369
entity-spec ::= identifier 'Qa' // associated type declaration
369370

370371
ACCESSOR ::= 'm' // materializeForSet
@@ -392,6 +393,10 @@ Entities
392393
RELATED-DISCRIMINATOR ::= [a-j]
393394
RELATED-DISCRIMINATOR ::= [A-J]
394395

396+
macro-discriminator-list ::= macro-discriminator-list? 'fM' macro-expansion-operator INDEX
397+
398+
macro-expansion-operator ::= 'f' // freestanding macro
399+
395400
file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration
396401

397402
The identifier in a ``<file-discriminator>`` and the second identifier in a

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,9 @@ class ASTContext final {
10721072
llvm::SetVector<AutoDiffConfig> &results);
10731073

10741074
/// Retrieve the next macro expansion discriminator within the given
1075-
/// context.
1076-
unsigned getNextMacroDiscriminator(MacroDiscriminatorContext context);
1075+
/// name and context.
1076+
unsigned getNextMacroDiscriminator(MacroDiscriminatorContext context,
1077+
DeclBaseName baseName);
10771078

10781079
/// Retrieve the Clang module loader for this ASTContext.
10791080
///

include/swift/AST/ASTMangler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace swift {
2626

2727
class AbstractClosureExpr;
2828
class ConformancePath;
29+
class MacroExpansionExpr;
2930
class RootProtocolConformance;
3031

3132
namespace Mangle {
@@ -365,6 +366,12 @@ class ASTMangler : public Mangler {
365366
mangleRuntimeAttributeGeneratorEntity(const ValueDecl *decl, CustomAttr *attr,
366367
SymbolKind SKind = SymbolKind::Default);
367368

369+
std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
370+
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
371+
void appendMacroExpansionContext(SourceLoc loc, DeclContext *origDC);
372+
void appendMacroExpansionOperator(
373+
StringRef macroName, unsigned discriminator);
374+
368375
enum SpecialContext {
369376
ObjCContext,
370377
ClangImporterContext,

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ NODE(LazyProtocolWitnessTableAccessor)
148148
NODE(LazyProtocolWitnessTableCacheVariable)
149149
NODE(LocalDeclName)
150150
NODE(Macro)
151+
NODE(MacroExpansion)
151152
CONTEXT_NODE(MaterializeForSet)
152153
NODE(MergedFunction)
153154
NODE(Metatype)

include/swift/Demangling/Demangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ class Demangler : public NodeFactory {
589589
NodePointer demangleGenericRequirement();
590590
NodePointer demangleGenericType();
591591
NodePointer demangleValueWitness();
592+
NodePointer demangleMacroExpansion();
592593

593594
NodePointer demangleTypeMangling();
594595
NodePointer demangleSymbolicReference(unsigned char rawKind);

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ struct ASTContext::Implementation {
402402
llvm::DenseMap<const AbstractFunctionDecl *, SourceRange> OriginalBodySourceRanges;
403403

404404
/// Macro discriminators per context.
405-
llvm::DenseMap<const void *, unsigned> NextMacroDiscriminator;
405+
llvm::DenseMap<std::pair<const void *, Identifier>, unsigned>
406+
NextMacroDiscriminator;
406407

407408
/// Structure that captures data that is segregated into different
408409
/// arenas.
@@ -2159,8 +2160,12 @@ void ASTContext::loadDerivativeFunctionConfigurations(
21592160
}
21602161

21612162
unsigned ASTContext::getNextMacroDiscriminator(
2162-
MacroDiscriminatorContext context) {
2163-
return getImpl().NextMacroDiscriminator[context.getOpaqueValue()]++;
2163+
MacroDiscriminatorContext context,
2164+
DeclBaseName baseName
2165+
) {
2166+
std::pair<const void *, Identifier> key(
2167+
context.getOpaqueValue(), baseName.getIdentifier());
2168+
return getImpl().NextMacroDiscriminator[key]++;
21642169
}
21652170

21662171
void ASTContext::verifyAllLoadedModules() const {

lib/AST/ASTMangler.cpp

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "swift/AST/ASTContext.h"
1919
#include "swift/AST/ASTVisitor.h"
2020
#include "swift/AST/AutoDiff.h"
21+
#include "swift/AST/Decl.h"
2122
#include "swift/AST/ExistentialLayout.h"
23+
#include "swift/AST/Expr.h"
2224
#include "swift/AST/FileUnit.h"
2325
#include "swift/AST/GenericSignature.h"
2426
#include "swift/AST/Initializer.h"
@@ -32,6 +34,7 @@
3234
#include "swift/AST/ProtocolConformanceRef.h"
3335
#include "swift/AST/SILLayout.h"
3436
#include "swift/Basic/Defer.h"
37+
#include "swift/Basic/SourceManager.h"
3538
#include "swift/ClangImporter/ClangImporter.h"
3639
#include "swift/Demangling/Demangler.h"
3740
#include "swift/Demangling/ManglingMacros.h"
@@ -46,6 +49,7 @@
4649
#include "clang/Basic/CharInfo.h"
4750
#include "llvm/ADT/DenseMap.h"
4851
#include "llvm/ADT/SmallString.h"
52+
#include "llvm/ADT/STLExtras.h"
4953
#include "llvm/ADT/StringRef.h"
5054
#include "llvm/Support/CommandLine.h"
5155
#include "llvm/Support/ErrorHandling.h"
@@ -3352,6 +3356,14 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {
33523356
return appendEntity(decl, "fp", decl->isStatic());
33533357
if (auto macro = dyn_cast<MacroDecl>(decl))
33543358
return appendEntity(decl, "fm", false);
3359+
if (auto expansion = dyn_cast<MacroExpansionDecl>(decl)) {
3360+
appendMacroExpansionContext(
3361+
expansion->getLoc(), expansion->getDeclContext());
3362+
appendMacroExpansionOperator(
3363+
expansion->getMacro().getBaseName().userFacingName(),
3364+
expansion->getDiscriminator());
3365+
return;
3366+
}
33553367

33563368
assert(isa<AbstractFunctionDecl>(decl) || isa<EnumElementDecl>(decl));
33573369

@@ -3692,36 +3704,80 @@ void ASTMangler::appendMacroExpansionContext(
36923704
if (!generatedSourceInfo)
36933705
return appendContext(origDC, StringRef());
36943706

3695-
auto parent = MacroDiscriminatorContext::getParentOf(loc, origDC);
3696-
if (auto dc = parent.dyn_cast<DeclContext *>())
3697-
return appendContext(dc, StringRef());
3698-
36993707
SourceLoc outerExpansionLoc;
3708+
DeclContext *outerExpansionDC;
3709+
DeclBaseName baseName;
37003710
unsigned discriminator;
3711+
switch (generatedSourceInfo->kind) {
3712+
case GeneratedSourceInfo::ExpressionMacroExpansion: {
3713+
auto parent = ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode);
3714+
if (auto expr =
3715+
cast_or_null<MacroExpansionExpr>(parent.dyn_cast<Expr *>())) {
3716+
outerExpansionLoc = expr->getLoc();
3717+
baseName = expr->getMacroName().getBaseName();
3718+
discriminator = expr->getDiscriminator();
3719+
} else {
3720+
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
3721+
outerExpansionLoc = decl->getLoc();
3722+
baseName = decl->getMacro().getBaseName();
3723+
discriminator = decl->getDiscriminator();
3724+
}
3725+
break;
3726+
}
37013727

3702-
if (auto expr = parent.dyn_cast<MacroExpansionExpr *>()) {
3703-
outerExpansionLoc = expr->getLoc();
3704-
discriminator = expr->getDiscriminator();
3705-
} else {
3706-
auto decl = parent.get<MacroExpansionDecl *>();
3707-
outerExpansionLoc = decl->getLoc();
3708-
discriminator = decl->getDiscriminator();
3728+
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
3729+
auto expansion =
3730+
cast<MacroExpansionDecl>(
3731+
ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode)
3732+
.get<Decl *>());
3733+
outerExpansionLoc = expansion->getLoc();
3734+
outerExpansionDC = expansion->getDeclContext();
3735+
discriminator = expansion->getDiscriminator();
3736+
break;
37093737
}
37103738

3739+
case GeneratedSourceInfo::AccessorMacroExpansion:
3740+
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
3741+
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
3742+
case GeneratedSourceInfo::PrettyPrinted:
3743+
case GeneratedSourceInfo::ReplacedFunctionBody:
3744+
return appendContext(origDC, StringRef());
3745+
}
3746+
3747+
// If we hit the point where the structure is represented as a DeclContext,
3748+
// we're done.
3749+
if (origDC->isChildContextOf(outerExpansionDC))
3750+
return appendContext(origDC, StringRef());
3751+
37113752
// Append our own context and discriminator.
37123753
appendMacroExpansionContext(outerExpansionLoc, origDC);
3713-
appendMacroExpansionOperator(discriminator);
3754+
appendMacroExpansionOperator(baseName.userFacingName(), discriminator);
37143755
}
37153756

3716-
void ASTMangler::appendMacroExpansionOperator(unsigned discriminator) {
3717-
appendOperator("fM", Index(discriminator));
3757+
void ASTMangler::appendMacroExpansionOperator(
3758+
StringRef macroName, unsigned discriminator
3759+
) {
3760+
appendIdentifier(macroName);
3761+
appendOperator("fMf", Index(discriminator));
37183762
}
37193763

37203764
std::string ASTMangler::mangleMacroExpansion(
37213765
const MacroExpansionExpr *expansion) {
37223766
beginMangling();
37233767
appendMacroExpansionContext(expansion->getLoc(), expansion->getDeclContext());
3724-
appendMacroExpansionOperator(expansion->getDiscriminator());
3768+
appendMacroExpansionOperator(
3769+
expansion->getMacroName().getBaseName().userFacingName(),
3770+
expansion->getDiscriminator());
3771+
return finalize();
3772+
}
3773+
3774+
std::string ASTMangler::mangleMacroExpansion(
3775+
const MacroExpansionDecl *expansion) {
3776+
beginMangling();
3777+
appendMacroExpansionContext(expansion->getLoc(), expansion->getDeclContext());
3778+
appendMacroExpansionOperator(
3779+
expansion->getMacro().getBaseName().userFacingName(),
3780+
expansion->getDiscriminator());
37253781
return finalize();
37263782
}
37273783

lib/AST/Decl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9871,7 +9871,8 @@ unsigned MacroExpansionDecl::getDiscriminator() const {
98719871
auto discriminatorContext =
98729872
MacroDiscriminatorContext::getParentOf(mutableThis);
98739873
mutableThis->setDiscriminator(
9874-
ctx.getNextMacroDiscriminator(discriminatorContext));
9874+
ctx.getNextMacroDiscriminator(
9875+
discriminatorContext, getMacro().getBaseName()));
98759876

98769877
assert(getRawDiscriminator() != InvalidDiscriminator);
98779878
return getRawDiscriminator();
@@ -9931,6 +9932,7 @@ MacroDiscriminatorContext MacroDiscriminatorContext::getParentOf(
99319932

99329933
case GeneratedSourceInfo::AccessorMacroExpansion:
99339934
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
9935+
case GeneratedSourceInfo::SynthesizedMemberMacroExpansion:
99349936
case GeneratedSourceInfo::PrettyPrinted:
99359937
case GeneratedSourceInfo::ReplacedFunctionBody:
99369938
return origDC;

lib/AST/Expr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2570,7 +2570,8 @@ unsigned MacroExpansionExpr::getDiscriminator() const {
25702570
auto discriminatorContext =
25712571
MacroDiscriminatorContext::getParentOf(mutableThis);
25722572
mutableThis->setDiscriminator(
2573-
ctx.getNextMacroDiscriminator(discriminatorContext));
2573+
ctx.getNextMacroDiscriminator(
2574+
discriminatorContext, getMacroName().getBaseName()));
25742575

25752576
assert(getRawDiscriminator() != InvalidDiscriminator);
25762577
return getRawDiscriminator();

lib/Demangling/Demangler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,7 @@ NodePointer Demangler::demangleFunctionEntity() {
35903590
Kind = Node::Kind::RuntimeAttributeGenerator;
35913591
break;
35923592
case 'm': return demangleEntity(Node::Kind::Macro);
3593+
case 'M': return demangleMacroExpansion();
35933594
case 'p': return demangleEntity(Node::Kind::GenericTypeParamDecl);
35943595
case 'P':
35953596
Args = None;
@@ -3889,3 +3890,20 @@ NodePointer Demangler::demangleValueWitness() {
38893890
addChild(VW, createNode(Node::Kind::Index, unsigned(Kind)));
38903891
return addChild(VW, popNode(Node::Kind::Type));
38913892
}
3893+
3894+
NodePointer Demangler::demangleMacroExpansion() {
3895+
Node::Kind kind;
3896+
switch (nextChar()) {
3897+
case 'f':
3898+
kind = Node::Kind::MacroExpansion;
3899+
break;
3900+
3901+
default:
3902+
return nullptr;
3903+
}
3904+
3905+
NodePointer name = popNode(Node::Kind::Identifier);
3906+
NodePointer context = popContext();
3907+
NodePointer discriminator = demangleIndexAsNode();
3908+
return createWithChildren(kind, context, name, discriminator);
3909+
}

0 commit comments

Comments
 (0)