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
37783816void 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
37853841std::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+
38053893static void gatherExistentialRequirements (SmallVectorImpl<Requirement> &reqs,
38063894 ParameterizedProtocolType *PPT) {
38073895 auto protoTy = PPT->getBaseType ();
0 commit comments