Skip to content

Commit 5870421

Browse files
committed
[ASTGen] Support specifiers on types such as inout and consuming
1 parent 682ac98 commit 5870421

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long {
112112
BridgedBuiltinExternalMacro
113113
} BridgedMacroDefinitionKind;
114114

115+
/// Bridged parameter specifiers
116+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long {
117+
BridgedAttributedTypeSpecifierInOut,
118+
BridgedAttributedTypeSpecifierBorrowing,
119+
BridgedAttributedTypeSpecifierConsuming,
120+
BridgedAttributedTypeSpecifierLegacyShared,
121+
BridgedAttributedTypeSpecifierLegacyOwned,
122+
BridgedAttributedTypeSpecifierConst,
123+
BridgedAttributedTypeSpecifierIsolated,
124+
} BridgedAttributedTypeSpecifier;
125+
115126
#ifdef __cplusplus
116127
extern "C" {
117128

@@ -261,6 +272,8 @@ void *ImplicitlyUnwrappedOptionalTypeRepr_create(void *ctx, void *base,
261272
void *exclamationLoc);
262273
void *MetatypeTypeRepr_create(void *ctx, void *baseType, void *typeLoc);
263274
void *ProtocolTypeRepr_create(void *ctx, void *baseType, void *protoLoc);
275+
void *AttributedTypeSpecifierRepr_create(
276+
void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc);
264277
void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc);
265278
void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc,
266279
void *rParenLoc);

lib/AST/CASTBridging.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,30 @@ void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc) {
484484
getSourceLocFromPointer(repeatLoc), (TypeRepr *)base);
485485
}
486486

487+
void *AttributedTypeSpecifierRepr_create(
488+
void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc
489+
) {
490+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
491+
SourceLoc loc = getSourceLocFromPointer(specifierLoc);
492+
TypeRepr *baseType = (TypeRepr *)base;
493+
switch (specifier) {
494+
case BridgedAttributedTypeSpecifierInOut:
495+
return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::InOut, loc);
496+
case BridgedAttributedTypeSpecifierBorrowing:
497+
return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::Borrowing, loc);
498+
case BridgedAttributedTypeSpecifierConsuming:
499+
return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::Consuming, loc);
500+
case BridgedAttributedTypeSpecifierLegacyShared:
501+
return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::LegacyShared, loc);
502+
case BridgedAttributedTypeSpecifierLegacyOwned:
503+
return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::LegacyOwned, loc);
504+
case BridgedAttributedTypeSpecifierConst:
505+
return new (Context) CompileTimeConstTypeRepr(baseType, loc);
506+
case BridgedAttributedTypeSpecifierIsolated:
507+
return new (Context) IsolatedTypeRepr(baseType, loc);
508+
}
509+
}
510+
487511
void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc,
488512
void *rParenLoc) {
489513
ASTContext &Context = *static_cast<ASTContext *>(ctx);

lib/ASTGen/Sources/ASTGen/Types.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,30 @@ extension ASTGenVisitor {
161161
}
162162

163163
public func visit(_ node: AttributedTypeSyntax) -> ASTNode {
164+
var type = visit(node.baseType)
165+
166+
// Handle specifiers.
167+
if let specifier = node.specifier {
168+
let specifierLoc = self.base.advanced(by: specifier.position.utf8Offset).raw
169+
170+
let kind: BridgedAttributedTypeSpecifier
171+
switch specifier.tokenKind {
172+
case .keyword(.inout): kind = .inOut
173+
case .keyword(.borrowing): kind = .borrowing
174+
case .keyword(.consuming): kind = .consuming
175+
case .keyword(.__shared): kind = .legacyShared
176+
case .keyword(.__owned): kind = .legacyOwned
177+
case .keyword(._const): kind = .const
178+
case .keyword(.isolated): kind = .isolated
179+
default: fatalError("unhandled specifier \(specifier.debugDescription)")
180+
}
181+
182+
type = .type(AttributedTypeSpecifierRepr_create(self.ctx, type.rawValue, kind, specifierLoc))
183+
}
184+
164185
// FIXME: Respect the attributes
165-
return visit(node.baseType)
186+
187+
return type
166188
}
167189
}
168190

test/ASTGen/verify-parse.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ func test6(_ b: Bool) -> Int {
3838
let x = if b { 0 } else { 1 }
3939
return x
4040
}
41+
42+
func test7(_ b: inout Bool) {
43+
// b = true
44+
}
45+
46+
func test8(_ i: _const Int) {
47+
}

0 commit comments

Comments
 (0)