Search in sources :

Example 1 with TupleType

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

the class TypeInferencer method visit.

@NotNull
@Override
public Type visit(ClassDef node, State s) {
    ClassType classType = new ClassType(node.name.id, s);
    List<Type> baseTypes = new ArrayList<>();
    for (Node base : node.bases) {
        Type baseType = visit(base, s);
        if (baseType instanceof ClassType) {
            classType.addSuper(baseType);
        } else if (baseType instanceof UnionType) {
            for (Type parent : ((UnionType) baseType).types) {
                classType.addSuper(parent);
            }
        } else {
            addWarningToNode(base, base + " is not a class");
        }
        baseTypes.add(baseType);
    }
    // XXX: Not sure if we should add "bases", "name" and "dict" here. They
    // must be added _somewhere_ but I'm just not sure if it should be HERE.
    node.addSpecialAttribute(classType.table, "__bases__", new TupleType(baseTypes));
    node.addSpecialAttribute(classType.table, "__name__", Types.StrInstance);
    node.addSpecialAttribute(classType.table, "__dict__", new DictType(Types.StrInstance, Types.UNKNOWN));
    node.addSpecialAttribute(classType.table, "__module__", Types.StrInstance);
    node.addSpecialAttribute(classType.table, "__doc__", Types.StrInstance);
    // Bind ClassType to name here before resolving the body because the
    // methods need node type as self.
    bind(s, node.name, classType, CLASS);
    if (node.body != null) {
        visit(node.body, classType.table);
    }
    return Types.CONT;
}
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) ArrayList(java.util.ArrayList) TupleType(org.yinwang.pysonar.types.TupleType) DictType(org.yinwang.pysonar.types.DictType) ClassType(org.yinwang.pysonar.types.ClassType) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TupleType

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

the class TypeInferencer method bindParams.

@NotNull
private Type bindParams(@NotNull State state, @NotNull FunctionDef func, @Nullable List<Type> pTypes, @Nullable List<Type> dTypes, @Nullable Map<String, Type> hash, @Nullable Type kw, @Nullable Type star) {
    List<Node> args = func.args;
    Name rest = func.vararg;
    Name restKw = func.kwarg;
    TupleType fromType = new TupleType();
    int pSize = args == null ? 0 : args.size();
    int aSize = pTypes == null ? 0 : pTypes.size();
    int dSize = dTypes == null ? 0 : dTypes.size();
    int nPos = pSize - dSize;
    if (star != null && star instanceof ListType) {
        star = ((ListType) star).toTupleType();
    }
    for (int i = 0, j = 0; i < pSize; i++) {
        Node arg = args.get(i);
        Type aType;
        if (i < aSize) {
            aType = pTypes.get(i);
        } else if (i - nPos >= 0 && i - nPos < dSize) {
            aType = dTypes.get(i - nPos);
        } else {
            if (hash != null && args.get(i) instanceof Name && hash.containsKey(((Name) args.get(i)).id)) {
                aType = hash.get(((Name) args.get(i)).id);
                hash.remove(((Name) args.get(i)).id);
            } else {
                if (star != null && star instanceof TupleType && j < ((TupleType) star).eltTypes.size()) {
                    aType = ((TupleType) star).get(j++);
                } else {
                    aType = Types.UNKNOWN;
                    addWarningToNode(args.get(i), "unable to bind argument:" + args.get(i));
                }
            }
        }
        bind(state, arg, aType, PARAMETER);
        fromType.add(aType);
    }
    if (restKw != null) {
        if (hash != null && !hash.isEmpty()) {
            Type hashType = UnionType.newUnion(hash.values());
            bind(state, restKw, new DictType(Types.StrInstance, hashType), PARAMETER);
        } else {
            bind(state, restKw, Types.UNKNOWN, PARAMETER);
        }
    }
    if (rest != null) {
        if (pTypes.size() > pSize) {
            if (func.afterRest != null) {
                int nAfter = func.afterRest.size();
                for (int i = 0; i < nAfter; i++) {
                    bind(state, func.afterRest.get(i), pTypes.get(pTypes.size() - nAfter + i), PARAMETER);
                }
                if (pTypes.size() - nAfter > 0) {
                    Type restType = new TupleType(pTypes.subList(pSize, pTypes.size() - nAfter));
                    bind(state, rest, restType, PARAMETER);
                }
            } else {
                Type restType = new TupleType(pTypes.subList(pSize, pTypes.size()));
                bind(state, rest, restType, PARAMETER);
            }
        } else {
            bind(state, rest, Types.UNKNOWN, PARAMETER);
        }
    }
    return fromType;
}
Also used : 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) ListType(org.yinwang.pysonar.types.ListType) TupleType(org.yinwang.pysonar.types.TupleType) DictType(org.yinwang.pysonar.types.DictType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)2 ClassType (org.yinwang.pysonar.types.ClassType)2 DictType (org.yinwang.pysonar.types.DictType)2 FunType (org.yinwang.pysonar.types.FunType)2 InstanceType (org.yinwang.pysonar.types.InstanceType)2 ListType (org.yinwang.pysonar.types.ListType)2 ModuleType (org.yinwang.pysonar.types.ModuleType)2 TupleType (org.yinwang.pysonar.types.TupleType)2 Type (org.yinwang.pysonar.types.Type)2 UnionType (org.yinwang.pysonar.types.UnionType)2 ArrayList (java.util.ArrayList)1