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