|
| 1 | +overlay[local] |
| 2 | +module; |
| 3 | + |
| 4 | +private import minimal.minimal |
| 5 | +private import codeql.util.Boolean |
| 6 | + |
| 7 | +private newtype TConstant = |
| 8 | + TInt(int n) { |
| 9 | + n = [0 .. 10] or |
| 10 | + n = any(Expr e).getIntValue() or |
| 11 | + n = any(Parameter p).getIndex() or |
| 12 | + exists(any(InvokeExpr e).getArgument(n)) |
| 13 | + } or |
| 14 | + TString(string s) { s = any(Expr e).getStringValue() } or |
| 15 | + TBoolean(Boolean b) or |
| 16 | + TNull() or |
| 17 | + TUndefined() |
| 18 | + |
| 19 | +class Constant extends TConstant { |
| 20 | + int asInt() { this = TInt(result) } |
| 21 | + |
| 22 | + string asString() { this = TString(result) } |
| 23 | + |
| 24 | + boolean asBoolean() { this = TBoolean(result) } |
| 25 | + |
| 26 | + predicate isNull() { this = TNull() } |
| 27 | + |
| 28 | + predicate isUndefined() { this = TUndefined() } |
| 29 | + |
| 30 | + string toString() { |
| 31 | + result = this.asInt().toString() |
| 32 | + or |
| 33 | + result = "\"" + this.asString() + "\"" |
| 34 | + or |
| 35 | + result = this.asBoolean().toString() |
| 36 | + or |
| 37 | + this.isNull() and result = "null" |
| 38 | + or |
| 39 | + this.isUndefined() and result = "undefined" |
| 40 | + } |
| 41 | + |
| 42 | + int asArrayIndex() { result = this.asInt() and result >= 0 } |
| 43 | + |
| 44 | + string getAsOperand() { |
| 45 | + result = this.asInt().toString() |
| 46 | + or |
| 47 | + result = "\"" + this.asString() + "\"" |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +module Constant { |
| 52 | + Constant fromInt(int n) { result = TInt(n) } |
| 53 | + |
| 54 | + Constant fromString(string s) { result = TString(s) } |
| 55 | + |
| 56 | + Constant fromBoolean(boolean b) { result = TBoolean(b) } |
| 57 | + |
| 58 | + Constant null() { result = TNull() } |
| 59 | + |
| 60 | + Constant undefined() { result = TUndefined() } |
| 61 | +} |
0 commit comments