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"
16 #define RETURN_VAL_IN_GENERATOR \
17 "'return' with argument inside generator"
20 static PySTEntryObject
*
21 ste_new(struct symtable
*st
, identifier name
, _Py_block_ty block
,
22 void *key
, int lineno
)
24 PySTEntryObject
*ste
= NULL
;
27 k
= PyLong_FromVoidPtr(key
);
30 ste
= PyObject_New(PySTEntryObject
, &PySTEntry_Type
);
40 ste
->ste_symbols
= NULL
;
41 ste
->ste_varnames
= NULL
;
42 ste
->ste_children
= NULL
;
44 ste
->ste_symbols
= PyDict_New();
45 if (ste
->ste_symbols
== NULL
)
48 ste
->ste_varnames
= PyList_New(0);
49 if (ste
->ste_varnames
== NULL
)
52 ste
->ste_children
= PyList_New(0);
53 if (ste
->ste_children
== NULL
)
56 ste
->ste_type
= block
;
57 ste
->ste_unoptimized
= 0;
61 ste
->ste_varkeywords
= 0;
62 ste
->ste_opt_lineno
= 0;
64 ste
->ste_lineno
= lineno
;
66 if (st
->st_cur
!= NULL
&&
67 (st
->st_cur
->ste_nested
||
68 st
->st_cur
->ste_type
== FunctionBlock
))
70 ste
->ste_child_free
= 0;
71 ste
->ste_generator
= 0;
72 ste
->ste_returns_value
= 0;
74 if (PyDict_SetItem(st
->st_symbols
, ste
->ste_id
, (PyObject
*)ste
) < 0)
84 ste_repr(PySTEntryObject
*ste
)
88 PyOS_snprintf(buf
, sizeof(buf
),
89 "<symtable entry %.100s(%ld), line %d>",
90 PyString_AS_STRING(ste
->ste_name
),
91 PyInt_AS_LONG(ste
->ste_id
), ste
->ste_lineno
);
92 return PyString_FromString(buf
);
96 ste_dealloc(PySTEntryObject
*ste
)
98 ste
->ste_table
= NULL
;
99 Py_XDECREF(ste
->ste_id
);
100 Py_XDECREF(ste
->ste_name
);
101 Py_XDECREF(ste
->ste_symbols
);
102 Py_XDECREF(ste
->ste_varnames
);
103 Py_XDECREF(ste
->ste_children
);
107 #define OFF(x) offsetof(PySTEntryObject, x)
109 static PyMemberDef ste_memberlist
[] = {
110 {"id", T_OBJECT
, OFF(ste_id
), READONLY
},
111 {"name", T_OBJECT
, OFF(ste_name
), READONLY
},
112 {"symbols", T_OBJECT
, OFF(ste_symbols
), READONLY
},
113 {"varnames", T_OBJECT
, OFF(ste_varnames
), READONLY
},
114 {"children", T_OBJECT
, OFF(ste_children
), READONLY
},
115 {"optimized",T_INT
, OFF(ste_unoptimized
), READONLY
},
116 {"nested", T_INT
, OFF(ste_nested
), READONLY
},
117 {"type", T_INT
, OFF(ste_type
), READONLY
},
118 {"lineno", T_INT
, OFF(ste_lineno
), READONLY
},
122 PyTypeObject PySTEntry_Type
= {
123 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
125 sizeof(PySTEntryObject
),
127 (destructor
)ste_dealloc
, /* tp_dealloc */
132 (reprfunc
)ste_repr
, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
139 PyObject_GenericGetAttr
, /* tp_getattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT
, /* tp_flags */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
151 ste_memberlist
, /* tp_members */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
163 static int symtable_analyze(struct symtable
*st
);
164 static int symtable_warn(struct symtable
*st
, char *msg
, int lineno
);
165 static int symtable_enter_block(struct symtable
*st
, identifier name
,
166 _Py_block_ty block
, void *ast
, int lineno
);
167 static int symtable_exit_block(struct symtable
*st
, void *ast
);
168 static int symtable_visit_stmt(struct symtable
*st
, stmt_ty s
);
169 static int symtable_visit_expr(struct symtable
*st
, expr_ty s
);
170 static int symtable_visit_genexp(struct symtable
*st
, expr_ty s
);
171 static int symtable_visit_arguments(struct symtable
*st
, arguments_ty
);
172 static int symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty
);
173 static int symtable_visit_alias(struct symtable
*st
, alias_ty
);
174 static int symtable_visit_comprehension(struct symtable
*st
, comprehension_ty
);
175 static int symtable_visit_keyword(struct symtable
*st
, keyword_ty
);
176 static int symtable_visit_slice(struct symtable
*st
, slice_ty
);
177 static int symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int top
);
178 static int symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
);
179 static int symtable_implicit_arg(struct symtable
*st
, int pos
);
182 static identifier top
= NULL
, lambda
= NULL
, genexpr
= NULL
;
184 #define GET_IDENTIFIER(VAR) \
185 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
187 #define DUPLICATE_ARGUMENT \
188 "duplicate argument '%s' in function definition"
190 static struct symtable
*
195 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
199 st
->st_filename
= NULL
;
200 st
->st_symbols
= NULL
;
202 if ((st
->st_stack
= PyList_New(0)) == NULL
)
204 if ((st
->st_symbols
= PyDict_New()) == NULL
)
208 st
->st_private
= NULL
;
216 PySymtable_Build(mod_ty mod
, const char *filename
, PyFutureFeatures
*future
)
218 struct symtable
*st
= symtable_new();
224 st
->st_filename
= filename
;
225 st
->st_future
= future
;
226 if (!GET_IDENTIFIER(top
) ||
227 !symtable_enter_block(st
, top
, ModuleBlock
, (void *)mod
, 0)) {
232 st
->st_top
= st
->st_cur
;
233 st
->st_cur
->ste_unoptimized
= OPT_TOPLEVEL
;
234 /* Any other top-level initialization? */
237 seq
= mod
->v
.Module
.body
;
238 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
239 if (!symtable_visit_stmt(st
,
240 (stmt_ty
)asdl_seq_GET(seq
, i
)))
243 case Expression_kind
:
244 if (!symtable_visit_expr(st
, mod
->v
.Expression
.body
))
247 case Interactive_kind
:
248 seq
= mod
->v
.Interactive
.body
;
249 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
250 if (!symtable_visit_stmt(st
,
251 (stmt_ty
)asdl_seq_GET(seq
, i
)))
255 PyErr_SetString(PyExc_RuntimeError
,
256 "this compiler does not handle Suites");
259 if (!symtable_exit_block(st
, (void *)mod
)) {
263 if (symtable_analyze(st
))
268 (void) symtable_exit_block(st
, (void *)mod
);
274 PySymtable_Free(struct symtable
*st
)
276 Py_XDECREF(st
->st_symbols
);
277 Py_XDECREF(st
->st_stack
);
278 PyMem_Free((void *)st
);
282 PySymtable_Lookup(struct symtable
*st
, void *key
)
286 k
= PyLong_FromVoidPtr(key
);
289 v
= PyDict_GetItem(st
->st_symbols
, k
);
291 assert(PySTEntry_Check(v
));
295 PyErr_SetString(PyExc_KeyError
,
296 "unknown symbol table entry");
300 return (PySTEntryObject
*)v
;
304 PyST_GetScope(PySTEntryObject
*ste
, PyObject
*name
)
306 PyObject
*v
= PyDict_GetItem(ste
->ste_symbols
, name
);
309 assert(PyInt_Check(v
));
310 return (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
314 /* Analyze raw symbol information to determine scope of each name.
316 The next several functions are helpers for PySymtable_Analyze(),
317 which determines whether a name is local, global, or free. In addition,
318 it determines which local variables are cell variables; they provide
319 bindings that are used for free variables in enclosed blocks.
321 There are also two kinds of free variables, implicit and explicit. An
322 explicit global is declared with the global statement. An implicit
323 global is a free variable for which the compiler has found no binding
324 in an enclosing function scope. The implicit global is either a global
325 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
326 to handle these names to implement slightly odd semantics. In such a
327 block, the name is treated as global until it is assigned to; then it
328 is treated as a local.
330 The symbol table requires two passes to determine the scope of each name.
331 The first pass collects raw facts from the AST: the name is a parameter
332 here, the name is used by not defined here, etc. The second pass analyzes
333 these facts during a pass over the PySTEntryObjects created during pass 1.
335 When a function is entered during the second pass, the parent passes
336 the set of all name bindings visible to its children. These bindings
337 are used to determine if the variable is free or an implicit global.
338 After doing the local analysis, it analyzes each of its child blocks
339 using an updated set of name bindings.
341 The children update the free variable set. If a local variable is free
342 in a child, the variable is marked as a cell. The current function must
343 provide runtime storage for the variable that may outlive the function's
344 frame. Cell variables are removed from the free set before the analyze
345 function returns to its parent.
347 The sets of bound and free variables are implemented as dictionaries
348 mapping strings to None.
351 #define SET_SCOPE(DICT, NAME, I) { \
352 PyObject *o = PyInt_FromLong(I); \
355 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
362 /* Decide on scope of name, given flags.
364 The dicts passed in as arguments are modified as necessary.
365 ste is passed so that flags can be updated.
369 analyze_name(PySTEntryObject
*ste
, PyObject
*dict
, PyObject
*name
, long flags
,
370 PyObject
*bound
, PyObject
*local
, PyObject
*free
,
373 if (flags
& DEF_GLOBAL
) {
374 if (flags
& DEF_PARAM
) {
375 PyErr_Format(PyExc_SyntaxError
,
376 "name '%s' is local and global",
377 PyString_AS_STRING(name
));
378 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
383 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
384 if (PyDict_SetItem(global
, name
, Py_None
) < 0)
386 if (bound
&& PyDict_GetItem(bound
, name
)) {
387 if (PyDict_DelItem(bound
, name
) < 0)
392 if (flags
& DEF_BOUND
) {
393 SET_SCOPE(dict
, name
, LOCAL
);
394 if (PyDict_SetItem(local
, name
, Py_None
) < 0)
396 if (PyDict_GetItem(global
, name
)) {
397 if (PyDict_DelItem(global
, name
) < 0)
402 /* If an enclosing block has a binding for this name, it
403 is a free variable rather than a global variable.
404 Note that having a non-NULL bound implies that the block
407 if (bound
&& PyDict_GetItem(bound
, name
)) {
408 SET_SCOPE(dict
, name
, FREE
);
410 if (PyDict_SetItem(free
, name
, Py_None
) < 0)
414 /* If a parent has a global statement, then call it global
415 explicit? It could also be global implicit.
417 else if (global
&& PyDict_GetItem(global
, name
)) {
418 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
424 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
427 return 0; /* Can't get here */
432 /* If a name is defined in free and also in locals, then this block
433 provides the binding for the free variable. The name should be
434 marked CELL in this block and removed from the free list.
436 Note that the current block's free variables are included in free.
437 That's safe because no name can be free and local in the same scope.
441 analyze_cells(PyObject
*scope
, PyObject
*free
)
443 PyObject
*name
, *v
, *w
;
447 w
= PyInt_FromLong(CELL
);
450 while (PyDict_Next(scope
, &pos
, &name
, &v
)) {
452 assert(PyInt_Check(v
));
453 flags
= PyInt_AS_LONG(v
);
456 if (!PyDict_GetItem(free
, name
))
458 /* Replace LOCAL with CELL for this name, and remove
459 from free. It is safe to replace the value of name
460 in the dict, because it will not cause a resize.
462 if (PyDict_SetItem(scope
, name
, w
) < 0)
464 if (!PyDict_DelItem(free
, name
) < 0)
473 /* Check for illegal statements in unoptimized namespaces */
475 check_unoptimized(const PySTEntryObject
* ste
) {
479 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
480 || !(ste
->ste_free
|| ste
->ste_child_free
))
483 trailer
= (ste
->ste_child_free
?
484 "contains a nested function with free variables" :
485 "is a nested function");
487 switch (ste
->ste_unoptimized
) {
488 case OPT_TOPLEVEL
: /* exec / import * at top-level is fine */
489 case OPT_EXEC
: /* qualified exec is fine */
491 case OPT_IMPORT_STAR
:
492 PyOS_snprintf(buf
, sizeof(buf
),
493 "import * is not allowed in function '%.100s' "
495 PyString_AS_STRING(ste
->ste_name
), trailer
);
498 PyOS_snprintf(buf
, sizeof(buf
),
499 "unqualified exec is not allowed in function "
501 PyString_AS_STRING(ste
->ste_name
), trailer
);
504 PyOS_snprintf(buf
, sizeof(buf
),
505 "function '%.100s' uses import * and bare exec, "
506 "which are illegal because it %s",
507 PyString_AS_STRING(ste
->ste_name
), trailer
);
511 PyErr_SetString(PyExc_SyntaxError
, buf
);
512 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
513 ste
->ste_opt_lineno
);
517 /* Enter the final scope information into the st_symbols dict.
519 * All arguments are dicts. Modifies symbols, others are read-only.
522 update_symbols(PyObject
*symbols
, PyObject
*scope
,
523 PyObject
*bound
, PyObject
*free
, int classflag
)
525 PyObject
*name
, *v
, *u
, *w
, *free_value
= NULL
;
528 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
530 assert(PyInt_Check(v
));
531 flags
= PyInt_AS_LONG(v
);
532 w
= PyDict_GetItem(scope
, name
);
533 assert(w
&& PyInt_Check(w
));
534 i
= PyInt_AS_LONG(w
);
535 flags
|= (i
<< SCOPE_OFF
);
536 u
= PyInt_FromLong(flags
);
539 if (PyDict_SetItem(symbols
, name
, u
) < 0) {
546 free_value
= PyInt_FromLong(FREE
<< SCOPE_OFF
);
550 /* add a free variable when it's only use is for creating a closure */
552 while (PyDict_Next(free
, &pos
, &name
, &v
)) {
553 PyObject
*o
= PyDict_GetItem(symbols
, name
);
556 /* It could be a free variable in a method of
557 the class that has the same name as a local
558 or global in the class scope.
561 PyInt_AS_LONG(o
) & (DEF_BOUND
| DEF_GLOBAL
)) {
562 long i
= PyInt_AS_LONG(o
) | DEF_FREE_CLASS
;
563 o
= PyInt_FromLong(i
);
565 Py_DECREF(free_value
);
568 if (PyDict_SetItem(symbols
, name
, o
) < 0) {
570 Py_DECREF(free_value
);
575 /* else it's not free, probably a cell */
578 if (!PyDict_GetItem(bound
, name
))
579 continue; /* it's a global */
581 if (PyDict_SetItem(symbols
, name
, free_value
) < 0) {
582 Py_DECREF(free_value
);
586 Py_DECREF(free_value
);
590 /* Make final symbol table decisions for block of ste.
592 ste -- current symtable entry (input/output)
593 bound -- set of variables bound in enclosing scopes (input)
594 free -- set of free variables in enclosed scopes (output)
595 globals -- set of declared global variables in enclosing scopes (input)
599 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
602 PyObject
*name
, *v
, *local
= NULL
, *scope
= NULL
, *newbound
= NULL
;
603 PyObject
*newglobal
= NULL
, *newfree
= NULL
;
607 local
= PyDict_New();
610 scope
= PyDict_New();
613 newglobal
= PyDict_New();
616 newfree
= PyDict_New();
619 newbound
= PyDict_New();
623 if (ste
->ste_type
== ClassBlock
) {
624 /* make a copy of globals before calling analyze_name(),
625 because global statements in the class have no effect
628 if (PyDict_Update(newglobal
, global
) < 0)
631 if (PyDict_Update(newbound
, bound
) < 0)
635 assert(PySTEntry_Check(ste
));
636 assert(PyDict_Check(ste
->ste_symbols
));
637 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
638 long flags
= PyInt_AS_LONG(v
);
639 if (!analyze_name(ste
, scope
, name
, flags
, bound
, local
, free
,
644 if (ste
->ste_type
!= ClassBlock
) {
645 if (ste
->ste_type
== FunctionBlock
) {
646 if (PyDict_Update(newbound
, local
) < 0)
650 if (PyDict_Update(newbound
, bound
) < 0)
653 if (PyDict_Update(newglobal
, global
) < 0)
657 /* Recursively call analyze_block() on each child block */
658 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
659 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
660 PySTEntryObject
* entry
;
661 assert(c
&& PySTEntry_Check(c
));
662 entry
= (PySTEntryObject
*)c
;
663 if (!analyze_block(entry
, newbound
, newfree
, newglobal
))
665 if (entry
->ste_free
|| entry
->ste_child_free
)
666 ste
->ste_child_free
= 1;
669 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scope
, newfree
))
671 if (!update_symbols(ste
->ste_symbols
, scope
, bound
, newfree
,
672 ste
->ste_type
== ClassBlock
))
674 if (!check_unoptimized(ste
))
677 if (PyDict_Update(free
, newfree
) < 0)
683 Py_XDECREF(newbound
);
684 Py_XDECREF(newglobal
);
687 assert(PyErr_Occurred());
692 symtable_analyze(struct symtable
*st
)
694 PyObject
*free
, *global
;
700 global
= PyDict_New();
705 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
713 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
715 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
716 lineno
, NULL
, NULL
) < 0) {
717 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
718 PyErr_SetString(PyExc_SyntaxError
, msg
);
719 PyErr_SyntaxLocation(st
->st_filename
,
720 st
->st_cur
->ste_lineno
);
727 /* symtable_enter_block() gets a reference via ste_new.
728 This reference is released when the block is exited, via the DECREF
729 in symtable_exit_block().
733 symtable_exit_block(struct symtable
*st
, void *ast
)
737 Py_CLEAR(st
->st_cur
);
738 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
740 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
742 if (st
->st_cur
== NULL
)
744 Py_INCREF(st
->st_cur
);
745 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
752 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
753 void *ast
, int lineno
)
755 PySTEntryObject
*prev
= NULL
;
759 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
762 Py_DECREF(st
->st_cur
);
764 st
->st_cur
= ste_new(st
, name
, block
, ast
, lineno
);
765 if (st
->st_cur
== NULL
)
767 if (name
== GET_IDENTIFIER(top
))
768 st
->st_global
= st
->st_cur
->ste_symbols
;
770 if (PyList_Append(prev
->ste_children
,
771 (PyObject
*)st
->st_cur
) < 0) {
779 symtable_lookup(struct symtable
*st
, PyObject
*name
)
782 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
785 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
789 return PyInt_AsLong(o
);
793 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
798 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
802 dict
= st
->st_cur
->ste_symbols
;
803 if ((o
= PyDict_GetItem(dict
, mangled
))) {
804 val
= PyInt_AS_LONG(o
);
805 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
806 /* Is it better to use 'mangled' or 'name' here? */
807 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
808 PyString_AsString(name
));
809 PyErr_SyntaxLocation(st
->st_filename
,
810 st
->st_cur
->ste_lineno
);
816 o
= PyInt_FromLong(val
);
819 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
825 if (flag
& DEF_PARAM
) {
826 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
828 } else if (flag
& DEF_GLOBAL
) {
829 /* XXX need to update DEF_GLOBAL for other flags too;
830 perhaps only DEF_FREE_GLOBAL */
832 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
833 val
|= PyInt_AS_LONG(o
);
835 o
= PyInt_FromLong(val
);
838 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
852 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
853 They use the ASDL name to synthesize the name of the C type and the visit
856 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
857 useful if the first node in the sequence requires special treatment.
860 #define VISIT(ST, TYPE, V) \
861 if (!symtable_visit_ ## TYPE((ST), (V))) \
864 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
865 if (!symtable_visit_ ## TYPE((ST), (V))) { \
866 symtable_exit_block((ST), (S)); \
870 #define VISIT_SEQ(ST, TYPE, SEQ) { \
872 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
873 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
874 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
875 if (!symtable_visit_ ## TYPE((ST), elt)) \
880 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
882 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
883 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
884 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
885 if (!symtable_visit_ ## TYPE((ST), elt)) { \
886 symtable_exit_block((ST), (S)); \
892 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
894 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
895 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
896 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
897 if (!symtable_visit_ ## TYPE((ST), elt)) \
902 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
904 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
905 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
906 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
907 if (!symtable_visit_ ## TYPE((ST), elt)) { \
908 symtable_exit_block((ST), (S)); \
915 symtable_new_tmpname(struct symtable
*st
)
920 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]",
921 ++st
->st_cur
->ste_tmpname
);
922 tmp
= PyString_InternFromString(tmpname
);
925 if (!symtable_add_def(st
, tmp
, DEF_LOCAL
))
932 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
935 case FunctionDef_kind
:
936 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
938 if (s
->v
.FunctionDef
.args
->defaults
)
939 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
940 if (s
->v
.FunctionDef
.decorator_list
)
941 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorator_list
);
942 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
943 FunctionBlock
, (void *)s
, s
->lineno
))
945 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
946 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
947 if (!symtable_exit_block(st
, s
))
950 case ClassDef_kind
: {
952 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
954 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
955 if (s
->v
.ClassDef
.decorator_list
)
956 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.decorator_list
);
957 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
958 (void *)s
, s
->lineno
))
960 tmp
= st
->st_private
;
961 st
->st_private
= s
->v
.ClassDef
.name
;
962 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
963 st
->st_private
= tmp
;
964 if (!symtable_exit_block(st
, s
))
969 if (s
->v
.Return
.value
) {
970 VISIT(st
, expr
, s
->v
.Return
.value
);
971 st
->st_cur
->ste_returns_value
= 1;
972 if (st
->st_cur
->ste_generator
) {
973 PyErr_SetString(PyExc_SyntaxError
,
974 RETURN_VAL_IN_GENERATOR
);
975 PyErr_SyntaxLocation(st
->st_filename
,
982 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
985 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
986 VISIT(st
, expr
, s
->v
.Assign
.value
);
989 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
990 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
994 VISIT(st
, expr
, s
->v
.Print
.dest
);
995 VISIT_SEQ(st
, expr
, s
->v
.Print
.values
);
998 VISIT(st
, expr
, s
->v
.For
.target
);
999 VISIT(st
, expr
, s
->v
.For
.iter
);
1000 VISIT_SEQ(st
, stmt
, s
->v
.For
.body
);
1001 if (s
->v
.For
.orelse
)
1002 VISIT_SEQ(st
, stmt
, s
->v
.For
.orelse
);
1005 VISIT(st
, expr
, s
->v
.While
.test
);
1006 VISIT_SEQ(st
, stmt
, s
->v
.While
.body
);
1007 if (s
->v
.While
.orelse
)
1008 VISIT_SEQ(st
, stmt
, s
->v
.While
.orelse
);
1011 /* XXX if 0: and lookup_yield() hacks */
1012 VISIT(st
, expr
, s
->v
.If
.test
);
1013 VISIT_SEQ(st
, stmt
, s
->v
.If
.body
);
1015 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
1018 if (s
->v
.Raise
.type
) {
1019 VISIT(st
, expr
, s
->v
.Raise
.type
);
1020 if (s
->v
.Raise
.inst
) {
1021 VISIT(st
, expr
, s
->v
.Raise
.inst
);
1022 if (s
->v
.Raise
.tback
)
1023 VISIT(st
, expr
, s
->v
.Raise
.tback
);
1027 case TryExcept_kind
:
1028 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
1029 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
1030 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
1032 case TryFinally_kind
:
1033 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
1034 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
1037 VISIT(st
, expr
, s
->v
.Assert
.test
);
1038 if (s
->v
.Assert
.msg
)
1039 VISIT(st
, expr
, s
->v
.Assert
.msg
);
1042 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
1043 /* XXX Don't have the lineno available inside
1045 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1046 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1048 case ImportFrom_kind
:
1049 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
1050 /* XXX Don't have the lineno available inside
1052 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1053 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1056 VISIT(st
, expr
, s
->v
.Exec
.body
);
1057 if (!st
->st_cur
->ste_opt_lineno
)
1058 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1059 if (s
->v
.Exec
.globals
) {
1060 st
->st_cur
->ste_unoptimized
|= OPT_EXEC
;
1061 VISIT(st
, expr
, s
->v
.Exec
.globals
);
1062 if (s
->v
.Exec
.locals
)
1063 VISIT(st
, expr
, s
->v
.Exec
.locals
);
1065 st
->st_cur
->ste_unoptimized
|= OPT_BARE_EXEC
;
1070 asdl_seq
*seq
= s
->v
.Global
.names
;
1071 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1072 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1073 char *c_name
= PyString_AS_STRING(name
);
1074 long cur
= symtable_lookup(st
, name
);
1077 if (cur
& (DEF_LOCAL
| USE
)) {
1079 if (cur
& DEF_LOCAL
)
1080 PyOS_snprintf(buf
, sizeof(buf
),
1081 GLOBAL_AFTER_ASSIGN
,
1084 PyOS_snprintf(buf
, sizeof(buf
),
1087 if (!symtable_warn(st
, buf
, s
->lineno
))
1090 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1096 VISIT(st
, expr
, s
->v
.Expr
.value
);
1101 /* nothing to do here */
1104 if (!symtable_new_tmpname(st
))
1106 VISIT(st
, expr
, s
->v
.With
.context_expr
);
1107 if (s
->v
.With
.optional_vars
) {
1108 if (!symtable_new_tmpname(st
))
1110 VISIT(st
, expr
, s
->v
.With
.optional_vars
);
1112 VISIT_SEQ(st
, stmt
, s
->v
.With
.body
);
1119 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1123 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1126 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1127 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1130 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1133 if (!GET_IDENTIFIER(lambda
) ||
1134 !symtable_add_def(st
, lambda
, DEF_LOCAL
))
1136 if (e
->v
.Lambda
.args
->defaults
)
1137 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1138 /* XXX how to get line numbers for expressions */
1139 if (!symtable_enter_block(st
, lambda
,
1140 FunctionBlock
, (void *)e
, 0))
1142 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1143 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1144 if (!symtable_exit_block(st
, (void *)e
))
1149 VISIT(st
, expr
, e
->v
.IfExp
.test
);
1150 VISIT(st
, expr
, e
->v
.IfExp
.body
);
1151 VISIT(st
, expr
, e
->v
.IfExp
.orelse
);
1154 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1155 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1158 if (!symtable_new_tmpname(st
))
1160 VISIT(st
, expr
, e
->v
.ListComp
.elt
);
1161 VISIT_SEQ(st
, comprehension
, e
->v
.ListComp
.generators
);
1163 case GeneratorExp_kind
:
1164 if (!symtable_visit_genexp(st
, e
))
1168 if (e
->v
.Yield
.value
)
1169 VISIT(st
, expr
, e
->v
.Yield
.value
);
1170 st
->st_cur
->ste_generator
= 1;
1171 if (st
->st_cur
->ste_returns_value
) {
1172 PyErr_SetString(PyExc_SyntaxError
,
1173 RETURN_VAL_IN_GENERATOR
);
1174 PyErr_SyntaxLocation(st
->st_filename
,
1180 VISIT(st
, expr
, e
->v
.Compare
.left
);
1181 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1184 VISIT(st
, expr
, e
->v
.Call
.func
);
1185 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1186 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1187 if (e
->v
.Call
.starargs
)
1188 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1189 if (e
->v
.Call
.kwargs
)
1190 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1193 VISIT(st
, expr
, e
->v
.Repr
.value
);
1197 /* Nothing to do here. */
1199 /* The following exprs can be assignment targets. */
1200 case Attribute_kind
:
1201 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1203 case Subscript_kind
:
1204 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1205 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1208 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1209 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1212 /* child nodes of List and Tuple will have expr_context set */
1214 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1217 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1224 symtable_implicit_arg(struct symtable
*st
, int pos
)
1226 PyObject
*id
= PyString_FromFormat(".%d", pos
);
1229 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1238 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int toplevel
)
1242 /* go through all the toplevel arguments first */
1243 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1244 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1245 if (arg
->kind
== Name_kind
) {
1246 assert(arg
->v
.Name
.ctx
== Param
||
1247 (arg
->v
.Name
.ctx
== Store
&& !toplevel
));
1248 if (!symtable_add_def(st
, arg
->v
.Name
.id
, DEF_PARAM
))
1251 else if (arg
->kind
== Tuple_kind
) {
1252 assert(arg
->v
.Tuple
.ctx
== Store
);
1254 if (!symtable_implicit_arg(st
, i
))
1259 PyErr_SetString(PyExc_SyntaxError
,
1260 "invalid expression in parameter list");
1261 PyErr_SyntaxLocation(st
->st_filename
,
1262 st
->st_cur
->ste_lineno
);
1268 if (!symtable_visit_params_nested(st
, args
))
1276 symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
)
1279 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1280 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1281 if (arg
->kind
== Tuple_kind
&&
1282 !symtable_visit_params(st
, arg
->v
.Tuple
.elts
, 0))
1290 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1292 /* skip default arguments inside function block
1293 XXX should ast be different?
1295 if (a
->args
&& !symtable_visit_params(st
, a
->args
, 1))
1298 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1300 st
->st_cur
->ste_varargs
= 1;
1303 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1305 st
->st_cur
->ste_varkeywords
= 1;
1307 if (a
->args
&& !symtable_visit_params_nested(st
, a
->args
))
1314 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1316 if (eh
->v
.ExceptHandler
.type
)
1317 VISIT(st
, expr
, eh
->v
.ExceptHandler
.type
);
1318 if (eh
->v
.ExceptHandler
.name
)
1319 VISIT(st
, expr
, eh
->v
.ExceptHandler
.name
);
1320 VISIT_SEQ(st
, stmt
, eh
->v
.ExceptHandler
.body
);
1326 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1328 /* Compute store_name, the name actually bound by the import
1329 operation. It is diferent than a->name when a->name is a
1330 dotted package name (e.g. spam.eggs)
1332 PyObject
*store_name
;
1333 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1334 const char *base
= PyString_AS_STRING(name
);
1335 char *dot
= strchr(base
, '.');
1337 store_name
= PyString_FromStringAndSize(base
, dot
- base
);
1343 Py_INCREF(store_name
);
1345 if (strcmp(PyString_AS_STRING(name
), "*")) {
1346 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1347 Py_DECREF(store_name
);
1351 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1352 int lineno
= st
->st_cur
->ste_lineno
;
1353 if (!symtable_warn(st
, IMPORT_STAR_WARNING
, lineno
)) {
1354 Py_DECREF(store_name
);
1358 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1359 Py_DECREF(store_name
);
1366 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1368 VISIT(st
, expr
, lc
->target
);
1369 VISIT(st
, expr
, lc
->iter
);
1370 VISIT_SEQ(st
, expr
, lc
->ifs
);
1376 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1378 VISIT(st
, expr
, k
->value
);
1384 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1388 if (s
->v
.Slice
.lower
)
1389 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1390 if (s
->v
.Slice
.upper
)
1391 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1392 if (s
->v
.Slice
.step
)
1393 VISIT(st
, expr
, s
->v
.Slice
.step
)
1396 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1399 VISIT(st
, expr
, s
->v
.Index
.value
)
1408 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1410 comprehension_ty outermost
= ((comprehension_ty
)
1411 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
, 0)));
1412 /* Outermost iterator is evaluated in current scope */
1413 VISIT(st
, expr
, outermost
->iter
);
1414 /* Create generator scope for the rest */
1415 if (!GET_IDENTIFIER(genexpr
) ||
1416 !symtable_enter_block(st
, genexpr
, FunctionBlock
, (void *)e
, 0)) {
1419 st
->st_cur
->ste_generator
= 1;
1420 /* Outermost iter is received as an argument */
1421 if (!symtable_implicit_arg(st
, 0)) {
1422 symtable_exit_block(st
, (void *)e
);
1425 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1426 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1427 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1428 e
->v
.GeneratorExp
.generators
, 1, (void*)e
);
1429 VISIT_IN_BLOCK(st
, expr
, e
->v
.GeneratorExp
.elt
, (void*)e
);
1430 return symtable_exit_block(st
, (void *)e
);