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