Skip to content

Commit 214315b

Browse files
committed
[ASTGen] Generate several type attributes
Also rework existing type attribute generate functino to use standard attribute generation facilities.
1 parent 44c1bf7 commit 214315b

File tree

5 files changed

+154
-96
lines changed

5 files changed

+154
-96
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,26 +2262,37 @@ BridgedConventionTypeAttr BridgedConventionTypeAttr_createParsed(
22622262
BridgedSourceLoc cNameLoc, BridgedDeclNameRef cWitnessMethodProtocol,
22632263
BridgedStringRef cClangType, BridgedSourceLoc cClangTypeLoc);
22642264

2265-
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
2266-
"isolationKindLoc:isolationKind:rpLoc:)")
2267-
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
2268-
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2269-
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
2270-
BridgedSourceLoc cIsolationLoc,
2271-
BridgedIsolatedTypeAttrIsolationKind cIsolation, BridgedSourceLoc cRPLoc);
2272-
22732265
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedExecutionTypeAttrExecutionKind {
22742266
BridgedExecutionTypeAttrExecutionKind_Concurrent,
22752267
BridgedExecutionTypeAttrExecutionKind_Caller
22762268
};
22772269

2278-
SWIFT_NAME("BridgedExecutionTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
2279-
"behaviorLoc:behavior:rpLoc:)")
2270+
SWIFT_NAME("BridgedExecutionTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
2271+
"behavior:behaviorLoc:)")
22802272
BridgedExecutionTypeAttr BridgedExecutionTypeAttr_createParsed(
22812273
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2282-
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
2283-
BridgedSourceLoc cBehaviorLoc,
2284-
BridgedExecutionTypeAttrExecutionKind behavior, BridgedSourceLoc cRPLoc);
2274+
BridgedSourceLoc cNameLoc, BridgedSourceRange cParensRange,
2275+
BridgedExecutionTypeAttrExecutionKind behavior,
2276+
BridgedSourceLoc cBehaviorLoc);
2277+
2278+
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
2279+
"isolationKind:isolationKindLoc:)")
2280+
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
2281+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2282+
BridgedSourceLoc cNameLoc, BridgedSourceRange cParensRange,
2283+
2284+
BridgedIsolatedTypeAttrIsolationKind cIsolation,
2285+
BridgedSourceLoc cIsolationLoc);
2286+
2287+
SWIFT_NAME("BridgedOpaqueReturnTypeOfTypeAttr.createParsed(_:atLoc:nameLoc:"
2288+
"parensRange:"
2289+
"mangled:mangledLoc:index:indexLoc:)")
2290+
BridgedOpaqueReturnTypeOfTypeAttr
2291+
BridgedOpaqueReturnTypeOfTypeAttr_createParsed(
2292+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2293+
BridgedSourceLoc cKwLoc, BridgedSourceRange cParens,
2294+
BridgedStringRef cMangled, BridgedSourceLoc cMangledDoc, size_t index,
2295+
BridgedSourceLoc cIndexLoc);
22852296

22862297
//===----------------------------------------------------------------------===//
22872298
// MARK: TypeReprs

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,13 +2037,14 @@ namespace {
20372037
void visitTypeAliasDecl(TypeAliasDecl *TAD, Label label) {
20382038
printCommon(TAD, "typealias", label);
20392039

2040+
printWhereRequirements(TAD);
2041+
printAttributes(TAD);
2042+
20402043
if (auto underlying = TAD->getCachedUnderlyingType()) {
20412044
printTypeField(underlying, Label::always("type"));
20422045
} else {
2043-
printFlag("unresolved_type", TypeColor);
2046+
printRec(TAD->getUnderlyingTypeRepr(), Label::always("type_repr"));
20442047
}
2045-
printWhereRequirements(TAD);
2046-
printAttributes(TAD);
20472048

20482049
printFoot();
20492050
}

lib/AST/Bridging/TypeAttributeBridging.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,40 +96,51 @@ BridgedConventionTypeAttr BridgedConventionTypeAttr_createParsed(
9696
{cClangType.unbridged(), cClangTypeLoc.unbridged()});
9797
}
9898

99+
BridgedExecutionTypeAttr BridgedExecutionTypeAttr_createParsed(
100+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
101+
BridgedSourceLoc cNameLoc, BridgedSourceRange cParensRange,
102+
BridgedExecutionTypeAttrExecutionKind behavior,
103+
BridgedSourceLoc cBehaviorLoc) {
104+
auto behaviorKind = [=] {
105+
switch (behavior) {
106+
case BridgedExecutionTypeAttrExecutionKind_Concurrent:
107+
return ExecutionKind::Concurrent;
108+
case BridgedExecutionTypeAttrExecutionKind_Caller:
109+
return ExecutionKind::Caller;
110+
}
111+
llvm_unreachable("bad kind");
112+
}();
113+
return new (cContext.unbridged()) ExecutionTypeAttr(
114+
cAtLoc.unbridged(), cNameLoc.unbridged(), cParensRange.unbridged(),
115+
{behaviorKind, cBehaviorLoc.unbridged()});
116+
}
117+
99118
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
100119
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
101-
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
102-
BridgedSourceLoc cIsolationLoc,
103-
BridgedIsolatedTypeAttrIsolationKind cIsolation, BridgedSourceLoc cRPLoc) {
120+
BridgedSourceLoc cNameLoc, BridgedSourceRange cParensRange,
121+
122+
BridgedIsolatedTypeAttrIsolationKind cIsolation,
123+
BridgedSourceLoc cIsolationLoc) {
104124
auto isolationKind = [=] {
105125
switch (cIsolation) {
106126
case BridgedIsolatedTypeAttrIsolationKind_DynamicIsolation:
107127
return IsolatedTypeAttr::IsolationKind::Dynamic;
108128
}
109129
llvm_unreachable("bad kind");
110130
}();
111-
return new (cContext.unbridged())
112-
IsolatedTypeAttr(cAtLoc.unbridged(), cNameLoc.unbridged(),
113-
{cLPLoc.unbridged(), cRPLoc.unbridged()},
114-
{isolationKind, cIsolationLoc.unbridged()});
131+
return new (cContext.unbridged()) IsolatedTypeAttr(
132+
cAtLoc.unbridged(), cNameLoc.unbridged(), cParensRange.unbridged(),
133+
{isolationKind, cIsolationLoc.unbridged()});
115134
}
116135

117-
BridgedExecutionTypeAttr BridgedExecutionTypeAttr_createParsed(
136+
BridgedOpaqueReturnTypeOfTypeAttr
137+
BridgedOpaqueReturnTypeOfTypeAttr_createParsed(
118138
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
119-
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
120-
BridgedSourceLoc cBehaviorLoc,
121-
BridgedExecutionTypeAttrExecutionKind behavior, BridgedSourceLoc cRPLoc) {
122-
auto behaviorKind = [=] {
123-
switch (behavior) {
124-
case BridgedExecutionTypeAttrExecutionKind_Concurrent:
125-
return ExecutionKind::Concurrent;
126-
case BridgedExecutionTypeAttrExecutionKind_Caller:
127-
return ExecutionKind::Caller;
128-
}
129-
llvm_unreachable("bad kind");
130-
}();
131-
return new (cContext.unbridged())
132-
ExecutionTypeAttr(cAtLoc.unbridged(), cNameLoc.unbridged(),
133-
{cLPLoc.unbridged(), cRPLoc.unbridged()},
134-
{behaviorKind, cBehaviorLoc.unbridged()});
139+
BridgedSourceLoc cKwLoc, BridgedSourceRange cParens,
140+
BridgedStringRef cMangled, BridgedSourceLoc cMangledLoc, size_t index,
141+
BridgedSourceLoc cIndexLoc) {
142+
return new (cContext.unbridged()) OpaqueReturnTypeOfTypeAttr(
143+
cAtLoc.unbridged(), cKwLoc.unbridged(), cParens.unbridged(),
144+
{cMangled.unbridged(), cMangledLoc.unbridged()},
145+
{static_cast<unsigned int>(index), cIndexLoc.unbridged()});
135146
}

lib/ASTGen/Sources/ASTGen/TypeAttrs.swift

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,17 @@ extension ASTGenVisitor {
7777
.unimplementable:
7878
return self.generateSimpleTypeAttr(attribute: node, kind: attrKind)
7979

80-
case .opened:
81-
fatalError("unimplemented")
82-
case .packElement:
83-
fatalError("unimplemented")
84-
case .differentiable:
85-
fatalError("unimplemented")
8680
case .convention:
8781
return self.generateConventionTypeAttr(attribute: node)?.asTypeAttribute
82+
case .differentiable:
8883
fatalError("unimplemented")
84+
case .execution:
85+
return self.generateExecutionTypeAttr(attribute: node)?.asTypeAttribute
8986
case .opaqueReturnTypeOf:
90-
fatalError("unimplemented")
91-
87+
return self.generateOpaqueReturnTypeOfTypeAttr(attribute: node)?.asTypeAttribute
9288
case .isolated:
9389
return self.generateIsolatedTypeAttr(attribute: node)?.asTypeAttribute
9490

95-
case .execution:
96-
return self.generateExecutionTypeAttr(attribute: node)?.asTypeAttribute
97-
9891
// SIL type attributes are not supported.
9992
case .autoreleased,
10093
.blockStorage,
@@ -116,8 +109,10 @@ extension ASTGenVisitor {
116109
.inoutAliasable,
117110
.moveOnly,
118111
.objCMetatype,
112+
.opened,
119113
.out,
120114
.owned,
115+
.packElement,
121116
.silIsolated,
122117
.silUnmanaged,
123118
.silUnowned,
@@ -148,7 +143,7 @@ extension ASTGenVisitor {
148143
}
149144

150145
func generateConventionTypeAttr(attribute node: AttributeSyntax) -> BridgedConventionTypeAttr? {
151-
// FIXME: This don't need custom attribute arguments syntax.
146+
// FIXME: This doesn't need custom attribute arguments syntax.
152147
// FIXME: Support 'witness_method' argument.
153148
guard let args = node.arguments?.as(ConventionAttributeArgumentsSyntax.self) else {
154149
// TODO: Diangose.
@@ -171,7 +166,7 @@ extension ASTGenVisitor {
171166
self.ctx,
172167
atLoc: self.generateSourceLoc(node.atSign),
173168
nameLoc: self.generateSourceLoc(node.attributeName),
174-
parensRange: self.generateSourceRange(start: node.leftParen!, end: node.rightParen!),
169+
parensRange: self.generateAttrParensRange(attribute: node),
175170
name: ctx.allocateCopy(string: args.conventionLabel.rawText.bridged),
176171
nameLoc: self.generateSourceLoc(args.conventionLabel),
177172
witnessMethodProtocol: witnessMethodProtocol,
@@ -180,67 +175,96 @@ extension ASTGenVisitor {
180175
)
181176
}
182177

183-
func generateIsolatedTypeAttr(attribute node: AttributeSyntax) -> BridgedIsolatedTypeAttr? {
184-
guard case .argumentList(let isolatedArgs) = node.arguments,
185-
isolatedArgs.count == 1,
186-
let labelArg = isolatedArgs.first,
187-
labelArg.label == nil,
188-
let isolationKindExpr = labelArg.expression.as(DeclReferenceExprSyntax.self),
189-
isolationKindExpr.argumentNames == nil
190-
else {
191-
// TODO: Diagnose.
178+
func generateExecutionTypeAttr(attribute node: AttributeSyntax) -> BridgedExecutionTypeAttr? {
179+
let behaviorLoc = self.generateSourceLoc(node.arguments)
180+
let behavior: BridgedExecutionTypeAttrExecutionKind? = self.generateSingleAttrOption(
181+
attribute: node,
182+
{
183+
switch $0.rawText {
184+
case "concurrent": return .concurrent
185+
case "caller": return .caller
186+
default:
187+
// TODO: Diagnose.
188+
return nil
189+
}
190+
}
191+
)
192+
guard let behavior else {
192193
return nil
193194
}
195+
196+
return .createParsed(
197+
self.ctx,
198+
atLoc: self.generateSourceLoc(node.atSign),
199+
nameLoc: self.generateSourceLoc(node.attributeName),
200+
parensRange: self.generateAttrParensRange(attribute: node),
201+
behavior: behavior,
202+
behaviorLoc: behaviorLoc
203+
)
204+
}
194205

195-
196-
var isolationKind: BridgedIsolatedTypeAttrIsolationKind
197-
switch isolationKindExpr.baseName {
198-
case "any": isolationKind = .dynamicIsolation
199-
default:
200-
// TODO: Diagnose.
206+
func generateIsolatedTypeAttr(attribute node: AttributeSyntax) -> BridgedIsolatedTypeAttr? {
207+
let isolationKindLoc = self.generateSourceLoc(node.arguments)
208+
let isolationKind: BridgedIsolatedTypeAttrIsolationKind? = self.generateSingleAttrOption(
209+
attribute: node,
210+
{
211+
switch $0.rawText {
212+
case "any": return .dynamicIsolation
213+
default:
214+
// TODO: Diagnose.
215+
return nil
216+
}
217+
}
218+
)
219+
guard let isolationKind else {
201220
return nil
202221
}
203222

204-
return BridgedIsolatedTypeAttr.createParsed(
223+
return .createParsed(
205224
self.ctx,
206225
atLoc: self.generateSourceLoc(node.atSign),
207226
nameLoc: self.generateSourceLoc(node.attributeName),
208-
lpLoc: self.generateSourceLoc(node.leftParen!),
209-
isolationKindLoc: self.generateSourceLoc(isolationKindExpr.baseName),
227+
parensRange: self.generateAttrParensRange(attribute: node),
210228
isolationKind: isolationKind,
211-
rpLoc: self.generateSourceLoc(node.rightParen!)
229+
isolationKindLoc: isolationKindLoc
212230
)
213231
}
214232

215-
func generateExecutionTypeAttr(attribute node: AttributeSyntax) -> BridgedExecutionTypeAttr? {
216-
guard case .argumentList(let executionArgs) = node.arguments,
217-
executionArgs.count == 1,
218-
let labelArg = executionArgs.first,
219-
labelArg.label == nil,
220-
let behaviorExpr = labelArg.expression.as(DeclReferenceExprSyntax.self),
221-
behaviorExpr.argumentNames == nil
222-
else {
223-
// TODO: Diagnose.
224-
return nil
233+
func generateOpaqueReturnTypeOfTypeAttr(attribute node: AttributeSyntax) -> BridgedOpaqueReturnTypeOfTypeAttr? {
234+
// FIXME: This doesn't need custom attribute arguments syntax.
235+
guard let args = node.arguments?.as(OpaqueReturnTypeOfAttributeArgumentsSyntax.self) else {
236+
// TODO: Diagnose
237+
fatalError("expected arguments for @_opaqueReturnTypeOfType type attribute")
225238
}
226239

227-
var behavior: BridgedExecutionTypeAttrExecutionKind
228-
switch behaviorExpr.baseName {
229-
case "concurrent": behavior = .concurrent
230-
case "caller": behavior = .caller
231-
default:
232-
// TODO: Diagnose.
233-
return nil
240+
let mangledLoc = self.generateSourceLoc(args.mangledName)
241+
guard let mangled = self.generateStringLiteralTextIfNotInterpolated(expr: args.mangledName) else {
242+
// TODO: Diagnose
243+
fatalError("expected string literal for @_opaqueReturnTypeOfType type attribute")
244+
}
245+
246+
let indexLoc = self.generateSourceLoc(args.ordinal)
247+
let index = Int(args.ordinal.text, radix: 10)
248+
guard let index else {
249+
// TODO: Diagnose
250+
fatalError("expected integer literal for @_opaqueReturnTypeOfType type attribute")
234251
}
235252

236-
return BridgedExecutionTypeAttr.createParsed(
253+
return .createParsed(
237254
self.ctx,
238255
atLoc: self.generateSourceLoc(node.atSign),
239256
nameLoc: self.generateSourceLoc(node.attributeName),
240-
lpLoc: self.generateSourceLoc(node.leftParen!),
241-
behaviorLoc: self.generateSourceLoc(behaviorExpr.baseName),
242-
behavior: behavior,
243-
rpLoc: self.generateSourceLoc(node.rightParen!)
257+
parensRange: self.generateAttrParensRange(attribute: node),
258+
mangled: mangled,
259+
mangledLoc: mangledLoc,
260+
index: index, indexLoc: indexLoc
244261
)
245262
}
263+
264+
func generateAttrParensRange(attribute node: AttributeSyntax) -> BridgedSourceRange {
265+
guard let lParen = node.leftParen else {
266+
return BridgedSourceRange()
267+
}
268+
return self.generateSourceRange(start: lParen, end: node.lastToken(viewMode: .sourceAccurate)!)
269+
}
246270
}

test/ASTGen/attrs.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast
2121

2222
// RUN: %target-typecheck-verify-swift \
23+
// RUN: -module-abi-name ASTGen \
24+
// RUN: -enable-experimental-feature ParserASTGen \
2325
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
2426
// RUN: -enable-experimental-feature ABIAttribute \
2527
// RUN: -enable-experimental-feature Extern \
2628
// RUN: -enable-experimental-move-only \
27-
// RUN: -enable-experimental-feature ParserASTGen \
2829
// RUN: -enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext
2930

3031
// REQUIRES: executable_test
@@ -190,4 +191,14 @@ do {
190191
}
191192
}
192193

193-
func testConvention(fn: @convention(c) (Int) -> Int) {}
194+
typealias testConvention = @convention(c) (Int) -> Int
195+
typealias testExecution = @execution(concurrent) () async -> Void
196+
typealias testIsolated = @isolated(any) () -> Void
197+
198+
protocol OpProto {}
199+
struct OpStruct: OpProto {}
200+
struct OpTest {
201+
func opResult() -> some OpProto { OpStruct() }
202+
// FIXME: Implement SF->addUnvalidatedDeclWithOpaqueResultType() in ASTGen
203+
// typealias Result = @_opaqueReturnTypeOf("$s6ASTGen6OpTestV8opResultQryF", 0) __
204+
}

0 commit comments

Comments
 (0)