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
);
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;
62 ste
->ste_lineno
= lineno
;
64 if (st
->st_cur
!= NULL
&&
65 (st
->st_cur
->ste_nested
||
66 st
->st_cur
->ste_type
== FunctionBlock
))
68 ste
->ste_child_free
= 0;
69 ste
->ste_generator
= 0;
70 ste
->ste_returns_value
= 0;
72 if (PyDict_SetItem(st
->st_symbols
, ste
->ste_id
, (PyObject
*)ste
) < 0)
82 ste_repr(PySTEntryObject
*ste
)
86 PyOS_snprintf(buf
, sizeof(buf
),
87 "<symtable entry %.100s(%ld), line %d>",
88 PyString_AS_STRING(ste
->ste_name
),
89 PyInt_AS_LONG(ste
->ste_id
), ste
->ste_lineno
);
90 return PyString_FromString(buf
);
94 ste_dealloc(PySTEntryObject
*ste
)
96 ste
->ste_table
= NULL
;
97 Py_XDECREF(ste
->ste_id
);
98 Py_XDECREF(ste
->ste_name
);
99 Py_XDECREF(ste
->ste_symbols
);
100 Py_XDECREF(ste
->ste_varnames
);
101 Py_XDECREF(ste
->ste_children
);
105 #define OFF(x) offsetof(PySTEntryObject, x)
107 static PyMemberDef ste_memberlist
[] = {
108 {"id", T_OBJECT
, OFF(ste_id
), READONLY
},
109 {"name", T_OBJECT
, OFF(ste_name
), READONLY
},
110 {"symbols", T_OBJECT
, OFF(ste_symbols
), READONLY
},
111 {"varnames", T_OBJECT
, OFF(ste_varnames
), READONLY
},
112 {"children", T_OBJECT
, OFF(ste_children
), READONLY
},
113 {"optimized",T_INT
, OFF(ste_unoptimized
), READONLY
},
114 {"nested", T_INT
, OFF(ste_nested
), READONLY
},
115 {"type", T_INT
, OFF(ste_type
), READONLY
},
116 {"lineno", T_INT
, OFF(ste_lineno
), READONLY
},
120 PyTypeObject PySTEntry_Type
= {
121 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
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
)
205 st
->st_private
= NULL
;
213 PySymtable_Build(mod_ty mod
, const char *filename
, PyFutureFeatures
*future
)
215 struct symtable
*st
= symtable_new();
221 st
->st_filename
= filename
;
222 st
->st_future
= future
;
223 if (!GET_IDENTIFIER(top
) ||
224 !symtable_enter_block(st
, top
, ModuleBlock
, (void *)mod
, 0)) {
229 st
->st_top
= st
->st_cur
;
230 st
->st_cur
->ste_unoptimized
= OPT_TOPLEVEL
;
231 /* Any other top-level initialization? */
234 seq
= mod
->v
.Module
.body
;
235 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
236 if (!symtable_visit_stmt(st
,
237 (stmt_ty
)asdl_seq_GET(seq
, i
)))
240 case Expression_kind
:
241 if (!symtable_visit_expr(st
, mod
->v
.Expression
.body
))
244 case Interactive_kind
:
245 seq
= mod
->v
.Interactive
.body
;
246 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
247 if (!symtable_visit_stmt(st
,
248 (stmt_ty
)asdl_seq_GET(seq
, i
)))
252 PyErr_SetString(PyExc_RuntimeError
,
253 "this compiler does not handle Suites");
256 if (!symtable_exit_block(st
, (void *)mod
)) {
260 if (symtable_analyze(st
))
265 (void) symtable_exit_block(st
, (void *)mod
);
271 PySymtable_Free(struct symtable
*st
)
273 Py_XDECREF(st
->st_symbols
);
274 Py_XDECREF(st
->st_stack
);
275 PyMem_Free((void *)st
);
279 PySymtable_Lookup(struct symtable
*st
, void *key
)
283 k
= PyLong_FromVoidPtr(key
);
286 v
= PyDict_GetItem(st
->st_symbols
, k
);
288 assert(PySTEntry_Check(v
));
292 PyErr_SetString(PyExc_KeyError
,
293 "unknown symbol table entry");
297 return (PySTEntryObject
*)v
;
301 PyST_GetScope(PySTEntryObject
*ste
, PyObject
*name
)
303 PyObject
*v
= PyDict_GetItem(ste
->ste_symbols
, name
);
306 assert(PyInt_Check(v
));
307 return (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
311 /* Analyze raw symbol information to determine scope of each name.
313 The next several functions are helpers for PySymtable_Analyze(),
314 which determines whether a name is local, global, or free. In addition,
315 it determines which local variables are cell variables; they provide
316 bindings that are used for free variables in enclosed blocks.
318 There are also two kinds of free variables, implicit and explicit. An
319 explicit global is declared with the global statement. An implicit
320 global is a free variable for which the compiler has found no binding
321 in an enclosing function scope. The implicit global is either a global
322 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
323 to handle these names to implement slightly odd semantics. In such a
324 block, the name is treated as global until it is assigned to; then it
325 is treated as a local.
327 The symbol table requires two passes to determine the scope of each name.
328 The first pass collects raw facts from the AST: the name is a parameter
329 here, the name is used by not defined here, etc. The second pass analyzes
330 these facts during a pass over the PySTEntryObjects created during pass 1.
332 When a function is entered during the second pass, the parent passes
333 the set of all name bindings visible to its children. These bindings
334 are used to determine if the variable is free or an implicit global.
335 After doing the local analysis, it analyzes each of its child blocks
336 using an updated set of name bindings.
338 The children update the free variable set. If a local variable is free
339 in a child, the variable is marked as a cell. The current function must
340 provide runtime storage for the variable that may outlive the function's
341 frame. Cell variables are removed from the free set before the analyze
342 function returns to its parent.
344 The sets of bound and free variables are implemented as dictionaries
345 mapping strings to None.
348 #define SET_SCOPE(DICT, NAME, I) { \
349 PyObject *o = PyInt_FromLong(I); \
352 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
359 /* Decide on scope of name, given flags.
361 The namespace dictionaries may be modified to record information
362 about the new name. For example, a new global will add an entry to
363 global. A name that was global can be changed to local.
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
));
376 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
381 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
382 if (PyDict_SetItem(global
, name
, Py_None
) < 0)
384 if (bound
&& PyDict_GetItem(bound
, name
)) {
385 if (PyDict_DelItem(bound
, name
) < 0)
390 if (flags
& DEF_BOUND
) {
391 SET_SCOPE(dict
, name
, LOCAL
);
392 if (PyDict_SetItem(local
, name
, Py_None
) < 0)
394 if (PyDict_GetItem(global
, name
)) {
395 if (PyDict_DelItem(global
, name
) < 0)
400 /* If an enclosing block has a binding for this name, it
401 is a free variable rather than a global variable.
402 Note that having a non-NULL bound implies that the block
405 if (bound
&& PyDict_GetItem(bound
, name
)) {
406 SET_SCOPE(dict
, name
, FREE
);
408 if (PyDict_SetItem(free
, name
, Py_None
) < 0)
412 /* If a parent has a global statement, then call it global
413 explicit? It could also be global implicit.
415 else if (global
&& PyDict_GetItem(global
, name
)) {
416 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
422 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
425 /* Should never get here. */
426 PyErr_Format(PyExc_SystemError
, "failed to set scope for %s",
427 PyString_AS_STRING(name
));
433 /* If a name is defined in free and also in locals, then this block
434 provides the binding for the free variable. The name should be
435 marked CELL in this block and removed from the free list.
437 Note that the current block's free variables are included in free.
438 That's safe because no name can be free and local in the same scope.
442 analyze_cells(PyObject
*scope
, PyObject
*free
)
444 PyObject
*name
, *v
, *w
;
448 w
= PyInt_FromLong(CELL
);
451 while (PyDict_Next(scope
, &pos
, &name
, &v
)) {
453 assert(PyInt_Check(v
));
454 flags
= PyInt_AS_LONG(v
);
457 if (!PyDict_GetItem(free
, name
))
459 /* Replace LOCAL with CELL for this name, and remove
460 from free. It is safe to replace the value of name
461 in the dict, because it will not cause a resize.
463 if (PyDict_SetItem(scope
, name
, w
) < 0)
465 if (!PyDict_DelItem(free
, name
) < 0)
474 /* Check for illegal statements in unoptimized namespaces */
476 check_unoptimized(const PySTEntryObject
* ste
) {
480 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
481 || !(ste
->ste_free
|| ste
->ste_child_free
))
484 trailer
= (ste
->ste_child_free
?
485 "contains a nested function with free variables" :
486 "is a nested function");
488 switch (ste
->ste_unoptimized
) {
489 case OPT_TOPLEVEL
: /* exec / import * at top-level is fine */
490 case OPT_EXEC
: /* qualified exec is fine */
492 case OPT_IMPORT_STAR
:
493 PyOS_snprintf(buf
, sizeof(buf
),
494 "import * is not allowed in function '%.100s' "
496 PyString_AS_STRING(ste
->ste_name
), trailer
);
499 PyOS_snprintf(buf
, sizeof(buf
),
500 "unqualified exec is not allowed in function "
502 PyString_AS_STRING(ste
->ste_name
), trailer
);
505 PyOS_snprintf(buf
, sizeof(buf
),
506 "function '%.100s' uses import * and bare exec, "
507 "which are illegal because it %s",
508 PyString_AS_STRING(ste
->ste_name
), trailer
);
512 PyErr_SetString(PyExc_SyntaxError
, buf
);
513 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
514 ste
->ste_opt_lineno
);
518 /* Enter the final scope information into the st_symbols dict.
520 * All arguments are dicts. Modifies symbols, others are read-only.
523 update_symbols(PyObject
*symbols
, PyObject
*scope
,
524 PyObject
*bound
, PyObject
*free
, int classflag
)
526 PyObject
*name
, *v
, *u
, *w
, *free_value
= NULL
;
529 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
531 assert(PyInt_Check(v
));
532 flags
= PyInt_AS_LONG(v
);
533 w
= PyDict_GetItem(scope
, name
);
534 assert(w
&& PyInt_Check(w
));
535 i
= PyInt_AS_LONG(w
);
536 flags
|= (i
<< SCOPE_OFF
);
537 u
= PyInt_FromLong(flags
);
540 if (PyDict_SetItem(symbols
, name
, u
) < 0) {
547 free_value
= PyInt_FromLong(FREE
<< SCOPE_OFF
);
551 /* add a free variable when it's only use is for creating a closure */
553 while (PyDict_Next(free
, &pos
, &name
, &v
)) {
554 PyObject
*o
= PyDict_GetItem(symbols
, name
);
557 /* It could be a free variable in a method of
558 the class that has the same name as a local
559 or global in the class scope.
562 PyInt_AS_LONG(o
) & (DEF_BOUND
| DEF_GLOBAL
)) {
563 long i
= PyInt_AS_LONG(o
) | DEF_FREE_CLASS
;
564 o
= PyInt_FromLong(i
);
566 Py_DECREF(free_value
);
569 if (PyDict_SetItem(symbols
, name
, o
) < 0) {
571 Py_DECREF(free_value
);
576 /* else it's not free, probably a cell */
579 if (!PyDict_GetItem(bound
, name
))
580 continue; /* it's a global */
582 if (PyDict_SetItem(symbols
, name
, free_value
) < 0) {
583 Py_DECREF(free_value
);
587 Py_DECREF(free_value
);
591 /* Make final symbol table decisions for block of ste.
594 ste -- current symtable entry (input/output)
595 bound -- set of variables bound in enclosing scopes (input). bound
596 is NULL for module blocks.
597 free -- set of free variables in enclosed scopes (output)
598 globals -- set of declared global variables in enclosing scopes (input)
600 The implementation uses two mutually recursive functions,
601 analyze_block() and analyze_child_block(). analyze_block() is
602 responsible for analyzing the individual names defined in a block.
603 analyze_child_block() prepares temporary namespace dictionaries
604 used to evaluated nested blocks.
606 The two functions exist because a child block should see the name
607 bindings of its enclosing blocks, but those bindings should not
608 propagate back to a parent block.
612 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
613 PyObject
*global
, PyObject
* child_free
);
616 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
619 PyObject
*name
, *v
, *local
= NULL
, *scope
= NULL
;
620 PyObject
*newbound
= NULL
, *newglobal
= NULL
;
621 PyObject
*newfree
= NULL
, *allfree
= NULL
;
625 local
= PyDict_New(); /* collect new names bound in block */
628 scope
= PyDict_New(); /* collect scopes defined for each name */
632 /* Allocate new global and bound variable dictionaries. These
633 dictionaries hold the names visible in nested blocks. For
634 ClassBlocks, the bound and global names are initialized
635 before analyzing names, because class bindings aren't
636 visible in methods. For other blocks, they are initialized
637 after names are analyzed.
640 /* TODO(jhylton): Package these dicts in a struct so that we
641 can write reasonable helper functions?
643 newglobal
= PyDict_New();
646 newbound
= PyDict_New();
649 newfree
= PyDict_New();
653 if (ste
->ste_type
== ClassBlock
) {
654 if (PyDict_Update(newglobal
, global
) < 0)
657 if (PyDict_Update(newbound
, bound
) < 0)
661 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
662 long flags
= PyInt_AS_LONG(v
);
663 if (!analyze_name(ste
, scope
, name
, flags
,
664 bound
, local
, free
, global
))
668 if (ste
->ste_type
!= ClassBlock
) {
669 if (ste
->ste_type
== FunctionBlock
) {
670 if (PyDict_Update(newbound
, local
) < 0)
674 if (PyDict_Update(newbound
, bound
) < 0)
677 if (PyDict_Update(newglobal
, global
) < 0)
681 /* Recursively call analyze_block() on each child block.
683 newbound, newglobal now contain the names visible in
684 nested blocks. The free variables in the children will
685 be collected in allfree.
687 allfree
= PyDict_New();
690 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
691 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
692 PySTEntryObject
* entry
;
693 assert(c
&& PySTEntry_Check(c
));
694 entry
= (PySTEntryObject
*)c
;
695 if (!analyze_child_block(entry
, newbound
, newfree
, newglobal
,
698 if (entry
->ste_free
|| entry
->ste_child_free
)
699 ste
->ste_child_free
= 1;
702 if (PyDict_Update(newfree
, allfree
) < 0)
704 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scope
, newfree
))
706 if (!update_symbols(ste
->ste_symbols
, scope
, bound
, newfree
,
707 ste
->ste_type
== ClassBlock
))
709 if (!check_unoptimized(ste
))
712 if (PyDict_Update(free
, newfree
) < 0)
718 Py_XDECREF(newbound
);
719 Py_XDECREF(newglobal
);
723 assert(PyErr_Occurred());
728 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
729 PyObject
*global
, PyObject
* child_free
)
731 PyObject
*temp_bound
= NULL
, *temp_global
= NULL
, *temp_free
= NULL
;
733 /* Copy the bound and global dictionaries.
735 These dictionary are used by all blocks enclosed by the
736 current block. The analyze_block() call modifies these
740 temp_bound
= PyDict_New();
743 if (PyDict_Update(temp_bound
, bound
) < 0)
745 temp_free
= PyDict_New();
748 if (PyDict_Update(temp_free
, free
) < 0)
750 temp_global
= PyDict_New();
753 if (PyDict_Update(temp_global
, global
) < 0)
756 if (!analyze_block(entry
, temp_bound
, temp_free
, temp_global
))
758 if (PyDict_Update(child_free
, temp_free
) < 0)
760 Py_DECREF(temp_bound
);
761 Py_DECREF(temp_free
);
762 Py_DECREF(temp_global
);
765 Py_XDECREF(temp_bound
);
766 Py_XDECREF(temp_free
);
767 Py_XDECREF(temp_global
);
772 symtable_analyze(struct symtable
*st
)
774 PyObject
*free
, *global
;
780 global
= PyDict_New();
785 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
793 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
795 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
796 lineno
, NULL
, NULL
) < 0) {
797 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
798 PyErr_SetString(PyExc_SyntaxError
, msg
);
799 PyErr_SyntaxLocation(st
->st_filename
,
800 st
->st_cur
->ste_lineno
);
807 /* symtable_enter_block() gets a reference via ste_new.
808 This reference is released when the block is exited, via the DECREF
809 in symtable_exit_block().
813 symtable_exit_block(struct symtable
*st
, void *ast
)
817 Py_CLEAR(st
->st_cur
);
818 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
820 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
822 if (st
->st_cur
== NULL
)
824 Py_INCREF(st
->st_cur
);
825 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
832 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
833 void *ast
, int lineno
)
835 PySTEntryObject
*prev
= NULL
;
839 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
842 Py_DECREF(st
->st_cur
);
844 st
->st_cur
= ste_new(st
, name
, block
, ast
, lineno
);
845 if (st
->st_cur
== NULL
)
847 if (name
== GET_IDENTIFIER(top
))
848 st
->st_global
= st
->st_cur
->ste_symbols
;
850 if (PyList_Append(prev
->ste_children
,
851 (PyObject
*)st
->st_cur
) < 0) {
859 symtable_lookup(struct symtable
*st
, PyObject
*name
)
862 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
865 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
869 return PyInt_AsLong(o
);
873 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
878 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
882 dict
= st
->st_cur
->ste_symbols
;
883 if ((o
= PyDict_GetItem(dict
, mangled
))) {
884 val
= PyInt_AS_LONG(o
);
885 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
886 /* Is it better to use 'mangled' or 'name' here? */
887 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
888 PyString_AsString(name
));
889 PyErr_SyntaxLocation(st
->st_filename
,
890 st
->st_cur
->ste_lineno
);
896 o
= PyInt_FromLong(val
);
899 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
905 if (flag
& DEF_PARAM
) {
906 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
908 } else if (flag
& DEF_GLOBAL
) {
909 /* XXX need to update DEF_GLOBAL for other flags too;
910 perhaps only DEF_FREE_GLOBAL */
912 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
913 val
|= PyInt_AS_LONG(o
);
915 o
= PyInt_FromLong(val
);
918 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
932 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
933 They use the ASDL name to synthesize the name of the C type and the visit
936 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
937 useful if the first node in the sequence requires special treatment.
940 #define VISIT(ST, TYPE, V) \
941 if (!symtable_visit_ ## TYPE((ST), (V))) \
944 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
945 if (!symtable_visit_ ## TYPE((ST), (V))) { \
946 symtable_exit_block((ST), (S)); \
950 #define VISIT_SEQ(ST, TYPE, SEQ) { \
952 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
953 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
954 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
955 if (!symtable_visit_ ## TYPE((ST), elt)) \
960 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
962 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
963 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
964 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
965 if (!symtable_visit_ ## TYPE((ST), elt)) { \
966 symtable_exit_block((ST), (S)); \
972 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
974 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
975 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
976 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
977 if (!symtable_visit_ ## TYPE((ST), elt)) \
982 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
984 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
985 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
986 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
987 if (!symtable_visit_ ## TYPE((ST), elt)) { \
988 symtable_exit_block((ST), (S)); \
995 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
998 case FunctionDef_kind
:
999 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
1001 if (s
->v
.FunctionDef
.args
->defaults
)
1002 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
1003 if (s
->v
.FunctionDef
.decorator_list
)
1004 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorator_list
);
1005 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
1006 FunctionBlock
, (void *)s
, s
->lineno
))
1008 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
1009 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
1010 if (!symtable_exit_block(st
, s
))
1013 case ClassDef_kind
: {
1015 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
1017 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
1018 if (s
->v
.ClassDef
.decorator_list
)
1019 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.decorator_list
);
1020 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
1021 (void *)s
, s
->lineno
))
1023 tmp
= st
->st_private
;
1024 st
->st_private
= s
->v
.ClassDef
.name
;
1025 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
1026 st
->st_private
= tmp
;
1027 if (!symtable_exit_block(st
, s
))
1032 if (s
->v
.Return
.value
) {
1033 VISIT(st
, expr
, s
->v
.Return
.value
);
1034 st
->st_cur
->ste_returns_value
= 1;
1035 if (st
->st_cur
->ste_generator
) {
1036 PyErr_SetString(PyExc_SyntaxError
,
1037 RETURN_VAL_IN_GENERATOR
);
1038 PyErr_SyntaxLocation(st
->st_filename
,
1045 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
1048 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
1049 VISIT(st
, expr
, s
->v
.Assign
.value
);
1051 case AugAssign_kind
:
1052 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
1053 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
1056 if (s
->v
.Print
.dest
)
1057 VISIT(st
, expr
, s
->v
.Print
.dest
);
1058 VISIT_SEQ(st
, expr
, s
->v
.Print
.values
);
1061 VISIT(st
, expr
, s
->v
.For
.target
);
1062 VISIT(st
, expr
, s
->v
.For
.iter
);
1063 VISIT_SEQ(st
, stmt
, s
->v
.For
.body
);
1064 if (s
->v
.For
.orelse
)
1065 VISIT_SEQ(st
, stmt
, s
->v
.For
.orelse
);
1068 VISIT(st
, expr
, s
->v
.While
.test
);
1069 VISIT_SEQ(st
, stmt
, s
->v
.While
.body
);
1070 if (s
->v
.While
.orelse
)
1071 VISIT_SEQ(st
, stmt
, s
->v
.While
.orelse
);
1074 /* XXX if 0: and lookup_yield() hacks */
1075 VISIT(st
, expr
, s
->v
.If
.test
);
1076 VISIT_SEQ(st
, stmt
, s
->v
.If
.body
);
1078 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
1081 if (s
->v
.Raise
.type
) {
1082 VISIT(st
, expr
, s
->v
.Raise
.type
);
1083 if (s
->v
.Raise
.inst
) {
1084 VISIT(st
, expr
, s
->v
.Raise
.inst
);
1085 if (s
->v
.Raise
.tback
)
1086 VISIT(st
, expr
, s
->v
.Raise
.tback
);
1090 case TryExcept_kind
:
1091 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
1092 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
1093 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
1095 case TryFinally_kind
:
1096 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
1097 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
1100 VISIT(st
, expr
, s
->v
.Assert
.test
);
1101 if (s
->v
.Assert
.msg
)
1102 VISIT(st
, expr
, s
->v
.Assert
.msg
);
1105 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
1106 /* XXX Don't have the lineno available inside
1108 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1109 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1111 case ImportFrom_kind
:
1112 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
1113 /* XXX Don't have the lineno available inside
1115 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1116 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1119 VISIT(st
, expr
, s
->v
.Exec
.body
);
1120 if (!st
->st_cur
->ste_opt_lineno
)
1121 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1122 if (s
->v
.Exec
.globals
) {
1123 st
->st_cur
->ste_unoptimized
|= OPT_EXEC
;
1124 VISIT(st
, expr
, s
->v
.Exec
.globals
);
1125 if (s
->v
.Exec
.locals
)
1126 VISIT(st
, expr
, s
->v
.Exec
.locals
);
1128 st
->st_cur
->ste_unoptimized
|= OPT_BARE_EXEC
;
1133 asdl_seq
*seq
= s
->v
.Global
.names
;
1134 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1135 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1136 char *c_name
= PyString_AS_STRING(name
);
1137 long cur
= symtable_lookup(st
, name
);
1140 if (cur
& (DEF_LOCAL
| USE
)) {
1142 if (cur
& DEF_LOCAL
)
1143 PyOS_snprintf(buf
, sizeof(buf
),
1144 GLOBAL_AFTER_ASSIGN
,
1147 PyOS_snprintf(buf
, sizeof(buf
),
1150 if (!symtable_warn(st
, buf
, s
->lineno
))
1153 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1159 VISIT(st
, expr
, s
->v
.Expr
.value
);
1164 /* nothing to do here */
1167 VISIT(st
, expr
, s
->v
.With
.context_expr
);
1168 if (s
->v
.With
.optional_vars
) {
1169 VISIT(st
, expr
, s
->v
.With
.optional_vars
);
1171 VISIT_SEQ(st
, stmt
, s
->v
.With
.body
);
1178 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1182 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1185 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1186 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1189 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1192 if (!GET_IDENTIFIER(lambda
))
1194 if (e
->v
.Lambda
.args
->defaults
)
1195 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1196 /* XXX how to get line numbers for expressions */
1197 if (!symtable_enter_block(st
, lambda
,
1198 FunctionBlock
, (void *)e
, 0))
1200 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1201 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1202 if (!symtable_exit_block(st
, (void *)e
))
1207 VISIT(st
, expr
, e
->v
.IfExp
.test
);
1208 VISIT(st
, expr
, e
->v
.IfExp
.body
);
1209 VISIT(st
, expr
, e
->v
.IfExp
.orelse
);
1212 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1213 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1216 VISIT(st
, expr
, e
->v
.ListComp
.elt
);
1217 VISIT_SEQ(st
, comprehension
, e
->v
.ListComp
.generators
);
1219 case GeneratorExp_kind
:
1220 if (!symtable_visit_genexp(st
, e
))
1224 if (e
->v
.Yield
.value
)
1225 VISIT(st
, expr
, e
->v
.Yield
.value
);
1226 st
->st_cur
->ste_generator
= 1;
1227 if (st
->st_cur
->ste_returns_value
) {
1228 PyErr_SetString(PyExc_SyntaxError
,
1229 RETURN_VAL_IN_GENERATOR
);
1230 PyErr_SyntaxLocation(st
->st_filename
,
1236 VISIT(st
, expr
, e
->v
.Compare
.left
);
1237 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1240 VISIT(st
, expr
, e
->v
.Call
.func
);
1241 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1242 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1243 if (e
->v
.Call
.starargs
)
1244 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1245 if (e
->v
.Call
.kwargs
)
1246 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1249 VISIT(st
, expr
, e
->v
.Repr
.value
);
1253 /* Nothing to do here. */
1255 /* The following exprs can be assignment targets. */
1256 case Attribute_kind
:
1257 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1259 case Subscript_kind
:
1260 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1261 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1264 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1265 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1268 /* child nodes of List and Tuple will have expr_context set */
1270 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1273 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1280 symtable_implicit_arg(struct symtable
*st
, int pos
)
1282 PyObject
*id
= PyString_FromFormat(".%d", pos
);
1285 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1294 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int toplevel
)
1298 /* go through all the toplevel arguments first */
1299 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1300 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1301 if (arg
->kind
== Name_kind
) {
1302 assert(arg
->v
.Name
.ctx
== Param
||
1303 (arg
->v
.Name
.ctx
== Store
&& !toplevel
));
1304 if (!symtable_add_def(st
, arg
->v
.Name
.id
, DEF_PARAM
))
1307 else if (arg
->kind
== Tuple_kind
) {
1308 assert(arg
->v
.Tuple
.ctx
== Store
);
1310 if (!symtable_implicit_arg(st
, i
))
1315 PyErr_SetString(PyExc_SyntaxError
,
1316 "invalid expression in parameter list");
1317 PyErr_SyntaxLocation(st
->st_filename
,
1318 st
->st_cur
->ste_lineno
);
1324 if (!symtable_visit_params_nested(st
, args
))
1332 symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
)
1335 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1336 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1337 if (arg
->kind
== Tuple_kind
&&
1338 !symtable_visit_params(st
, arg
->v
.Tuple
.elts
, 0))
1346 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1348 /* skip default arguments inside function block
1349 XXX should ast be different?
1351 if (a
->args
&& !symtable_visit_params(st
, a
->args
, 1))
1354 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1356 st
->st_cur
->ste_varargs
= 1;
1359 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1361 st
->st_cur
->ste_varkeywords
= 1;
1363 if (a
->args
&& !symtable_visit_params_nested(st
, a
->args
))
1370 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1372 if (eh
->v
.ExceptHandler
.type
)
1373 VISIT(st
, expr
, eh
->v
.ExceptHandler
.type
);
1374 if (eh
->v
.ExceptHandler
.name
)
1375 VISIT(st
, expr
, eh
->v
.ExceptHandler
.name
);
1376 VISIT_SEQ(st
, stmt
, eh
->v
.ExceptHandler
.body
);
1382 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1384 /* Compute store_name, the name actually bound by the import
1385 operation. It is diferent than a->name when a->name is a
1386 dotted package name (e.g. spam.eggs)
1388 PyObject
*store_name
;
1389 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1390 const char *base
= PyString_AS_STRING(name
);
1391 char *dot
= strchr(base
, '.');
1393 store_name
= PyString_FromStringAndSize(base
, dot
- base
);
1399 Py_INCREF(store_name
);
1401 if (strcmp(PyString_AS_STRING(name
), "*")) {
1402 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1403 Py_DECREF(store_name
);
1407 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1408 int lineno
= st
->st_cur
->ste_lineno
;
1409 if (!symtable_warn(st
, IMPORT_STAR_WARNING
, lineno
)) {
1410 Py_DECREF(store_name
);
1414 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1415 Py_DECREF(store_name
);
1422 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1424 VISIT(st
, expr
, lc
->target
);
1425 VISIT(st
, expr
, lc
->iter
);
1426 VISIT_SEQ(st
, expr
, lc
->ifs
);
1432 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1434 VISIT(st
, expr
, k
->value
);
1440 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1444 if (s
->v
.Slice
.lower
)
1445 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1446 if (s
->v
.Slice
.upper
)
1447 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1448 if (s
->v
.Slice
.step
)
1449 VISIT(st
, expr
, s
->v
.Slice
.step
)
1452 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1455 VISIT(st
, expr
, s
->v
.Index
.value
)
1464 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1466 comprehension_ty outermost
= ((comprehension_ty
)
1467 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
, 0)));
1468 /* Outermost iterator is evaluated in current scope */
1469 VISIT(st
, expr
, outermost
->iter
);
1470 /* Create generator scope for the rest */
1471 if (!GET_IDENTIFIER(genexpr
) ||
1472 !symtable_enter_block(st
, genexpr
, FunctionBlock
, (void *)e
, 0)) {
1475 st
->st_cur
->ste_generator
= 1;
1476 /* Outermost iter is received as an argument */
1477 if (!symtable_implicit_arg(st
, 0)) {
1478 symtable_exit_block(st
, (void *)e
);
1481 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1482 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1483 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1484 e
->v
.GeneratorExp
.generators
, 1, (void*)e
);
1485 VISIT_IN_BLOCK(st
, expr
, e
->v
.GeneratorExp
.elt
, (void*)e
);
1486 return symtable_exit_block(st
, (void *)e
);