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;
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 {"optimized",T_INT
, OFF(ste_unoptimized
), READONLY
},
115 {"nested", T_INT
, OFF(ste_nested
), READONLY
},
116 {"type", T_INT
, OFF(ste_type
), READONLY
},
117 {"lineno", T_INT
, OFF(ste_lineno
), READONLY
},
121 PyTypeObject PySTEntry_Type
= {
122 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
124 sizeof(PySTEntryObject
),
126 (destructor
)ste_dealloc
, /* tp_dealloc */
131 (reprfunc
)ste_repr
, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
138 PyObject_GenericGetAttr
, /* tp_getattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT
, /* tp_flags */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
150 ste_memberlist
, /* tp_members */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
162 static int symtable_analyze(struct symtable
*st
);
163 static int symtable_warn(struct symtable
*st
, char *msg
, int lineno
);
164 static int symtable_enter_block(struct symtable
*st
, identifier name
,
165 _Py_block_ty block
, void *ast
, int lineno
);
166 static int symtable_exit_block(struct symtable
*st
, void *ast
);
167 static int symtable_visit_stmt(struct symtable
*st
, stmt_ty s
);
168 static int symtable_visit_expr(struct symtable
*st
, expr_ty s
);
169 static int symtable_visit_genexp(struct symtable
*st
, expr_ty s
);
170 static int symtable_visit_setcomp(struct symtable
*st
, expr_ty e
);
171 static int symtable_visit_dictcomp(struct symtable
*st
, expr_ty e
);
172 static int symtable_visit_arguments(struct symtable
*st
, arguments_ty
);
173 static int symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty
);
174 static int symtable_visit_alias(struct symtable
*st
, alias_ty
);
175 static int symtable_visit_comprehension(struct symtable
*st
, comprehension_ty
);
176 static int symtable_visit_keyword(struct symtable
*st
, keyword_ty
);
177 static int symtable_visit_slice(struct symtable
*st
, slice_ty
);
178 static int symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int top
);
179 static int symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
);
180 static int symtable_implicit_arg(struct symtable
*st
, int pos
);
183 static identifier top
= NULL
, lambda
= NULL
, genexpr
= NULL
, setcomp
= NULL
,
186 #define GET_IDENTIFIER(VAR) \
187 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
189 #define DUPLICATE_ARGUMENT \
190 "duplicate argument '%s' in function definition"
192 static struct symtable
*
197 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
201 st
->st_filename
= NULL
;
202 st
->st_symbols
= NULL
;
204 if ((st
->st_stack
= PyList_New(0)) == NULL
)
206 if ((st
->st_symbols
= PyDict_New()) == NULL
)
209 st
->st_private
= NULL
;
217 PySymtable_Build(mod_ty mod
, const char *filename
, PyFutureFeatures
*future
)
219 struct symtable
*st
= symtable_new();
225 st
->st_filename
= filename
;
226 st
->st_future
= future
;
227 if (!GET_IDENTIFIER(top
) ||
228 !symtable_enter_block(st
, top
, ModuleBlock
, (void *)mod
, 0)) {
233 st
->st_top
= st
->st_cur
;
234 st
->st_cur
->ste_unoptimized
= OPT_TOPLEVEL
;
235 /* Any other top-level initialization? */
238 seq
= mod
->v
.Module
.body
;
239 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
240 if (!symtable_visit_stmt(st
,
241 (stmt_ty
)asdl_seq_GET(seq
, i
)))
244 case Expression_kind
:
245 if (!symtable_visit_expr(st
, mod
->v
.Expression
.body
))
248 case Interactive_kind
:
249 seq
= mod
->v
.Interactive
.body
;
250 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
251 if (!symtable_visit_stmt(st
,
252 (stmt_ty
)asdl_seq_GET(seq
, i
)))
256 PyErr_SetString(PyExc_RuntimeError
,
257 "this compiler does not handle Suites");
260 if (!symtable_exit_block(st
, (void *)mod
)) {
264 if (symtable_analyze(st
))
269 (void) symtable_exit_block(st
, (void *)mod
);
275 PySymtable_Free(struct symtable
*st
)
277 Py_XDECREF(st
->st_symbols
);
278 Py_XDECREF(st
->st_stack
);
279 PyMem_Free((void *)st
);
283 PySymtable_Lookup(struct symtable
*st
, void *key
)
287 k
= PyLong_FromVoidPtr(key
);
290 v
= PyDict_GetItem(st
->st_symbols
, k
);
292 assert(PySTEntry_Check(v
));
296 PyErr_SetString(PyExc_KeyError
,
297 "unknown symbol table entry");
301 return (PySTEntryObject
*)v
;
305 PyST_GetScope(PySTEntryObject
*ste
, PyObject
*name
)
307 PyObject
*v
= PyDict_GetItem(ste
->ste_symbols
, name
);
310 assert(PyInt_Check(v
));
311 return (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
315 /* Analyze raw symbol information to determine scope of each name.
317 The next several functions are helpers for PySymtable_Analyze(),
318 which determines whether a name is local, global, or free. In addition,
319 it determines which local variables are cell variables; they provide
320 bindings that are used for free variables in enclosed blocks.
322 There are also two kinds of free variables, implicit and explicit. An
323 explicit global is declared with the global statement. An implicit
324 global is a free variable for which the compiler has found no binding
325 in an enclosing function scope. The implicit global is either a global
326 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
327 to handle these names to implement slightly odd semantics. In such a
328 block, the name is treated as global until it is assigned to; then it
329 is treated as a local.
331 The symbol table requires two passes to determine the scope of each name.
332 The first pass collects raw facts from the AST: the name is a parameter
333 here, the name is used by not defined here, etc. The second pass analyzes
334 these facts during a pass over the PySTEntryObjects created during pass 1.
336 When a function is entered during the second pass, the parent passes
337 the set of all name bindings visible to its children. These bindings
338 are used to determine if the variable is free or an implicit global.
339 After doing the local analysis, it analyzes each of its child blocks
340 using an updated set of name bindings.
342 The children update the free variable set. If a local variable is free
343 in a child, the variable is marked as a cell. The current function must
344 provide runtime storage for the variable that may outlive the function's
345 frame. Cell variables are removed from the free set before the analyze
346 function returns to its parent.
348 The sets of bound and free variables are implemented as dictionaries
349 mapping strings to None.
352 #define SET_SCOPE(DICT, NAME, I) { \
353 PyObject *o = PyInt_FromLong(I); \
356 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
363 /* Decide on scope of name, given flags.
365 The namespace dictionaries may be modified to record information
366 about the new name. For example, a new global will add an entry to
367 global. A name that was global can be changed to local.
371 analyze_name(PySTEntryObject
*ste
, PyObject
*dict
, PyObject
*name
, long flags
,
372 PyObject
*bound
, PyObject
*local
, PyObject
*free
,
375 if (flags
& DEF_GLOBAL
) {
376 if (flags
& DEF_PARAM
) {
377 PyErr_Format(PyExc_SyntaxError
,
378 "name '%s' is local and global",
379 PyString_AS_STRING(name
));
380 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
385 SET_SCOPE(dict
, name
, GLOBAL_EXPLICIT
);
386 if (PyDict_SetItem(global
, name
, Py_None
) < 0)
388 if (bound
&& PyDict_GetItem(bound
, name
)) {
389 if (PyDict_DelItem(bound
, name
) < 0)
394 if (flags
& DEF_BOUND
) {
395 SET_SCOPE(dict
, name
, LOCAL
);
396 if (PyDict_SetItem(local
, name
, Py_None
) < 0)
398 if (PyDict_GetItem(global
, name
)) {
399 if (PyDict_DelItem(global
, name
) < 0)
404 /* If an enclosing block has a binding for this name, it
405 is a free variable rather than a global variable.
406 Note that having a non-NULL bound implies that the block
409 if (bound
&& PyDict_GetItem(bound
, name
)) {
410 SET_SCOPE(dict
, name
, FREE
);
412 if (PyDict_SetItem(free
, name
, Py_None
) < 0)
416 /* If a parent has a global statement, then call it global
417 explicit? It could also be global implicit.
419 else if (global
&& PyDict_GetItem(global
, name
)) {
420 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
426 SET_SCOPE(dict
, name
, GLOBAL_IMPLICIT
);
429 /* Should never get here. */
430 PyErr_Format(PyExc_SystemError
, "failed to set scope for %s",
431 PyString_AS_STRING(name
));
437 /* If a name is defined in free and also in locals, then this block
438 provides the binding for the free variable. The name should be
439 marked CELL in this block and removed from the free list.
441 Note that the current block's free variables are included in free.
442 That's safe because no name can be free and local in the same scope.
446 analyze_cells(PyObject
*scope
, PyObject
*free
)
448 PyObject
*name
, *v
, *w
;
452 w
= PyInt_FromLong(CELL
);
455 while (PyDict_Next(scope
, &pos
, &name
, &v
)) {
457 assert(PyInt_Check(v
));
458 flags
= PyInt_AS_LONG(v
);
461 if (!PyDict_GetItem(free
, name
))
463 /* Replace LOCAL with CELL for this name, and remove
464 from free. It is safe to replace the value of name
465 in the dict, because it will not cause a resize.
467 if (PyDict_SetItem(scope
, name
, w
) < 0)
469 if (!PyDict_DelItem(free
, name
) < 0)
478 /* Check for illegal statements in unoptimized namespaces */
480 check_unoptimized(const PySTEntryObject
* ste
) {
484 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
485 || !(ste
->ste_free
|| ste
->ste_child_free
))
488 trailer
= (ste
->ste_child_free
?
489 "contains a nested function with free variables" :
490 "is a nested function");
492 switch (ste
->ste_unoptimized
) {
493 case OPT_TOPLEVEL
: /* exec / import * at top-level is fine */
494 case OPT_EXEC
: /* qualified exec is fine */
496 case OPT_IMPORT_STAR
:
497 PyOS_snprintf(buf
, sizeof(buf
),
498 "import * is not allowed in function '%.100s' "
500 PyString_AS_STRING(ste
->ste_name
), trailer
);
503 PyOS_snprintf(buf
, sizeof(buf
),
504 "unqualified exec is not allowed in function "
506 PyString_AS_STRING(ste
->ste_name
), trailer
);
509 PyOS_snprintf(buf
, sizeof(buf
),
510 "function '%.100s' uses import * and bare exec, "
511 "which are illegal because it %s",
512 PyString_AS_STRING(ste
->ste_name
), trailer
);
516 PyErr_SetString(PyExc_SyntaxError
, buf
);
517 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
518 ste
->ste_opt_lineno
);
522 /* Enter the final scope information into the st_symbols dict.
524 * All arguments are dicts. Modifies symbols, others are read-only.
527 update_symbols(PyObject
*symbols
, PyObject
*scope
,
528 PyObject
*bound
, PyObject
*free
, int classflag
)
530 PyObject
*name
, *v
, *u
, *w
, *free_value
= NULL
;
533 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
535 assert(PyInt_Check(v
));
536 flags
= PyInt_AS_LONG(v
);
537 w
= PyDict_GetItem(scope
, name
);
538 assert(w
&& PyInt_Check(w
));
539 i
= PyInt_AS_LONG(w
);
540 flags
|= (i
<< SCOPE_OFF
);
541 u
= PyInt_FromLong(flags
);
544 if (PyDict_SetItem(symbols
, name
, u
) < 0) {
551 free_value
= PyInt_FromLong(FREE
<< SCOPE_OFF
);
555 /* add a free variable when it's only use is for creating a closure */
557 while (PyDict_Next(free
, &pos
, &name
, &v
)) {
558 PyObject
*o
= PyDict_GetItem(symbols
, name
);
561 /* It could be a free variable in a method of
562 the class that has the same name as a local
563 or global in the class scope.
566 PyInt_AS_LONG(o
) & (DEF_BOUND
| DEF_GLOBAL
)) {
567 long i
= PyInt_AS_LONG(o
) | DEF_FREE_CLASS
;
568 o
= PyInt_FromLong(i
);
570 Py_DECREF(free_value
);
573 if (PyDict_SetItem(symbols
, name
, o
) < 0) {
575 Py_DECREF(free_value
);
580 /* else it's not free, probably a cell */
583 if (!PyDict_GetItem(bound
, name
))
584 continue; /* it's a global */
586 if (PyDict_SetItem(symbols
, name
, free_value
) < 0) {
587 Py_DECREF(free_value
);
591 Py_DECREF(free_value
);
595 /* Make final symbol table decisions for block of ste.
598 ste -- current symtable entry (input/output)
599 bound -- set of variables bound in enclosing scopes (input). bound
600 is NULL for module blocks.
601 free -- set of free variables in enclosed scopes (output)
602 globals -- set of declared global variables in enclosing scopes (input)
604 The implementation uses two mutually recursive functions,
605 analyze_block() and analyze_child_block(). analyze_block() is
606 responsible for analyzing the individual names defined in a block.
607 analyze_child_block() prepares temporary namespace dictionaries
608 used to evaluated nested blocks.
610 The two functions exist because a child block should see the name
611 bindings of its enclosing blocks, but those bindings should not
612 propagate back to a parent block.
616 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
617 PyObject
*global
, PyObject
* child_free
);
620 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
623 PyObject
*name
, *v
, *local
= NULL
, *scope
= NULL
;
624 PyObject
*newbound
= NULL
, *newglobal
= NULL
;
625 PyObject
*newfree
= NULL
, *allfree
= NULL
;
629 local
= PyDict_New(); /* collect new names bound in block */
632 scope
= PyDict_New(); /* collect scopes defined for each name */
636 /* Allocate new global and bound variable dictionaries. These
637 dictionaries hold the names visible in nested blocks. For
638 ClassBlocks, the bound and global names are initialized
639 before analyzing names, because class bindings aren't
640 visible in methods. For other blocks, they are initialized
641 after names are analyzed.
644 /* TODO(jhylton): Package these dicts in a struct so that we
645 can write reasonable helper functions?
647 newglobal
= PyDict_New();
650 newbound
= PyDict_New();
653 newfree
= PyDict_New();
657 if (ste
->ste_type
== ClassBlock
) {
658 if (PyDict_Update(newglobal
, global
) < 0)
661 if (PyDict_Update(newbound
, bound
) < 0)
665 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
666 long flags
= PyInt_AS_LONG(v
);
667 if (!analyze_name(ste
, scope
, name
, flags
,
668 bound
, local
, free
, global
))
672 if (ste
->ste_type
!= ClassBlock
) {
673 if (ste
->ste_type
== FunctionBlock
) {
674 if (PyDict_Update(newbound
, local
) < 0)
678 if (PyDict_Update(newbound
, bound
) < 0)
681 if (PyDict_Update(newglobal
, global
) < 0)
685 /* Recursively call analyze_block() on each child block.
687 newbound, newglobal now contain the names visible in
688 nested blocks. The free variables in the children will
689 be collected in allfree.
691 allfree
= PyDict_New();
694 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
695 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
696 PySTEntryObject
* entry
;
697 assert(c
&& PySTEntry_Check(c
));
698 entry
= (PySTEntryObject
*)c
;
699 if (!analyze_child_block(entry
, newbound
, newfree
, newglobal
,
702 if (entry
->ste_free
|| entry
->ste_child_free
)
703 ste
->ste_child_free
= 1;
706 if (PyDict_Update(newfree
, allfree
) < 0)
708 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scope
, newfree
))
710 if (!update_symbols(ste
->ste_symbols
, scope
, bound
, newfree
,
711 ste
->ste_type
== ClassBlock
))
713 if (!check_unoptimized(ste
))
716 if (PyDict_Update(free
, newfree
) < 0)
722 Py_XDECREF(newbound
);
723 Py_XDECREF(newglobal
);
727 assert(PyErr_Occurred());
732 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
733 PyObject
*global
, PyObject
* child_free
)
735 PyObject
*temp_bound
= NULL
, *temp_global
= NULL
, *temp_free
= NULL
;
737 /* Copy the bound and global dictionaries.
739 These dictionary are used by all blocks enclosed by the
740 current block. The analyze_block() call modifies these
744 temp_bound
= PyDict_New();
747 if (PyDict_Update(temp_bound
, bound
) < 0)
749 temp_free
= PyDict_New();
752 if (PyDict_Update(temp_free
, free
) < 0)
754 temp_global
= PyDict_New();
757 if (PyDict_Update(temp_global
, global
) < 0)
760 if (!analyze_block(entry
, temp_bound
, temp_free
, temp_global
))
762 if (PyDict_Update(child_free
, temp_free
) < 0)
764 Py_DECREF(temp_bound
);
765 Py_DECREF(temp_free
);
766 Py_DECREF(temp_global
);
769 Py_XDECREF(temp_bound
);
770 Py_XDECREF(temp_free
);
771 Py_XDECREF(temp_global
);
776 symtable_analyze(struct symtable
*st
)
778 PyObject
*free
, *global
;
784 global
= PyDict_New();
789 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
797 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
799 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
800 lineno
, NULL
, NULL
) < 0) {
801 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
802 PyErr_SetString(PyExc_SyntaxError
, msg
);
803 PyErr_SyntaxLocation(st
->st_filename
,
804 st
->st_cur
->ste_lineno
);
811 /* symtable_enter_block() gets a reference via ste_new.
812 This reference is released when the block is exited, via the DECREF
813 in symtable_exit_block().
817 symtable_exit_block(struct symtable
*st
, void *ast
)
821 Py_CLEAR(st
->st_cur
);
822 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
824 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
826 if (st
->st_cur
== NULL
)
828 Py_INCREF(st
->st_cur
);
829 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
836 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
837 void *ast
, int lineno
)
839 PySTEntryObject
*prev
= NULL
;
843 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
846 Py_DECREF(st
->st_cur
);
848 st
->st_cur
= ste_new(st
, name
, block
, ast
, lineno
);
849 if (st
->st_cur
== NULL
)
851 if (block
== ModuleBlock
)
852 st
->st_global
= st
->st_cur
->ste_symbols
;
854 if (PyList_Append(prev
->ste_children
,
855 (PyObject
*)st
->st_cur
) < 0) {
863 symtable_lookup(struct symtable
*st
, PyObject
*name
)
866 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
869 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
873 return PyInt_AsLong(o
);
877 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
882 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
886 dict
= st
->st_cur
->ste_symbols
;
887 if ((o
= PyDict_GetItem(dict
, mangled
))) {
888 val
= PyInt_AS_LONG(o
);
889 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
890 /* Is it better to use 'mangled' or 'name' here? */
891 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
892 PyString_AsString(name
));
893 PyErr_SyntaxLocation(st
->st_filename
,
894 st
->st_cur
->ste_lineno
);
900 o
= PyInt_FromLong(val
);
903 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
909 if (flag
& DEF_PARAM
) {
910 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
912 } else if (flag
& DEF_GLOBAL
) {
913 /* XXX need to update DEF_GLOBAL for other flags too;
914 perhaps only DEF_FREE_GLOBAL */
916 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
917 val
|= PyInt_AS_LONG(o
);
919 o
= PyInt_FromLong(val
);
922 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
936 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
937 They use the ASDL name to synthesize the name of the C type and the visit
940 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
941 useful if the first node in the sequence requires special treatment.
944 #define VISIT(ST, TYPE, V) \
945 if (!symtable_visit_ ## TYPE((ST), (V))) \
948 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
949 if (!symtable_visit_ ## TYPE((ST), (V))) { \
950 symtable_exit_block((ST), (S)); \
954 #define VISIT_SEQ(ST, TYPE, SEQ) { \
956 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
957 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
958 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
959 if (!symtable_visit_ ## TYPE((ST), elt)) \
964 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
966 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
967 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
968 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
969 if (!symtable_visit_ ## TYPE((ST), elt)) { \
970 symtable_exit_block((ST), (S)); \
976 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
978 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
979 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
980 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
981 if (!symtable_visit_ ## TYPE((ST), elt)) \
986 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
988 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
989 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
990 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
991 if (!symtable_visit_ ## TYPE((ST), elt)) { \
992 symtable_exit_block((ST), (S)); \
999 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
1002 case FunctionDef_kind
:
1003 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
1005 if (s
->v
.FunctionDef
.args
->defaults
)
1006 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
1007 if (s
->v
.FunctionDef
.decorator_list
)
1008 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorator_list
);
1009 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
1010 FunctionBlock
, (void *)s
, s
->lineno
))
1012 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
1013 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
1014 if (!symtable_exit_block(st
, s
))
1017 case ClassDef_kind
: {
1019 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
1021 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
1022 if (s
->v
.ClassDef
.decorator_list
)
1023 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.decorator_list
);
1024 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
1025 (void *)s
, s
->lineno
))
1027 tmp
= st
->st_private
;
1028 st
->st_private
= s
->v
.ClassDef
.name
;
1029 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
1030 st
->st_private
= tmp
;
1031 if (!symtable_exit_block(st
, s
))
1036 if (s
->v
.Return
.value
) {
1037 VISIT(st
, expr
, s
->v
.Return
.value
);
1038 st
->st_cur
->ste_returns_value
= 1;
1039 if (st
->st_cur
->ste_generator
) {
1040 PyErr_SetString(PyExc_SyntaxError
,
1041 RETURN_VAL_IN_GENERATOR
);
1042 PyErr_SyntaxLocation(st
->st_filename
,
1049 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
1052 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
1053 VISIT(st
, expr
, s
->v
.Assign
.value
);
1055 case AugAssign_kind
:
1056 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
1057 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
1060 if (s
->v
.Print
.dest
)
1061 VISIT(st
, expr
, s
->v
.Print
.dest
);
1062 VISIT_SEQ(st
, expr
, s
->v
.Print
.values
);
1065 VISIT(st
, expr
, s
->v
.For
.target
);
1066 VISIT(st
, expr
, s
->v
.For
.iter
);
1067 VISIT_SEQ(st
, stmt
, s
->v
.For
.body
);
1068 if (s
->v
.For
.orelse
)
1069 VISIT_SEQ(st
, stmt
, s
->v
.For
.orelse
);
1072 VISIT(st
, expr
, s
->v
.While
.test
);
1073 VISIT_SEQ(st
, stmt
, s
->v
.While
.body
);
1074 if (s
->v
.While
.orelse
)
1075 VISIT_SEQ(st
, stmt
, s
->v
.While
.orelse
);
1078 /* XXX if 0: and lookup_yield() hacks */
1079 VISIT(st
, expr
, s
->v
.If
.test
);
1080 VISIT_SEQ(st
, stmt
, s
->v
.If
.body
);
1082 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
1085 if (s
->v
.Raise
.type
) {
1086 VISIT(st
, expr
, s
->v
.Raise
.type
);
1087 if (s
->v
.Raise
.inst
) {
1088 VISIT(st
, expr
, s
->v
.Raise
.inst
);
1089 if (s
->v
.Raise
.tback
)
1090 VISIT(st
, expr
, s
->v
.Raise
.tback
);
1094 case TryExcept_kind
:
1095 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
1096 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
1097 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
1099 case TryFinally_kind
:
1100 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
1101 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
1104 VISIT(st
, expr
, s
->v
.Assert
.test
);
1105 if (s
->v
.Assert
.msg
)
1106 VISIT(st
, expr
, s
->v
.Assert
.msg
);
1109 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
1110 /* XXX Don't have the lineno available inside
1112 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1113 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1115 case ImportFrom_kind
:
1116 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
1117 /* XXX Don't have the lineno available inside
1119 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1120 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1123 VISIT(st
, expr
, s
->v
.Exec
.body
);
1124 if (!st
->st_cur
->ste_opt_lineno
)
1125 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1126 if (s
->v
.Exec
.globals
) {
1127 st
->st_cur
->ste_unoptimized
|= OPT_EXEC
;
1128 VISIT(st
, expr
, s
->v
.Exec
.globals
);
1129 if (s
->v
.Exec
.locals
)
1130 VISIT(st
, expr
, s
->v
.Exec
.locals
);
1132 st
->st_cur
->ste_unoptimized
|= OPT_BARE_EXEC
;
1137 asdl_seq
*seq
= s
->v
.Global
.names
;
1138 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1139 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1140 char *c_name
= PyString_AS_STRING(name
);
1141 long cur
= symtable_lookup(st
, name
);
1144 if (cur
& (DEF_LOCAL
| USE
)) {
1146 if (cur
& DEF_LOCAL
)
1147 PyOS_snprintf(buf
, sizeof(buf
),
1148 GLOBAL_AFTER_ASSIGN
,
1151 PyOS_snprintf(buf
, sizeof(buf
),
1154 if (!symtable_warn(st
, buf
, s
->lineno
))
1157 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1163 VISIT(st
, expr
, s
->v
.Expr
.value
);
1168 /* nothing to do here */
1171 VISIT(st
, expr
, s
->v
.With
.context_expr
);
1172 if (s
->v
.With
.optional_vars
) {
1173 VISIT(st
, expr
, s
->v
.With
.optional_vars
);
1175 VISIT_SEQ(st
, stmt
, s
->v
.With
.body
);
1182 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1186 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1189 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1190 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1193 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1196 if (!GET_IDENTIFIER(lambda
))
1198 if (e
->v
.Lambda
.args
->defaults
)
1199 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1200 if (!symtable_enter_block(st
, lambda
,
1201 FunctionBlock
, (void *)e
, e
->lineno
))
1203 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1204 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1205 if (!symtable_exit_block(st
, (void *)e
))
1210 VISIT(st
, expr
, e
->v
.IfExp
.test
);
1211 VISIT(st
, expr
, e
->v
.IfExp
.body
);
1212 VISIT(st
, expr
, e
->v
.IfExp
.orelse
);
1215 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1216 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1219 VISIT_SEQ(st
, expr
, e
->v
.Set
.elts
);
1222 VISIT(st
, expr
, e
->v
.ListComp
.elt
);
1223 VISIT_SEQ(st
, comprehension
, e
->v
.ListComp
.generators
);
1225 case GeneratorExp_kind
:
1226 if (!symtable_visit_genexp(st
, e
))
1230 if (!symtable_visit_setcomp(st
, e
))
1234 if (!symtable_visit_dictcomp(st
, e
))
1238 if (e
->v
.Yield
.value
)
1239 VISIT(st
, expr
, e
->v
.Yield
.value
);
1240 st
->st_cur
->ste_generator
= 1;
1241 if (st
->st_cur
->ste_returns_value
) {
1242 PyErr_SetString(PyExc_SyntaxError
,
1243 RETURN_VAL_IN_GENERATOR
);
1244 PyErr_SyntaxLocation(st
->st_filename
,
1250 VISIT(st
, expr
, e
->v
.Compare
.left
);
1251 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1254 VISIT(st
, expr
, e
->v
.Call
.func
);
1255 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1256 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1257 if (e
->v
.Call
.starargs
)
1258 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1259 if (e
->v
.Call
.kwargs
)
1260 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1263 VISIT(st
, expr
, e
->v
.Repr
.value
);
1267 /* Nothing to do here. */
1269 /* The following exprs can be assignment targets. */
1270 case Attribute_kind
:
1271 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1273 case Subscript_kind
:
1274 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1275 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1278 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1279 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1282 /* child nodes of List and Tuple will have expr_context set */
1284 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1287 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1294 symtable_implicit_arg(struct symtable
*st
, int pos
)
1296 PyObject
*id
= PyString_FromFormat(".%d", pos
);
1299 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1308 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
, int toplevel
)
1312 /* go through all the toplevel arguments first */
1313 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1314 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1315 if (arg
->kind
== Name_kind
) {
1316 assert(arg
->v
.Name
.ctx
== Param
||
1317 (arg
->v
.Name
.ctx
== Store
&& !toplevel
));
1318 if (!symtable_add_def(st
, arg
->v
.Name
.id
, DEF_PARAM
))
1321 else if (arg
->kind
== Tuple_kind
) {
1322 assert(arg
->v
.Tuple
.ctx
== Store
);
1324 if (!symtable_implicit_arg(st
, i
))
1329 PyErr_SetString(PyExc_SyntaxError
,
1330 "invalid expression in parameter list");
1331 PyErr_SyntaxLocation(st
->st_filename
,
1332 st
->st_cur
->ste_lineno
);
1338 if (!symtable_visit_params_nested(st
, args
))
1346 symtable_visit_params_nested(struct symtable
*st
, asdl_seq
*args
)
1349 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1350 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
, i
);
1351 if (arg
->kind
== Tuple_kind
&&
1352 !symtable_visit_params(st
, arg
->v
.Tuple
.elts
, 0))
1360 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1362 /* skip default arguments inside function block
1363 XXX should ast be different?
1365 if (a
->args
&& !symtable_visit_params(st
, a
->args
, 1))
1368 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1370 st
->st_cur
->ste_varargs
= 1;
1373 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1375 st
->st_cur
->ste_varkeywords
= 1;
1377 if (a
->args
&& !symtable_visit_params_nested(st
, a
->args
))
1384 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1386 if (eh
->v
.ExceptHandler
.type
)
1387 VISIT(st
, expr
, eh
->v
.ExceptHandler
.type
);
1388 if (eh
->v
.ExceptHandler
.name
)
1389 VISIT(st
, expr
, eh
->v
.ExceptHandler
.name
);
1390 VISIT_SEQ(st
, stmt
, eh
->v
.ExceptHandler
.body
);
1396 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1398 /* Compute store_name, the name actually bound by the import
1399 operation. It is different than a->name when a->name is a
1400 dotted package name (e.g. spam.eggs)
1402 PyObject
*store_name
;
1403 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1404 const char *base
= PyString_AS_STRING(name
);
1405 char *dot
= strchr(base
, '.');
1407 store_name
= PyString_FromStringAndSize(base
, dot
- base
);
1413 Py_INCREF(store_name
);
1415 if (strcmp(PyString_AS_STRING(name
), "*")) {
1416 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1417 Py_DECREF(store_name
);
1421 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1422 int lineno
= st
->st_cur
->ste_lineno
;
1423 if (!symtable_warn(st
, IMPORT_STAR_WARNING
, lineno
)) {
1424 Py_DECREF(store_name
);
1428 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1429 Py_DECREF(store_name
);
1436 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1438 VISIT(st
, expr
, lc
->target
);
1439 VISIT(st
, expr
, lc
->iter
);
1440 VISIT_SEQ(st
, expr
, lc
->ifs
);
1446 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1448 VISIT(st
, expr
, k
->value
);
1454 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1458 if (s
->v
.Slice
.lower
)
1459 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1460 if (s
->v
.Slice
.upper
)
1461 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1462 if (s
->v
.Slice
.step
)
1463 VISIT(st
, expr
, s
->v
.Slice
.step
)
1466 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1469 VISIT(st
, expr
, s
->v
.Index
.value
)
1478 symtable_new_tmpname(struct symtable
*st
)
1483 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]",
1484 ++st
->st_cur
->ste_tmpname
);
1485 tmp
= PyString_InternFromString(tmpname
);
1488 if (!symtable_add_def(st
, tmp
, DEF_LOCAL
))
1495 symtable_handle_comprehension(struct symtable
*st
, expr_ty e
,
1496 identifier scope_name
, asdl_seq
*generators
,
1497 expr_ty elt
, expr_ty value
)
1499 int is_generator
= (e
->kind
== GeneratorExp_kind
);
1500 int needs_tmp
= !is_generator
;
1501 comprehension_ty outermost
= ((comprehension_ty
)
1502 asdl_seq_GET(generators
, 0));
1503 /* Outermost iterator is evaluated in current scope */
1504 VISIT(st
, expr
, outermost
->iter
);
1505 /* Create comprehension scope for the rest */
1507 !symtable_enter_block(st
, scope_name
, FunctionBlock
, (void *)e
, 0)) {
1510 st
->st_cur
->ste_generator
= is_generator
;
1511 /* Outermost iter is received as an argument */
1512 if (!symtable_implicit_arg(st
, 0)) {
1513 symtable_exit_block(st
, (void *)e
);
1516 /* Allocate temporary name if needed */
1517 if (needs_tmp
&& !symtable_new_tmpname(st
)) {
1518 symtable_exit_block(st
, (void *)e
);
1521 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1522 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1523 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1524 generators
, 1, (void*)e
);
1526 VISIT_IN_BLOCK(st
, expr
, value
, (void*)e
);
1527 VISIT_IN_BLOCK(st
, expr
, elt
, (void*)e
);
1528 return symtable_exit_block(st
, (void *)e
);
1532 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1534 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(genexpr
),
1535 e
->v
.GeneratorExp
.generators
,
1536 e
->v
.GeneratorExp
.elt
, NULL
);
1540 symtable_visit_setcomp(struct symtable
*st
, expr_ty e
)
1542 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(setcomp
),
1543 e
->v
.SetComp
.generators
,
1544 e
->v
.SetComp
.elt
, NULL
);
1548 symtable_visit_dictcomp(struct symtable
*st
, expr_ty e
)
1550 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(dictcomp
),
1551 e
->v
.DictComp
.generators
,
1553 e
->v
.DictComp
.value
);