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 namespace dictionaries may be modified to record information
365 about the new name. For example, a new global will add an entry to
366 global. A name that was global can be changed to local.
370 analyze_name(PySTEntryObject
*ste
, PyObject
*dict
, PyObject
*name
, long flags
,
371 PyObject
*bound
, PyObject
*local
, PyObject
*free
,
374 if (flags
& DEF_GLOBAL
) {
375 if (flags
& DEF_PARAM
) {
376 PyErr_Format(PyExc_SyntaxError
,
377 "name '%s' is local and global",
378 PyString_AS_STRING(name
));
379 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
384 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
385 if (PyDict_SetItem(global
, name
, Py_None
) < 0)
387 if (bound
&& PyDict_GetItem(bound
, name
)) {
388 if (PyDict_DelItem(bound
, name
) < 0)
393 if (flags
& DEF_BOUND
) {
394 SET_SCOPE(dict
, name
, LOCAL
);
395 if (PyDict_SetItem(local
, name
, Py_None
) < 0)
397 if (PyDict_GetItem(global
, name
)) {
398 if (PyDict_DelItem(global
, name
) < 0)
403 /* If an enclosing block has a binding for this name, it
404 is a free variable rather than a global variable.
405 Note that having a non-NULL bound implies that the block
408 if (bound
&& PyDict_GetItem(bound
, name
)) {
409 SET_SCOPE(dict
, name
, FREE
);
411 if (PyDict_SetItem(free
, name
, Py_None
) < 0)
415 /* If a parent has a global statement, then call it global
416 explicit? It could also be global implicit.
418 else if (global
&& PyDict_GetItem(global
, name
)) {
419 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
425 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
428 /* Should never get here. */
429 PyErr_Format(PyExc_SystemError
, "failed to set scope for %s",
430 PyString_AS_STRING(name
));
436 /* If a name is defined in free and also in locals, then this block
437 provides the binding for the free variable. The name should be
438 marked CELL in this block and removed from the free list.
440 Note that the current block's free variables are included in free.
441 That's safe because no name can be free and local in the same scope.
445 analyze_cells(PyObject
*scope
, PyObject
*free
)
447 PyObject
*name
, *v
, *w
;
451 w
= PyInt_FromLong(CELL
);
454 while (PyDict_Next(scope
, &pos
, &name
, &v
)) {
456 assert(PyInt_Check(v
));
457 flags
= PyInt_AS_LONG(v
);
460 if (!PyDict_GetItem(free
, name
))
462 /* Replace LOCAL with CELL for this name, and remove
463 from free. It is safe to replace the value of name
464 in the dict, because it will not cause a resize.
466 if (PyDict_SetItem(scope
, name
, w
) < 0)
468 if (!PyDict_DelItem(free
, name
) < 0)
477 /* Check for illegal statements in unoptimized namespaces */
479 check_unoptimized(const PySTEntryObject
* ste
) {
483 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
484 || !(ste
->ste_free
|| ste
->ste_child_free
))
487 trailer
= (ste
->ste_child_free
?
488 "contains a nested function with free variables" :
489 "is a nested function");
491 switch (ste
->ste_unoptimized
) {
492 case OPT_TOPLEVEL
: /* exec / import * at top-level is fine */
493 case OPT_EXEC
: /* qualified exec is fine */
495 case OPT_IMPORT_STAR
:
496 PyOS_snprintf(buf
, sizeof(buf
),
497 "import * is not allowed in function '%.100s' "
499 PyString_AS_STRING(ste
->ste_name
), trailer
);
502 PyOS_snprintf(buf
, sizeof(buf
),
503 "unqualified exec is not allowed in function "
505 PyString_AS_STRING(ste
->ste_name
), trailer
);
508 PyOS_snprintf(buf
, sizeof(buf
),
509 "function '%.100s' uses import * and bare exec, "
510 "which are illegal because it %s",
511 PyString_AS_STRING(ste
->ste_name
), trailer
);
515 PyErr_SetString(PyExc_SyntaxError
, buf
);
516 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
517 ste
->ste_opt_lineno
);
521 /* Enter the final scope information into the st_symbols dict.
523 * All arguments are dicts. Modifies symbols, others are read-only.
526 update_symbols(PyObject
*symbols
, PyObject
*scope
,
527 PyObject
*bound
, PyObject
*free
, int classflag
)
529 PyObject
*name
, *v
, *u
, *w
, *free_value
= NULL
;
532 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
534 assert(PyInt_Check(v
));
535 flags
= PyInt_AS_LONG(v
);
536 w
= PyDict_GetItem(scope
, name
);
537 assert(w
&& PyInt_Check(w
));
538 i
= PyInt_AS_LONG(w
);
539 flags
|= (i
<< SCOPE_OFF
);
540 u
= PyInt_FromLong(flags
);
543 if (PyDict_SetItem(symbols
, name
, u
) < 0) {
550 free_value
= PyInt_FromLong(FREE
<< SCOPE_OFF
);
554 /* add a free variable when it's only use is for creating a closure */
556 while (PyDict_Next(free
, &pos
, &name
, &v
)) {
557 PyObject
*o
= PyDict_GetItem(symbols
, name
);
560 /* It could be a free variable in a method of
561 the class that has the same name as a local
562 or global in the class scope.
565 PyInt_AS_LONG(o
) & (DEF_BOUND
| DEF_GLOBAL
)) {
566 long i
= PyInt_AS_LONG(o
) | DEF_FREE_CLASS
;
567 o
= PyInt_FromLong(i
);
569 Py_DECREF(free_value
);
572 if (PyDict_SetItem(symbols
, name
, o
) < 0) {
574 Py_DECREF(free_value
);
579 /* else it's not free, probably a cell */
582 if (!PyDict_GetItem(bound
, name
))
583 continue; /* it's a global */
585 if (PyDict_SetItem(symbols
, name
, free_value
) < 0) {
586 Py_DECREF(free_value
);
590 Py_DECREF(free_value
);
594 /* Make final symbol table decisions for block of ste.
597 ste -- current symtable entry (input/output)
598 bound -- set of variables bound in enclosing scopes (input). bound
599 is NULL for module blocks.
600 free -- set of free variables in enclosed scopes (output)
601 globals -- set of declared global variables in enclosing scopes (input)
603 The implementation uses two mutually recursive functions,
604 analyze_block() and analyze_child_block(). analyze_block() is
605 responsible for analyzing the individual names defined in a block.
606 analyze_child_block() prepares temporary namespace dictionaries
607 used to evaluated nested blocks.
609 The two functions exist because a child block should see the name
610 bindings of its enclosing blocks, but those bindings should not
611 propagate back to a parent block.
615 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
616 PyObject
*global
, PyObject
* child_free
);
619 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
622 PyObject
*name
, *v
, *local
= NULL
, *scope
= NULL
;
623 PyObject
*newbound
= NULL
, *newglobal
= NULL
;
624 PyObject
*newfree
= NULL
, *allfree
= NULL
;
628 local
= PyDict_New(); /* collect new names bound in block */
631 scope
= PyDict_New(); /* collect scopes defined for each name */
635 /* Allocate new global and bound variable dictionaries. These
636 dictionaries hold the names visible in nested blocks. For
637 ClassBlocks, the bound and global names are initialized
638 before analyzing names, because class bindings aren't
639 visible in methods. For other blocks, they are initialized
640 after names are analyzed.
643 /* TODO(jhylton): Package these dicts in a struct so that we
644 can write reasonable helper functions?
646 newglobal
= PyDict_New();
649 newbound
= PyDict_New();
652 newfree
= PyDict_New();
656 if (ste
->ste_type
== ClassBlock
) {
657 if (PyDict_Update(newglobal
, global
) < 0)
660 if (PyDict_Update(newbound
, bound
) < 0)
664 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
665 long flags
= PyInt_AS_LONG(v
);
666 if (!analyze_name(ste
, scope
, name
, flags
,
667 bound
, local
, free
, global
))
671 if (ste
->ste_type
!= ClassBlock
) {
672 if (ste
->ste_type
== FunctionBlock
) {
673 if (PyDict_Update(newbound
, local
) < 0)
677 if (PyDict_Update(newbound
, bound
) < 0)
680 if (PyDict_Update(newglobal
, global
) < 0)
684 /* Recursively call analyze_block() on each child block.
686 newbound, newglobal now contain the names visible in
687 nested blocks. The free variables in the children will
688 be collected in allfree.
690 allfree
= PyDict_New();
693 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
694 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
695 PySTEntryObject
* entry
;
696 assert(c
&& PySTEntry_Check(c
));
697 entry
= (PySTEntryObject
*)c
;
698 if (!analyze_child_block(entry
, newbound
, newfree
, newglobal
,
701 if (entry
->ste_free
|| entry
->ste_child_free
)
702 ste
->ste_child_free
= 1;
705 if (PyDict_Update(newfree
, allfree
) < 0)
707 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scope
, newfree
))
709 if (!update_symbols(ste
->ste_symbols
, scope
, bound
, newfree
,
710 ste
->ste_type
== ClassBlock
))
712 if (!check_unoptimized(ste
))
715 if (PyDict_Update(free
, newfree
) < 0)
721 Py_XDECREF(newbound
);
722 Py_XDECREF(newglobal
);
726 assert(PyErr_Occurred());
731 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
732 PyObject
*global
, PyObject
* child_free
)
734 PyObject
*temp_bound
= NULL
, *temp_global
= NULL
, *temp_free
= NULL
;
736 /* Copy the bound and global dictionaries.
738 These dictionary are used by all blocks enclosed by the
739 current block. The analyze_block() call modifies these
743 temp_bound
= PyDict_New();
746 if (PyDict_Update(temp_bound
, bound
) < 0)
748 temp_free
= PyDict_New();
751 if (PyDict_Update(temp_free
, free
) < 0)
753 temp_global
= PyDict_New();
756 if (PyDict_Update(temp_global
, global
) < 0)
759 if (!analyze_block(entry
, temp_bound
, temp_free
, temp_global
))
761 if (PyDict_Update(child_free
, temp_free
) < 0)
763 Py_DECREF(temp_bound
);
764 Py_DECREF(temp_free
);
765 Py_DECREF(temp_global
);
768 Py_XDECREF(temp_bound
);
769 Py_XDECREF(temp_free
);
770 Py_XDECREF(temp_global
);
775 symtable_analyze(struct symtable
*st
)
777 PyObject
*free
, *global
;
783 global
= PyDict_New();
788 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
796 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
798 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
799 lineno
, NULL
, NULL
) < 0) {
800 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
801 PyErr_SetString(PyExc_SyntaxError
, msg
);
802 PyErr_SyntaxLocation(st
->st_filename
,
803 st
->st_cur
->ste_lineno
);
810 /* symtable_enter_block() gets a reference via ste_new.
811 This reference is released when the block is exited, via the DECREF
812 in symtable_exit_block().
816 symtable_exit_block(struct symtable
*st
, void *ast
)
820 Py_CLEAR(st
->st_cur
);
821 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
823 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
825 if (st
->st_cur
== NULL
)
827 Py_INCREF(st
->st_cur
);
828 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
835 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
836 void *ast
, int lineno
)
838 PySTEntryObject
*prev
= NULL
;
842 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
845 Py_DECREF(st
->st_cur
);
847 st
->st_cur
= ste_new(st
, name
, block
, ast
, lineno
);
848 if (st
->st_cur
== NULL
)
850 if (name
== GET_IDENTIFIER(top
))
851 st
->st_global
= st
->st_cur
->ste_symbols
;
853 if (PyList_Append(prev
->ste_children
,
854 (PyObject
*)st
->st_cur
) < 0) {
862 symtable_lookup(struct symtable
*st
, PyObject
*name
)
865 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
868 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
872 return PyInt_AsLong(o
);
876 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
881 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
885 dict
= st
->st_cur
->ste_symbols
;
886 if ((o
= PyDict_GetItem(dict
, mangled
))) {
887 val
= PyInt_AS_LONG(o
);
888 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
889 /* Is it better to use 'mangled' or 'name' here? */
890 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
891 PyString_AsString(name
));
892 PyErr_SyntaxLocation(st
->st_filename
,
893 st
->st_cur
->ste_lineno
);
899 o
= PyInt_FromLong(val
);
902 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
908 if (flag
& DEF_PARAM
) {
909 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
911 } else if (flag
& DEF_GLOBAL
) {
912 /* XXX need to update DEF_GLOBAL for other flags too;
913 perhaps only DEF_FREE_GLOBAL */
915 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
916 val
|= PyInt_AS_LONG(o
);
918 o
= PyInt_FromLong(val
);
921 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
935 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
936 They use the ASDL name to synthesize the name of the C type and the visit
939 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
940 useful if the first node in the sequence requires special treatment.
943 #define VISIT(ST, TYPE, V) \
944 if (!symtable_visit_ ## TYPE((ST), (V))) \
947 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
948 if (!symtable_visit_ ## TYPE((ST), (V))) { \
949 symtable_exit_block((ST), (S)); \
953 #define VISIT_SEQ(ST, TYPE, SEQ) { \
955 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
956 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
957 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
958 if (!symtable_visit_ ## TYPE((ST), elt)) \
963 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
965 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
966 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
967 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
968 if (!symtable_visit_ ## TYPE((ST), elt)) { \
969 symtable_exit_block((ST), (S)); \
975 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
977 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
978 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
979 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
980 if (!symtable_visit_ ## TYPE((ST), elt)) \
985 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
987 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
988 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
989 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
990 if (!symtable_visit_ ## TYPE((ST), elt)) { \
991 symtable_exit_block((ST), (S)); \
998 symtable_new_tmpname(struct symtable
*st
)
1003 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]",
1004 ++st
->st_cur
->ste_tmpname
);
1005 tmp
= PyString_InternFromString(tmpname
);
1008 if (!symtable_add_def(st
, tmp
, DEF_LOCAL
))
1015 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
1018 case FunctionDef_kind
:
1019 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
1021 if (s
->v
.FunctionDef
.args
->defaults
)
1022 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
1023 if (s
->v
.FunctionDef
.decorator_list
)
1024 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorator_list
);
1025 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
1026 FunctionBlock
, (void *)s
, s
->lineno
))
1028 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
1029 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
1030 if (!symtable_exit_block(st
, s
))
1033 case ClassDef_kind
: {
1035 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
1037 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
1038 if (s
->v
.ClassDef
.decorator_list
)
1039 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.decorator_list
);
1040 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
1041 (void *)s
, s
->lineno
))
1043 tmp
= st
->st_private
;
1044 st
->st_private
= s
->v
.ClassDef
.name
;
1045 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
1046 st
->st_private
= tmp
;
1047 if (!symtable_exit_block(st
, s
))
1052 if (s
->v
.Return
.value
) {
1053 VISIT(st
, expr
, s
->v
.Return
.value
);
1054 st
->st_cur
->ste_returns_value
= 1;
1055 if (st
->st_cur
->ste_generator
) {
1056 PyErr_SetString(PyExc_SyntaxError
,
1057 RETURN_VAL_IN_GENERATOR
);
1058 PyErr_SyntaxLocation(st
->st_filename
,
1065 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
1068 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
1069 VISIT(st
, expr
, s
->v
.Assign
.value
);
1071 case AugAssign_kind
:
1072 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
1073 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
1076 if (s
->v
.Print
.dest
)
1077 VISIT(st
, expr
, s
->v
.Print
.dest
);
1078 VISIT_SEQ(st
, expr
, s
->v
.Print
.values
);
1081 VISIT(st
, expr
, s
->v
.For
.target
);
1082 VISIT(st
, expr
, s
->v
.For
.iter
);
1083 VISIT_SEQ(st
, stmt
, s
->v
.For
.body
);
1084 if (s
->v
.For
.orelse
)
1085 VISIT_SEQ(st
, stmt
, s
->v
.For
.orelse
);
1088 VISIT(st
, expr
, s
->v
.While
.test
);
1089 VISIT_SEQ(st
, stmt
, s
->v
.While
.body
);
1090 if (s
->v
.While
.orelse
)
1091 VISIT_SEQ(st
, stmt
, s
->v
.While
.orelse
);
1094 /* XXX if 0: and lookup_yield() hacks */
1095 VISIT(st
, expr
, s
->v
.If
.test
);
1096 VISIT_SEQ(st
, stmt
, s
->v
.If
.body
);
1098 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
1101 if (s
->v
.Raise
.type
) {
1102 VISIT(st
, expr
, s
->v
.Raise
.type
);
1103 if (s
->v
.Raise
.inst
) {
1104 VISIT(st
, expr
, s
->v
.Raise
.inst
);
1105 if (s
->v
.Raise
.tback
)
1106 VISIT(st
, expr
, s
->v
.Raise
.tback
);
1110 case TryExcept_kind
:
1111 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
1112 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
1113 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
1115 case TryFinally_kind
:
1116 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
1117 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
1120 VISIT(st
, expr
, s
->v
.Assert
.test
);
1121 if (s
->v
.Assert
.msg
)
1122 VISIT(st
, expr
, s
->v
.Assert
.msg
);
1125 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
1126 /* XXX Don't have the lineno available inside
1128 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1129 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1131 case ImportFrom_kind
:
1132 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
1133 /* XXX Don't have the lineno available inside
1135 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1136 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1139 VISIT(st
, expr
, s
->v
.Exec
.body
);
1140 if (!st
->st_cur
->ste_opt_lineno
)
1141 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1142 if (s
->v
.Exec
.globals
) {
1143 st
->st_cur
->ste_unoptimized
|= OPT_EXEC
;
1144 VISIT(st
, expr
, s
->v
.Exec
.globals
);
1145 if (s
->v
.Exec
.locals
)
1146 VISIT(st
, expr
, s
->v
.Exec
.locals
);
1148 st
->st_cur
->ste_unoptimized
|= OPT_BARE_EXEC
;
1153 asdl_seq
*seq
= s
->v
.Global
.names
;
1154 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1155 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1156 char *c_name
= PyString_AS_STRING(name
);
1157 long cur
= symtable_lookup(st
, name
);
1160 if (cur
& (DEF_LOCAL
| USE
)) {
1162 if (cur
& DEF_LOCAL
)
1163 PyOS_snprintf(buf
, sizeof(buf
),
1164 GLOBAL_AFTER_ASSIGN
,
1167 PyOS_snprintf(buf
, sizeof(buf
),
1170 if (!symtable_warn(st
, buf
, s
->lineno
))
1173 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1179 VISIT(st
, expr
, s
->v
.Expr
.value
);
1184 /* nothing to do here */
1187 if (!symtable_new_tmpname(st
))
1189 VISIT(st
, expr
, s
->v
.With
.context_expr
);
1190 if (s
->v
.With
.optional_vars
) {
1191 if (!symtable_new_tmpname(st
))
1193 VISIT(st
, expr
, s
->v
.With
.optional_vars
);
1195 VISIT_SEQ(st
, stmt
, s
->v
.With
.body
);
1202 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1206 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1209 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1210 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1213 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1216 if (!GET_IDENTIFIER(lambda
) ||
1217 !symtable_add_def(st
, lambda
, DEF_LOCAL
))
1219 if (e
->v
.Lambda
.args
->defaults
)
1220 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1221 /* XXX how to get line numbers for expressions */
1222 if (!symtable_enter_block(st
, lambda
,
1223 FunctionBlock
, (void *)e
, 0))
1225 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1226 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1227 if (!symtable_exit_block(st
, (void *)e
))
1232 VISIT(st
, expr
, e
->v
.IfExp
.test
);
1233 VISIT(st
, expr
, e
->v
.IfExp
.body
);
1234 VISIT(st
, expr
, e
->v
.IfExp
.orelse
);
1237 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1238 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1241 if (!symtable_new_tmpname(st
))
1243 VISIT(st
, expr
, e
->v
.ListComp
.elt
);
1244 VISIT_SEQ(st
, comprehension
, e
->v
.ListComp
.generators
);
1246 case GeneratorExp_kind
:
1247 if (!symtable_visit_genexp(st
, e
))
1251 if (e
->v
.Yield
.value
)
1252 VISIT(st
, expr
, e
->v
.Yield
.value
);
1253 st
->st_cur
->ste_generator
= 1;
1254 if (st
->st_cur
->ste_returns_value
) {
1255 PyErr_SetString(PyExc_SyntaxError
,
1256 RETURN_VAL_IN_GENERATOR
);
1257 PyErr_SyntaxLocation(st
->st_filename
,
1263 VISIT(st
, expr
, e
->v
.Compare
.left
);
1264 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1267 VISIT(st
, expr
, e
->v
.Call
.func
);
1268 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1269 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1270 if (e
->v
.Call
.starargs
)
1271 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1272 if (e
->v
.Call
.kwargs
)
1273 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1276 VISIT(st
, expr
, e
->v
.Repr
.value
);
1280 /* Nothing to do here. */
1282 /* The following exprs can be assignment targets. */
1283 case Attribute_kind
:
1284 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1286 case Subscript_kind
:
1287 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1288 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1291 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1292 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1295 /* child nodes of List and Tuple will have expr_context set */
1297 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1300 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1307 symtable_implicit_arg(struct symtable
*st
, int pos
)
1309 PyObject
*id
= PyString_FromFormat(".%d", pos
);
1312 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1321 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int toplevel
)
1325 /* go through all the toplevel arguments first */
1326 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1327 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1328 if (arg
->kind
== Name_kind
) {
1329 assert(arg
->v
.Name
.ctx
== Param
||
1330 (arg
->v
.Name
.ctx
== Store
&& !toplevel
));
1331 if (!symtable_add_def(st
, arg
->v
.Name
.id
, DEF_PARAM
))
1334 else if (arg
->kind
== Tuple_kind
) {
1335 assert(arg
->v
.Tuple
.ctx
== Store
);
1337 if (!symtable_implicit_arg(st
, i
))
1342 PyErr_SetString(PyExc_SyntaxError
,
1343 "invalid expression in parameter list");
1344 PyErr_SyntaxLocation(st
->st_filename
,
1345 st
->st_cur
->ste_lineno
);
1351 if (!symtable_visit_params_nested(st
, args
))
1359 symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
)
1362 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1363 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1364 if (arg
->kind
== Tuple_kind
&&
1365 !symtable_visit_params(st
, arg
->v
.Tuple
.elts
, 0))
1373 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1375 /* skip default arguments inside function block
1376 XXX should ast be different?
1378 if (a
->args
&& !symtable_visit_params(st
, a
->args
, 1))
1381 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1383 st
->st_cur
->ste_varargs
= 1;
1386 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1388 st
->st_cur
->ste_varkeywords
= 1;
1390 if (a
->args
&& !symtable_visit_params_nested(st
, a
->args
))
1397 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1399 if (eh
->v
.ExceptHandler
.type
)
1400 VISIT(st
, expr
, eh
->v
.ExceptHandler
.type
);
1401 if (eh
->v
.ExceptHandler
.name
)
1402 VISIT(st
, expr
, eh
->v
.ExceptHandler
.name
);
1403 VISIT_SEQ(st
, stmt
, eh
->v
.ExceptHandler
.body
);
1409 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1411 /* Compute store_name, the name actually bound by the import
1412 operation. It is diferent than a->name when a->name is a
1413 dotted package name (e.g. spam.eggs)
1415 PyObject
*store_name
;
1416 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1417 const char *base
= PyString_AS_STRING(name
);
1418 char *dot
= strchr(base
, '.');
1420 store_name
= PyString_FromStringAndSize(base
, dot
- base
);
1426 Py_INCREF(store_name
);
1428 if (strcmp(PyString_AS_STRING(name
), "*")) {
1429 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1430 Py_DECREF(store_name
);
1434 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1435 int lineno
= st
->st_cur
->ste_lineno
;
1436 if (!symtable_warn(st
, IMPORT_STAR_WARNING
, lineno
)) {
1437 Py_DECREF(store_name
);
1441 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1442 Py_DECREF(store_name
);
1449 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1451 VISIT(st
, expr
, lc
->target
);
1452 VISIT(st
, expr
, lc
->iter
);
1453 VISIT_SEQ(st
, expr
, lc
->ifs
);
1459 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1461 VISIT(st
, expr
, k
->value
);
1467 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1471 if (s
->v
.Slice
.lower
)
1472 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1473 if (s
->v
.Slice
.upper
)
1474 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1475 if (s
->v
.Slice
.step
)
1476 VISIT(st
, expr
, s
->v
.Slice
.step
)
1479 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1482 VISIT(st
, expr
, s
->v
.Index
.value
)
1491 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1493 comprehension_ty outermost
= ((comprehension_ty
)
1494 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
, 0)));
1495 /* Outermost iterator is evaluated in current scope */
1496 VISIT(st
, expr
, outermost
->iter
);
1497 /* Create generator scope for the rest */
1498 if (!GET_IDENTIFIER(genexpr
) ||
1499 !symtable_enter_block(st
, genexpr
, FunctionBlock
, (void *)e
, 0)) {
1502 st
->st_cur
->ste_generator
= 1;
1503 /* Outermost iter is received as an argument */
1504 if (!symtable_implicit_arg(st
, 0)) {
1505 symtable_exit_block(st
, (void *)e
);
1508 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1509 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1510 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1511 e
->v
.GeneratorExp
.generators
, 1, (void*)e
);
1512 VISIT_IN_BLOCK(st
, expr
, e
->v
.GeneratorExp
.elt
, (void*)e
);
1513 return symtable_exit_block(st
, (void *)e
);