-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathExpr.qll
More file actions
2703 lines (2265 loc) · 93.5 KB
/
Expr.qll
File metadata and controls
2703 lines (2265 loc) · 93.5 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
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 classes for working with Java expressions.
*/
overlay[local?]
module;
import java
private import semmle.code.java.frameworks.android.Compose
private import semmle.code.java.Constants
/** A common super-class that represents all kinds of expressions. */
class Expr extends ExprParent, @expr {
/*abstract*/ override string toString() { result = "expr" }
/**
* Gets the callable in which this expression occurs, if any.
*/
Callable getEnclosingCallable() { callableEnclosingExpr(this, result) }
/** Gets the index of this expression as a child of its parent. */
int getIndex() { exprs(this, _, _, _, result) }
/** Gets the parent of this expression. */
ExprParent getParent() { exprs(this, _, _, result, _) }
/** Holds if this expression is the child of the specified parent at the specified (zero-based) position. */
predicate isNthChildOf(ExprParent parent, int index) { exprs(this, _, _, parent, index) }
/** Gets the type of this expression. */
Type getType() { exprs(this, _, result, _, _) }
/** Gets the Kotlin type of this expression. */
KotlinType getKotlinType() { exprsKotlinType(this, result) }
/** Gets the compilation unit in which this expression occurs. */
CompilationUnit getCompilationUnit() { result = this.getFile() }
/**
* Gets the kind of this expression.
*
* Each kind of expression has a unique (integer) identifier.
* This is an implementation detail that should not normally
* be referred to by library users, since the kind of an expression
* is also represented by its QL type.
*
* In a few rare situations, referring to the kind of an expression
* via its unique identifier may be appropriate; for example, when
* comparing whether two expressions have the same kind (as opposed
* to checking whether an expression has a particular kind).
*/
int getKind() { exprs(this, result, _, _, _) }
/** Gets the statement containing this expression, if any. */
Stmt getEnclosingStmt() { statementEnclosingExpr(this, result) }
/**
* Gets a statement that directly or transitively contains this expression, if any.
* This is equivalent to `this.getEnclosingStmt().getEnclosingStmt*()`.
*/
Stmt getAnEnclosingStmt() { result = this.getEnclosingStmt().getEnclosingStmt*() }
/** Gets a child of this expression. */
Expr getAChildExpr() { exprs(result, _, _, this, _) }
/** Gets the basic block in which this expression occurs, if any. */
BasicBlock getBasicBlock() { result.getANode().asExpr() = this }
/** Gets the `ControlFlowNode` corresponding to this expression. */
ControlFlowNode getControlFlowNode() { result.asExpr() = this }
/** This statement's Halstead ID (used to compute Halstead metrics). */
string getHalsteadID() { result = this.toString() }
/**
* Holds if this expression is a compile-time constant.
*
* See JLS v8, section 15.28 (Constant Expressions).
*/
predicate isCompileTimeConstant() { this instanceof CompileTimeConstantExpr }
/** Holds if this expression occurs in a static context. */
predicate isInStaticContext() {
/*
* JLS 8.1.3 (Inner Classes and Enclosing Instances)
* A statement or expression occurs in a static context if and only if the
* innermost method, constructor, instance initializer, static initializer,
* field initializer, or explicit constructor invocation statement
* enclosing the statement or expression is a static method, a static
* initializer, the variable initializer of a static variable, or an
* explicit constructor invocation statement.
*/
this.getEnclosingCallable().isStatic()
or
this.getParent+() instanceof ThisConstructorInvocationStmt
or
this.getParent+() instanceof SuperConstructorInvocationStmt
or
exists(LambdaExpr lam |
lam.asMethod() = this.getEnclosingCallable() and lam.isInStaticContext()
)
}
/** Holds if this expression is parenthesized. */
predicate isParenthesized() { isParenthesized(this, _) }
/**
* Gets the underlying expression looking through casts and not-nulls, if any.
* Otherwise just gets this expression.
*/
Expr getUnderlyingExpr() {
if this instanceof CastingExpr or this instanceof NotNullExpr
then
result = this.(CastingExpr).getExpr().getUnderlyingExpr() or
result = this.(NotNullExpr).getExpr().getUnderlyingExpr()
else result = this
}
}
/**
* Holds if the specified type is either a primitive type or type `String`.
*
* Auxiliary predicate used by `CompileTimeConstantExpr`.
*/
private predicate primitiveOrString(Type t) {
t instanceof PrimitiveType or
t instanceof TypeString
}
/**
* A compile-time constant expression.
*
* See JLS v8, section 15.28 (Constant Expressions).
*/
class CompileTimeConstantExpr extends Expr {
CompileTimeConstantExpr() {
primitiveOrString(this.getType()) and
(
// Literals of primitive type and literals of type `String`.
this instanceof Literal
or
// Casts to primitive types and casts to type `String`.
this.(CastingExpr).getExpr().isCompileTimeConstant()
or
// The unary operators `+`, `-`, `~`, and `!` (but not `++` or `--`).
this.(PlusExpr).getExpr().isCompileTimeConstant()
or
this.(MinusExpr).getExpr().isCompileTimeConstant()
or
this.(BitNotExpr).getExpr().isCompileTimeConstant()
or
this.(LogNotExpr).getExpr().isCompileTimeConstant()
or
// The multiplicative operators `*`, `/`, and `%`,
// the additive operators `+` and `-`,
// the shift operators `<<`, `>>`, and `>>>`,
// the relational operators `<`, `<=`, `>`, and `>=` (but not `instanceof`),
// the equality operators `==` and `!=`,
// the bitwise and logical operators `&`, `^`, and `|`,
// the conditional-and operator `&&` and the conditional-or operator `||`.
// These are precisely the operators represented by `BinaryExpr`.
this.(BinaryExpr).getLeftOperand().isCompileTimeConstant() and
this.(BinaryExpr).getRightOperand().isCompileTimeConstant()
or
// The ternary conditional operator ` ? : `.
exists(ConditionalExpr e | this = e |
e.getCondition().isCompileTimeConstant() and
e.getTrueExpr().isCompileTimeConstant() and
e.getFalseExpr().isCompileTimeConstant()
)
or
// Access to a final variable initialized by a compile-time constant.
exists(Variable v | this = v.getAnAccess() |
v.isFinal() and
v.getInitializer().isCompileTimeConstant()
)
or
this instanceof LiveLiteral
)
}
/**
* Gets the string value of this expression, where possible.
*/
cached
string getStringValue() {
result = this.(StringLiteral).getValue()
or
result =
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringValue()
or
// Ternary conditional, with compile-time constant condition.
exists(ConditionalExpr ce, boolean condition |
ce = this and
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue() and
result = ce.getBranchExpr(condition).(CompileTimeConstantExpr).getStringValue()
)
or
exists(Variable v | this = v.getAnAccess() |
result = v.getInitializer().(CompileTimeConstantExpr).getStringValue()
)
or
result = this.(LiveLiteral).getValue().getStringValue()
}
/**
* Gets the boolean value of this expression, where possible.
*/
cached
boolean getBooleanValue() {
// Literal value.
result = this.(BooleanLiteral).getBooleanValue()
or
result = CalcCompileTimeConstants::calculateBooleanValue(this)
or
// Handle binary expressions that have `String` operands and a boolean result.
exists(BinaryExpr b, string left, string right |
b = this and
left = b.getLeftOperand().(CompileTimeConstantExpr).getStringValue() and
right = b.getRightOperand().(CompileTimeConstantExpr).getStringValue()
|
/*
* JLS 15.28 specifies that compile-time `String` constants are interned. Therefore `==`
* equality can be interpreted as equality over the constant values, not the references.
*
* Kotlin's `==` and `===` operators will return the same result for `String`s, so they
* can be handled alike:
*/
(
b instanceof ValueOrReferenceEqualsExpr and
if left = right then result = true else result = false
)
or
(
b instanceof ValueOrReferenceNotEqualsExpr and
if left != right then result = true else result = false
)
)
or
// Note: no `getFloatValue()`, so we cannot support binary expressions with float or double operands.
result = this.(LiveLiteral).getValue().getBooleanValue()
}
/**
* Gets the integer value of this expression, where possible.
*
* Note that this does not handle the following cases:
*
* - values of type `long`.
*/
cached
int getIntValue() {
exists(IntegralType t | this.getType() = t | t.getName().toLowerCase() != "long") and
(
result = this.(IntegerLiteral).getIntValue()
or
result = this.(CharacterLiteral).getCodePointValue()
)
or
result = CalcCompileTimeConstants::calculateIntValue(this)
or
result = this.(LiveLiteral).getValue().getIntValue()
}
}
private boolean getBoolValue(Expr e) { result = e.(CompileTimeConstantExpr).getBooleanValue() }
private int getIntValue(Expr e) { result = e.(CompileTimeConstantExpr).getIntValue() }
private module CalcCompileTimeConstants = CalculateConstants<getBoolValue/1, getIntValue/1>;
/** An expression parent is an element that may have an expression as its child. */
class ExprParent extends @exprparent, Top { }
/**
* An error expression.
*
* These may be generated by upgrade or downgrade scripts when databases
* cannot be fully converted, or generated by the extractor when extracting
* source code containing errors.
*/
class ErrorExpr extends Expr, @errorexpr {
override string toString() { result = "<error expr>" }
override string getAPrimaryQlClass() { result = "ErrorExpr" }
}
/**
* An array access.
*
* For example, `a[i++]` is an array access, where
* `a` is the accessed array and `i++` is
* the index expression of the array access.
*/
class ArrayAccess extends Expr, @arrayaccess {
/** Gets the array that is accessed in this array access. */
Expr getArray() { result.isNthChildOf(this, 0) }
/** Gets the index expression of this array access. */
Expr getIndexExpr() { result.isNthChildOf(this, 1) }
override string toString() { result = "...[...]" }
override string getAPrimaryQlClass() { result = "ArrayAccess" }
}
/**
* An array creation expression.
*
* For example, an expression such as `new String[2][3]` or
* `new String[][] { { "a", "b", "c" } , { "d", "e", "f" } }`.
*
* In both examples, `String` is the type name. In the first
* example, `2` and `3` are the 0th and 1st dimensions,
* respectively. In the second example,
* `{ { "a", "b", "c" } , { "d", "e", "f" } }` is the initializer.
*/
class ArrayCreationExpr extends Expr, @arraycreationexpr {
/** Gets a dimension of this array creation expression. */
Expr getADimension() { result.getParent() = this and result.getIndex() >= 0 }
/** Gets the dimension of this array creation expression at the specified (zero-based) position. */
Expr getDimension(int index) {
result = this.getADimension() and
result.getIndex() = index
}
/** Gets the initializer of this array creation expression, if any. */
ArrayInit getInit() { result.isNthChildOf(this, -2) }
/**
* Gets the size of the first dimension, if it can be statically determined.
*/
int getFirstDimensionSize() {
if exists(this.getInit())
then result = this.getInit().getSize()
else result = this.getDimension(0).(CompileTimeConstantExpr).getIntValue()
}
/** Gets a printable representation of this expression. */
override string toString() { result = "new " + this.getType().toString() }
override string getAPrimaryQlClass() { result = "ArrayCreationExpr" }
}
/**
* An array initializer consisting of an opening and closing curly bracket and
* optionally containing expressions (which themselves can be array initializers)
* representing the elements of the array. For example: `{ 'a', 'b' }`.
*
* This expression type matches array initializers representing the values for
* annotation elements as well, despite the Java Language Specification considering
* them a separate type, `ElementValueArrayInitializer`. It does however not match
* values for an array annotation element which consist of a single element
* without enclosing curly brackets (as per JLS).
*/
class ArrayInit extends Expr, @arrayinit {
/**
* An expression occurring in this initializer.
* This may either be an initializer itself or an
* expression representing an element of the array,
* depending on the level of nesting.
*/
Expr getAnInit() { result.getParent() = this }
/** Gets the initializer occurring at the specified (zero-based) position. */
Expr getInit(int index) { result = this.getAnInit() and result.getIndex() = index }
/**
* Gets the number of expressions in this initializer, that is, the size the
* created array will have.
*/
int getSize() { result = count(this.getAnInit()) }
/** Gets a printable representation of this expression. */
override string toString() { result = "{...}" }
override string getAPrimaryQlClass() { result = "ArrayInit" }
}
/**
* A common super-class that represents many varieties of assignments.
*
* This does not cover unary assignments such as `i++`, and initialization of
* local variables at their declaration such as `int i = 0;`.
*
* To cover more cases of variable updates, see the classes `VariableAssign`,
* `VariableUpdate` and `VarWrite`. But consider that they don't cover array
* element assignments since there the assignment destination is not directly
* the array variable but instead an `ArrayAccess`.
*/
class Assignment extends Expr, @assignment {
/** Gets the destination (left-hand side) of the assignment. */
Expr getDest() { result.isNthChildOf(this, 0) }
/**
* Gets the source (right-hand side) of the assignment.
*
* For assignments with an implicit operator such as `x += 23`,
* the left-hand side is also a source.
*/
Expr getSource() { result.isNthChildOf(this, 1) }
/** Gets the right-hand side of the assignment. */
Expr getRhs() { result.isNthChildOf(this, 1) }
/** Gets a printable representation of this expression. */
override string toString() { result = "...=..." }
}
/**
* A simple assignment expression using the `=` operator.
*
* For example, `x = 23`.
*/
class AssignExpr extends Assignment, @assignexpr {
override string getAPrimaryQlClass() { result = "AssignExpr" }
}
/**
* A Kotlin class member initializer assignment.
*
* For example, `class X { val y = 1 }`
*/
class KtInitializerAssignExpr extends AssignExpr {
KtInitializerAssignExpr() { ktInitializerAssignment(this) }
override string getAPrimaryQlClass() { result = "KtInitializerAssignExpr" }
}
/**
* A common super-class to represent compound assignments, which include an implicit operator.
*
* For example, the compound assignment `x += 23`
* uses `+` as the implicit operator.
*/
class AssignOp extends Assignment, @assignop {
/**
* Gets a source of the compound assignment, which includes both the right-hand side
* and the left-hand side of the assignment.
*/
override Expr getSource() { result.getParent() = this }
/** Gets a string representation of the assignment operator of this compound assignment. */
/*abstract*/ string getOp() { result = "??=" }
/** Gets a printable representation of this expression. */
override string toString() { result = "..." + this.getOp() + "..." }
}
/** A compound assignment expression using the `+=` operator. */
class AssignAddExpr extends AssignOp, @assignaddexpr {
override string getOp() { result = "+=" }
override string getAPrimaryQlClass() { result = "AssignAddExpr" }
}
/** A compound assignment expression using the `-=` operator. */
class AssignSubExpr extends AssignOp, @assignsubexpr {
override string getOp() { result = "-=" }
override string getAPrimaryQlClass() { result = "AssignSubExpr" }
}
/** A compound assignment expression using the `*=` operator. */
class AssignMulExpr extends AssignOp, @assignmulexpr {
override string getOp() { result = "*=" }
override string getAPrimaryQlClass() { result = "AssignMulExpr" }
}
/** A compound assignment expression using the `/=` operator. */
class AssignDivExpr extends AssignOp, @assigndivexpr {
override string getOp() { result = "/=" }
override string getAPrimaryQlClass() { result = "AssignDivExpr" }
}
/** A compound assignment expression using the `%=` operator. */
class AssignRemExpr extends AssignOp, @assignremexpr {
override string getOp() { result = "%=" }
override string getAPrimaryQlClass() { result = "AssignRemExpr" }
}
/** A compound assignment expression using the `&=` operator. */
class AssignAndExpr extends AssignOp, @assignandexpr {
override string getOp() { result = "&=" }
override string getAPrimaryQlClass() { result = "AssignAndExpr" }
}
/** A compound assignment expression using the `|=` operator. */
class AssignOrExpr extends AssignOp, @assignorexpr {
override string getOp() { result = "|=" }
override string getAPrimaryQlClass() { result = "AssignOrExpr" }
}
/** A compound assignment expression using the `^=` operator. */
class AssignXorExpr extends AssignOp, @assignxorexpr {
override string getOp() { result = "^=" }
override string getAPrimaryQlClass() { result = "AssignXorExpr" }
}
/** A compound assignment expression using the `<<=` operator. */
class AssignLeftShiftExpr extends AssignOp, @assignlshiftexpr {
override string getOp() { result = "<<=" }
override string getAPrimaryQlClass() { result = "AssignLeftShiftExpr" }
}
/** A compound assignment expression using the `>>=` operator. */
class AssignRightShiftExpr extends AssignOp, @assignrshiftexpr {
override string getOp() { result = ">>=" }
override string getAPrimaryQlClass() { result = "AssignRightShiftExpr" }
}
/** A compound assignment expression using the `>>>=` operator. */
class AssignUnsignedRightShiftExpr extends AssignOp, @assignurshiftexpr {
override string getOp() { result = ">>>=" }
override string getAPrimaryQlClass() { result = "AssignUnsignedRightShiftExpr" }
}
/** A common super-class to represent constant literals. */
class Literal extends Expr, @literal {
/**
* Gets a string representation of this literal as it appeared
* in the source code.
*
* For Kotlin the result might not match the exact representation
* used in the source code.
*
* **Important:** Unless a query explicitly wants to check how
* a literal was written in the source code, the predicate
* `getValue()` (or value predicates of subclasses) should be
* used instead. For example for the integer literal `0x7fff_ffff`
* the result of `getLiteral()` would be `0x7fff_ffff`, while
* the result of `getValue()` would be `2147483647`.
*/
string getLiteral() { namestrings(result, _, this) }
/**
* Gets a string representation of the value this literal
* represents.
*/
string getValue() { namestrings(_, result, this) }
/** Gets a printable representation of this expression. */
override string toString() { result = this.getLiteral() }
/** Holds if this literal is a compile-time constant expression (as per JLS v8, section 15.28). */
override predicate isCompileTimeConstant() {
this.getType() instanceof PrimitiveType or
this.getType() instanceof TypeString
}
}
/** A boolean literal. Either `true` or `false`. */
class BooleanLiteral extends Literal, @booleanliteral {
/** Gets the boolean representation of this literal. */
boolean getBooleanValue() {
result = true and this.getValue() = "true"
or
result = false and this.getValue() = "false"
}
override string getAPrimaryQlClass() { result = "BooleanLiteral" }
}
/**
* An integer literal. For example, `23`.
*
* An integer literal can never be negative except when:
* - It is written in binary, octal or hexadecimal notation
* - It is written in decimal notation, has the value `2147483648` and is preceded
* by a minus; in this case the value of the IntegerLiteral is -2147483648 and
* the preceding minus will *not* be modeled as `MinusExpr`.
*
* In all other cases the preceding minus, if any, will be modeled as a separate
* `MinusExpr`.
*
* The last exception is necessary because `2147483648` on its own would not be
* a valid integer literal (and could also not be parsed as CodeQL `int`).
*/
class IntegerLiteral extends Literal, @integerliteral {
/** Gets the int representation of this literal. */
int getIntValue() { result = this.getValue().toInt() }
override string getAPrimaryQlClass() { result = "IntegerLiteral" }
}
/**
* A long literal. For example, `23L`.
*
* A long literal can never be negative except when:
* - It is written in binary, octal or hexadecimal notation
* - It is written in decimal notation, has the value `9223372036854775808` and
* is preceded by a minus; in this case the value of the LongLiteral is
* -9223372036854775808 and the preceding minus will *not* be modeled as
* `MinusExpr`.
*
* In all other cases the preceding minus, if any, will be modeled as a separate
* `MinusExpr`.
*
* The last exception is necessary because `9223372036854775808` on its own
* would not be a valid long literal.
*/
class LongLiteral extends Literal, @longliteral {
override string getAPrimaryQlClass() { result = "LongLiteral" }
}
/**
* A float literal. For example, `4.2f`.
*
* A float literal is never negative; a preceding minus, if any, will always
* be modeled as separate `MinusExpr`.
*/
class FloatLiteral extends Literal, @floatingpointliteral {
/**
* Gets the value of this literal as CodeQL 64-bit `float`. The value will
* be parsed as Java 32-bit `float` and then converted to a CodeQL `float`.
*/
float getFloatValue() { result = this.getValue().toFloat() }
override string getAPrimaryQlClass() { result = "FloatLiteral" }
}
/**
* A double literal. For example, `4.2`.
*
* A double literal is never negative; a preceding minus, if any, will always
* be modeled as separate `MinusExpr`.
*/
class DoubleLiteral extends Literal, @doubleliteral {
/**
* Gets the value of this literal as CodeQL 64-bit `float`. The result will
* have the same effective value as the Java `double` literal.
*/
float getDoubleValue() { result = this.getValue().toFloat() }
override string getAPrimaryQlClass() { result = "DoubleLiteral" }
}
bindingset[s]
private int fromHex(string s) {
exists(string digits | s.toUpperCase() = digits |
result =
sum(int i |
|
"0123456789ABCDEF".indexOf(digits.charAt(i)).bitShiftLeft((digits.length() - i - 1) * 4)
)
)
}
/** A character literal. For example, `'\n'`. */
class CharacterLiteral extends Literal, @characterliteral {
override string getAPrimaryQlClass() { result = "CharacterLiteral" }
/**
* Gets a string which consists of the single character represented by
* this literal.
*
* Unicode surrogate characters (U+D800 to U+DFFF) have the replacement character
* U+FFFD as result instead.
*/
override string getValue() { result = super.getValue() }
/**
* Gets the Unicode code point value of the character represented by
* this literal. The result is the same as if the Java code had cast
* the character to an `int`.
*/
int getCodePointValue() {
if this.getLiteral().matches("'\\u____'")
then result = fromHex(this.getLiteral().substring(3, 7))
else result.toUnicode() = this.getValue()
}
}
/**
* A string literal or text block (Java 15 feature). For example, `"hello world"`
* or
* ```java
* """
* Text with "quotes"
* """
* ```
*/
class StringLiteral extends Literal, @stringliteral {
/**
* Gets the string represented by this string literal, that is, the content
* of the literal without enclosing quotes and with escape sequences translated.
*
* Unpaired Unicode surrogate characters (U+D800 to U+DFFF) are replaced with the
* replacement character U+FFFD.
*/
override string getValue() { result = super.getValue() }
/** Holds if this string literal is a text block (`""" ... """`). */
predicate isTextBlock() { this.getLiteral().matches("\"\"\"%") }
override string getAPrimaryQlClass() { result = "StringLiteral" }
}
/** The null literal, written `null`. */
class NullLiteral extends Literal, @nullliteral {
// Override these predicates because the inherited ones have no result
override string getLiteral() { result = "null" }
override string getValue() { result = "null" }
override string getAPrimaryQlClass() { result = "NullLiteral" }
}
/** A common super-class to represent binary operator expressions. */
class BinaryExpr extends Expr, @binaryexpr {
/** Gets the operand on the left-hand side of this binary expression. */
Expr getLeftOperand() { result.isNthChildOf(this, 0) }
/** Gets the operand on the right-hand side of this binary expression. */
Expr getRightOperand() { result.isNthChildOf(this, 1) }
/** Gets an operand (left or right). */
Expr getAnOperand() { result = this.getLeftOperand() or result = this.getRightOperand() }
/** The operands of this binary expression are `e` and `f`, in either order. */
predicate hasOperands(Expr e, Expr f) {
exists(int i | i in [0 .. 1] |
e.isNthChildOf(this, i) and
f.isNthChildOf(this, 1 - i)
)
}
/** Gets a printable representation of this expression. */
override string toString() { result = "..." + this.getOp() + "..." }
/** Gets a string representation of the operator of this binary expression. */
/*abstract*/ string getOp() { result = " ?? " }
}
/** A binary expression using the `*` operator. */
class MulExpr extends BinaryExpr, @mulexpr {
override string getOp() { result = " * " }
override string getAPrimaryQlClass() { result = "MulExpr" }
}
/** A binary expression using the `/` operator. */
class DivExpr extends BinaryExpr, @divexpr {
override string getOp() { result = " / " }
override string getAPrimaryQlClass() { result = "DivExpr" }
}
/** A binary expression using the `%` operator. */
class RemExpr extends BinaryExpr, @remexpr {
override string getOp() { result = " % " }
override string getAPrimaryQlClass() { result = "RemExpr" }
}
/** A binary expression using the `+` operator. */
class AddExpr extends BinaryExpr, @addexpr {
override string getOp() { result = " + " }
override string getAPrimaryQlClass() { result = "AddExpr" }
}
/** A binary expression using the `-` operator. */
class SubExpr extends BinaryExpr, @subexpr {
override string getOp() { result = " - " }
override string getAPrimaryQlClass() { result = "SubExpr" }
}
/** A binary expression using the `<<` operator. */
class LeftShiftExpr extends BinaryExpr, @lshiftexpr {
override string getOp() { result = " << " }
override string getAPrimaryQlClass() { result = "LeftShiftExpr" }
}
/** A binary expression using the `>>` operator. */
class RightShiftExpr extends BinaryExpr, @rshiftexpr {
override string getOp() { result = " >> " }
override string getAPrimaryQlClass() { result = "RightShiftExpr" }
}
/** A binary expression using the `>>>` operator. */
class UnsignedRightShiftExpr extends BinaryExpr, @urshiftexpr {
override string getOp() { result = " >>> " }
override string getAPrimaryQlClass() { result = "UnsignedRightShiftExpr" }
}
/** A binary expression using the `&` operator. */
class AndBitwiseExpr extends BinaryExpr, @andbitexpr {
override string getOp() { result = " & " }
override string getAPrimaryQlClass() { result = "AndBitwiseExpr" }
}
/** A binary expression using the `|` operator. */
class OrBitwiseExpr extends BinaryExpr, @orbitexpr {
override string getOp() { result = " | " }
override string getAPrimaryQlClass() { result = "OrBitwiseExpr" }
}
/** A binary expression using the `^` operator. */
class XorBitwiseExpr extends BinaryExpr, @xorbitexpr {
override string getOp() { result = " ^ " }
override string getAPrimaryQlClass() { result = "XorBitwiseExpr" }
}
/** A binary expression using the `&&` operator. */
class AndLogicalExpr extends BinaryExpr, @andlogicalexpr {
override string getOp() { result = " && " }
override string getAPrimaryQlClass() { result = "AndLogicalExpr" }
}
/** A binary expression using the `||` operator. */
class OrLogicalExpr extends BinaryExpr, @orlogicalexpr {
override string getOp() { result = " || " }
override string getAPrimaryQlClass() { result = "OrLogicalExpr" }
}
/** A binary expression using the `<` operator. */
class LTExpr extends BinaryExpr, @ltexpr {
override string getOp() { result = " < " }
override string getAPrimaryQlClass() { result = "LTExpr" }
}
/** A binary expression using the `>` operator. */
class GTExpr extends BinaryExpr, @gtexpr {
override string getOp() { result = " > " }
override string getAPrimaryQlClass() { result = "GTExpr" }
}
/** A binary expression using the `<=` operator. */
class LEExpr extends BinaryExpr, @leexpr {
override string getOp() { result = " <= " }
override string getAPrimaryQlClass() { result = "LEExpr" }
}
/** A binary expression using the `>=` operator. */
class GEExpr extends BinaryExpr, @geexpr {
override string getOp() { result = " >= " }
override string getAPrimaryQlClass() { result = "GEExpr" }
}
/** A binary expression using Java's `==` or Kotlin's `===` operator. */
class EQExpr extends BinaryExpr, @eqexpr {
override string getOp() { result = " == " }
override string getAPrimaryQlClass() { result = "EQExpr" }
}
/** A binary expression using the Kotlin `==` operator, semantically equivalent to `Objects.equals`. */
class ValueEQExpr extends BinaryExpr, @valueeqexpr {
override string getOp() { result = " (value equals) " }
override string getAPrimaryQlClass() { result = "ValueEQExpr" }
}
/** A binary expression using Java's `!=` or Kotlin's `!==` operator. */
class NEExpr extends BinaryExpr, @neexpr {
override string getOp() { result = " != " }
override string getAPrimaryQlClass() { result = "NEExpr" }
}
/** A binary expression using the Kotlin `!=` operator, semantically equivalent to `Objects.equals`. */
class ValueNEExpr extends BinaryExpr, @valueneexpr {
override string getOp() { result = " (value not-equals) " }
override string getAPrimaryQlClass() { result = "ValueNEExpr" }
}
/**
* A binary expression using either Java or Kotlin's `==` operator.
*
* This might test for reference equality or might function like `Objects.equals`. If you
* need to distinguish them, use `EQExpr` or `ValueEQExpr` instead.
*/
class ValueOrReferenceEqualsExpr extends BinaryExpr {
ValueOrReferenceEqualsExpr() { this instanceof EQExpr or this instanceof ValueEQExpr }
}
/**
* A binary expression using either Java or Kotlin's `!=` operator.
*
* This might test for reference equality or might function like `Objects.equals`. If you
* need to distinguish them, use `NEExpr` or `ValueNEExpr` instead.
*/
class ValueOrReferenceNotEqualsExpr extends BinaryExpr {
ValueOrReferenceNotEqualsExpr() { this instanceof NEExpr or this instanceof ValueNEExpr }
}
/**
* A bitwise expression.
*
* This includes expressions involving the operators
* `&`, `|`, `^`, or `~`.
*/
class BitwiseExpr extends Expr {
BitwiseExpr() {
this instanceof AndBitwiseExpr or
this instanceof OrBitwiseExpr or
this instanceof XorBitwiseExpr or
this instanceof BitNotExpr
}
}
/**
* A logical expression.
*
* This includes expressions involving the operators
* `&&`, `||`, or `!`.
*/
class LogicExpr extends Expr {
LogicExpr() {
this instanceof AndLogicalExpr or
this instanceof OrLogicalExpr or
this instanceof LogNotExpr
}
/** Gets an operand of this logical expression. */
Expr getAnOperand() {
this.(BinaryExpr).getAnOperand() = result or
this.(UnaryExpr).getExpr() = result
}
}
/**
* A comparison expression.
*
* This includes expressions using the operators
* `<=`, `>=`, `<` or `>`.
*/
abstract class ComparisonExpr extends BinaryExpr {
/**
* Gets the lesser operand of this comparison expression.
*
* For example, `x` is the lesser operand
* in `x < 0`, and `0` is the
* lesser operand in `x > 0`.
*/
abstract Expr getLesserOperand();
/**
* Gets the greater operand of this comparison expression.
*
* For example, `x` is the greater operand
* in `x > 0`, and `0` is the
* greater operand in `x < 0`.
*/
abstract Expr getGreaterOperand();
/** Holds if this comparison is strict, i.e. `<` or `>`. */
predicate isStrict() { this instanceof LTExpr or this instanceof GTExpr }
}
/** A comparison expression using the operator `<` or `<=`. */
class LessThanComparison extends ComparisonExpr {
LessThanComparison() { this instanceof LTExpr or this instanceof LEExpr }
/** Gets the lesser operand of this comparison expression. */
override Expr getLesserOperand() { result = this.getLeftOperand() }
/** Gets the greater operand of this comparison expression. */
override Expr getGreaterOperand() { result = this.getRightOperand() }
}
/** A comparison expression using the operator `>` or `>=`. */
class GreaterThanComparison extends ComparisonExpr {
GreaterThanComparison() { this instanceof GTExpr or this instanceof GEExpr }
/** Gets the lesser operand of this comparison expression. */
override Expr getLesserOperand() { result = this.getRightOperand() }
/** Gets the greater operand of this comparison expression. */
override Expr getGreaterOperand() { result = this.getLeftOperand() }
}
/**