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;
}
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;
}
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;
}
Aggregations