Python points-to: Convert two pairs of predicates to methods on booleans.

This commit is contained in:
Mark Shannon
2019-03-20 17:36:27 +00:00
parent 39b9723054
commit ec151e9b02
7 changed files with 41 additions and 193 deletions

View File

@@ -19,9 +19,7 @@ abstract class CallableObjectInternal extends ObjectInternal {
none()
}
override predicate isClass() { none() }
override predicate notClass() { any() }
override boolean isClass() { result = false }
/** The boolean value of this object, if it has one */
override boolean booleanValue() {
@@ -60,13 +58,7 @@ class PythonFunctionObjectInternal extends CallableObjectInternal, TPythonFuncti
result = TBuiltinClassObject(Builtin::special("FunctionType"))
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override boolean isComparable() { result = true }
override Builtin getBuiltin() {
none()
@@ -113,13 +105,7 @@ class BuiltinFunctionObjectInternal extends CallableObjectInternal, TBuiltinFunc
result = TBuiltinClassObject(this.getBuiltin().getClass())
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override boolean isComparable() { result = true }
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
// TO DO .. Result should be be a unknown value of a known class if the return type is known or just an unknown.
@@ -159,13 +145,7 @@ class BuiltinMethodObjectInternal extends CallableObjectInternal, TBuiltinMethod
none()
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override boolean isComparable() { result = true }
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
// TO DO .. Result should be be a unknown value of a known class if the return type is known or just an unknown.
@@ -212,13 +192,7 @@ class BoundMethodObjectInternal extends CallableObjectInternal, TBoundMethod {
this = TBoundMethod(node, _, _, context)
}
override predicate isComparable() {
none()
}
override predicate notComparable() {
any()
}
override boolean isComparable() { result = false }
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
this.getFunction().callResult(callee, obj, origin)

View File

@@ -17,9 +17,7 @@ abstract class ClassObjectInternal extends ObjectInternal {
override predicate maybe() { none() }
override predicate isClass() { any() }
override predicate notClass() { none() }
override boolean isClass() { result = true }
override int intValue() {
none()
@@ -93,13 +91,7 @@ class PythonClassObjectInternal extends ClassObjectInternal, TPythonClassObject
none()
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override boolean isComparable() { result = true }
}
@@ -142,13 +134,7 @@ class BuiltinClassObjectInternal extends ClassObjectInternal, TBuiltinClassObjec
none()
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override boolean isComparable() { result = true }
}
@@ -171,13 +157,7 @@ class UnknownClassInternal extends ClassObjectInternal, TUnknownClass {
none()
}
override predicate isComparable() {
none()
}
override predicate notComparable() {
any()
}
override boolean isComparable() { result = false }
override Builtin getBuiltin() {
none()

View File

@@ -19,22 +19,15 @@ abstract class BooleanObjectInternal extends ObjectInternal {
none()
}
override predicate isClass() { none() }
override boolean isClass() { result = false }
override boolean isComparable() { result = true }
override predicate notClass() { any() }
override ObjectInternal getClass() {
result = TBuiltinClassObject(Builtin::special("bool"))
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Builtin getBuiltin() {
none()
}
@@ -118,9 +111,10 @@ class NoneObjectInternal extends ObjectInternal, TNone {
none()
}
override predicate isClass() { none() }
override boolean isClass() { result = false }
override boolean isComparable() { result = true }
override predicate notClass() { any() }
override ObjectInternal getClass() {
result = TBuiltinClassObject(Builtin::special("NoneType"))
@@ -130,14 +124,6 @@ class NoneObjectInternal extends ObjectInternal, TNone {
node.(NameNode).getId() = "None" and context.appliesTo(node)
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Builtin getBuiltin() {
none()
}
@@ -183,21 +169,14 @@ class IntObjectInternal extends ObjectInternal, TInt {
none()
}
override predicate isClass() { none() }
override boolean isClass() { result = false }
override predicate notClass() { any() }
override boolean isComparable() { result = true }
override ObjectInternal getClass() {
result = TBuiltinClassObject(Builtin::special("int"))
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Builtin getBuiltin() {
none()
@@ -250,22 +229,14 @@ class StringObjectInternal extends ObjectInternal, TString {
none()
}
override predicate isClass() { none() }
override boolean isClass() { result = false }
override predicate notClass() { any() }
override boolean isComparable() { result = true }
override ObjectInternal getClass() {
result = TBuiltinClassObject(Builtin::special("str"))
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Builtin getBuiltin() {
none()
}

View File

@@ -34,31 +34,14 @@ class InstanceInternal extends TInstance, ObjectInternal {
none()
}
override predicate isClass() {
none()
}
override boolean isClass() { result = false }
override predicate notClass() {
any()
}
override boolean isComparable() { result = false }
override ObjectInternal getClass() {
this = TInstance(_, result, _)
}
/** Holds if whatever this "object" represents can be meaningfully analysed for
* truth or false in comparisons. For example, `None` or `int` can be, but `int()`
* or an unknown string cannot.
*/
override predicate isComparable() {
none()
}
/** The negation of `isComparable()` */
override predicate notComparable() {
any()
}
/** Gets the `Builtin` for this object, if any.
* All objects (except unknown and undefined values) should return
* exactly one result for either this method or `getOrigin()`.

View File

@@ -21,6 +21,10 @@ abstract class ModuleObjectInternal extends ObjectInternal {
result = this.getSourceModule().getEntryNode()
}
override boolean isClass() { result = false }
override boolean isComparable() { result = true }
}
class BuiltinModuleObjectInternal extends ModuleObjectInternal, TBuiltinModuleObject {
@@ -51,22 +55,10 @@ class BuiltinModuleObjectInternal extends ModuleObjectInternal, TBuiltinModuleOb
none()
}
override predicate isClass() { none() }
override predicate notClass() { any() }
override ObjectInternal getClass() {
result = TBuiltinClassObject(this.getBuiltin().getClass())
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Module getSourceModule() {
none()
}
@@ -117,22 +109,10 @@ class PackageObjectInternal extends ModuleObjectInternal, TPackageObject {
none()
}
override predicate isClass() { none() }
override predicate notClass() { any() }
override ObjectInternal getClass() {
result = TBuiltinClassObject(this.getBuiltin().getClass())
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Module getSourceModule() {
result.getFile() = this.getFolder().getFile("__init__.py")
}
@@ -201,22 +181,10 @@ class PythonModuleObjectInternal extends ModuleObjectInternal, TPythonModule {
none()
}
override predicate isClass() { none() }
override predicate notClass() { any() }
override ObjectInternal getClass() {
result = TBuiltinClassObject(this.getBuiltin().getClass())
}
override predicate isComparable() {
any()
}
override predicate notComparable() {
none()
}
override Module getSourceModule() {
this = TPythonModule(result)
}

View File

@@ -25,20 +25,16 @@ class ObjectInternal extends TObject {
/** Gets the class declaration for this object, if it is a declared class. */
abstract ClassDecl getClassDeclaration();
abstract predicate isClass();
abstract predicate notClass();
/** True if this "object" is a class. */
abstract boolean isClass();
abstract ObjectInternal getClass();
/** Holds if whatever this "object" represents can be meaningfully analysed for
/** True if this "object" can be meaningfully analysed for
* truth or false in comparisons. For example, `None` or `int` can be, but `int()`
* or an unknown string cannot.
*/
abstract predicate isComparable();
/** The negation of `isComparable()` */
abstract predicate notComparable();
abstract boolean isComparable();
/** Gets the `Builtin` for this object, if any.
* All objects (except unknown and undefined values) should return
@@ -105,9 +101,7 @@ class BuiltinOpaqueObjectInternal extends ObjectInternal, TBuiltinOpaqueObject {
none()
}
override predicate isClass() { none() }
override predicate notClass() { any() }
override boolean isClass() { result = false }
override ObjectInternal getClass() {
result = TBuiltinClassObject(this.getBuiltin().getClass())
@@ -117,13 +111,7 @@ class BuiltinOpaqueObjectInternal extends ObjectInternal, TBuiltinOpaqueObject {
none()
}
override predicate isComparable() {
none()
}
override predicate notComparable() {
any()
}
override boolean isComparable() { result = false }
override predicate callResult(PointsToContext2 callee, ObjectInternal obj, CfgOrigin origin) {
obj = ObjectInternal::unknown() and origin = CfgOrigin::unknown() and
@@ -165,9 +153,7 @@ class UnknownInternal extends ObjectInternal, TUnknown {
none()
}
override predicate isClass() { none() }
override predicate notClass() { any() }
override boolean isClass() { result = false }
override ObjectInternal getClass() {
result = TUnknownClass()
@@ -177,13 +163,7 @@ class UnknownInternal extends ObjectInternal, TUnknown {
none()
}
override predicate isComparable() {
none()
}
override predicate notComparable() {
any()
}
override boolean isComparable() { result = false }
override Builtin getBuiltin() {
none()
@@ -228,9 +208,9 @@ class UndefinedInternal extends ObjectInternal, TUndefined {
none()
}
override predicate isClass() { none() }
override boolean isClass() { result = false }
override predicate notClass() { any() }
override boolean isComparable() { result = false }
override ObjectInternal getClass() {
none()
@@ -240,14 +220,6 @@ class UndefinedInternal extends ObjectInternal, TUndefined {
none()
}
override predicate isComparable() {
none()
}
override predicate notComparable() {
any()
}
override Builtin getBuiltin() {
none()
}

View File

@@ -390,14 +390,14 @@ module PointsTo2 {
equality_test(cmp, a, is, b) and
points_to(a, context, o1, _) and
points_to(b, context, o2, _) |
(o1.isComparable() and o2.isComparable()) and
(o1.isComparable() = true and o2.isComparable() = true) and
(
o1 = o2 and value = ObjectInternal::bool(is)
or
o1 != o2 and value = ObjectInternal::bool(is.booleanNot())
)
or
(o1.notComparable() or o2.notComparable()) and
(o1.isComparable() = false or o2.isComparable() = false) and
value = ObjectInternal::bool(_)
)
or
@@ -777,16 +777,16 @@ module Conditionals {
exists(ObjectInternal other |
PointsTo2::points_to(use, context, val, origin) and
PointsTo2::points_to(r, context, other, _) |
val.isComparable() and other.isComparable() and
val.isComparable() = true and other.isComparable() = true and
(
other = val and result = sense
or
other != val and result = sense.booleanNot()
)
or
val.notComparable() and result = maybe()
val.isComparable() = false and result = maybe()
or
other.notComparable() and result = maybe()
other.isComparable() = false and result = maybe()
)
)
}
@@ -972,7 +972,7 @@ module Types {
exists(ObjectInternal base |
base = getBase(cls, _) and
result = ObjectInternal::unknownClass() |
base.notClass()
base.isClass() = false
or
base = ObjectInternal::unknownClass()
)