blob: 62bd1e2ec48f8af5dc6e723a1f7fcb71f9fc35f2 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner94faa072021-03-23 20:47:40 +01002#include "pycore_ast.h" // identifier, stmt_ty
Batuhan Taskayaad106c62021-05-03 10:43:00 +03003#include "pycore_compile.h" // _Py_Mangle(), _PyFuture_FromAST()
Victor Stinner57364ce2021-03-24 01:29:09 +01004#include "pycore_parser.h" // _PyParser_ASTFromString()
Victor Stinner28ad12f2021-03-19 12:41:49 +01005#include "pycore_pystate.h" // _PyThreadState_GET()
6#include "pycore_symtable.h" // PySTEntryObject
Victor Stinner4a21e572020-04-15 02:35:41 +02007#include "structmember.h" // PyMemberDef
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00008
Neal Norwitz5d0ad502005-12-19 04:27:42 +00009/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +020010#define GLOBAL_PARAM \
11"name '%U' is parameter and global"
12
13#define NONLOCAL_PARAM \
14"name '%U' is parameter and nonlocal"
15
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070017"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000018
Jeremy Hylton81e95022007-02-27 06:50:52 +000019#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070020"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070023"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000024
Jeremy Hylton81e95022007-02-27 06:50:52 +000025#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070026"name '%U' is used prior to nonlocal declaration"
27
28#define GLOBAL_ANNOT \
29"annotated name '%U' can't be global"
30
31#define NONLOCAL_ANNOT \
32"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000033
Neal Norwitz5d0ad502005-12-19 04:27:42 +000034#define IMPORT_STAR_WARNING "import * only allowed at module level"
35
Emily Morehouse8f59ee02019-01-24 16:49:56 -070036#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100037"assignment expression within a comprehension cannot be used in a class body"
38
39#define NAMED_EXPR_COMP_CONFLICT \
40"assignment expression cannot rebind comprehension iteration variable '%U'"
41
42#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
43"comprehension inner loop cannot rebind assignment expression target '%U'"
44
45#define NAMED_EXPR_COMP_ITER_EXPR \
46"assignment expression cannot be used in a comprehension iterable expression"
Emily Morehouse8f59ee02019-01-24 16:49:56 -070047
Batuhan Taskayaad106c62021-05-03 10:43:00 +030048#define ANNOTATION_NOT_ALLOWED \
49"'%s' can not be used within an annotation"
50
51
Neal Norwitz090b3dd2006-02-28 22:36:46 +000052static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000053ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Pablo Galindoa77aac42021-04-23 14:27:05 +010054 void *key, int lineno, int col_offset,
55 int end_lineno, int end_col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020058 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 k = PyLong_FromVoidPtr(key);
61 if (k == NULL)
62 goto fail;
63 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020064 if (ste == NULL) {
65 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020069 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020072 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 ste->ste_symbols = NULL;
75 ste->ste_varnames = NULL;
76 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000077
Benjamin Petersond9c87022012-10-31 20:26:20 -040078 ste->ste_directives = NULL;
79
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 ste->ste_nested = 0;
82 ste->ste_free = 0;
83 ste->ste_varargs = 0;
84 ste->ste_varkeywords = 0;
85 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000086 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000088 ste->ste_col_offset = col_offset;
Pablo Galindoa77aac42021-04-23 14:27:05 +010089 ste->ste_end_lineno = end_lineno;
90 ste->ste_end_col_offset = end_col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (st->st_cur != NULL &&
93 (st->st_cur->ste_nested ||
94 st->st_cur->ste_type == FunctionBlock))
95 ste->ste_nested = 1;
96 ste->ste_child_free = 0;
97 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070098 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070099 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500101 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000102 ste->ste_comp_iter_target = 0;
103 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000104
Victor Stinner9a4fb662013-07-11 22:49:00 +0200105 ste->ste_symbols = PyDict_New();
106 ste->ste_varnames = PyList_New(0);
107 ste->ste_children = PyList_New(0);
108 if (ste->ste_symbols == NULL
109 || ste->ste_varnames == NULL
110 || ste->ste_children == NULL)
111 goto fail;
112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
114 goto fail;
115
116 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 Py_XDECREF(ste);
119 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120}
121
122static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
126 ste->ste_name,
127 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128}
129
130static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 ste->ste_table = NULL;
134 Py_XDECREF(ste->ste_id);
135 Py_XDECREF(ste->ste_name);
136 Py_XDECREF(ste->ste_symbols);
137 Py_XDECREF(ste->ste_varnames);
138 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400139 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100140 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000141}
142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000144
Guido van Rossum6f799372001-09-20 20:46:19 +0000145static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 {"id", T_OBJECT, OFF(ste_id), READONLY},
147 {"name", T_OBJECT, OFF(ste_name), READONLY},
148 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
149 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
150 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 {"nested", T_INT, OFF(ste_nested), READONLY},
152 {"type", T_INT, OFF(ste_type), READONLY},
153 {"lineno", T_INT, OFF(ste_lineno), READONLY},
154 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000155};
156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyVarObject_HEAD_INIT(&PyType_Type, 0)
159 "symtable entry",
160 sizeof(PySTEntryObject),
161 0,
162 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200163 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 0, /* tp_getattr */
165 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200166 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 (reprfunc)ste_repr, /* tp_repr */
168 0, /* tp_as_number */
169 0, /* tp_as_sequence */
170 0, /* tp_as_mapping */
171 0, /* tp_hash */
172 0, /* tp_call */
173 0, /* tp_str */
174 PyObject_GenericGetAttr, /* tp_getattro */
175 0, /* tp_setattro */
176 0, /* tp_as_buffer */
177 Py_TPFLAGS_DEFAULT, /* tp_flags */
178 0, /* tp_doc */
179 0, /* tp_traverse */
180 0, /* tp_clear */
181 0, /* tp_richcompare */
182 0, /* tp_weaklistoffset */
183 0, /* tp_iter */
184 0, /* tp_iternext */
185 0, /* tp_methods */
186 ste_memberlist, /* tp_members */
187 0, /* tp_getset */
188 0, /* tp_base */
189 0, /* tp_dict */
190 0, /* tp_descr_get */
191 0, /* tp_descr_set */
192 0, /* tp_dictoffset */
193 0, /* tp_init */
194 0, /* tp_alloc */
195 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000196};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199static int symtable_enter_block(struct symtable *st, identifier name,
Pablo Galindoa77aac42021-04-23 14:27:05 +0100200 _Py_block_ty block, void *ast,
201 int lineno, int col_offset,
202 int end_lineno, int end_col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600203static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
205static int symtable_visit_expr(struct symtable *st, expr_ty s);
206static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000207static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
208static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000209static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210static int symtable_visit_arguments(struct symtable *st, arguments_ty);
211static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
212static int symtable_visit_alias(struct symtable *st, alias_ty);
213static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
214static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100215static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300216static int symtable_visit_annotation(struct symtable *st, expr_ty annotation);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100217static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218static int symtable_implicit_arg(struct symtable *st, int pos);
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300219static int symtable_visit_annotations(struct symtable *st, stmt_ty, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500220static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Brandt Bucher145bf262021-02-26 14:51:55 -0800221static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000222static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300223static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224
225
Nick Coghlan650f0d02007-04-15 12:05:43 +0000226static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300228 __class__ = NULL, _annotation = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
230#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232
233#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000234"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235
236static struct symtable *
237symtable_new(void)
238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700242 if (st == NULL) {
243 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700245 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 st->st_filename = NULL;
248 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if ((st->st_stack = PyList_New(0)) == NULL)
251 goto fail;
252 if ((st->st_blocks = PyDict_New()) == NULL)
253 goto fail;
254 st->st_cur = NULL;
255 st->st_private = NULL;
256 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 fail:
Victor Stinner28ad12f2021-03-19 12:41:49 +0100258 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260}
261
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000262/* When compiling the use of C stack is probably going to be a lot
263 lighter than when executing Python code but still can overflow
264 and causing a Python crash if not checked (e.g. eval("()"*300000)).
265 Using the current recursion limit for the compiler seems too
266 restrictive (it caused at least one test to fail) so a factor is
267 used to allow deeper recursion when compiling an expression.
268
269 Using a scaling factor means this should automatically adjust when
270 the recursion limit is adjusted for small or large C stack allocations.
271*/
272#define COMPILER_STACK_FRAME_SCALE 3
273
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274struct symtable *
Victor Stinner28ad12f2021-03-19 12:41:49 +0100275_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000277 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100278 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000280 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400281 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000282 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200285 return NULL;
286 if (filename == NULL) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100287 _PySymtable_Free(st);
Victor Stinner14e461d2013-08-26 22:28:21 +0200288 return NULL;
289 }
290 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 st->st_filename = filename;
292 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000293
294 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100295 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000296 if (!tstate) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100297 _PySymtable_Free(st);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000298 return NULL;
299 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400300 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000301 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400302 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000303 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400304 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
305 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* Make the initial symbol information gathering pass */
308 if (!GET_IDENTIFIER(top) ||
Pablo Galindoa77aac42021-04-23 14:27:05 +0100309 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0, 0, 0)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100310 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 return NULL;
312 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 switch (mod->kind) {
316 case Module_kind:
317 seq = mod->v.Module.body;
318 for (i = 0; i < asdl_seq_LEN(seq); i++)
319 if (!symtable_visit_stmt(st,
320 (stmt_ty)asdl_seq_GET(seq, i)))
321 goto error;
322 break;
323 case Expression_kind:
324 if (!symtable_visit_expr(st, mod->v.Expression.body))
325 goto error;
326 break;
327 case Interactive_kind:
328 seq = mod->v.Interactive.body;
329 for (i = 0; i < asdl_seq_LEN(seq); i++)
330 if (!symtable_visit_stmt(st,
331 (stmt_ty)asdl_seq_GET(seq, i)))
332 goto error;
333 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800334 case FunctionType_kind:
335 PyErr_SetString(PyExc_RuntimeError,
336 "this compiler does not handle FunctionTypes");
337 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 }
Andy Lester95668422020-03-06 09:46:04 -0600339 if (!symtable_exit_block(st)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100340 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return NULL;
342 }
Nick Coghlan06145232019-08-29 23:26:53 +1000343 /* Check that the recursion depth counting balanced correctly */
344 if (st->recursion_depth != starting_recursion_depth) {
345 PyErr_Format(PyExc_SystemError,
346 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
347 starting_recursion_depth, st->recursion_depth);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100348 _PySymtable_Free(st);
Nick Coghlan06145232019-08-29 23:26:53 +1000349 return NULL;
350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* Make the second symbol analysis pass */
352 if (symtable_analyze(st))
353 return st;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100354 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356 error:
Andy Lester95668422020-03-06 09:46:04 -0600357 (void) symtable_exit_block(st);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100358 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360}
361
Victor Stinner14e461d2013-08-26 22:28:21 +0200362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363void
Victor Stinner28ad12f2021-03-19 12:41:49 +0100364_PySymtable_Free(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365{
Victor Stinner14e461d2013-08-26 22:28:21 +0200366 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 Py_XDECREF(st->st_blocks);
368 Py_XDECREF(st->st_stack);
369 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370}
371
372PySTEntryObject *
373PySymtable_Lookup(struct symtable *st, void *key)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 k = PyLong_FromVoidPtr(key);
378 if (k == NULL)
379 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200380 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (v) {
382 assert(PySTEntry_Check(v));
383 Py_INCREF(v);
384 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200385 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyErr_SetString(PyExc_KeyError,
387 "unknown symbol table entry");
388 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 Py_DECREF(k);
391 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392}
393
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000394static long
395_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200397 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (!v)
399 return 0;
400 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000401 return PyLong_AS_LONG(v);
402}
403
404int
Victor Stinner28ad12f2021-03-19 12:41:49 +0100405_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000406{
407 long symbol = _PyST_GetSymbol(ste, name);
408 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409}
410
Benjamin Petersond9c87022012-10-31 20:26:20 -0400411static int
412error_at_directive(PySTEntryObject *ste, PyObject *name)
413{
414 Py_ssize_t i;
415 PyObject *data;
416 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600417 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400418 data = PyList_GET_ITEM(ste->ste_directives, i);
419 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600420 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
421 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
Pablo Galindoa77aac42021-04-23 14:27:05 +0100422 PyErr_RangedSyntaxLocationObject(ste->ste_table->st_filename,
423 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
424 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1,
425 PyLong_AsLong(PyTuple_GET_ITEM(data, 3)),
426 PyLong_AsLong(PyTuple_GET_ITEM(data, 4)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600427
428 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700429 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400430 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600431 PyErr_SetString(PyExc_RuntimeError,
432 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400433 return 0;
434}
435
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436
437/* Analyze raw symbol information to determine scope of each name.
438
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000439 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 explicit global is declared with the global statement. An implicit
446 global is a free variable for which the compiler has found no binding
447 in an enclosing function scope. The implicit global is either a global
448 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
449 to handle these names to implement slightly odd semantics. In such a
450 block, the name is treated as global until it is assigned to; then it
451 is treated as a local.
452
453 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000454 The first pass collects raw facts from the AST via the symtable_visit_*
455 functions: the name is a parameter here, the name is used but not defined
456 here, etc. The second pass analyzes these facts during a pass over the
457 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458
459 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000461 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000462 Names which are explicitly declared nonlocal must exist in this set of
463 visible names - if they do not, a syntax error is raised. After doing
464 the local analysis, it analyzes each of its child blocks using an
465 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466
Nick Coghlan650f0d02007-04-15 12:05:43 +0000467 The children update the free variable set. If a local variable is added to
468 the free variable set by the child, the variable is marked as a cell. The
469 function object being defined must provide runtime storage for the variable
470 that may outlive the function's frame. Cell variables are removed from the
471 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000472
Nick Coghlan650f0d02007-04-15 12:05:43 +0000473 During analysis, the names are:
474 symbols: dict mapping from symbol names to flag values (including offset scope values)
475 scopes: dict mapping from symbol names to scope values (no offset)
476 local: set of all symbol names local to the current scope
477 bound: set of all symbol names local to a containing function scope
478 free: set of all symbol names referenced but not bound in child scopes
479 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480*/
481
482#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyObject *o = PyLong_FromLong(I); \
484 if (!o) \
485 return 0; \
486 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
487 Py_DECREF(o); \
488 return 0; \
489 } \
490 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491}
492
493/* Decide on scope of name, given flags.
494
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000495 The namespace dictionaries may be modified to record information
496 about the new name. For example, a new global will add an entry to
497 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498*/
499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000501analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyObject *bound, PyObject *local, PyObject *free,
503 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (flags & DEF_NONLOCAL) {
507 PyErr_Format(PyExc_SyntaxError,
508 "name '%U' is nonlocal and global",
509 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400510 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 }
512 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
513 if (PySet_Add(global, name) < 0)
514 return 0;
515 if (bound && (PySet_Discard(bound, name) < 0))
516 return 0;
517 return 1;
518 }
519 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (!bound) {
521 PyErr_Format(PyExc_SyntaxError,
522 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400523 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 }
525 if (!PySet_Contains(bound, name)) {
526 PyErr_Format(PyExc_SyntaxError,
527 "no binding for nonlocal '%U' found",
528 name);
529
Benjamin Petersond9c87022012-10-31 20:26:20 -0400530 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 }
532 SET_SCOPE(scopes, name, FREE);
533 ste->ste_free = 1;
534 return PySet_Add(free, name) >= 0;
535 }
536 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000537 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (PySet_Add(local, name) < 0)
539 return 0;
540 if (PySet_Discard(global, name) < 0)
541 return 0;
542 return 1;
543 }
544 /* If an enclosing block has a binding for this name, it
545 is a free variable rather than a global variable.
546 Note that having a non-NULL bound implies that the block
547 is nested.
548 */
549 if (bound && PySet_Contains(bound, name)) {
550 SET_SCOPE(scopes, name, FREE);
551 ste->ste_free = 1;
552 return PySet_Add(free, name) >= 0;
553 }
554 /* If a parent has a global statement, then call it global
555 explicit? It could also be global implicit.
556 */
557 if (global && PySet_Contains(global, name)) {
558 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
559 return 1;
560 }
561 if (ste->ste_nested)
562 ste->ste_free = 1;
563 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
564 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565}
566
567#undef SET_SCOPE
568
569/* If a name is defined in free and also in locals, then this block
570 provides the binding for the free variable. The name should be
571 marked CELL in this block and removed from the free list.
572
573 Note that the current block's free variables are included in free.
574 That's safe because no name can be free and local in the same scope.
575*/
576
577static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500578analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyObject *name, *v, *v_cell;
581 int success = 0;
582 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 v_cell = PyLong_FromLong(CELL);
585 if (!v_cell)
586 return 0;
587 while (PyDict_Next(scopes, &pos, &name, &v)) {
588 long scope;
589 assert(PyLong_Check(v));
590 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000591 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 continue;
593 if (!PySet_Contains(free, name))
594 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* Replace LOCAL with CELL for this name, and remove
596 from free. It is safe to replace the value of name
597 in the dict, because it will not cause a resize.
598 */
599 if (PyDict_SetItem(scopes, name, v_cell) < 0)
600 goto error;
601 if (PySet_Discard(free, name) < 0)
602 goto error;
603 }
604 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 Py_DECREF(v_cell);
607 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608}
609
Benjamin Peterson312595c2013-05-15 15:26:42 -0500610static int
611drop_class_free(PySTEntryObject *ste, PyObject *free)
612{
613 int res;
614 if (!GET_IDENTIFIER(__class__))
615 return 0;
616 res = PySet_Discard(free, __class__);
617 if (res < 0)
618 return 0;
619 if (res)
620 ste->ste_needs_class_closure = 1;
621 return 1;
622}
623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624/* Enter the final scope information into the ste_symbols dict.
625 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626 * All arguments are dicts. Modifies symbols, others are read-only.
627*/
628static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyObject *name = NULL, *itr = NULL;
633 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
634 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* Update scope information for all symbols in this scope */
637 while (PyDict_Next(symbols, &pos, &name, &v)) {
638 long scope, flags;
639 assert(PyLong_Check(v));
640 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200641 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 assert(v_scope && PyLong_Check(v_scope));
643 scope = PyLong_AS_LONG(v_scope);
644 flags |= (scope << SCOPE_OFFSET);
645 v_new = PyLong_FromLong(flags);
646 if (!v_new)
647 return 0;
648 if (PyDict_SetItem(symbols, name, v_new) < 0) {
649 Py_DECREF(v_new);
650 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 Py_DECREF(v_new);
653 }
654
655 /* Record not yet resolved free variables from children (if any) */
656 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
657 if (!v_free)
658 return 0;
659
660 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600661 if (itr == NULL) {
662 Py_DECREF(v_free);
663 return 0;
664 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665
666 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200667 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668
669 /* Handle symbol that already exists in this scope */
670 if (v) {
671 /* Handle a free variable in a method of
672 the class that has the same name as a local
673 or global in the class scope.
674 */
675 if (classflag &&
676 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
677 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
678 v_new = PyLong_FromLong(flags);
679 if (!v_new) {
680 goto error;
681 }
682 if (PyDict_SetItem(symbols, name, v_new) < 0) {
683 Py_DECREF(v_new);
684 goto error;
685 }
686 Py_DECREF(v_new);
687 }
688 /* It's a cell, or already free in this scope */
689 Py_DECREF(name);
690 continue;
691 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200692 else if (PyErr_Occurred()) {
693 goto error;
694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200696 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 Py_DECREF(name);
698 continue; /* it's a global */
699 }
700 /* Propagate new free symbol up the lexical stack */
701 if (PyDict_SetItem(symbols, name, v_free) < 0) {
702 goto error;
703 }
704 Py_DECREF(name);
705 }
706 Py_DECREF(itr);
707 Py_DECREF(v_free);
708 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000709error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 Py_XDECREF(v_free);
711 Py_XDECREF(itr);
712 Py_XDECREF(name);
713 return 0;
714}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715
716/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 Arguments:
719 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000720 bound -- set of variables bound in enclosing scopes (input). bound
721 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 free -- set of free variables in enclosed scopes (output)
723 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000724
725 The implementation uses two mutually recursive functions,
726 analyze_block() and analyze_child_block(). analyze_block() is
727 responsible for analyzing the individual names defined in a block.
728 analyze_child_block() prepares temporary namespace dictionaries
729 used to evaluated nested blocks.
730
731 The two functions exist because a child block should see the name
732 bindings of its enclosing blocks, but those bindings should not
733 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734*/
735
736static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
738 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000739
740static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
742 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
745 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
746 PyObject *temp;
747 int i, success = 0;
748 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 local = PySet_New(NULL); /* collect new names bound in block */
751 if (!local)
752 goto error;
753 scopes = PyDict_New(); /* collect scopes defined for each name */
754 if (!scopes)
755 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* Allocate new global and bound variable dictionaries. These
758 dictionaries hold the names visible in nested blocks. For
759 ClassBlocks, the bound and global names are initialized
760 before analyzing names, because class bindings aren't
761 visible in methods. For other blocks, they are initialized
762 after names are analyzed.
763 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 /* TODO(jhylton): Package these dicts in a struct so that we
766 can write reasonable helper functions?
767 */
768 newglobal = PySet_New(NULL);
769 if (!newglobal)
770 goto error;
771 newfree = PySet_New(NULL);
772 if (!newfree)
773 goto error;
774 newbound = PySet_New(NULL);
775 if (!newbound)
776 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 /* Class namespace has no effect on names visible in
779 nested functions, so populate the global and bound
780 sets to be passed to child blocks before analyzing
781 this one.
782 */
783 if (ste->ste_type == ClassBlock) {
784 /* Pass down known globals */
785 temp = PyNumber_InPlaceOr(newglobal, global);
786 if (!temp)
787 goto error;
788 Py_DECREF(temp);
789 /* Pass down previously bound symbols */
790 if (bound) {
791 temp = PyNumber_InPlaceOr(newbound, bound);
792 if (!temp)
793 goto error;
794 Py_DECREF(temp);
795 }
796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
799 long flags = PyLong_AS_LONG(v);
800 if (!analyze_name(ste, scopes, name, flags,
801 bound, local, free, global))
802 goto error;
803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* Populate global and bound sets to be passed to children. */
806 if (ste->ste_type != ClassBlock) {
807 /* Add function locals to bound set */
808 if (ste->ste_type == FunctionBlock) {
809 temp = PyNumber_InPlaceOr(newbound, local);
810 if (!temp)
811 goto error;
812 Py_DECREF(temp);
813 }
814 /* Pass down previously bound symbols */
815 if (bound) {
816 temp = PyNumber_InPlaceOr(newbound, bound);
817 if (!temp)
818 goto error;
819 Py_DECREF(temp);
820 }
821 /* Pass down known globals */
822 temp = PyNumber_InPlaceOr(newglobal, global);
823 if (!temp)
824 goto error;
825 Py_DECREF(temp);
826 }
827 else {
828 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000829 if (!GET_IDENTIFIER(__class__))
830 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 if (PySet_Add(newbound, __class__) < 0)
832 goto error;
833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300835 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 newbound, newglobal now contain the names visible in
838 nested blocks. The free variables in the children will
839 be collected in allfree.
840 */
841 allfree = PySet_New(NULL);
842 if (!allfree)
843 goto error;
844 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
845 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
846 PySTEntryObject* entry;
847 assert(c && PySTEntry_Check(c));
848 entry = (PySTEntryObject*)c;
849 if (!analyze_child_block(entry, newbound, newfree, newglobal,
850 allfree))
851 goto error;
852 /* Check if any children have free variables */
853 if (entry->ste_free || entry->ste_child_free)
854 ste->ste_child_free = 1;
855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 temp = PyNumber_InPlaceOr(newfree, allfree);
858 if (!temp)
859 goto error;
860 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500863 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500865 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 goto error;
867 /* Records the results of the analysis in the symbol table entry */
868 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
869 ste->ste_type == ClassBlock))
870 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 temp = PyNumber_InPlaceOr(free, newfree);
873 if (!temp)
874 goto error;
875 Py_DECREF(temp);
876 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 Py_XDECREF(scopes);
879 Py_XDECREF(local);
880 Py_XDECREF(newbound);
881 Py_XDECREF(newglobal);
882 Py_XDECREF(newfree);
883 Py_XDECREF(allfree);
884 if (!success)
885 assert(PyErr_Occurred());
886 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
888
889static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
891 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
894 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000897
Martin Panter3ee62702016-06-04 04:57:19 +0000898 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 current block. The analyze_block() call modifies these
900 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 */
903 temp_bound = PySet_New(bound);
904 if (!temp_bound)
905 goto error;
906 temp_free = PySet_New(free);
907 if (!temp_free)
908 goto error;
909 temp_global = PySet_New(global);
910 if (!temp_global)
911 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
914 goto error;
915 temp = PyNumber_InPlaceOr(child_free, temp_free);
916 if (!temp)
917 goto error;
918 Py_DECREF(temp);
919 Py_DECREF(temp_bound);
920 Py_DECREF(temp_free);
921 Py_DECREF(temp_global);
922 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000923 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 Py_XDECREF(temp_bound);
925 Py_XDECREF(temp_free);
926 Py_XDECREF(temp_global);
927 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000928}
929
930static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931symtable_analyze(struct symtable *st)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 PyObject *free, *global;
934 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 free = PySet_New(NULL);
937 if (!free)
938 return 0;
939 global = PySet_New(NULL);
940 if (!global) {
941 Py_DECREF(free);
942 return 0;
943 }
944 r = analyze_block(st->st_top, NULL, free, global);
945 Py_DECREF(free);
946 Py_DECREF(global);
947 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948}
949
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000950/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 This reference is released when the block is exited, via the DECREF
952 in symtable_exit_block().
953*/
954
955static int
Andy Lester95668422020-03-06 09:46:04 -0600956symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957{
Benjamin Peterson609da582011-06-29 22:52:39 -0500958 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959
Benjamin Peterson609da582011-06-29 22:52:39 -0500960 st->st_cur = NULL;
961 size = PyList_GET_SIZE(st->st_stack);
962 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500963 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500965 if (--size)
966 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 }
968 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969}
970
971static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Pablo Galindoa77aac42021-04-23 14:27:05 +0100973 void *ast, int lineno, int col_offset,
974 int end_lineno, int end_col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975{
Benjamin Peterson609da582011-06-29 22:52:39 -0500976 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
Pablo Galindoa77aac42021-04-23 14:27:05 +0100978 ste = ste_new(st, name, block, ast, lineno, col_offset, end_lineno, end_col_offset);
Benjamin Peterson609da582011-06-29 22:52:39 -0500979 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500981 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
982 Py_DECREF(ste);
983 return 0;
984 }
985 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000986 /* bpo-37757: For now, disallow *all* assignment expressions in the
987 * outermost iterator expression of a comprehension, even those inside
988 * a nested comprehension or a lambda expression.
989 */
990 if (prev) {
991 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
992 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500993 /* The entry is owned by the stack. Borrow it for st_cur. */
994 Py_DECREF(ste);
995 st->st_cur = ste;
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300996
997 /* Annotation blocks shouldn't have any affect on the symbol table since in
998 * the compilation stage, they will all be transformed to strings. They are
999 * only created if future 'annotations' feature is activated. */
1000 if (block == AnnotationBlock) {
1001 return 1;
1002 }
1003
Benjamin Peterson230b2062010-10-16 03:45:45 +00001004 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 st->st_global = st->st_cur->ste_symbols;
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -05001008 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return 0;
1010 }
1011 }
1012 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013}
1014
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001015static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016symtable_lookup(struct symtable *st, PyObject *name)
1017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PyObject *mangled = _Py_Mangle(st->st_private, name);
1019 if (!mangled)
1020 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +01001021 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1022 Py_DECREF(mangled);
1023 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024}
1025
1026static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001027symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 PyObject *o;
1030 PyObject *dict;
1031 long val;
1032 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033
Jeremy Hylton81e95022007-02-27 06:50:52 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (!mangled)
1036 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001037 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001038 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 val = PyLong_AS_LONG(o);
1040 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1041 /* Is it better to use 'mangled' or 'name' here? */
1042 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001043 PyErr_RangedSyntaxLocationObject(st->st_filename,
1044 ste->ste_lineno,
1045 ste->ste_col_offset + 1,
1046 ste->ste_end_lineno,
1047 ste->ste_end_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 goto error;
1049 }
1050 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001051 }
1052 else if (PyErr_Occurred()) {
1053 goto error;
1054 }
1055 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001057 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001058 if (ste->ste_comp_iter_target) {
1059 /* This name is an iteration variable in a comprehension,
1060 * so check for a binding conflict with any named expressions.
1061 * Otherwise, mark it as an iteration variable so subsequent
1062 * named expressions can check for conflicts.
1063 */
1064 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1065 PyErr_Format(PyExc_SyntaxError,
1066 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001067 PyErr_RangedSyntaxLocationObject(st->st_filename,
1068 ste->ste_lineno,
1069 ste->ste_col_offset + 1,
1070 ste->ste_end_lineno,
1071 ste->ste_end_col_offset + 1);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001072 goto error;
1073 }
1074 val |= DEF_COMP_ITER;
1075 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 o = PyLong_FromLong(val);
1077 if (o == NULL)
1078 goto error;
1079 if (PyDict_SetItem(dict, mangled, o) < 0) {
1080 Py_DECREF(o);
1081 goto error;
1082 }
1083 Py_DECREF(o);
1084
1085 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001086 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 goto error;
1088 } else if (flag & DEF_GLOBAL) {
1089 /* XXX need to update DEF_GLOBAL for other flags too;
1090 perhaps only DEF_FREE_GLOBAL */
1091 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001092 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 val |= PyLong_AS_LONG(o);
1094 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001095 else if (PyErr_Occurred()) {
1096 goto error;
1097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 goto error;
1101 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1102 Py_DECREF(o);
1103 goto error;
1104 }
1105 Py_DECREF(o);
1106 }
1107 Py_DECREF(mangled);
1108 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001109
1110error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Py_DECREF(mangled);
1112 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113}
1114
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001115static int
1116symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1117 return symtable_add_def_helper(st, name, flag, st->st_cur);
1118}
1119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1121 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 function.
1123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1125 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001126
1127 VISIT_QUIT macro returns the specified value exiting from the function but
1128 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129*/
1130
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001131#define VISIT_QUIT(ST, X) \
1132 return --(ST)->recursion_depth,(X)
1133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001136 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001140 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1142 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1143 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001144 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001150 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1152 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1153 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001154 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001157
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001158#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001160 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001162 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001164 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001165 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001167}
1168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169static int
Pablo Galindoa77aac42021-04-23 14:27:05 +01001170symtable_record_directive(struct symtable *st, identifier name, int lineno,
1171 int col_offset, int end_lineno, int end_col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001172{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001173 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001174 int res;
1175 if (!st->st_cur->ste_directives) {
1176 st->st_cur->ste_directives = PyList_New(0);
1177 if (!st->st_cur->ste_directives)
1178 return 0;
1179 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001180 mangled = _Py_Mangle(st->st_private, name);
1181 if (!mangled)
1182 return 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +01001183 data = Py_BuildValue("(Niiii)", mangled, lineno, col_offset, end_lineno, end_col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001184 if (!data)
1185 return 0;
1186 res = PyList_Append(st->st_cur->ste_directives, data);
1187 Py_DECREF(data);
1188 return res == 0;
1189}
1190
1191
1192static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193symtable_visit_stmt(struct symtable *st, stmt_ty s)
1194{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001195 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001196 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001197 "maximum recursion depth exceeded during compilation");
1198 VISIT_QUIT(st, 0);
1199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 switch (s->kind) {
1201 case FunctionDef_kind:
1202 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001203 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (s->v.FunctionDef.args->defaults)
1205 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1206 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001207 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001208 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001209 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001210 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (s->v.FunctionDef.decorator_list)
1212 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1213 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001214 FunctionBlock, (void *)s,
1215 s->lineno, s->col_offset,
1216 s->end_lineno, s->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001217 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001218 VISIT(st, arguments, s->v.FunctionDef.args);
1219 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001220 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001221 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 break;
1223 case ClassDef_kind: {
1224 PyObject *tmp;
1225 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001226 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1228 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (s->v.ClassDef.decorator_list)
1230 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1231 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001232 (void *)s, s->lineno, s->col_offset,
1233 s->end_lineno, s->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001234 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 tmp = st->st_private;
1236 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001237 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001239 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001240 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 break;
1242 }
1243 case Return_kind:
1244 if (s->v.Return.value) {
1245 VISIT(st, expr, s->v.Return.value);
1246 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 }
1248 break;
1249 case Delete_kind:
1250 VISIT_SEQ(st, expr, s->v.Delete.targets);
1251 break;
1252 case Assign_kind:
1253 VISIT_SEQ(st, expr, s->v.Assign.targets);
1254 VISIT(st, expr, s->v.Assign.value);
1255 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001256 case AnnAssign_kind:
1257 if (s->v.AnnAssign.target->kind == Name_kind) {
1258 expr_ty e_name = s->v.AnnAssign.target;
1259 long cur = symtable_lookup(st, e_name->v.Name.id);
1260 if (cur < 0) {
1261 VISIT_QUIT(st, 0);
1262 }
1263 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001264 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001265 && s->v.AnnAssign.simple) {
1266 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001267 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1268 e_name->v.Name.id);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001269 PyErr_RangedSyntaxLocationObject(st->st_filename,
1270 s->lineno,
1271 s->col_offset + 1,
1272 s->end_lineno,
1273 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001274 VISIT_QUIT(st, 0);
1275 }
1276 if (s->v.AnnAssign.simple &&
1277 !symtable_add_def(st, e_name->v.Name.id,
1278 DEF_ANNOT | DEF_LOCAL)) {
1279 VISIT_QUIT(st, 0);
1280 }
1281 else {
1282 if (s->v.AnnAssign.value
1283 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1284 VISIT_QUIT(st, 0);
1285 }
1286 }
1287 }
1288 else {
1289 VISIT(st, expr, s->v.AnnAssign.target);
1290 }
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001291 if (!symtable_visit_annotation(st, s->v.AnnAssign.annotation)) {
1292 VISIT_QUIT(st, 0);
1293 }
1294
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001295 if (s->v.AnnAssign.value) {
1296 VISIT(st, expr, s->v.AnnAssign.value);
1297 }
1298 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 case AugAssign_kind:
1300 VISIT(st, expr, s->v.AugAssign.target);
1301 VISIT(st, expr, s->v.AugAssign.value);
1302 break;
1303 case For_kind:
1304 VISIT(st, expr, s->v.For.target);
1305 VISIT(st, expr, s->v.For.iter);
1306 VISIT_SEQ(st, stmt, s->v.For.body);
1307 if (s->v.For.orelse)
1308 VISIT_SEQ(st, stmt, s->v.For.orelse);
1309 break;
1310 case While_kind:
1311 VISIT(st, expr, s->v.While.test);
1312 VISIT_SEQ(st, stmt, s->v.While.body);
1313 if (s->v.While.orelse)
1314 VISIT_SEQ(st, stmt, s->v.While.orelse);
1315 break;
1316 case If_kind:
1317 /* XXX if 0: and lookup_yield() hacks */
1318 VISIT(st, expr, s->v.If.test);
1319 VISIT_SEQ(st, stmt, s->v.If.body);
1320 if (s->v.If.orelse)
1321 VISIT_SEQ(st, stmt, s->v.If.orelse);
1322 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001323 case Match_kind:
1324 VISIT(st, expr, s->v.Match.subject);
1325 VISIT_SEQ(st, match_case, s->v.Match.cases);
1326 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 case Raise_kind:
1328 if (s->v.Raise.exc) {
1329 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001330 if (s->v.Raise.cause) {
1331 VISIT(st, expr, s->v.Raise.cause);
1332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 }
1334 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001335 case Try_kind:
1336 VISIT_SEQ(st, stmt, s->v.Try.body);
1337 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1338 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1339 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 break;
1341 case Assert_kind:
1342 VISIT(st, expr, s->v.Assert.test);
1343 if (s->v.Assert.msg)
1344 VISIT(st, expr, s->v.Assert.msg);
1345 break;
1346 case Import_kind:
1347 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 break;
1349 case ImportFrom_kind:
1350 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 break;
1352 case Global_kind: {
1353 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001354 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1356 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 long cur = symtable_lookup(st, name);
1358 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001359 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001360 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1361 const char* msg;
1362 if (cur & DEF_PARAM) {
1363 msg = GLOBAL_PARAM;
1364 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001365 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001366 } else if (cur & DEF_ANNOT) {
1367 msg = GLOBAL_ANNOT;
1368 } else { /* DEF_LOCAL */
1369 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001370 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001371 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001372 msg, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001373 PyErr_RangedSyntaxLocationObject(st->st_filename,
1374 s->lineno,
1375 s->col_offset + 1,
1376 s->end_lineno,
1377 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001378 VISIT_QUIT(st, 0);
1379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001381 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001382 if (!symtable_record_directive(st, name, s->lineno, s->col_offset,
1383 s->end_lineno, s->end_col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001384 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
1386 break;
1387 }
1388 case Nonlocal_kind: {
1389 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001390 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1392 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 long cur = symtable_lookup(st, name);
1394 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001395 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001396 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1397 const char* msg;
1398 if (cur & DEF_PARAM) {
1399 msg = NONLOCAL_PARAM;
1400 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001401 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001402 } else if (cur & DEF_ANNOT) {
1403 msg = NONLOCAL_ANNOT;
1404 } else { /* DEF_LOCAL */
1405 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001406 }
1407 PyErr_Format(PyExc_SyntaxError, msg, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001408 PyErr_RangedSyntaxLocationObject(st->st_filename,
1409 s->lineno,
1410 s->col_offset + 1,
1411 s->end_lineno,
1412 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001413 VISIT_QUIT(st, 0);
1414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001416 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001417 if (!symtable_record_directive(st, name, s->lineno, s->col_offset,
1418 s->end_lineno, s->end_col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001419 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 }
1421 break;
1422 }
1423 case Expr_kind:
1424 VISIT(st, expr, s->v.Expr.value);
1425 break;
1426 case Pass_kind:
1427 case Break_kind:
1428 case Continue_kind:
1429 /* nothing to do here */
1430 break;
1431 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001432 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 VISIT_SEQ(st, stmt, s->v.With.body);
1434 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001435 case AsyncFunctionDef_kind:
1436 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1437 VISIT_QUIT(st, 0);
1438 if (s->v.AsyncFunctionDef.args->defaults)
1439 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1440 if (s->v.AsyncFunctionDef.args->kw_defaults)
1441 VISIT_SEQ_WITH_NULL(st, expr,
1442 s->v.AsyncFunctionDef.args->kw_defaults);
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001443 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001444 s->v.AsyncFunctionDef.returns))
1445 VISIT_QUIT(st, 0);
1446 if (s->v.AsyncFunctionDef.decorator_list)
1447 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1448 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001449 FunctionBlock, (void *)s,
1450 s->lineno, s->col_offset,
1451 s->end_lineno, s->end_col_offset))
Yury Selivanov75445082015-05-11 22:57:16 -04001452 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001453 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001454 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1455 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001456 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001457 VISIT_QUIT(st, 0);
1458 break;
1459 case AsyncWith_kind:
1460 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1461 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1462 break;
1463 case AsyncFor_kind:
1464 VISIT(st, expr, s->v.AsyncFor.target);
1465 VISIT(st, expr, s->v.AsyncFor.iter);
1466 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1467 if (s->v.AsyncFor.orelse)
1468 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1469 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001471 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472}
1473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001475symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1476{
1477 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001478 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001479
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001480 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001481 Py_ssize_t i, size;
1482 struct _symtable_entry *ste;
1483 size = PyList_GET_SIZE(st->st_stack);
1484 assert(size);
1485
1486 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1487 for (i = size - 1; i >= 0; i--) {
1488 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1489
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001490 /* If we find a comprehension scope, check for a target
1491 * binding conflict with iteration variables, otherwise skip it
1492 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001493 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001494 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1495 if (target_in_scope & DEF_COMP_ITER) {
1496 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001497 PyErr_RangedSyntaxLocationObject(st->st_filename,
1498 e->lineno,
1499 e->col_offset + 1,
1500 e->end_lineno,
1501 e->end_col_offset + 1);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001502 VISIT_QUIT(st, 0);
1503 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001504 continue;
1505 }
1506
Pablo Galindofd5c4142019-10-14 05:18:05 +01001507 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001508 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001509 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1510 if (target_in_scope & DEF_GLOBAL) {
1511 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1512 VISIT_QUIT(st, 0);
1513 } else {
1514 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1515 VISIT_QUIT(st, 0);
1516 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01001517 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset,
1518 e->end_lineno, e->end_col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001519 VISIT_QUIT(st, 0);
1520
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001521 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001522 }
1523 /* If we find a ModuleBlock entry, add as GLOBAL */
1524 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001525 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001526 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001527 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset,
1528 e->end_lineno, e->end_col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001529 VISIT_QUIT(st, 0);
1530
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001531 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001532 }
1533 /* Disallow usage in ClassBlock */
1534 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001535 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001536 PyErr_RangedSyntaxLocationObject(st->st_filename,
1537 e->lineno,
1538 e->col_offset + 1,
1539 e->end_lineno,
1540 e->end_col_offset + 1);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001541 VISIT_QUIT(st, 0);
1542 }
1543 }
1544
1545 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1546 and should never fall to this case
1547 */
1548 assert(0);
1549 return 0;
1550}
1551
1552static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001553symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1554{
1555 if (st->st_cur->ste_comp_iter_expr > 0) {
1556 /* Assignment isn't allowed in a comprehension iterable expression */
1557 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001558 PyErr_RangedSyntaxLocationObject(st->st_filename,
1559 e->lineno,
1560 e->col_offset + 1,
1561 e->end_lineno,
1562 e->end_col_offset + 1);
Nick Coghlan06145232019-08-29 23:26:53 +10001563 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001564 }
1565 if (st->st_cur->ste_comprehension) {
1566 /* Inside a comprehension body, so find the right target scope */
1567 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001568 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001569 }
1570 VISIT(st, expr, e->v.NamedExpr.value);
1571 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001572 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001573}
1574
1575static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576symtable_visit_expr(struct symtable *st, expr_ty e)
1577{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001578 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001579 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001580 "maximum recursion depth exceeded during compilation");
1581 VISIT_QUIT(st, 0);
1582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001584 case NamedExpr_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001585 if (!symtable_raise_if_annotation_block(st, "named expression", e)) {
1586 VISIT_QUIT(st, 0);
1587 }
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001588 if(!symtable_handle_namedexpr(st, e))
1589 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001590 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 case BoolOp_kind:
1592 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1593 break;
1594 case BinOp_kind:
1595 VISIT(st, expr, e->v.BinOp.left);
1596 VISIT(st, expr, e->v.BinOp.right);
1597 break;
1598 case UnaryOp_kind:
1599 VISIT(st, expr, e->v.UnaryOp.operand);
1600 break;
1601 case Lambda_kind: {
1602 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001603 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 if (e->v.Lambda.args->defaults)
1605 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001606 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001607 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 if (!symtable_enter_block(st, lambda,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001609 FunctionBlock, (void *)e,
1610 e->lineno, e->col_offset,
1611 e->end_lineno, e->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001612 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001613 VISIT(st, arguments, e->v.Lambda.args);
1614 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001615 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001616 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 break;
1618 }
1619 case IfExp_kind:
1620 VISIT(st, expr, e->v.IfExp.test);
1621 VISIT(st, expr, e->v.IfExp.body);
1622 VISIT(st, expr, e->v.IfExp.orelse);
1623 break;
1624 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001625 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 VISIT_SEQ(st, expr, e->v.Dict.values);
1627 break;
1628 case Set_kind:
1629 VISIT_SEQ(st, expr, e->v.Set.elts);
1630 break;
1631 case GeneratorExp_kind:
1632 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001633 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 break;
1635 case ListComp_kind:
1636 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001637 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 break;
1639 case SetComp_kind:
1640 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001641 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 break;
1643 case DictComp_kind:
1644 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001645 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 break;
1647 case Yield_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001648 if (!symtable_raise_if_annotation_block(st, "yield expression", e)) {
1649 VISIT_QUIT(st, 0);
1650 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001651 if (e->v.Yield.value)
1652 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001655 case YieldFrom_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001656 if (!symtable_raise_if_annotation_block(st, "yield expression", e)) {
1657 VISIT_QUIT(st, 0);
1658 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001659 VISIT(st, expr, e->v.YieldFrom.value);
1660 st->st_cur->ste_generator = 1;
1661 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001662 case Await_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001663 if (!symtable_raise_if_annotation_block(st, "await expression", e)) {
1664 VISIT_QUIT(st, 0);
1665 }
Yury Selivanov75445082015-05-11 22:57:16 -04001666 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001667 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001668 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 case Compare_kind:
1670 VISIT(st, expr, e->v.Compare.left);
1671 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1672 break;
1673 case Call_kind:
1674 VISIT(st, expr, e->v.Call.func);
1675 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001676 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001678 case FormattedValue_kind:
1679 VISIT(st, expr, e->v.FormattedValue.value);
1680 if (e->v.FormattedValue.format_spec)
1681 VISIT(st, expr, e->v.FormattedValue.format_spec);
1682 break;
1683 case JoinedStr_kind:
1684 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1685 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001686 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 /* Nothing to do here. */
1688 break;
1689 /* The following exprs can be assignment targets. */
1690 case Attribute_kind:
1691 VISIT(st, expr, e->v.Attribute.value);
1692 break;
1693 case Subscript_kind:
1694 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001695 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 break;
1697 case Starred_kind:
1698 VISIT(st, expr, e->v.Starred.value);
1699 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001700 case Slice_kind:
1701 if (e->v.Slice.lower)
1702 VISIT(st, expr, e->v.Slice.lower)
1703 if (e->v.Slice.upper)
1704 VISIT(st, expr, e->v.Slice.upper)
1705 if (e->v.Slice.step)
1706 VISIT(st, expr, e->v.Slice.step)
1707 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 case Name_kind:
1709 if (!symtable_add_def(st, e->v.Name.id,
1710 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001711 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 /* Special-case super: it counts as a use of __class__ */
1713 if (e->v.Name.ctx == Load &&
1714 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001715 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001716 if (!GET_IDENTIFIER(__class__) ||
1717 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001718 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 }
1720 break;
1721 /* child nodes of List and Tuple will have expr_context set */
1722 case List_kind:
1723 VISIT_SEQ(st, expr, e->v.List.elts);
1724 break;
1725 case Tuple_kind:
1726 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1727 break;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001728 }
1729 VISIT_QUIT(st, 1);
1730}
1731
1732static int
1733symtable_visit_pattern(struct symtable *st, pattern_ty p)
1734{
1735 if (++st->recursion_depth > st->recursion_limit) {
1736 PyErr_SetString(PyExc_RecursionError,
1737 "maximum recursion depth exceeded during compilation");
1738 VISIT_QUIT(st, 0);
1739 }
1740 switch (p->kind) {
1741 case MatchValue_kind:
1742 VISIT(st, expr, p->v.MatchValue.value);
1743 break;
1744 case MatchSingleton_kind:
1745 /* Nothing to do here. */
1746 break;
1747 case MatchSequence_kind:
1748 VISIT_SEQ(st, pattern, p->v.MatchSequence.patterns);
1749 break;
1750 case MatchStar_kind:
1751 if (p->v.MatchStar.name) {
1752 symtable_add_def(st, p->v.MatchStar.name, DEF_LOCAL);
1753 }
1754 break;
1755 case MatchMapping_kind:
1756 VISIT_SEQ(st, expr, p->v.MatchMapping.keys);
1757 VISIT_SEQ(st, pattern, p->v.MatchMapping.patterns);
1758 if (p->v.MatchMapping.rest) {
1759 symtable_add_def(st, p->v.MatchMapping.rest, DEF_LOCAL);
1760 }
1761 break;
1762 case MatchClass_kind:
1763 VISIT(st, expr, p->v.MatchClass.cls);
1764 VISIT_SEQ(st, pattern, p->v.MatchClass.patterns);
1765 VISIT_SEQ(st, pattern, p->v.MatchClass.kwd_patterns);
1766 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001767 case MatchAs_kind:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001768 if (p->v.MatchAs.pattern) {
1769 VISIT(st, pattern, p->v.MatchAs.pattern);
1770 }
1771 if (p->v.MatchAs.name) {
1772 symtable_add_def(st, p->v.MatchAs.name, DEF_LOCAL);
1773 }
Brandt Bucher145bf262021-02-26 14:51:55 -08001774 break;
1775 case MatchOr_kind:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001776 VISIT_SEQ(st, pattern, p->v.MatchOr.patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08001777 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001779 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780}
1781
1782static int
1783symtable_implicit_arg(struct symtable *st, int pos)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1786 if (id == NULL)
1787 return 0;
1788 if (!symtable_add_def(st, id, DEF_PARAM)) {
1789 Py_DECREF(id);
1790 return 0;
1791 }
1792 Py_DECREF(id);
1793 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794}
1795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001797symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (!args)
1802 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 for (i = 0; i < asdl_seq_LEN(args); i++) {
1805 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1806 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1807 return 0;
1808 }
1809
1810 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811}
1812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813static int
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001814symtable_visit_annotation(struct symtable *st, expr_ty annotation)
1815{
1816 int future_annotations = st->st_future->ff_features & CO_FUTURE_ANNOTATIONS;
1817 if (future_annotations &&
1818 !symtable_enter_block(st, GET_IDENTIFIER(_annotation), AnnotationBlock,
1819 (void *)annotation, annotation->lineno,
1820 annotation->col_offset, annotation->end_lineno,
1821 annotation->end_col_offset)) {
1822 VISIT_QUIT(st, 0);
1823 }
1824 VISIT(st, expr, annotation);
1825 if (future_annotations && !symtable_exit_block(st)) {
1826 VISIT_QUIT(st, 0);
1827 }
1828 return 1;
1829}
1830
1831static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001832symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (!args)
1837 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 for (i = 0; i < asdl_seq_LEN(args); i++) {
1840 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1841 if (arg->annotation)
1842 VISIT(st, expr, arg->annotation);
1843 }
1844
1845 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001846}
1847
Neal Norwitzc1505362006-12-28 06:47:50 +00001848static int
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001849symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001850{
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001851 int future_annotations = st->st_future->ff_features & CO_FUTURE_ANNOTATIONS;
1852 if (future_annotations &&
1853 !symtable_enter_block(st, GET_IDENTIFIER(_annotation), AnnotationBlock,
1854 (void *)o, o->lineno, o->col_offset, o->end_lineno,
1855 o->end_col_offset)) {
1856 VISIT_QUIT(st, 0);
1857 }
Anthony Sottileec007cb2020-01-04 20:57:21 -05001858 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1859 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 if (a->args && !symtable_visit_argannotations(st, a->args))
1861 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001862 if (a->vararg && a->vararg->annotation)
1863 VISIT(st, expr, a->vararg->annotation);
1864 if (a->kwarg && a->kwarg->annotation)
1865 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1867 return 0;
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001868 if (future_annotations && !symtable_exit_block(st)) {
1869 VISIT_QUIT(st, 0);
1870 }
1871 if (returns && !symtable_visit_annotation(st, returns)) {
1872 VISIT_QUIT(st, 0);
1873 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875}
1876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878symtable_visit_arguments(struct symtable *st, arguments_ty a)
1879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 /* skip default arguments inside function block
1881 XXX should ast be different?
1882 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001883 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1884 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 if (a->args && !symtable_visit_params(st, a->args))
1886 return 0;
1887 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1888 return 0;
1889 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001890 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 return 0;
1892 st->st_cur->ste_varargs = 1;
1893 }
1894 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001895 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 return 0;
1897 st->st_cur->ste_varkeywords = 1;
1898 }
1899 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900}
1901
1902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (eh->v.ExceptHandler.type)
1907 VISIT(st, expr, eh->v.ExceptHandler.type);
1908 if (eh->v.ExceptHandler.name)
1909 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1910 return 0;
1911 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1912 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913}
1914
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001915static int
1916symtable_visit_withitem(struct symtable *st, withitem_ty item)
1917{
1918 VISIT(st, expr, item->context_expr);
1919 if (item->optional_vars) {
1920 VISIT(st, expr, item->optional_vars);
1921 }
1922 return 1;
1923}
1924
Brandt Bucher145bf262021-02-26 14:51:55 -08001925static int
1926symtable_visit_match_case(struct symtable *st, match_case_ty m)
1927{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001928 VISIT(st, pattern, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08001929 if (m->guard) {
1930 VISIT(st, expr, m->guard);
1931 }
1932 VISIT_SEQ(st, stmt, m->body);
1933 return 1;
1934}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937symtable_visit_alias(struct symtable *st, alias_ty a)
1938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001940 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 dotted package name (e.g. spam.eggs)
1942 */
1943 PyObject *store_name;
1944 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001945 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1946 PyUnicode_GET_LENGTH(name), 1);
1947 if (dot != -1) {
1948 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 if (!store_name)
1950 return 0;
1951 }
1952 else {
1953 store_name = name;
1954 Py_INCREF(store_name);
1955 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001956 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1958 Py_DECREF(store_name);
1959 return r;
1960 }
1961 else {
1962 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001963 int lineno = st->st_cur->ste_lineno;
1964 int col_offset = st->st_cur->ste_col_offset;
Pablo Galindoa77aac42021-04-23 14:27:05 +01001965 int end_lineno = st->st_cur->ste_end_lineno;
1966 int end_col_offset = st->st_cur->ste_end_col_offset;
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001967 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001968 PyErr_RangedSyntaxLocationObject(st->st_filename,
1969 lineno, col_offset + 1,
1970 end_lineno, end_col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001971 Py_DECREF(store_name);
1972 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 Py_DECREF(store_name);
1975 return 1;
1976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977}
1978
1979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1982{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001983 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001985 st->st_cur->ste_comp_iter_target = 0;
1986 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001988 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001990 if (lc->is_async) {
1991 st->st_cur->ste_coroutine = 1;
1992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994}
1995
1996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998symtable_visit_keyword(struct symtable *st, keyword_ty k)
1999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 VISIT(st, expr, k->value);
2001 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002}
2003
2004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002006symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01002007 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002008 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 comprehension_ty outermost = ((comprehension_ty)
2012 asdl_seq_GET(generators, 0));
2013 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002014 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002016 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 /* Create comprehension scope for the rest */
2018 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002019 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
Pablo Galindoa77aac42021-04-23 14:27:05 +01002020 e->lineno, e->col_offset,
2021 e->end_lineno, e->end_col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 return 0;
2023 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002024 if (outermost->is_async) {
2025 st->st_cur->ste_coroutine = 1;
2026 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002027 st->st_cur->ste_comprehension = 1;
2028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 /* Outermost iter is received as an argument */
2030 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06002031 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 return 0;
2033 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002034 /* Visit iteration variable target, and mark them as such */
2035 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05002036 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002037 st->st_cur->ste_comp_iter_target = 0;
2038 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05002039 VISIT_SEQ(st, expr, outermost->ifs);
2040 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05002042 VISIT(st, expr, value);
2043 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02002044 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02002045 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02002046 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
2047 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
2048 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
2049 "'yield' inside generator expression");
Pablo Galindoa77aac42021-04-23 14:27:05 +01002050 PyErr_RangedSyntaxLocationObject(st->st_filename,
2051 st->st_cur->ste_lineno,
2052 st->st_cur->ste_col_offset + 1,
2053 st->st_cur->ste_end_lineno,
2054 st->st_cur->ste_end_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06002055 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02002056 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02002057 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02002058 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06002059 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002063symtable_visit_genexp(struct symtable *st, expr_ty e)
2064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
2066 e->v.GeneratorExp.generators,
2067 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002068}
2069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002071symtable_visit_listcomp(struct symtable *st, expr_ty e)
2072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
2074 e->v.ListComp.generators,
2075 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002076}
2077
2078static int
2079symtable_visit_setcomp(struct symtable *st, expr_ty e)
2080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
2082 e->v.SetComp.generators,
2083 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002084}
2085
2086static int
2087symtable_visit_dictcomp(struct symtable *st, expr_ty e)
2088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
2090 e->v.DictComp.generators,
2091 e->v.DictComp.key,
2092 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002093}
Victor Stinner28ad12f2021-03-19 12:41:49 +01002094
Batuhan Taskayaad106c62021-05-03 10:43:00 +03002095static int
2096symtable_raise_if_annotation_block(struct symtable *st, const char *name, expr_ty e)
2097{
2098 if (st->st_cur->ste_type != AnnotationBlock) {
2099 return 1;
2100 }
2101
2102 PyErr_Format(PyExc_SyntaxError, ANNOTATION_NOT_ALLOWED, name);
2103 PyErr_RangedSyntaxLocationObject(st->st_filename,
2104 e->lineno,
2105 e->col_offset + 1,
2106 e->end_lineno,
2107 e->end_col_offset + 1);
2108 return 0;
2109}
Victor Stinner28ad12f2021-03-19 12:41:49 +01002110
2111struct symtable *
2112_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
2113 int start, PyCompilerFlags *flags)
2114{
2115 struct symtable *st;
2116 mod_ty mod;
2117 PyArena *arena;
2118
Victor Stinner8370e072021-03-24 02:23:01 +01002119 arena = _PyArena_New();
Victor Stinner28ad12f2021-03-19 12:41:49 +01002120 if (arena == NULL)
2121 return NULL;
2122
Victor Stinner57364ce2021-03-24 01:29:09 +01002123 mod = _PyParser_ASTFromString(str, filename, start, flags, arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002124 if (mod == NULL) {
Victor Stinner8370e072021-03-24 02:23:01 +01002125 _PyArena_Free(arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002126 return NULL;
2127 }
Batuhan Taskayaad106c62021-05-03 10:43:00 +03002128 PyFutureFeatures *future = _PyFuture_FromAST(mod, filename);
2129 if (future == NULL) {
2130 _PyArena_Free(arena);
2131 return NULL;
2132 }
2133 future->ff_features |= flags->cf_flags;
2134 st = _PySymtable_Build(mod, filename, future);
2135 PyObject_Free((void *)future);
Victor Stinner8370e072021-03-24 02:23:01 +01002136 _PyArena_Free(arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002137 return st;
2138}