Skip to content

Commit b63fa56

Browse files
committed
[Macros] Add mangling for attached macro expansion.
Extend the name mangling scheme for macro expansions to cover attached macros, and use that scheme for the names of macro expansions buffers. Finishes rdar://104038303, stabilizing file/buffer names for macro expansion buffers.
1 parent 168027b commit b63fa56

File tree

13 files changed

+238
-45
lines changed

13 files changed

+238
-45
lines changed

docs/ABI/Mangling.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,10 @@ Entities
396396

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

399+
macro-expansion-operator ::= identifier 'a' // accessor attached macro
400+
macro-expansion-operator ::= identifier 'A' // member-attribute attached macro
399401
macro-expansion-operator ::= identifier 'f' // freestanding macro
402+
macro-expansion-operator ::= identifier 'm' // member attached macro
400403
macro-expansion-operator ::= identifier 'u' // uniquely-named entity
401404

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

include/swift/AST/ASTMangler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ class ASTMangler : public Mangler {
343343

344344
std::string mangleTypeAsContextUSR(const NominalTypeDecl *type);
345345

346+
void appendAnyDecl(const ValueDecl *Decl);
346347
std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix,
347348
bool respectOriginallyDefinedIn = false);
348349
std::string mangleDeclAsUSR(const ValueDecl *Decl, StringRef USRPrefix);
@@ -368,9 +369,12 @@ class ASTMangler : public Mangler {
368369

369370
std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
370371
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
372+
std::string mangleAttachedMacroExpansion(
373+
const Decl *decl, CustomAttr *attr, MacroRole role);
374+
371375
void appendMacroExpansionContext(SourceLoc loc, DeclContext *origDC);
372376
void appendMacroExpansionOperator(
373-
StringRef macroName, unsigned discriminator);
377+
StringRef macroName, MacroRole role, unsigned discriminator);
374378

375379
enum SpecialContext {
376380
ObjCContext,

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
875875
/// declaration.
876876
void forEachAttachedMacro(MacroRole role, MacroCallback) const;
877877

878+
/// Retrieve the discriminator for the given custom attribute that names
879+
/// an attached macro.
880+
unsigned getAttachedMacroDiscriminator(
881+
MacroRole role, const CustomAttr *attr) const;
882+
878883
/// Returns the innermost enclosing decl with an availability annotation.
879884
const Decl *getInnermostDeclWithAvailability() const;
880885

include/swift/Demangling/DemangleNodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ NODE(AssociatedType)
3232
NODE(AssociatedTypeRef)
3333
NODE(AssociatedTypeMetadataAccessor)
3434
NODE(DefaultAssociatedTypeMetadataAccessor)
35+
NODE(AccessorAttachedMacroExpansion)
3536
NODE(AssociatedTypeWitnessTableAccessor)
3637
NODE(BaseWitnessTableAccessor)
3738
NODE(AutoClosureType)
@@ -151,6 +152,8 @@ NODE(LocalDeclName)
151152
NODE(Macro)
152153
NODE(MacroExpansionUniqueName)
153154
CONTEXT_NODE(MaterializeForSet)
155+
NODE(MemberAttachedMacroExpansion)
156+
NODE(MemberAttributeAttachedMacroExpansion)
154157
NODE(MergedFunction)
155158
NODE(Metatype)
156159
NODE(MetatypeRepresentation)

lib/AST/ASTMangler.cpp

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/AST/ProtocolConformance.h"
3434
#include "swift/AST/ProtocolConformanceRef.h"
3535
#include "swift/AST/SILLayout.h"
36+
#include "swift/AST/TypeCheckRequests.h"
3637
#include "swift/Basic/Defer.h"
3738
#include "swift/Basic/SourceManager.h"
3839
#include "swift/ClangImporter/ClangImporter.h"
@@ -785,19 +786,7 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
785786
return finalize();
786787
}
787788

788-
std::string
789-
ASTMangler::mangleAnyDecl(const ValueDecl *Decl,
790-
bool prefix,
791-
bool respectOriginallyDefinedIn) {
792-
DWARFMangling = true;
793-
RespectOriginallyDefinedIn = respectOriginallyDefinedIn;
794-
if (prefix) {
795-
beginMangling();
796-
} else {
797-
beginManglingWithoutPrefix();
798-
}
799-
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
800-
789+
void ASTMangler::appendAnyDecl(const ValueDecl *Decl) {
801790
if (auto Ctor = dyn_cast<ConstructorDecl>(Decl)) {
802791
appendConstructorEntity(Ctor, /*isAllocating=*/false);
803792
} else if (auto Dtor = dyn_cast<DestructorDecl>(Decl)) {
@@ -811,6 +800,22 @@ ASTMangler::mangleAnyDecl(const ValueDecl *Decl,
811800
} else {
812801
appendEntity(Decl);
813802
}
803+
}
804+
805+
std::string
806+
ASTMangler::mangleAnyDecl(const ValueDecl *Decl,
807+
bool prefix,
808+
bool respectOriginallyDefinedIn) {
809+
DWARFMangling = true;
810+
RespectOriginallyDefinedIn = respectOriginallyDefinedIn;
811+
if (prefix) {
812+
beginMangling();
813+
} else {
814+
beginManglingWithoutPrefix();
815+
}
816+
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
817+
818+
appendAnyDecl(Decl);
814819

815820
// We have a custom prefix, so finalize() won't verify for us. If we're not
816821
// in invalid code (coming from an IDE caller) verify manually.
@@ -3382,6 +3387,7 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {
33823387
expansion->getLoc(), expansion->getDeclContext());
33833388
appendMacroExpansionOperator(
33843389
expansion->getMacro().getBaseName().userFacingName(),
3390+
MacroRole::Declaration,
33853391
expansion->getDiscriminator());
33863392
return;
33873393
}
@@ -3729,6 +3735,7 @@ void ASTMangler::appendMacroExpansionContext(
37293735
DeclContext *outerExpansionDC;
37303736
DeclBaseName baseName;
37313737
unsigned discriminator;
3738+
MacroRole role;
37323739
switch (generatedSourceInfo->kind) {
37333740
case GeneratedSourceInfo::ExpressionMacroExpansion: {
37343741
auto parent = ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode);
@@ -3737,11 +3744,13 @@ void ASTMangler::appendMacroExpansionContext(
37373744
outerExpansionLoc = expr->getLoc();
37383745
baseName = expr->getMacroName().getBaseName();
37393746
discriminator = expr->getDiscriminator();
3747+
role = MacroRole::Expression;
37403748
} else {
37413749
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
37423750
outerExpansionLoc = decl->getLoc();
37433751
baseName = decl->getMacro().getBaseName();
37443752
discriminator = decl->getDiscriminator();
3753+
role = MacroRole::Declaration;
37453754
}
37463755
break;
37473756
}
@@ -3754,12 +3763,40 @@ void ASTMangler::appendMacroExpansionContext(
37543763
outerExpansionLoc = expansion->getLoc();
37553764
outerExpansionDC = expansion->getDeclContext();
37563765
discriminator = expansion->getDiscriminator();
3766+
role = MacroRole::Declaration;
37573767
break;
37583768
}
37593769

37603770
case GeneratedSourceInfo::AccessorMacroExpansion:
37613771
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
3762-
case GeneratedSourceInfo::MemberMacroExpansion:
3772+
case GeneratedSourceInfo::MemberMacroExpansion: {
3773+
auto decl = ASTNode::getFromOpaqueValue(generatedSourceInfo->astNode)
3774+
.get<Decl *>();
3775+
auto attr = generatedSourceInfo->attachedMacroCustomAttr;
3776+
3777+
switch (generatedSourceInfo->kind) {
3778+
case GeneratedSourceInfo::AccessorMacroExpansion:
3779+
role = MacroRole::Accessor;
3780+
break;
3781+
3782+
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
3783+
role = MacroRole::MemberAttribute;
3784+
break;
3785+
3786+
case GeneratedSourceInfo::MemberMacroExpansion:
3787+
role = MacroRole::Member;
3788+
break;
3789+
3790+
default:
3791+
llvm_unreachable("Unhandled macro role");
3792+
}
3793+
3794+
outerExpansionLoc = decl->getLoc();
3795+
outerExpansionDC = decl->getDeclContext();
3796+
discriminator = decl->getAttachedMacroDiscriminator(role, attr);
3797+
break;
3798+
}
3799+
37633800
case GeneratedSourceInfo::PrettyPrinted:
37643801
case GeneratedSourceInfo::ReplacedFunctionBody:
37653802
return appendContext(origDC, StringRef());
@@ -3772,14 +3809,33 @@ void ASTMangler::appendMacroExpansionContext(
37723809

37733810
// Append our own context and discriminator.
37743811
appendMacroExpansionContext(outerExpansionLoc, origDC);
3775-
appendMacroExpansionOperator(baseName.userFacingName(), discriminator);
3812+
appendMacroExpansionOperator(
3813+
baseName.userFacingName(), role, discriminator);
37763814
}
37773815

37783816
void ASTMangler::appendMacroExpansionOperator(
3779-
StringRef macroName, unsigned discriminator
3817+
StringRef macroName, MacroRole role, unsigned discriminator
37803818
) {
37813819
appendIdentifier(macroName);
3782-
appendOperator("fMf", Index(discriminator));
3820+
3821+
switch (role) {
3822+
case MacroRole::Expression:
3823+
case MacroRole::Declaration:
3824+
appendOperator("fMf", Index(discriminator));
3825+
break;
3826+
3827+
case MacroRole::Accessor:
3828+
appendOperator("fMa", Index(discriminator));
3829+
break;
3830+
3831+
case MacroRole::MemberAttribute:
3832+
appendOperator("fMA", Index(discriminator));
3833+
break;
3834+
3835+
case MacroRole::Member:
3836+
appendOperator("fMm", Index(discriminator));
3837+
break;
3838+
}
37833839
}
37843840

37853841
std::string ASTMangler::mangleMacroExpansion(
@@ -3788,6 +3844,7 @@ std::string ASTMangler::mangleMacroExpansion(
37883844
appendMacroExpansionContext(expansion->getLoc(), expansion->getDeclContext());
37893845
appendMacroExpansionOperator(
37903846
expansion->getMacroName().getBaseName().userFacingName(),
3847+
MacroRole::Expression,
37913848
expansion->getDiscriminator());
37923849
return finalize();
37933850
}
@@ -3798,10 +3855,41 @@ std::string ASTMangler::mangleMacroExpansion(
37983855
appendMacroExpansionContext(expansion->getLoc(), expansion->getDeclContext());
37993856
appendMacroExpansionOperator(
38003857
expansion->getMacro().getBaseName().userFacingName(),
3858+
MacroRole::Declaration,
38013859
expansion->getDiscriminator());
38023860
return finalize();
38033861
}
38043862

3863+
std::string ASTMangler::mangleAttachedMacroExpansion(
3864+
const Decl *decl, CustomAttr *attr, MacroRole role) {
3865+
beginMangling();
3866+
3867+
DeclContext *macroDeclContext = decl->getDeclContext();
3868+
if (role == MacroRole::MemberAttribute) {
3869+
appendContextOf(cast<ValueDecl>(decl));
3870+
macroDeclContext = decl->getDeclContext()->getParent();
3871+
} else if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
3872+
appendAnyDecl(valueDecl);
3873+
} else {
3874+
appendContext(decl->getDeclContext(), "");
3875+
}
3876+
3877+
StringRef macroName;
3878+
auto *macroDecl = evaluateOrDefault(
3879+
decl->getASTContext().evaluator,
3880+
ResolveMacroRequest{attr, role, macroDeclContext},
3881+
nullptr);
3882+
if (macroDecl)
3883+
macroName = macroDecl->getName().getBaseName().userFacingName();
3884+
else
3885+
macroName = "__unknown_macro__";
3886+
3887+
appendMacroExpansionOperator(
3888+
macroName, role,
3889+
decl->getAttachedMacroDiscriminator(role, attr));
3890+
return finalize();
3891+
}
3892+
38053893
static void gatherExistentialRequirements(SmallVectorImpl<Requirement> &reqs,
38063894
ParameterizedProtocolType *PPT) {
38073895
auto protoTy = PPT->getBaseType();

lib/AST/Decl.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "swift/Demangling/ManglingMacros.h"
5959
#include "swift/Parse/Lexer.h" // FIXME: Bad dependency
6060
#include "clang/Lex/MacroInfo.h"
61+
#include "llvm/ADT/DenseMap.h"
6162
#include "llvm/ADT/SmallPtrSet.h"
6263
#include "llvm/ADT/SmallSet.h"
6364
#include "llvm/ADT/SmallString.h"
@@ -397,6 +398,33 @@ void Decl::forEachAttachedMacro(MacroRole role,
397398
}
398399
}
399400

401+
unsigned Decl::getAttachedMacroDiscriminator(
402+
MacroRole role, const CustomAttr *attr
403+
) const {
404+
assert(isAttachedMacro(role) && "Not an attached macro role");
405+
406+
// Member-attribute macros are written on the enclosing declaration,
407+
// but apply to the member itself. Adjust the owning declaration accordingly.
408+
const Decl *owningDecl = this;
409+
if (role == MacroRole::MemberAttribute) {
410+
owningDecl = getDeclContext()->getAsDecl();
411+
}
412+
413+
llvm::SmallDenseMap<Identifier, unsigned> nextDiscriminator;
414+
Optional<unsigned> foundDiscriminator;
415+
416+
owningDecl->forEachAttachedMacro(
417+
role,
418+
[&](CustomAttr *foundAttr, MacroDecl *foundMacro) {
419+
unsigned discriminator =
420+
nextDiscriminator[foundMacro->getBaseIdentifier()]++;
421+
if (attr == foundAttr)
422+
foundDiscriminator = discriminator;
423+
});
424+
425+
return *foundDiscriminator;
426+
}
427+
400428
const Decl *Decl::getInnermostDeclWithAvailability() const {
401429
if (auto attrAndDecl = getSemanticAvailableRangeAttr())
402430
return attrAndDecl.value().second;

lib/Demangling/Demangler.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3897,13 +3897,32 @@ NodePointer Demangler::demangleValueWitness() {
38973897
return addChild(VW, popNode(Node::Kind::Type));
38983898
}
38993899

3900+
static bool isMacroExpansionNodeKind(Node::Kind kind) {
3901+
return kind == Node::Kind::AccessorAttachedMacroExpansion ||
3902+
kind == Node::Kind::MemberAttributeAttachedMacroExpansion ||
3903+
kind == Node::Kind::FreestandingMacroExpansion ||
3904+
kind == Node::Kind::MemberAttachedMacroExpansion;
3905+
}
3906+
39003907
NodePointer Demangler::demangleMacroExpansion() {
39013908
Node::Kind kind;
39023909
switch (nextChar()) {
3910+
case 'a':
3911+
kind = Node::Kind::AccessorAttachedMacroExpansion;
3912+
break;
3913+
3914+
case 'A':
3915+
kind = Node::Kind::MemberAttributeAttachedMacroExpansion;
3916+
break;
3917+
39033918
case 'f':
39043919
kind = Node::Kind::FreestandingMacroExpansion;
39053920
break;
39063921

3922+
case 'm':
3923+
kind = Node::Kind::MemberAttachedMacroExpansion;
3924+
break;
3925+
39073926
case 'u':
39083927
kind = Node::Kind::MacroExpansionUniqueName;
39093928
break;
@@ -3913,7 +3932,7 @@ NodePointer Demangler::demangleMacroExpansion() {
39133932
}
39143933

39153934
NodePointer name = popNode(Node::Kind::Identifier);
3916-
NodePointer context = popNode(Node::Kind::FreestandingMacroExpansion);
3935+
NodePointer context = popNode(isMacroExpansionNodeKind);
39173936
if (!context)
39183937
context = popContext();
39193938
NodePointer discriminator = demangleIndexAsNode();

lib/Demangling/NodePrinter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ class NodePrinter {
340340

341341
case Node::Kind::PackExpansion:
342342
case Node::Kind::ProtocolListWithClass:
343+
case Node::Kind::AccessorAttachedMacroExpansion:
343344
case Node::Kind::AccessorFunctionReference:
344345
case Node::Kind::Allocator:
345346
case Node::Kind::ArgumentTuple:
@@ -442,6 +443,8 @@ class NodePrinter {
442443
case Node::Kind::Macro:
443444
case Node::Kind::MacroExpansionUniqueName:
444445
case Node::Kind::MaterializeForSet:
446+
case Node::Kind::MemberAttributeAttachedMacroExpansion:
447+
case Node::Kind::MemberAttachedMacroExpansion:
445448
case Node::Kind::MergedFunction:
446449
case Node::Kind::Metaclass:
447450
case Node::Kind::MethodDescriptor:
@@ -1338,10 +1341,22 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
13381341
Node->getNumChildren() == 3? TypePrinting::WithColon
13391342
: TypePrinting::FunctionStyle,
13401343
/*hasName*/ true);
1344+
case Node::Kind::AccessorAttachedMacroExpansion:
1345+
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
1346+
/*hasName*/true, "accessor macro expansion #",
1347+
(int)Node->getChild(2)->getIndex() + 1);
13411348
case Node::Kind::FreestandingMacroExpansion:
13421349
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
13431350
/*hasName*/true, "freestanding macro expansion #",
13441351
(int)Node->getChild(2)->getIndex() + 1);
1352+
case Node::Kind::MemberAttributeAttachedMacroExpansion:
1353+
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
1354+
/*hasName*/true, "member attribute macro expansion #",
1355+
(int)Node->getChild(2)->getIndex() + 1);
1356+
case Node::Kind::MemberAttachedMacroExpansion:
1357+
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
1358+
/*hasName*/true, "member macro expansion #",
1359+
(int)Node->getChild(2)->getIndex() + 1);
13451360
case Node::Kind::MacroExpansionUniqueName:
13461361
return printEntity(Node, depth, asPrefixContext, TypePrinting::NoType,
13471362
/*hasName*/true, "unique name #",

0 commit comments

Comments
 (0)