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