You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1556 lines
48 KiB

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