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