forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPathResolution.qll
More file actions
2191 lines (1874 loc) · 65.7 KB
/
PathResolution.qll
File metadata and controls
2191 lines (1874 loc) · 65.7 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 functionality for resolving paths, using the predicate `resolvePath`.
*/
private import rust
private import codeql.rust.elements.internal.generated.ParentChild
private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
private import codeql.rust.internal.CachedStages
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
private import codeql.util.Option
private newtype TNamespace =
TTypeNamespace() or
TValueNamespace() or
TMacroNamespace()
/**
* A namespace.
*
* Either the _value_ namespace, the _type_ namespace, or the _macro_ namespace,
* see https://doc.rust-lang.org/reference/names/namespaces.html.
*/
final class Namespace extends TNamespace {
/** Holds if this is the value namespace. */
predicate isValue() { this = TValueNamespace() }
/** Holds if this is the type namespace. */
predicate isType() { this = TTypeNamespace() }
/** Holds if this is the macro namespace. */
predicate isMacro() { this = TMacroNamespace() }
/** Gets a textual representation of this namespace. */
string toString() {
this.isValue() and result = "value"
or
this.isType() and result = "type"
or
this.isMacro() and result = "macro"
}
}
private newtype TSuccessorKind =
TInternal() or
TExternal() or
TBoth()
/** A successor kind. */
class SuccessorKind extends TSuccessorKind {
predicate isInternal() { this = TInternal() }
predicate isExternal() { this = TExternal() }
predicate isBoth() { this = TBoth() }
predicate isInternalOrBoth() { this.isInternal() or this.isBoth() }
predicate isExternalOrBoth() { this.isExternal() or this.isBoth() }
string toString() {
this.isInternal() and result = "internal"
or
this.isExternal() and result = "external"
or
this.isBoth() and result = "both"
}
}
pragma[nomagic]
private ItemNode getAChildSuccessor(ItemNode item, string name, SuccessorKind kind) {
item = result.getImmediateParent() and
name = result.getName() and
(
// type parameters are only available inside the declaring item
if result instanceof TypeParam
then kind.isInternal()
else
// associated items must always be qualified, also within the declaring
// item (using `Self`)
if item instanceof ImplOrTraitItemNode and result instanceof AssocItem
then kind.isExternal()
else
if result.isPublic()
then kind.isBoth()
else kind.isInternal()
)
}
private module UseOption = Option<Use>;
private class UseOption = UseOption::Option;
/**
* Holds if `n` is superseded by an attribute macro expansion. That is, `n` is
* an item or a transitive child of an item with an attribute macro expansion.
*/
predicate supersededByAttributeMacroExpansion(AstNode n) {
n.(Item).hasAttributeMacroExpansion()
or
exists(AstNode parent |
n.getParentNode() = parent and
supersededByAttributeMacroExpansion(parent) and
// Don't exclude expansions themselves as they supercede other nodes.
not n = parent.(Item).getAttributeMacroExpansion() and
// Don't consider attributes themselves to be superseded. E.g., in `#[a] fn
// f() {}` the macro expansion supercedes `fn f() {}` but not `#[a]`.
not n instanceof Attr
)
}
/**
* An item that may be referred to by a path, and which is a node in
* the _item graph_.
*
* The item graph is a labeled directed graph, where an edge
*
* ```
* item1 --name,kind--> item2
* ```
*
* means that:
*
* - `item2` is available _inside_ the scope of `item1` under the name `name`,
* when `kind` is either `internal` or `both`, and
*
* - `item2` is available _externally_ from `item1` under the name `name`, when
* `kind` is either `external` or `both`.
*
* For example, if we have
*
* ```rust
* pub mod m1 {
* pub mod m2 { }
* }
* ```
*
* then there is an edge `mod m1 --m2,both--> mod m2`.
*
* Associated items are example of externally visible items (inside the
* declaring item they must be `Self` prefixed), while type parameters are
* examples of internally visible items. For example, for
*
* ```rust
* mod m {
* pub trait<T> Trait {
* fn foo(&self) -> T;
* }
* }
* ```
*
* we have the following edges
*
* ```
* mod m --Trait,both--> trait Trait
* trait Trait --foo,external --> fn foo
* trait Trait --T,internal --> T
* ```
*
* Source files are also considered nodes in the item graph, and for
* each source file `f` there is an edge `f --name,both--> item` when `f`
* declares `item` with the name `name`.
*
* For imports like
*
* ```rust
* mod m1 {
* mod m2;
* use m2::foo;
* }
* ```
*
* we first generate an edge `mod m2 --name,kind--> f::item`, where `item` is
* any item (named `name`) inside the imported source file `f`, and `kind` is
* either `external` or `both`. Using this edge, `m2::foo` can resolve to
* `f::foo`, which results in the edge `use m2 --foo,internal--> f::foo`
* (would have been `external` if it was `pub use m2::foo`). Lastly, all edges
* out of `use` nodes are lifted to predecessors in the graph, so we get
* an edge `mod m1 --foo,internal--> f::foo`.
*
*
* References:
* - https://doc.rust-lang.org/reference/items/modules.html
* - https://doc.rust-lang.org/reference/names/scopes.html
* - https://doc.rust-lang.org/reference/paths.html
* - https://doc.rust-lang.org/reference/visibility-and-privacy.html
* - https://doc.rust-lang.org/reference/names/namespaces.html
*/
abstract class ItemNode extends Locatable {
ItemNode() {
// Exclude items that are superseded by the expansion of an attribute macro.
not supersededByAttributeMacroExpansion(this)
}
/** Gets the (original) name of this item. */
abstract string getName();
/** Gets the namespace that this item belongs to, if any. */
abstract Namespace getNamespace();
/** Gets the visibility of this item, if any. */
abstract Visibility getVisibility();
abstract Attr getAnAttr();
pragma[nomagic]
final Attr getAttr(string name) {
result = this.getAnAttr() and
result.getMeta().getPath().(RelevantPath).isUnqualified(name)
}
final predicate hasAttr(string name) { exists(this.getAttr(name)) }
/**
* Holds if this item is public.
*
* This is the case when this item either has `pub` visibility (but is not
* a `use`; a `use` itself is not visible from the outside), or when this
* item is a variant.
*/
predicate isPublic() {
exists(this.getVisibility()) and
not this instanceof Use
or
this instanceof Variant
or
this instanceof MacroItemNode
}
/** Gets the `i`th type parameter of this item, if any. */
abstract TypeParam getTypeParam(int i);
/** Gets an element that has this item as immediately enclosing item. */
pragma[nomagic]
Element getADescendant() {
getImmediateParent(result) = this
or
exists(Element mid |
mid = this.getADescendant() and
getImmediateParent(result) = mid and
not mid instanceof ItemNode
)
}
/** Gets the immediately enclosing item of this item, if any. */
pragma[nomagic]
ItemNode getImmediateParent() { this = result.getADescendant() }
/** Gets the immediately enclosing module (or source file) of this item. */
pragma[nomagic]
ModuleLikeNode getImmediateParentModule() {
this = result.getAnItemInScope()
or
result = this.(SourceFileItemNode).getSuper()
}
/**
* Gets a successor named `name` of the given `kind`, if any.
*
* `useOpt` represents the `use` statement that brought the item into scope,
* if any.
*/
cached
ItemNode getASuccessor(string name, SuccessorKind kind, UseOption useOpt) {
Stages::PathResolutionStage::ref() and
sourceFileEdge(this, name, result) and
kind.isBoth() and
useOpt.isNone()
or
result = getAChildSuccessor(this, name, kind) and
useOpt.isNone()
or
fileImportEdge(this, name, result, kind, useOpt)
or
useImportEdge(this, name, result, kind) and
useOpt.isNone()
or
crateDefEdge(this, name, result, kind, useOpt)
or
crateDependencyEdge(this, name, result) and
kind.isInternal() and
useOpt.isNone()
or
externCrateEdge(this, name, result) and
kind.isInternal() and
useOpt.isNone()
or
macroExportEdge(this, name, result) and
kind.isBoth() and
useOpt.isNone()
or
macroUseEdge(this, name, kind, useOpt, result)
or
// items made available through `use` are available to nodes that contain the `use`
useOpt.asSome() =
any(UseItemNode use_ |
use_ = this.getASuccessor(_, _, _) and
result = use_.getASuccessor(name, kind, _)
)
or
exists(ExternCrateItemNode ec | result = ec.(ItemNode).getASuccessor(name, kind, useOpt) |
ec = this.getASuccessor(_, _, _)
or
// if the extern crate appears in the crate root, then the crate name is also added
// to the 'extern prelude', see https://doc.rust-lang.org/reference/items/extern-crates.html
exists(Crate c |
ec = c.getSourceFile().(ItemNode).getASuccessor(_, _, _) and
this = c.getASourceFile()
)
)
or
// a trait has access to the associated items of its supertraits
this =
any(TraitItemNodeImpl trait |
result = trait.resolveABoundCand().getASuccessor(name, kind, useOpt) and
kind.isExternalOrBoth() and
result instanceof AssocItemNode and
not trait.hasAssocItem(name)
)
or
// items made available by an implementation where `this` is the implementing type
typeImplEdge(this, _, name, kind, result, useOpt)
or
// trait items with default implementations made available in an implementation
exists(ImplItemNodeImpl impl, ItemNode trait |
this = impl and
trait = impl.resolveTraitTyCand() and
result = trait.getASuccessor(name, kind, useOpt) and
result.(AssocItemNode).hasImplementation() and
kind.isExternalOrBoth() and
not impl.hasAssocItem(name)
)
or
// type parameters have access to the associated items of its bounds
result =
this.(TypeParamItemNodeImpl)
.resolveABoundCand()
.getASuccessor(name, kind, useOpt)
.(AssocItemNode) and
kind.isExternalOrBoth()
or
result =
this.(ImplTraitTypeReprItemNodeImpl)
.resolveABoundCand()
.getASuccessor(name, kind, useOpt)
.(AssocItemNode) and
kind.isExternalOrBoth()
or
result = this.(TypeAliasItemNodeImpl).resolveAliasCand().getASuccessor(name, kind, useOpt) and
kind.isExternalOrBoth()
or
name = "super" and
useOpt.isNone() and
(
if this instanceof Module or this instanceof SourceFile
then (
kind.isBoth() and result = this.getImmediateParentModule()
) else (
kind.isInternal() and result = this.getImmediateParentModule().getImmediateParentModule()
)
)
or
name = "self" and
useOpt.isNone() and
(
if
this instanceof Module or
this instanceof Enum or
this instanceof Struct or
this instanceof Crate
then (
kind.isBoth() and
result = this
) else (
kind.isInternal() and
result = this.getImmediateParentModule()
)
)
or
kind.isInternal() and
useOpt.isNone() and
(
preludeEdge(this, name, result)
or
this instanceof SourceFile and
builtin(name, result)
or
name = "Self" and
this = result.(ImplOrTraitItemNode).getAnItemInSelfScope()
or
name = "crate" and
this = result.(CrateItemNode).getASourceFile()
)
}
/** Gets an _external_ successor named `name`, if any. */
pragma[nomagic]
ItemNode getASuccessor(string name) {
exists(SuccessorKind kind |
result = this.getASuccessor(name, kind, _) and
kind.isExternalOrBoth()
)
}
/** Holds if this item has a canonical path belonging to the crate `c`. */
abstract predicate hasCanonicalPath(Crate c);
/** Holds if this node provides a canonical path prefix for `child` in crate `c`. */
pragma[nomagic]
predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
child.getImmediateParent() = this and
this.hasCanonicalPath(c)
}
/** Holds if this node has a canonical path prefix in crate `c`. */
pragma[nomagic]
final predicate hasCanonicalPathPrefix(Crate c) {
any(ItemNode parent).providesCanonicalPathPrefixFor(c, this)
}
/**
* Gets the canonical path of this item, if any.
*
* See [The Rust Reference][1] for more details.
*
* [1]: https://doc.rust-lang.org/reference/paths.html#canonical-paths
*/
cached
abstract string getCanonicalPath(Crate c);
/** Gets the canonical path prefix that this node provides for `child`. */
pragma[nomagic]
string getCanonicalPathPrefixFor(Crate c, ItemNode child) {
this.providesCanonicalPathPrefixFor(c, child) and
result = this.getCanonicalPath(c)
}
/** Gets the canonical path prefix of this node, if any. */
pragma[nomagic]
final string getCanonicalPathPrefix(Crate c) {
result = any(ItemNode parent).getCanonicalPathPrefixFor(c, this)
}
/** Gets the location of this item. */
Location getLocation() { result = super.getLocation() }
}
abstract class TypeItemNode extends ItemNode { }
/** A module or a source file. */
abstract private class ModuleLikeNode extends ItemNode {
/** Gets an item that may refer directly to items defined in this module. */
pragma[nomagic]
ItemNode getAnItemInScope() {
result.getImmediateParent() = this
or
exists(ItemNode mid |
mid = this.getAnItemInScope() and
result.getImmediateParent() = mid and
not mid instanceof ModuleLikeNode
)
}
}
private class SourceFileItemNode extends ModuleLikeNode instanceof SourceFile {
pragma[nomagic]
ModuleLikeNode getSuper() { fileImport(result.getAnItemInScope(), this) }
override string getName() { result = "(source file)" }
override Namespace getNamespace() {
result.isType() // can be referenced with `super`
}
override Visibility getVisibility() { none() }
override Attr getAnAttr() { result = SourceFile.super.getAnAttr() }
override TypeParam getTypeParam(int i) { none() }
override predicate hasCanonicalPath(Crate c) { none() }
override string getCanonicalPath(Crate c) { none() }
}
class CrateItemNode extends ItemNode instanceof Crate {
/**
* Gets the source file that defines this crate.
*/
pragma[nomagic]
SourceFileItemNode getSourceFile() { result = super.getSourceFile() }
/**
* Gets a source file that belongs to this crate.
*
* This is calculated as those source files that can be reached from the entry
* file of this crate using zero or more `mod` imports, without going through
* the entry point of some other crate.
*/
pragma[nomagic]
SourceFileItemNode getASourceFile() {
result = super.getSourceFile()
or
exists(SourceFileItemNode mid, Module mod |
mid = this.getASourceFile() and
mod.getFile() = mid.getFile() and
fileImport(mod, result) and
not result = any(Crate other).getSourceFile()
)
}
override string getName() { result = Crate.super.getName() }
override Namespace getNamespace() {
result.isType() // can be referenced with `crate`
}
override Visibility getVisibility() { none() }
override Attr getAnAttr() { none() }
override TypeParam getTypeParam(int i) { none() }
override predicate hasCanonicalPath(Crate c) { c = this }
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
this.hasCanonicalPath(c) and
exists(SourceFileItemNode file |
child.getImmediateParent() = file and
not file = child.(SourceFileItemNode).getSuper() and
file = super.getSourceFile()
)
or
c = this and
this.getName() = "core" and
child instanceof Builtins::BuiltinType
}
override string getCanonicalPath(Crate c) { c = this and result = Crate.super.getName() }
}
class ExternCrateItemNode extends ItemNode instanceof ExternCrate {
override string getName() {
result = super.getRename().getName().getText()
or
not super.hasRename() and
result = super.getIdentifier().getText()
}
override Namespace getNamespace() { none() }
override Visibility getVisibility() { none() }
override Attr getAnAttr() { result = ExternCrate.super.getAnAttr() }
override TypeParam getTypeParam(int i) { none() }
override predicate hasCanonicalPath(Crate c) { none() }
override string getCanonicalPath(Crate c) { none() }
}
/** An item that can occur in a trait or an `impl` block. */
abstract private class AssocItemNode extends ItemNode instanceof AssocItem {
/** Holds if this associated item has an implementation. */
abstract predicate hasImplementation();
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = this.getCanonicalPathPrefix(c)
or
i = 1 and
result = "::"
or
i = 2 and
result = this.getName()
}
language[monotonicAggregates]
override string getCanonicalPath(Crate c) {
this.hasCanonicalPath(c) and
result = strictconcat(int i | i in [0 .. 2] | this.getCanonicalPathPart(c, i) order by i)
}
}
private class ConstItemNode extends AssocItemNode instanceof Const {
override string getName() { result = Const.super.getName().getText() }
override predicate hasImplementation() { Const.super.hasImplementation() }
override Namespace getNamespace() { result.isValue() }
override Visibility getVisibility() { result = Const.super.getVisibility() }
override Attr getAnAttr() { result = Const.super.getAnAttr() }
override TypeParam getTypeParam(int i) { none() }
}
private class EnumItemNode extends TypeItemNode instanceof Enum {
override string getName() { result = Enum.super.getName().getText() }
override Namespace getNamespace() { result.isType() }
override Visibility getVisibility() { result = Enum.super.getVisibility() }
override Attr getAnAttr() { result = Enum.super.getAnAttr() }
override TypeParam getTypeParam(int i) { result = super.getGenericParamList().getTypeParam(i) }
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = this.getCanonicalPathPrefix(c)
or
i = 1 and
result = "::"
or
i = 2 and
result = this.getName()
}
language[monotonicAggregates]
override string getCanonicalPath(Crate c) {
this.hasCanonicalPath(c) and
result = strictconcat(int i | i in [0 .. 2] | this.getCanonicalPathPart(c, i) order by i)
}
}
/** An item that can be referenced with arguments. */
abstract class ParameterizableItemNode extends ItemNode {
/** Gets the arity this item. */
abstract int getArity();
}
private class VariantItemNode extends ParameterizableItemNode instanceof Variant {
override string getName() { result = Variant.super.getName().getText() }
override Namespace getNamespace() {
if super.getFieldList() instanceof StructFieldList then result.isType() else result.isValue()
}
override TypeParam getTypeParam(int i) {
result = super.getEnum().getGenericParamList().getTypeParam(i)
}
override Visibility getVisibility() { result = super.getEnum().getVisibility() }
override Attr getAnAttr() { result = Variant.super.getAnAttr() }
override int getArity() { result = super.getFieldList().(TupleFieldList).getNumberOfFields() }
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = this.getCanonicalPathPrefix(c)
or
i = 1 and
result = "::"
or
i = 2 and
result = this.getName()
}
language[monotonicAggregates]
override string getCanonicalPath(Crate c) {
this.hasCanonicalPath(c) and
result = strictconcat(int i | i in [0 .. 2] | this.getCanonicalPathPart(c, i) order by i)
}
}
class FunctionItemNode extends AssocItemNode, ParameterizableItemNode instanceof Function {
override string getName() { result = Function.super.getName().getText() }
override predicate hasImplementation() { Function.super.hasImplementation() }
override Namespace getNamespace() {
// see https://doc.rust-lang.org/reference/procedural-macros.html
if this.hasAttr(["proc_macro", "proc_macro_attribute", "proc_macro_derive"])
then result.isMacro()
else result.isValue()
}
override TypeParam getTypeParam(int i) { result = super.getGenericParamList().getTypeParam(i) }
override Visibility getVisibility() { result = Function.super.getVisibility() }
override Attr getAnAttr() { result = Function.super.getAnAttr() }
override int getArity() { result = super.getNumberOfParamsInclSelf() }
}
abstract class ImplOrTraitItemNode extends ItemNode {
/** Gets an item that may refer to this node using `Self`. */
pragma[nomagic]
ItemNode getAnItemInSelfScope() {
result = this
or
result.getImmediateParent() = this
or
exists(ItemNode mid |
mid = this.getAnItemInSelfScope() and
result.getImmediateParent() = mid and
not mid instanceof ImplOrTraitItemNode
)
}
/** Gets a `Self` path that refers to this item. */
cached
Path getASelfPath() {
Stages::PathResolutionStage::ref() and
isUnqualifiedSelfPath(result) and
this = unqualifiedPathLookup(result, _, _)
}
/** Gets an associated item belonging to this trait or `impl` block. */
abstract AssocItemNode getAnAssocItem();
/** Gets the associated item named `name` belonging to this trait or `impl` block. */
pragma[nomagic]
AssocItemNode getAssocItem(string name) {
result = this.getAnAssocItem() and
result.getName() = name
}
/** Holds if this trait or `impl` block declares an associated item named `name`. */
pragma[nomagic]
predicate hasAssocItem(string name) { name = this.getAnAssocItem().getName() }
}
final class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
Path getSelfPath() { result = super.getSelfTy().(PathTypeRepr).getPath() }
Path getTraitPath() { result = super.getTrait().(PathTypeRepr).getPath() }
TypeItemNode resolveSelfTy() { result = resolvePath(this.getSelfPath()) }
TraitItemNode resolveTraitTy() { result = resolvePath(this.getTraitPath()) }
override AssocItemNode getAnAssocItem() { result = this.getADescendant() }
override string getName() { result = "(impl)" }
override Namespace getNamespace() {
result.isType() // can be referenced with `Self`
}
override TypeParam getTypeParam(int i) { result = super.getGenericParamList().getTypeParam(i) }
override Visibility getVisibility() { result = Impl.super.getVisibility() }
override Attr getAnAttr() { result = Impl.super.getAnAttr() }
TypeParamItemNode getBlanketImplementationTypeParam() { result = this.resolveSelfTy() }
/**
* Holds if this impl block is a [blanket implementation][1]. That is, the
* implementation targets a generic parameter of the impl block.
*
* [1]: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods
*/
predicate isBlanketImplementation() { exists(this.getBlanketImplementationTypeParam()) }
override predicate hasCanonicalPath(Crate c) { this.resolveSelfTy().hasCanonicalPathPrefix(c) }
/**
* Holds if `(c1, c2)` forms a pair of crates for the type and trait
* being implemented, for which a canonical path can be computed.
*
* This is the case when either the type and the trait belong to the
* same crate, or when they belong to different crates where one depends
* on the other.
*/
pragma[nomagic]
private predicate selfTraitCratePair(Crate c1, Crate c2) {
this.hasCanonicalPath(pragma[only_bind_into](c1)) and
exists(TraitItemNode trait |
trait = this.resolveTraitTy() and
trait.hasCanonicalPath(c2) and
if this.hasCanonicalPath(c2)
then c1 = c2
else (
c2 = c1.getADependency+() or c1 = c2.getADependency+()
)
)
}
pragma[nomagic]
private string getTraitCanonicalPath(Crate c) {
result = this.resolveTraitTy().getCanonicalPath(c)
}
pragma[nomagic]
private string getSelfCanonicalPath(Crate c) { result = this.resolveSelfTy().getCanonicalPath(c) }
pragma[nomagic]
private string getCanonicalPathTraitPart(Crate c) {
exists(Crate c2 |
this.selfTraitCratePair(c, c2) and
result = this.getTraitCanonicalPath(c2)
)
}
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = "<"
or
i = 1 and
result = this.getSelfCanonicalPath(c)
or
if exists(this.getTraitPath())
then
i = 2 and
result = " as "
or
i = 3 and
result = this.getCanonicalPathTraitPart(c)
or
i = 4 and
result = ">"
else (
i = 2 and
result = ">"
)
}
language[monotonicAggregates]
override string getCanonicalPath(Crate c) {
this.hasCanonicalPath(c) and
exists(int m | if exists(this.getTraitPath()) then m = 4 else m = 2 |
result = strictconcat(int i | i in [0 .. m] | this.getCanonicalPathPart(c, i) order by i)
)
}
}
final class ImplTraitTypeReprItemNode extends TypeItemNode instanceof ImplTraitTypeRepr {
pragma[nomagic]
Path getABoundPath() {
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath()
}
pragma[nomagic]
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
override string getName() { result = "(impl trait)" }
override Namespace getNamespace() { result.isType() }
override Visibility getVisibility() { none() }
override Attr getAnAttr() { none() }
override TypeParam getTypeParam(int i) { none() }
override predicate hasCanonicalPath(Crate c) { none() }
override string getCanonicalPath(Crate c) { none() }
}
private class ImplTraitTypeReprItemNodeImpl extends ImplTraitTypeReprItemNode {
pragma[nomagic]
ItemNode resolveABoundCand() { result = resolvePathCand(this.getABoundPath()) }
}
private class ModuleItemNode extends ModuleLikeNode instanceof Module {
override string getName() { result = Module.super.getName().getText() }
override Namespace getNamespace() { result.isType() }
override Visibility getVisibility() { result = Module.super.getVisibility() }
override Attr getAnAttr() { result = Module.super.getAnAttr() }
override TypeParam getTypeParam(int i) { none() }
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
this.hasCanonicalPath(c) and
(
exists(SourceFile f |
fileImport(this, f) and
sourceFileEdge(f, _, child)
)
or
this = child.getImmediateParent()
)
}
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = this.getCanonicalPathPrefix(c)
or
i = 1 and
result = "::"
or
i = 2 and
result = this.getName()
}
language[monotonicAggregates]
override string getCanonicalPath(Crate c) {
this.hasCanonicalPath(c) and
result = strictconcat(int i | i in [0 .. 2] | this.getCanonicalPathPart(c, i) order by i)
}
}
private class ImplItemNodeImpl extends ImplItemNode {
TypeItemNode resolveSelfTyCand() { result = resolvePathCand(this.getSelfPath()) }
TraitItemNode resolveTraitTyCand() { result = resolvePathCand(this.getTraitPath()) }
}
private class StructItemNode extends TypeItemNode, ParameterizableItemNode instanceof Struct {
override string getName() { result = Struct.super.getName().getText() }
override Namespace getNamespace() {
result.isType() // the struct itself
or
not super.getFieldList() instanceof StructFieldList and
result.isValue() // the constructor
}
override Visibility getVisibility() { result = Struct.super.getVisibility() }
override Attr getAnAttr() { result = Struct.super.getAnAttr() }
override int getArity() { result = super.getFieldList().(TupleFieldList).getNumberOfFields() }
override TypeParam getTypeParam(int i) { result = super.getGenericParamList().getTypeParam(i) }
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = this.getCanonicalPathPrefix(c)
or
i = 1 and
result = "::"
or
i = 2 and
result = this.getName()
}
language[monotonicAggregates]
override string getCanonicalPath(Crate c) {
this.hasCanonicalPath(c) and
result = strictconcat(int i | i in [0 .. 2] | this.getCanonicalPathPart(c, i) order by i)
}
}
final class TraitItemNode extends ImplOrTraitItemNode, TypeItemNode instanceof Trait {
pragma[nomagic]
Path getABoundPath() { result = super.getATypeBound().getTypeRepr().(PathTypeRepr).getPath() }
pragma[nomagic]
ItemNode resolveBound(Path path) { path = this.getABoundPath() and result = resolvePath(path) }
ItemNode resolveABound() { result = this.resolveBound(_) }
override AssocItemNode getAnAssocItem() { result = this.getADescendant() }
override string getName() { result = Trait.super.getName().getText() }
override Namespace getNamespace() { result.isType() }
override Visibility getVisibility() { result = Trait.super.getVisibility() }
override Attr getAnAttr() { result = Trait.super.getAnAttr() }
override TypeParam getTypeParam(int i) { result = super.getGenericParamList().getTypeParam(i) }
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
override predicate providesCanonicalPathPrefixFor(Crate c, ItemNode child) {
this.hasCanonicalPath(c) and
child = this.getAnAssocItem()
}
bindingset[c]
private string getCanonicalPathPart(Crate c, int i) {
i = 0 and
result = "<_ as "
or
i = 1 and
result = this.getCanonicalPathPrefix(c)
or
i = 2 and
result = "::"
or
i = 3 and