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