Skip to content

Commit d6200d0

Browse files
committed
[Macros] Always visit macro-produced decls as auxiliary decls
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros.
1 parent b3b90a8 commit d6200d0

File tree

12 files changed

+69
-79
lines changed

12 files changed

+69
-79
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ enum class MacroWalking {
9595

9696
/// Walk into both the arguments of the macro as written in the source code
9797
/// and also the macro expansion.
98-
ArgumentsAndExpansion
98+
ArgumentsAndExpansion,
99+
100+
/// Don't walk into macros.
101+
None
99102
};
100103

101104
/// An abstract class used to traverse an AST.
@@ -545,6 +548,9 @@ class ASTWalker {
545548

546549
case MacroWalking::ArgumentsAndExpansion:
547550
return std::make_pair(true, true);
551+
552+
case MacroWalking::None:
553+
return std::make_pair(false, false);
548554
}
549555
}
550556

include/swift/AST/Decl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8497,7 +8497,6 @@ class MacroExpansionDecl : public Decl {
84978497
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
84988498
DeclNameRef getMacroName() const { return MacroName; }
84998499
ArgumentList *getArgs() const { return ArgList; }
8500-
ArrayRef<Decl *> getRewritten() const;
85018500
ConcreteDeclRef getMacroRef() const { return macroRef; }
85028501
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
85038502

include/swift/AST/TypeCheckRequests.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,16 +3835,16 @@ class MacroDefinitionRequest
38353835
/// Find the definition of a given macro.
38363836
class ExpandMacroExpansionDeclRequest
38373837
: public SimpleRequest<ExpandMacroExpansionDeclRequest,
3838-
ArrayRef<Decl *>(MacroExpansionDecl *),
3838+
Optional<unsigned>(MacroExpansionDecl *),
38393839
RequestFlags::Cached> {
38403840
public:
38413841
using SimpleRequest::SimpleRequest;
38423842

38433843
private:
38443844
friend SimpleRequest;
38453845

3846-
ArrayRef<Decl *> evaluate(Evaluator &evaluator,
3847-
MacroExpansionDecl *med) const;
3846+
Optional<unsigned>
3847+
evaluate(Evaluator &evaluator, MacroExpansionDecl *med) const;
38483848

38493849
public:
38503850
bool isCached() const { return true; }

include/swift/AST/TypeMemberVisitor.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
5454
return RetTy();
5555
}
5656

57+
RetTy visitMacroExpansionDecl(MacroExpansionDecl *D) {
58+
// Expansion already visited as auxiliary decls.
59+
return RetTy();
60+
}
61+
5762
/// A convenience method to visit all the members.
5863
void visitMembers(NominalTypeDecl *D) {
5964
for (Decl *member : D->getMembers()) {
65+
member->visitAuxiliaryDecls([&](Decl *decl) {
66+
asImpl().visit(decl);
67+
});
6068
asImpl().visit(member);
6169
}
6270
}
@@ -78,12 +86,6 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
7886
asImpl().visit(dd);
7987
}
8088
}
81-
82-
/// Visit expanded macros.
83-
void visitMacroExpansionDecl(MacroExpansionDecl *D) {
84-
for (auto *decl : D->getRewritten())
85-
asImpl().visit(decl);
86-
}
8789
};
8890

8991
template<typename ImplClass, typename RetTy = void>

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class Verifier : public ASTWalker {
272272
}
273273

274274
MacroWalking getMacroWalkingBehavior() const override {
275-
return MacroWalking::Expansion;
275+
return MacroWalking::None;
276276
}
277277

278278
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {

lib/AST/ASTWalker.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,16 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
433433
bool shouldWalkArguments, shouldWalkExpansion;
434434
std::tie(shouldWalkArguments, shouldWalkExpansion) =
435435
Walker.shouldWalkMacroArgumentsAndExpansion();
436-
if (shouldWalkArguments && MED->getArgs() && doIt(MED->getArgs()))
436+
if (shouldWalkArguments && MED->getArgs() && !doIt(MED->getArgs()))
437437
return true;
438-
438+
bool alreadyFailed = false;
439439
if (shouldWalkExpansion) {
440-
for (auto *decl : MED->getRewritten())
441-
if (doIt(decl))
442-
return true;
440+
MED->visitAuxiliaryDecls([&](Decl *decl) {
441+
if (alreadyFailed) return;
442+
alreadyFailed = inherited::visit(decl);
443+
});
443444
}
444-
return false;
445+
return alreadyFailed;
445446
}
446447

447448
bool visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {

lib/AST/Decl.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
413413
}
414414
}
415415

416+
else if (auto *med = dyn_cast<MacroExpansionDecl>(mutableThis)) {
417+
if (auto bufferID = evaluateOrDefault(
418+
ctx.evaluator, ExpandMacroExpansionDeclRequest{med}, {})) {
419+
auto startLoc = sourceMgr.getLocForBufferStart(*bufferID);
420+
auto *sourceFile = moduleDecl->getSourceFileContainingLocation(startLoc);
421+
for (auto *decl : sourceFile->getTopLevelDecls())
422+
callback(decl);
423+
}
424+
}
425+
416426
// FIXME: fold VarDecl::visitAuxiliaryDecls into this.
417427
}
418428

@@ -10073,13 +10083,6 @@ unsigned MacroExpansionDecl::getDiscriminator() const {
1007310083
return getRawDiscriminator();
1007410084
}
1007510085

10076-
ArrayRef<Decl *> MacroExpansionDecl::getRewritten() const {
10077-
auto mutableThis = const_cast<MacroExpansionDecl *>(this);
10078-
return evaluateOrDefault(
10079-
getASTContext().evaluator,
10080-
ExpandMacroExpansionDeclRequest{mutableThis}, {});
10081-
}
10082-
1008310086
NominalTypeDecl *
1008410087
ValueDecl::getRuntimeDiscoverableAttrTypeDecl(CustomAttr *attr) const {
1008510088
auto &ctx = getASTContext();

lib/AST/NameLookup.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,14 +1510,6 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15101510
MemberLookupTable &table,
15111511
DeclName name,
15121512
NominalTypeDecl *dc) {
1513-
auto expandAndPopulate = [&](MacroExpansionDecl *med) {
1514-
auto expanded = evaluateOrDefault(med->getASTContext().evaluator,
1515-
ExpandMacroExpansionDeclRequest{med},
1516-
nullptr);
1517-
for (auto *decl : expanded)
1518-
table.addMember(decl);
1519-
};
1520-
15211513
for (auto *member : dc->getCurrentMembersWithoutLoading()) {
15221514
auto *med = dyn_cast<MacroExpansionDecl>(member);
15231515
if (!med)
@@ -1532,7 +1524,9 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15321524
// If a macro produces arbitrary names, we have to expand it to factor its
15331525
// expansion results into name lookup.
15341526
if (attr->hasNameKind(MacroIntroducedDeclNameKind::Arbitrary)) {
1535-
expandAndPopulate(med);
1527+
med->visitAuxiliaryDecls([&](Decl *decl) {
1528+
table.addMember(decl);
1529+
});
15361530
}
15371531
// Otherwise, we expand the macro if it has the same decl base name being
15381532
// looked for.
@@ -1550,7 +1544,9 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15501544
name.getBaseName().userFacingName();
15511545
});
15521546
if (it != attr->getNames().end())
1553-
expandAndPopulate(med);
1547+
med->visitAuxiliaryDecls([&](Decl *decl) {
1548+
table.addMember(decl);
1549+
});
15541550
}
15551551
}
15561552
}

lib/IRGen/GenDecl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,9 @@ void swift::irgen::disableAddressSanitizer(IRGenModule &IGM, llvm::GlobalVariabl
24572457

24582458
/// Emit a global declaration.
24592459
void IRGenModule::emitGlobalDecl(Decl *D) {
2460+
D->visitAuxiliaryDecls([&](Decl *decl) {
2461+
emitGlobalDecl(decl);
2462+
});
24602463
switch (D->getKind()) {
24612464
case DeclKind::Extension:
24622465
return emitExtension(cast<ExtensionDecl>(D));
@@ -2542,8 +2545,7 @@ void IRGenModule::emitGlobalDecl(Decl *D) {
25422545
return;
25432546

25442547
case DeclKind::MacroExpansion:
2545-
for (auto *rewritten : cast<MacroExpansionDecl>(D)->getRewritten())
2546-
emitGlobalDecl(rewritten);
2548+
// Expansion already visited as auxiliary decls.
25472549
return;
25482550
}
25492551

@@ -5466,6 +5468,9 @@ Address IRGenModule::getAddrOfEnumCase(EnumElementDecl *Case,
54665468

54675469
void IRGenModule::emitNestedTypeDecls(DeclRange members) {
54685470
for (Decl *member : members) {
5471+
member->visitAuxiliaryDecls([&](Decl *decl) {
5472+
emitNestedTypeDecls({decl, nullptr});
5473+
});
54695474
switch (member->getKind()) {
54705475
case DeclKind::Import:
54715476
case DeclKind::TopLevelCode:
@@ -5526,8 +5531,7 @@ void IRGenModule::emitNestedTypeDecls(DeclRange members) {
55265531
emitClassDecl(cast<ClassDecl>(member));
55275532
continue;
55285533
case DeclKind::MacroExpansion:
5529-
for (auto *decl : cast<MacroExpansionDecl>(member)->getRewritten())
5530-
emitNestedTypeDecls({decl, nullptr});
5534+
// Expansion already visited as auxiliary decls.
55315535
continue;
55325536
}
55335537
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,12 +2063,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20632063
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {
20642064
// Assign a discriminator.
20652065
(void)MED->getDiscriminator();
2066-
2067-
auto rewritten = evaluateOrDefault(
2068-
Ctx.evaluator, ExpandMacroExpansionDeclRequest{MED}, {});
2069-
2070-
for (auto *decl : rewritten)
2071-
visit(decl);
2066+
// Expansion already visited as auxiliary decls.
20722067
}
20732068

20742069
void visitBoundVariable(VarDecl *VD) {
@@ -3763,7 +3758,7 @@ void TypeChecker::checkParameterList(ParameterList *params,
37633758
}
37643759
}
37653760

3766-
ArrayRef<Decl *>
3761+
Optional<unsigned>
37673762
ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37683763
MacroExpansionDecl *MED) const {
37693764
auto &ctx = MED->getASTContext();
@@ -3785,8 +3780,5 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37853780
MED->setMacroRef(macro);
37863781

37873782
// Expand the macro.
3788-
SmallVector<Decl *, 2> expandedTemporary;
3789-
if (!expandFreestandingDeclarationMacro(MED, expandedTemporary))
3790-
return {};
3791-
return ctx.AllocateCopy(expandedTemporary);
3783+
return expandFreestandingDeclarationMacro(MED);
37923784
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,17 @@ Expr *swift::expandMacroExpr(
676676
return expandedExpr;
677677
}
678678

679-
/// Expands the given macro expansion declaration and type-check the result.
680-
bool swift::expandFreestandingDeclarationMacro(
681-
MacroExpansionDecl *med, SmallVectorImpl<Decl *> &results) {
679+
/// Expands the given macro expansion declaration.
680+
Optional<unsigned>
681+
swift::expandFreestandingDeclarationMacro(MacroExpansionDecl *med) {
682682
auto *dc = med->getDeclContext();
683683
ASTContext &ctx = dc->getASTContext();
684684
SourceManager &sourceMgr = ctx.SourceMgr;
685685

686686
auto moduleDecl = dc->getParentModule();
687687
auto sourceFile = moduleDecl->getSourceFileContainingLocation(med->getLoc());
688688
if (!sourceFile)
689-
return false;
689+
return None;
690690

691691
// Evaluate the macro.
692692
NullTerminatedStringRef evaluatedSource;
@@ -697,21 +697,21 @@ bool swift::expandFreestandingDeclarationMacro(
697697

698698
if (isFromExpansionOfMacro(sourceFile, macro, MacroRole::Declaration)) {
699699
med->diagnose(diag::macro_recursive, macro->getName());
700-
return false;
700+
return None;
701701
}
702702

703703
auto macroDef = macro->getDefinition();
704704
switch (macroDef.kind) {
705705
case MacroDefinition::Kind::Undefined:
706706
case MacroDefinition::Kind::Invalid:
707707
// Already diagnosed as an error elsewhere.
708-
return false;
708+
return None;
709709

710710
case MacroDefinition::Kind::Builtin: {
711711
switch (macroDef.getBuiltinKind()) {
712712
case BuiltinMacroKind::ExternalMacro:
713713
// FIXME: Error here.
714-
return false;
714+
return None;
715715
}
716716
}
717717

@@ -729,15 +729,15 @@ bool swift::expandFreestandingDeclarationMacro(
729729
macro->getName()
730730
);
731731
macro->diagnose(diag::decl_declared_here, macro->getName());
732-
return false;
732+
return None;
733733
}
734734

735735
// Make sure freestanding macros are enabled before we expand.
736736
if (!ctx.LangOpts.hasFeature(Feature::FreestandingMacros) &&
737737
!macro->getMacroRoles().contains(MacroRole::Expression)) {
738738
med->diagnose(
739739
diag::macro_experimental, "freestanding", "FreestandingMacros");
740-
return false;
740+
return None;
741741
}
742742

743743
#if SWIFT_SWIFT_PARSER
@@ -746,7 +746,7 @@ bool swift::expandFreestandingDeclarationMacro(
746746
// Builtin macros are handled via ASTGen.
747747
auto astGenSourceFile = sourceFile->exportedSourceFile;
748748
if (!astGenSourceFile)
749-
return false;
749+
return None;
750750

751751
Mangle::ASTMangler mangler;
752752
auto discriminator = mangler.mangleMacroExpansion(med);
@@ -760,13 +760,13 @@ bool swift::expandFreestandingDeclarationMacro(
760760
med->getStartLoc().getOpaquePointerValue(), &evaluatedSourceAddress,
761761
&evaluatedSourceLength);
762762
if (!evaluatedSourceAddress)
763-
return false;
763+
return None;
764764
evaluatedSource = NullTerminatedStringRef(evaluatedSourceAddress,
765765
(size_t)evaluatedSourceLength);
766766
break;
767767
#else
768768
med->diagnose(diag::macro_unsupported);
769-
return false;
769+
return None;
770770
#endif
771771
}
772772
}
@@ -820,12 +820,11 @@ bool swift::expandFreestandingDeclarationMacro(
820820
if (!decl) {
821821
ctx.Diags.diagnose(
822822
macroBufferRange.getStart(), diag::expected_macro_expansion_decls);
823-
return false;
823+
return None;
824824
}
825825
decl->setDeclContext(dc);
826-
results.push_back(decl);
827826
}
828-
return true;
827+
return macroBufferID;
829828
}
830829

831830
// If this storage declaration is a variable with an explicit initializer,
@@ -1219,17 +1218,6 @@ swift::expandPeers(CustomAttr *attr, MacroDecl *macro, Decl *decl) {
12191218
return None;
12201219

12211220
PrettyStackTraceDecl debugStack("applying expanded peer macro", decl);
1222-
1223-
auto *parent = decl->getDeclContext();
1224-
auto topLevelDecls = macroSourceFile->getTopLevelDecls();
1225-
for (auto peer : topLevelDecls) {
1226-
if (auto *nominal = dyn_cast<NominalTypeDecl>(parent)) {
1227-
nominal->addMember(peer);
1228-
} else if (auto *extension = dyn_cast<ExtensionDecl>(parent)) {
1229-
extension->addMember(peer);
1230-
}
1231-
}
1232-
12331221
return macroSourceFile->getBufferID();
12341222
}
12351223

lib/Sema/TypeCheckMacros.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ Expr *expandMacroExpr(
4343
/// Expands the given macro expansion declaration, type-checks the replacement
4444
/// declarations, and adds them to \p results.
4545
///
46-
/// \returns true if expansion succeeded, false if failed.
47-
bool expandFreestandingDeclarationMacro(
48-
MacroExpansionDecl *med, SmallVectorImpl<Decl *> &results);
46+
/// \returns Expansion buffer ID if expansion succeeded, \p None if failed.
47+
Optional<unsigned> expandFreestandingDeclarationMacro(MacroExpansionDecl *med);
4948

5049
/// Expand the accessors for the given storage declaration based on the
5150
/// custom attribute that references the given macro.

0 commit comments

Comments
 (0)