Skip to content

Commit 03bf349

Browse files
committed
[Macros] Improve visitation of auxiliary decls
Use the same pattern as 'getAllMembers()'. This supports nested macro expansion: ``` std::function<void(Decl *)> visit; visit = [&](Decl *d) { doIt(d); d->visitAuxiliaryDecls(visit); }; for (auto *d : decls) visit(d); ``` Don't visit auxiliary decls in `PrintAST::visit(Decl *)` this function is only intended for single decl printing. The caller should visit them separately. For that, add `ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls()`
1 parent 6f5283e commit 03bf349

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,10 @@ class ModuleDecl
967967
/// The order of the results is not guaranteed to be meaningful.
968968
void getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const;
969969

970+
/// Finds all top-level decls of this module including auxiliary decls.
971+
void
972+
getTopLevelDeclsWithAuxiliaryDecls(SmallVectorImpl<Decl *> &Results) const;
973+
970974
void getExportedPrespecializations(SmallVectorImpl<Decl *> &results) const;
971975

972976
/// Finds top-level decls of this module filtered by their attributes.

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,12 +1035,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
10351035
Options.TransformContext->isPrintingSynthesizedExtension() &&
10361036
isa<ExtensionDecl>(D);
10371037

1038-
SWIFT_DEFER {
1039-
D->visitAuxiliaryDecls([&](Decl *auxDecl) {
1040-
visit(auxDecl);
1041-
});
1042-
};
1043-
10441038
if (!shouldPrint(D, true) && !Synthesize)
10451039
return false;
10461040

@@ -4510,13 +4504,20 @@ bool PrintAST::printASTNodes(const ArrayRef<ASTNode> &Elements,
45104504
bool NeedIndent) {
45114505
IndentRAII IndentMore(*this, NeedIndent);
45124506
bool PrintedSomething = false;
4507+
4508+
std::function<void(Decl *)> printDecl;
4509+
printDecl = [&](Decl *d) {
4510+
if (d->shouldPrintInContext(Options))
4511+
visit(d);
4512+
d->visitAuxiliaryDecls(printDecl);
4513+
};
4514+
45134515
for (auto element : Elements) {
45144516
PrintedSomething = true;
45154517
Printer.printNewline();
45164518
indent();
45174519
if (auto decl = element.dyn_cast<Decl*>()) {
4518-
if (decl->shouldPrintInContext(Options))
4519-
visit(decl);
4520+
printDecl(decl);
45204521
} else if (auto stmt = element.dyn_cast<Stmt*>()) {
45214522
visit(stmt);
45224523
} else {

lib/AST/Module.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,11 @@ void ModuleDecl::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
13311331
FORWARD(getTopLevelDecls, (Results));
13321332
}
13331333

1334+
void ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls(
1335+
SmallVectorImpl<Decl *> &Results) const {
1336+
FORWARD(getTopLevelDeclsWithAuxiliaryDecls, (Results));
1337+
}
1338+
13341339
void ModuleDecl::dumpDisplayDecls() const {
13351340
SmallVector<Decl *, 32> Decls;
13361341
getDisplayDecls(Decls);
@@ -3095,7 +3100,9 @@ void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) {
30953100
void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
30963101
std::set<DeclKind> MajorDeclKinds = {DeclKind::Class, DeclKind::Enum,
30973102
DeclKind::Extension, DeclKind::Protocol, DeclKind::Struct};
3098-
for (auto decl : getTopLevelDecls()) {
3103+
SmallVector<Decl *> topLevelDecls;
3104+
getTopLevelDeclsWithAuxiliaryDecls(topLevelDecls);
3105+
for (auto decl : topLevelDecls) {
30993106
if (!decl->shouldPrintInContext(PO))
31003107
continue;
31013108
// For a major decl, we print an empty line before it.
@@ -4190,14 +4197,18 @@ void FileUnit::getTopLevelDeclsWhereAttributesMatch(
41904197

41914198
void FileUnit::getTopLevelDeclsWithAuxiliaryDecls(
41924199
SmallVectorImpl<Decl*> &results) const {
4200+
4201+
std::function<void(Decl *)> addResult;
4202+
addResult = [&](Decl *decl) {
4203+
results.push_back(decl);
4204+
decl->visitAuxiliaryDecls(addResult);
4205+
};
4206+
41934207
SmallVector<Decl *, 32> nonExpandedDecls;
41944208
nonExpandedDecls.reserve(results.capacity());
41954209
getTopLevelDecls(nonExpandedDecls);
41964210
for (auto *decl : nonExpandedDecls) {
4197-
decl->visitAuxiliaryDecls([&](Decl *auxDecl) {
4198-
results.push_back(auxDecl);
4199-
});
4200-
results.push_back(decl);
4211+
addResult(decl);
42014212
}
42024213
}
42034214

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4005,16 +4005,12 @@ void FindLocalVal::visitBraceStmt(BraceStmt *S, bool isTopLevelCode) {
40054005
}
40064006
}
40074007

4008-
auto visitDecl = [&](Decl *D) {
4008+
std::function<void(Decl *)> visitDecl;
4009+
visitDecl = [&](Decl *D) {
40094010
if (auto *VD = dyn_cast<ValueDecl>(D))
40104011
checkValueDecl(VD, DeclVisibilityKind::LocalVariable);
4011-
D->visitAuxiliaryDecls([&](Decl *D) {
4012-
if (auto *VD = dyn_cast<ValueDecl>(D))
4013-
checkValueDecl(VD, DeclVisibilityKind::LocalVariable);
4014-
// FIXME: Recursively call `visitDecl` to handle nested macros.
4015-
});
4012+
D->visitAuxiliaryDecls(visitDecl);
40164013
};
4017-
40184014
for (auto elem : S->getElements()) {
40194015
if (auto *E = elem.dyn_cast<Expr *>()) {
40204016
// 'MacroExpansionExpr' at code-item position may introduce value decls.

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ bool swift::emitSwiftInterface(raw_ostream &out,
833833
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
834834

835835
SmallVector<Decl *, 16> topLevelDecls;
836-
M->getTopLevelDecls(topLevelDecls);
836+
M->getTopLevelDeclsWithAuxiliaryDecls(topLevelDecls);
837837
for (const Decl *D : topLevelDecls) {
838838
InheritedProtocolCollector::collectProtocols(inheritedProtocolMap, D);
839839

0 commit comments

Comments
 (0)