1
1
import SwiftSyntax
2
2
import SwiftSyntaxBuilder
3
3
4
- // TODO: Always normalize typed and pretend sugar doesn't exist (e.g. Int? looks like Optional<Int> to devs)
4
+ // TODO: Implement type normalisation and pretend sugar doesn't exist (e.g. Int? looks like Optional<Int> to devs)
5
5
/// Wraps type syntax (e.g. `Result<Success, Failure>`).
6
6
public enum Type : TypeProtocol , SyntaxExpressibleByStringInterpolation {
7
+ /// An array type (e.g. `[Int]`).
7
8
case array( ArrayType )
9
+ /// A `class` token in a conformance list. Equivalent to `AnyObject`.
8
10
case classRestriction( ClassRestrictionType )
11
+ /// A composition of two types (e.g. `Encodable & Decodable`). Used to
12
+ /// combine protocol requirements.
9
13
case composition( CompositionType )
14
+ /// A sugared protocl type (e.g. `any T` or `some T`).
10
15
case constrainedSugar( ConstrainedSugarType )
16
+ /// A dictionary type (e.g. `[Int: String]`).
11
17
case dictionary( DictionaryType )
18
+ /// A function type (e.g. `() -> ()`).
12
19
case function( FunctionType )
20
+ /// An implicitly unwrapped optional type (e.g. `Int!`).
13
21
case implicitlyUnwrappedOptional( ImplicitlyUnwrappedOptionalType )
22
+ /// A member type (e.g. `Array<Int>.Element`).
14
23
case member( MemberType )
24
+ /// A metatype (e.g. `Int.Type` or `Encodable.Protocol`).
15
25
case metatype( MetatypeType )
26
+ /// A placeholder for invalid types that the resilient parser ignored.
16
27
case missing( MissingType )
28
+ /// An optional type (e.g. `Int?`).
17
29
case optional( OptionalType )
30
+ /// A pack expansion type (e.g. `repeat each V`).
18
31
case packExpansion( PackExpansionType )
32
+ /// A pack reference type (e.g. `each V`).
19
33
case packReference( PackReferenceType )
34
+ /// A simple type (e.g. `Int` or `Box<Int>`).
20
35
case simple( SimpleType )
36
+ /// A suppressed type in a conformance position (e.g. `~Copyable`).
21
37
case suppressed( SuppressedType )
38
+ //// A tuple type (e.g. `(Int, String)`).
22
39
case tuple( TupleType )
23
-
40
+
24
41
public var _baseSyntax : TypeSyntax {
25
42
let type : any TypeProtocol = switch self {
26
43
case . array( let type) : type
@@ -65,11 +82,11 @@ public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
65
82
return type. _attributedSyntax
66
83
}
67
84
85
+ /// Wrap a ``TypeSyntax`` (e.g. `Int?` or `MyStruct<[String]>!`).
68
86
public init ( _ syntax: TypeSyntax ) {
69
87
self . init ( syntax, attributedSyntax: nil )
70
88
}
71
89
72
- /// Ignore the `attributedSyntax` attribute; it exists because of protocol conformance.
73
90
public init ( _ syntax: TypeSyntax , attributedSyntax: AttributedTypeSyntax ? = nil ) {
74
91
// TODO: Move this weird initializer to an internal protocol if possible
75
92
let syntax : TypeSyntaxProtocol = attributedSyntax ?? syntax
@@ -110,11 +127,15 @@ public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
110
127
}
111
128
}
112
129
130
+ // TODO: add an optional version to all type syntax wrappers maybe?
131
+ /// Allows string interpolation syntax to be used to express type syntax.
113
132
public init ( stringInterpolation: SyntaxStringInterpolation ) {
114
133
self . init ( TypeSyntax ( stringInterpolation: stringInterpolation) )
115
134
}
116
135
136
+ /// A normalized description of the type (e.g. for `()` this would be `Void`).
117
137
public var normalizedDescription : String {
138
+ // TODO: Implement proper type normalization
118
139
// TODO: Normalize types nested within the type too (e.g. the parameter types of a function type)
119
140
if let tupleSyntax = _syntax. as ( TupleTypeSyntax . self) {
120
141
if tupleSyntax. elements. count == 0 {
@@ -130,28 +151,33 @@ public enum Type: TypeProtocol, SyntaxExpressibleByStringInterpolation {
130
151
}
131
152
}
132
153
154
+ /// Gets whether the type is a void type (i.e. `Void`, `()`, `(Void)`, `((((()))))`, etc.).
133
155
public var isVoid : Bool {
134
156
normalizedDescription == " Void "
135
157
}
136
158
137
159
// TODO: Generate type conversions with macro?
160
+ /// Attempts to get the type as a simple type.
138
161
public var asSimpleType : SimpleType ? {
139
162
switch self {
140
163
case . simple( let type) : type
141
164
default : nil
142
165
}
143
166
}
144
167
168
+ /// Attempts to get the type as a function type.
145
169
public var asFunctionType : FunctionType ? {
146
170
switch self {
147
171
case . function( let type) : type
148
172
default : nil
149
173
}
150
174
}
175
+
176
+ // TODO: Implement rest of conversions
151
177
}
152
178
153
- extension Optional < Type > {
154
- /// If `nil`, the type is considered void, otherwise the underlying type is queried.
179
+ extension Type ? {
180
+ /// If `nil`, the type is considered void, otherwise the underlying type is queried (see ``Type/isVoid``) .
155
181
public var isVoid : Bool {
156
182
if let self = self {
157
183
return self . isVoid
0 commit comments