blob: 327858610a58d22d02bda28e0aff75a60708b931 [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 Rossum77f0ed72019-05-28 16:44:58 -0700780 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800781
782 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Jeremy Hyltona8293132006-02-28 17:58:27 +0000785 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 switch (TYPE(n)) {
787 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200788 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500790 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 for (i = 0; i < NCH(n) - 1; i++) {
792 ch = CHILD(n, i);
793 if (TYPE(ch) == NEWLINE)
794 continue;
795 REQ(ch, stmt);
796 num = num_stmts(ch);
797 if (num == 1) {
798 s = ast_for_stmt(&c, ch);
799 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500800 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000801 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 }
803 else {
804 ch = CHILD(ch, 0);
805 REQ(ch, simple_stmt);
806 for (j = 0; j < num; j++) {
807 s = ast_for_stmt(&c, CHILD(ch, j * 2));
808 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000810 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 }
813 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800814
815 /* Type ignores are stored under the ENDMARKER in file_input. */
816 ch = CHILD(n, NCH(n) - 1);
817 REQ(ch, ENDMARKER);
818 num = NCH(ch);
819 type_ignores = _Py_asdl_seq_new(num, arena);
820 if (!type_ignores)
821 goto out;
822
823 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700824 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
825 if (!type_comment)
826 goto out;
827 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800828 if (!ti)
829 goto out;
830 asdl_seq_SET(type_ignores, i, ti);
831 }
832
833 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500834 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 case eval_input: {
836 expr_ty testlist_ast;
837
Nick Coghlan650f0d02007-04-15 12:05:43 +0000838 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000839 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
842 res = Expression(testlist_ast, arena);
843 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 }
845 case single_input:
846 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200847 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500849 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000851 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000853 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 goto out;
855 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 }
857 else {
858 n = CHILD(n, 0);
859 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200860 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500862 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000864 s = ast_for_stmt(&c, n);
865 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 asdl_seq_SET(stmts, 0, s);
868 }
869 else {
870 /* Only a simple_stmt can contain multiple statements. */
871 REQ(n, simple_stmt);
872 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 if (TYPE(CHILD(n, i)) == NEWLINE)
874 break;
875 s = ast_for_stmt(&c, CHILD(n, i));
876 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500877 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 asdl_seq_SET(stmts, i / 2, s);
879 }
880 }
881
Benjamin Peterson55e00432012-01-16 17:22:31 -0500882 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500884 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800885 case func_type_input:
886 n = CHILD(n, 0);
887 REQ(n, func_type);
888
889 if (TYPE(CHILD(n, 1)) == typelist) {
890 ch = CHILD(n, 1);
891 /* this is overly permissive -- we don't pay any attention to
892 * stars on the args -- just parse them into an ordered list */
893 num = 0;
894 for (i = 0; i < NCH(ch); i++) {
895 if (TYPE(CHILD(ch, i)) == test) {
896 num++;
897 }
898 }
899
900 argtypes = _Py_asdl_seq_new(num, arena);
901 if (!argtypes)
902 goto out;
903
904 j = 0;
905 for (i = 0; i < NCH(ch); i++) {
906 if (TYPE(CHILD(ch, i)) == test) {
907 arg = ast_for_expr(&c, CHILD(ch, i));
908 if (!arg)
909 goto out;
910 asdl_seq_SET(argtypes, j++, arg);
911 }
912 }
913 }
914 else {
915 argtypes = _Py_asdl_seq_new(0, arena);
916 if (!argtypes)
917 goto out;
918 }
919
920 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
921 if (!ret)
922 goto out;
923 res = FunctionType(argtypes, ret, arena);
924 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000926 PyErr_Format(PyExc_SystemError,
927 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500928 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500930 out:
931 if (c.c_normalize) {
932 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500933 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500934 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935}
936
Victor Stinner14e461d2013-08-26 22:28:21 +0200937mod_ty
938PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
939 PyArena *arena)
940{
941 mod_ty mod;
942 PyObject *filename;
943 filename = PyUnicode_DecodeFSDefault(filename_str);
944 if (filename == NULL)
945 return NULL;
946 mod = PyAST_FromNodeObject(n, flags, filename, arena);
947 Py_DECREF(filename);
948 return mod;
949
950}
951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
953*/
954
955static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800956get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957{
958 switch (TYPE(n)) {
959 case VBAR:
960 return BitOr;
961 case CIRCUMFLEX:
962 return BitXor;
963 case AMPER:
964 return BitAnd;
965 case LEFTSHIFT:
966 return LShift;
967 case RIGHTSHIFT:
968 return RShift;
969 case PLUS:
970 return Add;
971 case MINUS:
972 return Sub;
973 case STAR:
974 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400975 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800976 if (c->c_feature_version < 5) {
977 ast_error(c, n,
978 "The '@' operator is only supported in Python 3.5 and greater");
979 return (operator_ty)0;
980 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400981 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 case SLASH:
983 return Div;
984 case DOUBLESLASH:
985 return FloorDiv;
986 case PERCENT:
987 return Mod;
988 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 }
991}
992
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200993static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000994 "None",
995 "True",
996 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200997 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000998 NULL,
999};
1000
1001static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001002forbidden_name(struct compiling *c, identifier name, const node *n,
1003 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001004{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001005 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001006 const char * const *p = FORBIDDEN;
1007 if (!full_checks) {
1008 /* In most cases, the parser will protect True, False, and None
1009 from being assign to. */
1010 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001011 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001012 for (; *p; p++) {
1013 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1014 ast_error(c, n, "cannot assign to %U", name);
1015 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001016 }
1017 }
1018 return 0;
1019}
1020
Serhiy Storchakab619b092018-11-27 09:40:29 +02001021static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001022copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001023{
1024 if (e) {
1025 e->lineno = LINENO(n);
1026 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001027 e->end_lineno = end->n_end_lineno;
1028 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001029 }
1030 return e;
1031}
1032
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001033static const char *
1034get_expr_name(expr_ty e)
1035{
1036 switch (e->kind) {
1037 case Attribute_kind:
1038 return "attribute";
1039 case Subscript_kind:
1040 return "subscript";
1041 case Starred_kind:
1042 return "starred";
1043 case Name_kind:
1044 return "name";
1045 case List_kind:
1046 return "list";
1047 case Tuple_kind:
1048 return "tuple";
1049 case Lambda_kind:
1050 return "lambda";
1051 case Call_kind:
1052 return "function call";
1053 case BoolOp_kind:
1054 case BinOp_kind:
1055 case UnaryOp_kind:
1056 return "operator";
1057 case GeneratorExp_kind:
1058 return "generator expression";
1059 case Yield_kind:
1060 case YieldFrom_kind:
1061 return "yield expression";
1062 case Await_kind:
1063 return "await expression";
1064 case ListComp_kind:
1065 return "list comprehension";
1066 case SetComp_kind:
1067 return "set comprehension";
1068 case DictComp_kind:
1069 return "dict comprehension";
1070 case Dict_kind:
1071 return "dict display";
1072 case Set_kind:
1073 return "set display";
1074 case JoinedStr_kind:
1075 case FormattedValue_kind:
1076 return "f-string expression";
1077 case Constant_kind: {
1078 PyObject *value = e->v.Constant.value;
1079 if (value == Py_None) {
1080 return "None";
1081 }
1082 if (value == Py_False) {
1083 return "False";
1084 }
1085 if (value == Py_True) {
1086 return "True";
1087 }
1088 if (value == Py_Ellipsis) {
1089 return "Ellipsis";
1090 }
1091 return "literal";
1092 }
1093 case Compare_kind:
1094 return "comparison";
1095 case IfExp_kind:
1096 return "conditional expression";
1097 case NamedExpr_kind:
1098 return "named expression";
1099 default:
1100 PyErr_Format(PyExc_SystemError,
1101 "unexpected expression in assignment %d (line %d)",
1102 e->kind, e->lineno);
1103 return NULL;
1104 }
1105}
1106
Jeremy Hyltona8293132006-02-28 17:58:27 +00001107/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108
1109 Only sets context for expr kinds that "can appear in assignment context"
1110 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1111 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112*/
1113
1114static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001115set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116{
1117 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001118
Serhiy Storchaka6b975982020-03-17 23:41:08 +02001119 /* Expressions in an augmented assignment have a Store context. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120
1121 switch (e->kind) {
1122 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001123 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001124 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001125 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001126 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001128 e->v.Subscript.ctx = ctx;
1129 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001130 case Starred_kind:
1131 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001132 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001133 return 0;
1134 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001136 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001137 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001138 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 }
1140 e->v.Name.ctx = ctx;
1141 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001143 e->v.List.ctx = ctx;
1144 s = e->v.List.elts;
1145 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001147 e->v.Tuple.ctx = ctx;
1148 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001149 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001150 default: {
1151 const char *expr_name = get_expr_name(e);
1152 if (expr_name != NULL) {
1153 ast_error(c, n, "cannot %s %s",
1154 ctx == Store ? "assign to" : "delete",
1155 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001156 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001157 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001158 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001159 }
1160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 */
1164 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001165 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001168 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001169 return 0;
1170 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 }
1172 return 1;
1173}
1174
1175static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001176ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177{
1178 REQ(n, augassign);
1179 n = CHILD(n, 0);
1180 switch (STR(n)[0]) {
1181 case '+':
1182 return Add;
1183 case '-':
1184 return Sub;
1185 case '/':
1186 if (STR(n)[1] == '/')
1187 return FloorDiv;
1188 else
1189 return Div;
1190 case '%':
1191 return Mod;
1192 case '<':
1193 return LShift;
1194 case '>':
1195 return RShift;
1196 case '&':
1197 return BitAnd;
1198 case '^':
1199 return BitXor;
1200 case '|':
1201 return BitOr;
1202 case '*':
1203 if (STR(n)[1] == '*')
1204 return Pow;
1205 else
1206 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001207 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001208 if (c->c_feature_version < 5) {
1209 ast_error(c, n,
1210 "The '@' operator is only supported in Python 3.5 and greater");
1211 return (operator_ty)0;
1212 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001213 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001215 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001216 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 }
1218}
1219
1220static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001221ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001223 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 |'is' 'not'
1225 */
1226 REQ(n, comp_op);
1227 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001228 n = CHILD(n, 0);
1229 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 case LESS:
1231 return Lt;
1232 case GREATER:
1233 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001234 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 return Eq;
1236 case LESSEQUAL:
1237 return LtE;
1238 case GREATEREQUAL:
1239 return GtE;
1240 case NOTEQUAL:
1241 return NotEq;
1242 case NAME:
1243 if (strcmp(STR(n), "in") == 0)
1244 return In;
1245 if (strcmp(STR(n), "is") == 0)
1246 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001247 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001249 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001252 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 }
1254 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001255 /* handle "not in" and "is not" */
1256 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 case NAME:
1258 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1259 return NotIn;
1260 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1261 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001262 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001264 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 }
Neal Norwitz79792652005-11-14 04:25:03 +00001269 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272}
1273
1274static asdl_seq *
1275seq_for_testlist(struct compiling *c, const node *n)
1276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001278 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1279 */
Armin Rigo31441302005-10-21 12:57:31 +00001280 asdl_seq *seq;
1281 expr_ty expression;
1282 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001283 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001285 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 if (!seq)
1287 return NULL;
1288
1289 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001291 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292
Benjamin Peterson4905e802009-09-27 02:43:28 +00001293 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001294 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296
1297 assert(i / 2 < seq->size);
1298 asdl_seq_SET(seq, i / 2, expression);
1299 }
1300 return seq;
1301}
1302
Neal Norwitzc1505362006-12-28 06:47:50 +00001303static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001304ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001305{
1306 identifier name;
1307 expr_ty annotation = NULL;
1308 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001309 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001310
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001311 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001312 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 name = NEW_IDENTIFIER(ch);
1314 if (!name)
1315 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001316 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001317 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001318
1319 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1320 annotation = ast_for_expr(c, CHILD(n, 2));
1321 if (!annotation)
1322 return NULL;
1323 }
1324
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001325 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001326 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001327 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001328 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001329 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330}
1331
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332/* returns -1 if failed to handle keyword only arguments
1333 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001334 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 ^^^
1336 start pointing here
1337 */
1338static int
1339handle_keywordonly_args(struct compiling *c, const node *n, int start,
1340 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1341{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001342 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001343 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001345 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 int i = start;
1347 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001348
1349 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001350 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001351 return -1;
1352 }
1353 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 while (i < NCH(n)) {
1355 ch = CHILD(n, i);
1356 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001357 case vfpdef:
1358 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001360 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001361 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001362 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001363 asdl_seq_SET(kwdefaults, j, expression);
1364 i += 2; /* '=' and test */
1365 }
1366 else { /* setting NULL if no default value exists */
1367 asdl_seq_SET(kwdefaults, j, NULL);
1368 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 if (NCH(ch) == 3) {
1370 /* ch is NAME ':' test */
1371 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001372 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001373 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001374 }
1375 else {
1376 annotation = NULL;
1377 }
1378 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001379 argname = NEW_IDENTIFIER(ch);
1380 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001382 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001383 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001384 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001385 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001386 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001387 if (!arg)
1388 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001389 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001390 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001391 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001392 i += 1; /* the comma, if present */
1393 break;
1394 case TYPE_COMMENT:
1395 /* arg will be equal to the last argument processed */
1396 arg->type_comment = NEW_TYPE_COMMENT(ch);
1397 if (!arg->type_comment)
1398 goto error;
1399 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 break;
1401 case DOUBLESTAR:
1402 return i;
1403 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001404 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 goto error;
1406 }
1407 }
1408 return i;
1409 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412
Jeremy Hyltona8293132006-02-28 17:58:27 +00001413/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414
1415static arguments_ty
1416ast_for_arguments(struct compiling *c, const node *n)
1417{
Neal Norwitzc1505362006-12-28 06:47:50 +00001418 /* This function handles both typedargslist (function definition)
1419 and varargslist (lambda definition).
1420
1421 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001422
1423 The following definition for typedarglist is equivalent to this set of rules:
1424
1425 arguments = argument (',' [TYPE_COMMENT] argument)*
1426 argument = tfpdef ['=' test]
1427 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1428 args = '*' [tfpdef]
1429 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1430 [TYPE_COMMENT] [kwargs]])
1431 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1432 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1433 [TYPE_COMMENT] [args_kwonly_kwargs]])
1434 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1435 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1436 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1437
1438 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1439 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1440 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1441 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1442 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1443 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1444 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1445 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1446 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1447 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1448 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1449 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1450 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1451 '**' tfpdef [','] [TYPE_COMMENT]))
1452
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001454
1455 The following definition for varargslist is equivalent to this set of rules:
1456
1457 arguments = argument (',' argument )*
1458 argument = vfpdef ['=' test]
1459 kwargs = '**' vfpdef [',']
1460 args = '*' [vfpdef]
1461 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1462 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1463 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1464 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1465 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1466 (vararglist_no_posonly)
1467
1468 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1469 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1470 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1471 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1472 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1473 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1474 [',']]] | '**' vfpdef [','])
1475
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001476 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001477
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001479 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001481 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001482 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001483 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 node *ch;
1485
1486 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001487 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001488 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001489 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001491 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492
Jeremy Hyltone921e022008-07-17 16:37:17 +00001493 /* First count the number of positional args & defaults. The
1494 variable i is the loop index for this for loop and the next.
1495 The next loop picks up where the first leaves off.
1496 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001498 ch = CHILD(n, i);
1499 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001500 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001501 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001502 if (i < NCH(n) && /* skip argument following star */
1503 (TYPE(CHILD(n, i)) == tfpdef ||
1504 TYPE(CHILD(n, i)) == vfpdef)) {
1505 i++;
1506 }
1507 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001508 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001509 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001510 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001511 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001512 if (TYPE(ch) == SLASH ) {
1513 nposonlyargs = nposargs;
1514 nposargs = 0;
1515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001518 defaults for keyword only args */
1519 for ( ; i < NCH(n); ++i) {
1520 ch = CHILD(n, i);
1521 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001522 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001523 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001524 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1525 if (!posonlyargs && nposonlyargs) {
1526 return NULL;
1527 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001528 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001529 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001530 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001531 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001532 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001533 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001534 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001536 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001537 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001538 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001540 since we set NULL as default for keyword only argument w/o default
1541 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001542 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001543 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001545 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001547 /* tfpdef: NAME [':' test]
1548 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 */
1550 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001551 j = 0; /* index for defaults */
1552 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001553 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001555 ch = CHILD(n, i);
1556 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001557 case tfpdef:
1558 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1560 anything other than EQUAL or a comma? */
1561 /* XXX Should NCH(n) check be made a separate check? */
1562 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001563 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1564 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001565 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001566 assert(posdefaults != NULL);
1567 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001569 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001571 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001572 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001573 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001574 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001575 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001576 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001577 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001578 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001579 if (l < nposonlyargs) {
1580 asdl_seq_SET(posonlyargs, l++, arg);
1581 } else {
1582 asdl_seq_SET(posargs, k++, arg);
1583 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001584 i += 1; /* the name */
1585 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1586 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001588 case SLASH:
1589 /* Advance the slash and the comma. If there are more names
1590 * after the slash there will be a comma so we are advancing
1591 * the correct number of nodes. If the slash is the last item,
1592 * we will be advancing an extra token but then * i > NCH(n)
1593 * and the enclosing while will finish correctly. */
1594 i += 2;
1595 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001597 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001598 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1599 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001600 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001601 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001602 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001603 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001604 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001605 if (TYPE(ch) == COMMA) {
1606 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001607 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001608
1609 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1610 ast_error(c, CHILD(n, i),
1611 "bare * has associated type comment");
1612 return NULL;
1613 }
1614
Guido van Rossum4f72a782006-10-27 23:31:49 +00001615 res = handle_keywordonly_args(c, n, i,
1616 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001617 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001618 i = res; /* res has new position to process */
1619 }
1620 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001621 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001622 if (!vararg)
1623 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001624
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001625 i += 2; /* the star and the name */
1626 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1627 i += 1; /* the comma, if present */
1628
1629 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1630 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1631 if (!vararg->type_comment)
1632 return NULL;
1633 i += 1;
1634 }
1635
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001636 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1637 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001638 int res = 0;
1639 res = handle_keywordonly_args(c, n, i,
1640 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001641 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001642 i = res; /* res has new position to process */
1643 }
1644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 break;
1646 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001647 ch = CHILD(n, i+1); /* tfpdef */
1648 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001649 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001650 if (!kwarg)
1651 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001652 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001653 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001654 i += 1; /* the comma, if present */
1655 break;
1656 case TYPE_COMMENT:
1657 assert(i);
1658
1659 if (kwarg)
1660 arg = kwarg;
1661
1662 /* arg will be equal to the last argument processed */
1663 arg->type_comment = NEW_TYPE_COMMENT(ch);
1664 if (!arg->type_comment)
1665 return NULL;
1666 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 break;
1668 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001669 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 "unexpected node in varargslist: %d @ %d",
1671 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001672 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001675 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676}
1677
1678static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679ast_for_decorator(struct compiling *c, const node *n)
1680{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001681 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001684 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001685 REQ(CHILD(n, 2), NEWLINE);
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001686
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001687 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688}
1689
1690static asdl_seq*
1691ast_for_decorators(struct compiling *c, const node *n)
1692{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001693 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001694 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001698 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 if (!decorator_seq)
1700 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001703 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001704 if (!d)
1705 return NULL;
1706 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 }
1708 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709}
1710
1711static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001712ast_for_funcdef_impl(struct compiling *c, const node *n0,
1713 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001715 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001716 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001717 identifier name;
1718 arguments_ty args;
1719 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001720 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001721 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001722 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001723 node *tc;
1724 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725
Guido van Rossum495da292019-03-07 12:38:08 -08001726 if (is_async && c->c_feature_version < 5) {
1727 ast_error(c, n,
1728 "Async functions are only supported in Python 3.5 and greater");
1729 return NULL;
1730 }
1731
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 REQ(n, funcdef);
1733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 name = NEW_IDENTIFIER(CHILD(n, name_i));
1735 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001736 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001737 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1740 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001741 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001742 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1743 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1744 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001745 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001746 name_i += 2;
1747 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001748 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1749 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1750 if (!type_comment)
1751 return NULL;
1752 name_i += 1;
1753 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001754 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001756 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001757 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001759 if (NCH(CHILD(n, name_i + 3)) > 1) {
1760 /* Check if the suite has a type comment in it. */
1761 tc = CHILD(CHILD(n, name_i + 3), 1);
1762
1763 if (TYPE(tc) == TYPE_COMMENT) {
1764 if (type_comment != NULL) {
1765 ast_error(c, n, "Cannot have two type comments on def");
1766 return NULL;
1767 }
1768 type_comment = NEW_TYPE_COMMENT(tc);
1769 if (!type_comment)
1770 return NULL;
1771 }
1772 }
1773
Yury Selivanov75445082015-05-11 22:57:16 -04001774 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001775 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001776 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001777 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001778 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001779 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001780}
1781
1782static stmt_ty
1783ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1784{
Guido van Rossum495da292019-03-07 12:38:08 -08001785 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001786 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001787 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001788 REQ(CHILD(n, 1), funcdef);
1789
guoci90fc8982018-09-11 17:45:45 -04001790 return ast_for_funcdef_impl(c, n, decorator_seq,
1791 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001792}
1793
1794static stmt_ty
1795ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1796{
1797 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1798 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001799 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001800}
1801
1802
1803static stmt_ty
1804ast_for_async_stmt(struct compiling *c, const node *n)
1805{
Guido van Rossum495da292019-03-07 12:38:08 -08001806 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001807 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001808 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001809
1810 switch (TYPE(CHILD(n, 1))) {
1811 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001812 return ast_for_funcdef_impl(c, n, NULL,
1813 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001814 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001815 return ast_for_with_stmt(c, n,
1816 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001817
1818 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001819 return ast_for_for_stmt(c, n,
1820 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001821
1822 default:
1823 PyErr_Format(PyExc_SystemError,
1824 "invalid async stament: %s",
1825 STR(CHILD(n, 1)));
1826 return NULL;
1827 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828}
1829
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001830static stmt_ty
1831ast_for_decorated(struct compiling *c, const node *n)
1832{
Yury Selivanov75445082015-05-11 22:57:16 -04001833 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001834 stmt_ty thing = NULL;
1835 asdl_seq *decorator_seq = NULL;
1836
1837 REQ(n, decorated);
1838
1839 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1840 if (!decorator_seq)
1841 return NULL;
1842
1843 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001844 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001846
1847 if (TYPE(CHILD(n, 1)) == funcdef) {
1848 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1849 } else if (TYPE(CHILD(n, 1)) == classdef) {
1850 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001851 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1852 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001853 }
1854 return thing;
1855}
1856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001858ast_for_namedexpr(struct compiling *c, const node *n)
1859{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001860 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001861 argument: ( test [comp_for] |
1862 test ':=' test |
1863 test '=' test |
1864 '**' test |
1865 '*' test )
1866 */
1867 expr_ty target, value;
1868
1869 target = ast_for_expr(c, CHILD(n, 0));
1870 if (!target)
1871 return NULL;
1872
1873 value = ast_for_expr(c, CHILD(n, 2));
1874 if (!value)
1875 return NULL;
1876
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001877 if (target->kind != Name_kind) {
1878 const char *expr_name = get_expr_name(target);
1879 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001880 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001881 }
1882 return NULL;
1883 }
1884
1885 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001886 return NULL;
1887
1888 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1889 n->n_end_col_offset, c->c_arena);
1890}
1891
1892static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893ast_for_lambdef(struct compiling *c, const node *n)
1894{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001895 /* lambdef: 'lambda' [varargslist] ':' test
1896 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 arguments_ty args;
1898 expr_ty expression;
1899
1900 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001901 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 if (!args)
1903 return NULL;
1904 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001905 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
1908 else {
1909 args = ast_for_arguments(c, CHILD(n, 1));
1910 if (!args)
1911 return NULL;
1912 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001913 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 }
1916
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001917 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1918 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919}
1920
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001921static expr_ty
1922ast_for_ifexpr(struct compiling *c, const node *n)
1923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001925 expr_ty expression, body, orelse;
1926
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001927 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001928 body = ast_for_expr(c, CHILD(n, 0));
1929 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001930 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001931 expression = ast_for_expr(c, CHILD(n, 2));
1932 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001933 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001934 orelse = ast_for_expr(c, CHILD(n, 4));
1935 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001936 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001937 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001938 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001939 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001940}
1941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001943 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944
Nick Coghlan650f0d02007-04-15 12:05:43 +00001945 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946*/
1947
1948static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001949count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001951 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952
Guido van Rossumd8faa362007-04-27 19:54:29 +00001953 count_comp_for:
1954 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001955 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001956 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001957 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001958 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001959 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001960 else if (NCH(n) == 1) {
1961 n = CHILD(n, 0);
1962 }
1963 else {
1964 goto error;
1965 }
1966 if (NCH(n) == (5)) {
1967 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001968 }
1969 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001970 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001971 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001972 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001973 REQ(n, comp_iter);
1974 n = CHILD(n, 0);
1975 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001976 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001977 else if (TYPE(n) == comp_if) {
1978 if (NCH(n) == 3) {
1979 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001980 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001981 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001982 else
1983 return n_fors;
1984 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001985
Jelle Zijlstraac317702017-10-05 20:24:46 -07001986 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001987 /* Should never be reached */
1988 PyErr_SetString(PyExc_SystemError,
1989 "logic error in count_comp_fors");
1990 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991}
1992
Nick Coghlan650f0d02007-04-15 12:05:43 +00001993/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994
Nick Coghlan650f0d02007-04-15 12:05:43 +00001995 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996*/
1997
1998static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001999count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002001 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002
Guido van Rossumd8faa362007-04-27 19:54:29 +00002003 while (1) {
2004 REQ(n, comp_iter);
2005 if (TYPE(CHILD(n, 0)) == comp_for)
2006 return n_ifs;
2007 n = CHILD(n, 0);
2008 REQ(n, comp_if);
2009 n_ifs++;
2010 if (NCH(n) == 2)
2011 return n_ifs;
2012 n = CHILD(n, 2);
2013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014}
2015
Guido van Rossum992d4a32007-07-11 13:09:30 +00002016static asdl_seq *
2017ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002020 asdl_seq *comps;
2021
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002022 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 if (n_fors == -1)
2024 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002025
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002026 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002027 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002031 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002033 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002034 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002036 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037
Guido van Rossum992d4a32007-07-11 13:09:30 +00002038 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039
Jelle Zijlstraac317702017-10-05 20:24:46 -07002040 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002041 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002042 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002043 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002044 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002045 else {
2046 sync_n = CHILD(n, 0);
2047 }
2048 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002049
Guido van Rossum495da292019-03-07 12:38:08 -08002050 /* Async comprehensions only allowed in Python 3.6 and greater */
2051 if (is_async && c->c_feature_version < 6) {
2052 ast_error(c, n,
2053 "Async comprehensions are only supported in Python 3.6 and greater");
2054 return NULL;
2055 }
2056
Jelle Zijlstraac317702017-10-05 20:24:46 -07002057 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002058 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002059 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002061 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002062 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002064
Thomas Wouters89f507f2006-12-13 04:49:30 +00002065 /* Check the # of children rather than the length of t, since
2066 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002067 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002068 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002069 comp = comprehension(first, expression, NULL,
2070 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002072 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2073 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2074 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002075 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002076 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002078
Jelle Zijlstraac317702017-10-05 20:24:46 -07002079 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 int j, n_ifs;
2081 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082
Jelle Zijlstraac317702017-10-05 20:24:46 -07002083 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002084 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002085 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002087
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002088 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002089 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002093 REQ(n, comp_iter);
2094 n = CHILD(n, 0);
2095 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096
Guido van Rossum992d4a32007-07-11 13:09:30 +00002097 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002098 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002099 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002100 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002101 if (NCH(n) == 3)
2102 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002104 /* on exit, must guarantee that n is a comp_for */
2105 if (TYPE(n) == comp_iter)
2106 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002107 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002109 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002111 return comps;
2112}
2113
2114static expr_ty
2115ast_for_itercomp(struct compiling *c, const node *n, int type)
2116{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002117 /* testlist_comp: (test|star_expr)
2118 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002119 expr_ty elt;
2120 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002121 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122
Guido van Rossum992d4a32007-07-11 13:09:30 +00002123 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002125 ch = CHILD(n, 0);
2126 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002127 if (!elt)
2128 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002129 if (elt->kind == Starred_kind) {
2130 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2131 return NULL;
2132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133
Guido van Rossum992d4a32007-07-11 13:09:30 +00002134 comps = ast_for_comprehension(c, CHILD(n, 1));
2135 if (!comps)
2136 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002137
2138 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002139 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2140 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002141 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002142 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2143 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002144 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002145 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2146 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002147 else
2148 /* Should never happen */
2149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150}
2151
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002152/* Fills in the key, value pair corresponding to the dict element. In case
2153 * of an unpacking, key is NULL. *i is advanced by the number of ast
2154 * elements. Iff successful, nonzero is returned.
2155 */
2156static int
2157ast_for_dictelement(struct compiling *c, const node *n, int *i,
2158 expr_ty *key, expr_ty *value)
2159{
2160 expr_ty expression;
2161 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2162 assert(NCH(n) - *i >= 2);
2163
2164 expression = ast_for_expr(c, CHILD(n, *i + 1));
2165 if (!expression)
2166 return 0;
2167 *key = NULL;
2168 *value = expression;
2169
2170 *i += 2;
2171 }
2172 else {
2173 assert(NCH(n) - *i >= 3);
2174
2175 expression = ast_for_expr(c, CHILD(n, *i));
2176 if (!expression)
2177 return 0;
2178 *key = expression;
2179
2180 REQ(CHILD(n, *i + 1), COLON);
2181
2182 expression = ast_for_expr(c, CHILD(n, *i + 2));
2183 if (!expression)
2184 return 0;
2185 *value = expression;
2186
2187 *i += 3;
2188 }
2189 return 1;
2190}
2191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002193ast_for_dictcomp(struct compiling *c, const node *n)
2194{
2195 expr_ty key, value;
2196 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002197 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002199 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002200 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 assert(key);
2202 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002204 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002205 if (!comps)
2206 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002208 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2209 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002210}
2211
2212static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002213ast_for_dictdisplay(struct compiling *c, const node *n)
2214{
2215 int i;
2216 int j;
2217 int size;
2218 asdl_seq *keys, *values;
2219
2220 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2221 keys = _Py_asdl_seq_new(size, c->c_arena);
2222 if (!keys)
2223 return NULL;
2224
2225 values = _Py_asdl_seq_new(size, c->c_arena);
2226 if (!values)
2227 return NULL;
2228
2229 j = 0;
2230 for (i = 0; i < NCH(n); i++) {
2231 expr_ty key, value;
2232
2233 if (!ast_for_dictelement(c, n, &i, &key, &value))
2234 return NULL;
2235 asdl_seq_SET(keys, j, key);
2236 asdl_seq_SET(values, j, value);
2237
2238 j++;
2239 }
2240 keys->size = j;
2241 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002242 return Dict(keys, values, LINENO(n), n->n_col_offset,
2243 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002244}
2245
2246static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002247ast_for_genexp(struct compiling *c, const node *n)
2248{
2249 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002250 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002251}
2252
2253static expr_ty
2254ast_for_listcomp(struct compiling *c, const node *n)
2255{
2256 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002257 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002258}
2259
2260static expr_ty
2261ast_for_setcomp(struct compiling *c, const node *n)
2262{
2263 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002264 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002265}
2266
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002267static expr_ty
2268ast_for_setdisplay(struct compiling *c, const node *n)
2269{
2270 int i;
2271 int size;
2272 asdl_seq *elts;
2273
2274 assert(TYPE(n) == (dictorsetmaker));
2275 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2276 elts = _Py_asdl_seq_new(size, c->c_arena);
2277 if (!elts)
2278 return NULL;
2279 for (i = 0; i < NCH(n); i += 2) {
2280 expr_ty expression;
2281 expression = ast_for_expr(c, CHILD(n, i));
2282 if (!expression)
2283 return NULL;
2284 asdl_seq_SET(elts, i / 2, expression);
2285 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002286 return Set(elts, LINENO(n), n->n_col_offset,
2287 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002288}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002289
2290static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291ast_for_atom(struct compiling *c, const node *n)
2292{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002293 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2294 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002295 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 */
2297 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002300 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002301 PyObject *name;
2302 const char *s = STR(ch);
2303 size_t len = strlen(s);
2304 if (len >= 4 && len <= 5) {
2305 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002306 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002307 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002308 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002309 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002310 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002311 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002312 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002313 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002314 }
2315 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002316 if (!name)
2317 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002318 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002319 return Name(name, Load, LINENO(n), n->n_col_offset,
2320 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002321 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002323 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002324 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002325 const char *errtype = NULL;
2326 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2327 errtype = "unicode error";
2328 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2329 errtype = "value error";
2330 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002331 PyObject *type, *value, *tback, *errstr;
2332 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002333 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002334 if (errstr) {
2335 ast_error(c, n, "(%s) %U", errtype, errstr);
2336 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002337 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002338 else {
2339 PyErr_Clear();
2340 ast_error(c, n, "(%s) unknown error", errtype);
2341 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002342 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002343 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002344 Py_XDECREF(tback);
2345 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002346 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002347 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002348 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 }
2350 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002351 PyObject *pynum;
2352 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2353 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2354 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2355 ast_error(c, ch,
2356 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2357 return NULL;
2358 }
2359 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002360 if (!pynum)
2361 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002362
Victor Stinner43d81952013-07-17 00:57:58 +02002363 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2364 Py_DECREF(pynum);
2365 return NULL;
2366 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002367 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002368 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369 }
Georg Brandldde00282007-03-18 19:01:53 +00002370 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002371 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002372 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002374 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375
Thomas Wouters89f507f2006-12-13 04:49:30 +00002376 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002377 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2378 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379
Thomas Wouters89f507f2006-12-13 04:49:30 +00002380 if (TYPE(ch) == yield_expr)
2381 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002384 if (NCH(ch) == 1) {
2385 return ast_for_testlist(c, ch);
2386 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002387
Serhiy Storchakab619b092018-11-27 09:40:29 +02002388 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002389 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002390 }
2391 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002392 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002395 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396
Thomas Wouters89f507f2006-12-13 04:49:30 +00002397 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002398 return List(NULL, Load, LINENO(n), n->n_col_offset,
2399 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400
Nick Coghlan650f0d02007-04-15 12:05:43 +00002401 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002402 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2403 asdl_seq *elts = seq_for_testlist(c, ch);
2404 if (!elts)
2405 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002406
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002407 return List(elts, Load, LINENO(n), n->n_col_offset,
2408 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002409 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002410 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002411 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002414 /* dictorsetmaker: ( ((test ':' test | '**' test)
2415 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2416 * ((test | '*' test)
2417 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002418 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002419 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002420 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002421 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002422 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2423 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002424 }
2425 else {
2426 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2427 if (NCH(ch) == 1 ||
2428 (NCH(ch) > 1 &&
2429 TYPE(CHILD(ch, 1)) == COMMA)) {
2430 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002431 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002432 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002433 else if (NCH(ch) > 1 &&
2434 TYPE(CHILD(ch, 1)) == comp_for) {
2435 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002436 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002437 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002438 else if (NCH(ch) > 3 - is_dict &&
2439 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2440 /* It's a dictionary comprehension. */
2441 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002442 ast_error(c, n,
2443 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002444 return NULL;
2445 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002446 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002447 }
2448 else {
2449 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002450 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002451 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002452 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002456 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 }
2459}
2460
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002461static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462ast_for_slice(struct compiling *c, const node *n)
2463{
2464 node *ch;
2465 expr_ty lower = NULL, upper = NULL, step = NULL;
2466
2467 REQ(n, subscript);
2468
2469 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002470 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 sliceop: ':' [test]
2472 */
2473 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002475 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 }
2477
2478 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 if (!lower)
2481 return NULL;
2482 }
2483
2484 /* If there's an upper bound it's in the second or third position. */
2485 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002486 if (NCH(n) > 1) {
2487 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488
Thomas Wouters89f507f2006-12-13 04:49:30 +00002489 if (TYPE(n2) == test) {
2490 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 if (!upper)
2492 return NULL;
2493 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002496 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497
Thomas Wouters89f507f2006-12-13 04:49:30 +00002498 if (TYPE(n2) == test) {
2499 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 if (!upper)
2501 return NULL;
2502 }
2503 }
2504
2505 ch = CHILD(n, NCH(n) - 1);
2506 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002507 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002508 ch = CHILD(ch, 1);
2509 if (TYPE(ch) == test) {
2510 step = ast_for_expr(c, ch);
2511 if (!step)
2512 return NULL;
2513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 }
2515 }
2516
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002517 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2518 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519}
2520
2521static expr_ty
2522ast_for_binop(struct compiling *c, const node *n)
2523{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002524 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002526 BinOp(BinOp(A, op, B), op, C).
2527 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
Guido van Rossumd8faa362007-04-27 19:54:29 +00002529 int i, nops;
2530 expr_ty expr1, expr2, result;
2531 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532
Guido van Rossumd8faa362007-04-27 19:54:29 +00002533 expr1 = ast_for_expr(c, CHILD(n, 0));
2534 if (!expr1)
2535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536
Guido van Rossumd8faa362007-04-27 19:54:29 +00002537 expr2 = ast_for_expr(c, CHILD(n, 2));
2538 if (!expr2)
2539 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540
Guido van Rossum495da292019-03-07 12:38:08 -08002541 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002542 if (!newoperator)
2543 return NULL;
2544
2545 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002546 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002547 c->c_arena);
2548 if (!result)
2549 return NULL;
2550
2551 nops = (NCH(n) - 1) / 2;
2552 for (i = 1; i < nops; i++) {
2553 expr_ty tmp_result, tmp;
2554 const node* next_oper = CHILD(n, i * 2 + 1);
2555
Guido van Rossum495da292019-03-07 12:38:08 -08002556 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002557 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 return NULL;
2559
Guido van Rossumd8faa362007-04-27 19:54:29 +00002560 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2561 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 return NULL;
2563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002565 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002566 CHILD(n, i * 2 + 2)->n_end_lineno,
2567 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002568 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002570 return NULL;
2571 result = tmp_result;
2572 }
2573 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574}
2575
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002576static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002577ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002580 subscriptlist: subscript (',' subscript)* [',']
2581 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2582 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002583 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002584 REQ(n, trailer);
2585 if (TYPE(CHILD(n, 0)) == LPAR) {
2586 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002587 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002588 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002589 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002590 return ast_for_call(c, CHILD(n, 1), left_expr,
2591 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002592 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002593 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002594 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2595 if (!attr_id)
2596 return NULL;
2597 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002598 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002599 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002600 }
2601 else {
2602 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002603 REQ(CHILD(n, 2), RSQB);
2604 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002605 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002606 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002607 if (!slc)
2608 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002609 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002610 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002611 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002612 }
2613 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002614 int j;
2615 expr_ty slc, e;
2616 asdl_seq *elts;
2617 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2618 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002619 return NULL;
2620 for (j = 0; j < NCH(n); j += 2) {
2621 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002622 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002623 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002624 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002625 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002626 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002627 n->n_end_lineno, n->n_end_col_offset,
2628 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002629 if (!e)
2630 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002631 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002632 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002633 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2634 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002635 }
2636 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002637}
2638
2639static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002640ast_for_factor(struct compiling *c, const node *n)
2641{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002642 expr_ty expression;
2643
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002644 expression = ast_for_expr(c, CHILD(n, 1));
2645 if (!expression)
2646 return NULL;
2647
2648 switch (TYPE(CHILD(n, 0))) {
2649 case PLUS:
2650 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002651 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002652 c->c_arena);
2653 case MINUS:
2654 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002655 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002656 c->c_arena);
2657 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002658 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2659 n->n_end_lineno, n->n_end_col_offset,
2660 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002661 }
2662 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2663 TYPE(CHILD(n, 0)));
2664 return NULL;
2665}
2666
2667static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002668ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002669{
Yury Selivanov75445082015-05-11 22:57:16 -04002670 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002671 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002672
2673 REQ(n, atom_expr);
2674 nch = NCH(n);
2675
Guido van Rossum495da292019-03-07 12:38:08 -08002676 if (TYPE(CHILD(n, 0)) == AWAIT) {
2677 if (c->c_feature_version < 5) {
2678 ast_error(c, n,
2679 "Await expressions are only supported in Python 3.5 and greater");
2680 return NULL;
2681 }
Yury Selivanov75445082015-05-11 22:57:16 -04002682 start = 1;
2683 assert(nch > 1);
2684 }
2685
2686 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002687 if (!e)
2688 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002689 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002690 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002691 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002692 return Await(e, LINENO(n), n->n_col_offset,
2693 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002694 }
2695
2696 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002697 node *ch = CHILD(n, i);
2698 if (TYPE(ch) != trailer)
2699 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002700 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2701 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002702 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002703 }
Yury Selivanov75445082015-05-11 22:57:16 -04002704
2705 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002706 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002707 return Await(e, LINENO(n), n->n_col_offset,
2708 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002709 }
2710 else {
2711 return e;
2712 }
2713}
2714
2715static expr_ty
2716ast_for_power(struct compiling *c, const node *n)
2717{
2718 /* power: atom trailer* ('**' factor)*
2719 */
2720 expr_ty e;
2721 REQ(n, power);
2722 e = ast_for_atom_expr(c, CHILD(n, 0));
2723 if (!e)
2724 return NULL;
2725 if (NCH(n) == 1)
2726 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002727 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2728 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002729 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002730 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002731 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2732 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002733 }
2734 return e;
2735}
2736
Guido van Rossum0368b722007-05-11 16:50:42 +00002737static expr_ty
2738ast_for_starred(struct compiling *c, const node *n)
2739{
2740 expr_ty tmp;
2741 REQ(n, star_expr);
2742
2743 tmp = ast_for_expr(c, CHILD(n, 1));
2744 if (!tmp)
2745 return NULL;
2746
2747 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002748 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2749 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002750}
2751
2752
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753/* Do not name a variable 'expr'! Will cause a compile error.
2754*/
2755
2756static expr_ty
2757ast_for_expr(struct compiling *c, const node *n)
2758{
2759 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002760 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002761 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002762 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764 and_test: not_test ('and' not_test)*
2765 not_test: 'not' not_test | comparison
2766 comparison: expr (comp_op expr)*
2767 expr: xor_expr ('|' xor_expr)*
2768 xor_expr: and_expr ('^' and_expr)*
2769 and_expr: shift_expr ('&' shift_expr)*
2770 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2771 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002772 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002774 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002775 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002776 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 */
2778
2779 asdl_seq *seq;
2780 int i;
2781
2782 loop:
2783 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002784 case namedexpr_test:
2785 if (NCH(n) == 3)
2786 return ast_for_namedexpr(c, n);
2787 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002790 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002791 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002793 else if (NCH(n) > 1)
2794 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002795 /* Fallthrough */
2796 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 case and_test:
2798 if (NCH(n) == 1) {
2799 n = CHILD(n, 0);
2800 goto loop;
2801 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002802 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 if (!seq)
2804 return NULL;
2805 for (i = 0; i < NCH(n); i += 2) {
2806 expr_ty e = ast_for_expr(c, CHILD(n, i));
2807 if (!e)
2808 return NULL;
2809 asdl_seq_SET(seq, i / 2, e);
2810 }
2811 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002812 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002813 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002814 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002815 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002816 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2817 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 case not_test:
2819 if (NCH(n) == 1) {
2820 n = CHILD(n, 0);
2821 goto loop;
2822 }
2823 else {
2824 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2825 if (!expression)
2826 return NULL;
2827
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002828 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002829 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002830 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 }
2832 case comparison:
2833 if (NCH(n) == 1) {
2834 n = CHILD(n, 0);
2835 goto loop;
2836 }
2837 else {
2838 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002839 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002840 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002841 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 if (!ops)
2843 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002844 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846 return NULL;
2847 }
2848 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002849 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002851 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855
2856 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002857 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002861 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 asdl_seq_SET(cmps, i / 2, expression);
2863 }
2864 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002865 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002869 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2870 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872
Guido van Rossum0368b722007-05-11 16:50:42 +00002873 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 /* The next five cases all handle BinOps. The main body of code
2876 is the same in each case, but the switch turned inside out to
2877 reuse the code for each type of operator.
2878 */
2879 case expr:
2880 case xor_expr:
2881 case and_expr:
2882 case shift_expr:
2883 case arith_expr:
2884 case term:
2885 if (NCH(n) == 1) {
2886 n = CHILD(n, 0);
2887 goto loop;
2888 }
2889 return ast_for_binop(c, n);
2890 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002891 node *an = NULL;
2892 node *en = NULL;
2893 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002894 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002895 if (NCH(n) > 1)
2896 an = CHILD(n, 1); /* yield_arg */
2897 if (an) {
2898 en = CHILD(an, NCH(an) - 1);
2899 if (NCH(an) == 2) {
2900 is_from = 1;
2901 exp = ast_for_expr(c, en);
2902 }
2903 else
2904 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 if (!exp)
2906 return NULL;
2907 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002908 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002909 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2910 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2911 return Yield(exp, LINENO(n), n->n_col_offset,
2912 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002914 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 if (NCH(n) == 1) {
2916 n = CHILD(n, 0);
2917 goto loop;
2918 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002920 case power:
2921 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002923 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 return NULL;
2925 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002926 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 return NULL;
2928}
2929
2930static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002931ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002932 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933{
2934 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002935 arglist: argument (',' argument)* [',']
2936 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937 */
2938
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002939 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002940 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002941 asdl_seq *args;
2942 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943
2944 REQ(n, arglist);
2945
2946 nargs = 0;
2947 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002949 node *ch = CHILD(n, i);
2950 if (TYPE(ch) == argument) {
2951 if (NCH(ch) == 1)
2952 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002953 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2954 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002955 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002956 ast_error(c, ch, "invalid syntax");
2957 return NULL;
2958 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002959 if (NCH(n) > 1) {
2960 ast_error(c, ch, "Generator expression must be parenthesized");
2961 return NULL;
2962 }
2963 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002964 else if (TYPE(CHILD(ch, 0)) == STAR)
2965 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002966 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2967 nargs++;
2968 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002970 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002971 nkeywords++;
2972 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002975 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002977 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002978 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002980 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002981
2982 nargs = 0; /* positional arguments + iterable argument unpackings */
2983 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2984 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002986 node *ch = CHILD(n, i);
2987 if (TYPE(ch) == argument) {
2988 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002989 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002990 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002991 /* a positional argument */
2992 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002993 if (ndoublestars) {
2994 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002995 "positional argument follows "
2996 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002997 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002998 else {
2999 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003000 "positional argument follows "
3001 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003002 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003003 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003004 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003005 e = ast_for_expr(c, chch);
3006 if (!e)
3007 return NULL;
3008 asdl_seq_SET(args, nargs++, e);
3009 }
3010 else if (TYPE(chch) == STAR) {
3011 /* an iterable argument unpacking */
3012 expr_ty starred;
3013 if (ndoublestars) {
3014 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003015 "iterable argument unpacking follows "
3016 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003017 return NULL;
3018 }
3019 e = ast_for_expr(c, CHILD(ch, 1));
3020 if (!e)
3021 return NULL;
3022 starred = Starred(e, Load, LINENO(chch),
3023 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003024 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003025 c->c_arena);
3026 if (!starred)
3027 return NULL;
3028 asdl_seq_SET(args, nargs++, starred);
3029
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003030 }
3031 else if (TYPE(chch) == DOUBLESTAR) {
3032 /* a keyword argument unpacking */
3033 keyword_ty kw;
3034 i++;
3035 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003037 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003038 kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset,
3039 e->end_lineno, e->end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003040 asdl_seq_SET(keywords, nkeywords++, kw);
3041 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003043 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003044 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003045 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003047 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003050 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3051 /* treat colon equal as positional argument */
3052 if (nkeywords) {
3053 if (ndoublestars) {
3054 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003055 "positional argument follows "
3056 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003057 }
3058 else {
3059 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003060 "positional argument follows "
3061 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003062 }
3063 return NULL;
3064 }
3065 e = ast_for_namedexpr(c, ch);
3066 if (!e)
3067 return NULL;
3068 asdl_seq_SET(args, nargs++, e);
3069 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003070 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003071 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003072 keyword_ty kw;
Pablo Galindo254ec782020-04-03 20:37:13 +01003073 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003075 // To remain LL(1), the grammar accepts any test (basically, any
3076 // expression) in the keyword slot of a call site. So, we need
3077 // to manually enforce that the keyword is a NAME here.
3078 static const int name_tree[] = {
3079 test,
3080 or_test,
3081 and_test,
3082 not_test,
3083 comparison,
3084 expr,
3085 xor_expr,
3086 and_expr,
3087 shift_expr,
3088 arith_expr,
3089 term,
3090 factor,
3091 power,
3092 atom_expr,
3093 atom,
3094 0,
3095 };
3096 node *expr_node = chch;
3097 for (int i = 0; name_tree[i]; i++) {
3098 if (TYPE(expr_node) != name_tree[i])
3099 break;
3100 if (NCH(expr_node) != 1)
3101 break;
3102 expr_node = CHILD(expr_node, 0);
3103 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003104 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003105 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003106 "expression cannot contain assignment, "
3107 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003108 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003110 key = new_identifier(STR(expr_node), c);
3111 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003112 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003114 if (forbidden_name(c, key, chch, 1)) {
3115 return NULL;
3116 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003117 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003119 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003120 kw = keyword(key, e, chch->n_lineno, chch->n_col_offset,
Pablo Galindo40cf35c2020-04-03 21:02:26 +01003121 e->end_lineno, e->end_col_offset, c->c_arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01003122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003124 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003125 asdl_seq_SET(keywords, nkeywords++, kw);
3126 }
3127 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 }
3129
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003130 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003131 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132}
3133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003135ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003137 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003138 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003140 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003141 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003142 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003143 }
3144 else {
3145 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003146 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003147 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003149 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 else {
3151 asdl_seq *tmp = seq_for_testlist(c, n);
3152 if (!tmp)
3153 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003154 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3155 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003157}
3158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159static stmt_ty
3160ast_for_expr_stmt(struct compiling *c, const node *n)
3161{
3162 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003163 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003164 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3165 annassign: ':' test ['=' (yield_expr|testlist)]
3166 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3167 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3168 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003169 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003171 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003173 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003174 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 if (!e)
3176 return NULL;
3177
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003178 return Expr(e, LINENO(n), n->n_col_offset,
3179 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 }
3181 else if (TYPE(CHILD(n, 1)) == augassign) {
3182 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003183 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003184 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185
Thomas Wouters89f507f2006-12-13 04:49:30 +00003186 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187 if (!expr1)
3188 return NULL;
Pablo Galindo16ab0702020-05-15 02:04:52 +01003189 /* Augmented assignments can only have a name, a subscript, or an
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003190 attribute on the left, though, so we have to explicitly check for
3191 those. */
3192 switch (expr1->kind) {
3193 case Name_kind:
3194 case Attribute_kind:
3195 case Subscript_kind:
3196 break;
3197 default:
Pablo Galindo16ab0702020-05-15 02:04:52 +01003198 ast_error(c, ch, "'%s' is an illegal expression for augmented assignment",
3199 get_expr_name(expr1));
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003200 return NULL;
3201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
Pablo Galindo16ab0702020-05-15 02:04:52 +01003203 /* set_context checks that most expressions are not the left side. */
3204 if(!set_context(c, expr1, Store, ch)) {
3205 return NULL;
3206 }
3207
Thomas Wouters89f507f2006-12-13 04:49:30 +00003208 ch = CHILD(n, 2);
3209 if (TYPE(ch) == testlist)
3210 expr2 = ast_for_testlist(c, ch);
3211 else
3212 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003213 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 return NULL;
3215
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003216 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003217 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 return NULL;
3219
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003220 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3221 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003223 else if (TYPE(CHILD(n, 1)) == annassign) {
3224 expr_ty expr1, expr2, expr3;
3225 node *ch = CHILD(n, 0);
3226 node *deep, *ann = CHILD(n, 1);
3227 int simple = 1;
3228
Guido van Rossum495da292019-03-07 12:38:08 -08003229 /* AnnAssigns are only allowed in Python 3.6 or greater */
3230 if (c->c_feature_version < 6) {
3231 ast_error(c, ch,
3232 "Variable annotation syntax is only supported in Python 3.6 and greater");
3233 return NULL;
3234 }
3235
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003236 /* we keep track of parens to qualify (x) as expression not name */
3237 deep = ch;
3238 while (NCH(deep) == 1) {
3239 deep = CHILD(deep, 0);
3240 }
3241 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3242 simple = 0;
3243 }
3244 expr1 = ast_for_testlist(c, ch);
3245 if (!expr1) {
3246 return NULL;
3247 }
3248 switch (expr1->kind) {
3249 case Name_kind:
3250 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3251 return NULL;
3252 }
3253 expr1->v.Name.ctx = Store;
3254 break;
3255 case Attribute_kind:
3256 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3257 return NULL;
3258 }
3259 expr1->v.Attribute.ctx = Store;
3260 break;
3261 case Subscript_kind:
3262 expr1->v.Subscript.ctx = Store;
3263 break;
3264 case List_kind:
3265 ast_error(c, ch,
3266 "only single target (not list) can be annotated");
3267 return NULL;
3268 case Tuple_kind:
3269 ast_error(c, ch,
3270 "only single target (not tuple) can be annotated");
3271 return NULL;
3272 default:
3273 ast_error(c, ch,
3274 "illegal target for annotation");
3275 return NULL;
3276 }
3277
3278 if (expr1->kind != Name_kind) {
3279 simple = 0;
3280 }
3281 ch = CHILD(ann, 1);
3282 expr2 = ast_for_expr(c, ch);
3283 if (!expr2) {
3284 return NULL;
3285 }
3286 if (NCH(ann) == 2) {
3287 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003288 LINENO(n), n->n_col_offset,
3289 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003290 }
3291 else {
3292 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003293 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003294 expr3 = ast_for_testlist(c, ch);
3295 }
3296 else {
3297 expr3 = ast_for_expr(c, ch);
3298 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003299 if (!expr3) {
3300 return NULL;
3301 }
3302 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003303 LINENO(n), n->n_col_offset,
3304 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003305 }
3306 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003308 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 asdl_seq *targets;
3310 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003312 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 /* a normal assignment */
3315 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003316
3317 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3318 nch_minus_type = num - has_type_comment;
3319
3320 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 if (!targets)
3322 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003323 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324 expr_ty e;
3325 node *ch = CHILD(n, i);
3326 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003327 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003328 return NULL;
3329 }
3330 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003334 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003335 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 asdl_seq_SET(targets, i / 2, e);
3339 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003340 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003341 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 expression = ast_for_testlist(c, value);
3343 else
3344 expression = ast_for_expr(c, value);
3345 if (!expression)
3346 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003347 if (has_type_comment) {
3348 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3349 if (!type_comment)
3350 return NULL;
3351 }
3352 else
3353 type_comment = NULL;
3354 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003355 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357}
3358
Benjamin Peterson78565b22009-06-28 19:19:51 +00003359
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003361ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362{
3363 asdl_seq *seq;
3364 int i;
3365 expr_ty e;
3366
3367 REQ(n, exprlist);
3368
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003369 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003371 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 e = ast_for_expr(c, CHILD(n, i));
3374 if (!e)
3375 return NULL;
3376 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003377 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003378 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 }
3380 return seq;
3381}
3382
3383static stmt_ty
3384ast_for_del_stmt(struct compiling *c, const node *n)
3385{
3386 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 /* del_stmt: 'del' exprlist */
3389 REQ(n, del_stmt);
3390
3391 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3392 if (!expr_list)
3393 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003394 return Delete(expr_list, LINENO(n), n->n_col_offset,
3395 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396}
3397
3398static stmt_ty
3399ast_for_flow_stmt(struct compiling *c, const node *n)
3400{
3401 /*
3402 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3403 | yield_stmt
3404 break_stmt: 'break'
3405 continue_stmt: 'continue'
3406 return_stmt: 'return' [testlist]
3407 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003408 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409 raise_stmt: 'raise' [test [',' test [',' test]]]
3410 */
3411 node *ch;
3412
3413 REQ(n, flow_stmt);
3414 ch = CHILD(n, 0);
3415 switch (TYPE(ch)) {
3416 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003417 return Break(LINENO(n), n->n_col_offset,
3418 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003420 return Continue(LINENO(n), n->n_col_offset,
3421 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003423 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3424 if (!exp)
3425 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003426 return Expr(exp, LINENO(n), n->n_col_offset,
3427 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 }
3429 case return_stmt:
3430 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003431 return Return(NULL, LINENO(n), n->n_col_offset,
3432 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003434 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 if (!expression)
3436 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003437 return Return(expression, LINENO(n), n->n_col_offset,
3438 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 }
3440 case raise_stmt:
3441 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003442 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3443 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003444 else if (NCH(ch) >= 2) {
3445 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3447 if (!expression)
3448 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003449 if (NCH(ch) == 4) {
3450 cause = ast_for_expr(c, CHILD(ch, 3));
3451 if (!cause)
3452 return NULL;
3453 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003454 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3455 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 }
Stefan Krahf432a322017-08-21 13:09:59 +02003457 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003459 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 "unexpected flow_stmt: %d", TYPE(ch));
3461 return NULL;
3462 }
3463}
3464
3465static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003466alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467{
3468 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003469 import_as_name: NAME ['as' NAME]
3470 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 dotted_name: NAME ('.' NAME)*
3472 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003473 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003474
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 loop:
3476 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003477 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003478 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003479 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003480 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003481 if (!name)
3482 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003483 if (NCH(n) == 3) {
3484 node *str_node = CHILD(n, 2);
3485 str = NEW_IDENTIFIER(str_node);
3486 if (!str)
3487 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003488 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003489 return NULL;
3490 }
3491 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003492 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003493 return NULL;
3494 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003495 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 case dotted_as_name:
3498 if (NCH(n) == 1) {
3499 n = CHILD(n, 0);
3500 goto loop;
3501 }
3502 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003503 node *asname_node = CHILD(n, 2);
3504 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003505 if (!a)
3506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003508 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003509 if (!a->asname)
3510 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003511 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 return a;
3514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003516 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003517 node *name_node = CHILD(n, 0);
3518 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003519 if (!name)
3520 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003521 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003522 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003523 return alias(name, NULL, c->c_arena);
3524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 else {
3526 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003527 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003528 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531
3532 len = 0;
3533 for (i = 0; i < NCH(n); i += 2)
3534 /* length of string plus one for the dot */
3535 len += strlen(STR(CHILD(n, i))) + 1;
3536 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003537 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 if (!str)
3539 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003540 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541 if (!s)
3542 return NULL;
3543 for (i = 0; i < NCH(n); i += 2) {
3544 char *sch = STR(CHILD(n, i));
3545 strcpy(s, STR(CHILD(n, i)));
3546 s += strlen(sch);
3547 *s++ = '.';
3548 }
3549 --s;
3550 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3552 PyBytes_GET_SIZE(str),
3553 NULL);
3554 Py_DECREF(str);
3555 if (!uni)
3556 return NULL;
3557 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003558 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003559 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3560 Py_DECREF(str);
3561 return NULL;
3562 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003563 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003566 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003567 if (!str)
3568 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003569 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3570 Py_DECREF(str);
3571 return NULL;
3572 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003573 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003575 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 "unexpected import name: %d", TYPE(n));
3577 return NULL;
3578 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579}
3580
3581static stmt_ty
3582ast_for_import_stmt(struct compiling *c, const node *n)
3583{
3584 /*
3585 import_stmt: import_name | import_from
3586 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003587 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3588 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003590 int lineno;
3591 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 int i;
3593 asdl_seq *aliases;
3594
3595 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003596 lineno = LINENO(n);
3597 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003599 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003601 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003602 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 if (!aliases)
3604 return NULL;
3605 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003606 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003607 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003609 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003611 // Even though n is modified above, the end position is not changed
3612 return Import(aliases, lineno, col_offset,
3613 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003615 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003617 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003618 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003620 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003622 /* Count the number of dots (for relative imports) and check for the
3623 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003624 for (idx = 1; idx < NCH(n); idx++) {
3625 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003626 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3627 if (!mod)
3628 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003629 idx++;
3630 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003631 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003633 ndots += 3;
3634 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003635 } else if (TYPE(CHILD(n, idx)) != DOT) {
3636 break;
3637 }
3638 ndots++;
3639 }
3640 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003641 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003642 case STAR:
3643 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003644 n = CHILD(n, idx);
3645 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 break;
3647 case LPAR:
3648 /* from ... import (x, y, z) */
3649 n = CHILD(n, idx + 1);
3650 n_children = NCH(n);
3651 break;
3652 case import_as_names:
3653 /* from ... import x, y, z */
3654 n = CHILD(n, idx);
3655 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003656 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003657 ast_error(c, n,
3658 "trailing comma not allowed without"
3659 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 return NULL;
3661 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003662 break;
3663 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003664 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003665 return NULL;
3666 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003668 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003669 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671
3672 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003673 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003674 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003675 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003677 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003679 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003680 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003681 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003682 if (!import_alias)
3683 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003684 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003687 if (mod != NULL)
3688 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003689 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003690 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003691 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 }
Neal Norwitz79792652005-11-14 04:25:03 +00003693 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 "unknown import statement: starts with command '%s'",
3695 STR(CHILD(n, 0)));
3696 return NULL;
3697}
3698
3699static stmt_ty
3700ast_for_global_stmt(struct compiling *c, const node *n)
3701{
3702 /* global_stmt: 'global' NAME (',' NAME)* */
3703 identifier name;
3704 asdl_seq *s;
3705 int i;
3706
3707 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003708 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003712 name = NEW_IDENTIFIER(CHILD(n, i));
3713 if (!name)
3714 return NULL;
3715 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003717 return Global(s, LINENO(n), n->n_col_offset,
3718 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719}
3720
3721static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003722ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3723{
3724 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3725 identifier name;
3726 asdl_seq *s;
3727 int i;
3728
3729 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003730 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003731 if (!s)
3732 return NULL;
3733 for (i = 1; i < NCH(n); i += 2) {
3734 name = NEW_IDENTIFIER(CHILD(n, i));
3735 if (!name)
3736 return NULL;
3737 asdl_seq_SET(s, i / 2, name);
3738 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003739 return Nonlocal(s, LINENO(n), n->n_col_offset,
3740 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003741}
3742
3743static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744ast_for_assert_stmt(struct compiling *c, const node *n)
3745{
3746 /* assert_stmt: 'assert' test [',' test] */
3747 REQ(n, assert_stmt);
3748 if (NCH(n) == 2) {
3749 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3750 if (!expression)
3751 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003752 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3753 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 }
3755 else if (NCH(n) == 4) {
3756 expr_ty expr1, expr2;
3757
3758 expr1 = ast_for_expr(c, CHILD(n, 1));
3759 if (!expr1)
3760 return NULL;
3761 expr2 = ast_for_expr(c, CHILD(n, 3));
3762 if (!expr2)
3763 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003765 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3766 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 }
Neal Norwitz79792652005-11-14 04:25:03 +00003768 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 "improper number of parts to 'assert' statement: %d",
3770 NCH(n));
3771 return NULL;
3772}
3773
3774static asdl_seq *
3775ast_for_suite(struct compiling *c, const node *n)
3776{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003777 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003778 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 stmt_ty s;
3780 int i, total, num, end, pos = 0;
3781 node *ch;
3782
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003783 if (TYPE(n) != func_body_suite) {
3784 REQ(n, suite);
3785 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786
3787 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003788 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003790 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003792 n = CHILD(n, 0);
3793 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003795 */
3796 end = NCH(n) - 1;
3797 if (TYPE(CHILD(n, end - 1)) == SEMI)
3798 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003800 for (i = 0; i < end; i += 2) {
3801 ch = CHILD(n, i);
3802 s = ast_for_stmt(c, ch);
3803 if (!s)
3804 return NULL;
3805 asdl_seq_SET(seq, pos++, s);
3806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807 }
3808 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003809 i = 2;
3810 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3811 i += 2;
3812 REQ(CHILD(n, 2), NEWLINE);
3813 }
3814
3815 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003816 ch = CHILD(n, i);
3817 REQ(ch, stmt);
3818 num = num_stmts(ch);
3819 if (num == 1) {
3820 /* small_stmt or compound_stmt with only one child */
3821 s = ast_for_stmt(c, ch);
3822 if (!s)
3823 return NULL;
3824 asdl_seq_SET(seq, pos++, s);
3825 }
3826 else {
3827 int j;
3828 ch = CHILD(ch, 0);
3829 REQ(ch, simple_stmt);
3830 for (j = 0; j < NCH(ch); j += 2) {
3831 /* statement terminates with a semi-colon ';' */
3832 if (NCH(CHILD(ch, j)) == 0) {
3833 assert((j + 1) == NCH(ch));
3834 break;
3835 }
3836 s = ast_for_stmt(c, CHILD(ch, j));
3837 if (!s)
3838 return NULL;
3839 asdl_seq_SET(seq, pos++, s);
3840 }
3841 }
3842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 }
3844 assert(pos == seq->size);
3845 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846}
3847
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003848static void
3849get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3850{
Pablo Galindo46a97922019-02-19 22:51:53 +00003851 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003852 // There must be no empty suites.
3853 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003854 stmt_ty last = asdl_seq_GET(s, tot - 1);
3855 *end_lineno = last->end_lineno;
3856 *end_col_offset = last->end_col_offset;
3857}
3858
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859static stmt_ty
3860ast_for_if_stmt(struct compiling *c, const node *n)
3861{
3862 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3863 ['else' ':' suite]
3864 */
3865 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003866 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867
3868 REQ(n, if_stmt);
3869
3870 if (NCH(n) == 4) {
3871 expr_ty expression;
3872 asdl_seq *suite_seq;
3873
3874 expression = ast_for_expr(c, CHILD(n, 1));
3875 if (!expression)
3876 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003878 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003880 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881
Guido van Rossumd8faa362007-04-27 19:54:29 +00003882 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003883 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 s = STR(CHILD(n, 4));
3887 /* s[2], the third character in the string, will be
3888 's' for el_s_e, or
3889 'i' for el_i_f
3890 */
3891 if (s[2] == 's') {
3892 expr_ty expression;
3893 asdl_seq *seq1, *seq2;
3894
3895 expression = ast_for_expr(c, CHILD(n, 1));
3896 if (!expression)
3897 return NULL;
3898 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003899 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 return NULL;
3901 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003902 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003904 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905
Guido van Rossumd8faa362007-04-27 19:54:29 +00003906 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003907 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 }
3909 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003910 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003911 expr_ty expression;
3912 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003913 asdl_seq *orelse = NULL;
3914 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915 /* must reference the child n_elif+1 since 'else' token is third,
3916 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003917 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3918 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3919 has_else = 1;
3920 n_elif -= 3;
3921 }
3922 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923
Thomas Wouters89f507f2006-12-13 04:49:30 +00003924 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003925 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003927 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 if (!orelse)
3929 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003931 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003933 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3934 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003936 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3937 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003939 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 asdl_seq_SET(orelse, 0,
3942 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003943 LINENO(CHILD(n, NCH(n) - 7)),
3944 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003945 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003946 /* the just-created orelse handled the last elif */
3947 n_elif--;
3948 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949
Thomas Wouters89f507f2006-12-13 04:49:30 +00003950 for (i = 0; i < n_elif; i++) {
3951 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003952 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003953 if (!newobj)
3954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003956 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003959 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003962 if (orelse != NULL) {
3963 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3964 } else {
3965 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3966 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003967 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003969 LINENO(CHILD(n, off - 1)),
3970 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003971 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003972 orelse = newobj;
3973 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003974 expression = ast_for_expr(c, CHILD(n, 1));
3975 if (!expression)
3976 return NULL;
3977 suite_seq = ast_for_suite(c, CHILD(n, 3));
3978 if (!suite_seq)
3979 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003980 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003981 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003982 LINENO(n), n->n_col_offset,
3983 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003985
3986 PyErr_Format(PyExc_SystemError,
3987 "unexpected token in 'if' statement: %s", s);
3988 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989}
3990
3991static stmt_ty
3992ast_for_while_stmt(struct compiling *c, const node *n)
3993{
3994 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3995 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003996 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997
3998 if (NCH(n) == 4) {
3999 expr_ty expression;
4000 asdl_seq *suite_seq;
4001
4002 expression = ast_for_expr(c, CHILD(n, 1));
4003 if (!expression)
4004 return NULL;
4005 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004006 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004008 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4009 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4010 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 }
4012 else if (NCH(n) == 7) {
4013 expr_ty expression;
4014 asdl_seq *seq1, *seq2;
4015
4016 expression = ast_for_expr(c, CHILD(n, 1));
4017 if (!expression)
4018 return NULL;
4019 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004020 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021 return NULL;
4022 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004023 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004025 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004027 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4028 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004030
4031 PyErr_Format(PyExc_SystemError,
4032 "wrong number of tokens for 'while' statement: %d",
4033 NCH(n));
4034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035}
4036
4037static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004038ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039{
guoci90fc8982018-09-11 17:45:45 -04004040 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004041 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004043 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004044 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004045 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004046 int has_type_comment;
4047 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004048
4049 if (is_async && c->c_feature_version < 5) {
4050 ast_error(c, n,
4051 "Async for loops are only supported in Python 3.5 and greater");
4052 return NULL;
4053 }
4054
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004055 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056 REQ(n, for_stmt);
4057
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004058 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4059
4060 if (NCH(n) == 9 + has_type_comment) {
4061 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 if (!seq)
4063 return NULL;
4064 }
4065
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004066 node_target = CHILD(n, 1);
4067 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004068 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004070 /* Check the # of children rather than the length of _target, since
4071 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004072 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004073 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004074 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004076 target = Tuple(_target, Store, first->lineno, first->col_offset,
4077 node_target->n_end_lineno, node_target->n_end_col_offset,
4078 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004080 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004081 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004083 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004084 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085 return NULL;
4086
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004087 if (seq != NULL) {
4088 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4089 } else {
4090 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4091 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004092
4093 if (has_type_comment) {
4094 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4095 if (!type_comment)
4096 return NULL;
4097 }
4098 else
4099 type_comment = NULL;
4100
Yury Selivanov75445082015-05-11 22:57:16 -04004101 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004102 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004103 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004104 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004105 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004106 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004107 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004108 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109}
4110
4111static excepthandler_ty
4112ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4113{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004114 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004115 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 REQ(exc, except_clause);
4117 REQ(body, suite);
4118
4119 if (NCH(exc) == 1) {
4120 asdl_seq *suite_seq = ast_for_suite(c, body);
4121 if (!suite_seq)
4122 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004123 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124
Neal Norwitzad74aa82008-03-31 05:14:30 +00004125 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004126 exc->n_col_offset,
4127 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 }
4129 else if (NCH(exc) == 2) {
4130 expr_ty expression;
4131 asdl_seq *suite_seq;
4132
4133 expression = ast_for_expr(c, CHILD(exc, 1));
4134 if (!expression)
4135 return NULL;
4136 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004137 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004139 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140
Neal Norwitzad74aa82008-03-31 05:14:30 +00004141 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004142 exc->n_col_offset,
4143 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144 }
4145 else if (NCH(exc) == 4) {
4146 asdl_seq *suite_seq;
4147 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004148 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004149 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004151 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004154 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155 return NULL;
4156 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004157 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004159 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004160
Neal Norwitzad74aa82008-03-31 05:14:30 +00004161 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004162 exc->n_col_offset,
4163 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004164 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004165
4166 PyErr_Format(PyExc_SystemError,
4167 "wrong number of children for 'except' clause: %d",
4168 NCH(exc));
4169 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004170}
4171
4172static stmt_ty
4173ast_for_try_stmt(struct compiling *c, const node *n)
4174{
Neal Norwitzf599f422005-12-17 21:33:47 +00004175 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004176 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004177 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004178 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004180 REQ(n, try_stmt);
4181
Neal Norwitzf599f422005-12-17 21:33:47 +00004182 body = ast_for_suite(c, CHILD(n, 2));
4183 if (body == NULL)
4184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185
Neal Norwitzf599f422005-12-17 21:33:47 +00004186 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4187 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4188 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4189 /* we can assume it's an "else",
4190 because nch >= 9 for try-else-finally and
4191 it would otherwise have a type of except_clause */
4192 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4193 if (orelse == NULL)
4194 return NULL;
4195 n_except--;
4196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004197
Neal Norwitzf599f422005-12-17 21:33:47 +00004198 finally = ast_for_suite(c, CHILD(n, nch - 1));
4199 if (finally == NULL)
4200 return NULL;
4201 n_except--;
4202 }
4203 else {
4204 /* we can assume it's an "else",
4205 otherwise it would have a type of except_clause */
4206 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4207 if (orelse == NULL)
4208 return NULL;
4209 n_except--;
4210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004211 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004212 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004213 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004214 return NULL;
4215 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216
Neal Norwitzf599f422005-12-17 21:33:47 +00004217 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004218 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004219 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004220 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004221 if (handlers == NULL)
4222 return NULL;
4223
4224 for (i = 0; i < n_except; i++) {
4225 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4226 CHILD(n, 5 + i * 3));
4227 if (!e)
4228 return NULL;
4229 asdl_seq_SET(handlers, i, e);
4230 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004231 }
4232
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004233 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004234 if (finally != NULL) {
4235 // finally is always last
4236 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4237 } else if (orelse != NULL) {
4238 // otherwise else is last
4239 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4240 } else {
4241 // inline the get_last_end_pos logic due to layout mismatch
4242 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4243 end_lineno = last_handler->end_lineno;
4244 end_col_offset = last_handler->end_col_offset;
4245 }
4246 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4247 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004248}
4249
Georg Brandl0c315622009-05-25 21:10:36 +00004250/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004251static withitem_ty
4252ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004253{
4254 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004255
Georg Brandl0c315622009-05-25 21:10:36 +00004256 REQ(n, with_item);
4257 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004258 if (!context_expr)
4259 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004260 if (NCH(n) == 3) {
4261 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004262
4263 if (!optional_vars) {
4264 return NULL;
4265 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004266 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004267 return NULL;
4268 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004269 }
4270
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004271 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004272}
4273
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004274/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004275static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004276ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004277{
guoci90fc8982018-09-11 17:45:45 -04004278 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004279 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004280 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004281 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004282
Guido van Rossum495da292019-03-07 12:38:08 -08004283 if (is_async && c->c_feature_version < 5) {
4284 ast_error(c, n,
4285 "Async with statements are only supported in Python 3.5 and greater");
4286 return NULL;
4287 }
4288
Georg Brandl0c315622009-05-25 21:10:36 +00004289 REQ(n, with_stmt);
4290
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004291 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4292 nch_minus_type = NCH(n) - has_type_comment;
4293
4294 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004295 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004296 if (!items)
4297 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004298 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004299 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4300 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004301 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004302 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004303 }
4304
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004305 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4306 if (!body)
4307 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004308 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004309
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004310 if (has_type_comment) {
4311 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4312 if (!type_comment)
4313 return NULL;
4314 }
4315 else
4316 type_comment = NULL;
4317
Yury Selivanov75445082015-05-11 22:57:16 -04004318 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004319 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004320 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004321 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004322 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004323 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004324}
4325
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004326static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004327ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004328{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004329 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004330 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004331 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004332 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004333 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004335 REQ(n, classdef);
4336
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004337 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004338 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004339 if (!s)
4340 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004341 get_last_end_pos(s, &end_lineno, &end_col_offset);
4342
Benjamin Peterson30760062008-11-25 04:02:28 +00004343 classname = NEW_IDENTIFIER(CHILD(n, 1));
4344 if (!classname)
4345 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004346 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004347 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004348 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004349 LINENO(n), n->n_col_offset,
4350 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004351 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004352
4353 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004354 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004355 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004356 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004357 get_last_end_pos(s, &end_lineno, &end_col_offset);
4358
Benjamin Peterson30760062008-11-25 04:02:28 +00004359 classname = NEW_IDENTIFIER(CHILD(n, 1));
4360 if (!classname)
4361 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004362 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004363 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004364 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004365 LINENO(n), n->n_col_offset,
4366 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004367 }
4368
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004369 /* class NAME '(' arglist ')' ':' suite */
4370 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004371 {
4372 PyObject *dummy_name;
4373 expr_ty dummy;
4374 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4375 if (!dummy_name)
4376 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004377 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4378 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4379 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004380 call = ast_for_call(c, CHILD(n, 3), dummy,
4381 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004382 if (!call)
4383 return NULL;
4384 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004385 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004386 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004387 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004388 get_last_end_pos(s, &end_lineno, &end_col_offset);
4389
Benjamin Peterson30760062008-11-25 04:02:28 +00004390 classname = NEW_IDENTIFIER(CHILD(n, 1));
4391 if (!classname)
4392 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004393 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004394 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004395
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004396 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004397 decorator_seq, LINENO(n), n->n_col_offset,
4398 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004399}
4400
4401static stmt_ty
4402ast_for_stmt(struct compiling *c, const node *n)
4403{
4404 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004405 assert(NCH(n) == 1);
4406 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004407 }
4408 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004409 assert(num_stmts(n) == 1);
4410 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411 }
4412 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004413 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004414 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4415 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004416 */
4417 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418 case expr_stmt:
4419 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004420 case del_stmt:
4421 return ast_for_del_stmt(c, n);
4422 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004423 return Pass(LINENO(n), n->n_col_offset,
4424 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004425 case flow_stmt:
4426 return ast_for_flow_stmt(c, n);
4427 case import_stmt:
4428 return ast_for_import_stmt(c, n);
4429 case global_stmt:
4430 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004431 case nonlocal_stmt:
4432 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433 case assert_stmt:
4434 return ast_for_assert_stmt(c, n);
4435 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004436 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004437 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4438 TYPE(n), NCH(n));
4439 return NULL;
4440 }
4441 }
4442 else {
4443 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004444 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004445 */
4446 node *ch = CHILD(n, 0);
4447 REQ(n, compound_stmt);
4448 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004449 case if_stmt:
4450 return ast_for_if_stmt(c, ch);
4451 case while_stmt:
4452 return ast_for_while_stmt(c, ch);
4453 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004454 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004455 case try_stmt:
4456 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004457 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004458 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004460 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004462 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 case decorated:
4464 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004465 case async_stmt:
4466 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004467 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004468 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004469 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004470 TYPE(n), NCH(n));
4471 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004473 }
4474}
4475
4476static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004477parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004478{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004479 const char *end;
4480 long x;
4481 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004482 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004483 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004484
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004485 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004486 errno = 0;
4487 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004488 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004489 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004490 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004491 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004492 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004493 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 }
4495 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004496 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004497 if (*end == '\0') {
4498 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004499 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004500 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004501 }
4502 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004503 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004504 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004505 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4506 if (compl.imag == -1.0 && PyErr_Occurred())
4507 return NULL;
4508 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004509 }
4510 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004511 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004512 dx = PyOS_string_to_double(s, NULL, NULL);
4513 if (dx == -1.0 && PyErr_Occurred())
4514 return NULL;
4515 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004516 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004517}
4518
4519static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004520parsenumber(struct compiling *c, const char *s)
4521{
4522 char *dup, *end;
4523 PyObject *res = NULL;
4524
4525 assert(s != NULL);
4526
4527 if (strchr(s, '_') == NULL) {
4528 return parsenumber_raw(c, s);
4529 }
4530 /* Create a duplicate without underscores. */
4531 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004532 if (dup == NULL) {
4533 return PyErr_NoMemory();
4534 }
Brett Cannona721aba2016-09-09 14:57:09 -07004535 end = dup;
4536 for (; *s; s++) {
4537 if (*s != '_') {
4538 *end++ = *s;
4539 }
4540 }
4541 *end = '\0';
4542 res = parsenumber_raw(c, dup);
4543 PyMem_Free(dup);
4544 return res;
4545}
4546
4547static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004548decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004549{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004550 const char *s, *t;
4551 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004552 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4553 while (s < end && (*s & 0x80)) s++;
4554 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004555 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004556}
4557
Eric V. Smith56466482016-10-31 14:46:26 -04004558static int
4559warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004560 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004561{
4562 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4563 first_invalid_escape_char);
4564 if (msg == NULL) {
4565 return -1;
4566 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004567 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004568 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004569 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004570 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004571 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4572 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004573 to get a more accurate error report */
4574 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004575 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004576 }
4577 Py_DECREF(msg);
4578 return -1;
4579 }
4580 Py_DECREF(msg);
4581 return 0;
4582}
4583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004584static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004585decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4586 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004587{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004588 PyObject *v, *u;
4589 char *buf;
4590 char *p;
4591 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004592
Benjamin Peterson202803a2016-02-25 22:34:45 -08004593 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004594 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004595 return NULL;
4596 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4597 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4598 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4599 if (u == NULL)
4600 return NULL;
4601 p = buf = PyBytes_AsString(u);
4602 end = s + len;
4603 while (s < end) {
4604 if (*s == '\\') {
4605 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004606 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004607 strcpy(p, "u005c");
4608 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004609 if (s >= end)
4610 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004611 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004612 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004613 if (*s & 0x80) { /* XXX inefficient */
4614 PyObject *w;
4615 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03004616 const void *data;
Benjamin Peterson202803a2016-02-25 22:34:45 -08004617 Py_ssize_t len, i;
4618 w = decode_utf8(c, &s, end);
4619 if (w == NULL) {
4620 Py_DECREF(u);
4621 return NULL;
4622 }
4623 kind = PyUnicode_KIND(w);
4624 data = PyUnicode_DATA(w);
4625 len = PyUnicode_GET_LENGTH(w);
4626 for (i = 0; i < len; i++) {
4627 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4628 sprintf(p, "\\U%08x", chr);
4629 p += 10;
4630 }
4631 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004632 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004633 Py_DECREF(w);
4634 } else {
4635 *p++ = *s++;
4636 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004637 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004638 len = p - buf;
4639 s = buf;
4640
Eric V. Smith56466482016-10-31 14:46:26 -04004641 const char *first_invalid_escape;
4642 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4643
4644 if (v != NULL && first_invalid_escape != NULL) {
4645 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4646 /* We have not decref u before because first_invalid_escape points
4647 inside u. */
4648 Py_XDECREF(u);
4649 Py_DECREF(v);
4650 return NULL;
4651 }
4652 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004653 Py_XDECREF(u);
4654 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004655}
4656
Eric V. Smith56466482016-10-31 14:46:26 -04004657static PyObject *
4658decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4659 size_t len)
4660{
4661 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004662 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004663 &first_invalid_escape);
4664 if (result == NULL)
4665 return NULL;
4666
4667 if (first_invalid_escape != NULL) {
4668 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4669 Py_DECREF(result);
4670 return NULL;
4671 }
4672 }
4673 return result;
4674}
4675
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004676/* Shift locations for the given node and all its children by adding `lineno`
4677 and `col_offset` to existing locations. */
4678static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4679{
4680 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004681 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004682 for (int i = 0; i < NCH(n); ++i) {
4683 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4684 /* Shifting column offsets unnecessary if there's been newlines. */
4685 col_offset = 0;
4686 }
4687 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4688 }
4689 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004690 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004691}
4692
4693/* Fix locations for the given node and its children.
4694
4695 `parent` is the enclosing node.
4696 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004697 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004698*/
4699static void
4700fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4701{
4702 char *substr = NULL;
4703 char *start;
4704 int lines = LINENO(parent) - 1;
4705 int cols = parent->n_col_offset;
4706 /* Find the full fstring to fix location information in `n`. */
4707 while (parent && parent->n_type != STRING)
4708 parent = parent->n_child;
4709 if (parent && parent->n_str) {
4710 substr = strstr(parent->n_str, expr_str);
4711 if (substr) {
4712 start = substr;
4713 while (start > parent->n_str) {
4714 if (start[0] == '\n')
4715 break;
4716 start--;
4717 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004718 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004719 /* adjust the start based on the number of newlines encountered
4720 before the f-string expression */
4721 for (char* p = parent->n_str; p < substr; p++) {
4722 if (*p == '\n') {
4723 lines++;
4724 }
4725 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004726 }
4727 }
4728 fstring_shift_node_locations(n, lines, cols);
4729}
4730
Eric V. Smith451d0e32016-09-09 21:56:20 -04004731/* Compile this expression in to an expr_ty. Add parens around the
4732 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004733static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004734fstring_compile_expr(const char *expr_start, const char *expr_end,
4735 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004736
Eric V. Smith235a6f02015-09-19 14:51:32 -04004737{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004738 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004739 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004740 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004742 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004743
Eric V. Smith1d44c412015-09-23 07:49:00 -04004744 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004745 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004746 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4747 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004748
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004749 /* If the substring is all whitespace, it's an error. We need to catch this
4750 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4751 because turning the expression '' in to '()' would go from being invalid
4752 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004753 for (s = expr_start; s != expr_end; s++) {
4754 char c = *s;
4755 /* The Python parser ignores only the following whitespace
4756 characters (\r already is converted to \n). */
4757 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004758 break;
4759 }
4760 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004761 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004762 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004763 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004764 }
4765
Eric V. Smith451d0e32016-09-09 21:56:20 -04004766 len = expr_end - expr_start;
4767 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03004768 str = PyMem_Malloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004769 if (str == NULL) {
4770 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004771 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004772 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004773
Eric V. Smith451d0e32016-09-09 21:56:20 -04004774 str[0] = '(';
4775 memcpy(str+1, expr_start, len);
4776 str[len+1] = ')';
4777 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004778
Victor Stinner37d66d72019-06-13 02:16:41 +02004779 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004780 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004781 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4782 Py_eval_input, 0);
4783 if (!mod_n) {
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03004784 PyMem_Free(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004785 return NULL;
4786 }
4787 /* Reuse str to find the correct column offset. */
4788 str[0] = '{';
4789 str[len+1] = '}';
4790 fstring_fix_node_location(n, mod_n, str);
4791 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03004792 PyMem_Free(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004793 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004794 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004795 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004796 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004797}
4798
4799/* Return -1 on error.
4800
4801 Return 0 if we reached the end of the literal.
4802
4803 Return 1 if we haven't reached the end of the literal, but we want
4804 the caller to process the literal up to this point. Used for
4805 doubled braces.
4806*/
4807static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004808fstring_find_literal(const char **str, const char *end, int raw,
4809 PyObject **literal, int recurse_lvl,
4810 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004811{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004812 /* Get any literal string. It ends when we hit an un-doubled left
4813 brace (which isn't part of a unicode name escape such as
4814 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004815
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004816 const char *s = *str;
4817 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004818 int result = 0;
4819
Eric V. Smith235a6f02015-09-19 14:51:32 -04004820 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004821 while (s < end) {
4822 char ch = *s++;
4823 if (!raw && ch == '\\' && s < end) {
4824 ch = *s++;
4825 if (ch == 'N') {
4826 if (s < end && *s++ == '{') {
4827 while (s < end && *s++ != '}') {
4828 }
4829 continue;
4830 }
4831 break;
4832 }
4833 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4834 return -1;
4835 }
4836 }
4837 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004838 /* Check for doubled braces, but only at the top level. If
4839 we checked at every level, then f'{0:{3}}' would fail
4840 with the two closing braces. */
4841 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004842 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843 /* We're going to tell the caller that the literal ends
4844 here, but that they should continue scanning. But also
4845 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004846 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004847 result = 1;
4848 goto done;
4849 }
4850
4851 /* Where a single '{' is the start of a new expression, a
4852 single '}' is not allowed. */
4853 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004854 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004855 ast_error(c, n, "f-string: single '}' is not allowed");
4856 return -1;
4857 }
4858 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004859 /* We're either at a '{', which means we're starting another
4860 expression; or a '}', which means we're at the end of this
4861 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004862 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004863 break;
4864 }
4865 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004866 *str = s;
4867 assert(s <= end);
4868 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004869done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004870 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004871 if (raw)
4872 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004873 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004874 NULL, NULL);
4875 else
Eric V. Smith56466482016-10-31 14:46:26 -04004876 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004877 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004878 if (!*literal)
4879 return -1;
4880 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004881 return result;
4882}
4883
4884/* Forward declaration because parsing is recursive. */
4885static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004886fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004887 struct compiling *c, const node *n);
4888
Eric V. Smith451d0e32016-09-09 21:56:20 -04004889/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004890 expression (so it must be a '{'). Returns the FormattedValue node, which
4891 includes the expression, conversion character, format_spec expression, and
4892 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004893
4894 Note that I don't do a perfect job here: I don't make sure that a
4895 closing brace doesn't match an opening paren, for example. It
4896 doesn't need to error on all invalid expressions, just correctly
4897 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004898 will be caught when we parse it later.
4899
4900 *expression is set to the expression. For an '=' "debug" expression,
4901 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004902 including the '=' and any whitespace around it, as a string object). If
4903 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004906 PyObject **expr_text, expr_ty *expression,
4907 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908{
4909 /* Return -1 on error, else 0. */
4910
Eric V. Smith451d0e32016-09-09 21:56:20 -04004911 const char *expr_start;
4912 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004913 expr_ty simple_expression;
4914 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004915 int conversion = -1; /* The conversion char. Use default if not
4916 specified, or !r if using = and no format
4917 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918
4919 /* 0 if we're not in a string, else the quote char we're trying to
4920 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004921 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004922
4923 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4924 int string_type = 0;
4925
4926 /* Keep track of nesting level for braces/parens/brackets in
4927 expressions. */
4928 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004929 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004930
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004931 *expr_text = NULL;
4932
Eric V. Smith235a6f02015-09-19 14:51:32 -04004933 /* Can only nest one level deep. */
4934 if (recurse_lvl >= 2) {
4935 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004936 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004937 }
4938
4939 /* The first char must be a left brace, or we wouldn't have gotten
4940 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004941 assert(**str == '{');
4942 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004943
Eric V. Smith451d0e32016-09-09 21:56:20 -04004944 expr_start = *str;
4945 for (; *str < end; (*str)++) {
4946 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947
4948 /* Loop invariants. */
4949 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004950 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004951 if (quote_char)
4952 assert(string_type == 1 || string_type == 3);
4953 else
4954 assert(string_type == 0);
4955
Eric V. Smith451d0e32016-09-09 21:56:20 -04004956 ch = **str;
4957 /* Nowhere inside an expression is a backslash allowed. */
4958 if (ch == '\\') {
4959 /* Error: can't include a backslash character, inside
4960 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004961 ast_error(c, n,
4962 "f-string expression part "
4963 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004964 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004965 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 if (quote_char) {
4967 /* We're inside a string. See if we're at the end. */
4968 /* This code needs to implement the same non-error logic
4969 as tok_get from tokenizer.c, at the letter_quote
4970 label. To actually share that code would be a
4971 nightmare. But, it's unlikely to change and is small,
4972 so duplicate it here. Note we don't need to catch all
4973 of the errors, since they'll be caught when parsing the
4974 expression. We just need to match the non-error
4975 cases. Thus we can ignore \n in single-quoted strings,
4976 for example. Or non-terminated strings. */
4977 if (ch == quote_char) {
4978 /* Does this match the string_type (single or triple
4979 quoted)? */
4980 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004981 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 string_type = 0;
4985 quote_char = 0;
4986 continue;
4987 }
4988 } else {
4989 /* We're at the end of a normal string. */
4990 quote_char = 0;
4991 string_type = 0;
4992 continue;
4993 }
4994 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 } else if (ch == '\'' || ch == '"') {
4996 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004997 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004998 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005000 } else {
5001 /* Start of a normal string. */
5002 string_type = 1;
5003 }
5004 /* Start looking for the end of the string. */
5005 quote_char = ch;
5006 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005007 if (nested_depth >= MAXLEVEL) {
5008 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005009 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005010 }
5011 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005012 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 } else if (ch == '#') {
5014 /* Error: can't include a comment character, inside parens
5015 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005016 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005017 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005018 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005019 (ch == '!' || ch == ':' || ch == '}' ||
5020 ch == '=' || ch == '>' || ch == '<')) {
5021 /* See if there's a next character. */
5022 if (*str+1 < end) {
5023 char next = *(*str+1);
5024
5025 /* For "!=". since '=' is not an allowed conversion character,
5026 nothing is lost in this test. */
5027 if ((ch == '!' && next == '=') || /* != */
5028 (ch == '=' && next == '=') || /* == */
5029 (ch == '<' && next == '=') || /* <= */
5030 (ch == '>' && next == '=') /* >= */
5031 ) {
5032 *str += 1;
5033 continue;
5034 }
5035 /* Don't get out of the loop for these, if they're single
5036 chars (not part of 2-char tokens). If by themselves, they
5037 don't end an expression (unlike say '!'). */
5038 if (ch == '>' || ch == '<') {
5039 continue;
5040 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005041 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005042
Eric V. Smith235a6f02015-09-19 14:51:32 -04005043 /* Normal way out of this loop. */
5044 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005045 } else if (ch == ']' || ch == '}' || ch == ')') {
5046 if (!nested_depth) {
5047 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005048 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005049 }
5050 nested_depth--;
5051 int opening = parenstack[nested_depth];
5052 if (!((opening == '(' && ch == ')') ||
5053 (opening == '[' && ch == ']') ||
5054 (opening == '{' && ch == '}')))
5055 {
5056 ast_error(c, n,
5057 "f-string: closing parenthesis '%c' "
5058 "does not match opening parenthesis '%c'",
5059 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005060 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005061 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 } else {
5063 /* Just consume this char and loop around. */
5064 }
5065 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005066 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005067 /* If we leave this loop in a string or with mismatched parens, we
5068 don't care. We'll get a syntax error when compiling the
5069 expression. But, we can produce a better error message, so
5070 let's just do that.*/
5071 if (quote_char) {
5072 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005073 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005074 }
5075 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005076 int opening = parenstack[nested_depth - 1];
5077 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005078 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079 }
5080
Eric V. Smith451d0e32016-09-09 21:56:20 -04005081 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005083
5084 /* Compile the expression as soon as possible, so we show errors
5085 related to the expression before errors related to the
5086 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005087 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005088 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005089 goto error;
5090
5091 /* Check for =, which puts the text value of the expression in
5092 expr_text. */
5093 if (**str == '=') {
Pablo Galindo9b838292020-05-27 22:01:11 +01005094 if (c->c_feature_version < 8) {
5095 ast_error(c, n,
5096 "f-string: self documenting expressions are "
5097 "only supported in Python 3.8 and greater");
5098 goto error;
5099 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005100 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005101
5102 /* Skip over ASCII whitespace. No need to test for end of string
5103 here, since we know there's at least a trailing quote somewhere
5104 ahead. */
5105 while (Py_ISSPACE(**str)) {
5106 *str += 1;
5107 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005108
5109 /* Set *expr_text to the text of the expression. */
5110 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5111 if (!*expr_text) {
5112 goto error;
5113 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005114 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005115
5116 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005117 if (**str == '!') {
5118 *str += 1;
5119 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005120 goto unexpected_end_of_string;
5121
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 conversion = **str;
5123 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124
5125 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005126 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005127 ast_error(c, n,
5128 "f-string: invalid conversion character: "
5129 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005130 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005132
5133 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134
5135 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005136 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005137 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 if (**str == ':') {
5139 *str += 1;
5140 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005141 goto unexpected_end_of_string;
5142
5143 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005144 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005146 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 }
5148
Eric V. Smith451d0e32016-09-09 21:56:20 -04005149 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005150 goto unexpected_end_of_string;
5151
5152 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005153 assert(*str < end);
5154 assert(**str == '}');
5155 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005156
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005157 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005158 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005159 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005160 conversion = 'r';
5161 }
5162
Eric V. Smith451d0e32016-09-09 21:56:20 -04005163 /* And now create the FormattedValue node that represents this
5164 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005165 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005166 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005167 n->n_col_offset, n->n_end_lineno,
5168 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005169 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005170 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005171
5172 return 0;
5173
5174unexpected_end_of_string:
5175 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005176 /* Falls through to error. */
5177
5178error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005179 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005180 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005181
Eric V. Smith235a6f02015-09-19 14:51:32 -04005182}
5183
5184/* Return -1 on error.
5185
5186 Return 0 if we have a literal (possible zero length) and an
5187 expression (zero length if at the end of the string.
5188
5189 Return 1 if we have a literal, but no expression, and we want the
5190 caller to call us again. This is used to deal with doubled
5191 braces.
5192
5193 When called multiple times on the string 'a{{b{0}c', this function
5194 will return:
5195
5196 1. the literal 'a{' with no expression, and a return value
5197 of 1. Despite the fact that there's no expression, the return
5198 value of 1 means we're not finished yet.
5199
5200 2. the literal 'b' and the expression '0', with a return value of
5201 0. The fact that there's an expression means we're not finished.
5202
5203 3. literal 'c' with no expression and a return value of 0. The
5204 combination of the return value of 0 with no expression means
5205 we're finished.
5206*/
5207static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005208fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5209 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005210 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005211 struct compiling *c, const node *n)
5212{
5213 int result;
5214
5215 assert(*literal == NULL && *expression == NULL);
5216
5217 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005218 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005219 if (result < 0)
5220 goto error;
5221
5222 assert(result == 0 || result == 1);
5223
5224 if (result == 1)
5225 /* We have a literal, but don't look at the expression. */
5226 return 1;
5227
Eric V. Smith451d0e32016-09-09 21:56:20 -04005228 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229 /* We're at the end of the string or the end of a nested
5230 f-string: no expression. The top-level error case where we
5231 expect to be at the end of the string but we're at a '}' is
5232 handled later. */
5233 return 0;
5234
5235 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005237
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005238 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5239 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005240 goto error;
5241
5242 return 0;
5243
5244error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005245 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005246 return -1;
5247}
5248
5249#define EXPRLIST_N_CACHED 64
5250
5251typedef struct {
5252 /* Incrementally build an array of expr_ty, so be used in an
5253 asdl_seq. Cache some small but reasonably sized number of
5254 expr_ty's, and then after that start dynamically allocating,
5255 doubling the number allocated each time. Note that the f-string
5256 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005257 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005258 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005259
5260 Py_ssize_t allocated; /* Number we've allocated. */
5261 Py_ssize_t size; /* Number we've used. */
5262 expr_ty *p; /* Pointer to the memory we're actually
5263 using. Will point to 'data' until we
5264 start dynamically allocating. */
5265 expr_ty data[EXPRLIST_N_CACHED];
5266} ExprList;
5267
5268#ifdef NDEBUG
5269#define ExprList_check_invariants(l)
5270#else
5271static void
5272ExprList_check_invariants(ExprList *l)
5273{
5274 /* Check our invariants. Make sure this object is "live", and
5275 hasn't been deallocated. */
5276 assert(l->size >= 0);
5277 assert(l->p != NULL);
5278 if (l->size <= EXPRLIST_N_CACHED)
5279 assert(l->data == l->p);
5280}
5281#endif
5282
5283static void
5284ExprList_Init(ExprList *l)
5285{
5286 l->allocated = EXPRLIST_N_CACHED;
5287 l->size = 0;
5288
5289 /* Until we start allocating dynamically, p points to data. */
5290 l->p = l->data;
5291
5292 ExprList_check_invariants(l);
5293}
5294
5295static int
5296ExprList_Append(ExprList *l, expr_ty exp)
5297{
5298 ExprList_check_invariants(l);
5299 if (l->size >= l->allocated) {
5300 /* We need to alloc (or realloc) the memory. */
5301 Py_ssize_t new_size = l->allocated * 2;
5302
5303 /* See if we've ever allocated anything dynamically. */
5304 if (l->p == l->data) {
5305 Py_ssize_t i;
5306 /* We're still using the cached data. Switch to
5307 alloc-ing. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005308 l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005309 if (!l->p)
5310 return -1;
5311 /* Copy the cached data into the new buffer. */
5312 for (i = 0; i < l->size; i++)
5313 l->p[i] = l->data[i];
5314 } else {
5315 /* Just realloc. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005316 expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005317 if (!tmp) {
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005318 PyMem_Free(l->p);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005319 l->p = NULL;
5320 return -1;
5321 }
5322 l->p = tmp;
5323 }
5324
5325 l->allocated = new_size;
5326 assert(l->allocated == 2 * l->size);
5327 }
5328
5329 l->p[l->size++] = exp;
5330
5331 ExprList_check_invariants(l);
5332 return 0;
5333}
5334
5335static void
5336ExprList_Dealloc(ExprList *l)
5337{
5338 ExprList_check_invariants(l);
5339
5340 /* If there's been an error, or we've never dynamically allocated,
5341 do nothing. */
5342 if (!l->p || l->p == l->data) {
5343 /* Do nothing. */
5344 } else {
5345 /* We have dynamically allocated. Free the memory. */
Lysandros Nikolaou5193d0a2020-06-27 21:35:18 +03005346 PyMem_Free(l->p);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005347 }
5348 l->p = NULL;
5349 l->size = -1;
5350}
5351
5352static asdl_seq *
5353ExprList_Finish(ExprList *l, PyArena *arena)
5354{
5355 asdl_seq *seq;
5356
5357 ExprList_check_invariants(l);
5358
5359 /* Allocate the asdl_seq and copy the expressions in to it. */
5360 seq = _Py_asdl_seq_new(l->size, arena);
5361 if (seq) {
5362 Py_ssize_t i;
5363 for (i = 0; i < l->size; i++)
5364 asdl_seq_SET(seq, i, l->p[i]);
5365 }
5366 ExprList_Dealloc(l);
5367 return seq;
5368}
5369
5370/* The FstringParser is designed to add a mix of strings and
5371 f-strings, and concat them together as needed. Ultimately, it
5372 generates an expr_ty. */
5373typedef struct {
5374 PyObject *last_str;
5375 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005376 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005377} FstringParser;
5378
5379#ifdef NDEBUG
5380#define FstringParser_check_invariants(state)
5381#else
5382static void
5383FstringParser_check_invariants(FstringParser *state)
5384{
5385 if (state->last_str)
5386 assert(PyUnicode_CheckExact(state->last_str));
5387 ExprList_check_invariants(&state->expr_list);
5388}
5389#endif
5390
5391static void
5392FstringParser_Init(FstringParser *state)
5393{
5394 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005395 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005396 ExprList_Init(&state->expr_list);
5397 FstringParser_check_invariants(state);
5398}
5399
5400static void
5401FstringParser_Dealloc(FstringParser *state)
5402{
5403 FstringParser_check_invariants(state);
5404
5405 Py_XDECREF(state->last_str);
5406 ExprList_Dealloc(&state->expr_list);
5407}
5408
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005409/* Constants for the following */
5410static PyObject *u_kind;
5411
5412/* Compute 'kind' field for string Constant (either 'u' or None) */
5413static PyObject *
5414make_kind(struct compiling *c, const node *n)
5415{
5416 char *s = NULL;
5417 PyObject *kind = NULL;
5418
5419 /* Find the first string literal, if any */
5420 while (TYPE(n) != STRING) {
5421 if (NCH(n) == 0)
5422 return NULL;
5423 n = CHILD(n, 0);
5424 }
5425 REQ(n, STRING);
5426
5427 /* If it starts with 'u', return a PyUnicode "u" string */
5428 s = STR(n);
5429 if (s && *s == 'u') {
5430 if (!u_kind) {
5431 u_kind = PyUnicode_InternFromString("u");
5432 if (!u_kind)
5433 return NULL;
5434 }
5435 kind = u_kind;
5436 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5437 return NULL;
5438 }
5439 Py_INCREF(kind);
5440 }
5441 return kind;
5442}
5443
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005444/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005445static expr_ty
5446make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5447{
5448 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005449 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005450 *str = NULL;
5451 assert(PyUnicode_CheckExact(s));
5452 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5453 Py_DECREF(s);
5454 return NULL;
5455 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005456 kind = make_kind(c, n);
5457 if (kind == NULL && PyErr_Occurred())
5458 return NULL;
5459 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005460 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005461}
5462
5463/* Add a non-f-string (that is, a regular literal string). str is
5464 decref'd. */
5465static int
5466FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5467{
5468 FstringParser_check_invariants(state);
5469
5470 assert(PyUnicode_CheckExact(str));
5471
5472 if (PyUnicode_GET_LENGTH(str) == 0) {
5473 Py_DECREF(str);
5474 return 0;
5475 }
5476
5477 if (!state->last_str) {
5478 /* We didn't have a string before, so just remember this one. */
5479 state->last_str = str;
5480 } else {
5481 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005482 PyUnicode_AppendAndDel(&state->last_str, str);
5483 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005484 return -1;
5485 }
5486 FstringParser_check_invariants(state);
5487 return 0;
5488}
5489
Eric V. Smith451d0e32016-09-09 21:56:20 -04005490/* Parse an f-string. The f-string is in *str to end, with no
5491 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005492static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005493FstringParser_ConcatFstring(FstringParser *state, const char **str,
5494 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005495 struct compiling *c, const node *n)
5496{
5497 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005498 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005499
5500 /* Parse the f-string. */
5501 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005502 PyObject *literal = NULL;
5503 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005504 expr_ty expression = NULL;
5505
5506 /* If there's a zero length literal in front of the
5507 expression, literal will be NULL. If we're at the end of
5508 the f-string, expression will be NULL (unless result == 1,
5509 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005510 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005511 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005512 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005513 if (result < 0)
5514 return -1;
5515
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005516 /* Add the literal, if any. */
5517 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5518 Py_XDECREF(expr_text);
5519 return -1;
5520 }
5521 /* Add the expr_text, if any. */
5522 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5523 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005524 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005525
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005526 /* We've dealt with the literal and expr_text, their ownership has
5527 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005528
5529 /* See if we should just loop around to get the next literal
5530 and expression, while ignoring the expression this
5531 time. This is used for un-doubling braces, as an
5532 optimization. */
5533 if (result == 1)
5534 continue;
5535
5536 if (!expression)
5537 /* We're done with this f-string. */
5538 break;
5539
5540 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005541 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005542 if (!state->last_str) {
5543 /* Do nothing. No previous literal. */
5544 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005545 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005546 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5547 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5548 return -1;
5549 }
5550
5551 if (ExprList_Append(&state->expr_list, expression) < 0)
5552 return -1;
5553 }
5554
Eric V. Smith235a6f02015-09-19 14:51:32 -04005555 /* If recurse_lvl is zero, then we must be at the end of the
5556 string. Otherwise, we must be at a right brace. */
5557
Eric V. Smith451d0e32016-09-09 21:56:20 -04005558 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005559 ast_error(c, n, "f-string: unexpected end of string");
5560 return -1;
5561 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005562 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005563 ast_error(c, n, "f-string: expecting '}'");
5564 return -1;
5565 }
5566
5567 FstringParser_check_invariants(state);
5568 return 0;
5569}
5570
5571/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005572 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005573static expr_ty
5574FstringParser_Finish(FstringParser *state, struct compiling *c,
5575 const node *n)
5576{
5577 asdl_seq *seq;
5578
5579 FstringParser_check_invariants(state);
5580
5581 /* If we're just a constant string with no expressions, return
5582 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005583 if (!state->fmode) {
5584 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005585 if (!state->last_str) {
5586 /* Create a zero length string. */
5587 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5588 if (!state->last_str)
5589 goto error;
5590 }
5591 return make_str_node_and_del(&state->last_str, c, n);
5592 }
5593
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005594 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005595 last node in our expression list. */
5596 if (state->last_str) {
5597 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5598 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5599 goto error;
5600 }
5601 /* This has already been freed. */
5602 assert(state->last_str == NULL);
5603
5604 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5605 if (!seq)
5606 goto error;
5607
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005608 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5609 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005610
5611error:
5612 FstringParser_Dealloc(state);
5613 return NULL;
5614}
5615
Eric V. Smith451d0e32016-09-09 21:56:20 -04005616/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5617 at end, parse it into an expr_ty. Return NULL on error. Adjust
5618 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005619static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005620fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005621 struct compiling *c, const node *n)
5622{
5623 FstringParser state;
5624
5625 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005626 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005627 c, n) < 0) {
5628 FstringParser_Dealloc(&state);
5629 return NULL;
5630 }
5631
5632 return FstringParser_Finish(&state, c, n);
5633}
5634
5635/* n is a Python string literal, including the bracketing quote
5636 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005637 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005638 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005639 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5640 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005641*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005642static int
5643parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5644 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005645{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005646 size_t len;
5647 const char *s = STR(n);
5648 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005649 int fmode = 0;
5650 *bytesmode = 0;
5651 *rawmode = 0;
5652 *result = NULL;
5653 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005654 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005655 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005656 if (quote == 'b' || quote == 'B') {
5657 quote = *++s;
5658 *bytesmode = 1;
5659 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005660 else if (quote == 'u' || quote == 'U') {
5661 quote = *++s;
5662 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005663 else if (quote == 'r' || quote == 'R') {
5664 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005665 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005666 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005667 else if (quote == 'f' || quote == 'F') {
5668 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005669 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005670 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005671 else {
5672 break;
5673 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005674 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005675 }
Guido van Rossum495da292019-03-07 12:38:08 -08005676
5677 /* fstrings are only allowed in Python 3.6 and greater */
5678 if (fmode && c->c_feature_version < 6) {
5679 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5680 return -1;
5681 }
5682
Eric V. Smith451d0e32016-09-09 21:56:20 -04005683 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005684 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005685 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005686 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005687 if (quote != '\'' && quote != '\"') {
5688 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005689 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005690 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005691 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005692 s++;
5693 len = strlen(s);
5694 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005695 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005696 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005697 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005698 }
5699 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005700 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005701 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005702 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005703 }
5704 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005705 /* A triple quoted string. We've already skipped one quote at
5706 the start and one at the end of the string. Now skip the
5707 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005708 s += 2;
5709 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005710 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005711 if (s[--len] != quote || s[--len] != quote) {
5712 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005713 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005714 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005715 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005716
Eric V. Smith451d0e32016-09-09 21:56:20 -04005717 if (fmode) {
5718 /* Just return the bytes. The caller will parse the resulting
5719 string. */
5720 *fstr = s;
5721 *fstrlen = len;
5722 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005723 }
5724
Eric V. Smith451d0e32016-09-09 21:56:20 -04005725 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005726 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005727 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005728 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005729 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005730 const char *ch;
5731 for (ch = s; *ch; ch++) {
5732 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005733 ast_error(c, n,
5734 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005735 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005736 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005737 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005738 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005739 if (*rawmode)
5740 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005741 else
Eric V. Smith56466482016-10-31 14:46:26 -04005742 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005743 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005744 if (*rawmode)
5745 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005746 else
Eric V. Smith56466482016-10-31 14:46:26 -04005747 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005748 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005749 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005750}
5751
Eric V. Smith235a6f02015-09-19 14:51:32 -04005752/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5753 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005754 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005755 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005756 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005757 node if there's just an f-string (with no leading or trailing
5758 literals), or a JoinedStr node if there are multiple f-strings or
5759 any literals involved. */
5760static expr_ty
5761parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005762{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005763 int bytesmode = 0;
5764 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005765 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005766
5767 FstringParser state;
5768 FstringParser_Init(&state);
5769
5770 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005771 int this_bytesmode;
5772 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005773 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005774 const char *fstr;
5775 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005776
5777 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005778 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5779 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005780 goto error;
5781
5782 /* Check that we're not mixing bytes with unicode. */
5783 if (i != 0 && bytesmode != this_bytesmode) {
5784 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005785 /* s is NULL if the current string part is an f-string. */
5786 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005787 goto error;
5788 }
5789 bytesmode = this_bytesmode;
5790
Eric V. Smith451d0e32016-09-09 21:56:20 -04005791 if (fstr != NULL) {
5792 int result;
5793 assert(s == NULL && !bytesmode);
5794 /* This is an f-string. Parse and concatenate it. */
5795 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5796 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005797 if (result < 0)
5798 goto error;
5799 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005800 /* A string or byte string. */
5801 assert(s != NULL && fstr == NULL);
5802
Eric V. Smith451d0e32016-09-09 21:56:20 -04005803 assert(bytesmode ? PyBytes_CheckExact(s) :
5804 PyUnicode_CheckExact(s));
5805
Eric V. Smith451d0e32016-09-09 21:56:20 -04005806 if (bytesmode) {
5807 /* For bytes, concat as we go. */
5808 if (i == 0) {
5809 /* First time, just remember this value. */
5810 bytes_str = s;
5811 } else {
5812 PyBytes_ConcatAndDel(&bytes_str, s);
5813 if (!bytes_str)
5814 goto error;
5815 }
5816 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005817 /* This is a regular string. Concatenate it. */
5818 if (FstringParser_ConcatAndDel(&state, s) < 0)
5819 goto error;
5820 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005821 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005822 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005823 if (bytesmode) {
5824 /* Just return the bytes object and we're done. */
5825 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5826 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005827 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005828 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005829 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005830
Eric V. Smith235a6f02015-09-19 14:51:32 -04005831 /* We're not a bytes string, bytes_str should never have been set. */
5832 assert(bytes_str == NULL);
5833
5834 return FstringParser_Finish(&state, c, n);
5835
5836error:
5837 Py_XDECREF(bytes_str);
5838 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005840}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005841
5842PyObject *
5843_PyAST_GetDocString(asdl_seq *body)
5844{
5845 if (!asdl_seq_LEN(body)) {
5846 return NULL;
5847 }
5848 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5849 if (st->kind != Expr_kind) {
5850 return NULL;
5851 }
5852 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005853 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5854 return e->v.Constant.value;
5855 }
5856 return NULL;
5857}