Search in sources :

Example 1 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class ClassDef method addSpecialAttribute.

public void addSpecialAttribute(@NotNull State s, String name, Type proptype) {
    Binding b = new Binding(name, Builtins.newTutUrl("classes.html"), proptype, Binding.Kind.ATTRIBUTE);
    s.update(name, b);
    b.markSynthetic();
    b.markStatic();
}
Also used : Binding(org.yinwang.pysonar.Binding)

Example 2 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class TypeInferencer method addReadOnlyAttr.

static void addReadOnlyAttr(@NotNull FunType fun, String name, @NotNull Type type, Binding.Kind kind) {
    Node loc = Builtins.newDataModelUrl("the-standard-type-hierarchy");
    Binding b = new Binding(name, loc, type, kind);
    fun.table.update(name, b);
    b.markSynthetic();
    b.markStatic();
}
Also used : Binding(org.yinwang.pysonar.Binding)

Example 3 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(Block node, State s) {
    // first pass: mark global names
    for (Node n : node.seq) {
        if (n instanceof Global) {
            for (Name name : ((Global) n).names) {
                s.addGlobalName(name.id);
                Set<Binding> nb = s.lookup(name.id);
                if (nb != null) {
                    Analyzer.self.putRef(name, nb);
                }
            }
        }
    }
    boolean returned = false;
    Type retType = Types.UNKNOWN;
    for (Node n : node.seq) {
        Type t = visit(n, s);
        if (!returned) {
            retType = UnionType.union(retType, t);
            if (!UnionType.contains(t, Types.CONT)) {
                returned = true;
                retType = UnionType.remove(retType, Types.CONT);
            }
        }
    }
    return retType;
}
Also used : Binding(org.yinwang.pysonar.Binding) 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 Binding

use of org.yinwang.pysonar.Binding 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 5 with Binding

use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.

the class TypeInferencer method getAttrType.

public Type getAttrType(Attribute node, @NotNull Type targetType) {
    Set<Binding> bs = targetType.table.lookupAttr(node.attr.id);
    if (bs == null) {
        addWarningToNode(node.attr, "attribute not found in type: " + targetType);
        Type t = Types.UNKNOWN;
        t.table.setPath(targetType.table.extendPath(node.attr.id));
        return t;
    } else {
        for (Binding b : bs) {
            Analyzer.self.putRef(node.attr, b);
        }
        return State.makeUnion(bs);
    }
}
Also used : Binding(org.yinwang.pysonar.Binding) 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)

Aggregations

Binding (org.yinwang.pysonar.Binding)9 ListType (org.yinwang.pysonar.types.ListType)5 Type (org.yinwang.pysonar.types.Type)5 ClassType (org.yinwang.pysonar.types.ClassType)4 DictType (org.yinwang.pysonar.types.DictType)4 FunType (org.yinwang.pysonar.types.FunType)4 InstanceType (org.yinwang.pysonar.types.InstanceType)4 ModuleType (org.yinwang.pysonar.types.ModuleType)4 TupleType (org.yinwang.pysonar.types.TupleType)4 UnionType (org.yinwang.pysonar.types.UnionType)4 NotNull (org.jetbrains.annotations.NotNull)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1