Python-2.7.3/Python/symtable.c

No issues found

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