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