498
// in src/org/python/indexer/AstConverter.java
private List<NExceptHandler> convertListExceptHandler(List<excepthandler> in) throws Exception {
List<NExceptHandler> out = new ArrayList<NExceptHandler>(in == null ? 0 : in.size());
if (in != null) {
for (excepthandler e : in) {
@SuppressWarnings("unchecked")
NExceptHandler nxh = (NExceptHandler)e.accept(this);
if (nxh != null) {
out.add(nxh);
}
}
}
return out;
}
// in src/org/python/indexer/AstConverter.java
private List<NNode> convertListExpr(List<expr> in) throws Exception {
List<NNode> out = new ArrayList<NNode>(in == null ? 0 : in.size());
if (in != null) {
for (expr e : in) {
@SuppressWarnings("unchecked")
NNode nx = (NNode)e.accept(this);
if (nx != null) {
out.add(nx);
}
}
}
return out;
}
// in src/org/python/indexer/AstConverter.java
private List<NName> convertListName(List<Name> in) throws Exception {
List<NName> out = new ArrayList<NName>(in == null ? 0 : in.size());
if (in != null) {
for (expr e : in) {
@SuppressWarnings("unchecked")
NName nn = (NName)e.accept(this);
if (nn != null) {
out.add(nn);
}
}
}
return out;
}
// in src/org/python/indexer/AstConverter.java
private NQname convertQname(List<Name> in) throws Exception {
if (in == null) {
return null;
}
// This would be less ugly if we generated Qname nodes in the antlr ast.
NQname out = null;
int end = -1;
for (int i = in.size() - 1; i >= 0; i--) {
Name n = in.get(i);
if (end == -1) {
end = n.getCharStopIndex();
}
@SuppressWarnings("unchecked")
NName nn = (NName)n.accept(this);
out = new NQname(out, nn, n.getCharStartIndex(), end);
}
return out;
}
// in src/org/python/indexer/AstConverter.java
private List<NKeyword> convertListKeyword(List<keyword> in) throws Exception {
List<NKeyword> out = new ArrayList<NKeyword>(in == null ? 0 : in.size());
if (in != null) {
for (keyword e : in) {
NKeyword nkw = new NKeyword(e.getInternalArg(), convExpr(e.getInternalValue()));
if (nkw != null) {
out.add(nkw);
}
}
}
return out;
}
// in src/org/python/indexer/AstConverter.java
private NBlock convertListStmt(List<stmt> in) throws Exception {
List<NNode> out = new ArrayList<NNode>(in == null ? 0 : in.size());
if (in != null) {
for (stmt e : in) {
@SuppressWarnings("unchecked")
NNode nx = (NNode)e.accept(this);
if (nx != null) {
out.add(nx);
}
}
}
return new NBlock(out, 0, 0);
}
// in src/org/python/indexer/AstConverter.java
private NNode convExpr(PythonTree e) throws Exception {
if (e == null) {
return null;
}
@SuppressWarnings("unchecked")
Object o = e.accept(this);
if (o instanceof NNode) {
return (NNode)o;
}
return null;
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitAssert(Assert n) throws Exception {
return new NAssert(convExpr(n.getInternalTest()),
convExpr(n.getInternalMsg()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitAssign(Assign n) throws Exception {
return new NAssign(convertListExpr(n.getInternalTargets()),
convExpr(n.getInternalValue()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitAttribute(Attribute n) throws Exception {
return new NAttribute(convExpr(n.getInternalValue()),
(NName)convExpr(n.getInternalAttrName()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitAugAssign(AugAssign n) throws Exception {
return new NAugAssign(convExpr(n.getInternalTarget()),
convExpr(n.getInternalValue()),
convOp(n.getInternalOp()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitBinOp(BinOp n) throws Exception {
return new NBinOp(convExpr(n.getInternalLeft()),
convExpr(n.getInternalRight()),
convOp(n.getInternalOp()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitBoolOp(BoolOp n) throws Exception {
NBoolOp.OpType op;
switch (n.getInternalOp()) {
case And:
op = NBoolOp.OpType.AND;
break;
case Or:
op = NBoolOp.OpType.OR;
break;
default:
op = NBoolOp.OpType.UNDEFINED;
break;
}
return new NBoolOp(op, convertListExpr(n.getInternalValues()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitBreak(Break n) throws Exception {
return new NBreak(start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitCall(Call n) throws Exception {
return new NCall(convExpr(n.getInternalFunc()),
convertListExpr(n.getInternalArgs()),
convertListKeyword(n.getInternalKeywords()),
convExpr(n.getInternalKwargs()),
convExpr(n.getInternalStarargs()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitClassDef(ClassDef n) throws Exception {
return new NClassDef((NName)convExpr(n.getInternalNameNode()),
convertListExpr(n.getInternalBases()),
convertListStmt(n.getInternalBody()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitCompare(Compare n) throws Exception {
return new NCompare(convExpr(n.getInternalLeft()),
null, // XXX: why null?
convertListExpr(n.getInternalComparators()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitContinue(Continue n) throws Exception {
return new NContinue(start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitDelete(Delete n) throws Exception {
return new NDelete(convertListExpr(n.getInternalTargets()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitDict(Dict n) throws Exception {
return new NDict(convertListExpr(n.getInternalKeys()),
convertListExpr(n.getInternalValues()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitEllipsis(Ellipsis n) throws Exception {
return new NEllipsis(start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitExceptHandler(ExceptHandler n) throws Exception {
return new NExceptHandler(convExpr(n.getInternalName()),
convExpr(n.getInternalType()),
convertListStmt(n.getInternalBody()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitExec(Exec n) throws Exception {
return new NExec(convExpr(n.getInternalBody()),
convExpr(n.getInternalGlobals()),
convExpr(n.getInternalLocals()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitExpr(Expr n) throws Exception {
return new NExprStmt(convExpr(n.getInternalValue()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitFor(For n) throws Exception {
return new NFor(convExpr(n.getInternalTarget()),
convExpr(n.getInternalIter()),
convertListStmt(n.getInternalBody()),
convertListStmt(n.getInternalOrelse()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitFunctionDef(FunctionDef n) throws Exception {
arguments args = n.getInternalArgs();
NFunctionDef fn = new NFunctionDef((NName)convExpr(n.getInternalNameNode()),
convertListExpr(args.getInternalArgs()),
convertListStmt(n.getInternalBody()),
convertListExpr(args.getInternalDefaults()),
(NName)convExpr(args.getInternalVarargName()),
(NName)convExpr(args.getInternalKwargName()),
start(n), stop(n));
fn.setDecoratorList(convertListExpr(n.getInternalDecorator_list()));
return fn;
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitGeneratorExp(GeneratorExp n) throws Exception {
List<NComprehension> generators =
new ArrayList<NComprehension>(n.getInternalGenerators().size());
for (comprehension c : n.getInternalGenerators()) {
generators.add(new NComprehension(convExpr(c.getInternalTarget()),
convExpr(c.getInternalIter()),
convertListExpr(c.getInternalIfs()),
start(c), stop(c)));
}
return new NGeneratorExp(convExpr(n.getInternalElt()), generators, start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitGlobal(Global n) throws Exception {
return new NGlobal(convertListName(n.getInternalNameNodes()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitIf(If n) throws Exception {
return new NIf(convExpr(n.getInternalTest()),
convertListStmt(n.getInternalBody()),
convertListStmt(n.getInternalOrelse()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitIfExp(IfExp n) throws Exception {
return new NIfExp(convExpr(n.getInternalTest()),
convExpr(n.getInternalBody()),
convExpr(n.getInternalOrelse()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitImport(Import n) throws Exception {
List<NAlias> aliases = new ArrayList<NAlias>(n.getInternalNames().size());
for (alias e : n.getInternalNames()) {
aliases.add(new NAlias(e.getInternalName(),
convertQname(e.getInternalNameNodes()),
(NName)convExpr(e.getInternalAsnameNode()),
start(e), stop(e)));
}
return new NImport(aliases, start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitImportFrom(ImportFrom n) throws Exception {
List<NAlias> aliases = new ArrayList<NAlias>(n.getInternalNames().size());
for (alias e : n.getInternalNames()) {
aliases.add(new NAlias(e.getInternalName(),
convertQname(e.getInternalNameNodes()),
(NName)convExpr(e.getInternalAsnameNode()),
start(e), stop(e)));
}
return new NImportFrom(n.getInternalModule(),
convertQname(n.getInternalModuleNames()),
aliases, start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitIndex(Index n) throws Exception {
return new NIndex(convExpr(n.getInternalValue()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitLambda(Lambda n) throws Exception {
arguments args = n.getInternalArgs();
return new NLambda(convertListExpr(args.getInternalArgs()),
convExpr(n.getInternalBody()),
convertListExpr(args.getInternalDefaults()),
(NName)convExpr(args.getInternalVarargName()),
(NName)convExpr(args.getInternalKwargName()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitList(org.python.antlr.ast.List n) throws Exception {
return new NList(convertListExpr(n.getInternalElts()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitListComp(ListComp n) throws Exception {
List<NComprehension> generators =
new ArrayList<NComprehension>(n.getInternalGenerators().size());
for (comprehension c : n.getInternalGenerators()) {
generators.add(new NComprehension(convExpr(c.getInternalTarget()),
convExpr(c.getInternalIter()),
convertListExpr(c.getInternalIfs()),
start(c), stop(c)));
}
return new NListComp(convExpr(n.getInternalElt()), generators, start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitModule(Module n) throws Exception {
return new NModule(convertListStmt(n.getInternalBody()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitName(Name n) throws Exception {
return new NName(n.getInternalId(), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitNum(Num n) throws Exception {
return new NNum(n.getInternalN(), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitPass(Pass n) throws Exception {
return new NPass(start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitPrint(Print n) throws Exception {
return new NPrint(convExpr(n.getInternalDest()),
convertListExpr(n.getInternalValues()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitRaise(Raise n) throws Exception {
return new NRaise(convExpr(n.getInternalType()),
convExpr(n.getInternalInst()),
convExpr(n.getInternalTback()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitRepr(Repr n) throws Exception {
return new NRepr(convExpr(n.getInternalValue()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitReturn(Return n) throws Exception {
return new NReturn(convExpr(n.getInternalValue()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitSlice(Slice n) throws Exception {
return new NSlice(convExpr(n.getInternalLower()),
convExpr(n.getInternalStep()),
convExpr(n.getInternalUpper()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitStr(Str n) throws Exception {
return new NStr(n.getInternalS(), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitSubscript(Subscript n) throws Exception {
return new NSubscript(convExpr(n.getInternalValue()),
convExpr(n.getInternalSlice()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitTryExcept(TryExcept n) throws Exception {
return new NTryExcept(convertListExceptHandler(n.getInternalHandlers()),
convertListStmt(n.getInternalBody()),
convertListStmt(n.getInternalOrelse()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitTryFinally(TryFinally n) throws Exception {
return new NTryFinally(convertListStmt(n.getInternalBody()),
convertListStmt(n.getInternalFinalbody()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitTuple(Tuple n) throws Exception {
return new NTuple(convertListExpr(n.getInternalElts()), start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitUnaryOp(UnaryOp n) throws Exception {
return new NUnaryOp(null, // XXX: why null for operator?
convExpr(n.getInternalOperand()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitWhile(While n) throws Exception {
return new NWhile(convExpr(n.getInternalTest()),
convertListStmt(n.getInternalBody()),
convertListStmt(n.getInternalOrelse()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitWith(With n) throws Exception {
return new NWith(convExpr(n.getInternalOptional_vars()),
convExpr(n.getInternalContext_expr()),
convertListStmt(n.getInternalBody()),
start(n), stop(n));
}
// in src/org/python/indexer/AstConverter.java
Override
public Object visitYield(Yield n) throws Exception {
return new NYield(convExpr(n.getInternalValue()), start(n), stop(n));
}
// in src/org/python/indexer/Indexer.java
public NModuleType getModuleForFile(String file) throws Exception {
if (failedModules.contains(file)) {
return null;
}
NModuleType m = getCachedModule(file);
if (m != null) {
return m;
}
return loadFile(file);
}
// in src/org/python/indexer/Indexer.java
public List<Outliner.Entry> generateOutline(String file) throws Exception {
return new Outliner().generate(this, file);
}
// in src/org/python/indexer/Indexer.java
public NModuleType loadFile(String path) throws Exception {
return loadFile(path, false);
}
// in src/org/python/indexer/Indexer.java
public NModuleType loadString(String path, String contents) throws Exception {
NModuleType module = getCachedModule(path);
if (module != null) {
finer("\nusing cached module " + path + " [succeeded]");
return module;
}
return parseAndResolve(path, contents);
}
// in src/org/python/indexer/Indexer.java
public NModuleType loadFile(String path, boolean skipChain) throws Exception {
File f = new File(path);
if (f.isDirectory()) {
finer("\n loading init file from directory: " + path);
f = Util.joinPath(path, "__init__.py");
path = f.getAbsolutePath();
}
if (!f.canRead()) {
finer("\nfile not not found or cannot be read: " + path);
return null;
}
NModuleType module = getCachedModule(path);
if (module != null) {
finer("\nusing cached module " + path + " [succeeded]");
return module;
}
if (!skipChain) {
loadParentPackage(path);
}
try {
return parseAndResolve(path);
} catch (StackOverflowError soe) {
handleException("Error loading " + path, soe);
return null;
}
}
// in src/org/python/indexer/Indexer.java
private void loadParentPackage(String file) throws Exception {
File f = new File(file);
File parent = f.getParentFile();
if (parent == null || isInLoadPath(parent)) {
return;
}
// the parent package of an __init__.py file is the grandparent dir
if (parent != null && f.isFile() && "__init__.py".equals(f.getName())) {
parent = parent.getParentFile();
}
if (parent == null || isInLoadPath(parent)) {
return;
}
File initpy = Util.joinPath(parent, "__init__.py");
if (!(initpy.isFile() && initpy.canRead())) {
return;
}
loadFile(initpy.getPath());
}
// in src/org/python/indexer/Indexer.java
private NModuleType parseAndResolve(String file) throws Exception {
return parseAndResolve(file, null);
}
// in src/org/python/indexer/Indexer.java
private AstCache getAstCache() throws Exception {
if (astCache == null) {
astCache = AstCache.get();
}
return astCache;
}
// in src/org/python/indexer/Indexer.java
public NModule getAstForFile(String file) throws Exception {
return getAstCache().getAST(file);
}
// in src/org/python/indexer/Indexer.java
public NModule getAstForFile(String file, String contents) throws Exception {
return getAstCache().getAST(file, contents);
}
// in src/org/python/indexer/Indexer.java
public NModuleType getBuiltinModule(String qname) throws Exception {
return builtins.get(qname);
}
// in src/org/python/indexer/Indexer.java
public NModuleType loadModule(String modname) throws Exception {
if (failedModules.contains(modname)) {
return null;
}
NModuleType cached = getCachedModule(modname); // builtin file-less modules
if (cached != null) {
finer("\nusing cached module " + modname);
return cached;
}
NModuleType mt = getBuiltinModule(modname);
if (mt != null) {
return mt;
}
finer("looking for module " + modname);
if (modname.endsWith(".py")) {
modname = modname.substring(0, modname.length() - 3);
}
String modpath = modname.replace('.', '/');
// A nasty hack to avoid e.g. python2.5 becoming python2/5.
// Should generalize this for directory components containing '.'.
modpath = modpath.replaceFirst("(/python[23])/([0-9]/)", "$1.$2");
List<String> loadPath = getLoadPath();
for (String p : loadPath) {
String dirname = p + modpath;
String pyname = dirname + ".py";
String initname = Util.joinPath(dirname, "__init__.py").getAbsolutePath();
String name;
// foo/bar has priority over foo/bar.py
// http://www.python.org/doc/essays/packages.html
if (Util.isReadableFile(initname)) {
name = initname;
} else if (Util.isReadableFile(pyname)) {
name = pyname;
} else {
continue;
}
name = Util.canonicalize(name);
NModuleType m = loadFile(name);
if (m != null) {
finer("load of module " + modname + "[succeeded]");
return m;
}
}
finer("failed to find module " + modname + " in load path");
failedModules.add(modname);
return null;
}
// in src/org/python/indexer/Indexer.java
public void loadFileRecursive(String fullname) throws Exception {
File file_or_dir = new File(fullname);
if (file_or_dir.isDirectory()) {
for (File file : file_or_dir.listFiles()) {
loadFileRecursive(file.getAbsolutePath());
}
} else {
if (file_or_dir.getAbsolutePath().endsWith(".py")) {
loadFile(file_or_dir.getAbsolutePath());
}
}
}
// in src/org/python/indexer/Outliner.java
public List<Entry> generate(Indexer idx, String abspath) throws Exception {
NModuleType mt = idx.getModuleForFile(abspath);
if (mt == null) {
return new ArrayList<Entry>();
}
return generate(mt.getTable(), abspath);
}
// in src/org/python/indexer/ast/NSubscript.java
Override
public NType resolve(Scope s) throws Exception {
NType vt = resolveExpr(value, s);
NType st = resolveExpr(slice, s);
// slicing
if (vt.isUnknownType()) {
if (st.isListType()) {
return setType(vt);
}
return setType(new NUnknownType());
}
if (st.isListType()) {
NType getslice_type = vt.getTable().lookupTypeAttr("__getslice__");
if (getslice_type == null) {
addError("The type can't be sliced: " + vt);
return setType(new NUnknownType());
}
if (!getslice_type.isFuncType()) {
addError("The type's __getslice__ method is not a function: "
+ getslice_type);
return setType(new NUnknownType());
}
return setType(getslice_type.asFuncType().getReturnType().follow());
}
// subscription
if (slice instanceof NIndex) {
if (vt.isListType()) {
warnUnlessNumIndex(st);
return setType(vt.asListType().getElementType());
}
if (vt.isTupleType()) {
warnUnlessNumIndex(st);
return setType(vt.asTupleType().toListType().getElementType());
}
if (vt.isStrType()) {
warnUnlessNumIndex(st);
return setType(Indexer.idx.builtins.BaseStr);
}
// XXX: unicode, buffer, xrange
if (vt.isDictType()) {
if (!st.follow().equals(vt.asDictType().getKeyType())) {
addWarning("Possible KeyError (wrong type for subscript)");
}
return setType(vt.asDictType().getValueType()); // infer it regardless
}
// else fall through
}
// subscription via delegation
if (vt.isUnionType()) {
for (NType u : vt.asUnionType().getTypes()) {
NType gt = vt.getTable().lookupTypeAttr("__getitem__");
if (gt != null) {
return setType(get__getitem__type(gt, gt));
}
}
}
NType gt = vt.getTable().lookupTypeAttr("__getitem__");
return setType(get__getitem__type(gt, vt));
}
// in src/org/python/indexer/ast/NWhile.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(test, s);
if (body != null) {
setType(resolveExpr(body, s));
}
if (orelse != null) {
addType(resolveExpr(orelse, s));
}
return getType();
}
// in src/org/python/indexer/ast/NAugAssign.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(target, s);
return setType(resolveExpr(value, s));
}
// in src/org/python/indexer/ast/NNum.java
Override
public NType resolve(Scope s) throws Exception {
return setType(Indexer.idx.builtins.BaseNum);
}
// in src/org/python/indexer/ast/NameBinder.java
public void bind(Scope s, NNode target, NType rvalue) throws Exception {
if (target instanceof NName) {
bindName(s, (NName)target, rvalue);
return;
}
if (target instanceof NTuple) {
bind(s, ((NTuple)target).elts, rvalue);
return;
}
if (target instanceof NList) {
bind(s, ((NList)target).elts, rvalue);
return;
}
if (target instanceof NAttribute) {
// This causes various problems if we let it happen during the
// name-binding pass. I believe the only name-binding context
// in which an NAttribute can be an lvalue is in an assignment.
// Assignments are statements, so they can only appear in blocks.
// Hence the scope for the top-level name is unambiguous; we can
// safely leave binding it until the resolve pass.
if (!s.isNameBindingPhase()) {
((NAttribute)target).setAttr(s, rvalue);
}
return;
}
if (target instanceof NSubscript) {
// Ditto. No resolving is allowed during the name-binding phase.
if (!s.isNameBindingPhase()) {
target.resolveExpr(target, s);
}
return;
}
Indexer.idx.putProblem(target, "invalid location for assignment");
}
// in src/org/python/indexer/ast/NameBinder.java
public void bind(Scope s, List<NNode> xs, NType rvalue) throws Exception {
if (rvalue.isTupleType()) {
List<NType> vs = rvalue.asTupleType().getElementTypes();
if (xs.size() != vs.size()) {
reportUnpackMismatch(xs, vs.size());
} else {
for (int i = 0; i < xs.size(); i++) {
bind(s, xs.get(i), vs.get(i));
}
}
return;
}
if (rvalue.isListType()) {
bind(s, xs, rvalue.asListType().toTupleType(xs.size()));
return;
}
if (rvalue.isDictType()) {
bind(s, xs, rvalue.asDictType().toTupleType(xs.size()));
return;
}
if (!rvalue.isUnknownType()) {
Indexer.idx.putProblem(xs.get(0).getFile(),
xs.get(0).start(),
xs.get(xs.size()-1).end(),
"unpacking non-iterable: " + rvalue);
}
for (int i = 0; i < xs.size(); i++) {
bind(s, xs.get(i), new NUnknownType());
}
}
// in src/org/python/indexer/ast/NameBinder.java
public NBinding bindName(Scope s, NName name, NType rvalue) throws Exception {
NBinding b;
if (s.isGlobalName(name.id)) {
b = s.getGlobalTable().put(name.id, name, rvalue, kindOr(SCOPE));
Indexer.idx.putLocation(name, b);
} else {
Scope bindingScope = s.getScopeSymtab();
b = bindingScope.put(name.id, name, rvalue,
kindOr(bindingScope.isFunctionScope() ? VARIABLE : SCOPE));
}
name.setType(b.followType());
// XXX: this seems like a bit of a hack; should at least figure out
// and document what use cases require it.
NType nameType = name.getType();
if (!(nameType.isModuleType() || nameType.isClassType())) {
nameType.getTable().setPath(b.getQname());
}
return b;
}
// in src/org/python/indexer/ast/NameBinder.java
public void bindIter(Scope s, NNode target, NNode iter) throws Exception {
NType iterType = NNode.resolveExpr(iter, s);
if (iterType.isListType()) {
bind(s, target, iterType.asListType().getElementType());
} else if (iterType.isTupleType()) {
bind(s, target, iterType.asTupleType().toListType().getElementType());
} else {
NBinding ent = iterType.getTable().lookupAttr("__iter__");
if (ent == null || !ent.getType().isFuncType()) {
if (!iterType.isUnknownType()) {
iter.addWarning("not an iterable type: " + iterType);
}
bind(s, target, new NUnknownType());
} else {
bind(s, target, ent.getType().asFuncType().getReturnType());
}
}
}
// in src/org/python/indexer/ast/NExprStmt.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(value, s);
return getType();
}
// in src/org/python/indexer/ast/NTryExcept.java
Override
public NType resolve(Scope s) throws Exception {
resolveList(handlers, s);
if (body != null) {
setType(resolveExpr(body, s));
}
if (orelse != null) {
addType(resolveExpr(orelse, s));
}
return getType();
}
// in src/org/python/indexer/ast/NAttribute.java
public void setAttr(Scope s, NType v) throws Exception {
setType(new NUnknownType());
NType targetType = resolveExpr(target, s);
if (targetType.isUnionType()) {
targetType = targetType.asUnionType().firstKnownNonNullAlternate();
if (targetType == null) {
return;
}
}
targetType = targetType.follow();
if (targetType == Indexer.idx.builtins.None) {
return;
}
NBinding b = targetType.getTable().putAttr(attr.id, attr, v, ATTRIBUTE);
if (b != null) {
setType(attr.setType(b.followType()));
}
}
// in src/org/python/indexer/ast/NAttribute.java
Override
public NType resolve(Scope s) throws Exception {
setType(new NUnknownType());
NType targetType = resolveExpr(target, s);
if (targetType.isUnionType()) {
NType ret = new NUnknownType();
for (NType tp : targetType.asUnionType().getTypes()) {
resolveAttributeOnType(tp);
ret = NUnionType.union(ret, getType());
}
setType(attr.setType(ret.follow()));
} else {
resolveAttributeOnType(targetType);
}
return getType();
}
// in src/org/python/indexer/ast/NIf.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(test, s);
if (body != null) {
setType(resolveExpr(body, s));
}
if (orelse != null) {
addType(resolveExpr(orelse, s));
}
return getType();
}
// in src/org/python/indexer/ast/NExec.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(body, s);
resolveExpr(globals, s);
resolveExpr(locals, s);
return getType();
}
// in src/org/python/indexer/ast/NWith.java
Override
public NType resolve(Scope s) throws Exception {
NType val = resolveExpr(context_expr, s);
NameBinder.make().bind(s, optional_vars, val);
return setType(resolveExpr(body, s));
}
// in src/org/python/indexer/ast/NCall.java
Override
public NType resolve(Scope s) throws Exception {
NType ft = resolveExpr(func, s);
List<NType> argTypes = new ArrayList<NType>();
for (NNode a : args) {
argTypes.add(resolveExpr(a, s));
}
resolveList(keywords, s);
resolveExpr(starargs, s);
resolveExpr(kwargs, s);
if (ft.isClassType()) {
return setType(ft); // XXX: was new NInstanceType(ft)
}
if (ft.isFuncType()) {
return setType(ft.asFuncType().getReturnType().follow());
}
if (ft.isUnknownType()) {
NUnknownType to = new NUnknownType();
NFuncType at = new NFuncType(to);
NUnionType.union(ft, at);
return setType(to);
}
addWarning("calling non-function " + ft);
return setType(new NUnknownType());
}
// in src/org/python/indexer/ast/NIfExp.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(test, s);
if (body != null) {
setType(resolveExpr(body, s));
}
if (orelse != null) {
addType(resolveExpr(orelse, s));
}
return getType();
}
// in src/org/python/indexer/ast/NPrint.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(dest, s);
resolveList(values, s);
return getType();
}
// in src/org/python/indexer/ast/NExceptHandler.java
Override
protected void bindNames(Scope s) throws Exception {
if (name != null) {
NameBinder.make().bind(s, name, new NUnknownType());
}
}
// in src/org/python/indexer/ast/NExceptHandler.java
Override
public NType resolve(Scope s) throws Exception {
NType typeval = new NUnknownType();
if (exceptionType != null) {
typeval = resolveExpr(exceptionType, s);
}
if (name != null) {
NameBinder.make().bind(s, name, typeval);
}
if (body != null) {
return setType(resolveExpr(body, s));
} else {
return setType(new NUnknownType());
}
}
// in src/org/python/indexer/ast/NComprehension.java
Override
protected void bindNames(Scope s) throws Exception {
bindNames(s, target, NameBinder.make());
}
// in src/org/python/indexer/ast/NComprehension.java
private void bindNames(Scope s, NNode target, NameBinder binder) throws Exception {
if (target instanceof NName) {
binder.bind(s, (NName)target, new NUnknownType());
return;
}
if (target instanceof NSequence) {
for (NNode n : ((NSequence)target).getElements()) {
bindNames(s, n, binder);
}
}
}
// in src/org/python/indexer/ast/NComprehension.java
Override
public NType resolve(Scope s) throws Exception {
NameBinder.make().bindIter(s, target, iter);
resolveList(ifs, s);
return setType(target.getType());
}
// in src/org/python/indexer/ast/NImport.java
Override
protected void bindNames(Scope s) throws Exception {
bindAliases(s, aliases);
}
// in src/org/python/indexer/ast/NImport.java
static void bindAliases(Scope s, List<NAlias> aliases) throws Exception {
NameBinder binder = NameBinder.make();
for (NAlias a : aliases) {
if (a.aname != null) {
binder.bind(s, a.aname, new NUnknownType());
}
}
}
// in src/org/python/indexer/ast/NImport.java
Override
public NType resolve(Scope s) throws Exception {
Scope scope = s.getScopeSymtab();
for (NAlias a : aliases) {
NType modtype = resolveExpr(a, s);
if (modtype.isModuleType()) {
importName(scope, a, modtype.asModuleType());
}
}
return getType();
}
// in src/org/python/indexer/ast/NImport.java
private void importName(Scope s, NAlias a, NModuleType mt) throws Exception {
if (a.aname != null) {
if (mt.getFile() != null) {
NameBinder.make().bind(s, a.aname, mt);
} else {
// XXX: seems like the url should be set in loadModule, not here.
// Can't the moduleTable store url-keyed modules too?
s.update(a.aname.id,
new NUrl(Builtins.LIBRARY_URL + mt.getTable().getPath() + ".html"),
mt, NBinding.Kind.SCOPE);
}
}
addReferences(s, a.qname, true/*put top name in scope*/);
}
// in src/org/python/indexer/ast/NList.java
Override
public NType resolve(Scope s) throws Exception {
if (elts.size() == 0) {
return setType(new NListType()); // list<unknown>
}
NListType listType = null;
for (NNode elt : elts) {
if (listType == null) {
listType = new NListType(resolveExpr(elt, s));
} else {
listType.add(resolveExpr(elt, s));
}
}
if (listType != null) {
setType(listType);
}
return getType();
}
// in src/org/python/indexer/ast/NBlock.java
Override
public NType resolve(Scope scope) throws Exception {
for (NNode n : seq) {
// XXX: This works for inferring lambda return types, but needs
// to be fixed for functions (should be union of return stmt types).
NType returnType = resolveExpr(n, scope);
if (returnType != Indexer.idx.builtins.None) {
setType(returnType);
}
}
return getType();
}
// in src/org/python/indexer/ast/NGeneratorExp.java
Override
public NType resolve(Scope s) throws Exception {
resolveList(generators, s);
return setType(new NListType(resolveExpr(elt, s)));
}
// in src/org/python/indexer/ast/NAssign.java
Override
protected void bindNames(Scope s) throws Exception {
NameBinder binder = NameBinder.make();
for (NNode target : targets) {
binder.bind(s, target, new NUnknownType());
}
}
// in src/org/python/indexer/ast/NAssign.java
Override
public NType resolve(Scope s) throws Exception {
NType valueType = resolveExpr(rvalue, s);
switch (targets.size()) {
case 0:
break;
case 1:
NameBinder.make().bind(s, targets.get(0), valueType);
break;
default:
NameBinder.make().bind(s, targets, valueType);
break;
}
return setType(valueType);
}
// in src/org/python/indexer/ast/NSlice.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(lower, s);
resolveExpr(step, s);
resolveExpr(upper, s);
return setType(new NListType());
}
// in src/org/python/indexer/ast/NReturn.java
Override
public NType resolve(Scope s) throws Exception {
return setType(resolveExpr(value, s));
}
// in src/org/python/indexer/ast/NDelete.java
Override
public NType resolve(Scope s) throws Exception {
for (NNode n : targets) {
resolveExpr(n, s);
if (n instanceof NName) {
s.remove(((NName)n).id);
}
}
return getType();
}
// in src/org/python/indexer/ast/NFunctionDef.java
Override
protected void bindNames(Scope s) throws Exception {
Scope owner = s.getScopeSymtab(); // enclosing class, function or module
setType(new NFuncType());
Scope funcTable = new Scope(s.getEnclosingLexicalScope(), Scope.Type.FUNCTION);
getType().setTable(funcTable);
funcTable.setPath(owner.extendPath(getBindingName(owner)));
// If we already defined this function in this scope, don't try it again.
NType existing = owner.lookupType(getBindingName(owner), true /* local scope */);
if (existing != null && existing.isFuncType()) {
return;
}
bindFunctionName(owner);
bindFunctionParams(funcTable);
bindFunctionDefaults(s);
bindMethodAttrs(owner);
}
// in src/org/python/indexer/ast/NFunctionDef.java
protected void bindFunctionName(Scope owner) throws Exception {
NBinding.Kind funkind = FUNCTION;
if (owner.getScopeType() == Scope.Type.CLASS) {
if ("__init__".equals(name.id)) {
funkind = CONSTRUCTOR;
} else {
funkind = METHOD;
}
}
NameBinder.make(funkind).bindName(owner, name, getType());
}
// in src/org/python/indexer/ast/NFunctionDef.java
protected void bindFunctionParams(Scope funcTable) throws Exception {
NameBinder param = NameBinder.make(PARAMETER);
for (NNode a : args) {
param.bind(funcTable, a, new NUnknownType());
}
if (varargs != null) {
param.bind(funcTable, varargs, new NListType());
}
if (kwargs != null) {
param.bind(funcTable, kwargs, new NDictType());
}
}
// in src/org/python/indexer/ast/NFunctionDef.java
protected void bindFunctionDefaults(Scope s) throws Exception {
for (NNode n : defaults) {
if (n.bindsName()) {
n.bindNames(s);
}
}
}
// in src/org/python/indexer/ast/NFunctionDef.java
protected void bindMethodAttrs(Scope owner) throws Exception {
NType cls = Indexer.idx.lookupQnameType(owner.getPath());
if (cls == null || !cls.isClassType()) {
return;
}
// We don't currently differentiate between classes and instances.
addReadOnlyAttr("im_class", cls, CLASS);
addReadOnlyAttr("__class__", cls, CLASS);
addReadOnlyAttr("im_self", cls, ATTRIBUTE);
addReadOnlyAttr("__self__", cls, ATTRIBUTE);
}
// in src/org/python/indexer/ast/NFunctionDef.java
Override
public NType resolve(Scope outer) throws Exception {
resolveList(defaults, outer);
resolveList(decoratorList, outer);
Scope funcTable = getTable();
NBinding selfBinding = funcTable.lookup("__self__");
if (selfBinding != null && !selfBinding.getType().isClassType()) {
selfBinding = null;
}
if (selfBinding != null) {
if (args.size() < 1) {
addWarning(name, "method should have at least one argument (self)");
} else if (!(args.get(0) instanceof NName)) {
addError(name, "self parameter must be an identifier");
}
}
NTupleType fromType = new NTupleType();
bindParamsToDefaults(selfBinding, fromType);
if (varargs != null) {
NBinding b = funcTable.lookupLocal(varargs.id);
if (b != null) {
fromType.add(b.getType());
}
}
if (kwargs != null) {
NBinding b = funcTable.lookupLocal(kwargs.id);
if (b != null) {
fromType.add(b.getType());
}
}
NType toType = resolveExpr(body, funcTable);
getType().asFuncType().setReturnType(toType);
return getType();
}
// in src/org/python/indexer/ast/NFunctionDef.java
private void bindParamsToDefaults(NBinding selfBinding, NTupleType fromType) throws Exception {
NameBinder param = NameBinder.make(PARAMETER);
Scope funcTable = getTable();
for (int i = 0; i < args.size(); i++) {
NNode arg = args.get(i);
NType argtype = ((i == 0 && selfBinding != null)
? selfBinding.getType()
: getArgType(args, defaults, i));
param.bind(funcTable, arg, argtype);
fromType.add(argtype);
}
}
// in src/org/python/indexer/ast/NRaise.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(exceptionType, s);
resolveExpr(inst, s);
resolveExpr(traceback, s);
return getType();
}
// in src/org/python/indexer/ast/NCompare.java
Override
public NType resolve(Scope s) throws Exception {
setType(Indexer.idx.builtins.BaseNum);
resolveExpr(left, s);
resolveList(comparators, s);
return getType();
}
// in src/org/python/indexer/ast/NQname.java
Override
public NType resolve(Scope s) throws Exception {
setType(name.setType(new NUnknownType()));
// Check for top-level native or standard module.
if (isUnqualified()) {
NModuleType mt = Indexer.idx.loadModule(name.id);
if (mt != null) {
return setType(name.setType(mt));
}
} else {
// Check for second-level builtin such as "os.path".
NModuleType mt = Indexer.idx.getBuiltinModule(thisQname());
if (mt != null) {
setType(name.setType(mt));
resolveExpr(next, s);
return mt;
}
}
return resolveInFilesystem(s);
}
// in src/org/python/indexer/ast/NQname.java
private NType resolveInFilesystem(Scope s) throws Exception {
NModuleType start = getStartModule(s);
if (start == null) {
reportUnresolvedModule();
return getType();
}
String qname = start.getTable().getPath();
String relQname;
if (isDot()) {
relQname = Util.getQnameParent(qname);
} else if (!isTop()) {
relQname = qname + "." + name.id;
} else {
// top name: first look in current dir, then sys.path
String dirQname = isInitPy() ? qname : Util.getQnameParent(qname);
relQname = dirQname + "." + name.id;
if (Indexer.idx.loadModule(relQname) == null) {
relQname = name.id;
}
}
NModuleType mod = Indexer.idx.loadModule(relQname);
if (mod == null) {
reportUnresolvedModule();
return getType();
}
setType(name.setType(mod));
if (!isTop() && mod.getFile() != null) {
Scope parentPkg = getPrevious().getTable();
NBinding mb = Indexer.idx.moduleTable.lookup(mod.getFile());
parentPkg.put(name.id, mb);
}
resolveExpr(next, s);
return getType();
}
// in src/org/python/indexer/ast/NQname.java
private NModuleType getStartModule(Scope s) throws Exception {
if (!isTop()) {
return getPrevious().getType().asModuleType();
}
// Start with module for current file (i.e. containing directory).
NModuleType start = null;
Scope mtable = s.getSymtabOfType(Scope.Type.MODULE);
if (mtable != null) {
start = Indexer.idx.loadModule(mtable.getPath());
if (start != null) {
return start;
}
}
String dir = new File(getFile()).getParent();
if (dir == null) {
Indexer.idx.warn("Unable to find parent dir for " + getFile());
return null;
}
return Indexer.idx.loadModule(dir);
}
// in src/org/python/indexer/ast/NModule.java
public void setFile(String file) throws Exception {
this.file = file;
this.name = Util.moduleNameFor(file);
this.md5 = Util.getMD5(new File(file));
}
// in src/org/python/indexer/ast/NModule.java
public void setFile(File path) throws Exception {
file = path.getCanonicalPath();
name = Util.moduleNameFor(file);
md5 = Util.getMD5(path);
}
// in src/org/python/indexer/ast/NModule.java
public void setFileAndMD5(String path, String md5) throws Exception {
file = path;
name = Util.moduleNameFor(file);
this.md5 = md5;
}
// in src/org/python/indexer/ast/NModule.java
Override
public NType resolve(Scope s) throws Exception {
NBinding mb = Indexer.idx.moduleTable.lookupLocal(file);
if (mb == null ) {
Indexer.idx.reportFailedAssertion("No module for " + name + ": " + file);
setType(new NModuleType(name, file, s));
} else {
setType(mb.getType());
}
resolveExpr(body, getTable());
resolveExportedNames();
return getType();
}
// in src/org/python/indexer/ast/NModule.java
private void resolveExportedNames() throws Exception {
NModuleType mtype = null;
NType thisType = getType();
if (thisType.isModuleType()) {
mtype = thisType.asModuleType();
} else if (thisType.isUnionType()) {
for (NType u : thisType.asUnionType().getTypes()) {
if (u.isModuleType()) {
mtype = u.asModuleType();
break;
}
}
}
if (mtype == null) {
Indexer.idx.reportFailedAssertion("Found non-module type for "
+ this + " in " + getFile() + ": " + thisType);
return;
}
Scope table = mtype.getTable();
for (NStr nstr : getExportedNameNodes()) {
String name = nstr.n.toString();
NBinding b = table.lookupLocal(name);
if (b != null) {
Indexer.idx.putLocation(nstr, b);
}
}
}
// in src/org/python/indexer/ast/NModule.java
public List<String> getExportedNames() throws Exception {
List<String> exports = new ArrayList<String>();
if (!getType().isModuleType()) {
return exports;
}
for (NStr nstr : getExportedNameNodes()) {
exports.add(nstr.n.toString());
}
return exports;
}
// in src/org/python/indexer/ast/NModule.java
public List<NStr> getExportedNameNodes() throws Exception {
List<NStr> exports = new ArrayList<NStr>();
if (!getType().isModuleType()) {
return exports;
}
NBinding all = getTable().lookupLocal("__all__");
if (all== null) {
return exports;
}
Def def = all.getSignatureNode();
if (def == null) {
return exports;
}
NNode __all__ = getDeepestNodeAtOffset(def.start());
if (!(__all__ instanceof NName)) {
return exports;
}
NNode assign = __all__.getParent();
if (!(assign instanceof NAssign)) {
return exports;
}
NNode rvalue = ((NAssign)assign).rvalue;
if (!(rvalue instanceof NList)) {
return exports;
}
for (NNode elt : ((NList)rvalue).elts) {
if (elt instanceof NStr) {
NStr nstr = (NStr)elt;
if (nstr.n != null) {
exports.add(nstr);
}
}
}
return exports;
}
// in src/org/python/indexer/ast/NListComp.java
Override
public NType resolve(Scope s) throws Exception {
NameBinder binder = NameBinder.make();
resolveList(generators, s);
return setType(new NListType(resolveExpr(elt, s)));
}
// in src/org/python/indexer/ast/NImportFrom.java
Override
protected void bindNames(Scope s) throws Exception {
// XXX: we can support this by resolving the qname now.
if (isImportStar()) {
return;
}
NImport.bindAliases(s, aliases);
}
// in src/org/python/indexer/ast/NImportFrom.java
Override
public NType resolve(Scope s) throws Exception {
Scope scope = s.getScopeSymtab();
resolveExpr(qname, s);
NType bottomType = qname.getBottom().getType();
if (!bottomType.isModuleType()) {
return setType(new NUnknownType());
}
NModuleType mt = (NModuleType)bottomType;
setType(mt);
NImport.addReferences(s, qname, false /* don't put top name in scope */);
if (isImportStar()) {
importStar(s, mt);
return getType();
}
for (NAlias a : aliases) {
resolveAlias(scope, mt, a);
}
return getType();
}
// in src/org/python/indexer/ast/NImportFrom.java
private void resolveAlias(Scope scope, NModuleType mt, NAlias a) throws Exception {
// Possibilities 1 & 2: x/y.py or x/y/__init__.py
NBinding entry = mt.getTable().lookup(a.name);
if (entry == null) {
// Possibility 3: try looking for x/y/foo.py
String mqname = qname.toQname() + "." + a.qname.toQname();
NModuleType mt2 = Indexer.idx.loadModule(mqname);
if (mt2 != null) {
entry = Indexer.idx.lookupQname(mt2.getTable().getPath());
}
}
if (entry == null) {
addError(a, "name " + a.qname.getName().id
+ " not found in module " + this.module);
return;
}
String qname = a.qname.getName().id;
String aname = a.aname != null ? a.aname.id : null;
// Create references for both the name and the alias (if present).
// Then if "foo", add "foo" to scope. If "foo as bar", add "bar".
Indexer.idx.putLocation(a.qname.getName(), entry);
if (aname != null) {
Indexer.idx.putLocation(a.aname, entry);
scope.put(aname, entry);
} else {
scope.put(qname, entry);
}
}
// in src/org/python/indexer/ast/NImportFrom.java
private void importStar(Scope s, NModuleType mt) throws Exception {
if (mt == null || mt.getFile() == null) {
return;
}
NModule mod = Indexer.idx.getAstForFile(mt.getFile());
if (mod == null) {
return;
}
List<String> names = mod.getExportedNames();
if (!names.isEmpty()) {
for (String name : names) {
NBinding nb = mt.getTable().lookupLocal(name);
if (nb != null) {
s.put(name, nb);
}
}
} else {
// Fall back to importing all names not starting with "_".
for (Entry<String, NBinding> e : mt.getTable().entrySet()) {
if (!e.getKey().startsWith("_")) {
s.put(e.getKey(), e.getValue());
}
}
}
}
// in src/org/python/indexer/ast/NUrl.java
Override
public NType resolve(Scope s) throws Exception {
return setType(Indexer.idx.builtins.BaseStr);
}
// in src/org/python/indexer/ast/NClassDef.java
Override
protected void bindNames(Scope s) throws Exception {
Scope container = s.getScopeSymtab();
setType(new NClassType(name.id, container));
// If we already defined this class in this scope, don't redefine it.
NType existing = container.lookupType(name.id);
if (existing != null && existing.isClassType()) {
return;
}
NameBinder.make(NBinding.Kind.CLASS).bind(container, name, getType());
}
// in src/org/python/indexer/ast/NClassDef.java
Override
public NType resolve(Scope s) throws Exception {
NClassType thisType = getType().asClassType();
List<NType> baseTypes = new ArrayList<NType>();
for (NNode base : bases) {
NType baseType = resolveExpr(base, s);
if (baseType.isClassType()) {
thisType.addSuper(baseType);
}
baseTypes.add(baseType);
}
Builtins builtins = Indexer.idx.builtins;
addSpecialAttribute("__bases__", new NTupleType(baseTypes));
addSpecialAttribute("__name__", builtins.BaseStr);
addSpecialAttribute("__module__", builtins.BaseStr);
addSpecialAttribute("__doc__", builtins.BaseStr);
addSpecialAttribute("__dict__", new NDictType(builtins.BaseStr, new NUnknownType()));
resolveExpr(body, getTable());
return getType();
}
// in src/org/python/indexer/ast/NAlias.java
Override
public NType resolve(Scope s) throws Exception {
setType(resolveExpr(qname, s));
// "import a.b.c" defines 'a' (the top module) in the scope, whereas
// "import a.b.c as x" defines 'x', which refers to the bottom module.
if (aname != null && qname != null) {
setType(qname.getBottom().getType());
aname.setType(getType());
}
return getType();
}
// in src/org/python/indexer/ast/NBoolOp.java
Override
public NType resolve(Scope s) throws Exception {
if (op == OpType.AND) {
NType last = null;
for (NNode e : values) {
last = resolveExpr(e, s);
}
return setType(last == null ? new NUnknownType() : last);
}
// OR
return setType(resolveListAsUnion(values, s));
}
// in src/org/python/indexer/ast/NBinOp.java
Override
public NType resolve(Scope s) throws Exception {
NType ltype = null, rtype = null;
if (left != null) {
ltype = resolveExpr(left, s).follow();
}
if (right != null) {
rtype = resolveExpr(right, s).follow();
}
// If either non-null operand is a string, assume the result is a string.
if (ltype == Indexer.idx.builtins.BaseStr || rtype == Indexer.idx.builtins.BaseStr) {
return setType(Indexer.idx.builtins.BaseStr);
}
// If either non-null operand is a number, assume the result is a number.
if (ltype == Indexer.idx.builtins.BaseNum || rtype == Indexer.idx.builtins.BaseNum) {
return setType(Indexer.idx.builtins.BaseNum);
}
if (ltype == null) {
return setType(rtype == null ? new NUnknownType() : rtype);
}
if (rtype == null) {
return setType(ltype == null ? new NUnknownType() : ltype);
}
return setType(NUnionType.union(ltype, rtype));
}
// in src/org/python/indexer/ast/NTuple.java
Override
public NType resolve(Scope s) throws Exception {
NTupleType thisType = new NTupleType();
for (NNode e : elts) {
thisType.add(resolveExpr(e, s));
}
return setType(thisType);
}
// in src/org/python/indexer/ast/NStr.java
Override
public NType resolve(Scope s) throws Exception {
return setType(Indexer.idx.builtins.BaseStr);
}
// in src/org/python/indexer/ast/NRepr.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(value, s);
return setType(Indexer.idx.builtins.BaseStr);
}
// in src/org/python/indexer/ast/NYield.java
Override
public NType resolve(Scope s) throws Exception {
return setType(new NListType(resolveExpr(value, s)));
}
// in src/org/python/indexer/ast/NUnaryOp.java
Override
public NType resolve(Scope s) throws Exception {
return setType(resolveExpr(operand, s));
}
// in src/org/python/indexer/ast/NNode.java
protected void bindNames(Scope s) throws Exception {
throw new UnsupportedOperationException("Not a name-binding node type");
}
// in src/org/python/indexer/ast/NNode.java
public NType resolve(Scope s) throws Exception {
return getType();
}
// in src/org/python/indexer/ast/NIndex.java
Override
public NType resolve(Scope s) throws Exception {
return setType(resolveExpr(value, s));
}
// in src/org/python/indexer/ast/NName.java
Override
public NType resolve(Scope s) throws Exception {
NBinding b = s.lookup(id);
if (b == null) {
b = makeTempBinding(s);
}
Indexer.idx.putLocation(this, b);
return setType(b.followType());
}
// in src/org/python/indexer/ast/NGlobal.java
Override
public NType resolve(Scope s) throws Exception {
Scope moduleTable = s.getGlobalTable();
for (NName name : names) {
if (s.isGlobalName(name.id)) {
continue; // already bound by this (or another) global stmt
}
s.addGlobalName(name.id);
NBinding b = moduleTable.lookup(name);
if (b == null) {
b = moduleTable.put(name.id, null, new NUnknownType(), NBinding.Kind.SCOPE);
}
Indexer.idx.putLocation(name, b);
}
return getType();
}
// in src/org/python/indexer/ast/NBody.java
Override
public NType resolve(Scope scope) throws Exception {
try {
scope.setNameBindingPhase(true);
visit(new GlobalFinder(scope));
visit(new BindingFinder(scope));
} finally {
scope.setNameBindingPhase(false);
}
return super.resolve(scope);
}
// in src/org/python/indexer/ast/NAssert.java
Override
public NType resolve(Scope s) throws Exception {
resolveExpr(test, s);
resolveExpr(msg, s);
return getType();
}
// in src/org/python/indexer/ast/NLambda.java
Override
protected void bindFunctionName(Scope owner) throws Exception {
NameBinder.make(NBinding.Kind.FUNCTION).bindName(owner, fname, getType());
}
// in src/org/python/indexer/ast/NLambda.java
Override
protected void bindMethodAttrs(Scope owner) throws Exception {
// no-op
}
// in src/org/python/indexer/ast/NLambda.java
Override
public NType resolve(Scope s) throws Exception {
if (!getType().isFuncType()) {
org.python.indexer.Indexer.idx.reportFailedAssertion(
"Bad type on " + this + ": type=" + getType() +
" in file " + getFile() + " at " + start());
}
NTupleType fromType = new NTupleType();
NameBinder param = NameBinder.make(NBinding.Kind.PARAMETER);
resolveList(defaults, s);
Scope funcTable = getTable();
int argnum = 0;
for (NNode a : args) {
NType argtype = NFunctionDef.getArgType(args, defaults, argnum++);
param.bind(funcTable, a, argtype);
fromType.add(argtype);
}
if (varargs != null) {
NType u = new NUnknownType();
param.bind(funcTable, varargs, u);
fromType.add(u);
}
if (kwargs != null) {
NType u = new NUnknownType();
param.bind(funcTable, kwargs, u);
fromType.add(u);
}
// A lambda body is not an NBody, so it doesn't undergo the two
// pre-resolve passes for finding global statements and name-binding
// constructs. However, the lambda expression may itself contain
// name-binding constructs (generally, other lambdas), so we need to
// perform the name-binding pass on it before resolving.
try {
funcTable.setNameBindingPhase(true);
body.visit(new BindingFinder(funcTable));
} finally {
funcTable.setNameBindingPhase(false);
}
NType toType = resolveExpr(body, funcTable);
if (getType().isFuncType()) { // else warning logged at method entry above
getType().asFuncType().setReturnType(toType);
}
return getType();
}
// in src/org/python/indexer/ast/NFor.java
Override
protected void bindNames(Scope s) throws Exception {
bindNames(s, target, NameBinder.make());
}
// in src/org/python/indexer/ast/NFor.java
private void bindNames(Scope s, NNode target, NameBinder binder) throws Exception {
if (target instanceof NName) {
binder.bind(s, (NName)target, new NUnknownType());
return;
}
if (target instanceof NSequence) {
for (NNode n : ((NSequence)target).getElements()) {
bindNames(s, n, binder);
}
}
}
// in src/org/python/indexer/ast/NFor.java
Override
public NType resolve(Scope s) throws Exception {
NameBinder.make().bindIter(s, target, iter);
if (body == null) {
setType(new NUnknownType());
} else {
setType(resolveExpr(body, s));
}
if (orelse != null) {
addType(resolveExpr(orelse, s));
}
return getType();
}
// in src/org/python/indexer/ast/NKeyword.java
Override
public NType resolve(Scope s) throws Exception {
return setType(resolveExpr(value, s));
}
// in src/org/python/indexer/ast/NDict.java
Override
public NType resolve(Scope s) throws Exception {
NType keyType = resolveListAsUnion(keys, s);
NType valType = resolveListAsUnion(values, s);
return setType(new NDictType(keyType, valType));
}
// in src/org/python/indexer/ast/NTryFinally.java
Override
public NType resolve(Scope s) throws Exception {
if (body != null) {
setType(resolveExpr(body, s));
}
if (finalbody != null) {
addType(resolveExpr(finalbody, s));
}
return getType();
}
// in src/org/python/indexer/Util.java
public static void writeFile(String path, String contents) throws Exception {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(path)));
out.print(contents);
out.flush();
} finally {
if (out != null) {
out.close();
}
}
}
// in src/org/python/indexer/Util.java
public static String readFile(String filename) throws Exception {
return readFile(new File(filename));
}
// in src/org/python/indexer/Util.java
public static String readFile(File path) throws Exception {
// Don't use line-oriented file read -- need to retain CRLF if present
// so the style-run and link offsets are correct.
return new String(getBytesFromFile(path), UTF_8);
}
// in src/org/python/indexer/Util.java
public static String getMD5(File path) throws Exception {
byte[] bytes = getBytesFromFile(path);
return getMD5(bytes);
}
// in src/org/python/indexer/Util.java
public static String getMD5(byte[] fileContents) throws Exception {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(fileContents);
byte messageDigest[] = algorithm.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < messageDigest.length; i++) {
sb.append(String.format("%02x", 0xFF & messageDigest[i]));
}
return sb.toString();
}
// in src/org/python/indexer/AstCache.java
public static AstCache get() throws Exception {
if (INSTANCE == null) {
INSTANCE = new AstCache();
}
return INSTANCE;
}
// in src/org/python/indexer/AstCache.java
public NModule getAST(String path) throws Exception {
if (path == null) throw new IllegalArgumentException("null path");
return fetch(path);
}
// in src/org/python/indexer/AstCache.java
public NModule getAST(String path, String contents) throws Exception {
if (path == null) throw new IllegalArgumentException("null path");
if (contents == null) throw new IllegalArgumentException("null contents");
// Cache stores null value if the parse failed.
if (cache.containsKey(path)) {
return cache.get(path);
}
NModule mod = null;
try {
mod = parse(path, contents);
if (mod != null) {
mod.setFileAndMD5(path, Util.getMD5(contents.getBytes("UTF-8")));
}
} finally {
cache.put(path, mod); // may be null
}
return mod;
}
// in src/org/python/indexer/AstCache.java
private NModule fetch(String path) throws Exception {
// Cache stores null value if the parse failed.
if (cache.containsKey(path)) {
return cache.get(path);
}
// Might be cached on disk but not in memory.
NModule mod = getSerializedModule(path);
if (mod != null) {
fine("reusing " + path);
cache.put(path, mod);
return mod;
}
try {
mod = parse(path);
} finally {
cache.put(path, mod); // may be null
}
if (mod != null) {
serialize(mod);
}
return mod;
}
// in src/org/python/indexer/AstCache.java
private NModule parse(String path) throws Exception {
fine("parsing " + path);
mod ast = invokeANTLR(path);
return generateAST(ast, path);
}
// in src/org/python/indexer/AstCache.java
private NModule parse(String path, String contents) throws Exception {
fine("parsing " + path);
mod ast = invokeANTLR(path, contents);
return generateAST(ast, path);
}
// in src/org/python/indexer/AstCache.java
private NModule generateAST(mod ast, String path) throws Exception {
if (ast == null) {
Indexer.idx.reportFailedAssertion("ANTLR returned NULL for " + path);
return null;
}
// Convert to indexer's AST. Type conversion warnings are harmless here.
@SuppressWarnings("unchecked")
Object obj = ast.accept(new AstConverter());
if (!(obj instanceof NModule)) {
warn("\n[warning] converted AST is not a module: " + obj);
return null;
}
NModule module = (NModule)obj;
if (new File(path).canRead()) {
module.setFile(path);
}
return module;
}
// in src/org/python/indexer/AstCache.java
public String getCachePath(File sourcePath) throws Exception {
return getCachePath(Util.getMD5(sourcePath), sourcePath.getName());
}
// in src/org/python/indexer/AstCache.java
void serialize(NModule ast) throws Exception {
String path = getCachePath(ast.getMD5(), new File(ast.getFile()).getName());
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
oos = new ObjectOutputStream(fos);
oos.writeObject(ast);
} finally {
if (oos != null) {
oos.close();
} else if (fos != null) {
fos.close();
}
}
}
// in src/org/python/indexer/AstCache.java
NModule deserialize(File sourcePath) throws Exception {
String cachePath = getCachePath(sourcePath);
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(cachePath);
ois = new ObjectInputStream(fis);
NModule mod = (NModule)ois.readObject();
// Files in different dirs may have the same base name and contents.
mod.setFile(sourcePath);
return mod;
} finally {
if (ois != null) {
ois.close();
} else if (fis != null) {
fis.close();
}
}
}
// in src/org/python/indexer/demos/HtmlDemo.java
private void makeOutputDir() throws Exception {
if (!OUTPUT_DIR.exists()) {
OUTPUT_DIR.mkdirs();
info("created directory: " + OUTPUT_DIR.getAbsolutePath());
}
}
// in src/org/python/indexer/demos/HtmlDemo.java
private void start(File stdlib, File fileOrDir) throws Exception {
rootDir = fileOrDir.isFile() ? fileOrDir.getParentFile() : fileOrDir;
rootPath = rootDir.getCanonicalPath();
indexer = new Indexer();
indexer.addPath(stdlib.getCanonicalPath());
info("building index...");
indexer.loadFileRecursive(fileOrDir.getCanonicalPath());
indexer.ready();
info(indexer.getStatusReport());
generateHtml();
}
// in src/org/python/indexer/demos/HtmlDemo.java
private void generateHtml() throws Exception {
info("generating html...");
makeOutputDir();
linker = new Linker(rootPath, OUTPUT_DIR);
linker.findLinks(indexer);
int rootLength = rootPath.length();
for (String path : indexer.getLoadedFiles()) {
if (!path.startsWith(rootPath)) {
continue;
}
File destFile = Util.joinPath(OUTPUT_DIR, path.substring(rootLength));
destFile.getParentFile().mkdirs();
String destPath = destFile.getAbsolutePath() + ".html";
String html = markup(path);
Util.writeFile(destPath, html);
}
info("wrote " + indexer.getLoadedFiles().size() + " files to " + OUTPUT_DIR);
}
// in src/org/python/indexer/demos/HtmlDemo.java
private String markup(String path) throws Exception {
String source = Util.readFile(path);
List<StyleRun> styles = new Styler(indexer, linker).addStyles(path, source);
styles.addAll(linker.getStyles(path));
source = new StyleApplier(path, source, styles).apply();
String outline = new HtmlOutline(indexer).generate(path);
return "<html><head title=\"" + path + "\">"
+ "<style type='text/css'>\n" + CSS + "</style>\n"
+ "</head>\n<body>\n"
+ "<table width=100% border='1px solid gray'><tr><td valign='top'>"
+ outline
+ "</td><td>"
+ "<pre>" + addLineNumbers(source) + "</pre>"
+ "</td></tr></table></body></html>";
}
// in src/org/python/indexer/demos/HtmlDemo.java
public static void main(String[] args) throws Exception {
if (args.length != 2) {
usage();
}
File fileOrDir = checkFile(args[1]);
File stdlib = checkFile(args[0]);
if (!stdlib.isDirectory()) {
abort("Not a directory: " + stdlib);
}
new HtmlDemo().start(stdlib, fileOrDir);
}
// in src/org/python/indexer/demos/HtmlOutline.java
public String generate(String path) throws Exception {
buffer = new StringBuilder(1024);
List<Outliner.Entry> entries = indexer.generateOutline(path);
addOutline(entries);
String html = buffer.toString();
buffer = null;
return html;
}
// in src/org/python/indexer/demos/Styler.java
public List<StyleRun> addStyles(String path, String src) throws Exception {
this.path = path;
source = src;
NModule m = indexer.getAstForFile(path);
if (m != null) {
m.visit(this);
highlightLexicalTokens();
}
return styles;
}
// in src/org/python/compiler/ProxyMaker.java
public void doConstants() throws Exception {
Code code = classfile.addMethod("<clinit>", makeSig("V"), Modifier.STATIC);
code.return_();
}
// in src/org/python/compiler/ProxyMaker.java
public static void doReturn(Code code, Class<?> type) throws Exception {
switch (getType(type)) {
case tNone:
break;
case tCharacter:
case tBoolean:
case tByte:
case tShort:
case tInteger:
code.ireturn();
break;
case tLong:
code.lreturn();
break;
case tFloat:
code.freturn();
break;
case tDouble:
code.dreturn();
break;
case tVoid:
code.return_();
break;
default:
code.areturn();
break;
}
}
// in src/org/python/compiler/ProxyMaker.java
public static void doNullReturn(Code code, Class<?> type) throws Exception {
switch (getType(type)) {
case tNone:
break;
case tCharacter:
case tBoolean:
case tByte:
case tShort:
case tInteger:
code.iconst_0();
code.ireturn();
break;
case tLong:
code.lconst_0();
code.lreturn();
break;
case tFloat:
code.fconst_0();
code.freturn();
break;
case tDouble:
code.dconst_0();
code.dreturn();
break;
case tVoid:
code.return_();
break;
default:
code.aconst_null();
code.areturn();
break;
}
}
// in src/org/python/compiler/ProxyMaker.java
public void callSuper(Code code,
String name,
String superclass,
Class<?>[] parameters,
Class<?> ret,
String sig) throws Exception {
code.aload(0);
int local_index;
int i;
for (i=0, local_index=1; i<parameters.length; i++) {
switch(getType(parameters[i])) {
case tCharacter:
case tBoolean:
case tByte:
case tShort:
case tInteger:
code.iload(local_index);
local_index += 1;
break;
case tLong:
code.lload(local_index);
local_index += 2;
break;
case tFloat:
code.fload(local_index);
local_index += 1;
break;
case tDouble:
code.dload(local_index);
local_index += 2;
break;
default:
code.aload(local_index);
local_index += 1;
break;
}
}
code.invokespecial(superclass, name, sig);
doReturn(code, ret);
}
// in src/org/python/compiler/ProxyMaker.java
public void doJavaCall(Code code, String name, String type,
String jcallName)
throws Exception
{
code.invokevirtual("org/python/core/PyObject", jcallName, makeSig($pyObj, $objArr));
code.invokestatic("org/python/core/Py", "py2"+name, makeSig(type, $pyObj));
}
// in src/org/python/compiler/ProxyMaker.java
public void getArgs(Code code, Class<?>[] parameters) throws Exception {
if (parameters.length == 0) {
code.getstatic("org/python/core/Py", "EmptyObjects", $pyObjArr);
} else {
code.iconst(parameters.length);
code.anewarray("java/lang/Object");
int array = code.getLocal("[org/python/core/PyObject");
code.astore(array);
int local_index;
int i;
for (i=0, local_index=1; i<parameters.length; i++) {
code.aload(array);
code.iconst(i);
switch (getType(parameters[i])) {
case tBoolean:
case tByte:
case tShort:
case tInteger:
code.iload(local_index);
local_index += 1;
code.invokestatic("org/python/core/Py", "newInteger", "(I)" + $pyInteger);
break;
case tLong:
code.lload(local_index);
local_index += 2;
code.invokestatic("org/python/core/Py", "newInteger", "(J)" + $pyObj);
break;
case tFloat:
code.fload(local_index);
local_index += 1;
code.invokestatic("org/python/core/Py", "newFloat", "(F)" + $pyFloat);
break;
case tDouble:
code.dload(local_index);
local_index += 2;
code.invokestatic("org/python/core/Py", "newFloat", "(D)" + $pyFloat);
break;
case tCharacter:
code.iload(local_index);
local_index += 1;
code.invokestatic("org/python/core/Py", "newString", "(C)" + $pyStr);
break;
default:
code.aload(local_index);
local_index += 1;
break;
}
code.aastore();
}
code.aload(array);
}
}
// in src/org/python/compiler/ProxyMaker.java
public void callMethod(Code code,
String name,
Class<?>[] parameters,
Class<?> ret,
Class<?>[] exceptions) throws Exception {
Label start = null;
Label end = null;
String jcallName = "_jcall";
int instLocal = 0;
if (exceptions.length > 0) {
start = new Label();
end = new Label();
jcallName = "_jcallexc";
instLocal = code.getLocal("org/python/core/PyObject");
code.astore(instLocal);
code.label(start);
code.aload(instLocal);
}
getArgs(code, parameters);
switch (getType(ret)) {
case tCharacter:
doJavaCall(code, "char", "C", jcallName);
break;
case tBoolean:
doJavaCall(code, "boolean", "Z", jcallName);
break;
case tByte:
case tShort:
case tInteger:
doJavaCall(code, "int", "I", jcallName);
break;
case tLong:
doJavaCall(code, "long", "J", jcallName);
break;
case tFloat:
doJavaCall(code, "float", "F", jcallName);
break;
case tDouble:
doJavaCall(code, "double", "D", jcallName);
break;
case tVoid:
doJavaCall(code, "void", "V", jcallName);
break;
default:
code.invokevirtual("org/python/core/PyObject", jcallName, makeSig($pyObj, $objArr));
code.ldc(ret.getName());
code.invokestatic("java/lang/Class","forName", makeSig($clss, $str));
code.invokestatic("org/python/core/Py", "tojava", makeSig($obj, $pyObj, $clss));
// I guess I need this checkcast to keep the verifier happy
code.checkcast(mapClass(ret));
break;
}
if (end != null) {
code.label(end);
}
doReturn(code, ret);
if (exceptions.length > 0) {
boolean throwableFound = false;
Label handlerStart = null;
for (Class<?> exception : exceptions) {
handlerStart = new Label();
code.label(handlerStart);
int excLocal = code.getLocal("java/lang/Throwable");
code.astore(excLocal);
code.aload(excLocal);
code.athrow();
code.visitTryCatchBlock(start, end, handlerStart, mapClass(exception));
doNullReturn(code, ret);
code.freeLocal(excLocal);
if (exception == Throwable.class)
throwableFound = true;
}
if (!throwableFound) {
// The final catch (Throwable)
handlerStart = new Label();
code.label(handlerStart);
int excLocal = code.getLocal("java/lang/Throwable");
code.astore(excLocal);
code.aload(instLocal);
code.aload(excLocal);
code.invokevirtual("org/python/core/PyObject", "_jthrow", makeSig("V", $throwable));
code.visitTryCatchBlock(start, end, handlerStart, "java/lang/Throwable");
code.freeLocal(excLocal);
doNullReturn(code, ret);
}
code.freeLocal(instLocal);
}
}
// in src/org/python/compiler/ProxyMaker.java
public void addMethod(Method method, int access) throws Exception {
boolean isAbstract = false;
if (Modifier.isAbstract(access)) {
access = access & ~Modifier.ABSTRACT;
isAbstract = true;
}
Class<?>[] parameters = method.getParameterTypes();
Class<?> ret = method.getReturnType();
String sig = makeSig(ret, parameters);
String name = method.getName();
names.add(name);
Code code = classfile.addMethod(name, sig, access);
code.aload(0);
code.ldc(name);
if (!isAbstract) {
int tmp = code.getLocal("org/python/core/PyObject");
code.invokestatic("org/python/compiler/ProxyMaker", "findPython",
makeSig($pyObj, $pyProxy, $str));
code.astore(tmp);
code.aload(tmp);
Label callPython = new Label();
code.ifnonnull(callPython);
String superClass = mapClass(method.getDeclaringClass());
callSuper(code, name, superClass, parameters, ret, sig);
code.label(callPython);
code.aload(tmp);
callMethod(code, name, parameters, ret, method.getExceptionTypes());
addSuperMethod("super__"+name, name, superClass, parameters,
ret, sig, access);
} else {
code.invokestatic("org/python/compiler/ProxyMaker", "findPython",
makeSig($pyObj, $pyProxy, $str));
code.dup();
Label returnNull = new Label();
code.ifnull(returnNull);
callMethod(code, name, parameters, ret, method.getExceptionTypes());
code.label(returnNull);
code.pop();
doNullReturn(code, ret);
}
}
// in src/org/python/compiler/ProxyMaker.java
protected void addMethods(Class<?> c, Set<String> t) throws Exception {
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
if (!t.add(methodString(method))) {
continue;
}
int access = method.getModifiers();
if (Modifier.isStatic(access) || Modifier.isPrivate(access)) {
continue;
}
if (Modifier.isNative(access)) {
access = access & ~Modifier.NATIVE;
}
if (Modifier.isProtected(access)) {
access = (access & ~Modifier.PROTECTED) | Modifier.PUBLIC;
if (Modifier.isFinal(access)) {
addSuperMethod(method, access);
continue;
}
} else if (Modifier.isFinal(access)) {
continue;
} else if (!Modifier.isPublic(access)) {
continue; // package protected by process of elimination; we can't override
}
addMethod(method, access);
}
Class<?> sc = c.getSuperclass();
if (sc != null) {
addMethods(sc, t);
}
for (Class<?> iface : c.getInterfaces()) {
addMethods(iface, t);
}
}
// in src/org/python/compiler/ProxyMaker.java
public void addConstructor(String name,
Class<?>[] parameters,
Class<?> ret,
String sig,
int access) throws Exception {
Code code = classfile.addMethod("<init>", sig, access);
callSuper(code, "<init>", name, parameters, Void.TYPE, sig);
}
// in src/org/python/compiler/ProxyMaker.java
public void addConstructors(Class<?> c) throws Exception {
Constructor<?>[] constructors = c.getDeclaredConstructors();
String name = mapClass(c);
for (Constructor<?> constructor : constructors) {
int access = constructor.getModifiers();
if (Modifier.isPrivate(access)) {
continue;
}
if (Modifier.isNative(access)) {
access = access & ~Modifier.NATIVE;
}
if (Modifier.isProtected(access)) {
access = access & ~Modifier.PROTECTED | Modifier.PUBLIC;
}
Class<?>[] parameters = constructor.getParameterTypes();
addConstructor(name, parameters, Void.TYPE, makeSig(Void.TYPE, parameters), access);
}
}
// in src/org/python/compiler/ProxyMaker.java
public void addSuperMethod(Method method, int access) throws Exception {
Class<?>[] parameters = method.getParameterTypes();
Class<?> ret = method.getReturnType();
String superClass = mapClass(method.getDeclaringClass());
String superName = method.getName();
String methodName = superName;
if (Modifier.isFinal(access)) {
methodName = "super__" + superName;
access &= ~Modifier.FINAL;
}
addSuperMethod(methodName, superName, superClass, parameters,
ret, makeSig(ret, parameters), access);
}
// in src/org/python/compiler/ProxyMaker.java
public void addSuperMethod(String methodName,
String superName,
String declClass,
Class<?>[] parameters,
Class<?> ret,
String sig,
int access) throws Exception {
if (methodName.startsWith("super__")) {
/* rationale: JC java-class, P proxy-class subclassing JC
in order to avoid infinite recursion P should define super__foo
only if no class between P and JC in the hierarchy defines
it yet; this means that the python class needing P is the
first that redefines the JC method foo.
*/
try {
superclass.getMethod(methodName, parameters);
return;
} catch (NoSuchMethodException e) {
// OK, no one else defines it, so we need to
} catch (SecurityException e) {
return;
}
}
supernames.add(methodName);
Code code = classfile.addMethod(methodName, sig, access);
callSuper(code, superName, declClass, parameters, ret, sig);
}
// in src/org/python/compiler/ProxyMaker.java
public void addProxy() throws Exception {
// implement PyProxy interface
classfile.addField("__proxy", $pyObj, Modifier.PROTECTED);
// setProxy methods
Code code = classfile.addMethod("_setPyInstance", makeSig("V", $pyObj), Modifier.PUBLIC);
code.aload(0);
code.aload(1);
code.putfield(classfile.name, "__proxy", $pyObj);
code.return_();
// getProxy method
code = classfile.addMethod("_getPyInstance", makeSig($pyObj), Modifier.PUBLIC);
code.aload(0);
code.getfield(classfile.name, "__proxy", $pyObj);
code.areturn();
String pySys = "Lorg/python/core/PySystemState;";
// implement PyProxy interface
classfile.addField("__systemState", pySys, Modifier.PROTECTED | Modifier.TRANSIENT);
// setProxy method
code = classfile.addMethod("_setPySystemState",
makeSig("V", pySys),
Modifier.PUBLIC);
code.aload(0);
code.aload(1);
code.putfield(classfile.name, "__systemState", pySys);
code.return_();
// getProxy method
code = classfile.addMethod("_getPySystemState", makeSig(pySys), Modifier.PUBLIC);
code.aload(0);
code.getfield(classfile.name, "__systemState", pySys);
code.areturn();
}
// in src/org/python/compiler/ProxyMaker.java
public void addClassDictInit() throws Exception {
// classDictInit method
classfile.addInterface(mapClass(org.python.core.ClassDictInit.class));
Code code = classfile.addMethod("classDictInit", makeSig("V", $pyObj),
Modifier.PUBLIC | Modifier.STATIC);
code.aload(0);
code.ldc("__supernames__");
int strArray = CodeCompiler.makeStrings(code, supernames);
code.aload(strArray);
code.freeLocal(strArray);
code.invokestatic("org/python/core/Py", "java2py", makeSig($pyObj, $obj));
code.invokevirtual("org/python/core/PyObject", "__setitem__", makeSig("V", $str, $pyObj));
code.return_();
}
// in src/org/python/compiler/ProxyMaker.java
public void build(OutputStream out) throws Exception {
build();
classfile.write(out);
}
// in src/org/python/compiler/ProxyMaker.java
public void build() throws Exception {
names = Generic.set();
int access = superclass.getModifiers();
if ((access & Modifier.FINAL) != 0) {
throw new InstantiationException("can't subclass final class");
}
access = Modifier.PUBLIC | Modifier.SYNCHRONIZED;
classfile = new ClassFile(myClass, mapClass(superclass), access);
addProxy();
addConstructors(superclass);
classfile.addInterface("org/python/core/PyProxy");
Set<String> seenmethods = Generic.set();
addMethods(superclass, seenmethods);
for (Class<?> iface : interfaces) {
if (iface.isAssignableFrom(superclass)) {
Py.writeWarning("compiler", "discarding redundant interface: " + iface.getName());
continue;
}
classfile.addInterface(mapClass(iface));
addMethods(iface, seenmethods);
}
doConstants();
addClassDictInit();
}
// in src/org/python/compiler/AdapterMaker.java
Override
public void build() throws Exception {
names = Generic.set();
int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNCHRONIZED;
classfile = new ClassFile(myClass, "java/lang/Object", access);
classfile.addInterface(mapClass(interfaces[0]));
addMethods(interfaces[0], new HashSet<String>());
addConstructors(Object.class);
doConstants();
}
// in src/org/python/compiler/AdapterMaker.java
Override
public void doConstants() throws Exception {
for (String name : names) {
classfile.addField(name, $pyObj, Opcodes.ACC_PUBLIC);
}
}
// in src/org/python/compiler/AdapterMaker.java
Override
public void addMethod(Method method, int access) throws Exception {
Class<?>[] parameters = method.getParameterTypes();
Class<?> ret = method.getReturnType();
String name = method.getName();
names.add(name);
Code code = classfile.addMethod(name, makeSig(ret, parameters), Opcodes.ACC_PUBLIC);
code.aload(0);
code.getfield(classfile.name, name, $pyObj);
code.dup();
Label returnNull = new Label();
code.ifnull(returnNull);
callMethod(code, name, parameters, ret, method.getExceptionTypes());
code.label(returnNull);
doNullReturn(code, ret);
}
// in src/org/python/compiler/ScopeInfo.java
public void cook(ScopeInfo up, int distance, CompilationContext ctxt) throws Exception {
if(up == null)
return; // top level => nop
this.up = up;
this.distance = distance;
boolean func = kind == FUNCSCOPE;
Vector<String> purecells = new Vector<String>();
cell = 0;
boolean some_inner_free = inner_free.size() > 0;
for (Enumeration e = inner_free.keys(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
SymInfo info = tbl.get(name);
if (info == null) {
tbl.put(name,new SymInfo(FREE));
continue;
}
int flags = info.flags;
if (func) {
// not func global and bound ?
if ((flags&NGLOBAL) == 0 && (flags&BOUND) != 0) {
info.flags |= CELL;
if ((info.flags&PARAM) != 0)
jy_paramcells.addElement(name);
cellvars.addElement(name);
info.env_index = cell++;
if ((flags&PARAM) == 0) purecells.addElement(name);
continue;
}
} else {
info.flags |= FREE;
}
}
boolean some_free = false;
boolean nested = up.kind != TOPSCOPE;
for (Map.Entry<String, SymInfo> entry : tbl.entrySet()) {
String name = entry.getKey();
SymInfo info = entry.getValue();
int flags = info.flags;
if (nested && (flags&FREE) != 0) up.inner_free.put(name,PRESENT);
if ((flags&(GLOBAL|PARAM|CELL)) == 0) {
if ((flags&BOUND) != 0) { // ?? only func
// System.err.println("local: "+name);
names.addElement(name);
info.locals_index = local++;
continue;
}
info.flags |= FREE;
some_free = true;
if (nested) up.inner_free.put(name,PRESENT);
}
}
if ((jy_npurecell = purecells.size()) > 0) {
int sz = purecells.size();
for (int i = 0; i < sz; i++) {
names.addElement(purecells.elementAt(i));
}
}
if (some_free && nested) {
up.contains_ns_free_vars = true;
}
// XXX - this doesn't catch all cases - may depend subtly
// on how visiting NOW works with antlr compared to javacc
if ((unqual_exec || from_import_star)) {
if(some_inner_free) dynastuff_trouble(true, ctxt);
else if(func_level > 1 && some_free)
dynastuff_trouble(false, ctxt);
}
}
// in src/org/python/compiler/ScopeInfo.java
private void dynastuff_trouble(boolean inner_free, CompilationContext ctxt) throws Exception {
StringBuilder illegal = new StringBuilder();
if (unqual_exec && from_import_star) {
illegal.append("function '")
.append(scope_name)
.append("' uses import * and bare exec, which are illegal");
} else if (unqual_exec) {
illegal.append("unqualified exec is not allowed in function '")
.append(scope_name)
.append("'");
} else {
illegal.append("import * is not allowed in function '").append(scope_name).append("'");
}
if (inner_free) {
illegal.append(" because it contains a function with free variables");
} else {
illegal.append(" because it contains free variables");
}
ctxt.error(illegal.toString(), true, scope_node);
}
// in src/org/python/compiler/Future.java
private boolean check(ImportFrom cand) throws Exception {
if (!cand.getInternalModule().equals(FutureFeature.MODULE_NAME))
return false;
if (cand.getInternalNames().isEmpty()) {
throw new ParseException(
"future statement does not support import *", cand);
}
try {
for (alias feature : cand.getInternalNames()) {
// *known* features
FutureFeature.addFeature(feature.getInternalName(), features);
}
} catch (ParseException pe) {
throw new ParseException(pe.getMessage(), cand);
}
return true;
}
// in src/org/python/compiler/Future.java
public void preprocessFutures(mod node, org.python.core.CompilerFlags cflags)
throws Exception {
if (cflags != null) {
if (cflags.isFlagSet(CodeFlag.CO_FUTURE_DIVISION))
FutureFeature.division.addTo(features);
if (cflags.isFlagSet(CodeFlag.CO_FUTURE_WITH_STATEMENT))
FutureFeature.with_statement.addTo(features);
if (cflags.isFlagSet(CodeFlag.CO_FUTURE_ABSOLUTE_IMPORT))
FutureFeature.absolute_import.addTo(features);
}
int beg = 0;
List<stmt> suite = null;
if (node instanceof Module) {
suite = ((Module) node).getInternalBody();
if (suite.size() > 0 && suite.get(0) instanceof Expr
&& ((Expr) suite.get(0)).getInternalValue() instanceof Str) {
beg++;
}
} else if (node instanceof Interactive) {
suite = ((Interactive) node).getInternalBody();
} else {
return;
}
for (int i = beg; i < suite.size(); i++) {
stmt s = suite.get(i);
if (!(s instanceof ImportFrom)) break;
s.from_future_checked = true;
if (!check((ImportFrom) s)) break;
}
if (cflags != null) {
for (FutureFeature feature : featureSet) {
feature.setFlag(cflags);
}
}
}
// in src/org/python/compiler/Future.java
public static void checkFromFuture(ImportFrom node) throws Exception {
if (node.from_future_checked) return;
if (node.getInternalModule().equals(FutureFeature.MODULE_NAME)) {
throw new ParseException("from __future__ imports must occur "
+ "at the beginning of the file", node);
}
node.from_future_checked = true;
}
// in src/org/python/compiler/CodeCompiler.java
public void loadFrame() throws Exception {
code.aload(1);
}
// in src/org/python/compiler/CodeCompiler.java
public void loadThreadState() throws Exception {
code.aload(2);
}
// in src/org/python/compiler/CodeCompiler.java
public void setLastI(int idx) throws Exception {
loadFrame();
code.iconst(idx);
code.putfield(p(PyFrame.class), "f_lasti", "I");
}
// in src/org/python/compiler/CodeCompiler.java
private void loadf_back() throws Exception {
code.getfield(p(PyFrame.class), "f_back", ci(PyFrame.class));
}
// in src/org/python/compiler/CodeCompiler.java
public int storeTop() throws Exception {
int tmp = code.getLocal(p(PyObject.class));
code.astore(tmp);
return tmp;
}
// in src/org/python/compiler/CodeCompiler.java
public void setline(int line) throws Exception {
if (module.linenumbers) {
code.setline(line);
loadFrame();
code.iconst(line);
code.invokevirtual(p(PyFrame.class), "setline", sig(Void.TYPE, Integer.TYPE));
}
}
// in src/org/python/compiler/CodeCompiler.java
public void setline(PythonTree node) throws Exception {
setline(node.getLine());
}
// in src/org/python/compiler/CodeCompiler.java
public void set(PythonTree node) throws Exception {
int tmp = storeTop();
set(node, tmp);
code.aconst_null();
code.astore(tmp);
code.freeLocal(tmp);
}
// in src/org/python/compiler/CodeCompiler.java
public void set(PythonTree node, int tmp) throws Exception {
temporary = tmp;
visit(node);
}
// in src/org/python/compiler/CodeCompiler.java
private void saveAugTmps(PythonTree node, int count) throws Exception {
if (count >= 4) {
augtmp4 = code.getLocal(ci(PyObject.class));
code.astore(augtmp4);
}
if (count >= 3) {
augtmp3 = code.getLocal(ci(PyObject.class));
code.astore(augtmp3);
}
if (count >= 2) {
augtmp2 = code.getLocal(ci(PyObject.class));
code.astore(augtmp2);
}
augtmp1 = code.getLocal(ci(PyObject.class));
code.astore(augtmp1);
code.aload(augtmp1);
if (count >= 2) {
code.aload(augtmp2);
}
if (count >= 3) {
code.aload(augtmp3);
}
if (count >= 4) {
code.aload(augtmp4);
}
}
// in src/org/python/compiler/CodeCompiler.java
private void restoreAugTmps(PythonTree node, int count) throws Exception {
code.aload(augtmp1);
code.freeLocal(augtmp1);
if (count == 1) {
return;
}
code.aload(augtmp2);
code.freeLocal(augtmp2);
if (count == 2) {
return;
}
code.aload(augtmp3);
code.freeLocal(augtmp3);
if (count == 3) {
return;
}
code.aload(augtmp4);
code.freeLocal(augtmp4);
}
// in src/org/python/compiler/CodeCompiler.java
void parse(mod node, Code code, boolean fast_locals, String className, Str classDoc,
boolean classBody, ScopeInfo scope, CompilerFlags cflags) throws Exception {
this.fast_locals = fast_locals;
this.className = className;
this.code = code;
this.cflags = cflags;
this.my_scope = scope;
this.tbl = scope.tbl;
//BEGIN preparse
if (classBody) {
// Set the class's __module__ to __name__. fails when there's no __name__
loadFrame();
code.ldc("__module__");
loadFrame();
code.ldc("__name__");
code.invokevirtual(p(PyFrame.class), "getname", sig(PyObject.class, String.class));
code.invokevirtual(p(PyFrame.class), "setlocal", sig(Void.TYPE, String.class,
PyObject.class));
if (classDoc != null) {
loadFrame();
code.ldc("__doc__");
visit(classDoc);
code.invokevirtual(p(PyFrame.class), "setlocal", sig(Void.TYPE, String.class,
PyObject.class));
}
}
Label genswitch = new Label();
if (my_scope.generator) {
code.goto_(genswitch);
}
Label start = new Label();
code.label(start);
int nparamcell = my_scope.jy_paramcells.size();
if (nparamcell > 0) {
java.util.List<String> paramcells = my_scope.jy_paramcells;
for (int i = 0; i < nparamcell; i++) {
code.aload(1);
SymInfo syminf = tbl.get(paramcells.get(i));
code.iconst(syminf.locals_index);
code.iconst(syminf.env_index);
code.invokevirtual(p(PyFrame.class), "to_cell", sig(Void.TYPE, Integer.TYPE,
Integer.TYPE));
}
}
//END preparse
optimizeGlobals = checkOptimizeGlobals(fast_locals, my_scope);
if (my_scope.max_with_count > 0) {
// allocate for all the with-exits we will have in the frame;
// this allows yield and with to happily co-exist
loadFrame();
code.iconst(my_scope.max_with_count);
code.anewarray(p(PyObject.class));
code.putfield(p(PyFrame.class), "f_exits", ci(PyObject[].class));
}
Object exit = visit(node);
if (classBody) {
loadFrame();
code.invokevirtual(p(PyFrame.class), "getf_locals", sig(PyObject.class));
code.areturn();
} else {
if (exit == null) {
setLastI(-1);
getNone();
code.areturn();
}
}
//BEGIN postparse
// similar to visitResume code in pyasm.py
if (my_scope.generator) {
code.label(genswitch);
code.aload(1);
code.getfield(p(PyFrame.class), "f_lasti", "I");
Label[] y = new Label[yields.size() + 1];
y[0] = start;
for (int i = 1; i < y.length; i++) {
y[i] = yields.get(i - 1);
}
code.tableswitch(0, y.length - 1, start, y);
}
//END postparse
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitInteractive(Interactive node) throws Exception {
traverse(node);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitModule(org.python.antlr.ast.Module suite) throws Exception {
Str docStr = getDocStr(suite.getInternalBody());
if (docStr != null) {
loadFrame();
code.ldc("__doc__");
visit(docStr);
code.invokevirtual(p(PyFrame.class), "setglobal", sig(Void.TYPE, String.class,
PyObject.class));
}
traverse(suite);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitExpression(Expression node) throws Exception {
if (my_scope.generator && node.getInternalBody() != null) {
module.error("'return' with argument inside generator",
true, node);
}
return visitReturn(new Return(node, node.getInternalBody()), true);
}
// in src/org/python/compiler/CodeCompiler.java
public int makeArray(java.util.List<? extends PythonTree> nodes) throws Exception {
// XXX: This should produce an array on the stack (if possible) instead of a local
// the caller is responsible for freeing.
int n;
if (nodes == null) {
n = 0;
} else {
n = nodes.size();
}
int array = code.getLocal(ci(PyObject[].class));
if (n == 0) {
code.getstatic(p(Py.class), "EmptyObjects", ci(PyObject[].class));
code.astore(array);
} else {
code.iconst(n);
code.anewarray(p(PyObject.class));
code.astore(array);
for (int i = 0; i < n; i++) {
visit(nodes.get(i));
code.aload(array);
code.swap();
code.iconst(i);
code.swap();
code.aastore();
}
}
return array;
}
// in src/org/python/compiler/CodeCompiler.java
public boolean makeClosure(ScopeInfo scope) throws Exception {
if (scope == null || scope.freevars == null) {
return false;
}
int n = scope.freevars.size();
if (n == 0) {
return false;
}
int tmp = code.getLocal(ci(PyObject[].class));
code.iconst(n);
code.anewarray(p(PyObject.class));
code.astore(tmp);
Map<String, SymInfo> upTbl = scope.up.tbl;
for (int i = 0; i < n; i++) {
code.aload(tmp);
code.iconst(i);
loadFrame();
for (int j = 1; j < scope.distance; j++) {
loadf_back();
}
SymInfo symInfo = upTbl.get(scope.freevars.elementAt(i));
code.iconst(symInfo.env_index);
code.invokevirtual(p(PyFrame.class), "getclosure", sig(PyObject.class, Integer.TYPE));
code.aastore();
}
code.aload(tmp);
code.freeLocal(tmp);
return true;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitFunctionDef(FunctionDef node) throws Exception {
String name = getName(node.getInternalName());
setline(node);
ScopeInfo scope = module.getScopeInfo(node);
// NOTE: this is attached to the constructed PyFunction, so it cannot be nulled out
// with freeArray, unlike other usages of makeArray here
int defaults = makeArray(scope.ac.getDefaults());
code.new_(p(PyFunction.class));
code.dup();
loadFrame();
code.getfield(p(PyFrame.class), "f_globals", ci(PyObject.class));
code.aload(defaults);
code.freeLocal(defaults);
scope.setup_closure();
scope.dump();
module.codeConstant(new Suite(node, node.getInternalBody()), name, true,
className, false, false,
node.getLine(), scope, cflags).get(code);
Str docStr = getDocStr(node.getInternalBody());
if (docStr != null) {
visit(docStr);
} else {
code.aconst_null();
}
if (!makeClosure(scope)) {
code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject[].class, PyCode.class, PyObject.class));
} else {
code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject[].class, PyCode.class, PyObject.class, PyObject[].class));
}
applyDecorators(node.getInternalDecorator_list());
set(new Name(node, node.getInternalName(), expr_contextType.Store));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
private void applyDecorators(java.util.List<expr> decorators) throws Exception {
if (decorators != null && !decorators.isEmpty()) {
int res = storeTop();
for (expr decorator : decorators) {
visit(decorator);
stackProduce();
}
for (int i = decorators.size(); i > 0; i--) {
stackConsume();
loadThreadState();
code.aload(res);
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class));
code.astore(res);
}
code.aload(res);
code.freeLocal(res);
}
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitExpr(Expr node) throws Exception {
setline(node);
visit(node.getInternalValue());
if (print_results) {
code.invokestatic(p(Py.class), "printResult", sig(Void.TYPE, PyObject.class));
} else {
code.pop();
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitAssign(Assign node) throws Exception {
setline(node);
visit(node.getInternalValue());
if (node.getInternalTargets().size() == 1) {
set(node.getInternalTargets().get(0));
} else {
int tmp = storeTop();
for (expr target : node.getInternalTargets()) {
set(target, tmp);
}
code.freeLocal(tmp);
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitPrint(Print node) throws Exception {
setline(node);
int tmp = -1;
if (node.getInternalDest() != null) {
visit(node.getInternalDest());
tmp = storeTop();
}
if (node.getInternalValues() == null || node.getInternalValues().size() == 0) {
if (node.getInternalDest() != null) {
code.aload(tmp);
code.invokestatic(p(Py.class), "printlnv", sig(Void.TYPE, PyObject.class));
} else {
code.invokestatic(p(Py.class), "println", sig(Void.TYPE));
}
} else {
for (int i = 0; i < node.getInternalValues().size(); i++) {
if (node.getInternalDest() != null) {
code.aload(tmp);
visit(node.getInternalValues().get(i));
if (node.getInternalNl() && i == node.getInternalValues().size() - 1) {
code.invokestatic(p(Py.class), "println", sig(Void.TYPE, PyObject.class,
PyObject.class));
} else {
code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE, PyObject.class,
PyObject.class));
}
} else {
visit(node.getInternalValues().get(i));
if (node.getInternalNl() && i == node.getInternalValues().size() - 1) {
code.invokestatic(p(Py.class), "println", sig(Void.TYPE, PyObject.class));
} else {
code.invokestatic(p(Py.class), "printComma", sig(Void.TYPE,
PyObject.class));
}
}
}
}
if (node.getInternalDest() != null) {
code.freeLocal(tmp);
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitDelete(Delete node) throws Exception {
setline(node);
traverse(node);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitPass(Pass node) throws Exception {
setline(node);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitBreak(Break node) throws Exception {
//setline(node); Not needed here...
if (breakLabels.empty()) {
throw new ParseException("'break' outside loop", node);
}
doFinallysDownTo(bcfLevel);
code.goto_(breakLabels.peek());
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitContinue(Continue node) throws Exception {
//setline(node); Not needed here...
if (continueLabels.empty()) {
throw new ParseException("'continue' not properly in loop", node);
}
doFinallysDownTo(bcfLevel);
code.goto_(continueLabels.peek());
return Exit;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitYield(Yield node) throws Exception {
setline(node);
if (!fast_locals) {
throw new ParseException("'yield' outside function", node);
}
int stackState = saveStack();
if (node.getInternalValue() != null) {
visit(node.getInternalValue());
} else {
getNone();
}
setLastI(++yield_count);
saveLocals();
code.areturn();
Label restart = new Label();
yields.addElement(restart);
code.label(restart);
restoreLocals();
restoreStack(stackState);
loadFrame();
code.invokevirtual(p(PyFrame.class), "getGeneratorInput", sig(Object.class));
code.dup();
code.instanceof_(p(PyException.class));
Label done2 = new Label();
code.ifeq(done2);
code.checkcast(p(Throwable.class));
code.athrow();
code.label(done2);
code.checkcast(p(PyObject.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
private int saveStack() throws Exception {
if (stack.size() > 0) {
int array = code.getLocal(ci(Object[].class));
code.iconst(stack.size());
code.anewarray(p(Object.class));
code.astore(array);
ListIterator<String> content = stack.listIterator(stack.size());
for (int i = 0; content.hasPrevious(); i++) {
String signature = content.previous();
if (p(ThreadState.class).equals(signature)) {
// Stack: ... threadstate
code.pop();
// Stack: ...
} else {
code.aload(array);
// Stack: |- ... value array
code.swap();
code.iconst(i++);
code.swap();
// Stack: |- ... array index value
code.aastore();
// Stack: |- ...
}
}
return array;
} else {
return -1;
}
}
// in src/org/python/compiler/CodeCompiler.java
private void restoreStack(int array) throws Exception {
if (stack.size() > 0) {
int i = stack.size() - 1;
for (String signature : stack) {
if (p(ThreadState.class).equals(signature)) {
loadThreadState();
} else {
code.aload(array);
// Stack: |- ... array
code.iconst(i--);
code.aaload();
// Stack: |- ... value
code.checkcast(signature);
}
}
code.freeLocal(array);
}
}
// in src/org/python/compiler/CodeCompiler.java
private void restoreLocals() throws Exception {
endExceptionHandlers();
Vector<String> v = code.getActiveLocals();
loadFrame();
code.getfield(p(PyFrame.class), "f_savedlocals", ci(Object[].class));
int locals = code.getLocal(ci(Object[].class));
code.astore(locals);
for (int i = 0; i < v.size(); i++) {
String type = v.elementAt(i);
if (type == null) {
continue;
}
code.aload(locals);
code.iconst(i);
code.aaload();
code.checkcast(type);
code.astore(i);
}
code.freeLocal(locals);
restartExceptionHandlers();
}
// in src/org/python/compiler/CodeCompiler.java
private void saveLocals() throws Exception {
Vector<String> v = code.getActiveLocals();
code.iconst(v.size());
code.anewarray(p(Object.class));
int locals = code.getLocal(ci(Object[].class));
code.astore(locals);
for (int i = 0; i < v.size(); i++) {
String type = v.elementAt(i);
if (type == null) {
continue;
}
code.aload(locals);
code.iconst(i);
//code.checkcast(code.pool.Class(p(Object.class)));
if (i == 2222) {
code.aconst_null();
} else {
code.aload(i);
}
code.aastore();
}
loadFrame();
code.aload(locals);
code.putfield(p(PyFrame.class), "f_savedlocals", ci(Object[].class));
code.freeLocal(locals);
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitReturn(Return node) throws Exception {
return visitReturn(node, false);
}
// in src/org/python/compiler/CodeCompiler.java
public Object visitReturn(Return node, boolean inEval) throws Exception {
setline(node);
if (!inEval && !fast_locals) {
throw new ParseException("'return' outside function", node);
}
int tmp = 0;
if (node.getInternalValue() != null) {
if (my_scope.generator) {
throw new ParseException("'return' with argument " +
"inside generator", node);
}
visit(node.getInternalValue());
tmp = code.getReturnLocal();
code.astore(tmp);
}
doFinallysDownTo(0);
setLastI(-1);
if (node.getInternalValue() != null) {
code.aload(tmp);
} else {
getNone();
}
code.areturn();
return Exit;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitRaise(Raise node) throws Exception {
setline(node);
if (node.getInternalType() != null) {
visit(node.getInternalType());
stackProduce();
}
if (node.getInternalInst() != null) {
visit(node.getInternalInst());
stackProduce();
}
if (node.getInternalTback() != null) {
visit(node.getInternalTback());
stackProduce();
}
if (node.getInternalType() == null) {
code.invokestatic(p(Py.class), "makeException", sig(PyException.class));
} else if (node.getInternalInst() == null) {
stackConsume();
code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class));
} else if (node.getInternalTback() == null) {
stackConsume(2);
code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class,
PyObject.class));
} else {
stackConsume(3);
code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class,
PyObject.class, PyObject.class));
}
code.athrow();
return Exit;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitImport(Import node) throws Exception {
setline(node);
for (alias a : node.getInternalNames()) {
String asname = null;
if (a.getInternalAsname() != null) {
String name = a.getInternalName();
asname = a.getInternalAsname();
code.ldc(name);
loadFrame();
defaultImportLevel();
code.invokestatic(p(imp.class), "importOneAs", sig(PyObject.class, String.class,
PyFrame.class, Integer.TYPE));
} else {
String name = a.getInternalName();
asname = name;
if (asname.indexOf('.') > 0) {
asname = asname.substring(0, asname.indexOf('.'));
}
code.ldc(name);
loadFrame();
defaultImportLevel();
code.invokestatic(p(imp.class), "importOne", sig(PyObject.class, String.class,
PyFrame.class, Integer.TYPE));
}
set(new Name(a, asname, expr_contextType.Store));
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitImportFrom(ImportFrom node) throws Exception {
Future.checkFromFuture(node); // future stmt support
setline(node);
code.ldc(node.getInternalModule());
java.util.List<alias> aliases = node.getInternalNames();
if (aliases == null || aliases.size() == 0) {
throw new ParseException("Internel parser error", node);
} else if (aliases.size() == 1 && aliases.get(0).getInternalName().equals("*")) {
if (node.getInternalLevel() > 0) {
throw new ParseException("'import *' not allowed with 'from .'", node);
}
if (my_scope.func_level > 0) {
module.error("import * only allowed at module level", false, node);
if (my_scope.contains_ns_free_vars) {
module.error("import * is not allowed in function '" +
my_scope.scope_name +
"' because it contains a nested function with free variables",
true, node);
}
}
if (my_scope.func_level > 1) {
module.error("import * is not allowed in function '" +
my_scope.scope_name +
"' because it is a nested function",
true, node);
}
loadFrame();
defaultImportLevel();
code.invokestatic(p(imp.class), "importAll", sig(Void.TYPE, String.class,
PyFrame.class, Integer.TYPE));
} else {
java.util.List<String> fromNames = new ArrayList<String>();//[names.size()];
java.util.List<String> asnames = new ArrayList<String>();//[names.size()];
for (int i = 0; i < aliases.size(); i++) {
fromNames.add(aliases.get(i).getInternalName());
asnames.add(aliases.get(i).getInternalAsname());
if (asnames.get(i) == null) {
asnames.set(i, fromNames.get(i));
}
}
int strArray = makeStrings(code, fromNames);
code.aload(strArray);
code.freeLocal(strArray);
loadFrame();
if (node.getInternalLevel() == 0) {
defaultImportLevel();
} else {
code.iconst(node.getInternalLevel());
}
code.invokestatic(p(imp.class), "importFrom", sig(PyObject[].class, String.class,
String[].class, PyFrame.class, Integer.TYPE));
int tmp = storeTop();
for (int i = 0; i < aliases.size(); i++) {
code.aload(tmp);
code.iconst(i);
code.aaload();
set(new Name(aliases.get(i), asnames.get(i), expr_contextType.Store));
}
code.freeLocal(tmp);
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitGlobal(Global node) throws Exception {
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitExec(Exec node) throws Exception {
setline(node);
visit(node.getInternalBody());
stackProduce();
if (node.getInternalGlobals() != null) {
visit(node.getInternalGlobals());
} else {
code.aconst_null();
}
stackProduce();
if (node.getInternalLocals() != null) {
visit(node.getInternalLocals());
} else {
code.aconst_null();
}
stackProduce();
//do the real work here
stackConsume(3);
code.invokestatic(p(Py.class), "exec", sig(Void.TYPE, PyObject.class, PyObject.class,
PyObject.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitAssert(Assert node) throws Exception {
setline(node);
Label end_of_assert = new Label();
/* First do an if __debug__: */
loadFrame();
emitGetGlobal("__debug__");
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
code.ifeq(end_of_assert);
/* Now do the body of the assert. If PyObject.__nonzero__ is true,
then the assertion succeeded, the message portion should not be
processed. Otherwise, the message will be processed. */
visit(node.getInternalTest());
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
/* If evaluation is false, then branch to end of method */
code.ifne(end_of_assert);
/* Visit the message part of the assertion, or pass Py.None */
if (node.getInternalMsg() != null) {
visit(node.getInternalMsg());
} else {
getNone();
}
/* Push exception type onto stack(AssertionError) */
loadFrame();
emitGetGlobal("AssertionError");
code.swap(); // The type is the first argument, but the message could be a yield
code.invokestatic(p(Py.class), "makeException", sig(PyException.class, PyObject.class,
PyObject.class));
/* Raise assertion error. Only executes this logic if assertion failed */
code.athrow();
/* And finally set the label for the end of it all */
code.label(end_of_assert);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public Object doTest(Label end_of_if, If node, int index)
throws Exception {
Label end_of_suite = new Label();
setline(node.getInternalTest());
visit(node.getInternalTest());
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
code.ifeq(end_of_suite);
Object exit = suite(node.getInternalBody());
if (end_of_if != null && exit == null) {
code.goto_(end_of_if);
}
code.label(end_of_suite);
if (node.getInternalOrelse() != null) {
return suite(node.getInternalOrelse()) != null ? exit : null;
} else {
return null;
}
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitIf(If node) throws Exception {
Label end_of_if = null;
if (node.getInternalOrelse() != null) {
end_of_if = new Label();
}
Object exit = doTest(end_of_if, node, 0);
if (end_of_if != null) {
code.label(end_of_if);
}
return exit;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitIfExp(IfExp node) throws Exception {
setline(node.getInternalTest());
Label end = new Label();
Label end_of_else = new Label();
visit(node.getInternalTest());
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
code.ifeq(end_of_else);
visit(node.getInternalBody());
code.goto_(end);
code.label(end_of_else);
visit(node.getInternalOrelse());
code.label(end);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitWhile(While node) throws Exception {
int savebcf = beginLoop();
Label continue_loop = continueLabels.peek();
Label break_loop = breakLabels.peek();
Label start_loop = new Label();
code.goto_(continue_loop);
code.label(start_loop);
//Do suite
suite(node.getInternalBody());
code.label(continue_loop);
setline(node);
//Do test
visit(node.getInternalTest());
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
code.ifne(start_loop);
finishLoop(savebcf);
if (node.getInternalOrelse() != null) {
//Do else
suite(node.getInternalOrelse());
}
code.label(break_loop);
// Probably need to detect "guaranteed exits"
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitFor(For node) throws Exception {
int savebcf = beginLoop();
Label continue_loop = continueLabels.peek();
Label break_loop = breakLabels.peek();
Label start_loop = new Label();
Label next_loop = new Label();
setline(node);
//parse the list
visit(node.getInternalIter());
int iter_tmp = code.getLocal(p(PyObject.class));
int expr_tmp = code.getLocal(p(PyObject.class));
//set up the loop iterator
code.invokevirtual(p(PyObject.class), "__iter__", sig(PyObject.class));
code.astore(iter_tmp);
//do check at end of loop. Saves one opcode ;-)
code.goto_(next_loop);
code.label(start_loop);
//set iter variable to current entry in list
set(node.getInternalTarget(), expr_tmp);
//evaluate for body
suite(node.getInternalBody());
code.label(continue_loop);
code.label(next_loop);
setline(node);
//get the next element from the list
code.aload(iter_tmp);
code.invokevirtual(p(PyObject.class), "__iternext__", sig(PyObject.class));
code.astore(expr_tmp);
code.aload(expr_tmp);
//if no more elements then fall through
code.ifnonnull(start_loop);
finishLoop(savebcf);
if (node.getInternalOrelse() != null) {
//Do else clause if provided
suite(node.getInternalOrelse());
}
code.label(break_loop);
code.freeLocal(iter_tmp);
code.freeLocal(expr_tmp);
// Probably need to detect "guaranteed exits"
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public void exceptionTest(int exc, Label end_of_exceptions,
TryExcept node, int index)
throws Exception {
for (int i = 0; i < node.getInternalHandlers().size(); i++) {
ExceptHandler handler = (ExceptHandler) node.getInternalHandlers().get(i);
//setline(name);
Label end_of_self = new Label();
if (handler.getInternalType() != null) {
code.aload(exc);
//get specific exception
visit(handler.getInternalType());
code.invokevirtual(p(PyException.class), "match", sig(Boolean.TYPE,
PyObject.class));
code.ifeq(end_of_self);
} else {
if (i != node.getInternalHandlers().size() - 1) {
throw new ParseException(
"default 'except:' must be last", handler);
}
}
if (handler.getInternalName() != null) {
code.aload(exc);
code.getfield(p(PyException.class), "value", ci(PyObject.class));
set(handler.getInternalName());
}
//do exception body
suite(handler.getInternalBody());
code.goto_(end_of_exceptions);
code.label(end_of_self);
}
code.aload(exc);
code.athrow();
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitTryFinally(TryFinally node) throws Exception {
Label start = new Label();
Label end = new Label();
Label handlerStart = new Label();
Label finallyEnd = new Label();
Object ret;
ExceptionHandler inFinally = new ExceptionHandler(node);
// Do protected suite
exceptionHandlers.push(inFinally);
int excLocal = code.getLocal(p(Throwable.class));
code.aconst_null();
code.astore(excLocal);
code.label(start);
inFinally.exceptionStarts.addElement(start);
ret = suite(node.getInternalBody());
code.label(end);
inFinally.exceptionEnds.addElement(end);
inFinally.bodyDone = true;
exceptionHandlers.pop();
if (ret == NoExit) {
inlineFinally(inFinally);
code.goto_(finallyEnd);
}
// Handle any exceptions that get thrown in suite
code.label(handlerStart);
code.astore(excLocal);
code.aload(excLocal);
loadFrame();
code.invokestatic(p(Py.class), "addTraceback",
sig(Void.TYPE, Throwable.class, PyFrame.class));
inlineFinally(inFinally);
code.aload(excLocal);
code.checkcast(p(Throwable.class));
code.athrow();
code.label(finallyEnd);
code.freeLocal(excLocal);
inFinally.addExceptionHandlers(handlerStart);
// According to any JVM verifiers, this code block might not return
return null;
}
// in src/org/python/compiler/CodeCompiler.java
private void inlineFinally(ExceptionHandler handler) throws Exception {
if (!handler.bodyDone) {
// end the previous exception block so inlined finally code doesn't
// get covered by our exception handler.
Label end = new Label();
code.label(end);
handler.exceptionEnds.addElement(end);
// also exiting the try: portion of this particular finally
}
if (handler.isFinallyHandler()) {
handler.finalBody(this);
}
}
// in src/org/python/compiler/CodeCompiler.java
private void reenterProtectedBody(ExceptionHandler handler) throws Exception {
// restart exception coverage
Label restart = new Label();
code.label(restart);
handler.exceptionStarts.addElement(restart);
}
// in src/org/python/compiler/CodeCompiler.java
private void doFinallysDownTo(int level) throws Exception {
Stack<ExceptionHandler> poppedHandlers = new Stack<ExceptionHandler>();
while (exceptionHandlers.size() > level) {
ExceptionHandler handler = exceptionHandlers.pop();
inlineFinally(handler);
poppedHandlers.push(handler);
}
while (poppedHandlers.size() > 0) {
ExceptionHandler handler = poppedHandlers.pop();
reenterProtectedBody(handler);
exceptionHandlers.push(handler);
}
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitTryExcept(TryExcept node) throws Exception {
Label start = new Label();
Label end = new Label();
Label handler_start = new Label();
Label handler_end = new Label();
ExceptionHandler handler = new ExceptionHandler();
code.label(start);
handler.exceptionStarts.addElement(start);
exceptionHandlers.push(handler);
//Do suite
Object exit = suite(node.getInternalBody());
exceptionHandlers.pop();
code.label(end);
handler.exceptionEnds.addElement(end);
if (exit == null) {
code.goto_(handler_end);
}
code.label(handler_start);
loadFrame();
code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class,
PyFrame.class));
int exc = code.getFinallyLocal(p(Throwable.class));
code.astore(exc);
if (node.getInternalOrelse() == null) {
//No else clause to worry about
exceptionTest(exc, handler_end, node, 1);
code.label(handler_end);
} else {
//Have else clause
Label else_end = new Label();
exceptionTest(exc, else_end, node, 1);
code.label(handler_end);
//do else clause
suite(node.getInternalOrelse());
code.label(else_end);
}
code.freeFinallyLocal(exc);
handler.addExceptionHandlers(handler_start);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitSuite(Suite node) throws Exception {
return suite(node.getInternalBody());
}
// in src/org/python/compiler/CodeCompiler.java
public Object suite(java.util.List<stmt> stmts) throws Exception {
for (stmt s : stmts) {
Object exit = visit(s);
if (exit != null) {
return Exit;
}
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitBoolOp(BoolOp node) throws Exception {
Label end = new Label();
visit(node.getInternalValues().get(0));
for (int i = 1; i < node.getInternalValues().size(); i++) {
code.dup();
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
switch (node.getInternalOp()) {
case Or:
code.ifne(end);
break;
case And:
code.ifeq(end);
break;
}
code.pop();
visit(node.getInternalValues().get(i));
}
code.label(end);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitCompare(Compare node) throws Exception {
int last = code.getLocal(p(PyObject.class));
int result = code.getLocal(p(PyObject.class));
Label end = new Label();
visit(node.getInternalLeft());
code.astore(last);
int n = node.getInternalOps().size();
for (int i = 0; i < n - 1; i++) {
visit(node.getInternalComparators().get(i));
code.aload(last);
code.swap();
code.dup();
code.astore(last);
visitCmpop(node.getInternalOps().get(i));
code.dup();
code.astore(result);
code.invokevirtual(p(PyObject.class), "__nonzero__", sig(Boolean.TYPE));
code.ifeq(end);
}
visit(node.getInternalComparators().get(n - 1));
code.aload(last);
code.swap();
visitCmpop(node.getInternalOps().get(n - 1));
if (n > 1) {
code.astore(result);
code.label(end);
code.aload(result);
}
code.aconst_null();
code.astore(last);
code.freeLocal(last);
code.freeLocal(result);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public void visitCmpop(cmpopType op) throws Exception {
String name = null;
switch (op) {
case Eq:
name = "_eq";
break;
case NotEq:
name = "_ne";
break;
case Lt:
name = "_lt";
break;
case LtE:
name = "_le";
break;
case Gt:
name = "_gt";
break;
case GtE:
name = "_ge";
break;
case Is:
name = "_is";
break;
case IsNot:
name = "_isnot";
break;
case In:
name = "_in";
break;
case NotIn:
name = "_notin";
break;
}
code.invokevirtual(p(PyObject.class), name, sig(PyObject.class, PyObject.class));
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitBinOp(BinOp node) throws Exception {
visit(node.getInternalLeft());
stackProduce();
visit(node.getInternalRight());
stackConsume();
String name = null;
switch (node.getInternalOp()) {
case Add:
name = "_add";
break;
case Sub:
name = "_sub";
break;
case Mult:
name = "_mul";
break;
case Div:
name = "_div";
break;
case Mod:
name = "_mod";
break;
case Pow:
name = "_pow";
break;
case LShift:
name = "_lshift";
break;
case RShift:
name = "_rshift";
break;
case BitOr:
name = "_or";
break;
case BitXor:
name = "_xor";
break;
case BitAnd:
name = "_and";
break;
case FloorDiv:
name = "_floordiv";
break;
}
if (node.getInternalOp() == operatorType.Div && module.getFutures().areDivisionOn()) {
name = "_truediv";
}
code.invokevirtual(p(PyObject.class), name, sig(PyObject.class, PyObject.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitUnaryOp(UnaryOp node) throws Exception {
visit(node.getInternalOperand());
String name = null;
switch (node.getInternalOp()) {
case Invert:
name = "__invert__";
break;
case Not:
name = "__not__";
break;
case UAdd:
name = "__pos__";
break;
case USub:
name = "__neg__";
break;
}
code.invokevirtual(p(PyObject.class), name, sig(PyObject.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitAugAssign(AugAssign node) throws Exception {
setline(node);
augmode = expr_contextType.Load;
visit(node.getInternalTarget());
int target = storeTop();
visit(node.getInternalValue());
code.aload(target);
code.swap();
String name = null;
switch (node.getInternalOp()) {
case Add:
name = "_iadd";
break;
case Sub:
name = "_isub";
break;
case Mult:
name = "_imul";
break;
case Div:
name = "_idiv";
break;
case Mod:
name = "_imod";
break;
case Pow:
name = "_ipow";
break;
case LShift:
name = "_ilshift";
break;
case RShift:
name = "_irshift";
break;
case BitOr:
name = "_ior";
break;
case BitXor:
name = "_ixor";
break;
case BitAnd:
name = "_iand";
break;
case FloorDiv:
name = "_ifloordiv";
break;
}
if (node.getInternalOp() == operatorType.Div && module.getFutures().areDivisionOn()) {
name = "_itruediv";
}
code.invokevirtual(p(PyObject.class), name, sig(PyObject.class, PyObject.class));
code.freeLocal(target);
temporary = storeTop();
augmode = expr_contextType.Store;
visit(node.getInternalTarget());
code.freeLocal(temporary);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public Object invokeNoKeywords(Attribute node, java.util.List<expr> values)
throws Exception {
String name = getName(node.getInternalAttr());
visit(node.getInternalValue());
stackProduce();
code.ldc(name);
code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class));
loadThreadState();
stackProduce(p(ThreadState.class));
switch (values.size()) {
case 0:
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class));
break;
case 1:
visit(values.get(0));
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class));
break;
case 2:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackConsume(3); // target + ts + arguments
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class, PyObject.class));
break;
case 3:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackProduce();
visit(values.get(2));
stackConsume(4); // target + ts + arguments
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class, PyObject.class, PyObject.class));
break;
case 4:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackProduce();
visit(values.get(2));
stackProduce();
visit(values.get(3));
stackConsume(5); // target + ts + arguments
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class, PyObject.class, PyObject.class,
PyObject.class));
break;
default:
int argArray = makeArray(values);
code.aload(argArray);
code.freeLocal(argArray);
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject[].class));
break;
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitCall(Call node) throws Exception {
java.util.List<String> keys = new ArrayList<String>();
java.util.List<expr> values = new ArrayList<expr>();
for (int i = 0; i < node.getInternalArgs().size(); i++) {
values.add(node.getInternalArgs().get(i));
}
for (int i = 0; i < node.getInternalKeywords().size(); i++) {
keys.add(node.getInternalKeywords().get(i).getInternalArg());
values.add(node.getInternalKeywords().get(i).getInternalValue());
}
if ((node.getInternalKeywords() == null || node.getInternalKeywords().size() == 0) &&
node.getInternalStarargs() == null && node.getInternalKwargs() == null &&
node.getInternalFunc() instanceof Attribute) {
return invokeNoKeywords((Attribute) node.getInternalFunc(), values);
}
visit(node.getInternalFunc());
stackProduce();
if (node.getInternalStarargs() != null || node.getInternalKwargs() != null) {
int argArray = makeArray(values);
int strArray = makeStrings(code, keys);
if (node.getInternalStarargs() == null) {
code.aconst_null();
} else {
visit(node.getInternalStarargs());
}
stackProduce();
if (node.getInternalKwargs() == null) {
code.aconst_null();
} else {
visit(node.getInternalKwargs());
}
stackProduce();
code.aload(argArray);
code.aload(strArray);
code.freeLocal(strArray);
code.dup2_x2();
code.pop2();
stackConsume(3); // target + starargs + kwargs
code.invokevirtual(p(PyObject.class), "_callextra", sig(PyObject.class,
PyObject[].class, String[].class, PyObject.class, PyObject.class));
freeArrayRef(argArray);
} else if (keys.size() > 0) {
loadThreadState();
stackProduce(p(ThreadState.class));
int argArray = makeArray(values);
int strArray = makeStrings(code, keys);
code.aload(argArray);
code.aload(strArray);
code.freeLocal(strArray);
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class, ThreadState.class,
PyObject[].class, String[].class));
freeArrayRef(argArray);
} else {
loadThreadState();
stackProduce(p(ThreadState.class));
switch (values.size()) {
case 0:
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class));
break;
case 1:
visit(values.get(0));
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class));
break;
case 2:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackConsume(3); // target + ts + arguments
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class, PyObject.class));
break;
case 3:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackProduce();
visit(values.get(2));
stackConsume(4); // target + ts + arguments
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class, PyObject.class, PyObject.class));
break;
case 4:
visit(values.get(0));
stackProduce();
visit(values.get(1));
stackProduce();
visit(values.get(2));
stackProduce();
visit(values.get(3));
stackConsume(5); // target + ts + arguments
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject.class, PyObject.class, PyObject.class,
PyObject.class));
break;
default:
int argArray = makeArray(values);
code.aload(argArray);
code.freeLocal(argArray);
stackConsume(2); // target + ts
code.invokevirtual(p(PyObject.class), "__call__", sig(PyObject.class,
ThreadState.class, PyObject[].class));
break;
}
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public Object Slice(Subscript node, Slice slice) throws Exception {
expr_contextType ctx = node.getInternalCtx();
if (ctx == expr_contextType.AugStore && augmode == expr_contextType.Store) {
restoreAugTmps(node, 4);
ctx = expr_contextType.Store;
} else {
visit(node.getInternalValue());
stackProduce();
if (slice.getInternalLower() != null) {
visit(slice.getInternalLower());
} else {
code.aconst_null();
}
stackProduce();
if (slice.getInternalUpper() != null) {
visit(slice.getInternalUpper());
} else {
code.aconst_null();
}
stackProduce();
if (slice.getInternalStep() != null) {
visit(slice.getInternalStep());
} else {
code.aconst_null();
}
stackProduce();
if (node.getInternalCtx() == expr_contextType.AugStore &&
augmode == expr_contextType.Load) {
saveAugTmps(node, 4);
ctx = expr_contextType.Load;
}
stackConsume(4);
}
switch (ctx) {
case Del:
code.invokevirtual(p(PyObject.class), "__delslice__", sig(Void.TYPE, PyObject.class,
PyObject.class, PyObject.class));
return null;
case Load:
code.invokevirtual(p(PyObject.class), "__getslice__", sig(PyObject.class,
PyObject.class, PyObject.class, PyObject.class));
return null;
case Param:
case Store:
code.aload(temporary);
code.invokevirtual(p(PyObject.class), "__setslice__", sig(Void.TYPE, PyObject.class,
PyObject.class, PyObject.class, PyObject.class));
return null;
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitSubscript(Subscript node) throws Exception {
if (node.getInternalSlice() instanceof Slice) {
return Slice(node, (Slice) node.getInternalSlice());
}
int value = temporary;
expr_contextType ctx = node.getInternalCtx();
if (node.getInternalCtx() == expr_contextType.AugStore &&
augmode == expr_contextType.Store) {
restoreAugTmps(node, 2);
ctx = expr_contextType.Store;
} else {
visit(node.getInternalValue());
stackProduce();
visit(node.getInternalSlice());
stackConsume();
if (node.getInternalCtx() == expr_contextType.AugStore &&
augmode == expr_contextType.Load) {
saveAugTmps(node, 2);
ctx = expr_contextType.Load;
}
}
switch (ctx) {
case Del:
code.invokevirtual(p(PyObject.class), "__delitem__",
sig(Void.TYPE, PyObject.class));
return null;
case Load:
code.invokevirtual(p(PyObject.class), "__getitem__",
sig(PyObject.class, PyObject.class));
return null;
case Param:
case Store:
code.aload(value);
code.invokevirtual(p(PyObject.class), "__setitem__",
sig(Void.TYPE, PyObject.class, PyObject.class));
return null;
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitIndex(Index node) throws Exception {
traverse(node);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitExtSlice(ExtSlice node) throws Exception {
int dims = makeArray(node.getInternalDims());
code.new_(p(PyTuple.class));
code.dup();
code.aload(dims);
code.invokespecial(p(PyTuple.class), "<init>", sig(Void.TYPE, PyObject[].class));
freeArray(dims);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitAttribute(Attribute node) throws Exception {
expr_contextType ctx = node.getInternalCtx();
if (node.getInternalCtx() == expr_contextType.AugStore &&
augmode == expr_contextType.Store) {
restoreAugTmps(node, 2);
ctx = expr_contextType.Store;
} else {
visit(node.getInternalValue());
code.ldc(getName(node.getInternalAttr()));
if (node.getInternalCtx() == expr_contextType.AugStore &&
augmode == expr_contextType.Load) {
saveAugTmps(node, 2);
ctx = expr_contextType.Load;
}
}
switch (ctx) {
case Del:
code.invokevirtual(p(PyObject.class), "__delattr__", sig(Void.TYPE, String.class));
return null;
case Load:
code.invokevirtual(p(PyObject.class), "__getattr__",
sig(PyObject.class, String.class));
return null;
case Param:
case Store:
code.aload(temporary);
code.invokevirtual(p(PyObject.class), "__setattr__",
sig(Void.TYPE, String.class, PyObject.class));
return null;
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public Object seqSet(java.util.List<expr> nodes) throws Exception {
code.aload(temporary);
code.iconst(nodes.size());
code.invokestatic(p(Py.class), "unpackSequence",
sig(PyObject[].class, PyObject.class, Integer.TYPE));
int tmp = code.getLocal("[org/python/core/PyObject");
code.astore(tmp);
for (int i = 0; i < nodes.size(); i++) {
code.aload(tmp);
code.iconst(i);
code.aaload();
set(nodes.get(i));
}
code.freeLocal(tmp);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
public Object seqDel(java.util.List<expr> nodes) throws Exception {
for (expr e : nodes) {
visit(e);
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitTuple(Tuple node) throws Exception {
if (node.getInternalCtx() == expr_contextType.Store) {
return seqSet(node.getInternalElts());
}
if (node.getInternalCtx() == expr_contextType.Del) {
return seqDel(node.getInternalElts());
}
int content = makeArray(node.getInternalElts());
code.new_(p(PyTuple.class));
code.dup();
code.aload(content);
code.invokespecial(p(PyTuple.class), "<init>", sig(Void.TYPE, PyObject[].class));
freeArray(content);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitList(List node) throws Exception {
if (node.getInternalCtx() == expr_contextType.Store) {
return seqSet(node.getInternalElts());
}
if (node.getInternalCtx() == expr_contextType.Del) {
return seqDel(node.getInternalElts());
}
int content = makeArray(node.getInternalElts());
code.new_(p(PyList.class));
code.dup();
code.aload(content);
code.invokespecial(p(PyList.class), "<init>", sig(Void.TYPE, PyObject[].class));
freeArray(content);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitListComp(ListComp node) throws Exception {
code.new_(p(PyList.class));
code.dup();
code.invokespecial(p(PyList.class), "<init>", sig(Void.TYPE));
code.dup();
code.ldc("append");
code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class));
String tmp_append = "_[" + node.getLine() + "_" + node.getCharPositionInLine() + "]";
set(new Name(node, tmp_append, expr_contextType.Store));
java.util.List<expr> args = new ArrayList<expr>();
args.add(node.getInternalElt());
stmt n = new Expr(node, new Call(node, new Name(node, tmp_append, expr_contextType.Load),
args,
new ArrayList<keyword>(), null, null));
for (int i = node.getInternalGenerators().size() - 1; i >= 0; i--) {
comprehension lc = node.getInternalGenerators().get(i);
for (int j = lc.getInternalIfs().size() - 1; j >= 0; j--) {
java.util.List<stmt> body = new ArrayList<stmt>();
body.add(n);
n = new If(lc.getInternalIfs().get(j), lc.getInternalIfs().get(j), body,
new ArrayList<stmt>());
}
java.util.List<stmt> body = new ArrayList<stmt>();
body.add(n);
n = new For(lc, lc.getInternalTarget(), lc.getInternalIter(), body,
new ArrayList<stmt>());
}
visit(n);
java.util.List<expr> targets = new ArrayList<expr>();
targets.add(new Name(n, tmp_append, expr_contextType.Del));
visit(new Delete(n, targets));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitDict(Dict node) throws Exception {
java.util.List<PythonTree> elts = new ArrayList<PythonTree>();
for (int i = 0; i < node.getInternalKeys().size(); i++) {
elts.add(node.getInternalKeys().get(i));
elts.add(node.getInternalValues().get(i));
}
int content = makeArray(elts);
code.new_(p(PyDictionary.class));
code.dup();
code.aload(content);
code.invokespecial(p(PyDictionary.class), "<init>", sig(Void.TYPE, PyObject[].class));
freeArray(content);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitRepr(Repr node) throws Exception {
visit(node.getInternalValue());
code.invokevirtual(p(PyObject.class), "__repr__", sig(PyString.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitLambda(Lambda node) throws Exception {
String name = "<lambda>";
//Add a return node onto the outside of suite;
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(new Return(node, node.getInternalBody()));
mod retSuite = new Suite(node, bod);
setline(node);
ScopeInfo scope = module.getScopeInfo(node);
int defaultsArray = makeArray(scope.ac.getDefaults());
code.new_(p(PyFunction.class));
code.dup();
code.aload(defaultsArray);
code.freeLocal(defaultsArray);
loadFrame();
code.getfield(p(PyFrame.class), "f_globals", ci(PyObject.class));
code.swap();
scope.setup_closure();
scope.dump();
module.codeConstant(retSuite, name, true, className,
false, false, node.getLine(), scope, cflags).get(code);
if (!makeClosure(scope)) {
code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject[].class, PyCode.class));
} else {
code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject[].class, PyCode.class, PyObject[].class));
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitEllipsis(Ellipsis node) throws Exception {
code.getstatic(p(Py.class), "Ellipsis", ci(PyObject.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitSlice(Slice node) throws Exception {
if (node.getInternalLower() == null) {
getNone();
} else {
visit(node.getInternalLower());
}
stackProduce();
if (node.getInternalUpper() == null) {
getNone();
} else {
visit(node.getInternalUpper());
}
stackProduce();
if (node.getInternalStep() == null) {
getNone();
} else {
visit(node.getInternalStep());
}
int step = storeTop();
stackConsume(2);
code.new_(p(PySlice.class));
code.dup();
code.dup2_x2();
code.pop2();
code.aload(step);
code.freeLocal(step);
code.invokespecial(p(PySlice.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject.class, PyObject.class));
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitClassDef(ClassDef node) throws Exception {
setline(node);
int baseArray = makeArray(node.getInternalBases());
//Get class name
String name = getName(node.getInternalName());
code.ldc(name);
code.aload(baseArray);
ScopeInfo scope = module.getScopeInfo(node);
scope.setup_closure();
scope.dump();
//Make code object out of suite
module.codeConstant(new Suite(node, node.getInternalBody()), name, false, name,
getDocStr(node.getInternalBody()), true, false, node.getLine(), scope,
cflags).get(code);
//Make class out of name, bases, and code
if (!makeClosure(scope)) {
code.invokestatic(p(Py.class), "makeClass", sig(PyObject.class, String.class,
PyObject[].class, PyCode.class));
} else {
code.invokestatic(p(Py.class), "makeClass", sig(PyObject.class, String.class,
PyObject[].class, PyCode.class, PyObject[].class));
}
applyDecorators(node.getInternalDecorator_list());
//Assign this new class to the given name
set(new Name(node, node.getInternalName(), expr_contextType.Store));
freeArray(baseArray);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitNum(Num node) throws Exception {
if (node.getInternalN() instanceof PyInteger) {
module.integerConstant(((PyInteger) node.getInternalN()).getValue()).get(code);
} else if (node.getInternalN() instanceof PyLong) {
module.longConstant(((PyObject) node.getInternalN()).__str__().toString()).get(code);
} else if (node.getInternalN() instanceof PyFloat) {
module.floatConstant(((PyFloat) node.getInternalN()).getValue()).get(code);
} else if (node.getInternalN() instanceof PyComplex) {
module.complexConstant(((PyComplex) node.getInternalN()).imag).get(code);
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
void emitGetGlobal(String name) throws Exception {
code.ldc(name);
code.invokevirtual(p(PyFrame.class), "getglobal", sig(PyObject.class, String.class));
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitName(Name node) throws Exception {
String name;
if (fast_locals) {
name = node.getInternalId();
} else {
name = getName(node.getInternalId());
}
SymInfo syminf = tbl.get(name);
expr_contextType ctx = node.getInternalCtx();
if (ctx == expr_contextType.AugStore) {
ctx = augmode;
}
switch (ctx) {
case Load:
loadFrame();
if (syminf != null) {
int flags = syminf.flags;
if ((flags & ScopeInfo.GLOBAL) != 0 || optimizeGlobals &&
(flags & (ScopeInfo.BOUND | ScopeInfo.CELL |
ScopeInfo.FREE)) == 0) {
emitGetGlobal(name);
return null;
}
if (fast_locals) {
if ((flags & ScopeInfo.CELL) != 0) {
code.iconst(syminf.env_index);
code.invokevirtual(p(PyFrame.class), "getderef", sig(PyObject.class,
Integer.TYPE));
return null;
}
if ((flags & ScopeInfo.BOUND) != 0) {
code.iconst(syminf.locals_index);
code.invokevirtual(p(PyFrame.class), "getlocal", sig(PyObject.class,
Integer.TYPE));
return null;
}
}
if ((flags & ScopeInfo.FREE) != 0 &&
(flags & ScopeInfo.BOUND) == 0) {
code.iconst(syminf.env_index);
code.invokevirtual(p(PyFrame.class), "getderef", sig(PyObject.class,
Integer.TYPE));
return null;
}
}
code.ldc(name);
code.invokevirtual(p(PyFrame.class), "getname", sig(PyObject.class, String.class));
return null;
case Param:
case Store:
loadFrame();
if (syminf != null && (syminf.flags & ScopeInfo.GLOBAL) != 0) {
code.ldc(name);
code.aload(temporary);
code.invokevirtual(p(PyFrame.class), "setglobal", sig(Void.TYPE, String.class,
PyObject.class));
} else {
if (!fast_locals) {
code.ldc(name);
code.aload(temporary);
code.invokevirtual(p(PyFrame.class), "setlocal",
sig(Void.TYPE, String.class, PyObject.class));
} else {
if (syminf == null) {
throw new ParseException("internal compiler error", node);
}
if ((syminf.flags & ScopeInfo.CELL) != 0) {
code.iconst(syminf.env_index);
code.aload(temporary);
code.invokevirtual(p(PyFrame.class), "setderef", sig(Void.TYPE,
Integer.TYPE, PyObject.class));
} else {
code.iconst(syminf.locals_index);
code.aload(temporary);
code.invokevirtual(p(PyFrame.class), "setlocal", sig(Void.TYPE,
Integer.TYPE, PyObject.class));
}
}
}
return null;
case Del: {
loadFrame();
if (syminf != null && (syminf.flags & ScopeInfo.GLOBAL) != 0) {
code.ldc(name);
code.invokevirtual(p(PyFrame.class), "delglobal", sig(Void.TYPE, String.class));
} else {
if (!fast_locals) {
code.ldc(name);
code.invokevirtual(p(PyFrame.class), "dellocal",
sig(Void.TYPE, String.class));
} else {
if (syminf == null) {
throw new ParseException("internal compiler error", node);
}
if ((syminf.flags & ScopeInfo.CELL) != 0) {
module.error("can not delete variable '" + name +
"' referenced in nested scope", true, node);
}
code.iconst(syminf.locals_index);
code.invokevirtual(p(PyFrame.class), "dellocal",
sig(Void.TYPE, Integer.TYPE));
}
}
return null;
}
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitStr(Str node) throws Exception {
PyString s = (PyString) node.getInternalS();
if (s instanceof PyUnicode) {
module.unicodeConstant(s.asString()).get(code);
} else {
module.stringConstant(s.asString()).get(code);
}
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitGeneratorExp(GeneratorExp node) throws Exception {
String bound_exp = "_(x)";
setline(node);
code.new_(p(PyFunction.class));
code.dup();
loadFrame();
code.getfield(p(PyFrame.class), "f_globals", ci(PyObject.class));
ScopeInfo scope = module.getScopeInfo(node);
int emptyArray = makeArray(new ArrayList<expr>());
code.aload(emptyArray);
scope.setup_closure();
scope.dump();
stmt n = new Expr(node, new Yield(node, node.getInternalElt()));
expr iter = null;
for (int i = node.getInternalGenerators().size() - 1; i >= 0; i--) {
comprehension comp = node.getInternalGenerators().get(i);
for (int j = comp.getInternalIfs().size() - 1; j >= 0; j--) {
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
n = new If(comp.getInternalIfs().get(j), comp.getInternalIfs().get(j), bod,
new ArrayList<stmt>());
}
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
if (i != 0) {
n = new For(comp, comp.getInternalTarget(), comp.getInternalIter(), bod,
new ArrayList<stmt>());
} else {
n = new For(comp, comp.getInternalTarget(), new Name(node, bound_exp,
expr_contextType.Load), bod, new ArrayList<stmt>());
iter = comp.getInternalIter();
}
}
java.util.List<stmt> bod = new ArrayList<stmt>();
bod.add(n);
module.codeConstant(new Suite(node, bod), "<genexpr>", true,
className, false, false,
node.getLine(), scope, cflags).get(code);
code.aconst_null();
if (!makeClosure(scope)) {
code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject[].class, PyCode.class, PyObject.class));
} else {
code.invokespecial(p(PyFunction.class), "<init>", sig(Void.TYPE, PyObject.class,
PyObject[].class, PyCode.class, PyObject.class, PyObject[].class));
}
int genExp = storeTop();
visit(iter);
code.aload(genExp);
code.freeLocal(genExp);
code.swap();
code.invokevirtual(p(PyObject.class), "__iter__", sig(PyObject.class));
loadThreadState();
code.swap();
code.invokevirtual(p(PyObject.class), "__call__",
sig(PyObject.class, ThreadState.class, PyObject.class));
freeArray(emptyArray);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public Object visitWith(With node) throws Exception {
if (!module.getFutures().withStatementSupported()) {
throw new ParseException("'with' will become a reserved keyword in Python 2.6", node);
}
final Label label_body_start = new Label();
final Label label_body_end = new Label();
final Label label_catch = new Label();
final Label label_end = new Label();
final Method contextGuard_getManager = Method.getMethod(
"org.python.core.ContextManager getManager (org.python.core.PyObject)");
final Method __enter__ = Method.getMethod(
"org.python.core.PyObject __enter__ (org.python.core.ThreadState)");
final Method __exit__ = Method.getMethod(
"boolean __exit__ (org.python.core.ThreadState,org.python.core.PyException)");
// mgr = (EXPR)
visit(node.getInternalContext_expr());
// wrap the manager with the ContextGuard (or get it directly if it
// supports the ContextManager interface)
code.invokestatic(Type.getType(ContextGuard.class).getInternalName(),
contextGuard_getManager.getName(), contextGuard_getManager.getDescriptor());
code.dup();
final int mgr_tmp = code.getLocal(Type.getType(ContextManager.class).getInternalName());
code.astore(mgr_tmp);
// value = mgr.__enter__()
loadThreadState();
code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
__enter__.getName(), __enter__.getDescriptor());
int value_tmp = code.getLocal(p(PyObject.class));
code.astore(value_tmp);
// exc = True # not necessary, since we don't exec finally if exception
// FINALLY (preparation)
// ordinarily with a finally, we need to duplicate the code. that's not the case
// here
// # The normal and non-local-goto cases are handled here
// if exc: # implicit
// exit(None, None, None)
ExceptionHandler normalExit = new ExceptionHandler() {
@Override
public boolean isFinallyHandler() {
return true;
}
@Override
public void finalBody(CodeCompiler compiler) throws Exception {
compiler.code.aload(mgr_tmp);
loadThreadState();
compiler.code.aconst_null();
compiler.code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
__exit__.getName(), __exit__.getDescriptor());
compiler.code.pop();
}
};
exceptionHandlers.push(normalExit);
// try-catch block here
ExceptionHandler handler = new ExceptionHandler();
exceptionHandlers.push(handler);
handler.exceptionStarts.addElement(label_body_start);
// VAR = value # Only if "as VAR" is present
code.label(label_body_start);
if (node.getInternalOptional_vars() != null) {
set(node.getInternalOptional_vars(), value_tmp);
}
code.freeLocal(value_tmp);
// BLOCK + FINALLY if non-local-goto
Object blockResult = suite(node.getInternalBody());
normalExit.bodyDone = true;
exceptionHandlers.pop();
exceptionHandlers.pop();
code.label(label_body_end);
handler.exceptionEnds.addElement(label_body_end);
// FINALLY if *not* non-local-goto
if (blockResult == NoExit) {
// BLOCK would have generated FINALLY for us if it exited (due to a break,
// continue or return)
inlineFinally(normalExit);
code.goto_(label_end);
}
// CATCH
code.label(label_catch);
loadFrame();
code.invokestatic(p(Py.class), "setException", sig(PyException.class, Throwable.class,
PyFrame.class));
code.aload(mgr_tmp);
code.swap();
loadThreadState();
code.swap();
code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
__exit__.getName(), __exit__.getDescriptor());
// # The exceptional case is handled here
// exc = False # implicit
// if not exit(*sys.exc_info()):
code.ifne(label_end);
// raise
// # The exception is swallowed if exit() returns true
code.invokestatic(p(Py.class), "makeException", sig(PyException.class));
code.checkcast(p(Throwable.class));
code.athrow();
code.label(label_end);
code.freeLocal(mgr_tmp);
handler.addExceptionHandlers(label_catch);
return null;
}
// in src/org/python/compiler/CodeCompiler.java
Override
public void finalBody(CodeCompiler compiler) throws Exception {
compiler.code.aload(mgr_tmp);
loadThreadState();
compiler.code.aconst_null();
compiler.code.invokeinterface(Type.getType(ContextManager.class).getInternalName(),
__exit__.getName(), __exit__.getDescriptor());
compiler.code.pop();
}
// in src/org/python/compiler/CodeCompiler.java
Override
protected Object unhandled_node(PythonTree node) throws Exception {
throw new Exception("Unhandled node " + node);
}
// in src/org/python/compiler/CodeCompiler.java
public void addExceptionHandlers(Label handlerStart) throws Exception {
for (int i = 0; i < exceptionStarts.size(); ++i) {
Label start = exceptionStarts.elementAt(i);
Label end = exceptionEnds.elementAt(i);
//FIXME: not at all sure that getOffset() test is correct or necessary.
if (start.getOffset() != end.getOffset()) {
code.trycatch(
exceptionStarts.elementAt(i),
exceptionEnds.elementAt(i),
handlerStart,
p(Throwable.class));
}
}
}
// in src/org/python/compiler/CodeCompiler.java
public void finalBody(CodeCompiler compiler) throws Exception {
if (node instanceof TryFinally) {
suite(((TryFinally) node).getInternalFinalbody());
}
}
// in src/org/python/compiler/JavaMaker.java
Override
public void addConstructor(String name,
Class<?>[] parameters,
Class<?> ret,
String sig,
int access) throws Exception {
/* Need a fancy constructor for the Java side of things */
Code code = classfile.addMethod("<init>", sig, access);
callSuper(code, "<init>", name, parameters, null, sig);
code.visitVarInsn(ALOAD, 0);
getArgs(code, parameters);
code.visitMethodInsn(INVOKEVIRTUAL, classfile.name, "__initProxy__", makeSig("V", $objArr));
code.visitInsn(RETURN);
}
// in src/org/python/compiler/JavaMaker.java
Override
public void addProxy() throws Exception {
if (methods != null)
super.addProxy();
// _initProxy method
Code code = classfile.addMethod("__initProxy__", makeSig("V", $objArr), Modifier.PUBLIC);
code.visitVarInsn(ALOAD, 0);
code.visitLdcInsn(pythonModule);
code.visitLdcInsn(pythonClass);
code.visitVarInsn(ALOAD, 1);
code.visitMethodInsn(INVOKESTATIC, "org/python/core/Py", "initProxy",
makeSig("V", $pyProxy, $str, $str, $objArr));
code.visitInsn(RETURN);
}
// in src/org/python/compiler/JavaMaker.java
Override
public void addMethod(Method method, int access) throws Exception {
if (Modifier.isAbstract(access)) {
// Maybe throw an exception here???
super.addMethod(method, access);
} else if (methods.__finditem__(method.getName().intern()) != null) {
super.addMethod(method, access);
} else if (Modifier.isProtected(method.getModifiers())) {
addSuperMethod(method, access);
}
}
// in src/org/python/compiler/Module.java
PyCodeConstant codeConstant(mod tree, String name, boolean fast_locals, String className,
boolean classBody, boolean printResults, int firstlineno,
ScopeInfo scope, CompilerFlags cflags) throws Exception {
return codeConstant(tree, name, fast_locals, className, null, classBody, printResults,
firstlineno, scope, cflags);
}
// in src/org/python/compiler/Module.java
PyCodeConstant codeConstant(mod tree, String name, boolean fast_locals, String className,
Str classDoc, boolean classBody, boolean printResults,
int firstlineno, ScopeInfo scope, CompilerFlags cflags)
throws Exception {
PyCodeConstant code = new PyCodeConstant(tree, name, fast_locals,
className, classBody, printResults, firstlineno, scope, cflags,
this);
codes.add(code);
CodeCompiler compiler = new CodeCompiler(this, printResults);
Code c = classfile.addMethod(
code.fname,
sig(PyObject.class, PyFrame.class, ThreadState.class),
ACC_PUBLIC);
compiler.parse(tree, c, fast_locals, className, classDoc, classBody, scope, cflags);
return code;
}
// in src/org/python/compiler/Module.java
public void error(String msg, boolean err, PythonTree node)
throws Exception {
if (!err) {
try {
Py.warning(Py.SyntaxWarning, msg, (sfilename != null) ? sfilename : "?",
node.getLine(), null, Py.None);
return;
} catch (PyException e) {
if (!e.match(Py.SyntaxWarning)) {
throw e;
}
}
}
throw new ParseException(msg, node);
}
// in src/org/python/compiler/Module.java
public static void compile(mod node, OutputStream ostream, String name, String filename,
boolean linenumbers, boolean printResults, CompilerFlags cflags)
throws Exception {
compile(node, ostream, name, filename, linenumbers, printResults, cflags,
org.python.core.imp.NO_MTIME);
}
// in src/org/python/compiler/Module.java
public static void compile(mod node, OutputStream ostream, String name, String filename,
boolean linenumbers, boolean printResults, CompilerFlags cflags, long mtime)
throws Exception {
Module module = new Module(name, filename, linenumbers, mtime);
if (cflags == null) {
cflags = new CompilerFlags();
}
module.futures.preprocessFutures(node, cflags);
new ScopesCompiler(module, module.scopes).parse(node);
//Add __doc__ if it exists
Constant main = module.codeConstant(node, "<module>", false, null, false,
printResults, 0,
module.getScopeInfo(node),
cflags);
module.mainCode = main;
module.write(ostream);
}
// in src/org/python/compiler/LegacyCompiler.java
public PyCode loadCode() throws Exception {
return BytecodeLoader.makeCode(name, ostream().toByteArray(),
filename);
}
// in src/org/python/compiler/LegacyCompiler.java
public void writeTo(OutputStream stream) throws Exception {
if (this.ostream != null) {
stream.write(ostream.toByteArray());
} else {
Module.compile(node, stream, name, filename, linenumbers,
printResults, cflags);
}
}
// in src/org/python/compiler/LegacyCompiler.java
public void saveCode(String directory) throws Exception {
// FIXME: this is slightly broken, it should use the directory
Py.saveClassFile(name, ostream());
}
// in src/org/python/compiler/LegacyCompiler.java
private ByteArrayOutputStream ostream() throws Exception {
if (ostream == null) {
ostream = new ByteArrayOutputStream();
Module.compile(node, ostream, name, filename, linenumbers,
printResults, cflags);
}
return ostream;
}
// in src/org/python/compiler/ArgListCompiler.java
public void visitArgs(arguments args) throws Exception {
for (int i = 0; i < args.getInternalArgs().size(); i++) {
String name = (String) visit(args.getInternalArgs().get(i));
names.add(name);
if (args.getInternalArgs().get(i) instanceof Tuple) {
List<expr> targets = new ArrayList<expr>();
targets.add(args.getInternalArgs().get(i));
Assign ass = new Assign(args.getInternalArgs().get(i),
targets,
new Name(args.getInternalArgs().get(i), name, expr_contextType.Load));
init_code.add(ass);
}
}
if (args.getInternalVararg() != null) {
arglist = true;
names.add(args.getInternalVararg());
}
if (args.getInternalKwarg() != null) {
keywordlist = true;
names.add(args.getInternalKwarg());
}
defaults = args.getInternalDefaults();
for (int i = 0; i < defaults.size(); i++) {
if (defaults.get(i) == null)
throw new ParseException(
"non-default argument follows default argument",
args.getInternalArgs().get(args.getInternalArgs().size() - defaults.size() + i));
}
}
// in src/org/python/compiler/ArgListCompiler.java
Override
public Object visitName(Name node) throws Exception {
//FIXME: do we need Store and Param, or just Param?
if (node.getInternalCtx() != expr_contextType.Store && node.getInternalCtx() != expr_contextType.Param) {
return null;
}
if (fpnames.contains(node.getInternalId())) {
throw new ParseException("duplicate argument name found: " +
node.getInternalId(), node);
}
fpnames.add(node.getInternalId());
return node.getInternalId();
}
// in src/org/python/compiler/ArgListCompiler.java
Override
public Object visitTuple(Tuple node) throws Exception {
StringBuffer name = new StringBuffer("(");
int n = node.getInternalElts().size();
for (int i = 0; i < n-1; i++) {
name.append(visit(node.getInternalElts().get(i)));
name.append(", ");
}
name.append(visit(node.getInternalElts().get(n - 1)));
name.append(")");
return name.toString();
}
// in src/org/python/compiler/ScopesCompiler.java
public void endScope() throws Exception {
if (cur.kind == FUNCSCOPE) {
func_level--;
}
level--;
ScopeInfo up = null;
if (!scopes.empty()) {
up = scopes.pop();
}
//Go into the stack to find a non class containing scope to use making the closure
//See PEP 227
int dist = 1;
ScopeInfo referenceable = up;
for (int i = scopes.size() - 1; i >= 0
&& referenceable.kind == CLASSSCOPE; i--, dist++) {
referenceable = (scopes.get(i));
}
cur.cook(referenceable, dist, code_compiler);
cur.dump(); // debug
cur = up;
}
// in src/org/python/compiler/ScopesCompiler.java
public void parse(PythonTree node) throws Exception {
try {
visit(node);
} catch (Throwable t) {
throw ParserFacade.fixParseError(null, t, code_compiler.getFilename());
}
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitInteractive(Interactive node) throws Exception {
beginScope("<single-top>", TOPSCOPE, node, null);
suite(node.getInternalBody());
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitModule(org.python.antlr.ast.Module node)
throws Exception {
beginScope("<file-top>", TOPSCOPE, node, null);
suite(node.getInternalBody());
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitExpression(Expression node) throws Exception {
beginScope("<eval-top>", TOPSCOPE, node, null);
visit(new Return(node,node.getInternalBody()));
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitFunctionDef(FunctionDef node) throws Exception {
def(node.getInternalName());
ArgListCompiler ac = new ArgListCompiler();
ac.visitArgs(node.getInternalArgs());
List<expr> defaults = ac.getDefaults();
for (int i = 0; i < defaults.size(); i++) {
visit(defaults.get(i));
}
List<expr> decs = node.getInternalDecorator_list();
for (int i = decs.size() - 1; i >= 0; i--) {
visit(decs.get(i));
}
beginScope(node.getInternalName(), FUNCSCOPE, node, ac);
int n = ac.names.size();
for (int i = 0; i < n; i++) {
cur.addParam(ac.names.get(i));
}
for (int i = 0; i < ac.init_code.size(); i++) {
visit(ac.init_code.get(i));
}
cur.markFromParam();
suite(node.getInternalBody());
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitLambda(Lambda node) throws Exception {
ArgListCompiler ac = new ArgListCompiler();
ac.visitArgs(node.getInternalArgs());
List<? extends PythonTree> defaults = ac.getDefaults();
for (int i = 0; i < defaults.size(); i++) {
visit(defaults.get(i));
}
beginScope("<lambda>", FUNCSCOPE, node, ac);
for (Object o : ac.names) {
cur.addParam((String) o);
}
for (Object o : ac.init_code) {
visit((stmt) o);
}
cur.markFromParam();
visit(node.getInternalBody());
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
public void suite(List<stmt> stmts) throws Exception {
for (int i = 0; i < stmts.size(); i++)
visit(stmts.get(i));
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitImport(Import node) throws Exception {
for (int i = 0; i < node.getInternalNames().size(); i++) {
if (node.getInternalNames().get(i).getInternalAsname() != null) {
cur.addBound(node.getInternalNames().get(i).getInternalAsname());
} else {
String name = node.getInternalNames().get(i).getInternalName();
if (name.indexOf('.') > 0) {
name = name.substring(0, name.indexOf('.'));
}
cur.addBound(name);
}
}
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitImportFrom(ImportFrom node) throws Exception {
Future.checkFromFuture(node); // future stmt support
int n = node.getInternalNames().size();
if (n == 0) {
cur.from_import_star = true;
return null;
}
for (int i = 0; i < n; i++) {
if (node.getInternalNames().get(i).getInternalAsname() != null) {
cur.addBound(node.getInternalNames().get(i).getInternalAsname());
} else {
cur.addBound(node.getInternalNames().get(i).getInternalName());
}
}
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitGlobal(Global node) throws Exception {
int n = node.getInternalNames().size();
for (int i = 0; i < n; i++) {
String name = node.getInternalNames().get(i);
int prev = cur.addGlobal(name);
if (prev >= 0) {
if ((prev & FROM_PARAM) != 0) {
code_compiler.error("name '" + name
+ "' is local and global", true, node);
}
if ((prev & GLOBAL) != 0) {
continue;
}
String what;
if ((prev & BOUND) != 0) {
what = "assignment";
} else {
what = "use";
}
code_compiler.error("name '" + name
+ "' declared global after " + what, false, node);
}
}
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitExec(Exec node) throws Exception {
cur.exec = true;
if (node.getInternalGlobals() == null && node.getInternalLocals() == null) {
cur.unqual_exec = true;
}
traverse(node);
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitClassDef(ClassDef node) throws Exception {
def(node.getInternalName());
int n = node.getInternalBases().size();
for (int i = 0; i < n; i++) {
visit(node.getInternalBases().get(i));
}
beginScope(node.getInternalName(), CLASSSCOPE, node, null);
suite(node.getInternalBody());
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitName(Name node) throws Exception {
String name = node.getInternalId();
if (node.getInternalCtx() != expr_contextType.Load) {
if (name.equals("__debug__")) {
code_compiler.error("can not assign to __debug__", true, node);
}
cur.addBound(name);
} else {
cur.addUsed(name);
}
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitListComp(ListComp node) throws Exception {
String tmp = "_[" + node.getLine() + "_" + node.getCharPositionInLine()
+ "]";
cur.addBound(tmp);
traverse(node);
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitYield(Yield node) throws Exception {
cur.defineAsGenerator(node);
cur.yield_count++;
traverse(node);
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitReturn(Return node) throws Exception {
if (node.getInternalValue() != null) {
cur.noteReturnValue(node);
}
traverse(node);
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitGeneratorExp(GeneratorExp node) throws Exception {
// The first iterator is evaluated in the outer scope
if (node.getInternalGenerators() != null && node.getInternalGenerators().size() > 0) {
visit(node.getInternalGenerators().get(0).getInternalIter());
}
String bound_exp = "_(x)";
String tmp = "_(" + node.getLine() + "_" + node.getCharPositionInLine()
+ ")";
def(tmp);
ArgListCompiler ac = new ArgListCompiler();
List<expr> args = new ArrayList<expr>();
args.add(new Name(node.getToken(), bound_exp, expr_contextType.Param));
ac.visitArgs(new arguments(node, args, null, null, new ArrayList<expr>()));
beginScope(tmp, FUNCSCOPE, node, ac);
cur.addParam(bound_exp);
cur.markFromParam();
cur.defineAsGenerator(node);
cur.yield_count++;
// The reset of the iterators are evaluated in the inner scope
if (node.getInternalElt() != null) {
visit(node.getInternalElt());
}
if (node.getInternalGenerators() != null) {
for (int i = 0; i < node.getInternalGenerators().size(); i++) {
if (node.getInternalGenerators().get(i) != null) {
if (i == 0) {
visit(node.getInternalGenerators().get(i).getInternalTarget());
if (node.getInternalGenerators().get(i).getInternalIfs() != null) {
for (expr cond : node.getInternalGenerators().get(i).getInternalIfs()) {
if (cond != null) {
visit(cond);
}
}
}
} else {
visit(node.getInternalGenerators().get(i));
}
}
}
}
endScope();
return null;
}
// in src/org/python/compiler/ScopesCompiler.java
Override
public Object visitWith(With node) throws Exception {
cur.max_with_count++;
traverse(node);
return null;
}
// in src/org/python/antlr/PythonTree.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
throw new RuntimeException("Unexpected node: " + this);
}
// in src/org/python/antlr/PythonTree.java
public void traverse(VisitorIF<?> visitor) throws Exception {
throw new RuntimeException("Cannot traverse node: " + this);
}
// in src/org/python/antlr/ast/ClassDef.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitClassDef(this);
}
// in src/org/python/antlr/ast/ClassDef.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (bases != null) {
for (PythonTree t : bases) {
if (t != null)
t.accept(visitor);
}
}
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (decorator_list != null) {
for (PythonTree t : decorator_list) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Ellipsis.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitEllipsis(this);
}
// in src/org/python/antlr/ast/Ellipsis.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/ExtSlice.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitExtSlice(this);
}
// in src/org/python/antlr/ast/ExtSlice.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (dims != null) {
for (PythonTree t : dims) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Dict.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitDict(this);
}
// in src/org/python/antlr/ast/Dict.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (keys != null) {
for (PythonTree t : keys) {
if (t != null)
t.accept(visitor);
}
}
if (values != null) {
for (PythonTree t : values) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Assign.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitAssign(this);
}
// in src/org/python/antlr/ast/Assign.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (targets != null) {
for (PythonTree t : targets) {
if (t != null)
t.accept(visitor);
}
}
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/Name.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitName(this);
}
// in src/org/python/antlr/ast/Name.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/UnaryOp.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitUnaryOp(this);
}
// in src/org/python/antlr/ast/UnaryOp.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (operand != null)
operand.accept(visitor);
}
// in src/org/python/antlr/ast/Expr.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitExpr(this);
}
// in src/org/python/antlr/ast/Expr.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/ImportFrom.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitImportFrom(this);
}
// in src/org/python/antlr/ast/ImportFrom.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (names != null) {
for (PythonTree t : names) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Subscript.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitSubscript(this);
}
// in src/org/python/antlr/ast/Subscript.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
if (slice != null)
slice.accept(visitor);
}
// in src/org/python/antlr/ast/FunctionDef.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitFunctionDef(this);
}
// in src/org/python/antlr/ast/FunctionDef.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (args != null)
args.accept(visitor);
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (decorator_list != null) {
for (PythonTree t : decorator_list) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Import.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitImport(this);
}
// in src/org/python/antlr/ast/Import.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (names != null) {
for (PythonTree t : names) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/keyword.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
traverse(visitor);
return null;
}
// in src/org/python/antlr/ast/keyword.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/Pass.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitPass(this);
}
// in src/org/python/antlr/ast/Pass.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/Num.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitNum(this);
}
// in src/org/python/antlr/ast/Num.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/List.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitList(this);
}
// in src/org/python/antlr/ast/List.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (elts != null) {
for (PythonTree t : elts) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/comprehension.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
traverse(visitor);
return null;
}
// in src/org/python/antlr/ast/comprehension.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (target != null)
target.accept(visitor);
if (iter != null)
iter.accept(visitor);
if (ifs != null) {
for (PythonTree t : ifs) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/ErrorSlice.java
public void traverse(VisitorIF visitor) throws Exception {
//no op.
}
// in src/org/python/antlr/ast/Break.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitBreak(this);
}
// in src/org/python/antlr/ast/Break.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/Compare.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitCompare(this);
}
// in src/org/python/antlr/ast/Compare.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (left != null)
left.accept(visitor);
if (comparators != null) {
for (PythonTree t : comparators) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Return.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitReturn(this);
}
// in src/org/python/antlr/ast/Return.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/While.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitWhile(this);
}
// in src/org/python/antlr/ast/While.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (test != null)
test.accept(visitor);
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (orelse != null) {
for (PythonTree t : orelse) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/ListComp.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitListComp(this);
}
// in src/org/python/antlr/ast/ListComp.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (elt != null)
elt.accept(visitor);
if (generators != null) {
for (PythonTree t : generators) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Yield.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitYield(this);
}
// in src/org/python/antlr/ast/Yield.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/arguments.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
traverse(visitor);
return null;
}
// in src/org/python/antlr/ast/arguments.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (args != null) {
for (PythonTree t : args) {
if (t != null)
t.accept(visitor);
}
}
if (defaults != null) {
for (PythonTree t : defaults) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/alias.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
traverse(visitor);
return null;
}
// in src/org/python/antlr/ast/alias.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/Delete.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitDelete(this);
}
// in src/org/python/antlr/ast/Delete.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (targets != null) {
for (PythonTree t : targets) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Interactive.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitInteractive(this);
}
// in src/org/python/antlr/ast/Interactive.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Module.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitModule(this);
}
// in src/org/python/antlr/ast/Module.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/IfExp.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitIfExp(this);
}
// in src/org/python/antlr/ast/IfExp.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (test != null)
test.accept(visitor);
if (body != null)
body.accept(visitor);
if (orelse != null)
orelse.accept(visitor);
}
// in src/org/python/antlr/ast/TryFinally.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitTryFinally(this);
}
// in src/org/python/antlr/ast/TryFinally.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (finalbody != null) {
for (PythonTree t : finalbody) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/ErrorStmt.java
public void traverse(VisitorIF visitor) throws Exception {
//no op.
}
// in src/org/python/antlr/ast/If.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitIf(this);
}
// in src/org/python/antlr/ast/If.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (test != null)
test.accept(visitor);
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (orelse != null) {
for (PythonTree t : orelse) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Suite.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitSuite(this);
}
// in src/org/python/antlr/ast/Suite.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Attribute.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitAttribute(this);
}
// in src/org/python/antlr/ast/Attribute.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/ErrorExpr.java
public void traverse(VisitorIF visitor) throws Exception {
//no op.
}
// in src/org/python/antlr/ast/BinOp.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitBinOp(this);
}
// in src/org/python/antlr/ast/BinOp.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (left != null)
left.accept(visitor);
if (right != null)
right.accept(visitor);
}
// in src/org/python/antlr/ast/Lambda.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitLambda(this);
}
// in src/org/python/antlr/ast/Lambda.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (args != null)
args.accept(visitor);
if (body != null)
body.accept(visitor);
}
// in src/org/python/antlr/ast/Call.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitCall(this);
}
// in src/org/python/antlr/ast/Call.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (func != null)
func.accept(visitor);
if (args != null) {
for (PythonTree t : args) {
if (t != null)
t.accept(visitor);
}
}
if (keywords != null) {
for (PythonTree t : keywords) {
if (t != null)
t.accept(visitor);
}
}
if (starargs != null)
starargs.accept(visitor);
if (kwargs != null)
kwargs.accept(visitor);
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitModule(Module node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitInteractive(Interactive node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitExpression(Expression node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitSuite(Suite node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitFunctionDef(FunctionDef node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitClassDef(ClassDef node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitReturn(Return node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitDelete(Delete node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitAssign(Assign node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitAugAssign(AugAssign node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitPrint(Print node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitFor(For node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitWhile(While node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitIf(If node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitWith(With node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitRaise(Raise node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitTryExcept(TryExcept node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitTryFinally(TryFinally node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitAssert(Assert node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitImport(Import node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitImportFrom(ImportFrom node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitExec(Exec node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitGlobal(Global node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitExpr(Expr node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitPass(Pass node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitBreak(Break node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitContinue(Continue node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitBoolOp(BoolOp node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitBinOp(BinOp node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitUnaryOp(UnaryOp node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitLambda(Lambda node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitIfExp(IfExp node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitDict(Dict node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitListComp(ListComp node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitGeneratorExp(GeneratorExp node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitYield(Yield node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitCompare(Compare node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitCall(Call node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitRepr(Repr node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitNum(Num node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitStr(Str node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitAttribute(Attribute node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitSubscript(Subscript node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitName(Name node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitList(List node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitTuple(Tuple node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitEllipsis(Ellipsis node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitSlice(Slice node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitExtSlice(ExtSlice node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitIndex(Index node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/VisitorBase.java
public R visitExceptHandler(ExceptHandler node) throws Exception {
R ret = unhandled_node(node);
traverse(node);
return ret;
}
// in src/org/python/antlr/ast/Global.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitGlobal(this);
}
// in src/org/python/antlr/ast/Global.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/Tuple.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitTuple(this);
}
// in src/org/python/antlr/ast/Tuple.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (elts != null) {
for (PythonTree t : elts) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/ExceptHandler.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitExceptHandler(this);
}
// in src/org/python/antlr/ast/ExceptHandler.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (type != null)
type.accept(visitor);
if (name != null)
name.accept(visitor);
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/With.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitWith(this);
}
// in src/org/python/antlr/ast/With.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (context_expr != null)
context_expr.accept(visitor);
if (optional_vars != null)
optional_vars.accept(visitor);
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Repr.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitRepr(this);
}
// in src/org/python/antlr/ast/Repr.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/Slice.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitSlice(this);
}
// in src/org/python/antlr/ast/Slice.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (lower != null)
lower.accept(visitor);
if (upper != null)
upper.accept(visitor);
if (step != null)
step.accept(visitor);
}
// in src/org/python/antlr/ast/TryExcept.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitTryExcept(this);
}
// in src/org/python/antlr/ast/TryExcept.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (handlers != null) {
for (PythonTree t : handlers) {
if (t != null)
t.accept(visitor);
}
}
if (orelse != null) {
for (PythonTree t : orelse) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Str.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitStr(this);
}
// in src/org/python/antlr/ast/Str.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/Exec.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitExec(this);
}
// in src/org/python/antlr/ast/Exec.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null)
body.accept(visitor);
if (globals != null)
globals.accept(visitor);
if (locals != null)
locals.accept(visitor);
}
// in src/org/python/antlr/ast/Raise.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitRaise(this);
}
// in src/org/python/antlr/ast/Raise.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (type != null)
type.accept(visitor);
if (inst != null)
inst.accept(visitor);
if (tback != null)
tback.accept(visitor);
}
// in src/org/python/antlr/ast/Continue.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitContinue(this);
}
// in src/org/python/antlr/ast/Continue.java
public void traverse(VisitorIF<?> visitor) throws Exception {
}
// in src/org/python/antlr/ast/BoolOp.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitBoolOp(this);
}
// in src/org/python/antlr/ast/BoolOp.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (values != null) {
for (PythonTree t : values) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/GeneratorExp.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitGeneratorExp(this);
}
// in src/org/python/antlr/ast/GeneratorExp.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (elt != null)
elt.accept(visitor);
if (generators != null) {
for (PythonTree t : generators) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/AugAssign.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitAugAssign(this);
}
// in src/org/python/antlr/ast/AugAssign.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (target != null)
target.accept(visitor);
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/ast/ErrorMod.java
public void traverse(VisitorIF visitor) throws Exception {
//no op.
}
// in src/org/python/antlr/ast/Assert.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitAssert(this);
}
// in src/org/python/antlr/ast/Assert.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (test != null)
test.accept(visitor);
if (msg != null)
msg.accept(visitor);
}
// in src/org/python/antlr/ast/Print.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitPrint(this);
}
// in src/org/python/antlr/ast/Print.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (dest != null)
dest.accept(visitor);
if (values != null) {
for (PythonTree t : values) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/For.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitFor(this);
}
// in src/org/python/antlr/ast/For.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (target != null)
target.accept(visitor);
if (iter != null)
iter.accept(visitor);
if (body != null) {
for (PythonTree t : body) {
if (t != null)
t.accept(visitor);
}
}
if (orelse != null) {
for (PythonTree t : orelse) {
if (t != null)
t.accept(visitor);
}
}
}
// in src/org/python/antlr/ast/Expression.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitExpression(this);
}
// in src/org/python/antlr/ast/Expression.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (body != null)
body.accept(visitor);
}
// in src/org/python/antlr/ast/Index.java
public <R> R accept(VisitorIF<R> visitor) throws Exception {
return visitor.visitIndex(this);
}
// in src/org/python/antlr/ast/Index.java
public void traverse(VisitorIF<?> visitor) throws Exception {
if (value != null)
value.accept(visitor);
}
// in src/org/python/antlr/Visitor.java
public void traverse(PythonTree node) throws Exception {
node.traverse(this);
}
// in src/org/python/antlr/Visitor.java
public void visit(PythonTree[] nodes) throws Exception {
for (int i = 0; i < nodes.length; i++) {
visit(nodes[i]);
}
}
// in src/org/python/antlr/Visitor.java
public Object visit(PythonTree node) throws Exception {
Object ret = node.accept(this);
return ret;
}
// in src/org/python/antlr/Visitor.java
protected Object unhandled_node(PythonTree node) throws Exception {
return this;
}
// in src/org/python/modules/ucnhash.java
public static void loadTables() throws Exception {
InputStream instream = ucnhash.class.
getResourceAsStream("ucnhash.dat");
if (instream == null)
throw new IOException("Unicode name database not found: " +
"ucnhash.dat");
DataInputStream in = new DataInputStream(
new BufferedInputStream(instream));
n = in.readShort();
m = in.readShort();
minchar= in.readShort();
maxchar = in.readShort();
alphasz = in.readShort();
maxlen = in.readShort();
maxidx = maxlen*alphasz-minchar;
G = readShortTable(in);
if (in.readShort() != 3)
throw new IOException("UnicodeNameMap file corrupt, " +
"unknown dimension");
T0 = readShortTable(in);
T1 = readShortTable(in);
T2 = readShortTable(in);
wordoffs = readShortTable(in);
worddata = readByteTable(in);
wordstart = in.readShort();
wordcutoff = in.readShort();
maxklen = in.readShort();
rawdata = readByteTable(in);
rawindex = readCharTable(in);
codepoint = readCharTable(in);
}
// in src/org/python/modules/ucnhash.java
public static void main(String[] args) throws Exception {
loadTables();
debug = true;
/*
System.out.println(getWord(hash("ARABIC")));
System.out.println(getWord(hash("SMALL")));
System.out.println(getWord(hash("YI")));
System.out.println(getWord(hash("SYLLABLE")));
System.out.println(getWord(hash("WITH")));
System.out.println(getWord(hash("LETTER")));
System.out.println(lookup("NULL"));
System.out.println(lookup("LATIN CAPITAL LETTER AFRICAN D"));
System.out.println(lookup("GURMUKHI TIPPI"));
System.out.println(lookup("TIBETAN MARK GTER YIG MGO -UM " +
"RNAM BCAD MA"));
System.out.println(lookup("HANGUL CHOSEONG PIEUP"));
System.out.println(lookup("SINGLE LOW-9 QUOTATION MARK"));
*/
System.out.println(lookup("BACKSPACE"));
// System.out.println(lookup("ACTIVATE SYMMETRIC SWAPPING"));
/*
System.out.println(lookup("LATIN CAPITAL LETTER A"));
System.out.println(lookup("GREATER-THAN SIGN"));
System.out.println(lookup("EURO-CURRENCY SIGN"));
*/
}
// in src/org/python/core/Py.java
public static void runMain(PyRunnable main, String[] args) throws Exception {
runMain(new PyRunnableBootstrap(main), args);
}
// in src/org/python/core/Py.java
public static void runMain(CodeBootstrap main, String[] args)
throws Exception {
PySystemState.initialize(null, null, args, main.getClass().getClassLoader());
try {
imp.createFromCode("__main__", CodeLoader.loadCode(main));
} catch (PyException e) {
Py.getSystemState().callExitFunc();
if (e.match(Py.SystemExit)) {
return;
}
throw e;
}
Py.getSystemState().callExitFunc();
}