def resolve(self, scope): from pysonarsq.java.Analyzer import Analyzer from pysonarsq.java.Scope import Scope # find global names and mark them for n in self.seq: if n.isGlobal(): for name in n.asGlobal().getNames(): scope.addGlobalName(name.id) nb = scope.lookup(name.id) if nb is not None: Analyzer.self.putRef(name, nb) returned = False retType = Analyzer.self.builtins.unknown for n in self.seq: t = self.resolveExpr(n, scope) if not returned: retType = UnionType.union(retType, t) if not UnionType.contains(t, Analyzer.self.builtins.Cont): returned = True retType = UnionType.remove(retType, Analyzer.self.builtins.Cont) elif scope.getScopeType( ) != Scope.ScopeType.GLOBAL and scope.getScopeType( ) != Scope.ScopeType.MODULE: Analyzer.self.putProblem(n, "unreachable code") return retType
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer type1 = None type2 = None self.resolveExpr(self.test, s) s1 = s.copy() s2 = s.copy() if self.body is not None and not self.body.isEmpty(): type1 = self.resolveExpr(self.body, s1) else: type1 = Analyzer.self.builtins.Cont if self.orelse is not None and not self.orelse.isEmpty(): type2 = self.resolveExpr(self.orelse, s2) else: type2 = Analyzer.self.builtins.Cont cont1 = UnionType.contains(type1, Analyzer.self.builtins.Cont) cont2 = UnionType.contains(type2, Analyzer.self.builtins.Cont) if cont1 and cont2: s.overwrite(Scope.merge(s1, s2)) elif cont1: s.overwrite(s1) elif cont2: s.overwrite(s2) return UnionType.union(type1, type2)
def resolve(self, scope): from pysonarsq.java.Analyzer import Analyzer from pysonarsq.java.Scope import Scope # find global names and mark them for n in self.seq: if n.isGlobal(): for name in n.asGlobal().getNames(): scope.addGlobalName(name.id) nb = scope.lookup(name.id); if nb is not None: Analyzer.self.putRef(name, nb) returned = False retType = Analyzer.self.builtins.unknown for n in self.seq: t = self.resolveExpr(n, scope); if not returned: retType = UnionType.union(retType, t) if not UnionType.contains(t, Analyzer.self.builtins.Cont): returned = True retType = UnionType.remove(retType, Analyzer.self.builtins.Cont) elif scope.getScopeType() != Scope.ScopeType.GLOBAL and scope.getScopeType() != Scope.ScopeType.MODULE: Analyzer.self.putProblem(n, "unreachable code") return retType
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer tp1 = Analyzer.self.builtins.unknown tp2 = Analyzer.self.builtins.unknown tph = Analyzer.self.builtins.unknown for h in self.handlers: tph = UnionType.union(tph, self.resolveExpr(h, s)) if self.body is not None: tp1 = self.resolveExpr(self.body, s) if self.orelse is not None: tp2 = self.resolveExpr(self.orelse, s) return UnionType.union(tp1, UnionType.union(tp2, tph))
def bindParams(cls, call, funcTable, args, fvarargs, fkwargs, aTypes, dTypes, kwTypes, kwargsType, starargsType): from pysonarsq.java.Analyzer import Analyzer from pysonarsq.java.Binder import Binder fromType = TupleType() aSize = 0 if aTypes is None else len(aTypes) dSize = 0 if dTypes is None else len(dTypes) nPositional = len(args) - dSize if starargsType is not None and starargsType.isListType(): starargsType = starargsType.asListType().toTupleType() j = 0 for i, arg in enumerate(range(len(args))): arg = args[i] if i < aSize: aType = aTypes[i] elif i - nPositional >= 0 and i - nPositional < dSize: aType = dTypes[i - nPositional] else: if kwTypes is not None and args[i].isName() and args[i].asName( ).id in kwTypes: aType = kwTypes[args[i].asName().id] del kwTypes[args[i].asName().id] elif starargsType is not None and starargsType.isTupleType( ) and j < len(starargsType.asTupleType().getElementTypes()): aType = starargsType.asTupleType().get(j) j += 1 else: aType = Analyzer.self.builtins.unknown if call is not None: Analyzer.self.putProblem( args[i], "unable to bind argument:" + str(args[i])) Binder.bind(funcTable, arg, aType, Binding.Kind.PARAMETER) fromType.add(aType) if fkwargs is not None: if kwTypes is not None and len(kwTypes): kwValType = UnionType.newUnion(kwTypes.values()) Binder.bind( funcTable, fkwargs, DictType(Analyzer.self.builtins.BaseStr, kwValType), Binding.Kind.PARAMETER) else: Binder.bind(funcTable, fkwargs, Analyzer.self.builtins.unknown, Binding.Kind.PARAMETER) if fvarargs is not None: if len(aTypes) > len(args): starType = TupleType(aTypes[len(args):len(aTypes)]) Binder.bind(funcTable, fvarargs, starType, Binding.Kind.PARAMETER) else: Binder.bind(funcTable, fvarargs, Analyzer.self.builtins.unknown, Binding.Kind.PARAMETER) return fromType
def resolve(self, s): # // experiment with isinstance # if (func.isName() && func.asName().id == "isinstance") { # if (len(args) == 2) { # if (args.get(0).isName()) { # Type rType = resolveExpr(args.get(1), s); # s.put(args.get(0).asName().id, args.get(0), rType, SCOPE); # } # } # } opType = self.resolveExpr(self.func, s) aTypes = self.resolveAndConstructList(self.args, s) kwTypes = dict() for kw in self.keywords: kwTypes[kw.getArg()] = self.resolveExpr(kw.getValue(), s) kwargsType = None if self.kwargs is None else self.resolveExpr(self.kwargs, s) starargsType = None if self.starargs is None else self.resolveExpr(self.starargs, s) if opType.isUnionType(): from pysonarsq.java.Analyzer import Analyzer types = opType.asUnionType().getTypes(); retType = Analyzer.self.builtins.unknown; for funcType in types: t = self.resolveCall(funcType, aTypes, kwTypes, kwargsType, starargsType); retType = UnionType.union(retType, t) return retType else: return self.resolveCall(opType, aTypes, kwTypes, kwargsType, starargsType)
def makeUnion(cls, bs): from pysonarsq.java.Analyzer import Analyzer t = Analyzer.self.builtins.unknown for b in bs: t = UnionType.union(t, b.getType()) return t
def resolveListAsUnion(self, nodes, s): from pysonarsq.java.Analyzer import Analyzer if nodes is None or len(nodes) == 0: return Analyzer.self.builtins.unknown result = Analyzer.self.builtins.unknown for node in nodes: nodeType = self.resolveExpr(node, s) result = UnionType.union(result, nodeType) return result
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer self.resolveExpr(self.test, s) t = Analyzer.self.builtins.unknown if self.body is not None: t = self.resolveExpr(self.body, s) if self.orelse is not None: t = UnionType.union(t, self.resolveExpr(self.orelse, s)) return t
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer vt = self.resolveExpr(self.value, s) st = self.resolveExpr(self.slice_, s) if vt.isUnionType(): for t in vt.asUnionType().getTypes(): retType = Analyzer.self.builtins.unknown retType = UnionType.union(retType, self.getSubscript(t, st, s)) return retType else: return self.getSubscript(vt, st, s)
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer Binder.bindIter(s, self.target, self.iter, Binding.Kind.SCOPE) ret = None if self.body is None: ret = Analyzer.self.builtins.unknown else: ret = self.resolveExpr(self.body, s) if self.orelse is not None: ret = UnionType.union(ret, self.resolveExpr(self.orelse, s)) return ret
def resolve(self, s): targetType = self.resolveExpr(self.target, s) if targetType.isUnionType(): from pysonarsq.java.Analyzer import Analyzer types = targetType.asUnionType().getTypes(); retType = Analyzer.self.builtins.unknown; for tt in types: retType = UnionType.union(retType, self.getAttrType(tt)) return retType else: return self.getAttrType(targetType)
def bindParams(cls, call, funcTable, args, fvarargs, fkwargs, aTypes, dTypes, kwTypes, kwargsType, starargsType): from pysonarsq.java.Analyzer import Analyzer from pysonarsq.java.Binder import Binder fromType = TupleType() aSize = 0 if aTypes is None else len(aTypes) dSize = 0 if dTypes is None else len(dTypes) nPositional = len(args) - dSize if starargsType is not None and starargsType.isListType(): starargsType = starargsType.asListType().toTupleType() j = 0 for i, arg in enumerate(range(len(args))): arg = args[i] if i < aSize: aType = aTypes[i] elif i - nPositional >= 0 and i - nPositional < dSize: aType = dTypes[i - nPositional] else: if kwTypes is not None and args[i].isName() and args[i].asName().id in kwTypes: aType = kwTypes[args[i].asName().id] del kwTypes[args[i].asName().id] elif starargsType is not None and starargsType.isTupleType() and j < len(starargsType.asTupleType().getElementTypes()): aType = starargsType.asTupleType().get(j) j += 1 else: aType = Analyzer.self.builtins.unknown if call is not None: Analyzer.self.putProblem(args[i], "unable to bind argument:" + str(args[i])) Binder.bind(funcTable, arg, aType, Binding.Kind.PARAMETER) fromType.add(aType) if fkwargs is not None: if kwTypes is not None and len(kwTypes): kwValType = UnionType.newUnion(kwTypes.values()); Binder.bind(funcTable, fkwargs, DictType(Analyzer.self.builtins.BaseStr, kwValType), Binding.Kind.PARAMETER) else: Binder.bind(funcTable, fkwargs, Analyzer.self.builtins.unknown, Binding.Kind.PARAMETER) if fvarargs is not None: if len(aTypes) > len(args): starType = TupleType(aTypes[len(args) : len(aTypes)]); Binder.bind(funcTable, fvarargs, starType, Binding.Kind.PARAMETER) else: Binder.bind(funcTable, fvarargs, Analyzer.self.builtins.unknown, Binding.Kind.PARAMETER) return fromType
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer type1 = None type2 = None self.resolveExpr(self.test, s) if self.body is not None: type1 = self.resolveExpr(self.body, s) else: type1 = Analyzer.self.builtins.Cont if self.orelse is not None: type2 = self.resolveExpr(self.orelse, s) else: type2 = Analyzer.self.builtins.Cont return UnionType.union(type1, type2)
def resolve(self, s): from pysonarsq.java.Analyzer import Analyzer ltype = None rtype = None if self.left is not None: ltype = self.resolveExpr(self.left, s) if self.right is not None: rtype = self.resolveExpr(self.right, s) # If either non-null operand is a string, assume the result is a string. if ltype == Analyzer.self.builtins.BaseStr or rtype == Analyzer.self.builtins.BaseStr: return Analyzer.self.builtins.BaseStr # If either non-null operand is a number, assume the result is a number. if ltype == Analyzer.self.builtins.BaseNum or rtype == Analyzer.self.builtins.BaseNum: return Analyzer.self.builtins.BaseNum if ltype is None: return (Analyzer.self.builtins.unknown if rtype is None else rtype) if rtype is None: return (Analyzer.self.builtins.unknown if ltype is None else ltype) return UnionType.union(ltype, rtype)
def resolve(self, s): # // experiment with isinstance # if (func.isName() && func.asName().id == "isinstance") { # if (len(args) == 2) { # if (args.get(0).isName()) { # Type rType = resolveExpr(args.get(1), s); # s.put(args.get(0).asName().id, args.get(0), rType, SCOPE); # } # } # } opType = self.resolveExpr(self.func, s) aTypes = self.resolveAndConstructList(self.args, s) kwTypes = dict() for kw in self.keywords: kwTypes[kw.getArg()] = self.resolveExpr(kw.getValue(), s) kwargsType = None if self.kwargs is None else self.resolveExpr( self.kwargs, s) starargsType = None if self.starargs is None else self.resolveExpr( self.starargs, s) if opType.isUnionType(): from pysonarsq.java.Analyzer import Analyzer types = opType.asUnionType().getTypes() retType = Analyzer.self.builtins.unknown for funcType in types: t = self.resolveCall(funcType, aTypes, kwTypes, kwargsType, starargsType) retType = UnionType.union(retType, t) return retType else: return self.resolveCall(opType, aTypes, kwTypes, kwargsType, starargsType)
def bind(cls, s, target, rvalue, kind=None): from Binding import Binding from pysonarsq.java.Analyzer import Analyzer from Scope import Scope if kind is None: kind = Binding.Kind() if s.getScopeType() == Scope.ScopeType.FUNCTION: kind = Binding.Kind.VARIABLE else: kind = Binding.Kind.SCOPE if isinstance(target, list): xs = target if rvalue.isTupleType(): vs = rvalue.asTupleType().getElementTypes() if len(xs) != len(vs): cls.reportUnpackMismatch(xs, len(vs)) else: for i in range(len(xs)): cls.bind(s, xs[i], vs[i], kind) elif rvalue.isListType(): cls.bind(s, xs, rvalue.asListType().toTupleType(len(xs)), kind) elif rvalue.isDictType(): cls.bind(s, xs, rvalue.asDictType().toTupleType(len(xs)), kind) elif rvalue.isUnknownType(): for x in xs: cls.bind(s, x, Analyzer.self.builtins.unknown, kind) else: Analyzer.self.putProblem(xs[0].getFile(), xs[0].start, xs[len(xs) - 1].end, "unpacking non-iterable: " + str(rvalue)) return if isinstance(target, (Name, )): name = target if (s.isGlobalName(name.id)): from Binding import Binding b = Binding(name.id, name, rvalue, kind); s.getGlobalTable().update(name.id, b) Analyzer.self.putRef(name, b) else: s.insert(name.id, name, rvalue, kind) elif isinstance(target, (Tuple, )): cls.bind(s, (target).elts, rvalue, kind) elif isinstance(target, (NList, )): cls.bind(s, (target).elts, rvalue, kind) elif isinstance(target, (Attribute, )): (target).setAttr(s, rvalue) elif isinstance(target, (Subscript, )): sub = target; valueType = Node.resolveExpr(sub.value, s); Node.resolveExpr(sub.slice_, s) if isinstance(valueType, (ListType, )): t = valueType; t.setElementType(UnionType.union(t.getElementType(), rvalue)) elif target is not None: Analyzer.self.putProblem(target, "invalid location for assignment")