@@ -4,14 +4,12 @@ public static class TypeExtensions
44 {
55 public static bool IsPrimitiveType ( this Type t )
66 {
7- PrimitiveType type ;
8- return t . IsPrimitiveType ( out type ) ;
7+ return t . IsPrimitiveType ( out PrimitiveType _ ) ;
98 }
109
1110 public static bool IsPrimitiveType ( this Type t , out PrimitiveType primitive )
1211 {
13- var builtin = t . Desugar ( ) as BuiltinType ;
14- if ( builtin != null )
12+ if ( t . Desugar ( ) is BuiltinType builtin )
1513 {
1614 primitive = builtin . Type ;
1715 return true ;
@@ -32,9 +30,7 @@ public static bool IsPrimitiveType(this Type t, PrimitiveType primitive)
3230
3331 public static bool IsEnumType ( this Type t )
3432 {
35- var tag = t . Desugar ( ) as TagType ;
36-
37- if ( tag == null )
33+ if ( t . Desugar ( ) is not TagType tag )
3834 return false ;
3935
4036 return tag . Declaration is Enumeration ;
@@ -47,36 +43,28 @@ public static bool IsAddress(this Type t)
4743
4844 public static bool IsPointer ( this Type t )
4945 {
50- var functionPointer = t as MemberPointerType ;
51- if ( functionPointer != null )
46+ if ( t is MemberPointerType )
5247 return true ;
53- var pointer = t as PointerType ;
54- if ( pointer == null )
48+
49+ if ( t is not PointerType pointer )
5550 return false ;
51+
5652 return pointer . Modifier == PointerType . TypeModifier . Pointer ;
5753 }
5854
5955 public static bool IsReference ( this Type t )
6056 {
61- var pointer = t as PointerType ;
62- if ( pointer == null )
63- return false ;
64- return pointer . IsReference ;
57+ return t is PointerType { IsReference : true } ;
6558 }
6659
6760 public static bool IsPointerToPrimitiveType ( this Type t )
6861 {
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 _ ) ;
7463 }
7564
7665 public static bool IsPointerToPrimitiveType ( this Type t , out PrimitiveType primitive )
7766 {
78- var ptr = t as PointerType ;
79- if ( ptr == null )
67+ if ( t is not PointerType ptr )
8068 {
8169 primitive = PrimitiveType . Null ;
8270 return false ;
@@ -86,24 +74,21 @@ public static bool IsPointerToPrimitiveType(this Type t, out PrimitiveType primi
8674
8775 public static bool IsPointerToPrimitiveType ( this Type t , PrimitiveType primitive )
8876 {
89- var ptr = t as PointerType ;
90- if ( ptr == null )
77+ if ( t is not PointerType ptr )
9178 return false ;
9279 return ptr . Pointee . IsPrimitiveType ( primitive ) ;
9380 }
9481
9582 public static bool IsPointerToEnum ( this Type t )
9683 {
97- var ptr = t as PointerType ;
98- if ( ptr == null )
84+ if ( t is not PointerType ptr )
9985 return false ;
10086 return ptr . Pointee . IsEnumType ( ) ;
10187 }
10288
10389 public static bool IsPointerToEnum ( this Type t , out Enumeration @enum )
10490 {
105- var ptr = t as PointerType ;
106- if ( ptr == null )
91+ if ( t is not PointerType ptr )
10792 {
10893 @enum = null ;
10994 return false ;
@@ -113,33 +98,31 @@ public static bool IsPointerToEnum(this Type t, out Enumeration @enum)
11398
11499 public static bool IsPointerTo < T > ( this Type t , out T type ) where T : Type
115100 {
116- var pointee = t . GetPointee ( ) ;
117- type = pointee as T ;
118- if ( type == null )
119- {
120- var attributedType = pointee as AttributedType ;
121- if ( attributedType != null )
122- type = attributedType . Modified . Type as T ;
123- }
101+ type = t . GetPointee ( ) as T ;
102+ if ( type != null )
103+ return true ;
104+
105+ if ( type is AttributedType attributedType )
106+ type = attributedType . Modified . Type as T ;
124107 return type != null ;
125108 }
126109
127110 public static bool IsClass ( this Type t )
128111 {
129- Class @class ;
130- return t . TryGetClass ( out @class ) ;
112+ return t . TryGetClass ( out _ ) ;
131113 }
132114
133115 public static bool TryGetClass ( this Type t , out Class @class , Class value = null )
134116 {
135117 return TryGetDeclaration ( t , out @class , value ) ;
136118 }
137119
138- public static bool TryGetDeclaration < T > ( this Type t , out T decl , T value = null ) where T : Declaration
120+ public static bool TryGetDeclaration < T > ( this Type t , out T decl , T value = null )
121+ where T : Declaration
139122 {
140123 t = t . Desugar ( ) ;
141124
142- TagType tagType = null ;
125+ TagType tagType ;
143126 if ( t is TemplateSpecializationType type )
144127 {
145128 if ( type . IsDependent )
@@ -193,15 +176,12 @@ public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
193176
194177 public static bool IsEnum ( this Type t )
195178 {
196- Enumeration @enum ;
197- return t . TryGetEnum ( out @enum ) ;
179+ return t . TryGetEnum ( out _ ) ;
198180 }
199181
200182 public static bool TryGetEnum ( this Type t , out Enumeration @enum )
201183 {
202- var tag = t . Desugar ( ) as TagType ;
203-
204- if ( tag == null )
184+ if ( t . Desugar ( ) is not TagType tag )
205185 {
206186 @enum = null ;
207187 return false ;
@@ -269,13 +249,12 @@ public static Type SkipPointerRefs(this Type t)
269249 /// </summary>
270250 public static Type GetPointee ( this Type t )
271251 {
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 ;
252+ return t switch
253+ {
254+ PointerType ptr => ptr . Pointee ,
255+ MemberPointerType memberPtr => memberPtr . QualifiedPointee . Type ,
256+ _ => null
257+ } ;
279258 }
280259
281260 /// <summary>
@@ -296,17 +275,28 @@ public static Type GetFinalPointee(this Type t)
296275 return finalPointee ;
297276 }
298277
278+ public static PointerType GetFinalPointer ( this Type t )
279+ {
280+ if ( t is not PointerType type )
281+ return null ;
282+
283+ var pointee = type . Desugar ( ) . GetPointee ( ) ;
284+
285+ if ( pointee . IsPointer ( ) )
286+ return pointee . GetFinalPointer ( ) ;
287+
288+ return type ;
289+ }
290+
299291 /// <summary>
300292 /// If t is a pointer type the type pointed to by t will be returned.
301293 /// Otherwise the default qualified type.
302294 /// </summary>
303295 public static QualifiedType GetQualifiedPointee ( this Type t )
304296 {
305- var ptr = t as PointerType ;
306- if ( ptr != null )
297+ if ( t is PointerType ptr )
307298 return ptr . QualifiedPointee ;
308- var memberPtr = t as MemberPointerType ;
309- if ( memberPtr != null )
299+ if ( t is MemberPointerType memberPtr )
310300 return memberPtr . QualifiedPointee ;
311301 return new QualifiedType ( ) ;
312302 }
@@ -329,31 +319,14 @@ public static QualifiedType GetFinalQualifiedPointee(this Type t)
329319 return finalPointee ;
330320 }
331321
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-
347322 public static bool ResolvesTo ( this QualifiedType type , QualifiedType other )
348323 {
349324 if ( ! type . Qualifiers . Equals ( other . Qualifiers ) )
350325 return false ;
351326
352327 var left = type . Type . Desugar ( ) ;
353328 var right = other . Type . Desugar ( ) ;
354- var leftPointer = left as PointerType ;
355- var rightPointer = right as PointerType ;
356- if ( leftPointer != null && rightPointer != null )
329+ if ( left is PointerType leftPointer && right is PointerType rightPointer )
357330 {
358331 return leftPointer . Modifier == rightPointer . Modifier &&
359332 leftPointer . QualifiedPointee . ResolvesTo ( rightPointer . QualifiedPointee ) ;
@@ -388,8 +361,7 @@ public static QualifiedType StripConst(this QualifiedType type)
388361 qualifiers . IsConst = false ;
389362 type . Qualifiers = qualifiers ;
390363
391- var ptr = type . Type as PointerType ;
392- if ( ptr != null )
364+ if ( type . Type is PointerType ptr )
393365 {
394366 var pointee = ptr . QualifiedPointee ;
395367 var pointeeQualifiers = pointee . Qualifiers ;
0 commit comments