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