Skip to content

Commit 0e7d07e

Browse files
authored
Merge pull request #69353 from rintaro/5.10-macros-visitauxiliary-toplevel
[5.10][Macros] Improve visitation of auxiliary decls
2 parents d69010e + 533464e commit 0e7d07e

File tree

9 files changed

+80
-36
lines changed

9 files changed

+80
-36
lines changed

include/swift/AST/Module.h

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

962+
/// Finds all top-level decls of this module including auxiliary decls.
963+
void
964+
getTopLevelDeclsWithAuxiliaryDecls(SmallVectorImpl<Decl *> &Results) const;
965+
962966
void getExportedPrespecializations(SmallVectorImpl<Decl *> &results) const;
963967

964968
/// 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
@@ -1024,12 +1024,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
10241024
Options.TransformContext->isPrintingSynthesizedExtension() &&
10251025
isa<ExtensionDecl>(D);
10261026

1027-
SWIFT_DEFER {
1028-
D->visitAuxiliaryDecls([&](Decl *auxDecl) {
1029-
visit(auxDecl);
1030-
});
1031-
};
1032-
10331027
if (!shouldPrint(D, true) && !Synthesize)
10341028
return false;
10351029

@@ -4389,13 +4383,20 @@ bool PrintAST::printASTNodes(const ArrayRef<ASTNode> &Elements,
43894383
bool NeedIndent) {
43904384
IndentRAII IndentMore(*this, NeedIndent);
43914385
bool PrintedSomething = false;
4386+
4387+
std::function<void(Decl *)> printDecl;
4388+
printDecl = [&](Decl *d) {
4389+
if (d->shouldPrintInContext(Options))
4390+
visit(d);
4391+
d->visitAuxiliaryDecls(printDecl);
4392+
};
4393+
43924394
for (auto element : Elements) {
43934395
PrintedSomething = true;
43944396
Printer.printNewline();
43954397
indent();
43964398
if (auto decl = element.dyn_cast<Decl*>()) {
4397-
if (decl->shouldPrintInContext(Options))
4398-
visit(decl);
4399+
printDecl(decl);
43994400
} else if (auto stmt = element.dyn_cast<Stmt*>()) {
44004401
visit(stmt);
44014402
} 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);
@@ -3060,7 +3065,9 @@ void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) {
30603065
void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
30613066
std::set<DeclKind> MajorDeclKinds = {DeclKind::Class, DeclKind::Enum,
30623067
DeclKind::Extension, DeclKind::Protocol, DeclKind::Struct};
3063-
for (auto decl : getTopLevelDecls()) {
3068+
SmallVector<Decl *> topLevelDecls;
3069+
getTopLevelDeclsWithAuxiliaryDecls(topLevelDecls);
3070+
for (auto decl : topLevelDecls) {
30643071
if (!decl->shouldPrintInContext(PO))
30653072
continue;
30663073
// For a major decl, we print an empty line before it.
@@ -4137,14 +4144,18 @@ void FileUnit::getTopLevelDeclsWhereAttributesMatch(
41374144

41384145
void FileUnit::getTopLevelDeclsWithAuxiliaryDecls(
41394146
SmallVectorImpl<Decl*> &results) const {
4147+
4148+
std::function<void(Decl *)> addResult;
4149+
addResult = [&](Decl *decl) {
4150+
results.push_back(decl);
4151+
decl->visitAuxiliaryDecls(addResult);
4152+
};
4153+
41404154
SmallVector<Decl *, 32> nonExpandedDecls;
41414155
nonExpandedDecls.reserve(results.capacity());
41424156
getTopLevelDecls(nonExpandedDecls);
41434157
for (auto *decl : nonExpandedDecls) {
4144-
decl->visitAuxiliaryDecls([&](Decl *auxDecl) {
4145-
results.push_back(auxDecl);
4146-
});
4147-
results.push_back(decl);
4158+
addResult(decl);
41484159
}
41494160
}
41504161

lib/AST/NameLookup.cpp

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

4007-
auto visitDecl = [&](Decl *D) {
4007+
std::function<void(Decl *)> visitDecl;
4008+
visitDecl = [&](Decl *D) {
40084009
if (auto *VD = dyn_cast<ValueDecl>(D))
40094010
checkValueDecl(VD, DeclVisibilityKind::LocalVariable);
4010-
D->visitAuxiliaryDecls([&](Decl *D) {
4011-
if (auto *VD = dyn_cast<ValueDecl>(D))
4012-
checkValueDecl(VD, DeclVisibilityKind::LocalVariable);
4013-
// FIXME: Recursively call `visitDecl` to handle nested macros.
4014-
});
4011+
D->visitAuxiliaryDecls(visitDecl);
40154012
};
4016-
40174013
for (auto elem : S->getElements()) {
40184014
if (auto *E = elem.dyn_cast<Expr *>()) {
40194015
// '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
@@ -837,7 +837,7 @@ bool swift::emitSwiftInterface(raw_ostream &out,
837837
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
838838

839839
SmallVector<Decl *, 16> topLevelDecls;
840-
M->getTopLevelDecls(topLevelDecls);
840+
M->getTopLevelDeclsWithAuxiliaryDecls(topLevelDecls);
841841
for (const Decl *D : topLevelDecls) {
842842
InheritedProtocolCollector::collectProtocols(inheritedProtocolMap, D);
843843

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,24 +217,17 @@ static void collectVisibleMemberDecls(const DeclContext *CurrDC, LookupState LS,
217217
Type BaseType,
218218
IterableDeclContext *Parent,
219219
SmallVectorImpl<ValueDecl *> &FoundDecls) {
220-
auto check = [&](Decl *decl) {
221-
auto *VD = dyn_cast<ValueDecl>(decl);
220+
for (auto Member : Parent->getAllMembers()) {
221+
auto *VD = dyn_cast<ValueDecl>(Member);
222222
if (!VD)
223-
return;
223+
continue;
224224
if (!isDeclVisibleInLookupMode(VD, LS, CurrDC))
225-
return;
225+
continue;
226226
if (!evaluateOrDefault(CurrDC->getASTContext().evaluator,
227227
IsDeclApplicableRequest(DeclApplicabilityOwner(CurrDC, BaseType, VD)),
228228
false))
229-
return;
229+
continue;
230230
FoundDecls.push_back(VD);
231-
};
232-
233-
for (auto Member : Parent->getAllMembers()) {
234-
check(Member);
235-
Member->visitAuxiliaryDecls([&](Decl *d) {
236-
check(d);
237-
});
238231
}
239232
}
240233

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
19001900
return [
19011901
"""
19021902
1903-
private var _foo: Int = 100
1903+
public var _foo: Int = 100
19041904
"""
19051905
]
19061906
}

test/ModuleInterface/macros.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,35 @@ macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition",
6464

6565
let world = 17
6666

67-
// CHECK-NOT: structWithUnqualifiedLookup
6867
public
6968
#structWithUnqualifiedLookup
70-
69+
// CHECK-NOT: structWithUnqualifiedLookup
70+
// CHECK-NOT: struct StructWithUnqualifiedLookup
7171
// CHECK: struct StructWithUnqualifiedLookup
72+
// CHECK-NOT: struct StructWithUnqualifiedLookup
73+
74+
@attached(peer, names: named(_foo))
75+
macro AddPeerStoredProperty() = #externalMacro(module: "MacroDefinition", type: "AddPeerStoredPropertyMacro")
76+
77+
@AddPeerStoredProperty
78+
public var test: Int = 10
79+
// CHECK: var test
80+
// CHECK-NOT: var _foo
81+
// CHECK: var _foo
82+
// CHECK-NOT: var _foo
83+
84+
// CHECK: struct TestStruct {
85+
public struct TestStruct {
86+
public #structWithUnqualifiedLookup
87+
// CHECK-NOT: structWithUnqualifiedLookup
88+
// CHECK-NOT: struct StructWithUnqualifiedLookup
89+
// CHECK: struct StructWithUnqualifiedLookup
90+
// CHECK-NOT: struct StructWithUnqualifiedLookup
91+
92+
@AddPeerStoredProperty
93+
public var test: Int = 10
94+
// CHECK: var test
95+
// CHECK-NOT: var _foo
96+
// CHECK: var _foo
97+
// CHECK-NOT: var _foo
98+
}

test/SourceKit/Macros/macro_basic.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
6060
@freestanding(expression) macro assert(_: String) = #externalMacro(module: "MacroDefinition", type: "AssertMacro")
6161
#assert("foobar")
6262

63+
@attached(peer, names: named(_foo))
64+
macro AddPeerStoredProperty() = #externalMacro(module: "MacroDefinition", type: "AddPeerStoredPropertyMacro")
65+
struct S5 {
66+
@AddPeerStoredProperty
67+
var test: Int = 10
68+
}
69+
6370
// REQUIRES: swift_swift_parser, executable_test, shell
6471

6572
// RUN: %empty-directory(%t)
@@ -281,3 +288,8 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
281288
//##-- Expansion on "fails to typecheck" macro expression
282289
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=61:2 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ERRONEOUS_EXPAND %s
283290
// ERRONEOUS_EXPAND: 61:1-61:18 (@__swiftmacro_{{.+}}.swift) "assert("foobar")"
291+
292+
//##-- Cursor-info on a decl where a peer macro attached.
293+
// RUN: %sourcekitd-test -req=cursor -pos=67:7 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=CURSOR_ON_DECL_WITH_PEER %s
294+
// CURSOR_ON_DECL_WITH_PEER: <decl.var.instance><syntaxtype.keyword>var</syntaxtype.keyword> <decl.name>test</decl.name>: <decl.var.type><ref.struct usr="s:Si">Int</ref.struct></decl.var.type></decl.var.instance>
295+
// CURSOR_ON_DECL_WITH_PEER-NOT: _foo

0 commit comments

Comments
 (0)