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