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