Skip to content

Commit efd5890

Browse files
authored
Merge pull request #64942 from DougGregor/sil-symbol-visitor-aux-decls
2 parents c6746a5 + 27fbafc commit efd5890

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
390390
void visit(FileUnit *file) {
391391
auto visitFile = [this](FileUnit *file) {
392392
SmallVector<Decl *, 16> decls;
393-
file->getTopLevelDecls(decls);
393+
file->getTopLevelDeclsWithAuxiliaryDecls(decls);
394394

395395
addMainIfNecessary(file);
396396

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,20 @@ public struct DefineStructWithUnqualifiedLookupMacro: DeclarationMacro {
12531253
}
12541254
}
12551255

1256+
extension TupleExprElementListSyntax {
1257+
/// Retrieve the first element with the given label.
1258+
func first(labeled name: String) -> Element? {
1259+
return first { element in
1260+
if let label = element.label, label.text == name {
1261+
return true
1262+
}
1263+
1264+
return false
1265+
}
1266+
}
1267+
}
1268+
1269+
12561270
public struct DefineAnonymousTypesMacro: DeclarationMacro {
12571271
public static func expansion(
12581272
of node: some FreestandingMacroExpansionSyntax,
@@ -1261,13 +1275,22 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
12611275
guard let body = node.trailingClosure else {
12621276
throw CustomError.message("#anonymousTypes macro requires a trailing closure")
12631277
}
1278+
1279+
let accessSpecifier: String
1280+
if let _ = node.argumentList.first(labeled: "public") {
1281+
accessSpecifier = "public "
1282+
} else {
1283+
accessSpecifier = ""
1284+
}
12641285
return [
12651286
"""
12661287
1267-
class \(context.makeUniqueName("name")) {
1268-
func hello() -> String {
1288+
\(raw:accessSpecifier)class \(context.makeUniqueName("name")) {
1289+
\(raw:accessSpecifier)func hello() -> String {
12691290
\(body.statements)
12701291
}
1292+
1293+
\(raw:accessSpecifier)func getSelf() -> Any.Type { return Self.self }
12711294
}
12721295
""",
12731296
"""

test/Macros/top_level_freestanding.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ func lookupGlobalFreestandingExpansion() {
2626
}
2727

2828
@freestanding(declaration)
29-
macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
29+
macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
3030

31-
#anonymousTypes { "hello" }
31+
#anonymousTypes(public: true) { "hello" }
3232

33-
// CHECK-SIL: sil hidden @$s9MacroUser03$s9A71User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_4namefMu_C5helloSSyF
33+
// CHECK-SIL: sil @$s9MacroUser03$s9A71User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_4namefMu_C5helloSSyF
3434

3535
@main
3636
struct Main {

test/SourceKit/Macros/macro_basic.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
131131
// EXPAND_MACRO_DECL-NEXT: func hello() -> String {
132132
// EXPAND_MACRO_DECL-NEXT: "hello"
133133
// EXPAND_MACRO_DECL-NEXT: }
134+
// EXPAND_MACRO_DECL: func getSelf() -> Any.Type { return Self.self }
134135
// EXPAND_MACRO_DECL-NEXT: }
135136
// EXPAND_MACRO_DECL-NEXT: enum $s9MacroUser33_70D4178875715FB9B8B50C58F66F8D53Ll14anonymousTypesfMf0_4namefMu0_ {
136137
// EXPAND_MACRO_DECL-NEXT: case apple

0 commit comments

Comments
 (0)