Skip to content

Commit 667a976

Browse files
committed
[ASTGen] Add support for @execution(...) attribute
1 parent 0445e5a commit 667a976

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,16 @@ BridgedABIAttr BridgedABIAttr_createParsed(
601601
BridgedASTContext cContext, BridgedSourceLoc atLoc,
602602
BridgedSourceRange range, BridgedNullableDecl abiDecl);
603603

604+
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedExecutionKind {
605+
BridgedExecutionKindConcurrent,
606+
BridgedExecutionKindCaller,
607+
};
608+
609+
SWIFT_NAME("BridgedExecutionAttr.createParsed(_:atLoc:range:behavior:)")
610+
BridgedExecutionAttr BridgedExecutionAttr_createParsed(
611+
BridgedASTContext cContext, BridgedSourceLoc atLoc,
612+
BridgedSourceRange range, BridgedExecutionKind behavior);
613+
604614
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedAccessLevel {
605615
BridgedAccessLevelPrivate,
606616
BridgedAccessLevelFilePrivate,

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,21 @@ BridgedUnavailableFromAsyncAttr BridgedUnavailableFromAsyncAttr_createParsed(
585585
UnavailableFromAsyncAttr(cMessage.unbridged(), cAtLoc.unbridged(),
586586
cRange.unbridged(), /*implicit=*/false);
587587
}
588+
589+
static ExecutionKind unbridged(BridgedExecutionKind kind) {
590+
switch (kind) {
591+
case BridgedExecutionKindConcurrent:
592+
return ExecutionKind::Concurrent;
593+
case BridgedExecutionKindCaller:
594+
return ExecutionKind::Caller;
595+
}
596+
llvm_unreachable("unhandled enum value");
597+
}
598+
599+
BridgedExecutionAttr BridgedExecutionAttr_createParsed(
600+
BridgedASTContext cContext, BridgedSourceLoc atLoc,
601+
BridgedSourceRange range, BridgedExecutionKind behavior) {
602+
return new (cContext.unbridged())
603+
ExecutionAttr(atLoc.unbridged(), range.unbridged(),
604+
unbridged(behavior), /*implicit=*/false);
605+
}

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ extension ASTGenVisitor {
111111
let attrName = identTy.name.rawText
112112
let attrKind = BridgedDeclAttrKind(from: attrName.bridged)
113113
switch attrKind {
114+
case .execution:
115+
return handle(self.generateExecutionAttr(attribute: node)?.asDeclAttribute)
114116
case .ABI:
115117
return handle(self.generateABIAttr(attribute: node)?.asDeclAttribute)
116118
case .alignment:
@@ -329,6 +331,33 @@ extension ASTGenVisitor {
329331
return handle(self.generateCustomAttr(attribute: node)?.asDeclAttribute)
330332
}
331333

334+
/// E.g.:
335+
/// ```
336+
/// @execution(concurrent)
337+
/// @execution(caller)
338+
/// ```
339+
func generateExecutionAttr(attribute node: AttributeSyntax) -> BridgedExecutionAttr? {
340+
let behavior: BridgedExecutionKind? = self.generateSingleAttrOption(
341+
attribute: node,
342+
{
343+
switch $0.rawText {
344+
case "concurrent": return .concurrent
345+
case "caller": return .caller
346+
default: return nil
347+
}
348+
}
349+
)
350+
guard let behavior else {
351+
return nil
352+
}
353+
return .createParsed(
354+
self.ctx,
355+
atLoc: self.generateSourceLoc(node.atSign),
356+
range: self.generateAttrSourceRange(node),
357+
behavior: behavior
358+
)
359+
}
360+
332361
/// E.g.:
333362
/// ```
334363
/// @abi(func fn())

test/ASTGen/attrs.swift

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
// RUN: %empty-directory(%t)
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
2+
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking \
3+
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
4+
// RUN: -enable-experimental-feature ABIAttribute \
5+
// RUN: -enable-experimental-feature Extern \
6+
// RUN: -enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext \
7+
// RUN: -enable-experimental-move-only \
8+
// RUN: -enable-experimental-feature ParserASTGen > %t/astgen.ast.raw
9+
10+
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking \
11+
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
12+
// RUN: -enable-experimental-feature ABIAttribute \
13+
// RUN: -enable-experimental-feature Extern \
14+
// RUN: -enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext \
15+
// RUN: -enable-experimental-move-only > %t/cpp-parser.ast.raw
416

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

921
// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast
1022

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
23+
// RUN: %target-typecheck-verify-swift \
24+
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
25+
// RUN: -enable-experimental-feature ABIAttribute \
26+
// RUN: -enable-experimental-feature Extern \
27+
// RUN: -enable-experimental-move-only \
28+
// RUN: -enable-experimental-feature ParserASTGen \
29+
// RUN: -enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext
1230

1331
// REQUIRES: executable_test
1432
// REQUIRES: swift_swift_parser
1533
// REQUIRES: swift_feature_SymbolLinkageMarkers
1634
// REQUIRES: swift_feature_Extern
1735
// REQUIRES: swift_feature_ParserASTGen
1836
// REQUIRES: swift_feature_ABIAttribute
37+
// REQUIRES: swift_feature_NonIsolatedAsyncInheritsIsolationFromContext
1938

2039
// rdar://116686158
2140
// UNSUPPORTED: asan
@@ -26,7 +45,7 @@ struct S1 {
2645

2746
func testStatic() {
2847
// static.
29-
S1.staticMethod()
48+
S1.staticMethod()
3049
S1().staticMethod() // expected-error {{static member 'staticMethod' cannot be used on instance of type 'S1'}}
3150
}
3251

@@ -160,3 +179,14 @@ struct StorageRestrctionTest {
160179

161180
@_unavailableFromAsync struct UnavailFromAsyncStruct { } // expected-error {{'@_unavailableFromAsync' attribute cannot be applied to this declaration}}
162181
@_unavailableFromAsync(message: "foo bar") func UnavailFromAsyncFn() {}
182+
183+
@execution(concurrent) func testGlobal() async { // Ok
184+
}
185+
186+
do {
187+
@execution(caller) func testLocal() async {} // Ok
188+
189+
struct Test {
190+
@execution(concurrent) func testMember() async {} // Ok
191+
}
192+
}

0 commit comments

Comments
 (0)