@@ -4,14 +4,12 @@ public static class TypeExtensions
4
4
{
5
5
public static bool IsPrimitiveType ( this Type t )
6
6
{
7
- PrimitiveType type ;
8
- return t . IsPrimitiveType ( out type ) ;
7
+ return t . IsPrimitiveType ( out PrimitiveType _ ) ;
9
8
}
10
9
11
10
public static bool IsPrimitiveType ( this Type t , out PrimitiveType primitive )
12
11
{
13
- var builtin = t . Desugar ( ) as BuiltinType ;
14
- if ( builtin != null )
12
+ if ( t . Desugar ( ) is BuiltinType builtin )
15
13
{
16
14
primitive = builtin . Type ;
17
15
return true ;
@@ -32,9 +30,7 @@ public static bool IsPrimitiveType(this Type t, PrimitiveType primitive)
32
30
33
31
public static bool IsEnumType ( this Type t )
34
32
{
35
- var tag = t . Desugar ( ) as TagType ;
36
-
37
- if ( tag == null )
33
+ if ( t . Desugar ( ) is not TagType tag )
38
34
return false ;
39
35
40
36
return tag . Declaration is Enumeration ;
@@ -47,36 +43,28 @@ public static bool IsAddress(this Type t)
47
43
48
44
public static bool IsPointer ( this Type t )
49
45
{
50
- var functionPointer = t as MemberPointerType ;
51
- if ( functionPointer != null )
46
+ if ( t is MemberPointerType )
52
47
return true ;
53
- var pointer = t as PointerType ;
54
- if ( pointer == null )
48
+
49
+ if ( t is not PointerType pointer )
55
50
return false ;
51
+
56
52
return pointer . Modifier == PointerType . TypeModifier . Pointer ;
57
53
}
58
54
59
55
public static bool IsReference ( this Type t )
60
56
{
61
- var pointer = t as PointerType ;
62
- if ( pointer == null )
63
- return false ;
64
- return pointer . IsReference ;
57
+ return t is PointerType { IsReference : true } ;
65
58
}
66
59
67
60
public static bool IsPointerToPrimitiveType ( this Type t )
68
61
{
69
- var ptr = t as PointerType ;
70
- if ( ptr == null )
71
- return false ;
72
- PrimitiveType primitiveType ;
73
- return ptr . Pointee . IsPrimitiveType ( out primitiveType ) ;
62
+ return t is PointerType ptr && ptr . Pointee . IsPrimitiveType ( out _ ) ;
74
63
}
75
64
76
65
public static bool IsPointerToPrimitiveType ( this Type t , out PrimitiveType primitive )
77
66
{
78
- var ptr = t as PointerType ;
79
- if ( ptr == null )
67
+ if ( t is not PointerType ptr )
80
68
{
81
69
primitive = PrimitiveType . Null ;
82
70
return false ;
@@ -86,60 +74,57 @@ public static bool IsPointerToPrimitiveType(this Type t, out PrimitiveType primi
86
74
87
75
public static bool IsPointerToPrimitiveType ( this Type t , PrimitiveType primitive )
88
76
{
89
- var ptr = t as PointerType ;
90
- if ( ptr == null )
77
+ if ( t is not PointerType ptr )
91
78
return false ;
92
79
return ptr . Pointee . IsPrimitiveType ( primitive ) ;
93
80
}
94
81
95
82
public static bool IsPointerToEnum ( this Type t )
96
83
{
97
- var ptr = t as PointerType ;
98
- if ( ptr == null )
84
+ if ( t is not PointerType ptr )
99
85
return false ;
100
86
return ptr . Pointee . IsEnumType ( ) ;
101
87
}
102
88
103
89
public static bool IsPointerToEnum ( this Type t , out Enumeration @enum )
104
90
{
105
- var ptr = t as PointerType ;
106
- if ( ptr == null )
91
+ if ( t is not PointerType ptr )
107
92
{
108
93
@enum = null ;
109
94
return false ;
110
95
}
111
96
return ptr . Pointee . TryGetEnum ( out @enum ) ;
112
97
}
113
98
114
- public static bool IsPointerTo < T > ( this Type t , out T type ) where T : Type
99
+ public static bool IsPointerTo < T > ( this Type t , out T type )
100
+ where T : Type
115
101
{
116
- var pointee = t . GetPointee ( ) ;
117
- type = pointee as T ;
118
- if ( type == null )
102
+ type = t . GetPointee ( ) switch
119
103
{
120
- var attributedType = pointee as AttributedType ;
121
- if ( attributedType != null )
122
- type = attributedType . Modified . Type as T ;
123
- }
104
+ T tType => tType ,
105
+ AttributedType attributedType => attributedType . Modified . Type as T ,
106
+ _ => null
107
+ } ;
108
+
124
109
return type != null ;
125
110
}
126
111
127
112
public static bool IsClass ( this Type t )
128
113
{
129
- Class @class ;
130
- return t . TryGetClass ( out @class ) ;
114
+ return t . TryGetClass ( out _ ) ;
131
115
}
132
116
133
117
public static bool TryGetClass ( this Type t , out Class @class , Class value = null )
134
118
{
135
119
return TryGetDeclaration ( t , out @class , value ) ;
136
120
}
137
121
138
- public static bool TryGetDeclaration < T > ( this Type t , out T decl , T value = null ) where T : Declaration
122
+ public static bool TryGetDeclaration < T > ( this Type t , out T decl , T value = null )
123
+ where T : Declaration
139
124
{
140
125
t = t . Desugar ( ) ;
141
126
142
- TagType tagType = null ;
127
+ TagType tagType ;
143
128
if ( t is TemplateSpecializationType type )
144
129
{
145
130
if ( type . IsDependent )
@@ -150,20 +135,20 @@ public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
150
135
type . Desugared . Type . TryGetDeclaration ( out decl , value ) ;
151
136
return decl != null ;
152
137
case ClassTemplate classTemplate :
153
- {
154
- var templatedClass = classTemplate . TemplatedClass ;
155
- decl = templatedClass . CompleteDeclaration == null
156
- ? templatedClass as T
157
- : ( T ) templatedClass . CompleteDeclaration ;
138
+ {
139
+ var templatedClass = classTemplate . TemplatedClass ;
140
+ decl = templatedClass . CompleteDeclaration == null
141
+ ? templatedClass as T
142
+ : ( T ) templatedClass . CompleteDeclaration ;
158
143
159
- if ( decl == null )
160
- return false ;
144
+ if ( decl == null )
145
+ return false ;
161
146
162
- if ( value != null )
163
- type . Template = new ClassTemplate { TemplatedDecl = value } ;
147
+ if ( value != null )
148
+ type . Template = new ClassTemplate { TemplatedDecl = value } ;
164
149
165
- return true ;
166
- }
150
+ return true ;
151
+ }
167
152
case TemplateTemplateParameter templateTemplateParameter :
168
153
return ( decl = templateTemplateParameter . TemplatedDecl as T ) != null ;
169
154
}
@@ -193,15 +178,12 @@ public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
193
178
194
179
public static bool IsEnum ( this Type t )
195
180
{
196
- Enumeration @enum ;
197
- return t . TryGetEnum ( out @enum ) ;
181
+ return t . TryGetEnum ( out _ ) ;
198
182
}
199
183
200
184
public static bool TryGetEnum ( this Type t , out Enumeration @enum )
201
185
{
202
- var tag = t . Desugar ( ) as TagType ;
203
-
204
- if ( tag == null )
186
+ if ( t . Desugar ( ) is not TagType tag )
205
187
{
206
188
@enum = null ;
207
189
return false ;
@@ -269,13 +251,12 @@ public static Type SkipPointerRefs(this Type t)
269
251
/// </summary>
270
252
public static Type GetPointee ( this Type t )
271
253
{
272
- var ptr = t as PointerType ;
273
- if ( ptr != null )
274
- return ptr . Pointee ;
275
- var memberPtr = t as MemberPointerType ;
276
- if ( memberPtr != null )
277
- return memberPtr . QualifiedPointee . Type ;
278
- return null ;
254
+ return t switch
255
+ {
256
+ PointerType ptr => ptr . Pointee ,
257
+ MemberPointerType memberPtr => memberPtr . QualifiedPointee . Type ,
258
+ _ => null
259
+ } ;
279
260
}
280
261
281
262
/// <summary>
@@ -296,17 +277,28 @@ public static Type GetFinalPointee(this Type t)
296
277
return finalPointee ;
297
278
}
298
279
280
+ public static PointerType GetFinalPointer ( this Type t )
281
+ {
282
+ if ( t is not PointerType type )
283
+ return null ;
284
+
285
+ var pointee = type . Desugar ( ) . GetPointee ( ) ;
286
+
287
+ if ( pointee . IsPointer ( ) )
288
+ return pointee . GetFinalPointer ( ) ;
289
+
290
+ return type ;
291
+ }
292
+
299
293
/// <summary>
300
294
/// If t is a pointer type the type pointed to by t will be returned.
301
295
/// Otherwise the default qualified type.
302
296
/// </summary>
303
297
public static QualifiedType GetQualifiedPointee ( this Type t )
304
298
{
305
- var ptr = t as PointerType ;
306
- if ( ptr != null )
299
+ if ( t is PointerType ptr )
307
300
return ptr . QualifiedPointee ;
308
- var memberPtr = t as MemberPointerType ;
309
- if ( memberPtr != null )
301
+ if ( t is MemberPointerType memberPtr )
310
302
return memberPtr . QualifiedPointee ;
311
303
return new QualifiedType ( ) ;
312
304
}
@@ -329,31 +321,14 @@ public static QualifiedType GetFinalQualifiedPointee(this Type t)
329
321
return finalPointee ;
330
322
}
331
323
332
- public static PointerType GetFinalPointer ( this Type t )
333
- {
334
- var type = t as PointerType ;
335
-
336
- if ( type == null )
337
- return null ;
338
-
339
- var pointee = type . Desugar ( ) . GetPointee ( ) ;
340
-
341
- if ( pointee . IsPointer ( ) )
342
- return pointee . GetFinalPointer ( ) ;
343
-
344
- return type ;
345
- }
346
-
347
324
public static bool ResolvesTo ( this QualifiedType type , QualifiedType other )
348
325
{
349
326
if ( ! type . Qualifiers . Equals ( other . Qualifiers ) )
350
327
return false ;
351
328
352
329
var left = type . Type . Desugar ( ) ;
353
330
var right = other . Type . Desugar ( ) ;
354
- var leftPointer = left as PointerType ;
355
- var rightPointer = right as PointerType ;
356
- if ( leftPointer != null && rightPointer != null )
331
+ if ( left is PointerType leftPointer && right is PointerType rightPointer )
357
332
{
358
333
return leftPointer . Modifier == rightPointer . Modifier &&
359
334
leftPointer . QualifiedPointee . ResolvesTo ( rightPointer . QualifiedPointee ) ;
@@ -388,8 +363,7 @@ public static QualifiedType StripConst(this QualifiedType type)
388
363
qualifiers . IsConst = false ;
389
364
type . Qualifiers = qualifiers ;
390
365
391
- var ptr = type . Type as PointerType ;
392
- if ( ptr != null )
366
+ if ( type . Type is PointerType ptr )
393
367
{
394
368
var pointee = ptr . QualifiedPointee ;
395
369
var pointeeQualifiers = pointee . Qualifiers ;
0 commit comments