-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathType.qll
More file actions
318 lines (235 loc) · 9.1 KB
/
Type.qll
File metadata and controls
318 lines (235 loc) · 9.1 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
/** Provides classes representing types without type arguments. */
private import rust
private import PathResolution
private import TypeMention
private import codeql.rust.internal.CachedStages
private import codeql.rust.elements.internal.generated.Raw
private import codeql.rust.elements.internal.generated.Synth
cached
newtype TType =
TUnit() or
TStruct(Struct s) { Stages::TypeInferenceStage::ref() } or
TEnum(Enum e) or
TTrait(Trait t) or
TArrayType() or // todo: add size?
TRefType() or // todo: add mut?
TTypeParamTypeParameter(TypeParam t) or
TAssociatedTypeTypeParameter(TypeAlias t) { any(TraitItemNode trait).getAnAssocItem() = t } or
TRefTypeParameter() or
TSelfTypeParameter(Trait t)
/**
* A type without type arguments.
*
* Note that this type includes things that, strictly speaking, are not Rust
* types, such as traits and implementation blocks.
*/
abstract class Type extends TType {
/** Gets the struct field `name` belonging to this type, if any. */
pragma[nomagic]
abstract StructField getStructField(string name);
/** Gets the `i`th tuple field belonging to this type, if any. */
pragma[nomagic]
abstract TupleField getTupleField(int i);
/** Gets the `i`th type parameter of this type, if any. */
abstract TypeParameter getTypeParameter(int i);
/** Gets a type parameter of this type. */
final TypeParameter getATypeParameter() { result = this.getTypeParameter(_) }
/** Gets a textual representation of this type. */
abstract string toString();
/** Gets the location of this type. */
abstract Location getLocation();
}
/** The unit type `()`. */
class UnitType extends Type, TUnit {
UnitType() { this = TUnit() }
override StructField getStructField(string name) { none() }
override TupleField getTupleField(int i) { none() }
override TypeParameter getTypeParameter(int i) { none() }
override string toString() { result = "()" }
override Location getLocation() { result instanceof EmptyLocation }
}
abstract private class StructOrEnumType extends Type {
abstract ItemNode asItemNode();
}
/** A struct type. */
class StructType extends StructOrEnumType, TStruct {
private Struct struct;
StructType() { this = TStruct(struct) }
override ItemNode asItemNode() { result = struct }
override StructField getStructField(string name) { result = struct.getStructField(name) }
override TupleField getTupleField(int i) { result = struct.getTupleField(i) }
override TypeParameter getTypeParameter(int i) {
result = TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
}
override string toString() { result = struct.getName().getText() }
override Location getLocation() { result = struct.getLocation() }
}
/** An enum type. */
class EnumType extends StructOrEnumType, TEnum {
private Enum enum;
EnumType() { this = TEnum(enum) }
override ItemNode asItemNode() { result = enum }
override StructField getStructField(string name) { none() }
override TupleField getTupleField(int i) { none() }
override TypeParameter getTypeParameter(int i) {
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
}
override string toString() { result = enum.getName().getText() }
override Location getLocation() { result = enum.getLocation() }
}
/** A trait type. */
class TraitType extends Type, TTrait {
private Trait trait;
TraitType() { this = TTrait(trait) }
override StructField getStructField(string name) { none() }
override TupleField getTupleField(int i) { none() }
override TypeParameter getTypeParameter(int i) {
result = TTypeParamTypeParameter(trait.getGenericParamList().getTypeParam(i))
or
result =
any(AssociatedTypeTypeParameter param | param.getTrait() = trait and param.getIndex() = i)
}
override string toString() { result = trait.toString() }
override Location getLocation() { result = trait.getLocation() }
}
/**
* An array type.
*
* Array types like `[i64; 5]` are modeled as normal generic types
* with a single type argument.
*/
class ArrayType extends Type, TArrayType {
ArrayType() { this = TArrayType() }
override StructField getStructField(string name) { none() }
override TupleField getTupleField(int i) { none() }
override TypeParameter getTypeParameter(int i) {
none() // todo
}
override string toString() { result = "[]" }
override Location getLocation() { result instanceof EmptyLocation }
}
/**
* A reference type.
*
* Reference types like `& i64` are modeled as normal generic types
* with a single type argument.
*/
class RefType extends Type, TRefType {
RefType() { this = TRefType() }
override StructField getStructField(string name) { none() }
override TupleField getTupleField(int i) { none() }
override TypeParameter getTypeParameter(int i) {
result = TRefTypeParameter() and
i = 0
}
override string toString() { result = "&" }
override Location getLocation() { result instanceof EmptyLocation }
}
/** A type parameter. */
abstract class TypeParameter extends Type {
override StructField getStructField(string name) { none() }
override TupleField getTupleField(int i) { none() }
override TypeParameter getTypeParameter(int i) { none() }
}
private class RawTypeParameter = @type_param or @trait or @type_alias;
private predicate id(RawTypeParameter x, RawTypeParameter y) { x = y }
private predicate idOfRaw(RawTypeParameter x, int y) = equivalenceRelation(id/2)(x, y)
int idOfTypeParameterAstNode(AstNode node) { idOfRaw(Synth::convertAstNodeToRaw(node), result) }
/** A type parameter from source code. */
class TypeParamTypeParameter extends TypeParameter, TTypeParamTypeParameter {
private TypeParam typeParam;
TypeParamTypeParameter() { this = TTypeParamTypeParameter(typeParam) }
TypeParam getTypeParam() { result = typeParam }
override string toString() { result = typeParam.toString() }
override Location getLocation() { result = typeParam.getLocation() }
}
/**
* Gets the type alias that is the `i`th type parameter of `trait`. Type aliases
* are numbered consecutively but in arbitrary order, starting from the index
* following the last ordinary type parameter.
*/
predicate traitAliasIndex(Trait trait, int i, TypeAlias typeAlias) {
typeAlias =
rank[i + 1 - trait.getNumberOfGenericParams()](TypeAlias alias |
trait.(TraitItemNode).getADescendant() = alias
|
alias order by idOfTypeParameterAstNode(alias)
)
}
/**
* A type parameter corresponding to an associated type in a trait.
*
* We treat associated type declarations in traits as type parameters. E.g., a
* trait such as
* ```rust
* trait ATrait {
* type AssociatedType;
* // ...
* }
* ```
* is treated as if it was
* ```rust
* trait ATrait<AssociatedType> {
* // ...
* }
* ```
*/
class AssociatedTypeTypeParameter extends TypeParameter, TAssociatedTypeTypeParameter {
private TypeAlias typeAlias;
AssociatedTypeTypeParameter() { this = TAssociatedTypeTypeParameter(typeAlias) }
TypeAlias getTypeAlias() { result = typeAlias }
/** Gets the trait that contains this associated type declaration. */
TraitItemNode getTrait() { result.getAnAssocItem() = typeAlias }
int getIndex() { traitAliasIndex(_, result, typeAlias) }
override string toString() { result = typeAlias.getName().getText() }
override Location getLocation() { result = typeAlias.getLocation() }
}
/** An implicit reference type parameter. */
class RefTypeParameter extends TypeParameter, TRefTypeParameter {
override string toString() { result = "&T" }
override Location getLocation() { result instanceof EmptyLocation }
}
/**
* The implicit `Self` type parameter of a trait, that refers to the
* implementing type of the trait.
*
* The Rust Reference on the implicit `Self` parameter:
* https://doc.rust-lang.org/reference/items/traits.html#r-items.traits.self-param
*/
class SelfTypeParameter extends TypeParameter, TSelfTypeParameter {
private Trait trait;
SelfTypeParameter() { this = TSelfTypeParameter(trait) }
Trait getTrait() { result = trait }
override string toString() { result = "Self [" + trait.toString() + "]" }
override Location getLocation() { result = trait.getLocation() }
}
/**
* A type abstraction. I.e., a place in the program where type variables are
* introduced.
*
* Example:
* ```rust
* impl<A, B> Foo<A, B> { }
* // ^^^^^^ a type abstraction
* ```
*/
abstract class TypeAbstraction extends AstNode {
abstract TypeParameter getATypeParameter();
}
final class ImplTypeAbstraction extends TypeAbstraction, Impl {
override TypeParamTypeParameter getATypeParameter() {
result.getTypeParam() = this.getGenericParamList().getATypeParam()
}
}
final class TraitTypeAbstraction extends TypeAbstraction, Trait {
override TypeParamTypeParameter getATypeParameter() {
result.getTypeParam() = this.getGenericParamList().getATypeParam()
}
}
final class TypeBoundTypeAbstraction extends TypeAbstraction, TypeBound {
override TypeParamTypeParameter getATypeParameter() { none() }
}
final class SelfTypeBoundTypeAbstraction extends TypeAbstraction, Name {
SelfTypeBoundTypeAbstraction() { any(Trait trait).getName() = this }
override TypeParamTypeParameter getATypeParameter() { none() }
}