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