-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
481 lines (405 loc) · 15.2 KB
/
TypeExtensions.cs
File metadata and controls
481 lines (405 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
namespace CppSharp.AST.Extensions
{
public static class TypeExtensions
{
public static bool IsPrimitiveType(this Type t)
{
return t.IsPrimitiveType(out PrimitiveType _);
}
public static bool IsPrimitiveType(this Type t, out PrimitiveType primitive)
{
if (t.Desugar() is BuiltinType builtin)
{
primitive = builtin.Type;
return true;
}
primitive = PrimitiveType.Null;
return false;
}
public static bool IsPrimitiveType(this Type t, PrimitiveType primitive)
{
PrimitiveType type;
if (!t.IsPrimitiveType(out type))
return false;
return primitive == type;
}
public static bool IsEnumType(this Type t)
{
if (t.Desugar() is not TagType tag)
return false;
return tag.Declaration is Enumeration;
}
public static bool IsAddress(this Type t)
{
return t.IsPointer() || t.IsReference();
}
public static bool IsPointer(this Type t)
{
if (t is MemberPointerType)
return true;
if (t is not PointerType pointer)
return false;
return pointer.Modifier == PointerType.TypeModifier.Pointer;
}
public static bool IsReference(this Type t)
{
return t is PointerType { IsReference: true };
}
public static bool IsPointerToPrimitiveType(this Type t)
{
return t is PointerType ptr && ptr.Pointee.IsPrimitiveType(out _);
}
public static bool IsPointerToPrimitiveType(this Type t, out PrimitiveType primitive)
{
if (t is not PointerType ptr)
{
primitive = PrimitiveType.Null;
return false;
}
return ptr.Pointee.IsPrimitiveType(out primitive);
}
public static bool IsPointerToPrimitiveType(this Type t, PrimitiveType primitive)
{
if (t is not PointerType ptr)
return false;
return ptr.Pointee.IsPrimitiveType(primitive);
}
public static bool IsPointerToEnum(this Type t)
{
if (t is not PointerType ptr)
return false;
return ptr.Pointee.IsEnumType();
}
public static bool IsPointerToEnum(this Type t, out Enumeration @enum)
{
if (t is not PointerType ptr)
{
@enum = null;
return false;
}
return ptr.Pointee.TryGetEnum(out @enum);
}
public static bool IsPointerTo<T>(this Type t, out T type)
where T : Type
{
type = t.GetPointee() switch
{
T tType => tType,
AttributedType attributedType => attributedType.Modified.Type as T,
_ => null
};
return type != null;
}
public static bool IsClass(this Type t)
{
return t.TryGetClass(out _);
}
public static bool TryGetClass(this Type t, out Class @class, Class value = null)
{
return TryGetDeclaration(t, out @class, value);
}
public static bool TryGetDeclaration<T>(this Type t, out T decl, T value = null)
where T : Declaration
{
t = t.Desugar();
TagType tagType;
if (t is TemplateSpecializationType type)
{
if (type.IsDependent)
{
switch (type.Template)
{
case TypeAliasTemplate _:
type.Desugared.Type.TryGetDeclaration(out decl, value);
return decl != null;
case ClassTemplate classTemplate:
{
var templatedClass = classTemplate.TemplatedClass;
decl = templatedClass.CompleteDeclaration == null
? templatedClass as T
: (T)templatedClass.CompleteDeclaration;
if (decl == null)
return false;
if (value != null)
type.Template = new ClassTemplate { TemplatedDecl = value };
return true;
}
case TemplateTemplateParameter templateTemplateParameter:
return (decl = templateTemplateParameter.TemplatedDecl as T) != null;
}
}
tagType = (type.Desugared.Type.GetFinalPointee() ?? type.Desugared.Type) as TagType;
}
else
{
tagType = t as TagType;
}
if (tagType != null)
{
decl = tagType.Declaration as T;
if (decl != null)
{
if (value != null)
tagType.Declaration = value;
return true;
}
return false;
}
decl = null;
return false;
}
public static bool IsEnum(this Type t)
{
return t.TryGetEnum(out _);
}
public static bool TryGetEnum(this Type t, out Enumeration @enum)
{
if (t.Desugar() is not TagType tag)
{
@enum = null;
return false;
}
@enum = tag.Declaration as Enumeration;
return @enum != null;
}
public static Type Desugar(this Type t, bool resolveTemplateSubstitution = true)
{
var typeDef = t as TypedefType;
if (typeDef != null)
{
var decl = typeDef.Declaration.Type;
if (decl != null)
return decl.Desugar(resolveTemplateSubstitution);
}
if (resolveTemplateSubstitution)
{
var substType = t as TemplateParameterSubstitutionType;
if (substType != null)
{
var replacement = substType.Replacement.Type;
if (replacement != null)
return replacement.Desugar(resolveTemplateSubstitution);
}
}
var injectedType = t as InjectedClassNameType;
if (injectedType != null)
{
if (injectedType.InjectedSpecializationType.Type != null)
return injectedType.InjectedSpecializationType.Type.Desugar(
resolveTemplateSubstitution);
return new TagType(injectedType.Class);
}
var attributedType = t as AttributedType;
if (attributedType != null)
return attributedType.Equivalent.Type.Desugar(resolveTemplateSubstitution);
return t;
}
public static Type SkipPointerRefs(this Type t)
{
var type = t as PointerType;
if (type != null)
{
var pointee = type.Pointee;
if (type.IsReference())
return pointee.Desugar().SkipPointerRefs();
}
return t;
}
/// <summary>
/// If t is a pointer type the type pointed to by t will be returned.
/// Otherwise null.
/// </summary>
public static Type GetPointee(this Type t)
{
return t switch
{
PointerType ptr => ptr.Pointee,
MemberPointerType memberPtr => memberPtr.QualifiedPointee.Type,
_ => null
};
}
/// <summary>
/// If t is a pointer type the type pointed to by t will be returned
/// after fully dereferencing it. Otherwise null.
/// For example int** -> int.
/// </summary>
public static Type GetFinalPointee(this Type t)
{
var finalPointee = t.GetPointee();
var pointee = finalPointee;
while (pointee != null)
{
pointee = pointee.GetPointee();
if (pointee != null)
finalPointee = pointee;
}
return finalPointee;
}
public static PointerType GetFinalPointer(this Type t)
{
if (t is not PointerType type)
return null;
var pointee = type.Desugar().GetPointee();
if (pointee.IsPointer())
return pointee.GetFinalPointer();
return type;
}
/// <summary>
/// If t is a pointer type the type pointed to by t will be returned.
/// Otherwise the default qualified type.
/// </summary>
public static QualifiedType GetQualifiedPointee(this Type t)
{
if (t is PointerType ptr)
return ptr.QualifiedPointee;
if (t is MemberPointerType memberPtr)
return memberPtr.QualifiedPointee;
return new QualifiedType();
}
/// <summary>
/// If t is a pointer type the type pointed to by t will be returned
/// after fully dereferencing it. Otherwise the default qualified type.
/// For example int** -> int.
/// </summary>
public static QualifiedType GetFinalQualifiedPointee(this Type t)
{
var finalPointee = t.GetQualifiedPointee();
var pointee = finalPointee;
while (pointee.Type != null)
{
pointee = pointee.Type.GetQualifiedPointee();
if (pointee.Type != null)
finalPointee = pointee;
}
return finalPointee;
}
public static bool ResolvesTo(this QualifiedType type, QualifiedType other)
{
if (!type.Qualifiers.Equals(other.Qualifiers))
return false;
var left = type.Type.Desugar();
var right = other.Type.Desugar();
if (left is PointerType leftPointer && right is PointerType rightPointer)
{
return leftPointer.Modifier == rightPointer.Modifier &&
leftPointer.QualifiedPointee.ResolvesTo(rightPointer.QualifiedPointee);
}
return left.Equals(right);
}
public static bool IsConstRef(this QualifiedType type)
{
Type desugared = type.Type.Desugar();
return desugared.IsReference() && type.IsConst();
}
public static bool IsConstRefToPrimitive(this QualifiedType type)
{
Type desugared = type.Type.Desugar();
Type pointee = desugared.GetFinalPointee().Desugar();
pointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
return desugared.IsReference() &&
(pointee.IsPrimitiveType() || pointee.IsEnum()) && type.IsConst();
}
public static bool IsConst(this QualifiedType type)
{
return type.Type != null && (type.Qualifiers.IsConst ||
type.Type.GetQualifiedPointee().IsConst());
}
public static QualifiedType StripConst(this QualifiedType type)
{
var qualifiers = type.Qualifiers;
qualifiers.IsConst = false;
type.Qualifiers = qualifiers;
if (type.Type is PointerType ptr)
{
var pointee = ptr.QualifiedPointee;
var pointeeQualifiers = pointee.Qualifiers;
pointeeQualifiers.IsConst = false;
pointee.Qualifiers = pointeeQualifiers;
ptr.QualifiedPointee = pointee;
}
return type;
}
public static bool IsConstCharString(this Type type)
{
var desugared = type.Desugar();
if (!(desugared is PointerType))
return false;
var pointer = desugared as PointerType;
return IsConstCharString(pointer);
}
public static bool IsConstCharString(this PointerType pointer)
{
if (pointer.IsReference)
return false;
var pointee = pointer.Pointee.Desugar();
return (pointee.IsPrimitiveType(PrimitiveType.Char) ||
pointee.IsPrimitiveType(PrimitiveType.Char16) ||
pointee.IsPrimitiveType(PrimitiveType.Char32) ||
pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst;
}
public static bool IsDependentPointer(this Type type)
{
var desugared = type.Desugar();
if (desugared.IsAddress())
{
var pointee = desugared.GetFinalPointee().Desugar();
return pointee.IsDependent
&& !(pointee is TemplateSpecializationType)
&& !(pointee is InjectedClassNameType);
}
return false;
}
public static bool IsTemplateParameterType(this Type type)
{
if (type is TemplateParameterType or TemplateParameterSubstitutionType)
return true;
if (type is PointerType pt)
return pt.GetFinalPointee() is TemplateParameterType or TemplateParameterSubstitutionType;
return false;
}
public static Module GetModule(this Type type)
{
Declaration declaration;
if (!(type.GetFinalPointee() ?? type).TryGetDeclaration(out declaration))
return null;
declaration = declaration.CompleteDeclaration ?? declaration;
if (declaration.Namespace == null || declaration.TranslationUnit.Module == null)
return null;
return declaration.TranslationUnit.Module;
}
public static long GetSizeInBytes(this ArrayType array)
{
return GetSizeInBits(array) / 8;
}
public static long GetSizeInBits(this ArrayType array)
{
return array.Size * array.ElementSize;
}
internal static bool TryGetReferenceToPtrToClass(this Type type, out Type classType)
{
classType = null;
var @ref = type.Desugar().AsLvReference();
if (@ref == null)
return false;
var @ptr = @ref.Pointee.Desugar(false).AsPtr();
if (@ptr == null)
return false;
var @class = @ptr.Pointee;
if (!@class.IsClass())
return false;
classType = @class;
return true;
}
internal static PointerType AsLvReference(this Type type)
{
var reference = type as PointerType;
return reference?.Modifier == PointerType.TypeModifier.LVReference ? reference : null;
}
internal static PointerType AsPtr(this Type type)
{
var ptr = type as PointerType;
return ptr?.Modifier == PointerType.TypeModifier.Pointer ? ptr : null;
}
}
}