Skip to content

Commit 361473c

Browse files
authored
Merge pull request swiftlang#79630 from rintaro/astgen-typespecifiers
[ASTGen] Rework type specifiers
2 parents 71f9aac + 0c23098 commit 361473c

File tree

4 files changed

+182
-87
lines changed

4 files changed

+182
-87
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,28 +2459,10 @@ BridgedOpaqueReturnTypeOfTypeAttr_createParsed(
24592459
// MARK: TypeReprs
24602460
//===----------------------------------------------------------------------===//
24612461

2462-
/// Bridged parameter specifiers
2463-
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : size_t {
2464-
BridgedAttributedTypeSpecifierInOut,
2465-
BridgedAttributedTypeSpecifierBorrowing,
2466-
BridgedAttributedTypeSpecifierConsuming,
2467-
BridgedAttributedTypeSpecifierLegacyShared,
2468-
BridgedAttributedTypeSpecifierLegacyOwned,
2469-
BridgedAttributedTypeSpecifierConst,
2470-
BridgedAttributedTypeSpecifierIsolated,
2471-
BridgedAttributedTypeSpecifierSending,
2472-
};
2473-
24742462
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:loc:name:)")
24752463
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
24762464
BridgedASTContext cContext, BridgedSourceLoc cLoc, BridgedIdentifier id);
24772465

2478-
SWIFT_NAME(
2479-
"BridgedSpecifierTypeRepr.createParsed(_:base:specifier:specifierLoc:)")
2480-
BridgedSpecifierTypeRepr BridgedSpecifierTypeRepr_createParsed(
2481-
BridgedASTContext cContext, BridgedTypeRepr base,
2482-
BridgedAttributedTypeSpecifier specifier, BridgedSourceLoc cSpecifierLoc);
2483-
24842466
SWIFT_NAME(
24852467
"BridgedArrayTypeRepr.createParsed(_:base:leftSquareLoc:rightSquareLoc:)")
24862468
BridgedArrayTypeRepr BridgedArrayTypeRepr_createParsed(
@@ -2504,6 +2486,19 @@ BridgedCompositionTypeRepr_createParsed(BridgedASTContext cContext,
25042486
BridgedArrayRef types,
25052487
BridgedSourceLoc cFirstAmpLoc);
25062488

2489+
SWIFT_NAME("BridgedCompileTimeConstTypeRepr.createParsed(_:base:specifierLoc:)")
2490+
BridgedCompileTimeConstTypeRepr
2491+
BridgedCompileTimeConstTypeRepr_createParsed(BridgedASTContext cContext,
2492+
BridgedTypeRepr base,
2493+
BridgedSourceLoc cSpecifierLoc);
2494+
2495+
SWIFT_NAME("BridgedDeclRefTypeRepr.createParsed(_:base:name:nameLoc:"
2496+
"genericArguments:angleRange:)")
2497+
BridgedDeclRefTypeRepr BridgedDeclRefTypeRepr_createParsed(
2498+
BridgedASTContext cContext, BridgedTypeRepr cBase, BridgedIdentifier cName,
2499+
BridgedSourceLoc cLoc, BridgedArrayRef cGenericArguments,
2500+
BridgedSourceRange cAngleRange);
2501+
25072502
SWIFT_NAME("BridgedDictionaryTypeRepr.createParsed(_:leftSquareLoc:keyType:"
25082503
"colonLoc:valueType:rightSquareLoc:)")
25092504
BridgedDictionaryTypeRepr BridgedDictionaryTypeRepr_createParsed(
@@ -2549,18 +2544,30 @@ BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,
25492544
BridgedSourceLoc cTildeLoc,
25502545
BridgedTypeRepr cConstraint);
25512546

2552-
SWIFT_NAME("BridgedDeclRefTypeRepr.createParsed(_:base:name:nameLoc:genericArguments:angleRange:)")
2553-
BridgedDeclRefTypeRepr BridgedDeclRefTypeRepr_createParsed(
2554-
BridgedASTContext cContext, BridgedTypeRepr cBase, BridgedIdentifier cName,
2555-
BridgedSourceLoc cLoc, BridgedArrayRef cGenericArguments,
2556-
BridgedSourceRange cAngleRange);
2547+
SWIFT_NAME("BridgedIsolatedTypeRepr.createParsed(_:base:specifierLoc:)")
2548+
BridgedIsolatedTypeRepr
2549+
BridgedIsolatedTypeRepr_createParsed(BridgedASTContext cContext,
2550+
BridgedTypeRepr base,
2551+
BridgedSourceLoc cSpecifierLoc);
2552+
2553+
SWIFT_NAME("BridgedLifetimeDependentTypeRepr.createParsed(_:base:entry:)")
2554+
BridgedLifetimeDependentTypeRepr
2555+
BridgedLifetimeDependentTypeRepr_createParsed(BridgedASTContext cContext,
2556+
BridgedTypeRepr base,
2557+
BridgedLifetimeEntry cEntry);
25572558

25582559
SWIFT_NAME("BridgedMetatypeTypeRepr.createParsed(_:base:typeKeywordLoc:)")
25592560
BridgedMetatypeTypeRepr
25602561
BridgedMetatypeTypeRepr_createParsed(BridgedASTContext cContext,
25612562
BridgedTypeRepr baseType,
25622563
BridgedSourceLoc cTypeLoc);
25632564

2565+
SWIFT_NAME(
2566+
"BridgedOwnershipTypeRepr.createParsed(_:base:specifier:specifierLoc:)")
2567+
BridgedOwnershipTypeRepr BridgedOwnershipTypeRepr_createParsed(
2568+
BridgedASTContext cContext, BridgedTypeRepr base,
2569+
BridgedParamSpecifier cSpecifier, BridgedSourceLoc cSpecifierLoc);
2570+
25642571
SWIFT_NAME("BridgedProtocolTypeRepr.createParsed(_:base:protocolKeywordLoc:)")
25652572
BridgedProtocolTypeRepr
25662573
BridgedProtocolTypeRepr_createParsed(BridgedASTContext cContext,
@@ -2580,6 +2587,12 @@ BridgedPackExpansionTypeRepr_createParsed(BridgedASTContext cContext,
25802587
BridgedTypeRepr base,
25812588
BridgedSourceLoc cRepeatLoc);
25822589

2590+
SWIFT_NAME("BridgedSendingTypeRepr.createParsed(_:base:specifierLoc:)")
2591+
BridgedSendingTypeRepr
2592+
BridgedSendingTypeRepr_createParsed(BridgedASTContext cContext,
2593+
BridgedTypeRepr base,
2594+
BridgedSourceLoc cSpecifierLoc);
2595+
25832596
SWIFT_NAME(
25842597
"BridgedTupleTypeRepr.createParsed(_:elements:leftParenLoc:rightParenLoc:)")
25852598
BridgedTupleTypeRepr BridgedTupleTypeRepr_createParsed(

lib/AST/Bridging/TypeReprBridging.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,
109109
InverseTypeRepr(cTildeLoc.unbridged(), cConstraint.unbridged());
110110
}
111111

112+
BridgedIsolatedTypeRepr
113+
BridgedIsolatedTypeRepr_createParsed(BridgedASTContext cContext,
114+
BridgedTypeRepr base,
115+
BridgedSourceLoc cSpecifierLoc) {
116+
return new (cContext.unbridged())
117+
IsolatedTypeRepr(base.unbridged(), cSpecifierLoc.unbridged());
118+
}
119+
120+
BridgedLifetimeDependentTypeRepr
121+
BridgedLifetimeDependentTypeRepr_createParsed(BridgedASTContext cContext,
122+
BridgedTypeRepr base,
123+
BridgedLifetimeEntry cEntry) {
124+
return new (cContext.unbridged())
125+
LifetimeDependentTypeRepr(base.unbridged(), cEntry.unbridged());
126+
}
127+
112128
BridgedMetatypeTypeRepr
113129
BridgedMetatypeTypeRepr_createParsed(BridgedASTContext cContext,
114130
BridgedTypeRepr baseType,
@@ -118,6 +134,13 @@ BridgedMetatypeTypeRepr_createParsed(BridgedASTContext cContext,
118134
return new (context) MetatypeTypeRepr(baseType.unbridged(), tyLoc);
119135
}
120136

137+
BridgedOwnershipTypeRepr BridgedOwnershipTypeRepr_createParsed(
138+
BridgedASTContext cContext, BridgedTypeRepr base,
139+
BridgedParamSpecifier cSpecifier, BridgedSourceLoc cSpecifierLoc) {
140+
return new (cContext.unbridged()) OwnershipTypeRepr(
141+
base.unbridged(), unbridge(cSpecifier), cSpecifierLoc.unbridged());
142+
}
143+
121144
BridgedProtocolTypeRepr
122145
BridgedProtocolTypeRepr_createParsed(BridgedASTContext cContext,
123146
BridgedTypeRepr baseType,
@@ -166,43 +189,12 @@ BridgedAttributedTypeRepr_createParsed(BridgedASTContext cContext,
166189
base.unbridged());
167190
}
168191

169-
BridgedSpecifierTypeRepr BridgedSpecifierTypeRepr_createParsed(
170-
BridgedASTContext cContext, BridgedTypeRepr base,
171-
BridgedAttributedTypeSpecifier specifier, BridgedSourceLoc cSpecifierLoc) {
172-
ASTContext &context = cContext.unbridged();
173-
SourceLoc loc = cSpecifierLoc.unbridged();
174-
TypeRepr *baseType = base.unbridged();
175-
switch (specifier) {
176-
case BridgedAttributedTypeSpecifierInOut: {
177-
return new (context)
178-
OwnershipTypeRepr(baseType, ParamSpecifier::InOut, loc);
179-
}
180-
case BridgedAttributedTypeSpecifierBorrowing: {
181-
return new (context)
182-
OwnershipTypeRepr(baseType, ParamSpecifier::Borrowing, loc);
183-
}
184-
case BridgedAttributedTypeSpecifierConsuming: {
185-
return new (context)
186-
OwnershipTypeRepr(baseType, ParamSpecifier::Consuming, loc);
187-
}
188-
case BridgedAttributedTypeSpecifierLegacyShared: {
189-
return new (context)
190-
OwnershipTypeRepr(baseType, ParamSpecifier::LegacyShared, loc);
191-
}
192-
case BridgedAttributedTypeSpecifierLegacyOwned: {
193-
return new (context)
194-
OwnershipTypeRepr(baseType, ParamSpecifier::LegacyOwned, loc);
195-
}
196-
case BridgedAttributedTypeSpecifierSending: {
197-
return new (context) SendingTypeRepr(baseType, loc);
198-
}
199-
case BridgedAttributedTypeSpecifierConst: {
200-
return new (context) CompileTimeConstTypeRepr(baseType, loc);
201-
}
202-
case BridgedAttributedTypeSpecifierIsolated: {
203-
return new (context) IsolatedTypeRepr(baseType, loc);
204-
}
205-
}
192+
BridgedSendingTypeRepr
193+
BridgedSendingTypeRepr_createParsed(BridgedASTContext cContext,
194+
BridgedTypeRepr base,
195+
BridgedSourceLoc cSpecifierLoc) {
196+
return new (cContext.unbridged())
197+
SendingTypeRepr(base.unbridged(), cSpecifierLoc.unbridged());
206198
}
207199

208200
BridgedVarargTypeRepr
@@ -275,6 +267,14 @@ BridgedCompositionTypeRepr_createParsed(BridgedASTContext cContext,
275267
SourceRange{firstAmpLoc, types.back()->getEndLoc()});
276268
}
277269

270+
BridgedCompileTimeConstTypeRepr
271+
BridgedCompileTimeConstTypeRepr_createParsed(BridgedASTContext cContext,
272+
BridgedTypeRepr base,
273+
BridgedSourceLoc cSpecifierLoc) {
274+
return new (cContext.unbridged())
275+
CompileTimeConstTypeRepr(base.unbridged(), cSpecifierLoc.unbridged());
276+
}
277+
278278
BridgedFunctionTypeRepr BridgedFunctionTypeRepr_createParsed(
279279
BridgedASTContext cContext, BridgedTypeRepr argsTy,
280280
BridgedSourceLoc cAsyncLoc, BridgedSourceLoc cThrowsLoc,

lib/ASTGen/Sources/ASTGen/Types.swift

Lines changed: 107 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -301,43 +301,82 @@ extension ASTGenVisitor {
301301

302302
// MARK: - SpecifierTypeRepr/AttributedTypeRepr
303303

304-
extension BridgedAttributedTypeSpecifier {
305-
fileprivate init?(from keyword: Keyword?) {
306-
switch keyword {
307-
case .inout: self = .inOut
308-
case .borrowing: self = .borrowing
309-
case .consuming: self = .consuming
310-
case .__shared: self = .legacyShared
311-
case .__owned: self = .legacyOwned
312-
case ._const: self = .const
313-
case .isolated: self = .isolated
314-
default: return nil
304+
extension ASTGenVisitor {
305+
func generateLifetimeDescriptor(lifetimeSpecifierArgument node: LifetimeSpecifierArgumentSyntax) -> BridgedLifetimeDescriptor? {
306+
switch node.parameter.rawTokenKind {
307+
case .identifier, .keyword:
308+
return self.generateLifetimeDescriptor(
309+
nameToken: node.parameter,
310+
lifetimeDependenceKind: .default
311+
)
312+
case .integerLiteral:
313+
guard let index = Int(node.parameter.text) else {
314+
// TODO: Diagnose.
315+
fatalError("(compiler bug) invalid integer literal")
316+
}
317+
return.forOrdered(
318+
index,
319+
dependenceKind: .default,
320+
loc: self.generateSourceLoc(node.parameter)
321+
)
322+
default:
323+
// TODO: Diagnose.
324+
fatalError("expected identifier, 'self', or integer in @lifetime")
315325
}
316326
}
317-
}
318327

319-
extension ASTGenVisitor {
320328
func generate(attributedType node: AttributedTypeSyntax) -> BridgedTypeRepr {
321329
var type = generate(type: node.baseType)
322330

323-
// Handle specifiers.
324-
if case .simpleTypeSpecifier(let simpleSpecifier) = node.specifiers.first {
325-
let specifier = simpleSpecifier.specifier
326-
if let kind = BridgedAttributedTypeSpecifier(from: specifier.keywordKind) {
327-
type =
328-
BridgedSpecifierTypeRepr.createParsed(
329-
self.ctx,
330-
base: type,
331-
specifier: kind,
332-
specifierLoc: self.generateSourceLoc(specifier)
333-
).asTypeRepr
334-
} else {
335-
self.diagnose(.unexpectedTokenKind(token: specifier))
331+
// Specifiers
332+
var ownership: BridgedParamSpecifier = .default
333+
var ownershipLoc: BridgedSourceLoc = nil
334+
var isolatedLoc: BridgedSourceLoc = nil
335+
var constLoc: BridgedSourceLoc = nil
336+
var sendingLoc: BridgedSourceLoc = nil
337+
var lifetimeEntry: BridgedLifetimeEntry? = nil
338+
339+
// TODO: Diagnostics for duplicated specifiers, and ordering.
340+
for node in node.specifiers {
341+
let loc = self.generateSourceLoc(node)
342+
switch node {
343+
case .simpleTypeSpecifier(let node):
344+
switch node.specifier.keywordKind {
345+
case .inout:
346+
(ownership, ownershipLoc) = (.inOut, loc)
347+
case .__shared:
348+
(ownership, ownershipLoc) = (.legacyShared, loc)
349+
case .__owned:
350+
(ownership, ownershipLoc) = (.legacyOwned, loc)
351+
case .borrowing:
352+
(ownership, ownershipLoc) = (.borrowing, loc)
353+
case .consuming:
354+
(ownership, ownershipLoc) = (.consuming, loc)
355+
case .isolated:
356+
isolatedLoc = loc
357+
case ._const:
358+
constLoc = loc
359+
case .sending:
360+
sendingLoc = loc
361+
default:
362+
// TODO: Diagnostics.
363+
fatalError("(compiler bug) unrecognized type specifier")
364+
}
365+
case .lifetimeTypeSpecifier(let node):
366+
lifetimeEntry = .createParsed(
367+
self.ctx,
368+
range: self.generateSourceRange(
369+
start: node.dependsOnKeyword,
370+
end: node.rightParen
371+
),
372+
sources: node.arguments.lazy.compactMap(self.generateLifetimeDescriptor(lifetimeSpecifierArgument:)).bridgedArray(in: self)
373+
)
336374
}
337375
}
338376

339-
// Handle type attributes.
377+
// Attributes.
340378
let typeAttributes = self.generateTypeAttributes(node)
379+
341380
if !typeAttributes.isEmpty {
342381
type =
343382
BridgedAttributedTypeRepr.createParsed(
@@ -347,6 +386,47 @@ extension ASTGenVisitor {
347386
).asTypeRepr
348387
}
349388

389+
if ownershipLoc.isValid && ownership != .default {
390+
type = BridgedOwnershipTypeRepr.createParsed(
391+
self.ctx,
392+
base: type,
393+
specifier: ownership,
394+
specifierLoc: ownershipLoc
395+
).asTypeRepr
396+
}
397+
398+
if isolatedLoc.isValid {
399+
type = BridgedIsolatedTypeRepr.createParsed(
400+
self.ctx,
401+
base: type,
402+
specifierLoc: isolatedLoc
403+
).asTypeRepr
404+
}
405+
406+
if constLoc.isValid {
407+
type = BridgedCompileTimeConstTypeRepr.createParsed(
408+
self.ctx,
409+
base: type,
410+
specifierLoc: constLoc
411+
).asTypeRepr
412+
}
413+
414+
if sendingLoc.isValid {
415+
type = BridgedSendingTypeRepr.createParsed(
416+
self.ctx,
417+
base: type,
418+
specifierLoc: constLoc
419+
).asTypeRepr
420+
}
421+
422+
if let lifetimeEntry {
423+
type = BridgedLifetimeDependentTypeRepr.createParsed(
424+
self.ctx,
425+
base: type,
426+
entry: lifetimeEntry
427+
).asTypeRepr
428+
}
429+
350430
return type
351431
}
352432
}

test/ASTGen/types.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ struct SomeGlobalActor {
6868
static let shared = SomeActor()
6969
}
7070
typealias SomeGlobalActorIsolated = @SomeGlobalActor () -> Void
71+
typealias TestSpecifiers<Value, Result, E> = (inout sending Value) throws(E) -> sending Result where Value: ~Copyable, Result: ~Copyable, E: Error
72+
typealias TestSpecifierAndAttr<T> = (__owned @Sendable @escaping () async -> T) -> T

0 commit comments

Comments
 (0)