@@ -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,26 @@ 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 }
83
76
}
84
77
78
+ /**
79
+ * A variable referenced by the IR for a function.
80
+ *
81
+ * The variable may be a user-declared variable (`IRUserVariable`) or a temporary variable generated
82
+ * by the AST-to-IR translation (`IRTempVariable`).
83
+ */
84
+ final class IRVariable = AbstractIRVariable ;
85
+
85
86
/**
86
87
* A user-declared variable referenced by the IR for a function.
87
88
*/
88
- class IRUserVariable extends IRVariable , TIRUserVariable {
89
+ class IRUserVariable extends AbstractIRVariable , TIRUserVariable {
89
90
Language:: Variable var ;
90
91
Language:: LanguageType type ;
91
92
@@ -114,27 +115,30 @@ class IRUserVariable extends IRVariable, TIRUserVariable {
114
115
* A variable (user-declared or temporary) that is allocated on the stack. This includes all
115
116
* parameters, non-static local variables, and temporary variables.
116
117
*/
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
- }
118
+ abstract private class AbstractIRAutomaticVariable extends AbstractIRVariable { }
119
+
120
+ /**
121
+ * A variable (user-declared or temporary) that is allocated on the stack. This includes all
122
+ * parameters, non-static local variables, and temporary variables.
123
+ */
124
+ final class IRAutomaticVariable = AbstractIRAutomaticVariable ;
127
125
128
126
/**
129
127
* A user-declared variable that is allocated on the stack. This includes all parameters and
130
128
* non-static local variables.
131
129
*/
132
- class IRAutomaticUserVariable extends IRUserVariable , IRAutomaticVariable {
130
+ private class AbstractIRAutomaticUserVariable extends IRUserVariable , AbstractIRAutomaticVariable {
133
131
override Language:: AutomaticVariable var ;
134
132
135
133
final override Language:: AutomaticVariable getVariable ( ) { result = var }
136
134
}
137
135
136
+ /**
137
+ * A user-declared variable that is allocated on the stack. This includes all parameters and
138
+ * non-static local variables.
139
+ */
140
+ final class IRAutomaticUserVariable = AbstractIRAutomaticUserVariable ;
141
+
138
142
/**
139
143
* A user-declared variable that is not allocated on the stack. This includes all global variables,
140
144
* namespace-scope variables, static fields, and static local variables.
@@ -151,16 +155,10 @@ class IRStaticUserVariable extends IRUserVariable {
151
155
* A variable that is not user-declared. This includes temporary variables generated as part of IR
152
156
* construction, as well as string literals.
153
157
*/
154
- class IRGeneratedVariable extends IRVariable {
158
+ abstract private class AbstractIRGeneratedVariable extends AbstractIRVariable {
155
159
Language:: AST ast ;
156
160
Language:: LanguageType type ;
157
161
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
162
final override Language:: LanguageType getLanguageType ( ) { result = type }
165
163
166
164
final override Language:: AST getAst ( ) { result = ast }
@@ -196,12 +194,20 @@ class IRGeneratedVariable extends IRVariable {
196
194
string getBaseString ( ) { none ( ) }
197
195
}
198
196
197
+ /**
198
+ * A variable that is not user-declared. This includes temporary variables generated as part of IR
199
+ * construction, as well as string literals.
200
+ */
201
+ final class IRGeneratedVariable = AbstractIRGeneratedVariable ;
202
+
199
203
/**
200
204
* A temporary variable introduced by IR construction. The most common examples are the variable
201
205
* generated to hold the return value of a function, or the variable generated to hold the result of
202
206
* a condition operator (`a ? b : c`).
203
207
*/
204
- class IRTempVariable extends IRGeneratedVariable , IRAutomaticVariable , TIRTempVariable {
208
+ class IRTempVariable extends AbstractIRGeneratedVariable , AbstractIRAutomaticVariable ,
209
+ TIRTempVariable
210
+ {
205
211
TempVariableTag tag ;
206
212
207
213
IRTempVariable ( ) { this = TIRTempVariable ( func , ast , tag , type ) }
@@ -241,7 +247,7 @@ class IRThrowVariable extends IRTempVariable {
241
247
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
242
248
* function that accepts a variable number of arguments.
243
249
*/
244
- class IREllipsisVariable extends IRTempVariable , IRParameter {
250
+ class IREllipsisVariable extends IRTempVariable , AbstractIRParameter {
245
251
IREllipsisVariable ( ) { tag = EllipsisTempVar ( ) }
246
252
247
253
final override string toString ( ) { result = "#ellipsis" }
@@ -252,7 +258,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
252
258
/**
253
259
* A temporary variable generated to hold the `this` pointer.
254
260
*/
255
- class IRThisVariable extends IRTempVariable , IRParameter {
261
+ class IRThisVariable extends IRTempVariable , AbstractIRParameter {
256
262
IRThisVariable ( ) { tag = ThisTempVar ( ) }
257
263
258
264
final override string toString ( ) { result = "#this" }
@@ -264,7 +270,7 @@ class IRThisVariable extends IRTempVariable, IRParameter {
264
270
* A variable generated to represent the contents of a string literal. This variable acts much like
265
271
* a read-only global variable.
266
272
*/
267
- class IRStringLiteral extends IRGeneratedVariable , TIRStringLiteral {
273
+ class IRStringLiteral extends AbstractIRGeneratedVariable , TIRStringLiteral {
268
274
Language:: StringLiteral literal ;
269
275
270
276
IRStringLiteral ( ) { this = TIRStringLiteral ( func , ast , type , literal ) }
@@ -288,7 +294,7 @@ class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
288
294
* used to model the runtime initialization of static local variables in C++, as well as static
289
295
* fields in C#.
290
296
*/
291
- class IRDynamicInitializationFlag extends IRGeneratedVariable , TIRDynamicInitializationFlag {
297
+ class IRDynamicInitializationFlag extends AbstractIRGeneratedVariable , TIRDynamicInitializationFlag {
292
298
Language:: Variable var ;
293
299
294
300
IRDynamicInitializationFlag ( ) {
@@ -314,24 +320,24 @@ class IRDynamicInitializationFlag extends IRGeneratedVariable, TIRDynamicInitial
314
320
* An IR variable which acts like a function parameter, including positional parameters and the
315
321
* temporary variables generated for `this` and ellipsis parameters.
316
322
*/
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
-
323
+ abstract private class AbstractIRParameter extends AbstractIRAutomaticVariable {
326
324
/**
327
325
* Gets the zero-based index of this parameter. The `this` parameter has index -1.
328
326
*/
329
327
int getIndex ( ) { none ( ) }
330
328
}
331
329
330
+ /**
331
+ * An IR variable which acts like a function parameter, including positional parameters and the
332
+ * temporary variables generated for `this` and ellipsis parameters.
333
+ */
334
+ final class IRParameter = AbstractIRParameter ;
335
+
332
336
/**
333
337
* An IR variable representing a positional parameter.
334
338
*/
335
- class IRPositionalParameter extends IRParameter , IRAutomaticUserVariable {
339
+ class IRPositionalParameter extends AbstractIRParameter , AbstractIRAutomaticUserVariable {
340
+ IRPositionalParameter ( ) { this .getVariable ( ) instanceof Language:: Parameter }
341
+
336
342
final override int getIndex ( ) { result = this .getVariable ( ) .( Language:: Parameter ) .getIndex ( ) }
337
343
}
0 commit comments