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