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