@@ -17,18 +17,11 @@ private import Imports::IRType
17
17
* The variable may be a user-declared variable (`IRUserVariable`) or a temporary variable generated
18
18
* by the AST-to-IR translation (`IRTempVariable`).
19
19
*/
20
- class IRVariable extends TIRVariable {
20
+ abstract private class AbstractIRVariable extends TIRVariable {
21
21
Language:: Declaration func ;
22
22
23
- IRVariable ( ) {
24
- this = TIRUserVariable ( _, _, func ) or
25
- this = TIRTempVariable ( func , _, _, _) or
26
- this = TIRStringLiteral ( func , _, _, _) or
27
- this = TIRDynamicInitializationFlag ( func , _, _)
28
- }
29
-
30
23
/** Gets a textual representation of this element. */
31
- string toString ( ) { none ( ) }
24
+ abstract string toString ( ) ;
32
25
33
26
/**
34
27
* Holds if this variable's value cannot be changed within a function. Currently used for string
@@ -49,13 +42,13 @@ class IRVariable extends TIRVariable {
49
42
/**
50
43
* Gets the type of the variable.
51
44
*/
52
- Language:: LanguageType getLanguageType ( ) { none ( ) }
45
+ abstract Language:: LanguageType getLanguageType ( ) ;
53
46
54
47
/**
55
48
* Gets the AST node that declared this variable, or that introduced this
56
49
* variable as part of the AST-to-IR translation.
57
50
*/
58
- Language:: AST getAst ( ) { none ( ) }
51
+ abstract Language:: AST getAst ( ) ;
59
52
60
53
/** DEPRECATED: Alias for getAst */
61
54
deprecated Language:: AST getAST ( ) { result = this .getAst ( ) }
@@ -64,7 +57,7 @@ class IRVariable extends TIRVariable {
64
57
* Gets an identifier string for the variable. This identifier is unique
65
58
* within the function.
66
59
*/
67
- string getUniqueId ( ) { none ( ) }
60
+ abstract string getUniqueId ( ) ;
68
61
69
62
/**
70
63
* Gets the source location of this variable.
@@ -74,18 +67,28 @@ class IRVariable extends TIRVariable {
74
67
/**
75
68
* Gets the IR for the function that references this variable.
76
69
*/
77
- final IRFunction getEnclosingIRFunction ( ) { result .getFunction ( ) = func }
70
+ final IRFunction getEnclosingIRFunction ( ) { result .getFunction ( ) = this . getEnclosingFunction ( ) }
78
71
79
72
/**
80
73
* Gets the function that references this variable.
81
74
*/
82
75
final Language:: Declaration getEnclosingFunction ( ) { result = func }
76
+
77
+ IRBlock getDeclarationBlock ( ) { none ( ) }
83
78
}
84
79
80
+ /**
81
+ * A variable referenced by the IR for a function.
82
+ *
83
+ * The variable may be a user-declared variable (`IRUserVariable`) or a temporary variable generated
84
+ * by the AST-to-IR translation (`IRTempVariable`).
85
+ */
86
+ final class IRVariable = AbstractIRVariable ;
87
+
85
88
/**
86
89
* A user-declared variable referenced by the IR for a function.
87
90
*/
88
- class IRUserVariable extends IRVariable , TIRUserVariable {
91
+ class IRUserVariable extends AbstractIRVariable , TIRUserVariable {
89
92
Language:: Variable var ;
90
93
Language:: LanguageType type ;
91
94
@@ -114,27 +117,30 @@ class IRUserVariable extends IRVariable, TIRUserVariable {
114
117
* A variable (user-declared or temporary) that is allocated on the stack. This includes all
115
118
* parameters, non-static local variables, and temporary variables.
116
119
*/
117
- class IRAutomaticVariable extends IRVariable {
118
- IRAutomaticVariable ( ) {
119
- exists ( Language:: Variable var |
120
- this = TIRUserVariable ( var , _, func ) and
121
- Language:: isVariableAutomatic ( var )
122
- )
123
- or
124
- this = TIRTempVariable ( func , _, _, _)
125
- }
126
- }
120
+ abstract private class AbstractIRAutomaticVariable extends AbstractIRVariable { }
121
+
122
+ /**
123
+ * A variable (user-declared or temporary) that is allocated on the stack. This includes all
124
+ * parameters, non-static local variables, and temporary variables.
125
+ */
126
+ final class IRAutomaticVariable = AbstractIRAutomaticVariable ;
127
127
128
128
/**
129
129
* A user-declared variable that is allocated on the stack. This includes all parameters and
130
130
* non-static local variables.
131
131
*/
132
- class IRAutomaticUserVariable extends IRUserVariable , IRAutomaticVariable {
132
+ private class AbstractIRAutomaticUserVariable extends IRUserVariable , AbstractIRAutomaticVariable {
133
133
override Language:: AutomaticVariable var ;
134
134
135
135
final override Language:: AutomaticVariable getVariable ( ) { result = var }
136
136
}
137
137
138
+ /**
139
+ * A user-declared variable that is allocated on the stack. This includes all parameters and
140
+ * non-static local variables.
141
+ */
142
+ final class IRAutomaticUserVariable = AbstractIRAutomaticUserVariable ;
143
+
138
144
/**
139
145
* A user-declared variable that is not allocated on the stack. This includes all global variables,
140
146
* namespace-scope variables, static fields, and static local variables.
@@ -151,16 +157,10 @@ class IRStaticUserVariable extends IRUserVariable {
151
157
* A variable that is not user-declared. This includes temporary variables generated as part of IR
152
158
* construction, as well as string literals.
153
159
*/
154
- class IRGeneratedVariable extends IRVariable {
160
+ abstract private class AbstractIRGeneratedVariable extends AbstractIRVariable {
155
161
Language:: AST ast ;
156
162
Language:: LanguageType type ;
157
163
158
- IRGeneratedVariable ( ) {
159
- this = TIRTempVariable ( func , ast , _, type ) or
160
- this = TIRStringLiteral ( func , ast , type , _) or
161
- this = TIRDynamicInitializationFlag ( func , ast , type )
162
- }
163
-
164
164
final override Language:: LanguageType getLanguageType ( ) { result = type }
165
165
166
166
final override Language:: AST getAst ( ) { result = ast }
@@ -196,12 +196,20 @@ class IRGeneratedVariable extends IRVariable {
196
196
string getBaseString ( ) { none ( ) }
197
197
}
198
198
199
+ /**
200
+ * A variable that is not user-declared. This includes temporary variables generated as part of IR
201
+ * construction, as well as string literals.
202
+ */
203
+ final class IRGeneratedVariable = AbstractIRGeneratedVariable ;
204
+
199
205
/**
200
206
* A temporary variable introduced by IR construction. The most common examples are the variable
201
207
* generated to hold the return value of a function, or the variable generated to hold the result of
202
208
* a condition operator (`a ? b : c`).
203
209
*/
204
- class IRTempVariable extends IRGeneratedVariable , IRAutomaticVariable , TIRTempVariable {
210
+ class IRTempVariable extends AbstractIRGeneratedVariable , AbstractIRAutomaticVariable ,
211
+ TIRTempVariable
212
+ {
205
213
TempVariableTag tag ;
206
214
207
215
IRTempVariable ( ) { this = TIRTempVariable ( func , ast , tag , type ) }
@@ -241,7 +249,7 @@ class IRThrowVariable extends IRTempVariable {
241
249
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
242
250
* function that accepts a variable number of arguments.
243
251
*/
244
- class IREllipsisVariable extends IRTempVariable , IRParameter {
252
+ class IREllipsisVariable extends IRTempVariable , AbstractIRParameter {
245
253
IREllipsisVariable ( ) { tag = EllipsisTempVar ( ) }
246
254
247
255
final override string toString ( ) { result = "#ellipsis" }
@@ -252,7 +260,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
252
260
/**
253
261
* A temporary variable generated to hold the `this` pointer.
254
262
*/
255
- class IRThisVariable extends IRTempVariable , IRParameter {
263
+ class IRThisVariable extends IRTempVariable , AbstractIRParameter {
256
264
IRThisVariable ( ) { tag = ThisTempVar ( ) }
257
265
258
266
final override string toString ( ) { result = "#this" }
@@ -264,7 +272,7 @@ class IRThisVariable extends IRTempVariable, IRParameter {
264
272
* A variable generated to represent the contents of a string literal. This variable acts much like
265
273
* a read-only global variable.
266
274
*/
267
- class IRStringLiteral extends IRGeneratedVariable , TIRStringLiteral {
275
+ class IRStringLiteral extends AbstractIRGeneratedVariable , TIRStringLiteral {
268
276
Language:: StringLiteral literal ;
269
277
270
278
IRStringLiteral ( ) { this = TIRStringLiteral ( func , ast , type , literal ) }
@@ -288,7 +296,7 @@ class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
288
296
* used to model the runtime initialization of static local variables in C++, as well as static
289
297
* fields in C#.
290
298
*/
291
- class IRDynamicInitializationFlag extends IRGeneratedVariable , TIRDynamicInitializationFlag {
299
+ class IRDynamicInitializationFlag extends AbstractIRGeneratedVariable , TIRDynamicInitializationFlag {
292
300
Language:: Variable var ;
293
301
294
302
IRDynamicInitializationFlag ( ) {
@@ -314,24 +322,24 @@ class IRDynamicInitializationFlag extends IRGeneratedVariable, TIRDynamicInitial
314
322
* An IR variable which acts like a function parameter, including positional parameters and the
315
323
* temporary variables generated for `this` and ellipsis parameters.
316
324
*/
317
- class IRParameter extends IRAutomaticVariable {
318
- IRParameter ( ) {
319
- this .( IRAutomaticUserVariable ) .getVariable ( ) instanceof Language:: Parameter
320
- or
321
- this = TIRTempVariable ( _, _, ThisTempVar ( ) , _)
322
- or
323
- this = TIRTempVariable ( _, _, EllipsisTempVar ( ) , _)
324
- }
325
-
325
+ abstract private class AbstractIRParameter extends AbstractIRAutomaticVariable {
326
326
/**
327
327
* Gets the zero-based index of this parameter. The `this` parameter has index -1.
328
328
*/
329
329
int getIndex ( ) { none ( ) }
330
330
}
331
331
332
+ /**
333
+ * An IR variable which acts like a function parameter, including positional parameters and the
334
+ * temporary variables generated for `this` and ellipsis parameters.
335
+ */
336
+ final class IRParameter = AbstractIRParameter ;
337
+
332
338
/**
333
339
* An IR variable representing a positional parameter.
334
340
*/
335
- class IRPositionalParameter extends IRParameter , IRAutomaticUserVariable {
341
+ class IRPositionalParameter extends AbstractIRParameter , AbstractIRAutomaticUserVariable {
342
+ IRPositionalParameter ( ) { this .getVariable ( ) instanceof Language:: Parameter }
343
+
336
344
final override int getIndex ( ) { result = this .getVariable ( ) .( Language:: Parameter ) .getIndex ( ) }
337
345
}
0 commit comments