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