use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.
the class ImportFrom method importStar.
public void importStar(@NotNull State s, @Nullable Type mt) {
if (mt == null || mt.file == null) {
return;
}
Node node = Analyzer.self.getAstForFile(mt.file);
if (node == null) {
return;
}
List<String> names = new ArrayList<>();
Type allType = mt.table.lookupType("__all__");
if (allType != null && allType instanceof ListType) {
ListType lt = (ListType) allType;
for (Object o : lt.values) {
if (o instanceof String) {
names.add((String) o);
}
}
}
if (!names.isEmpty()) {
int start = this.start;
int col = this.col;
for (String name : names) {
Set<Binding> b = mt.table.lookupLocal(name);
if (b != null) {
s.update(name, b);
} else {
List<Name> m2 = new ArrayList<>(module);
Name fakeName = new Name(name, this.file, start, start + name.length(), this.line, col);
m2.add(fakeName);
Type type = Analyzer.self.loadModule(m2, s);
if (type != null) {
start += name.length();
col += name.length();
s.insert(name, fakeName, type, Binding.Kind.VARIABLE);
}
}
}
} else {
// Fall back to importing all names not starting with "_".
for (Entry<String, Set<Binding>> e : mt.table.entrySet()) {
if (!e.getKey().startsWith("_")) {
s.update(e.getKey(), e.getValue());
}
}
}
}
use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.
the class TypeInferencer method setAttrType.
private void setAttrType(Attribute node, @NotNull Type targetType, @NotNull Type v) {
if (targetType.isUnknownType()) {
addWarningToNode(node, "Can't set attribute for UnknownType");
return;
}
Set<Binding> bs = targetType.table.lookupAttr(node.attr.id);
if (bs != null) {
for (Binding b : bs) {
b.addType(v);
Analyzer.self.putRef(node.attr, b);
}
} else {
targetType.table.insert(node.attr.id, node.attr, v, ATTRIBUTE);
}
}
use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.
the class TypeInferencer method bind.
public static void bind(@NotNull State s, @NotNull Name name, @NotNull Type rvalue, Binding.Kind kind) {
if (s.isGlobalName(name.id)) {
Set<Binding> bs = s.lookup(name.id);
if (bs != null) {
for (Binding b : bs) {
b.addType(rvalue);
Analyzer.self.putRef(name, b);
}
}
} else {
s.insert(name.id, name, rvalue, kind);
}
}
use of org.yinwang.pysonar.Binding in project pysonar2 by yinwang0.
the class TypeInferencer method visit.
@NotNull
@Override
public Type visit(Name node, State s) {
Set<Binding> b = s.lookup(node.id);
if (b != null) {
Analyzer.self.putRef(node, b);
Analyzer.self.resolved.add(node);
Analyzer.self.unresolved.remove(node);
return State.makeUnion(b);
} else {
addWarningToNode(node, "unbound variable " + node.id);
Analyzer.self.unresolved.add(node);
Type t = Types.UNKNOWN;
t.table.setPath(s.extendPath(node.id));
return t;
}
}
Aggregations