LCOV - code coverage report
Current view: top level - Python - symtable.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 502 787 63.8 %
Date: 2017-04-19 Functions: 32 36 88.9 %

          Line data    Source code
       1             : #include "Python.h"
       2             : #include "Python-ast.h"
       3             : #include "code.h"
       4             : #include "symtable.h"
       5             : #include "structmember.h"
       6             : 
       7             : /* error strings used for warnings */
       8             : #define GLOBAL_AFTER_ASSIGN \
       9             : "name '%.400s' is assigned to before global declaration"
      10             : 
      11             : #define GLOBAL_AFTER_USE \
      12             : "name '%.400s' is used prior to global declaration"
      13             : 
      14             : #define IMPORT_STAR_WARNING "import * only allowed at module level"
      15             : 
      16             : #define RETURN_VAL_IN_GENERATOR \
      17             :     "'return' with argument inside generator"
      18             : 
      19             : 
      20             : static PySTEntryObject *
      21        1043 : ste_new(struct symtable *st, identifier name, _Py_block_ty block,
      22             :               void *key, int lineno)
      23             : {
      24        1043 :     PySTEntryObject *ste = NULL;
      25        1043 :     PyObject *k = NULL;
      26             : 
      27        1043 :     k = PyLong_FromVoidPtr(key);
      28        1043 :     if (k == NULL)
      29           0 :         goto fail;
      30        1043 :     ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
      31        1043 :     if (ste == NULL) {
      32           0 :         Py_DECREF(k);
      33           0 :         goto fail;
      34             :     }
      35        1043 :     ste->ste_table = st;
      36        1043 :     ste->ste_id = k; /* ste owns reference to k */
      37             : 
      38        1043 :     ste->ste_name = name;
      39        1043 :     Py_INCREF(name);
      40             : 
      41        1043 :     ste->ste_symbols = NULL;
      42        1043 :     ste->ste_varnames = NULL;
      43        1043 :     ste->ste_children = NULL;
      44             : 
      45        1043 :     ste->ste_symbols = PyDict_New();
      46        1043 :     if (ste->ste_symbols == NULL)
      47           0 :         goto fail;
      48             : 
      49        1043 :     ste->ste_varnames = PyList_New(0);
      50        1043 :     if (ste->ste_varnames == NULL)
      51           0 :         goto fail;
      52             : 
      53        1043 :     ste->ste_children = PyList_New(0);
      54        1043 :     if (ste->ste_children == NULL)
      55           0 :         goto fail;
      56             : 
      57        1043 :     ste->ste_type = block;
      58        1043 :     ste->ste_unoptimized = 0;
      59        1043 :     ste->ste_nested = 0;
      60        1043 :     ste->ste_free = 0;
      61        1043 :     ste->ste_varargs = 0;
      62        1043 :     ste->ste_varkeywords = 0;
      63        1043 :     ste->ste_opt_lineno = 0;
      64        1043 :     ste->ste_tmpname = 0;
      65        1043 :     ste->ste_lineno = lineno;
      66             : 
      67        2023 :     if (st->st_cur != NULL &&
      68        1960 :         (st->st_cur->ste_nested ||
      69         980 :          st->st_cur->ste_type == FunctionBlock))
      70          16 :         ste->ste_nested = 1;
      71        1043 :     ste->ste_child_free = 0;
      72        1043 :     ste->ste_generator = 0;
      73        1043 :     ste->ste_returns_value = 0;
      74             : 
      75        1043 :     if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
      76           0 :         goto fail;
      77             : 
      78        1043 :     return ste;
      79             :  fail:
      80           0 :     Py_XDECREF(ste);
      81           0 :     return NULL;
      82             : }
      83             : 
      84             : static PyObject *
      85           0 : ste_repr(PySTEntryObject *ste)
      86             : {
      87             :     char buf[256];
      88             : 
      89           0 :     PyOS_snprintf(buf, sizeof(buf),
      90             :                   "<symtable entry %.100s(%ld), line %d>",
      91           0 :                   PyString_AS_STRING(ste->ste_name),
      92           0 :                   PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
      93           0 :     return PyString_FromString(buf);
      94             : }
      95             : 
      96             : static void
      97        1043 : ste_dealloc(PySTEntryObject *ste)
      98             : {
      99        1043 :     ste->ste_table = NULL;
     100        1043 :     Py_XDECREF(ste->ste_id);
     101        1043 :     Py_XDECREF(ste->ste_name);
     102        1043 :     Py_XDECREF(ste->ste_symbols);
     103        1043 :     Py_XDECREF(ste->ste_varnames);
     104        1043 :     Py_XDECREF(ste->ste_children);
     105        1043 :     PyObject_Del(ste);
     106        1043 : }
     107             : 
     108             : #define OFF(x) offsetof(PySTEntryObject, x)
     109             : 
     110             : static PyMemberDef ste_memberlist[] = {
     111             :     {"id",       T_OBJECT, OFF(ste_id), READONLY},
     112             :     {"name",     T_OBJECT, OFF(ste_name), READONLY},
     113             :     {"symbols",  T_OBJECT, OFF(ste_symbols), READONLY},
     114             :     {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
     115             :     {"children", T_OBJECT, OFF(ste_children), READONLY},
     116             :     {"optimized",T_INT,    OFF(ste_unoptimized), READONLY},
     117             :     {"nested",   T_INT,    OFF(ste_nested), READONLY},
     118             :     {"type",     T_INT,    OFF(ste_type), READONLY},
     119             :     {"lineno",   T_INT,    OFF(ste_lineno), READONLY},
     120             :     {NULL}
     121             : };
     122             : 
     123             : PyTypeObject PySTEntry_Type = {
     124             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     125             :     "symtable entry",
     126             :     sizeof(PySTEntryObject),
     127             :     0,
     128             :     (destructor)ste_dealloc,                /* tp_dealloc */
     129             :     0,                                      /* tp_print */
     130             :     0,                                         /* tp_getattr */
     131             :     0,                                          /* tp_setattr */
     132             :     0,                                          /* tp_compare */
     133             :     (reprfunc)ste_repr,                         /* tp_repr */
     134             :     0,                                          /* tp_as_number */
     135             :     0,                                          /* tp_as_sequence */
     136             :     0,                                          /* tp_as_mapping */
     137             :     0,                                          /* tp_hash */
     138             :     0,                                          /* tp_call */
     139             :     0,                                          /* tp_str */
     140             :     PyObject_GenericGetAttr,                    /* tp_getattro */
     141             :     0,                                          /* tp_setattro */
     142             :     0,                                          /* tp_as_buffer */
     143             :     Py_TPFLAGS_DEFAULT,                         /* tp_flags */
     144             :     0,                                          /* tp_doc */
     145             :     0,                                          /* tp_traverse */
     146             :     0,                                          /* tp_clear */
     147             :     0,                                          /* tp_richcompare */
     148             :     0,                                          /* tp_weaklistoffset */
     149             :     0,                                          /* tp_iter */
     150             :     0,                                          /* tp_iternext */
     151             :     0,                                          /* tp_methods */
     152             :     ste_memberlist,                             /* tp_members */
     153             :     0,                                          /* tp_getset */
     154             :     0,                                          /* tp_base */
     155             :     0,                                          /* tp_dict */
     156             :     0,                                          /* tp_descr_get */
     157             :     0,                                          /* tp_descr_set */
     158             :     0,                                          /* tp_dictoffset */
     159             :     0,                                          /* tp_init */
     160             :     0,                                          /* tp_alloc */
     161             :     0,                                          /* tp_new */
     162             : };
     163             : 
     164             : static int symtable_analyze(struct symtable *st);
     165             : static int symtable_warn(struct symtable *st, char *msg, int lineno);
     166             : static int symtable_enter_block(struct symtable *st, identifier name,
     167             :                                 _Py_block_ty block, void *ast, int lineno);
     168             : static int symtable_exit_block(struct symtable *st, void *ast);
     169             : static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
     170             : static int symtable_visit_expr(struct symtable *st, expr_ty s);
     171             : static int symtable_visit_genexp(struct symtable *st, expr_ty s);
     172             : static int symtable_visit_setcomp(struct symtable *st, expr_ty e);
     173             : static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);
     174             : static int symtable_visit_arguments(struct symtable *st, arguments_ty);
     175             : static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
     176             : static int symtable_visit_alias(struct symtable *st, alias_ty);
     177             : static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
     178             : static int symtable_visit_keyword(struct symtable *st, keyword_ty);
     179             : static int symtable_visit_slice(struct symtable *st, slice_ty);
     180             : static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
     181             : static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
     182             : static int symtable_implicit_arg(struct symtable *st, int pos);
     183             : 
     184             : 
     185             : static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,
     186             :     dictcomp = NULL;
     187             : 
     188             : #define GET_IDENTIFIER(VAR) \
     189             :     ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
     190             : 
     191             : #define DUPLICATE_ARGUMENT \
     192             : "duplicate argument '%s' in function definition"
     193             : 
     194             : static struct symtable *
     195          63 : symtable_new(void)
     196             : {
     197             :     struct symtable *st;
     198             : 
     199          63 :     st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
     200          63 :     if (st == NULL)
     201           0 :         return NULL;
     202             : 
     203          63 :     st->st_filename = NULL;
     204          63 :     st->st_symbols = NULL;
     205             : 
     206          63 :     if ((st->st_stack = PyList_New(0)) == NULL)
     207           0 :         goto fail;
     208          63 :     if ((st->st_symbols = PyDict_New()) == NULL)
     209           0 :         goto fail;
     210          63 :     st->st_cur = NULL;
     211          63 :     st->st_private = NULL;
     212          63 :     return st;
     213             :  fail:
     214           0 :     PySymtable_Free(st);
     215           0 :     return NULL;
     216             : }
     217             : 
     218             : struct symtable *
     219          63 : PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
     220             : {
     221          63 :     struct symtable *st = symtable_new();
     222             :     asdl_seq *seq;
     223             :     int i;
     224             : 
     225          63 :     if (st == NULL)
     226           0 :         return st;
     227          63 :     st->st_filename = filename;
     228          63 :     st->st_future = future;
     229         126 :     if (!GET_IDENTIFIER(top) ||
     230          63 :         !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
     231           0 :         PySymtable_Free(st);
     232           0 :         return NULL;
     233             :     }
     234             : 
     235          63 :     st->st_top = st->st_cur;
     236          63 :     st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
     237             :     /* Any other top-level initialization? */
     238          63 :     switch (mod->kind) {
     239             :     case Module_kind:
     240          63 :         seq = mod->v.Module.body;
     241         746 :         for (i = 0; i < asdl_seq_LEN(seq); i++)
     242         683 :             if (!symtable_visit_stmt(st,
     243         683 :                         (stmt_ty)asdl_seq_GET(seq, i)))
     244           0 :                 goto error;
     245          63 :         break;
     246             :     case Expression_kind:
     247           0 :         if (!symtable_visit_expr(st, mod->v.Expression.body))
     248           0 :             goto error;
     249           0 :         break;
     250             :     case Interactive_kind:
     251           0 :         seq = mod->v.Interactive.body;
     252           0 :         for (i = 0; i < asdl_seq_LEN(seq); i++)
     253           0 :             if (!symtable_visit_stmt(st,
     254           0 :                         (stmt_ty)asdl_seq_GET(seq, i)))
     255           0 :                 goto error;
     256           0 :         break;
     257             :     case Suite_kind:
     258           0 :         PyErr_SetString(PyExc_RuntimeError,
     259             :                         "this compiler does not handle Suites");
     260           0 :         goto error;
     261             :     }
     262          63 :     if (!symtable_exit_block(st, (void *)mod)) {
     263           0 :         PySymtable_Free(st);
     264           0 :         return NULL;
     265             :     }
     266          63 :     if (symtable_analyze(st))
     267          63 :         return st;
     268           0 :     PySymtable_Free(st);
     269           0 :     return NULL;
     270             :  error:
     271           0 :     (void) symtable_exit_block(st, (void *)mod);
     272           0 :     PySymtable_Free(st);
     273           0 :     return NULL;
     274             : }
     275             : 
     276             : void
     277          63 : PySymtable_Free(struct symtable *st)
     278             : {
     279          63 :     Py_XDECREF(st->st_symbols);
     280          63 :     Py_XDECREF(st->st_stack);
     281          63 :     PyMem_Free((void *)st);
     282          63 : }
     283             : 
     284             : PySTEntryObject *
     285        1043 : PySymtable_Lookup(struct symtable *st, void *key)
     286             : {
     287             :     PyObject *k, *v;
     288             : 
     289        1043 :     k = PyLong_FromVoidPtr(key);
     290        1043 :     if (k == NULL)
     291           0 :         return NULL;
     292        1043 :     v = PyDict_GetItem(st->st_symbols, k);
     293        1043 :     if (v) {
     294             :         assert(PySTEntry_Check(v));
     295        1043 :         Py_INCREF(v);
     296             :     }
     297             :     else {
     298           0 :         PyErr_SetString(PyExc_KeyError,
     299             :                         "unknown symbol table entry");
     300             :     }
     301             : 
     302        1043 :     Py_DECREF(k);
     303        1043 :     return (PySTEntryObject *)v;
     304             : }
     305             : 
     306             : int
     307       15588 : PyST_GetScope(PySTEntryObject *ste, PyObject *name)
     308             : {
     309       15588 :     PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
     310       15588 :     if (!v)
     311         404 :         return 0;
     312             :     assert(PyInt_Check(v));
     313       15184 :     return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
     314             : }
     315             : 
     316             : 
     317             : /* Analyze raw symbol information to determine scope of each name.
     318             : 
     319             :    The next several functions are helpers for PySymtable_Analyze(),
     320             :    which determines whether a name is local, global, or free.  In addition,
     321             :    it determines which local variables are cell variables; they provide
     322             :    bindings that are used for free variables in enclosed blocks.
     323             : 
     324             :    There are also two kinds of free variables, implicit and explicit.  An
     325             :    explicit global is declared with the global statement.  An implicit
     326             :    global is a free variable for which the compiler has found no binding
     327             :    in an enclosing function scope.  The implicit global is either a global
     328             :    or a builtin.  Python's module and class blocks use the xxx_NAME opcodes
     329             :    to handle these names to implement slightly odd semantics.  In such a
     330             :    block, the name is treated as global until it is assigned to; then it
     331             :    is treated as a local.
     332             : 
     333             :    The symbol table requires two passes to determine the scope of each name.
     334             :    The first pass collects raw facts from the AST: the name is a parameter
     335             :    here, the name is used by not defined here, etc.  The second pass analyzes
     336             :    these facts during a pass over the PySTEntryObjects created during pass 1.
     337             : 
     338             :    When a function is entered during the second pass, the parent passes
     339             :    the set of all name bindings visible to its children.  These bindings
     340             :    are used to determine if the variable is free or an implicit global.
     341             :    After doing the local analysis, it analyzes each of its child blocks
     342             :    using an updated set of name bindings.
     343             : 
     344             :    The children update the free variable set.  If a local variable is free
     345             :    in a child, the variable is marked as a cell.  The current function must
     346             :    provide runtime storage for the variable that may outlive the function's
     347             :    frame.  Cell variables are removed from the free set before the analyze
     348             :    function returns to its parent.
     349             : 
     350             :    The sets of bound and free variables are implemented as dictionaries
     351             :    mapping strings to None.
     352             : */
     353             : 
     354             : #define SET_SCOPE(DICT, NAME, I) { \
     355             :     PyObject *o = PyInt_FromLong(I); \
     356             :     if (!o) \
     357             :         return 0; \
     358             :     if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
     359             :         Py_DECREF(o); \
     360             :         return 0; \
     361             :     } \
     362             :     Py_DECREF(o); \
     363             : }
     364             : 
     365             : /* Decide on scope of name, given flags.
     366             : 
     367             :    The namespace dictionaries may be modified to record information
     368             :    about the new name.  For example, a new global will add an entry to
     369             :    global.  A name that was global can be changed to local.
     370             : */
     371             : 
     372             : static int
     373        6640 : analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
     374             :              PyObject *bound, PyObject *local, PyObject *free,
     375             :              PyObject *global)
     376             : {
     377        6640 :     if (flags & DEF_GLOBAL) {
     378           0 :         if (flags & DEF_PARAM) {
     379           0 :             PyErr_Format(PyExc_SyntaxError,
     380             :                          "name '%s' is local and global",
     381           0 :                          PyString_AS_STRING(name));
     382           0 :             PyErr_SyntaxLocation(ste->ste_table->st_filename,
     383             :                                  ste->ste_lineno);
     384             : 
     385           0 :             return 0;
     386             :         }
     387           0 :         SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
     388           0 :         if (PyDict_SetItem(global, name, Py_None) < 0)
     389           0 :             return 0;
     390           0 :         if (bound && PyDict_GetItem(bound, name)) {
     391           0 :             if (PyDict_DelItem(bound, name) < 0)
     392           0 :                 return 0;
     393             :         }
     394           0 :         return 1;
     395             :     }
     396        6640 :     if (flags & DEF_BOUND) {
     397        4615 :         SET_SCOPE(dict, name, LOCAL);
     398        4615 :         if (PyDict_SetItem(local, name, Py_None) < 0)
     399           0 :             return 0;
     400        4615 :         if (PyDict_GetItem(global, name)) {
     401           0 :             if (PyDict_DelItem(global, name) < 0)
     402           0 :                 return 0;
     403             :         }
     404        4615 :         return 1;
     405             :     }
     406             :     /* If an enclosing block has a binding for this name, it
     407             :        is a free variable rather than a global variable.
     408             :        Note that having a non-NULL bound implies that the block
     409             :        is nested.
     410             :     */
     411        2025 :     if (bound && PyDict_GetItem(bound, name)) {
     412           3 :         SET_SCOPE(dict, name, FREE);
     413           3 :         ste->ste_free = 1;
     414           3 :         if (PyDict_SetItem(free, name, Py_None) < 0)
     415           0 :             return 0;
     416           3 :         return 1;
     417             :     }
     418             :     /* If a parent has a global statement, then call it global
     419             :        explicit?  It could also be global implicit.
     420             :      */
     421        2022 :     else if (global && PyDict_GetItem(global, name)) {
     422           0 :         SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
     423           0 :         return 1;
     424             :     }
     425             :     else {
     426        2022 :         if (ste->ste_nested)
     427           5 :             ste->ste_free = 1;
     428        2022 :         SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
     429        2022 :         return 1;
     430             :     }
     431             :     /* Should never get here. */
     432             :     PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
     433             :                  PyString_AS_STRING(name));
     434             :     return 0;
     435             : }
     436             : 
     437             : #undef SET_SCOPE
     438             : 
     439             : /* If a name is defined in free and also in locals, then this block
     440             :    provides the binding for the free variable.  The name should be
     441             :    marked CELL in this block and removed from the free list.
     442             : 
     443             :    Note that the current block's free variables are included in free.
     444             :    That's safe because no name can be free and local in the same scope.
     445             : */
     446             : 
     447             : static int
     448         836 : analyze_cells(PyObject *scope, PyObject *free)
     449             : {
     450             :     PyObject *name, *v, *w;
     451         836 :     int success = 0;
     452         836 :     Py_ssize_t pos = 0;
     453             : 
     454         836 :     w = PyInt_FromLong(CELL);
     455         836 :     if (!w)
     456           0 :         return 0;
     457        6475 :     while (PyDict_Next(scope, &pos, &name, &v)) {
     458             :         long flags;
     459             :         assert(PyInt_Check(v));
     460        4803 :         flags = PyInt_AS_LONG(v);
     461        4803 :         if (flags != LOCAL)
     462        1763 :             continue;
     463        3040 :         if (!PyDict_GetItem(free, name))
     464        3037 :             continue;
     465             :         /* Replace LOCAL with CELL for this name, and remove
     466             :            from free. It is safe to replace the value of name
     467             :            in the dict, because it will not cause a resize.
     468             :          */
     469           3 :         if (PyDict_SetItem(scope, name, w) < 0)
     470           0 :             goto error;
     471           3 :         if (PyDict_DelItem(free, name) < 0)
     472           0 :             goto error;
     473             :     }
     474         836 :     success = 1;
     475             :  error:
     476         836 :     Py_DECREF(w);
     477         836 :     return success;
     478             : }
     479             : 
     480             : /* Check for illegal statements in unoptimized namespaces */
     481             : static int
     482        1043 : check_unoptimized(const PySTEntryObject* ste) {
     483             :     char buf[300];
     484             :     const char* trailer;
     485             : 
     486        1043 :     if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
     487           0 :         || !(ste->ste_free || ste->ste_child_free))
     488        1043 :         return 1;
     489             : 
     490           0 :     trailer = (ste->ste_child_free ?
     491           0 :                    "contains a nested function with free variables" :
     492             :                    "is a nested function");
     493             : 
     494           0 :     switch (ste->ste_unoptimized) {
     495             :     case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
     496             :     case OPT_EXEC: /* qualified exec is fine */
     497           0 :         return 1;
     498             :     case OPT_IMPORT_STAR:
     499           0 :         PyOS_snprintf(buf, sizeof(buf),
     500             :                       "import * is not allowed in function '%.100s' "
     501             :                       "because it %s",
     502           0 :                       PyString_AS_STRING(ste->ste_name), trailer);
     503           0 :         break;
     504             :     case OPT_BARE_EXEC:
     505           0 :         PyOS_snprintf(buf, sizeof(buf),
     506             :                       "unqualified exec is not allowed in function "
     507             :                       "'%.100s' because it %s",
     508           0 :                       PyString_AS_STRING(ste->ste_name), trailer);
     509           0 :         break;
     510             :     default:
     511           0 :         PyOS_snprintf(buf, sizeof(buf),
     512             :                       "function '%.100s' uses import * and bare exec, "
     513             :                       "which are illegal because it %s",
     514           0 :                       PyString_AS_STRING(ste->ste_name), trailer);
     515           0 :         break;
     516             :     }
     517             : 
     518           0 :     PyErr_SetString(PyExc_SyntaxError, buf);
     519           0 :     PyErr_SyntaxLocation(ste->ste_table->st_filename,
     520             :                          ste->ste_opt_lineno);
     521           0 :     return 0;
     522             : }
     523             : 
     524             : /* Enter the final scope information into the st_symbols dict.
     525             :  *
     526             :  * All arguments are dicts.  Modifies symbols, others are read-only.
     527             : */
     528             : static int
     529        1043 : update_symbols(PyObject *symbols, PyObject *scope,
     530             :                PyObject *bound, PyObject *free, int classflag)
     531             : {
     532        1043 :     PyObject *name, *v, *u, *w, *free_value = NULL;
     533        1043 :     Py_ssize_t pos = 0;
     534             : 
     535        8726 :     while (PyDict_Next(symbols, &pos, &name, &v)) {
     536             :         long i, flags;
     537             :         assert(PyInt_Check(v));
     538        6640 :         flags = PyInt_AS_LONG(v);
     539        6640 :         w = PyDict_GetItem(scope, name);
     540             :         assert(w && PyInt_Check(w));
     541        6640 :         i = PyInt_AS_LONG(w);
     542        6640 :         flags |= (i << SCOPE_OFF);
     543        6640 :         u = PyInt_FromLong(flags);
     544        6640 :         if (!u)
     545           0 :             return 0;
     546        6640 :         if (PyDict_SetItem(symbols, name, u) < 0) {
     547           0 :             Py_DECREF(u);
     548           0 :             return 0;
     549             :         }
     550        6640 :         Py_DECREF(u);
     551             :     }
     552             : 
     553        1043 :     free_value = PyInt_FromLong(FREE << SCOPE_OFF);
     554        1043 :     if (!free_value)
     555           0 :         return 0;
     556             : 
     557             :     /* add a free variable when it's only use is for creating a closure */
     558        1043 :     pos = 0;
     559        2086 :     while (PyDict_Next(free, &pos, &name, &v)) {
     560           0 :         PyObject *o = PyDict_GetItem(symbols, name);
     561             : 
     562           0 :         if (o) {
     563             :             /* It could be a free variable in a method of
     564             :                the class that has the same name as a local
     565             :                or global in the class scope.
     566             :             */
     567           0 :             if  (classflag &&
     568           0 :                  PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
     569           0 :                 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
     570           0 :                 o = PyInt_FromLong(i);
     571           0 :                 if (!o) {
     572           0 :                     Py_DECREF(free_value);
     573           0 :                     return 0;
     574             :                 }
     575           0 :                 if (PyDict_SetItem(symbols, name, o) < 0) {
     576           0 :                     Py_DECREF(o);
     577           0 :                     Py_DECREF(free_value);
     578           0 :                     return 0;
     579             :                 }
     580           0 :                 Py_DECREF(o);
     581             :             }
     582             :             /* else it's not free, probably a cell */
     583           0 :             continue;
     584             :         }
     585           0 :         if (!PyDict_GetItem(bound, name))
     586           0 :             continue;       /* it's a global */
     587             : 
     588           0 :         if (PyDict_SetItem(symbols, name, free_value) < 0) {
     589           0 :             Py_DECREF(free_value);
     590           0 :             return 0;
     591             :         }
     592             :     }
     593        1043 :     Py_DECREF(free_value);
     594        1043 :     return 1;
     595             : }
     596             : 
     597             : /* Make final symbol table decisions for block of ste.
     598             : 
     599             :    Arguments:
     600             :    ste -- current symtable entry (input/output)
     601             :    bound -- set of variables bound in enclosing scopes (input).  bound
     602             :        is NULL for module blocks.
     603             :    free -- set of free variables in enclosed scopes (output)
     604             :    globals -- set of declared global variables in enclosing scopes (input)
     605             : 
     606             :    The implementation uses two mutually recursive functions,
     607             :    analyze_block() and analyze_child_block().  analyze_block() is
     608             :    responsible for analyzing the individual names defined in a block.
     609             :    analyze_child_block() prepares temporary namespace dictionaries
     610             :    used to evaluated nested blocks.
     611             : 
     612             :    The two functions exist because a child block should see the name
     613             :    bindings of its enclosing blocks, but those bindings should not
     614             :    propagate back to a parent block.
     615             : */
     616             : 
     617             : static int
     618             : analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
     619             :                     PyObject *global, PyObject* child_free);
     620             : 
     621             : static int
     622        1043 : analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
     623             :               PyObject *global)
     624             : {
     625        1043 :     PyObject *name, *v, *local = NULL, *scope = NULL;
     626        1043 :     PyObject *newbound = NULL, *newglobal = NULL;
     627        1043 :     PyObject *newfree = NULL, *allfree = NULL;
     628        1043 :     int i, success = 0;
     629        1043 :     Py_ssize_t pos = 0;
     630             : 
     631        1043 :     local = PyDict_New();  /* collect new names bound in block */
     632        1043 :     if (!local)
     633           0 :         goto error;
     634        1043 :     scope = PyDict_New(); /* collect scopes defined for each name */
     635        1043 :     if (!scope)
     636           0 :         goto error;
     637             : 
     638             :     /* Allocate new global and bound variable dictionaries.  These
     639             :        dictionaries hold the names visible in nested blocks.  For
     640             :        ClassBlocks, the bound and global names are initialized
     641             :        before analyzing names, because class bindings aren't
     642             :        visible in methods.  For other blocks, they are initialized
     643             :        after names are analyzed.
     644             :      */
     645             : 
     646             :     /* TODO(jhylton): Package these dicts in a struct so that we
     647             :        can write reasonable helper functions?
     648             :     */
     649        1043 :     newglobal = PyDict_New();
     650        1043 :     if (!newglobal)
     651           0 :         goto error;
     652        1043 :     newbound = PyDict_New();
     653        1043 :     if (!newbound)
     654           0 :         goto error;
     655        1043 :     newfree = PyDict_New();
     656        1043 :     if (!newfree)
     657           0 :         goto error;
     658             : 
     659        1043 :     if (ste->ste_type == ClassBlock) {
     660         144 :         if (PyDict_Update(newglobal, global) < 0)
     661           0 :             goto error;
     662         144 :         if (bound)
     663         144 :             if (PyDict_Update(newbound, bound) < 0)
     664           0 :                 goto error;
     665             :     }
     666             : 
     667        8726 :     while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
     668        6640 :         long flags = PyInt_AS_LONG(v);
     669        6640 :         if (!analyze_name(ste, scope, name, flags,
     670             :                           bound, local, free, global))
     671           0 :             goto error;
     672             :     }
     673             : 
     674        1043 :     if (ste->ste_type != ClassBlock) {
     675         899 :         if (ste->ste_type == FunctionBlock) {
     676         836 :             if (PyDict_Update(newbound, local) < 0)
     677           0 :                 goto error;
     678             :         }
     679         899 :         if (bound) {
     680         836 :             if (PyDict_Update(newbound, bound) < 0)
     681           0 :                 goto error;
     682             :         }
     683         899 :         if (PyDict_Update(newglobal, global) < 0)
     684           0 :             goto error;
     685             :     }
     686             : 
     687             :     /* Recursively call analyze_block() on each child block.
     688             : 
     689             :        newbound, newglobal now contain the names visible in
     690             :        nested blocks.  The free variables in the children will
     691             :        be collected in allfree.
     692             :     */
     693        1043 :     allfree = PyDict_New();
     694        1043 :     if (!allfree)
     695           0 :         goto error;
     696        2023 :     for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
     697         980 :         PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
     698             :         PySTEntryObject* entry;
     699             :         assert(c && PySTEntry_Check(c));
     700         980 :         entry = (PySTEntryObject*)c;
     701         980 :         if (!analyze_child_block(entry, newbound, newfree, newglobal,
     702             :                                  allfree))
     703           0 :             goto error;
     704         980 :         if (entry->ste_free || entry->ste_child_free)
     705          13 :             ste->ste_child_free = 1;
     706             :     }
     707             : 
     708        1043 :     if (PyDict_Update(newfree, allfree) < 0)
     709           0 :         goto error;
     710        1043 :     if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
     711           0 :         goto error;
     712        1043 :     if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
     713        1043 :                         ste->ste_type == ClassBlock))
     714           0 :         goto error;
     715        1043 :     if (!check_unoptimized(ste))
     716           0 :         goto error;
     717             : 
     718        1043 :     if (PyDict_Update(free, newfree) < 0)
     719           0 :         goto error;
     720        1043 :     success = 1;
     721             :  error:
     722        1043 :     Py_XDECREF(local);
     723        1043 :     Py_XDECREF(scope);
     724        1043 :     Py_XDECREF(newbound);
     725        1043 :     Py_XDECREF(newglobal);
     726        1043 :     Py_XDECREF(newfree);
     727        1043 :     Py_XDECREF(allfree);
     728             :     if (!success)
     729             :         assert(PyErr_Occurred());
     730        1043 :     return success;
     731             : }
     732             : 
     733             : static int
     734         980 : analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
     735             :                     PyObject *global, PyObject* child_free)
     736             : {
     737         980 :     PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
     738             : 
     739             :     /* Copy the bound and global dictionaries.
     740             : 
     741             :        These dictionaries are used by all blocks enclosed by the
     742             :        current block.  The analyze_block() call modifies these
     743             :        dictionaries.
     744             : 
     745             :     */
     746         980 :     temp_bound = PyDict_New();
     747         980 :     if (!temp_bound)
     748           0 :         goto error;
     749         980 :     if (PyDict_Update(temp_bound, bound) < 0)
     750           0 :         goto error;
     751         980 :     temp_free = PyDict_New();
     752         980 :     if (!temp_free)
     753           0 :         goto error;
     754         980 :     if (PyDict_Update(temp_free, free) < 0)
     755           0 :         goto error;
     756         980 :     temp_global = PyDict_New();
     757         980 :     if (!temp_global)
     758           0 :         goto error;
     759         980 :     if (PyDict_Update(temp_global, global) < 0)
     760           0 :         goto error;
     761             : 
     762         980 :     if (!analyze_block(entry, temp_bound, temp_free, temp_global))
     763           0 :         goto error;
     764         980 :     if (PyDict_Update(child_free, temp_free) < 0)
     765           0 :         goto error;
     766         980 :     Py_DECREF(temp_bound);
     767         980 :     Py_DECREF(temp_free);
     768         980 :     Py_DECREF(temp_global);
     769         980 :     return 1;
     770             :  error:
     771           0 :     Py_XDECREF(temp_bound);
     772           0 :     Py_XDECREF(temp_free);
     773           0 :     Py_XDECREF(temp_global);
     774           0 :     return 0;
     775             : }
     776             : 
     777             : static int
     778          63 : symtable_analyze(struct symtable *st)
     779             : {
     780             :     PyObject *free, *global;
     781             :     int r;
     782             : 
     783          63 :     free = PyDict_New();
     784          63 :     if (!free)
     785           0 :         return 0;
     786          63 :     global = PyDict_New();
     787          63 :     if (!global) {
     788           0 :         Py_DECREF(free);
     789           0 :         return 0;
     790             :     }
     791          63 :     r = analyze_block(st->st_top, NULL, free, global);
     792          63 :     Py_DECREF(free);
     793          63 :     Py_DECREF(global);
     794          63 :     return r;
     795             : }
     796             : 
     797             : 
     798             : static int
     799           0 : symtable_warn(struct symtable *st, char *msg, int lineno)
     800             : {
     801           0 :     if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
     802             :                            lineno, NULL, NULL) < 0)     {
     803           0 :         if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
     804           0 :             PyErr_SetString(PyExc_SyntaxError, msg);
     805           0 :             PyErr_SyntaxLocation(st->st_filename,
     806           0 :                                  st->st_cur->ste_lineno);
     807             :         }
     808           0 :         return 0;
     809             :     }
     810           0 :     return 1;
     811             : }
     812             : 
     813             : /* symtable_enter_block() gets a reference via ste_new.
     814             :    This reference is released when the block is exited, via the DECREF
     815             :    in symtable_exit_block().
     816             : */
     817             : 
     818             : static int
     819        1043 : symtable_exit_block(struct symtable *st, void *ast)
     820             : {
     821             :     Py_ssize_t end;
     822             : 
     823        1043 :     Py_CLEAR(st->st_cur);
     824        1043 :     end = PyList_GET_SIZE(st->st_stack) - 1;
     825        1043 :     if (end >= 0) {
     826         980 :         st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
     827             :                                                         end);
     828         980 :         if (st->st_cur == NULL)
     829           0 :             return 0;
     830         980 :         Py_INCREF(st->st_cur);
     831         980 :         if (PySequence_DelItem(st->st_stack, end) < 0)
     832           0 :             return 0;
     833             :     }
     834        1043 :     return 1;
     835             : }
     836             : 
     837             : static int
     838        1043 : symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
     839             :                      void *ast, int lineno)
     840             : {
     841        1043 :     PySTEntryObject *prev = NULL;
     842             : 
     843        1043 :     if (st->st_cur) {
     844         980 :         prev = st->st_cur;
     845         980 :         if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
     846           0 :             return 0;
     847             :         }
     848         980 :         Py_DECREF(st->st_cur);
     849             :     }
     850        1043 :     st->st_cur = ste_new(st, name, block, ast, lineno);
     851        1043 :     if (st->st_cur == NULL)
     852           0 :         return 0;
     853        1043 :     if (block == ModuleBlock)
     854          63 :         st->st_global = st->st_cur->ste_symbols;
     855        1043 :     if (prev) {
     856         980 :         if (PyList_Append(prev->ste_children,
     857         980 :                           (PyObject *)st->st_cur) < 0) {
     858           0 :             return 0;
     859             :         }
     860             :     }
     861        1043 :     return 1;
     862             : }
     863             : 
     864             : static long
     865           0 : symtable_lookup(struct symtable *st, PyObject *name)
     866             : {
     867             :     PyObject *o;
     868           0 :     PyObject *mangled = _Py_Mangle(st->st_private, name);
     869           0 :     if (!mangled)
     870           0 :         return 0;
     871           0 :     o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
     872           0 :     Py_DECREF(mangled);
     873           0 :     if (!o)
     874           0 :         return 0;
     875           0 :     return PyInt_AsLong(o);
     876             : }
     877             : 
     878             : static int
     879       16834 : symtable_add_def(struct symtable *st, PyObject *name, int flag)
     880             : {
     881             :     PyObject *o;
     882             :     PyObject *dict;
     883             :     long val;
     884       16834 :     PyObject *mangled = _Py_Mangle(st->st_private, name);
     885             : 
     886       16834 :     if (!mangled)
     887           0 :         return 0;
     888       16834 :     dict = st->st_cur->ste_symbols;
     889       16834 :     if ((o = PyDict_GetItem(dict, mangled))) {
     890       10194 :         val = PyInt_AS_LONG(o);
     891       10194 :         if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
     892             :             /* Is it better to use 'mangled' or 'name' here? */
     893           0 :             PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
     894             :                          PyString_AsString(name));
     895           0 :             PyErr_SyntaxLocation(st->st_filename,
     896           0 :                                st->st_cur->ste_lineno);
     897           0 :             goto error;
     898             :         }
     899       10194 :         val |= flag;
     900             :     } else
     901        6640 :         val = flag;
     902       16834 :     o = PyInt_FromLong(val);
     903       16834 :     if (o == NULL)
     904           0 :         goto error;
     905       16834 :     if (PyDict_SetItem(dict, mangled, o) < 0) {
     906           0 :         Py_DECREF(o);
     907           0 :         goto error;
     908             :     }
     909       16834 :     Py_DECREF(o);
     910             : 
     911       16834 :     if (flag & DEF_PARAM) {
     912        1679 :         if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
     913           0 :             goto error;
     914       15155 :     } else      if (flag & DEF_GLOBAL) {
     915             :         /* XXX need to update DEF_GLOBAL for other flags too;
     916             :            perhaps only DEF_FREE_GLOBAL */
     917           0 :         val = flag;
     918           0 :         if ((o = PyDict_GetItem(st->st_global, mangled))) {
     919           0 :             val |= PyInt_AS_LONG(o);
     920             :         }
     921           0 :         o = PyInt_FromLong(val);
     922           0 :         if (o == NULL)
     923           0 :             goto error;
     924           0 :         if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
     925           0 :             Py_DECREF(o);
     926           0 :             goto error;
     927             :         }
     928           0 :         Py_DECREF(o);
     929             :     }
     930       16834 :     Py_DECREF(mangled);
     931       16834 :     return 1;
     932             : 
     933             : error:
     934           0 :     Py_DECREF(mangled);
     935           0 :     return 0;
     936             : }
     937             : 
     938             : /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
     939             :    They use the ASDL name to synthesize the name of the C type and the visit
     940             :    function.
     941             : 
     942             :    VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
     943             :    useful if the first node in the sequence requires special treatment.
     944             : */
     945             : 
     946             : #define VISIT(ST, TYPE, V) \
     947             :     if (!symtable_visit_ ## TYPE((ST), (V))) \
     948             :         return 0;
     949             : 
     950             : #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
     951             :     if (!symtable_visit_ ## TYPE((ST), (V))) { \
     952             :         symtable_exit_block((ST), (S)); \
     953             :         return 0; \
     954             :     }
     955             : 
     956             : #define VISIT_SEQ(ST, TYPE, SEQ) { \
     957             :     int i; \
     958             :     asdl_seq *seq = (SEQ); /* avoid variable capture */ \
     959             :     for (i = 0; i < asdl_seq_LEN(seq); i++) { \
     960             :         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
     961             :         if (!symtable_visit_ ## TYPE((ST), elt)) \
     962             :             return 0; \
     963             :     } \
     964             : }
     965             : 
     966             : #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
     967             :     int i; \
     968             :     asdl_seq *seq = (SEQ); /* avoid variable capture */ \
     969             :     for (i = 0; i < asdl_seq_LEN(seq); i++) { \
     970             :         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
     971             :         if (!symtable_visit_ ## TYPE((ST), elt)) { \
     972             :             symtable_exit_block((ST), (S)); \
     973             :             return 0; \
     974             :         } \
     975             :     } \
     976             : }
     977             : 
     978             : #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
     979             :     int i; \
     980             :     asdl_seq *seq = (SEQ); /* avoid variable capture */ \
     981             :     for (i = (START); i < asdl_seq_LEN(seq); i++) { \
     982             :         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
     983             :         if (!symtable_visit_ ## TYPE((ST), elt)) \
     984             :             return 0; \
     985             :     } \
     986             : }
     987             : 
     988             : #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
     989             :     int i; \
     990             :     asdl_seq *seq = (SEQ); /* avoid variable capture */ \
     991             :     for (i = (START); i < asdl_seq_LEN(seq); i++) { \
     992             :         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
     993             :         if (!symtable_visit_ ## TYPE((ST), elt)) { \
     994             :             symtable_exit_block((ST), (S)); \
     995             :             return 0; \
     996             :         } \
     997             :     } \
     998             : }
     999             : 
    1000             : static int
    1001        8231 : symtable_visit_stmt(struct symtable *st, stmt_ty s)
    1002             : {
    1003        8231 :     switch (s->kind) {
    1004             :     case FunctionDef_kind:
    1005         821 :         if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
    1006           0 :             return 0;
    1007         821 :         if (s->v.FunctionDef.args->defaults)
    1008          75 :             VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
    1009         821 :         if (s->v.FunctionDef.decorator_list)
    1010          27 :             VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
    1011         821 :         if (!symtable_enter_block(st, s->v.FunctionDef.name,
    1012             :                                   FunctionBlock, (void *)s, s->lineno))
    1013           0 :             return 0;
    1014         821 :         VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
    1015         821 :         VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
    1016         821 :         if (!symtable_exit_block(st, s))
    1017           0 :             return 0;
    1018         821 :         break;
    1019             :     case ClassDef_kind: {
    1020             :         PyObject *tmp;
    1021         144 :         if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
    1022           0 :             return 0;
    1023         144 :         VISIT_SEQ(st, expr, s->v.ClassDef.bases);
    1024         144 :         if (s->v.ClassDef.decorator_list)
    1025           0 :             VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
    1026         144 :         if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
    1027             :                                   (void *)s, s->lineno))
    1028           0 :             return 0;
    1029         144 :         tmp = st->st_private;
    1030         144 :         st->st_private = s->v.ClassDef.name;
    1031         144 :         VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
    1032         144 :         st->st_private = tmp;
    1033         144 :         if (!symtable_exit_block(st, s))
    1034           0 :             return 0;
    1035         144 :         break;
    1036             :     }
    1037             :     case Return_kind:
    1038         999 :         if (s->v.Return.value) {
    1039         984 :             VISIT(st, expr, s->v.Return.value);
    1040         984 :             st->st_cur->ste_returns_value = 1;
    1041         984 :             if (st->st_cur->ste_generator) {
    1042           0 :                 PyErr_SetString(PyExc_SyntaxError,
    1043             :                     RETURN_VAL_IN_GENERATOR);
    1044           0 :                 PyErr_SyntaxLocation(st->st_filename,
    1045             :                              s->lineno);
    1046           0 :                 return 0;
    1047             :             }
    1048             :         }
    1049         999 :         break;
    1050             :     case Delete_kind:
    1051           2 :         VISIT_SEQ(st, expr, s->v.Delete.targets);
    1052           2 :         break;
    1053             :     case Assign_kind:
    1054        2207 :         VISIT_SEQ(st, expr, s->v.Assign.targets);
    1055        2207 :         VISIT(st, expr, s->v.Assign.value);
    1056        2207 :         break;
    1057             :     case AugAssign_kind:
    1058          48 :         VISIT(st, expr, s->v.AugAssign.target);
    1059          48 :         VISIT(st, expr, s->v.AugAssign.value);
    1060          48 :         break;
    1061             :     case Print_kind:
    1062           8 :         if (s->v.Print.dest)
    1063           0 :             VISIT(st, expr, s->v.Print.dest);
    1064           8 :         VISIT_SEQ(st, expr, s->v.Print.values);
    1065           8 :         break;
    1066             :     case For_kind:
    1067         172 :         VISIT(st, expr, s->v.For.target);
    1068         172 :         VISIT(st, expr, s->v.For.iter);
    1069         172 :         VISIT_SEQ(st, stmt, s->v.For.body);
    1070         172 :         if (s->v.For.orelse)
    1071           0 :             VISIT_SEQ(st, stmt, s->v.For.orelse);
    1072         172 :         break;
    1073             :     case While_kind:
    1074          31 :         VISIT(st, expr, s->v.While.test);
    1075          31 :         VISIT_SEQ(st, stmt, s->v.While.body);
    1076          31 :         if (s->v.While.orelse)
    1077           0 :             VISIT_SEQ(st, stmt, s->v.While.orelse);
    1078          31 :         break;
    1079             :     case If_kind:
    1080             :         /* XXX if 0: and lookup_yield() hacks */
    1081        1293 :         VISIT(st, expr, s->v.If.test);
    1082        1293 :         VISIT_SEQ(st, stmt, s->v.If.body);
    1083        1293 :         if (s->v.If.orelse)
    1084         598 :             VISIT_SEQ(st, stmt, s->v.If.orelse);
    1085        1293 :         break;
    1086             :     case Raise_kind:
    1087         245 :         if (s->v.Raise.type) {
    1088         233 :             VISIT(st, expr, s->v.Raise.type);
    1089         233 :             if (s->v.Raise.inst) {
    1090           0 :                 VISIT(st, expr, s->v.Raise.inst);
    1091           0 :                 if (s->v.Raise.tback)
    1092           0 :                     VISIT(st, expr, s->v.Raise.tback);
    1093             :             }
    1094             :         }
    1095         245 :         break;
    1096             :     case TryExcept_kind:
    1097          60 :         VISIT_SEQ(st, stmt, s->v.TryExcept.body);
    1098          60 :         VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
    1099          60 :         VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
    1100          60 :         break;
    1101             :     case TryFinally_kind:
    1102           0 :         VISIT_SEQ(st, stmt, s->v.TryFinally.body);
    1103           0 :         VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
    1104           0 :         break;
    1105             :     case Assert_kind:
    1106          73 :         VISIT(st, expr, s->v.Assert.test);
    1107          73 :         if (s->v.Assert.msg)
    1108          31 :             VISIT(st, expr, s->v.Assert.msg);
    1109          73 :         break;
    1110             :     case Import_kind:
    1111          69 :         VISIT_SEQ(st, alias, s->v.Import.names);
    1112             :         /* XXX Don't have the lineno available inside
    1113             :            visit_alias */
    1114          69 :         if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
    1115          13 :             st->st_cur->ste_opt_lineno = s->lineno;
    1116          69 :         break;
    1117             :     case ImportFrom_kind:
    1118         176 :         VISIT_SEQ(st, alias, s->v.ImportFrom.names);
    1119             :         /* XXX Don't have the lineno available inside
    1120             :            visit_alias */
    1121         176 :         if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
    1122          19 :             st->st_cur->ste_opt_lineno = s->lineno;
    1123         176 :         break;
    1124             :     case Exec_kind:
    1125           0 :         VISIT(st, expr, s->v.Exec.body);
    1126           0 :         if (!st->st_cur->ste_opt_lineno)
    1127           0 :             st->st_cur->ste_opt_lineno = s->lineno;
    1128           0 :         if (s->v.Exec.globals) {
    1129           0 :             st->st_cur->ste_unoptimized |= OPT_EXEC;
    1130           0 :             VISIT(st, expr, s->v.Exec.globals);
    1131           0 :             if (s->v.Exec.locals)
    1132           0 :                 VISIT(st, expr, s->v.Exec.locals);
    1133             :         } else {
    1134           0 :             st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
    1135             :         }
    1136           0 :         break;
    1137             :     case Global_kind: {
    1138             :         int i;
    1139           0 :         asdl_seq *seq = s->v.Global.names;
    1140           0 :         for (i = 0; i < asdl_seq_LEN(seq); i++) {
    1141           0 :             identifier name = (identifier)asdl_seq_GET(seq, i);
    1142           0 :             char *c_name = PyString_AS_STRING(name);
    1143           0 :             long cur = symtable_lookup(st, name);
    1144           0 :             if (cur < 0)
    1145           0 :                 return 0;
    1146           0 :             if (cur & (DEF_LOCAL | USE)) {
    1147             :                 char buf[256];
    1148           0 :                 if (cur & DEF_LOCAL)
    1149           0 :                     PyOS_snprintf(buf, sizeof(buf),
    1150             :                                   GLOBAL_AFTER_ASSIGN,
    1151             :                                   c_name);
    1152             :                 else
    1153           0 :                     PyOS_snprintf(buf, sizeof(buf),
    1154             :                                   GLOBAL_AFTER_USE,
    1155             :                                   c_name);
    1156           0 :                 if (!symtable_warn(st, buf, s->lineno))
    1157           0 :                     return 0;
    1158             :             }
    1159           0 :             if (!symtable_add_def(st, name, DEF_GLOBAL))
    1160           0 :                 return 0;
    1161             :         }
    1162           0 :         break;
    1163             :     }
    1164             :     case Expr_kind:
    1165        1743 :         VISIT(st, expr, s->v.Expr.value);
    1166        1743 :         break;
    1167             :     case Pass_kind:
    1168             :     case Break_kind:
    1169             :     case Continue_kind:
    1170             :         /* nothing to do here */
    1171         132 :         break;
    1172             :     case With_kind:
    1173           8 :         VISIT(st, expr, s->v.With.context_expr);
    1174           8 :         if (s->v.With.optional_vars) {
    1175           8 :             VISIT(st, expr, s->v.With.optional_vars);
    1176             :         }
    1177           8 :         VISIT_SEQ(st, stmt, s->v.With.body);
    1178           8 :         break;
    1179             :     }
    1180        8231 :     return 1;
    1181             : }
    1182             : 
    1183             : static int
    1184       29595 : symtable_visit_expr(struct symtable *st, expr_ty e)
    1185             : {
    1186       29595 :     switch (e->kind) {
    1187             :     case BoolOp_kind:
    1188          56 :         VISIT_SEQ(st, expr, e->v.BoolOp.values);
    1189          56 :         break;
    1190             :     case BinOp_kind:
    1191         372 :         VISIT(st, expr, e->v.BinOp.left);
    1192         372 :         VISIT(st, expr, e->v.BinOp.right);
    1193         372 :         break;
    1194             :     case UnaryOp_kind:
    1195         254 :         VISIT(st, expr, e->v.UnaryOp.operand);
    1196         254 :         break;
    1197             :     case Lambda_kind: {
    1198           4 :         if (!GET_IDENTIFIER(lambda))
    1199           0 :             return 0;
    1200           4 :         if (e->v.Lambda.args->defaults)
    1201           0 :             VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
    1202           4 :         if (!symtable_enter_block(st, lambda,
    1203             :                                   FunctionBlock, (void *)e, e->lineno))
    1204           0 :             return 0;
    1205           4 :         VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
    1206           4 :         VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
    1207           4 :         if (!symtable_exit_block(st, (void *)e))
    1208           0 :             return 0;
    1209           4 :         break;
    1210             :     }
    1211             :     case IfExp_kind:
    1212          14 :         VISIT(st, expr, e->v.IfExp.test);
    1213          14 :         VISIT(st, expr, e->v.IfExp.body);
    1214          14 :         VISIT(st, expr, e->v.IfExp.orelse);
    1215          14 :         break;
    1216             :     case Dict_kind:
    1217          44 :         VISIT_SEQ(st, expr, e->v.Dict.keys);
    1218          44 :         VISIT_SEQ(st, expr, e->v.Dict.values);
    1219          44 :         break;
    1220             :     case Set_kind:
    1221           1 :         VISIT_SEQ(st, expr, e->v.Set.elts);
    1222           1 :         break;
    1223             :     case ListComp_kind:
    1224          11 :         VISIT(st, expr, e->v.ListComp.elt);
    1225          11 :         VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
    1226          11 :         break;
    1227             :     case GeneratorExp_kind:
    1228           9 :         if (!symtable_visit_genexp(st, e))
    1229           0 :             return 0;
    1230           9 :         break;
    1231             :     case SetComp_kind:
    1232           0 :         if (!symtable_visit_setcomp(st, e))
    1233           0 :             return 0;
    1234           0 :         break;
    1235             :     case DictComp_kind:
    1236           2 :         if (!symtable_visit_dictcomp(st, e))
    1237           0 :             return 0;
    1238           2 :         break;
    1239             :     case Yield_kind:
    1240          12 :         if (e->v.Yield.value)
    1241          12 :             VISIT(st, expr, e->v.Yield.value);
    1242          12 :         st->st_cur->ste_generator = 1;
    1243          12 :         if (st->st_cur->ste_returns_value) {
    1244           0 :             PyErr_SetString(PyExc_SyntaxError,
    1245             :                 RETURN_VAL_IN_GENERATOR);
    1246           0 :             PyErr_SyntaxLocation(st->st_filename,
    1247             :                          e->lineno);
    1248           0 :             return 0;
    1249             :         }
    1250          12 :         break;
    1251             :     case Compare_kind:
    1252         911 :         VISIT(st, expr, e->v.Compare.left);
    1253         911 :         VISIT_SEQ(st, expr, e->v.Compare.comparators);
    1254         911 :         break;
    1255             :     case Call_kind:
    1256        3664 :         VISIT(st, expr, e->v.Call.func);
    1257        3664 :         VISIT_SEQ(st, expr, e->v.Call.args);
    1258        3664 :         VISIT_SEQ(st, keyword, e->v.Call.keywords);
    1259        3664 :         if (e->v.Call.starargs)
    1260           7 :             VISIT(st, expr, e->v.Call.starargs);
    1261        3664 :         if (e->v.Call.kwargs)
    1262           5 :             VISIT(st, expr, e->v.Call.kwargs);
    1263        3664 :         break;
    1264             :     case Repr_kind:
    1265           0 :         VISIT(st, expr, e->v.Repr.value);
    1266           0 :         break;
    1267             :     case Num_kind:
    1268             :     case Str_kind:
    1269             :         /* Nothing to do here. */
    1270        3112 :         break;
    1271             :     /* The following exprs can be assignment targets. */
    1272             :     case Attribute_kind:
    1273        6185 :         VISIT(st, expr, e->v.Attribute.value);
    1274        6185 :         break;
    1275             :     case Subscript_kind:
    1276         284 :         VISIT(st, expr, e->v.Subscript.value);
    1277         284 :         VISIT(st, slice, e->v.Subscript.slice);
    1278         284 :         break;
    1279             :     case Name_kind:
    1280       13909 :         if (!symtable_add_def(st, e->v.Name.id,
    1281       13909 :                               e->v.Name.ctx == Load ? USE : DEF_LOCAL))
    1282           0 :             return 0;
    1283       13909 :         break;
    1284             :     /* child nodes of List and Tuple will have expr_context set */
    1285             :     case List_kind:
    1286         221 :         VISIT_SEQ(st, expr, e->v.List.elts);
    1287         221 :         break;
    1288             :     case Tuple_kind:
    1289         530 :         VISIT_SEQ(st, expr, e->v.Tuple.elts);
    1290         530 :         break;
    1291             :     }
    1292       29595 :     return 1;
    1293             : }
    1294             : 
    1295             : static int
    1296          11 : symtable_implicit_arg(struct symtable *st, int pos)
    1297             : {
    1298          11 :     PyObject *id = PyString_FromFormat(".%d", pos);
    1299          11 :     if (id == NULL)
    1300           0 :         return 0;
    1301          11 :     if (!symtable_add_def(st, id, DEF_PARAM)) {
    1302           0 :         Py_DECREF(id);
    1303           0 :         return 0;
    1304             :     }
    1305          11 :     Py_DECREF(id);
    1306          11 :     return 1;
    1307             : }
    1308             : 
    1309             : static int
    1310         815 : symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
    1311             : {
    1312             :     int i;
    1313             : 
    1314             :     /* go through all the toplevel arguments first */
    1315        2432 :     for (i = 0; i < asdl_seq_LEN(args); i++) {
    1316        1617 :         expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
    1317        1617 :         if (arg->kind == Name_kind) {
    1318             :             assert(arg->v.Name.ctx == Param ||
    1319             :                    (arg->v.Name.ctx == Store && !toplevel));
    1320        1617 :             if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
    1321           0 :                 return 0;
    1322             :         }
    1323           0 :         else if (arg->kind == Tuple_kind) {
    1324             :             assert(arg->v.Tuple.ctx == Store);
    1325           0 :             if (toplevel) {
    1326           0 :                 if (!symtable_implicit_arg(st, i))
    1327           0 :                     return 0;
    1328             :             }
    1329             :         }
    1330             :         else {
    1331           0 :             PyErr_SetString(PyExc_SyntaxError,
    1332             :                             "invalid expression in parameter list");
    1333           0 :             PyErr_SyntaxLocation(st->st_filename,
    1334           0 :                                  st->st_cur->ste_lineno);
    1335           0 :             return 0;
    1336             :         }
    1337             :     }
    1338             : 
    1339         815 :     if (!toplevel) {
    1340           0 :         if (!symtable_visit_params_nested(st, args))
    1341           0 :             return 0;
    1342             :     }
    1343             : 
    1344         815 :     return 1;
    1345             : }
    1346             : 
    1347             : static int
    1348         815 : symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
    1349             : {
    1350             :     int i;
    1351        2432 :     for (i = 0; i < asdl_seq_LEN(args); i++) {
    1352        1617 :         expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
    1353        1617 :         if (arg->kind == Tuple_kind &&
    1354           0 :             !symtable_visit_params(st, arg->v.Tuple.elts, 0))
    1355           0 :             return 0;
    1356             :     }
    1357             : 
    1358         815 :     return 1;
    1359             : }
    1360             : 
    1361             : static int
    1362         825 : symtable_visit_arguments(struct symtable *st, arguments_ty a)
    1363             : {
    1364             :     /* skip default arguments inside function block
    1365             :        XXX should ast be different?
    1366             :     */
    1367         825 :     if (a->args && !symtable_visit_params(st, a->args, 1))
    1368           0 :         return 0;
    1369         825 :     if (a->vararg) {
    1370          17 :         if (!symtable_add_def(st, a->vararg, DEF_PARAM))
    1371           0 :             return 0;
    1372          17 :         st->st_cur->ste_varargs = 1;
    1373             :     }
    1374         825 :     if (a->kwarg) {
    1375          34 :         if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
    1376           0 :             return 0;
    1377          34 :         st->st_cur->ste_varkeywords = 1;
    1378             :     }
    1379         825 :     if (a->args && !symtable_visit_params_nested(st, a->args))
    1380           0 :         return 0;
    1381         825 :     return 1;
    1382             : }
    1383             : 
    1384             : 
    1385             : static int
    1386          68 : symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
    1387             : {
    1388          68 :     if (eh->v.ExceptHandler.type)
    1389          68 :         VISIT(st, expr, eh->v.ExceptHandler.type);
    1390          68 :     if (eh->v.ExceptHandler.name)
    1391          29 :         VISIT(st, expr, eh->v.ExceptHandler.name);
    1392          68 :     VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
    1393          68 :     return 1;
    1394             : }
    1395             : 
    1396             : 
    1397             : static int
    1398         279 : symtable_visit_alias(struct symtable *st, alias_ty a)
    1399             : {
    1400             :     /* Compute store_name, the name actually bound by the import
    1401             :        operation.  It is different than a->name when a->name is a
    1402             :        dotted package name (e.g. spam.eggs)
    1403             :     */
    1404             :     PyObject *store_name;
    1405         279 :     PyObject *name = (a->asname == NULL) ? a->name : a->asname;
    1406         279 :     const char *base = PyString_AS_STRING(name);
    1407         279 :     char *dot = strchr(base, '.');
    1408         279 :     if (dot) {
    1409           0 :         store_name = PyString_FromStringAndSize(base, dot - base);
    1410           0 :         if (!store_name)
    1411           0 :             return 0;
    1412             :     }
    1413             :     else {
    1414         279 :         store_name = name;
    1415         279 :         Py_INCREF(store_name);
    1416             :     }
    1417         279 :     if (strcmp(PyString_AS_STRING(name), "*")) {
    1418         279 :         int r = symtable_add_def(st, store_name, DEF_IMPORT);
    1419         279 :         Py_DECREF(store_name);
    1420         279 :         return r;
    1421             :     }
    1422             :     else {
    1423           0 :         if (st->st_cur->ste_type != ModuleBlock) {
    1424           0 :             int lineno = st->st_cur->ste_lineno;
    1425           0 :             if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
    1426           0 :                 Py_DECREF(store_name);
    1427           0 :                 return 0;
    1428             :             }
    1429             :         }
    1430           0 :         st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
    1431           0 :         Py_DECREF(store_name);
    1432           0 :         return 1;
    1433             :     }
    1434             : }
    1435             : 
    1436             : 
    1437             : static int
    1438          11 : symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
    1439             : {
    1440          11 :     VISIT(st, expr, lc->target);
    1441          11 :     VISIT(st, expr, lc->iter);
    1442          11 :     VISIT_SEQ(st, expr, lc->ifs);
    1443          11 :     return 1;
    1444             : }
    1445             : 
    1446             : 
    1447             : static int
    1448         356 : symtable_visit_keyword(struct symtable *st, keyword_ty k)
    1449             : {
    1450         356 :     VISIT(st, expr, k->value);
    1451         356 :     return 1;
    1452             : }
    1453             : 
    1454             : 
    1455             : static int
    1456         284 : symtable_visit_slice(struct symtable *st, slice_ty s)
    1457             : {
    1458         284 :     switch (s->kind) {
    1459             :     case Slice_kind:
    1460          42 :         if (s->v.Slice.lower)
    1461          32 :             VISIT(st, expr, s->v.Slice.lower)
    1462          42 :         if (s->v.Slice.upper)
    1463          16 :             VISIT(st, expr, s->v.Slice.upper)
    1464          42 :         if (s->v.Slice.step)
    1465           0 :             VISIT(st, expr, s->v.Slice.step)
    1466          42 :         break;
    1467             :     case ExtSlice_kind:
    1468           0 :         VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
    1469           0 :         break;
    1470             :     case Index_kind:
    1471         242 :         VISIT(st, expr, s->v.Index.value)
    1472         242 :         break;
    1473             :     case Ellipsis_kind:
    1474           0 :         break;
    1475             :     }
    1476         284 :     return 1;
    1477             : }
    1478             : 
    1479             : static int
    1480           2 : symtable_new_tmpname(struct symtable *st)
    1481             : {
    1482             :     char tmpname[256];
    1483             :     identifier tmp;
    1484             : 
    1485           4 :     PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
    1486           4 :                   ++st->st_cur->ste_tmpname);
    1487           2 :     tmp = PyString_InternFromString(tmpname);
    1488           2 :     if (!tmp)
    1489           0 :         return 0;
    1490           2 :     if (!symtable_add_def(st, tmp, DEF_LOCAL))
    1491           0 :         return 0;
    1492           2 :     Py_DECREF(tmp);
    1493           2 :     return 1;
    1494             : }
    1495             : 
    1496             : static int
    1497          11 : symtable_handle_comprehension(struct symtable *st, expr_ty e,
    1498             :                               identifier scope_name, asdl_seq *generators,
    1499             :                               expr_ty elt, expr_ty value)
    1500             : {
    1501          11 :     int is_generator = (e->kind == GeneratorExp_kind);
    1502          11 :     int needs_tmp = !is_generator;
    1503          11 :     comprehension_ty outermost = ((comprehension_ty)
    1504             :                                     asdl_seq_GET(generators, 0));
    1505             :     /* Outermost iterator is evaluated in current scope */
    1506          11 :     VISIT(st, expr, outermost->iter);
    1507             :     /* Create comprehension scope for the rest */
    1508          22 :     if (!scope_name ||
    1509          11 :         !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
    1510           0 :         return 0;
    1511             :     }
    1512          11 :     st->st_cur->ste_generator = is_generator;
    1513             :     /* Outermost iter is received as an argument */
    1514          11 :     if (!symtable_implicit_arg(st, 0)) {
    1515           0 :         symtable_exit_block(st, (void *)e);
    1516           0 :         return 0;
    1517             :     }
    1518             :     /* Allocate temporary name if needed */
    1519          11 :     if (needs_tmp && !symtable_new_tmpname(st)) {
    1520           0 :         symtable_exit_block(st, (void *)e);
    1521           0 :         return 0;
    1522             :     }
    1523          11 :     VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
    1524          11 :     VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
    1525          11 :     VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
    1526             :                             generators, 1, (void*)e);
    1527          11 :     if (value)
    1528           2 :         VISIT_IN_BLOCK(st, expr, value, (void*)e);
    1529          11 :     VISIT_IN_BLOCK(st, expr, elt, (void*)e);
    1530          11 :     return symtable_exit_block(st, (void *)e);
    1531             : }
    1532             : 
    1533             : static int
    1534           9 : symtable_visit_genexp(struct symtable *st, expr_ty e)
    1535             : {
    1536           9 :     return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
    1537             :                                          e->v.GeneratorExp.generators,
    1538             :                                          e->v.GeneratorExp.elt, NULL);
    1539             : }
    1540             : 
    1541             : static int
    1542           0 : symtable_visit_setcomp(struct symtable *st, expr_ty e)
    1543             : {
    1544           0 :     return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
    1545             :                                          e->v.SetComp.generators,
    1546             :                                          e->v.SetComp.elt, NULL);
    1547             : }
    1548             : 
    1549             : static int
    1550           2 : symtable_visit_dictcomp(struct symtable *st, expr_ty e)
    1551             : {
    1552           2 :     return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
    1553             :                                          e->v.DictComp.generators,
    1554             :                                          e->v.DictComp.key,
    1555             :                                          e->v.DictComp.value);
    1556             : }

Generated by: LCOV version 1.10