Skip to content

Commit 141f5f7

Browse files
authored
Merge pull request #308 from github/hvitved/operation-method-call
Make `{Unary,Binary}Operation` a sub class of `MethodCall`
2 parents ca2ff9a + 30d2df5 commit 141f5f7

File tree

10 files changed

+565
-455
lines changed

10 files changed

+565
-455
lines changed

ql/lib/codeql/ruby/ast/Operation.qll

Lines changed: 22 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,38 @@ private import internal.Operation
88
*
99
* This is the QL root class for all operations.
1010
*/
11-
class Operation extends Expr, TOperation {
11+
class Operation extends Expr instanceof OperationImpl {
1212
/** Gets the operator of this operation. */
13-
string getOperator() { none() }
13+
final string getOperator() { result = super.getOperatorImpl() }
1414

1515
/** Gets an operand of this operation. */
16-
Expr getAnOperand() { none() }
16+
final Expr getAnOperand() { result = super.getAnOperandImpl() }
1717

1818
override AstNode getAChild(string pred) {
19-
result = super.getAChild(pred)
19+
result = Expr.super.getAChild(pred)
2020
or
2121
pred = "getAnOperand" and result = this.getAnOperand()
2222
}
2323
}
2424

2525
/** A unary operation. */
26-
class UnaryOperation extends Operation, TUnaryOperation {
26+
class UnaryOperation extends Operation, MethodCall instanceof UnaryOperationImpl {
2727
/** Gets the operand of this unary operation. */
28-
Expr getOperand() { none() }
29-
30-
final override Expr getAnOperand() { result = this.getOperand() }
28+
final Expr getOperand() { result = super.getOperandImpl() }
3129

3230
final override AstNode getAChild(string pred) {
33-
result = super.getAChild(pred)
31+
result = Operation.super.getAChild(pred)
32+
or
33+
result = MethodCall.super.getAChild(pred)
3434
or
3535
pred = "getOperand" and result = this.getOperand()
3636
}
3737

3838
final override string toString() { result = this.getOperator() + " ..." }
3939
}
4040

41-
private class UnaryOperationGenerated extends UnaryOperation, TUnaryOperation {
42-
private Ruby::Unary g;
43-
44-
UnaryOperationGenerated() { g = toGenerated(this) }
45-
46-
final override Expr getOperand() { toGenerated(result) = g.getOperand() }
47-
48-
final override string getOperator() { result = g.getOperator() }
49-
}
50-
5141
/** A unary logical operation. */
52-
class UnaryLogicalOperation extends UnaryOperationGenerated, TUnaryLogicalOperation { }
42+
class UnaryLogicalOperation extends UnaryOperation, TUnaryLogicalOperation { }
5343

5444
/**
5545
* A logical NOT operation, using either `!` or `not`.
@@ -63,7 +53,7 @@ class NotExpr extends UnaryLogicalOperation, TNotExpr {
6353
}
6454

6555
/** A unary arithmetic operation. */
66-
class UnaryArithmeticOperation extends UnaryOperationGenerated, TUnaryArithmeticOperation { }
56+
class UnaryArithmeticOperation extends UnaryOperation, TUnaryArithmeticOperation { }
6757

6858
/**
6959
* A unary plus expression.
@@ -92,10 +82,6 @@ class UnaryMinusExpr extends UnaryArithmeticOperation, TUnaryMinusExpr {
9282
* ```
9383
*/
9484
class SplatExpr extends UnaryOperation, TSplatExpr {
95-
final override Expr getOperand() { result = this.(SplatExprImpl).getOperandImpl() }
96-
97-
final override string getOperator() { result = "*" }
98-
9985
final override string getAPrimaryQlClass() { result = "SplatExpr" }
10086
}
10187

@@ -110,10 +96,6 @@ class HashSplatExpr extends UnaryOperation, THashSplatExpr {
11096

11197
HashSplatExpr() { this = THashSplatExpr(g) }
11298

113-
final override Expr getOperand() { toGenerated(result) = g.getChild() }
114-
115-
final override string getOperator() { result = "**" }
116-
11799
final override string getAPrimaryQlClass() { result = "HashSplatExpr" }
118100
}
119101

@@ -141,44 +123,24 @@ class DefinedExpr extends UnaryOperation, TDefinedExpr {
141123
}
142124

143125
/** A binary operation. */
144-
class BinaryOperation extends Operation, TBinaryOperation {
145-
final override Expr getAnOperand() {
146-
result = this.getLeftOperand() or result = this.getRightOperand()
147-
}
148-
126+
class BinaryOperation extends Operation, MethodCall instanceof BinaryOperationImpl {
149127
final override string toString() { result = "... " + this.getOperator() + " ..." }
150128

151129
override AstNode getAChild(string pred) {
152-
result = super.getAChild(pred)
130+
result = Operation.super.getAChild(pred)
131+
or
132+
result = MethodCall.super.getAChild(pred)
153133
or
154134
pred = "getLeftOperand" and result = this.getLeftOperand()
155135
or
156136
pred = "getRightOperand" and result = this.getRightOperand()
157137
}
158138

159139
/** Gets the left operand of this binary operation. */
160-
Stmt getLeftOperand() { none() }
140+
final Stmt getLeftOperand() { result = super.getLeftOperandImpl() }
161141

162142
/** Gets the right operand of this binary operation. */
163-
Stmt getRightOperand() { none() }
164-
}
165-
166-
private class BinaryOperationReal extends BinaryOperation {
167-
private Ruby::Binary g;
168-
169-
BinaryOperationReal() { g = toGenerated(this) }
170-
171-
final override string getOperator() { result = g.getOperator() }
172-
173-
final override Stmt getLeftOperand() { toGenerated(result) = g.getLeft() }
174-
175-
final override Stmt getRightOperand() { toGenerated(result) = g.getRight() }
176-
}
177-
178-
abstract private class BinaryOperationSynth extends BinaryOperation {
179-
final override Stmt getLeftOperand() { synthChild(this, 0, result) }
180-
181-
final override Stmt getRightOperand() { synthChild(this, 1, result) }
143+
final Stmt getRightOperand() { result = super.getRightOperandImpl() }
182144
}
183145

184146
/**
@@ -196,10 +158,6 @@ class AddExpr extends BinaryArithmeticOperation, TAddExpr {
196158
final override string getAPrimaryQlClass() { result = "AddExpr" }
197159
}
198160

199-
private class AddExprSynth extends AddExpr, BinaryOperationSynth, TAddExprSynth {
200-
final override string getOperator() { result = "+" }
201-
}
202-
203161
/**
204162
* A subtract expression.
205163
* ```rb
@@ -210,10 +168,6 @@ class SubExpr extends BinaryArithmeticOperation, TSubExpr {
210168
final override string getAPrimaryQlClass() { result = "SubExpr" }
211169
}
212170

213-
private class SubExprSynth extends SubExpr, BinaryOperationSynth, TSubExprSynth {
214-
final override string getOperator() { result = "-" }
215-
}
216-
217171
/**
218172
* A multiply expression.
219173
* ```rb
@@ -224,10 +178,6 @@ class MulExpr extends BinaryArithmeticOperation, TMulExpr {
224178
final override string getAPrimaryQlClass() { result = "MulExpr" }
225179
}
226180

227-
private class MulExprSynth extends MulExpr, BinaryOperationSynth, TMulExprSynth {
228-
final override string getOperator() { result = "*" }
229-
}
230-
231181
/**
232182
* A divide expression.
233183
* ```rb
@@ -238,10 +188,6 @@ class DivExpr extends BinaryArithmeticOperation, TDivExpr {
238188
final override string getAPrimaryQlClass() { result = "DivExpr" }
239189
}
240190

241-
private class DivExprSynth extends DivExpr, BinaryOperationSynth, TDivExprSynth {
242-
final override string getOperator() { result = "/" }
243-
}
244-
245191
/**
246192
* A modulo expression.
247193
* ```rb
@@ -252,10 +198,6 @@ class ModuloExpr extends BinaryArithmeticOperation, TModuloExpr {
252198
final override string getAPrimaryQlClass() { result = "ModuloExpr" }
253199
}
254200

255-
private class ModuloExprSynth extends ModuloExpr, BinaryOperationSynth, TModuloExprSynth {
256-
final override string getOperator() { result = "%" }
257-
}
258-
259201
/**
260202
* An exponent expression.
261203
* ```rb
@@ -266,10 +208,6 @@ class ExponentExpr extends BinaryArithmeticOperation, TExponentExpr {
266208
final override string getAPrimaryQlClass() { result = "ExponentExpr" }
267209
}
268210

269-
private class ExponentExprSynth extends ExponentExpr, BinaryOperationSynth, TExponentExprSynth {
270-
final override string getOperator() { result = "**" }
271-
}
272-
273211
/**
274212
* A binary logical operation.
275213
*/
@@ -286,10 +224,6 @@ class LogicalAndExpr extends BinaryLogicalOperation, TLogicalAndExpr {
286224
final override string getAPrimaryQlClass() { result = "LogicalAndExpr" }
287225
}
288226

289-
private class LogicalAndExprSynth extends LogicalAndExpr, BinaryOperationSynth, TLogicalAndExprSynth {
290-
final override string getOperator() { result = "&&" }
291-
}
292-
293227
/**
294228
* A logical OR operation, using either `or` or `||`.
295229
* ```rb
@@ -301,10 +235,6 @@ class LogicalOrExpr extends BinaryLogicalOperation, TLogicalOrExpr {
301235
final override string getAPrimaryQlClass() { result = "LogicalOrExpr" }
302236
}
303237

304-
private class LogicalOrExprSynth extends LogicalOrExpr, BinaryOperationSynth, TLogicalOrExprSynth {
305-
final override string getOperator() { result = "||" }
306-
}
307-
308238
/**
309239
* A binary bitwise operation.
310240
*/
@@ -320,10 +250,6 @@ class LShiftExpr extends BinaryBitwiseOperation, TLShiftExpr {
320250
final override string getAPrimaryQlClass() { result = "LShiftExpr" }
321251
}
322252

323-
private class LShiftExprSynth extends LShiftExpr, BinaryOperationSynth, TLShiftExprSynth {
324-
final override string getOperator() { result = "<<" }
325-
}
326-
327253
/**
328254
* A right-shift operation.
329255
* ```rb
@@ -334,10 +260,6 @@ class RShiftExpr extends BinaryBitwiseOperation, TRShiftExpr {
334260
final override string getAPrimaryQlClass() { result = "RShiftExpr" }
335261
}
336262

337-
private class RShiftExprSynth extends RShiftExpr, BinaryOperationSynth, TRShiftExprSynth {
338-
final override string getOperator() { result = ">>" }
339-
}
340-
341263
/**
342264
* A bitwise AND operation.
343265
* ```rb
@@ -348,10 +270,6 @@ class BitwiseAndExpr extends BinaryBitwiseOperation, TBitwiseAndExpr {
348270
final override string getAPrimaryQlClass() { result = "BitwiseAndExpr" }
349271
}
350272

351-
private class BitwiseAndSynthExpr extends BitwiseAndExpr, BinaryOperationSynth, TBitwiseAndExprSynth {
352-
final override string getOperator() { result = "&" }
353-
}
354-
355273
/**
356274
* A bitwise OR operation.
357275
* ```rb
@@ -362,10 +280,6 @@ class BitwiseOrExpr extends BinaryBitwiseOperation, TBitwiseOrExpr {
362280
final override string getAPrimaryQlClass() { result = "BitwiseOrExpr" }
363281
}
364282

365-
private class BitwiseOrSynthExpr extends BitwiseOrExpr, BinaryOperationSynth, TBitwiseOrExprSynth {
366-
final override string getOperator() { result = "|" }
367-
}
368-
369283
/**
370284
* An XOR (exclusive OR) operation.
371285
* ```rb
@@ -376,10 +290,6 @@ class BitwiseXorExpr extends BinaryBitwiseOperation, TBitwiseXorExpr {
376290
final override string getAPrimaryQlClass() { result = "BitwiseXorExpr" }
377291
}
378292

379-
private class BitwiseXorSynthExpr extends BitwiseXorExpr, BinaryOperationSynth, TBitwiseXorExprSynth {
380-
final override string getOperator() { result = "^" }
381-
}
382-
383293
/**
384294
* A comparison operation. That is, either an equality operation or a
385295
* relational operation.
@@ -531,21 +441,17 @@ class NoRegExpMatchExpr extends BinaryOperation, TNoRegExpMatchExpr {
531441
*
532442
* This is a QL base class for all assignments.
533443
*/
534-
class Assignment extends Operation, TAssignment {
444+
class Assignment extends Operation instanceof AssignmentImpl {
535445
/** Gets the left hand side of this assignment. */
536-
final Pattern getLeftOperand() { result = this.(AssignmentImpl).getLeftOperandImpl() }
446+
final Pattern getLeftOperand() { result = super.getLeftOperandImpl() }
537447

538448
/** Gets the right hand side of this assignment. */
539-
final Expr getRightOperand() { result = this.(AssignmentImpl).getRightOperandImpl() }
540-
541-
final override Expr getAnOperand() {
542-
result = this.getLeftOperand() or result = this.getRightOperand()
543-
}
449+
final Expr getRightOperand() { result = super.getRightOperandImpl() }
544450

545451
final override string toString() { result = "... " + this.getOperator() + " ..." }
546452

547453
override AstNode getAChild(string pred) {
548-
result = super.getAChild(pred)
454+
result = Operation.super.getAChild(pred)
549455
or
550456
pred = "getLeftOperand" and result = getLeftOperand()
551457
or
@@ -560,21 +466,13 @@ class Assignment extends Operation, TAssignment {
560466
* ```
561467
*/
562468
class AssignExpr extends Assignment, TAssignExpr {
563-
final override string getOperator() { result = "=" }
564-
565469
final override string getAPrimaryQlClass() { result = "AssignExpr" }
566470
}
567471

568472
/**
569473
* A binary assignment operation other than `=`.
570474
*/
571-
class AssignOperation extends Assignment, TAssignOperation {
572-
Ruby::OperatorAssignment g;
573-
574-
AssignOperation() { g = toGenerated(this) }
575-
576-
final override string getOperator() { result = g.getOperator() }
577-
}
475+
class AssignOperation extends Assignment instanceof AssignOperationImpl { }
578476

579477
/**
580478
* An arithmetic assignment operation: `+=`, `-=`, `*=`, `/=`, `**=`, and `%=`.

ql/lib/codeql/ruby/ast/internal/AST.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class TCall = TMethodCall or TYieldCall;
527527

528528
class TMethodCall =
529529
TMethodCallSynth or TIdentifierMethodCall or TScopeResolutionMethodCall or TRegularMethodCall or
530-
TElementReference or TSuperCall;
530+
TElementReference or TSuperCall or TUnaryOperation or TBinaryOperation;
531531

532532
class TSuperCall = TTokenSuperCall or TRegularSuperCall;
533533

0 commit comments

Comments
 (0)