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