blob: 07f9d1132c797e0c9a43cd626362a6549007aca0 [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
Miss Islington (bot)438817f2021-12-11 17:24:12 -080052#define LOCATION(x) \
53 (x)->lineno, (x)->col_offset, (x)->end_lineno, (x)->end_col_offset
54
55#define ST_LOCATION(x) \
56 (x)->ste_lineno, (x)->ste_col_offset, (x)->ste_end_lineno, (x)->ste_end_col_offset
57
Neal Norwitz090b3dd2006-02-28 22:36:46 +000058static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000059ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Pablo Galindoa77aac42021-04-23 14:27:05 +010060 void *key, int lineno, int col_offset,
61 int end_lineno, int end_col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020064 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 k = PyLong_FromVoidPtr(key);
67 if (k == NULL)
68 goto fail;
69 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020070 if (ste == NULL) {
71 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020073 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020075 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020078 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 ste->ste_symbols = NULL;
81 ste->ste_varnames = NULL;
82 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083
Benjamin Petersond9c87022012-10-31 20:26:20 -040084 ste->ste_directives = NULL;
85
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 ste->ste_nested = 0;
88 ste->ste_free = 0;
89 ste->ste_varargs = 0;
90 ste->ste_varkeywords = 0;
91 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000092 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000094 ste->ste_col_offset = col_offset;
Pablo Galindoa77aac42021-04-23 14:27:05 +010095 ste->ste_end_lineno = end_lineno;
96 ste->ste_end_col_offset = end_col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (st->st_cur != NULL &&
99 (st->st_cur->ste_nested ||
100 st->st_cur->ste_type == FunctionBlock))
101 ste->ste_nested = 1;
102 ste->ste_child_free = 0;
103 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700104 ste->ste_coroutine = 0;
Miss Islington (bot)438817f2021-12-11 17:24:12 -0800105 ste->ste_comprehension = NoComprehension;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500107 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000108 ste->ste_comp_iter_target = 0;
109 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110
Victor Stinner9a4fb662013-07-11 22:49:00 +0200111 ste->ste_symbols = PyDict_New();
112 ste->ste_varnames = PyList_New(0);
113 ste->ste_children = PyList_New(0);
114 if (ste->ste_symbols == NULL
115 || ste->ste_varnames == NULL
116 || ste->ste_children == NULL)
117 goto fail;
118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
120 goto fail;
121
122 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 Py_XDECREF(ste);
125 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126}
127
128static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
132 ste->ste_name,
133 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134}
135
136static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 ste->ste_table = NULL;
140 Py_XDECREF(ste->ste_id);
141 Py_XDECREF(ste->ste_name);
142 Py_XDECREF(ste->ste_symbols);
143 Py_XDECREF(ste->ste_varnames);
144 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400145 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100146 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000147}
148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000150
Guido van Rossum6f799372001-09-20 20:46:19 +0000151static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 {"id", T_OBJECT, OFF(ste_id), READONLY},
153 {"name", T_OBJECT, OFF(ste_name), READONLY},
154 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
155 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
156 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 {"nested", T_INT, OFF(ste_nested), READONLY},
158 {"type", T_INT, OFF(ste_type), READONLY},
159 {"lineno", T_INT, OFF(ste_lineno), READONLY},
160 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 PyVarObject_HEAD_INIT(&PyType_Type, 0)
165 "symtable entry",
166 sizeof(PySTEntryObject),
167 0,
168 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200169 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 0, /* tp_getattr */
171 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200172 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 (reprfunc)ste_repr, /* tp_repr */
174 0, /* tp_as_number */
175 0, /* tp_as_sequence */
176 0, /* tp_as_mapping */
177 0, /* tp_hash */
178 0, /* tp_call */
179 0, /* tp_str */
180 PyObject_GenericGetAttr, /* tp_getattro */
181 0, /* tp_setattro */
182 0, /* tp_as_buffer */
183 Py_TPFLAGS_DEFAULT, /* tp_flags */
184 0, /* tp_doc */
185 0, /* tp_traverse */
186 0, /* tp_clear */
187 0, /* tp_richcompare */
188 0, /* tp_weaklistoffset */
189 0, /* tp_iter */
190 0, /* tp_iternext */
191 0, /* tp_methods */
192 ste_memberlist, /* tp_members */
193 0, /* tp_getset */
194 0, /* tp_base */
195 0, /* tp_dict */
196 0, /* tp_descr_get */
197 0, /* tp_descr_set */
198 0, /* tp_dictoffset */
199 0, /* tp_init */
200 0, /* tp_alloc */
201 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000202};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
204static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205static int symtable_enter_block(struct symtable *st, identifier name,
Pablo Galindoa77aac42021-04-23 14:27:05 +0100206 _Py_block_ty block, void *ast,
207 int lineno, int col_offset,
208 int end_lineno, int end_col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600209static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
211static int symtable_visit_expr(struct symtable *st, expr_ty s);
212static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000213static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
214static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000215static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216static int symtable_visit_arguments(struct symtable *st, arguments_ty);
217static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
218static int symtable_visit_alias(struct symtable *st, alias_ty);
219static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
220static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100221static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300222static int symtable_visit_annotation(struct symtable *st, expr_ty annotation);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100223static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224static int symtable_implicit_arg(struct symtable *st, int pos);
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300225static int symtable_visit_annotations(struct symtable *st, stmt_ty, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500226static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Brandt Bucher145bf262021-02-26 14:51:55 -0800227static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000228static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300229static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
Miss Islington (bot)438817f2021-12-11 17:24:12 -0800230static int symtable_raise_if_comprehension_block(struct symtable *st, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231
232
Nick Coghlan650f0d02007-04-15 12:05:43 +0000233static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Batuhan Taskayaad106c62021-05-03 10:43:00 +0300235 __class__ = NULL, _annotation = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236
237#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239
240#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000241"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242
243static struct symtable *
244symtable_new(void)
245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700249 if (st == NULL) {
250 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700252 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 st->st_filename = NULL;
255 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if ((st->st_stack = PyList_New(0)) == NULL)
258 goto fail;
259 if ((st->st_blocks = PyDict_New()) == NULL)
260 goto fail;
261 st->st_cur = NULL;
262 st->st_private = NULL;
263 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 fail:
Victor Stinner28ad12f2021-03-19 12:41:49 +0100265 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267}
268
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000269/* When compiling the use of C stack is probably going to be a lot
270 lighter than when executing Python code but still can overflow
271 and causing a Python crash if not checked (e.g. eval("()"*300000)).
272 Using the current recursion limit for the compiler seems too
273 restrictive (it caused at least one test to fail) so a factor is
274 used to allow deeper recursion when compiling an expression.
275
276 Using a scaling factor means this should automatically adjust when
277 the recursion limit is adjusted for small or large C stack allocations.
278*/
279#define COMPILER_STACK_FRAME_SCALE 3
280
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281struct symtable *
Victor Stinner28ad12f2021-03-19 12:41:49 +0100282_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000284 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100285 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000287 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400288 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000289 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200292 return NULL;
293 if (filename == NULL) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100294 _PySymtable_Free(st);
Victor Stinner14e461d2013-08-26 22:28:21 +0200295 return NULL;
296 }
297 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 st->st_filename = filename;
299 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000300
301 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100302 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000303 if (!tstate) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100304 _PySymtable_Free(st);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000305 return NULL;
306 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400307 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000308 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400309 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000310 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400311 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
312 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* Make the initial symbol information gathering pass */
315 if (!GET_IDENTIFIER(top) ||
Pablo Galindoa77aac42021-04-23 14:27:05 +0100316 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0, 0, 0)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100317 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 return NULL;
319 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 switch (mod->kind) {
323 case Module_kind:
324 seq = mod->v.Module.body;
325 for (i = 0; i < asdl_seq_LEN(seq); i++)
326 if (!symtable_visit_stmt(st,
327 (stmt_ty)asdl_seq_GET(seq, i)))
328 goto error;
329 break;
330 case Expression_kind:
331 if (!symtable_visit_expr(st, mod->v.Expression.body))
332 goto error;
333 break;
334 case Interactive_kind:
335 seq = mod->v.Interactive.body;
336 for (i = 0; i < asdl_seq_LEN(seq); i++)
337 if (!symtable_visit_stmt(st,
338 (stmt_ty)asdl_seq_GET(seq, i)))
339 goto error;
340 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800341 case FunctionType_kind:
342 PyErr_SetString(PyExc_RuntimeError,
343 "this compiler does not handle FunctionTypes");
344 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 }
Andy Lester95668422020-03-06 09:46:04 -0600346 if (!symtable_exit_block(st)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100347 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return NULL;
349 }
Nick Coghlan06145232019-08-29 23:26:53 +1000350 /* Check that the recursion depth counting balanced correctly */
351 if (st->recursion_depth != starting_recursion_depth) {
352 PyErr_Format(PyExc_SystemError,
353 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
354 starting_recursion_depth, st->recursion_depth);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100355 _PySymtable_Free(st);
Nick Coghlan06145232019-08-29 23:26:53 +1000356 return NULL;
357 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* Make the second symbol analysis pass */
359 if (symtable_analyze(st))
360 return st;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100361 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363 error:
Andy Lester95668422020-03-06 09:46:04 -0600364 (void) symtable_exit_block(st);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100365 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367}
368
Victor Stinner14e461d2013-08-26 22:28:21 +0200369
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370void
Victor Stinner28ad12f2021-03-19 12:41:49 +0100371_PySymtable_Free(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372{
Victor Stinner14e461d2013-08-26 22:28:21 +0200373 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 Py_XDECREF(st->st_blocks);
375 Py_XDECREF(st->st_stack);
376 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377}
378
379PySTEntryObject *
380PySymtable_Lookup(struct symtable *st, void *key)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 k = PyLong_FromVoidPtr(key);
385 if (k == NULL)
386 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200387 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (v) {
389 assert(PySTEntry_Check(v));
390 Py_INCREF(v);
391 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200392 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 PyErr_SetString(PyExc_KeyError,
394 "unknown symbol table entry");
395 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_DECREF(k);
398 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399}
400
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000401static long
402_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200404 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (!v)
406 return 0;
407 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000408 return PyLong_AS_LONG(v);
409}
410
411int
Victor Stinner28ad12f2021-03-19 12:41:49 +0100412_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000413{
414 long symbol = _PyST_GetSymbol(ste, name);
415 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416}
417
Benjamin Petersond9c87022012-10-31 20:26:20 -0400418static int
419error_at_directive(PySTEntryObject *ste, PyObject *name)
420{
421 Py_ssize_t i;
422 PyObject *data;
423 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600424 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400425 data = PyList_GET_ITEM(ste->ste_directives, i);
426 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600427 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
428 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
Pablo Galindoa77aac42021-04-23 14:27:05 +0100429 PyErr_RangedSyntaxLocationObject(ste->ste_table->st_filename,
430 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
431 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1,
432 PyLong_AsLong(PyTuple_GET_ITEM(data, 3)),
433 PyLong_AsLong(PyTuple_GET_ITEM(data, 4)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600434
435 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700436 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400437 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600438 PyErr_SetString(PyExc_RuntimeError,
439 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400440 return 0;
441}
442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443
444/* Analyze raw symbol information to determine scope of each name.
445
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000446 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 explicit global is declared with the global statement. An implicit
453 global is a free variable for which the compiler has found no binding
454 in an enclosing function scope. The implicit global is either a global
455 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
456 to handle these names to implement slightly odd semantics. In such a
457 block, the name is treated as global until it is assigned to; then it
458 is treated as a local.
459
460 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000461 The first pass collects raw facts from the AST via the symtable_visit_*
462 functions: the name is a parameter here, the name is used but not defined
463 here, etc. The second pass analyzes these facts during a pass over the
464 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465
466 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000468 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000469 Names which are explicitly declared nonlocal must exist in this set of
470 visible names - if they do not, a syntax error is raised. After doing
471 the local analysis, it analyzes each of its child blocks using an
472 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
Nick Coghlan650f0d02007-04-15 12:05:43 +0000474 The children update the free variable set. If a local variable is added to
475 the free variable set by the child, the variable is marked as a cell. The
476 function object being defined must provide runtime storage for the variable
477 that may outlive the function's frame. Cell variables are removed from the
478 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000479
Nick Coghlan650f0d02007-04-15 12:05:43 +0000480 During analysis, the names are:
481 symbols: dict mapping from symbol names to flag values (including offset scope values)
482 scopes: dict mapping from symbol names to scope values (no offset)
483 local: set of all symbol names local to the current scope
484 bound: set of all symbol names local to a containing function scope
485 free: set of all symbol names referenced but not bound in child scopes
486 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487*/
488
489#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyObject *o = PyLong_FromLong(I); \
491 if (!o) \
492 return 0; \
493 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
494 Py_DECREF(o); \
495 return 0; \
496 } \
497 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498}
499
500/* Decide on scope of name, given flags.
501
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000502 The namespace dictionaries may be modified to record information
503 about the new name. For example, a new global will add an entry to
504 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505*/
506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000508analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 PyObject *bound, PyObject *local, PyObject *free,
510 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (flags & DEF_NONLOCAL) {
514 PyErr_Format(PyExc_SyntaxError,
515 "name '%U' is nonlocal and global",
516 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400517 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 }
519 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
520 if (PySet_Add(global, name) < 0)
521 return 0;
522 if (bound && (PySet_Discard(bound, name) < 0))
523 return 0;
524 return 1;
525 }
526 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (!bound) {
528 PyErr_Format(PyExc_SyntaxError,
529 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400530 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 }
532 if (!PySet_Contains(bound, name)) {
533 PyErr_Format(PyExc_SyntaxError,
534 "no binding for nonlocal '%U' found",
535 name);
536
Benjamin Petersond9c87022012-10-31 20:26:20 -0400537 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 }
539 SET_SCOPE(scopes, name, FREE);
540 ste->ste_free = 1;
541 return PySet_Add(free, name) >= 0;
542 }
543 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000544 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (PySet_Add(local, name) < 0)
546 return 0;
547 if (PySet_Discard(global, name) < 0)
548 return 0;
549 return 1;
550 }
551 /* If an enclosing block has a binding for this name, it
552 is a free variable rather than a global variable.
553 Note that having a non-NULL bound implies that the block
554 is nested.
555 */
556 if (bound && PySet_Contains(bound, name)) {
557 SET_SCOPE(scopes, name, FREE);
558 ste->ste_free = 1;
559 return PySet_Add(free, name) >= 0;
560 }
561 /* If a parent has a global statement, then call it global
562 explicit? It could also be global implicit.
563 */
564 if (global && PySet_Contains(global, name)) {
565 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
566 return 1;
567 }
568 if (ste->ste_nested)
569 ste->ste_free = 1;
570 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
571 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572}
573
574#undef SET_SCOPE
575
576/* If a name is defined in free and also in locals, then this block
577 provides the binding for the free variable. The name should be
578 marked CELL in this block and removed from the free list.
579
580 Note that the current block's free variables are included in free.
581 That's safe because no name can be free and local in the same scope.
582*/
583
584static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500585analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyObject *name, *v, *v_cell;
588 int success = 0;
589 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 v_cell = PyLong_FromLong(CELL);
592 if (!v_cell)
593 return 0;
594 while (PyDict_Next(scopes, &pos, &name, &v)) {
595 long scope;
596 assert(PyLong_Check(v));
597 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000598 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 continue;
600 if (!PySet_Contains(free, name))
601 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 /* Replace LOCAL with CELL for this name, and remove
603 from free. It is safe to replace the value of name
604 in the dict, because it will not cause a resize.
605 */
606 if (PyDict_SetItem(scopes, name, v_cell) < 0)
607 goto error;
608 if (PySet_Discard(free, name) < 0)
609 goto error;
610 }
611 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 Py_DECREF(v_cell);
614 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615}
616
Benjamin Peterson312595c2013-05-15 15:26:42 -0500617static int
618drop_class_free(PySTEntryObject *ste, PyObject *free)
619{
620 int res;
621 if (!GET_IDENTIFIER(__class__))
622 return 0;
623 res = PySet_Discard(free, __class__);
624 if (res < 0)
625 return 0;
626 if (res)
627 ste->ste_needs_class_closure = 1;
628 return 1;
629}
630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631/* Enter the final scope information into the ste_symbols dict.
632 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 * All arguments are dicts. Modifies symbols, others are read-only.
634*/
635static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyObject *name = NULL, *itr = NULL;
640 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
641 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 /* Update scope information for all symbols in this scope */
644 while (PyDict_Next(symbols, &pos, &name, &v)) {
645 long scope, flags;
646 assert(PyLong_Check(v));
647 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200648 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 assert(v_scope && PyLong_Check(v_scope));
650 scope = PyLong_AS_LONG(v_scope);
651 flags |= (scope << SCOPE_OFFSET);
652 v_new = PyLong_FromLong(flags);
653 if (!v_new)
654 return 0;
655 if (PyDict_SetItem(symbols, name, v_new) < 0) {
656 Py_DECREF(v_new);
657 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 Py_DECREF(v_new);
660 }
661
662 /* Record not yet resolved free variables from children (if any) */
663 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
664 if (!v_free)
665 return 0;
666
667 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600668 if (itr == NULL) {
669 Py_DECREF(v_free);
670 return 0;
671 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672
673 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200674 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675
676 /* Handle symbol that already exists in this scope */
677 if (v) {
678 /* Handle a free variable in a method of
679 the class that has the same name as a local
680 or global in the class scope.
681 */
682 if (classflag &&
683 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
684 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
685 v_new = PyLong_FromLong(flags);
686 if (!v_new) {
687 goto error;
688 }
689 if (PyDict_SetItem(symbols, name, v_new) < 0) {
690 Py_DECREF(v_new);
691 goto error;
692 }
693 Py_DECREF(v_new);
694 }
695 /* It's a cell, or already free in this scope */
696 Py_DECREF(name);
697 continue;
698 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200699 else if (PyErr_Occurred()) {
700 goto error;
701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200703 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 Py_DECREF(name);
705 continue; /* it's a global */
706 }
707 /* Propagate new free symbol up the lexical stack */
708 if (PyDict_SetItem(symbols, name, v_free) < 0) {
709 goto error;
710 }
711 Py_DECREF(name);
712 }
713 Py_DECREF(itr);
714 Py_DECREF(v_free);
715 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000716error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 Py_XDECREF(v_free);
718 Py_XDECREF(itr);
719 Py_XDECREF(name);
720 return 0;
721}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722
723/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000724
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 Arguments:
726 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000727 bound -- set of variables bound in enclosing scopes (input). bound
728 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 free -- set of free variables in enclosed scopes (output)
730 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000731
732 The implementation uses two mutually recursive functions,
733 analyze_block() and analyze_child_block(). analyze_block() is
734 responsible for analyzing the individual names defined in a block.
735 analyze_child_block() prepares temporary namespace dictionaries
736 used to evaluated nested blocks.
737
738 The two functions exist because a child block should see the name
739 bindings of its enclosing blocks, but those bindings should not
740 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741*/
742
743static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
745 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000746
747static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
749 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
752 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
753 PyObject *temp;
754 int i, success = 0;
755 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 local = PySet_New(NULL); /* collect new names bound in block */
758 if (!local)
759 goto error;
760 scopes = PyDict_New(); /* collect scopes defined for each name */
761 if (!scopes)
762 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 /* Allocate new global and bound variable dictionaries. These
765 dictionaries hold the names visible in nested blocks. For
766 ClassBlocks, the bound and global names are initialized
767 before analyzing names, because class bindings aren't
768 visible in methods. For other blocks, they are initialized
769 after names are analyzed.
770 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 /* TODO(jhylton): Package these dicts in a struct so that we
773 can write reasonable helper functions?
774 */
775 newglobal = PySet_New(NULL);
776 if (!newglobal)
777 goto error;
778 newfree = PySet_New(NULL);
779 if (!newfree)
780 goto error;
781 newbound = PySet_New(NULL);
782 if (!newbound)
783 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 /* Class namespace has no effect on names visible in
786 nested functions, so populate the global and bound
787 sets to be passed to child blocks before analyzing
788 this one.
789 */
790 if (ste->ste_type == ClassBlock) {
791 /* Pass down known globals */
792 temp = PyNumber_InPlaceOr(newglobal, global);
793 if (!temp)
794 goto error;
795 Py_DECREF(temp);
796 /* Pass down previously bound symbols */
797 if (bound) {
798 temp = PyNumber_InPlaceOr(newbound, bound);
799 if (!temp)
800 goto error;
801 Py_DECREF(temp);
802 }
803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
806 long flags = PyLong_AS_LONG(v);
807 if (!analyze_name(ste, scopes, name, flags,
808 bound, local, free, global))
809 goto error;
810 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* Populate global and bound sets to be passed to children. */
813 if (ste->ste_type != ClassBlock) {
814 /* Add function locals to bound set */
815 if (ste->ste_type == FunctionBlock) {
816 temp = PyNumber_InPlaceOr(newbound, local);
817 if (!temp)
818 goto error;
819 Py_DECREF(temp);
820 }
821 /* Pass down previously bound symbols */
822 if (bound) {
823 temp = PyNumber_InPlaceOr(newbound, bound);
824 if (!temp)
825 goto error;
826 Py_DECREF(temp);
827 }
828 /* Pass down known globals */
829 temp = PyNumber_InPlaceOr(newglobal, global);
830 if (!temp)
831 goto error;
832 Py_DECREF(temp);
833 }
834 else {
835 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000836 if (!GET_IDENTIFIER(__class__))
837 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (PySet_Add(newbound, __class__) < 0)
839 goto error;
840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300842 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 newbound, newglobal now contain the names visible in
845 nested blocks. The free variables in the children will
846 be collected in allfree.
847 */
848 allfree = PySet_New(NULL);
849 if (!allfree)
850 goto error;
851 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
852 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
853 PySTEntryObject* entry;
854 assert(c && PySTEntry_Check(c));
855 entry = (PySTEntryObject*)c;
856 if (!analyze_child_block(entry, newbound, newfree, newglobal,
857 allfree))
858 goto error;
859 /* Check if any children have free variables */
860 if (entry->ste_free || entry->ste_child_free)
861 ste->ste_child_free = 1;
862 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 temp = PyNumber_InPlaceOr(newfree, allfree);
865 if (!temp)
866 goto error;
867 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500870 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500872 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 goto error;
874 /* Records the results of the analysis in the symbol table entry */
875 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
876 ste->ste_type == ClassBlock))
877 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 temp = PyNumber_InPlaceOr(free, newfree);
880 if (!temp)
881 goto error;
882 Py_DECREF(temp);
883 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_XDECREF(scopes);
886 Py_XDECREF(local);
887 Py_XDECREF(newbound);
888 Py_XDECREF(newglobal);
889 Py_XDECREF(newfree);
890 Py_XDECREF(allfree);
891 if (!success)
892 assert(PyErr_Occurred());
893 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894}
895
896static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
898 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
901 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000904
Martin Panter3ee62702016-06-04 04:57:19 +0000905 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 current block. The analyze_block() call modifies these
907 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 */
910 temp_bound = PySet_New(bound);
911 if (!temp_bound)
912 goto error;
913 temp_free = PySet_New(free);
914 if (!temp_free)
915 goto error;
916 temp_global = PySet_New(global);
917 if (!temp_global)
918 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
921 goto error;
922 temp = PyNumber_InPlaceOr(child_free, temp_free);
923 if (!temp)
924 goto error;
925 Py_DECREF(temp);
926 Py_DECREF(temp_bound);
927 Py_DECREF(temp_free);
928 Py_DECREF(temp_global);
929 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000930 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Py_XDECREF(temp_bound);
932 Py_XDECREF(temp_free);
933 Py_XDECREF(temp_global);
934 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000935}
936
937static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938symtable_analyze(struct symtable *st)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 PyObject *free, *global;
941 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 free = PySet_New(NULL);
944 if (!free)
945 return 0;
946 global = PySet_New(NULL);
947 if (!global) {
948 Py_DECREF(free);
949 return 0;
950 }
951 r = analyze_block(st->st_top, NULL, free, global);
952 Py_DECREF(free);
953 Py_DECREF(global);
954 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955}
956
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000957/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 This reference is released when the block is exited, via the DECREF
959 in symtable_exit_block().
960*/
961
962static int
Andy Lester95668422020-03-06 09:46:04 -0600963symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964{
Benjamin Peterson609da582011-06-29 22:52:39 -0500965 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
Benjamin Peterson609da582011-06-29 22:52:39 -0500967 st->st_cur = NULL;
968 size = PyList_GET_SIZE(st->st_stack);
969 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500970 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500972 if (--size)
973 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 }
975 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976}
977
978static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Pablo Galindoa77aac42021-04-23 14:27:05 +0100980 void *ast, int lineno, int col_offset,
981 int end_lineno, int end_col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982{
Benjamin Peterson609da582011-06-29 22:52:39 -0500983 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984
Pablo Galindoa77aac42021-04-23 14:27:05 +0100985 ste = ste_new(st, name, block, ast, lineno, col_offset, end_lineno, end_col_offset);
Benjamin Peterson609da582011-06-29 22:52:39 -0500986 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500988 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
989 Py_DECREF(ste);
990 return 0;
991 }
992 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000993 /* bpo-37757: For now, disallow *all* assignment expressions in the
994 * outermost iterator expression of a comprehension, even those inside
995 * a nested comprehension or a lambda expression.
996 */
997 if (prev) {
998 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
999 }
Benjamin Peterson609da582011-06-29 22:52:39 -05001000 /* The entry is owned by the stack. Borrow it for st_cur. */
1001 Py_DECREF(ste);
1002 st->st_cur = ste;
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001003
1004 /* Annotation blocks shouldn't have any affect on the symbol table since in
1005 * the compilation stage, they will all be transformed to strings. They are
1006 * only created if future 'annotations' feature is activated. */
1007 if (block == AnnotationBlock) {
1008 return 1;
1009 }
1010
Benjamin Peterson230b2062010-10-16 03:45:45 +00001011 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 st->st_global = st->st_cur->ste_symbols;
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -05001015 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 return 0;
1017 }
1018 }
1019 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020}
1021
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001022static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023symtable_lookup(struct symtable *st, PyObject *name)
1024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 PyObject *mangled = _Py_Mangle(st->st_private, name);
1026 if (!mangled)
1027 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +01001028 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1029 Py_DECREF(mangled);
1030 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031}
1032
1033static int
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001034symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste,
1035 int lineno, int col_offset, int end_lineno, int end_col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 PyObject *o;
1038 PyObject *dict;
1039 long val;
1040 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Jeremy Hylton81e95022007-02-27 06:50:52 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 if (!mangled)
1044 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001045 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001046 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 val = PyLong_AS_LONG(o);
1048 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1049 /* Is it better to use 'mangled' or 'name' here? */
1050 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001051 PyErr_RangedSyntaxLocationObject(st->st_filename,
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001052 lineno, col_offset + 1,
1053 end_lineno, end_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 goto error;
1055 }
1056 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001057 }
1058 else if (PyErr_Occurred()) {
1059 goto error;
1060 }
1061 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001063 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001064 if (ste->ste_comp_iter_target) {
1065 /* This name is an iteration variable in a comprehension,
1066 * so check for a binding conflict with any named expressions.
1067 * Otherwise, mark it as an iteration variable so subsequent
1068 * named expressions can check for conflicts.
1069 */
1070 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1071 PyErr_Format(PyExc_SyntaxError,
1072 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001073 PyErr_RangedSyntaxLocationObject(st->st_filename,
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001074 lineno, col_offset + 1,
1075 end_lineno, end_col_offset + 1);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001076 goto error;
1077 }
1078 val |= DEF_COMP_ITER;
1079 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 o = PyLong_FromLong(val);
1081 if (o == NULL)
1082 goto error;
1083 if (PyDict_SetItem(dict, mangled, o) < 0) {
1084 Py_DECREF(o);
1085 goto error;
1086 }
1087 Py_DECREF(o);
1088
1089 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001090 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 goto error;
1092 } else if (flag & DEF_GLOBAL) {
1093 /* XXX need to update DEF_GLOBAL for other flags too;
1094 perhaps only DEF_FREE_GLOBAL */
1095 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001096 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 val |= PyLong_AS_LONG(o);
1098 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001099 else if (PyErr_Occurred()) {
1100 goto error;
1101 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 goto error;
1105 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1106 Py_DECREF(o);
1107 goto error;
1108 }
1109 Py_DECREF(o);
1110 }
1111 Py_DECREF(mangled);
1112 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001113
1114error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 Py_DECREF(mangled);
1116 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117}
1118
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001119static int
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001120symtable_add_def(struct symtable *st, PyObject *name, int flag,
1121 int lineno, int col_offset, int end_lineno, int end_col_offset)
1122{
1123 return symtable_add_def_helper(st, name, flag, st->st_cur,
1124 lineno, col_offset, end_lineno, end_col_offset);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001125}
1126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1128 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 function.
1130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1132 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001133
1134 VISIT_QUIT macro returns the specified value exiting from the function but
1135 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136*/
1137
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001138#define VISIT_QUIT(ST, X) \
1139 return --(ST)->recursion_depth,(X)
1140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001143 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001147 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1149 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1150 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001151 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001157 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1159 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1160 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001164
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001165#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001167 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001169 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001171 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001172 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001174}
1175
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176static int
Pablo Galindoa77aac42021-04-23 14:27:05 +01001177symtable_record_directive(struct symtable *st, identifier name, int lineno,
1178 int col_offset, int end_lineno, int end_col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001179{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001180 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001181 int res;
1182 if (!st->st_cur->ste_directives) {
1183 st->st_cur->ste_directives = PyList_New(0);
1184 if (!st->st_cur->ste_directives)
1185 return 0;
1186 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001187 mangled = _Py_Mangle(st->st_private, name);
1188 if (!mangled)
1189 return 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +01001190 data = Py_BuildValue("(Niiii)", mangled, lineno, col_offset, end_lineno, end_col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001191 if (!data)
1192 return 0;
1193 res = PyList_Append(st->st_cur->ste_directives, data);
1194 Py_DECREF(data);
1195 return res == 0;
1196}
1197
1198
1199static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200symtable_visit_stmt(struct symtable *st, stmt_ty s)
1201{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001202 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001203 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001204 "maximum recursion depth exceeded during compilation");
1205 VISIT_QUIT(st, 0);
1206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 switch (s->kind) {
1208 case FunctionDef_kind:
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001209 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL, LOCATION(s)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001210 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (s->v.FunctionDef.args->defaults)
1212 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1213 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001214 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001215 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001216 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001217 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (s->v.FunctionDef.decorator_list)
1219 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1220 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001221 FunctionBlock, (void *)s,
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001222 LOCATION(s)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001223 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001224 VISIT(st, arguments, s->v.FunctionDef.args);
1225 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001226 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001227 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 break;
1229 case ClassDef_kind: {
1230 PyObject *tmp;
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001231 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL, LOCATION(s)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001232 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1234 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 if (s->v.ClassDef.decorator_list)
1236 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1237 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001238 (void *)s, s->lineno, s->col_offset,
1239 s->end_lineno, s->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001240 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 tmp = st->st_private;
1242 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001243 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001245 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001246 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 break;
1248 }
1249 case Return_kind:
1250 if (s->v.Return.value) {
1251 VISIT(st, expr, s->v.Return.value);
1252 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 }
1254 break;
1255 case Delete_kind:
1256 VISIT_SEQ(st, expr, s->v.Delete.targets);
1257 break;
1258 case Assign_kind:
1259 VISIT_SEQ(st, expr, s->v.Assign.targets);
1260 VISIT(st, expr, s->v.Assign.value);
1261 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001262 case AnnAssign_kind:
1263 if (s->v.AnnAssign.target->kind == Name_kind) {
1264 expr_ty e_name = s->v.AnnAssign.target;
1265 long cur = symtable_lookup(st, e_name->v.Name.id);
1266 if (cur < 0) {
1267 VISIT_QUIT(st, 0);
1268 }
1269 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001270 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001271 && s->v.AnnAssign.simple) {
1272 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001273 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1274 e_name->v.Name.id);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001275 PyErr_RangedSyntaxLocationObject(st->st_filename,
1276 s->lineno,
1277 s->col_offset + 1,
1278 s->end_lineno,
1279 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001280 VISIT_QUIT(st, 0);
1281 }
1282 if (s->v.AnnAssign.simple &&
1283 !symtable_add_def(st, e_name->v.Name.id,
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001284 DEF_ANNOT | DEF_LOCAL, LOCATION(e_name))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001285 VISIT_QUIT(st, 0);
1286 }
1287 else {
1288 if (s->v.AnnAssign.value
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001289 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL, LOCATION(e_name))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001290 VISIT_QUIT(st, 0);
1291 }
1292 }
1293 }
1294 else {
1295 VISIT(st, expr, s->v.AnnAssign.target);
1296 }
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001297 if (!symtable_visit_annotation(st, s->v.AnnAssign.annotation)) {
1298 VISIT_QUIT(st, 0);
1299 }
1300
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001301 if (s->v.AnnAssign.value) {
1302 VISIT(st, expr, s->v.AnnAssign.value);
1303 }
1304 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 case AugAssign_kind:
1306 VISIT(st, expr, s->v.AugAssign.target);
1307 VISIT(st, expr, s->v.AugAssign.value);
1308 break;
1309 case For_kind:
1310 VISIT(st, expr, s->v.For.target);
1311 VISIT(st, expr, s->v.For.iter);
1312 VISIT_SEQ(st, stmt, s->v.For.body);
1313 if (s->v.For.orelse)
1314 VISIT_SEQ(st, stmt, s->v.For.orelse);
1315 break;
1316 case While_kind:
1317 VISIT(st, expr, s->v.While.test);
1318 VISIT_SEQ(st, stmt, s->v.While.body);
1319 if (s->v.While.orelse)
1320 VISIT_SEQ(st, stmt, s->v.While.orelse);
1321 break;
1322 case If_kind:
1323 /* XXX if 0: and lookup_yield() hacks */
1324 VISIT(st, expr, s->v.If.test);
1325 VISIT_SEQ(st, stmt, s->v.If.body);
1326 if (s->v.If.orelse)
1327 VISIT_SEQ(st, stmt, s->v.If.orelse);
1328 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001329 case Match_kind:
1330 VISIT(st, expr, s->v.Match.subject);
1331 VISIT_SEQ(st, match_case, s->v.Match.cases);
1332 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 case Raise_kind:
1334 if (s->v.Raise.exc) {
1335 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001336 if (s->v.Raise.cause) {
1337 VISIT(st, expr, s->v.Raise.cause);
1338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 }
1340 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001341 case Try_kind:
1342 VISIT_SEQ(st, stmt, s->v.Try.body);
1343 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1344 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1345 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 break;
1347 case Assert_kind:
1348 VISIT(st, expr, s->v.Assert.test);
1349 if (s->v.Assert.msg)
1350 VISIT(st, expr, s->v.Assert.msg);
1351 break;
1352 case Import_kind:
1353 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 break;
1355 case ImportFrom_kind:
1356 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 break;
1358 case Global_kind: {
1359 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001360 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1362 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 long cur = symtable_lookup(st, name);
1364 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001365 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001366 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1367 const char* msg;
1368 if (cur & DEF_PARAM) {
1369 msg = GLOBAL_PARAM;
1370 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001371 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001372 } else if (cur & DEF_ANNOT) {
1373 msg = GLOBAL_ANNOT;
1374 } else { /* DEF_LOCAL */
1375 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001376 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001377 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001378 msg, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001379 PyErr_RangedSyntaxLocationObject(st->st_filename,
1380 s->lineno,
1381 s->col_offset + 1,
1382 s->end_lineno,
1383 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001384 VISIT_QUIT(st, 0);
1385 }
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001386 if (!symtable_add_def(st, name, DEF_GLOBAL, LOCATION(s)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001387 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001388 if (!symtable_record_directive(st, name, s->lineno, s->col_offset,
1389 s->end_lineno, s->end_col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001390 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 }
1392 break;
1393 }
1394 case Nonlocal_kind: {
1395 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001396 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1398 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 long cur = symtable_lookup(st, name);
1400 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001401 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001402 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1403 const char* msg;
1404 if (cur & DEF_PARAM) {
1405 msg = NONLOCAL_PARAM;
1406 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001407 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001408 } else if (cur & DEF_ANNOT) {
1409 msg = NONLOCAL_ANNOT;
1410 } else { /* DEF_LOCAL */
1411 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001412 }
1413 PyErr_Format(PyExc_SyntaxError, msg, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001414 PyErr_RangedSyntaxLocationObject(st->st_filename,
1415 s->lineno,
1416 s->col_offset + 1,
1417 s->end_lineno,
1418 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001419 VISIT_QUIT(st, 0);
1420 }
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001421 if (!symtable_add_def(st, name, DEF_NONLOCAL, LOCATION(s)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001422 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001423 if (!symtable_record_directive(st, name, s->lineno, s->col_offset,
1424 s->end_lineno, s->end_col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001425 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 }
1427 break;
1428 }
1429 case Expr_kind:
1430 VISIT(st, expr, s->v.Expr.value);
1431 break;
1432 case Pass_kind:
1433 case Break_kind:
1434 case Continue_kind:
1435 /* nothing to do here */
1436 break;
1437 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001438 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 VISIT_SEQ(st, stmt, s->v.With.body);
1440 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001441 case AsyncFunctionDef_kind:
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001442 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL, LOCATION(s)))
Yury Selivanov75445082015-05-11 22:57:16 -04001443 VISIT_QUIT(st, 0);
1444 if (s->v.AsyncFunctionDef.args->defaults)
1445 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1446 if (s->v.AsyncFunctionDef.args->kw_defaults)
1447 VISIT_SEQ_WITH_NULL(st, expr,
1448 s->v.AsyncFunctionDef.args->kw_defaults);
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001449 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001450 s->v.AsyncFunctionDef.returns))
1451 VISIT_QUIT(st, 0);
1452 if (s->v.AsyncFunctionDef.decorator_list)
1453 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1454 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001455 FunctionBlock, (void *)s,
1456 s->lineno, s->col_offset,
1457 s->end_lineno, s->end_col_offset))
Yury Selivanov75445082015-05-11 22:57:16 -04001458 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001459 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001460 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1461 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001462 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001463 VISIT_QUIT(st, 0);
1464 break;
1465 case AsyncWith_kind:
1466 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1467 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1468 break;
1469 case AsyncFor_kind:
1470 VISIT(st, expr, s->v.AsyncFor.target);
1471 VISIT(st, expr, s->v.AsyncFor.iter);
1472 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1473 if (s->v.AsyncFor.orelse)
1474 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1475 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001477 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478}
1479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001481symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1482{
1483 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001484 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001485
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001486 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001487 Py_ssize_t i, size;
1488 struct _symtable_entry *ste;
1489 size = PyList_GET_SIZE(st->st_stack);
1490 assert(size);
1491
1492 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1493 for (i = size - 1; i >= 0; i--) {
1494 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1495
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001496 /* If we find a comprehension scope, check for a target
1497 * binding conflict with iteration variables, otherwise skip it
1498 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001499 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001500 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1501 if (target_in_scope & DEF_COMP_ITER) {
1502 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001503 PyErr_RangedSyntaxLocationObject(st->st_filename,
1504 e->lineno,
1505 e->col_offset + 1,
1506 e->end_lineno,
1507 e->end_col_offset + 1);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001508 VISIT_QUIT(st, 0);
1509 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001510 continue;
1511 }
1512
Pablo Galindofd5c4142019-10-14 05:18:05 +01001513 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001514 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001515 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1516 if (target_in_scope & DEF_GLOBAL) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001517 if (!symtable_add_def(st, target_name, DEF_GLOBAL, LOCATION(e)))
Pablo Galindofd5c4142019-10-14 05:18:05 +01001518 VISIT_QUIT(st, 0);
1519 } else {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001520 if (!symtable_add_def(st, target_name, DEF_NONLOCAL, LOCATION(e)))
Pablo Galindofd5c4142019-10-14 05:18:05 +01001521 VISIT_QUIT(st, 0);
1522 }
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001523 if (!symtable_record_directive(st, target_name, LOCATION(e)))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001524 VISIT_QUIT(st, 0);
1525
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001526 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste, LOCATION(e));
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001527 }
1528 /* If we find a ModuleBlock entry, add as GLOBAL */
1529 if (ste->ste_type == ModuleBlock) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001530 if (!symtable_add_def(st, target_name, DEF_GLOBAL, LOCATION(e)))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001531 VISIT_QUIT(st, 0);
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001532 if (!symtable_record_directive(st, target_name, LOCATION(e)))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001533 VISIT_QUIT(st, 0);
1534
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001535 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste, LOCATION(e));
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001536 }
1537 /* Disallow usage in ClassBlock */
1538 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001539 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001540 PyErr_RangedSyntaxLocationObject(st->st_filename,
1541 e->lineno,
1542 e->col_offset + 1,
1543 e->end_lineno,
1544 e->end_col_offset + 1);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001545 VISIT_QUIT(st, 0);
1546 }
1547 }
1548
1549 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1550 and should never fall to this case
1551 */
1552 assert(0);
1553 return 0;
1554}
1555
1556static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001557symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1558{
1559 if (st->st_cur->ste_comp_iter_expr > 0) {
1560 /* Assignment isn't allowed in a comprehension iterable expression */
1561 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001562 PyErr_RangedSyntaxLocationObject(st->st_filename,
1563 e->lineno,
1564 e->col_offset + 1,
1565 e->end_lineno,
1566 e->end_col_offset + 1);
Nick Coghlan06145232019-08-29 23:26:53 +10001567 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001568 }
1569 if (st->st_cur->ste_comprehension) {
1570 /* Inside a comprehension body, so find the right target scope */
1571 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001572 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001573 }
1574 VISIT(st, expr, e->v.NamedExpr.value);
1575 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001576 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001577}
1578
1579static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580symtable_visit_expr(struct symtable *st, expr_ty e)
1581{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001582 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001583 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001584 "maximum recursion depth exceeded during compilation");
1585 VISIT_QUIT(st, 0);
1586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001588 case NamedExpr_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001589 if (!symtable_raise_if_annotation_block(st, "named expression", e)) {
1590 VISIT_QUIT(st, 0);
1591 }
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001592 if(!symtable_handle_namedexpr(st, e))
1593 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001594 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 case BoolOp_kind:
1596 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1597 break;
1598 case BinOp_kind:
1599 VISIT(st, expr, e->v.BinOp.left);
1600 VISIT(st, expr, e->v.BinOp.right);
1601 break;
1602 case UnaryOp_kind:
1603 VISIT(st, expr, e->v.UnaryOp.operand);
1604 break;
1605 case Lambda_kind: {
1606 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001607 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 if (e->v.Lambda.args->defaults)
1609 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001610 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001611 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (!symtable_enter_block(st, lambda,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001613 FunctionBlock, (void *)e,
1614 e->lineno, e->col_offset,
1615 e->end_lineno, e->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001616 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001617 VISIT(st, arguments, e->v.Lambda.args);
1618 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001619 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001620 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 break;
1622 }
1623 case IfExp_kind:
1624 VISIT(st, expr, e->v.IfExp.test);
1625 VISIT(st, expr, e->v.IfExp.body);
1626 VISIT(st, expr, e->v.IfExp.orelse);
1627 break;
1628 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001629 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 VISIT_SEQ(st, expr, e->v.Dict.values);
1631 break;
1632 case Set_kind:
1633 VISIT_SEQ(st, expr, e->v.Set.elts);
1634 break;
1635 case GeneratorExp_kind:
1636 if (!symtable_visit_genexp(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 ListComp_kind:
1640 if (!symtable_visit_listcomp(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 SetComp_kind:
1644 if (!symtable_visit_setcomp(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 DictComp_kind:
1648 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001649 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 break;
1651 case Yield_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001652 if (!symtable_raise_if_annotation_block(st, "yield expression", e)) {
1653 VISIT_QUIT(st, 0);
1654 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001655 if (e->v.Yield.value)
1656 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 st->st_cur->ste_generator = 1;
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001658 if (st->st_cur->ste_comprehension) {
1659 return symtable_raise_if_comprehension_block(st, e);
1660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001662 case YieldFrom_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001663 if (!symtable_raise_if_annotation_block(st, "yield expression", e)) {
1664 VISIT_QUIT(st, 0);
1665 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001666 VISIT(st, expr, e->v.YieldFrom.value);
1667 st->st_cur->ste_generator = 1;
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001668 if (st->st_cur->ste_comprehension) {
1669 return symtable_raise_if_comprehension_block(st, e);
1670 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001671 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001672 case Await_kind:
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001673 if (!symtable_raise_if_annotation_block(st, "await expression", e)) {
1674 VISIT_QUIT(st, 0);
1675 }
Yury Selivanov75445082015-05-11 22:57:16 -04001676 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001677 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001678 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 case Compare_kind:
1680 VISIT(st, expr, e->v.Compare.left);
1681 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1682 break;
1683 case Call_kind:
1684 VISIT(st, expr, e->v.Call.func);
1685 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001686 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001688 case FormattedValue_kind:
1689 VISIT(st, expr, e->v.FormattedValue.value);
1690 if (e->v.FormattedValue.format_spec)
1691 VISIT(st, expr, e->v.FormattedValue.format_spec);
1692 break;
1693 case JoinedStr_kind:
1694 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1695 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001696 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 /* Nothing to do here. */
1698 break;
1699 /* The following exprs can be assignment targets. */
1700 case Attribute_kind:
1701 VISIT(st, expr, e->v.Attribute.value);
1702 break;
1703 case Subscript_kind:
1704 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001705 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 break;
1707 case Starred_kind:
1708 VISIT(st, expr, e->v.Starred.value);
1709 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001710 case Slice_kind:
1711 if (e->v.Slice.lower)
1712 VISIT(st, expr, e->v.Slice.lower)
1713 if (e->v.Slice.upper)
1714 VISIT(st, expr, e->v.Slice.upper)
1715 if (e->v.Slice.step)
1716 VISIT(st, expr, e->v.Slice.step)
1717 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 case Name_kind:
1719 if (!symtable_add_def(st, e->v.Name.id,
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001720 e->v.Name.ctx == Load ? USE : DEF_LOCAL, LOCATION(e)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001721 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 /* Special-case super: it counts as a use of __class__ */
1723 if (e->v.Name.ctx == Load &&
1724 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001725 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001726 if (!GET_IDENTIFIER(__class__) ||
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001727 !symtable_add_def(st, __class__, USE, LOCATION(e)))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001728 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 }
1730 break;
1731 /* child nodes of List and Tuple will have expr_context set */
1732 case List_kind:
1733 VISIT_SEQ(st, expr, e->v.List.elts);
1734 break;
1735 case Tuple_kind:
1736 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1737 break;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001738 }
1739 VISIT_QUIT(st, 1);
1740}
1741
1742static int
1743symtable_visit_pattern(struct symtable *st, pattern_ty p)
1744{
1745 if (++st->recursion_depth > st->recursion_limit) {
1746 PyErr_SetString(PyExc_RecursionError,
1747 "maximum recursion depth exceeded during compilation");
1748 VISIT_QUIT(st, 0);
1749 }
1750 switch (p->kind) {
1751 case MatchValue_kind:
1752 VISIT(st, expr, p->v.MatchValue.value);
1753 break;
1754 case MatchSingleton_kind:
1755 /* Nothing to do here. */
1756 break;
1757 case MatchSequence_kind:
1758 VISIT_SEQ(st, pattern, p->v.MatchSequence.patterns);
1759 break;
1760 case MatchStar_kind:
1761 if (p->v.MatchStar.name) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001762 symtable_add_def(st, p->v.MatchStar.name, DEF_LOCAL, LOCATION(p));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001763 }
1764 break;
1765 case MatchMapping_kind:
1766 VISIT_SEQ(st, expr, p->v.MatchMapping.keys);
1767 VISIT_SEQ(st, pattern, p->v.MatchMapping.patterns);
1768 if (p->v.MatchMapping.rest) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001769 symtable_add_def(st, p->v.MatchMapping.rest, DEF_LOCAL, LOCATION(p));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001770 }
1771 break;
1772 case MatchClass_kind:
1773 VISIT(st, expr, p->v.MatchClass.cls);
1774 VISIT_SEQ(st, pattern, p->v.MatchClass.patterns);
1775 VISIT_SEQ(st, pattern, p->v.MatchClass.kwd_patterns);
1776 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001777 case MatchAs_kind:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001778 if (p->v.MatchAs.pattern) {
1779 VISIT(st, pattern, p->v.MatchAs.pattern);
1780 }
1781 if (p->v.MatchAs.name) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001782 symtable_add_def(st, p->v.MatchAs.name, DEF_LOCAL, LOCATION(p));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001783 }
Brandt Bucher145bf262021-02-26 14:51:55 -08001784 break;
1785 case MatchOr_kind:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001786 VISIT_SEQ(st, pattern, p->v.MatchOr.patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08001787 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001789 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790}
1791
1792static int
1793symtable_implicit_arg(struct symtable *st, int pos)
1794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1796 if (id == NULL)
1797 return 0;
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001798 if (!symtable_add_def(st, id, DEF_PARAM, ST_LOCATION(st->st_cur))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 Py_DECREF(id);
1800 return 0;
1801 }
1802 Py_DECREF(id);
1803 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804}
1805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001807symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 if (!args)
1812 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 for (i = 0; i < asdl_seq_LEN(args); i++) {
1815 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001816 if (!symtable_add_def(st, arg->arg, DEF_PARAM, LOCATION(arg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 return 0;
1818 }
1819
1820 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821}
1822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823static int
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001824symtable_visit_annotation(struct symtable *st, expr_ty annotation)
1825{
1826 int future_annotations = st->st_future->ff_features & CO_FUTURE_ANNOTATIONS;
1827 if (future_annotations &&
1828 !symtable_enter_block(st, GET_IDENTIFIER(_annotation), AnnotationBlock,
1829 (void *)annotation, annotation->lineno,
1830 annotation->col_offset, annotation->end_lineno,
1831 annotation->end_col_offset)) {
1832 VISIT_QUIT(st, 0);
1833 }
1834 VISIT(st, expr, annotation);
1835 if (future_annotations && !symtable_exit_block(st)) {
1836 VISIT_QUIT(st, 0);
1837 }
1838 return 1;
1839}
1840
1841static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001842symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 if (!args)
1847 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 for (i = 0; i < asdl_seq_LEN(args); i++) {
1850 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1851 if (arg->annotation)
1852 VISIT(st, expr, arg->annotation);
1853 }
1854
1855 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001856}
1857
Neal Norwitzc1505362006-12-28 06:47:50 +00001858static int
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001859symtable_visit_annotations(struct symtable *st, stmt_ty o, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001860{
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001861 int future_annotations = st->st_future->ff_features & CO_FUTURE_ANNOTATIONS;
1862 if (future_annotations &&
1863 !symtable_enter_block(st, GET_IDENTIFIER(_annotation), AnnotationBlock,
1864 (void *)o, o->lineno, o->col_offset, o->end_lineno,
1865 o->end_col_offset)) {
1866 VISIT_QUIT(st, 0);
1867 }
Anthony Sottileec007cb2020-01-04 20:57:21 -05001868 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1869 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 if (a->args && !symtable_visit_argannotations(st, a->args))
1871 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001872 if (a->vararg && a->vararg->annotation)
1873 VISIT(st, expr, a->vararg->annotation);
1874 if (a->kwarg && a->kwarg->annotation)
1875 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1877 return 0;
Batuhan Taskayaad106c62021-05-03 10:43:00 +03001878 if (future_annotations && !symtable_exit_block(st)) {
1879 VISIT_QUIT(st, 0);
1880 }
1881 if (returns && !symtable_visit_annotation(st, returns)) {
1882 VISIT_QUIT(st, 0);
1883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885}
1886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888symtable_visit_arguments(struct symtable *st, arguments_ty a)
1889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 /* skip default arguments inside function block
1891 XXX should ast be different?
1892 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001893 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1894 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (a->args && !symtable_visit_params(st, a->args))
1896 return 0;
1897 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1898 return 0;
1899 if (a->vararg) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001900 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM, LOCATION(a->vararg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 return 0;
1902 st->st_cur->ste_varargs = 1;
1903 }
1904 if (a->kwarg) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001905 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM, LOCATION(a->kwarg)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 return 0;
1907 st->st_cur->ste_varkeywords = 1;
1908 }
1909 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910}
1911
1912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 if (eh->v.ExceptHandler.type)
1917 VISIT(st, expr, eh->v.ExceptHandler.type);
1918 if (eh->v.ExceptHandler.name)
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001919 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL, LOCATION(eh)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return 0;
1921 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1922 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923}
1924
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001925static int
1926symtable_visit_withitem(struct symtable *st, withitem_ty item)
1927{
1928 VISIT(st, expr, item->context_expr);
1929 if (item->optional_vars) {
1930 VISIT(st, expr, item->optional_vars);
1931 }
1932 return 1;
1933}
1934
Brandt Bucher145bf262021-02-26 14:51:55 -08001935static int
1936symtable_visit_match_case(struct symtable *st, match_case_ty m)
1937{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001938 VISIT(st, pattern, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08001939 if (m->guard) {
1940 VISIT(st, expr, m->guard);
1941 }
1942 VISIT_SEQ(st, stmt, m->body);
1943 return 1;
1944}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947symtable_visit_alias(struct symtable *st, alias_ty a)
1948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001950 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 dotted package name (e.g. spam.eggs)
1952 */
1953 PyObject *store_name;
1954 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001955 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1956 PyUnicode_GET_LENGTH(name), 1);
1957 if (dot != -1) {
1958 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (!store_name)
1960 return 0;
1961 }
1962 else {
1963 store_name = name;
1964 Py_INCREF(store_name);
1965 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001966 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001967 int r = symtable_add_def(st, store_name, DEF_IMPORT, LOCATION(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 Py_DECREF(store_name);
1969 return r;
1970 }
1971 else {
1972 if (st->st_cur->ste_type != ModuleBlock) {
Miss Islington (bot)438817f2021-12-11 17:24:12 -08001973 int lineno = a->lineno;
1974 int col_offset = a->col_offset;
1975 int end_lineno = a->end_lineno;
1976 int end_col_offset = a->end_col_offset;
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001977 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001978 PyErr_RangedSyntaxLocationObject(st->st_filename,
1979 lineno, col_offset + 1,
1980 end_lineno, end_col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001981 Py_DECREF(store_name);
1982 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_DECREF(store_name);
1985 return 1;
1986 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987}
1988
1989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1992{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001993 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001995 st->st_cur->ste_comp_iter_target = 0;
1996 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001998 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002000 if (lc->is_async) {
2001 st->st_cur->ste_coroutine = 1;
2002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004}
2005
2006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008symtable_visit_keyword(struct symtable *st, keyword_ty k)
2009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 VISIT(st, expr, k->value);
2011 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012}
2013
2014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002016symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01002017 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002018 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 comprehension_ty outermost = ((comprehension_ty)
2022 asdl_seq_GET(generators, 0));
2023 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002024 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002026 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 /* Create comprehension scope for the rest */
2028 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002029 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
Pablo Galindoa77aac42021-04-23 14:27:05 +01002030 e->lineno, e->col_offset,
2031 e->end_lineno, e->end_col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 return 0;
2033 }
Miss Islington (bot)438817f2021-12-11 17:24:12 -08002034 switch(e->kind) {
2035 case ListComp_kind:
2036 st->st_cur->ste_comprehension = ListComprehension;
2037 break;
2038 case SetComp_kind:
2039 st->st_cur->ste_comprehension = SetComprehension;
2040 break;
2041 case DictComp_kind:
2042 st->st_cur->ste_comprehension = DictComprehension;
2043 break;
2044 default:
2045 st->st_cur->ste_comprehension = GeneratorExpression;
2046 break;
2047 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002048 if (outermost->is_async) {
2049 st->st_cur->ste_coroutine = 1;
2050 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 /* Outermost iter is received as an argument */
2053 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06002054 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 return 0;
2056 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002057 /* Visit iteration variable target, and mark them as such */
2058 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05002059 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10002060 st->st_cur->ste_comp_iter_target = 0;
2061 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05002062 VISIT_SEQ(st, expr, outermost->ifs);
2063 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05002065 VISIT(st, expr, value);
2066 VISIT(st, expr, elt);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02002067 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06002068 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002072symtable_visit_genexp(struct symtable *st, expr_ty e)
2073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
2075 e->v.GeneratorExp.generators,
2076 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077}
2078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080symtable_visit_listcomp(struct symtable *st, expr_ty e)
2081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
2083 e->v.ListComp.generators,
2084 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002085}
2086
2087static int
2088symtable_visit_setcomp(struct symtable *st, expr_ty e)
2089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
2091 e->v.SetComp.generators,
2092 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002093}
2094
2095static int
2096symtable_visit_dictcomp(struct symtable *st, expr_ty e)
2097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
2099 e->v.DictComp.generators,
2100 e->v.DictComp.key,
2101 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002102}
Victor Stinner28ad12f2021-03-19 12:41:49 +01002103
Batuhan Taskayaad106c62021-05-03 10:43:00 +03002104static int
2105symtable_raise_if_annotation_block(struct symtable *st, const char *name, expr_ty e)
2106{
2107 if (st->st_cur->ste_type != AnnotationBlock) {
2108 return 1;
2109 }
2110
2111 PyErr_Format(PyExc_SyntaxError, ANNOTATION_NOT_ALLOWED, name);
2112 PyErr_RangedSyntaxLocationObject(st->st_filename,
2113 e->lineno,
2114 e->col_offset + 1,
2115 e->end_lineno,
2116 e->end_col_offset + 1);
2117 return 0;
2118}
Victor Stinner28ad12f2021-03-19 12:41:49 +01002119
Miss Islington (bot)438817f2021-12-11 17:24:12 -08002120static int
2121symtable_raise_if_comprehension_block(struct symtable *st, expr_ty e) {
2122 _Py_comprehension_ty type = st->st_cur->ste_comprehension;
2123 PyErr_SetString(PyExc_SyntaxError,
2124 (type == ListComprehension) ? "'yield' inside list comprehension" :
2125 (type == SetComprehension) ? "'yield' inside set comprehension" :
2126 (type == DictComprehension) ? "'yield' inside dict comprehension" :
2127 "'yield' inside generator expression");
2128 PyErr_RangedSyntaxLocationObject(st->st_filename,
2129 e->lineno, e->col_offset + 1,
2130 e->end_lineno, e->end_col_offset + 1);
2131 VISIT_QUIT(st, 0);
2132}
2133
Victor Stinner28ad12f2021-03-19 12:41:49 +01002134struct symtable *
2135_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
2136 int start, PyCompilerFlags *flags)
2137{
2138 struct symtable *st;
2139 mod_ty mod;
2140 PyArena *arena;
2141
Victor Stinner8370e072021-03-24 02:23:01 +01002142 arena = _PyArena_New();
Victor Stinner28ad12f2021-03-19 12:41:49 +01002143 if (arena == NULL)
2144 return NULL;
2145
Victor Stinner57364ce2021-03-24 01:29:09 +01002146 mod = _PyParser_ASTFromString(str, filename, start, flags, arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002147 if (mod == NULL) {
Victor Stinner8370e072021-03-24 02:23:01 +01002148 _PyArena_Free(arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002149 return NULL;
2150 }
Batuhan Taskayaad106c62021-05-03 10:43:00 +03002151 PyFutureFeatures *future = _PyFuture_FromAST(mod, filename);
2152 if (future == NULL) {
2153 _PyArena_Free(arena);
2154 return NULL;
2155 }
2156 future->ff_features |= flags->cf_flags;
2157 st = _PySymtable_Build(mod, filename, future);
2158 PyObject_Free((void *)future);
Victor Stinner8370e072021-03-24 02:23:01 +01002159 _PyArena_Free(arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002160 return st;
2161}