Skip to content

Commit f08bf56

Browse files
committed
[ASTGen] Generate ConventionTypeAttr
E.g. `@convention(c)`
1 parent e1dfed3 commit f08bf56

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,14 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedIsolatedTypeAttrIsolationKind {
22092209
BridgedIsolatedTypeAttrIsolationKind_DynamicIsolation,
22102210
};
22112211

2212+
SWIFT_NAME("BridgedConventionTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
2213+
"name:nameLoc:witnessMethodProtocol:clangType:clangTypeLoc:)")
2214+
BridgedConventionTypeAttr BridgedConventionTypeAttr_createParsed(
2215+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2216+
BridgedSourceLoc cKwLoc, BridgedSourceRange cParens, BridgedStringRef cName,
2217+
BridgedSourceLoc cNameLoc, BridgedDeclNameRef cWitnessMethodProtocol,
2218+
BridgedStringRef cClangType, BridgedSourceLoc cClangTypeLoc);
2219+
22122220
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
22132221
"isolationKindLoc:isolationKind:rpLoc:)")
22142222
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ BridgedTypeAttribute BridgedTypeAttribute_createSimple(
8484
cAtLoc.unbridged(), cNameLoc.unbridged());
8585
}
8686

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+
8799
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
88100
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
89101
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ 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")
@@ -146,6 +147,39 @@ extension ASTGenVisitor {
146147
)
147148
}
148149

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+
149183
func generateIsolatedTypeAttr(attribute node: AttributeSyntax) -> BridgedIsolatedTypeAttr? {
150184
guard case .argumentList(let isolatedArgs) = node.arguments,
151185
isolatedArgs.count == 1,

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)