-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathType.qll
1914 lines (1634 loc) · 61 KB
/
Type.qll
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Provides a hierarchy of classes for modeling C/C++ types.
*/
import semmle.code.cpp.Element
import semmle.code.cpp.Function
import semmle.code.cpp.TemplateParameter
private import semmle.code.cpp.internal.ResolveClass
/**
* A C/C++ type.
*
* This QL class represents the root of the C/C++ type hierarchy.
*/
class Type extends Locatable, @type {
Type() { isType(underlyingElement(this)) }
/**
* Gets the name of this type.
*/
string getName() { none() }
/**
* Holds if this type is called `name`.
*/
predicate hasName(string name) { name = this.getName() }
/**
* Holds if this declaration has a specifier called `name`, recursively looking
* through `typedef` and `decltype`. For example, in the context of
* `typedef const int *restrict t`, the type `volatile t` has specifiers
* `volatile` and `restrict` but not `const` since the `const` is attached to
* the type being pointed to rather than the pointer itself.
*/
// This predicate should not be overridden, but it cannot be declared `final`
// because there is a similarly-named predicate in Declaration, and UserType
// inherits from both Type and Declaration and must override it to resolve
// the ambiguity.
predicate hasSpecifier(string name) { this.getASpecifier().hasName(name) }
/**
* Gets a specifier of this type, recursively looking through `typedef` and
* `decltype`. For example, in the context of `typedef const int *restrict t`,
* the type `volatile t` has specifiers `volatile` and `restrict` but not
* `const` since the `const` is attached to the type being pointed to rather
* than the pointer itself.
*/
// This predicate should not be overridden, but it cannot be declared `final`
// because there is a similarly-named predicate in Declaration, and UserType
// inherits from both Type and Declaration and must override it to resolve
// the ambiguity.
Specifier getASpecifier() {
typespecifiers(underlyingElement(this), unresolveElement(result))
or
result = this.internal_getAnAdditionalSpecifier()
}
/**
* Gets an attribute of this type.
*/
Attribute getAnAttribute() { typeattributes(underlyingElement(this), unresolveElement(result)) }
/**
* Internal -- should be `protected` when QL supports such a flag. Subtypes
* override this to recursively get specifiers that are not attached directly
* to this `@type` in the database but arise through type aliases such as
* `typedef` and `decltype`.
*/
Specifier internal_getAnAdditionalSpecifier() { none() }
/**
* Holds if this type is const.
*/
predicate isConst() { this.hasSpecifier("const") }
/**
* Holds if this type is volatile.
*/
predicate isVolatile() { this.hasSpecifier("volatile") }
/**
* Holds if this type refers to type `t` (by default,
* a type always refers to itself).
*/
predicate refersTo(Type t) { this.refersToDirectly*(t) }
/**
* Holds if this type refers to type `t` directly.
*/
predicate refersToDirectly(Type t) { none() }
/**
* Gets this type after typedefs have been resolved.
*
* The result of this predicate will be the type itself, except in the case of a TypedefType, a Decltype,
* or a TypeofType, in which case the result will be type which results from (possibly recursively)
* resolving typedefs.
*/
pragma[nomagic]
Type getUnderlyingType() { result = this }
/**
* Gets this type after specifiers have been deeply stripped and typedefs have been resolved.
*
* For example, starting with `const i64* const` in the context of `typedef long long i64;`, this predicate will return `long long*`.
*/
pragma[nomagic]
Type getUnspecifiedType() { unspecifiedtype(underlyingElement(this), unresolveElement(result)) }
/**
* Gets this type after any top-level specifiers and typedefs have been stripped.
*
* For example, starting with `const i64* const`, this predicate will return `const i64*`.
*/
Type stripTopLevelSpecifiers() { result = this }
/**
* Gets the size of this type in bytes.
*/
int getSize() {
builtintypes(underlyingElement(this), _, _, result, _, _) or
pointerishsize(underlyingElement(this), result, _) or
usertypesize(underlyingElement(this), result, _)
}
/**
* Gets the alignment of this type in bytes.
*/
int getAlignment() {
builtintypes(underlyingElement(this), _, _, _, _, result) or
pointerishsize(underlyingElement(this), _, result) or
usertypesize(underlyingElement(this), _, result)
}
/**
* Gets the pointer indirection level of this type.
*/
int getPointerIndirectionLevel() { result = 0 }
/**
* Gets a detailed string representation explaining the AST of this type
* (with all specifiers and nested constructs such as pointers). This is
* intended to help debug queries and is a very expensive operation; not
* to be used in production queries.
*
* An example output is "const {pointer to {const {char}}}".
*/
string explain() { result = "type" } // Concrete base impl to allow filters on Type
/**
* Holds if this type is constant and only contains constant types.
* For instance, a `char *const` is a constant type, but not deeply constant,
* because while the pointer can't be modified the character can. The type
* `const char *const*` is a deeply constant type though - both the pointer
* and what it points to are immutable.
*/
predicate isDeeplyConst() { this.isConst() and this.isDeeplyConstBelow() }
/**
* Holds if this type is constant and only contains constant types, excluding
* the type itself. It is implied by Type.isDeeplyConst() and is just used to
* implement that predicate.
* For example, `const char *const` is deeply constant and deeply constant below,
* but `const char *` is only deeply constant below (the pointer can be changed,
* but not the underlying char). `char *const` is neither (it is just `const`).
*/
predicate isDeeplyConstBelow() { none() } // Concrete base impl to allow filters on Type
/**
* Gets as many places as possible where this type is used by name in the source after macros have been replaced
* (in particular, therefore, this will find type name uses caused by macros). Note that all type name uses within
* instantiations are currently excluded - this is too draconian in the absence of indexing prototype instantiations
* of functions, and is likely to improve in the future. At present, the method takes the conservative approach of
* giving valid type name uses, but not necessarily *all* type name uses.
*/
Element getATypeNameUse() {
// An explicit cast to a type referring to T uses T. We exclude casts within instantiations,
// since they do not appear directly in the source.
exists(Cast c |
not c.isImplicit() and
c.getType().refersTo(this) and
result = c and
not c.getEnclosingFunction().isConstructedFrom(_)
)
or
// A class derivation from a type referring to T uses T. We exclude class derivations within
// instantiations, since they do not appear directly in the source.
exists(ClassDerivation cd |
cd.getBaseType().refersTo(this) and
result = cd and
not cd.getDerivedClass() instanceof ClassTemplateInstantiation
)
or
// A new, new array, or placement new expression with a type that refers to T uses T.
// We exclude news within instantiations, since they do not appear directly in the source.
exists(Expr e |
(
e instanceof NewArrayExpr or
e instanceof NewExpr
) and
e.getType().refersTo(this) and
result = e and
not e.getEnclosingFunction().isConstructedFrom(_)
)
or
// The declaration of a function that returns a type referring to T uses T. We exclude
// declarations of function template instantiations, since their return types do not
// appear directly in the source. We also exclude constructors and destructors, since
// they are indexed with a dummy return type of void that does not appear in the source.
exists(FunctionDeclarationEntry fde, Type t |
(if exists(fde.getTypedefType()) then t = fde.getTypedefType() else t = fde.getType()) and
t.refersTo(this) and
result = fde and
not fde.getDeclaration().isConstructedFrom(_) and
not fde.getDeclaration() instanceof Constructor and
not fde.getDeclaration() instanceof Destructor
)
or
// A function call that provides an explicit template argument that refers to T uses T.
// We exclude calls within instantiations, since they do not appear directly in the source.
exists(FunctionCall c |
c.getAnExplicitTemplateArgument().(Type).refersTo(this) and
result = c and
not c.getEnclosingFunction().isConstructedFrom(_)
)
or
// Qualifying an expression with a type that refers to T uses T. We exclude qualifiers
// within instantiations, since they do not appear directly in the source.
exists(NameQualifier nq |
nq.getQualifyingElement().(Type).refersTo(this) and
result = nq and
not nq.getExpr().getEnclosingFunction().isConstructedFrom(_)
)
or
// Calculating the size of a type that refers to T uses T. We exclude sizeofs within
// instantiations, since they do not appear directly in the source.
exists(SizeofTypeOperator soto |
soto.getTypeOperand().refersTo(this) and
result = soto and
not soto.getEnclosingFunction().isConstructedFrom(_)
)
or
// A typedef of a type that refers to T uses T.
exists(TypeDeclarationEntry tde |
tde.getDeclaration().(TypedefType).getBaseType().refersTo(this) and
result = tde
)
or
// Using something declared within a type that refers to T uses T.
exists(UsingDeclarationEntry ude |
ude.getDeclaration().getDeclaringType().refersTo(this) and
result = ude
)
or
// The declaration of a variable with a type that refers to T uses T. We exclude declarations within
// instantiations, since those do not appear directly in the source.
exists(VariableDeclarationEntry vde |
vde.getType().refersTo(this) and
result = vde and
not exists(LocalScopeVariable sv |
sv = vde.getDeclaration() and sv.getFunction().isConstructedFrom(_)
) and
not exists(MemberVariable mv |
mv = vde.getDeclaration() and mv.getDeclaringType() instanceof ClassTemplateInstantiation
)
)
}
/**
* Holds if this type involves a reference.
*/
predicate involvesReference() { none() }
/**
* Holds if this type involves a template parameter.
*/
predicate involvesTemplateParameter() { none() }
/**
* Gets this type with any typedefs resolved. For example, given
* `typedef C T`, this would resolve `const T&` to `const C&`.
* Note that this will only work if the resolved type actually appears
* on its own elsewhere in the program.
*/
Type resolveTypedefs() { result = this }
/**
* Gets the type stripped of pointers, references and cv-qualifiers, and resolving typedefs.
* For example, given `typedef const C& T`, `stripType` returns `C`.
*/
Type stripType() { result = this }
override Location getLocation() { result instanceof UnknownDefaultLocation }
}
/**
* A C/C++ built-in primitive type (int, float, void, and so on). See 4.1.1.
* In the following example, `unsigned int` and `double` denote primitive
* built-in types:
* ```
* double a;
* unsigned int ua[40];
* typedef double LargeFloat;
* ```
*/
class BuiltInType extends Type, @builtintype {
override string toString() { result = this.getName() }
override string getName() { builtintypes(underlyingElement(this), result, _, _, _, _) }
override string explain() { result = this.getName() }
override predicate isDeeplyConstBelow() { any() } // No subparts
}
/**
* An erroneous type. This type has no corresponding C/C++ syntax.
*
* `ErroneousType` is the type of `ErrorExpr`, which in turn refers to an illegal
* language construct. In the example below, a temporary (`0`) cannot be bound
* to an lvalue reference (`int &`):
* ```
* int &intref = 0;
* ```
*/
class ErroneousType extends BuiltInType {
ErroneousType() { builtintypes(underlyingElement(this), _, 1, _, _, _) }
override string getAPrimaryQlClass() { result = "ErroneousType" }
}
/**
* The unknown type. This type has no corresponding C/C++ syntax.
*
* Unknown types usually occur inside _uninstantiated_ template functions.
* In the example below, the expressions `x.a` and `x.b` have unknown type
* in the _uninstantiated_ template.
* ```
* template<typename T>
* bool check(T x) {
* if (x.a == x.b)
* abort();
* }
* ```
*/
class UnknownType extends BuiltInType {
UnknownType() { builtintypes(underlyingElement(this), _, 2, _, _, _) }
override string getAPrimaryQlClass() { result = "UnknownType" }
}
private predicate isArithmeticType(@builtintype type, int kind) {
builtintypes(type, _, kind, _, _, _) and
kind >= 4 and
kind != 34 // Exclude decltype(nullptr)
}
/**
* The C/C++ arithmetic types. See 4.1.1.
*
* This includes primitive types on which arithmetic, bitwise or logical
* operations may be performed. Examples of arithmetic types include
* `char`, `int`, `float`, and `bool`.
*/
class ArithmeticType extends BuiltInType {
ArithmeticType() { isArithmeticType(underlyingElement(this), _) }
override string getAPrimaryQlClass() { result = "ArithmeticType" }
}
private predicate isIntegralType(@builtintype type, int kind) {
isArithmeticType(type, kind) and
(
kind < 24
or
kind = 33
or
35 <= kind and kind <= 37
or
kind = 43
or
kind = 44
or
kind = 51
)
}
/**
* A C/C++ integral or `enum` type.
*
* The definition of "integral type" in the C++ standard excludes `enum` types,
* but because an `enum` type holds a value of its underlying integral type,
* it is often useful to have a common category that includes both integral
* and `enum` types.
*
* In the following example, `a`, `b` and `c` are all declared with an
* integral or `enum` type:
* ```
* unsigned long a;
* enum e1 { val1, val2 } b;
* enum class e2: short { val3, val4 } c;
* ```
*/
class IntegralOrEnumType extends Type {
IntegralOrEnumType() {
// Integral type
isIntegralType(underlyingElement(this), _)
or
// Enum type
usertypes(underlyingElement(this), _, [4, 13])
}
}
/**
* Maps between different integral types of the same size.
*
* original: The original type. Can be any integral type kind.
* canonical: The canonical form of the type
* - plain T -> T
* - signed T -> T (except signed char -> signed char)
* - unsigned T -> unsigned T
* unsigned: The explicitly unsigned form of the type.
* signed: The explicitly signed form of the type.
*/
private predicate integralTypeMapping(int original, int canonical, int unsigned, int signed) {
original = 4 and canonical = 4 and unsigned = -1 and signed = -1 // bool
or
original = 5 and canonical = 5 and unsigned = 6 and signed = 7 // char
or
original = 6 and canonical = 6 and unsigned = 6 and signed = 7 // unsigned char
or
original = 7 and canonical = 7 and unsigned = 6 and signed = 7 // signed char
or
original = 8 and canonical = 8 and unsigned = 9 and signed = 10 // short
or
original = 9 and canonical = 9 and unsigned = 9 and signed = 10 // unsigned short
or
original = 10 and canonical = 8 and unsigned = 9 and signed = 10 // signed short
or
original = 11 and canonical = 11 and unsigned = 12 and signed = 13 // int
or
original = 12 and canonical = 12 and unsigned = 12 and signed = 13 // unsigned int
or
original = 13 and canonical = 11 and unsigned = 12 and signed = 13 // signed int
or
original = 14 and canonical = 14 and unsigned = 15 and signed = 16 // long
or
original = 15 and canonical = 15 and unsigned = 15 and signed = 16 // unsigned long
or
original = 16 and canonical = 14 and unsigned = 15 and signed = 16 // signed long
or
original = 17 and canonical = 17 and unsigned = 18 and signed = 19 // long long
or
original = 18 and canonical = 18 and unsigned = 18 and signed = 19 // unsigned long long
or
original = 19 and canonical = 17 and unsigned = 18 and signed = 19 // signed long long
or
original = 33 and canonical = 33 and unsigned = -1 and signed = -1 // wchar_t
or
original = 35 and canonical = 35 and unsigned = 36 and signed = 37 // __int128
or
original = 36 and canonical = 36 and unsigned = 36 and signed = 37 // unsigned __int128
or
original = 37 and canonical = 35 and unsigned = 36 and signed = 37 // signed __int128
or
original = 43 and canonical = 43 and unsigned = -1 and signed = -1 // char16_t
or
original = 44 and canonical = 44 and unsigned = -1 and signed = -1 // char32_t
or
original = 51 and canonical = 51 and unsigned = -1 and signed = -1 // char8_t
}
/**
* The C/C++ integral types. See 4.1.1. These are types that are represented
* as integers of varying sizes. Both `enum` types and floating-point types
* are excluded.
*
* In the following examples, `a`, `b` and `c` are declared using integral
* types:
* ```
* unsigned int a;
* long long b;
* char c;
* ```
*/
class IntegralType extends ArithmeticType, IntegralOrEnumType {
int kind;
IntegralType() { isIntegralType(underlyingElement(this), kind) }
/** Holds if this integral type is signed. */
predicate isSigned() { builtintypes(underlyingElement(this), _, _, _, -1, _) }
/** Holds if this integral type is unsigned. */
predicate isUnsigned() { builtintypes(underlyingElement(this), _, _, _, 1, _) }
/** Holds if this integral type is explicitly signed. */
predicate isExplicitlySigned() {
builtintypes(underlyingElement(this), _, 7, _, _, _) or
builtintypes(underlyingElement(this), _, 10, _, _, _) or
builtintypes(underlyingElement(this), _, 13, _, _, _) or
builtintypes(underlyingElement(this), _, 16, _, _, _) or
builtintypes(underlyingElement(this), _, 19, _, _, _) or
builtintypes(underlyingElement(this), _, 37, _, _, _)
}
/** Holds if this integral type is explicitly unsigned. */
predicate isExplicitlyUnsigned() {
builtintypes(underlyingElement(this), _, 6, _, _, _) or
builtintypes(underlyingElement(this), _, 9, _, _, _) or
builtintypes(underlyingElement(this), _, 12, _, _, _) or
builtintypes(underlyingElement(this), _, 15, _, _, _) or
builtintypes(underlyingElement(this), _, 18, _, _, _) or
builtintypes(underlyingElement(this), _, 36, _, _, _)
}
/** Holds if this integral type is implicitly signed. */
predicate isImplicitlySigned() {
builtintypes(underlyingElement(this), _, 5, _, -1, _) or
builtintypes(underlyingElement(this), _, 8, _, -1, _) or
builtintypes(underlyingElement(this), _, 11, _, -1, _) or
builtintypes(underlyingElement(this), _, 14, _, -1, _) or
builtintypes(underlyingElement(this), _, 17, _, -1, _) or
builtintypes(underlyingElement(this), _, 35, _, -1, _)
}
/**
* Gets the unsigned type corresponding to this integral type. For
* example on a `short`, this would give the type `unsigned short`.
*/
IntegralType getUnsigned() {
exists(int unsignedKind |
integralTypeMapping(kind, _, unsignedKind, _) and
builtintypes(unresolveElement(result), _, unsignedKind, _, _, _)
)
}
/**
* Gets the canonical type corresponding to this integral type.
*
* For a plain type, this gives the same type (e.g. `short` -> `short`).
* For an explicitly unsigned type, this gives the same type (e.g. `unsigned short` -> `unsigned short`).
* For an explicitly signed type, this gives the plain version of that type (e.g. `signed short` -> `short`), except
* that `signed char` -> `signed char`.
*/
IntegralType getCanonicalArithmeticType() {
exists(int canonicalKind |
integralTypeMapping(kind, canonicalKind, _, _) and
builtintypes(unresolveElement(result), _, canonicalKind, _, _, _)
)
}
}
/**
* The C/C++ boolean type. See 4.2. This is the C `_Bool` type
* or the C++ `bool` type. For example:
* ```
* extern bool a, b; // C++
* _Bool c, d; // C
* ```
*/
class BoolType extends IntegralType {
BoolType() { builtintypes(underlyingElement(this), _, 4, _, _, _) }
override string getAPrimaryQlClass() { result = "BoolType" }
}
/**
* The C/C++ character types. See 4.3. This includes the `char`,
* `signed char` and `unsigned char` types, all of which are
* distinct from one another. For example:
* ```
* char a, b;
* signed char c, d;
* unsigned char e, f;
* ```
*/
class CharType extends IntegralType {
CharType() { builtintypes(underlyingElement(this), _, [5, 6, 7], _, _, _) }
}
/**
* The C/C++ `char` type (which is distinct from `signed char` and
* `unsigned char`). For example:
* ```
* char a, b;
* ```
*/
class PlainCharType extends CharType {
PlainCharType() { builtintypes(underlyingElement(this), _, 5, _, _, _) }
override string getAPrimaryQlClass() { result = "PlainCharType" }
}
/**
* The C/C++ `unsigned char` type (which is distinct from plain `char`
* even when `char` is `unsigned` by default).
* ```
* unsigned char e, f;
* ```
*/
class UnsignedCharType extends CharType {
UnsignedCharType() { builtintypes(underlyingElement(this), _, 6, _, _, _) }
override string getAPrimaryQlClass() { result = "UnsignedCharType" }
}
/**
* The C/C++ `signed char` type (which is distinct from plain `char`
* even when `char` is `signed` by default).
* ```
* signed char c, d;
* ```
*/
class SignedCharType extends CharType {
SignedCharType() { builtintypes(underlyingElement(this), _, 7, _, _, _) }
override string getAPrimaryQlClass() { result = "SignedCharType" }
}
/**
* The C/C++ short types. See 4.3. This includes `short`, `signed short`
* and `unsigned short`.
* ```
* signed short ss;
* ```
*/
class ShortType extends IntegralType {
ShortType() {
builtintypes(underlyingElement(this), _, 8, _, _, _) or
builtintypes(underlyingElement(this), _, 9, _, _, _) or
builtintypes(underlyingElement(this), _, 10, _, _, _)
}
override string getAPrimaryQlClass() { result = "ShortType" }
}
/**
* The C/C++ integer types. See 4.4. This includes `int`, `signed int`
* and `unsigned int`.
* ```
* unsigned int ui;
* ```
*/
class IntType extends IntegralType {
IntType() {
builtintypes(underlyingElement(this), _, 11, _, _, _) or
builtintypes(underlyingElement(this), _, 12, _, _, _) or
builtintypes(underlyingElement(this), _, 13, _, _, _)
}
override string getAPrimaryQlClass() { result = "IntType" }
}
/**
* The C/C++ long types. See 4.4. This includes `long`, `signed long`
* and `unsigned long`.
* ```
* long l;
* ```
*/
class LongType extends IntegralType {
LongType() {
builtintypes(underlyingElement(this), _, 14, _, _, _) or
builtintypes(underlyingElement(this), _, 15, _, _, _) or
builtintypes(underlyingElement(this), _, 16, _, _, _)
}
override string getAPrimaryQlClass() { result = "LongType" }
}
/**
* The C/C++ long long types. See 4.4. This includes `long long`, `signed long long`
* and `unsigned long long`.
* ```
* signed long long sll;
* ```
*/
class LongLongType extends IntegralType {
LongLongType() {
builtintypes(underlyingElement(this), _, 17, _, _, _) or
builtintypes(underlyingElement(this), _, 18, _, _, _) or
builtintypes(underlyingElement(this), _, 19, _, _, _)
}
override string getAPrimaryQlClass() { result = "LongLongType" }
}
/**
* The GNU C __int128 primitive types. They are not part of standard C/C++.
*
* This includes `__int128`, `signed __int128` and `unsigned __int128`.
* ```
* unsigned __int128 ui128;
* ```
*/
class Int128Type extends IntegralType {
Int128Type() {
builtintypes(underlyingElement(this), _, 35, _, _, _) or
builtintypes(underlyingElement(this), _, 36, _, _, _) or
builtintypes(underlyingElement(this), _, 37, _, _, _)
}
override string getAPrimaryQlClass() { result = "Int128Type" }
}
private newtype TTypeDomain =
TRealDomain() or
TComplexDomain() or
TImaginaryDomain()
/**
* The type domain of a floating-point type. One of `RealDomain`, `ComplexDomain`, or
* `ImaginaryDomain`.
*/
class TypeDomain extends TTypeDomain {
/** Gets a textual representation of this type domain. */
string toString() { none() }
}
/**
* The type domain of a floating-point type that represents a real number.
*/
class RealDomain extends TypeDomain, TRealDomain {
final override string toString() { result = "real" }
}
/**
* The type domain of a floating-point type that represents a complex number.
*/
class ComplexDomain extends TypeDomain, TComplexDomain {
final override string toString() { result = "complex" }
}
/**
* The type domain of a floating-point type that represents an imaginary number.
*/
class ImaginaryDomain extends TypeDomain, TImaginaryDomain {
final override string toString() { result = "imaginary" }
}
/**
* Data for floating-point types.
*
* kind: The original type kind. Can be any floating-point type kind.
* base: The numeric base of the number's representation. Can be 2 (binary) or 10 (decimal).
* domain: The type domain of the type. Can be `RealDomain`, `ComplexDomain`, or `ImaginaryDomain`.
* realKind: The type kind of the corresponding real type. For example, the corresponding real type
* of `_Complex double` is `double`.
* extended: `true` if the number is an extended-precision floating-point number, such as
* `_Float32x`.
*/
private predicate floatingPointTypeMapping(
int kind, int base, TTypeDomain domain, int realKind, boolean extended
) {
// float
kind = 24 and base = 2 and domain = TRealDomain() and realKind = 24 and extended = false
or
// double
kind = 25 and base = 2 and domain = TRealDomain() and realKind = 25 and extended = false
or
// long double
kind = 26 and base = 2 and domain = TRealDomain() and realKind = 26 and extended = false
or
// _Complex float
kind = 27 and base = 2 and domain = TComplexDomain() and realKind = 24 and extended = false
or
// _Complex double
kind = 28 and base = 2 and domain = TComplexDomain() and realKind = 25 and extended = false
or
// _Complex long double
kind = 29 and base = 2 and domain = TComplexDomain() and realKind = 26 and extended = false
or
// _Imaginary float
kind = 30 and base = 2 and domain = TImaginaryDomain() and realKind = 24 and extended = false
or
// _Imaginary double
kind = 31 and base = 2 and domain = TImaginaryDomain() and realKind = 25 and extended = false
or
// _Imaginary long double
kind = 32 and base = 2 and domain = TImaginaryDomain() and realKind = 26 and extended = false
or
// __float128
kind = 38 and base = 2 and domain = TRealDomain() and realKind = 38 and extended = false
or
// _Complex __float128
kind = 39 and base = 2 and domain = TComplexDomain() and realKind = 38 and extended = false
or
// _Decimal32
kind = 40 and base = 10 and domain = TRealDomain() and realKind = 40 and extended = false
or
// _Decimal64
kind = 41 and base = 10 and domain = TRealDomain() and realKind = 41 and extended = false
or
// _Decimal128
kind = 42 and base = 10 and domain = TRealDomain() and realKind = 42 and extended = false
or
// _Float32
kind = 45 and base = 2 and domain = TRealDomain() and realKind = 45 and extended = false
or
// _Float32x
kind = 46 and base = 2 and domain = TRealDomain() and realKind = 46 and extended = true
or
// _Float64
kind = 47 and base = 2 and domain = TRealDomain() and realKind = 47 and extended = false
or
// _Float64x
kind = 48 and base = 2 and domain = TRealDomain() and realKind = 48 and extended = true
or
// _Float128
kind = 49 and base = 2 and domain = TRealDomain() and realKind = 49 and extended = false
or
// _Float16
kind = 52 and base = 2 and domain = TRealDomain() and realKind = 52 and extended = false
or
// _Complex _Float16
kind = 53 and base = 2 and domain = TComplexDomain() and realKind = 52 and extended = false
or
// __fp16
kind = 54 and base = 2 and domain = TRealDomain() and realKind = 54 and extended = false
or
// __bf16
kind = 55 and base = 2 and domain = TRealDomain() and realKind = 55 and extended = false
or
// std::float16_t
kind = 56 and base = 2 and domain = TRealDomain() and realKind = 56 and extended = false
or
// _Complex _Float32
kind = 57 and base = 2 and domain = TComplexDomain() and realKind = 45 and extended = false
or
// _Complex _Float32x
kind = 58 and base = 2 and domain = TComplexDomain() and realKind = 46 and extended = true
or
// _Complex _Float64
kind = 59 and base = 2 and domain = TComplexDomain() and realKind = 47 and extended = false
or
// _Complex _Float64x
kind = 60 and base = 2 and domain = TComplexDomain() and realKind = 48 and extended = true
or
// _Complex _Float128
kind = 61 and base = 2 and domain = TComplexDomain() and realKind = 49 and extended = false
}
/**
* The C/C++ floating point types. See 4.5. This includes `float`, `double` and `long double`, the
* fixed-size floating-point types like `_Float32`, the extended-precision floating-point types like
* `_Float64x`, and the decimal floating-point types like `_Decimal32`. It also includes the complex
* and imaginary versions of all of these types.
*/
class FloatingPointType extends ArithmeticType {
final int base;
final TypeDomain domain;
final int realKind;
final boolean extended;
FloatingPointType() {
exists(int kind |
builtintypes(underlyingElement(this), _, kind, _, _, _) and
floatingPointTypeMapping(kind, base, domain, realKind, extended)
)
}
/** Gets the numeric base of this type's representation: 2 (binary) or 10 (decimal). */
final int getBase() { result = base }
/**
* Gets the type domain of this type. Can be `RealDomain`, `ComplexDomain`, or `ImaginaryDomain`.
*/
final TypeDomain getDomain() { result = domain }
/**
* Gets the corresponding real type of this type. For example, the corresponding real type of
* `_Complex double` is `double`.
*/
final RealNumberType getRealType() {
builtintypes(unresolveElement(result), _, realKind, _, _, _)
}
/** Holds if this type is an extended precision floating-point type, such as `_Float32x`. */
final predicate isExtendedPrecision() { extended = true }
}
/**
* A floating-point type representing a real number.
*/
class RealNumberType extends FloatingPointType {
RealNumberType() { domain instanceof RealDomain }
}
/**
* A floating-point type representing a complex number.
*/
class ComplexNumberType extends FloatingPointType {
ComplexNumberType() { domain instanceof ComplexDomain }
}
/**
* A floating-point type representing an imaginary number.
*/
class ImaginaryNumberType extends FloatingPointType {
ImaginaryNumberType() { domain instanceof ImaginaryDomain }
}
/**
* A floating-point type whose representation is base 2.
*/
class BinaryFloatingPointType extends FloatingPointType {
BinaryFloatingPointType() { base = 2 }
}
/**
* A floating-point type whose representation is base 10.
*/
class DecimalFloatingPointType extends FloatingPointType {
DecimalFloatingPointType() { base = 10 }
}
/**
* The C/C++ `float` type.
* ```
* float f;
* ```
*/
class FloatType extends RealNumberType, BinaryFloatingPointType {
FloatType() { builtintypes(underlyingElement(this), _, 24, _, _, _) }
override string getAPrimaryQlClass() { result = "FloatType" }
}
/**
* The C/C++ `double` type.
* ```
* double d;
* ```
*/
class DoubleType extends RealNumberType, BinaryFloatingPointType {
DoubleType() { builtintypes(underlyingElement(this), _, 25, _, _, _) }
override string getAPrimaryQlClass() { result = "DoubleType" }
}
/**
* The C/C++ `long double` type.
* ```
* long double ld;
* ```
*/
class LongDoubleType extends RealNumberType, BinaryFloatingPointType {
LongDoubleType() { builtintypes(underlyingElement(this), _, 26, _, _, _) }
override string getAPrimaryQlClass() { result = "LongDoubleType" }
}
/**
* The GNU C `__float128` primitive type. This is not standard C/C++.
* ```
* __float128 f128;
* ```
*/
class Float128Type extends RealNumberType, BinaryFloatingPointType {
Float128Type() { builtintypes(underlyingElement(this), _, 38, _, _, _) }
override string getAPrimaryQlClass() { result = "Float128Type" }
}
/**
* The GNU C `_Decimal32` primitive type. This is not standard C/C++.
* ```
* _Decimal32 d32;
* ```
*/
class Decimal32Type extends RealNumberType, DecimalFloatingPointType {
Decimal32Type() { builtintypes(underlyingElement(this), _, 40, _, _, _) }
override string getAPrimaryQlClass() { result = "Decimal32Type" }
}
/**
* The GNU C `_Decimal64` primitive type. This is not standard C/C++.
* ```
* _Decimal64 d64;
* ```
*/
class Decimal64Type extends RealNumberType, DecimalFloatingPointType {
Decimal64Type() { builtintypes(underlyingElement(this), _, 41, _, _, _) }
override string getAPrimaryQlClass() { result = "Decimal64Type" }
}
/**
* The GNU C `_Decimal128` primitive type. This is not standard C/C++.
* ```
* _Decimal128 d128;
* ```
*/
class Decimal128Type extends RealNumberType, DecimalFloatingPointType {
Decimal128Type() { builtintypes(underlyingElement(this), _, 42, _, _, _) }
override string getAPrimaryQlClass() { result = "Decimal128Type" }
}