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"
19 /* XXX(nnorwitz): change name since static? */
20 static PySTEntryObject
*
21 PySTEntry_New(struct symtable
*st
, identifier name
, _Py_block_ty block
,
22 void *key
, int lineno
)
24 PySTEntryObject
*ste
= NULL
;
27 k
= PyLong_FromVoidPtr(key
);
30 ste
= (PySTEntryObject
*)PyObject_New(PySTEntryObject
,
39 ste
->ste_symbols
= NULL
;
40 ste
->ste_varnames
= NULL
;
41 ste
->ste_children
= NULL
;
43 ste
->ste_symbols
= PyDict_New();
44 if (ste
->ste_symbols
== NULL
)
47 ste
->ste_varnames
= PyList_New(0);
48 if (ste
->ste_varnames
== NULL
)
51 ste
->ste_children
= PyList_New(0);
52 if (ste
->ste_children
== NULL
)
55 ste
->ste_type
= block
;
56 ste
->ste_unoptimized
= 0;
60 ste
->ste_varkeywords
= 0;
61 ste
->ste_opt_lineno
= 0;
63 ste
->ste_lineno
= lineno
;
65 if (st
->st_cur
!= NULL
&&
66 (st
->st_cur
->ste_nested
||
67 st
->st_cur
->ste_type
== FunctionBlock
))
69 ste
->ste_child_free
= 0;
70 ste
->ste_generator
= 0;
71 ste
->ste_returns_value
= 0;
73 if (PyDict_SetItem(st
->st_symbols
, ste
->ste_id
, (PyObject
*)ste
) < 0)
83 ste_repr(PySTEntryObject
*ste
)
87 PyOS_snprintf(buf
, sizeof(buf
),
88 "<symtable entry %.100s(%ld), line %d>",
89 PyString_AS_STRING(ste
->ste_name
),
90 PyInt_AS_LONG(ste
->ste_id
), ste
->ste_lineno
);
91 return PyString_FromString(buf
);
95 ste_dealloc(PySTEntryObject
*ste
)
97 ste
->ste_table
= NULL
;
98 Py_XDECREF(ste
->ste_id
);
99 Py_XDECREF(ste
->ste_name
);
100 Py_XDECREF(ste
->ste_symbols
);
101 Py_XDECREF(ste
->ste_varnames
);
102 Py_XDECREF(ste
->ste_children
);
106 #define OFF(x) offsetof(PySTEntryObject, x)
108 static PyMemberDef ste_memberlist
[] = {
109 {"id", T_OBJECT
, OFF(ste_id
), READONLY
},
110 {"name", T_OBJECT
, OFF(ste_name
), READONLY
},
111 {"symbols", T_OBJECT
, OFF(ste_symbols
), READONLY
},
112 {"varnames", T_OBJECT
, OFF(ste_varnames
), READONLY
},
113 {"children", T_OBJECT
, OFF(ste_children
), READONLY
},
114 {"type", T_INT
, OFF(ste_type
), READONLY
},
115 {"lineno", T_INT
, OFF(ste_lineno
), READONLY
},
119 PyTypeObject PySTEntry_Type
= {
120 PyObject_HEAD_INIT(&PyType_Type
)
123 sizeof(PySTEntryObject
),
125 (destructor
)ste_dealloc
, /* tp_dealloc */
130 (reprfunc
)ste_repr
, /* tp_repr */
131 0, /* tp_as_number */
132 0, /* tp_as_sequence */
133 0, /* tp_as_mapping */
137 PyObject_GenericGetAttr
, /* tp_getattro */
139 0, /* tp_as_buffer */
140 Py_TPFLAGS_DEFAULT
, /* tp_flags */
144 0, /* tp_richcompare */
145 0, /* tp_weaklistoffset */
149 ste_memberlist
, /* tp_members */
153 0, /* tp_descr_get */
154 0, /* tp_descr_set */
155 0, /* tp_dictoffset */
161 static int symtable_analyze(struct symtable
*st
);
162 static int symtable_warn(struct symtable
*st
, char *msg
, int lineno
);
163 static int symtable_enter_block(struct symtable
*st
, identifier name
,
164 _Py_block_ty block
, void *ast
, int lineno
);
165 static int symtable_exit_block(struct symtable
*st
, void *ast
);
166 static int symtable_visit_stmt(struct symtable
*st
, stmt_ty s
);
167 static int symtable_visit_expr(struct symtable
*st
, expr_ty s
);
168 static int symtable_visit_genexp(struct symtable
*st
, expr_ty s
);
169 static int symtable_visit_arguments(struct symtable
*st
, arguments_ty
);
170 static int symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty
);
171 static int symtable_visit_alias(struct symtable
*st
, alias_ty
);
172 static int symtable_visit_comprehension(struct symtable
*st
, comprehension_ty
);
173 static int symtable_visit_keyword(struct symtable
*st
, keyword_ty
);
174 static int symtable_visit_slice(struct symtable
*st
, slice_ty
);
175 static int symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int top
);
176 static int symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
);
177 static int symtable_implicit_arg(struct symtable
*st
, int pos
);
180 static identifier top
= NULL
, lambda
= NULL
, genexpr
= NULL
;
182 #define GET_IDENTIFIER(VAR) \
183 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
185 #define DUPLICATE_ARGUMENT \
186 "duplicate argument '%s' in function definition"
188 static struct symtable
*
193 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
197 st
->st_filename
= NULL
;
198 st
->st_symbols
= NULL
;
200 if ((st
->st_stack
= PyList_New(0)) == NULL
)
202 if ((st
->st_symbols
= PyDict_New()) == NULL
)
206 st
->st_private
= NULL
;
214 PySymtable_Build(mod_ty mod
, const char *filename
, PyFutureFeatures
*future
)
216 struct symtable
*st
= symtable_new();
222 st
->st_filename
= filename
;
223 st
->st_future
= future
;
224 if (!symtable_enter_block(st
, GET_IDENTIFIER(top
), ModuleBlock
,
230 st
->st_top
= st
->st_cur
;
231 st
->st_cur
->ste_unoptimized
= OPT_TOPLEVEL
;
232 /* Any other top-level initialization? */
235 seq
= mod
->v
.Module
.body
;
236 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
237 if (!symtable_visit_stmt(st
,
238 (stmt_ty
)asdl_seq_GET(seq
, i
)))
241 case Expression_kind
:
242 if (!symtable_visit_expr(st
, mod
->v
.Expression
.body
))
245 case Interactive_kind
:
246 seq
= mod
->v
.Interactive
.body
;
247 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
248 if (!symtable_visit_stmt(st
,
249 (stmt_ty
)asdl_seq_GET(seq
, i
)))
253 PyErr_SetString(PyExc_RuntimeError
,
254 "this compiler does not handle Suites");
257 if (!symtable_exit_block(st
, (void *)mod
)) {
261 if (symtable_analyze(st
))
266 (void) symtable_exit_block(st
, (void *)mod
);
272 PySymtable_Free(struct symtable
*st
)
274 Py_XDECREF(st
->st_symbols
);
275 Py_XDECREF(st
->st_stack
);
276 PyMem_Free((void *)st
);
280 PySymtable_Lookup(struct symtable
*st
, void *key
)
284 k
= PyLong_FromVoidPtr(key
);
287 v
= PyDict_GetItem(st
->st_symbols
, k
);
289 assert(PySTEntry_Check(v
));
293 PyErr_SetString(PyExc_KeyError
,
294 "unknown symbol table entry");
298 return (PySTEntryObject
*)v
;
302 PyST_GetScope(PySTEntryObject
*ste
, PyObject
*name
)
304 PyObject
*v
= PyDict_GetItem(ste
->ste_symbols
, name
);
307 assert(PyInt_Check(v
));
308 return (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
312 /* Analyze raw symbol information to determine scope of each name.
314 The next several functions are helpers for PySymtable_Analyze(),
315 which determines whether a name is local, global, or free. In addition,
316 it determines which local variables are cell variables; they provide
317 bindings that are used for free variables in enclosed blocks.
319 There are also two kinds of free variables, implicit and explicit. An
320 explicit global is declared with the global statement. An implicit
321 global is a free variable for which the compiler has found no binding
322 in an enclosing function scope. The implicit global is either a global
323 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
324 to handle these names to implement slightly odd semantics. In such a
325 block, the name is treated as global until it is assigned to; then it
326 is treated as a local.
328 The symbol table requires two passes to determine the scope of each name.
329 The first pass collects raw facts from the AST: the name is a parameter
330 here, the name is used by not defined here, etc. The second pass analyzes
331 these facts during a pass over the PySTEntryObjects created during pass 1.
333 When a function is entered during the second pass, the parent passes
334 the set of all name bindings visible to its children. These bindings
335 are used to determine if the variable is free or an implicit global.
336 After doing the local analysis, it analyzes each of its child blocks
337 using an updated set of name bindings.
339 The children update the free variable set. If a local variable is free
340 in a child, the variable is marked as a cell. The current function must
341 provide runtime storage for the variable that may outlive the function's
342 frame. Cell variables are removed from the free set before the analyze
343 function returns to its parent.
345 The sets of bound and free variables are implemented as dictionaries
346 mapping strings to None.
349 #define SET_SCOPE(DICT, NAME, I) { \
350 PyObject *o = PyInt_FromLong(I); \
353 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
360 /* Decide on scope of name, given flags.
362 The dicts passed in as arguments are modified as necessary.
363 ste is passed so that flags can be updated.
367 analyze_name(PySTEntryObject
*ste
, PyObject
*dict
, PyObject
*name
, long flags
,
368 PyObject
*bound
, PyObject
*local
, PyObject
*free
,
371 if (flags
& DEF_GLOBAL
) {
372 if (flags
& DEF_PARAM
) {
373 PyErr_Format(PyExc_SyntaxError
,
374 "name '%s' is local and global",
375 PyString_AS_STRING(name
));
378 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
379 if (PyDict_SetItem(global
, name
, Py_None
) < 0)
381 if (bound
&& PyDict_GetItem(bound
, name
)) {
382 if (PyDict_DelItem(bound
, name
) < 0)
387 if (flags
& DEF_BOUND
) {
388 SET_SCOPE(dict
, name
, LOCAL
);
389 if (PyDict_SetItem(local
, name
, Py_None
) < 0)
391 if (PyDict_GetItem(global
, name
)) {
392 if (PyDict_DelItem(global
, name
) < 0)
397 /* If an enclosing block has a binding for this name, it
398 is a free variable rather than a global variable.
399 Note that having a non-NULL bound implies that the block
402 if (bound
&& PyDict_GetItem(bound
, name
)) {
403 SET_SCOPE(dict
, name
, FREE
);
405 if (PyDict_SetItem(free
, name
, Py_None
) < 0)
409 /* If a parent has a global statement, then call it global
410 explicit? It could also be global implicit.
412 else if (global
&& PyDict_GetItem(global
, name
)) {
413 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
419 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
422 return 0; /* Can't get here */
427 /* If a name is defined in free and also in locals, then this block
428 provides the binding for the free variable. The name should be
429 marked CELL in this block and removed from the free list.
431 Note that the current block's free variables are included in free.
432 That's safe because no name can be free and local in the same scope.
436 analyze_cells(PyObject
*scope
, PyObject
*free
)
438 PyObject
*name
, *v
, *w
;
442 w
= PyInt_FromLong(CELL
);
445 while (PyDict_Next(scope
, &pos
, &name
, &v
)) {
447 assert(PyInt_Check(v
));
448 flags
= PyInt_AS_LONG(v
);
451 if (!PyDict_GetItem(free
, name
))
453 /* Replace LOCAL with CELL for this name, and remove
454 from free. It is safe to replace the value of name
455 in the dict, because it will not cause a resize.
457 if (PyDict_SetItem(scope
, name
, w
) < 0)
459 if (!PyDict_DelItem(free
, name
) < 0)
468 /* Check for illegal statements in unoptimized namespaces */
470 check_unoptimized(const PySTEntryObject
* ste
) {
474 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
475 || !(ste
->ste_free
|| ste
->ste_child_free
))
478 trailer
= (ste
->ste_child_free
?
479 "contains a nested function with free variables" :
480 "is a nested function");
482 switch (ste
->ste_unoptimized
) {
483 case OPT_TOPLEVEL
: /* exec / import * at top-level is fine */
484 case OPT_EXEC
: /* qualified exec is fine */
486 case OPT_IMPORT_STAR
:
487 PyOS_snprintf(buf
, sizeof(buf
),
488 "import * is not allowed in function '%.100s' "
490 PyString_AS_STRING(ste
->ste_name
), trailer
);
493 PyOS_snprintf(buf
, sizeof(buf
),
494 "unqualified exec is not allowed in function "
496 PyString_AS_STRING(ste
->ste_name
), trailer
);
499 PyOS_snprintf(buf
, sizeof(buf
),
500 "function '%.100s' uses import * and bare exec, "
501 "which are illegal because it %s",
502 PyString_AS_STRING(ste
->ste_name
), trailer
);
506 PyErr_SetString(PyExc_SyntaxError
, buf
);
507 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
508 ste
->ste_opt_lineno
);
512 /* Enter the final scope information into the st_symbols dict.
514 * All arguments are dicts. Modifies symbols, others are read-only.
517 update_symbols(PyObject
*symbols
, PyObject
*scope
,
518 PyObject
*bound
, PyObject
*free
, int classflag
)
520 PyObject
*name
, *v
, *u
, *w
, *free_value
= NULL
;
523 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
525 assert(PyInt_Check(v
));
526 flags
= PyInt_AS_LONG(v
);
527 w
= PyDict_GetItem(scope
, name
);
528 assert(w
&& PyInt_Check(w
));
529 i
= PyInt_AS_LONG(w
);
530 flags
|= (i
<< SCOPE_OFF
);
531 u
= PyInt_FromLong(flags
);
534 if (PyDict_SetItem(symbols
, name
, u
) < 0) {
541 free_value
= PyInt_FromLong(FREE
<< SCOPE_OFF
);
545 /* add a free variable when it's only use is for creating a closure */
547 while (PyDict_Next(free
, &pos
, &name
, &v
)) {
548 PyObject
*o
= PyDict_GetItem(symbols
, name
);
551 /* It could be a free variable in a method of
552 the class that has the same name as a local
553 or global in the class scope.
556 PyInt_AS_LONG(o
) & (DEF_BOUND
| DEF_GLOBAL
)) {
557 long i
= PyInt_AS_LONG(o
) | DEF_FREE_CLASS
;
558 o
= PyInt_FromLong(i
);
560 Py_DECREF(free_value
);
563 if (PyDict_SetItem(symbols
, name
, o
) < 0) {
565 Py_DECREF(free_value
);
570 /* else it's not free, probably a cell */
573 if (!PyDict_GetItem(bound
, name
))
574 continue; /* it's a global */
576 if (PyDict_SetItem(symbols
, name
, free_value
) < 0) {
577 Py_DECREF(free_value
);
581 Py_DECREF(free_value
);
585 /* Make final symbol table decisions for block of ste.
587 ste -- current symtable entry (input/output)
588 bound -- set of variables bound in enclosing scopes (input)
589 free -- set of free variables in enclosed scopes (output)
590 globals -- set of declared global variables in enclosing scopes (input)
594 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
597 PyObject
*name
, *v
, *local
= NULL
, *scope
= NULL
, *newbound
= NULL
;
598 PyObject
*newglobal
= NULL
, *newfree
= NULL
;
602 local
= PyDict_New();
605 scope
= PyDict_New();
608 newglobal
= PyDict_New();
611 newfree
= PyDict_New();
614 newbound
= PyDict_New();
618 if (ste
->ste_type
== ClassBlock
) {
619 /* make a copy of globals before calling analyze_name(),
620 because global statements in the class have no effect
623 if (PyDict_Update(newglobal
, global
) < 0)
626 if (PyDict_Update(newbound
, bound
) < 0)
630 assert(PySTEntry_Check(ste
));
631 assert(PyDict_Check(ste
->ste_symbols
));
632 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
633 long flags
= PyInt_AS_LONG(v
);
634 if (!analyze_name(ste
, scope
, name
, flags
, bound
, local
, free
,
639 if (ste
->ste_type
!= ClassBlock
) {
640 if (ste
->ste_type
== FunctionBlock
) {
641 if (PyDict_Update(newbound
, local
) < 0)
645 if (PyDict_Update(newbound
, bound
) < 0)
648 if (PyDict_Update(newglobal
, global
) < 0)
652 /* Recursively call analyze_block() on each child block */
653 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
654 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
655 PySTEntryObject
* entry
;
656 assert(c
&& PySTEntry_Check(c
));
657 entry
= (PySTEntryObject
*)c
;
658 if (!analyze_block(entry
, newbound
, newfree
, newglobal
))
660 if (entry
->ste_free
|| entry
->ste_child_free
)
661 ste
->ste_child_free
= 1;
664 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scope
, newfree
))
666 if (!update_symbols(ste
->ste_symbols
, scope
, bound
, newfree
,
667 ste
->ste_type
== ClassBlock
))
669 if (!check_unoptimized(ste
))
672 if (PyDict_Update(free
, newfree
) < 0)
678 Py_XDECREF(newbound
);
679 Py_XDECREF(newglobal
);
682 assert(PyErr_Occurred());
687 symtable_analyze(struct symtable
*st
)
689 PyObject
*free
, *global
;
695 global
= PyDict_New();
700 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
708 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
710 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
711 lineno
, NULL
, NULL
) < 0) {
712 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
713 PyErr_SetString(PyExc_SyntaxError
, msg
);
714 PyErr_SyntaxLocation(st
->st_filename
,
715 st
->st_cur
->ste_lineno
);
722 /* symtable_enter_block() gets a reference via PySTEntry_New().
723 This reference is released when the block is exited, via the DECREF
724 in symtable_exit_block().
728 symtable_exit_block(struct symtable
*st
, void *ast
)
732 Py_CLEAR(st
->st_cur
);
733 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
735 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
737 if (st
->st_cur
== NULL
)
739 Py_INCREF(st
->st_cur
);
740 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
747 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
748 void *ast
, int lineno
)
750 PySTEntryObject
*prev
= NULL
;
754 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
757 Py_DECREF(st
->st_cur
);
759 st
->st_cur
= PySTEntry_New(st
, name
, block
, ast
, lineno
);
760 if (st
->st_cur
== NULL
)
762 if (name
== GET_IDENTIFIER(top
))
763 st
->st_global
= st
->st_cur
->ste_symbols
;
765 if (PyList_Append(prev
->ste_children
,
766 (PyObject
*)st
->st_cur
) < 0) {
774 symtable_lookup(struct symtable
*st
, PyObject
*name
)
777 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
780 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
784 return PyInt_AsLong(o
);
788 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
793 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
797 dict
= st
->st_cur
->ste_symbols
;
798 if ((o
= PyDict_GetItem(dict
, mangled
))) {
799 val
= PyInt_AS_LONG(o
);
800 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
801 /* Is it better to use 'mangled' or 'name' here? */
802 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
803 PyString_AsString(name
));
804 PyErr_SyntaxLocation(st
->st_filename
,
805 st
->st_cur
->ste_lineno
);
811 o
= PyInt_FromLong(val
);
814 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
820 if (flag
& DEF_PARAM
) {
821 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
823 } else if (flag
& DEF_GLOBAL
) {
824 /* XXX need to update DEF_GLOBAL for other flags too;
825 perhaps only DEF_FREE_GLOBAL */
827 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
828 val
|= PyInt_AS_LONG(o
);
830 o
= PyInt_FromLong(val
);
833 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
847 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
848 They use the ASDL name to synthesize the name of the C type and the visit
851 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
852 useful if the first node in the sequence requires special treatment.
855 #define VISIT(ST, TYPE, V) \
856 if (!symtable_visit_ ## TYPE((ST), (V))) \
859 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
860 if (!symtable_visit_ ## TYPE((ST), (V))) { \
861 symtable_exit_block((ST), (S)); \
865 #define VISIT_SEQ(ST, TYPE, SEQ) { \
867 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
868 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
869 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
870 if (!symtable_visit_ ## TYPE((ST), elt)) \
875 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
877 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
878 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
879 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
880 if (!symtable_visit_ ## TYPE((ST), elt)) { \
881 symtable_exit_block((ST), (S)); \
887 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
889 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
890 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
891 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
892 if (!symtable_visit_ ## TYPE((ST), elt)) \
897 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
899 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
900 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
901 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
902 if (!symtable_visit_ ## TYPE((ST), elt)) { \
903 symtable_exit_block((ST), (S)); \
910 symtable_new_tmpname(struct symtable
*st
)
915 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]",
916 ++st
->st_cur
->ste_tmpname
);
917 tmp
= PyString_InternFromString(tmpname
);
918 if (!symtable_add_def(st
, tmp
, DEF_LOCAL
))
925 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
928 case FunctionDef_kind
:
929 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
931 if (s
->v
.FunctionDef
.args
->defaults
)
932 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
933 if (s
->v
.FunctionDef
.decorators
)
934 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorators
);
935 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
936 FunctionBlock
, (void *)s
, s
->lineno
))
938 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
939 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
940 if (!symtable_exit_block(st
, s
))
943 case ClassDef_kind
: {
945 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
947 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
948 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
949 (void *)s
, s
->lineno
))
951 tmp
= st
->st_private
;
952 st
->st_private
= s
->v
.ClassDef
.name
;
953 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
954 st
->st_private
= tmp
;
955 if (!symtable_exit_block(st
, s
))
960 if (s
->v
.Return
.value
) {
961 VISIT(st
, expr
, s
->v
.Return
.value
);
962 st
->st_cur
->ste_returns_value
= 1;
963 if (st
->st_cur
->ste_generator
) {
964 PyErr_SetString(PyExc_SyntaxError
,
965 RETURN_VAL_IN_GENERATOR
);
966 PyErr_SyntaxLocation(st
->st_filename
,
973 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
976 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
977 VISIT(st
, expr
, s
->v
.Assign
.value
);
980 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
981 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
985 VISIT(st
, expr
, s
->v
.Print
.dest
);
986 VISIT_SEQ(st
, expr
, s
->v
.Print
.values
);
989 VISIT(st
, expr
, s
->v
.For
.target
);
990 VISIT(st
, expr
, s
->v
.For
.iter
);
991 VISIT_SEQ(st
, stmt
, s
->v
.For
.body
);
993 VISIT_SEQ(st
, stmt
, s
->v
.For
.orelse
);
996 VISIT(st
, expr
, s
->v
.While
.test
);
997 VISIT_SEQ(st
, stmt
, s
->v
.While
.body
);
998 if (s
->v
.While
.orelse
)
999 VISIT_SEQ(st
, stmt
, s
->v
.While
.orelse
);
1002 /* XXX if 0: and lookup_yield() hacks */
1003 VISIT(st
, expr
, s
->v
.If
.test
);
1004 VISIT_SEQ(st
, stmt
, s
->v
.If
.body
);
1006 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
1009 if (s
->v
.Raise
.type
) {
1010 VISIT(st
, expr
, s
->v
.Raise
.type
);
1011 if (s
->v
.Raise
.inst
) {
1012 VISIT(st
, expr
, s
->v
.Raise
.inst
);
1013 if (s
->v
.Raise
.tback
)
1014 VISIT(st
, expr
, s
->v
.Raise
.tback
);
1018 case TryExcept_kind
:
1019 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
1020 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
1021 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
1023 case TryFinally_kind
:
1024 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
1025 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
1028 VISIT(st
, expr
, s
->v
.Assert
.test
);
1029 if (s
->v
.Assert
.msg
)
1030 VISIT(st
, expr
, s
->v
.Assert
.msg
);
1033 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
1034 /* XXX Don't have the lineno available inside
1036 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1037 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1039 case ImportFrom_kind
:
1040 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
1041 /* XXX Don't have the lineno available inside
1043 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1044 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1047 VISIT(st
, expr
, s
->v
.Exec
.body
);
1048 if (!st
->st_cur
->ste_opt_lineno
)
1049 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1050 if (s
->v
.Exec
.globals
) {
1051 st
->st_cur
->ste_unoptimized
|= OPT_EXEC
;
1052 VISIT(st
, expr
, s
->v
.Exec
.globals
);
1053 if (s
->v
.Exec
.locals
)
1054 VISIT(st
, expr
, s
->v
.Exec
.locals
);
1056 st
->st_cur
->ste_unoptimized
|= OPT_BARE_EXEC
;
1061 asdl_seq
*seq
= s
->v
.Global
.names
;
1062 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1063 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1064 char *c_name
= PyString_AS_STRING(name
);
1065 long cur
= symtable_lookup(st
, name
);
1068 if (cur
& (DEF_LOCAL
| USE
)) {
1070 if (cur
& DEF_LOCAL
)
1071 PyOS_snprintf(buf
, sizeof(buf
),
1072 GLOBAL_AFTER_ASSIGN
,
1075 PyOS_snprintf(buf
, sizeof(buf
),
1078 if (!symtable_warn(st
, buf
, s
->lineno
))
1081 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1087 VISIT(st
, expr
, s
->v
.Expr
.value
);
1092 /* nothing to do here */
1095 if (!symtable_new_tmpname(st
))
1097 VISIT(st
, expr
, s
->v
.With
.context_expr
);
1098 if (s
->v
.With
.optional_vars
) {
1099 if (!symtable_new_tmpname(st
))
1101 VISIT(st
, expr
, s
->v
.With
.optional_vars
);
1103 VISIT_SEQ(st
, stmt
, s
->v
.With
.body
);
1110 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1114 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1117 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1118 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1121 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1124 if (!symtable_add_def(st
, GET_IDENTIFIER(lambda
), DEF_LOCAL
))
1126 if (e
->v
.Lambda
.args
->defaults
)
1127 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1128 /* XXX how to get line numbers for expressions */
1129 if (!symtable_enter_block(st
, GET_IDENTIFIER(lambda
),
1130 FunctionBlock
, (void *)e
, 0))
1132 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1133 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1134 if (!symtable_exit_block(st
, (void *)e
))
1139 VISIT(st
, expr
, e
->v
.IfExp
.test
);
1140 VISIT(st
, expr
, e
->v
.IfExp
.body
);
1141 VISIT(st
, expr
, e
->v
.IfExp
.orelse
);
1144 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1145 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1148 if (!symtable_new_tmpname(st
))
1150 VISIT(st
, expr
, e
->v
.ListComp
.elt
);
1151 VISIT_SEQ(st
, comprehension
, e
->v
.ListComp
.generators
);
1153 case GeneratorExp_kind
:
1154 if (!symtable_visit_genexp(st
, e
))
1158 if (e
->v
.Yield
.value
)
1159 VISIT(st
, expr
, e
->v
.Yield
.value
);
1160 st
->st_cur
->ste_generator
= 1;
1161 if (st
->st_cur
->ste_returns_value
) {
1162 PyErr_SetString(PyExc_SyntaxError
,
1163 RETURN_VAL_IN_GENERATOR
);
1164 PyErr_SyntaxLocation(st
->st_filename
,
1170 VISIT(st
, expr
, e
->v
.Compare
.left
);
1171 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1174 VISIT(st
, expr
, e
->v
.Call
.func
);
1175 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1176 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1177 if (e
->v
.Call
.starargs
)
1178 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1179 if (e
->v
.Call
.kwargs
)
1180 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1183 VISIT(st
, expr
, e
->v
.Repr
.value
);
1187 /* Nothing to do here. */
1189 /* The following exprs can be assignment targets. */
1190 case Attribute_kind
:
1191 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1193 case Subscript_kind
:
1194 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1195 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1198 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1199 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1202 /* child nodes of List and Tuple will have expr_context set */
1204 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1207 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1214 symtable_implicit_arg(struct symtable
*st
, int pos
)
1216 PyObject
*id
= PyString_FromFormat(".%d", pos
);
1219 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1228 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int toplevel
)
1232 /* go through all the toplevel arguments first */
1233 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1234 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1235 if (arg
->kind
== Name_kind
) {
1236 assert(arg
->v
.Name
.ctx
== Param
||
1237 (arg
->v
.Name
.ctx
== Store
&& !toplevel
));
1238 if (!symtable_add_def(st
, arg
->v
.Name
.id
, DEF_PARAM
))
1241 else if (arg
->kind
== Tuple_kind
) {
1242 assert(arg
->v
.Tuple
.ctx
== Store
);
1244 if (!symtable_implicit_arg(st
, i
))
1249 PyErr_SetString(PyExc_SyntaxError
,
1250 "invalid expression in parameter list");
1251 PyErr_SyntaxLocation(st
->st_filename
,
1252 st
->st_cur
->ste_lineno
);
1258 if (!symtable_visit_params_nested(st
, args
))
1266 symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
)
1269 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1270 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1271 if (arg
->kind
== Tuple_kind
&&
1272 !symtable_visit_params(st
, arg
->v
.Tuple
.elts
, 0))
1280 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1282 /* skip default arguments inside function block
1283 XXX should ast be different?
1285 if (a
->args
&& !symtable_visit_params(st
, a
->args
, 1))
1288 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1290 st
->st_cur
->ste_varargs
= 1;
1293 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1295 st
->st_cur
->ste_varkeywords
= 1;
1297 if (a
->args
&& !symtable_visit_params_nested(st
, a
->args
))
1304 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1307 VISIT(st
, expr
, eh
->type
);
1309 VISIT(st
, expr
, eh
->name
);
1310 VISIT_SEQ(st
, stmt
, eh
->body
);
1316 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1318 /* Compute store_name, the name actually bound by the import
1319 operation. It is diferent than a->name when a->name is a
1320 dotted package name (e.g. spam.eggs)
1322 PyObject
*store_name
;
1323 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1324 const char *base
= PyString_AS_STRING(name
);
1325 char *dot
= strchr(base
, '.');
1327 store_name
= PyString_FromStringAndSize(base
, dot
- base
);
1330 Py_INCREF(store_name
);
1332 if (strcmp(PyString_AS_STRING(name
), "*")) {
1333 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1334 Py_DECREF(store_name
);
1338 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1339 int lineno
= st
->st_cur
->ste_lineno
;
1340 if (!symtable_warn(st
, IMPORT_STAR_WARNING
, lineno
)) {
1341 Py_DECREF(store_name
);
1345 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1346 Py_DECREF(store_name
);
1353 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1355 VISIT(st
, expr
, lc
->target
);
1356 VISIT(st
, expr
, lc
->iter
);
1357 VISIT_SEQ(st
, expr
, lc
->ifs
);
1363 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1365 VISIT(st
, expr
, k
->value
);
1371 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1375 if (s
->v
.Slice
.lower
)
1376 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1377 if (s
->v
.Slice
.upper
)
1378 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1379 if (s
->v
.Slice
.step
)
1380 VISIT(st
, expr
, s
->v
.Slice
.step
)
1383 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1386 VISIT(st
, expr
, s
->v
.Index
.value
)
1395 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1397 comprehension_ty outermost
= ((comprehension_ty
)
1398 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
, 0)));
1399 /* Outermost iterator is evaluated in current scope */
1400 VISIT(st
, expr
, outermost
->iter
);
1401 /* Create generator scope for the rest */
1402 if (!symtable_enter_block(st
, GET_IDENTIFIER(genexpr
),
1403 FunctionBlock
, (void *)e
, 0)) {
1406 st
->st_cur
->ste_generator
= 1;
1407 /* Outermost iter is received as an argument */
1408 if (!symtable_implicit_arg(st
, 0)) {
1409 symtable_exit_block(st
, (void *)e
);
1412 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1413 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1414 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1415 e
->v
.GeneratorExp
.generators
, 1, (void*)e
);
1416 VISIT_IN_BLOCK(st
, expr
, e
->v
.GeneratorExp
.elt
, (void*)e
);
1417 if (!symtable_exit_block(st
, (void *)e
))