@@ -9986,26 +9986,28 @@ MacroRoles swift::getAttachedMacroRoles() {
99869986 return attachedMacroRoles;
99879987}
99889988
9989- void
9990- MissingDecl::forEachExpandedPeer (ExpandedPeerCallback callback) {
9991- auto *macro = unexpandedPeer.macroAttr ;
9992- auto *attachedTo = unexpandedPeer.attachedTo ;
9993- if (!macro || !attachedTo)
9989+ void MissingDecl::forEachMacroExpandedDecl (MacroExpandedDeclCallback callback) {
9990+ auto macroRef = unexpandedMacro.macroRef ;
9991+ auto *baseDecl = unexpandedMacro.baseDecl ;
9992+ if (!macroRef || !baseDecl)
99949993 return ;
99959994
9996- attachedTo->visitAuxiliaryDecls (
9997- [&](Decl *auxiliaryDecl) {
9998- auto *sf = auxiliaryDecl->getInnermostDeclContext ()->getParentSourceFile ();
9999- auto *macroAttr = sf->getAttachedMacroAttribute ();
10000- if (macroAttr != unexpandedPeer.macroAttr )
10001- return ;
10002-
10003- auto *value = dyn_cast<ValueDecl>(auxiliaryDecl);
10004- if (!value)
10005- return ;
10006-
10007- callback (value);
10008- });
9995+ baseDecl->visitAuxiliaryDecls ([&](Decl *auxiliaryDecl) {
9996+ auto *sf = auxiliaryDecl->getInnermostDeclContext ()->getParentSourceFile ();
9997+ // We only visit auxiliary decls that are macro expansions associated with
9998+ // this macro reference.
9999+ if (auto *med = macroRef.dyn_cast <MacroExpansionDecl *>()) {
10000+ if (med != sf->getMacroExpansion ().dyn_cast <Decl *>())
10001+ return ;
10002+ } else if (auto *attr = macroRef.dyn_cast <CustomAttr *>()) {
10003+ if (attr != sf->getAttachedMacroAttribute ())
10004+ return ;
10005+ } else {
10006+ return ;
10007+ }
10008+ if (auto *vd = dyn_cast<ValueDecl>(auxiliaryDecl))
10009+ callback (vd);
10010+ });
1000910011}
1001010012
1001110013MacroDecl::MacroDecl (
@@ -10142,10 +10144,24 @@ Optional<BuiltinMacroKind> MacroDecl::getBuiltinKind() const {
1014210144 return def.getBuiltinKind ();
1014310145}
1014410146
10147+ MacroExpansionDecl::MacroExpansionDecl (
10148+ DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
10149+ DeclNameLoc macroLoc, SourceLoc leftAngleLoc,
10150+ ArrayRef<TypeRepr *> genericArgs, SourceLoc rightAngleLoc,
10151+ ArgumentList *args)
10152+ : Decl(DeclKind::MacroExpansion, dc), PoundLoc(poundLoc),
10153+ MacroName(macro), MacroNameLoc(macroLoc),
10154+ LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
10155+ GenericArgs(genericArgs),
10156+ ArgList(args ? args
10157+ : ArgumentList::createImplicit(dc->getASTContext (), {})) {
10158+ Bits.MacroExpansionDecl .Discriminator = InvalidDiscriminator;
10159+ }
10160+
1014510161SourceRange MacroExpansionDecl::getSourceRange () const {
1014610162 SourceLoc endLoc;
10147- if (ArgList)
10148- endLoc = ArgList-> getEndLoc () ;
10163+ if (auto argsEndList = ArgList-> getEndLoc () )
10164+ endLoc = argsEndList ;
1014910165 else if (RightAngleLoc.isValid ())
1015010166 endLoc = RightAngleLoc;
1015110167 else
@@ -10171,6 +10187,23 @@ unsigned MacroExpansionDecl::getDiscriminator() const {
1017110187 return getRawDiscriminator ();
1017210188}
1017310189
10190+ void MacroExpansionDecl::forEachExpandedExprOrStmt (
10191+ ExprOrStmtExpansionCallback callback) const {
10192+ auto mutableThis = const_cast <MacroExpansionDecl *>(this );
10193+ auto bufferID = evaluateOrDefault (
10194+ getASTContext ().evaluator ,
10195+ ExpandMacroExpansionDeclRequest{mutableThis}, {});
10196+ auto &sourceMgr = getASTContext ().SourceMgr ;
10197+ auto *moduleDecl = getModuleContext ();
10198+ if (!bufferID)
10199+ return ;
10200+ auto startLoc = sourceMgr.getLocForBufferStart (*bufferID);
10201+ auto *sourceFile = moduleDecl->getSourceFileContainingLocation (startLoc);
10202+ for (auto node : sourceFile->getTopLevelItems ())
10203+ if (node.is <Expr *>() || node.is <Stmt *>())
10204+ callback (node);
10205+ }
10206+
1017410207NominalTypeDecl *
1017510208ValueDecl::getRuntimeDiscoverableAttrTypeDecl (CustomAttr *attr) const {
1017610209 auto &ctx = getASTContext ();
0 commit comments