Skip to content

Commit 4b5105d

Browse files
committed
[ASTGen] Implement bridging for using declaration
1 parent 816ea9f commit 4b5105d

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,19 @@ BridgedImportDecl BridgedImportDecl_createParsed(
17441744
BridgedSourceLoc cImportKeywordLoc, BridgedImportKind cImportKind,
17451745
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements);
17461746

1747+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedUsingSpecifier {
1748+
BridgedUsingSpecifierMainActor,
1749+
BridgedUsingSpecifierNonisolated,
1750+
};
1751+
1752+
SWIFT_NAME("BridgedUsingDecl.createParsed(_:declContext:usingKeywordLoc:"
1753+
"specifierLoc:specifier:)")
1754+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
1755+
BridgedDeclContext cDeclContext,
1756+
BridgedSourceLoc usingKeywordLoc,
1757+
BridgedSourceLoc specifierLoc,
1758+
BridgedUsingSpecifier specifier);
1759+
17471760
SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
17481761
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
17491762
"arrowLoc:returnType:genericWhereClause:)")

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,17 @@ BridgedImportDecl BridgedImportDecl_createParsed(
637637
std::move(builder).get());
638638
}
639639

640+
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
641+
BridgedDeclContext cDeclContext,
642+
BridgedSourceLoc usingKeywordLoc,
643+
BridgedSourceLoc specifierLoc,
644+
BridgedUsingSpecifier specifier) {
645+
ASTContext &ctx = cContext.unbridged();
646+
return UsingDecl::create(
647+
ctx, usingKeywordLoc.unbridged(), specifierLoc.unbridged(),
648+
static_cast<UsingSpecifier>(specifier), cDeclContext.unbridged());
649+
}
650+
640651
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
641652
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
642653
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ extension ASTGenVisitor {
6969
return self.generate(typeAliasDecl: node)?.asDecl
7070
case .variableDecl(let node):
7171
return self.generate(variableDecl: node)
72+
case .usingDecl(let node):
73+
return self.generate(usingDecl: node)?.asDecl
7274
}
7375
}
7476

@@ -1083,6 +1085,37 @@ extension ASTGenVisitor {
10831085
}
10841086
}
10851087

1088+
extension ASTGenVisitor {
1089+
func generate(usingDecl node: UsingDeclSyntax) -> BridgedUsingDecl? {
1090+
var specifier: BridgedUsingSpecifier? = nil
1091+
1092+
switch node.specifier {
1093+
case .attribute(let attr):
1094+
if let identifier = attr.attributeName.as(IdentifierTypeSyntax.self),
1095+
identifier.name.tokenKind == .identifier("MainActor") {
1096+
specifier = .mainActor
1097+
}
1098+
case .modifier(let modifier):
1099+
if case .identifier("nonisolated") = modifier.tokenKind {
1100+
specifier = .nonisolated
1101+
}
1102+
}
1103+
1104+
guard let specifier else {
1105+
self.diagnose(.invalidDefaultIsolationSpecifier(node.specifier))
1106+
return nil
1107+
}
1108+
1109+
return BridgedUsingDecl.createParsed(
1110+
self.ctx,
1111+
declContext: self.declContext,
1112+
usingKeywordLoc: self.generateSourceLoc(node.usingKeyword),
1113+
specifierLoc: self.generateSourceLoc(node.specifier),
1114+
specifier: specifier
1115+
)
1116+
}
1117+
}
1118+
10861119
extension ASTGenVisitor {
10871120
func generate(memberBlockItem node: MemberBlockItemSyntax) -> BridgedDecl? {
10881121
if let node = node.decl.as(MacroExpansionDeclSyntax.self) {

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ extension ASTGenDiagnostic {
146146
message: "expressions are not allowed at the top level"
147147
)
148148
}
149+
150+
static func invalidDefaultIsolationSpecifier(_ specifier: some SyntaxProtocol) -> Self {
151+
Self(
152+
node: specifier,
153+
message: "default isolation can only be set to '@MainActor' or 'nonisolated'"
154+
)
155+
}
149156
}
150157

151158
/// DeclAttributes diagnostics

test/ASTGen/decls.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import protocol Swift.Sequence
3131
import func Swift.max
3232
import var Swift._playgroundPrintHook
3333

34+
35+
using @MainActor
36+
// FIXME: cannot add `using nonisolated` here because it's a re-declaration
37+
3438
func
3539
test1
3640
(

test/ASTGen/diagnostics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// REQUIRES: swift_swift_parser
66
// REQUIRES: swift_feature_ParserASTGen
7+
78
// rdar://116686158
89
// UNSUPPORTED: asan
910

@@ -66,3 +67,12 @@ func misisngPatternTest(arr: [Int]) {
6667
for {} // expected-error {{expected pattern, 'in', and expression in 'for' statement}}
6768
// expected-note@-1 {{insert pattern, 'in', and expression}} {{7-7=<#pattern#> }} {{7-7=in }} {{7-7=<#expression#> }}
6869
}
70+
71+
using @MainActor // expected-note {{default isolation was previously declared here}}
72+
using nonisolated // expected-error {{invalid redeclaration of file-level default actor isolation}}
73+
74+
using @Test
75+
// expected-error@-1 {{default isolation can only be set to '@MainActor' or 'nonisolated'}}
76+
77+
using test
78+
// expected-error@-1 {{default isolation can only be set to '@MainActor' or 'nonisolated'}}

0 commit comments

Comments
 (0)