2 #include "Python-ast.h"
5 #include "structmember.h"
7 /* error strings used for warnings */
8 #define GLOBAL_AFTER_ASSIGN \
9 "name '%.400s' is assigned to before global declaration"
11 #define GLOBAL_AFTER_USE \
12 "name '%.400s' is used prior to global declaration"
14 #define IMPORT_STAR_WARNING "import * only allowed at module level"
18 PySTEntry_New(struct symtable
*st
, identifier name
, _Py_block_ty block
,
19 void *key
, int lineno
)
21 PySTEntryObject
*ste
= NULL
;
24 k
= PyLong_FromVoidPtr(key
);
27 ste
= (PySTEntryObject
*)PyObject_New(PySTEntryObject
,
36 ste
->ste_symbols
= NULL
;
37 ste
->ste_varnames
= NULL
;
38 ste
->ste_children
= NULL
;
40 ste
->ste_symbols
= PyDict_New();
41 if (ste
->ste_symbols
== NULL
)
44 ste
->ste_varnames
= PyList_New(0);
45 if (ste
->ste_varnames
== NULL
)
48 ste
->ste_children
= PyList_New(0);
49 if (ste
->ste_children
== NULL
)
52 ste
->ste_type
= block
;
53 ste
->ste_unoptimized
= 0;
57 ste
->ste_varkeywords
= 0;
58 ste
->ste_opt_lineno
= 0;
60 ste
->ste_lineno
= lineno
;
62 if (st
->st_cur
!= NULL
&&
63 (st
->st_cur
->ste_nested
||
64 st
->st_cur
->ste_type
== FunctionBlock
))
66 ste
->ste_child_free
= 0;
67 ste
->ste_generator
= 0;
69 if (PyDict_SetItem(st
->st_symbols
, ste
->ste_id
, (PyObject
*)ste
) < 0)
79 ste_repr(PySTEntryObject
*ste
)
83 PyOS_snprintf(buf
, sizeof(buf
),
84 "<symtable entry %.100s(%ld), line %d>",
85 PyString_AS_STRING(ste
->ste_name
),
86 PyInt_AS_LONG(ste
->ste_id
), ste
->ste_lineno
);
87 return PyString_FromString(buf
);
91 ste_dealloc(PySTEntryObject
*ste
)
93 ste
->ste_table
= NULL
;
94 Py_XDECREF(ste
->ste_id
);
95 Py_XDECREF(ste
->ste_name
);
96 Py_XDECREF(ste
->ste_symbols
);
97 Py_XDECREF(ste
->ste_varnames
);
98 Py_XDECREF(ste
->ste_children
);
102 #define OFF(x) offsetof(PySTEntryObject, x)
104 static PyMemberDef ste_memberlist
[] = {
105 {"id", T_OBJECT
, OFF(ste_id
), READONLY
},
106 {"name", T_OBJECT
, OFF(ste_name
), READONLY
},
107 {"symbols", T_OBJECT
, OFF(ste_symbols
), READONLY
},
108 {"varnames", T_OBJECT
, OFF(ste_varnames
), READONLY
},
109 {"children", T_OBJECT
, OFF(ste_children
), READONLY
},
110 {"type", T_INT
, OFF(ste_type
), READONLY
},
111 {"lineno", T_INT
, OFF(ste_lineno
), READONLY
},
115 PyTypeObject PySTEntry_Type
= {
116 PyObject_HEAD_INIT(&PyType_Type
)
119 sizeof(PySTEntryObject
),
121 (destructor
)ste_dealloc
, /* tp_dealloc */
126 (reprfunc
)ste_repr
, /* tp_repr */
127 0, /* tp_as_number */
128 0, /* tp_as_sequence */
129 0, /* tp_as_mapping */
133 PyObject_GenericGetAttr
, /* tp_getattro */
135 0, /* tp_as_buffer */
136 Py_TPFLAGS_DEFAULT
, /* tp_flags */
140 0, /* tp_richcompare */
141 0, /* tp_weaklistoffset */
145 ste_memberlist
, /* tp_members */
149 0, /* tp_descr_get */
150 0, /* tp_descr_set */
151 0, /* tp_dictoffset */
157 static int symtable_analyze(struct symtable
*st
);
158 static int symtable_warn(struct symtable
*st
, char *msg
, int lineno
);
159 static int symtable_enter_block(struct symtable
*st
, identifier name
,
160 _Py_block_ty block
, void *ast
, int lineno
);
161 static int symtable_exit_block(struct symtable
*st
, void *ast
);
162 static int symtable_visit_stmt(struct symtable
*st
, stmt_ty s
);
163 static int symtable_visit_expr(struct symtable
*st
, expr_ty s
);
164 static int symtable_visit_genexp(struct symtable
*st
, expr_ty s
);
165 static int symtable_visit_arguments(struct symtable
*st
, arguments_ty
);
166 static int symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty
);
167 static int symtable_visit_alias(struct symtable
*st
, alias_ty
);
168 static int symtable_visit_comprehension(struct symtable
*st
, comprehension_ty
);
169 static int symtable_visit_keyword(struct symtable
*st
, keyword_ty
);
170 static int symtable_visit_slice(struct symtable
*st
, slice_ty
);
171 static int symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int top
);
172 static int symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
);
173 static int symtable_implicit_arg(struct symtable
*st
, int pos
);
176 static identifier top
= NULL
, lambda
= NULL
, genexpr
= NULL
;
178 #define GET_IDENTIFIER(VAR) \
179 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
181 #define DUPLICATE_ARGUMENT \
182 "duplicate argument '%s' in function definition"
184 static struct symtable
*
189 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
193 st
->st_filename
= NULL
;
194 st
->st_symbols
= NULL
;
196 if ((st
->st_stack
= PyList_New(0)) == NULL
)
198 if ((st
->st_symbols
= PyDict_New()) == NULL
)
202 st
->st_private
= NULL
;
210 PySymtable_Build(mod_ty mod
, const char *filename
, PyFutureFeatures
*future
)
212 struct symtable
*st
= symtable_new();
218 st
->st_filename
= filename
;
219 st
->st_future
= future
;
220 symtable_enter_block(st
, GET_IDENTIFIER(top
), ModuleBlock
,
222 st
->st_top
= st
->st_cur
;
223 st
->st_cur
->ste_unoptimized
= OPT_TOPLEVEL
;
224 /* Any other top-level initialization? */
227 seq
= mod
->v
.Module
.body
;
228 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
229 if (!symtable_visit_stmt(st
, asdl_seq_GET(seq
, i
)))
232 case Expression_kind
:
233 if (!symtable_visit_expr(st
, mod
->v
.Expression
.body
))
236 case Interactive_kind
:
237 seq
= mod
->v
.Interactive
.body
;
238 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
239 if (!symtable_visit_stmt(st
, asdl_seq_GET(seq
, i
)))
243 PyErr_SetString(PyExc_RuntimeError
,
244 "this compiler does not handle Suites");
247 if (!symtable_exit_block(st
, (void *)mod
)) {
251 if (symtable_analyze(st
))
256 (void) symtable_exit_block(st
, (void *)mod
);
262 PySymtable_Free(struct symtable
*st
)
264 Py_XDECREF(st
->st_symbols
);
265 Py_XDECREF(st
->st_stack
);
266 PyMem_Free((void *)st
);
270 PySymtable_Lookup(struct symtable
*st
, void *key
)
274 k
= PyLong_FromVoidPtr(key
);
277 v
= PyDict_GetItem(st
->st_symbols
, k
);
279 assert(PySTEntry_Check(v
));
283 PyErr_SetString(PyExc_KeyError
,
284 "unknown symbol table entry");
288 return (PySTEntryObject
*)v
;
292 PyST_GetScope(PySTEntryObject
*ste
, PyObject
*name
)
294 PyObject
*v
= PyDict_GetItem(ste
->ste_symbols
, name
);
297 assert(PyInt_Check(v
));
298 return (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
302 /* Analyze raw symbol information to determine scope of each name.
304 The next several functions are helpers for PySymtable_Analyze(),
305 which determines whether a name is local, global, or free. In addition,
306 it determines which local variables are cell variables; they provide
307 bindings that are used for free variables in enclosed blocks.
309 There are also two kinds of free variables, implicit and explicit. An
310 explicit global is declared with the global statement. An implicit
311 global is a free variable for which the compiler has found no binding
312 in an enclosing function scope. The implicit global is either a global
313 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
314 to handle these names to implement slightly odd semantics. In such a
315 block, the name is treated as global until it is assigned to; then it
316 is treated as a local.
318 The symbol table requires two passes to determine the scope of each name.
319 The first pass collects raw facts from the AST: the name is a parameter
320 here, the name is used by not defined here, etc. The second pass analyzes
321 these facts during a pass over the PySTEntryObjects created during pass 1.
323 When a function is entered during the second pass, the parent passes
324 the set of all name bindings visible to its children. These bindings
325 are used to determine if the variable is free or an implicit global.
326 After doing the local analysis, it analyzes each of its child blocks
327 using an updated set of name bindings.
329 The children update the free variable set. If a local variable is free
330 in a child, the variable is marked as a cell. The current function must
331 provide runtime storage for the variable that may outlive the function's
332 frame. Cell variables are removed from the free set before the analyze
333 function returns to its parent.
335 The sets of bound and free variables are implemented as dictionaries
336 mapping strings to None.
339 #define SET_SCOPE(DICT, NAME, I) { \
340 PyObject *o = PyInt_FromLong(I); \
343 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
350 /* Decide on scope of name, given flags.
352 The dicts passed in as arguments are modified as necessary.
353 ste is passed so that flags can be updated.
357 analyze_name(PySTEntryObject
*ste
, PyObject
*dict
, PyObject
*name
, long flags
,
358 PyObject
*bound
, PyObject
*local
, PyObject
*free
,
361 if (flags
& DEF_GLOBAL
) {
362 if (flags
& DEF_PARAM
) {
363 PyErr_Format(PyExc_SyntaxError
,
364 "name '%s' is local and global",
365 PyString_AS_STRING(name
));
368 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
369 if (PyDict_SetItem(global
, name
, Py_None
) < 0)
371 if (bound
&& PyDict_GetItem(bound
, name
)) {
372 if (PyDict_DelItem(bound
, name
) < 0)
377 if (flags
& DEF_BOUND
) {
378 SET_SCOPE(dict
, name
, LOCAL
);
379 if (PyDict_SetItem(local
, name
, Py_None
) < 0)
381 if (PyDict_GetItem(global
, name
)) {
382 if (PyDict_DelItem(global
, name
) < 0)
387 /* If an enclosing block has a binding for this name, it
388 is a free variable rather than a global variable.
389 Note that having a non-NULL bound implies that the block
392 if (bound
&& PyDict_GetItem(bound
, name
)) {
393 SET_SCOPE(dict
, name
, FREE
);
395 if (PyDict_SetItem(free
, name
, Py_None
) < 0)
399 /* If a parent has a global statement, then call it global
400 explicit? It could also be global implicit.
402 else if (global
&& PyDict_GetItem(global
, name
)) {
403 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
409 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
412 return 0; /* Can't get here */
417 /* If a name is defined in free and also in locals, then this block
418 provides the binding for the free variable. The name should be
419 marked CELL in this block and removed from the free list.
421 Note that the current block's free variables are included in free.
422 That's safe because no name can be free and local in the same scope.
426 analyze_cells(PyObject
*scope
, PyObject
*free
)
428 PyObject
*name
, *v
, *w
;
429 int pos
= 0, success
= 0;
431 w
= PyInt_FromLong(CELL
);
434 while (PyDict_Next(scope
, &pos
, &name
, &v
)) {
436 assert(PyInt_Check(v
));
437 flags
= PyInt_AS_LONG(v
);
440 if (!PyDict_GetItem(free
, name
))
442 /* Replace LOCAL with CELL for this name, and remove
443 from free. It is safe to replace the value of name
444 in the dict, because it will not cause a resize.
446 if (PyDict_SetItem(scope
, name
, w
) < 0)
448 if (!PyDict_DelItem(free
, name
) < 0)
457 /* Check for illegal statements in unoptimized namespaces */
459 check_unoptimized(const PySTEntryObject
* ste
) {
463 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
464 || !(ste
->ste_free
|| ste
->ste_child_free
))
467 trailer
= (ste
->ste_child_free
?
468 "contains a nested function with free variables" :
469 "is a nested function");
471 switch (ste
->ste_unoptimized
) {
472 case OPT_TOPLEVEL
: /* exec / import * at top-level is fine */
473 case OPT_EXEC
: /* qualified exec is fine */
475 case OPT_IMPORT_STAR
:
476 PyOS_snprintf(buf
, sizeof(buf
),
477 "import * is not allowed in function '%.100s' "
479 PyString_AS_STRING(ste
->ste_name
), trailer
);
482 PyOS_snprintf(buf
, sizeof(buf
),
483 "unqualified exec is not allowed in function "
485 PyString_AS_STRING(ste
->ste_name
), trailer
);
488 PyOS_snprintf(buf
, sizeof(buf
),
489 "function '%.100s' uses import * and bare exec, "
490 "which are illegal because it %s",
491 PyString_AS_STRING(ste
->ste_name
), trailer
);
495 PyErr_SetString(PyExc_SyntaxError
, buf
);
496 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
497 ste
->ste_opt_lineno
);
501 /* Enter the final scope information into the st_symbols dict.
503 * All arguments are dicts. Modifies symbols, others are read-only.
506 update_symbols(PyObject
*symbols
, PyObject
*scope
,
507 PyObject
*bound
, PyObject
*free
, int class)
509 PyObject
*name
, *v
, *u
, *w
, *free_value
= NULL
;
512 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
514 assert(PyInt_Check(v
));
515 flags
= PyInt_AS_LONG(v
);
516 w
= PyDict_GetItem(scope
, name
);
517 assert(w
&& PyInt_Check(w
));
518 i
= PyInt_AS_LONG(w
);
519 flags
|= (i
<< SCOPE_OFF
);
520 u
= PyInt_FromLong(flags
);
521 if (PyDict_SetItem(symbols
, name
, u
) < 0) {
528 free_value
= PyInt_FromLong(FREE
<< SCOPE_OFF
);
532 /* add a free variable when it's only use is for creating a closure */
534 while (PyDict_Next(free
, &pos
, &name
, &v
)) {
535 PyObject
*o
= PyDict_GetItem(symbols
, name
);
538 /* It could be a free variable in a method of
539 the class that has the same name as a local
540 or global in the class scope.
543 PyInt_AS_LONG(o
) & (DEF_BOUND
| DEF_GLOBAL
)) {
544 long i
= PyInt_AS_LONG(o
) | DEF_FREE_CLASS
;
545 o
= PyInt_FromLong(i
);
547 Py_DECREF(free_value
);
550 if (PyDict_SetItem(symbols
, name
, o
) < 0) {
552 Py_DECREF(free_value
);
557 /* else it's not free, probably a cell */
560 if (!PyDict_GetItem(bound
, name
))
561 continue; /* it's a global */
563 if (PyDict_SetItem(symbols
, name
, free_value
) < 0) {
564 Py_DECREF(free_value
);
568 Py_DECREF(free_value
);
572 /* Make final symbol table decisions for block of ste.
574 ste -- current symtable entry (input/output)
575 bound -- set of variables bound in enclosing scopes (input)
576 free -- set of free variables in enclosed scopes (output)
577 globals -- set of declared global variables in enclosing scopes (input)
581 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
584 PyObject
*name
, *v
, *local
= NULL
, *scope
= NULL
, *newbound
= NULL
;
585 PyObject
*newglobal
= NULL
, *newfree
= NULL
;
586 int i
, pos
= 0, success
= 0;
588 local
= PyDict_New();
591 scope
= PyDict_New();
594 newglobal
= PyDict_New();
597 newfree
= PyDict_New();
600 newbound
= PyDict_New();
604 if (ste
->ste_type
== ClassBlock
) {
605 /* make a copy of globals before calling analyze_name(),
606 because global statements in the class have no effect
609 if (PyDict_Update(newglobal
, global
) < 0)
612 if (PyDict_Update(newbound
, bound
) < 0)
616 assert(PySTEntry_Check(ste
));
617 assert(PyDict_Check(ste
->ste_symbols
));
618 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
619 long flags
= PyInt_AS_LONG(v
);
620 if (!analyze_name(ste
, scope
, name
, flags
, bound
, local
, free
,
625 if (ste
->ste_type
!= ClassBlock
) {
626 if (ste
->ste_type
== FunctionBlock
) {
627 if (PyDict_Update(newbound
, local
) < 0)
631 if (PyDict_Update(newbound
, bound
) < 0)
634 if (PyDict_Update(newglobal
, global
) < 0)
638 /* Recursively call analyze_block() on each child block */
639 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
640 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
641 PySTEntryObject
* entry
;
642 assert(c
&& PySTEntry_Check(c
));
643 entry
= (PySTEntryObject
*)c
;
644 if (!analyze_block(entry
, newbound
, newfree
, newglobal
))
646 if (entry
->ste_free
|| entry
->ste_child_free
)
647 ste
->ste_child_free
= 1;
650 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scope
, newfree
))
652 if (!update_symbols(ste
->ste_symbols
, scope
, bound
, newfree
,
653 ste
->ste_type
== ClassBlock
))
655 if (!check_unoptimized(ste
))
658 if (PyDict_Update(free
, newfree
) < 0)
664 Py_XDECREF(newbound
);
665 Py_XDECREF(newglobal
);
668 assert(PyErr_Occurred());
673 symtable_analyze(struct symtable
*st
)
675 PyObject
*free
, *global
;
681 global
= PyDict_New();
686 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
694 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
696 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
697 lineno
, NULL
, NULL
) < 0) {
698 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
699 PyErr_SetString(PyExc_SyntaxError
, msg
);
700 PyErr_SyntaxLocation(st
->st_filename
,
701 st
->st_cur
->ste_lineno
);
708 /* symtable_enter_block() gets a reference via PySTEntry_New().
709 This reference is released when the block is exited, via the DECREF
710 in symtable_exit_block().
714 symtable_exit_block(struct symtable
*st
, void *ast
)
718 Py_DECREF(st
->st_cur
);
719 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
721 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
723 Py_INCREF(st
->st_cur
);
724 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
731 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
732 void *ast
, int lineno
)
734 PySTEntryObject
*prev
= NULL
;
738 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
741 Py_DECREF(st
->st_cur
);
743 st
->st_cur
= PySTEntry_New(st
, name
, block
, ast
, lineno
);
744 if (name
== GET_IDENTIFIER(top
))
745 st
->st_global
= st
->st_cur
->ste_symbols
;
747 if (PyList_Append(prev
->ste_children
,
748 (PyObject
*)st
->st_cur
) < 0) {
756 symtable_lookup(struct symtable
*st
, PyObject
*name
)
759 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
762 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
766 return PyInt_AsLong(o
);
770 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
775 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
779 dict
= st
->st_cur
->ste_symbols
;
780 if ((o
= PyDict_GetItem(dict
, mangled
))) {
781 val
= PyInt_AS_LONG(o
);
782 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
783 /* Is it better to use 'mangled' or 'name' here? */
784 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
785 PyString_AsString(name
));
786 PyErr_SyntaxLocation(st
->st_filename
,
787 st
->st_cur
->ste_lineno
);
793 o
= PyInt_FromLong(val
);
796 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
802 if (flag
& DEF_PARAM
) {
803 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
805 } else if (flag
& DEF_GLOBAL
) {
806 /* XXX need to update DEF_GLOBAL for other flags too;
807 perhaps only DEF_FREE_GLOBAL */
809 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
810 val
|= PyInt_AS_LONG(o
);
812 o
= PyInt_FromLong(val
);
815 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
829 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
830 They use the ASDL name to synthesize the name of the C type and the visit
833 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
834 useful if the first node in the sequence requires special treatment.
837 #define VISIT(ST, TYPE, V) \
838 if (!symtable_visit_ ## TYPE((ST), (V))) \
841 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
842 if (!symtable_visit_ ## TYPE((ST), (V))) { \
843 symtable_exit_block((ST), (S)); \
847 #define VISIT_SEQ(ST, TYPE, SEQ) { \
849 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
850 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
851 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
852 if (!symtable_visit_ ## TYPE((ST), elt)) \
857 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
859 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
860 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
861 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
862 if (!symtable_visit_ ## TYPE((ST), elt)) { \
863 symtable_exit_block((ST), (S)); \
869 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
871 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
872 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
873 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
874 if (!symtable_visit_ ## TYPE((ST), elt)) \
879 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
881 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
882 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
883 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
884 if (!symtable_visit_ ## TYPE((ST), elt)) { \
885 symtable_exit_block((ST), (S)); \
892 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
895 case FunctionDef_kind
:
896 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
898 if (s
->v
.FunctionDef
.args
->defaults
)
899 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
900 if (s
->v
.FunctionDef
.decorators
)
901 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorators
);
902 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
903 FunctionBlock
, (void *)s
, s
->lineno
))
905 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
906 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
907 if (!symtable_exit_block(st
, s
))
910 case ClassDef_kind
: {
912 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
914 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
915 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
916 (void *)s
, s
->lineno
))
918 tmp
= st
->st_private
;
919 st
->st_private
= s
->v
.ClassDef
.name
;
920 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
921 st
->st_private
= tmp
;
922 if (!symtable_exit_block(st
, s
))
927 if (s
->v
.Return
.value
)
928 VISIT(st
, expr
, s
->v
.Return
.value
);
931 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
934 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
935 VISIT(st
, expr
, s
->v
.Assign
.value
);
938 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
939 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
943 VISIT(st
, expr
, s
->v
.Print
.dest
);
944 VISIT_SEQ(st
, expr
, s
->v
.Print
.values
);
947 VISIT(st
, expr
, s
->v
.For
.target
);
948 VISIT(st
, expr
, s
->v
.For
.iter
);
949 VISIT_SEQ(st
, stmt
, s
->v
.For
.body
);
951 VISIT_SEQ(st
, stmt
, s
->v
.For
.orelse
);
954 VISIT(st
, expr
, s
->v
.While
.test
);
955 VISIT_SEQ(st
, stmt
, s
->v
.While
.body
);
956 if (s
->v
.While
.orelse
)
957 VISIT_SEQ(st
, stmt
, s
->v
.While
.orelse
);
960 /* XXX if 0: and lookup_yield() hacks */
961 VISIT(st
, expr
, s
->v
.If
.test
);
962 VISIT_SEQ(st
, stmt
, s
->v
.If
.body
);
964 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
967 if (s
->v
.Raise
.type
) {
968 VISIT(st
, expr
, s
->v
.Raise
.type
);
969 if (s
->v
.Raise
.inst
) {
970 VISIT(st
, expr
, s
->v
.Raise
.inst
);
971 if (s
->v
.Raise
.tback
)
972 VISIT(st
, expr
, s
->v
.Raise
.tback
);
977 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
978 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
979 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
981 case TryFinally_kind
:
982 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
983 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
986 VISIT(st
, expr
, s
->v
.Assert
.test
);
988 VISIT(st
, expr
, s
->v
.Assert
.msg
);
991 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
992 /* XXX Don't have the lineno available inside
994 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
995 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
997 case ImportFrom_kind
:
998 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
999 /* XXX Don't have the lineno available inside
1001 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1002 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1005 VISIT(st
, expr
, s
->v
.Exec
.body
);
1006 if (!st
->st_cur
->ste_opt_lineno
)
1007 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1008 if (s
->v
.Exec
.globals
) {
1009 st
->st_cur
->ste_unoptimized
|= OPT_EXEC
;
1010 VISIT(st
, expr
, s
->v
.Exec
.globals
);
1011 if (s
->v
.Exec
.locals
)
1012 VISIT(st
, expr
, s
->v
.Exec
.locals
);
1014 st
->st_cur
->ste_unoptimized
|= OPT_BARE_EXEC
;
1019 asdl_seq
*seq
= s
->v
.Global
.names
;
1020 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1021 identifier name
= asdl_seq_GET(seq
, i
);
1022 char *c_name
= PyString_AS_STRING(name
);
1023 long cur
= symtable_lookup(st
, name
);
1026 if (cur
& (DEF_LOCAL
| USE
)) {
1028 if (cur
& DEF_LOCAL
)
1029 PyOS_snprintf(buf
, sizeof(buf
),
1030 GLOBAL_AFTER_ASSIGN
,
1033 PyOS_snprintf(buf
, sizeof(buf
),
1036 if (!symtable_warn(st
, buf
, s
->lineno
))
1039 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1045 VISIT(st
, expr
, s
->v
.Expr
.value
);
1050 /* nothing to do here */
1057 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1061 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1064 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1065 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1068 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1071 if (!symtable_add_def(st
, GET_IDENTIFIER(lambda
), DEF_LOCAL
))
1073 if (e
->v
.Lambda
.args
->defaults
)
1074 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1075 /* XXX how to get line numbers for expressions */
1076 if (!symtable_enter_block(st
, GET_IDENTIFIER(lambda
),
1077 FunctionBlock
, (void *)e
, 0))
1079 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1080 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1081 if (!symtable_exit_block(st
, (void *)e
))
1086 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1087 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1089 case ListComp_kind
: {
1093 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]",
1094 ++st
->st_cur
->ste_tmpname
);
1095 tmp
= PyString_InternFromString(tmpname
);
1096 if (!symtable_add_def(st
, tmp
, DEF_LOCAL
))
1099 VISIT(st
, expr
, e
->v
.ListComp
.elt
);
1100 VISIT_SEQ(st
, comprehension
, e
->v
.ListComp
.generators
);
1103 case GeneratorExp_kind
: {
1104 if (!symtable_visit_genexp(st
, e
)) {
1110 if (e
->v
.Yield
.value
)
1111 VISIT(st
, expr
, e
->v
.Yield
.value
);
1112 st
->st_cur
->ste_generator
= 1;
1115 VISIT(st
, expr
, e
->v
.Compare
.left
);
1116 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1119 VISIT(st
, expr
, e
->v
.Call
.func
);
1120 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1121 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1122 if (e
->v
.Call
.starargs
)
1123 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1124 if (e
->v
.Call
.kwargs
)
1125 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1128 VISIT(st
, expr
, e
->v
.Repr
.value
);
1132 /* Nothing to do here. */
1134 /* The following exprs can be assignment targets. */
1135 case Attribute_kind
:
1136 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1138 case Subscript_kind
:
1139 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1140 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1143 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1144 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1147 /* child nodes of List and Tuple will have expr_context set */
1149 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1152 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1159 symtable_implicit_arg(struct symtable
*st
, int pos
)
1161 PyObject
*id
= PyString_FromFormat(".%d", pos
);
1164 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1173 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int toplevel
)
1177 /* go through all the toplevel arguments first */
1178 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1179 expr_ty arg
= asdl_seq_GET(args
, i
);
1180 if (arg
->kind
== Name_kind
) {
1181 assert(arg
->v
.Name
.ctx
== Param
||
1182 (arg
->v
.Name
.ctx
== Store
&& !toplevel
));
1183 if (!symtable_add_def(st
, arg
->v
.Name
.id
, DEF_PARAM
))
1186 else if (arg
->kind
== Tuple_kind
) {
1187 assert(arg
->v
.Tuple
.ctx
== Store
);
1189 if (!symtable_implicit_arg(st
, i
))
1194 PyErr_SetString(PyExc_SyntaxError
,
1195 "invalid expression in parameter list");
1196 PyErr_SyntaxLocation(st
->st_filename
,
1197 st
->st_cur
->ste_lineno
);
1203 if (!symtable_visit_params_nested(st
, args
))
1211 symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
)
1214 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1215 expr_ty arg
= asdl_seq_GET(args
, i
);
1216 if (arg
->kind
== Tuple_kind
&&
1217 !symtable_visit_params(st
, arg
->v
.Tuple
.elts
, 0))
1225 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1227 /* skip default arguments inside function block
1228 XXX should ast be different?
1230 if (a
->args
&& !symtable_visit_params(st
, a
->args
, 1))
1233 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1235 st
->st_cur
->ste_varargs
= 1;
1238 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1240 st
->st_cur
->ste_varkeywords
= 1;
1242 if (a
->args
&& !symtable_visit_params_nested(st
, a
->args
))
1249 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1252 VISIT(st
, expr
, eh
->type
);
1254 VISIT(st
, expr
, eh
->name
);
1255 VISIT_SEQ(st
, stmt
, eh
->body
);
1261 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1263 /* Compute store_name, the name actually bound by the import
1264 operation. It is diferent than a->name when a->name is a
1265 dotted package name (e.g. spam.eggs)
1267 PyObject
*store_name
;
1268 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1269 const char *base
= PyString_AS_STRING(name
);
1270 char *dot
= strchr(base
, '.');
1272 store_name
= PyString_FromStringAndSize(base
, dot
- base
);
1275 Py_INCREF(store_name
);
1277 if (strcmp(PyString_AS_STRING(name
), "*")) {
1278 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1279 Py_DECREF(store_name
);
1283 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1284 int lineno
= st
->st_cur
->ste_lineno
;
1285 if (!symtable_warn(st
, IMPORT_STAR_WARNING
, lineno
)) {
1286 Py_DECREF(store_name
);
1290 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1291 Py_DECREF(store_name
);
1298 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1300 VISIT(st
, expr
, lc
->target
);
1301 VISIT(st
, expr
, lc
->iter
);
1302 VISIT_SEQ(st
, expr
, lc
->ifs
);
1308 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1310 VISIT(st
, expr
, k
->value
);
1316 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1320 if (s
->v
.Slice
.lower
)
1321 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1322 if (s
->v
.Slice
.upper
)
1323 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1324 if (s
->v
.Slice
.step
)
1325 VISIT(st
, expr
, s
->v
.Slice
.step
)
1328 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1331 VISIT(st
, expr
, s
->v
.Index
.value
)
1340 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1342 comprehension_ty outermost
= ((comprehension_ty
)
1343 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
, 0)));
1344 /* Outermost iterator is evaluated in current scope */
1345 VISIT(st
, expr
, outermost
->iter
);
1346 /* Create generator scope for the rest */
1347 if (!symtable_enter_block(st
, GET_IDENTIFIER(genexpr
),
1348 FunctionBlock
, (void *)e
, 0)) {
1351 st
->st_cur
->ste_generator
= 1;
1352 /* Outermost iter is received as an argument */
1353 if (!symtable_implicit_arg(st
, 0)) {
1354 symtable_exit_block(st
, (void *)e
);
1357 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1358 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1359 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1360 e
->v
.GeneratorExp
.generators
, 1, (void*)e
);
1361 VISIT_IN_BLOCK(st
, expr
, e
->v
.GeneratorExp
.elt
, (void*)e
);
1362 if (!symtable_exit_block(st
, (void *)e
))