Skip to content

Commit 5d5e313

Browse files
authored
Merge pull request #16415 from MathiasVP/cleanup-ir-variables
2 parents 5cde3fa + d5475c4 commit 5d5e313

File tree

3 files changed

+159
-141
lines changed

3 files changed

+159
-141
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRVariable.qll

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,11 @@ private import Imports::IRType
1717
* The variable may be a user-declared variable (`IRUserVariable`) or a temporary variable generated
1818
* by the AST-to-IR translation (`IRTempVariable`).
1919
*/
20-
class IRVariable extends TIRVariable {
20+
abstract private class AbstractIRVariable extends TIRVariable {
2121
Language::Declaration func;
2222

23-
IRVariable() {
24-
this = TIRUserVariable(_, _, func) or
25-
this = TIRTempVariable(func, _, _, _) or
26-
this = TIRStringLiteral(func, _, _, _) or
27-
this = TIRDynamicInitializationFlag(func, _, _)
28-
}
29-
3023
/** Gets a textual representation of this element. */
31-
string toString() { none() }
24+
abstract string toString();
3225

3326
/**
3427
* Holds if this variable's value cannot be changed within a function. Currently used for string
@@ -49,13 +42,13 @@ class IRVariable extends TIRVariable {
4942
/**
5043
* Gets the type of the variable.
5144
*/
52-
Language::LanguageType getLanguageType() { none() }
45+
abstract Language::LanguageType getLanguageType();
5346

5447
/**
5548
* Gets the AST node that declared this variable, or that introduced this
5649
* variable as part of the AST-to-IR translation.
5750
*/
58-
Language::AST getAst() { none() }
51+
abstract Language::AST getAst();
5952

6053
/** DEPRECATED: Alias for getAst */
6154
deprecated Language::AST getAST() { result = this.getAst() }
@@ -64,7 +57,7 @@ class IRVariable extends TIRVariable {
6457
* Gets an identifier string for the variable. This identifier is unique
6558
* within the function.
6659
*/
67-
string getUniqueId() { none() }
60+
abstract string getUniqueId();
6861

6962
/**
7063
* Gets the source location of this variable.
@@ -74,18 +67,26 @@ class IRVariable extends TIRVariable {
7467
/**
7568
* Gets the IR for the function that references this variable.
7669
*/
77-
final IRFunction getEnclosingIRFunction() { result.getFunction() = func }
70+
final IRFunction getEnclosingIRFunction() { result.getFunction() = this.getEnclosingFunction() }
7871

7972
/**
8073
* Gets the function that references this variable.
8174
*/
8275
final Language::Declaration getEnclosingFunction() { result = func }
8376
}
8477

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+
8586
/**
8687
* A user-declared variable referenced by the IR for a function.
8788
*/
88-
class IRUserVariable extends IRVariable, TIRUserVariable {
89+
class IRUserVariable extends AbstractIRVariable, TIRUserVariable {
8990
Language::Variable var;
9091
Language::LanguageType type;
9192

@@ -114,27 +115,30 @@ class IRUserVariable extends IRVariable, TIRUserVariable {
114115
* A variable (user-declared or temporary) that is allocated on the stack. This includes all
115116
* parameters, non-static local variables, and temporary variables.
116117
*/
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;
127125

128126
/**
129127
* A user-declared variable that is allocated on the stack. This includes all parameters and
130128
* non-static local variables.
131129
*/
132-
class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable {
130+
private class AbstractIRAutomaticUserVariable extends IRUserVariable, AbstractIRAutomaticVariable {
133131
override Language::AutomaticVariable var;
134132

135133
final override Language::AutomaticVariable getVariable() { result = var }
136134
}
137135

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+
138142
/**
139143
* A user-declared variable that is not allocated on the stack. This includes all global variables,
140144
* namespace-scope variables, static fields, and static local variables.
@@ -151,16 +155,10 @@ class IRStaticUserVariable extends IRUserVariable {
151155
* A variable that is not user-declared. This includes temporary variables generated as part of IR
152156
* construction, as well as string literals.
153157
*/
154-
class IRGeneratedVariable extends IRVariable {
158+
abstract private class AbstractIRGeneratedVariable extends AbstractIRVariable {
155159
Language::AST ast;
156160
Language::LanguageType type;
157161

158-
IRGeneratedVariable() {
159-
this = TIRTempVariable(func, ast, _, type) or
160-
this = TIRStringLiteral(func, ast, type, _) or
161-
this = TIRDynamicInitializationFlag(func, ast, type)
162-
}
163-
164162
final override Language::LanguageType getLanguageType() { result = type }
165163

166164
final override Language::AST getAst() { result = ast }
@@ -196,12 +194,20 @@ class IRGeneratedVariable extends IRVariable {
196194
string getBaseString() { none() }
197195
}
198196

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+
199203
/**
200204
* A temporary variable introduced by IR construction. The most common examples are the variable
201205
* generated to hold the return value of a function, or the variable generated to hold the result of
202206
* a condition operator (`a ? b : c`).
203207
*/
204-
class IRTempVariable extends IRGeneratedVariable, IRAutomaticVariable, TIRTempVariable {
208+
class IRTempVariable extends AbstractIRGeneratedVariable, AbstractIRAutomaticVariable,
209+
TIRTempVariable
210+
{
205211
TempVariableTag tag;
206212

207213
IRTempVariable() { this = TIRTempVariable(func, ast, tag, type) }
@@ -241,7 +247,7 @@ class IRThrowVariable extends IRTempVariable {
241247
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
242248
* function that accepts a variable number of arguments.
243249
*/
244-
class IREllipsisVariable extends IRTempVariable, IRParameter {
250+
class IREllipsisVariable extends IRTempVariable, AbstractIRParameter {
245251
IREllipsisVariable() { tag = EllipsisTempVar() }
246252

247253
final override string toString() { result = "#ellipsis" }
@@ -252,7 +258,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
252258
/**
253259
* A temporary variable generated to hold the `this` pointer.
254260
*/
255-
class IRThisVariable extends IRTempVariable, IRParameter {
261+
class IRThisVariable extends IRTempVariable, AbstractIRParameter {
256262
IRThisVariable() { tag = ThisTempVar() }
257263

258264
final override string toString() { result = "#this" }
@@ -264,7 +270,7 @@ class IRThisVariable extends IRTempVariable, IRParameter {
264270
* A variable generated to represent the contents of a string literal. This variable acts much like
265271
* a read-only global variable.
266272
*/
267-
class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
273+
class IRStringLiteral extends AbstractIRGeneratedVariable, TIRStringLiteral {
268274
Language::StringLiteral literal;
269275

270276
IRStringLiteral() { this = TIRStringLiteral(func, ast, type, literal) }
@@ -288,7 +294,7 @@ class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
288294
* used to model the runtime initialization of static local variables in C++, as well as static
289295
* fields in C#.
290296
*/
291-
class IRDynamicInitializationFlag extends IRGeneratedVariable, TIRDynamicInitializationFlag {
297+
class IRDynamicInitializationFlag extends AbstractIRGeneratedVariable, TIRDynamicInitializationFlag {
292298
Language::Variable var;
293299

294300
IRDynamicInitializationFlag() {
@@ -314,24 +320,24 @@ class IRDynamicInitializationFlag extends IRGeneratedVariable, TIRDynamicInitial
314320
* An IR variable which acts like a function parameter, including positional parameters and the
315321
* temporary variables generated for `this` and ellipsis parameters.
316322
*/
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 {
326324
/**
327325
* Gets the zero-based index of this parameter. The `this` parameter has index -1.
328326
*/
329327
int getIndex() { none() }
330328
}
331329

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+
332336
/**
333337
* An IR variable representing a positional parameter.
334338
*/
335-
class IRPositionalParameter extends IRParameter, IRAutomaticUserVariable {
339+
class IRPositionalParameter extends AbstractIRParameter, AbstractIRAutomaticUserVariable {
340+
IRPositionalParameter() { this.getVariable() instanceof Language::Parameter }
341+
336342
final override int getIndex() { result = this.getVariable().(Language::Parameter).getIndex() }
337343
}

0 commit comments

Comments
 (0)