19
19
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
20
20
public protocol DeclSyntaxProtocol : SyntaxProtocol { }
21
21
22
+ public extension Syntax {
23
+ /// Check whether the non-type erased version of this syntax node conforms to
24
+ /// DeclSyntaxProtocol.
25
+ func `is`( _: DeclSyntaxProtocol . Protocol ) -> Bool {
26
+ return self . as ( DeclSyntaxProtocol . self) != nil
27
+ }
28
+
29
+ /// Return the non-type erased version of this syntax node if it conforms to
30
+ /// DeclSyntaxProtocol. Otherwise return nil.
31
+ func `as`( _: DeclSyntaxProtocol . Protocol ) -> DeclSyntaxProtocol ? {
32
+ return self . as ( SyntaxProtocol . self) as? DeclSyntaxProtocol
33
+ }
34
+ }
35
+
22
36
public struct DeclSyntax : DeclSyntaxProtocol , SyntaxHashable {
23
37
public let _syntaxNode : Syntax
24
38
@@ -64,13 +78,25 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
64
78
public func `as`< S: DeclSyntaxProtocol > ( _ syntaxType: S . Type ) -> S ? {
65
79
return S . init ( _syntaxNode)
66
80
}
81
+
82
+ /// Syntax nodes always conform to `DeclSyntaxProtocol`. This API is just
83
+ /// added for consistency.
84
+ @available ( * , deprecated, message: " Expression always evaluates to true " )
85
+ public func `is`( _: DeclSyntaxProtocol . Protocol ) -> Bool {
86
+ return true
87
+ }
88
+
89
+ /// Return the non-type erased version of this syntax node.
90
+ public func `as`( _: DeclSyntaxProtocol . Protocol ) -> DeclSyntaxProtocol {
91
+ return Syntax ( self ) . as ( DeclSyntaxProtocol . self) !
92
+ }
67
93
}
68
94
69
95
extension DeclSyntax : CustomReflectable {
70
96
/// Reconstructs the real syntax type for this type from the node's kind and
71
97
/// provides a mirror that reflects this type.
72
98
public var customMirror : Mirror {
73
- return Mirror ( reflecting: Syntax ( self ) . _asConcreteType )
99
+ return Mirror ( reflecting: Syntax ( self ) . as ( SyntaxProtocol . self ) )
74
100
}
75
101
}
76
102
@@ -81,6 +107,20 @@ extension DeclSyntax: CustomReflectable {
81
107
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
82
108
public protocol ExprSyntaxProtocol : SyntaxProtocol { }
83
109
110
+ public extension Syntax {
111
+ /// Check whether the non-type erased version of this syntax node conforms to
112
+ /// ExprSyntaxProtocol.
113
+ func `is`( _: ExprSyntaxProtocol . Protocol ) -> Bool {
114
+ return self . as ( ExprSyntaxProtocol . self) != nil
115
+ }
116
+
117
+ /// Return the non-type erased version of this syntax node if it conforms to
118
+ /// ExprSyntaxProtocol. Otherwise return nil.
119
+ func `as`( _: ExprSyntaxProtocol . Protocol ) -> ExprSyntaxProtocol ? {
120
+ return self . as ( SyntaxProtocol . self) as? ExprSyntaxProtocol
121
+ }
122
+ }
123
+
84
124
public struct ExprSyntax : ExprSyntaxProtocol , SyntaxHashable {
85
125
public let _syntaxNode : Syntax
86
126
@@ -126,13 +166,25 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
126
166
public func `as`< S: ExprSyntaxProtocol > ( _ syntaxType: S . Type ) -> S ? {
127
167
return S . init ( _syntaxNode)
128
168
}
169
+
170
+ /// Syntax nodes always conform to `ExprSyntaxProtocol`. This API is just
171
+ /// added for consistency.
172
+ @available ( * , deprecated, message: " Expression always evaluates to true " )
173
+ public func `is`( _: ExprSyntaxProtocol . Protocol ) -> Bool {
174
+ return true
175
+ }
176
+
177
+ /// Return the non-type erased version of this syntax node.
178
+ public func `as`( _: ExprSyntaxProtocol . Protocol ) -> ExprSyntaxProtocol {
179
+ return Syntax ( self ) . as ( ExprSyntaxProtocol . self) !
180
+ }
129
181
}
130
182
131
183
extension ExprSyntax : CustomReflectable {
132
184
/// Reconstructs the real syntax type for this type from the node's kind and
133
185
/// provides a mirror that reflects this type.
134
186
public var customMirror : Mirror {
135
- return Mirror ( reflecting: Syntax ( self ) . _asConcreteType )
187
+ return Mirror ( reflecting: Syntax ( self ) . as ( SyntaxProtocol . self ) )
136
188
}
137
189
}
138
190
@@ -143,6 +195,20 @@ extension ExprSyntax: CustomReflectable {
143
195
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
144
196
public protocol StmtSyntaxProtocol : SyntaxProtocol { }
145
197
198
+ public extension Syntax {
199
+ /// Check whether the non-type erased version of this syntax node conforms to
200
+ /// StmtSyntaxProtocol.
201
+ func `is`( _: StmtSyntaxProtocol . Protocol ) -> Bool {
202
+ return self . as ( StmtSyntaxProtocol . self) != nil
203
+ }
204
+
205
+ /// Return the non-type erased version of this syntax node if it conforms to
206
+ /// StmtSyntaxProtocol. Otherwise return nil.
207
+ func `as`( _: StmtSyntaxProtocol . Protocol ) -> StmtSyntaxProtocol ? {
208
+ return self . as ( SyntaxProtocol . self) as? StmtSyntaxProtocol
209
+ }
210
+ }
211
+
146
212
public struct StmtSyntax : StmtSyntaxProtocol , SyntaxHashable {
147
213
public let _syntaxNode : Syntax
148
214
@@ -188,13 +254,25 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
188
254
public func `as`< S: StmtSyntaxProtocol > ( _ syntaxType: S . Type ) -> S ? {
189
255
return S . init ( _syntaxNode)
190
256
}
257
+
258
+ /// Syntax nodes always conform to `StmtSyntaxProtocol`. This API is just
259
+ /// added for consistency.
260
+ @available ( * , deprecated, message: " Expression always evaluates to true " )
261
+ public func `is`( _: StmtSyntaxProtocol . Protocol ) -> Bool {
262
+ return true
263
+ }
264
+
265
+ /// Return the non-type erased version of this syntax node.
266
+ public func `as`( _: StmtSyntaxProtocol . Protocol ) -> StmtSyntaxProtocol {
267
+ return Syntax ( self ) . as ( StmtSyntaxProtocol . self) !
268
+ }
191
269
}
192
270
193
271
extension StmtSyntax : CustomReflectable {
194
272
/// Reconstructs the real syntax type for this type from the node's kind and
195
273
/// provides a mirror that reflects this type.
196
274
public var customMirror : Mirror {
197
- return Mirror ( reflecting: Syntax ( self ) . _asConcreteType )
275
+ return Mirror ( reflecting: Syntax ( self ) . as ( SyntaxProtocol . self ) )
198
276
}
199
277
}
200
278
@@ -205,6 +283,20 @@ extension StmtSyntax: CustomReflectable {
205
283
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
206
284
public protocol TypeSyntaxProtocol : SyntaxProtocol { }
207
285
286
+ public extension Syntax {
287
+ /// Check whether the non-type erased version of this syntax node conforms to
288
+ /// TypeSyntaxProtocol.
289
+ func `is`( _: TypeSyntaxProtocol . Protocol ) -> Bool {
290
+ return self . as ( TypeSyntaxProtocol . self) != nil
291
+ }
292
+
293
+ /// Return the non-type erased version of this syntax node if it conforms to
294
+ /// TypeSyntaxProtocol. Otherwise return nil.
295
+ func `as`( _: TypeSyntaxProtocol . Protocol ) -> TypeSyntaxProtocol ? {
296
+ return self . as ( SyntaxProtocol . self) as? TypeSyntaxProtocol
297
+ }
298
+ }
299
+
208
300
public struct TypeSyntax : TypeSyntaxProtocol , SyntaxHashable {
209
301
public let _syntaxNode : Syntax
210
302
@@ -250,13 +342,25 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
250
342
public func `as`< S: TypeSyntaxProtocol > ( _ syntaxType: S . Type ) -> S ? {
251
343
return S . init ( _syntaxNode)
252
344
}
345
+
346
+ /// Syntax nodes always conform to `TypeSyntaxProtocol`. This API is just
347
+ /// added for consistency.
348
+ @available ( * , deprecated, message: " Expression always evaluates to true " )
349
+ public func `is`( _: TypeSyntaxProtocol . Protocol ) -> Bool {
350
+ return true
351
+ }
352
+
353
+ /// Return the non-type erased version of this syntax node.
354
+ public func `as`( _: TypeSyntaxProtocol . Protocol ) -> TypeSyntaxProtocol {
355
+ return Syntax ( self ) . as ( TypeSyntaxProtocol . self) !
356
+ }
253
357
}
254
358
255
359
extension TypeSyntax : CustomReflectable {
256
360
/// Reconstructs the real syntax type for this type from the node's kind and
257
361
/// provides a mirror that reflects this type.
258
362
public var customMirror : Mirror {
259
- return Mirror ( reflecting: Syntax ( self ) . _asConcreteType )
363
+ return Mirror ( reflecting: Syntax ( self ) . as ( SyntaxProtocol . self ) )
260
364
}
261
365
}
262
366
@@ -267,6 +371,20 @@ extension TypeSyntax: CustomReflectable {
267
371
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
268
372
public protocol PatternSyntaxProtocol : SyntaxProtocol { }
269
373
374
+ public extension Syntax {
375
+ /// Check whether the non-type erased version of this syntax node conforms to
376
+ /// PatternSyntaxProtocol.
377
+ func `is`( _: PatternSyntaxProtocol . Protocol ) -> Bool {
378
+ return self . as ( PatternSyntaxProtocol . self) != nil
379
+ }
380
+
381
+ /// Return the non-type erased version of this syntax node if it conforms to
382
+ /// PatternSyntaxProtocol. Otherwise return nil.
383
+ func `as`( _: PatternSyntaxProtocol . Protocol ) -> PatternSyntaxProtocol ? {
384
+ return self . as ( SyntaxProtocol . self) as? PatternSyntaxProtocol
385
+ }
386
+ }
387
+
270
388
public struct PatternSyntax : PatternSyntaxProtocol , SyntaxHashable {
271
389
public let _syntaxNode : Syntax
272
390
@@ -312,13 +430,25 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
312
430
public func `as`< S: PatternSyntaxProtocol > ( _ syntaxType: S . Type ) -> S ? {
313
431
return S . init ( _syntaxNode)
314
432
}
433
+
434
+ /// Syntax nodes always conform to `PatternSyntaxProtocol`. This API is just
435
+ /// added for consistency.
436
+ @available ( * , deprecated, message: " Expression always evaluates to true " )
437
+ public func `is`( _: PatternSyntaxProtocol . Protocol ) -> Bool {
438
+ return true
439
+ }
440
+
441
+ /// Return the non-type erased version of this syntax node.
442
+ public func `as`( _: PatternSyntaxProtocol . Protocol ) -> PatternSyntaxProtocol {
443
+ return Syntax ( self ) . as ( PatternSyntaxProtocol . self) !
444
+ }
315
445
}
316
446
317
447
extension PatternSyntax : CustomReflectable {
318
448
/// Reconstructs the real syntax type for this type from the node's kind and
319
449
/// provides a mirror that reflects this type.
320
450
public var customMirror : Mirror {
321
- return Mirror ( reflecting: Syntax ( self ) . _asConcreteType )
451
+ return Mirror ( reflecting: Syntax ( self ) . as ( SyntaxProtocol . self ) )
322
452
}
323
453
}
324
454
0 commit comments