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