forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintAst.qll
More file actions
1005 lines (866 loc) · 27.4 KB
/
PrintAst.qll
File metadata and controls
1005 lines (866 loc) · 27.4 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 pretty-printed representations of the AST, in particular top-level
* classes and interfaces.
*/
import java
/**
* Holds if the pretty-printed representation of `c` has the line `s` at line
* number `line`.
*/
predicate pp(ClassOrInterface c, string s, int line) {
not c instanceof NestedType and
s =
strictconcat(string part, int i |
exists(PpAst e | getEnclosingAst*(e) = c | ppPart(e, part, line, i))
|
part order by i
)
}
private PpAst getEnclosingAst(PpAst e) {
e.(Expr).getEnclosingCallable() = result or
e.(Stmt).getEnclosingCallable() = result or
e.(Member).getDeclaringType() = result or
e.(NestedType).getEnclosingType() = result
}
/**
* An AST element to pretty-print. This is either an `Expr`, `Stmt`, `Class`,
* `Interface`, or `Member`.
*
* Subclasses specify how they are printed by giving a sequence of printable
* items. Each item in the sequence is either a string given by `getPart`, a
* line break given by `newline`, or another AST element given by `getChild`.
* The ordering of the sequence is given by the indices `i` in each of the
* three predicates.
*/
private class PpAst extends Top {
/** Gets the `i`th item to print. */
string getPart(int i) { none() }
/** Holds if the `i`th item to print is a line break. */
predicate newline(int i) { none() }
/** Gets the `i`th item to print. */
PpAst getChild(int i) { none() }
/**
* Holds if the `i`th item to print is a `PpAst` given by `getChild(i)` and
* that this should be indented.
*/
predicate indents(int i) { none() }
}
/** Gets the indentation level of the AST element `e`. */
private int indentLevel(PpAst e) {
exists(ClassOrInterface c | c = e and not c instanceof NestedType and result = 0)
or
exists(PpAst parent, int i, int lev |
e = parent.getChild(i) and
lev = indentLevel(parent) and
if parent.indents(i) then result = lev + 1 else result = lev
)
}
/**
* Gets the `i`th item to print belonging to `e`. This is similar to
* `e.getPart(i)`, but also include parentheses.
*/
private string getPart(PpAst e, int i) {
result = e.getPart(i)
or
e.(Expr).isParenthesized() and
(
i = -1 + min(int j | exists(e.getPart(j)) or e.newline(j) or exists(e.getChild(j))) and
result = "("
or
i = 1 + max(int j | exists(e.getPart(j)) or e.newline(j) or exists(e.getChild(j))) and
result = ")"
)
}
/**
* Gets the number of string parts contained in `e` and recursively in its
* children.
*/
language[monotonicAggregates]
private int numParts(PpAst e) {
result =
count(int i | exists(getPart(e, i))) +
sum(PpAst child | child = e.getChild(_) | numParts(child))
}
/**
* Gets the number of line breaks contained in `e` and recursively in its
* children.
*/
language[monotonicAggregates]
private int numLines(PpAst e) {
result = count(int i | e.newline(i)) + sum(PpAst child | child = e.getChild(_) | numLines(child))
}
/**
* Gets an index to a string part, line break, or child in `e` with rank `r`.
*/
private int getIndex(PpAst e, int r) {
result = rank[r](int i | exists(getPart(e, i)) or e.newline(i) or exists(e.getChild(i)))
}
/** Holds if the `ix`th item of `e` should be printed at `(line, pos)`. */
private predicate startPos(PpAst e, int ix, int line, int pos) {
exists(ClassOrInterface c |
c = e and not c instanceof NestedType and ix = getIndex(e, 1) and line = 0 and pos = 0
)
or
exists(PpAst parent, int parix |
startPos(parent, parix, line, pos) and e = parent.getChild(parix) and ix = getIndex(e, 1)
)
or
exists(int prevIx, int r | prevIx = getIndex(e, r - 1) and ix = getIndex(e, r) |
exists(getPart(e, prevIx)) and startPos(e, prevIx, line, pos - 1)
or
e.newline(prevIx) and startPos(e, prevIx, line - 1, _) and pos = 0
or
exists(PpAst child, int l, int p |
child = e.getChild(prevIx) and
startPos(e, prevIx, l, p) and
line = l + numLines(child) and
pos = p + numParts(child)
)
)
}
/**
* Holds if the pretty-printed representation of `e` contributes `part` to occur
* on `(line, pos)`. This does not include string parts belonging to children of
* `e`.
*/
private predicate ppPart(PpAst e, string part, int line, int pos) {
exists(int i | part = getPart(e, i) and startPos(e, i, line, pos))
or
exists(int i | exists(getPart(e, i)) or e.newline(i) |
startPos(e, i, line, 0) and
pos = -1 and
part = concat(int ind | ind in [1 .. indentLevel(e)] | " ")
)
}
/*
* Expressions
*/
private class PpArrayAccess extends PpAst, ArrayAccess {
override string getPart(int i) {
i = 1 and result = "["
or
i = 3 and result = "]"
}
override PpAst getChild(int i) {
i = 0 and result = this.getArray()
or
i = 2 and result = this.getIndexExpr()
}
}
private class PpArrayCreationExpr extends PpAst, ArrayCreationExpr {
override string getPart(int i) {
i = 0 and result = "new "
or
i = 1 and result = baseType()
or
i = 2 + 3 * dimensionIndex() and result = "["
or
i = 4 + 3 * dimensionIndex() and result = "]"
or
i = 4 + 3 * exprDims() + [1 .. nonExprDims()] and result = "[]"
}
private string baseType() { result = this.getType().(Array).getElementType().toString() }
private int dimensionIndex() { exists(this.getDimension(result)) }
private int exprDims() { result = max(int j | j = 0 or j = 1 + dimensionIndex()) }
private int nonExprDims() { result = this.getType().(Array).getDimension() - exprDims() }
override PpAst getChild(int i) {
exists(int j | result = this.getDimension(j) and i = 3 + 3 * j)
or
i = 5 + 3 * exprDims() + nonExprDims() and result = this.getInit()
}
}
private class PpArrayInit extends PpAst, ArrayInit {
override string getPart(int i) {
i = 0 and result = "{ "
or
exists(int j | exists(this.getInit(j)) and j != 0 and i = 2 * j and result = ", ")
or
i = 2 + 2 * max(int j | exists(this.getInit(j)) or j = 0) and result = " }"
}
override PpAst getChild(int i) { exists(int j | result = this.getInit(j) and i = 1 + 2 * j) }
}
private class PpAssignment extends PpAst, Assignment {
override string getPart(int i) {
i = 1 and
this instanceof AssignExpr and
result = " = "
or
i = 1 and
result = " " + this.(AssignOp).getOp() + " "
}
override PpAst getChild(int i) {
i = 0 and result = this.getDest()
or
i = 2 and result = this.getRhs()
}
}
private class PpLiteral extends PpAst, Literal {
override string getPart(int i) { i = 0 and result = this.getLiteral() }
}
private class PpBinaryExpr extends PpAst, BinaryExpr {
override string getPart(int i) { i = 1 and result = this.getOp() }
override PpAst getChild(int i) {
i = 0 and result = this.getLeftOperand()
or
i = 2 and result = this.getRightOperand()
}
}
private class PpUnaryExpr extends PpAst, UnaryExpr {
override string getPart(int i) {
i = 0 and result = "++" and this instanceof PreIncExpr
or
i = 0 and result = "--" and this instanceof PreDecExpr
or
i = 0 and result = "-" and this instanceof MinusExpr
or
i = 0 and result = "+" and this instanceof PlusExpr
or
i = 0 and result = "~" and this instanceof BitNotExpr
or
i = 0 and result = "!" and this instanceof LogNotExpr
or
i = 2 and result = "++" and this instanceof PostIncExpr
or
i = 2 and result = "--" and this instanceof PostDecExpr
}
override PpAst getChild(int i) { i = 1 and result = this.getExpr() }
}
private class PpCastExpr extends PpAst, CastExpr {
override string getPart(int i) {
i = 0 and result = "("
or
i = 2 and result = ")"
}
override PpAst getChild(int i) {
i = 1 and result = this.getTypeExpr()
or
i = 3 and result = this.getExpr()
}
}
private class PpCall extends PpAst, Call {
override string getPart(int i) {
i = 1 and exists(this.getQualifier()) and result = "."
or
i = 2 and
(
result = this.(MethodAccess).getMethod().getName()
or
result = "this" and this instanceof ThisConstructorInvocationStmt
or
result = "super" and this instanceof SuperConstructorInvocationStmt
or
result = "new " and this instanceof ClassInstanceExpr and not this instanceof FunctionalExpr
or
result = "new /* -> */ " and this instanceof LambdaExpr
or
result = "new /* :: */ " and this instanceof MemberRefExpr
)
or
i = 4 and result = "("
or
exists(int argi |
exists(this.getArgument(argi)) and argi != 0 and i = 4 + 2 * argi and result = ", "
)
or
i = 5 + 2 * this.getNumArgument() and result = ")"
or
i = 6 + 2 * this.getNumArgument() and result = ";" and this instanceof Stmt
or
i = 6 + 2 * this.getNumArgument() and
result = " " and
exists(this.(ClassInstanceExpr).getAnonymousClass())
}
override PpAst getChild(int i) {
i = 0 and result = this.getQualifier()
or
i = 3 and result = this.(ClassInstanceExpr).getTypeName()
or
exists(int argi | i = 5 + 2 * argi and result = this.getArgument(argi))
or
i = 7 + 2 * this.getNumArgument() and result = this.(ClassInstanceExpr).getAnonymousClass()
}
}
private class PpConditionalExpr extends PpAst, ConditionalExpr {
override string getPart(int i) {
i = 1 and result = " ? "
or
i = 3 and result = " : "
}
override PpAst getChild(int i) {
i = 0 and result = this.getCondition()
or
i = 2 and result = this.getTrueExpr()
or
i = 4 and result = this.getFalseExpr()
}
}
private class PpSwitchExpr extends PpAst, SwitchExpr {
override string getPart(int i) {
i = 0 and result = "switch ("
or
i = 2 and result = ") {"
or
i = 4 + 2 * count(this.getAStmt()) and result = "}"
}
override predicate newline(int i) { i = 3 or this.hasChildAt(_, i - 1) }
override PpAst getChild(int i) {
i = 1 and result = this.getExpr()
or
this.hasChildAt(result, i)
}
override predicate indents(int i) { this.hasChildAt(_, i) }
private predicate hasChildAt(PpAst c, int i) {
exists(int index | c = this.getStmt(index) and i = 4 + 2 * index)
}
}
private class PpInstanceOfExpr extends PpAst, InstanceOfExpr {
override string getPart(int i) {
i = 1 and result = " instanceof "
or
i = 3 and result = " " and this.isPattern()
or
i = 4 and result = this.getLocalVariableDeclExpr().getName()
}
override PpAst getChild(int i) {
i = 0 and result = this.getExpr()
or
i = 2 and result = this.getTypeName()
}
}
private class PpLocalVariableDeclExpr extends PpAst, LocalVariableDeclExpr {
override string getPart(int i) {
i = 0 and result = this.getName()
or
i = 1 and result = " = " and exists(this.getInit())
}
override PpAst getChild(int i) { i = 2 and result = this.getInit() }
}
private class PpTypeLiteral extends PpAst, TypeLiteral {
override string getPart(int i) { i = 1 and result = ".class" }
override PpAst getChild(int i) { i = 0 and result = this.getTypeName() }
}
private class PpThisAccess extends PpAst, ThisAccess {
override string getPart(int i) {
i = 1 and
if exists(this.getQualifier()) then result = ".this" else result = "this"
}
override PpAst getChild(int i) { i = 0 and result = this.getQualifier() }
}
private class PpSuperAccess extends PpAst, SuperAccess {
override string getPart(int i) {
i = 1 and
if exists(this.getQualifier()) then result = ".super" else result = "super"
}
override PpAst getChild(int i) { i = 0 and result = this.getQualifier() }
}
private class PpVarAccess extends PpAst, VarAccess {
override string getPart(int i) {
exists(string name | name = this.(VarAccess).getVariable().getName() and i = 1 |
if exists(this.getQualifier()) then result = "." + name else result = name
)
}
override PpAst getChild(int i) { i = 0 and result = this.getQualifier() }
}
private class PpTypeAccess extends PpAst, TypeAccess {
override string getPart(int i) { i = 0 and result = this.toString() }
}
private class PpArrayTypeAccess extends PpAst, ArrayTypeAccess {
override string getPart(int i) { i = 1 and result = "[]" }
override PpAst getChild(int i) { i = 0 and result = this.getComponentName() }
}
private class PpUnionTypeAccess extends PpAst, UnionTypeAccess {
override string getPart(int i) {
exists(int j | i = 2 * j - 1 and j != 0 and result = " | " and exists(this.getAlternative(j)))
}
private Expr getAlternative(int j) { result = this.getAnAlternative() and j = result.getIndex() }
override PpAst getChild(int i) { exists(int j | i = 2 * j and result = this.getAlternative(j)) }
}
private class PpIntersectionTypeAccess extends PpAst, IntersectionTypeAccess {
override string getPart(int i) {
exists(int j | i = 2 * j - 1 and j != 0 and result = " & " and exists(this.getBound(j)))
}
override PpAst getChild(int i) { exists(int j | i = 2 * j and result = this.getBound(j)) }
}
private class PpPackageAccess extends PpAst, PackageAccess {
override string getPart(int i) { i = 0 and result = "package" }
}
private class PpWildcardTypeAccess extends PpAst, WildcardTypeAccess {
override string getPart(int i) {
i = 0 and result = "?"
or
i = 1 and result = " extends " and exists(this.getUpperBound())
or
i = 1 and result = " super " and exists(this.getLowerBound())
}
override PpAst getChild(int i) {
i = 2 and result = this.getUpperBound()
or
i = 2 and result = this.getLowerBound()
}
}
/*
* Statements
*/
private class PpBlock extends PpAst, Block {
override string getPart(int i) {
i = 0 and result = "{"
or
i = 2 + 2 * this.getNumStmt() and result = "}"
}
override predicate newline(int i) { i = 1 or this.hasChildAt(_, i - 1) }
override PpAst getChild(int i) { this.hasChildAt(result, i) }
override predicate indents(int i) { this.hasChildAt(_, i) }
private predicate hasChildAt(PpAst c, int i) {
exists(int index | c = this.getStmt(index) and i = 2 + 2 * index)
}
}
private class PpIfStmt extends PpAst, IfStmt {
override string getPart(int i) {
i = 0 and result = "if ("
or
i = 2 and result = ")"
or
i = 3 and result = " " and this.getThen() instanceof Block
or
exists(this.getElse()) and
(
i = 5 and result = " " and this.getThen() instanceof Block
or
i = 6 and result = "else"
or
i = 7 and result = " " and this.getElse() instanceof Block
)
}
override predicate newline(int i) {
i = 3 and not this.getThen() instanceof Block
or
exists(this.getElse()) and
(
i = 5 and not this.getThen() instanceof Block
or
i = 7 and not this.getElse() instanceof Block
)
}
override PpAst getChild(int i) {
i = 1 and result = this.getCondition()
or
i = 4 and result = this.getThen()
or
i = 8 and result = this.getElse()
}
override predicate indents(int i) {
i = 4 and not this.getThen() instanceof Block
or
i = 8 and not this.getElse() instanceof Block
}
}
private class PpForStmt extends PpAst, ForStmt {
override string getPart(int i) {
i = 0 and result = "for ("
or
i = 2 and result = " " and this.getInit(0) instanceof LocalVariableDeclExpr
or
exists(int j | j > 0 and exists(this.getInit(j)) and i = 2 + 2 * j and result = ", ")
or
i = 1 + lastInitIndex() and result = "; "
or
i = 3 + lastInitIndex() and result = "; "
or
exists(int j |
j > 0 and exists(this.getUpdate(j)) and i = 3 + lastInitIndex() + 2 * j and result = ", "
)
or
i = 1 + lastUpdateIndex() and result = ")"
or
i = 2 + lastUpdateIndex() and result = " " and this.getStmt() instanceof Block
}
private int lastInitIndex() { result = 3 + 2 * max(int j | exists(this.getInit(j))) }
private int lastUpdateIndex() {
result = 4 + lastInitIndex() + 2 * max(int j | exists(this.getUpdate(j)))
}
override predicate newline(int i) {
i = 2 + lastUpdateIndex() and not this.getStmt() instanceof Block
}
override PpAst getChild(int i) {
i = 1 and result = this.getInit(0).(LocalVariableDeclExpr).getTypeAccess()
or
exists(int j | result = this.getInit(j) and i = 3 + 2 * j)
or
i = 2 + lastInitIndex() and result = this.getCondition()
or
exists(int j | result = this.getUpdate(j) and i = 4 + lastInitIndex() + 2 * j)
or
i = 3 + lastUpdateIndex() and result = this.getStmt()
}
override predicate indents(int i) {
i = 3 + lastUpdateIndex() and not this.getStmt() instanceof Block
}
}
private class PpEnhancedForStmt extends PpAst, EnhancedForStmt {
override string getPart(int i) {
i = 0 and result = "for ("
or
i = 2 and result = " "
or
i = 4 and result = " : "
or
i = 6 and
if this.getStmt() instanceof Block then result = ") " else result = ")"
}
override PpAst getChild(int i) {
i = 1 and result = this.getVariable().getTypeAccess()
or
i = 3 and result = this.getVariable()
or
i = 5 and result = this.getExpr()
or
i = 7 and result = this.getStmt()
}
override predicate indents(int i) { i = 7 and not this.getStmt() instanceof Block }
}
private class PpWhileStmt extends PpAst, WhileStmt {
override string getPart(int i) {
i = 0 and result = "while ("
or
i = 2 and result = ")"
or
i = 3 and result = " " and this.getStmt() instanceof Block
}
override predicate newline(int i) { i = 3 and not this.getStmt() instanceof Block }
override PpAst getChild(int i) {
i = 1 and result = this.getCondition()
or
i = 4 and result = this.getStmt()
}
override predicate indents(int i) { i = 4 and not this.getStmt() instanceof Block }
}
private class PpDoStmt extends PpAst, DoStmt {
override string getPart(int i) {
i = 0 and result = "do"
or
i in [1, 3] and result = " " and this.getStmt() instanceof Block
or
i = 4 and result = "while ("
or
i = 6 and result = ");"
}
override predicate newline(int i) { i in [1, 3] and not this.getStmt() instanceof Block }
override PpAst getChild(int i) {
i = 2 and result = this.getStmt()
or
i = 5 and result = this.getCondition()
}
override predicate indents(int i) { i = 2 and not this.getStmt() instanceof Block }
}
private class PpTryStmt extends PpAst, TryStmt {
override string getPart(int i) {
i = 0 and result = "try "
or
i = 1 and result = "(" and exists(this.getAResource())
or
exists(int j | exists(this.getResourceExpr(j)) and i = 3 + 2 * j and result = ";")
or
i = 2 + lastResourceIndex() and result = ") " and exists(this.getAResource())
or
i = 1 + lastCatchIndex() and result = " finally " and exists(this.getFinally())
}
private int lastResourceIndex() {
result = 2 + 2 * max(int j | exists(this.getResource(j)) or j = 0)
}
private int lastCatchIndex() {
result = 4 + lastResourceIndex() + max(int j | exists(this.getCatchClause(j)) or j = 0)
}
override PpAst getChild(int i) {
exists(int j | i = 2 + 2 * j and result = this.getResource(j))
or
i = 3 + lastResourceIndex() and result = this.getBlock()
or
exists(int j | i = 4 + lastResourceIndex() + j and result = this.getCatchClause(j))
or
i = 2 + lastCatchIndex() and result = this.getFinally()
}
}
private class PpCatchClause extends PpAst, CatchClause {
override string getPart(int i) {
i = 0 and result = " catch ("
or
i = 2 and result = " "
or
i = 4 and result = ") "
}
override PpAst getChild(int i) {
i = 1 and result = this.getVariable().getTypeAccess()
or
i = 3 and result = this.getVariable()
or
i = 5 and result = this.getBlock()
}
}
private class PpSwitchStmt extends PpAst, SwitchStmt {
override string getPart(int i) {
i = 0 and result = "switch ("
or
i = 2 and result = ") {"
or
i = 4 + 2 * count(this.getAStmt()) and result = "}"
}
override predicate newline(int i) { i = 3 or this.hasChildAt(_, i - 1) }
override PpAst getChild(int i) {
i = 1 and result = this.getExpr()
or
this.hasChildAt(result, i)
}
override predicate indents(int i) { this.hasChildAt(_, i) }
private predicate hasChildAt(PpAst c, int i) {
exists(int index | c = this.getStmt(index) and i = 4 + 2 * index)
}
}
private class PpSwitchCase extends PpAst, SwitchCase {
override string getPart(int i) {
i = 0 and result = "default" and this instanceof DefaultCase
or
i = 0 and result = "case " and this instanceof ConstCase
or
exists(int j | i = 2 * j and j != 0 and result = ", " and exists(this.(ConstCase).getValue(j)))
or
i = 1 + lastConstCaseValueIndex() and result = ":" and not this.isRule()
or
i = 1 + lastConstCaseValueIndex() and result = " -> " and this.isRule()
or
i = 3 + lastConstCaseValueIndex() and result = ";" and exists(this.getRuleExpression())
}
private int lastConstCaseValueIndex() {
result = 1 + 2 * max(int j | j = 0 or exists(this.(ConstCase).getValue(j)))
}
override PpAst getChild(int i) {
exists(int j | i = 1 + 2 * j and result = this.(ConstCase).getValue(j))
or
i = 2 + lastConstCaseValueIndex() and result = this.getRuleExpression()
or
i = 2 + lastConstCaseValueIndex() and result = this.getRuleStatement()
}
}
private class PpSynchronizedStmt extends PpAst, SynchronizedStmt {
override string getPart(int i) {
i = 0 and result = "synchronized ("
or
i = 2 and result = ")"
or
i = 3 and result = " "
}
override PpAst getChild(int i) {
i = 1 and result = this.getExpr()
or
i = 4 and result = this.getBlock()
}
}
private class PpReturnStmt extends PpAst, ReturnStmt {
override string getPart(int i) {
if exists(this.getResult())
then
i = 0 and result = "return "
or
i = 2 and result = ";"
else (
i = 0 and result = "return;"
)
}
override PpAst getChild(int i) { i = 1 and result = this.getResult() }
}
private class PpThrowStmt extends PpAst, ThrowStmt {
override string getPart(int i) {
i = 0 and result = "throw "
or
i = 2 and result = ";"
}
override PpAst getChild(int i) { i = 1 and result = this.getExpr() }
}
private class PpBreakStmt extends PpAst, BreakStmt {
override string getPart(int i) {
i = 0 and result = "break"
or
i = 1 and result = " " and exists(this.getLabel())
or
i = 2 and result = this.getLabel()
or
i = 3 and result = ";"
}
}
private class PpYieldStmt extends PpAst, YieldStmt {
override string getPart(int i) {
i = 0 and result = "yield "
or
i = 2 and result = ";"
}
override PpAst getChild(int i) { i = 1 and result = this.getValue() }
}
private class PpContinueStmt extends PpAst, ContinueStmt {
override string getPart(int i) {
i = 0 and result = "continue"
or
i = 1 and result = " " and exists(this.getLabel())
or
i = 2 and result = this.getLabel()
or
i = 3 and result = ";"
}
}
private class PpEmptyStmt extends PpAst, EmptyStmt {
override string getPart(int i) { i = 0 and result = ";" }
}
private class PpExprStmt extends PpAst, ExprStmt {
override string getPart(int i) { i = 1 and result = ";" }
override PpAst getChild(int i) { i = 0 and result = this.getExpr() }
}
private class PpLabeledStmt extends PpAst, LabeledStmt {
override string getPart(int i) {
i = 0 and result = this.getLabel()
or
i = 1 and result = ":"
}
override predicate newline(int i) { i = 2 }
override PpAst getChild(int i) { i = 3 and result = this.getStmt() }
}
private class PpAssertStmt extends PpAst, AssertStmt {
override string getPart(int i) {
i = 0 and result = "assert "
or
i = 2 and result = " : " and exists(this.getMessage())
or
i = 4 and result = ";"
}
override PpAst getChild(int i) {
i = 1 and result = this.getExpr()
or
i = 3 and result = this.getMessage()
}
}
private class PpLocalVariableDeclStmt extends PpAst, LocalVariableDeclStmt {
override string getPart(int i) {
i = 1 and result = " "
or
exists(int v | v > 1 and i = 2 * v - 1 and result = ", " and v = this.getAVariableIndex())
or
i = 2 * max(this.getAVariableIndex()) + 1 and result = ";"
}
override PpAst getChild(int i) {
i = 0 and result = this.getAVariable().getTypeAccess()
or
exists(int v | i = 2 * v and result = this.getVariable(v))
}
}
private class PpLocalClassDeclStmt extends PpAst, LocalClassDeclStmt {
override PpAst getChild(int i) { i = 0 and result = this.getLocalClass() }
}
/*
* Classes, interfaces, and members
*/
private string getMemberId(Member m) {
result = m.(Callable).getSignature()
or
result = m.getName() and not m instanceof Callable
}
private class PpClassOrInterface extends PpAst, ClassOrInterface {
override string getPart(int i) {
not this instanceof AnonymousClass and
(
result = getModifierPart(this, i)
or
i = 0 and result = "class " and this instanceof Class
or
i = 0 and result = "interface " and this instanceof Interface
or
i = 1 and result = this.getName()
or
i = 2 and result = " "
)
or
i = 3 and result = "{"
or
i = 5 + 3 * max(this.memberRank(_)) and result = "}"
}
override predicate newline(int i) {
exists(int ci | ci = 3 + 3 * this.memberRank(_) | i = ci - 1 or i = ci + 1)
}
private int memberRank(Member member) {
member =
rank[result](Member m |
m = this.getAMember()
|
m order by m.getLocation().getStartLine(), m.getLocation().getStartColumn(), getMemberId(m)
)
}
override PpAst getChild(int i) { this.memberRank(result) * 3 + 3 = i }
override predicate indents(int i) { this.memberRank(_) * 3 + 3 = i }
}
private string getModifierPart(Modifiable m, int i) {
m.isAbstract() and result = "abstract " and i = -12
or
m.isPublic() and result = "public " and i = -11
or
m.isProtected() and result = "protected " and i = -10
or
m.isPrivate() and result = "private " and i = -9
or
m.isStatic() and result = "static " and i = -8
or
m.isFinal() and result = "final " and i = -7
or
m.isVolatile() and result = "volatile " and i = -6
or
m.isSynchronized() and result = "synchronized " and i = -5
or
m.isNative() and result = "native " and i = -4
or
m.isDefault() and result = "default " and i = -3
or
m.isTransient() and result = "transient " and i = -2
or
m.isStrictfp() and result = "strictfp " and i = -1
}
private class PpField extends PpAst, Field {
override string getPart(int i) {
result = getModifierPart(this, i)
or
i = 0 and result = this.getType().toString()
or
i = 1 and result = " "
or
i = 2 and result = this.getName()
or
i = 3 and result = ";"
}
}
private class PpCallable extends PpAst, Callable {
override string getPart(int i) {
result = getModifierPart(this, i)
or
i = 0 and result = this.getReturnType().toString() and this instanceof Method
or
i = 1 and result = " " and this instanceof Method
or
i = 2 and
(if this.getName() = "" then result = "<no name>" else result = this.getName())
or
i = 3 and result = "("
or
exists(Parameter p, int n | this.getParameter(n) = p |
i = 4 + 4 * n and result = p.getType().toString()
or
i = 5 + 4 * n and result = " "
or
i = 6 + 4 * n and result = p.getName()
or
i = 7 + 4 * n and result = ", " and n < this.getNumberOfParameters() - 1
)
or
i = 4 + 4 * this.getNumberOfParameters() and result = ") "
or
i = 5 + 4 * this.getNumberOfParameters() and
not exists(this.getBody()) and
result = "{ <missing body> }"
}