Skip to content

Commit f206956

Browse files
committed
[AST] Add @execution(concurrent | caller) type attribute
1 parent 01e3bd0 commit f206956

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,18 @@ BridgedTypeAttribute BridgedTypeAttribute_createIsolated(
19841984
BridgedSourceLoc cLPLoc, BridgedSourceLoc cIsolationLoc,
19851985
BridgedIsolatedTypeAttrIsolationKind cIsolation, BridgedSourceLoc cRPLoc);
19861986

1987+
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedExecutionTypeAttrExecutionKind {
1988+
BridgedExecutionTypeAttrExecutionKind_Concurrent,
1989+
BridgedExecutionTypeAttrExecutionKind_Caller
1990+
};
1991+
1992+
SWIFT_NAME("BridgedTypeAttribute.createExecution(_:atLoc:nameLoc:lpLoc:behaviorLoc:behavior:rpLoc:)")
1993+
BridgedTypeAttribute BridgedTypeAttribute_createExecution(
1994+
BridgedASTContext cContext,
1995+
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc,
1996+
BridgedSourceLoc cLPLoc, BridgedSourceLoc cBehaviorLoc,
1997+
BridgedExecutionTypeAttrExecutionKind behavior, BridgedSourceLoc cRPLoc);
1998+
19871999
//===----------------------------------------------------------------------===//
19882000
// MARK: TypeReprs
19892001
//===----------------------------------------------------------------------===//

include/swift/AST/Attr.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,10 @@ class alignas(1 << AttrAlignInBits) TypeAttribute
34753475
SWIFT_INLINE_BITFIELD_FULL(IsolatedTypeAttr, TypeAttribute, 8,
34763476
Kind : 8
34773477
);
3478+
3479+
SWIFT_INLINE_BITFIELD_FULL(ExecutionTypeAttr, TypeAttribute, 8,
3480+
Behavior : 8
3481+
);
34783482
} Bits;
34793483
// clang-format on
34803484

@@ -3740,6 +3744,28 @@ class IsolatedTypeAttr : public SimpleTypeAttrWithArgs<TypeAttrKind::Isolated> {
37403744
void printImpl(ASTPrinter &printer, const PrintOptions &options) const;
37413745
};
37423746

3747+
/// The @execution function type attribute.
3748+
class ExecutionTypeAttr : public SimpleTypeAttrWithArgs<TypeAttrKind::Execution> {
3749+
SourceLoc BehaviorLoc;
3750+
3751+
public:
3752+
ExecutionTypeAttr(SourceLoc atLoc, SourceLoc kwLoc, SourceRange parensRange,
3753+
Located<ExecutionKind> behavior)
3754+
: SimpleTypeAttr(atLoc, kwLoc, parensRange), BehaviorLoc(behavior.Loc) {
3755+
Bits.ExecutionTypeAttr.Behavior = uint8_t(behavior.Item);
3756+
}
3757+
3758+
ExecutionKind getBehavior() const {
3759+
return ExecutionKind(Bits.ExecutionTypeAttr.Behavior);
3760+
}
3761+
3762+
SourceLoc getBehaviorLoc() const {
3763+
return BehaviorLoc;
3764+
}
3765+
3766+
void printImpl(ASTPrinter &printer, const PrintOptions &options) const;
3767+
};
3768+
37433769
using TypeOrCustomAttr =
37443770
llvm::PointerUnion<CustomAttr*, TypeAttribute*>;
37453771

include/swift/AST/TypeAttr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ SIMPLE_TYPE_ATTR(_noMetadata, NoMetadata)
6666
TYPE_ATTR(_opaqueReturnTypeOf, OpaqueReturnTypeOf)
6767
TYPE_ATTR(isolated, Isolated)
6868
SIMPLE_TYPE_ATTR(_addressable, Addressable)
69+
TYPE_ATTR(execution, Execution)
6970

7071
// SIL-specific attributes
7172
SIMPLE_SIL_TYPE_ATTR(async, Async)

lib/AST/Attr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,23 @@ void IsolatedTypeAttr::printImpl(ASTPrinter &printer,
303303
printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
304304
}
305305

306+
void ExecutionTypeAttr::printImpl(ASTPrinter &printer,
307+
const PrintOptions &options) const {
308+
printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
309+
printer.printAttrName("@execution");
310+
printer << "(";
311+
switch (getBehavior()) {
312+
case ExecutionKind::Concurrent:
313+
printer << "concurrent";
314+
break;
315+
case ExecutionKind::Caller:
316+
printer << "caller";
317+
break;
318+
}
319+
printer << ")";
320+
printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
321+
}
322+
306323
/// Given a name like "inline", return the decl attribute ID that corresponds
307324
/// to it. Note that this is a many-to-one mapping, and that the identifier
308325
/// passed in may only be the first portion of the attribute (e.g. in the case

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,23 @@ BridgedTypeAttribute BridgedTypeAttribute_createIsolated(
9191
{cLPLoc.unbridged(), cRPLoc.unbridged()},
9292
{isolationKind, cIsolationLoc.unbridged()});
9393
}
94+
95+
BridgedTypeAttribute BridgedTypeAttribute_createExecution(
96+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
97+
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
98+
BridgedSourceLoc cBehaviorLoc,
99+
BridgedExecutionTypeAttrExecutionKind behavior, BridgedSourceLoc cRPLoc) {
100+
auto behaviorKind = [=] {
101+
switch (behavior) {
102+
case BridgedExecutionTypeAttrExecutionKind_Concurrent:
103+
return ExecutionKind::Concurrent;
104+
case BridgedExecutionTypeAttrExecutionKind_Caller:
105+
return ExecutionKind::Caller;
106+
}
107+
llvm_unreachable("bad kind");
108+
}();
109+
return new (cContext.unbridged())
110+
ExecutionTypeAttr(cAtLoc.unbridged(), cNameLoc.unbridged(),
111+
{cLPLoc.unbridged(), cRPLoc.unbridged()},
112+
{behaviorKind, cBehaviorLoc.unbridged()});
113+
}

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ extension ASTGenVisitor {
9191
case .isolated:
9292
return self.generateIsolatedTypeAttr(attribute: node)
9393

94+
case .execution:
95+
return self.generateExecutionTypeAttr(attribute: node)
96+
9497
// SIL type attributes are not supported.
9598
case .autoreleased,
9699
.blockStorage,
@@ -173,4 +176,35 @@ extension ASTGenVisitor {
173176
isolationKind: isolationKind,
174177
rpLoc: self.generateSourceLoc(node.rightParen!))
175178
}
179+
180+
func generateExecutionTypeAttr(attribute node: AttributeSyntax) -> BridgedTypeAttribute? {
181+
guard case .argumentList(let executionArgs) = node.arguments,
182+
executionArgs.count == 1,
183+
let labelArg = executionArgs.first,
184+
labelArg.label == nil,
185+
let behaviorExpr = labelArg.expression.as(DeclReferenceExprSyntax.self),
186+
behaviorExpr.argumentNames == nil
187+
else {
188+
// TODO: Diagnose.
189+
return nil
190+
}
191+
192+
var behavior: BridgedExecutionTypeAttrExecutionKind
193+
switch behaviorExpr.baseName {
194+
case "concurrent": behavior = .concurrent
195+
case "caller": behavior = .caller
196+
default:
197+
// TODO: Diagnose.
198+
return nil
199+
}
200+
201+
return BridgedTypeAttribute.createExecution(
202+
self.ctx,
203+
atLoc: self.generateSourceLoc(node.atSign),
204+
nameLoc: self.generateSourceLoc(node.attributeName),
205+
lpLoc: self.generateSourceLoc(node.leftParen!),
206+
behaviorLoc: self.generateSourceLoc(behaviorExpr.baseName),
207+
behavior: behavior,
208+
rpLoc: self.generateSourceLoc(node.rightParen!))
209+
}
176210
}

0 commit comments

Comments
 (0)