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