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