symtable.c revision 46b7bda9bcd0fb11878a154234c3064e19e35f3c
1#include "Python.h"
2#include "Python-ast.h"
3#include "code.h"
4#include "symtable.h"
5#include "structmember.h"
6
7/* error strings used for warnings */
8#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
10
11#define GLOBAL_AFTER_USE \
12"name '%.400s' is used prior to global declaration"
13
14#define IMPORT_STAR_WARNING "import * only allowed at module level"
15
16
17PySTEntryObject *
18PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
19	      void *key, int lineno)
20{
21	PySTEntryObject *ste = NULL;
22	PyObject *k;
23
24	k = PyLong_FromVoidPtr(key);
25	if (k == NULL)
26		goto fail;
27	ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
28					      &PySTEntry_Type);
29	ste->ste_table = st;
30	ste->ste_id = k;
31	ste->ste_tmpname = 0;
32
33	ste->ste_name = name;
34	Py_INCREF(name);
35
36	ste->ste_symbols = NULL;
37	ste->ste_varnames = NULL;
38	ste->ste_children = NULL;
39
40	ste->ste_symbols = PyDict_New();
41	if (ste->ste_symbols == NULL)
42	    goto fail;
43
44	ste->ste_varnames = PyList_New(0);
45	if (ste->ste_varnames == NULL)
46	    goto fail;
47
48	ste->ste_children = PyList_New(0);
49	if (ste->ste_children == NULL)
50	    goto fail;
51
52	ste->ste_type = block;
53	ste->ste_unoptimized = 0;
54	ste->ste_nested = 0;
55	ste->ste_free = 0;
56	ste->ste_varargs = 0;
57	ste->ste_varkeywords = 0;
58	ste->ste_opt_lineno = 0;
59	ste->ste_tmpname = 0;
60	ste->ste_lineno = lineno;
61
62	if (st->st_cur != NULL &&
63	    (st->st_cur->ste_nested ||
64	     st->st_cur->ste_type == FunctionBlock))
65		ste->ste_nested = 1;
66	ste->ste_child_free = 0;
67	ste->ste_generator = 0;
68
69	if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
70	    goto fail;
71
72	return ste;
73 fail:
74	Py_XDECREF(ste);
75	return NULL;
76}
77
78static PyObject *
79ste_repr(PySTEntryObject *ste)
80{
81	char buf[256];
82
83	PyOS_snprintf(buf, sizeof(buf),
84		      "<symtable entry %.100s(%ld), line %d>",
85		      PyString_AS_STRING(ste->ste_name),
86		      PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
87	return PyString_FromString(buf);
88}
89
90static void
91ste_dealloc(PySTEntryObject *ste)
92{
93	ste->ste_table = NULL;
94	Py_XDECREF(ste->ste_id);
95	Py_XDECREF(ste->ste_name);
96	Py_XDECREF(ste->ste_symbols);
97	Py_XDECREF(ste->ste_varnames);
98	Py_XDECREF(ste->ste_children);
99	PyObject_Del(ste);
100}
101
102#define OFF(x) offsetof(PySTEntryObject, x)
103
104static PyMemberDef ste_memberlist[] = {
105	{"id",       T_OBJECT, OFF(ste_id), READONLY},
106	{"name",     T_OBJECT, OFF(ste_name), READONLY},
107	{"symbols",  T_OBJECT, OFF(ste_symbols), READONLY},
108	{"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
109	{"children", T_OBJECT, OFF(ste_children), READONLY},
110	{"type",     T_INT,    OFF(ste_type), READONLY},
111	{"lineno",   T_INT,    OFF(ste_lineno), READONLY},
112	{NULL}
113};
114
115PyTypeObject PySTEntry_Type = {
116	PyObject_HEAD_INIT(&PyType_Type)
117	0,
118	"symtable entry",
119	sizeof(PySTEntryObject),
120	0,
121	(destructor)ste_dealloc,                /* tp_dealloc */
122	0,                                      /* tp_print */
123	0,			               /* tp_getattr */
124	0,					/* tp_setattr */
125	0,			                /* tp_compare */
126	(reprfunc)ste_repr,			/* tp_repr */
127	0,					/* tp_as_number */
128	0,			                /* tp_as_sequence */
129	0,					/* tp_as_mapping */
130	0,					/* tp_hash */
131	0,					/* tp_call */
132	0,					/* tp_str */
133	PyObject_GenericGetAttr,		/* tp_getattro */
134	0,					/* tp_setattro */
135	0,					/* tp_as_buffer */
136	Py_TPFLAGS_DEFAULT,	                /* tp_flags */
137 	0,					/* tp_doc */
138	0,					/* tp_traverse */
139	0,					/* tp_clear */
140	0,					/* tp_richcompare */
141	0,					/* tp_weaklistoffset */
142	0,					/* tp_iter */
143	0,					/* tp_iternext */
144	0,					/* tp_methods */
145	ste_memberlist,				/* tp_members */
146	0,					/* tp_getset */
147	0,					/* tp_base */
148	0,					/* tp_dict */
149	0,					/* tp_descr_get */
150	0,					/* tp_descr_set */
151	0,					/* tp_dictoffset */
152	0,					/* tp_init */
153	0,					/* tp_alloc */
154	0,					/* tp_new */
155};
156
157static int symtable_analyze(struct symtable *st);
158static int symtable_warn(struct symtable *st, char *msg, int lineno);
159static int symtable_enter_block(struct symtable *st, identifier name,
160				_Py_block_ty block, void *ast, int lineno);
161static int symtable_exit_block(struct symtable *st, void *ast);
162static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
163static int symtable_visit_expr(struct symtable *st, expr_ty s);
164static int symtable_visit_genexp(struct symtable *st, expr_ty s);
165static int symtable_visit_arguments(struct symtable *st, arguments_ty);
166static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
167static int symtable_visit_alias(struct symtable *st, alias_ty);
168static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
169static int symtable_visit_keyword(struct symtable *st, keyword_ty);
170static int symtable_visit_slice(struct symtable *st, slice_ty);
171static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
172static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
173static int symtable_implicit_arg(struct symtable *st, int pos);
174
175
176static identifier top = NULL, lambda = NULL, genexpr = NULL;
177
178#define GET_IDENTIFIER(VAR) \
179	((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
180
181#define DUPLICATE_ARGUMENT \
182"duplicate argument '%s' in function definition"
183
184static struct symtable *
185symtable_new(void)
186{
187	struct symtable *st;
188
189	st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
190	if (st == NULL)
191		return NULL;
192
193	st->st_filename = NULL;
194	st->st_symbols = NULL;
195
196	if ((st->st_stack = PyList_New(0)) == NULL)
197		goto fail;
198	if ((st->st_symbols = PyDict_New()) == NULL)
199		goto fail;
200	st->st_cur = NULL;
201	st->st_tmpname = 0;
202	st->st_private = NULL;
203	return st;
204 fail:
205	PySymtable_Free(st);
206	return NULL;
207}
208
209struct symtable *
210PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
211{
212	struct symtable *st = symtable_new();
213	asdl_seq *seq;
214	int i;
215
216	if (st == NULL)
217		return st;
218	st->st_filename = filename;
219	st->st_future = future;
220	symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
221			     (void *)mod, 0);
222	st->st_top = st->st_cur;
223	st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
224	/* Any other top-level initialization? */
225	switch (mod->kind) {
226	case Module_kind:
227		seq = mod->v.Module.body;
228		for (i = 0; i < asdl_seq_LEN(seq); i++)
229			if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
230				goto error;
231		break;
232	case Expression_kind:
233		if (!symtable_visit_expr(st, mod->v.Expression.body))
234			goto error;
235		break;
236	case Interactive_kind:
237		seq = mod->v.Interactive.body;
238		for (i = 0; i < asdl_seq_LEN(seq); i++)
239			if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
240				goto error;
241		break;
242	case Suite_kind:
243		PyErr_SetString(PyExc_RuntimeError,
244				"this compiler does not handle Suites");
245		goto error;
246	}
247	if (!symtable_exit_block(st, (void *)mod)) {
248		PySymtable_Free(st);
249		return NULL;
250	}
251	if (symtable_analyze(st))
252		return st;
253	PySymtable_Free(st);
254	return NULL;
255 error:
256	(void) symtable_exit_block(st, (void *)mod);
257	PySymtable_Free(st);
258	return NULL;
259}
260
261void
262PySymtable_Free(struct symtable *st)
263{
264	Py_XDECREF(st->st_symbols);
265	Py_XDECREF(st->st_stack);
266	PyMem_Free((void *)st);
267}
268
269PySTEntryObject *
270PySymtable_Lookup(struct symtable *st, void *key)
271{
272	PyObject *k, *v;
273
274	k = PyLong_FromVoidPtr(key);
275	if (k == NULL)
276		return NULL;
277	v = PyDict_GetItem(st->st_symbols, k);
278	if (v) {
279		assert(PySTEntry_Check(v));
280		Py_INCREF(v);
281	}
282	else {
283		PyErr_SetString(PyExc_KeyError,
284				"unknown symbol table entry");
285	}
286
287	Py_DECREF(k);
288	return (PySTEntryObject *)v;
289}
290
291int
292PyST_GetScope(PySTEntryObject *ste, PyObject *name)
293{
294	PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
295	if (!v)
296		return 0;
297	assert(PyInt_Check(v));
298	return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
299}
300
301
302/* Analyze raw symbol information to determine scope of each name.
303
304   The next several functions are helpers for PySymtable_Analyze(),
305   which determines whether a name is local, global, or free.  In addition,
306   it determines which local variables are cell variables; they provide
307   bindings that are used for free variables in enclosed blocks.
308
309   There are also two kinds of free variables, implicit and explicit.  An
310   explicit global is declared with the global statement.  An implicit
311   global is a free variable for which the compiler has found no binding
312   in an enclosing function scope.  The implicit global is either a global
313   or a builtin.  Python's module and class blocks use the xxx_NAME opcodes
314   to handle these names to implement slightly odd semantics.  In such a
315   block, the name is treated as global until it is assigned to; then it
316   is treated as a local.
317
318   The symbol table requires two passes to determine the scope of each name.
319   The first pass collects raw facts from the AST: the name is a parameter
320   here, the name is used by not defined here, etc.  The second pass analyzes
321   these facts during a pass over the PySTEntryObjects created during pass 1.
322
323   When a function is entered during the second pass, the parent passes
324   the set of all name bindings visible to its children.  These bindings
325   are used to determine if the variable is free or an implicit global.
326   After doing the local analysis, it analyzes each of its child blocks
327   using an updated set of name bindings.
328
329   The children update the free variable set.  If a local variable is free
330   in a child, the variable is marked as a cell.  The current function must
331   provide runtime storage for the variable that may outlive the function's
332   frame.  Cell variables are removed from the free set before the analyze
333   function returns to its parent.
334
335   The sets of bound and free variables are implemented as dictionaries
336   mapping strings to None.
337*/
338
339#define SET_SCOPE(DICT, NAME, I) { \
340	PyObject *o = PyInt_FromLong(I); \
341	if (!o) \
342		return 0; \
343	if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
344		Py_DECREF(o); \
345		return 0; \
346	} \
347	Py_DECREF(o); \
348}
349
350/* Decide on scope of name, given flags.
351
352   The dicts passed in as arguments are modified as necessary.
353   ste is passed so that flags can be updated.
354*/
355
356static int
357analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
358	     PyObject *bound, PyObject *local, PyObject *free,
359	     PyObject *global)
360{
361	if (flags & DEF_GLOBAL) {
362		if (flags & DEF_PARAM) {
363			PyErr_Format(PyExc_SyntaxError,
364				     "name '%s' is local and global",
365				     PyString_AS_STRING(name));
366			return 0;
367		}
368		SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
369		if (PyDict_SetItem(global, name, Py_None) < 0)
370			return 0;
371		if (bound && PyDict_GetItem(bound, name)) {
372			if (PyDict_DelItem(bound, name) < 0)
373				return 0;
374		}
375		return 1;
376	}
377	if (flags & DEF_BOUND) {
378		SET_SCOPE(dict, name, LOCAL);
379		if (PyDict_SetItem(local, name, Py_None) < 0)
380			return 0;
381		if (PyDict_GetItem(global, name)) {
382			if (PyDict_DelItem(global, name) < 0)
383				return 0;
384		}
385		return 1;
386	}
387	/* If an enclosing block has a binding for this name, it
388	   is a free variable rather than a global variable.
389	   Note that having a non-NULL bound implies that the block
390	   is nested.
391	*/
392	if (bound && PyDict_GetItem(bound, name)) {
393		SET_SCOPE(dict, name, FREE);
394		ste->ste_free = 1;
395		if (PyDict_SetItem(free, name, Py_None) < 0)
396			return 0;
397		return 1;
398	}
399	/* If a parent has a global statement, then call it global
400	   explicit?  It could also be global implicit.
401	 */
402	else if (global && PyDict_GetItem(global, name)) {
403		SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
404		return 1;
405	}
406	else {
407		if (ste->ste_nested)
408			ste->ste_free = 1;
409		SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
410		return 1;
411	}
412	return 0; /* Can't get here */
413}
414
415#undef SET_SCOPE
416
417/* If a name is defined in free and also in locals, then this block
418   provides the binding for the free variable.  The name should be
419   marked CELL in this block and removed from the free list.
420
421   Note that the current block's free variables are included in free.
422   That's safe because no name can be free and local in the same scope.
423*/
424
425static int
426analyze_cells(PyObject *scope, PyObject *free)
427{
428        PyObject *name, *v, *w;
429	int pos = 0, success = 0;
430
431	w = PyInt_FromLong(CELL);
432	if (!w)
433		return 0;
434	while (PyDict_Next(scope, &pos, &name, &v)) {
435		assert(PyInt_Check(v));
436		long flags = PyInt_AS_LONG(v);
437		if (flags != LOCAL)
438			continue;
439		if (!PyDict_GetItem(free, name))
440			continue;
441		/* Replace LOCAL with CELL for this name, and remove
442		   from free. It is safe to replace the value of name
443		   in the dict, because it will not cause a resize.
444		 */
445		if (PyDict_SetItem(scope, name, w) < 0)
446			goto error;
447		if (!PyDict_DelItem(free, name) < 0)
448			goto error;
449	}
450	success = 1;
451 error:
452	Py_DECREF(w);
453	return success;
454}
455
456/* Check for illegal statements in unoptimized namespaces */
457static int
458check_unoptimized(const PySTEntryObject* ste) {
459	char buf[300];
460	const char* trailer;
461
462	if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
463	    || !(ste->ste_free || ste->ste_child_free))
464		return 1;
465
466	trailer = (ste->ste_child_free ?
467		       "contains a nested function with free variables" :
468			       "is a nested function");
469
470	switch (ste->ste_unoptimized) {
471	case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
472	case OPT_EXEC: /* qualified exec is fine */
473		return 1;
474	case OPT_IMPORT_STAR:
475		PyOS_snprintf(buf, sizeof(buf),
476			      "import * is not allowed in function '%.100s' "
477			      "because it is %s",
478			      PyString_AS_STRING(ste->ste_name), trailer);
479		break;
480	case OPT_BARE_EXEC:
481		PyOS_snprintf(buf, sizeof(buf),
482			      "unqualified exec is not allowed in function "
483			      "'%.100s' it %s",
484			      PyString_AS_STRING(ste->ste_name), trailer);
485		break;
486	default:
487		PyOS_snprintf(buf, sizeof(buf),
488			      "function '%.100s' uses import * and bare exec, "
489			      "which are illegal because it %s",
490			      PyString_AS_STRING(ste->ste_name), trailer);
491		break;
492	}
493
494	PyErr_SetString(PyExc_SyntaxError, buf);
495	PyErr_SyntaxLocation(ste->ste_table->st_filename,
496			     ste->ste_opt_lineno);
497	return 0;
498}
499
500/* Enter the final scope information into the st_symbols dict.
501 *
502 * All arguments are dicts.  Modifies symbols, others are read-only.
503*/
504static int
505update_symbols(PyObject *symbols, PyObject *scope,
506               PyObject *bound, PyObject *free, int class)
507{
508	PyObject *name, *v, *u, *w, *free_value = NULL;
509	int pos = 0;
510
511	while (PyDict_Next(symbols, &pos, &name, &v)) {
512		long i, flags;
513		assert(PyInt_Check(v));
514		flags = PyInt_AS_LONG(v);
515		w = PyDict_GetItem(scope, name);
516		assert(w && PyInt_Check(w));
517		i = PyInt_AS_LONG(w);
518		flags |= (i << SCOPE_OFF);
519		u = PyInt_FromLong(flags);
520		if (PyDict_SetItem(symbols, name, u) < 0) {
521			Py_DECREF(u);
522			return 0;
523		}
524		Py_DECREF(u);
525	}
526
527        free_value = PyInt_FromLong(FREE << SCOPE_OFF);
528        if (!free_value)
529		return 0;
530
531        /* add a free variable when it's only use is for creating a closure */
532        pos = 0;
533	while (PyDict_Next(free, &pos, &name, &v)) {
534		PyObject *o = PyDict_GetItem(symbols, name);
535
536		if (o) {
537			/* It could be a free variable in a method of
538			   the class that has the same name as a local
539			   or global in the class scope.
540			*/
541			if  (class &&
542			     PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
543				long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
544				o = PyInt_FromLong(i);
545				if (!o) {
546					Py_DECREF(free_value);
547					return 0;
548				}
549				if (PyDict_SetItem(symbols, name, o) < 0) {
550					Py_DECREF(o);
551					Py_DECREF(free_value);
552					return 0;
553				}
554				Py_DECREF(o);
555			}
556			/* else it's not free, probably a cell */
557			continue;
558		}
559		if (!PyDict_GetItem(bound, name))
560			continue;       /* it's a global */
561
562		if (PyDict_SetItem(symbols, name, free_value) < 0) {
563			Py_DECREF(free_value);
564			return 0;
565		}
566        }
567        Py_DECREF(free_value);
568	return 1;
569}
570
571/* Make final symbol table decisions for block of ste.
572   Arguments:
573   ste -- current symtable entry (input/output)
574   bound -- set of variables bound in enclosing scopes (input)
575   free -- set of free variables in enclosed scopes (output)
576   globals -- set of declared global variables in enclosing scopes (input)
577*/
578
579static int
580analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
581	      PyObject *global)
582{
583	PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
584	PyObject *newglobal = NULL, *newfree = NULL;
585	int i, pos = 0, success = 0;
586
587	local = PyDict_New();
588	if (!local)
589		goto error;
590	scope = PyDict_New();
591	if (!scope)
592		goto error;
593	newglobal = PyDict_New();
594	if (!newglobal)
595		goto error;
596	newfree = PyDict_New();
597	if (!newfree)
598		goto error;
599	newbound = PyDict_New();
600	if (!newbound)
601		goto error;
602
603	if (ste->ste_type == ClassBlock) {
604		/* make a copy of globals before calling analyze_name(),
605		   because global statements in the class have no effect
606		   on nested functions.
607		*/
608		if (PyDict_Update(newglobal, global) < 0)
609			goto error;
610		if (bound)
611			if (PyDict_Update(newbound, bound) < 0)
612				goto error;
613	}
614
615	assert(PySTEntry_Check(ste));
616	assert(PyDict_Check(ste->ste_symbols));
617	while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
618		long flags = PyInt_AS_LONG(v);
619		if (!analyze_name(ste, scope, name, flags, bound, local, free,
620				  global))
621			goto error;
622	}
623
624	if (ste->ste_type != ClassBlock) {
625		if (ste->ste_type == FunctionBlock) {
626			if (PyDict_Update(newbound, local) < 0)
627				goto error;
628		}
629		if (bound) {
630			if (PyDict_Update(newbound, bound) < 0)
631				goto error;
632		}
633		if (PyDict_Update(newglobal, global) < 0)
634			goto error;
635	}
636
637	/* Recursively call analyze_block() on each child block */
638	for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
639		PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
640		PySTEntryObject* entry;
641		assert(c && PySTEntry_Check(c));
642		entry = (PySTEntryObject*)c;
643		if (!analyze_block(entry, newbound, newfree, newglobal))
644			goto error;
645		if (entry->ste_free || entry->ste_child_free)
646			ste->ste_child_free = 1;
647	}
648
649	if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
650		goto error;
651	if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
652			    ste->ste_type == ClassBlock))
653		goto error;
654	if (!check_unoptimized(ste))
655		goto error;
656
657	if (PyDict_Update(free, newfree) < 0)
658		goto error;
659	success = 1;
660 error:
661	Py_XDECREF(local);
662	Py_XDECREF(scope);
663	Py_XDECREF(newbound);
664	Py_XDECREF(newglobal);
665	Py_XDECREF(newfree);
666	if (!success)
667		assert(PyErr_Occurred());
668	return success;
669}
670
671static int
672symtable_analyze(struct symtable *st)
673{
674	PyObject *free, *global;
675	int r;
676
677	free = PyDict_New();
678	if (!free)
679	    return 0;
680	global = PyDict_New();
681	if (!global) {
682	    Py_DECREF(free);
683	    return 0;
684	}
685	r = analyze_block(st->st_top, NULL, free, global);
686	Py_DECREF(free);
687	Py_DECREF(global);
688	return r;
689}
690
691
692static int
693symtable_warn(struct symtable *st, char *msg, int lineno)
694{
695	if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
696			       lineno, NULL, NULL) < 0)	{
697		if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
698			PyErr_SetString(PyExc_SyntaxError, msg);
699			PyErr_SyntaxLocation(st->st_filename,
700					     st->st_cur->ste_lineno);
701		}
702		return 0;
703	}
704	return 1;
705}
706
707/* symtable_enter_block() gets a reference via PySTEntry_New().
708   This reference is released when the block is exited, via the DECREF
709   in symtable_exit_block().
710*/
711
712static int
713symtable_exit_block(struct symtable *st, void *ast)
714{
715	int end;
716
717	Py_DECREF(st->st_cur);
718	end = PyList_GET_SIZE(st->st_stack) - 1;
719	if (end >= 0) {
720		st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
721								end);
722		Py_INCREF(st->st_cur);
723		if (PySequence_DelItem(st->st_stack, end) < 0)
724			return 0;
725	}
726	return 1;
727}
728
729static int
730symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
731		     void *ast, int lineno)
732{
733	PySTEntryObject *prev = NULL;
734
735	if (st->st_cur) {
736		prev = st->st_cur;
737		if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
738			return 0;
739		}
740		Py_DECREF(st->st_cur);
741	}
742	st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
743	if (name == GET_IDENTIFIER(top))
744		st->st_global = st->st_cur->ste_symbols;
745	if (prev) {
746		if (PyList_Append(prev->ste_children,
747				  (PyObject *)st->st_cur) < 0) {
748			return 0;
749		}
750	}
751	return 1;
752}
753
754static long
755symtable_lookup(struct symtable *st, PyObject *name)
756{
757	PyObject *o;
758	PyObject *mangled = _Py_Mangle(st->st_private, name);
759	if (!mangled)
760		return 0;
761	o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
762	Py_DECREF(mangled);
763	if (!o)
764		return 0;
765	return PyInt_AsLong(o);
766}
767
768static int
769symtable_add_def(struct symtable *st, PyObject *name, int flag)
770{
771	PyObject *o;
772	PyObject *dict;
773	long val;
774	PyObject *mangled = _Py_Mangle(st->st_private, name);
775
776	if (!mangled)
777		return 0;
778	dict = st->st_cur->ste_symbols;
779	if ((o = PyDict_GetItem(dict, mangled))) {
780	    val = PyInt_AS_LONG(o);
781	    if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
782		    /* Is it better to use 'mangled' or 'name' here? */
783		    PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
784				 PyString_AsString(name));
785		    PyErr_SyntaxLocation(st->st_filename,
786				       st->st_cur->ste_lineno);
787		    goto error;
788	    }
789	    val |= flag;
790	} else
791	    val = flag;
792	o = PyInt_FromLong(val);
793        if (o == NULL)
794	    goto error;
795	if (PyDict_SetItem(dict, mangled, o) < 0) {
796		Py_DECREF(o);
797		goto error;
798	}
799	Py_DECREF(o);
800
801	if (flag & DEF_PARAM) {
802		if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
803			goto error;
804	} else	if (flag & DEF_GLOBAL) {
805		/* XXX need to update DEF_GLOBAL for other flags too;
806		   perhaps only DEF_FREE_GLOBAL */
807		val = flag;
808		if ((o = PyDict_GetItem(st->st_global, mangled))) {
809			val |= PyInt_AS_LONG(o);
810		}
811		o = PyInt_FromLong(val);
812		if (o == NULL)
813			goto error;
814		if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
815			Py_DECREF(o);
816			goto error;
817		}
818		Py_DECREF(o);
819	}
820	Py_DECREF(mangled);
821	return 1;
822
823error:
824	Py_DECREF(mangled);
825	return 0;
826}
827
828/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
829   They use the ASDL name to synthesize the name of the C type and the visit
830   function.
831
832   VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
833   useful if the first node in the sequence requires special treatment.
834*/
835
836#define VISIT(ST, TYPE, V) \
837	if (!symtable_visit_ ## TYPE((ST), (V))) \
838		return 0;
839
840#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
841	if (!symtable_visit_ ## TYPE((ST), (V))) { \
842		symtable_exit_block((ST), (S)); \
843		return 0; \
844	}
845
846#define VISIT_SEQ(ST, TYPE, SEQ) { \
847	int i; \
848	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
849	for (i = 0; i < asdl_seq_LEN(seq); i++) { \
850		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
851		if (!symtable_visit_ ## TYPE((ST), elt)) \
852			return 0; \
853	} \
854}
855
856#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
857	int i; \
858	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
859	for (i = 0; i < asdl_seq_LEN(seq); i++) { \
860		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
861		if (!symtable_visit_ ## TYPE((ST), elt)) { \
862			symtable_exit_block((ST), (S)); \
863			return 0; \
864		} \
865	} \
866}
867
868#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
869	int i; \
870	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
871	for (i = (START); i < asdl_seq_LEN(seq); i++) { \
872		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
873		if (!symtable_visit_ ## TYPE((ST), elt)) \
874			return 0; \
875	} \
876}
877
878#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
879	int i; \
880	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
881	for (i = (START); i < asdl_seq_LEN(seq); i++) { \
882		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
883		if (!symtable_visit_ ## TYPE((ST), elt)) { \
884			symtable_exit_block((ST), (S)); \
885			return 0; \
886		} \
887	} \
888}
889
890static int
891symtable_visit_stmt(struct symtable *st, stmt_ty s)
892{
893	switch (s->kind) {
894        case FunctionDef_kind:
895		if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
896			return 0;
897		if (s->v.FunctionDef.args->defaults)
898			VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
899		if (s->v.FunctionDef.decorators)
900			VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
901		if (!symtable_enter_block(st, s->v.FunctionDef.name,
902					  FunctionBlock, (void *)s, s->lineno))
903			return 0;
904		VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
905		VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
906		if (!symtable_exit_block(st, s))
907			return 0;
908		break;
909        case ClassDef_kind: {
910		PyObject *tmp;
911		if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
912			return 0;
913		VISIT_SEQ(st, expr, s->v.ClassDef.bases);
914		if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
915					  (void *)s, s->lineno))
916			return 0;
917		tmp = st->st_private;
918		st->st_private = s->v.ClassDef.name;
919		VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
920		st->st_private = tmp;
921		if (!symtable_exit_block(st, s))
922			return 0;
923		break;
924	}
925        case Return_kind:
926		if (s->v.Return.value)
927			VISIT(st, expr, s->v.Return.value);
928		break;
929        case Delete_kind:
930		VISIT_SEQ(st, expr, s->v.Delete.targets);
931		break;
932        case Assign_kind:
933		VISIT_SEQ(st, expr, s->v.Assign.targets);
934		VISIT(st, expr, s->v.Assign.value);
935		break;
936        case AugAssign_kind:
937		VISIT(st, expr, s->v.AugAssign.target);
938		VISIT(st, expr, s->v.AugAssign.value);
939		break;
940        case Print_kind:
941		if (s->v.Print.dest)
942			VISIT(st, expr, s->v.Print.dest);
943		VISIT_SEQ(st, expr, s->v.Print.values);
944		break;
945        case For_kind:
946		VISIT(st, expr, s->v.For.target);
947		VISIT(st, expr, s->v.For.iter);
948		VISIT_SEQ(st, stmt, s->v.For.body);
949		if (s->v.For.orelse)
950			VISIT_SEQ(st, stmt, s->v.For.orelse);
951		break;
952        case While_kind:
953		VISIT(st, expr, s->v.While.test);
954		VISIT_SEQ(st, stmt, s->v.While.body);
955		if (s->v.While.orelse)
956			VISIT_SEQ(st, stmt, s->v.While.orelse);
957		break;
958        case If_kind:
959		/* XXX if 0: and lookup_yield() hacks */
960		VISIT(st, expr, s->v.If.test);
961		VISIT_SEQ(st, stmt, s->v.If.body);
962		if (s->v.If.orelse)
963			VISIT_SEQ(st, stmt, s->v.If.orelse);
964		break;
965        case Raise_kind:
966		if (s->v.Raise.type) {
967			VISIT(st, expr, s->v.Raise.type);
968			if (s->v.Raise.inst) {
969				VISIT(st, expr, s->v.Raise.inst);
970				if (s->v.Raise.tback)
971					VISIT(st, expr, s->v.Raise.tback);
972			}
973		}
974		break;
975        case TryExcept_kind:
976		VISIT_SEQ(st, stmt, s->v.TryExcept.body);
977		VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
978		VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
979		break;
980        case TryFinally_kind:
981		VISIT_SEQ(st, stmt, s->v.TryFinally.body);
982		VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
983		break;
984        case Assert_kind:
985		VISIT(st, expr, s->v.Assert.test);
986		if (s->v.Assert.msg)
987			VISIT(st, expr, s->v.Assert.msg);
988		break;
989        case Import_kind:
990		VISIT_SEQ(st, alias, s->v.Import.names);
991		/* XXX Don't have the lineno available inside
992		   visit_alias */
993		if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
994			st->st_cur->ste_opt_lineno = s->lineno;
995		break;
996        case ImportFrom_kind:
997		VISIT_SEQ(st, alias, s->v.ImportFrom.names);
998		/* XXX Don't have the lineno available inside
999		   visit_alias */
1000		if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1001			st->st_cur->ste_opt_lineno = s->lineno;
1002		break;
1003        case Exec_kind:
1004		VISIT(st, expr, s->v.Exec.body);
1005		if (!st->st_cur->ste_opt_lineno)
1006			st->st_cur->ste_opt_lineno = s->lineno;
1007		if (s->v.Exec.globals) {
1008			st->st_cur->ste_unoptimized |= OPT_EXEC;
1009			VISIT(st, expr, s->v.Exec.globals);
1010			if (s->v.Exec.locals)
1011				VISIT(st, expr, s->v.Exec.locals);
1012		} else {
1013			st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1014		}
1015		break;
1016        case Global_kind: {
1017		int i;
1018		asdl_seq *seq = s->v.Global.names;
1019		for (i = 0; i < asdl_seq_LEN(seq); i++) {
1020			identifier name = asdl_seq_GET(seq, i);
1021			char *c_name = PyString_AS_STRING(name);
1022			long cur = symtable_lookup(st, name);
1023			if (cur < 0)
1024				return 0;
1025			if (cur & (DEF_LOCAL | USE)) {
1026				char buf[256];
1027				if (cur & DEF_LOCAL)
1028					PyOS_snprintf(buf, sizeof(buf),
1029						      GLOBAL_AFTER_ASSIGN,
1030						      c_name);
1031				else
1032					PyOS_snprintf(buf, sizeof(buf),
1033						      GLOBAL_AFTER_USE,
1034						      c_name);
1035				if (!symtable_warn(st, buf, s->lineno))
1036                                    return 0;
1037			}
1038			if (!symtable_add_def(st, name, DEF_GLOBAL))
1039				return 0;
1040		}
1041		break;
1042	}
1043        case Expr_kind:
1044		VISIT(st, expr, s->v.Expr.value);
1045		break;
1046        case Pass_kind:
1047        case Break_kind:
1048        case Continue_kind:
1049		/* nothing to do here */
1050		break;
1051	}
1052	return 1;
1053}
1054
1055static int
1056symtable_visit_expr(struct symtable *st, expr_ty e)
1057{
1058	switch (e->kind) {
1059        case BoolOp_kind:
1060		VISIT_SEQ(st, expr, e->v.BoolOp.values);
1061		break;
1062        case BinOp_kind:
1063		VISIT(st, expr, e->v.BinOp.left);
1064		VISIT(st, expr, e->v.BinOp.right);
1065		break;
1066        case UnaryOp_kind:
1067		VISIT(st, expr, e->v.UnaryOp.operand);
1068		break;
1069        case Lambda_kind: {
1070		if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1071			return 0;
1072		if (e->v.Lambda.args->defaults)
1073			VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1074		/* XXX how to get line numbers for expressions */
1075		if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1076                                          FunctionBlock, (void *)e, 0))
1077			return 0;
1078		VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1079		VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1080		if (!symtable_exit_block(st, (void *)e))
1081			return 0;
1082		break;
1083	}
1084        case Dict_kind:
1085		VISIT_SEQ(st, expr, e->v.Dict.keys);
1086		VISIT_SEQ(st, expr, e->v.Dict.values);
1087		break;
1088        case ListComp_kind: {
1089		char tmpname[256];
1090		identifier tmp;
1091
1092		PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1093			      ++st->st_cur->ste_tmpname);
1094		tmp = PyString_InternFromString(tmpname);
1095		if (!symtable_add_def(st, tmp, DEF_LOCAL))
1096			return 0;
1097		Py_DECREF(tmp);
1098		VISIT(st, expr, e->v.ListComp.elt);
1099		VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1100		break;
1101	}
1102        case GeneratorExp_kind: {
1103		if (!symtable_visit_genexp(st, e)) {
1104			return 0;
1105		}
1106		break;
1107	}
1108        case Yield_kind:
1109		if (e->v.Yield.value)
1110			VISIT(st, expr, e->v.Yield.value);
1111                st->st_cur->ste_generator = 1;
1112		break;
1113        case Compare_kind:
1114		VISIT(st, expr, e->v.Compare.left);
1115		VISIT_SEQ(st, expr, e->v.Compare.comparators);
1116		break;
1117        case Call_kind:
1118		VISIT(st, expr, e->v.Call.func);
1119		VISIT_SEQ(st, expr, e->v.Call.args);
1120		VISIT_SEQ(st, keyword, e->v.Call.keywords);
1121		if (e->v.Call.starargs)
1122			VISIT(st, expr, e->v.Call.starargs);
1123		if (e->v.Call.kwargs)
1124			VISIT(st, expr, e->v.Call.kwargs);
1125		break;
1126        case Repr_kind:
1127		VISIT(st, expr, e->v.Repr.value);
1128		break;
1129        case Num_kind:
1130        case Str_kind:
1131		/* Nothing to do here. */
1132		break;
1133	/* The following exprs can be assignment targets. */
1134        case Attribute_kind:
1135		VISIT(st, expr, e->v.Attribute.value);
1136		break;
1137        case Subscript_kind:
1138		VISIT(st, expr, e->v.Subscript.value);
1139		VISIT(st, slice, e->v.Subscript.slice);
1140		break;
1141        case Name_kind:
1142		if (!symtable_add_def(st, e->v.Name.id,
1143				      e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1144			return 0;
1145		break;
1146	/* child nodes of List and Tuple will have expr_context set */
1147        case List_kind:
1148		VISIT_SEQ(st, expr, e->v.List.elts);
1149		break;
1150        case Tuple_kind:
1151		VISIT_SEQ(st, expr, e->v.Tuple.elts);
1152		break;
1153	}
1154	return 1;
1155}
1156
1157static int
1158symtable_implicit_arg(struct symtable *st, int pos)
1159{
1160	PyObject *id = PyString_FromFormat(".%d", pos);
1161	if (id == NULL)
1162		return 0;
1163	if (!symtable_add_def(st, id, DEF_PARAM)) {
1164		Py_DECREF(id);
1165		return 0;
1166	}
1167	Py_DECREF(id);
1168	return 1;
1169}
1170
1171static int
1172symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1173{
1174	int i;
1175
1176        /* go through all the toplevel arguments first */
1177	for (i = 0; i < asdl_seq_LEN(args); i++) {
1178		expr_ty arg = asdl_seq_GET(args, i);
1179		if (arg->kind == Name_kind) {
1180			assert(arg->v.Name.ctx == Param ||
1181                               (arg->v.Name.ctx == Store && !toplevel));
1182			if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1183				return 0;
1184		}
1185		else if (arg->kind == Tuple_kind) {
1186			assert(arg->v.Tuple.ctx == Store);
1187			if (toplevel) {
1188				if (!symtable_implicit_arg(st, i))
1189					return 0;
1190			}
1191		}
1192		else {
1193		        PyErr_SetString(PyExc_SyntaxError,
1194					"invalid expression in parameter list");
1195		        PyErr_SyntaxLocation(st->st_filename,
1196				             st->st_cur->ste_lineno);
1197			return 0;
1198		}
1199	}
1200
1201	if (!toplevel) {
1202		if (!symtable_visit_params_nested(st, args))
1203			return 0;
1204	}
1205
1206	return 1;
1207}
1208
1209static int
1210symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1211{
1212	int i;
1213	for (i = 0; i < asdl_seq_LEN(args); i++) {
1214		expr_ty arg = asdl_seq_GET(args, i);
1215		if (arg->kind == Tuple_kind &&
1216		    !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1217			return 0;
1218	}
1219
1220	return 1;
1221}
1222
1223static int
1224symtable_visit_arguments(struct symtable *st, arguments_ty a)
1225{
1226	/* skip default arguments inside function block
1227	   XXX should ast be different?
1228	*/
1229	if (a->args && !symtable_visit_params(st, a->args, 1))
1230		return 0;
1231	if (a->vararg) {
1232		if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1233			return 0;
1234		st->st_cur->ste_varargs = 1;
1235	}
1236	if (a->kwarg) {
1237		if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1238			return 0;
1239		st->st_cur->ste_varkeywords = 1;
1240	}
1241	if (a->args && !symtable_visit_params_nested(st, a->args))
1242		return 0;
1243	return 1;
1244}
1245
1246
1247static int
1248symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1249{
1250	if (eh->type)
1251		VISIT(st, expr, eh->type);
1252	if (eh->name)
1253		VISIT(st, expr, eh->name);
1254	VISIT_SEQ(st, stmt, eh->body);
1255	return 1;
1256}
1257
1258
1259static int
1260symtable_visit_alias(struct symtable *st, alias_ty a)
1261{
1262	/* Compute store_name, the name actually bound by the import
1263	   operation.  It is diferent than a->name when a->name is a
1264	   dotted package name (e.g. spam.eggs)
1265	*/
1266	PyObject *store_name;
1267	PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1268	const char *base = PyString_AS_STRING(name);
1269	char *dot = strchr(base, '.');
1270	if (dot)
1271		store_name = PyString_FromStringAndSize(base, dot - base);
1272	else {
1273		store_name = name;
1274		Py_INCREF(store_name);
1275	}
1276	if (strcmp(PyString_AS_STRING(name), "*")) {
1277		int r = symtable_add_def(st, store_name, DEF_IMPORT);
1278		Py_DECREF(store_name);
1279		return r;
1280	}
1281	else {
1282            if (st->st_cur->ste_type != ModuleBlock) {
1283                int lineno = st->st_cur->ste_lineno;
1284                if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
1285                    Py_DECREF(store_name);
1286                    return 0;
1287		}
1288            }
1289	    st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1290	    Py_DECREF(store_name);
1291	    return 1;
1292	}
1293}
1294
1295
1296static int
1297symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1298{
1299	VISIT(st, expr, lc->target);
1300	VISIT(st, expr, lc->iter);
1301	VISIT_SEQ(st, expr, lc->ifs);
1302	return 1;
1303}
1304
1305
1306static int
1307symtable_visit_keyword(struct symtable *st, keyword_ty k)
1308{
1309	VISIT(st, expr, k->value);
1310	return 1;
1311}
1312
1313
1314static int
1315symtable_visit_slice(struct symtable *st, slice_ty s)
1316{
1317	switch (s->kind) {
1318	case Slice_kind:
1319		if (s->v.Slice.lower)
1320			VISIT(st, expr, s->v.Slice.lower)
1321		if (s->v.Slice.upper)
1322			VISIT(st, expr, s->v.Slice.upper)
1323		if (s->v.Slice.step)
1324			VISIT(st, expr, s->v.Slice.step)
1325		break;
1326	case ExtSlice_kind:
1327		VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1328		break;
1329	case Index_kind:
1330		VISIT(st, expr, s->v.Index.value)
1331		break;
1332	case Ellipsis_kind:
1333		break;
1334	}
1335	return 1;
1336}
1337
1338static int
1339symtable_visit_genexp(struct symtable *st, expr_ty e)
1340{
1341	comprehension_ty outermost = ((comprehension_ty)
1342			 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1343	/* Outermost iterator is evaluated in current scope */
1344	VISIT(st, expr, outermost->iter);
1345	/* Create generator scope for the rest */
1346	if (!symtable_enter_block(st, GET_IDENTIFIER(genexpr),
1347				  FunctionBlock, (void *)e, 0)) {
1348		return 0;
1349	}
1350	st->st_cur->ste_generator = 1;
1351	/* Outermost iter is received as an argument */
1352	if (!symtable_implicit_arg(st, 0)) {
1353		symtable_exit_block(st, (void *)e);
1354		return 0;
1355	}
1356	VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1357	VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1358	VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1359				e->v.GeneratorExp.generators, 1, (void*)e);
1360	VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
1361	if (!symtable_exit_block(st, (void *)e))
1362		return 0;
1363	return 1;
1364}
1365