@@ -78,6 +78,16 @@ class Value extends TObject {
7878 predicate isBuiltin ( ) {
7979 this .( ObjectInternal ) .isBuiltin ( )
8080 }
81+
82+ /** INTERNAL -- Do not use */
83+ Builtin asBuiltin ( ) {
84+ this = TBuiltinClassObject ( result ) or
85+ this = TBuiltinFunctionObject ( result ) or
86+ this = TBuiltinMethodObject ( result ) or
87+ this = TBuiltinModuleObject ( result ) or
88+ this = TBuiltinOpaqueObject ( result ) or
89+ this = TBuiltinTuple ( result )
90+ }
8191
8292 predicate hasLocationInfo ( string filepath , int bl , int bc , int el , int ec ) {
8393 this .( ObjectInternal ) .getOrigin ( ) .getLocation ( ) .hasLocationInfo ( filepath , bl , bc , el , ec )
@@ -134,6 +144,62 @@ class Value extends TObject {
134144 }
135145}
136146
147+ /** Numeric values (ints and floats).
148+ * Includes those occurring in the source as a literal
149+ * or in a builtin module as a value.
150+ */
151+ class NumericValue extends Value {
152+
153+ NumericValue ( ) {
154+ this .asBuiltin ( ) .getClass ( ) = theIntType ( ) .asBuiltin ( ) or
155+ this .asBuiltin ( ) .getClass ( ) = theLongType ( ) .asBuiltin ( ) or
156+ this .asBuiltin ( ) .getClass ( ) = theFloatType ( ) .asBuiltin ( )
157+ }
158+
159+ /** Gets the Boolean value that this object
160+ * would evaluate to in a Boolean context,
161+ * such as `bool(x)` or `if x: ...`
162+ */
163+ override boolean booleanValue ( ) {
164+ this .intValue ( ) != 0 and result = true
165+ or
166+ this .intValue ( ) = 0 and result = false
167+ or
168+ this .floatValue ( ) != 0 and result = true
169+ or
170+ this .floatValue ( ) = 0 and result = false
171+ }
172+
173+ /** Gets the value of this object if it is a constant integer and it fits in a QL int */
174+ int intValue ( ) {
175+ (
176+ this .asBuiltin ( ) .getClass ( ) = theIntType ( ) .asBuiltin ( ) or
177+ this .asBuiltin ( ) .getClass ( ) = theLongType ( ) .asBuiltin ( )
178+ )
179+ and
180+ result = this .asBuiltin ( ) .getName ( ) .toInt ( )
181+ }
182+
183+ /** Gets the value of this object if it is a constant float */
184+ float floatValue ( ) {
185+ this .asBuiltin ( ) .getClass ( ) = theFloatType ( ) .asBuiltin ( )
186+ and
187+ result = this .asBuiltin ( ) .getName ( ) .toFloat ( )
188+ }
189+
190+ /** Gets the string representation of this object, equivalent to calling repr() in Python */
191+ string repr ( ) {
192+ exists ( string s |
193+ s = this .asBuiltin ( ) .getName ( ) |
194+ if this .asBuiltin ( ) .getClass ( ) = theLongType ( ) .asBuiltin ( ) then
195+ result = s + "L"
196+ else
197+ result = s
198+ )
199+ }
200+
201+ }
202+
137203/** Class representing modules in the Python program
138204 * Each `ModuleValue` represents a module object in the Python program.
139205 */
0 commit comments