[ASTGen] Implement bridging for using declaration

This commit is contained in:
Pavel Yaskevich
2025-05-28 09:34:29 -07:00
parent 816ea9fda6
commit 4b5105df5e
6 changed files with 78 additions and 0 deletions

View File

@@ -1744,6 +1744,19 @@ BridgedImportDecl BridgedImportDecl_createParsed(
BridgedSourceLoc cImportKeywordLoc, BridgedImportKind cImportKind,
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements);
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedUsingSpecifier {
BridgedUsingSpecifierMainActor,
BridgedUsingSpecifierNonisolated,
};
SWIFT_NAME("BridgedUsingDecl.createParsed(_:declContext:usingKeywordLoc:"
"specifierLoc:specifier:)")
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
BridgedDeclContext cDeclContext,
BridgedSourceLoc usingKeywordLoc,
BridgedSourceLoc specifierLoc,
BridgedUsingSpecifier specifier);
SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
"arrowLoc:returnType:genericWhereClause:)")

View File

@@ -637,6 +637,17 @@ BridgedImportDecl BridgedImportDecl_createParsed(
std::move(builder).get());
}
BridgedUsingDecl BridgedUsingDecl_createParsed(BridgedASTContext cContext,
BridgedDeclContext cDeclContext,
BridgedSourceLoc usingKeywordLoc,
BridgedSourceLoc specifierLoc,
BridgedUsingSpecifier specifier) {
ASTContext &ctx = cContext.unbridged();
return UsingDecl::create(
ctx, usingKeywordLoc.unbridged(), specifierLoc.unbridged(),
static_cast<UsingSpecifier>(specifier), cDeclContext.unbridged());
}
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,

View File

@@ -69,6 +69,8 @@ extension ASTGenVisitor {
return self.generate(typeAliasDecl: node)?.asDecl
case .variableDecl(let node):
return self.generate(variableDecl: node)
case .usingDecl(let node):
return self.generate(usingDecl: node)?.asDecl
}
}
@@ -1083,6 +1085,37 @@ extension ASTGenVisitor {
}
}
extension ASTGenVisitor {
func generate(usingDecl node: UsingDeclSyntax) -> BridgedUsingDecl? {
var specifier: BridgedUsingSpecifier? = nil
switch node.specifier {
case .attribute(let attr):
if let identifier = attr.attributeName.as(IdentifierTypeSyntax.self),
identifier.name.tokenKind == .identifier("MainActor") {
specifier = .mainActor
}
case .modifier(let modifier):
if case .identifier("nonisolated") = modifier.tokenKind {
specifier = .nonisolated
}
}
guard let specifier else {
self.diagnose(.invalidDefaultIsolationSpecifier(node.specifier))
return nil
}
return BridgedUsingDecl.createParsed(
self.ctx,
declContext: self.declContext,
usingKeywordLoc: self.generateSourceLoc(node.usingKeyword),
specifierLoc: self.generateSourceLoc(node.specifier),
specifier: specifier
)
}
}
extension ASTGenVisitor {
func generate(memberBlockItem node: MemberBlockItemSyntax) -> BridgedDecl? {
if let node = node.decl.as(MacroExpansionDeclSyntax.self) {

View File

@@ -146,6 +146,13 @@ extension ASTGenDiagnostic {
message: "expressions are not allowed at the top level"
)
}
static func invalidDefaultIsolationSpecifier(_ specifier: some SyntaxProtocol) -> Self {
Self(
node: specifier,
message: "default isolation can only be set to '@MainActor' or 'nonisolated'"
)
}
}
/// DeclAttributes diagnostics

View File

@@ -31,6 +31,10 @@ import protocol Swift.Sequence
import func Swift.max
import var Swift._playgroundPrintHook
using @MainActor
// FIXME: cannot add `using nonisolated` here because it's a re-declaration
func
test1
(

View File

@@ -4,6 +4,7 @@
// REQUIRES: swift_swift_parser
// REQUIRES: swift_feature_ParserASTGen
// rdar://116686158
// UNSUPPORTED: asan
@@ -66,3 +67,12 @@ func misisngPatternTest(arr: [Int]) {
for {} // expected-error {{expected pattern, 'in', and expression in 'for' statement}}
// expected-note@-1 {{insert pattern, 'in', and expression}} {{7-7=<#pattern#> }} {{7-7=in }} {{7-7=<#expression#> }}
}
using @MainActor // expected-note {{default isolation was previously declared here}}
using nonisolated // expected-error {{invalid redeclaration of file-level default actor isolation}}
using @Test
// expected-error@-1 {{default isolation can only be set to '@MainActor' or 'nonisolated'}}
using test
// expected-error@-1 {{default isolation can only be set to '@MainActor' or 'nonisolated'}}