forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpr.qll
More file actions
executable file
·1842 lines (1576 loc) · 63.2 KB
/
Expr.qll
File metadata and controls
executable file
·1842 lines (1576 loc) · 63.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
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.
*/
import Member
import Type
import Variable
import Statement
/** 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 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, _, _, _) }
/**
* DEPRECATED: This is no longer necessary. See `Expr.isParenthesized()`.
*
* Gets this expression with any surrounding parentheses removed.
*/
deprecated Expr getProperExpr() {
result = this.(ParExpr).getExpr().getProperExpr()
or
result = this and not this instanceof ParExpr
}
/** 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() = this }
/** Gets the `ControlFlowNode` corresponding to this expression. */
ControlFlowNode getControlFlowNode() { result = 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.
*/
getEnclosingCallable().isStatic()
or
getParent+() instanceof ThisConstructorInvocationStmt
or
getParent+() instanceof SuperConstructorInvocationStmt
or
exists(LambdaExpr lam | lam.asMethod() = getEnclosingCallable() and lam.isInStaticContext())
}
/** Holds if this expression is parenthesized. */
predicate isParenthesized() { isParenthesized(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(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.(CastExpr).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()
)
)
}
/**
* Gets the string value of this expression, where possible.
*/
pragma[nomagic]
string getStringValue() {
result = this.(StringLiteral).getRepresentedString()
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()
|
if condition = true
then result = ce.getTrueExpr().(CompileTimeConstantExpr).getStringValue()
else result = ce.getFalseExpr().(CompileTimeConstantExpr).getStringValue()
)
or
exists(Variable v | this = v.getAnAccess() |
result = v.getInitializer().(CompileTimeConstantExpr).getStringValue()
)
}
/**
* Gets the boolean value of this expression, where possible.
*/
boolean getBooleanValue() {
// Literal value.
result = this.(BooleanLiteral).getBooleanValue()
or
// No casts relevant to booleans.
// `!` is the only unary operator that evaluates to a boolean.
result = this.(LogNotExpr).getExpr().(CompileTimeConstantExpr).getBooleanValue().booleanNot()
or
// Handle binary expressions that have integer operands and a boolean result.
exists(BinaryExpr b, int left, int right |
b = this and
left = b.getLeftOperand().(CompileTimeConstantExpr).getIntValue() and
right = b.getRightOperand().(CompileTimeConstantExpr).getIntValue()
|
(
b instanceof LTExpr and
if left < right then result = true else result = false
)
or
(
b instanceof LEExpr and
if left <= right then result = true else result = false
)
or
(
b instanceof GTExpr and
if left > right then result = true else result = false
)
or
(
b instanceof GEExpr and
if left >= right then result = true else result = false
)
or
(
b instanceof EQExpr and
if left = right then result = true else result = false
)
or
(
b instanceof NEExpr and
if left != right then result = true else result = false
)
)
or
// Handle binary expressions that have boolean operands and a boolean result.
exists(BinaryExpr b, boolean left, boolean right |
b = this and
left = b.getLeftOperand().(CompileTimeConstantExpr).getBooleanValue() and
right = b.getRightOperand().(CompileTimeConstantExpr).getBooleanValue()
|
(
b instanceof EQExpr and
if left = right then result = true else result = false
)
or
(
b instanceof NEExpr and
if left != right then result = true else result = false
)
or
(b instanceof AndBitwiseExpr or b instanceof AndLogicalExpr) and
result = left.booleanAnd(right)
or
(b instanceof OrBitwiseExpr or b instanceof OrLogicalExpr) and
result = left.booleanOr(right)
or
b instanceof XorBitwiseExpr and result = left.booleanXor(right)
)
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.
*/
(
b instanceof EQExpr and
if left = right then result = true else result = false
)
or
(
b instanceof NEExpr 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.
// Ternary expressions, where the `true` and `false` expressions are boolean compile-time constants.
exists(ConditionalExpr ce, boolean condition |
ce = this and
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue()
|
if condition = true
then result = ce.getTrueExpr().(CompileTimeConstantExpr).getBooleanValue()
else result = ce.getFalseExpr().(CompileTimeConstantExpr).getBooleanValue()
)
or
// Simple or qualified names where the variable is final and the initializer is a constant.
exists(Variable v | this = v.getAnAccess() |
result = v.getInitializer().(CompileTimeConstantExpr).getBooleanValue()
)
}
/**
* Gets the integer value of this expression, where possible.
*
* Note that this does not handle the following cases:
*
* - values of type `long`,
* - `char` literals.
*/
cached
int getIntValue() {
exists(IntegralType t | this.getType() = t | t.getName().toLowerCase() != "long") and
(
exists(string lit | lit = this.(Literal).getValue() |
// `char` literals may get parsed incorrectly, so disallow.
not this instanceof CharacterLiteral and
result = lit.toInt()
)
or
exists(CastExpr cast, int val |
cast = this and val = cast.getExpr().(CompileTimeConstantExpr).getIntValue()
|
if cast.getType().hasName("byte")
then result = (val + 128).bitAnd(255) - 128
else
if cast.getType().hasName("short")
then result = (val + 32768).bitAnd(65535) - 32768
else
if cast.getType().hasName("char")
then result = val.bitAnd(65535)
else result = val
)
or
result = this.(PlusExpr).getExpr().(CompileTimeConstantExpr).getIntValue()
or
result = -this.(MinusExpr).getExpr().(CompileTimeConstantExpr).getIntValue()
or
result = this.(BitNotExpr).getExpr().(CompileTimeConstantExpr).getIntValue().bitNot()
or
// No `int` value for `LogNotExpr`.
exists(BinaryExpr b, int v1, int v2 |
b = this and
v1 = b.getLeftOperand().(CompileTimeConstantExpr).getIntValue() and
v2 = b.getRightOperand().(CompileTimeConstantExpr).getIntValue()
|
b instanceof MulExpr and result = v1 * v2
or
b instanceof DivExpr and result = v1 / v2
or
b instanceof RemExpr and result = v1 % v2
or
b instanceof AddExpr and result = v1 + v2
or
b instanceof SubExpr and result = v1 - v2
or
b instanceof LShiftExpr and result = v1.bitShiftLeft(v2)
or
b instanceof RShiftExpr and result = v1.bitShiftRightSigned(v2)
or
b instanceof URShiftExpr and result = v1.bitShiftRight(v2)
or
b instanceof AndBitwiseExpr and result = v1.bitAnd(v2)
or
b instanceof OrBitwiseExpr and result = v1.bitOr(v2)
or
b instanceof XorBitwiseExpr and result = v1.bitXor(v2)
// No `int` value for `AndLogicalExpr` or `OrLogicalExpr`.
// No `int` value for `LTExpr`, `GTExpr`, `LEExpr`, `GEExpr`, `EQExpr` or `NEExpr`.
)
or
// Ternary conditional, with compile-time constant condition.
exists(ConditionalExpr ce, boolean condition |
ce = this and
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue()
|
if condition = true
then result = ce.getTrueExpr().(CompileTimeConstantExpr).getIntValue()
else result = ce.getFalseExpr().(CompileTimeConstantExpr).getIntValue()
)
or
// If a `Variable` is a `CompileTimeConstantExpr`, its value is its initializer.
exists(Variable v | this = v.getAnAccess() |
result = v.getInitializer().(CompileTimeConstantExpr).getIntValue()
)
)
}
}
/** An expression parent is an element that may have an expression as its child. */
class ExprParent extends @exprparent, Top { }
/**
* 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 = "...[...]" }
}
/**
* 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. */
ArrayInit getInit() { result.isNthChildOf(this, -2) }
/**
* Gets the size of the first dimension, if it can be statically determined.
*/
int getFirstDimensionSize() {
if exists(getInit())
then result = count(getInit().getAnInit())
else result = getDimension(0).(CompileTimeConstantExpr).getIntValue()
}
/** Gets a printable representation of this expression. */
override string toString() { result = "new " + this.getType().toString() }
}
/** An array initializer occurs in an array creation expression. */
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 a printable representation of this expression. */
override string toString() { result = "{...}" }
}
/** A common super-class that represents all varieties of assignments. */
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 { }
/**
* 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 = "+=" }
}
/** A compound assignment expression using the `-=` operator. */
class AssignSubExpr extends AssignOp, @assignsubexpr {
override string getOp() { result = "-=" }
}
/** A compound assignment expression using the `*=` operator. */
class AssignMulExpr extends AssignOp, @assignmulexpr {
override string getOp() { result = "*=" }
}
/** A compound assignment expression using the `/=` operator. */
class AssignDivExpr extends AssignOp, @assigndivexpr {
override string getOp() { result = "/=" }
}
/** A compound assignment expression using the `%=` operator. */
class AssignRemExpr extends AssignOp, @assignremexpr {
override string getOp() { result = "%=" }
}
/** A compound assignment expression using the `&=` operator. */
class AssignAndExpr extends AssignOp, @assignandexpr {
override string getOp() { result = "&=" }
}
/** A compound assignment expression using the `|=` operator. */
class AssignOrExpr extends AssignOp, @assignorexpr {
override string getOp() { result = "|=" }
}
/** A compound assignment expression using the `^=` operator. */
class AssignXorExpr extends AssignOp, @assignxorexpr {
override string getOp() { result = "^=" }
}
/** A compound assignment expression using the `<<=` operator. */
class AssignLShiftExpr extends AssignOp, @assignlshiftexpr {
override string getOp() { result = "<<=" }
}
/** A compound assignment expression using the `>>=` operator. */
class AssignRShiftExpr extends AssignOp, @assignrshiftexpr {
override string getOp() { result = ">>=" }
}
/** A compound assignment expression using the `>>>=` operator. */
class AssignURShiftExpr extends AssignOp, @assignurshiftexpr {
override string getOp() { result = ">>>=" }
}
/** A common super-class to represent constant literals. */
class Literal extends Expr, @literal {
/** Gets a string representation of this literal. */
string getLiteral() { namestrings(result, _, this) }
/** Gets a string representation of the value of this literal. */
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 getLiteral() = "true"
or
result = false and getLiteral() = "false"
}
}
/** An integer literal. For example, `23`. */
class IntegerLiteral extends Literal, @integerliteral {
/** Gets the int representation of this literal. */
int getIntValue() { result = getValue().toInt() }
}
/** A long literal. For example, `23l`. */
class LongLiteral extends Literal, @longliteral { }
/** A floating point literal. For example, `4.2f`. */
class FloatingPointLiteral extends Literal, @floatingpointliteral { }
/** A double literal. For example, `4.2`. */
class DoubleLiteral extends Literal, @doubleliteral { }
/** A character literal. For example, `'\n'`. */
class CharacterLiteral extends Literal, @characterliteral { }
/** A string literal. For example, `"hello world"`. */
class StringLiteral extends Literal, @stringliteral {
/**
* Gets the literal string without the quotes.
*/
string getRepresentedString() { result = getValue() }
}
/** The null literal, written `null`. */
class NullLiteral extends Literal, @nullliteral {
override string getLiteral() { result = "null" }
override string getValue() { result = "null" }
}
/** 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 = " * " }
}
/** A binary expression using the `/` operator. */
class DivExpr extends BinaryExpr, @divexpr {
override string getOp() { result = " / " }
}
/** A binary expression using the `%` operator. */
class RemExpr extends BinaryExpr, @remexpr {
override string getOp() { result = " % " }
}
/** A binary expression using the `+` operator. */
class AddExpr extends BinaryExpr, @addexpr {
override string getOp() { result = " + " }
}
/** A binary expression using the `-` operator. */
class SubExpr extends BinaryExpr, @subexpr {
override string getOp() { result = " - " }
}
/** A binary expression using the `<<` operator. */
class LShiftExpr extends BinaryExpr, @lshiftexpr {
override string getOp() { result = " << " }
}
/** A binary expression using the `>>` operator. */
class RShiftExpr extends BinaryExpr, @rshiftexpr {
override string getOp() { result = " >> " }
}
/** A binary expression using the `>>>` operator. */
class URShiftExpr extends BinaryExpr, @urshiftexpr {
override string getOp() { result = " >>> " }
}
/** A binary expression using the `&` operator. */
class AndBitwiseExpr extends BinaryExpr, @andbitexpr {
override string getOp() { result = " & " }
}
/** A binary expression using the `|` operator. */
class OrBitwiseExpr extends BinaryExpr, @orbitexpr {
override string getOp() { result = " | " }
}
/** A binary expression using the `^` operator. */
class XorBitwiseExpr extends BinaryExpr, @xorbitexpr {
override string getOp() { result = " ^ " }
}
/** A binary expression using the `&&` operator. */
class AndLogicalExpr extends BinaryExpr, @andlogicalexpr {
override string getOp() { result = " && " }
}
/** A binary expression using the `||` operator. */
class OrLogicalExpr extends BinaryExpr, @orlogicalexpr {
override string getOp() { result = " || " }
}
/** A binary expression using the `<` operator. */
class LTExpr extends BinaryExpr, @ltexpr {
override string getOp() { result = " < " }
}
/** A binary expression using the `>` operator. */
class GTExpr extends BinaryExpr, @gtexpr {
override string getOp() { result = " > " }
}
/** A binary expression using the `<=` operator. */
class LEExpr extends BinaryExpr, @leexpr {
override string getOp() { result = " <= " }
}
/** A binary expression using the `>=` operator. */
class GEExpr extends BinaryExpr, @geexpr {
override string getOp() { result = " >= " }
}
/** A binary expression using the `==` operator. */
class EQExpr extends BinaryExpr, @eqexpr {
override string getOp() { result = " == " }
}
/** A binary expression using the `!=` operator. */
class NEExpr extends BinaryExpr, @neexpr {
override string getOp() { result = " != " }
}
/**
* 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() }
}
/**
* An equality test is a binary expression using
* the `==` or `!=` operator.
*/
class EqualityTest extends BinaryExpr {
EqualityTest() {
this instanceof EQExpr or
this instanceof NEExpr
}
/** Gets a boolean indicating whether this is `==` (true) or `!=` (false). */
boolean polarity() {
result = true and this instanceof EQExpr
or
result = false and this instanceof NEExpr
}
}
/** A common super-class that represents unary operator expressions. */
class UnaryExpr extends Expr, @unaryexpr {
/** Gets the operand expression. */
Expr getExpr() { result.getParent() = this }
}
/**
* A unary assignment expression is a unary expression using the
* prefix or postfix `++` or `--` operator.
*/
class UnaryAssignExpr extends UnaryExpr, @unaryassignment { }
/** A post-increment expression. For example, `i++`. */
class PostIncExpr extends UnaryAssignExpr, @postincexpr {
override string toString() { result = "...++" }
}
/** A post-decrement expression. For example, `i--`. */
class PostDecExpr extends UnaryAssignExpr, @postdecexpr {
override string toString() { result = "...--" }
}
/** A pre-increment expression. For example, `++i`. */
class PreIncExpr extends UnaryAssignExpr, @preincexpr {
override string toString() { result = "++..." }
}
/** A pre-decrement expression. For example, `--i`. */
class PreDecExpr extends UnaryAssignExpr, @predecexpr {
override string toString() { result = "--..." }
}
/** A unary minus expression. For example, `-i`. */
class MinusExpr extends UnaryExpr, @minusexpr {
override string toString() { result = "-..." }
}
/** A unary plus expression. For example, `+i`. */
class PlusExpr extends UnaryExpr, @plusexpr {
override string toString() { result = "+..." }
}
/** A bit negation expression. For example, `~x`. */
class BitNotExpr extends UnaryExpr, @bitnotexpr {
override string toString() { result = "~..." }
}
/** A logical negation expression. For example, `!b`. */
class LogNotExpr extends UnaryExpr, @lognotexpr {
override string toString() { result = "!..." }
}
/** A cast expression. */
class CastExpr extends Expr, @castexpr {
/** Gets the target type of this cast expression. */
Expr getTypeExpr() { result.isNthChildOf(this, 0) }
/** Gets the expression to which the cast operator is applied. */
Expr getExpr() { result.isNthChildOf(this, 1) }
/** Gets a printable representation of this expression. */
override string toString() { result = "(...)..." }
}
/** A class instance creation expression. */
class ClassInstanceExpr extends Expr, ConstructorCall, @classinstancexpr {
/** Gets the number of arguments provided to the constructor of the class instance creation expression. */
override int getNumArgument() { count(this.getAnArgument()) = result }
/** Gets an argument provided to the constructor of this class instance creation expression. */
override Expr getAnArgument() { result.getIndex() >= 0 and result.getParent() = this }
/**
* Gets the argument provided to the constructor of this class instance creation expression
* at the specified (zero-based) position.
*/
override Expr getArgument(int index) {
result.getIndex() = index and
result = this.getAnArgument()
}
/**
* Gets a type argument provided to the constructor of this class instance creation expression.
*
* This is used for instantiations of parameterized classes.
*/
Expr getATypeArgument() { result = this.getTypeName().(TypeAccess).getATypeArgument() }
/**
* Gets the type argument provided to the constructor of this class instance creation expression
* at the specified (zero-based) position.
*/
Expr getTypeArgument(int index) {
result = this.getTypeName().(TypeAccess).getTypeArgument(index)
}
/** Gets the qualifier of this class instance creation expression, if any. */
override Expr getQualifier() { result.isNthChildOf(this, -2) }
/**
* Gets the access to the type that is instantiated or subclassed by this
* class instance creation expression.
*/
Expr getTypeName() { result.isNthChildOf(this, -3) }
/** Gets the constructor invoked by this class instance creation expression. */
override Constructor getConstructor() { callableBinding(this, result) }
/** Gets the anonymous class created by this class instance creation expression, if any. */
AnonymousClass getAnonymousClass() { isAnonymClass(result, this) }
/**
* Holds if this class instance creation expression has an
* empty type argument list of the form `<>`.
*/
predicate isDiamond() {
this.getType() instanceof ParameterizedClass and
not exists(this.getATypeArgument())
}
/** Gets the immediately enclosing callable of this class instance creation expression. */
override Callable getEnclosingCallable() { result = Expr.super.getEnclosingCallable() }
/** Gets the immediately enclosing statement of this class instance creation expression. */
override Stmt getEnclosingStmt() { result = Expr.super.getEnclosingStmt() }
/** Gets a printable representation of this expression. */
override string toString() { result = "new " + this.getConstructor().getName() + "(...)" }
}
/** A functional expression is either a lambda expression or a member reference expression. */
abstract class FunctionalExpr extends ClassInstanceExpr {
/** Gets the implicit method corresponding to this functional expression. */
abstract Method asMethod();
}
/**
* Lambda expressions are represented by their implicit class instance creation expressions,
* which instantiate an anonymous class that overrides the unique method designated by
* their functional interface type. The parameters of the lambda expression correspond
* to the parameters of the overriding method, and the lambda body corresponds to the