blob: c7ba4d9c544c8c41e5f86aed40c37a6695fb1ea3 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
Miss Islington (bot)90ee51f2020-06-06 10:04:38 -070025validate_name(PyObject *name)
26{
27 assert(PyUnicode_Check(name));
28 static const char * const forbidden[] = {
29 "None",
30 "True",
31 "False",
32 NULL
33 };
34 for (int i = 0; forbidden[i] != NULL; i++) {
35 if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
36 PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]);
37 return 0;
38 }
39 }
40 return 1;
41}
42
43static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -050044validate_comprehension(asdl_seq *gens)
45{
Victor Stinner4d73ae72018-11-22 14:45:16 +010046 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050047 if (!asdl_seq_LEN(gens)) {
48 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
49 return 0;
50 }
51 for (i = 0; i < asdl_seq_LEN(gens); i++) {
52 comprehension_ty comp = asdl_seq_GET(gens, i);
53 if (!validate_expr(comp->target, Store) ||
54 !validate_expr(comp->iter, Load) ||
55 !validate_exprs(comp->ifs, Load, 0))
56 return 0;
57 }
58 return 1;
59}
60
61static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -050062validate_keywords(asdl_seq *keywords)
63{
Victor Stinner4d73ae72018-11-22 14:45:16 +010064 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050065 for (i = 0; i < asdl_seq_LEN(keywords); i++)
66 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
67 return 0;
68 return 1;
69}
70
71static int
72validate_args(asdl_seq *args)
73{
Victor Stinner4d73ae72018-11-22 14:45:16 +010074 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050075 for (i = 0; i < asdl_seq_LEN(args); i++) {
76 arg_ty arg = asdl_seq_GET(args, i);
77 if (arg->annotation && !validate_expr(arg->annotation, Load))
78 return 0;
79 }
80 return 1;
81}
82
83static const char *
84expr_context_name(expr_context_ty ctx)
85{
86 switch (ctx) {
87 case Load:
88 return "Load";
89 case Store:
90 return "Store";
91 case Del:
92 return "Del";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050093 default:
Barry Warsawb2e57942017-09-14 18:13:16 -070094 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -050095 }
96}
97
98static int
99validate_arguments(arguments_ty args)
100{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100101 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500102 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100103 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700104 if (args->vararg && args->vararg->annotation
105 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500106 return 0;
107 }
108 if (!validate_args(args->kwonlyargs))
109 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100110 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700111 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500112 return 0;
113 }
Pablo Galindo2f58a842019-05-31 14:09:49 +0100114 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500115 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
116 return 0;
117 }
118 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
119 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
120 "kw_defaults on arguments");
121 return 0;
122 }
123 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
124}
125
126static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100127validate_constant(PyObject *value)
128{
129 if (value == Py_None || value == Py_Ellipsis)
130 return 1;
131
132 if (PyLong_CheckExact(value)
133 || PyFloat_CheckExact(value)
134 || PyComplex_CheckExact(value)
135 || PyBool_Check(value)
136 || PyUnicode_CheckExact(value)
137 || PyBytes_CheckExact(value))
138 return 1;
139
140 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
141 PyObject *it;
142
143 it = PyObject_GetIter(value);
144 if (it == NULL)
145 return 0;
146
147 while (1) {
148 PyObject *item = PyIter_Next(it);
149 if (item == NULL) {
150 if (PyErr_Occurred()) {
151 Py_DECREF(it);
152 return 0;
153 }
154 break;
155 }
156
157 if (!validate_constant(item)) {
158 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100159 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100160 return 0;
161 }
Victor Stinner726f6902016-01-27 00:11:47 +0100162 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100163 }
164
165 Py_DECREF(it);
166 return 1;
167 }
168
Batuhan Taßkaya0ac59f92020-03-19 14:32:28 +0300169 if (!PyErr_Occurred()) {
170 PyErr_Format(PyExc_TypeError,
171 "got an invalid type in Constant: %s",
172 _PyType_Name(Py_TYPE(value)));
173 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100174 return 0;
175}
176
177static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500178validate_expr(expr_ty exp, expr_context_ty ctx)
179{
180 int check_ctx = 1;
181 expr_context_ty actual_ctx;
182
183 /* First check expression context. */
184 switch (exp->kind) {
185 case Attribute_kind:
186 actual_ctx = exp->v.Attribute.ctx;
187 break;
188 case Subscript_kind:
189 actual_ctx = exp->v.Subscript.ctx;
190 break;
191 case Starred_kind:
192 actual_ctx = exp->v.Starred.ctx;
193 break;
194 case Name_kind:
Miss Islington (bot)90ee51f2020-06-06 10:04:38 -0700195 if (!validate_name(exp->v.Name.id)) {
196 return 0;
197 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100291 return 0;
292 }
293 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400294 case JoinedStr_kind:
295 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
296 case FormattedValue_kind:
297 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
298 return 0;
299 if (exp->v.FormattedValue.format_spec)
300 return validate_expr(exp->v.FormattedValue.format_spec, Load);
301 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500302 case Attribute_kind:
303 return validate_expr(exp->v.Attribute.value, Load);
304 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200305 return validate_expr(exp->v.Subscript.slice, Load) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500306 validate_expr(exp->v.Subscript.value, Load);
307 case Starred_kind:
308 return validate_expr(exp->v.Starred.value, ctx);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200309 case Slice_kind:
310 return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) &&
311 (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) &&
312 (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500313 case List_kind:
314 return validate_exprs(exp->v.List.elts, ctx, 0);
315 case Tuple_kind:
316 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000317 case NamedExpr_kind:
318 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300319 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500320 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000323 PyErr_SetString(PyExc_SystemError, "unexpected expression");
324 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325}
326
327static int
328validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
329{
330 if (asdl_seq_LEN(seq))
331 return 1;
332 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
333 return 0;
334}
335
336static int
337validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
338{
339 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
340 validate_exprs(targets, ctx, 0);
341}
342
343static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300344validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500345{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300346 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500347}
348
349static int
350validate_stmt(stmt_ty stmt)
351{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100352 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500353 switch (stmt->kind) {
354 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300355 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 validate_arguments(stmt->v.FunctionDef.args) &&
357 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
358 (!stmt->v.FunctionDef.returns ||
359 validate_expr(stmt->v.FunctionDef.returns, Load));
360 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300361 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500362 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
363 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400364 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 case Return_kind:
366 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
367 case Delete_kind:
368 return validate_assignlist(stmt->v.Delete.targets, Del);
369 case Assign_kind:
370 return validate_assignlist(stmt->v.Assign.targets, Store) &&
371 validate_expr(stmt->v.Assign.value, Load);
372 case AugAssign_kind:
373 return validate_expr(stmt->v.AugAssign.target, Store) &&
374 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700375 case AnnAssign_kind:
376 if (stmt->v.AnnAssign.target->kind != Name_kind &&
377 stmt->v.AnnAssign.simple) {
378 PyErr_SetString(PyExc_TypeError,
379 "AnnAssign with simple non-Name target");
380 return 0;
381 }
382 return validate_expr(stmt->v.AnnAssign.target, Store) &&
383 (!stmt->v.AnnAssign.value ||
384 validate_expr(stmt->v.AnnAssign.value, Load)) &&
385 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500386 case For_kind:
387 return validate_expr(stmt->v.For.target, Store) &&
388 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300389 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500390 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400391 case AsyncFor_kind:
392 return validate_expr(stmt->v.AsyncFor.target, Store) &&
393 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300394 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400395 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500396 case While_kind:
397 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300398 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 validate_stmts(stmt->v.While.orelse);
400 case If_kind:
401 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300402 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500403 validate_stmts(stmt->v.If.orelse);
404 case With_kind:
405 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
406 return 0;
407 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
408 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
409 if (!validate_expr(item->context_expr, Load) ||
410 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
411 return 0;
412 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300413 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400414 case AsyncWith_kind:
415 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
416 return 0;
417 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
418 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
419 if (!validate_expr(item->context_expr, Load) ||
420 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
421 return 0;
422 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300423 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500424 case Raise_kind:
425 if (stmt->v.Raise.exc) {
426 return validate_expr(stmt->v.Raise.exc, Load) &&
427 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
428 }
429 if (stmt->v.Raise.cause) {
430 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
431 return 0;
432 }
433 return 1;
434 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300435 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500436 return 0;
437 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
438 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
439 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
440 return 0;
441 }
442 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
443 asdl_seq_LEN(stmt->v.Try.orelse)) {
444 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
445 return 0;
446 }
447 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
448 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
449 if ((handler->v.ExceptHandler.type &&
450 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300451 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500452 return 0;
453 }
454 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
455 validate_stmts(stmt->v.Try.finalbody)) &&
456 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
457 validate_stmts(stmt->v.Try.orelse));
458 case Assert_kind:
459 return validate_expr(stmt->v.Assert.test, Load) &&
460 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
461 case Import_kind:
462 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
463 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300464 if (stmt->v.ImportFrom.level < 0) {
465 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500466 return 0;
467 }
468 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
469 case Global_kind:
470 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
471 case Nonlocal_kind:
472 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
473 case Expr_kind:
474 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400475 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300476 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400477 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
478 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
479 (!stmt->v.AsyncFunctionDef.returns ||
480 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500481 case Pass_kind:
482 case Break_kind:
483 case Continue_kind:
484 return 1;
485 default:
486 PyErr_SetString(PyExc_SystemError, "unexpected statement");
487 return 0;
488 }
489}
490
491static int
492validate_stmts(asdl_seq *seq)
493{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100494 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500495 for (i = 0; i < asdl_seq_LEN(seq); i++) {
496 stmt_ty stmt = asdl_seq_GET(seq, i);
497 if (stmt) {
498 if (!validate_stmt(stmt))
499 return 0;
500 }
501 else {
502 PyErr_SetString(PyExc_ValueError,
503 "None disallowed in statement list");
504 return 0;
505 }
506 }
507 return 1;
508}
509
510static int
511validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
512{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100513 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500514 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
515 expr_ty expr = asdl_seq_GET(exprs, i);
516 if (expr) {
517 if (!validate_expr(expr, ctx))
518 return 0;
519 }
520 else if (!null_ok) {
521 PyErr_SetString(PyExc_ValueError,
522 "None disallowed in expression list");
523 return 0;
524 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100525
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500526 }
527 return 1;
528}
529
530int
531PyAST_Validate(mod_ty mod)
532{
533 int res = 0;
534
535 switch (mod->kind) {
536 case Module_kind:
537 res = validate_stmts(mod->v.Module.body);
538 break;
539 case Interactive_kind:
540 res = validate_stmts(mod->v.Interactive.body);
541 break;
542 case Expression_kind:
543 res = validate_expr(mod->v.Expression.body, Load);
544 break;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500545 default:
546 PyErr_SetString(PyExc_SystemError, "impossible module node");
547 res = 0;
548 break;
549 }
550 return res;
551}
552
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500553/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500554#include "grammar.h"
555#include "parsetok.h"
556#include "graminit.h"
557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558/* Data structure used internally */
559struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400560 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200561 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500562 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800563 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564};
565
566static asdl_seq *seq_for_testlist(struct compiling *, const node *);
567static expr_ty ast_for_expr(struct compiling *, const node *);
568static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300569static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000570static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
571 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000572static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000573static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574
guoci90fc8982018-09-11 17:45:45 -0400575static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
576static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400577
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200579static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +0200580 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000582static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400583static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000584static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585
Nick Coghlan650f0d02007-04-15 12:05:43 +0000586#define COMP_GENEXP 0
587#define COMP_LISTCOMP 1
588#define COMP_SETCOMP 2
589
Benjamin Peterson55e00432012-01-16 17:22:31 -0500590static int
591init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000592{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500593 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
594 if (!m)
595 return 0;
596 c->c_normalize = PyObject_GetAttrString(m, "normalize");
597 Py_DECREF(m);
598 if (!c->c_normalize)
599 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500600 return 1;
601}
602
603static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400604new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400606 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500607 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000608 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500609 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500610 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000611 /* Check whether there are non-ASCII characters in the
612 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500613 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200614 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500616 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500618 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700619 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300620 if (form == NULL) {
621 Py_DECREF(id);
622 return NULL;
623 }
624 PyObject *args[2] = {form, id};
625 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500626 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100627 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200628 if (!id2)
629 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300630 if (!PyUnicode_Check(id2)) {
631 PyErr_Format(PyExc_TypeError,
632 "unicodedata.normalize() must return a string, not "
633 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700634 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300635 Py_DECREF(id2);
636 return NULL;
637 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200638 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000639 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000640 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200641 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
642 Py_DECREF(id);
643 return NULL;
644 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000645 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646}
647
Benjamin Peterson55e00432012-01-16 17:22:31 -0500648#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200651ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400653 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200654 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656 va_start(va, errmsg);
657 errstr = PyUnicode_FromFormatV(errmsg, va);
658 va_end(va);
659 if (!errstr) {
660 return 0;
661 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200662 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000664 Py_INCREF(Py_None);
665 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 }
Ammar Askar025eb982018-09-24 17:12:49 -0400667 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200668 if (!tmp) {
669 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400670 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000671 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 Py_DECREF(errstr);
674 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 if (value) {
676 PyErr_SetObject(PyExc_SyntaxError, value);
677 Py_DECREF(value);
678 }
679 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680}
681
682/* num_stmts() returns number of contained statements.
683
684 Use this routine to determine how big a sequence is needed for
685 the statements in a parse tree. Its raison d'etre is this bit of
686 grammar:
687
688 stmt: simple_stmt | compound_stmt
689 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
690
691 A simple_stmt can contain multiple small_stmt elements joined
692 by semicolons. If the arg is a simple_stmt, the number of
693 small_stmt elements is returned.
694*/
695
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800696static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800697new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800698{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800699 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800700 if (res == NULL)
701 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800702 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
703 Py_DECREF(res);
704 return NULL;
705 }
706 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800707}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800708#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710static int
711num_stmts(const node *n)
712{
713 int i, l;
714 node *ch;
715
716 switch (TYPE(n)) {
717 case single_input:
718 if (TYPE(CHILD(n, 0)) == NEWLINE)
719 return 0;
720 else
721 return num_stmts(CHILD(n, 0));
722 case file_input:
723 l = 0;
724 for (i = 0; i < NCH(n); i++) {
725 ch = CHILD(n, i);
726 if (TYPE(ch) == stmt)
727 l += num_stmts(ch);
728 }
729 return l;
730 case stmt:
731 return num_stmts(CHILD(n, 0));
732 case compound_stmt:
733 return 1;
734 case simple_stmt:
735 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
736 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800737 case func_body_suite:
738 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
739 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 if (NCH(n) == 1)
741 return num_stmts(CHILD(n, 0));
742 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800743 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800745 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
746 i += 2;
747 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 l += num_stmts(CHILD(n, i));
749 return l;
750 }
751 default: {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100752 _Py_FatalErrorFormat(__func__, "Non-statement found: %d %d",
753 TYPE(n), NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 }
755 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700756 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757}
758
759/* Transform the CST rooted at node * to the appropriate AST
760*/
761
762mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200763PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
764 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000766 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800768 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 stmt_ty s;
770 node *ch;
771 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500772 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800773 asdl_seq *argtypes = NULL;
774 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400776 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200777 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400778 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800779 c.c_normalize = NULL;
Guido van Rossum2a1ee1d2020-06-27 17:34:30 -0700780 c.c_feature_version = flags && (flags->cf_flags & PyCF_ONLY_AST) ?
781 flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800782
783 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
Jeremy Hyltona8293132006-02-28 17:58:27 +0000786 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 switch (TYPE(n)) {
788 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200789 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500791 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 for (i = 0; i < NCH(n) - 1; i++) {
793 ch = CHILD(n, i);
794 if (TYPE(ch) == NEWLINE)
795 continue;
796 REQ(ch, stmt);
797 num = num_stmts(ch);
798 if (num == 1) {
799 s = ast_for_stmt(&c, ch);
800 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500801 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000802 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 }
804 else {
805 ch = CHILD(ch, 0);
806 REQ(ch, simple_stmt);
807 for (j = 0; j < num; j++) {
808 s = ast_for_stmt(&c, CHILD(ch, j * 2));
809 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500810 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000811 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 }
813 }
814 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800815
816 /* Type ignores are stored under the ENDMARKER in file_input. */
817 ch = CHILD(n, NCH(n) - 1);
818 REQ(ch, ENDMARKER);
819 num = NCH(ch);
820 type_ignores = _Py_asdl_seq_new(num, arena);
821 if (!type_ignores)
822 goto out;
823
824 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700825 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
826 if (!type_comment)
827 goto out;
828 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800829 if (!ti)
830 goto out;
831 asdl_seq_SET(type_ignores, i, ti);
832 }
833
834 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500835 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 case eval_input: {
837 expr_ty testlist_ast;
838
Nick Coghlan650f0d02007-04-15 12:05:43 +0000839 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000840 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
843 res = Expression(testlist_ast, arena);
844 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 }
846 case single_input:
847 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200848 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000851 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000852 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000854 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 goto out;
856 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 }
858 else {
859 n = CHILD(n, 0);
860 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200861 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000865 s = ast_for_stmt(&c, n);
866 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500867 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 asdl_seq_SET(stmts, 0, s);
869 }
870 else {
871 /* Only a simple_stmt can contain multiple statements. */
872 REQ(n, simple_stmt);
873 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 if (TYPE(CHILD(n, i)) == NEWLINE)
875 break;
876 s = ast_for_stmt(&c, CHILD(n, i));
877 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500878 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 asdl_seq_SET(stmts, i / 2, s);
880 }
881 }
882
Benjamin Peterson55e00432012-01-16 17:22:31 -0500883 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500885 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800886 case func_type_input:
887 n = CHILD(n, 0);
888 REQ(n, func_type);
889
890 if (TYPE(CHILD(n, 1)) == typelist) {
891 ch = CHILD(n, 1);
892 /* this is overly permissive -- we don't pay any attention to
893 * stars on the args -- just parse them into an ordered list */
894 num = 0;
895 for (i = 0; i < NCH(ch); i++) {
896 if (TYPE(CHILD(ch, i)) == test) {
897 num++;
898 }
899 }
900
901 argtypes = _Py_asdl_seq_new(num, arena);
902 if (!argtypes)
903 goto out;
904
905 j = 0;
906 for (i = 0; i < NCH(ch); i++) {
907 if (TYPE(CHILD(ch, i)) == test) {
908 arg = ast_for_expr(&c, CHILD(ch, i));
909 if (!arg)
910 goto out;
911 asdl_seq_SET(argtypes, j++, arg);
912 }
913 }
914 }
915 else {
916 argtypes = _Py_asdl_seq_new(0, arena);
917 if (!argtypes)
918 goto out;
919 }
920
921 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
922 if (!ret)
923 goto out;
924 res = FunctionType(argtypes, ret, arena);
925 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000927 PyErr_Format(PyExc_SystemError,
928 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500929 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500931 out:
932 if (c.c_normalize) {
933 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500934 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500935 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
Victor Stinner14e461d2013-08-26 22:28:21 +0200938mod_ty
939PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
940 PyArena *arena)
941{
942 mod_ty mod;
943 PyObject *filename;
944 filename = PyUnicode_DecodeFSDefault(filename_str);
945 if (filename == NULL)
946 return NULL;
947 mod = PyAST_FromNodeObject(n, flags, filename, arena);
948 Py_DECREF(filename);
949 return mod;
950
951}
952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
954*/
955
956static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800957get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958{
959 switch (TYPE(n)) {
960 case VBAR:
961 return BitOr;
962 case CIRCUMFLEX:
963 return BitXor;
964 case AMPER:
965 return BitAnd;
966 case LEFTSHIFT:
967 return LShift;
968 case RIGHTSHIFT:
969 return RShift;
970 case PLUS:
971 return Add;
972 case MINUS:
973 return Sub;
974 case STAR:
975 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400976 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800977 if (c->c_feature_version < 5) {
978 ast_error(c, n,
979 "The '@' operator is only supported in Python 3.5 and greater");
980 return (operator_ty)0;
981 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400982 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 case SLASH:
984 return Div;
985 case DOUBLESLASH:
986 return FloorDiv;
987 case PERCENT:
988 return Mod;
989 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000990 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 }
992}
993
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200994static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 "None",
996 "True",
997 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200998 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000999 NULL,
1000};
1001
1002static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001003forbidden_name(struct compiling *c, identifier name, const node *n,
1004 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001005{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001006 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001007 const char * const *p = FORBIDDEN;
1008 if (!full_checks) {
1009 /* In most cases, the parser will protect True, False, and None
1010 from being assign to. */
1011 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001012 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001013 for (; *p; p++) {
1014 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1015 ast_error(c, n, "cannot assign to %U", name);
1016 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001017 }
1018 }
1019 return 0;
1020}
1021
Serhiy Storchakab619b092018-11-27 09:40:29 +02001022static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001023copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001024{
1025 if (e) {
1026 e->lineno = LINENO(n);
1027 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001028 e->end_lineno = end->n_end_lineno;
1029 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001030 }
1031 return e;
1032}
1033
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001034static const char *
1035get_expr_name(expr_ty e)
1036{
1037 switch (e->kind) {
1038 case Attribute_kind:
1039 return "attribute";
1040 case Subscript_kind:
1041 return "subscript";
1042 case Starred_kind:
1043 return "starred";
1044 case Name_kind:
1045 return "name";
1046 case List_kind:
1047 return "list";
1048 case Tuple_kind:
1049 return "tuple";
1050 case Lambda_kind:
1051 return "lambda";
1052 case Call_kind:
1053 return "function call";
1054 case BoolOp_kind:
1055 case BinOp_kind:
1056 case UnaryOp_kind:
1057 return "operator";
1058 case GeneratorExp_kind:
1059 return "generator expression";
1060 case Yield_kind:
1061 case YieldFrom_kind:
1062 return "yield expression";
1063 case Await_kind:
1064 return "await expression";
1065 case ListComp_kind:
1066 return "list comprehension";
1067 case SetComp_kind:
1068 return "set comprehension";
1069 case DictComp_kind:
1070 return "dict comprehension";
1071 case Dict_kind:
1072 return "dict display";
1073 case Set_kind:
1074 return "set display";
1075 case JoinedStr_kind:
1076 case FormattedValue_kind:
1077 return "f-string expression";
1078 case Constant_kind: {
1079 PyObject *value = e->v.Constant.value;
1080 if (value == Py_None) {
1081 return "None";
1082 }
1083 if (value == Py_False) {
1084 return "False";
1085 }
1086 if (value == Py_True) {
1087 return "True";
1088 }
1089 if (value == Py_Ellipsis) {
1090 return "Ellipsis";
1091 }
1092 return "literal";
1093 }
1094 case Compare_kind:
1095 return "comparison";
1096 case IfExp_kind:
1097 return "conditional expression";
1098 case NamedExpr_kind:
1099 return "named expression";
1100 default:
1101 PyErr_Format(PyExc_SystemError,
1102 "unexpected expression in assignment %d (line %d)",
1103 e->kind, e->lineno);
1104 return NULL;
1105 }
1106}
1107
Jeremy Hyltona8293132006-02-28 17:58:27 +00001108/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109
1110 Only sets context for expr kinds that "can appear in assignment context"
1111 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1112 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113*/
1114
1115static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001116set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117{
1118 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001119
Serhiy Storchaka6b975982020-03-17 23:41:08 +02001120 /* Expressions in an augmented assignment have a Store context. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121
1122 switch (e->kind) {
1123 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001124 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001125 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001126 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001127 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001129 e->v.Subscript.ctx = ctx;
1130 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001131 case Starred_kind:
1132 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001133 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001134 return 0;
1135 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001137 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001138 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001139 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001140 }
1141 e->v.Name.ctx = ctx;
1142 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001144 e->v.List.ctx = ctx;
1145 s = e->v.List.elts;
1146 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001148 e->v.Tuple.ctx = ctx;
1149 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001150 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001151 default: {
1152 const char *expr_name = get_expr_name(e);
1153 if (expr_name != NULL) {
1154 ast_error(c, n, "cannot %s %s",
1155 ctx == Store ? "assign to" : "delete",
1156 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001157 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001158 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001159 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001160 }
1161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 */
1165 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001166 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167
Thomas Wouters89f507f2006-12-13 04:49:30 +00001168 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001169 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001170 return 0;
1171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 }
1173 return 1;
1174}
1175
1176static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001177ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178{
1179 REQ(n, augassign);
1180 n = CHILD(n, 0);
1181 switch (STR(n)[0]) {
1182 case '+':
1183 return Add;
1184 case '-':
1185 return Sub;
1186 case '/':
1187 if (STR(n)[1] == '/')
1188 return FloorDiv;
1189 else
1190 return Div;
1191 case '%':
1192 return Mod;
1193 case '<':
1194 return LShift;
1195 case '>':
1196 return RShift;
1197 case '&':
1198 return BitAnd;
1199 case '^':
1200 return BitXor;
1201 case '|':
1202 return BitOr;
1203 case '*':
1204 if (STR(n)[1] == '*')
1205 return Pow;
1206 else
1207 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001208 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001209 if (c->c_feature_version < 5) {
1210 ast_error(c, n,
1211 "The '@' operator is only supported in Python 3.5 and greater");
1212 return (operator_ty)0;
1213 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001214 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001216 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001217 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 }
1219}
1220
1221static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001222ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001224 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 |'is' 'not'
1226 */
1227 REQ(n, comp_op);
1228 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001229 n = CHILD(n, 0);
1230 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 case LESS:
1232 return Lt;
1233 case GREATER:
1234 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001235 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 return Eq;
1237 case LESSEQUAL:
1238 return LtE;
1239 case GREATEREQUAL:
1240 return GtE;
1241 case NOTEQUAL:
1242 return NotEq;
1243 case NAME:
1244 if (strcmp(STR(n), "in") == 0)
1245 return In;
1246 if (strcmp(STR(n), "is") == 0)
1247 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001248 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001250 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001253 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 }
1255 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001256 /* handle "not in" and "is not" */
1257 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 case NAME:
1259 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1260 return NotIn;
1261 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1262 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001263 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001265 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
Neal Norwitz79792652005-11-14 04:25:03 +00001270 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001272 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273}
1274
1275static asdl_seq *
1276seq_for_testlist(struct compiling *c, const node *n)
1277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001279 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1280 */
Armin Rigo31441302005-10-21 12:57:31 +00001281 asdl_seq *seq;
1282 expr_ty expression;
1283 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001284 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001286 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 if (!seq)
1288 return NULL;
1289
1290 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001292 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293
Benjamin Peterson4905e802009-09-27 02:43:28 +00001294 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001295 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297
1298 assert(i / 2 < seq->size);
1299 asdl_seq_SET(seq, i / 2, expression);
1300 }
1301 return seq;
1302}
1303
Neal Norwitzc1505362006-12-28 06:47:50 +00001304static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001305ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001306{
1307 identifier name;
1308 expr_ty annotation = NULL;
1309 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001310 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001311
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001312 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001314 name = NEW_IDENTIFIER(ch);
1315 if (!name)
1316 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001317 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001318 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001319
1320 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1321 annotation = ast_for_expr(c, CHILD(n, 2));
1322 if (!annotation)
1323 return NULL;
1324 }
1325
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001326 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001327 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001328 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001329 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001330 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331}
1332
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333/* returns -1 if failed to handle keyword only arguments
1334 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001336 ^^^
1337 start pointing here
1338 */
1339static int
1340handle_keywordonly_args(struct compiling *c, const node *n, int start,
1341 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1342{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001343 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001345 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001346 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347 int i = start;
1348 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001349
1350 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001351 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001352 return -1;
1353 }
1354 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 while (i < NCH(n)) {
1356 ch = CHILD(n, i);
1357 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001358 case vfpdef:
1359 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001361 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001362 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001363 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 asdl_seq_SET(kwdefaults, j, expression);
1365 i += 2; /* '=' and test */
1366 }
1367 else { /* setting NULL if no default value exists */
1368 asdl_seq_SET(kwdefaults, j, NULL);
1369 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 if (NCH(ch) == 3) {
1371 /* ch is NAME ':' test */
1372 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001374 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001375 }
1376 else {
1377 annotation = NULL;
1378 }
1379 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001380 argname = NEW_IDENTIFIER(ch);
1381 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001383 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001384 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001385 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001386 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001387 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001388 if (!arg)
1389 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001390 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001391 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001392 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001393 i += 1; /* the comma, if present */
1394 break;
1395 case TYPE_COMMENT:
1396 /* arg will be equal to the last argument processed */
1397 arg->type_comment = NEW_TYPE_COMMENT(ch);
1398 if (!arg->type_comment)
1399 goto error;
1400 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401 break;
1402 case DOUBLESTAR:
1403 return i;
1404 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001405 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001406 goto error;
1407 }
1408 }
1409 return i;
1410 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
Jeremy Hyltona8293132006-02-28 17:58:27 +00001414/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
1416static arguments_ty
1417ast_for_arguments(struct compiling *c, const node *n)
1418{
Neal Norwitzc1505362006-12-28 06:47:50 +00001419 /* This function handles both typedargslist (function definition)
1420 and varargslist (lambda definition).
1421
1422 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001423
1424 The following definition for typedarglist is equivalent to this set of rules:
1425
1426 arguments = argument (',' [TYPE_COMMENT] argument)*
1427 argument = tfpdef ['=' test]
1428 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1429 args = '*' [tfpdef]
1430 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1431 [TYPE_COMMENT] [kwargs]])
1432 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1433 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1434 [TYPE_COMMENT] [args_kwonly_kwargs]])
1435 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1436 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1437 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1438
1439 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1440 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1441 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1442 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1443 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1444 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1445 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1446 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1447 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1448 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1449 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1450 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1451 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1452 '**' tfpdef [','] [TYPE_COMMENT]))
1453
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001454 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001455
1456 The following definition for varargslist is equivalent to this set of rules:
1457
1458 arguments = argument (',' argument )*
1459 argument = vfpdef ['=' test]
1460 kwargs = '**' vfpdef [',']
1461 args = '*' [vfpdef]
1462 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1463 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1464 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1465 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1466 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1467 (vararglist_no_posonly)
1468
1469 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1470 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1471 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1472 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1473 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1474 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1475 [',']]] | '**' vfpdef [','])
1476
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001477 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001478
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001480 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001481 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001482 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001483 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001484 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 node *ch;
1486
1487 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001489 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001490 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001492 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Jeremy Hyltone921e022008-07-17 16:37:17 +00001494 /* First count the number of positional args & defaults. The
1495 variable i is the loop index for this for loop and the next.
1496 The next loop picks up where the first leaves off.
1497 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001499 ch = CHILD(n, i);
1500 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001501 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001502 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001503 if (i < NCH(n) && /* skip argument following star */
1504 (TYPE(CHILD(n, i)) == tfpdef ||
1505 TYPE(CHILD(n, i)) == vfpdef)) {
1506 i++;
1507 }
1508 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001509 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001510 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001511 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001512 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001513 if (TYPE(ch) == SLASH ) {
1514 nposonlyargs = nposargs;
1515 nposargs = 0;
1516 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001519 defaults for keyword only args */
1520 for ( ; i < NCH(n); ++i) {
1521 ch = CHILD(n, i);
1522 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001523 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001524 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001525 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1526 if (!posonlyargs && nposonlyargs) {
1527 return NULL;
1528 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001529 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001530 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001531 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001532 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001533 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001534 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001535 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001537 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001538 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001539 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001541 since we set NULL as default for keyword only argument w/o default
1542 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001543 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001544 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001545 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001546 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001548 /* tfpdef: NAME [':' test]
1549 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 */
1551 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001552 j = 0; /* index for defaults */
1553 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001554 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 ch = CHILD(n, i);
1557 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001558 case tfpdef:
1559 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1561 anything other than EQUAL or a comma? */
1562 /* XXX Should NCH(n) check be made a separate check? */
1563 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001564 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1565 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001566 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001567 assert(posdefaults != NULL);
1568 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001570 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001572 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001573 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001574 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001575 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001576 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001577 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001578 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001579 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001580 if (l < nposonlyargs) {
1581 asdl_seq_SET(posonlyargs, l++, arg);
1582 } else {
1583 asdl_seq_SET(posargs, k++, arg);
1584 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001585 i += 1; /* the name */
1586 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1587 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001589 case SLASH:
1590 /* Advance the slash and the comma. If there are more names
1591 * after the slash there will be a comma so we are advancing
1592 * the correct number of nodes. If the slash is the last item,
1593 * we will be advancing an extra token but then * i > NCH(n)
1594 * and the enclosing while will finish correctly. */
1595 i += 2;
1596 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001598 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001599 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1600 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001601 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001602 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001603 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001604 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001605 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001606 if (TYPE(ch) == COMMA) {
1607 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001608 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001609
1610 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1611 ast_error(c, CHILD(n, i),
1612 "bare * has associated type comment");
1613 return NULL;
1614 }
1615
Guido van Rossum4f72a782006-10-27 23:31:49 +00001616 res = handle_keywordonly_args(c, n, i,
1617 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001618 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001619 i = res; /* res has new position to process */
1620 }
1621 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001622 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001623 if (!vararg)
1624 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001625
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001626 i += 2; /* the star and the name */
1627 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1628 i += 1; /* the comma, if present */
1629
1630 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1631 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1632 if (!vararg->type_comment)
1633 return NULL;
1634 i += 1;
1635 }
1636
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001637 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1638 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001639 int res = 0;
1640 res = handle_keywordonly_args(c, n, i,
1641 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001642 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001643 i = res; /* res has new position to process */
1644 }
1645 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 break;
1647 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001648 ch = CHILD(n, i+1); /* tfpdef */
1649 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001650 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001651 if (!kwarg)
1652 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001653 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001654 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001655 i += 1; /* the comma, if present */
1656 break;
1657 case TYPE_COMMENT:
1658 assert(i);
1659
1660 if (kwarg)
1661 arg = kwarg;
1662
1663 /* arg will be equal to the last argument processed */
1664 arg->type_comment = NEW_TYPE_COMMENT(ch);
1665 if (!arg->type_comment)
1666 return NULL;
1667 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 break;
1669 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001670 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 "unexpected node in varargslist: %d @ %d",
1672 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001673 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001676 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
1679static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680ast_for_decorator(struct compiling *c, const node *n)
1681{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001682 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001685 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001686 REQ(CHILD(n, 2), NEWLINE);
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001687
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001688 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689}
1690
1691static asdl_seq*
1692ast_for_decorators(struct compiling *c, const node *n)
1693{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001694 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001695 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001699 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 if (!decorator_seq)
1701 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001704 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001705 if (!d)
1706 return NULL;
1707 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 }
1709 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710}
1711
1712static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001713ast_for_funcdef_impl(struct compiling *c, const node *n0,
1714 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001716 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001717 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001718 identifier name;
1719 arguments_ty args;
1720 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001721 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001722 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001723 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001724 node *tc;
1725 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726
Guido van Rossum495da292019-03-07 12:38:08 -08001727 if (is_async && c->c_feature_version < 5) {
1728 ast_error(c, n,
1729 "Async functions are only supported in Python 3.5 and greater");
1730 return NULL;
1731 }
1732
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 REQ(n, funcdef);
1734
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 name = NEW_IDENTIFIER(CHILD(n, name_i));
1736 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001737 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001738 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1741 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001742 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001743 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1744 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1745 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001746 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001747 name_i += 2;
1748 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001749 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1750 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1751 if (!type_comment)
1752 return NULL;
1753 name_i += 1;
1754 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001755 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001758 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001760 if (NCH(CHILD(n, name_i + 3)) > 1) {
1761 /* Check if the suite has a type comment in it. */
1762 tc = CHILD(CHILD(n, name_i + 3), 1);
1763
1764 if (TYPE(tc) == TYPE_COMMENT) {
1765 if (type_comment != NULL) {
1766 ast_error(c, n, "Cannot have two type comments on def");
1767 return NULL;
1768 }
1769 type_comment = NEW_TYPE_COMMENT(tc);
1770 if (!type_comment)
1771 return NULL;
1772 }
1773 }
1774
Yury Selivanov75445082015-05-11 22:57:16 -04001775 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001776 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001777 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001778 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001779 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001780 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001781}
1782
1783static stmt_ty
1784ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1785{
Guido van Rossum495da292019-03-07 12:38:08 -08001786 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001787 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001788 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001789 REQ(CHILD(n, 1), funcdef);
1790
guoci90fc8982018-09-11 17:45:45 -04001791 return ast_for_funcdef_impl(c, n, decorator_seq,
1792 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001793}
1794
1795static stmt_ty
1796ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1797{
1798 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1799 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001800 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001801}
1802
1803
1804static stmt_ty
1805ast_for_async_stmt(struct compiling *c, const node *n)
1806{
Guido van Rossum495da292019-03-07 12:38:08 -08001807 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001808 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001809 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001810
1811 switch (TYPE(CHILD(n, 1))) {
1812 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001813 return ast_for_funcdef_impl(c, n, NULL,
1814 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001815 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001816 return ast_for_with_stmt(c, n,
1817 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001818
1819 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001820 return ast_for_for_stmt(c, n,
1821 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001822
1823 default:
1824 PyErr_Format(PyExc_SystemError,
1825 "invalid async stament: %s",
1826 STR(CHILD(n, 1)));
1827 return NULL;
1828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829}
1830
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001831static stmt_ty
1832ast_for_decorated(struct compiling *c, const node *n)
1833{
Yury Selivanov75445082015-05-11 22:57:16 -04001834 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001835 stmt_ty thing = NULL;
1836 asdl_seq *decorator_seq = NULL;
1837
1838 REQ(n, decorated);
1839
1840 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1841 if (!decorator_seq)
1842 return NULL;
1843
1844 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001845 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001847
1848 if (TYPE(CHILD(n, 1)) == funcdef) {
1849 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1850 } else if (TYPE(CHILD(n, 1)) == classdef) {
1851 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1853 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001854 }
1855 return thing;
1856}
1857
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001859ast_for_namedexpr(struct compiling *c, const node *n)
1860{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001861 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001862 argument: ( test [comp_for] |
1863 test ':=' test |
1864 test '=' test |
1865 '**' test |
1866 '*' test )
1867 */
1868 expr_ty target, value;
1869
1870 target = ast_for_expr(c, CHILD(n, 0));
1871 if (!target)
1872 return NULL;
1873
1874 value = ast_for_expr(c, CHILD(n, 2));
1875 if (!value)
1876 return NULL;
1877
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001878 if (target->kind != Name_kind) {
1879 const char *expr_name = get_expr_name(target);
1880 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001881 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001882 }
1883 return NULL;
1884 }
1885
1886 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001887 return NULL;
1888
1889 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1890 n->n_end_col_offset, c->c_arena);
1891}
1892
1893static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894ast_for_lambdef(struct compiling *c, const node *n)
1895{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001896 /* lambdef: 'lambda' [varargslist] ':' test
1897 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 arguments_ty args;
1899 expr_ty expression;
1900
1901 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001902 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 if (!args)
1904 return NULL;
1905 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001906 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 }
1909 else {
1910 args = ast_for_arguments(c, CHILD(n, 1));
1911 if (!args)
1912 return NULL;
1913 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001914 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 }
1917
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001918 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1919 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920}
1921
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001922static expr_ty
1923ast_for_ifexpr(struct compiling *c, const node *n)
1924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001926 expr_ty expression, body, orelse;
1927
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001928 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001929 body = ast_for_expr(c, CHILD(n, 0));
1930 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001931 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001932 expression = ast_for_expr(c, CHILD(n, 2));
1933 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001934 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001935 orelse = ast_for_expr(c, CHILD(n, 4));
1936 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001937 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001938 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001939 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001941}
1942
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001944 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
Nick Coghlan650f0d02007-04-15 12:05:43 +00001946 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947*/
1948
1949static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001950count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001952 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953
Guido van Rossumd8faa362007-04-27 19:54:29 +00001954 count_comp_for:
1955 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001956 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001957 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001958 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001959 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001960 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001961 else if (NCH(n) == 1) {
1962 n = CHILD(n, 0);
1963 }
1964 else {
1965 goto error;
1966 }
1967 if (NCH(n) == (5)) {
1968 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001969 }
1970 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001971 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001972 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001973 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001974 REQ(n, comp_iter);
1975 n = CHILD(n, 0);
1976 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001977 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001978 else if (TYPE(n) == comp_if) {
1979 if (NCH(n) == 3) {
1980 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001981 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001982 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001983 else
1984 return n_fors;
1985 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001986
Jelle Zijlstraac317702017-10-05 20:24:46 -07001987 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001988 /* Should never be reached */
1989 PyErr_SetString(PyExc_SystemError,
1990 "logic error in count_comp_fors");
1991 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992}
1993
Nick Coghlan650f0d02007-04-15 12:05:43 +00001994/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995
Nick Coghlan650f0d02007-04-15 12:05:43 +00001996 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997*/
1998
1999static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002000count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002002 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003
Guido van Rossumd8faa362007-04-27 19:54:29 +00002004 while (1) {
2005 REQ(n, comp_iter);
2006 if (TYPE(CHILD(n, 0)) == comp_for)
2007 return n_ifs;
2008 n = CHILD(n, 0);
2009 REQ(n, comp_if);
2010 n_ifs++;
2011 if (NCH(n) == 2)
2012 return n_ifs;
2013 n = CHILD(n, 2);
2014 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015}
2016
Guido van Rossum992d4a32007-07-11 13:09:30 +00002017static asdl_seq *
2018ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002021 asdl_seq *comps;
2022
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002023 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 if (n_fors == -1)
2025 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002026
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002027 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002028 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002032 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002034 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002035 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002036 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002037 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038
Guido van Rossum992d4a32007-07-11 13:09:30 +00002039 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040
Jelle Zijlstraac317702017-10-05 20:24:46 -07002041 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002042 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002043 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002044 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002045 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002046 else {
2047 sync_n = CHILD(n, 0);
2048 }
2049 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002050
Guido van Rossum495da292019-03-07 12:38:08 -08002051 /* Async comprehensions only allowed in Python 3.6 and greater */
2052 if (is_async && c->c_feature_version < 6) {
2053 ast_error(c, n,
2054 "Async comprehensions are only supported in Python 3.6 and greater");
2055 return NULL;
2056 }
2057
Jelle Zijlstraac317702017-10-05 20:24:46 -07002058 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002059 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002060 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002062 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065
Thomas Wouters89f507f2006-12-13 04:49:30 +00002066 /* Check the # of children rather than the length of t, since
2067 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002068 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002069 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002070 comp = comprehension(first, expression, NULL,
2071 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002073 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2074 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2075 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002076 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079
Jelle Zijlstraac317702017-10-05 20:24:46 -07002080 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 int j, n_ifs;
2082 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083
Jelle Zijlstraac317702017-10-05 20:24:46 -07002084 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002085 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002086 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002089 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002090 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002094 REQ(n, comp_iter);
2095 n = CHILD(n, 0);
2096 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097
Guido van Rossum992d4a32007-07-11 13:09:30 +00002098 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002100 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002101 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002102 if (NCH(n) == 3)
2103 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002105 /* on exit, must guarantee that n is a comp_for */
2106 if (TYPE(n) == comp_iter)
2107 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002108 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002110 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002112 return comps;
2113}
2114
2115static expr_ty
2116ast_for_itercomp(struct compiling *c, const node *n, int type)
2117{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002118 /* testlist_comp: (test|star_expr)
2119 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002120 expr_ty elt;
2121 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002122 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123
Guido van Rossum992d4a32007-07-11 13:09:30 +00002124 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002126 ch = CHILD(n, 0);
2127 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002128 if (!elt)
2129 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002130 if (elt->kind == Starred_kind) {
2131 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2132 return NULL;
2133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134
Guido van Rossum992d4a32007-07-11 13:09:30 +00002135 comps = ast_for_comprehension(c, CHILD(n, 1));
2136 if (!comps)
2137 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002138
2139 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002140 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2141 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002142 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002143 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2144 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002145 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002146 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2147 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002148 else
2149 /* Should never happen */
2150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151}
2152
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002153/* Fills in the key, value pair corresponding to the dict element. In case
2154 * of an unpacking, key is NULL. *i is advanced by the number of ast
2155 * elements. Iff successful, nonzero is returned.
2156 */
2157static int
2158ast_for_dictelement(struct compiling *c, const node *n, int *i,
2159 expr_ty *key, expr_ty *value)
2160{
2161 expr_ty expression;
2162 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2163 assert(NCH(n) - *i >= 2);
2164
2165 expression = ast_for_expr(c, CHILD(n, *i + 1));
2166 if (!expression)
2167 return 0;
2168 *key = NULL;
2169 *value = expression;
2170
2171 *i += 2;
2172 }
2173 else {
2174 assert(NCH(n) - *i >= 3);
2175
2176 expression = ast_for_expr(c, CHILD(n, *i));
2177 if (!expression)
2178 return 0;
2179 *key = expression;
2180
2181 REQ(CHILD(n, *i + 1), COLON);
2182
2183 expression = ast_for_expr(c, CHILD(n, *i + 2));
2184 if (!expression)
2185 return 0;
2186 *value = expression;
2187
2188 *i += 3;
2189 }
2190 return 1;
2191}
2192
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002194ast_for_dictcomp(struct compiling *c, const node *n)
2195{
2196 expr_ty key, value;
2197 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002198 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002201 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002202 assert(key);
2203 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002206 if (!comps)
2207 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002209 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2210 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002211}
2212
2213static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214ast_for_dictdisplay(struct compiling *c, const node *n)
2215{
2216 int i;
2217 int j;
2218 int size;
2219 asdl_seq *keys, *values;
2220
2221 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2222 keys = _Py_asdl_seq_new(size, c->c_arena);
2223 if (!keys)
2224 return NULL;
2225
2226 values = _Py_asdl_seq_new(size, c->c_arena);
2227 if (!values)
2228 return NULL;
2229
2230 j = 0;
2231 for (i = 0; i < NCH(n); i++) {
2232 expr_ty key, value;
2233
2234 if (!ast_for_dictelement(c, n, &i, &key, &value))
2235 return NULL;
2236 asdl_seq_SET(keys, j, key);
2237 asdl_seq_SET(values, j, value);
2238
2239 j++;
2240 }
2241 keys->size = j;
2242 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002243 return Dict(keys, values, LINENO(n), n->n_col_offset,
2244 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002245}
2246
2247static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002248ast_for_genexp(struct compiling *c, const node *n)
2249{
2250 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002251 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002252}
2253
2254static expr_ty
2255ast_for_listcomp(struct compiling *c, const node *n)
2256{
2257 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002258 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002259}
2260
2261static expr_ty
2262ast_for_setcomp(struct compiling *c, const node *n)
2263{
2264 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002265 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002266}
2267
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002268static expr_ty
2269ast_for_setdisplay(struct compiling *c, const node *n)
2270{
2271 int i;
2272 int size;
2273 asdl_seq *elts;
2274
2275 assert(TYPE(n) == (dictorsetmaker));
2276 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2277 elts = _Py_asdl_seq_new(size, c->c_arena);
2278 if (!elts)
2279 return NULL;
2280 for (i = 0; i < NCH(n); i += 2) {
2281 expr_ty expression;
2282 expression = ast_for_expr(c, CHILD(n, i));
2283 if (!expression)
2284 return NULL;
2285 asdl_seq_SET(elts, i / 2, expression);
2286 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002287 return Set(elts, LINENO(n), n->n_col_offset,
2288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002289}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002290
2291static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292ast_for_atom(struct compiling *c, const node *n)
2293{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002294 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2295 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002296 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 */
2298 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002301 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002302 PyObject *name;
2303 const char *s = STR(ch);
2304 size_t len = strlen(s);
2305 if (len >= 4 && len <= 5) {
2306 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002307 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002308 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002309 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002310 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002311 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002312 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002313 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002314 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002315 }
2316 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002317 if (!name)
2318 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002319 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002320 return Name(name, Load, LINENO(n), n->n_col_offset,
2321 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002322 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002324 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002325 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002326 const char *errtype = NULL;
2327 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2328 errtype = "unicode error";
2329 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2330 errtype = "value error";
2331 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002332 PyObject *type, *value, *tback, *errstr;
2333 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002334 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002335 if (errstr) {
2336 ast_error(c, n, "(%s) %U", errtype, errstr);
2337 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002338 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002339 else {
2340 PyErr_Clear();
2341 ast_error(c, n, "(%s) unknown error", errtype);
2342 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002343 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002344 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002345 Py_XDECREF(tback);
2346 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002347 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002348 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002349 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 }
2351 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002352 PyObject *pynum;
2353 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2354 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2355 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2356 ast_error(c, ch,
2357 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2358 return NULL;
2359 }
2360 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002361 if (!pynum)
2362 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002363
Victor Stinner43d81952013-07-17 00:57:58 +02002364 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2365 Py_DECREF(pynum);
2366 return NULL;
2367 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002368 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002369 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 }
Georg Brandldde00282007-03-18 19:01:53 +00002371 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002372 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002373 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002375 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376
Thomas Wouters89f507f2006-12-13 04:49:30 +00002377 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002378 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2379 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380
Thomas Wouters89f507f2006-12-13 04:49:30 +00002381 if (TYPE(ch) == yield_expr)
2382 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002385 if (NCH(ch) == 1) {
2386 return ast_for_testlist(c, ch);
2387 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002388
Serhiy Storchakab619b092018-11-27 09:40:29 +02002389 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002390 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002391 }
2392 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002393 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002394 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002396 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397
Thomas Wouters89f507f2006-12-13 04:49:30 +00002398 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002399 return List(NULL, Load, LINENO(n), n->n_col_offset,
2400 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401
Nick Coghlan650f0d02007-04-15 12:05:43 +00002402 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002403 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2404 asdl_seq *elts = seq_for_testlist(c, ch);
2405 if (!elts)
2406 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002407
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002408 return List(elts, Load, LINENO(n), n->n_col_offset,
2409 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002410 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002411 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002412 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002413 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002415 /* dictorsetmaker: ( ((test ':' test | '**' test)
2416 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2417 * ((test | '*' test)
2418 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002419 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002420 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002421 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002422 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002423 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2424 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 }
2426 else {
2427 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2428 if (NCH(ch) == 1 ||
2429 (NCH(ch) > 1 &&
2430 TYPE(CHILD(ch, 1)) == COMMA)) {
2431 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002432 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002433 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002434 else if (NCH(ch) > 1 &&
2435 TYPE(CHILD(ch, 1)) == comp_for) {
2436 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002437 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002438 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002439 else if (NCH(ch) > 3 - is_dict &&
2440 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2441 /* It's a dictionary comprehension. */
2442 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002443 ast_error(c, n,
2444 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002445 return NULL;
2446 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002447 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002448 }
2449 else {
2450 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002451 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002452 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002453 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002457 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2458 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 }
2460}
2461
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002462static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463ast_for_slice(struct compiling *c, const node *n)
2464{
2465 node *ch;
2466 expr_ty lower = NULL, upper = NULL, step = NULL;
2467
2468 REQ(n, subscript);
2469
2470 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002471 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 sliceop: ':' [test]
2473 */
2474 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002476 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 }
2478
2479 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002480 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 if (!lower)
2482 return NULL;
2483 }
2484
2485 /* If there's an upper bound it's in the second or third position. */
2486 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002487 if (NCH(n) > 1) {
2488 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489
Thomas Wouters89f507f2006-12-13 04:49:30 +00002490 if (TYPE(n2) == test) {
2491 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 if (!upper)
2493 return NULL;
2494 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002495 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002497 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498
Thomas Wouters89f507f2006-12-13 04:49:30 +00002499 if (TYPE(n2) == test) {
2500 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 if (!upper)
2502 return NULL;
2503 }
2504 }
2505
2506 ch = CHILD(n, NCH(n) - 1);
2507 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002508 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002509 ch = CHILD(ch, 1);
2510 if (TYPE(ch) == test) {
2511 step = ast_for_expr(c, ch);
2512 if (!step)
2513 return NULL;
2514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 }
2516 }
2517
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002518 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2519 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520}
2521
2522static expr_ty
2523ast_for_binop(struct compiling *c, const node *n)
2524{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002525 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002527 BinOp(BinOp(A, op, B), op, C).
2528 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529
Guido van Rossumd8faa362007-04-27 19:54:29 +00002530 int i, nops;
2531 expr_ty expr1, expr2, result;
2532 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533
Guido van Rossumd8faa362007-04-27 19:54:29 +00002534 expr1 = ast_for_expr(c, CHILD(n, 0));
2535 if (!expr1)
2536 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537
Guido van Rossumd8faa362007-04-27 19:54:29 +00002538 expr2 = ast_for_expr(c, CHILD(n, 2));
2539 if (!expr2)
2540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541
Guido van Rossum495da292019-03-07 12:38:08 -08002542 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002543 if (!newoperator)
2544 return NULL;
2545
2546 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002547 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002548 c->c_arena);
2549 if (!result)
2550 return NULL;
2551
2552 nops = (NCH(n) - 1) / 2;
2553 for (i = 1; i < nops; i++) {
2554 expr_ty tmp_result, tmp;
2555 const node* next_oper = CHILD(n, i * 2 + 1);
2556
Guido van Rossum495da292019-03-07 12:38:08 -08002557 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002558 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 return NULL;
2560
Guido van Rossumd8faa362007-04-27 19:54:29 +00002561 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2562 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 return NULL;
2564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002566 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002567 CHILD(n, i * 2 + 2)->n_end_lineno,
2568 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002569 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002571 return NULL;
2572 result = tmp_result;
2573 }
2574 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575}
2576
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002577static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002578ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002581 subscriptlist: subscript (',' subscript)* [',']
2582 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2583 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002584 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002585 REQ(n, trailer);
2586 if (TYPE(CHILD(n, 0)) == LPAR) {
2587 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002588 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002589 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002590 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002591 return ast_for_call(c, CHILD(n, 1), left_expr,
2592 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002593 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002594 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002595 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2596 if (!attr_id)
2597 return NULL;
2598 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002599 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002600 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002601 }
2602 else {
2603 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002604 REQ(CHILD(n, 2), RSQB);
2605 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002606 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002607 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002608 if (!slc)
2609 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002610 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002611 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002612 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002613 }
2614 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002615 int j;
2616 expr_ty slc, e;
2617 asdl_seq *elts;
2618 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2619 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002620 return NULL;
2621 for (j = 0; j < NCH(n); j += 2) {
2622 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002623 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002624 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002625 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002626 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002627 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002628 n->n_end_lineno, n->n_end_col_offset,
2629 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002630 if (!e)
2631 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002632 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002633 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002634 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2635 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002636 }
2637 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002638}
2639
2640static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002641ast_for_factor(struct compiling *c, const node *n)
2642{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002643 expr_ty expression;
2644
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002645 expression = ast_for_expr(c, CHILD(n, 1));
2646 if (!expression)
2647 return NULL;
2648
2649 switch (TYPE(CHILD(n, 0))) {
2650 case PLUS:
2651 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002652 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002653 c->c_arena);
2654 case MINUS:
2655 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002656 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002657 c->c_arena);
2658 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002659 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2660 n->n_end_lineno, n->n_end_col_offset,
2661 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002662 }
2663 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2664 TYPE(CHILD(n, 0)));
2665 return NULL;
2666}
2667
2668static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002669ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002670{
Yury Selivanov75445082015-05-11 22:57:16 -04002671 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002672 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002673
2674 REQ(n, atom_expr);
2675 nch = NCH(n);
2676
Guido van Rossum495da292019-03-07 12:38:08 -08002677 if (TYPE(CHILD(n, 0)) == AWAIT) {
2678 if (c->c_feature_version < 5) {
2679 ast_error(c, n,
2680 "Await expressions are only supported in Python 3.5 and greater");
2681 return NULL;
2682 }
Yury Selivanov75445082015-05-11 22:57:16 -04002683 start = 1;
2684 assert(nch > 1);
2685 }
2686
2687 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002688 if (!e)
2689 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002690 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002691 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002692 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002693 return Await(e, LINENO(n), n->n_col_offset,
2694 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002695 }
2696
2697 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002698 node *ch = CHILD(n, i);
2699 if (TYPE(ch) != trailer)
2700 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002701 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2702 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002703 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002704 }
Yury Selivanov75445082015-05-11 22:57:16 -04002705
2706 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002707 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002708 return Await(e, LINENO(n), n->n_col_offset,
2709 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002710 }
2711 else {
2712 return e;
2713 }
2714}
2715
2716static expr_ty
2717ast_for_power(struct compiling *c, const node *n)
2718{
2719 /* power: atom trailer* ('**' factor)*
2720 */
2721 expr_ty e;
2722 REQ(n, power);
2723 e = ast_for_atom_expr(c, CHILD(n, 0));
2724 if (!e)
2725 return NULL;
2726 if (NCH(n) == 1)
2727 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002728 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2729 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002730 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002731 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002732 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2733 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002734 }
2735 return e;
2736}
2737
Guido van Rossum0368b722007-05-11 16:50:42 +00002738static expr_ty
2739ast_for_starred(struct compiling *c, const node *n)
2740{
2741 expr_ty tmp;
2742 REQ(n, star_expr);
2743
2744 tmp = ast_for_expr(c, CHILD(n, 1));
2745 if (!tmp)
2746 return NULL;
2747
2748 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002749 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2750 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002751}
2752
2753
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754/* Do not name a variable 'expr'! Will cause a compile error.
2755*/
2756
2757static expr_ty
2758ast_for_expr(struct compiling *c, const node *n)
2759{
2760 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002761 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002762 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002763 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 and_test: not_test ('and' not_test)*
2766 not_test: 'not' not_test | comparison
2767 comparison: expr (comp_op expr)*
2768 expr: xor_expr ('|' xor_expr)*
2769 xor_expr: and_expr ('^' and_expr)*
2770 and_expr: shift_expr ('&' shift_expr)*
2771 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2772 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002773 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002775 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002776 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002777 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 */
2779
2780 asdl_seq *seq;
2781 int i;
2782
2783 loop:
2784 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002785 case namedexpr_test:
2786 if (NCH(n) == 3)
2787 return ast_for_namedexpr(c, n);
2788 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002790 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002791 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002794 else if (NCH(n) > 1)
2795 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002796 /* Fallthrough */
2797 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 case and_test:
2799 if (NCH(n) == 1) {
2800 n = CHILD(n, 0);
2801 goto loop;
2802 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002803 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 if (!seq)
2805 return NULL;
2806 for (i = 0; i < NCH(n); i += 2) {
2807 expr_ty e = ast_for_expr(c, CHILD(n, i));
2808 if (!e)
2809 return NULL;
2810 asdl_seq_SET(seq, i / 2, e);
2811 }
2812 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002813 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002814 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002815 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002816 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002817 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2818 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 case not_test:
2820 if (NCH(n) == 1) {
2821 n = CHILD(n, 0);
2822 goto loop;
2823 }
2824 else {
2825 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2826 if (!expression)
2827 return NULL;
2828
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002829 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002830 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 }
2833 case comparison:
2834 if (NCH(n) == 1) {
2835 n = CHILD(n, 0);
2836 goto loop;
2837 }
2838 else {
2839 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002840 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002841 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002842 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 if (!ops)
2844 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002845 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 return NULL;
2848 }
2849 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002850 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002852 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002853 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856
2857 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002858 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002862 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 asdl_seq_SET(cmps, i / 2, expression);
2864 }
2865 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002866 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002870 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2871 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873
Guido van Rossum0368b722007-05-11 16:50:42 +00002874 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 /* The next five cases all handle BinOps. The main body of code
2877 is the same in each case, but the switch turned inside out to
2878 reuse the code for each type of operator.
2879 */
2880 case expr:
2881 case xor_expr:
2882 case and_expr:
2883 case shift_expr:
2884 case arith_expr:
2885 case term:
2886 if (NCH(n) == 1) {
2887 n = CHILD(n, 0);
2888 goto loop;
2889 }
2890 return ast_for_binop(c, n);
2891 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002892 node *an = NULL;
2893 node *en = NULL;
2894 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002895 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002896 if (NCH(n) > 1)
2897 an = CHILD(n, 1); /* yield_arg */
2898 if (an) {
2899 en = CHILD(an, NCH(an) - 1);
2900 if (NCH(an) == 2) {
2901 is_from = 1;
2902 exp = ast_for_expr(c, en);
2903 }
2904 else
2905 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002906 if (!exp)
2907 return NULL;
2908 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002909 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002910 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2911 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2912 return Yield(exp, LINENO(n), n->n_col_offset,
2913 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002915 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 if (NCH(n) == 1) {
2917 n = CHILD(n, 0);
2918 goto loop;
2919 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002920 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002921 case power:
2922 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002924 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 return NULL;
2926 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002927 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002928 return NULL;
2929}
2930
2931static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002932ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002933 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934{
2935 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002936 arglist: argument (',' argument)* [',']
2937 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 */
2939
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002940 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002941 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002942 asdl_seq *args;
2943 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944
2945 REQ(n, arglist);
2946
2947 nargs = 0;
2948 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002950 node *ch = CHILD(n, i);
2951 if (TYPE(ch) == argument) {
2952 if (NCH(ch) == 1)
2953 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002954 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2955 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002956 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002957 ast_error(c, ch, "invalid syntax");
2958 return NULL;
2959 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002960 if (NCH(n) > 1) {
2961 ast_error(c, ch, "Generator expression must be parenthesized");
2962 return NULL;
2963 }
2964 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002965 else if (TYPE(CHILD(ch, 0)) == STAR)
2966 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002967 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2968 nargs++;
2969 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002971 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002972 nkeywords++;
2973 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002976 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002978 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002979 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002981 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002982
2983 nargs = 0; /* positional arguments + iterable argument unpackings */
2984 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2985 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002987 node *ch = CHILD(n, i);
2988 if (TYPE(ch) == argument) {
2989 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002990 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002991 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002992 /* a positional argument */
2993 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002994 if (ndoublestars) {
2995 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002996 "positional argument follows "
2997 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002998 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002999 else {
3000 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003001 "positional argument follows "
3002 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003003 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003004 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003005 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003006 e = ast_for_expr(c, chch);
3007 if (!e)
3008 return NULL;
3009 asdl_seq_SET(args, nargs++, e);
3010 }
3011 else if (TYPE(chch) == STAR) {
3012 /* an iterable argument unpacking */
3013 expr_ty starred;
3014 if (ndoublestars) {
3015 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003016 "iterable argument unpacking follows "
3017 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003018 return NULL;
3019 }
3020 e = ast_for_expr(c, CHILD(ch, 1));
3021 if (!e)
3022 return NULL;
3023 starred = Starred(e, Load, LINENO(chch),
3024 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003025 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003026 c->c_arena);
3027 if (!starred)
3028 return NULL;
3029 asdl_seq_SET(args, nargs++, starred);
3030
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003031 }
3032 else if (TYPE(chch) == DOUBLESTAR) {
3033 /* a keyword argument unpacking */
3034 keyword_ty kw;
3035 i++;
3036 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003038 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003039 kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset,
3040 e->end_lineno, e->end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003041 asdl_seq_SET(keywords, nkeywords++, kw);
3042 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003044 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003045 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003046 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003048 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003051 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3052 /* treat colon equal as positional argument */
3053 if (nkeywords) {
3054 if (ndoublestars) {
3055 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003056 "positional argument follows "
3057 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003058 }
3059 else {
3060 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003061 "positional argument follows "
3062 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003063 }
3064 return NULL;
3065 }
3066 e = ast_for_namedexpr(c, ch);
3067 if (!e)
3068 return NULL;
3069 asdl_seq_SET(args, nargs++, e);
3070 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003071 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003072 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003073 keyword_ty kw;
Pablo Galindo254ec782020-04-03 20:37:13 +01003074 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003076 // To remain LL(1), the grammar accepts any test (basically, any
3077 // expression) in the keyword slot of a call site. So, we need
3078 // to manually enforce that the keyword is a NAME here.
3079 static const int name_tree[] = {
3080 test,
3081 or_test,
3082 and_test,
3083 not_test,
3084 comparison,
3085 expr,
3086 xor_expr,
3087 and_expr,
3088 shift_expr,
3089 arith_expr,
3090 term,
3091 factor,
3092 power,
3093 atom_expr,
3094 atom,
3095 0,
3096 };
3097 node *expr_node = chch;
3098 for (int i = 0; name_tree[i]; i++) {
3099 if (TYPE(expr_node) != name_tree[i])
3100 break;
3101 if (NCH(expr_node) != 1)
3102 break;
3103 expr_node = CHILD(expr_node, 0);
3104 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003105 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003106 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003107 "expression cannot contain assignment, "
3108 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003109 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003110 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003111 key = new_identifier(STR(expr_node), c);
3112 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003113 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003115 if (forbidden_name(c, key, chch, 1)) {
3116 return NULL;
3117 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003118 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003120 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003121 kw = keyword(key, e, chch->n_lineno, chch->n_col_offset,
Pablo Galindo40cf35c2020-04-03 21:02:26 +01003122 e->end_lineno, e->end_col_offset, c->c_arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01003123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003125 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003126 asdl_seq_SET(keywords, nkeywords++, kw);
3127 }
3128 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 }
3130
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003131 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003132 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133}
3134
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003136ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003138 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003139 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003141 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003142 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003143 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003144 }
3145 else {
3146 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003147 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003148 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003150 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 else {
3152 asdl_seq *tmp = seq_for_testlist(c, n);
3153 if (!tmp)
3154 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003155 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3156 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003158}
3159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160static stmt_ty
3161ast_for_expr_stmt(struct compiling *c, const node *n)
3162{
3163 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003164 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003165 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3166 annassign: ':' test ['=' (yield_expr|testlist)]
3167 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3168 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3169 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003170 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003172 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003174 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003175 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 if (!e)
3177 return NULL;
3178
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003179 return Expr(e, LINENO(n), n->n_col_offset,
3180 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 }
3182 else if (TYPE(CHILD(n, 1)) == augassign) {
3183 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003184 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003185 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186
Thomas Wouters89f507f2006-12-13 04:49:30 +00003187 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188 if (!expr1)
3189 return NULL;
Pablo Galindo16ab0702020-05-15 02:04:52 +01003190 /* Augmented assignments can only have a name, a subscript, or an
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003191 attribute on the left, though, so we have to explicitly check for
3192 those. */
3193 switch (expr1->kind) {
3194 case Name_kind:
3195 case Attribute_kind:
3196 case Subscript_kind:
3197 break;
3198 default:
Pablo Galindo16ab0702020-05-15 02:04:52 +01003199 ast_error(c, ch, "'%s' is an illegal expression for augmented assignment",
3200 get_expr_name(expr1));
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003201 return NULL;
3202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203
Pablo Galindo16ab0702020-05-15 02:04:52 +01003204 /* set_context checks that most expressions are not the left side. */
3205 if(!set_context(c, expr1, Store, ch)) {
3206 return NULL;
3207 }
3208
Thomas Wouters89f507f2006-12-13 04:49:30 +00003209 ch = CHILD(n, 2);
3210 if (TYPE(ch) == testlist)
3211 expr2 = ast_for_testlist(c, ch);
3212 else
3213 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003214 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 return NULL;
3216
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003217 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003218 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 return NULL;
3220
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003221 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3222 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003224 else if (TYPE(CHILD(n, 1)) == annassign) {
3225 expr_ty expr1, expr2, expr3;
3226 node *ch = CHILD(n, 0);
3227 node *deep, *ann = CHILD(n, 1);
3228 int simple = 1;
3229
Guido van Rossum495da292019-03-07 12:38:08 -08003230 /* AnnAssigns are only allowed in Python 3.6 or greater */
3231 if (c->c_feature_version < 6) {
3232 ast_error(c, ch,
3233 "Variable annotation syntax is only supported in Python 3.6 and greater");
3234 return NULL;
3235 }
3236
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003237 /* we keep track of parens to qualify (x) as expression not name */
3238 deep = ch;
3239 while (NCH(deep) == 1) {
3240 deep = CHILD(deep, 0);
3241 }
3242 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3243 simple = 0;
3244 }
3245 expr1 = ast_for_testlist(c, ch);
3246 if (!expr1) {
3247 return NULL;
3248 }
3249 switch (expr1->kind) {
3250 case Name_kind:
3251 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3252 return NULL;
3253 }
3254 expr1->v.Name.ctx = Store;
3255 break;
3256 case Attribute_kind:
3257 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3258 return NULL;
3259 }
3260 expr1->v.Attribute.ctx = Store;
3261 break;
3262 case Subscript_kind:
3263 expr1->v.Subscript.ctx = Store;
3264 break;
3265 case List_kind:
3266 ast_error(c, ch,
3267 "only single target (not list) can be annotated");
3268 return NULL;
3269 case Tuple_kind:
3270 ast_error(c, ch,
3271 "only single target (not tuple) can be annotated");
3272 return NULL;
3273 default:
3274 ast_error(c, ch,
3275 "illegal target for annotation");
3276 return NULL;
3277 }
3278
3279 if (expr1->kind != Name_kind) {
3280 simple = 0;
3281 }
3282 ch = CHILD(ann, 1);
3283 expr2 = ast_for_expr(c, ch);
3284 if (!expr2) {
3285 return NULL;
3286 }
3287 if (NCH(ann) == 2) {
3288 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003289 LINENO(n), n->n_col_offset,
3290 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003291 }
3292 else {
3293 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003294 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003295 expr3 = ast_for_testlist(c, ch);
3296 }
3297 else {
3298 expr3 = ast_for_expr(c, ch);
3299 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003300 if (!expr3) {
3301 return NULL;
3302 }
3303 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003304 LINENO(n), n->n_col_offset,
3305 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003306 }
3307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003309 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 asdl_seq *targets;
3311 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003313 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314
Thomas Wouters89f507f2006-12-13 04:49:30 +00003315 /* a normal assignment */
3316 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003317
3318 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3319 nch_minus_type = num - has_type_comment;
3320
3321 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 if (!targets)
3323 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003324 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 expr_ty e;
3326 node *ch = CHILD(n, i);
3327 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003328 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003329 return NULL;
3330 }
3331 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003335 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003336 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 asdl_seq_SET(targets, i / 2, e);
3340 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003341 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003342 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003343 expression = ast_for_testlist(c, value);
3344 else
3345 expression = ast_for_expr(c, value);
3346 if (!expression)
3347 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003348 if (has_type_comment) {
3349 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3350 if (!type_comment)
3351 return NULL;
3352 }
3353 else
3354 type_comment = NULL;
3355 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003356 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358}
3359
Benjamin Peterson78565b22009-06-28 19:19:51 +00003360
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003362ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363{
3364 asdl_seq *seq;
3365 int i;
3366 expr_ty e;
3367
3368 REQ(n, exprlist);
3369
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003370 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003372 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003374 e = ast_for_expr(c, CHILD(n, i));
3375 if (!e)
3376 return NULL;
3377 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003378 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380 }
3381 return seq;
3382}
3383
3384static stmt_ty
3385ast_for_del_stmt(struct compiling *c, const node *n)
3386{
3387 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389 /* del_stmt: 'del' exprlist */
3390 REQ(n, del_stmt);
3391
3392 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3393 if (!expr_list)
3394 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003395 return Delete(expr_list, LINENO(n), n->n_col_offset,
3396 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397}
3398
3399static stmt_ty
3400ast_for_flow_stmt(struct compiling *c, const node *n)
3401{
3402 /*
3403 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3404 | yield_stmt
3405 break_stmt: 'break'
3406 continue_stmt: 'continue'
3407 return_stmt: 'return' [testlist]
3408 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003409 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 raise_stmt: 'raise' [test [',' test [',' test]]]
3411 */
3412 node *ch;
3413
3414 REQ(n, flow_stmt);
3415 ch = CHILD(n, 0);
3416 switch (TYPE(ch)) {
3417 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003418 return Break(LINENO(n), n->n_col_offset,
3419 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003421 return Continue(LINENO(n), n->n_col_offset,
3422 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003424 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3425 if (!exp)
3426 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003427 return Expr(exp, LINENO(n), n->n_col_offset,
3428 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429 }
3430 case return_stmt:
3431 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003432 return Return(NULL, LINENO(n), n->n_col_offset,
3433 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003435 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 if (!expression)
3437 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003438 return Return(expression, LINENO(n), n->n_col_offset,
3439 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
3441 case raise_stmt:
3442 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003443 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3444 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003445 else if (NCH(ch) >= 2) {
3446 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3448 if (!expression)
3449 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003450 if (NCH(ch) == 4) {
3451 cause = ast_for_expr(c, CHILD(ch, 3));
3452 if (!cause)
3453 return NULL;
3454 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003455 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3456 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 }
Stefan Krahf432a322017-08-21 13:09:59 +02003458 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003460 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 "unexpected flow_stmt: %d", TYPE(ch));
3462 return NULL;
3463 }
3464}
3465
3466static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003467alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468{
3469 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003470 import_as_name: NAME ['as' NAME]
3471 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 dotted_name: NAME ('.' NAME)*
3473 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003474 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 loop:
3477 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003478 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003479 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003480 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003481 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003482 if (!name)
3483 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003484 if (NCH(n) == 3) {
3485 node *str_node = CHILD(n, 2);
3486 str = NEW_IDENTIFIER(str_node);
3487 if (!str)
3488 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003489 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003490 return NULL;
3491 }
3492 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003493 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003494 return NULL;
3495 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003496 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 case dotted_as_name:
3499 if (NCH(n) == 1) {
3500 n = CHILD(n, 0);
3501 goto loop;
3502 }
3503 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003504 node *asname_node = CHILD(n, 2);
3505 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003506 if (!a)
3507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003509 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003510 if (!a->asname)
3511 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003512 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003513 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514 return a;
3515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003517 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003518 node *name_node = CHILD(n, 0);
3519 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003520 if (!name)
3521 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003522 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003523 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003524 return alias(name, NULL, c->c_arena);
3525 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 else {
3527 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003528 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003529 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532
3533 len = 0;
3534 for (i = 0; i < NCH(n); i += 2)
3535 /* length of string plus one for the dot */
3536 len += strlen(STR(CHILD(n, i))) + 1;
3537 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003538 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 if (!str)
3540 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003541 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 if (!s)
3543 return NULL;
3544 for (i = 0; i < NCH(n); i += 2) {
3545 char *sch = STR(CHILD(n, i));
3546 strcpy(s, STR(CHILD(n, i)));
3547 s += strlen(sch);
3548 *s++ = '.';
3549 }
3550 --s;
3551 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3553 PyBytes_GET_SIZE(str),
3554 NULL);
3555 Py_DECREF(str);
3556 if (!uni)
3557 return NULL;
3558 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003559 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003560 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3561 Py_DECREF(str);
3562 return NULL;
3563 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003564 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003567 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003568 if (!str)
3569 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003570 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3571 Py_DECREF(str);
3572 return NULL;
3573 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003574 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003576 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 "unexpected import name: %d", TYPE(n));
3578 return NULL;
3579 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580}
3581
3582static stmt_ty
3583ast_for_import_stmt(struct compiling *c, const node *n)
3584{
3585 /*
3586 import_stmt: import_name | import_from
3587 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003588 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3589 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003591 int lineno;
3592 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 int i;
3594 asdl_seq *aliases;
3595
3596 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003597 lineno = LINENO(n);
3598 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003600 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003602 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003603 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 if (!aliases)
3605 return NULL;
3606 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003607 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003608 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003612 // Even though n is modified above, the end position is not changed
3613 return Import(aliases, lineno, col_offset,
3614 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003616 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003619 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003620 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003621 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003623 /* Count the number of dots (for relative imports) and check for the
3624 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003625 for (idx = 1; idx < NCH(n); idx++) {
3626 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003627 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3628 if (!mod)
3629 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003630 idx++;
3631 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003632 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003634 ndots += 3;
3635 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003636 } else if (TYPE(CHILD(n, idx)) != DOT) {
3637 break;
3638 }
3639 ndots++;
3640 }
3641 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003642 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003643 case STAR:
3644 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003645 n = CHILD(n, idx);
3646 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003647 break;
3648 case LPAR:
3649 /* from ... import (x, y, z) */
3650 n = CHILD(n, idx + 1);
3651 n_children = NCH(n);
3652 break;
3653 case import_as_names:
3654 /* from ... import x, y, z */
3655 n = CHILD(n, idx);
3656 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003657 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003658 ast_error(c, n,
3659 "trailing comma not allowed without"
3660 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 return NULL;
3662 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003663 break;
3664 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003665 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003666 return NULL;
3667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003669 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003670 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672
3673 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003674 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003675 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003676 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003678 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003680 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003681 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003682 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003683 if (!import_alias)
3684 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003685 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003686 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003688 if (mod != NULL)
3689 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003690 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003691 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003692 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 }
Neal Norwitz79792652005-11-14 04:25:03 +00003694 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 "unknown import statement: starts with command '%s'",
3696 STR(CHILD(n, 0)));
3697 return NULL;
3698}
3699
3700static stmt_ty
3701ast_for_global_stmt(struct compiling *c, const node *n)
3702{
3703 /* global_stmt: 'global' NAME (',' NAME)* */
3704 identifier name;
3705 asdl_seq *s;
3706 int i;
3707
3708 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003709 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003713 name = NEW_IDENTIFIER(CHILD(n, i));
3714 if (!name)
3715 return NULL;
3716 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003718 return Global(s, LINENO(n), n->n_col_offset,
3719 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720}
3721
3722static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003723ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3724{
3725 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3726 identifier name;
3727 asdl_seq *s;
3728 int i;
3729
3730 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003731 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003732 if (!s)
3733 return NULL;
3734 for (i = 1; i < NCH(n); i += 2) {
3735 name = NEW_IDENTIFIER(CHILD(n, i));
3736 if (!name)
3737 return NULL;
3738 asdl_seq_SET(s, i / 2, name);
3739 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003740 return Nonlocal(s, LINENO(n), n->n_col_offset,
3741 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003742}
3743
3744static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745ast_for_assert_stmt(struct compiling *c, const node *n)
3746{
3747 /* assert_stmt: 'assert' test [',' test] */
3748 REQ(n, assert_stmt);
3749 if (NCH(n) == 2) {
3750 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3751 if (!expression)
3752 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003753 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3754 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 }
3756 else if (NCH(n) == 4) {
3757 expr_ty expr1, expr2;
3758
3759 expr1 = ast_for_expr(c, CHILD(n, 1));
3760 if (!expr1)
3761 return NULL;
3762 expr2 = ast_for_expr(c, CHILD(n, 3));
3763 if (!expr2)
3764 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003766 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3767 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 }
Neal Norwitz79792652005-11-14 04:25:03 +00003769 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 "improper number of parts to 'assert' statement: %d",
3771 NCH(n));
3772 return NULL;
3773}
3774
3775static asdl_seq *
3776ast_for_suite(struct compiling *c, const node *n)
3777{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003778 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003779 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 stmt_ty s;
3781 int i, total, num, end, pos = 0;
3782 node *ch;
3783
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003784 if (TYPE(n) != func_body_suite) {
3785 REQ(n, suite);
3786 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787
3788 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003789 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003793 n = CHILD(n, 0);
3794 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003796 */
3797 end = NCH(n) - 1;
3798 if (TYPE(CHILD(n, end - 1)) == SEMI)
3799 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003801 for (i = 0; i < end; i += 2) {
3802 ch = CHILD(n, i);
3803 s = ast_for_stmt(c, ch);
3804 if (!s)
3805 return NULL;
3806 asdl_seq_SET(seq, pos++, s);
3807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 }
3809 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003810 i = 2;
3811 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3812 i += 2;
3813 REQ(CHILD(n, 2), NEWLINE);
3814 }
3815
3816 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003817 ch = CHILD(n, i);
3818 REQ(ch, stmt);
3819 num = num_stmts(ch);
3820 if (num == 1) {
3821 /* small_stmt or compound_stmt with only one child */
3822 s = ast_for_stmt(c, ch);
3823 if (!s)
3824 return NULL;
3825 asdl_seq_SET(seq, pos++, s);
3826 }
3827 else {
3828 int j;
3829 ch = CHILD(ch, 0);
3830 REQ(ch, simple_stmt);
3831 for (j = 0; j < NCH(ch); j += 2) {
3832 /* statement terminates with a semi-colon ';' */
3833 if (NCH(CHILD(ch, j)) == 0) {
3834 assert((j + 1) == NCH(ch));
3835 break;
3836 }
3837 s = ast_for_stmt(c, CHILD(ch, j));
3838 if (!s)
3839 return NULL;
3840 asdl_seq_SET(seq, pos++, s);
3841 }
3842 }
3843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 }
3845 assert(pos == seq->size);
3846 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847}
3848
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003849static void
3850get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3851{
Pablo Galindo46a97922019-02-19 22:51:53 +00003852 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003853 // There must be no empty suites.
3854 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003855 stmt_ty last = asdl_seq_GET(s, tot - 1);
3856 *end_lineno = last->end_lineno;
3857 *end_col_offset = last->end_col_offset;
3858}
3859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860static stmt_ty
3861ast_for_if_stmt(struct compiling *c, const node *n)
3862{
3863 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3864 ['else' ':' suite]
3865 */
3866 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003867 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868
3869 REQ(n, if_stmt);
3870
3871 if (NCH(n) == 4) {
3872 expr_ty expression;
3873 asdl_seq *suite_seq;
3874
3875 expression = ast_for_expr(c, CHILD(n, 1));
3876 if (!expression)
3877 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003879 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003881 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882
Guido van Rossumd8faa362007-04-27 19:54:29 +00003883 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003884 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003886
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 s = STR(CHILD(n, 4));
3888 /* s[2], the third character in the string, will be
3889 's' for el_s_e, or
3890 'i' for el_i_f
3891 */
3892 if (s[2] == 's') {
3893 expr_ty expression;
3894 asdl_seq *seq1, *seq2;
3895
3896 expression = ast_for_expr(c, CHILD(n, 1));
3897 if (!expression)
3898 return NULL;
3899 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003900 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 return NULL;
3902 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003903 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003905 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906
Guido van Rossumd8faa362007-04-27 19:54:29 +00003907 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003908 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 }
3910 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003911 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003912 expr_ty expression;
3913 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003914 asdl_seq *orelse = NULL;
3915 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 /* must reference the child n_elif+1 since 'else' token is third,
3917 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003918 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3919 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3920 has_else = 1;
3921 n_elif -= 3;
3922 }
3923 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924
Thomas Wouters89f507f2006-12-13 04:49:30 +00003925 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003926 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003928 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003929 if (!orelse)
3930 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003932 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003934 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3935 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003937 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3938 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003940 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 asdl_seq_SET(orelse, 0,
3943 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003944 LINENO(CHILD(n, NCH(n) - 7)),
3945 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003946 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003947 /* the just-created orelse handled the last elif */
3948 n_elif--;
3949 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950
Thomas Wouters89f507f2006-12-13 04:49:30 +00003951 for (i = 0; i < n_elif; i++) {
3952 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003953 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003954 if (!newobj)
3955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003957 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003960 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003963 if (orelse != NULL) {
3964 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3965 } else {
3966 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3967 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003968 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003970 LINENO(CHILD(n, off - 1)),
3971 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003972 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003973 orelse = newobj;
3974 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003975 expression = ast_for_expr(c, CHILD(n, 1));
3976 if (!expression)
3977 return NULL;
3978 suite_seq = ast_for_suite(c, CHILD(n, 3));
3979 if (!suite_seq)
3980 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003981 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003982 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003983 LINENO(n), n->n_col_offset,
3984 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003986
3987 PyErr_Format(PyExc_SystemError,
3988 "unexpected token in 'if' statement: %s", s);
3989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990}
3991
3992static stmt_ty
3993ast_for_while_stmt(struct compiling *c, const node *n)
3994{
3995 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3996 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003997 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998
3999 if (NCH(n) == 4) {
4000 expr_ty expression;
4001 asdl_seq *suite_seq;
4002
4003 expression = ast_for_expr(c, CHILD(n, 1));
4004 if (!expression)
4005 return NULL;
4006 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004007 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004009 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4010 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4011 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 }
4013 else if (NCH(n) == 7) {
4014 expr_ty expression;
4015 asdl_seq *seq1, *seq2;
4016
4017 expression = ast_for_expr(c, CHILD(n, 1));
4018 if (!expression)
4019 return NULL;
4020 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004021 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 return NULL;
4023 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004024 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004026 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004028 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4029 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004031
4032 PyErr_Format(PyExc_SystemError,
4033 "wrong number of tokens for 'while' statement: %d",
4034 NCH(n));
4035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036}
4037
4038static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004039ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040{
guoci90fc8982018-09-11 17:45:45 -04004041 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004042 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004044 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004045 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004046 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004047 int has_type_comment;
4048 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004049
4050 if (is_async && c->c_feature_version < 5) {
4051 ast_error(c, n,
4052 "Async for loops are only supported in Python 3.5 and greater");
4053 return NULL;
4054 }
4055
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004056 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 REQ(n, for_stmt);
4058
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004059 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4060
4061 if (NCH(n) == 9 + has_type_comment) {
4062 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 if (!seq)
4064 return NULL;
4065 }
4066
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004067 node_target = CHILD(n, 1);
4068 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004069 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004071 /* Check the # of children rather than the length of _target, since
4072 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004073 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004074 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004075 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004077 target = Tuple(_target, Store, first->lineno, first->col_offset,
4078 node_target->n_end_lineno, node_target->n_end_col_offset,
4079 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004081 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004082 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004084 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004085 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086 return NULL;
4087
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004088 if (seq != NULL) {
4089 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4090 } else {
4091 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4092 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004093
4094 if (has_type_comment) {
4095 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4096 if (!type_comment)
4097 return NULL;
4098 }
4099 else
4100 type_comment = NULL;
4101
Yury Selivanov75445082015-05-11 22:57:16 -04004102 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004103 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004104 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004105 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004106 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004107 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004108 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004109 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110}
4111
4112static excepthandler_ty
4113ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4114{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004115 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004116 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117 REQ(exc, except_clause);
4118 REQ(body, suite);
4119
4120 if (NCH(exc) == 1) {
4121 asdl_seq *suite_seq = ast_for_suite(c, body);
4122 if (!suite_seq)
4123 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004124 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125
Neal Norwitzad74aa82008-03-31 05:14:30 +00004126 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004127 exc->n_col_offset,
4128 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004129 }
4130 else if (NCH(exc) == 2) {
4131 expr_ty expression;
4132 asdl_seq *suite_seq;
4133
4134 expression = ast_for_expr(c, CHILD(exc, 1));
4135 if (!expression)
4136 return NULL;
4137 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004138 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004140 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141
Neal Norwitzad74aa82008-03-31 05:14:30 +00004142 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004143 exc->n_col_offset,
4144 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145 }
4146 else if (NCH(exc) == 4) {
4147 asdl_seq *suite_seq;
4148 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004149 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004150 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004152 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004155 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156 return NULL;
4157 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004158 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161
Neal Norwitzad74aa82008-03-31 05:14:30 +00004162 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004163 exc->n_col_offset,
4164 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004165 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004166
4167 PyErr_Format(PyExc_SystemError,
4168 "wrong number of children for 'except' clause: %d",
4169 NCH(exc));
4170 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171}
4172
4173static stmt_ty
4174ast_for_try_stmt(struct compiling *c, const node *n)
4175{
Neal Norwitzf599f422005-12-17 21:33:47 +00004176 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004177 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004178 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004179 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 REQ(n, try_stmt);
4182
Neal Norwitzf599f422005-12-17 21:33:47 +00004183 body = ast_for_suite(c, CHILD(n, 2));
4184 if (body == NULL)
4185 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186
Neal Norwitzf599f422005-12-17 21:33:47 +00004187 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4188 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4189 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4190 /* we can assume it's an "else",
4191 because nch >= 9 for try-else-finally and
4192 it would otherwise have a type of except_clause */
4193 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4194 if (orelse == NULL)
4195 return NULL;
4196 n_except--;
4197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198
Neal Norwitzf599f422005-12-17 21:33:47 +00004199 finally = ast_for_suite(c, CHILD(n, nch - 1));
4200 if (finally == NULL)
4201 return NULL;
4202 n_except--;
4203 }
4204 else {
4205 /* we can assume it's an "else",
4206 otherwise it would have a type of except_clause */
4207 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4208 if (orelse == NULL)
4209 return NULL;
4210 n_except--;
4211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004212 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004213 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004214 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004215 return NULL;
4216 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217
Neal Norwitzf599f422005-12-17 21:33:47 +00004218 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004219 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004220 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004221 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004222 if (handlers == NULL)
4223 return NULL;
4224
4225 for (i = 0; i < n_except; i++) {
4226 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4227 CHILD(n, 5 + i * 3));
4228 if (!e)
4229 return NULL;
4230 asdl_seq_SET(handlers, i, e);
4231 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004232 }
4233
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004234 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004235 if (finally != NULL) {
4236 // finally is always last
4237 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4238 } else if (orelse != NULL) {
4239 // otherwise else is last
4240 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4241 } else {
4242 // inline the get_last_end_pos logic due to layout mismatch
4243 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4244 end_lineno = last_handler->end_lineno;
4245 end_col_offset = last_handler->end_col_offset;
4246 }
4247 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4248 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249}
4250
Georg Brandl0c315622009-05-25 21:10:36 +00004251/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004252static withitem_ty
4253ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004254{
4255 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004256
Georg Brandl0c315622009-05-25 21:10:36 +00004257 REQ(n, with_item);
4258 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004259 if (!context_expr)
4260 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004261 if (NCH(n) == 3) {
4262 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004263
4264 if (!optional_vars) {
4265 return NULL;
4266 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004267 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004268 return NULL;
4269 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004270 }
4271
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004272 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004273}
4274
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004275/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004276static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004277ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004278{
guoci90fc8982018-09-11 17:45:45 -04004279 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004280 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004281 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004282 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004283
Guido van Rossum495da292019-03-07 12:38:08 -08004284 if (is_async && c->c_feature_version < 5) {
4285 ast_error(c, n,
4286 "Async with statements are only supported in Python 3.5 and greater");
4287 return NULL;
4288 }
4289
Georg Brandl0c315622009-05-25 21:10:36 +00004290 REQ(n, with_stmt);
4291
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004292 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4293 nch_minus_type = NCH(n) - has_type_comment;
4294
4295 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004296 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004297 if (!items)
4298 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004299 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004300 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4301 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004302 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004303 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004304 }
4305
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004306 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4307 if (!body)
4308 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004309 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004310
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004311 if (has_type_comment) {
4312 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4313 if (!type_comment)
4314 return NULL;
4315 }
4316 else
4317 type_comment = NULL;
4318
Yury Selivanov75445082015-05-11 22:57:16 -04004319 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004320 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004321 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004322 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004323 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004324 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004325}
4326
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004327static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004328ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004329{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004330 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004331 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004332 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004333 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004334 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004336 REQ(n, classdef);
4337
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004338 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004339 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004340 if (!s)
4341 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004342 get_last_end_pos(s, &end_lineno, &end_col_offset);
4343
Benjamin Peterson30760062008-11-25 04:02:28 +00004344 classname = NEW_IDENTIFIER(CHILD(n, 1));
4345 if (!classname)
4346 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004347 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004348 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004349 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004350 LINENO(n), n->n_col_offset,
4351 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004352 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004353
4354 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004355 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004356 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004357 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004358 get_last_end_pos(s, &end_lineno, &end_col_offset);
4359
Benjamin Peterson30760062008-11-25 04:02:28 +00004360 classname = NEW_IDENTIFIER(CHILD(n, 1));
4361 if (!classname)
4362 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004363 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004364 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004365 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004366 LINENO(n), n->n_col_offset,
4367 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004368 }
4369
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004370 /* class NAME '(' arglist ')' ':' suite */
4371 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004372 {
4373 PyObject *dummy_name;
4374 expr_ty dummy;
4375 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4376 if (!dummy_name)
4377 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004378 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4379 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4380 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004381 call = ast_for_call(c, CHILD(n, 3), dummy,
4382 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004383 if (!call)
4384 return NULL;
4385 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004386 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004387 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004388 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004389 get_last_end_pos(s, &end_lineno, &end_col_offset);
4390
Benjamin Peterson30760062008-11-25 04:02:28 +00004391 classname = NEW_IDENTIFIER(CHILD(n, 1));
4392 if (!classname)
4393 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004394 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004395 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004396
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004397 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004398 decorator_seq, LINENO(n), n->n_col_offset,
4399 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004400}
4401
4402static stmt_ty
4403ast_for_stmt(struct compiling *c, const node *n)
4404{
4405 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004406 assert(NCH(n) == 1);
4407 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004408 }
4409 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004410 assert(num_stmts(n) == 1);
4411 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004412 }
4413 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004414 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004415 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4416 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004417 */
4418 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004419 case expr_stmt:
4420 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421 case del_stmt:
4422 return ast_for_del_stmt(c, n);
4423 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004424 return Pass(LINENO(n), n->n_col_offset,
4425 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004426 case flow_stmt:
4427 return ast_for_flow_stmt(c, n);
4428 case import_stmt:
4429 return ast_for_import_stmt(c, n);
4430 case global_stmt:
4431 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004432 case nonlocal_stmt:
4433 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004434 case assert_stmt:
4435 return ast_for_assert_stmt(c, n);
4436 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004437 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004438 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4439 TYPE(n), NCH(n));
4440 return NULL;
4441 }
4442 }
4443 else {
4444 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004445 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004446 */
4447 node *ch = CHILD(n, 0);
4448 REQ(n, compound_stmt);
4449 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004450 case if_stmt:
4451 return ast_for_if_stmt(c, ch);
4452 case while_stmt:
4453 return ast_for_while_stmt(c, ch);
4454 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004455 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456 case try_stmt:
4457 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004458 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004459 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004461 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004463 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 case decorated:
4465 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004466 case async_stmt:
4467 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004468 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004469 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004470 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004471 TYPE(n), NCH(n));
4472 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004474 }
4475}
4476
4477static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004478parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004479{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004480 const char *end;
4481 long x;
4482 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004483 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004484 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004485
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004486 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004487 errno = 0;
4488 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004489 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004490 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004491 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004492 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004493 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004494 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004495 }
4496 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004497 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004498 if (*end == '\0') {
4499 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004500 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004501 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004502 }
4503 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004504 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004505 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004506 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4507 if (compl.imag == -1.0 && PyErr_Occurred())
4508 return NULL;
4509 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004510 }
4511 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004512 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004513 dx = PyOS_string_to_double(s, NULL, NULL);
4514 if (dx == -1.0 && PyErr_Occurred())
4515 return NULL;
4516 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004518}
4519
4520static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004521parsenumber(struct compiling *c, const char *s)
4522{
4523 char *dup, *end;
4524 PyObject *res = NULL;
4525
4526 assert(s != NULL);
4527
4528 if (strchr(s, '_') == NULL) {
4529 return parsenumber_raw(c, s);
4530 }
4531 /* Create a duplicate without underscores. */
4532 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004533 if (dup == NULL) {
4534 return PyErr_NoMemory();
4535 }
Brett Cannona721aba2016-09-09 14:57:09 -07004536 end = dup;
4537 for (; *s; s++) {
4538 if (*s != '_') {
4539 *end++ = *s;
4540 }
4541 }
4542 *end = '\0';
4543 res = parsenumber_raw(c, dup);
4544 PyMem_Free(dup);
4545 return res;
4546}
4547
4548static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004549decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004550{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004551 const char *s, *t;
4552 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004553 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4554 while (s < end && (*s & 0x80)) s++;
4555 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004556 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004557}
4558
Eric V. Smith56466482016-10-31 14:46:26 -04004559static int
4560warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004561 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004562{
4563 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4564 first_invalid_escape_char);
4565 if (msg == NULL) {
4566 return -1;
4567 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004568 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004569 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004570 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004571 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004572 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4573 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004574 to get a more accurate error report */
4575 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004576 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004577 }
4578 Py_DECREF(msg);
4579 return -1;
4580 }
4581 Py_DECREF(msg);
4582 return 0;
4583}
4584
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004585static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004586decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4587 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004588{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004589 PyObject *v, *u;
4590 char *buf;
4591 char *p;
4592 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004593
Benjamin Peterson202803a2016-02-25 22:34:45 -08004594 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004595 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004596 return NULL;
4597 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4598 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4599 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4600 if (u == NULL)
4601 return NULL;
4602 p = buf = PyBytes_AsString(u);
4603 end = s + len;
4604 while (s < end) {
4605 if (*s == '\\') {
4606 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004607 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004608 strcpy(p, "u005c");
4609 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004610 if (s >= end)
4611 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004612 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004613 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004614 if (*s & 0x80) { /* XXX inefficient */
4615 PyObject *w;
4616 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03004617 const void *data;
Benjamin Peterson202803a2016-02-25 22:34:45 -08004618 Py_ssize_t len, i;
4619 w = decode_utf8(c, &s, end);
4620 if (w == NULL) {
4621 Py_DECREF(u);
4622 return NULL;
4623 }
4624 kind = PyUnicode_KIND(w);
4625 data = PyUnicode_DATA(w);
4626 len = PyUnicode_GET_LENGTH(w);
4627 for (i = 0; i < len; i++) {
4628 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4629 sprintf(p, "\\U%08x", chr);
4630 p += 10;
4631 }
4632 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004633 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004634 Py_DECREF(w);
4635 } else {
4636 *p++ = *s++;
4637 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004638 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004639 len = p - buf;
4640 s = buf;
4641
Eric V. Smith56466482016-10-31 14:46:26 -04004642 const char *first_invalid_escape;
4643 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4644
4645 if (v != NULL && first_invalid_escape != NULL) {
4646 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4647 /* We have not decref u before because first_invalid_escape points
4648 inside u. */
4649 Py_XDECREF(u);
4650 Py_DECREF(v);
4651 return NULL;
4652 }
4653 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004654 Py_XDECREF(u);
4655 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004656}
4657
Eric V. Smith56466482016-10-31 14:46:26 -04004658static PyObject *
4659decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4660 size_t len)
4661{
4662 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004663 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004664 &first_invalid_escape);
4665 if (result == NULL)
4666 return NULL;
4667
4668 if (first_invalid_escape != NULL) {
4669 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4670 Py_DECREF(result);
4671 return NULL;
4672 }
4673 }
4674 return result;
4675}
4676
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004677/* Shift locations for the given node and all its children by adding `lineno`
4678 and `col_offset` to existing locations. */
4679static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4680{
4681 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004682 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004683 for (int i = 0; i < NCH(n); ++i) {
4684 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4685 /* Shifting column offsets unnecessary if there's been newlines. */
4686 col_offset = 0;
4687 }
4688 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4689 }
4690 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004691 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004692}
4693
4694/* Fix locations for the given node and its children.
4695
4696 `parent` is the enclosing node.
4697 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004698 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004699*/
4700static void
4701fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4702{
4703 char *substr = NULL;
4704 char *start;
4705 int lines = LINENO(parent) - 1;
4706 int cols = parent->n_col_offset;
4707 /* Find the full fstring to fix location information in `n`. */
4708 while (parent && parent->n_type != STRING)
4709 parent = parent->n_child;
4710 if (parent && parent->n_str) {
4711 substr = strstr(parent->n_str, expr_str);
4712 if (substr) {
4713 start = substr;
4714 while (start > parent->n_str) {
4715 if (start[0] == '\n')
4716 break;
4717 start--;
4718 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004719 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004720 /* adjust the start based on the number of newlines encountered
4721 before the f-string expression */
4722 for (char* p = parent->n_str; p < substr; p++) {
4723 if (*p == '\n') {
4724 lines++;
4725 }
4726 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004727 }
4728 }
4729 fstring_shift_node_locations(n, lines, cols);
4730}
4731
Eric V. Smith451d0e32016-09-09 21:56:20 -04004732/* Compile this expression in to an expr_ty. Add parens around the
4733 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004735fstring_compile_expr(const char *expr_start, const char *expr_end,
4736 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004737
Eric V. Smith235a6f02015-09-19 14:51:32 -04004738{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004739 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004740 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004742 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004743 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004744
Eric V. Smith1d44c412015-09-23 07:49:00 -04004745 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004746 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004747 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4748 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004749
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004750 /* If the substring is all whitespace, it's an error. We need to catch this
4751 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4752 because turning the expression '' in to '()' would go from being invalid
4753 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004754 for (s = expr_start; s != expr_end; s++) {
4755 char c = *s;
4756 /* The Python parser ignores only the following whitespace
4757 characters (\r already is converted to \n). */
4758 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004759 break;
4760 }
4761 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004762 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004763 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004764 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004765 }
4766
Eric V. Smith451d0e32016-09-09 21:56:20 -04004767 len = expr_end - expr_start;
4768 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03004769 str = PyMem_Malloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004770 if (str == NULL) {
4771 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004772 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004773 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004774
Eric V. Smith451d0e32016-09-09 21:56:20 -04004775 str[0] = '(';
4776 memcpy(str+1, expr_start, len);
4777 str[len+1] = ')';
4778 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004779
Victor Stinner37d66d72019-06-13 02:16:41 +02004780 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004781 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004782 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4783 Py_eval_input, 0);
4784 if (!mod_n) {
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03004785 PyMem_Free(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004786 return NULL;
4787 }
4788 /* Reuse str to find the correct column offset. */
4789 str[0] = '{';
4790 str[len+1] = '}';
4791 fstring_fix_node_location(n, mod_n, str);
4792 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03004793 PyMem_Free(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004794 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004795 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004796 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004797 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004798}
4799
4800/* Return -1 on error.
4801
4802 Return 0 if we reached the end of the literal.
4803
4804 Return 1 if we haven't reached the end of the literal, but we want
4805 the caller to process the literal up to this point. Used for
4806 doubled braces.
4807*/
4808static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004809fstring_find_literal(const char **str, const char *end, int raw,
4810 PyObject **literal, int recurse_lvl,
4811 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004812{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004813 /* Get any literal string. It ends when we hit an un-doubled left
4814 brace (which isn't part of a unicode name escape such as
4815 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004816
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004817 const char *s = *str;
4818 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004819 int result = 0;
4820
Eric V. Smith235a6f02015-09-19 14:51:32 -04004821 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004822 while (s < end) {
4823 char ch = *s++;
4824 if (!raw && ch == '\\' && s < end) {
4825 ch = *s++;
4826 if (ch == 'N') {
4827 if (s < end && *s++ == '{') {
4828 while (s < end && *s++ != '}') {
4829 }
4830 continue;
4831 }
4832 break;
4833 }
4834 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4835 return -1;
4836 }
4837 }
4838 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004839 /* Check for doubled braces, but only at the top level. If
4840 we checked at every level, then f'{0:{3}}' would fail
4841 with the two closing braces. */
4842 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004843 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004844 /* We're going to tell the caller that the literal ends
4845 here, but that they should continue scanning. But also
4846 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004847 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004848 result = 1;
4849 goto done;
4850 }
4851
4852 /* Where a single '{' is the start of a new expression, a
4853 single '}' is not allowed. */
4854 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004855 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856 ast_error(c, n, "f-string: single '}' is not allowed");
4857 return -1;
4858 }
4859 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004860 /* We're either at a '{', which means we're starting another
4861 expression; or a '}', which means we're at the end of this
4862 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004863 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864 break;
4865 }
4866 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004867 *str = s;
4868 assert(s <= end);
4869 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004870done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004871 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004872 if (raw)
4873 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004874 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004875 NULL, NULL);
4876 else
Eric V. Smith56466482016-10-31 14:46:26 -04004877 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004878 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879 if (!*literal)
4880 return -1;
4881 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004882 return result;
4883}
4884
4885/* Forward declaration because parsing is recursive. */
4886static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004887fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 struct compiling *c, const node *n);
4889
Eric V. Smith451d0e32016-09-09 21:56:20 -04004890/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004891 expression (so it must be a '{'). Returns the FormattedValue node, which
4892 includes the expression, conversion character, format_spec expression, and
4893 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004894
4895 Note that I don't do a perfect job here: I don't make sure that a
4896 closing brace doesn't match an opening paren, for example. It
4897 doesn't need to error on all invalid expressions, just correctly
4898 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004899 will be caught when we parse it later.
4900
4901 *expression is set to the expression. For an '=' "debug" expression,
4902 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004903 including the '=' and any whitespace around it, as a string object). If
4904 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004906fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004907 PyObject **expr_text, expr_ty *expression,
4908 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004909{
4910 /* Return -1 on error, else 0. */
4911
Eric V. Smith451d0e32016-09-09 21:56:20 -04004912 const char *expr_start;
4913 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914 expr_ty simple_expression;
4915 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004916 int conversion = -1; /* The conversion char. Use default if not
4917 specified, or !r if using = and no format
4918 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919
4920 /* 0 if we're not in a string, else the quote char we're trying to
4921 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923
4924 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4925 int string_type = 0;
4926
4927 /* Keep track of nesting level for braces/parens/brackets in
4928 expressions. */
4929 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004930 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004932 *expr_text = NULL;
4933
Eric V. Smith235a6f02015-09-19 14:51:32 -04004934 /* Can only nest one level deep. */
4935 if (recurse_lvl >= 2) {
4936 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004937 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004938 }
4939
4940 /* The first char must be a left brace, or we wouldn't have gotten
4941 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004942 assert(**str == '{');
4943 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944
Eric V. Smith451d0e32016-09-09 21:56:20 -04004945 expr_start = *str;
4946 for (; *str < end; (*str)++) {
4947 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948
4949 /* Loop invariants. */
4950 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004951 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004952 if (quote_char)
4953 assert(string_type == 1 || string_type == 3);
4954 else
4955 assert(string_type == 0);
4956
Eric V. Smith451d0e32016-09-09 21:56:20 -04004957 ch = **str;
4958 /* Nowhere inside an expression is a backslash allowed. */
4959 if (ch == '\\') {
4960 /* Error: can't include a backslash character, inside
4961 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004962 ast_error(c, n,
4963 "f-string expression part "
4964 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004965 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004966 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967 if (quote_char) {
4968 /* We're inside a string. See if we're at the end. */
4969 /* This code needs to implement the same non-error logic
4970 as tok_get from tokenizer.c, at the letter_quote
4971 label. To actually share that code would be a
4972 nightmare. But, it's unlikely to change and is small,
4973 so duplicate it here. Note we don't need to catch all
4974 of the errors, since they'll be caught when parsing the
4975 expression. We just need to match the non-error
4976 cases. Thus we can ignore \n in single-quoted strings,
4977 for example. Or non-terminated strings. */
4978 if (ch == quote_char) {
4979 /* Does this match the string_type (single or triple
4980 quoted)? */
4981 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004982 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004983 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004984 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004985 string_type = 0;
4986 quote_char = 0;
4987 continue;
4988 }
4989 } else {
4990 /* We're at the end of a normal string. */
4991 quote_char = 0;
4992 string_type = 0;
4993 continue;
4994 }
4995 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 } else if (ch == '\'' || ch == '"') {
4997 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004998 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005000 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 } else {
5002 /* Start of a normal string. */
5003 string_type = 1;
5004 }
5005 /* Start looking for the end of the string. */
5006 quote_char = ch;
5007 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005008 if (nested_depth >= MAXLEVEL) {
5009 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005010 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005011 }
5012 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014 } else if (ch == '#') {
5015 /* Error: can't include a comment character, inside parens
5016 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005017 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005018 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005020 (ch == '!' || ch == ':' || ch == '}' ||
5021 ch == '=' || ch == '>' || ch == '<')) {
5022 /* See if there's a next character. */
5023 if (*str+1 < end) {
5024 char next = *(*str+1);
5025
5026 /* For "!=". since '=' is not an allowed conversion character,
5027 nothing is lost in this test. */
5028 if ((ch == '!' && next == '=') || /* != */
5029 (ch == '=' && next == '=') || /* == */
5030 (ch == '<' && next == '=') || /* <= */
5031 (ch == '>' && next == '=') /* >= */
5032 ) {
5033 *str += 1;
5034 continue;
5035 }
5036 /* Don't get out of the loop for these, if they're single
5037 chars (not part of 2-char tokens). If by themselves, they
5038 don't end an expression (unlike say '!'). */
5039 if (ch == '>' || ch == '<') {
5040 continue;
5041 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005042 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005043
Eric V. Smith235a6f02015-09-19 14:51:32 -04005044 /* Normal way out of this loop. */
5045 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005046 } else if (ch == ']' || ch == '}' || ch == ')') {
5047 if (!nested_depth) {
5048 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005049 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005050 }
5051 nested_depth--;
5052 int opening = parenstack[nested_depth];
5053 if (!((opening == '(' && ch == ')') ||
5054 (opening == '[' && ch == ']') ||
5055 (opening == '{' && ch == '}')))
5056 {
5057 ast_error(c, n,
5058 "f-string: closing parenthesis '%c' "
5059 "does not match opening parenthesis '%c'",
5060 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005061 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005062 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 } else {
5064 /* Just consume this char and loop around. */
5065 }
5066 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005067 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005068 /* If we leave this loop in a string or with mismatched parens, we
5069 don't care. We'll get a syntax error when compiling the
5070 expression. But, we can produce a better error message, so
5071 let's just do that.*/
5072 if (quote_char) {
5073 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005074 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075 }
5076 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005077 int opening = parenstack[nested_depth - 1];
5078 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005079 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005080 }
5081
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005083 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005084
5085 /* Compile the expression as soon as possible, so we show errors
5086 related to the expression before errors related to the
5087 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005088 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005089 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005090 goto error;
5091
5092 /* Check for =, which puts the text value of the expression in
5093 expr_text. */
5094 if (**str == '=') {
Pablo Galindo9b838292020-05-27 22:01:11 +01005095 if (c->c_feature_version < 8) {
5096 ast_error(c, n,
5097 "f-string: self documenting expressions are "
5098 "only supported in Python 3.8 and greater");
5099 goto error;
5100 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005101 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005102
5103 /* Skip over ASCII whitespace. No need to test for end of string
5104 here, since we know there's at least a trailing quote somewhere
5105 ahead. */
5106 while (Py_ISSPACE(**str)) {
5107 *str += 1;
5108 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005109
5110 /* Set *expr_text to the text of the expression. */
5111 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5112 if (!*expr_text) {
5113 goto error;
5114 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005115 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005116
5117 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005118 if (**str == '!') {
5119 *str += 1;
5120 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005121 goto unexpected_end_of_string;
5122
Eric V. Smith451d0e32016-09-09 21:56:20 -04005123 conversion = **str;
5124 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125
5126 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005127 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005128 ast_error(c, n,
5129 "f-string: invalid conversion character: "
5130 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005131 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005133
5134 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005135
5136 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005137 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005139 if (**str == ':') {
5140 *str += 1;
5141 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005142 goto unexpected_end_of_string;
5143
5144 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005145 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005146 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005147 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005148 }
5149
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151 goto unexpected_end_of_string;
5152
5153 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005154 assert(*str < end);
5155 assert(**str == '}');
5156 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005157
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005158 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005159 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005160 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005161 conversion = 'r';
5162 }
5163
Eric V. Smith451d0e32016-09-09 21:56:20 -04005164 /* And now create the FormattedValue node that represents this
5165 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005166 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005167 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005168 n->n_col_offset, n->n_end_lineno,
5169 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005170 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005171 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005172
5173 return 0;
5174
5175unexpected_end_of_string:
5176 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005177 /* Falls through to error. */
5178
5179error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005180 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005181 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005182
Eric V. Smith235a6f02015-09-19 14:51:32 -04005183}
5184
5185/* Return -1 on error.
5186
5187 Return 0 if we have a literal (possible zero length) and an
5188 expression (zero length if at the end of the string.
5189
5190 Return 1 if we have a literal, but no expression, and we want the
5191 caller to call us again. This is used to deal with doubled
5192 braces.
5193
5194 When called multiple times on the string 'a{{b{0}c', this function
5195 will return:
5196
5197 1. the literal 'a{' with no expression, and a return value
5198 of 1. Despite the fact that there's no expression, the return
5199 value of 1 means we're not finished yet.
5200
5201 2. the literal 'b' and the expression '0', with a return value of
5202 0. The fact that there's an expression means we're not finished.
5203
5204 3. literal 'c' with no expression and a return value of 0. The
5205 combination of the return value of 0 with no expression means
5206 we're finished.
5207*/
5208static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005209fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5210 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005211 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 struct compiling *c, const node *n)
5213{
5214 int result;
5215
5216 assert(*literal == NULL && *expression == NULL);
5217
5218 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005219 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005220 if (result < 0)
5221 goto error;
5222
5223 assert(result == 0 || result == 1);
5224
5225 if (result == 1)
5226 /* We have a literal, but don't look at the expression. */
5227 return 1;
5228
Eric V. Smith451d0e32016-09-09 21:56:20 -04005229 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005230 /* We're at the end of the string or the end of a nested
5231 f-string: no expression. The top-level error case where we
5232 expect to be at the end of the string but we're at a '}' is
5233 handled later. */
5234 return 0;
5235
5236 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005237 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005238
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005239 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5240 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005241 goto error;
5242
5243 return 0;
5244
5245error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005246 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005247 return -1;
5248}
5249
5250#define EXPRLIST_N_CACHED 64
5251
5252typedef struct {
5253 /* Incrementally build an array of expr_ty, so be used in an
5254 asdl_seq. Cache some small but reasonably sized number of
5255 expr_ty's, and then after that start dynamically allocating,
5256 doubling the number allocated each time. Note that the f-string
5257 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005258 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005259 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005260
5261 Py_ssize_t allocated; /* Number we've allocated. */
5262 Py_ssize_t size; /* Number we've used. */
5263 expr_ty *p; /* Pointer to the memory we're actually
5264 using. Will point to 'data' until we
5265 start dynamically allocating. */
5266 expr_ty data[EXPRLIST_N_CACHED];
5267} ExprList;
5268
5269#ifdef NDEBUG
5270#define ExprList_check_invariants(l)
5271#else
5272static void
5273ExprList_check_invariants(ExprList *l)
5274{
5275 /* Check our invariants. Make sure this object is "live", and
5276 hasn't been deallocated. */
5277 assert(l->size >= 0);
5278 assert(l->p != NULL);
5279 if (l->size <= EXPRLIST_N_CACHED)
5280 assert(l->data == l->p);
5281}
5282#endif
5283
5284static void
5285ExprList_Init(ExprList *l)
5286{
5287 l->allocated = EXPRLIST_N_CACHED;
5288 l->size = 0;
5289
5290 /* Until we start allocating dynamically, p points to data. */
5291 l->p = l->data;
5292
5293 ExprList_check_invariants(l);
5294}
5295
5296static int
5297ExprList_Append(ExprList *l, expr_ty exp)
5298{
5299 ExprList_check_invariants(l);
5300 if (l->size >= l->allocated) {
5301 /* We need to alloc (or realloc) the memory. */
5302 Py_ssize_t new_size = l->allocated * 2;
5303
5304 /* See if we've ever allocated anything dynamically. */
5305 if (l->p == l->data) {
5306 Py_ssize_t i;
5307 /* We're still using the cached data. Switch to
5308 alloc-ing. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005309 l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005310 if (!l->p)
5311 return -1;
5312 /* Copy the cached data into the new buffer. */
5313 for (i = 0; i < l->size; i++)
5314 l->p[i] = l->data[i];
5315 } else {
5316 /* Just realloc. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005317 expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005318 if (!tmp) {
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005319 PyMem_Free(l->p);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005320 l->p = NULL;
5321 return -1;
5322 }
5323 l->p = tmp;
5324 }
5325
5326 l->allocated = new_size;
5327 assert(l->allocated == 2 * l->size);
5328 }
5329
5330 l->p[l->size++] = exp;
5331
5332 ExprList_check_invariants(l);
5333 return 0;
5334}
5335
5336static void
5337ExprList_Dealloc(ExprList *l)
5338{
5339 ExprList_check_invariants(l);
5340
5341 /* If there's been an error, or we've never dynamically allocated,
5342 do nothing. */
5343 if (!l->p || l->p == l->data) {
5344 /* Do nothing. */
5345 } else {
5346 /* We have dynamically allocated. Free the memory. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005347 PyMem_Free(l->p);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005348 }
5349 l->p = NULL;
5350 l->size = -1;
5351}
5352
5353static asdl_seq *
5354ExprList_Finish(ExprList *l, PyArena *arena)
5355{
5356 asdl_seq *seq;
5357
5358 ExprList_check_invariants(l);
5359
5360 /* Allocate the asdl_seq and copy the expressions in to it. */
5361 seq = _Py_asdl_seq_new(l->size, arena);
5362 if (seq) {
5363 Py_ssize_t i;
5364 for (i = 0; i < l->size; i++)
5365 asdl_seq_SET(seq, i, l->p[i]);
5366 }
5367 ExprList_Dealloc(l);
5368 return seq;
5369}
5370
5371/* The FstringParser is designed to add a mix of strings and
5372 f-strings, and concat them together as needed. Ultimately, it
5373 generates an expr_ty. */
5374typedef struct {
5375 PyObject *last_str;
5376 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005377 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005378} FstringParser;
5379
5380#ifdef NDEBUG
5381#define FstringParser_check_invariants(state)
5382#else
5383static void
5384FstringParser_check_invariants(FstringParser *state)
5385{
5386 if (state->last_str)
5387 assert(PyUnicode_CheckExact(state->last_str));
5388 ExprList_check_invariants(&state->expr_list);
5389}
5390#endif
5391
5392static void
5393FstringParser_Init(FstringParser *state)
5394{
5395 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005396 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005397 ExprList_Init(&state->expr_list);
5398 FstringParser_check_invariants(state);
5399}
5400
5401static void
5402FstringParser_Dealloc(FstringParser *state)
5403{
5404 FstringParser_check_invariants(state);
5405
5406 Py_XDECREF(state->last_str);
5407 ExprList_Dealloc(&state->expr_list);
5408}
5409
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005410/* Constants for the following */
5411static PyObject *u_kind;
5412
5413/* Compute 'kind' field for string Constant (either 'u' or None) */
5414static PyObject *
5415make_kind(struct compiling *c, const node *n)
5416{
5417 char *s = NULL;
5418 PyObject *kind = NULL;
5419
5420 /* Find the first string literal, if any */
5421 while (TYPE(n) != STRING) {
5422 if (NCH(n) == 0)
5423 return NULL;
5424 n = CHILD(n, 0);
5425 }
5426 REQ(n, STRING);
5427
5428 /* If it starts with 'u', return a PyUnicode "u" string */
5429 s = STR(n);
5430 if (s && *s == 'u') {
5431 if (!u_kind) {
5432 u_kind = PyUnicode_InternFromString("u");
5433 if (!u_kind)
5434 return NULL;
5435 }
5436 kind = u_kind;
5437 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5438 return NULL;
5439 }
5440 Py_INCREF(kind);
5441 }
5442 return kind;
5443}
5444
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005445/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005446static expr_ty
5447make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5448{
5449 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005450 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005451 *str = NULL;
5452 assert(PyUnicode_CheckExact(s));
5453 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5454 Py_DECREF(s);
5455 return NULL;
5456 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005457 kind = make_kind(c, n);
5458 if (kind == NULL && PyErr_Occurred())
5459 return NULL;
5460 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005461 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005462}
5463
5464/* Add a non-f-string (that is, a regular literal string). str is
5465 decref'd. */
5466static int
5467FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5468{
5469 FstringParser_check_invariants(state);
5470
5471 assert(PyUnicode_CheckExact(str));
5472
5473 if (PyUnicode_GET_LENGTH(str) == 0) {
5474 Py_DECREF(str);
5475 return 0;
5476 }
5477
5478 if (!state->last_str) {
5479 /* We didn't have a string before, so just remember this one. */
5480 state->last_str = str;
5481 } else {
5482 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005483 PyUnicode_AppendAndDel(&state->last_str, str);
5484 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005485 return -1;
5486 }
5487 FstringParser_check_invariants(state);
5488 return 0;
5489}
5490
Eric V. Smith451d0e32016-09-09 21:56:20 -04005491/* Parse an f-string. The f-string is in *str to end, with no
5492 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005493static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005494FstringParser_ConcatFstring(FstringParser *state, const char **str,
5495 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005496 struct compiling *c, const node *n)
5497{
5498 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005499 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005500
5501 /* Parse the f-string. */
5502 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005503 PyObject *literal = NULL;
5504 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005505 expr_ty expression = NULL;
5506
5507 /* If there's a zero length literal in front of the
5508 expression, literal will be NULL. If we're at the end of
5509 the f-string, expression will be NULL (unless result == 1,
5510 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005511 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005512 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005513 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005514 if (result < 0)
5515 return -1;
5516
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005517 /* Add the literal, if any. */
5518 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5519 Py_XDECREF(expr_text);
5520 return -1;
5521 }
5522 /* Add the expr_text, if any. */
5523 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5524 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005525 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005526
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005527 /* We've dealt with the literal and expr_text, their ownership has
5528 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005529
5530 /* See if we should just loop around to get the next literal
5531 and expression, while ignoring the expression this
5532 time. This is used for un-doubling braces, as an
5533 optimization. */
5534 if (result == 1)
5535 continue;
5536
5537 if (!expression)
5538 /* We're done with this f-string. */
5539 break;
5540
5541 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005542 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005543 if (!state->last_str) {
5544 /* Do nothing. No previous literal. */
5545 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005546 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005547 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5548 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5549 return -1;
5550 }
5551
5552 if (ExprList_Append(&state->expr_list, expression) < 0)
5553 return -1;
5554 }
5555
Eric V. Smith235a6f02015-09-19 14:51:32 -04005556 /* If recurse_lvl is zero, then we must be at the end of the
5557 string. Otherwise, we must be at a right brace. */
5558
Eric V. Smith451d0e32016-09-09 21:56:20 -04005559 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005560 ast_error(c, n, "f-string: unexpected end of string");
5561 return -1;
5562 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005563 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005564 ast_error(c, n, "f-string: expecting '}'");
5565 return -1;
5566 }
5567
5568 FstringParser_check_invariants(state);
5569 return 0;
5570}
5571
5572/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005573 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005574static expr_ty
5575FstringParser_Finish(FstringParser *state, struct compiling *c,
5576 const node *n)
5577{
5578 asdl_seq *seq;
5579
5580 FstringParser_check_invariants(state);
5581
5582 /* If we're just a constant string with no expressions, return
5583 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005584 if (!state->fmode) {
5585 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005586 if (!state->last_str) {
5587 /* Create a zero length string. */
5588 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5589 if (!state->last_str)
5590 goto error;
5591 }
5592 return make_str_node_and_del(&state->last_str, c, n);
5593 }
5594
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005595 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005596 last node in our expression list. */
5597 if (state->last_str) {
5598 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5599 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5600 goto error;
5601 }
5602 /* This has already been freed. */
5603 assert(state->last_str == NULL);
5604
5605 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5606 if (!seq)
5607 goto error;
5608
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005609 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5610 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005611
5612error:
5613 FstringParser_Dealloc(state);
5614 return NULL;
5615}
5616
Eric V. Smith451d0e32016-09-09 21:56:20 -04005617/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5618 at end, parse it into an expr_ty. Return NULL on error. Adjust
5619 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005620static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005621fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005622 struct compiling *c, const node *n)
5623{
5624 FstringParser state;
5625
5626 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005628 c, n) < 0) {
5629 FstringParser_Dealloc(&state);
5630 return NULL;
5631 }
5632
5633 return FstringParser_Finish(&state, c, n);
5634}
5635
5636/* n is a Python string literal, including the bracketing quote
5637 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005638 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005639 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005640 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5641 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005642*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005643static int
5644parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5645 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005646{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005647 size_t len;
5648 const char *s = STR(n);
5649 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005650 int fmode = 0;
5651 *bytesmode = 0;
5652 *rawmode = 0;
5653 *result = NULL;
5654 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005655 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005656 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005657 if (quote == 'b' || quote == 'B') {
5658 quote = *++s;
5659 *bytesmode = 1;
5660 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005661 else if (quote == 'u' || quote == 'U') {
5662 quote = *++s;
5663 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005664 else if (quote == 'r' || quote == 'R') {
5665 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005666 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005667 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005668 else if (quote == 'f' || quote == 'F') {
5669 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005670 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005671 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005672 else {
5673 break;
5674 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005675 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005676 }
Guido van Rossum495da292019-03-07 12:38:08 -08005677
5678 /* fstrings are only allowed in Python 3.6 and greater */
5679 if (fmode && c->c_feature_version < 6) {
5680 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5681 return -1;
5682 }
5683
Eric V. Smith451d0e32016-09-09 21:56:20 -04005684 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005685 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005686 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005687 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005688 if (quote != '\'' && quote != '\"') {
5689 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005690 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005691 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005692 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005693 s++;
5694 len = strlen(s);
5695 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005696 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005697 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005698 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005699 }
5700 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005701 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005702 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005703 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005704 }
5705 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005706 /* A triple quoted string. We've already skipped one quote at
5707 the start and one at the end of the string. Now skip the
5708 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005709 s += 2;
5710 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005711 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005712 if (s[--len] != quote || s[--len] != quote) {
5713 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005715 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005716 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005717
Eric V. Smith451d0e32016-09-09 21:56:20 -04005718 if (fmode) {
5719 /* Just return the bytes. The caller will parse the resulting
5720 string. */
5721 *fstr = s;
5722 *fstrlen = len;
5723 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005724 }
5725
Eric V. Smith451d0e32016-09-09 21:56:20 -04005726 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005727 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005728 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005729 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005730 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005731 const char *ch;
5732 for (ch = s; *ch; ch++) {
5733 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005734 ast_error(c, n,
5735 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005736 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005737 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005738 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005739 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005740 if (*rawmode)
5741 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005742 else
Eric V. Smith56466482016-10-31 14:46:26 -04005743 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005744 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005745 if (*rawmode)
5746 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005747 else
Eric V. Smith56466482016-10-31 14:46:26 -04005748 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005749 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005750 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005751}
5752
Eric V. Smith235a6f02015-09-19 14:51:32 -04005753/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5754 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005755 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005756 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005757 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005758 node if there's just an f-string (with no leading or trailing
5759 literals), or a JoinedStr node if there are multiple f-strings or
5760 any literals involved. */
5761static expr_ty
5762parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005763{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005764 int bytesmode = 0;
5765 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005766 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005767
5768 FstringParser state;
5769 FstringParser_Init(&state);
5770
5771 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005772 int this_bytesmode;
5773 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005774 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005775 const char *fstr;
5776 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005777
5778 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005779 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5780 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005781 goto error;
5782
5783 /* Check that we're not mixing bytes with unicode. */
5784 if (i != 0 && bytesmode != this_bytesmode) {
5785 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005786 /* s is NULL if the current string part is an f-string. */
5787 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005788 goto error;
5789 }
5790 bytesmode = this_bytesmode;
5791
Eric V. Smith451d0e32016-09-09 21:56:20 -04005792 if (fstr != NULL) {
5793 int result;
5794 assert(s == NULL && !bytesmode);
5795 /* This is an f-string. Parse and concatenate it. */
5796 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5797 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005798 if (result < 0)
5799 goto error;
5800 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005801 /* A string or byte string. */
5802 assert(s != NULL && fstr == NULL);
5803
Eric V. Smith451d0e32016-09-09 21:56:20 -04005804 assert(bytesmode ? PyBytes_CheckExact(s) :
5805 PyUnicode_CheckExact(s));
5806
Eric V. Smith451d0e32016-09-09 21:56:20 -04005807 if (bytesmode) {
5808 /* For bytes, concat as we go. */
5809 if (i == 0) {
5810 /* First time, just remember this value. */
5811 bytes_str = s;
5812 } else {
5813 PyBytes_ConcatAndDel(&bytes_str, s);
5814 if (!bytes_str)
5815 goto error;
5816 }
5817 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005818 /* This is a regular string. Concatenate it. */
5819 if (FstringParser_ConcatAndDel(&state, s) < 0)
5820 goto error;
5821 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005822 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005823 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005824 if (bytesmode) {
5825 /* Just return the bytes object and we're done. */
5826 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5827 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005828 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005829 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005831
Eric V. Smith235a6f02015-09-19 14:51:32 -04005832 /* We're not a bytes string, bytes_str should never have been set. */
5833 assert(bytes_str == NULL);
5834
5835 return FstringParser_Finish(&state, c, n);
5836
5837error:
5838 Py_XDECREF(bytes_str);
5839 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005840 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005841}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005842
5843PyObject *
5844_PyAST_GetDocString(asdl_seq *body)
5845{
5846 if (!asdl_seq_LEN(body)) {
5847 return NULL;
5848 }
5849 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5850 if (st->kind != Expr_kind) {
5851 return NULL;
5852 }
5853 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005854 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5855 return e->v.Constant.value;
5856 }
5857 return NULL;
5858}