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