Skip to content

Commit 10aac85

Browse files
authored
Merge pull request swiftlang#79379 from rintaro/astgen-conventiontypeattr
[ASTGen] Generate ConventionTypeAttr
2 parents 9229b07 + f08bf56 commit 10aac85

File tree

5 files changed

+107
-22
lines changed

5 files changed

+107
-22
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,15 @@ struct BridgedASTNode {
444444
Bridged##CLASS##Attr attr);
445445
#include "swift/AST/DeclAttr.def"
446446

447+
// Declare `.asTypeAttr` on each BridgedXXXTypeAttr type, which upcasts a
448+
// wrapper for a TypeAttr subclass to a BridgedTypeAttr.
449+
#define SIMPLE_TYPE_ATTR(...)
450+
#define TYPE_ATTR(SPELLING, CLASS) \
451+
SWIFT_NAME("getter:Bridged" #CLASS "TypeAttr.asTypeAttribute(self:)") \
452+
BridgedTypeAttribute Bridged##CLASS##TypeAttr_asTypeAttribute( \
453+
Bridged##CLASS##TypeAttr attr);
454+
#include "swift/AST/TypeAttr.def"
455+
447456
struct BridgedPatternBindingEntry {
448457
BridgedPattern pattern;
449458
BridgedSourceLoc equalLoc;
@@ -2214,23 +2223,33 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedIsolatedTypeAttrIsolationKind {
22142223
BridgedIsolatedTypeAttrIsolationKind_DynamicIsolation,
22152224
};
22162225

2217-
SWIFT_NAME("BridgedTypeAttribute.createIsolated(_:atLoc:nameLoc:lpLoc:isolationKindLoc:isolationKind:rpLoc:)")
2218-
BridgedTypeAttribute BridgedTypeAttribute_createIsolated(
2219-
BridgedASTContext cContext,
2220-
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc,
2221-
BridgedSourceLoc cLPLoc, BridgedSourceLoc cIsolationLoc,
2226+
SWIFT_NAME("BridgedConventionTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
2227+
"name:nameLoc:witnessMethodProtocol:clangType:clangTypeLoc:)")
2228+
BridgedConventionTypeAttr BridgedConventionTypeAttr_createParsed(
2229+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2230+
BridgedSourceLoc cKwLoc, BridgedSourceRange cParens, BridgedStringRef cName,
2231+
BridgedSourceLoc cNameLoc, BridgedDeclNameRef cWitnessMethodProtocol,
2232+
BridgedStringRef cClangType, BridgedSourceLoc cClangTypeLoc);
2233+
2234+
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
2235+
"isolationKindLoc:isolationKind:rpLoc:)")
2236+
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
2237+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2238+
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
2239+
BridgedSourceLoc cIsolationLoc,
22222240
BridgedIsolatedTypeAttrIsolationKind cIsolation, BridgedSourceLoc cRPLoc);
22232241

22242242
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedExecutionTypeAttrExecutionKind {
22252243
BridgedExecutionTypeAttrExecutionKind_Concurrent,
22262244
BridgedExecutionTypeAttrExecutionKind_Caller
22272245
};
22282246

2229-
SWIFT_NAME("BridgedTypeAttribute.createExecution(_:atLoc:nameLoc:lpLoc:behaviorLoc:behavior:rpLoc:)")
2230-
BridgedTypeAttribute BridgedTypeAttribute_createExecution(
2231-
BridgedASTContext cContext,
2232-
BridgedSourceLoc cAtLoc, BridgedSourceLoc cNameLoc,
2233-
BridgedSourceLoc cLPLoc, BridgedSourceLoc cBehaviorLoc,
2247+
SWIFT_NAME("BridgedExecutionTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
2248+
"behaviorLoc:behavior:rpLoc:)")
2249+
BridgedExecutionTypeAttr BridgedExecutionTypeAttr_createParsed(
2250+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2251+
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
2252+
BridgedSourceLoc cBehaviorLoc,
22342253
BridgedExecutionTypeAttrExecutionKind behavior, BridgedSourceLoc cRPLoc);
22352254

22362255
//===----------------------------------------------------------------------===//

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
#endif
8686
#include "swift/AST/DeclAttr.def"
8787

88+
#ifndef TYPE_ATTR
89+
#define SIMPLE_TYPE_ATTR(...)
90+
#define TYPE_ATTR(SPELLING, CLASS) AST_BRIDGING_WRAPPER_NONNULL(CLASS##TypeAttr)
91+
#endif
92+
#include "swift/AST/TypeAttr.def"
93+
8894
// Some of the base classes need to be nullable to allow them to be used as
8995
// optional parameters.
9096
AST_BRIDGING_WRAPPER_NULLABLE(Decl)

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ using namespace swift;
2323
// MARK: TypeAttributes
2424
//===----------------------------------------------------------------------===//
2525

26+
// Define `.asTypeAttr` on each BridgedXXXTypeAttr type.
27+
#define SIMPLE_TYPE_ATTR(...)
28+
#define TYPE_ATTR(SPELLING, CLASS) \
29+
SWIFT_NAME("getter:Bridged" #CLASS "TypeAttr.asTypeAttribute(self:)") \
30+
BridgedTypeAttribute Bridged##CLASS##TypeAttr_asTypeAttribute( \
31+
Bridged##CLASS##TypeAttr attr) { \
32+
return attr.unbridged(); \
33+
}
34+
#include "swift/AST/TypeAttr.def"
35+
2636
BridgedTypeAttrKind BridgedTypeAttrKind_fromString(BridgedStringRef cStr) {
2737
auto optKind = TypeAttribute::getAttrKindFromString(cStr.unbridged());
2838
if (!optKind)
@@ -74,7 +84,19 @@ BridgedTypeAttribute BridgedTypeAttribute_createSimple(
7484
cAtLoc.unbridged(), cNameLoc.unbridged());
7585
}
7686

77-
BridgedTypeAttribute BridgedTypeAttribute_createIsolated(
87+
BridgedConventionTypeAttr BridgedConventionTypeAttr_createParsed(
88+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
89+
BridgedSourceLoc cKwLoc, BridgedSourceRange cParens, BridgedStringRef cName,
90+
BridgedSourceLoc cNameLoc, BridgedDeclNameRef cWitnessMethodProtocol,
91+
BridgedStringRef cClangType, BridgedSourceLoc cClangTypeLoc) {
92+
return new (cContext.unbridged()) ConventionTypeAttr(
93+
cAtLoc.unbridged(), cKwLoc.unbridged(), cParens.unbridged(),
94+
{cName.unbridged(), cNameLoc.unbridged()},
95+
cWitnessMethodProtocol.unbridged(),
96+
{cClangType.unbridged(), cClangTypeLoc.unbridged()});
97+
}
98+
99+
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
78100
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
79101
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
80102
BridgedSourceLoc cIsolationLoc,
@@ -92,7 +114,7 @@ BridgedTypeAttribute BridgedTypeAttribute_createIsolated(
92114
{isolationKind, cIsolationLoc.unbridged()});
93115
}
94116

95-
BridgedTypeAttribute BridgedTypeAttribute_createExecution(
117+
BridgedExecutionTypeAttr BridgedExecutionTypeAttr_createParsed(
96118
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
97119
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
98120
BridgedSourceLoc cBehaviorLoc,
@@ -110,4 +132,4 @@ BridgedTypeAttribute BridgedTypeAttribute_createExecution(
110132
ExecutionTypeAttr(cAtLoc.unbridged(), cNameLoc.unbridged(),
111133
{cLPLoc.unbridged(), cRPLoc.unbridged()},
112134
{behaviorKind, cBehaviorLoc.unbridged()});
113-
}
135+
}

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ extension ASTGenVisitor {
8484
case .differentiable:
8585
fatalError("unimplemented")
8686
case .convention:
87+
return self.generateConventionTypeAttr(attribute: node)?.asTypeAttribute
8788
fatalError("unimplemented")
8889
case .opaqueReturnTypeOf:
8990
fatalError("unimplemented")
9091

9192
case .isolated:
92-
return self.generateIsolatedTypeAttr(attribute: node)
93+
return self.generateIsolatedTypeAttr(attribute: node)?.asTypeAttribute
9394

9495
case .execution:
95-
return self.generateExecutionTypeAttr(attribute: node)
96+
return self.generateExecutionTypeAttr(attribute: node)?.asTypeAttribute
9697

9798
// SIL type attributes are not supported.
9899
case .autoreleased,
@@ -145,8 +146,41 @@ extension ASTGenVisitor {
145146
nameLoc: self.generateSourceLoc(node.attributeName)
146147
)
147148
}
148-
149-
func generateIsolatedTypeAttr(attribute node: AttributeSyntax) -> BridgedTypeAttribute? {
149+
150+
func generateConventionTypeAttr(attribute node: AttributeSyntax) -> BridgedConventionTypeAttr? {
151+
// FIXME: This don't need custom attribute arguments syntax.
152+
// FIXME: Support 'witness_method' argument.
153+
guard let args = node.arguments?.as(ConventionAttributeArgumentsSyntax.self) else {
154+
// TODO: Diangose.
155+
return nil
156+
}
157+
158+
let cTypeName: BridgedStringRef?
159+
let cTypeNameLoc: BridgedSourceLoc?
160+
if let ctypeString = args.cTypeString {
161+
cTypeName = self.generateStringLiteralTextIfNotInterpolated(expr: ctypeString)
162+
cTypeNameLoc = cTypeName != nil ? self.generateSourceLoc(ctypeString) : nil
163+
} else {
164+
cTypeName = nil
165+
cTypeNameLoc = nil
166+
}
167+
168+
let witnessMethodProtocol: BridgedDeclNameRef = BridgedDeclNameRef()
169+
170+
return .createParsed(
171+
self.ctx,
172+
atLoc: self.generateSourceLoc(node.atSign),
173+
nameLoc: self.generateSourceLoc(node.attributeName),
174+
parensRange: self.generateSourceRange(start: node.leftParen!, end: node.rightParen!),
175+
name: ctx.allocateCopy(string: args.conventionLabel.rawText.bridged),
176+
nameLoc: self.generateSourceLoc(args.conventionLabel),
177+
witnessMethodProtocol: witnessMethodProtocol,
178+
clangType: cTypeName ?? BridgedStringRef(),
179+
clangTypeLoc: cTypeNameLoc ?? BridgedSourceLoc()
180+
)
181+
}
182+
183+
func generateIsolatedTypeAttr(attribute node: AttributeSyntax) -> BridgedIsolatedTypeAttr? {
150184
guard case .argumentList(let isolatedArgs) = node.arguments,
151185
isolatedArgs.count == 1,
152186
let labelArg = isolatedArgs.first,
@@ -167,17 +201,18 @@ extension ASTGenVisitor {
167201
return nil
168202
}
169203

170-
return BridgedTypeAttribute.createIsolated(
204+
return BridgedIsolatedTypeAttr.createParsed(
171205
self.ctx,
172206
atLoc: self.generateSourceLoc(node.atSign),
173207
nameLoc: self.generateSourceLoc(node.attributeName),
174208
lpLoc: self.generateSourceLoc(node.leftParen!),
175209
isolationKindLoc: self.generateSourceLoc(isolationKindExpr.baseName),
176210
isolationKind: isolationKind,
177-
rpLoc: self.generateSourceLoc(node.rightParen!))
211+
rpLoc: self.generateSourceLoc(node.rightParen!)
212+
)
178213
}
179214

180-
func generateExecutionTypeAttr(attribute node: AttributeSyntax) -> BridgedTypeAttribute? {
215+
func generateExecutionTypeAttr(attribute node: AttributeSyntax) -> BridgedExecutionTypeAttr? {
181216
guard case .argumentList(let executionArgs) = node.arguments,
182217
executionArgs.count == 1,
183218
let labelArg = executionArgs.first,
@@ -198,13 +233,14 @@ extension ASTGenVisitor {
198233
return nil
199234
}
200235

201-
return BridgedTypeAttribute.createExecution(
236+
return BridgedExecutionTypeAttr.createParsed(
202237
self.ctx,
203238
atLoc: self.generateSourceLoc(node.atSign),
204239
nameLoc: self.generateSourceLoc(node.attributeName),
205240
lpLoc: self.generateSourceLoc(node.leftParen!),
206241
behaviorLoc: self.generateSourceLoc(behaviorExpr.baseName),
207242
behavior: behavior,
208-
rpLoc: self.generateSourceLoc(node.rightParen!))
243+
rpLoc: self.generateSourceLoc(node.rightParen!)
244+
)
209245
}
210246
}

test/ASTGen/attrs.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,5 @@ do {
190190
@execution(concurrent) func testMember() async {} // Ok
191191
}
192192
}
193+
194+
func testConvention(fn: @convention(c) (Int) -> Int) {}

0 commit comments

Comments
 (0)