Track type of Classes/Modules and and self in singleton methods

This commit is contained in:
Arthur Baars
2021-05-12 20:56:04 +02:00
parent f157f1f359
commit 84da0cb2f3

View File

@@ -130,6 +130,15 @@ private DataFlow::LocalSourceNode trackInstance(Module tp, TypeTracker t) {
not self.getEnclosingModule().getEnclosingMethod() = enclosing
)
or
// `self` in singleton method
exists(Self self, MethodBase enclosing, DataFlow::Node objectNode |
self = result.asExpr().getExpr() and
singletonMethod(enclosing, objectNode.asExpr().getExpr()) and
enclosing = self.getEnclosingMethod() and
trackInstance(tp).flowsTo(objectNode) and
not self.getEnclosingModule().getEnclosingMethod() = enclosing
)
or
// `self` in top-level
exists(Self self, Toplevel enclosing |
self = result.asExpr().getExpr() and
@@ -137,6 +146,14 @@ private DataFlow::LocalSourceNode trackInstance(Module tp, TypeTracker t) {
tp = TResolved("Object") and
not self.getEnclosingMethod().getEnclosingModule() = enclosing
)
or
// a module or class
exists(Module m |
result = trackModule(m) and
if m.getADeclaration() instanceof ClassDeclaration
then tp = TResolved("Class")
else tp = TResolved("Module")
)
)
or
exists(TypeTracker t2 | result = trackInstance(tp, t2).track(t2, t))
@@ -172,6 +189,15 @@ private DataFlow::LocalSourceNode trackSingletonMethod(MethodBase m, string name
name = m.getName()
}
private DataFlow::Node selfInModule(Module tp) {
exists(Self self, ModuleBase enclosing |
self = result.asExpr().getExpr() and
enclosing = self.getEnclosingModule() and
tp = enclosing.getModule() and
not self.getEnclosingMethod().getEnclosingModule() = enclosing
)
}
private DataFlow::LocalSourceNode trackModule(Module tp, TypeTracker t) {
t.start() and
(
@@ -179,12 +205,7 @@ private DataFlow::LocalSourceNode trackModule(Module tp, TypeTracker t) {
resolveScopeExpr(result.asExpr().getExpr()) = tp
or
// `self` reference to Module
exists(Self self, ModuleBase enclosing |
self = result.asExpr().getExpr() and
enclosing = self.getEnclosingModule() and
tp = enclosing.getModule() and
not self.getEnclosingMethod().getEnclosingModule() = enclosing
)
result = selfInModule(tp)
)
or
exists(TypeTracker t2 | result = trackModule(tp, t2).track(t2, t))