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