Skip to content

Commit 6930068

Browse files
authored
Merge pull request #69340 from rintaro/macros-visitauxiliary-toplevel
[Macros] Improve visitation of auxiliary decls
2 parents a5967fa + 55144fb commit 6930068

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
@@ -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

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)