Skip to content

Commit 6c8adfb

Browse files
committed
[ASTGen] Lower @abi in ASTGen
1 parent 65a1f58 commit 6c8adfb

File tree

8 files changed

+70
-8
lines changed

8 files changed

+70
-8
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ BridgedDeclAttribute BridgedDeclAttribute_createSimple(
572572
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
573573
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc);
574574

575+
SWIFT_NAME("BridgedABIAttr.createParsed(_:atLoc:range:abiDecl:)")
576+
BridgedABIAttr BridgedABIAttr_createParsed(
577+
BridgedASTContext cContext, BridgedSourceLoc atLoc,
578+
BridgedSourceRange range, BridgedNullableDecl abiDecl);
579+
575580
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAccessLevel {
576581
BridgedAccessLevelPrivate,
577582
BridgedAccessLevelFilePrivate,

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ 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+
/*isInverse=*/false,
104+
/*isImplicit=*/false);
105+
}
106+
96107
BridgedAccessControlAttr
97108
BridgedAccessControlAttr_createParsed(BridgedASTContext cContext,
98109
BridgedSourceRange cRange,

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 4 additions & 0 deletions
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 {}
@@ -59,6 +60,9 @@ extension Optional where Wrapped: BridgedHasNullable {
5960
extension BridgedStmt: BridgedHasNullable {
6061
typealias Nullable = BridgedNullableStmt
6162
}
63+
extension BridgedDecl: BridgedHasNullable {
64+
typealias Nullable = BridgedNullableDecl
65+
}
6266
extension BridgedExpr: BridgedHasNullable {
6367
typealias Nullable = BridgedNullableExpr
6468
}

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,45 @@ extension ASTGenVisitor {
326326
/// @abi(func fn())
327327
/// ```
328328
func generateABIAttr(attribute node: AttributeSyntax) -> BridgedABIAttr? {
329-
#warning("TODO: implement generateABIAttr")
330-
fatalError("TODO: implement generateABIAttr")
329+
guard
330+
let arg = node.arguments?.as(ABIAttributeArgumentsSyntax.self)
331+
else {
332+
// TODO: diagnose
333+
return nil
334+
}
335+
336+
let abiDecl: BridgedDecl?
337+
switch arg.provider {
338+
case .associatedType(let assocTyDecl):
339+
abiDecl = self.generate(associatedTypeDecl: assocTyDecl)?.asDecl
340+
case .deinitializer(let deinitDecl):
341+
abiDecl = self.generate(deinitializerDecl: deinitDecl).asDecl
342+
case .enumCase(let caseDecl):
343+
abiDecl = self.generate(enumCaseDecl: caseDecl).asDecl
344+
case .function(let funcDecl):
345+
abiDecl = self.generate(functionDecl: funcDecl)?.asDecl
346+
case .initializer(let initDecl):
347+
abiDecl = self.generate(initializerDecl: initDecl).asDecl
348+
case .`subscript`(let subscriptDecl):
349+
abiDecl = self.generate(subscriptDecl: subscriptDecl).asDecl
350+
case .typeAlias(let typealiasDecl):
351+
abiDecl = self.generate(typeAliasDecl: typealiasDecl)?.asDecl
352+
case .variable(let varDecl):
353+
abiDecl = self.generate(variableDecl: varDecl).asDecl
354+
case .missing(_):
355+
// This error condition will have been diagnosed in SwiftSyntax.
356+
abiDecl = nil
357+
}
358+
359+
// TODO: Diagnose if `abiDecl` has a body/initial value/etc.
360+
// The C++ parser considers it syntactically invalid but SwiftSyntax does not.
361+
362+
return .createParsed(
363+
self.ctx,
364+
atLoc: self.generateSourceLoc(node.atSign),
365+
range: self.generateAttrSourceRange(node),
366+
abiDecl: abiDecl.asNullable
367+
)
331368
}
332369

333370
/// E.g.:

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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)