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