Skip to content

Commit 5836709

Browse files
authored
Merge pull request #69345 from rintaro/5.9-macros-visitauxiliary-toplevel
[5.9][Macros] Improve visitation of auxiliary decls
2 parents 668e8fd + 13ce4cc commit 5836709

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
@@ -951,6 +951,10 @@ class ModuleDecl
951951
/// The order of the results is not guaranteed to be meaningful.
952952
void getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const;
953953

954+
/// Finds all top-level decls of this module including auxiliary decls.
955+
void
956+
getTopLevelDeclsWithAuxiliaryDecls(SmallVectorImpl<Decl *> &Results) const;
957+
954958
void getExportedPrespecializations(SmallVectorImpl<Decl *> &results) const;
955959

956960
/// 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
@@ -1020,12 +1020,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
10201020
Options.TransformContext->isPrintingSynthesizedExtension() &&
10211021
isa<ExtensionDecl>(D);
10221022

1023-
SWIFT_DEFER {
1024-
D->visitAuxiliaryDecls([&](Decl *auxDecl) {
1025-
visit(auxDecl);
1026-
});
1027-
};
1028-
10291023
if (!shouldPrint(D, true) && !Synthesize)
10301024
return false;
10311025

@@ -4328,13 +4322,20 @@ bool PrintAST::printASTNodes(const ArrayRef<ASTNode> &Elements,
43284322
bool NeedIndent) {
43294323
IndentRAII IndentMore(*this, NeedIndent);
43304324
bool PrintedSomething = false;
4325+
4326+
std::function<void(Decl *)> printDecl;
4327+
printDecl = [&](Decl *d) {
4328+
if (d->shouldPrintInContext(Options))
4329+
visit(d);
4330+
d->visitAuxiliaryDecls(printDecl);
4331+
};
4332+
43314333
for (auto element : Elements) {
43324334
PrintedSomething = true;
43334335
Printer.printNewline();
43344336
indent();
43354337
if (auto decl = element.dyn_cast<Decl*>()) {
4336-
if (decl->shouldPrintInContext(Options))
4337-
visit(decl);
4338+
printDecl(decl);
43384339
} else if (auto stmt = element.dyn_cast<Stmt*>()) {
43394340
visit(stmt);
43404341
} else {

lib/AST/Module.cpp

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

1332+
void ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls(
1333+
SmallVectorImpl<Decl *> &Results) const {
1334+
FORWARD(getTopLevelDeclsWithAuxiliaryDecls, (Results));
1335+
}
1336+
13321337
void ModuleDecl::dumpDisplayDecls() const {
13331338
SmallVector<Decl *, 32> Decls;
13341339
getDisplayDecls(Decls);
@@ -3132,7 +3137,9 @@ void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) {
31323137
void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
31333138
std::set<DeclKind> MajorDeclKinds = {DeclKind::Class, DeclKind::Enum,
31343139
DeclKind::Extension, DeclKind::Protocol, DeclKind::Struct};
3135-
for (auto decl : getTopLevelDecls()) {
3140+
SmallVector<Decl *> topLevelDecls;
3141+
getTopLevelDeclsWithAuxiliaryDecls(topLevelDecls);
3142+
for (auto decl : topLevelDecls) {
31363143
if (!decl->shouldPrintInContext(PO))
31373144
continue;
31383145
// For a major decl, we print an empty line before it.
@@ -4179,14 +4186,18 @@ void FileUnit::getTopLevelDeclsWhereAttributesMatch(
41794186

41804187
void FileUnit::getTopLevelDeclsWithAuxiliaryDecls(
41814188
SmallVectorImpl<Decl*> &results) const {
4189+
4190+
std::function<void(Decl *)> addResult;
4191+
addResult = [&](Decl *decl) {
4192+
results.push_back(decl);
4193+
decl->visitAuxiliaryDecls(addResult);
4194+
};
4195+
41824196
SmallVector<Decl *, 32> nonExpandedDecls;
41834197
nonExpandedDecls.reserve(results.capacity());
41844198
getTopLevelDecls(nonExpandedDecls);
41854199
for (auto *decl : nonExpandedDecls) {
4186-
decl->visitAuxiliaryDecls([&](Decl *auxDecl) {
4187-
results.push_back(auxDecl);
4188-
});
4189-
results.push_back(decl);
4200+
addResult(decl);
41904201
}
41914202
}
41924203

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,16 +3939,12 @@ void FindLocalVal::visitBraceStmt(BraceStmt *S, bool isTopLevelCode) {
39393939
}
39403940
}
39413941

3942-
auto visitDecl = [&](Decl *D) {
3942+
std::function<void(Decl *)> visitDecl;
3943+
visitDecl = [&](Decl *D) {
39433944
if (auto *VD = dyn_cast<ValueDecl>(D))
39443945
checkValueDecl(VD, DeclVisibilityKind::LocalVariable);
3945-
D->visitAuxiliaryDecls([&](Decl *D) {
3946-
if (auto *VD = dyn_cast<ValueDecl>(D))
3947-
checkValueDecl(VD, DeclVisibilityKind::LocalVariable);
3948-
// FIXME: Recursively call `visitDecl` to handle nested macros.
3949-
});
3946+
D->visitAuxiliaryDecls(visitDecl);
39503947
};
3951-
39523948
for (auto elem : S->getElements()) {
39533949
if (auto *E = elem.dyn_cast<Expr *>()) {
39543950
// '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

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
@@ -1928,7 +1928,7 @@ public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
19281928
return [
19291929
"""
19301930
1931-
private var _foo: Int = 100
1931+
public var _foo: Int = 100
19321932
"""
19331933
]
19341934
}

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)
@@ -284,3 +291,8 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
284291
//##-- Expansion on "fails to typecheck" macro expression
285292
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=61:2 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=ERRONEOUS_EXPAND %s
286293
// ERRONEOUS_EXPAND: 61:1-61:18 (@__swiftmacro_{{.+}}.swift) "assert("foobar")"
294+
295+
//##-- Cursor-info on a decl where a peer macro attached.
296+
// RUN: %sourcekitd-test -req=cursor -pos=67:7 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=CURSOR_ON_DECL_WITH_PEER %s
297+
// 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>
298+
// CURSOR_ON_DECL_WITH_PEER-NOT: _foo

0 commit comments

Comments
 (0)