blob: 550ee03b1ac384d7f521601731c0c62b48583c6f [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
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -050043validate_keywords(asdl_seq *keywords)
44{
Victor Stinner4d73ae72018-11-22 14:45:16 +010045 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050046 for (i = 0; i < asdl_seq_LEN(keywords); i++)
47 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
48 return 0;
49 return 1;
50}
51
52static int
53validate_args(asdl_seq *args)
54{
Victor Stinner4d73ae72018-11-22 14:45:16 +010055 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050056 for (i = 0; i < asdl_seq_LEN(args); i++) {
57 arg_ty arg = asdl_seq_GET(args, i);
58 if (arg->annotation && !validate_expr(arg->annotation, Load))
59 return 0;
60 }
61 return 1;
62}
63
64static const char *
65expr_context_name(expr_context_ty ctx)
66{
67 switch (ctx) {
68 case Load:
69 return "Load";
70 case Store:
71 return "Store";
72 case Del:
73 return "Del";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050074 default:
Barry Warsawb2e57942017-09-14 18:13:16 -070075 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -050076 }
77}
78
79static int
80validate_arguments(arguments_ty args)
81{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010082 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050083 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010084 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -070085 if (args->vararg && args->vararg->annotation
86 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050087 return 0;
88 }
89 if (!validate_args(args->kwonlyargs))
90 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +010091 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -070092 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050093 return 0;
94 }
Pablo Galindo2f58a842019-05-31 14:09:49 +010095 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050096 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
97 return 0;
98 }
99 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
100 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
101 "kw_defaults on arguments");
102 return 0;
103 }
104 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
105}
106
107static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100108validate_constant(PyObject *value)
109{
110 if (value == Py_None || value == Py_Ellipsis)
111 return 1;
112
113 if (PyLong_CheckExact(value)
114 || PyFloat_CheckExact(value)
115 || PyComplex_CheckExact(value)
116 || PyBool_Check(value)
117 || PyUnicode_CheckExact(value)
118 || PyBytes_CheckExact(value))
119 return 1;
120
121 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
122 PyObject *it;
123
124 it = PyObject_GetIter(value);
125 if (it == NULL)
126 return 0;
127
128 while (1) {
129 PyObject *item = PyIter_Next(it);
130 if (item == NULL) {
131 if (PyErr_Occurred()) {
132 Py_DECREF(it);
133 return 0;
134 }
135 break;
136 }
137
138 if (!validate_constant(item)) {
139 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100140 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100141 return 0;
142 }
Victor Stinner726f6902016-01-27 00:11:47 +0100143 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100144 }
145
146 Py_DECREF(it);
147 return 1;
148 }
149
Batuhan Taßkaya0ac59f92020-03-19 14:32:28 +0300150 if (!PyErr_Occurred()) {
151 PyErr_Format(PyExc_TypeError,
152 "got an invalid type in Constant: %s",
153 _PyType_Name(Py_TYPE(value)));
154 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100155 return 0;
156}
157
158static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500159validate_expr(expr_ty exp, expr_context_ty ctx)
160{
161 int check_ctx = 1;
162 expr_context_ty actual_ctx;
163
164 /* First check expression context. */
165 switch (exp->kind) {
166 case Attribute_kind:
167 actual_ctx = exp->v.Attribute.ctx;
168 break;
169 case Subscript_kind:
170 actual_ctx = exp->v.Subscript.ctx;
171 break;
172 case Starred_kind:
173 actual_ctx = exp->v.Starred.ctx;
174 break;
175 case Name_kind:
176 actual_ctx = exp->v.Name.ctx;
177 break;
178 case List_kind:
179 actual_ctx = exp->v.List.ctx;
180 break;
181 case Tuple_kind:
182 actual_ctx = exp->v.Tuple.ctx;
183 break;
184 default:
185 if (ctx != Load) {
186 PyErr_Format(PyExc_ValueError, "expression which can't be "
187 "assigned to in %s context", expr_context_name(ctx));
188 return 0;
189 }
190 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100191 /* set actual_ctx to prevent gcc warning */
192 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500193 }
194 if (check_ctx && actual_ctx != ctx) {
195 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
196 expr_context_name(ctx), expr_context_name(actual_ctx));
197 return 0;
198 }
199
200 /* Now validate expression. */
201 switch (exp->kind) {
202 case BoolOp_kind:
203 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
204 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
205 return 0;
206 }
207 return validate_exprs(exp->v.BoolOp.values, Load, 0);
208 case BinOp_kind:
209 return validate_expr(exp->v.BinOp.left, Load) &&
210 validate_expr(exp->v.BinOp.right, Load);
211 case UnaryOp_kind:
212 return validate_expr(exp->v.UnaryOp.operand, Load);
213 case Lambda_kind:
214 return validate_arguments(exp->v.Lambda.args) &&
215 validate_expr(exp->v.Lambda.body, Load);
216 case IfExp_kind:
217 return validate_expr(exp->v.IfExp.test, Load) &&
218 validate_expr(exp->v.IfExp.body, Load) &&
219 validate_expr(exp->v.IfExp.orelse, Load);
220 case Dict_kind:
221 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
222 PyErr_SetString(PyExc_ValueError,
223 "Dict doesn't have the same number of keys as values");
224 return 0;
225 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400226 /* null_ok=1 for keys expressions to allow dict unpacking to work in
227 dict literals, i.e. ``{**{a:b}}`` */
228 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
229 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500230 case Set_kind:
231 return validate_exprs(exp->v.Set.elts, Load, 0);
232#define COMP(NAME) \
233 case NAME ## _kind: \
234 return validate_comprehension(exp->v.NAME.generators) && \
235 validate_expr(exp->v.NAME.elt, Load);
236 COMP(ListComp)
237 COMP(SetComp)
238 COMP(GeneratorExp)
239#undef COMP
240 case DictComp_kind:
241 return validate_comprehension(exp->v.DictComp.generators) &&
242 validate_expr(exp->v.DictComp.key, Load) &&
243 validate_expr(exp->v.DictComp.value, Load);
244 case Yield_kind:
245 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500246 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000247 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400248 case Await_kind:
249 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500250 case Compare_kind:
251 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
252 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
253 return 0;
254 }
255 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
256 asdl_seq_LEN(exp->v.Compare.ops)) {
257 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
258 "of comparators and operands");
259 return 0;
260 }
261 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
262 validate_expr(exp->v.Compare.left, Load);
263 case Call_kind:
264 return validate_expr(exp->v.Call.func, Load) &&
265 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400266 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100267 case Constant_kind:
268 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100269 return 0;
270 }
271 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400272 case JoinedStr_kind:
273 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
274 case FormattedValue_kind:
275 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
276 return 0;
277 if (exp->v.FormattedValue.format_spec)
278 return validate_expr(exp->v.FormattedValue.format_spec, Load);
279 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500280 case Attribute_kind:
281 return validate_expr(exp->v.Attribute.value, Load);
282 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200283 return validate_expr(exp->v.Subscript.slice, Load) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500284 validate_expr(exp->v.Subscript.value, Load);
285 case Starred_kind:
286 return validate_expr(exp->v.Starred.value, ctx);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200287 case Slice_kind:
288 return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) &&
289 (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) &&
290 (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500291 case List_kind:
292 return validate_exprs(exp->v.List.elts, ctx, 0);
293 case Tuple_kind:
294 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000295 case NamedExpr_kind:
296 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300297 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500298 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500299 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500300 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000301 PyErr_SetString(PyExc_SystemError, "unexpected expression");
302 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500303}
304
305static int
306validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
307{
308 if (asdl_seq_LEN(seq))
309 return 1;
310 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
311 return 0;
312}
313
314static int
315validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
316{
317 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
318 validate_exprs(targets, ctx, 0);
319}
320
321static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300322validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300324 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325}
326
327static int
328validate_stmt(stmt_ty stmt)
329{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100330 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500331 switch (stmt->kind) {
332 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300333 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500334 validate_arguments(stmt->v.FunctionDef.args) &&
335 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
336 (!stmt->v.FunctionDef.returns ||
337 validate_expr(stmt->v.FunctionDef.returns, Load));
338 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300339 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500340 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
341 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400342 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500343 case Return_kind:
344 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
345 case Delete_kind:
346 return validate_assignlist(stmt->v.Delete.targets, Del);
347 case Assign_kind:
348 return validate_assignlist(stmt->v.Assign.targets, Store) &&
349 validate_expr(stmt->v.Assign.value, Load);
350 case AugAssign_kind:
351 return validate_expr(stmt->v.AugAssign.target, Store) &&
352 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700353 case AnnAssign_kind:
354 if (stmt->v.AnnAssign.target->kind != Name_kind &&
355 stmt->v.AnnAssign.simple) {
356 PyErr_SetString(PyExc_TypeError,
357 "AnnAssign with simple non-Name target");
358 return 0;
359 }
360 return validate_expr(stmt->v.AnnAssign.target, Store) &&
361 (!stmt->v.AnnAssign.value ||
362 validate_expr(stmt->v.AnnAssign.value, Load)) &&
363 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500364 case For_kind:
365 return validate_expr(stmt->v.For.target, Store) &&
366 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300367 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400369 case AsyncFor_kind:
370 return validate_expr(stmt->v.AsyncFor.target, Store) &&
371 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300372 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400373 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500374 case While_kind:
375 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300376 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500377 validate_stmts(stmt->v.While.orelse);
378 case If_kind:
379 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300380 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500381 validate_stmts(stmt->v.If.orelse);
382 case With_kind:
383 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
384 return 0;
385 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
386 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
387 if (!validate_expr(item->context_expr, Load) ||
388 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
389 return 0;
390 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300391 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400392 case AsyncWith_kind:
393 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
394 return 0;
395 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
396 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
397 if (!validate_expr(item->context_expr, Load) ||
398 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
399 return 0;
400 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 case Raise_kind:
403 if (stmt->v.Raise.exc) {
404 return validate_expr(stmt->v.Raise.exc, Load) &&
405 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
406 }
407 if (stmt->v.Raise.cause) {
408 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
409 return 0;
410 }
411 return 1;
412 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300413 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500414 return 0;
415 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
416 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
417 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
418 return 0;
419 }
420 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
421 asdl_seq_LEN(stmt->v.Try.orelse)) {
422 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
423 return 0;
424 }
425 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
426 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
427 if ((handler->v.ExceptHandler.type &&
428 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300429 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500430 return 0;
431 }
432 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
433 validate_stmts(stmt->v.Try.finalbody)) &&
434 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
435 validate_stmts(stmt->v.Try.orelse));
436 case Assert_kind:
437 return validate_expr(stmt->v.Assert.test, Load) &&
438 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
439 case Import_kind:
440 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
441 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300442 if (stmt->v.ImportFrom.level < 0) {
443 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500444 return 0;
445 }
446 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
447 case Global_kind:
448 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
449 case Nonlocal_kind:
450 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
451 case Expr_kind:
452 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400453 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400455 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
456 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
457 (!stmt->v.AsyncFunctionDef.returns ||
458 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500459 case Pass_kind:
460 case Break_kind:
461 case Continue_kind:
462 return 1;
463 default:
464 PyErr_SetString(PyExc_SystemError, "unexpected statement");
465 return 0;
466 }
467}
468
469static int
470validate_stmts(asdl_seq *seq)
471{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100472 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500473 for (i = 0; i < asdl_seq_LEN(seq); i++) {
474 stmt_ty stmt = asdl_seq_GET(seq, i);
475 if (stmt) {
476 if (!validate_stmt(stmt))
477 return 0;
478 }
479 else {
480 PyErr_SetString(PyExc_ValueError,
481 "None disallowed in statement list");
482 return 0;
483 }
484 }
485 return 1;
486}
487
488static int
489validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
490{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100491 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500492 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
493 expr_ty expr = asdl_seq_GET(exprs, i);
494 if (expr) {
495 if (!validate_expr(expr, ctx))
496 return 0;
497 }
498 else if (!null_ok) {
499 PyErr_SetString(PyExc_ValueError,
500 "None disallowed in expression list");
501 return 0;
502 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100503
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500504 }
505 return 1;
506}
507
508int
509PyAST_Validate(mod_ty mod)
510{
511 int res = 0;
512
513 switch (mod->kind) {
514 case Module_kind:
515 res = validate_stmts(mod->v.Module.body);
516 break;
517 case Interactive_kind:
518 res = validate_stmts(mod->v.Interactive.body);
519 break;
520 case Expression_kind:
521 res = validate_expr(mod->v.Expression.body, Load);
522 break;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500523 default:
524 PyErr_SetString(PyExc_SystemError, "impossible module node");
525 res = 0;
526 break;
527 }
528 return res;
529}
530
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500531/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500532#include "grammar.h"
533#include "parsetok.h"
534#include "graminit.h"
535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536/* Data structure used internally */
537struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400538 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200539 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500540 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800541 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542};
543
544static asdl_seq *seq_for_testlist(struct compiling *, const node *);
545static expr_ty ast_for_expr(struct compiling *, const node *);
546static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300547static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000548static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
549 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000550static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000551static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552
guoci90fc8982018-09-11 17:45:45 -0400553static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
554static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200557static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +0200558 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000560static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400561static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000562static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563
Nick Coghlan650f0d02007-04-15 12:05:43 +0000564#define COMP_GENEXP 0
565#define COMP_LISTCOMP 1
566#define COMP_SETCOMP 2
567
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568static int
569init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000570{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500571 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
572 if (!m)
573 return 0;
574 c->c_normalize = PyObject_GetAttrString(m, "normalize");
575 Py_DECREF(m);
576 if (!c->c_normalize)
577 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500578 return 1;
579}
580
581static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400582new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500583{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400584 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500585 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000586 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500587 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500588 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000589 /* Check whether there are non-ASCII characters in the
590 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500591 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500593 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500594 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500596 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700597 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300598 if (form == NULL) {
599 Py_DECREF(id);
600 return NULL;
601 }
602 PyObject *args[2] = {form, id};
603 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500604 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100605 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 if (!id2)
607 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300608 if (!PyUnicode_Check(id2)) {
609 PyErr_Format(PyExc_TypeError,
610 "unicodedata.normalize() must return a string, not "
611 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700612 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300613 Py_DECREF(id2);
614 return NULL;
615 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000617 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000618 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200619 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
620 Py_DECREF(id);
621 return NULL;
622 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000623 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624}
625
Benjamin Peterson55e00432012-01-16 17:22:31 -0500626#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200629ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400631 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200632 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200634 va_start(va, errmsg);
635 errstr = PyUnicode_FromFormatV(errmsg, va);
636 va_end(va);
637 if (!errstr) {
638 return 0;
639 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200640 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000642 Py_INCREF(Py_None);
643 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 }
Ammar Askar025eb982018-09-24 17:12:49 -0400645 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200646 if (!tmp) {
647 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400648 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000649 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 Py_DECREF(errstr);
652 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400653 if (value) {
654 PyErr_SetObject(PyExc_SyntaxError, value);
655 Py_DECREF(value);
656 }
657 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658}
659
660/* num_stmts() returns number of contained statements.
661
662 Use this routine to determine how big a sequence is needed for
663 the statements in a parse tree. Its raison d'etre is this bit of
664 grammar:
665
666 stmt: simple_stmt | compound_stmt
667 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
668
669 A simple_stmt can contain multiple small_stmt elements joined
670 by semicolons. If the arg is a simple_stmt, the number of
671 small_stmt elements is returned.
672*/
673
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800674static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800675new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800676{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800677 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800678 if (res == NULL)
679 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800680 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
681 Py_DECREF(res);
682 return NULL;
683 }
684 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800685}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800686#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688static int
689num_stmts(const node *n)
690{
691 int i, l;
692 node *ch;
693
694 switch (TYPE(n)) {
695 case single_input:
696 if (TYPE(CHILD(n, 0)) == NEWLINE)
697 return 0;
698 else
699 return num_stmts(CHILD(n, 0));
700 case file_input:
701 l = 0;
702 for (i = 0; i < NCH(n); i++) {
703 ch = CHILD(n, i);
704 if (TYPE(ch) == stmt)
705 l += num_stmts(ch);
706 }
707 return l;
708 case stmt:
709 return num_stmts(CHILD(n, 0));
710 case compound_stmt:
711 return 1;
712 case simple_stmt:
713 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
714 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800715 case func_body_suite:
716 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
717 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 if (NCH(n) == 1)
719 return num_stmts(CHILD(n, 0));
720 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800721 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800723 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
724 i += 2;
725 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 l += num_stmts(CHILD(n, i));
727 return l;
728 }
729 default: {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100730 _Py_FatalErrorFormat(__func__, "Non-statement found: %d %d",
731 TYPE(n), NCH(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 }
733 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700734 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735}
736
737/* Transform the CST rooted at node * to the appropriate AST
738*/
739
740mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200741PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
742 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000744 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800746 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 stmt_ty s;
748 node *ch;
749 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500750 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800751 asdl_seq *argtypes = NULL;
752 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400754 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200755 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400756 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800757 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700758 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800759
760 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762
Jeremy Hyltona8293132006-02-28 17:58:27 +0000763 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 switch (TYPE(n)) {
765 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200766 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500768 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 for (i = 0; i < NCH(n) - 1; i++) {
770 ch = CHILD(n, i);
771 if (TYPE(ch) == NEWLINE)
772 continue;
773 REQ(ch, stmt);
774 num = num_stmts(ch);
775 if (num == 1) {
776 s = ast_for_stmt(&c, ch);
777 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500778 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000779 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 }
781 else {
782 ch = CHILD(ch, 0);
783 REQ(ch, simple_stmt);
784 for (j = 0; j < num; j++) {
785 s = ast_for_stmt(&c, CHILD(ch, j * 2));
786 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500787 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000788 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 }
791 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800792
793 /* Type ignores are stored under the ENDMARKER in file_input. */
794 ch = CHILD(n, NCH(n) - 1);
795 REQ(ch, ENDMARKER);
796 num = NCH(ch);
797 type_ignores = _Py_asdl_seq_new(num, arena);
798 if (!type_ignores)
799 goto out;
800
801 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700802 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
803 if (!type_comment)
804 goto out;
805 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800806 if (!ti)
807 goto out;
808 asdl_seq_SET(type_ignores, i, ti);
809 }
810
811 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500812 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 case eval_input: {
814 expr_ty testlist_ast;
815
Nick Coghlan650f0d02007-04-15 12:05:43 +0000816 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000817 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 goto out;
820 res = Expression(testlist_ast, arena);
821 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 }
823 case single_input:
824 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200825 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500827 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000829 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000831 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500832 goto out;
833 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 }
835 else {
836 n = CHILD(n, 0);
837 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200838 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500840 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000842 s = ast_for_stmt(&c, n);
843 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500844 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 asdl_seq_SET(stmts, 0, s);
846 }
847 else {
848 /* Only a simple_stmt can contain multiple statements. */
849 REQ(n, simple_stmt);
850 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (TYPE(CHILD(n, i)) == NEWLINE)
852 break;
853 s = ast_for_stmt(&c, CHILD(n, i));
854 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 asdl_seq_SET(stmts, i / 2, s);
857 }
858 }
859
Benjamin Peterson55e00432012-01-16 17:22:31 -0500860 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500862 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800863 case func_type_input:
864 n = CHILD(n, 0);
865 REQ(n, func_type);
866
867 if (TYPE(CHILD(n, 1)) == typelist) {
868 ch = CHILD(n, 1);
869 /* this is overly permissive -- we don't pay any attention to
870 * stars on the args -- just parse them into an ordered list */
871 num = 0;
872 for (i = 0; i < NCH(ch); i++) {
873 if (TYPE(CHILD(ch, i)) == test) {
874 num++;
875 }
876 }
877
878 argtypes = _Py_asdl_seq_new(num, arena);
879 if (!argtypes)
880 goto out;
881
882 j = 0;
883 for (i = 0; i < NCH(ch); i++) {
884 if (TYPE(CHILD(ch, i)) == test) {
885 arg = ast_for_expr(&c, CHILD(ch, i));
886 if (!arg)
887 goto out;
888 asdl_seq_SET(argtypes, j++, arg);
889 }
890 }
891 }
892 else {
893 argtypes = _Py_asdl_seq_new(0, arena);
894 if (!argtypes)
895 goto out;
896 }
897
898 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
899 if (!ret)
900 goto out;
901 res = FunctionType(argtypes, ret, arena);
902 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000904 PyErr_Format(PyExc_SystemError,
905 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500906 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500908 out:
909 if (c.c_normalize) {
910 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500911 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500912 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913}
914
Victor Stinner14e461d2013-08-26 22:28:21 +0200915mod_ty
916PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
917 PyArena *arena)
918{
919 mod_ty mod;
920 PyObject *filename;
921 filename = PyUnicode_DecodeFSDefault(filename_str);
922 if (filename == NULL)
923 return NULL;
924 mod = PyAST_FromNodeObject(n, flags, filename, arena);
925 Py_DECREF(filename);
926 return mod;
927
928}
929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
931*/
932
933static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800934get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935{
936 switch (TYPE(n)) {
937 case VBAR:
938 return BitOr;
939 case CIRCUMFLEX:
940 return BitXor;
941 case AMPER:
942 return BitAnd;
943 case LEFTSHIFT:
944 return LShift;
945 case RIGHTSHIFT:
946 return RShift;
947 case PLUS:
948 return Add;
949 case MINUS:
950 return Sub;
951 case STAR:
952 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400953 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800954 if (c->c_feature_version < 5) {
955 ast_error(c, n,
956 "The '@' operator is only supported in Python 3.5 and greater");
957 return (operator_ty)0;
958 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400959 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 case SLASH:
961 return Div;
962 case DOUBLESLASH:
963 return FloorDiv;
964 case PERCENT:
965 return Mod;
966 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000967 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 }
969}
970
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200971static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000972 "None",
973 "True",
974 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200975 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000976 NULL,
977};
978
979static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400980forbidden_name(struct compiling *c, identifier name, const node *n,
981 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000982{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000983 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200984 const char * const *p = FORBIDDEN;
985 if (!full_checks) {
986 /* In most cases, the parser will protect True, False, and None
987 from being assign to. */
988 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000989 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200990 for (; *p; p++) {
991 if (_PyUnicode_EqualToASCIIString(name, *p)) {
992 ast_error(c, n, "cannot assign to %U", name);
993 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000994 }
995 }
996 return 0;
997}
998
Serhiy Storchakab619b092018-11-27 09:40:29 +0200999static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001000copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001001{
1002 if (e) {
1003 e->lineno = LINENO(n);
1004 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001005 e->end_lineno = end->n_end_lineno;
1006 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001007 }
1008 return e;
1009}
1010
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001011static const char *
1012get_expr_name(expr_ty e)
1013{
1014 switch (e->kind) {
1015 case Attribute_kind:
1016 return "attribute";
1017 case Subscript_kind:
1018 return "subscript";
1019 case Starred_kind:
1020 return "starred";
1021 case Name_kind:
1022 return "name";
1023 case List_kind:
1024 return "list";
1025 case Tuple_kind:
1026 return "tuple";
1027 case Lambda_kind:
1028 return "lambda";
1029 case Call_kind:
1030 return "function call";
1031 case BoolOp_kind:
1032 case BinOp_kind:
1033 case UnaryOp_kind:
1034 return "operator";
1035 case GeneratorExp_kind:
1036 return "generator expression";
1037 case Yield_kind:
1038 case YieldFrom_kind:
1039 return "yield expression";
1040 case Await_kind:
1041 return "await expression";
1042 case ListComp_kind:
1043 return "list comprehension";
1044 case SetComp_kind:
1045 return "set comprehension";
1046 case DictComp_kind:
1047 return "dict comprehension";
1048 case Dict_kind:
1049 return "dict display";
1050 case Set_kind:
1051 return "set display";
1052 case JoinedStr_kind:
1053 case FormattedValue_kind:
1054 return "f-string expression";
1055 case Constant_kind: {
1056 PyObject *value = e->v.Constant.value;
1057 if (value == Py_None) {
1058 return "None";
1059 }
1060 if (value == Py_False) {
1061 return "False";
1062 }
1063 if (value == Py_True) {
1064 return "True";
1065 }
1066 if (value == Py_Ellipsis) {
1067 return "Ellipsis";
1068 }
1069 return "literal";
1070 }
1071 case Compare_kind:
1072 return "comparison";
1073 case IfExp_kind:
1074 return "conditional expression";
1075 case NamedExpr_kind:
1076 return "named expression";
1077 default:
1078 PyErr_Format(PyExc_SystemError,
1079 "unexpected expression in assignment %d (line %d)",
1080 e->kind, e->lineno);
1081 return NULL;
1082 }
1083}
1084
Jeremy Hyltona8293132006-02-28 17:58:27 +00001085/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086
1087 Only sets context for expr kinds that "can appear in assignment context"
1088 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1089 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090*/
1091
1092static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001093set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094{
1095 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001096
Serhiy Storchaka6b975982020-03-17 23:41:08 +02001097 /* Expressions in an augmented assignment have a Store context. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098
1099 switch (e->kind) {
1100 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001101 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001102 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001103 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001104 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001106 e->v.Subscript.ctx = ctx;
1107 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001108 case Starred_kind:
1109 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001110 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001111 return 0;
1112 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001114 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001115 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001116 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001117 }
1118 e->v.Name.ctx = ctx;
1119 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001121 e->v.List.ctx = ctx;
1122 s = e->v.List.elts;
1123 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001125 e->v.Tuple.ctx = ctx;
1126 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001127 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001128 default: {
1129 const char *expr_name = get_expr_name(e);
1130 if (expr_name != NULL) {
1131 ast_error(c, n, "cannot %s %s",
1132 ctx == Store ? "assign to" : "delete",
1133 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001134 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001135 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001136 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001137 }
1138
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 */
1142 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001143 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144
Thomas Wouters89f507f2006-12-13 04:49:30 +00001145 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001146 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001147 return 0;
1148 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 }
1150 return 1;
1151}
1152
1153static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001154ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155{
1156 REQ(n, augassign);
1157 n = CHILD(n, 0);
1158 switch (STR(n)[0]) {
1159 case '+':
1160 return Add;
1161 case '-':
1162 return Sub;
1163 case '/':
1164 if (STR(n)[1] == '/')
1165 return FloorDiv;
1166 else
1167 return Div;
1168 case '%':
1169 return Mod;
1170 case '<':
1171 return LShift;
1172 case '>':
1173 return RShift;
1174 case '&':
1175 return BitAnd;
1176 case '^':
1177 return BitXor;
1178 case '|':
1179 return BitOr;
1180 case '*':
1181 if (STR(n)[1] == '*')
1182 return Pow;
1183 else
1184 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001185 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001186 if (c->c_feature_version < 5) {
1187 ast_error(c, n,
1188 "The '@' operator is only supported in Python 3.5 and greater");
1189 return (operator_ty)0;
1190 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001191 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001193 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001194 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 }
1196}
1197
1198static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001199ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001201 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 |'is' 'not'
1203 */
1204 REQ(n, comp_op);
1205 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001206 n = CHILD(n, 0);
1207 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 case LESS:
1209 return Lt;
1210 case GREATER:
1211 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001212 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 return Eq;
1214 case LESSEQUAL:
1215 return LtE;
1216 case GREATEREQUAL:
1217 return GtE;
1218 case NOTEQUAL:
1219 return NotEq;
1220 case NAME:
1221 if (strcmp(STR(n), "in") == 0)
1222 return In;
1223 if (strcmp(STR(n), "is") == 0)
1224 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001225 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001227 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001230 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 }
1232 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001233 /* handle "not in" and "is not" */
1234 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 case NAME:
1236 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1237 return NotIn;
1238 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1239 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001240 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001242 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001244 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001245 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 }
Neal Norwitz79792652005-11-14 04:25:03 +00001247 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250}
1251
1252static asdl_seq *
1253seq_for_testlist(struct compiling *c, const node *n)
1254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001256 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1257 */
Armin Rigo31441302005-10-21 12:57:31 +00001258 asdl_seq *seq;
1259 expr_ty expression;
1260 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001261 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001263 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 if (!seq)
1265 return NULL;
1266
1267 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001269 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270
Benjamin Peterson4905e802009-09-27 02:43:28 +00001271 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001272 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274
1275 assert(i / 2 < seq->size);
1276 asdl_seq_SET(seq, i / 2, expression);
1277 }
1278 return seq;
1279}
1280
Neal Norwitzc1505362006-12-28 06:47:50 +00001281static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001282ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001283{
1284 identifier name;
1285 expr_ty annotation = NULL;
1286 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001287 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001288
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001289 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001290 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 name = NEW_IDENTIFIER(ch);
1292 if (!name)
1293 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001294 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001295 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001296
1297 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1298 annotation = ast_for_expr(c, CHILD(n, 2));
1299 if (!annotation)
1300 return NULL;
1301 }
1302
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001303 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001304 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001305 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001306 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001307 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308}
1309
Guido van Rossum4f72a782006-10-27 23:31:49 +00001310/* returns -1 if failed to handle keyword only arguments
1311 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001312 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001313 ^^^
1314 start pointing here
1315 */
1316static int
1317handle_keywordonly_args(struct compiling *c, const node *n, int start,
1318 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1319{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001320 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001321 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001322 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001323 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 int i = start;
1325 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001326
1327 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001328 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001329 return -1;
1330 }
1331 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 while (i < NCH(n)) {
1333 ch = CHILD(n, i);
1334 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 case vfpdef:
1336 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001339 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001340 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001341 asdl_seq_SET(kwdefaults, j, expression);
1342 i += 2; /* '=' and test */
1343 }
1344 else { /* setting NULL if no default value exists */
1345 asdl_seq_SET(kwdefaults, j, NULL);
1346 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001347 if (NCH(ch) == 3) {
1348 /* ch is NAME ':' test */
1349 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001350 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001351 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001352 }
1353 else {
1354 annotation = NULL;
1355 }
1356 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001357 argname = NEW_IDENTIFIER(ch);
1358 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001360 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001361 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001362 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001363 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001364 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001365 if (!arg)
1366 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001367 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001368 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001369 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001370 i += 1; /* the comma, if present */
1371 break;
1372 case TYPE_COMMENT:
1373 /* arg will be equal to the last argument processed */
1374 arg->type_comment = NEW_TYPE_COMMENT(ch);
1375 if (!arg->type_comment)
1376 goto error;
1377 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 break;
1379 case DOUBLESTAR:
1380 return i;
1381 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001382 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 goto error;
1384 }
1385 }
1386 return i;
1387 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390
Jeremy Hyltona8293132006-02-28 17:58:27 +00001391/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392
1393static arguments_ty
1394ast_for_arguments(struct compiling *c, const node *n)
1395{
Neal Norwitzc1505362006-12-28 06:47:50 +00001396 /* This function handles both typedargslist (function definition)
1397 and varargslist (lambda definition).
1398
1399 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001400
1401 The following definition for typedarglist is equivalent to this set of rules:
1402
1403 arguments = argument (',' [TYPE_COMMENT] argument)*
1404 argument = tfpdef ['=' test]
1405 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1406 args = '*' [tfpdef]
1407 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1408 [TYPE_COMMENT] [kwargs]])
1409 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1410 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1411 [TYPE_COMMENT] [args_kwonly_kwargs]])
1412 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1413 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1414 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1415
1416 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1417 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1418 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1419 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1420 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1421 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1422 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1423 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1424 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1425 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1426 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1427 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1428 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1429 '**' tfpdef [','] [TYPE_COMMENT]))
1430
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001431 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001432
1433 The following definition for varargslist is equivalent to this set of rules:
1434
1435 arguments = argument (',' argument )*
1436 argument = vfpdef ['=' test]
1437 kwargs = '**' vfpdef [',']
1438 args = '*' [vfpdef]
1439 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1440 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1441 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1442 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1443 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1444 (vararglist_no_posonly)
1445
1446 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1447 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1448 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1449 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1450 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1451 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1452 [',']]] | '**' vfpdef [','])
1453
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001454 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001455
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001457 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001459 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001460 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001461 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 node *ch;
1463
1464 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001466 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001469 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470
Jeremy Hyltone921e022008-07-17 16:37:17 +00001471 /* First count the number of positional args & defaults. The
1472 variable i is the loop index for this for loop and the next.
1473 The next loop picks up where the first leaves off.
1474 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 ch = CHILD(n, i);
1477 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001478 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001479 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001480 if (i < NCH(n) && /* skip argument following star */
1481 (TYPE(CHILD(n, i)) == tfpdef ||
1482 TYPE(CHILD(n, i)) == vfpdef)) {
1483 i++;
1484 }
1485 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001486 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001487 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001488 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001489 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001490 if (TYPE(ch) == SLASH ) {
1491 nposonlyargs = nposargs;
1492 nposargs = 0;
1493 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 defaults for keyword only args */
1497 for ( ; i < NCH(n); ++i) {
1498 ch = CHILD(n, i);
1499 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001500 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001501 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001502 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1503 if (!posonlyargs && nposonlyargs) {
1504 return NULL;
1505 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001506 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001507 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001508 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001509 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001510 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001511 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001512 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001514 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001515 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001516 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001518 since we set NULL as default for keyword only argument w/o default
1519 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001520 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001521 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001522 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001523 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001524
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001525 /* tfpdef: NAME [':' test]
1526 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 */
1528 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001529 j = 0; /* index for defaults */
1530 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001531 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001533 ch = CHILD(n, i);
1534 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001535 case tfpdef:
1536 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1538 anything other than EQUAL or a comma? */
1539 /* XXX Should NCH(n) check be made a separate check? */
1540 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001541 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1542 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001543 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 assert(posdefaults != NULL);
1545 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001549 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001550 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001551 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001552 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001553 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001554 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001555 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001556 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001557 if (l < nposonlyargs) {
1558 asdl_seq_SET(posonlyargs, l++, arg);
1559 } else {
1560 asdl_seq_SET(posargs, k++, arg);
1561 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001562 i += 1; /* the name */
1563 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1564 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001566 case SLASH:
1567 /* Advance the slash and the comma. If there are more names
1568 * after the slash there will be a comma so we are advancing
1569 * the correct number of nodes. If the slash is the last item,
1570 * we will be advancing an extra token but then * i > NCH(n)
1571 * and the enclosing while will finish correctly. */
1572 i += 2;
1573 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001575 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001576 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1577 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001578 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001579 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001580 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001581 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001582 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001583 if (TYPE(ch) == COMMA) {
1584 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001585 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001586
1587 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1588 ast_error(c, CHILD(n, i),
1589 "bare * has associated type comment");
1590 return NULL;
1591 }
1592
Guido van Rossum4f72a782006-10-27 23:31:49 +00001593 res = handle_keywordonly_args(c, n, i,
1594 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001595 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001596 i = res; /* res has new position to process */
1597 }
1598 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001599 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001600 if (!vararg)
1601 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001602
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001603 i += 2; /* the star and the name */
1604 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1605 i += 1; /* the comma, if present */
1606
1607 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1608 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1609 if (!vararg->type_comment)
1610 return NULL;
1611 i += 1;
1612 }
1613
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001614 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1615 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001616 int res = 0;
1617 res = handle_keywordonly_args(c, n, i,
1618 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001619 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001620 i = res; /* res has new position to process */
1621 }
1622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 break;
1624 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001625 ch = CHILD(n, i+1); /* tfpdef */
1626 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001627 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001628 if (!kwarg)
1629 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001630 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001631 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001632 i += 1; /* the comma, if present */
1633 break;
1634 case TYPE_COMMENT:
1635 assert(i);
1636
1637 if (kwarg)
1638 arg = kwarg;
1639
1640 /* arg will be equal to the last argument processed */
1641 arg->type_comment = NEW_TYPE_COMMENT(ch);
1642 if (!arg->type_comment)
1643 return NULL;
1644 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 break;
1646 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001647 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 "unexpected node in varargslist: %d @ %d",
1649 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001650 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001653 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654}
1655
1656static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657ast_for_decorator(struct compiling *c, const node *n)
1658{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001659 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001662 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001663 REQ(CHILD(n, 2), NEWLINE);
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001664
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001665 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666}
1667
1668static asdl_seq*
1669ast_for_decorators(struct compiling *c, const node *n)
1670{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001671 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001672 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001676 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 if (!decorator_seq)
1678 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001681 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001682 if (!d)
1683 return NULL;
1684 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 }
1686 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687}
1688
1689static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001690ast_for_funcdef_impl(struct compiling *c, const node *n0,
1691 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001693 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001694 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001695 identifier name;
1696 arguments_ty args;
1697 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001698 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001699 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001700 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001701 node *tc;
1702 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703
Guido van Rossum495da292019-03-07 12:38:08 -08001704 if (is_async && c->c_feature_version < 5) {
1705 ast_error(c, n,
1706 "Async functions are only supported in Python 3.5 and greater");
1707 return NULL;
1708 }
1709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 REQ(n, funcdef);
1711
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 name = NEW_IDENTIFIER(CHILD(n, name_i));
1713 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001714 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001715 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1718 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001719 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001720 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1721 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1722 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001723 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001724 name_i += 2;
1725 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001726 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1727 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1728 if (!type_comment)
1729 return NULL;
1730 name_i += 1;
1731 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001732 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001734 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001735 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001737 if (NCH(CHILD(n, name_i + 3)) > 1) {
1738 /* Check if the suite has a type comment in it. */
1739 tc = CHILD(CHILD(n, name_i + 3), 1);
1740
1741 if (TYPE(tc) == TYPE_COMMENT) {
1742 if (type_comment != NULL) {
1743 ast_error(c, n, "Cannot have two type comments on def");
1744 return NULL;
1745 }
1746 type_comment = NEW_TYPE_COMMENT(tc);
1747 if (!type_comment)
1748 return NULL;
1749 }
1750 }
1751
Yury Selivanov75445082015-05-11 22:57:16 -04001752 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001753 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001754 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001755 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001756 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001757 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001758}
1759
1760static stmt_ty
1761ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1762{
Guido van Rossum495da292019-03-07 12:38:08 -08001763 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001764 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001765 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001766 REQ(CHILD(n, 1), funcdef);
1767
guoci90fc8982018-09-11 17:45:45 -04001768 return ast_for_funcdef_impl(c, n, decorator_seq,
1769 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001770}
1771
1772static stmt_ty
1773ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1774{
1775 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1776 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001777 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001778}
1779
1780
1781static stmt_ty
1782ast_for_async_stmt(struct compiling *c, const node *n)
1783{
Guido van Rossum495da292019-03-07 12:38:08 -08001784 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001785 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001786 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001787
1788 switch (TYPE(CHILD(n, 1))) {
1789 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001790 return ast_for_funcdef_impl(c, n, NULL,
1791 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001792 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001793 return ast_for_with_stmt(c, n,
1794 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001795
1796 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001797 return ast_for_for_stmt(c, n,
1798 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001799
1800 default:
1801 PyErr_Format(PyExc_SystemError,
1802 "invalid async stament: %s",
1803 STR(CHILD(n, 1)));
1804 return NULL;
1805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806}
1807
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001808static stmt_ty
1809ast_for_decorated(struct compiling *c, const node *n)
1810{
Yury Selivanov75445082015-05-11 22:57:16 -04001811 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001812 stmt_ty thing = NULL;
1813 asdl_seq *decorator_seq = NULL;
1814
1815 REQ(n, decorated);
1816
1817 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1818 if (!decorator_seq)
1819 return NULL;
1820
1821 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001822 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001824
1825 if (TYPE(CHILD(n, 1)) == funcdef) {
1826 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1827 } else if (TYPE(CHILD(n, 1)) == classdef) {
1828 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001829 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1830 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001831 }
1832 return thing;
1833}
1834
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001836ast_for_namedexpr(struct compiling *c, const node *n)
1837{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001838 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001839 argument: ( test [comp_for] |
1840 test ':=' test |
1841 test '=' test |
1842 '**' test |
1843 '*' test )
1844 */
1845 expr_ty target, value;
1846
1847 target = ast_for_expr(c, CHILD(n, 0));
1848 if (!target)
1849 return NULL;
1850
1851 value = ast_for_expr(c, CHILD(n, 2));
1852 if (!value)
1853 return NULL;
1854
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001855 if (target->kind != Name_kind) {
1856 const char *expr_name = get_expr_name(target);
1857 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001858 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001859 }
1860 return NULL;
1861 }
1862
1863 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001864 return NULL;
1865
1866 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1867 n->n_end_col_offset, c->c_arena);
1868}
1869
1870static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871ast_for_lambdef(struct compiling *c, const node *n)
1872{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001873 /* lambdef: 'lambda' [varargslist] ':' test
1874 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 arguments_ty args;
1876 expr_ty expression;
1877
1878 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001879 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 if (!args)
1881 return NULL;
1882 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001883 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 }
1886 else {
1887 args = ast_for_arguments(c, CHILD(n, 1));
1888 if (!args)
1889 return NULL;
1890 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 }
1894
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001895 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1896 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897}
1898
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001899static expr_ty
1900ast_for_ifexpr(struct compiling *c, const node *n)
1901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001903 expr_ty expression, body, orelse;
1904
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001905 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001906 body = ast_for_expr(c, CHILD(n, 0));
1907 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001908 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001909 expression = ast_for_expr(c, CHILD(n, 2));
1910 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001911 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001912 orelse = ast_for_expr(c, CHILD(n, 4));
1913 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001914 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001915 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001916 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001917 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001918}
1919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001921 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922
Nick Coghlan650f0d02007-04-15 12:05:43 +00001923 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924*/
1925
1926static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001927count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001929 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930
Guido van Rossumd8faa362007-04-27 19:54:29 +00001931 count_comp_for:
1932 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001933 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001934 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001935 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001936 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001937 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001938 else if (NCH(n) == 1) {
1939 n = CHILD(n, 0);
1940 }
1941 else {
1942 goto error;
1943 }
1944 if (NCH(n) == (5)) {
1945 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001946 }
1947 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001948 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001949 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001950 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001951 REQ(n, comp_iter);
1952 n = CHILD(n, 0);
1953 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001954 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001955 else if (TYPE(n) == comp_if) {
1956 if (NCH(n) == 3) {
1957 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001958 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001959 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001960 else
1961 return n_fors;
1962 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001963
Jelle Zijlstraac317702017-10-05 20:24:46 -07001964 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001965 /* Should never be reached */
1966 PyErr_SetString(PyExc_SystemError,
1967 "logic error in count_comp_fors");
1968 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969}
1970
Nick Coghlan650f0d02007-04-15 12:05:43 +00001971/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972
Nick Coghlan650f0d02007-04-15 12:05:43 +00001973 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974*/
1975
1976static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001977count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001979 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980
Guido van Rossumd8faa362007-04-27 19:54:29 +00001981 while (1) {
1982 REQ(n, comp_iter);
1983 if (TYPE(CHILD(n, 0)) == comp_for)
1984 return n_ifs;
1985 n = CHILD(n, 0);
1986 REQ(n, comp_if);
1987 n_ifs++;
1988 if (NCH(n) == 2)
1989 return n_ifs;
1990 n = CHILD(n, 2);
1991 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992}
1993
Guido van Rossum992d4a32007-07-11 13:09:30 +00001994static asdl_seq *
1995ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001998 asdl_seq *comps;
1999
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002000 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 if (n_fors == -1)
2002 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002003
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002004 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002005 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002007
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002009 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002011 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002012 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002013 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002014 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015
Guido van Rossum992d4a32007-07-11 13:09:30 +00002016 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017
Jelle Zijlstraac317702017-10-05 20:24:46 -07002018 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002019 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002020 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002021 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002022 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002023 else {
2024 sync_n = CHILD(n, 0);
2025 }
2026 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002027
Guido van Rossum495da292019-03-07 12:38:08 -08002028 /* Async comprehensions only allowed in Python 3.6 and greater */
2029 if (is_async && c->c_feature_version < 6) {
2030 ast_error(c, n,
2031 "Async comprehensions are only supported in Python 3.6 and greater");
2032 return NULL;
2033 }
2034
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002036 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002037 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002039 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002040 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002042
Thomas Wouters89f507f2006-12-13 04:49:30 +00002043 /* Check the # of children rather than the length of t, since
2044 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002045 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002046 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002047 comp = comprehension(first, expression, NULL,
2048 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002050 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2051 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2052 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002053 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002054 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002056
Jelle Zijlstraac317702017-10-05 20:24:46 -07002057 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 int j, n_ifs;
2059 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060
Jelle Zijlstraac317702017-10-05 20:24:46 -07002061 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002062 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002066 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002071 REQ(n, comp_iter);
2072 n = CHILD(n, 0);
2073 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074
Guido van Rossum992d4a32007-07-11 13:09:30 +00002075 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002076 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002077 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002078 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002079 if (NCH(n) == 3)
2080 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002082 /* on exit, must guarantee that n is a comp_for */
2083 if (TYPE(n) == comp_iter)
2084 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002085 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002089 return comps;
2090}
2091
2092static expr_ty
2093ast_for_itercomp(struct compiling *c, const node *n, int type)
2094{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002095 /* testlist_comp: (test|star_expr)
2096 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002097 expr_ty elt;
2098 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002099 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100
Guido van Rossum992d4a32007-07-11 13:09:30 +00002101 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002103 ch = CHILD(n, 0);
2104 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002105 if (!elt)
2106 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002107 if (elt->kind == Starred_kind) {
2108 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2109 return NULL;
2110 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111
Guido van Rossum992d4a32007-07-11 13:09:30 +00002112 comps = ast_for_comprehension(c, CHILD(n, 1));
2113 if (!comps)
2114 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002115
2116 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002117 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2118 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002119 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002120 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2121 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002122 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002123 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2124 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002125 else
2126 /* Should never happen */
2127 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128}
2129
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002130/* Fills in the key, value pair corresponding to the dict element. In case
2131 * of an unpacking, key is NULL. *i is advanced by the number of ast
2132 * elements. Iff successful, nonzero is returned.
2133 */
2134static int
2135ast_for_dictelement(struct compiling *c, const node *n, int *i,
2136 expr_ty *key, expr_ty *value)
2137{
2138 expr_ty expression;
2139 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2140 assert(NCH(n) - *i >= 2);
2141
2142 expression = ast_for_expr(c, CHILD(n, *i + 1));
2143 if (!expression)
2144 return 0;
2145 *key = NULL;
2146 *value = expression;
2147
2148 *i += 2;
2149 }
2150 else {
2151 assert(NCH(n) - *i >= 3);
2152
2153 expression = ast_for_expr(c, CHILD(n, *i));
2154 if (!expression)
2155 return 0;
2156 *key = expression;
2157
2158 REQ(CHILD(n, *i + 1), COLON);
2159
2160 expression = ast_for_expr(c, CHILD(n, *i + 2));
2161 if (!expression)
2162 return 0;
2163 *value = expression;
2164
2165 *i += 3;
2166 }
2167 return 1;
2168}
2169
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002171ast_for_dictcomp(struct compiling *c, const node *n)
2172{
2173 expr_ty key, value;
2174 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002175 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002177 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002178 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002179 assert(key);
2180 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002182 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002183 if (!comps)
2184 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002186 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2187 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002188}
2189
2190static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191ast_for_dictdisplay(struct compiling *c, const node *n)
2192{
2193 int i;
2194 int j;
2195 int size;
2196 asdl_seq *keys, *values;
2197
2198 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2199 keys = _Py_asdl_seq_new(size, c->c_arena);
2200 if (!keys)
2201 return NULL;
2202
2203 values = _Py_asdl_seq_new(size, c->c_arena);
2204 if (!values)
2205 return NULL;
2206
2207 j = 0;
2208 for (i = 0; i < NCH(n); i++) {
2209 expr_ty key, value;
2210
2211 if (!ast_for_dictelement(c, n, &i, &key, &value))
2212 return NULL;
2213 asdl_seq_SET(keys, j, key);
2214 asdl_seq_SET(values, j, value);
2215
2216 j++;
2217 }
2218 keys->size = j;
2219 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002220 return Dict(keys, values, LINENO(n), n->n_col_offset,
2221 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002222}
2223
2224static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002225ast_for_genexp(struct compiling *c, const node *n)
2226{
2227 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002228 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002229}
2230
2231static expr_ty
2232ast_for_listcomp(struct compiling *c, const node *n)
2233{
2234 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002235 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002236}
2237
2238static expr_ty
2239ast_for_setcomp(struct compiling *c, const node *n)
2240{
2241 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002242 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002243}
2244
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002245static expr_ty
2246ast_for_setdisplay(struct compiling *c, const node *n)
2247{
2248 int i;
2249 int size;
2250 asdl_seq *elts;
2251
2252 assert(TYPE(n) == (dictorsetmaker));
2253 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2254 elts = _Py_asdl_seq_new(size, c->c_arena);
2255 if (!elts)
2256 return NULL;
2257 for (i = 0; i < NCH(n); i += 2) {
2258 expr_ty expression;
2259 expression = ast_for_expr(c, CHILD(n, i));
2260 if (!expression)
2261 return NULL;
2262 asdl_seq_SET(elts, i / 2, expression);
2263 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002264 return Set(elts, LINENO(n), n->n_col_offset,
2265 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002266}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002267
2268static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269ast_for_atom(struct compiling *c, const node *n)
2270{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002271 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2272 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002273 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 */
2275 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002278 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002279 PyObject *name;
2280 const char *s = STR(ch);
2281 size_t len = strlen(s);
2282 if (len >= 4 && len <= 5) {
2283 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002284 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002285 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002286 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002287 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002289 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002290 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002291 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002292 }
2293 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002294 if (!name)
2295 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002296 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002297 return Name(name, Load, LINENO(n), n->n_col_offset,
2298 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002301 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002302 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002303 const char *errtype = NULL;
2304 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2305 errtype = "unicode error";
2306 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2307 errtype = "value error";
2308 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002309 PyObject *type, *value, *tback, *errstr;
2310 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002311 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002312 if (errstr) {
2313 ast_error(c, n, "(%s) %U", errtype, errstr);
2314 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002315 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002316 else {
2317 PyErr_Clear();
2318 ast_error(c, n, "(%s) unknown error", errtype);
2319 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002320 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002321 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002322 Py_XDECREF(tback);
2323 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002324 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002325 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002326 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 }
2328 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002329 PyObject *pynum;
2330 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2331 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2332 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2333 ast_error(c, ch,
2334 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2335 return NULL;
2336 }
2337 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002338 if (!pynum)
2339 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002340
Victor Stinner43d81952013-07-17 00:57:58 +02002341 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2342 Py_DECREF(pynum);
2343 return NULL;
2344 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002345 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002346 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 }
Georg Brandldde00282007-03-18 19:01:53 +00002348 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002349 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002350 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002352 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353
Thomas Wouters89f507f2006-12-13 04:49:30 +00002354 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002355 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2356 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357
Thomas Wouters89f507f2006-12-13 04:49:30 +00002358 if (TYPE(ch) == yield_expr)
2359 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002362 if (NCH(ch) == 1) {
2363 return ast_for_testlist(c, ch);
2364 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002365
Serhiy Storchakab619b092018-11-27 09:40:29 +02002366 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002367 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002368 }
2369 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002370 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002371 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002373 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374
Thomas Wouters89f507f2006-12-13 04:49:30 +00002375 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002376 return List(NULL, Load, LINENO(n), n->n_col_offset,
2377 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378
Nick Coghlan650f0d02007-04-15 12:05:43 +00002379 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002380 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2381 asdl_seq *elts = seq_for_testlist(c, ch);
2382 if (!elts)
2383 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002384
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002385 return List(elts, Load, LINENO(n), n->n_col_offset,
2386 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002387 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002388 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002389 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002390 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002392 /* dictorsetmaker: ( ((test ':' test | '**' test)
2393 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2394 * ((test | '*' test)
2395 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002396 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002397 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002398 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002399 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002400 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2401 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002402 }
2403 else {
2404 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2405 if (NCH(ch) == 1 ||
2406 (NCH(ch) > 1 &&
2407 TYPE(CHILD(ch, 1)) == COMMA)) {
2408 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002409 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002410 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002411 else if (NCH(ch) > 1 &&
2412 TYPE(CHILD(ch, 1)) == comp_for) {
2413 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002414 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002415 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002416 else if (NCH(ch) > 3 - is_dict &&
2417 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2418 /* It's a dictionary comprehension. */
2419 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002420 ast_error(c, n,
2421 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002422 return NULL;
2423 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002424 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 }
2426 else {
2427 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002428 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002429 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002430 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002434 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2435 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 }
2437}
2438
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002439static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440ast_for_slice(struct compiling *c, const node *n)
2441{
2442 node *ch;
2443 expr_ty lower = NULL, upper = NULL, step = NULL;
2444
2445 REQ(n, subscript);
2446
2447 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002448 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 sliceop: ':' [test]
2450 */
2451 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002453 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 }
2455
2456 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002457 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 if (!lower)
2459 return NULL;
2460 }
2461
2462 /* If there's an upper bound it's in the second or third position. */
2463 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002464 if (NCH(n) > 1) {
2465 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466
Thomas Wouters89f507f2006-12-13 04:49:30 +00002467 if (TYPE(n2) == test) {
2468 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 if (!upper)
2470 return NULL;
2471 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002474 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475
Thomas Wouters89f507f2006-12-13 04:49:30 +00002476 if (TYPE(n2) == test) {
2477 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 if (!upper)
2479 return NULL;
2480 }
2481 }
2482
2483 ch = CHILD(n, NCH(n) - 1);
2484 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002485 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002486 ch = CHILD(ch, 1);
2487 if (TYPE(ch) == test) {
2488 step = ast_for_expr(c, ch);
2489 if (!step)
2490 return NULL;
2491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 }
2493 }
2494
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002495 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2496 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497}
2498
2499static expr_ty
2500ast_for_binop(struct compiling *c, const node *n)
2501{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002502 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002504 BinOp(BinOp(A, op, B), op, C).
2505 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506
Guido van Rossumd8faa362007-04-27 19:54:29 +00002507 int i, nops;
2508 expr_ty expr1, expr2, result;
2509 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510
Guido van Rossumd8faa362007-04-27 19:54:29 +00002511 expr1 = ast_for_expr(c, CHILD(n, 0));
2512 if (!expr1)
2513 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
Guido van Rossumd8faa362007-04-27 19:54:29 +00002515 expr2 = ast_for_expr(c, CHILD(n, 2));
2516 if (!expr2)
2517 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518
Guido van Rossum495da292019-03-07 12:38:08 -08002519 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002520 if (!newoperator)
2521 return NULL;
2522
2523 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002524 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002525 c->c_arena);
2526 if (!result)
2527 return NULL;
2528
2529 nops = (NCH(n) - 1) / 2;
2530 for (i = 1; i < nops; i++) {
2531 expr_ty tmp_result, tmp;
2532 const node* next_oper = CHILD(n, i * 2 + 1);
2533
Guido van Rossum495da292019-03-07 12:38:08 -08002534 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002535 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 return NULL;
2537
Guido van Rossumd8faa362007-04-27 19:54:29 +00002538 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2539 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 return NULL;
2541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002543 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002544 CHILD(n, i * 2 + 2)->n_end_lineno,
2545 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002546 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002548 return NULL;
2549 result = tmp_result;
2550 }
2551 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002554static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002555ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002558 subscriptlist: subscript (',' subscript)* [',']
2559 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2560 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002561 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002562 REQ(n, trailer);
2563 if (TYPE(CHILD(n, 0)) == LPAR) {
2564 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002565 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002566 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002567 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002568 return ast_for_call(c, CHILD(n, 1), left_expr,
2569 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002570 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002571 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002572 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2573 if (!attr_id)
2574 return NULL;
2575 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002576 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002577 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002578 }
2579 else {
2580 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002581 REQ(CHILD(n, 2), RSQB);
2582 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002583 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002584 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002585 if (!slc)
2586 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002587 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002588 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002589 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002590 }
2591 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002592 int j;
2593 expr_ty slc, e;
2594 asdl_seq *elts;
2595 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2596 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002597 return NULL;
2598 for (j = 0; j < NCH(n); j += 2) {
2599 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002600 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002601 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002602 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002603 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002604 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002605 n->n_end_lineno, n->n_end_col_offset,
2606 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002607 if (!e)
2608 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002609 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002610 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002611 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2612 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002613 }
2614 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002615}
2616
2617static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002618ast_for_factor(struct compiling *c, const node *n)
2619{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002620 expr_ty expression;
2621
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002622 expression = ast_for_expr(c, CHILD(n, 1));
2623 if (!expression)
2624 return NULL;
2625
2626 switch (TYPE(CHILD(n, 0))) {
2627 case PLUS:
2628 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002629 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002630 c->c_arena);
2631 case MINUS:
2632 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002633 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002634 c->c_arena);
2635 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002636 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2637 n->n_end_lineno, n->n_end_col_offset,
2638 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002639 }
2640 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2641 TYPE(CHILD(n, 0)));
2642 return NULL;
2643}
2644
2645static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002646ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002647{
Yury Selivanov75445082015-05-11 22:57:16 -04002648 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002649 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002650
2651 REQ(n, atom_expr);
2652 nch = NCH(n);
2653
Guido van Rossum495da292019-03-07 12:38:08 -08002654 if (TYPE(CHILD(n, 0)) == AWAIT) {
2655 if (c->c_feature_version < 5) {
2656 ast_error(c, n,
2657 "Await expressions are only supported in Python 3.5 and greater");
2658 return NULL;
2659 }
Yury Selivanov75445082015-05-11 22:57:16 -04002660 start = 1;
2661 assert(nch > 1);
2662 }
2663
2664 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002665 if (!e)
2666 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002667 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002668 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002669 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002670 return Await(e, LINENO(n), n->n_col_offset,
2671 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002672 }
2673
2674 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002675 node *ch = CHILD(n, i);
2676 if (TYPE(ch) != trailer)
2677 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002678 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2679 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002680 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002681 }
Yury Selivanov75445082015-05-11 22:57:16 -04002682
2683 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002684 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002685 return Await(e, LINENO(n), n->n_col_offset,
2686 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002687 }
2688 else {
2689 return e;
2690 }
2691}
2692
2693static expr_ty
2694ast_for_power(struct compiling *c, const node *n)
2695{
2696 /* power: atom trailer* ('**' factor)*
2697 */
2698 expr_ty e;
2699 REQ(n, power);
2700 e = ast_for_atom_expr(c, CHILD(n, 0));
2701 if (!e)
2702 return NULL;
2703 if (NCH(n) == 1)
2704 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002705 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2706 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002707 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002708 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002709 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2710 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002711 }
2712 return e;
2713}
2714
Guido van Rossum0368b722007-05-11 16:50:42 +00002715static expr_ty
2716ast_for_starred(struct compiling *c, const node *n)
2717{
2718 expr_ty tmp;
2719 REQ(n, star_expr);
2720
2721 tmp = ast_for_expr(c, CHILD(n, 1));
2722 if (!tmp)
2723 return NULL;
2724
2725 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002726 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2727 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002728}
2729
2730
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731/* Do not name a variable 'expr'! Will cause a compile error.
2732*/
2733
2734static expr_ty
2735ast_for_expr(struct compiling *c, const node *n)
2736{
2737 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002738 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002739 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002740 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 and_test: not_test ('and' not_test)*
2743 not_test: 'not' not_test | comparison
2744 comparison: expr (comp_op expr)*
2745 expr: xor_expr ('|' xor_expr)*
2746 xor_expr: and_expr ('^' and_expr)*
2747 and_expr: shift_expr ('&' shift_expr)*
2748 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2749 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002750 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002752 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002753 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002754 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 */
2756
2757 asdl_seq *seq;
2758 int i;
2759
2760 loop:
2761 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002762 case namedexpr_test:
2763 if (NCH(n) == 3)
2764 return ast_for_namedexpr(c, n);
2765 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002767 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002768 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002769 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002771 else if (NCH(n) > 1)
2772 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002773 /* Fallthrough */
2774 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 case and_test:
2776 if (NCH(n) == 1) {
2777 n = CHILD(n, 0);
2778 goto loop;
2779 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002780 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 if (!seq)
2782 return NULL;
2783 for (i = 0; i < NCH(n); i += 2) {
2784 expr_ty e = ast_for_expr(c, CHILD(n, i));
2785 if (!e)
2786 return NULL;
2787 asdl_seq_SET(seq, i / 2, e);
2788 }
2789 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002790 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002791 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002792 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002793 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002794 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2795 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 case not_test:
2797 if (NCH(n) == 1) {
2798 n = CHILD(n, 0);
2799 goto loop;
2800 }
2801 else {
2802 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2803 if (!expression)
2804 return NULL;
2805
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002806 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002807 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002808 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 }
2810 case comparison:
2811 if (NCH(n) == 1) {
2812 n = CHILD(n, 0);
2813 goto loop;
2814 }
2815 else {
2816 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002817 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002819 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 if (!ops)
2821 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002822 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 return NULL;
2825 }
2826 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002827 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002829 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002830 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833
2834 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002835 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002839 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 asdl_seq_SET(cmps, i / 2, expression);
2841 }
2842 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002843 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002845 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002847 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2848 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
Guido van Rossum0368b722007-05-11 16:50:42 +00002851 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 /* The next five cases all handle BinOps. The main body of code
2854 is the same in each case, but the switch turned inside out to
2855 reuse the code for each type of operator.
2856 */
2857 case expr:
2858 case xor_expr:
2859 case and_expr:
2860 case shift_expr:
2861 case arith_expr:
2862 case term:
2863 if (NCH(n) == 1) {
2864 n = CHILD(n, 0);
2865 goto loop;
2866 }
2867 return ast_for_binop(c, n);
2868 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002869 node *an = NULL;
2870 node *en = NULL;
2871 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002872 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002873 if (NCH(n) > 1)
2874 an = CHILD(n, 1); /* yield_arg */
2875 if (an) {
2876 en = CHILD(an, NCH(an) - 1);
2877 if (NCH(an) == 2) {
2878 is_from = 1;
2879 exp = ast_for_expr(c, en);
2880 }
2881 else
2882 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002883 if (!exp)
2884 return NULL;
2885 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002886 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002887 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2888 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2889 return Yield(exp, LINENO(n), n->n_col_offset,
2890 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002892 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 if (NCH(n) == 1) {
2894 n = CHILD(n, 0);
2895 goto loop;
2896 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002897 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002898 case power:
2899 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002901 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 return NULL;
2903 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002904 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 return NULL;
2906}
2907
2908static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002909ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002910 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911{
2912 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002913 arglist: argument (',' argument)* [',']
2914 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 */
2916
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002917 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002918 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002919 asdl_seq *args;
2920 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
2922 REQ(n, arglist);
2923
2924 nargs = 0;
2925 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002927 node *ch = CHILD(n, i);
2928 if (TYPE(ch) == argument) {
2929 if (NCH(ch) == 1)
2930 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002931 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2932 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002933 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002934 ast_error(c, ch, "invalid syntax");
2935 return NULL;
2936 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002937 if (NCH(n) > 1) {
2938 ast_error(c, ch, "Generator expression must be parenthesized");
2939 return NULL;
2940 }
2941 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002942 else if (TYPE(CHILD(ch, 0)) == STAR)
2943 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002944 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2945 nargs++;
2946 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002948 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002949 nkeywords++;
2950 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002953 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002955 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002956 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002958 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002959
2960 nargs = 0; /* positional arguments + iterable argument unpackings */
2961 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2962 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002964 node *ch = CHILD(n, i);
2965 if (TYPE(ch) == argument) {
2966 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002967 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002968 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002969 /* a positional argument */
2970 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002971 if (ndoublestars) {
2972 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002973 "positional argument follows "
2974 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002975 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002976 else {
2977 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002978 "positional argument follows "
2979 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002980 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002981 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002982 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002983 e = ast_for_expr(c, chch);
2984 if (!e)
2985 return NULL;
2986 asdl_seq_SET(args, nargs++, e);
2987 }
2988 else if (TYPE(chch) == STAR) {
2989 /* an iterable argument unpacking */
2990 expr_ty starred;
2991 if (ndoublestars) {
2992 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002993 "iterable argument unpacking follows "
2994 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002995 return NULL;
2996 }
2997 e = ast_for_expr(c, CHILD(ch, 1));
2998 if (!e)
2999 return NULL;
3000 starred = Starred(e, Load, LINENO(chch),
3001 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003002 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003003 c->c_arena);
3004 if (!starred)
3005 return NULL;
3006 asdl_seq_SET(args, nargs++, starred);
3007
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003008 }
3009 else if (TYPE(chch) == DOUBLESTAR) {
3010 /* a keyword argument unpacking */
3011 keyword_ty kw;
3012 i++;
3013 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003015 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003016 kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset,
3017 e->end_lineno, e->end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003018 asdl_seq_SET(keywords, nkeywords++, kw);
3019 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003021 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003022 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003023 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003025 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003026 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003028 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3029 /* treat colon equal as positional argument */
3030 if (nkeywords) {
3031 if (ndoublestars) {
3032 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003033 "positional argument follows "
3034 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003035 }
3036 else {
3037 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003038 "positional argument follows "
3039 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003040 }
3041 return NULL;
3042 }
3043 e = ast_for_namedexpr(c, ch);
3044 if (!e)
3045 return NULL;
3046 asdl_seq_SET(args, nargs++, e);
3047 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003049 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003051 identifier key, tmp;
3052 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003054 // To remain LL(1), the grammar accepts any test (basically, any
3055 // expression) in the keyword slot of a call site. So, we need
3056 // to manually enforce that the keyword is a NAME here.
3057 static const int name_tree[] = {
3058 test,
3059 or_test,
3060 and_test,
3061 not_test,
3062 comparison,
3063 expr,
3064 xor_expr,
3065 and_expr,
3066 shift_expr,
3067 arith_expr,
3068 term,
3069 factor,
3070 power,
3071 atom_expr,
3072 atom,
3073 0,
3074 };
3075 node *expr_node = chch;
3076 for (int i = 0; name_tree[i]; i++) {
3077 if (TYPE(expr_node) != name_tree[i])
3078 break;
3079 if (NCH(expr_node) != 1)
3080 break;
3081 expr_node = CHILD(expr_node, 0);
3082 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003083 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003084 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003085 "expression cannot contain assignment, "
3086 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003087 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003088 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003089 key = new_identifier(STR(expr_node), c);
3090 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003091 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003093 if (forbidden_name(c, key, chch, 1)) {
3094 return NULL;
3095 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003096 for (k = 0; k < nkeywords; k++) {
3097 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003098 if (tmp && !PyUnicode_Compare(tmp, key)) {
3099 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003100 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003101 return NULL;
3102 }
3103 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003104 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003106 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003107 kw = keyword(key, e, chch->n_lineno, chch->n_col_offset,
3108 chch->n_end_lineno, chch->n_end_col_offset, c->c_arena);
3109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003111 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003112 asdl_seq_SET(keywords, nkeywords++, kw);
3113 }
3114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 }
3116
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003117 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003118 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119}
3120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003122ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003124 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003125 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003127 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003128 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003129 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003130 }
3131 else {
3132 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003133 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003134 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003136 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 else {
3138 asdl_seq *tmp = seq_for_testlist(c, n);
3139 if (!tmp)
3140 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003141 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3142 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003144}
3145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146static stmt_ty
3147ast_for_expr_stmt(struct compiling *c, const node *n)
3148{
3149 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003150 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003151 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3152 annassign: ':' test ['=' (yield_expr|testlist)]
3153 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3154 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3155 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003156 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003158 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003160 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003161 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 if (!e)
3163 return NULL;
3164
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003165 return Expr(e, LINENO(n), n->n_col_offset,
3166 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 }
3168 else if (TYPE(CHILD(n, 1)) == augassign) {
3169 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003170 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003171 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172
Thomas Wouters89f507f2006-12-13 04:49:30 +00003173 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 if (!expr1)
3175 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003176 if(!set_context(c, expr1, Store, ch))
3177 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003178 /* set_context checks that most expressions are not the left side.
3179 Augmented assignments can only have a name, a subscript, or an
3180 attribute on the left, though, so we have to explicitly check for
3181 those. */
3182 switch (expr1->kind) {
3183 case Name_kind:
3184 case Attribute_kind:
3185 case Subscript_kind:
3186 break;
3187 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003188 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003189 return NULL;
3190 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191
Thomas Wouters89f507f2006-12-13 04:49:30 +00003192 ch = CHILD(n, 2);
3193 if (TYPE(ch) == testlist)
3194 expr2 = ast_for_testlist(c, ch);
3195 else
3196 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003197 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 return NULL;
3199
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003200 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003201 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 return NULL;
3203
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003204 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3205 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003207 else if (TYPE(CHILD(n, 1)) == annassign) {
3208 expr_ty expr1, expr2, expr3;
3209 node *ch = CHILD(n, 0);
3210 node *deep, *ann = CHILD(n, 1);
3211 int simple = 1;
3212
Guido van Rossum495da292019-03-07 12:38:08 -08003213 /* AnnAssigns are only allowed in Python 3.6 or greater */
3214 if (c->c_feature_version < 6) {
3215 ast_error(c, ch,
3216 "Variable annotation syntax is only supported in Python 3.6 and greater");
3217 return NULL;
3218 }
3219
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003220 /* we keep track of parens to qualify (x) as expression not name */
3221 deep = ch;
3222 while (NCH(deep) == 1) {
3223 deep = CHILD(deep, 0);
3224 }
3225 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3226 simple = 0;
3227 }
3228 expr1 = ast_for_testlist(c, ch);
3229 if (!expr1) {
3230 return NULL;
3231 }
3232 switch (expr1->kind) {
3233 case Name_kind:
3234 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3235 return NULL;
3236 }
3237 expr1->v.Name.ctx = Store;
3238 break;
3239 case Attribute_kind:
3240 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3241 return NULL;
3242 }
3243 expr1->v.Attribute.ctx = Store;
3244 break;
3245 case Subscript_kind:
3246 expr1->v.Subscript.ctx = Store;
3247 break;
3248 case List_kind:
3249 ast_error(c, ch,
3250 "only single target (not list) can be annotated");
3251 return NULL;
3252 case Tuple_kind:
3253 ast_error(c, ch,
3254 "only single target (not tuple) can be annotated");
3255 return NULL;
3256 default:
3257 ast_error(c, ch,
3258 "illegal target for annotation");
3259 return NULL;
3260 }
3261
3262 if (expr1->kind != Name_kind) {
3263 simple = 0;
3264 }
3265 ch = CHILD(ann, 1);
3266 expr2 = ast_for_expr(c, ch);
3267 if (!expr2) {
3268 return NULL;
3269 }
3270 if (NCH(ann) == 2) {
3271 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003272 LINENO(n), n->n_col_offset,
3273 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003274 }
3275 else {
3276 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003277 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003278 expr3 = ast_for_testlist(c, ch);
3279 }
3280 else {
3281 expr3 = ast_for_expr(c, ch);
3282 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003283 if (!expr3) {
3284 return NULL;
3285 }
3286 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003287 LINENO(n), n->n_col_offset,
3288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003289 }
3290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003292 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003293 asdl_seq *targets;
3294 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003296 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 /* a normal assignment */
3299 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003300
3301 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3302 nch_minus_type = num - has_type_comment;
3303
3304 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 if (!targets)
3306 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003307 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 expr_ty e;
3309 node *ch = CHILD(n, i);
3310 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003311 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 return NULL;
3313 }
3314 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003318 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003319 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 asdl_seq_SET(targets, i / 2, e);
3323 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003324 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003325 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 expression = ast_for_testlist(c, value);
3327 else
3328 expression = ast_for_expr(c, value);
3329 if (!expression)
3330 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003331 if (has_type_comment) {
3332 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3333 if (!type_comment)
3334 return NULL;
3335 }
3336 else
3337 type_comment = NULL;
3338 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003339 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
Benjamin Peterson78565b22009-06-28 19:19:51 +00003343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003345ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346{
3347 asdl_seq *seq;
3348 int i;
3349 expr_ty e;
3350
3351 REQ(n, exprlist);
3352
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003353 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 e = ast_for_expr(c, CHILD(n, i));
3358 if (!e)
3359 return NULL;
3360 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003361 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 }
3364 return seq;
3365}
3366
3367static stmt_ty
3368ast_for_del_stmt(struct compiling *c, const node *n)
3369{
3370 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 /* del_stmt: 'del' exprlist */
3373 REQ(n, del_stmt);
3374
3375 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3376 if (!expr_list)
3377 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003378 return Delete(expr_list, LINENO(n), n->n_col_offset,
3379 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380}
3381
3382static stmt_ty
3383ast_for_flow_stmt(struct compiling *c, const node *n)
3384{
3385 /*
3386 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3387 | yield_stmt
3388 break_stmt: 'break'
3389 continue_stmt: 'continue'
3390 return_stmt: 'return' [testlist]
3391 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003392 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 raise_stmt: 'raise' [test [',' test [',' test]]]
3394 */
3395 node *ch;
3396
3397 REQ(n, flow_stmt);
3398 ch = CHILD(n, 0);
3399 switch (TYPE(ch)) {
3400 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003401 return Break(LINENO(n), n->n_col_offset,
3402 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003404 return Continue(LINENO(n), n->n_col_offset,
3405 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003407 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3408 if (!exp)
3409 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003410 return Expr(exp, LINENO(n), n->n_col_offset,
3411 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 }
3413 case return_stmt:
3414 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003415 return Return(NULL, LINENO(n), n->n_col_offset,
3416 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003418 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 if (!expression)
3420 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003421 return Return(expression, LINENO(n), n->n_col_offset,
3422 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 }
3424 case raise_stmt:
3425 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003426 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3427 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003428 else if (NCH(ch) >= 2) {
3429 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3431 if (!expression)
3432 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003433 if (NCH(ch) == 4) {
3434 cause = ast_for_expr(c, CHILD(ch, 3));
3435 if (!cause)
3436 return NULL;
3437 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003438 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3439 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
Stefan Krahf432a322017-08-21 13:09:59 +02003441 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003443 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 "unexpected flow_stmt: %d", TYPE(ch));
3445 return NULL;
3446 }
3447}
3448
3449static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003450alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451{
3452 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003453 import_as_name: NAME ['as' NAME]
3454 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 dotted_name: NAME ('.' NAME)*
3456 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003457 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 loop:
3460 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003461 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003462 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003463 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003464 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003465 if (!name)
3466 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003467 if (NCH(n) == 3) {
3468 node *str_node = CHILD(n, 2);
3469 str = NEW_IDENTIFIER(str_node);
3470 if (!str)
3471 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003472 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003473 return NULL;
3474 }
3475 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003476 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003477 return NULL;
3478 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003479 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003480 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 case dotted_as_name:
3482 if (NCH(n) == 1) {
3483 n = CHILD(n, 0);
3484 goto loop;
3485 }
3486 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003487 node *asname_node = CHILD(n, 2);
3488 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003489 if (!a)
3490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003492 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003493 if (!a->asname)
3494 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003495 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 return a;
3498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003500 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003501 node *name_node = CHILD(n, 0);
3502 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003503 if (!name)
3504 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003505 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003506 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003507 return alias(name, NULL, c->c_arena);
3508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 else {
3510 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003511 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003512 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515
3516 len = 0;
3517 for (i = 0; i < NCH(n); i += 2)
3518 /* length of string plus one for the dot */
3519 len += strlen(STR(CHILD(n, i))) + 1;
3520 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003521 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 if (!str)
3523 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003524 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 if (!s)
3526 return NULL;
3527 for (i = 0; i < NCH(n); i += 2) {
3528 char *sch = STR(CHILD(n, i));
3529 strcpy(s, STR(CHILD(n, i)));
3530 s += strlen(sch);
3531 *s++ = '.';
3532 }
3533 --s;
3534 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3536 PyBytes_GET_SIZE(str),
3537 NULL);
3538 Py_DECREF(str);
3539 if (!uni)
3540 return NULL;
3541 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003542 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003543 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3544 Py_DECREF(str);
3545 return NULL;
3546 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003547 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003550 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003551 if (!str)
3552 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003553 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3554 Py_DECREF(str);
3555 return NULL;
3556 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003557 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003559 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 "unexpected import name: %d", TYPE(n));
3561 return NULL;
3562 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563}
3564
3565static stmt_ty
3566ast_for_import_stmt(struct compiling *c, const node *n)
3567{
3568 /*
3569 import_stmt: import_name | import_from
3570 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003571 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3572 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003574 int lineno;
3575 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 int i;
3577 asdl_seq *aliases;
3578
3579 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003580 lineno = LINENO(n);
3581 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003583 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003585 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003586 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003587 if (!aliases)
3588 return NULL;
3589 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003590 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003591 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003593 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003595 // Even though n is modified above, the end position is not changed
3596 return Import(aliases, lineno, col_offset,
3597 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003599 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003601 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003602 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003604 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003606 /* Count the number of dots (for relative imports) and check for the
3607 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003608 for (idx = 1; idx < NCH(n); idx++) {
3609 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003610 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3611 if (!mod)
3612 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 idx++;
3614 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003615 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003617 ndots += 3;
3618 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 } else if (TYPE(CHILD(n, idx)) != DOT) {
3620 break;
3621 }
3622 ndots++;
3623 }
3624 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003625 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003626 case STAR:
3627 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003628 n = CHILD(n, idx);
3629 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003630 break;
3631 case LPAR:
3632 /* from ... import (x, y, z) */
3633 n = CHILD(n, idx + 1);
3634 n_children = NCH(n);
3635 break;
3636 case import_as_names:
3637 /* from ... import x, y, z */
3638 n = CHILD(n, idx);
3639 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003640 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003641 ast_error(c, n,
3642 "trailing comma not allowed without"
3643 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 return NULL;
3645 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 break;
3647 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003648 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003649 return NULL;
3650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003652 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003653 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655
3656 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003657 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003658 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003659 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003661 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003663 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003665 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003666 if (!import_alias)
3667 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003668 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003671 if (mod != NULL)
3672 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003673 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003674 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003675 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 }
Neal Norwitz79792652005-11-14 04:25:03 +00003677 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 "unknown import statement: starts with command '%s'",
3679 STR(CHILD(n, 0)));
3680 return NULL;
3681}
3682
3683static stmt_ty
3684ast_for_global_stmt(struct compiling *c, const node *n)
3685{
3686 /* global_stmt: 'global' NAME (',' NAME)* */
3687 identifier name;
3688 asdl_seq *s;
3689 int i;
3690
3691 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003692 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003694 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003696 name = NEW_IDENTIFIER(CHILD(n, i));
3697 if (!name)
3698 return NULL;
3699 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003701 return Global(s, LINENO(n), n->n_col_offset,
3702 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703}
3704
3705static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003706ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3707{
3708 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3709 identifier name;
3710 asdl_seq *s;
3711 int i;
3712
3713 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003714 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003715 if (!s)
3716 return NULL;
3717 for (i = 1; i < NCH(n); i += 2) {
3718 name = NEW_IDENTIFIER(CHILD(n, i));
3719 if (!name)
3720 return NULL;
3721 asdl_seq_SET(s, i / 2, name);
3722 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003723 return Nonlocal(s, LINENO(n), n->n_col_offset,
3724 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003725}
3726
3727static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728ast_for_assert_stmt(struct compiling *c, const node *n)
3729{
3730 /* assert_stmt: 'assert' test [',' test] */
3731 REQ(n, assert_stmt);
3732 if (NCH(n) == 2) {
3733 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3734 if (!expression)
3735 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003736 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3737 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 }
3739 else if (NCH(n) == 4) {
3740 expr_ty expr1, expr2;
3741
3742 expr1 = ast_for_expr(c, CHILD(n, 1));
3743 if (!expr1)
3744 return NULL;
3745 expr2 = ast_for_expr(c, CHILD(n, 3));
3746 if (!expr2)
3747 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003749 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3750 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 }
Neal Norwitz79792652005-11-14 04:25:03 +00003752 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 "improper number of parts to 'assert' statement: %d",
3754 NCH(n));
3755 return NULL;
3756}
3757
3758static asdl_seq *
3759ast_for_suite(struct compiling *c, const node *n)
3760{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003761 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003762 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 stmt_ty s;
3764 int i, total, num, end, pos = 0;
3765 node *ch;
3766
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003767 if (TYPE(n) != func_body_suite) {
3768 REQ(n, suite);
3769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770
3771 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003772 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003776 n = CHILD(n, 0);
3777 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003779 */
3780 end = NCH(n) - 1;
3781 if (TYPE(CHILD(n, end - 1)) == SEMI)
3782 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003784 for (i = 0; i < end; i += 2) {
3785 ch = CHILD(n, i);
3786 s = ast_for_stmt(c, ch);
3787 if (!s)
3788 return NULL;
3789 asdl_seq_SET(seq, pos++, s);
3790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 }
3792 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003793 i = 2;
3794 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3795 i += 2;
3796 REQ(CHILD(n, 2), NEWLINE);
3797 }
3798
3799 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003800 ch = CHILD(n, i);
3801 REQ(ch, stmt);
3802 num = num_stmts(ch);
3803 if (num == 1) {
3804 /* small_stmt or compound_stmt with only one child */
3805 s = ast_for_stmt(c, ch);
3806 if (!s)
3807 return NULL;
3808 asdl_seq_SET(seq, pos++, s);
3809 }
3810 else {
3811 int j;
3812 ch = CHILD(ch, 0);
3813 REQ(ch, simple_stmt);
3814 for (j = 0; j < NCH(ch); j += 2) {
3815 /* statement terminates with a semi-colon ';' */
3816 if (NCH(CHILD(ch, j)) == 0) {
3817 assert((j + 1) == NCH(ch));
3818 break;
3819 }
3820 s = ast_for_stmt(c, CHILD(ch, j));
3821 if (!s)
3822 return NULL;
3823 asdl_seq_SET(seq, pos++, s);
3824 }
3825 }
3826 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 }
3828 assert(pos == seq->size);
3829 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830}
3831
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003832static void
3833get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3834{
Pablo Galindo46a97922019-02-19 22:51:53 +00003835 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003836 // There must be no empty suites.
3837 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003838 stmt_ty last = asdl_seq_GET(s, tot - 1);
3839 *end_lineno = last->end_lineno;
3840 *end_col_offset = last->end_col_offset;
3841}
3842
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843static stmt_ty
3844ast_for_if_stmt(struct compiling *c, const node *n)
3845{
3846 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3847 ['else' ':' suite]
3848 */
3849 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003850 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851
3852 REQ(n, if_stmt);
3853
3854 if (NCH(n) == 4) {
3855 expr_ty expression;
3856 asdl_seq *suite_seq;
3857
3858 expression = ast_for_expr(c, CHILD(n, 1));
3859 if (!expression)
3860 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003862 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003864 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865
Guido van Rossumd8faa362007-04-27 19:54:29 +00003866 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003867 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 s = STR(CHILD(n, 4));
3871 /* s[2], the third character in the string, will be
3872 's' for el_s_e, or
3873 'i' for el_i_f
3874 */
3875 if (s[2] == 's') {
3876 expr_ty expression;
3877 asdl_seq *seq1, *seq2;
3878
3879 expression = ast_for_expr(c, CHILD(n, 1));
3880 if (!expression)
3881 return NULL;
3882 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003883 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 return NULL;
3885 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003886 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003888 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889
Guido van Rossumd8faa362007-04-27 19:54:29 +00003890 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003891 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892 }
3893 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003894 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003895 expr_ty expression;
3896 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003897 asdl_seq *orelse = NULL;
3898 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 /* must reference the child n_elif+1 since 'else' token is third,
3900 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003901 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3902 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3903 has_else = 1;
3904 n_elif -= 3;
3905 }
3906 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907
Thomas Wouters89f507f2006-12-13 04:49:30 +00003908 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003909 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003911 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003912 if (!orelse)
3913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003915 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003917 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3918 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003920 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3921 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003923 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 asdl_seq_SET(orelse, 0,
3926 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003927 LINENO(CHILD(n, NCH(n) - 7)),
3928 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003929 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003930 /* the just-created orelse handled the last elif */
3931 n_elif--;
3932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933
Thomas Wouters89f507f2006-12-13 04:49:30 +00003934 for (i = 0; i < n_elif; i++) {
3935 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003936 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003937 if (!newobj)
3938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003940 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003943 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003946 if (orelse != NULL) {
3947 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3948 } else {
3949 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3950 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003951 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003953 LINENO(CHILD(n, off - 1)),
3954 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003955 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003956 orelse = newobj;
3957 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 expression = ast_for_expr(c, CHILD(n, 1));
3959 if (!expression)
3960 return NULL;
3961 suite_seq = ast_for_suite(c, CHILD(n, 3));
3962 if (!suite_seq)
3963 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003964 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003965 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003966 LINENO(n), n->n_col_offset,
3967 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003969
3970 PyErr_Format(PyExc_SystemError,
3971 "unexpected token in 'if' statement: %s", s);
3972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973}
3974
3975static stmt_ty
3976ast_for_while_stmt(struct compiling *c, const node *n)
3977{
3978 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3979 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003980 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981
3982 if (NCH(n) == 4) {
3983 expr_ty expression;
3984 asdl_seq *suite_seq;
3985
3986 expression = ast_for_expr(c, CHILD(n, 1));
3987 if (!expression)
3988 return NULL;
3989 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003990 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003992 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3993 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3994 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 }
3996 else if (NCH(n) == 7) {
3997 expr_ty expression;
3998 asdl_seq *seq1, *seq2;
3999
4000 expression = ast_for_expr(c, CHILD(n, 1));
4001 if (!expression)
4002 return NULL;
4003 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004004 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 return NULL;
4006 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004007 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004009 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004011 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4012 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004014
4015 PyErr_Format(PyExc_SystemError,
4016 "wrong number of tokens for 'while' statement: %d",
4017 NCH(n));
4018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019}
4020
4021static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004022ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023{
guoci90fc8982018-09-11 17:45:45 -04004024 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004025 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004027 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004028 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004029 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004030 int has_type_comment;
4031 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004032
4033 if (is_async && c->c_feature_version < 5) {
4034 ast_error(c, n,
4035 "Async for loops are only supported in Python 3.5 and greater");
4036 return NULL;
4037 }
4038
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004039 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 REQ(n, for_stmt);
4041
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004042 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4043
4044 if (NCH(n) == 9 + has_type_comment) {
4045 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 if (!seq)
4047 return NULL;
4048 }
4049
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004050 node_target = CHILD(n, 1);
4051 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004052 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004054 /* Check the # of children rather than the length of _target, since
4055 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004056 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004057 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004058 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004060 target = Tuple(_target, Store, first->lineno, first->col_offset,
4061 node_target->n_end_lineno, node_target->n_end_col_offset,
4062 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004064 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004065 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004067 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004068 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 return NULL;
4070
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004071 if (seq != NULL) {
4072 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4073 } else {
4074 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4075 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004076
4077 if (has_type_comment) {
4078 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4079 if (!type_comment)
4080 return NULL;
4081 }
4082 else
4083 type_comment = NULL;
4084
Yury Selivanov75445082015-05-11 22:57:16 -04004085 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004086 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004087 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004088 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004089 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004090 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004091 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004092 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093}
4094
4095static excepthandler_ty
4096ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4097{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004098 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004099 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004100 REQ(exc, except_clause);
4101 REQ(body, suite);
4102
4103 if (NCH(exc) == 1) {
4104 asdl_seq *suite_seq = ast_for_suite(c, body);
4105 if (!suite_seq)
4106 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004107 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108
Neal Norwitzad74aa82008-03-31 05:14:30 +00004109 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004110 exc->n_col_offset,
4111 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112 }
4113 else if (NCH(exc) == 2) {
4114 expr_ty expression;
4115 asdl_seq *suite_seq;
4116
4117 expression = ast_for_expr(c, CHILD(exc, 1));
4118 if (!expression)
4119 return NULL;
4120 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004121 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 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(expression, 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) == 4) {
4130 asdl_seq *suite_seq;
4131 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004132 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004133 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004135 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004136 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004138 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 return NULL;
4140 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004141 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004143 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144
Neal Norwitzad74aa82008-03-31 05:14:30 +00004145 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004146 exc->n_col_offset,
4147 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004149
4150 PyErr_Format(PyExc_SystemError,
4151 "wrong number of children for 'except' clause: %d",
4152 NCH(exc));
4153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154}
4155
4156static stmt_ty
4157ast_for_try_stmt(struct compiling *c, const node *n)
4158{
Neal Norwitzf599f422005-12-17 21:33:47 +00004159 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004161 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004162 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004164 REQ(n, try_stmt);
4165
Neal Norwitzf599f422005-12-17 21:33:47 +00004166 body = ast_for_suite(c, CHILD(n, 2));
4167 if (body == NULL)
4168 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004169
Neal Norwitzf599f422005-12-17 21:33:47 +00004170 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4171 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4172 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4173 /* we can assume it's an "else",
4174 because nch >= 9 for try-else-finally and
4175 it would otherwise have a type of except_clause */
4176 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4177 if (orelse == NULL)
4178 return NULL;
4179 n_except--;
4180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181
Neal Norwitzf599f422005-12-17 21:33:47 +00004182 finally = ast_for_suite(c, CHILD(n, nch - 1));
4183 if (finally == NULL)
4184 return NULL;
4185 n_except--;
4186 }
4187 else {
4188 /* we can assume it's an "else",
4189 otherwise it would have a type of except_clause */
4190 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4191 if (orelse == NULL)
4192 return NULL;
4193 n_except--;
4194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004196 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004197 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198 return NULL;
4199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200
Neal Norwitzf599f422005-12-17 21:33:47 +00004201 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004202 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004203 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004204 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004205 if (handlers == NULL)
4206 return NULL;
4207
4208 for (i = 0; i < n_except; i++) {
4209 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4210 CHILD(n, 5 + i * 3));
4211 if (!e)
4212 return NULL;
4213 asdl_seq_SET(handlers, i, e);
4214 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004215 }
4216
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004217 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004218 if (finally != NULL) {
4219 // finally is always last
4220 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4221 } else if (orelse != NULL) {
4222 // otherwise else is last
4223 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4224 } else {
4225 // inline the get_last_end_pos logic due to layout mismatch
4226 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4227 end_lineno = last_handler->end_lineno;
4228 end_col_offset = last_handler->end_col_offset;
4229 }
4230 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4231 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232}
4233
Georg Brandl0c315622009-05-25 21:10:36 +00004234/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004235static withitem_ty
4236ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004237{
4238 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004239
Georg Brandl0c315622009-05-25 21:10:36 +00004240 REQ(n, with_item);
4241 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004242 if (!context_expr)
4243 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004244 if (NCH(n) == 3) {
4245 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004246
4247 if (!optional_vars) {
4248 return NULL;
4249 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004250 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004251 return NULL;
4252 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004253 }
4254
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004255 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004256}
4257
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004258/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004259static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004260ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004261{
guoci90fc8982018-09-11 17:45:45 -04004262 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004263 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004264 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004265 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004266
Guido van Rossum495da292019-03-07 12:38:08 -08004267 if (is_async && c->c_feature_version < 5) {
4268 ast_error(c, n,
4269 "Async with statements are only supported in Python 3.5 and greater");
4270 return NULL;
4271 }
4272
Georg Brandl0c315622009-05-25 21:10:36 +00004273 REQ(n, with_stmt);
4274
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004275 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4276 nch_minus_type = NCH(n) - has_type_comment;
4277
4278 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004279 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004280 if (!items)
4281 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004282 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004283 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4284 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004285 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004286 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004287 }
4288
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004289 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4290 if (!body)
4291 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004292 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004293
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004294 if (has_type_comment) {
4295 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4296 if (!type_comment)
4297 return NULL;
4298 }
4299 else
4300 type_comment = NULL;
4301
Yury Selivanov75445082015-05-11 22:57:16 -04004302 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004303 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004304 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004305 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004306 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004307 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004308}
4309
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004310static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004311ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004312{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004313 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004314 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004315 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004316 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004317 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004318
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004319 REQ(n, classdef);
4320
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004321 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004322 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004323 if (!s)
4324 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004325 get_last_end_pos(s, &end_lineno, &end_col_offset);
4326
Benjamin Peterson30760062008-11-25 04:02:28 +00004327 classname = NEW_IDENTIFIER(CHILD(n, 1));
4328 if (!classname)
4329 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004330 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004331 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004332 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004333 LINENO(n), n->n_col_offset,
4334 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004335 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004336
4337 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004338 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004339 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004340 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 }
4352
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004353 /* class NAME '(' arglist ')' ':' suite */
4354 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004355 {
4356 PyObject *dummy_name;
4357 expr_ty dummy;
4358 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4359 if (!dummy_name)
4360 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004361 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4362 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4363 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004364 call = ast_for_call(c, CHILD(n, 3), dummy,
4365 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004366 if (!call)
4367 return NULL;
4368 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004369 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004370 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004371 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004372 get_last_end_pos(s, &end_lineno, &end_col_offset);
4373
Benjamin Peterson30760062008-11-25 04:02:28 +00004374 classname = NEW_IDENTIFIER(CHILD(n, 1));
4375 if (!classname)
4376 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004377 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004378 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004379
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004380 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004381 decorator_seq, LINENO(n), n->n_col_offset,
4382 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004383}
4384
4385static stmt_ty
4386ast_for_stmt(struct compiling *c, const node *n)
4387{
4388 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004389 assert(NCH(n) == 1);
4390 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004391 }
4392 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004393 assert(num_stmts(n) == 1);
4394 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004395 }
4396 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004397 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004398 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4399 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004400 */
4401 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402 case expr_stmt:
4403 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004404 case del_stmt:
4405 return ast_for_del_stmt(c, n);
4406 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004407 return Pass(LINENO(n), n->n_col_offset,
4408 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004409 case flow_stmt:
4410 return ast_for_flow_stmt(c, n);
4411 case import_stmt:
4412 return ast_for_import_stmt(c, n);
4413 case global_stmt:
4414 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004415 case nonlocal_stmt:
4416 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004417 case assert_stmt:
4418 return ast_for_assert_stmt(c, n);
4419 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004420 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4422 TYPE(n), NCH(n));
4423 return NULL;
4424 }
4425 }
4426 else {
4427 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004428 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004429 */
4430 node *ch = CHILD(n, 0);
4431 REQ(n, compound_stmt);
4432 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433 case if_stmt:
4434 return ast_for_if_stmt(c, ch);
4435 case while_stmt:
4436 return ast_for_while_stmt(c, ch);
4437 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004438 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004439 case try_stmt:
4440 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004441 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004442 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004443 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004444 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004446 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 case decorated:
4448 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004449 case async_stmt:
4450 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004452 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004453 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454 TYPE(n), NCH(n));
4455 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004456 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004457 }
4458}
4459
4460static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004461parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004463 const char *end;
4464 long x;
4465 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004466 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004467 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004468
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004469 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004470 errno = 0;
4471 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004472 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004473 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004474 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004475 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004476 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004477 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004478 }
4479 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004480 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004481 if (*end == '\0') {
4482 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004483 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004484 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004485 }
4486 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004487 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004488 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004489 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4490 if (compl.imag == -1.0 && PyErr_Occurred())
4491 return NULL;
4492 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004493 }
4494 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004495 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004496 dx = PyOS_string_to_double(s, NULL, NULL);
4497 if (dx == -1.0 && PyErr_Occurred())
4498 return NULL;
4499 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004501}
4502
4503static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004504parsenumber(struct compiling *c, const char *s)
4505{
4506 char *dup, *end;
4507 PyObject *res = NULL;
4508
4509 assert(s != NULL);
4510
4511 if (strchr(s, '_') == NULL) {
4512 return parsenumber_raw(c, s);
4513 }
4514 /* Create a duplicate without underscores. */
4515 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004516 if (dup == NULL) {
4517 return PyErr_NoMemory();
4518 }
Brett Cannona721aba2016-09-09 14:57:09 -07004519 end = dup;
4520 for (; *s; s++) {
4521 if (*s != '_') {
4522 *end++ = *s;
4523 }
4524 }
4525 *end = '\0';
4526 res = parsenumber_raw(c, dup);
4527 PyMem_Free(dup);
4528 return res;
4529}
4530
4531static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004532decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004533{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004534 const char *s, *t;
4535 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004536 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4537 while (s < end && (*s & 0x80)) s++;
4538 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004539 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540}
4541
Eric V. Smith56466482016-10-31 14:46:26 -04004542static int
4543warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004544 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004545{
4546 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4547 first_invalid_escape_char);
4548 if (msg == NULL) {
4549 return -1;
4550 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004551 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004552 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004553 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004554 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004555 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4556 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004557 to get a more accurate error report */
4558 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004559 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004560 }
4561 Py_DECREF(msg);
4562 return -1;
4563 }
4564 Py_DECREF(msg);
4565 return 0;
4566}
4567
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004568static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004569decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4570 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004571{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004572 PyObject *v, *u;
4573 char *buf;
4574 char *p;
4575 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004576
Benjamin Peterson202803a2016-02-25 22:34:45 -08004577 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004578 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004579 return NULL;
4580 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4581 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4582 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4583 if (u == NULL)
4584 return NULL;
4585 p = buf = PyBytes_AsString(u);
4586 end = s + len;
4587 while (s < end) {
4588 if (*s == '\\') {
4589 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004590 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004591 strcpy(p, "u005c");
4592 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004593 if (s >= end)
4594 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004595 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004596 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004597 if (*s & 0x80) { /* XXX inefficient */
4598 PyObject *w;
4599 int kind;
4600 void *data;
4601 Py_ssize_t len, i;
4602 w = decode_utf8(c, &s, end);
4603 if (w == NULL) {
4604 Py_DECREF(u);
4605 return NULL;
4606 }
4607 kind = PyUnicode_KIND(w);
4608 data = PyUnicode_DATA(w);
4609 len = PyUnicode_GET_LENGTH(w);
4610 for (i = 0; i < len; i++) {
4611 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4612 sprintf(p, "\\U%08x", chr);
4613 p += 10;
4614 }
4615 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004616 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004617 Py_DECREF(w);
4618 } else {
4619 *p++ = *s++;
4620 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004621 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004622 len = p - buf;
4623 s = buf;
4624
Eric V. Smith56466482016-10-31 14:46:26 -04004625 const char *first_invalid_escape;
4626 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4627
4628 if (v != NULL && first_invalid_escape != NULL) {
4629 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4630 /* We have not decref u before because first_invalid_escape points
4631 inside u. */
4632 Py_XDECREF(u);
4633 Py_DECREF(v);
4634 return NULL;
4635 }
4636 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004637 Py_XDECREF(u);
4638 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004639}
4640
Eric V. Smith56466482016-10-31 14:46:26 -04004641static PyObject *
4642decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4643 size_t len)
4644{
4645 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004646 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004647 &first_invalid_escape);
4648 if (result == NULL)
4649 return NULL;
4650
4651 if (first_invalid_escape != NULL) {
4652 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4653 Py_DECREF(result);
4654 return NULL;
4655 }
4656 }
4657 return result;
4658}
4659
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004660/* Shift locations for the given node and all its children by adding `lineno`
4661 and `col_offset` to existing locations. */
4662static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4663{
4664 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004665 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004666 for (int i = 0; i < NCH(n); ++i) {
4667 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4668 /* Shifting column offsets unnecessary if there's been newlines. */
4669 col_offset = 0;
4670 }
4671 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4672 }
4673 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004674 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004675}
4676
4677/* Fix locations for the given node and its children.
4678
4679 `parent` is the enclosing node.
4680 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004681 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004682*/
4683static void
4684fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4685{
4686 char *substr = NULL;
4687 char *start;
4688 int lines = LINENO(parent) - 1;
4689 int cols = parent->n_col_offset;
4690 /* Find the full fstring to fix location information in `n`. */
4691 while (parent && parent->n_type != STRING)
4692 parent = parent->n_child;
4693 if (parent && parent->n_str) {
4694 substr = strstr(parent->n_str, expr_str);
4695 if (substr) {
4696 start = substr;
4697 while (start > parent->n_str) {
4698 if (start[0] == '\n')
4699 break;
4700 start--;
4701 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004702 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004703 /* adjust the start based on the number of newlines encountered
4704 before the f-string expression */
4705 for (char* p = parent->n_str; p < substr; p++) {
4706 if (*p == '\n') {
4707 lines++;
4708 }
4709 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004710 }
4711 }
4712 fstring_shift_node_locations(n, lines, cols);
4713}
4714
Eric V. Smith451d0e32016-09-09 21:56:20 -04004715/* Compile this expression in to an expr_ty. Add parens around the
4716 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004717static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004718fstring_compile_expr(const char *expr_start, const char *expr_end,
4719 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004720
Eric V. Smith235a6f02015-09-19 14:51:32 -04004721{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004722 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004723 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004724 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004725 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004726 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004727
Eric V. Smith1d44c412015-09-23 07:49:00 -04004728 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004729 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004730 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4731 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004732
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004733 /* If the substring is all whitespace, it's an error. We need to catch this
4734 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4735 because turning the expression '' in to '()' would go from being invalid
4736 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004737 for (s = expr_start; s != expr_end; s++) {
4738 char c = *s;
4739 /* The Python parser ignores only the following whitespace
4740 characters (\r already is converted to \n). */
4741 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742 break;
4743 }
4744 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004745 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004746 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004747 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004748 }
4749
Eric V. Smith451d0e32016-09-09 21:56:20 -04004750 len = expr_end - expr_start;
4751 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4752 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004753 if (str == NULL) {
4754 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004755 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004756 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004757
Eric V. Smith451d0e32016-09-09 21:56:20 -04004758 str[0] = '(';
4759 memcpy(str+1, expr_start, len);
4760 str[len+1] = ')';
4761 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004762
Victor Stinner37d66d72019-06-13 02:16:41 +02004763 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004764 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004765 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4766 Py_eval_input, 0);
4767 if (!mod_n) {
4768 PyMem_RawFree(str);
4769 return NULL;
4770 }
4771 /* Reuse str to find the correct column offset. */
4772 str[0] = '{';
4773 str[len+1] = '}';
4774 fstring_fix_node_location(n, mod_n, str);
4775 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004776 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004777 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004778 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004779 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004780 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004781}
4782
4783/* Return -1 on error.
4784
4785 Return 0 if we reached the end of the literal.
4786
4787 Return 1 if we haven't reached the end of the literal, but we want
4788 the caller to process the literal up to this point. Used for
4789 doubled braces.
4790*/
4791static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004792fstring_find_literal(const char **str, const char *end, int raw,
4793 PyObject **literal, int recurse_lvl,
4794 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004795{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004796 /* Get any literal string. It ends when we hit an un-doubled left
4797 brace (which isn't part of a unicode name escape such as
4798 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004799
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004800 const char *s = *str;
4801 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004802 int result = 0;
4803
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004805 while (s < end) {
4806 char ch = *s++;
4807 if (!raw && ch == '\\' && s < end) {
4808 ch = *s++;
4809 if (ch == 'N') {
4810 if (s < end && *s++ == '{') {
4811 while (s < end && *s++ != '}') {
4812 }
4813 continue;
4814 }
4815 break;
4816 }
4817 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4818 return -1;
4819 }
4820 }
4821 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004822 /* Check for doubled braces, but only at the top level. If
4823 we checked at every level, then f'{0:{3}}' would fail
4824 with the two closing braces. */
4825 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004826 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004827 /* We're going to tell the caller that the literal ends
4828 here, but that they should continue scanning. But also
4829 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004830 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004831 result = 1;
4832 goto done;
4833 }
4834
4835 /* Where a single '{' is the start of a new expression, a
4836 single '}' is not allowed. */
4837 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004838 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004839 ast_error(c, n, "f-string: single '}' is not allowed");
4840 return -1;
4841 }
4842 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843 /* We're either at a '{', which means we're starting another
4844 expression; or a '}', which means we're at the end of this
4845 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004846 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004847 break;
4848 }
4849 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004850 *str = s;
4851 assert(s <= end);
4852 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004853done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004854 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004855 if (raw)
4856 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004857 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004858 NULL, NULL);
4859 else
Eric V. Smith56466482016-10-31 14:46:26 -04004860 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004861 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004862 if (!*literal)
4863 return -1;
4864 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865 return result;
4866}
4867
4868/* Forward declaration because parsing is recursive. */
4869static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004870fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871 struct compiling *c, const node *n);
4872
Eric V. Smith451d0e32016-09-09 21:56:20 -04004873/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004874 expression (so it must be a '{'). Returns the FormattedValue node, which
4875 includes the expression, conversion character, format_spec expression, and
4876 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004877
4878 Note that I don't do a perfect job here: I don't make sure that a
4879 closing brace doesn't match an opening paren, for example. It
4880 doesn't need to error on all invalid expressions, just correctly
4881 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004882 will be caught when we parse it later.
4883
4884 *expression is set to the expression. For an '=' "debug" expression,
4885 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004886 including the '=' and any whitespace around it, as a string object). If
4887 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004889fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004890 PyObject **expr_text, expr_ty *expression,
4891 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892{
4893 /* Return -1 on error, else 0. */
4894
Eric V. Smith451d0e32016-09-09 21:56:20 -04004895 const char *expr_start;
4896 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 expr_ty simple_expression;
4898 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004899 int conversion = -1; /* The conversion char. Use default if not
4900 specified, or !r if using = and no format
4901 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004902
4903 /* 0 if we're not in a string, else the quote char we're trying to
4904 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906
4907 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4908 int string_type = 0;
4909
4910 /* Keep track of nesting level for braces/parens/brackets in
4911 expressions. */
4912 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004913 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004915 *expr_text = NULL;
4916
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917 /* Can only nest one level deep. */
4918 if (recurse_lvl >= 2) {
4919 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004920 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921 }
4922
4923 /* The first char must be a left brace, or we wouldn't have gotten
4924 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925 assert(**str == '{');
4926 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927
Eric V. Smith451d0e32016-09-09 21:56:20 -04004928 expr_start = *str;
4929 for (; *str < end; (*str)++) {
4930 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931
4932 /* Loop invariants. */
4933 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004934 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004935 if (quote_char)
4936 assert(string_type == 1 || string_type == 3);
4937 else
4938 assert(string_type == 0);
4939
Eric V. Smith451d0e32016-09-09 21:56:20 -04004940 ch = **str;
4941 /* Nowhere inside an expression is a backslash allowed. */
4942 if (ch == '\\') {
4943 /* Error: can't include a backslash character, inside
4944 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004945 ast_error(c, n,
4946 "f-string expression part "
4947 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004948 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004949 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950 if (quote_char) {
4951 /* We're inside a string. See if we're at the end. */
4952 /* This code needs to implement the same non-error logic
4953 as tok_get from tokenizer.c, at the letter_quote
4954 label. To actually share that code would be a
4955 nightmare. But, it's unlikely to change and is small,
4956 so duplicate it here. Note we don't need to catch all
4957 of the errors, since they'll be caught when parsing the
4958 expression. We just need to match the non-error
4959 cases. Thus we can ignore \n in single-quoted strings,
4960 for example. Or non-terminated strings. */
4961 if (ch == quote_char) {
4962 /* Does this match the string_type (single or triple
4963 quoted)? */
4964 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004965 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004967 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004968 string_type = 0;
4969 quote_char = 0;
4970 continue;
4971 }
4972 } else {
4973 /* We're at the end of a normal string. */
4974 quote_char = 0;
4975 string_type = 0;
4976 continue;
4977 }
4978 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 } else if (ch == '\'' || ch == '"') {
4980 /* Is this a triple quoted string? */
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 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 } else {
4985 /* Start of a normal string. */
4986 string_type = 1;
4987 }
4988 /* Start looking for the end of the string. */
4989 quote_char = ch;
4990 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004991 if (nested_depth >= MAXLEVEL) {
4992 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004993 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004994 }
4995 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 } else if (ch == '#') {
4998 /* Error: can't include a comment character, inside parens
4999 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005000 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005001 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005002 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005003 (ch == '!' || ch == ':' || ch == '}' ||
5004 ch == '=' || ch == '>' || ch == '<')) {
5005 /* See if there's a next character. */
5006 if (*str+1 < end) {
5007 char next = *(*str+1);
5008
5009 /* For "!=". since '=' is not an allowed conversion character,
5010 nothing is lost in this test. */
5011 if ((ch == '!' && next == '=') || /* != */
5012 (ch == '=' && next == '=') || /* == */
5013 (ch == '<' && next == '=') || /* <= */
5014 (ch == '>' && next == '=') /* >= */
5015 ) {
5016 *str += 1;
5017 continue;
5018 }
5019 /* Don't get out of the loop for these, if they're single
5020 chars (not part of 2-char tokens). If by themselves, they
5021 don't end an expression (unlike say '!'). */
5022 if (ch == '>' || ch == '<') {
5023 continue;
5024 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005025 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005026
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 /* Normal way out of this loop. */
5028 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005029 } else if (ch == ']' || ch == '}' || ch == ')') {
5030 if (!nested_depth) {
5031 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005032 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005033 }
5034 nested_depth--;
5035 int opening = parenstack[nested_depth];
5036 if (!((opening == '(' && ch == ')') ||
5037 (opening == '[' && ch == ']') ||
5038 (opening == '{' && ch == '}')))
5039 {
5040 ast_error(c, n,
5041 "f-string: closing parenthesis '%c' "
5042 "does not match opening parenthesis '%c'",
5043 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005044 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005045 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 } else {
5047 /* Just consume this char and loop around. */
5048 }
5049 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 /* If we leave this loop in a string or with mismatched parens, we
5052 don't care. We'll get a syntax error when compiling the
5053 expression. But, we can produce a better error message, so
5054 let's just do that.*/
5055 if (quote_char) {
5056 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005057 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058 }
5059 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005060 int opening = parenstack[nested_depth - 1];
5061 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005062 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 }
5064
Eric V. Smith451d0e32016-09-09 21:56:20 -04005065 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005066 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005067
5068 /* Compile the expression as soon as possible, so we show errors
5069 related to the expression before errors related to the
5070 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005071 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005072 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005073 goto error;
5074
5075 /* Check for =, which puts the text value of the expression in
5076 expr_text. */
5077 if (**str == '=') {
5078 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005079
5080 /* Skip over ASCII whitespace. No need to test for end of string
5081 here, since we know there's at least a trailing quote somewhere
5082 ahead. */
5083 while (Py_ISSPACE(**str)) {
5084 *str += 1;
5085 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005086
5087 /* Set *expr_text to the text of the expression. */
5088 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5089 if (!*expr_text) {
5090 goto error;
5091 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005092 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005093
5094 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005095 if (**str == '!') {
5096 *str += 1;
5097 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005098 goto unexpected_end_of_string;
5099
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 conversion = **str;
5101 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005102
5103 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005104 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005105 ast_error(c, n,
5106 "f-string: invalid conversion character: "
5107 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005108 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005110
5111 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112
5113 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005114 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 if (**str == ':') {
5117 *str += 1;
5118 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 goto unexpected_end_of_string;
5120
5121 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005124 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125 }
5126
Eric V. Smith451d0e32016-09-09 21:56:20 -04005127 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005128 goto unexpected_end_of_string;
5129
5130 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 assert(*str < end);
5132 assert(**str == '}');
5133 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005135 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005136 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005137 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005138 conversion = 'r';
5139 }
5140
Eric V. Smith451d0e32016-09-09 21:56:20 -04005141 /* And now create the FormattedValue node that represents this
5142 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005143 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005144 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005145 n->n_col_offset, n->n_end_lineno,
5146 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005148 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149
5150 return 0;
5151
5152unexpected_end_of_string:
5153 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005154 /* Falls through to error. */
5155
5156error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005157 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005159
Eric V. Smith235a6f02015-09-19 14:51:32 -04005160}
5161
5162/* Return -1 on error.
5163
5164 Return 0 if we have a literal (possible zero length) and an
5165 expression (zero length if at the end of the string.
5166
5167 Return 1 if we have a literal, but no expression, and we want the
5168 caller to call us again. This is used to deal with doubled
5169 braces.
5170
5171 When called multiple times on the string 'a{{b{0}c', this function
5172 will return:
5173
5174 1. the literal 'a{' with no expression, and a return value
5175 of 1. Despite the fact that there's no expression, the return
5176 value of 1 means we're not finished yet.
5177
5178 2. the literal 'b' and the expression '0', with a return value of
5179 0. The fact that there's an expression means we're not finished.
5180
5181 3. literal 'c' with no expression and a return value of 0. The
5182 combination of the return value of 0 with no expression means
5183 we're finished.
5184*/
5185static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5187 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005188 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005189 struct compiling *c, const node *n)
5190{
5191 int result;
5192
5193 assert(*literal == NULL && *expression == NULL);
5194
5195 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005196 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005197 if (result < 0)
5198 goto error;
5199
5200 assert(result == 0 || result == 1);
5201
5202 if (result == 1)
5203 /* We have a literal, but don't look at the expression. */
5204 return 1;
5205
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005207 /* We're at the end of the string or the end of a nested
5208 f-string: no expression. The top-level error case where we
5209 expect to be at the end of the string but we're at a '}' is
5210 handled later. */
5211 return 0;
5212
5213 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005214 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005216 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5217 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005218 goto error;
5219
5220 return 0;
5221
5222error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005223 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005224 return -1;
5225}
5226
5227#define EXPRLIST_N_CACHED 64
5228
5229typedef struct {
5230 /* Incrementally build an array of expr_ty, so be used in an
5231 asdl_seq. Cache some small but reasonably sized number of
5232 expr_ty's, and then after that start dynamically allocating,
5233 doubling the number allocated each time. Note that the f-string
5234 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005235 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005236 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005237
5238 Py_ssize_t allocated; /* Number we've allocated. */
5239 Py_ssize_t size; /* Number we've used. */
5240 expr_ty *p; /* Pointer to the memory we're actually
5241 using. Will point to 'data' until we
5242 start dynamically allocating. */
5243 expr_ty data[EXPRLIST_N_CACHED];
5244} ExprList;
5245
5246#ifdef NDEBUG
5247#define ExprList_check_invariants(l)
5248#else
5249static void
5250ExprList_check_invariants(ExprList *l)
5251{
5252 /* Check our invariants. Make sure this object is "live", and
5253 hasn't been deallocated. */
5254 assert(l->size >= 0);
5255 assert(l->p != NULL);
5256 if (l->size <= EXPRLIST_N_CACHED)
5257 assert(l->data == l->p);
5258}
5259#endif
5260
5261static void
5262ExprList_Init(ExprList *l)
5263{
5264 l->allocated = EXPRLIST_N_CACHED;
5265 l->size = 0;
5266
5267 /* Until we start allocating dynamically, p points to data. */
5268 l->p = l->data;
5269
5270 ExprList_check_invariants(l);
5271}
5272
5273static int
5274ExprList_Append(ExprList *l, expr_ty exp)
5275{
5276 ExprList_check_invariants(l);
5277 if (l->size >= l->allocated) {
5278 /* We need to alloc (or realloc) the memory. */
5279 Py_ssize_t new_size = l->allocated * 2;
5280
5281 /* See if we've ever allocated anything dynamically. */
5282 if (l->p == l->data) {
5283 Py_ssize_t i;
5284 /* We're still using the cached data. Switch to
5285 alloc-ing. */
5286 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5287 if (!l->p)
5288 return -1;
5289 /* Copy the cached data into the new buffer. */
5290 for (i = 0; i < l->size; i++)
5291 l->p[i] = l->data[i];
5292 } else {
5293 /* Just realloc. */
5294 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5295 if (!tmp) {
5296 PyMem_RawFree(l->p);
5297 l->p = NULL;
5298 return -1;
5299 }
5300 l->p = tmp;
5301 }
5302
5303 l->allocated = new_size;
5304 assert(l->allocated == 2 * l->size);
5305 }
5306
5307 l->p[l->size++] = exp;
5308
5309 ExprList_check_invariants(l);
5310 return 0;
5311}
5312
5313static void
5314ExprList_Dealloc(ExprList *l)
5315{
5316 ExprList_check_invariants(l);
5317
5318 /* If there's been an error, or we've never dynamically allocated,
5319 do nothing. */
5320 if (!l->p || l->p == l->data) {
5321 /* Do nothing. */
5322 } else {
5323 /* We have dynamically allocated. Free the memory. */
5324 PyMem_RawFree(l->p);
5325 }
5326 l->p = NULL;
5327 l->size = -1;
5328}
5329
5330static asdl_seq *
5331ExprList_Finish(ExprList *l, PyArena *arena)
5332{
5333 asdl_seq *seq;
5334
5335 ExprList_check_invariants(l);
5336
5337 /* Allocate the asdl_seq and copy the expressions in to it. */
5338 seq = _Py_asdl_seq_new(l->size, arena);
5339 if (seq) {
5340 Py_ssize_t i;
5341 for (i = 0; i < l->size; i++)
5342 asdl_seq_SET(seq, i, l->p[i]);
5343 }
5344 ExprList_Dealloc(l);
5345 return seq;
5346}
5347
5348/* The FstringParser is designed to add a mix of strings and
5349 f-strings, and concat them together as needed. Ultimately, it
5350 generates an expr_ty. */
5351typedef struct {
5352 PyObject *last_str;
5353 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005354 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005355} FstringParser;
5356
5357#ifdef NDEBUG
5358#define FstringParser_check_invariants(state)
5359#else
5360static void
5361FstringParser_check_invariants(FstringParser *state)
5362{
5363 if (state->last_str)
5364 assert(PyUnicode_CheckExact(state->last_str));
5365 ExprList_check_invariants(&state->expr_list);
5366}
5367#endif
5368
5369static void
5370FstringParser_Init(FstringParser *state)
5371{
5372 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005373 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005374 ExprList_Init(&state->expr_list);
5375 FstringParser_check_invariants(state);
5376}
5377
5378static void
5379FstringParser_Dealloc(FstringParser *state)
5380{
5381 FstringParser_check_invariants(state);
5382
5383 Py_XDECREF(state->last_str);
5384 ExprList_Dealloc(&state->expr_list);
5385}
5386
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005387/* Constants for the following */
5388static PyObject *u_kind;
5389
5390/* Compute 'kind' field for string Constant (either 'u' or None) */
5391static PyObject *
5392make_kind(struct compiling *c, const node *n)
5393{
5394 char *s = NULL;
5395 PyObject *kind = NULL;
5396
5397 /* Find the first string literal, if any */
5398 while (TYPE(n) != STRING) {
5399 if (NCH(n) == 0)
5400 return NULL;
5401 n = CHILD(n, 0);
5402 }
5403 REQ(n, STRING);
5404
5405 /* If it starts with 'u', return a PyUnicode "u" string */
5406 s = STR(n);
5407 if (s && *s == 'u') {
5408 if (!u_kind) {
5409 u_kind = PyUnicode_InternFromString("u");
5410 if (!u_kind)
5411 return NULL;
5412 }
5413 kind = u_kind;
5414 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5415 return NULL;
5416 }
5417 Py_INCREF(kind);
5418 }
5419 return kind;
5420}
5421
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005422/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005423static expr_ty
5424make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5425{
5426 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005427 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005428 *str = NULL;
5429 assert(PyUnicode_CheckExact(s));
5430 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5431 Py_DECREF(s);
5432 return NULL;
5433 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005434 kind = make_kind(c, n);
5435 if (kind == NULL && PyErr_Occurred())
5436 return NULL;
5437 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005438 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005439}
5440
5441/* Add a non-f-string (that is, a regular literal string). str is
5442 decref'd. */
5443static int
5444FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5445{
5446 FstringParser_check_invariants(state);
5447
5448 assert(PyUnicode_CheckExact(str));
5449
5450 if (PyUnicode_GET_LENGTH(str) == 0) {
5451 Py_DECREF(str);
5452 return 0;
5453 }
5454
5455 if (!state->last_str) {
5456 /* We didn't have a string before, so just remember this one. */
5457 state->last_str = str;
5458 } else {
5459 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005460 PyUnicode_AppendAndDel(&state->last_str, str);
5461 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005462 return -1;
5463 }
5464 FstringParser_check_invariants(state);
5465 return 0;
5466}
5467
Eric V. Smith451d0e32016-09-09 21:56:20 -04005468/* Parse an f-string. The f-string is in *str to end, with no
5469 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005470static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005471FstringParser_ConcatFstring(FstringParser *state, const char **str,
5472 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005473 struct compiling *c, const node *n)
5474{
5475 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005476 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005477
5478 /* Parse the f-string. */
5479 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005480 PyObject *literal = NULL;
5481 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005482 expr_ty expression = NULL;
5483
5484 /* If there's a zero length literal in front of the
5485 expression, literal will be NULL. If we're at the end of
5486 the f-string, expression will be NULL (unless result == 1,
5487 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005488 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005489 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005490 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005491 if (result < 0)
5492 return -1;
5493
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005494 /* Add the literal, if any. */
5495 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5496 Py_XDECREF(expr_text);
5497 return -1;
5498 }
5499 /* Add the expr_text, if any. */
5500 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5501 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005502 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005503
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005504 /* We've dealt with the literal and expr_text, their ownership has
5505 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005506
5507 /* See if we should just loop around to get the next literal
5508 and expression, while ignoring the expression this
5509 time. This is used for un-doubling braces, as an
5510 optimization. */
5511 if (result == 1)
5512 continue;
5513
5514 if (!expression)
5515 /* We're done with this f-string. */
5516 break;
5517
5518 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005519 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005520 if (!state->last_str) {
5521 /* Do nothing. No previous literal. */
5522 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005523 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005524 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5525 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5526 return -1;
5527 }
5528
5529 if (ExprList_Append(&state->expr_list, expression) < 0)
5530 return -1;
5531 }
5532
Eric V. Smith235a6f02015-09-19 14:51:32 -04005533 /* If recurse_lvl is zero, then we must be at the end of the
5534 string. Otherwise, we must be at a right brace. */
5535
Eric V. Smith451d0e32016-09-09 21:56:20 -04005536 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005537 ast_error(c, n, "f-string: unexpected end of string");
5538 return -1;
5539 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005540 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005541 ast_error(c, n, "f-string: expecting '}'");
5542 return -1;
5543 }
5544
5545 FstringParser_check_invariants(state);
5546 return 0;
5547}
5548
5549/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005550 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005551static expr_ty
5552FstringParser_Finish(FstringParser *state, struct compiling *c,
5553 const node *n)
5554{
5555 asdl_seq *seq;
5556
5557 FstringParser_check_invariants(state);
5558
5559 /* If we're just a constant string with no expressions, return
5560 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005561 if (!state->fmode) {
5562 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005563 if (!state->last_str) {
5564 /* Create a zero length string. */
5565 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5566 if (!state->last_str)
5567 goto error;
5568 }
5569 return make_str_node_and_del(&state->last_str, c, n);
5570 }
5571
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005572 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005573 last node in our expression list. */
5574 if (state->last_str) {
5575 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5576 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5577 goto error;
5578 }
5579 /* This has already been freed. */
5580 assert(state->last_str == NULL);
5581
5582 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5583 if (!seq)
5584 goto error;
5585
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005586 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5587 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005588
5589error:
5590 FstringParser_Dealloc(state);
5591 return NULL;
5592}
5593
Eric V. Smith451d0e32016-09-09 21:56:20 -04005594/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5595 at end, parse it into an expr_ty. Return NULL on error. Adjust
5596 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005597static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005598fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599 struct compiling *c, const node *n)
5600{
5601 FstringParser state;
5602
5603 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005604 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005605 c, n) < 0) {
5606 FstringParser_Dealloc(&state);
5607 return NULL;
5608 }
5609
5610 return FstringParser_Finish(&state, c, n);
5611}
5612
5613/* n is a Python string literal, including the bracketing quote
5614 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005616 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005617 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5618 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005619*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005620static int
5621parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5622 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005623{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005624 size_t len;
5625 const char *s = STR(n);
5626 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627 int fmode = 0;
5628 *bytesmode = 0;
5629 *rawmode = 0;
5630 *result = NULL;
5631 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005632 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005633 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005634 if (quote == 'b' || quote == 'B') {
5635 quote = *++s;
5636 *bytesmode = 1;
5637 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005638 else if (quote == 'u' || quote == 'U') {
5639 quote = *++s;
5640 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005641 else if (quote == 'r' || quote == 'R') {
5642 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005643 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005644 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005645 else if (quote == 'f' || quote == 'F') {
5646 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005647 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005648 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005649 else {
5650 break;
5651 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005652 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005653 }
Guido van Rossum495da292019-03-07 12:38:08 -08005654
5655 /* fstrings are only allowed in Python 3.6 and greater */
5656 if (fmode && c->c_feature_version < 6) {
5657 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5658 return -1;
5659 }
5660
Eric V. Smith451d0e32016-09-09 21:56:20 -04005661 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005663 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005664 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005665 if (quote != '\'' && quote != '\"') {
5666 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005667 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005668 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005669 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005670 s++;
5671 len = strlen(s);
5672 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005673 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005674 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005675 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005676 }
5677 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005678 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005679 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005680 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005681 }
5682 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005683 /* A triple quoted string. We've already skipped one quote at
5684 the start and one at the end of the string. Now skip the
5685 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005686 s += 2;
5687 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005688 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005689 if (s[--len] != quote || s[--len] != quote) {
5690 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005692 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005693 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005694
Eric V. Smith451d0e32016-09-09 21:56:20 -04005695 if (fmode) {
5696 /* Just return the bytes. The caller will parse the resulting
5697 string. */
5698 *fstr = s;
5699 *fstrlen = len;
5700 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005701 }
5702
Eric V. Smith451d0e32016-09-09 21:56:20 -04005703 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005704 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005705 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005706 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005707 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005708 const char *ch;
5709 for (ch = s; *ch; ch++) {
5710 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005711 ast_error(c, n,
5712 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005713 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005715 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005716 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005717 if (*rawmode)
5718 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005719 else
Eric V. Smith56466482016-10-31 14:46:26 -04005720 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005721 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005722 if (*rawmode)
5723 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005724 else
Eric V. Smith56466482016-10-31 14:46:26 -04005725 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005726 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005727 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005728}
5729
Eric V. Smith235a6f02015-09-19 14:51:32 -04005730/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5731 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005732 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005733 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005734 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005735 node if there's just an f-string (with no leading or trailing
5736 literals), or a JoinedStr node if there are multiple f-strings or
5737 any literals involved. */
5738static expr_ty
5739parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005740{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741 int bytesmode = 0;
5742 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005743 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744
5745 FstringParser state;
5746 FstringParser_Init(&state);
5747
5748 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005749 int this_bytesmode;
5750 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005751 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005752 const char *fstr;
5753 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005754
5755 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005756 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5757 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005758 goto error;
5759
5760 /* Check that we're not mixing bytes with unicode. */
5761 if (i != 0 && bytesmode != this_bytesmode) {
5762 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005763 /* s is NULL if the current string part is an f-string. */
5764 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005765 goto error;
5766 }
5767 bytesmode = this_bytesmode;
5768
Eric V. Smith451d0e32016-09-09 21:56:20 -04005769 if (fstr != NULL) {
5770 int result;
5771 assert(s == NULL && !bytesmode);
5772 /* This is an f-string. Parse and concatenate it. */
5773 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5774 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005775 if (result < 0)
5776 goto error;
5777 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005778 /* A string or byte string. */
5779 assert(s != NULL && fstr == NULL);
5780
Eric V. Smith451d0e32016-09-09 21:56:20 -04005781 assert(bytesmode ? PyBytes_CheckExact(s) :
5782 PyUnicode_CheckExact(s));
5783
Eric V. Smith451d0e32016-09-09 21:56:20 -04005784 if (bytesmode) {
5785 /* For bytes, concat as we go. */
5786 if (i == 0) {
5787 /* First time, just remember this value. */
5788 bytes_str = s;
5789 } else {
5790 PyBytes_ConcatAndDel(&bytes_str, s);
5791 if (!bytes_str)
5792 goto error;
5793 }
5794 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005795 /* This is a regular string. Concatenate it. */
5796 if (FstringParser_ConcatAndDel(&state, s) < 0)
5797 goto error;
5798 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005799 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005800 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005801 if (bytesmode) {
5802 /* Just return the bytes object and we're done. */
5803 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5804 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005805 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005806 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005808
Eric V. Smith235a6f02015-09-19 14:51:32 -04005809 /* We're not a bytes string, bytes_str should never have been set. */
5810 assert(bytes_str == NULL);
5811
5812 return FstringParser_Finish(&state, c, n);
5813
5814error:
5815 Py_XDECREF(bytes_str);
5816 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005818}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005819
5820PyObject *
5821_PyAST_GetDocString(asdl_seq *body)
5822{
5823 if (!asdl_seq_LEN(body)) {
5824 return NULL;
5825 }
5826 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5827 if (st->kind != Expr_kind) {
5828 return NULL;
5829 }
5830 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005831 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5832 return e->v.Constant.value;
5833 }
5834 return NULL;
5835}