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