Skip to content

Commit 6d85bb5

Browse files
authored
Merge pull request #79412 from rintaro/astgen-typeattrs
[ASTGen] Generate OpaqueReturnTypeOfTypeAttr
2 parents 20a6f2c + 6184367 commit 6d85bb5

File tree

8 files changed

+242
-123
lines changed

8 files changed

+242
-123
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,26 +2299,37 @@ BridgedConventionTypeAttr BridgedConventionTypeAttr_createParsed(
22992299
BridgedSourceLoc cNameLoc, BridgedDeclNameRef cWitnessMethodProtocol,
23002300
BridgedStringRef cClangType, BridgedSourceLoc cClangTypeLoc);
23012301

2302-
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
2303-
"isolationKindLoc:isolationKind:rpLoc:)")
2304-
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
2305-
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2306-
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
2307-
BridgedSourceLoc cIsolationLoc,
2308-
BridgedIsolatedTypeAttrIsolationKind cIsolation, BridgedSourceLoc cRPLoc);
2309-
23102302
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedExecutionTypeAttrExecutionKind {
23112303
BridgedExecutionTypeAttrExecutionKind_Concurrent,
23122304
BridgedExecutionTypeAttrExecutionKind_Caller
23132305
};
23142306

2315-
SWIFT_NAME("BridgedExecutionTypeAttr.createParsed(_:atLoc:nameLoc:lpLoc:"
2316-
"behaviorLoc:behavior:rpLoc:)")
2307+
SWIFT_NAME("BridgedExecutionTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
2308+
"behavior:behaviorLoc:)")
23172309
BridgedExecutionTypeAttr BridgedExecutionTypeAttr_createParsed(
23182310
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2319-
BridgedSourceLoc cNameLoc, BridgedSourceLoc cLPLoc,
2320-
BridgedSourceLoc cBehaviorLoc,
2321-
BridgedExecutionTypeAttrExecutionKind behavior, BridgedSourceLoc cRPLoc);
2311+
BridgedSourceLoc cNameLoc, BridgedSourceRange cParensRange,
2312+
BridgedExecutionTypeAttrExecutionKind behavior,
2313+
BridgedSourceLoc cBehaviorLoc);
2314+
2315+
SWIFT_NAME("BridgedIsolatedTypeAttr.createParsed(_:atLoc:nameLoc:parensRange:"
2316+
"isolationKind:isolationKindLoc:)")
2317+
BridgedIsolatedTypeAttr BridgedIsolatedTypeAttr_createParsed(
2318+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2319+
BridgedSourceLoc cNameLoc, BridgedSourceRange cParensRange,
2320+
2321+
BridgedIsolatedTypeAttrIsolationKind cIsolation,
2322+
BridgedSourceLoc cIsolationLoc);
2323+
2324+
SWIFT_NAME("BridgedOpaqueReturnTypeOfTypeAttr.createParsed(_:atLoc:nameLoc:"
2325+
"parensRange:"
2326+
"mangled:mangledLoc:index:indexLoc:)")
2327+
BridgedOpaqueReturnTypeOfTypeAttr
2328+
BridgedOpaqueReturnTypeOfTypeAttr_createParsed(
2329+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
2330+
BridgedSourceLoc cKwLoc, BridgedSourceRange cParens,
2331+
BridgedStringRef cMangled, BridgedSourceLoc cMangledDoc, size_t index,
2332+
BridgedSourceLoc cIndexLoc);
23222333

23232334
//===----------------------------------------------------------------------===//
23242335
// MARK: TypeReprs

include/swift/AST/ASTBridgingImpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr) {
106106
return *static_cast<swift::ASTContext *>(ptr);
107107
}
108108

109-
BRIDGED_INLINE
110109
void *_Nullable BridgedASTContext_allocate(BridgedASTContext bridged,
111110
size_t size, size_t alignment) {
112111
return bridged.unbridged().Allocate(size, alignment);
@@ -162,7 +161,7 @@ BridgedDeclContext_getParentSourceFile(BridgedDeclContext dc) {
162161
// MARK: BridgedSoureFile
163162
//===----------------------------------------------------------------------===//
164163

165-
BRIDGED_INLINE bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) {
164+
bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) {
166165
return sf.unbridged()->isScriptMode();
167166
}
168167

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,8 +2040,9 @@ namespace {
20402040
if (auto underlying = TAD->getCachedUnderlyingType()) {
20412041
printTypeField(underlying, Label::always("type"));
20422042
} else {
2043-
printFlag("unresolved_type", TypeColor);
2043+
printRec(TAD->getUnderlyingTypeRepr(), Label::always("type_repr"));
20442044
}
2045+
20452046
printWhereRequirements(TAD);
20462047
printAttributes(TAD);
20472048

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
}

0 commit comments

Comments
 (0)