Skip to content

Commit 1fd92db

Browse files
committed
[ASTGen] Lower @abi in ASTGen
1 parent e1ddeb0 commit 1fd92db

File tree

8 files changed

+70
-9
lines changed

8 files changed

+70
-9
lines changed

include/swift/AST/ASTBridging.h

+5
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ BridgedDeclAttribute BridgedDeclAttribute_createSimple(
581581
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
582582
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc);
583583

584+
SWIFT_NAME("BridgedABIAttr.createParsed(_:atLoc:range:abiDecl:)")
585+
BridgedABIAttr BridgedABIAttr_createParsed(
586+
BridgedASTContext cContext, BridgedSourceLoc atLoc,
587+
BridgedSourceRange range, BridgedNullableDecl abiDecl);
588+
584589
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAccessLevel {
585590
BridgedAccessLevelPrivate,
586591
BridgedAccessLevelFilePrivate,

include/swift/AST/ASTBridgingWrappers.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
// Some of the base classes need to be nullable to allow them to be used as
7070
// optional parameters.
71-
AST_BRIDGING_WRAPPER_NONNULL(Decl)
71+
AST_BRIDGING_WRAPPER_NULLABLE(Decl)
7272
AST_BRIDGING_WRAPPER_NONNULL(DeclContext)
7373
AST_BRIDGING_WRAPPER_NONNULL(SourceFile)
7474
AST_BRIDGING_WRAPPER_NULLABLE(Stmt)

lib/AST/Bridging/DeclAttributeBridging.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ static std::optional<AccessLevel> unbridge(BridgedAccessLevel level) {
9393
llvm_unreachable("unhandled BridgedAccessLevel");
9494
}
9595

96+
BridgedABIAttr BridgedABIAttr_createParsed(BridgedASTContext cContext,
97+
BridgedSourceLoc atLoc,
98+
BridgedSourceRange range,
99+
BridgedNullableDecl abiDecl) {
100+
return new (cContext.unbridged()) ABIAttr(abiDecl.unbridged(),
101+
atLoc.unbridged(),
102+
range.unbridged(),
103+
/*isImplicit=*/false);
104+
}
105+
96106
BridgedAccessControlAttr
97107
BridgedAccessControlAttr_createParsed(BridgedASTContext cContext,
98108
BridgedSourceRange cRange,

lib/ASTGen/Sources/ASTGen/Bridge.swift

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extension BridgedNullable {
2626

2727
extension BridgedSourceLoc: /*@retroactive*/ swiftASTGen.BridgedNullable {}
2828
extension BridgedIdentifier: /*@retroactive*/ swiftASTGen.BridgedNullable {}
29+
extension BridgedNullableDecl: /*@retroactive*/ swiftASTGen.BridgedNullable {}
2930
extension BridgedNullableExpr: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3031
extension BridgedNullableStmt: /*@retroactive*/ swiftASTGen.BridgedNullable {}
3132
extension BridgedNullableTypeRepr: /*@retroactive*/ swiftASTGen.BridgedNullable {}
@@ -60,6 +61,9 @@ extension Optional where Wrapped: BridgedHasNullable {
6061
extension BridgedStmt: BridgedHasNullable {
6162
typealias Nullable = BridgedNullableStmt
6263
}
64+
extension BridgedDecl: BridgedHasNullable {
65+
typealias Nullable = BridgedNullableDecl
66+
}
6367
extension BridgedExpr: BridgedHasNullable {
6468
typealias Nullable = BridgedNullableExpr
6569
}

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

+40-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ extension ASTGenVisitor {
112112
let attrKind = BridgedDeclAttrKind(from: attrName.bridged)
113113
switch attrKind {
114114
case .ABI:
115-
return self.generateABIAttr(attribute: node)?.asDeclAttribute
115+
return handle(self.generateABIAttr(attribute: node)?.asDeclAttribute)
116116
case .alignment:
117117
return handle(self.generateAlignmentAttr(attribute: node)?.asDeclAttribute)
118118
case .allowFeatureSuppression:
@@ -333,8 +333,45 @@ extension ASTGenVisitor {
333333
/// @abi(func fn())
334334
/// ```
335335
func generateABIAttr(attribute node: AttributeSyntax) -> BridgedABIAttr? {
336-
#warning("TODO: implement generateABIAttr")
337-
fatalError("TODO: implement generateABIAttr")
336+
guard
337+
let arg = node.arguments?.as(ABIAttributeArgumentsSyntax.self)
338+
else {
339+
// TODO: diagnose
340+
return nil
341+
}
342+
343+
let abiDecl: BridgedDecl?
344+
switch arg.provider {
345+
case .associatedType(let assocTyDecl):
346+
abiDecl = self.generate(associatedTypeDecl: assocTyDecl)?.asDecl
347+
case .deinitializer(let deinitDecl):
348+
abiDecl = self.generate(deinitializerDecl: deinitDecl).asDecl
349+
case .enumCase(let caseDecl):
350+
abiDecl = self.generate(enumCaseDecl: caseDecl).asDecl
351+
case .function(let funcDecl):
352+
abiDecl = self.generate(functionDecl: funcDecl)?.asDecl
353+
case .initializer(let initDecl):
354+
abiDecl = self.generate(initializerDecl: initDecl).asDecl
355+
case .`subscript`(let subscriptDecl):
356+
abiDecl = self.generate(subscriptDecl: subscriptDecl).asDecl
357+
case .typeAlias(let typealiasDecl):
358+
abiDecl = self.generate(typeAliasDecl: typealiasDecl)?.asDecl
359+
case .variable(let varDecl):
360+
abiDecl = self.generate(variableDecl: varDecl).asDecl
361+
case .missing(_):
362+
// This error condition will have been diagnosed in SwiftSyntax.
363+
abiDecl = nil
364+
}
365+
366+
// TODO: Diagnose if `abiDecl` has a body/initial value/etc.
367+
// The C++ parser considers it syntactically invalid but SwiftSyntax does not.
368+
369+
return .createParsed(
370+
self.ctx,
371+
atLoc: self.generateSourceLoc(node.atSign),
372+
range: self.generateAttrSourceRange(node),
373+
abiDecl: abiDecl.asNullable
374+
)
338375
}
339376

340377
/// E.g.:

lib/ASTGen/Sources/ASTGen/SourceFile.swift

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extension Parser.ExperimentalFeatures {
7676
mapFeature(.TrailingComma, to: .trailingComma)
7777
mapFeature(.CoroutineAccessors, to: .coroutineAccessors)
7878
mapFeature(.ValueGenerics, to: .valueGenerics)
79+
mapFeature(.ABIAttribute, to: .abiAttribute)
7980
}
8081
}
8182

test/ASTGen/attrs.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Extern -enable-experimental-move-only -enable-experimental-feature ParserASTGen > %t/astgen.ast.raw
3-
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Extern -enable-experimental-move-only > %t/cpp-parser.ast.raw
2+
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature ABIAttribute -enable-experimental-feature Extern -enable-experimental-move-only -enable-experimental-feature ParserASTGen > %t/astgen.ast.raw
3+
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature ABIAttribute -enable-experimental-feature Extern -enable-experimental-move-only > %t/cpp-parser.ast.raw
44

55
// Filter out any addresses in the dump, since they can differ.
66
// RUN: sed -E 's#0x[0-9a-fA-F]+##g' %t/cpp-parser.ast.raw > %t/cpp-parser.ast
77
// RUN: sed -E 's#0x[0-9a-fA-F]+##g' %t/astgen.ast.raw > %t/astgen.ast
88

99
// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast
1010

11-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Extern -enable-experimental-move-only -enable-experimental-feature ParserASTGen
11+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature ABIAttribute -enable-experimental-feature Extern -enable-experimental-move-only -enable-experimental-feature ParserASTGen
1212

1313
// REQUIRES: executable_test
1414
// REQUIRES: swift_swift_parser
1515
// REQUIRES: swift_feature_SymbolLinkageMarkers
1616
// REQUIRES: swift_feature_Extern
1717
// REQUIRES: swift_feature_ParserASTGen
18+
// REQUIRES: swift_feature_ABIAttribute
1819

1920
// rdar://116686158
2021
// UNSUPPORTED: asan
@@ -62,7 +63,10 @@ struct S4 {}
6263
@implementation extension ObjCClass1 {} // expected-error {{cannot find type 'ObjCClass1' in scope}}
6364
@implementation(Category) extension ObjCClass1 {} // expected-error {{cannot find type 'ObjCClass1' in scope}}
6465

65-
@_alignment(8) struct AnyAlignment {}
66+
@abi(func fn_abi()) // expected-error {{cannot give global function 'fn' the ABI of a global function with a different number of low-level parameters}}
67+
func fn(_: Int) {}
68+
69+
@_alignment(8) struct AnyAlignment {}
6670

6771
@_allowFeatureSuppression(IsolatedAny) public func testFeatureSuppression(fn: @isolated(any) @Sendable () -> ()) {}
6872

test/attr/feature_requirement.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -parse-as-library -verify-additional-prefix disabled-
1+
// RUN: %target-typecheck-verify-swift -parse-as-library -disable-experimental-parser-round-trip -verify-additional-prefix disabled-
22
// RUN: %target-typecheck-verify-swift -parse-as-library -verify-additional-prefix enabled- -enable-experimental-feature ABIAttribute
33

44
// REQUIRES: asserts

0 commit comments

Comments
 (0)