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