+++ /dev/null
-#include "Python.h"\r
-#include "Python-ast.h"\r
-#include "code.h"\r
-#include "symtable.h"\r
-#include "structmember.h"\r
-\r
-/* error strings used for warnings */\r
-#define GLOBAL_AFTER_ASSIGN \\r
-"name '%.400s' is assigned to before global declaration"\r
-\r
-#define GLOBAL_AFTER_USE \\r
-"name '%.400s' is used prior to global declaration"\r
-\r
-#define IMPORT_STAR_WARNING "import * only allowed at module level"\r
-\r
-#define RETURN_VAL_IN_GENERATOR \\r
- "'return' with argument inside generator"\r
-\r
-\r
-static PySTEntryObject *\r
-ste_new(struct symtable *st, identifier name, _Py_block_ty block,\r
- void *key, int lineno)\r
-{\r
- PySTEntryObject *ste = NULL;\r
- PyObject *k;\r
-\r
- k = PyLong_FromVoidPtr(key);\r
- if (k == NULL)\r
- goto fail;\r
- ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);\r
- if (ste == NULL)\r
- goto fail;\r
- ste->ste_table = st;\r
- ste->ste_id = k;\r
-\r
- ste->ste_name = name;\r
- Py_INCREF(name);\r
-\r
- ste->ste_symbols = NULL;\r
- ste->ste_varnames = NULL;\r
- ste->ste_children = NULL;\r
-\r
- ste->ste_symbols = PyDict_New();\r
- if (ste->ste_symbols == NULL)\r
- goto fail;\r
-\r
- ste->ste_varnames = PyList_New(0);\r
- if (ste->ste_varnames == NULL)\r
- goto fail;\r
-\r
- ste->ste_children = PyList_New(0);\r
- if (ste->ste_children == NULL)\r
- goto fail;\r
-\r
- ste->ste_type = block;\r
- ste->ste_unoptimized = 0;\r
- ste->ste_nested = 0;\r
- ste->ste_free = 0;\r
- ste->ste_varargs = 0;\r
- ste->ste_varkeywords = 0;\r
- ste->ste_opt_lineno = 0;\r
- ste->ste_tmpname = 0;\r
- ste->ste_lineno = lineno;\r
-\r
- if (st->st_cur != NULL &&\r
- (st->st_cur->ste_nested ||\r
- st->st_cur->ste_type == FunctionBlock))\r
- ste->ste_nested = 1;\r
- ste->ste_child_free = 0;\r
- ste->ste_generator = 0;\r
- ste->ste_returns_value = 0;\r
-\r
- if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)\r
- goto fail;\r
-\r
- return ste;\r
- fail:\r
- Py_XDECREF(ste);\r
- return NULL;\r
-}\r
-\r
-static PyObject *\r
-ste_repr(PySTEntryObject *ste)\r
-{\r
- char buf[256];\r
-\r
- PyOS_snprintf(buf, sizeof(buf),\r
- "<symtable entry %.100s(%ld), line %d>",\r
- PyString_AS_STRING(ste->ste_name),\r
- PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);\r
- return PyString_FromString(buf);\r
-}\r
-\r
-static void\r
-ste_dealloc(PySTEntryObject *ste)\r
-{\r
- ste->ste_table = NULL;\r
- Py_XDECREF(ste->ste_id);\r
- Py_XDECREF(ste->ste_name);\r
- Py_XDECREF(ste->ste_symbols);\r
- Py_XDECREF(ste->ste_varnames);\r
- Py_XDECREF(ste->ste_children);\r
- PyObject_Del(ste);\r
-}\r
-\r
-#define OFF(x) offsetof(PySTEntryObject, x)\r
-\r
-static PyMemberDef ste_memberlist[] = {\r
- {"id", T_OBJECT, OFF(ste_id), READONLY},\r
- {"name", T_OBJECT, OFF(ste_name), READONLY},\r
- {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},\r
- {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},\r
- {"children", T_OBJECT, OFF(ste_children), READONLY},\r
- {"optimized",T_INT, OFF(ste_unoptimized), READONLY},\r
- {"nested", T_INT, OFF(ste_nested), READONLY},\r
- {"type", T_INT, OFF(ste_type), READONLY},\r
- {"lineno", T_INT, OFF(ste_lineno), READONLY},\r
- {NULL}\r
-};\r
-\r
-PyTypeObject PySTEntry_Type = {\r
- PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
- "symtable entry",\r
- sizeof(PySTEntryObject),\r
- 0,\r
- (destructor)ste_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- 0, /* tp_compare */\r
- (reprfunc)ste_repr, /* tp_repr */\r
- 0, /* tp_as_number */\r
- 0, /* tp_as_sequence */\r
- 0, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- 0, /* tp_call */\r
- 0, /* tp_str */\r
- PyObject_GenericGetAttr, /* tp_getattro */\r
- 0, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT, /* tp_flags */\r
- 0, /* tp_doc */\r
- 0, /* tp_traverse */\r
- 0, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- 0, /* tp_iter */\r
- 0, /* tp_iternext */\r
- 0, /* tp_methods */\r
- ste_memberlist, /* tp_members */\r
- 0, /* tp_getset */\r
- 0, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- 0, /* tp_dictoffset */\r
- 0, /* tp_init */\r
- 0, /* tp_alloc */\r
- 0, /* tp_new */\r
-};\r
-\r
-static int symtable_analyze(struct symtable *st);\r
-static int symtable_warn(struct symtable *st, char *msg, int lineno);\r
-static int symtable_enter_block(struct symtable *st, identifier name,\r
- _Py_block_ty block, void *ast, int lineno);\r
-static int symtable_exit_block(struct symtable *st, void *ast);\r
-static int symtable_visit_stmt(struct symtable *st, stmt_ty s);\r
-static int symtable_visit_expr(struct symtable *st, expr_ty s);\r
-static int symtable_visit_genexp(struct symtable *st, expr_ty s);\r
-static int symtable_visit_setcomp(struct symtable *st, expr_ty e);\r
-static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);\r
-static int symtable_visit_arguments(struct symtable *st, arguments_ty);\r
-static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);\r
-static int symtable_visit_alias(struct symtable *st, alias_ty);\r
-static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);\r
-static int symtable_visit_keyword(struct symtable *st, keyword_ty);\r
-static int symtable_visit_slice(struct symtable *st, slice_ty);\r
-static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);\r
-static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);\r
-static int symtable_implicit_arg(struct symtable *st, int pos);\r
-\r
-\r
-static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,\r
- dictcomp = NULL;\r
-\r
-#define GET_IDENTIFIER(VAR) \\r
- ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))\r
-\r
-#define DUPLICATE_ARGUMENT \\r
-"duplicate argument '%s' in function definition"\r
-\r
-static struct symtable *\r
-symtable_new(void)\r
-{\r
- struct symtable *st;\r
-\r
- st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));\r
- if (st == NULL)\r
- return NULL;\r
-\r
- st->st_filename = NULL;\r
- st->st_symbols = NULL;\r
-\r
- if ((st->st_stack = PyList_New(0)) == NULL)\r
- goto fail;\r
- if ((st->st_symbols = PyDict_New()) == NULL)\r
- goto fail;\r
- st->st_cur = NULL;\r
- st->st_private = NULL;\r
- return st;\r
- fail:\r
- PySymtable_Free(st);\r
- return NULL;\r
-}\r
-\r
-struct symtable *\r
-PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)\r
-{\r
- struct symtable *st = symtable_new();\r
- asdl_seq *seq;\r
- int i;\r
-\r
- if (st == NULL)\r
- return st;\r
- st->st_filename = filename;\r
- st->st_future = future;\r
- if (!GET_IDENTIFIER(top) ||\r
- !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {\r
- PySymtable_Free(st);\r
- return NULL;\r
- }\r
-\r
- st->st_top = st->st_cur;\r
- st->st_cur->ste_unoptimized = OPT_TOPLEVEL;\r
- /* Any other top-level initialization? */\r
- switch (mod->kind) {\r
- case Module_kind:\r
- seq = mod->v.Module.body;\r
- for (i = 0; i < asdl_seq_LEN(seq); i++)\r
- if (!symtable_visit_stmt(st,\r
- (stmt_ty)asdl_seq_GET(seq, i)))\r
- goto error;\r
- break;\r
- case Expression_kind:\r
- if (!symtable_visit_expr(st, mod->v.Expression.body))\r
- goto error;\r
- break;\r
- case Interactive_kind:\r
- seq = mod->v.Interactive.body;\r
- for (i = 0; i < asdl_seq_LEN(seq); i++)\r
- if (!symtable_visit_stmt(st,\r
- (stmt_ty)asdl_seq_GET(seq, i)))\r
- goto error;\r
- break;\r
- case Suite_kind:\r
- PyErr_SetString(PyExc_RuntimeError,\r
- "this compiler does not handle Suites");\r
- goto error;\r
- }\r
- if (!symtable_exit_block(st, (void *)mod)) {\r
- PySymtable_Free(st);\r
- return NULL;\r
- }\r
- if (symtable_analyze(st))\r
- return st;\r
- PySymtable_Free(st);\r
- return NULL;\r
- error:\r
- (void) symtable_exit_block(st, (void *)mod);\r
- PySymtable_Free(st);\r
- return NULL;\r
-}\r
-\r
-void\r
-PySymtable_Free(struct symtable *st)\r
-{\r
- Py_XDECREF(st->st_symbols);\r
- Py_XDECREF(st->st_stack);\r
- PyMem_Free((void *)st);\r
-}\r
-\r
-PySTEntryObject *\r
-PySymtable_Lookup(struct symtable *st, void *key)\r
-{\r
- PyObject *k, *v;\r
-\r
- k = PyLong_FromVoidPtr(key);\r
- if (k == NULL)\r
- return NULL;\r
- v = PyDict_GetItem(st->st_symbols, k);\r
- if (v) {\r
- assert(PySTEntry_Check(v));\r
- Py_INCREF(v);\r
- }\r
- else {\r
- PyErr_SetString(PyExc_KeyError,\r
- "unknown symbol table entry");\r
- }\r
-\r
- Py_DECREF(k);\r
- return (PySTEntryObject *)v;\r
-}\r
-\r
-int\r
-PyST_GetScope(PySTEntryObject *ste, PyObject *name)\r
-{\r
- PyObject *v = PyDict_GetItem(ste->ste_symbols, name);\r
- if (!v)\r
- return 0;\r
- assert(PyInt_Check(v));\r
- return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;\r
-}\r
-\r
-\r
-/* Analyze raw symbol information to determine scope of each name.\r
-\r
- The next several functions are helpers for PySymtable_Analyze(),\r
- which determines whether a name is local, global, or free. In addition,\r
- it determines which local variables are cell variables; they provide\r
- bindings that are used for free variables in enclosed blocks.\r
-\r
- There are also two kinds of free variables, implicit and explicit. An\r
- explicit global is declared with the global statement. An implicit\r
- global is a free variable for which the compiler has found no binding\r
- in an enclosing function scope. The implicit global is either a global\r
- or a builtin. Python's module and class blocks use the xxx_NAME opcodes\r
- to handle these names to implement slightly odd semantics. In such a\r
- block, the name is treated as global until it is assigned to; then it\r
- is treated as a local.\r
-\r
- The symbol table requires two passes to determine the scope of each name.\r
- The first pass collects raw facts from the AST: the name is a parameter\r
- here, the name is used by not defined here, etc. The second pass analyzes\r
- these facts during a pass over the PySTEntryObjects created during pass 1.\r
-\r
- When a function is entered during the second pass, the parent passes\r
- the set of all name bindings visible to its children. These bindings\r
- are used to determine if the variable is free or an implicit global.\r
- After doing the local analysis, it analyzes each of its child blocks\r
- using an updated set of name bindings.\r
-\r
- The children update the free variable set. If a local variable is free\r
- in a child, the variable is marked as a cell. The current function must\r
- provide runtime storage for the variable that may outlive the function's\r
- frame. Cell variables are removed from the free set before the analyze\r
- function returns to its parent.\r
-\r
- The sets of bound and free variables are implemented as dictionaries\r
- mapping strings to None.\r
-*/\r
-\r
-#define SET_SCOPE(DICT, NAME, I) { \\r
- PyObject *o = PyInt_FromLong(I); \\r
- if (!o) \\r
- return 0; \\r
- if (PyDict_SetItem((DICT), (NAME), o) < 0) { \\r
- Py_DECREF(o); \\r
- return 0; \\r
- } \\r
- Py_DECREF(o); \\r
-}\r
-\r
-/* Decide on scope of name, given flags.\r
-\r
- The namespace dictionaries may be modified to record information\r
- about the new name. For example, a new global will add an entry to\r
- global. A name that was global can be changed to local.\r
-*/\r
-\r
-static int\r
-analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,\r
- PyObject *bound, PyObject *local, PyObject *free,\r
- PyObject *global)\r
-{\r
- if (flags & DEF_GLOBAL) {\r
- if (flags & DEF_PARAM) {\r
- PyErr_Format(PyExc_SyntaxError,\r
- "name '%s' is local and global",\r
- PyString_AS_STRING(name));\r
- PyErr_SyntaxLocation(ste->ste_table->st_filename,\r
- ste->ste_lineno);\r
-\r
- return 0;\r
- }\r
- SET_SCOPE(dict, name, GLOBAL_EXPLICIT);\r
- if (PyDict_SetItem(global, name, Py_None) < 0)\r
- return 0;\r
- if (bound && PyDict_GetItem(bound, name)) {\r
- if (PyDict_DelItem(bound, name) < 0)\r
- return 0;\r
- }\r
- return 1;\r
- }\r
- if (flags & DEF_BOUND) {\r
- SET_SCOPE(dict, name, LOCAL);\r
- if (PyDict_SetItem(local, name, Py_None) < 0)\r
- return 0;\r
- if (PyDict_GetItem(global, name)) {\r
- if (PyDict_DelItem(global, name) < 0)\r
- return 0;\r
- }\r
- return 1;\r
- }\r
- /* If an enclosing block has a binding for this name, it\r
- is a free variable rather than a global variable.\r
- Note that having a non-NULL bound implies that the block\r
- is nested.\r
- */\r
- if (bound && PyDict_GetItem(bound, name)) {\r
- SET_SCOPE(dict, name, FREE);\r
- ste->ste_free = 1;\r
- if (PyDict_SetItem(free, name, Py_None) < 0)\r
- return 0;\r
- return 1;\r
- }\r
- /* If a parent has a global statement, then call it global\r
- explicit? It could also be global implicit.\r
- */\r
- else if (global && PyDict_GetItem(global, name)) {\r
- SET_SCOPE(dict, name, GLOBAL_IMPLICIT);\r
- return 1;\r
- }\r
- else {\r
- if (ste->ste_nested)\r
- ste->ste_free = 1;\r
- SET_SCOPE(dict, name, GLOBAL_IMPLICIT);\r
- return 1;\r
- }\r
- /* Should never get here. */\r
- PyErr_Format(PyExc_SystemError, "failed to set scope for %s",\r
- PyString_AS_STRING(name));\r
- return 0;\r
-}\r
-\r
-#undef SET_SCOPE\r
-\r
-/* If a name is defined in free and also in locals, then this block\r
- provides the binding for the free variable. The name should be\r
- marked CELL in this block and removed from the free list.\r
-\r
- Note that the current block's free variables are included in free.\r
- That's safe because no name can be free and local in the same scope.\r
-*/\r
-\r
-static int\r
-analyze_cells(PyObject *scope, PyObject *free)\r
-{\r
- PyObject *name, *v, *w;\r
- int success = 0;\r
- Py_ssize_t pos = 0;\r
-\r
- w = PyInt_FromLong(CELL);\r
- if (!w)\r
- return 0;\r
- while (PyDict_Next(scope, &pos, &name, &v)) {\r
- long flags;\r
- assert(PyInt_Check(v));\r
- flags = PyInt_AS_LONG(v);\r
- if (flags != LOCAL)\r
- continue;\r
- if (!PyDict_GetItem(free, name))\r
- continue;\r
- /* Replace LOCAL with CELL for this name, and remove\r
- from free. It is safe to replace the value of name\r
- in the dict, because it will not cause a resize.\r
- */\r
- if (PyDict_SetItem(scope, name, w) < 0)\r
- goto error;\r
- if (!PyDict_DelItem(free, name) < 0)\r
- goto error;\r
- }\r
- success = 1;\r
- error:\r
- Py_DECREF(w);\r
- return success;\r
-}\r
-\r
-/* Check for illegal statements in unoptimized namespaces */\r
-static int\r
-check_unoptimized(const PySTEntryObject* ste) {\r
- char buf[300];\r
- const char* trailer;\r
-\r
- if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized\r
- || !(ste->ste_free || ste->ste_child_free))\r
- return 1;\r
-\r
- trailer = (ste->ste_child_free ?\r
- "contains a nested function with free variables" :\r
- "is a nested function");\r
-\r
- switch (ste->ste_unoptimized) {\r
- case OPT_TOPLEVEL: /* exec / import * at top-level is fine */\r
- case OPT_EXEC: /* qualified exec is fine */\r
- return 1;\r
- case OPT_IMPORT_STAR:\r
- PyOS_snprintf(buf, sizeof(buf),\r
- "import * is not allowed in function '%.100s' "\r
- "because it %s",\r
- PyString_AS_STRING(ste->ste_name), trailer);\r
- break;\r
- case OPT_BARE_EXEC:\r
- PyOS_snprintf(buf, sizeof(buf),\r
- "unqualified exec is not allowed in function "\r
- "'%.100s' it %s",\r
- PyString_AS_STRING(ste->ste_name), trailer);\r
- break;\r
- default:\r
- PyOS_snprintf(buf, sizeof(buf),\r
- "function '%.100s' uses import * and bare exec, "\r
- "which are illegal because it %s",\r
- PyString_AS_STRING(ste->ste_name), trailer);\r
- break;\r
- }\r
-\r
- PyErr_SetString(PyExc_SyntaxError, buf);\r
- PyErr_SyntaxLocation(ste->ste_table->st_filename,\r
- ste->ste_opt_lineno);\r
- return 0;\r
-}\r
-\r
-/* Enter the final scope information into the st_symbols dict.\r
- *\r
- * All arguments are dicts. Modifies symbols, others are read-only.\r
-*/\r
-static int\r
-update_symbols(PyObject *symbols, PyObject *scope,\r
- PyObject *bound, PyObject *free, int classflag)\r
-{\r
- PyObject *name, *v, *u, *w, *free_value = NULL;\r
- Py_ssize_t pos = 0;\r
-\r
- while (PyDict_Next(symbols, &pos, &name, &v)) {\r
- long i, flags;\r
- assert(PyInt_Check(v));\r
- flags = PyInt_AS_LONG(v);\r
- w = PyDict_GetItem(scope, name);\r
- assert(w && PyInt_Check(w));\r
- i = PyInt_AS_LONG(w);\r
- flags |= (i << SCOPE_OFF);\r
- u = PyInt_FromLong(flags);\r
- if (!u)\r
- return 0;\r
- if (PyDict_SetItem(symbols, name, u) < 0) {\r
- Py_DECREF(u);\r
- return 0;\r
- }\r
- Py_DECREF(u);\r
- }\r
-\r
- free_value = PyInt_FromLong(FREE << SCOPE_OFF);\r
- if (!free_value)\r
- return 0;\r
-\r
- /* add a free variable when it's only use is for creating a closure */\r
- pos = 0;\r
- while (PyDict_Next(free, &pos, &name, &v)) {\r
- PyObject *o = PyDict_GetItem(symbols, name);\r
-\r
- if (o) {\r
- /* It could be a free variable in a method of\r
- the class that has the same name as a local\r
- or global in the class scope.\r
- */\r
- if (classflag &&\r
- PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {\r
- long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;\r
- o = PyInt_FromLong(i);\r
- if (!o) {\r
- Py_DECREF(free_value);\r
- return 0;\r
- }\r
- if (PyDict_SetItem(symbols, name, o) < 0) {\r
- Py_DECREF(o);\r
- Py_DECREF(free_value);\r
- return 0;\r
- }\r
- Py_DECREF(o);\r
- }\r
- /* else it's not free, probably a cell */\r
- continue;\r
- }\r
- if (!PyDict_GetItem(bound, name))\r
- continue; /* it's a global */\r
-\r
- if (PyDict_SetItem(symbols, name, free_value) < 0) {\r
- Py_DECREF(free_value);\r
- return 0;\r
- }\r
- }\r
- Py_DECREF(free_value);\r
- return 1;\r
-}\r
-\r
-/* Make final symbol table decisions for block of ste.\r
-\r
- Arguments:\r
- ste -- current symtable entry (input/output)\r
- bound -- set of variables bound in enclosing scopes (input). bound\r
- is NULL for module blocks.\r
- free -- set of free variables in enclosed scopes (output)\r
- globals -- set of declared global variables in enclosing scopes (input)\r
-\r
- The implementation uses two mutually recursive functions,\r
- analyze_block() and analyze_child_block(). analyze_block() is\r
- responsible for analyzing the individual names defined in a block.\r
- analyze_child_block() prepares temporary namespace dictionaries\r
- used to evaluated nested blocks.\r
-\r
- The two functions exist because a child block should see the name\r
- bindings of its enclosing blocks, but those bindings should not\r
- propagate back to a parent block.\r
-*/\r
-\r
-static int\r
-analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,\r
- PyObject *global, PyObject* child_free);\r
-\r
-static int\r
-analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,\r
- PyObject *global)\r
-{\r
- PyObject *name, *v, *local = NULL, *scope = NULL;\r
- PyObject *newbound = NULL, *newglobal = NULL;\r
- PyObject *newfree = NULL, *allfree = NULL;\r
- int i, success = 0;\r
- Py_ssize_t pos = 0;\r
-\r
- local = PyDict_New(); /* collect new names bound in block */\r
- if (!local)\r
- goto error;\r
- scope = PyDict_New(); /* collect scopes defined for each name */\r
- if (!scope)\r
- goto error;\r
-\r
- /* Allocate new global and bound variable dictionaries. These\r
- dictionaries hold the names visible in nested blocks. For\r
- ClassBlocks, the bound and global names are initialized\r
- before analyzing names, because class bindings aren't\r
- visible in methods. For other blocks, they are initialized\r
- after names are analyzed.\r
- */\r
-\r
- /* TODO(jhylton): Package these dicts in a struct so that we\r
- can write reasonable helper functions?\r
- */\r
- newglobal = PyDict_New();\r
- if (!newglobal)\r
- goto error;\r
- newbound = PyDict_New();\r
- if (!newbound)\r
- goto error;\r
- newfree = PyDict_New();\r
- if (!newfree)\r
- goto error;\r
-\r
- if (ste->ste_type == ClassBlock) {\r
- if (PyDict_Update(newglobal, global) < 0)\r
- goto error;\r
- if (bound)\r
- if (PyDict_Update(newbound, bound) < 0)\r
- goto error;\r
- }\r
-\r
- while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {\r
- long flags = PyInt_AS_LONG(v);\r
- if (!analyze_name(ste, scope, name, flags,\r
- bound, local, free, global))\r
- goto error;\r
- }\r
-\r
- if (ste->ste_type != ClassBlock) {\r
- if (ste->ste_type == FunctionBlock) {\r
- if (PyDict_Update(newbound, local) < 0)\r
- goto error;\r
- }\r
- if (bound) {\r
- if (PyDict_Update(newbound, bound) < 0)\r
- goto error;\r
- }\r
- if (PyDict_Update(newglobal, global) < 0)\r
- goto error;\r
- }\r
-\r
- /* Recursively call analyze_block() on each child block.\r
-\r
- newbound, newglobal now contain the names visible in\r
- nested blocks. The free variables in the children will\r
- be collected in allfree.\r
- */\r
- allfree = PyDict_New();\r
- if (!allfree)\r
- goto error;\r
- for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {\r
- PyObject *c = PyList_GET_ITEM(ste->ste_children, i);\r
- PySTEntryObject* entry;\r
- assert(c && PySTEntry_Check(c));\r
- entry = (PySTEntryObject*)c;\r
- if (!analyze_child_block(entry, newbound, newfree, newglobal,\r
- allfree))\r
- goto error;\r
- if (entry->ste_free || entry->ste_child_free)\r
- ste->ste_child_free = 1;\r
- }\r
-\r
- if (PyDict_Update(newfree, allfree) < 0)\r
- goto error;\r
- if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))\r
- goto error;\r
- if (!update_symbols(ste->ste_symbols, scope, bound, newfree,\r
- ste->ste_type == ClassBlock))\r
- goto error;\r
- if (!check_unoptimized(ste))\r
- goto error;\r
-\r
- if (PyDict_Update(free, newfree) < 0)\r
- goto error;\r
- success = 1;\r
- error:\r
- Py_XDECREF(local);\r
- Py_XDECREF(scope);\r
- Py_XDECREF(newbound);\r
- Py_XDECREF(newglobal);\r
- Py_XDECREF(newfree);\r
- Py_XDECREF(allfree);\r
- if (!success)\r
- assert(PyErr_Occurred());\r
- return success;\r
-}\r
-\r
-static int\r
-analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,\r
- PyObject *global, PyObject* child_free)\r
-{\r
- PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;\r
-\r
- /* Copy the bound and global dictionaries.\r
-\r
- These dictionary are used by all blocks enclosed by the\r
- current block. The analyze_block() call modifies these\r
- dictionaries.\r
-\r
- */\r
- temp_bound = PyDict_New();\r
- if (!temp_bound)\r
- goto error;\r
- if (PyDict_Update(temp_bound, bound) < 0)\r
- goto error;\r
- temp_free = PyDict_New();\r
- if (!temp_free)\r
- goto error;\r
- if (PyDict_Update(temp_free, free) < 0)\r
- goto error;\r
- temp_global = PyDict_New();\r
- if (!temp_global)\r
- goto error;\r
- if (PyDict_Update(temp_global, global) < 0)\r
- goto error;\r
-\r
- if (!analyze_block(entry, temp_bound, temp_free, temp_global))\r
- goto error;\r
- if (PyDict_Update(child_free, temp_free) < 0)\r
- goto error;\r
- Py_DECREF(temp_bound);\r
- Py_DECREF(temp_free);\r
- Py_DECREF(temp_global);\r
- return 1;\r
- error:\r
- Py_XDECREF(temp_bound);\r
- Py_XDECREF(temp_free);\r
- Py_XDECREF(temp_global);\r
- return 0;\r
-}\r
-\r
-static int\r
-symtable_analyze(struct symtable *st)\r
-{\r
- PyObject *free, *global;\r
- int r;\r
-\r
- free = PyDict_New();\r
- if (!free)\r
- return 0;\r
- global = PyDict_New();\r
- if (!global) {\r
- Py_DECREF(free);\r
- return 0;\r
- }\r
- r = analyze_block(st->st_top, NULL, free, global);\r
- Py_DECREF(free);\r
- Py_DECREF(global);\r
- return r;\r
-}\r
-\r
-\r
-static int\r
-symtable_warn(struct symtable *st, char *msg, int lineno)\r
-{\r
- if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,\r
- lineno, NULL, NULL) < 0) {\r
- if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {\r
- PyErr_SetString(PyExc_SyntaxError, msg);\r
- PyErr_SyntaxLocation(st->st_filename,\r
- st->st_cur->ste_lineno);\r
- }\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-/* symtable_enter_block() gets a reference via ste_new.\r
- This reference is released when the block is exited, via the DECREF\r
- in symtable_exit_block().\r
-*/\r
-\r
-static int\r
-symtable_exit_block(struct symtable *st, void *ast)\r
-{\r
- Py_ssize_t end;\r
-\r
- Py_CLEAR(st->st_cur);\r
- end = PyList_GET_SIZE(st->st_stack) - 1;\r
- if (end >= 0) {\r
- st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,\r
- end);\r
- if (st->st_cur == NULL)\r
- return 0;\r
- Py_INCREF(st->st_cur);\r
- if (PySequence_DelItem(st->st_stack, end) < 0)\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,\r
- void *ast, int lineno)\r
-{\r
- PySTEntryObject *prev = NULL;\r
-\r
- if (st->st_cur) {\r
- prev = st->st_cur;\r
- if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {\r
- return 0;\r
- }\r
- Py_DECREF(st->st_cur);\r
- }\r
- st->st_cur = ste_new(st, name, block, ast, lineno);\r
- if (st->st_cur == NULL)\r
- return 0;\r
- if (block == ModuleBlock)\r
- st->st_global = st->st_cur->ste_symbols;\r
- if (prev) {\r
- if (PyList_Append(prev->ste_children,\r
- (PyObject *)st->st_cur) < 0) {\r
- return 0;\r
- }\r
- }\r
- return 1;\r
-}\r
-\r
-static long\r
-symtable_lookup(struct symtable *st, PyObject *name)\r
-{\r
- PyObject *o;\r
- PyObject *mangled = _Py_Mangle(st->st_private, name);\r
- if (!mangled)\r
- return 0;\r
- o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);\r
- Py_DECREF(mangled);\r
- if (!o)\r
- return 0;\r
- return PyInt_AsLong(o);\r
-}\r
-\r
-static int\r
-symtable_add_def(struct symtable *st, PyObject *name, int flag)\r
-{\r
- PyObject *o;\r
- PyObject *dict;\r
- long val;\r
- PyObject *mangled = _Py_Mangle(st->st_private, name);\r
-\r
- if (!mangled)\r
- return 0;\r
- dict = st->st_cur->ste_symbols;\r
- if ((o = PyDict_GetItem(dict, mangled))) {\r
- val = PyInt_AS_LONG(o);\r
- if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {\r
- /* Is it better to use 'mangled' or 'name' here? */\r
- PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,\r
- PyString_AsString(name));\r
- PyErr_SyntaxLocation(st->st_filename,\r
- st->st_cur->ste_lineno);\r
- goto error;\r
- }\r
- val |= flag;\r
- } else\r
- val = flag;\r
- o = PyInt_FromLong(val);\r
- if (o == NULL)\r
- goto error;\r
- if (PyDict_SetItem(dict, mangled, o) < 0) {\r
- Py_DECREF(o);\r
- goto error;\r
- }\r
- Py_DECREF(o);\r
-\r
- if (flag & DEF_PARAM) {\r
- if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)\r
- goto error;\r
- } else if (flag & DEF_GLOBAL) {\r
- /* XXX need to update DEF_GLOBAL for other flags too;\r
- perhaps only DEF_FREE_GLOBAL */\r
- val = flag;\r
- if ((o = PyDict_GetItem(st->st_global, mangled))) {\r
- val |= PyInt_AS_LONG(o);\r
- }\r
- o = PyInt_FromLong(val);\r
- if (o == NULL)\r
- goto error;\r
- if (PyDict_SetItem(st->st_global, mangled, o) < 0) {\r
- Py_DECREF(o);\r
- goto error;\r
- }\r
- Py_DECREF(o);\r
- }\r
- Py_DECREF(mangled);\r
- return 1;\r
-\r
-error:\r
- Py_DECREF(mangled);\r
- return 0;\r
-}\r
-\r
-/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.\r
- They use the ASDL name to synthesize the name of the C type and the visit\r
- function.\r
-\r
- VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is\r
- useful if the first node in the sequence requires special treatment.\r
-*/\r
-\r
-#define VISIT(ST, TYPE, V) \\r
- if (!symtable_visit_ ## TYPE((ST), (V))) \\r
- return 0;\r
-\r
-#define VISIT_IN_BLOCK(ST, TYPE, V, S) \\r
- if (!symtable_visit_ ## TYPE((ST), (V))) { \\r
- symtable_exit_block((ST), (S)); \\r
- return 0; \\r
- }\r
-\r
-#define VISIT_SEQ(ST, TYPE, SEQ) { \\r
- int i; \\r
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \\r
- for (i = 0; i < asdl_seq_LEN(seq); i++) { \\r
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \\r
- if (!symtable_visit_ ## TYPE((ST), elt)) \\r
- return 0; \\r
- } \\r
-}\r
-\r
-#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \\r
- int i; \\r
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \\r
- for (i = 0; i < asdl_seq_LEN(seq); i++) { \\r
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \\r
- if (!symtable_visit_ ## TYPE((ST), elt)) { \\r
- symtable_exit_block((ST), (S)); \\r
- return 0; \\r
- } \\r
- } \\r
-}\r
-\r
-#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \\r
- int i; \\r
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \\r
- for (i = (START); i < asdl_seq_LEN(seq); i++) { \\r
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \\r
- if (!symtable_visit_ ## TYPE((ST), elt)) \\r
- return 0; \\r
- } \\r
-}\r
-\r
-#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \\r
- int i; \\r
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \\r
- for (i = (START); i < asdl_seq_LEN(seq); i++) { \\r
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \\r
- if (!symtable_visit_ ## TYPE((ST), elt)) { \\r
- symtable_exit_block((ST), (S)); \\r
- return 0; \\r
- } \\r
- } \\r
-}\r
-\r
-static int\r
-symtable_visit_stmt(struct symtable *st, stmt_ty s)\r
-{\r
- switch (s->kind) {\r
- case FunctionDef_kind:\r
- if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))\r
- return 0;\r
- if (s->v.FunctionDef.args->defaults)\r
- VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);\r
- if (s->v.FunctionDef.decorator_list)\r
- VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);\r
- if (!symtable_enter_block(st, s->v.FunctionDef.name,\r
- FunctionBlock, (void *)s, s->lineno))\r
- return 0;\r
- VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);\r
- VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);\r
- if (!symtable_exit_block(st, s))\r
- return 0;\r
- break;\r
- case ClassDef_kind: {\r
- PyObject *tmp;\r
- if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))\r
- return 0;\r
- VISIT_SEQ(st, expr, s->v.ClassDef.bases);\r
- if (s->v.ClassDef.decorator_list)\r
- VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);\r
- if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,\r
- (void *)s, s->lineno))\r
- return 0;\r
- tmp = st->st_private;\r
- st->st_private = s->v.ClassDef.name;\r
- VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);\r
- st->st_private = tmp;\r
- if (!symtable_exit_block(st, s))\r
- return 0;\r
- break;\r
- }\r
- case Return_kind:\r
- if (s->v.Return.value) {\r
- VISIT(st, expr, s->v.Return.value);\r
- st->st_cur->ste_returns_value = 1;\r
- if (st->st_cur->ste_generator) {\r
- PyErr_SetString(PyExc_SyntaxError,\r
- RETURN_VAL_IN_GENERATOR);\r
- PyErr_SyntaxLocation(st->st_filename,\r
- s->lineno);\r
- return 0;\r
- }\r
- }\r
- break;\r
- case Delete_kind:\r
- VISIT_SEQ(st, expr, s->v.Delete.targets);\r
- break;\r
- case Assign_kind:\r
- VISIT_SEQ(st, expr, s->v.Assign.targets);\r
- VISIT(st, expr, s->v.Assign.value);\r
- break;\r
- case AugAssign_kind:\r
- VISIT(st, expr, s->v.AugAssign.target);\r
- VISIT(st, expr, s->v.AugAssign.value);\r
- break;\r
- case Print_kind:\r
- if (s->v.Print.dest)\r
- VISIT(st, expr, s->v.Print.dest);\r
- VISIT_SEQ(st, expr, s->v.Print.values);\r
- break;\r
- case For_kind:\r
- VISIT(st, expr, s->v.For.target);\r
- VISIT(st, expr, s->v.For.iter);\r
- VISIT_SEQ(st, stmt, s->v.For.body);\r
- if (s->v.For.orelse)\r
- VISIT_SEQ(st, stmt, s->v.For.orelse);\r
- break;\r
- case While_kind:\r
- VISIT(st, expr, s->v.While.test);\r
- VISIT_SEQ(st, stmt, s->v.While.body);\r
- if (s->v.While.orelse)\r
- VISIT_SEQ(st, stmt, s->v.While.orelse);\r
- break;\r
- case If_kind:\r
- /* XXX if 0: and lookup_yield() hacks */\r
- VISIT(st, expr, s->v.If.test);\r
- VISIT_SEQ(st, stmt, s->v.If.body);\r
- if (s->v.If.orelse)\r
- VISIT_SEQ(st, stmt, s->v.If.orelse);\r
- break;\r
- case Raise_kind:\r
- if (s->v.Raise.type) {\r
- VISIT(st, expr, s->v.Raise.type);\r
- if (s->v.Raise.inst) {\r
- VISIT(st, expr, s->v.Raise.inst);\r
- if (s->v.Raise.tback)\r
- VISIT(st, expr, s->v.Raise.tback);\r
- }\r
- }\r
- break;\r
- case TryExcept_kind:\r
- VISIT_SEQ(st, stmt, s->v.TryExcept.body);\r
- VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);\r
- VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);\r
- break;\r
- case TryFinally_kind:\r
- VISIT_SEQ(st, stmt, s->v.TryFinally.body);\r
- VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);\r
- break;\r
- case Assert_kind:\r
- VISIT(st, expr, s->v.Assert.test);\r
- if (s->v.Assert.msg)\r
- VISIT(st, expr, s->v.Assert.msg);\r
- break;\r
- case Import_kind:\r
- VISIT_SEQ(st, alias, s->v.Import.names);\r
- /* XXX Don't have the lineno available inside\r
- visit_alias */\r
- if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)\r
- st->st_cur->ste_opt_lineno = s->lineno;\r
- break;\r
- case ImportFrom_kind:\r
- VISIT_SEQ(st, alias, s->v.ImportFrom.names);\r
- /* XXX Don't have the lineno available inside\r
- visit_alias */\r
- if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)\r
- st->st_cur->ste_opt_lineno = s->lineno;\r
- break;\r
- case Exec_kind:\r
- VISIT(st, expr, s->v.Exec.body);\r
- if (!st->st_cur->ste_opt_lineno)\r
- st->st_cur->ste_opt_lineno = s->lineno;\r
- if (s->v.Exec.globals) {\r
- st->st_cur->ste_unoptimized |= OPT_EXEC;\r
- VISIT(st, expr, s->v.Exec.globals);\r
- if (s->v.Exec.locals)\r
- VISIT(st, expr, s->v.Exec.locals);\r
- } else {\r
- st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;\r
- }\r
- break;\r
- case Global_kind: {\r
- int i;\r
- asdl_seq *seq = s->v.Global.names;\r
- for (i = 0; i < asdl_seq_LEN(seq); i++) {\r
- identifier name = (identifier)asdl_seq_GET(seq, i);\r
- char *c_name = PyString_AS_STRING(name);\r
- long cur = symtable_lookup(st, name);\r
- if (cur < 0)\r
- return 0;\r
- if (cur & (DEF_LOCAL | USE)) {\r
- char buf[256];\r
- if (cur & DEF_LOCAL)\r
- PyOS_snprintf(buf, sizeof(buf),\r
- GLOBAL_AFTER_ASSIGN,\r
- c_name);\r
- else\r
- PyOS_snprintf(buf, sizeof(buf),\r
- GLOBAL_AFTER_USE,\r
- c_name);\r
- if (!symtable_warn(st, buf, s->lineno))\r
- return 0;\r
- }\r
- if (!symtable_add_def(st, name, DEF_GLOBAL))\r
- return 0;\r
- }\r
- break;\r
- }\r
- case Expr_kind:\r
- VISIT(st, expr, s->v.Expr.value);\r
- break;\r
- case Pass_kind:\r
- case Break_kind:\r
- case Continue_kind:\r
- /* nothing to do here */\r
- break;\r
- case With_kind:\r
- VISIT(st, expr, s->v.With.context_expr);\r
- if (s->v.With.optional_vars) {\r
- VISIT(st, expr, s->v.With.optional_vars);\r
- }\r
- VISIT_SEQ(st, stmt, s->v.With.body);\r
- break;\r
- }\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_visit_expr(struct symtable *st, expr_ty e)\r
-{\r
- switch (e->kind) {\r
- case BoolOp_kind:\r
- VISIT_SEQ(st, expr, e->v.BoolOp.values);\r
- break;\r
- case BinOp_kind:\r
- VISIT(st, expr, e->v.BinOp.left);\r
- VISIT(st, expr, e->v.BinOp.right);\r
- break;\r
- case UnaryOp_kind:\r
- VISIT(st, expr, e->v.UnaryOp.operand);\r
- break;\r
- case Lambda_kind: {\r
- if (!GET_IDENTIFIER(lambda))\r
- return 0;\r
- if (e->v.Lambda.args->defaults)\r
- VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);\r
- if (!symtable_enter_block(st, lambda,\r
- FunctionBlock, (void *)e, e->lineno))\r
- return 0;\r
- VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);\r
- VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);\r
- if (!symtable_exit_block(st, (void *)e))\r
- return 0;\r
- break;\r
- }\r
- case IfExp_kind:\r
- VISIT(st, expr, e->v.IfExp.test);\r
- VISIT(st, expr, e->v.IfExp.body);\r
- VISIT(st, expr, e->v.IfExp.orelse);\r
- break;\r
- case Dict_kind:\r
- VISIT_SEQ(st, expr, e->v.Dict.keys);\r
- VISIT_SEQ(st, expr, e->v.Dict.values);\r
- break;\r
- case Set_kind:\r
- VISIT_SEQ(st, expr, e->v.Set.elts);\r
- break;\r
- case ListComp_kind:\r
- VISIT(st, expr, e->v.ListComp.elt);\r
- VISIT_SEQ(st, comprehension, e->v.ListComp.generators);\r
- break;\r
- case GeneratorExp_kind:\r
- if (!symtable_visit_genexp(st, e))\r
- return 0;\r
- break;\r
- case SetComp_kind:\r
- if (!symtable_visit_setcomp(st, e))\r
- return 0;\r
- break;\r
- case DictComp_kind:\r
- if (!symtable_visit_dictcomp(st, e))\r
- return 0;\r
- break;\r
- case Yield_kind:\r
- if (e->v.Yield.value)\r
- VISIT(st, expr, e->v.Yield.value);\r
- st->st_cur->ste_generator = 1;\r
- if (st->st_cur->ste_returns_value) {\r
- PyErr_SetString(PyExc_SyntaxError,\r
- RETURN_VAL_IN_GENERATOR);\r
- PyErr_SyntaxLocation(st->st_filename,\r
- e->lineno);\r
- return 0;\r
- }\r
- break;\r
- case Compare_kind:\r
- VISIT(st, expr, e->v.Compare.left);\r
- VISIT_SEQ(st, expr, e->v.Compare.comparators);\r
- break;\r
- case Call_kind:\r
- VISIT(st, expr, e->v.Call.func);\r
- VISIT_SEQ(st, expr, e->v.Call.args);\r
- VISIT_SEQ(st, keyword, e->v.Call.keywords);\r
- if (e->v.Call.starargs)\r
- VISIT(st, expr, e->v.Call.starargs);\r
- if (e->v.Call.kwargs)\r
- VISIT(st, expr, e->v.Call.kwargs);\r
- break;\r
- case Repr_kind:\r
- VISIT(st, expr, e->v.Repr.value);\r
- break;\r
- case Num_kind:\r
- case Str_kind:\r
- /* Nothing to do here. */\r
- break;\r
- /* The following exprs can be assignment targets. */\r
- case Attribute_kind:\r
- VISIT(st, expr, e->v.Attribute.value);\r
- break;\r
- case Subscript_kind:\r
- VISIT(st, expr, e->v.Subscript.value);\r
- VISIT(st, slice, e->v.Subscript.slice);\r
- break;\r
- case Name_kind:\r
- if (!symtable_add_def(st, e->v.Name.id,\r
- e->v.Name.ctx == Load ? USE : DEF_LOCAL))\r
- return 0;\r
- break;\r
- /* child nodes of List and Tuple will have expr_context set */\r
- case List_kind:\r
- VISIT_SEQ(st, expr, e->v.List.elts);\r
- break;\r
- case Tuple_kind:\r
- VISIT_SEQ(st, expr, e->v.Tuple.elts);\r
- break;\r
- }\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_implicit_arg(struct symtable *st, int pos)\r
-{\r
- PyObject *id = PyString_FromFormat(".%d", pos);\r
- if (id == NULL)\r
- return 0;\r
- if (!symtable_add_def(st, id, DEF_PARAM)) {\r
- Py_DECREF(id);\r
- return 0;\r
- }\r
- Py_DECREF(id);\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)\r
-{\r
- int i;\r
-\r
- /* go through all the toplevel arguments first */\r
- for (i = 0; i < asdl_seq_LEN(args); i++) {\r
- expr_ty arg = (expr_ty)asdl_seq_GET(args, i);\r
- if (arg->kind == Name_kind) {\r
- assert(arg->v.Name.ctx == Param ||\r
- (arg->v.Name.ctx == Store && !toplevel));\r
- if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))\r
- return 0;\r
- }\r
- else if (arg->kind == Tuple_kind) {\r
- assert(arg->v.Tuple.ctx == Store);\r
- if (toplevel) {\r
- if (!symtable_implicit_arg(st, i))\r
- return 0;\r
- }\r
- }\r
- else {\r
- PyErr_SetString(PyExc_SyntaxError,\r
- "invalid expression in parameter list");\r
- PyErr_SyntaxLocation(st->st_filename,\r
- st->st_cur->ste_lineno);\r
- return 0;\r
- }\r
- }\r
-\r
- if (!toplevel) {\r
- if (!symtable_visit_params_nested(st, args))\r
- return 0;\r
- }\r
-\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_visit_params_nested(struct symtable *st, asdl_seq *args)\r
-{\r
- int i;\r
- for (i = 0; i < asdl_seq_LEN(args); i++) {\r
- expr_ty arg = (expr_ty)asdl_seq_GET(args, i);\r
- if (arg->kind == Tuple_kind &&\r
- !symtable_visit_params(st, arg->v.Tuple.elts, 0))\r
- return 0;\r
- }\r
-\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_visit_arguments(struct symtable *st, arguments_ty a)\r
-{\r
- /* skip default arguments inside function block\r
- XXX should ast be different?\r
- */\r
- if (a->args && !symtable_visit_params(st, a->args, 1))\r
- return 0;\r
- if (a->vararg) {\r
- if (!symtable_add_def(st, a->vararg, DEF_PARAM))\r
- return 0;\r
- st->st_cur->ste_varargs = 1;\r
- }\r
- if (a->kwarg) {\r
- if (!symtable_add_def(st, a->kwarg, DEF_PARAM))\r
- return 0;\r
- st->st_cur->ste_varkeywords = 1;\r
- }\r
- if (a->args && !symtable_visit_params_nested(st, a->args))\r
- return 0;\r
- return 1;\r
-}\r
-\r
-\r
-static int\r
-symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)\r
-{\r
- if (eh->v.ExceptHandler.type)\r
- VISIT(st, expr, eh->v.ExceptHandler.type);\r
- if (eh->v.ExceptHandler.name)\r
- VISIT(st, expr, eh->v.ExceptHandler.name);\r
- VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);\r
- return 1;\r
-}\r
-\r
-\r
-static int\r
-symtable_visit_alias(struct symtable *st, alias_ty a)\r
-{\r
- /* Compute store_name, the name actually bound by the import\r
- operation. It is different than a->name when a->name is a\r
- dotted package name (e.g. spam.eggs)\r
- */\r
- PyObject *store_name;\r
- PyObject *name = (a->asname == NULL) ? a->name : a->asname;\r
- const char *base = PyString_AS_STRING(name);\r
- char *dot = strchr(base, '.');\r
- if (dot) {\r
- store_name = PyString_FromStringAndSize(base, dot - base);\r
- if (!store_name)\r
- return 0;\r
- }\r
- else {\r
- store_name = name;\r
- Py_INCREF(store_name);\r
- }\r
- if (strcmp(PyString_AS_STRING(name), "*")) {\r
- int r = symtable_add_def(st, store_name, DEF_IMPORT);\r
- Py_DECREF(store_name);\r
- return r;\r
- }\r
- else {\r
- if (st->st_cur->ste_type != ModuleBlock) {\r
- int lineno = st->st_cur->ste_lineno;\r
- if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {\r
- Py_DECREF(store_name);\r
- return 0;\r
- }\r
- }\r
- st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;\r
- Py_DECREF(store_name);\r
- return 1;\r
- }\r
-}\r
-\r
-\r
-static int\r
-symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)\r
-{\r
- VISIT(st, expr, lc->target);\r
- VISIT(st, expr, lc->iter);\r
- VISIT_SEQ(st, expr, lc->ifs);\r
- return 1;\r
-}\r
-\r
-\r
-static int\r
-symtable_visit_keyword(struct symtable *st, keyword_ty k)\r
-{\r
- VISIT(st, expr, k->value);\r
- return 1;\r
-}\r
-\r
-\r
-static int\r
-symtable_visit_slice(struct symtable *st, slice_ty s)\r
-{\r
- switch (s->kind) {\r
- case Slice_kind:\r
- if (s->v.Slice.lower)\r
- VISIT(st, expr, s->v.Slice.lower)\r
- if (s->v.Slice.upper)\r
- VISIT(st, expr, s->v.Slice.upper)\r
- if (s->v.Slice.step)\r
- VISIT(st, expr, s->v.Slice.step)\r
- break;\r
- case ExtSlice_kind:\r
- VISIT_SEQ(st, slice, s->v.ExtSlice.dims)\r
- break;\r
- case Index_kind:\r
- VISIT(st, expr, s->v.Index.value)\r
- break;\r
- case Ellipsis_kind:\r
- break;\r
- }\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_new_tmpname(struct symtable *st)\r
-{\r
- char tmpname[256];\r
- identifier tmp;\r
-\r
- PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",\r
- ++st->st_cur->ste_tmpname);\r
- tmp = PyString_InternFromString(tmpname);\r
- if (!tmp)\r
- return 0;\r
- if (!symtable_add_def(st, tmp, DEF_LOCAL))\r
- return 0;\r
- Py_DECREF(tmp);\r
- return 1;\r
-}\r
-\r
-static int\r
-symtable_handle_comprehension(struct symtable *st, expr_ty e,\r
- identifier scope_name, asdl_seq *generators,\r
- expr_ty elt, expr_ty value)\r
-{\r
- int is_generator = (e->kind == GeneratorExp_kind);\r
- int needs_tmp = !is_generator;\r
- comprehension_ty outermost = ((comprehension_ty)\r
- asdl_seq_GET(generators, 0));\r
- /* Outermost iterator is evaluated in current scope */\r
- VISIT(st, expr, outermost->iter);\r
- /* Create comprehension scope for the rest */\r
- if (!scope_name ||\r
- !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {\r
- return 0;\r
- }\r
- st->st_cur->ste_generator = is_generator;\r
- /* Outermost iter is received as an argument */\r
- if (!symtable_implicit_arg(st, 0)) {\r
- symtable_exit_block(st, (void *)e);\r
- return 0;\r
- }\r
- /* Allocate temporary name if needed */\r
- if (needs_tmp && !symtable_new_tmpname(st)) {\r
- symtable_exit_block(st, (void *)e);\r
- return 0;\r
- }\r
- VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);\r
- VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);\r
- VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,\r
- generators, 1, (void*)e);\r
- if (value)\r
- VISIT_IN_BLOCK(st, expr, value, (void*)e);\r
- VISIT_IN_BLOCK(st, expr, elt, (void*)e);\r
- return symtable_exit_block(st, (void *)e);\r
-}\r
-\r
-static int\r
-symtable_visit_genexp(struct symtable *st, expr_ty e)\r
-{\r
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),\r
- e->v.GeneratorExp.generators,\r
- e->v.GeneratorExp.elt, NULL);\r
-}\r
-\r
-static int\r
-symtable_visit_setcomp(struct symtable *st, expr_ty e)\r
-{\r
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),\r
- e->v.SetComp.generators,\r
- e->v.SetComp.elt, NULL);\r
-}\r
-\r
-static int\r
-symtable_visit_dictcomp(struct symtable *st, expr_ty e)\r
-{\r
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),\r
- e->v.DictComp.generators,\r
- e->v.DictComp.key,\r
- e->v.DictComp.value);\r
-}\r