Search in sources :

Example 1 with UnionType

use of org.yinwang.pysonar.types.UnionType in project pysonar2 by yinwang0.

the class JSONDump method writeSymJson.

private static void writeSymJson(Binding binding, JsonGenerator json) throws IOException {
    if (binding.start < 0) {
        return;
    }
    String name = binding.name;
    boolean isExported = !(Binding.Kind.VARIABLE == binding.kind || Binding.Kind.PARAMETER == binding.kind || Binding.Kind.SCOPE == binding.kind || Binding.Kind.ATTRIBUTE == binding.kind || (name.length() == 0 || name.charAt(0) == '_' || name.startsWith("lambda%")));
    String path = binding.qname.replace('.', '/').replace("%20", ".");
    if (!seenDef.contains(path)) {
        seenDef.add(path);
        json.writeStartObject();
        json.writeStringField("name", name);
        json.writeStringField("path", path);
        json.writeStringField("file", binding.fileOrUrl);
        json.writeNumberField("identStart", binding.start);
        json.writeNumberField("identEnd", binding.end);
        json.writeNumberField("defStart", binding.bodyStart);
        json.writeNumberField("defEnd", binding.bodyEnd);
        json.writeBooleanField("exported", isExported);
        json.writeStringField("kind", binding.kind.toString());
        if (Binding.Kind.FUNCTION == binding.kind || Binding.Kind.METHOD == binding.kind || Binding.Kind.CONSTRUCTOR == binding.kind) {
            json.writeObjectFieldStart("funcData");
            // get args expression
            String argExpr = null;
            Type t = binding.type;
            if (t instanceof UnionType) {
                t = ((UnionType) t).firstUseful();
            }
            if (t != null && t instanceof FunType) {
                FunctionDef func = ((FunType) t).func;
                if (func != null) {
                    argExpr = func.getArgumentExpr();
                }
            }
            String typeExpr = binding.type.toString();
            json.writeNullField("params");
            String signature = argExpr == null ? "" : argExpr + "\n" + typeExpr;
            json.writeStringField("signature", signature);
            json.writeEndObject();
        }
        json.writeEndObject();
    }
}
Also used : UnionType(org.yinwang.pysonar.types.UnionType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) FunType(org.yinwang.pysonar.types.FunType) FunType(org.yinwang.pysonar.types.FunType) FunctionDef(org.yinwang.pysonar.ast.FunctionDef)

Example 2 with UnionType

use of org.yinwang.pysonar.types.UnionType in project pysonar2 by yinwang0.

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(Try node, State s) {
    Type tp1 = Types.UNKNOWN;
    Type tp2 = Types.UNKNOWN;
    Type tph = Types.UNKNOWN;
    Type tpFinal = Types.UNKNOWN;
    if (node.handlers != null) {
        for (Handler h : node.handlers) {
            tph = UnionType.union(tph, visit(h, s));
        }
    }
    if (node.body != null) {
        tp1 = visit(node.body, s);
    }
    if (node.orelse != null) {
        tp2 = visit(node.orelse, s);
    }
    if (node.finalbody != null) {
        tpFinal = visit(node.finalbody, s);
    }
    return new UnionType(tp1, tp2, tph, tpFinal);
}
Also used : UnionType(org.yinwang.pysonar.types.UnionType) ClassType(org.yinwang.pysonar.types.ClassType) ListType(org.yinwang.pysonar.types.ListType) DictType(org.yinwang.pysonar.types.DictType) InstanceType(org.yinwang.pysonar.types.InstanceType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) TupleType(org.yinwang.pysonar.types.TupleType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with UnionType

use of org.yinwang.pysonar.types.UnionType in project pysonar2 by yinwang0.

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(Call node, State s) {
    Type fun;
    Type selfType = null;
    if (node.func instanceof Attribute) {
        Node target = ((Attribute) node.func).target;
        Name attr = ((Attribute) node.func).attr;
        selfType = visit(target, s);
        Set<Binding> b = selfType.table.lookupAttr(attr.id);
        if (b != null) {
            Analyzer.self.putRef(attr, b);
            fun = State.makeUnion(b);
        } else {
            Analyzer.self.putProblem(attr, "Attribute is not found in type: " + attr.id);
            fun = Types.UNKNOWN;
        }
    } else {
        fun = visit(node.func, s);
    }
    // Infer positional argument types
    List<Type> positional = visit(node.args, s);
    // Infer keyword argument types
    Map<String, Type> kwTypes = new HashMap<>();
    if (node.keywords != null) {
        for (Keyword k : node.keywords) {
            kwTypes.put(k.arg, visit(k.value, s));
        }
    }
    Type kwArg = node.kwargs == null ? null : visit(node.kwargs, s);
    Type starArg = node.starargs == null ? null : visit(node.starargs, s);
    if (fun instanceof UnionType) {
        Set<Type> types = ((UnionType) fun).types;
        Type resultType = Types.UNKNOWN;
        for (Type funType : types) {
            Type returnType = resolveCall(funType, selfType, positional, kwTypes, kwArg, starArg, node);
            resultType = UnionType.union(resultType, returnType);
        }
        return resultType;
    } else {
        return resolveCall(fun, selfType, positional, kwTypes, kwArg, starArg, node);
    }
}
Also used : Binding(org.yinwang.pysonar.Binding) UnionType(org.yinwang.pysonar.types.UnionType) HashMap(java.util.HashMap) ClassType(org.yinwang.pysonar.types.ClassType) ListType(org.yinwang.pysonar.types.ListType) DictType(org.yinwang.pysonar.types.DictType) InstanceType(org.yinwang.pysonar.types.InstanceType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) TupleType(org.yinwang.pysonar.types.TupleType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with UnionType

use of org.yinwang.pysonar.types.UnionType in project pysonar2 by yinwang0.

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(Attribute node, State s) {
    Type targetType = visit(node.target, s);
    if (targetType instanceof UnionType) {
        Set<Type> types = ((UnionType) targetType).types;
        Type retType = Types.UNKNOWN;
        for (Type tt : types) {
            retType = UnionType.union(retType, getAttrType(node, tt));
        }
        return retType;
    } else {
        return getAttrType(node, targetType);
    }
}
Also used : UnionType(org.yinwang.pysonar.types.UnionType) ClassType(org.yinwang.pysonar.types.ClassType) ListType(org.yinwang.pysonar.types.ListType) DictType(org.yinwang.pysonar.types.DictType) InstanceType(org.yinwang.pysonar.types.InstanceType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) TupleType(org.yinwang.pysonar.types.TupleType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with UnionType

use of org.yinwang.pysonar.types.UnionType in project pysonar2 by yinwang0.

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(Subscript node, State s) {
    Type vt = visit(node.value, s);
    Type st = node.slice == null ? null : visit(node.slice, s);
    if (vt instanceof UnionType) {
        Type retType = Types.UNKNOWN;
        for (Type t : ((UnionType) vt).types) {
            retType = UnionType.union(retType, getSubscript(node, t, st, s));
        }
        return retType;
    } else {
        return getSubscript(node, vt, st, s);
    }
}
Also used : UnionType(org.yinwang.pysonar.types.UnionType) ClassType(org.yinwang.pysonar.types.ClassType) ListType(org.yinwang.pysonar.types.ListType) DictType(org.yinwang.pysonar.types.DictType) InstanceType(org.yinwang.pysonar.types.InstanceType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) TupleType(org.yinwang.pysonar.types.TupleType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Type (org.yinwang.pysonar.types.Type)8 UnionType (org.yinwang.pysonar.types.UnionType)8 ClassType (org.yinwang.pysonar.types.ClassType)7 FunType (org.yinwang.pysonar.types.FunType)7 NotNull (org.jetbrains.annotations.NotNull)6 DictType (org.yinwang.pysonar.types.DictType)6 InstanceType (org.yinwang.pysonar.types.InstanceType)6 ListType (org.yinwang.pysonar.types.ListType)6 ModuleType (org.yinwang.pysonar.types.ModuleType)6 TupleType (org.yinwang.pysonar.types.TupleType)6 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 Binding (org.yinwang.pysonar.Binding)1 FunctionDef (org.yinwang.pysonar.ast.FunctionDef)1