forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAst.qll
More file actions
2830 lines (2209 loc) · 80.3 KB
/
Ast.qll
File metadata and controls
2830 lines (2209 loc) · 80.3 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
import ql
private import codeql_ql.ast.internal.AstNodes
private import codeql_ql.ast.internal.Module
private import codeql_ql.ast.internal.Predicate
import codeql_ql.ast.internal.Type
private import codeql_ql.ast.internal.Variable
private import codeql_ql.ast.internal.Builtins
private import internal.AstMocks as Mocks
bindingset[name]
private string directMember(string name) { result = name + "()" }
bindingset[name, i]
private string indexedMember(string name, int i) { result = name + "(_)" and exists(i) }
bindingset[name, index]
private string stringIndexedMember(string name, string index) {
result = name + "(_)" and exists(index)
}
/** An AST node of a QL program */
class AstNode extends TAstNode {
string toString() { result = this.getAPrimaryQlClass() }
/** Gets the location of the AST node. */
cached
Location getLocation() { result = this.getFullLocation() } // overridden in some subclasses
/** Gets the file containing this AST node. */
cached
File getFile() { result = this.getFullLocation().getFile() }
/** Gets the location that spans the entire AST node. */
cached
final Location getFullLocation() {
exists(QL::AstNode node | not node instanceof QL::ParExpr |
node = toQL(this) and
result = node.getLocation()
)
or
result = toDbscheme(this).getLocation()
}
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
if exists(this.getLocation())
then this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
else (
filepath = "" and
startline = 0 and
startcolumn = 0 and
endline = 0 and
endcolumn = 0
)
}
/**
* Gets the parent in the AST for this node.
*/
cached
AstNode getParent() { result.getAChild(_) = this }
/**
* Gets a child of this node, which can also be retrieved using a predicate
* named `pred`.
*/
cached
AstNode getAChild(string pred) {
pred = directMember("getAnAnnotation") and result = this.getAnAnnotation()
or
pred = directMember("getQLDoc") and result = this.getQLDoc()
}
/** Gets any child of this node. */
AstNode getAChild() { result = this.getAChild(_) }
/**
* Gets the primary QL class for the ast node.
*/
string getAPrimaryQlClass() { result = "???" }
/** Gets the QLDoc comment for this AST node, if any. */
QLDoc getQLDoc() { none() }
/** Holds if `node` has an annotation with `name`. */
predicate hasAnnotation(string name) { this.getAnAnnotation().getName() = name }
/** Gets an annotation of this AST node. */
Annotation getAnAnnotation() {
not this instanceof Annotation and // avoid cyclic parent-child relationship
toQL(this).getParent() = pragma[only_bind_out](toQL(result)).getParent() and
// special case that is handled in `NewTypeBranch`
not any(QL::DatatypeBranch branch) = pragma[only_bind_out](toQL(result)).getParent()
}
/**
* Gets the predicate that contains this AST node.
*/
cached
Predicate getEnclosingPredicate() { this = getANodeInPredicate(result) }
}
private AstNode getANodeInPredicate(Predicate pred) {
result = pred.getAChild(_)
or
result = getANodeInPredicate(pred).getAChild(_)
}
/** A toplevel QL program, i.e. a file. */
class TopLevel extends TTopLevel, AstNode {
QL::Ql file;
TopLevel() { this = TTopLevel(file) }
/**
* Gets a member from contained in this top-level module.
* Includes private members.
*/
ModuleMember getAMember() { result = this.getMember(_) }
/** Gets the `i`'th member of this top-level module. */
ModuleMember getMember(int i) { toQL(result) = file.getChild(i).getChild(_) }
/** Gets a top-level import in this module. */
Import getAnImport() { result = this.getAMember() }
/** Gets a top-level class in this module. */
Class getAClass() { result = this.getAMember() }
/** Gets a top-level predicate in this module. */
ClasslessPredicate getAPredicate() { result = this.getAMember() }
/** Gets a module defined at the top-level of this module. */
Module getAModule() { result = this.getAMember() }
/** Gets a `newtype` defined at the top-level of this module. */
NewType getANewType() { result = this.getAMember() }
/** Gets a `select` clause in the top-level of this module. */
Select getASelect() { result = this.getAMember() }
override ModuleMember getAChild(string pred) {
pred = directMember("getAnImport") and result = this.getAnImport()
or
pred = directMember("getAClass") and result = this.getAClass()
or
pred = directMember("getAPredicate") and result = this.getAPredicate()
or
pred = directMember("getAModule") and result = this.getAModule()
or
pred = directMember("getANewType") and result = this.getANewType()
or
pred = directMember("getQLDoc") and result = this.getQLDoc()
or
pred = directMember("getASelect") and result = this.getASelect()
}
QLDoc getQLDocFor(ModuleMember m) {
exists(int i | result = this.getMember(i) and m = this.getMember(i + 1)) and
(
m instanceof ClasslessPredicate
or
m instanceof Class
or
m instanceof Module
)
}
override string getAPrimaryQlClass() { result = "TopLevel" }
override QLDoc getQLDoc() {
result = this.getMember(0) and
// it's not the QLDoc for a module member
not this.getQLDocFor(_) = result and
result.getLocation().getStartLine() = 1 // this might not hold if there is a block comment above, and that's the point.
}
}
abstract class Comment extends AstNode, TComment {
abstract string getContents();
}
class QLDoc extends TQLDoc, Comment {
QL::Qldoc qldoc;
QLDoc() { this = TQLDoc(qldoc) }
override string getContents() { result = qldoc.getValue() }
override string getAPrimaryQlClass() { result = "QLDoc" }
override AstNode getParent() { result.getQLDoc() = this }
}
/** The QLDoc for a query (i.e. the top comment in a .ql file). */
class QueryDoc extends QLDoc {
QueryDoc() {
this.getLocation().getFile().getExtension() = "ql" and
this = any(TopLevel t).getQLDoc()
}
override string getAPrimaryQlClass() { result = "QueryDoc" }
/** Gets the @kind for the query. */
string getQueryKind() {
result = this.getContents().regexpCapture("(?s).*@kind ([\\w-]+)\\s.*", 1)
}
/** Gets the @name for the query. */
string getQueryName() {
result = this.getContents().regexpCapture("(?s).*@name (.+?)(?=\\n).*", 1)
}
/** Gets the id part (without language) of the @id. */
string getQueryId() {
result = this.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-/]+)\\s.*", 2)
}
/** Gets the language of the @id. */
string getQueryLanguage() {
result = this.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-/]+)\\s.*", 1)
}
/** Gets the @precision for the query. */
string getQueryPrecision() {
result = this.getContents().regexpCapture("(?s).*@precision ([\\w\\-]+)\\s.*", 1)
}
/** Gets the @security-severity for the query. */
string getQuerySecuritySeverity() {
result = this.getContents().regexpCapture("(?s).*@security\\-severity ([\\d\\.]+)\\s.*", 1)
}
/** Gets the individual @tags for the query, if any. */
string getAQueryTag() {
exists(string tags | tags = this.getContents().regexpCapture("(?s).*@tags ([^@]+)", 1) |
result = tags.splitAt("*").trim() and
result.regexpMatch("[\\w\\s\\-]+")
)
}
}
class BlockComment extends TBlockComment, Comment {
QL::BlockComment comment;
BlockComment() { this = TBlockComment(comment) }
override string getContents() { result = comment.getValue() }
override string getAPrimaryQlClass() { result = "BlockComment" }
}
class LineComment extends TLineComment, Comment {
QL::LineComment comment;
LineComment() { this = TLineComment(comment) }
override string getContents() { result = comment.getValue() }
override string getAPrimaryQlClass() { result = "LineComment" }
}
/**
* The `from, where, select` part of a QL query.
*/
class Select extends TSelect, AstNode {
QL::Select sel;
Select() { this = TSelect(sel) }
/**
* Gets the `i`th variable in the `from` clause.
*/
VarDecl getVarDecl(int i) { toQL(result) = sel.getChild(i) }
/**
* Gets the formula in the `where`.
*/
Formula getWhere() { toQL(result) = sel.getChild(_) }
/**
* Gets the `i`th expression in the `select` clause.
*/
Expr getExpr(int i) { toQL(result) = sel.getChild(_).(QL::AsExprs).getChild(i) }
Expr getMessage() {
if this.getQueryDoc().getQueryKind() = "path-problem"
then result = this.getExpr(3)
else result = this.getExpr(1)
}
// TODO: This gets the `i`th order-by, but some expressions might not have an order-by.
Expr getOrderBy(int i) { toQL(result) = sel.getChild(_).(QL::OrderBys).getChild(i).getChild(0) }
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getWhere") and result = this.getWhere()
or
exists(int i |
pred = indexedMember("getVarDecl", i) and result = this.getVarDecl(i)
or
pred = indexedMember("getExpr", i) and result = this.getExpr(i)
or
pred = indexedMember("getOrderBy", i) and result = this.getOrderBy(i)
)
}
override string getAPrimaryQlClass() { result = "Select" }
QueryDoc getQueryDoc() { result.getLocation().getFile() = this.getLocation().getFile() }
}
class PredicateOrBuiltin extends TPredOrBuiltin, AstNode {
string getName() { none() }
Type getDeclaringType() { none() }
Type getParameterType(int i) { none() }
Type getReturnType() { none() }
int getArity() { result = count(int i | exists(this.getParameterType(i))) }
predicate isPrivate() { none() }
}
class BuiltinPredicate extends PredicateOrBuiltin, TBuiltin {
override string toString() { result = this.getName() }
override string getAPrimaryQlClass() { result = "BuiltinPredicate" }
}
class BuiltinClassless extends BuiltinPredicate, TBuiltinClassless {
string name;
string ret;
string args;
BuiltinClassless() { this = TBuiltinClassless(ret, name, args) }
override string getName() { result = name }
override PrimitiveType getReturnType() { result.getName() = ret }
override PrimitiveType getParameterType(int i) { result.getName() = getArgType(args, i) }
}
class BuiltinMember extends BuiltinPredicate, TBuiltinMember {
string name;
string qual;
string ret;
string args;
BuiltinMember() { this = TBuiltinMember(qual, ret, name, args) }
override string getName() { result = name }
override PrimitiveType getReturnType() { result.getName() = ret }
override PrimitiveType getParameterType(int i) { result.getName() = getArgType(args, i) }
override PrimitiveType getDeclaringType() { result.getName() = qual }
}
/**
* A QL predicate.
* Either a classless predicate, a class predicate, or a characteristic predicate.
*/
class Predicate extends TPredicate, AstNode, PredicateOrBuiltin, Declaration {
/**
* Gets the body of the predicate.
*/
Formula getBody() { none() }
/**
* Gets the name of the predicate.
*/
override string getName() { none() }
/**
* Gets the `i`th parameter of the predicate.
*/
VarDecl getParameter(int i) { none() }
/**
* Gets the number of parameters.
*/
override int getArity() {
not this.(ClasslessPredicate).getAlias() instanceof PredicateExpr and
result = count(this.getParameter(_))
or
exists(PredicateExpr alias | alias = this.(ClasslessPredicate).getAlias() |
result = alias.getArity()
)
}
/**
* Holds if this predicate is private.
*/
override predicate isPrivate() { this.hasAnnotation("private") }
/**
* Gets the return type (if any) of the predicate.
*/
TypeExpr getReturnTypeExpr() { none() }
override Type getReturnType() { result = this.getReturnTypeExpr().getResolvedType() }
override Type getParameterType(int i) { result = this.getParameter(i).getType() }
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getBody") and result = this.getBody()
or
exists(int i | pred = indexedMember("getParameter", i) and result = this.getParameter(i))
or
pred = directMember("getReturnTypeExpr") and result = this.getReturnTypeExpr()
}
override string getAPrimaryQlClass() { result = "Predicate" }
}
/**
* A relation in the database.
*/
class Relation extends TDBRelation, AstNode, Declaration {
Dbscheme::Table table;
Relation() { this = TDBRelation(table) }
/**
* Gets the name of the relation.
*/
override string getName() { result = table.getTableName().getChild().getValue() }
private Dbscheme::Column getColumn(int i) {
result =
rank[i + 1](Dbscheme::Column column, int child |
table.getChild(child) = column
|
column order by child
)
}
/** Gets the `i`th parameter name */
string getParameterName(int i) { result = this.getColumn(i).getColName().getValue() }
/** Gets the `i`th parameter type */
string getParameterType(int i) {
// TODO: This is just using the name of the type, not the actual type. Checkout Type.qll
result = this.getColumn(i).getColType().getChild().(Dbscheme::Token).getValue()
}
/**
* Gets the number of parameters.
*/
int getArity() { result = count(this.getColumn(_)) }
override string getAPrimaryQlClass() { result = "Relation" }
}
/**
* An expression that refers to a predicate, e.g. `BasicBlock::succ/2`.
*/
class PredicateExpr extends TPredicateExpr, AstNode {
QL::PredicateExpr pe;
PredicateExpr() { this = TPredicateExpr(pe) }
override string toString() { result = "predicate" }
/**
* Gets the name of the predicate.
* E.g. for `BasicBlock::succ/2` the result is "succ".
*/
string getName() {
exists(QL::AritylessPredicateExpr ape, QL::LiteralId id |
ape.getParent() = pe and
id.getParent() = ape and
result = id.getValue()
)
}
/**
* Gets the arity of the predicate.
* E.g. for `BasicBlock::succ/2` the result is 2.
*/
int getArity() {
exists(QL::Integer i |
i.getParent() = pe and
result = i.getValue().toInt()
)
}
/**
* Gets the module containing the predicate.
* E.g. for `BasicBlock::succ/2` the result is a `ModuleExpr` representing "BasicBlock".
*/
ModuleExpr getQualifier() {
exists(QL::AritylessPredicateExpr ape |
ape.getParent() = pe and
toQL(result).getParent() = ape
)
}
/**
* Gets the predicate that this expression refers to.
*/
Predicate getResolvedPredicate() { resolvePredicateExpr(this, result) }
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getQualifier") and result = this.getQualifier()
}
override string getAPrimaryQlClass() { result = "PredicateExpr" }
}
/**
* A classless predicate.
*/
class ClasslessPredicate extends TClasslessPredicate, Predicate, ModuleDeclaration {
Mocks::ClasslessPredicateOrMock pred;
ClasslessPredicate() { this = TClasslessPredicate(pred) }
override Location getLocation() { result = pred.asLeft().getName().getLocation() }
/**
* Gets the aliased value if this predicate is an alias
* E.g. for `predicate foo = Module::bar/2;` gets `Module::bar/2`.
* The result is either a `PredicateExpr` or `HigherOrderFormula`.
*/
final AstNode getAlias() {
exists(QL::PredicateAliasBody alias |
alias.getParent() = pred.asLeft() and
toQL(result).getParent() = alias
)
or
toQL(result) = pred.asLeft().getChild(_).(QL::HigherOrderTerm)
}
override string getAPrimaryQlClass() { result = "ClasslessPredicate" }
override Formula getBody() { toQL(result) = pred.asLeft().getChild(_).(QL::Body).getChild() }
override string getName() {
result = pred.asLeft().getName().getValue()
or
result = pred.asRight().getName()
}
override VarDecl getParameter(int i) {
toQL(result) =
rank[i + 1](QL::VarDecl decl, int index |
decl = pred.asLeft().getChild(index)
|
decl order by index
)
or
toMock(result) = pred.asRight().getParameter(i)
}
override TypeExpr getReturnTypeExpr() {
toQL(result) = pred.asLeft().getReturnType()
or
toMock(result) = pred.asRight().getReturnTypeExpr()
}
override AstNode getAChild(string pred_name) {
result = Predicate.super.getAChild(pred_name)
or
pred_name = directMember("getAlias") and result = this.getAlias()
or
pred_name = directMember("getBody") and result = this.getBody()
or
exists(int i | pred_name = indexedMember("getParameter", i) and result = this.getParameter(i))
or
pred_name = directMember("getReturnTypeExpr") and result = this.getReturnTypeExpr()
}
override predicate isPrivate() { Predicate.super.isPrivate() }
/** Holds if this classless predicate is a signature predicate with no body. */
override predicate isSignature() { not exists(this.getBody()) }
override QLDoc getQLDoc() {
result = any(TopLevel m).getQLDocFor(this)
or
result = any(Module m).getQLDocFor(this)
}
}
/**
* A predicate in a class.
*/
class ClassPredicate extends TClassPredicate, Predicate {
QL::MemberPredicate pred;
ClassPredicate() { this = TClassPredicate(pred) }
override Location getLocation() { result = pred.getName().getLocation() }
override string getName() { result = pred.getName().getValue() }
override Formula getBody() { toQL(result) = pred.getChild(_).(QL::Body).getChild() }
override string getAPrimaryQlClass() { result = "ClassPredicate" }
override Class getParent() { result.getAClassPredicate() = this }
/**
* Holds if this predicate is annotated as overriding another predicate.
*/
predicate isOverride() { this.hasAnnotation("override") }
override VarDecl getParameter(int i) {
toQL(result) =
rank[i + 1](QL::VarDecl decl, int index | decl = pred.getChild(index) | decl order by index)
}
/**
* Gets the type representing this class.
*/
override ClassType getDeclaringType() { result.getDeclaration() = this.getParent() }
predicate overrides(ClassPredicate other) { predOverrides(this, other) }
predicate shadows(ClassPredicate other) { predShadows(this, other) }
override TypeExpr getReturnTypeExpr() { toQL(result) = pred.getReturnType() }
override AstNode getAChild(string pred_name) {
result = super.getAChild(pred_name)
or
pred_name = directMember("getBody") and result = this.getBody()
or
exists(int i | pred_name = indexedMember("getParameter", i) and result = this.getParameter(i))
or
pred_name = directMember("getReturnTypeExpr") and result = this.getReturnTypeExpr()
}
}
/**
* A characteristic predicate of a class.
*/
class CharPred extends TCharPred, Predicate {
QL::Charpred pred;
CharPred() { this = TCharPred(pred) }
override string getAPrimaryQlClass() { result = "CharPred" }
override Formula getBody() { toQL(result) = pred.getBody() }
override string getName() { result = this.getParent().(Class).getName() }
override AstNode getAChild(string pred_name) {
result = super.getAChild(pred_name)
or
pred_name = directMember("getBody") and result = this.getBody()
}
override ClassType getDeclaringType() { result.getDeclaration() = this.getParent() }
}
/**
* A variable definition. This is either a variable declaration or
* an `as` expression.
*/
class VarDef extends TVarDef, AstNode {
/** Gets the name of the declared variable. */
string getName() { none() }
Type getType() { none() }
/** Gets a variable access to this `VarDef` */
VarAccess getAnAccess() { result.getDeclaration() = this }
override string getAPrimaryQlClass() { result = "VarDef" }
override string toString() { result = this.getName() }
}
/**
* A variable declaration, with a type and a name.
*/
class VarDecl extends TVarDecl, VarDef, Declaration {
Mocks::VarDeclOrMock var;
VarDecl() { this = TVarDecl(var) }
override string getName() {
result = var.asLeft().getChild(1).(QL::VarName).getChild().getValue()
or
result = var.asRight().getName()
}
override Type getType() { result = this.getTypeExpr().getResolvedType() }
override string getAPrimaryQlClass() { result = "VarDecl" }
/**
* Gets the type part of this variable declaration.
*/
TypeExpr getTypeExpr() {
toQL(result) = var.asLeft().getChild(0)
or
toMock(result) = var.asRight().getType()
}
/**
* Holds if this variable declaration is a private field on a class.
*/
predicate isPrivate() {
exists(QL::ClassMember member |
var.asLeft() = member.getChild(_).(QL::Field).getChild() and
member.getAFieldOrChild().(QL::Annotation).getName().getValue() = "private"
)
}
/** If this is declared in a field, returns the class type that declares it. */
ClassType getDeclaringType() {
exists(FieldDecl f | f.getVarDecl() = this and result = f.getParent().(Class).getType())
}
/**
* Holds if this is a class field that overrides the field `other`.
*/
predicate overrides(VarDecl other) { fieldOverrides(this, other) }
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getTypeExpr") and result = this.getTypeExpr()
}
override string toString() { result = this.getName() }
}
/**
* A field declaration;
*/
class FieldDecl extends TFieldDecl, AstNode {
QL::Field f;
FieldDecl() { this = TFieldDecl(f) }
VarDecl getVarDecl() { toQL(result) = f.getChild() }
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getVarDecl") and result = this.getVarDecl()
}
override string getAPrimaryQlClass() { result = "FieldDecl" }
/** Holds if this field is annotated as overriding another field. */
predicate isOverride() { this.hasAnnotation("override") }
string getName() { result = this.getVarDecl().getName() }
override QLDoc getQLDoc() { result = any(Class c).getQLDocFor(this) }
}
/**
* A type reference, such as `DataFlow::Node`.
*/
class TypeExpr extends TType, TypeRef {
Mocks::TypeExprOrMock type;
TypeExpr() { this = TType(type) }
override string getAPrimaryQlClass() { result = "TypeExpr" }
/**
* Gets the class name for the type.
* E.g. `Node` in `DataFlow::Node`.
* Also gets the name for primitive types such as `string` or `int`
* or db-types such as `@locateable`.
*/
string getClassName() {
result = type.asLeft().getName().getValue()
or
result = type.asLeft().getChild().(QL::PrimitiveType).getValue()
or
result = type.asLeft().getChild().(QL::Dbtype).getValue()
or
result = type.asRight().getClassName()
}
/**
* Holds if this type is a primitive such as `string` or `int`.
*/
predicate isPrimitive() { type.asLeft().getChild() instanceof QL::PrimitiveType }
/**
* Holds if this type is a db-type.
*/
predicate isDBType() { type.asLeft().getChild() instanceof QL::Dbtype }
/**
* Gets the module of the type, if it exists.
* E.g. `DataFlow` in `DataFlow::Node`.
*/
ModuleExpr getModule() { toQL(result) = type.asLeft().getQualifier() }
/** Gets the type that this type reference refers to. */
override Type getResolvedType() {
// resolve type
resolveTypeExpr(this, result)
or
// if it resolves to a module,
exists(FileOrModule mod | resolveModuleRef(this, mod) | result = mod.toType())
}
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getModule") and result = this.getModule()
}
}
/**
* A QL module.
*/
class Module extends TModule, ModuleDeclaration {
Mocks::ModuleOrMock mod;
Module() { this = TModule(mod) }
override Location getLocation() { result = mod.asLeft().getName().getLocation() }
override string getAPrimaryQlClass() { result = "Module" }
override string getName() {
result = mod.asLeft().getName().getChild().getValue()
or
result = mod.asRight().getName()
}
/**
* Gets a member of the module.
*/
AstNode getAMember() { result = this.getMember(_) }
AstNode getMember(int i) {
toQL(result) = mod.asLeft().getChild(i).(QL::ModuleMember).getChild(_)
or
toMock(result) = mod.asRight().getMember(i)
}
pragma[nomagic]
Declaration getDeclaration(string name) {
result = this.getAMember() and
name = result.getName()
}
QLDoc getQLDocFor(AstNode m) {
exists(int i | result = this.getMember(i) and m = this.getMember(i + 1))
}
/** Gets a ref to the module that this module implements. */
TypeRef getImplements(int i) {
exists(SignatureExpr sig | sig.toQL() = mod.asLeft().getImplements(i) | result = sig.asType())
}
/** Gets the module expression that this module is an alias for, if any. */
ModuleExpr getAlias() {
toQL(result) = mod.asLeft().getAFieldOrChild().(QL::ModuleAliasBody).getChild()
}
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = directMember("getAlias") and result = this.getAlias()
or
pred = directMember("getAMember") and result = this.getAMember()
or
exists(int i | pred = indexedMember("getImplements", i) and result = this.getImplements(i))
or
exists(int i | pred = indexedMember("hasParameter", i) and this.hasParameter(i, _, result))
}
/** Holds if the `i`th parameter of this module has `name` and type `sig`. */
predicate hasParameter(int i, string name, SignatureExpr sig) {
exists(QL::ModuleParam param |
param = mod.asLeft().getParameter(i) and
name = param.getParameter().getValue() and
sig.toQL() = param.getSignature()
)
or
mod.asRight().hasTypeParam(i, toMock(sig), name)
}
}
/**
* A member of a module.
*/
class ModuleMember extends TModuleMember, AstNode {
/** Holds if this member is declared as `private`. */
predicate isPrivate() { this.hasAnnotation("private") }
/** Holds if this member is declared as `final`. */
predicate isFinal() { this.hasAnnotation("final") }
/** Holds if this member is declared as `deprecated`. */
predicate isDeprecated() { this.hasAnnotation("deprecated") }
}
private newtype TDeclarationKind =
TClassKind() or
TModuleKind() or
TPredicateKind(int arity) { arity = any(Predicate p).getArity() }
private TDeclarationKind getDeclarationKind(Declaration d) {
d instanceof Class and result = TClassKind()
or
d instanceof Module and result = TModuleKind()
or
d = any(Predicate p | result = TPredicateKind(p.getArity()))
}
/** Holds if module `m` must implement signature declaration `d` with name `name` and kind `kind`. */
pragma[nomagic]
private predicate mustImplement(Module m, string name, TDeclarationKind kind, Declaration d) {
d = m.getImplements(_).getResolvedType().getDeclaration().(Module).getAMember() and
name = d.getName() and
kind = getDeclarationKind(d)
}
pragma[nomagic]
private Declaration getDeclaration(Module m, string name, TDeclarationKind kind) {
result = m.getDeclaration(name) and
kind = getDeclarationKind(result)
}
/** A declaration. E.g. a class, type, predicate, newtype... */
class Declaration extends TDeclaration, AstNode {
/** Gets the name of this declaration. */
string getName() { none() }
override string toString() { result = this.getAPrimaryQlClass() + " " + this.getName() }
override QLDoc getQLDoc() {
result = any(TopLevel m).getQLDocFor(this)
or
result = any(Module m).getQLDocFor(this)
or
result = any(Class c).getQLDocFor(this)
}
predicate isSignature() { this.hasAnnotation("signature") }
/** Holds if this declaration implements `other`. */
predicate implements(Declaration other) {
exists(Module m, string name, TDeclarationKind kind |
this = getDeclaration(m, name, kind) and
mustImplement(m, name, kind, other)
)
}
}
/** An entity that can be declared in a module. */
class ModuleDeclaration extends TModuleDeclaration, Declaration, ModuleMember { }
/** An type declaration. Either a `class` or a `newtype`. */
class TypeDeclaration extends TTypeDeclaration, Declaration { }
/**
* A QL class.
*/
class Class extends TClass, TypeDeclaration, ModuleDeclaration {
Mocks::ClassOrMock cls;
Class() { this = TClass(cls) }
override Location getLocation() { result = cls.asLeft().getName().getLocation() }
override string getAPrimaryQlClass() { result = "Class" }
override string getName() {
result = cls.asLeft().getName().getValue()
or
result = cls.asRight().getName()
}
/**
* Gets the characteristic predicate for this class.
*/
CharPred getCharPred() { toQL(result) = cls.asLeft().getChild(_).(QL::ClassMember).getChild(_) }
AstNode getMember(int i) { toQL(result) = cls.asLeft().getChild(i).(QL::ClassMember).getChild(_) }
QLDoc getQLDocFor(AstNode m) {
exists(int i | result = this.getMember(i) and m = this.getMember(i + 1))