@@ -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,60 +74,57 @@ 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 ;
11095 }
11196 return ptr . Pointee . TryGetEnum ( out @enum ) ;
11297 }
11398
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
115101 {
116- var pointee = t . GetPointee ( ) ;
117- type = pointee as T ;
118- if ( type == null )
102+ type = t . GetPointee ( ) switch
119103 {
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+
124109 return type != null ;
125110 }
126111
127112 public static bool IsClass ( this Type t )
128113 {
129- Class @class ;
130- return t . TryGetClass ( out @class ) ;
114+ return t . TryGetClass ( out _ ) ;
131115 }
132116
133117 public static bool TryGetClass ( this Type t , out Class @class , Class value = null )
134118 {
135119 return TryGetDeclaration ( t , out @class , value ) ;
136120 }
137121
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
139124 {
140125 t = t . Desugar ( ) ;
141126
142- TagType tagType = null ;
127+ TagType tagType ;
143128 if ( t is TemplateSpecializationType type )
144129 {
145130 if ( type . IsDependent )
@@ -150,20 +135,20 @@ public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
150135 type . Desugared . Type . TryGetDeclaration ( out decl , value ) ;
151136 return decl != null ;
152137 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 ;
158143
159- if ( decl == null )
160- return false ;
144+ if ( decl == null )
145+ return false ;
161146
162- if ( value != null )
163- type . Template = new ClassTemplate { TemplatedDecl = value } ;
147+ if ( value != null )
148+ type . Template = new ClassTemplate { TemplatedDecl = value } ;
164149
165- return true ;
166- }
150+ return true ;
151+ }
167152 case TemplateTemplateParameter templateTemplateParameter :
168153 return ( decl = templateTemplateParameter . TemplatedDecl as T ) != null ;
169154 }
@@ -193,15 +178,12 @@ public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
193178
194179 public static bool IsEnum ( this Type t )
195180 {
196- Enumeration @enum ;
197- return t . TryGetEnum ( out @enum ) ;
181+ return t . TryGetEnum ( out _ ) ;
198182 }
199183
200184 public static bool TryGetEnum ( this Type t , out Enumeration @enum )
201185 {
202- var tag = t . Desugar ( ) as TagType ;
203-
204- if ( tag == null )
186+ if ( t . Desugar ( ) is not TagType tag )
205187 {
206188 @enum = null ;
207189 return false ;
@@ -269,13 +251,12 @@ public static Type SkipPointerRefs(this Type t)
269251 /// </summary>
270252 public static Type GetPointee ( this Type t )
271253 {
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+ } ;
279260 }
280261
281262 /// <summary>
@@ -296,17 +277,28 @@ public static Type GetFinalPointee(this Type t)
296277 return finalPointee ;
297278 }
298279
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+
299293 /// <summary>
300294 /// If t is a pointer type the type pointed to by t will be returned.
301295 /// Otherwise the default qualified type.
302296 /// </summary>
303297 public static QualifiedType GetQualifiedPointee ( this Type t )
304298 {
305- var ptr = t as PointerType ;
306- if ( ptr != null )
299+ if ( t is PointerType ptr )
307300 return ptr . QualifiedPointee ;
308- var memberPtr = t as MemberPointerType ;
309- if ( memberPtr != null )
301+ if ( t is MemberPointerType memberPtr )
310302 return memberPtr . QualifiedPointee ;
311303 return new QualifiedType ( ) ;
312304 }
@@ -329,31 +321,14 @@ public static QualifiedType GetFinalQualifiedPointee(this Type t)
329321 return finalPointee ;
330322 }
331323
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-
347324 public static bool ResolvesTo ( this QualifiedType type , QualifiedType other )
348325 {
349326 if ( ! type . Qualifiers . Equals ( other . Qualifiers ) )
350327 return false ;
351328
352329 var left = type . Type . Desugar ( ) ;
353330 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 )
357332 {
358333 return leftPointer . Modifier == rightPointer . Modifier &&
359334 leftPointer . QualifiedPointee . ResolvesTo ( rightPointer . QualifiedPointee ) ;
@@ -388,8 +363,7 @@ public static QualifiedType StripConst(this QualifiedType type)
388363 qualifiers . IsConst = false ;
389364 type . Qualifiers = qualifiers ;
390365
391- var ptr = type . Type as PointerType ;
392- if ( ptr != null )
366+ if ( type . Type is PointerType ptr )
393367 {
394368 var pointee = ptr . QualifiedPointee ;
395369 var pointeeQualifiers = pointee . Qualifiers ;
0 commit comments