|
| 1 | +/** |
| 2 | + * Provides classes and predicates related to jump-to-definition links |
| 3 | + * in the code viewer. |
| 4 | + */ |
| 5 | + |
| 6 | +import csharp |
| 7 | + |
| 8 | +/** An element with an associated definition. */ |
| 9 | +abstract class Use extends @type_mention_parent { |
| 10 | + /** |
| 11 | + * Holds if this element is at the specified location. |
| 12 | + * The location spans column `startcolumn` of line `startline` to |
| 13 | + * column `endcolumn` of line `endline` in file `filepath`. |
| 14 | + * For more information, see |
| 15 | + * [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html). |
| 16 | + */ |
| 17 | + predicate hasLocationInfo( |
| 18 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 19 | + ) { |
| 20 | + exists(Location l | |
| 21 | + l = this.(Element).getLocation() or |
| 22 | + l = this.(TypeMention).getLocation() |
| 23 | + | |
| 24 | + filepath = l.getFile().getAbsolutePath() and |
| 25 | + startline = l.getStartLine() and |
| 26 | + startcolumn = l.getStartColumn() and |
| 27 | + endline = l.getEndLine() and |
| 28 | + endcolumn = l.getEndColumn() |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + /** Gets the definition associated with this element. */ |
| 33 | + abstract Declaration getDefinition(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the type of use. |
| 37 | + * |
| 38 | + * - `"M"`: call. |
| 39 | + * - `"V"`: variable use. |
| 40 | + * - `"T"`: type reference. |
| 41 | + */ |
| 42 | + abstract string getUseType(); |
| 43 | + |
| 44 | + /** Gets a textual representation of this element. */ |
| 45 | + abstract string toString(); |
| 46 | +} |
| 47 | + |
| 48 | +/** A method call/access. */ |
| 49 | +class MethodUse extends Use, QualifiableExpr { |
| 50 | + MethodUse() { |
| 51 | + this instanceof MethodCall or |
| 52 | + this instanceof MethodAccess |
| 53 | + } |
| 54 | + |
| 55 | + /** Gets the qualifier of this method use, if any. */ |
| 56 | + private Expr getFormatQualifier() { |
| 57 | + ( |
| 58 | + if this.getQualifiedDeclaration().(Method).isExtensionMethod() |
| 59 | + then result = this.(MethodCall).getArgument(0) |
| 60 | + else result = this.getQualifier() |
| 61 | + ) and |
| 62 | + not result.isImplicit() |
| 63 | + } |
| 64 | + |
| 65 | + override predicate hasLocationInfo( |
| 66 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 67 | + ) { |
| 68 | + Use.super.hasLocationInfo(filepath, _, _, _, _) and |
| 69 | + endline = startline and |
| 70 | + endcolumn = startcolumn + this.getQualifiedDeclaration().getName().length() - 1 and |
| 71 | + ( |
| 72 | + exists(Location ql | ql = this.getFormatQualifier().getLocation() | |
| 73 | + startline = ql.getEndLine() and |
| 74 | + startcolumn = ql.getEndColumn() + 2 |
| 75 | + ) |
| 76 | + or |
| 77 | + not exists(this.getFormatQualifier()) and |
| 78 | + exists(Location l | l = this.getLocation() | |
| 79 | + startline = l.getStartLine() and |
| 80 | + startcolumn = l.getStartColumn() |
| 81 | + ) |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + override Method getDefinition() { result = getQualifiedDeclaration().getSourceDeclaration() } |
| 86 | + |
| 87 | + override string getUseType() { result = "M" } |
| 88 | + |
| 89 | + override string toString() { result = this.(Expr).toString() } |
| 90 | +} |
| 91 | + |
| 92 | +/** An access. */ |
| 93 | +class AccessUse extends Access, Use { |
| 94 | + AccessUse() { |
| 95 | + not this.getTarget().(Parameter).getCallable() instanceof Accessor and |
| 96 | + not this = any(LocalVariableDeclAndInitExpr d).getLValue() and |
| 97 | + not this.isImplicit() and |
| 98 | + not this instanceof MethodAccess and // handled by `MethodUse` |
| 99 | + not this instanceof TypeAccess and // handled by `TypeMentionUse` |
| 100 | + not this.(FieldAccess).getParent() instanceof Field and // Enum initializer |
| 101 | + not this.(FieldAccess).getParent().getParent() instanceof Field and // Field initializer |
| 102 | + not this.(PropertyAccess).getParent().getParent() instanceof Property // Property initializer |
| 103 | + } |
| 104 | + |
| 105 | + /** Gets the qualifier of this acccess, if any. */ |
| 106 | + private Expr getFormatQualifier() { |
| 107 | + result = this.(QualifiableExpr).getQualifier() and |
| 108 | + not result.isImplicit() |
| 109 | + } |
| 110 | + |
| 111 | + override predicate hasLocationInfo( |
| 112 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 113 | + ) { |
| 114 | + exists(Location ql | ql = this.getFormatQualifier().getLocation() | |
| 115 | + startline = ql.getEndLine() and |
| 116 | + startcolumn = ql.getEndColumn() + 2 and |
| 117 | + Use.super.hasLocationInfo(filepath, _, _, endline, endcolumn) |
| 118 | + ) |
| 119 | + or |
| 120 | + not exists(this.getFormatQualifier()) and |
| 121 | + Use.super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
| 122 | + } |
| 123 | + |
| 124 | + override Declaration getDefinition() { result = this.getTarget().getSourceDeclaration() } |
| 125 | + |
| 126 | + override string getUseType() { |
| 127 | + if this instanceof Call or this instanceof LocalFunctionAccess |
| 128 | + then result = "M" |
| 129 | + else |
| 130 | + if this instanceof BaseAccess or this instanceof ThisAccess |
| 131 | + then result = "T" |
| 132 | + else result = "V" |
| 133 | + } |
| 134 | + |
| 135 | + override string toString() { result = this.(Access).toString() } |
| 136 | +} |
| 137 | + |
| 138 | +/** A type mention. */ |
| 139 | +class TypeMentionUse extends Use, TypeMention { |
| 140 | + TypeMentionUse() { |
| 141 | + // In type mentions such as `T[]`, `T?`, `T*`, and `(S, T)`, we only want |
| 142 | + // uses for the nested type mentions |
| 143 | + forall(TypeMention child, Type t | |
| 144 | + child.getParent() = this and |
| 145 | + t = this.getType() |
| 146 | + | |
| 147 | + not t instanceof ArrayType and |
| 148 | + not t instanceof NullableType and |
| 149 | + not t instanceof PointerType and |
| 150 | + not t instanceof TupleType |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + override Type getDefinition() { result = this.getType().getSourceDeclaration() } |
| 155 | + |
| 156 | + override string getUseType() { |
| 157 | + if this.getTarget() instanceof ObjectCreation |
| 158 | + then result = "M" // constructor call |
| 159 | + else result = "T" |
| 160 | + } |
| 161 | + |
| 162 | + override string toString() { result = TypeMention.super.toString() } |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Gets an element, of kind `kind`, that element `e` uses, if any. |
| 167 | + */ |
| 168 | +cached |
| 169 | +Declaration definitionOf(Use use, string kind) { |
| 170 | + result = use.getDefinition() and |
| 171 | + result.fromSource() and |
| 172 | + kind = use.getUseType() |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Returns an appropriately encoded version of a filename `name` |
| 177 | + * passed by the VS Code extension in order to coincide with the |
| 178 | + * output of `.getFile()` on locatable entities. |
| 179 | + */ |
| 180 | +cached |
| 181 | +File getEncodedFile(string name) { result.getAbsolutePath().replaceAll(":", "_") = name } |
0 commit comments