Skip to content

Commit 7ca54a6

Browse files
committed
C++: Sync identical files.
1 parent e8cb8b4 commit 7ca54a6

File tree

2 files changed

+110
-94
lines changed

2 files changed

+110
-94
lines changed

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

Lines changed: 55 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,28 @@ 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 }
76+
77+
IRBlock getDeclarationBlock() { none() }
8378
}
8479

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+
8588
/**
8689
* A user-declared variable referenced by the IR for a function.
8790
*/
88-
class IRUserVariable extends IRVariable, TIRUserVariable {
91+
class IRUserVariable extends AbstractIRVariable, TIRUserVariable {
8992
Language::Variable var;
9093
Language::LanguageType type;
9194

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

128128
/**
129129
* A user-declared variable that is allocated on the stack. This includes all parameters and
130130
* non-static local variables.
131131
*/
132-
class IRAutomaticUserVariable extends IRUserVariable, IRAutomaticVariable {
132+
private class AbstractIRAutomaticUserVariable extends IRUserVariable, AbstractIRAutomaticVariable {
133133
override Language::AutomaticVariable var;
134134

135135
final override Language::AutomaticVariable getVariable() { result = var }
136136
}
137137

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+
138144
/**
139145
* A user-declared variable that is not allocated on the stack. This includes all global variables,
140146
* namespace-scope variables, static fields, and static local variables.
@@ -151,16 +157,10 @@ class IRStaticUserVariable extends IRUserVariable {
151157
* A variable that is not user-declared. This includes temporary variables generated as part of IR
152158
* construction, as well as string literals.
153159
*/
154-
class IRGeneratedVariable extends IRVariable {
160+
abstract private class AbstractIRGeneratedVariable extends AbstractIRVariable {
155161
Language::AST ast;
156162
Language::LanguageType type;
157163

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

166166
final override Language::AST getAst() { result = ast }
@@ -196,12 +196,20 @@ class IRGeneratedVariable extends IRVariable {
196196
string getBaseString() { none() }
197197
}
198198

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+
199205
/**
200206
* A temporary variable introduced by IR construction. The most common examples are the variable
201207
* generated to hold the return value of a function, or the variable generated to hold the result of
202208
* a condition operator (`a ? b : c`).
203209
*/
204-
class IRTempVariable extends IRGeneratedVariable, IRAutomaticVariable, TIRTempVariable {
210+
class IRTempVariable extends AbstractIRGeneratedVariable, AbstractIRAutomaticVariable,
211+
TIRTempVariable
212+
{
205213
TempVariableTag tag;
206214

207215
IRTempVariable() { this = TIRTempVariable(func, ast, tag, type) }
@@ -241,7 +249,7 @@ class IRThrowVariable extends IRTempVariable {
241249
* A temporary variable generated to hold the contents of all arguments passed to the `...` of a
242250
* function that accepts a variable number of arguments.
243251
*/
244-
class IREllipsisVariable extends IRTempVariable, IRParameter {
252+
class IREllipsisVariable extends IRTempVariable, AbstractIRParameter {
245253
IREllipsisVariable() { tag = EllipsisTempVar() }
246254

247255
final override string toString() { result = "#ellipsis" }
@@ -252,7 +260,7 @@ class IREllipsisVariable extends IRTempVariable, IRParameter {
252260
/**
253261
* A temporary variable generated to hold the `this` pointer.
254262
*/
255-
class IRThisVariable extends IRTempVariable, IRParameter {
263+
class IRThisVariable extends IRTempVariable, AbstractIRParameter {
256264
IRThisVariable() { tag = ThisTempVar() }
257265

258266
final override string toString() { result = "#this" }
@@ -264,7 +272,7 @@ class IRThisVariable extends IRTempVariable, IRParameter {
264272
* A variable generated to represent the contents of a string literal. This variable acts much like
265273
* a read-only global variable.
266274
*/
267-
class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
275+
class IRStringLiteral extends AbstractIRGeneratedVariable, TIRStringLiteral {
268276
Language::StringLiteral literal;
269277

270278
IRStringLiteral() { this = TIRStringLiteral(func, ast, type, literal) }
@@ -288,7 +296,7 @@ class IRStringLiteral extends IRGeneratedVariable, TIRStringLiteral {
288296
* used to model the runtime initialization of static local variables in C++, as well as static
289297
* fields in C#.
290298
*/
291-
class IRDynamicInitializationFlag extends IRGeneratedVariable, TIRDynamicInitializationFlag {
299+
class IRDynamicInitializationFlag extends AbstractIRGeneratedVariable, TIRDynamicInitializationFlag {
292300
Language::Variable var;
293301

294302
IRDynamicInitializationFlag() {
@@ -314,24 +322,24 @@ class IRDynamicInitializationFlag extends IRGeneratedVariable, TIRDynamicInitial
314322
* An IR variable which acts like a function parameter, including positional parameters and the
315323
* temporary variables generated for `this` and ellipsis parameters.
316324
*/
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 {
326326
/**
327327
* Gets the zero-based index of this parameter. The `this` parameter has index -1.
328328
*/
329329
int getIndex() { none() }
330330
}
331331

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+
332338
/**
333339
* An IR variable representing a positional parameter.
334340
*/
335-
class IRPositionalParameter extends IRParameter, IRAutomaticUserVariable {
341+
class IRPositionalParameter extends AbstractIRParameter, AbstractIRAutomaticUserVariable {
342+
IRPositionalParameter() { this.getVariable() instanceof Language::Parameter }
343+
336344
final override int getIndex() { result = this.getVariable().(Language::Parameter).getIndex() }
337345
}

0 commit comments

Comments
 (0)