|
1 | 1 | private import codeql.ruby.AST
|
2 | 2 | private import internal.AST
|
| 3 | +private import internal.Constant |
3 | 4 | private import internal.Module
|
4 | 5 | private import internal.Variable
|
5 | 6 | private import internal.TreeSitter
|
6 | 7 |
|
| 8 | +/** A constant value. */ |
| 9 | +class ConstantValue extends TConstantValue { |
| 10 | + /** Gets a textual representation of this constant value. */ |
| 11 | + final string toString() { |
| 12 | + result = this.getInt().toString() |
| 13 | + or |
| 14 | + result = this.getFloat().toString() |
| 15 | + or |
| 16 | + exists(int numerator, int denominator | |
| 17 | + this.isRational(numerator, denominator) and |
| 18 | + result = numerator + "/" + denominator |
| 19 | + ) |
| 20 | + or |
| 21 | + exists(float real, float imaginary | |
| 22 | + this.isComplex(real, imaginary) and |
| 23 | + result = real + "+" + imaginary + "i" |
| 24 | + ) |
| 25 | + or |
| 26 | + result = this.getString() |
| 27 | + or |
| 28 | + result = ":" + this.getSymbol() |
| 29 | + or |
| 30 | + result = this.getBoolean().toString() |
| 31 | + or |
| 32 | + this.isNil() and result = "nil" |
| 33 | + } |
| 34 | + |
| 35 | + /** Gets the integer value, if this is an integer. */ |
| 36 | + int getInt() { this = TInt(result) } |
| 37 | + |
| 38 | + /** Holds if this is the integer value `i`. */ |
| 39 | + predicate isInt(int i) { i = this.getInt() } |
| 40 | + |
| 41 | + /** Gets the float value, if this is a float. */ |
| 42 | + float getFloat() { this = TFloat(result) } |
| 43 | + |
| 44 | + /** Holds if this is the float value `f`. */ |
| 45 | + predicate isFloat(float f) { f = this.getFloat() } |
| 46 | + |
| 47 | + /** Holds if this is the rational value `numerator / denominator`. */ |
| 48 | + predicate isRational(int numerator, int denominator) { this = TRational(numerator, denominator) } |
| 49 | + |
| 50 | + /** Holds if this is the complex value `real + imaginary * i`. */ |
| 51 | + predicate isComplex(float real, float imaginary) { this = TComplex(real, imaginary) } |
| 52 | + |
| 53 | + /** Gets the string value, if this is a string. */ |
| 54 | + string getString() { this = TString(result) } |
| 55 | + |
| 56 | + /** Holds if this is the string value `s`. */ |
| 57 | + predicate isString(string s) { s = this.getString() } |
| 58 | + |
| 59 | + /** Gets the symbol value (exluding the `:` prefix), if this is a symbol. */ |
| 60 | + string getSymbol() { this = TSymbol(result) } |
| 61 | + |
| 62 | + /** Holds if this is the symbol value `:s`. */ |
| 63 | + predicate isSymbol(string s) { s = this.getSymbol() } |
| 64 | + |
| 65 | + /** Gets the string or symbol value, if any. */ |
| 66 | + string getStringOrSymbol() { result = [this.getString(), this.getSymbol()] } |
| 67 | + |
| 68 | + /** Holds if this is the string value `s` or the symbol value `:s`. */ |
| 69 | + predicate isStringOrSymbol(string s) { s = this.getStringOrSymbol() } |
| 70 | + |
| 71 | + /** Gets the Boolean value, if this is a Boolean. */ |
| 72 | + boolean getBoolean() { this = TBoolean(result) } |
| 73 | + |
| 74 | + /** Holds if this is the Boolean value `b`. */ |
| 75 | + predicate isBoolean(boolean b) { b = this.getBoolean() } |
| 76 | + |
| 77 | + /** Holds if this is the `nil` value. */ |
| 78 | + predicate isNil() { this = TNil() } |
| 79 | +} |
| 80 | + |
| 81 | +/** Provides different sub classes of `ConstantValue`. */ |
| 82 | +module ConstantValue { |
| 83 | + /** A constant integer value. */ |
| 84 | + class ConstantIntegerValue extends ConstantValue, TInt { } |
| 85 | + |
| 86 | + /** A constant float value. */ |
| 87 | + class ConstantFloatValue extends ConstantValue, TFloat { } |
| 88 | + |
| 89 | + /** A constant rational value. */ |
| 90 | + class ConstantRationalValue extends ConstantValue, TRational { } |
| 91 | + |
| 92 | + /** A constant complex value. */ |
| 93 | + class ConstantComplexValue extends ConstantValue, TComplex { } |
| 94 | + |
| 95 | + /** A constant string value. */ |
| 96 | + class ConstantStringValue extends ConstantValue, TString { } |
| 97 | + |
| 98 | + /** A constant symbol value. */ |
| 99 | + class ConstantSymbolValue extends ConstantValue, TSymbol { } |
| 100 | + |
| 101 | + /** A constant Boolean value. */ |
| 102 | + class ConstantBooleanValue extends ConstantValue, TBoolean { } |
| 103 | + |
| 104 | + /** A constant `nil` value. */ |
| 105 | + class ConstantNilValue extends ConstantValue, TNil { } |
| 106 | +} |
| 107 | + |
7 | 108 | /** An access to a constant. */
|
8 | 109 | class ConstantAccess extends Expr, TConstantAccess {
|
9 | 110 | /** Gets the name of the constant being accessed. */
|
@@ -139,7 +240,7 @@ class ConstantReadAccess extends ConstantAccess {
|
139 | 240 | result = lookupConst(resolveConstantReadAccess(this.getScopeExpr()), this.getName())
|
140 | 241 | }
|
141 | 242 |
|
142 |
| - override string getValueText() { result = this.getValue().getValueText() } |
| 243 | + final override ConstantValue getConstantValue() { result = this.getValue().getConstantValue() } |
143 | 244 |
|
144 | 245 | final override string getAPrimaryQlClass() { result = "ConstantReadAccess" }
|
145 | 246 | }
|
|
0 commit comments