Search in sources :

Example 6 with UnionType

use of org.yinwang.pysonar.types.UnionType 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 7 with UnionType

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

the class Outliner method generate.

/**
     * Create an outline for a symbol table.
     *
     * @param state the file state
     * @param path  the file for which we're building the outline
     * @return a list of entries constituting the outline
     */
@NotNull
public List<Entry> generate(@NotNull State state, @NotNull String path) {
    List<Entry> result = new ArrayList<>();
    Set<Binding> entries = new TreeSet<>();
    for (Binding b : state.values()) {
        if (!b.isSynthetic() && !b.isBuiltin() && path.equals(b.getFile())) {
            entries.add(b);
        }
    }
    for (Binding nb : entries) {
        List<Entry> kids = null;
        if (nb.kind == Binding.Kind.CLASS) {
            Type realType = nb.type;
            if (realType instanceof UnionType) {
                for (Type t : ((UnionType) realType).types) {
                    if (t instanceof ClassType) {
                        realType = t;
                        break;
                    }
                }
            }
            kids = generate(realType.table, path);
        }
        Entry kid = kids != null ? new Branch() : new Leaf();
        kid.setOffset(nb.start);
        kid.setQname(nb.qname);
        kid.setKind(nb.kind);
        if (kids != null) {
            kid.setChildren(kids);
        }
        result.add(kid);
    }
    return result;
}
Also used : UnionType(org.yinwang.pysonar.types.UnionType) ClassType(org.yinwang.pysonar.types.ClassType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ClassType(org.yinwang.pysonar.types.ClassType) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with UnionType

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

the class TypeInferencer method missingReturn.

static boolean missingReturn(@NotNull Type toType) {
    boolean hasNone = false;
    boolean hasOther = false;
    if (toType instanceof UnionType) {
        for (Type t : ((UnionType) toType).types) {
            if (t == Types.NoneInstance || t == Types.CONT) {
                hasNone = true;
            } else {
                hasOther = true;
            }
        }
    }
    return hasNone && hasOther;
}
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)

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