blob: 1a4a3110e69559c7b407c93406b4fd838a658444 [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;
Pablo Galindo254ec782020-04-03 20:37:13 +01003051 identifier key;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003053 // To remain LL(1), the grammar accepts any test (basically, any
3054 // expression) in the keyword slot of a call site. So, we need
3055 // to manually enforce that the keyword is a NAME here.
3056 static const int name_tree[] = {
3057 test,
3058 or_test,
3059 and_test,
3060 not_test,
3061 comparison,
3062 expr,
3063 xor_expr,
3064 and_expr,
3065 shift_expr,
3066 arith_expr,
3067 term,
3068 factor,
3069 power,
3070 atom_expr,
3071 atom,
3072 0,
3073 };
3074 node *expr_node = chch;
3075 for (int i = 0; name_tree[i]; i++) {
3076 if (TYPE(expr_node) != name_tree[i])
3077 break;
3078 if (NCH(expr_node) != 1)
3079 break;
3080 expr_node = CHILD(expr_node, 0);
3081 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003082 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003083 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003084 "expression cannot contain assignment, "
3085 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003086 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003087 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003088 key = new_identifier(STR(expr_node), c);
3089 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003090 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003092 if (forbidden_name(c, key, chch, 1)) {
3093 return NULL;
3094 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003095 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003097 return NULL;
Pablo Galindo168660b2020-04-02 00:47:39 +01003098 kw = keyword(key, e, chch->n_lineno, chch->n_col_offset,
Pablo Galindo40cf35c2020-04-03 21:02:26 +01003099 e->end_lineno, e->end_col_offset, c->c_arena);
Pablo Galindo168660b2020-04-02 00:47:39 +01003100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003102 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003103 asdl_seq_SET(keywords, nkeywords++, kw);
3104 }
3105 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 }
3107
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003108 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003109 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110}
3111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003113ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003115 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003116 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003118 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003119 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003120 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003121 }
3122 else {
3123 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003124 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003125 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003127 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 else {
3129 asdl_seq *tmp = seq_for_testlist(c, n);
3130 if (!tmp)
3131 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003132 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3133 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003135}
3136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137static stmt_ty
3138ast_for_expr_stmt(struct compiling *c, const node *n)
3139{
3140 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003141 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003142 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3143 annassign: ':' test ['=' (yield_expr|testlist)]
3144 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3145 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3146 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003147 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003149 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003151 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003152 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 if (!e)
3154 return NULL;
3155
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003156 return Expr(e, LINENO(n), n->n_col_offset,
3157 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 }
3159 else if (TYPE(CHILD(n, 1)) == augassign) {
3160 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003161 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003162 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163
Thomas Wouters89f507f2006-12-13 04:49:30 +00003164 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 if (!expr1)
3166 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003167 if(!set_context(c, expr1, Store, ch))
3168 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003169 /* set_context checks that most expressions are not the left side.
3170 Augmented assignments can only have a name, a subscript, or an
3171 attribute on the left, though, so we have to explicitly check for
3172 those. */
3173 switch (expr1->kind) {
3174 case Name_kind:
3175 case Attribute_kind:
3176 case Subscript_kind:
3177 break;
3178 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003179 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003180 return NULL;
3181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182
Thomas Wouters89f507f2006-12-13 04:49:30 +00003183 ch = CHILD(n, 2);
3184 if (TYPE(ch) == testlist)
3185 expr2 = ast_for_testlist(c, ch);
3186 else
3187 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003188 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 return NULL;
3190
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003191 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003192 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 return NULL;
3194
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003195 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3196 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003198 else if (TYPE(CHILD(n, 1)) == annassign) {
3199 expr_ty expr1, expr2, expr3;
3200 node *ch = CHILD(n, 0);
3201 node *deep, *ann = CHILD(n, 1);
3202 int simple = 1;
3203
Guido van Rossum495da292019-03-07 12:38:08 -08003204 /* AnnAssigns are only allowed in Python 3.6 or greater */
3205 if (c->c_feature_version < 6) {
3206 ast_error(c, ch,
3207 "Variable annotation syntax is only supported in Python 3.6 and greater");
3208 return NULL;
3209 }
3210
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003211 /* we keep track of parens to qualify (x) as expression not name */
3212 deep = ch;
3213 while (NCH(deep) == 1) {
3214 deep = CHILD(deep, 0);
3215 }
3216 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3217 simple = 0;
3218 }
3219 expr1 = ast_for_testlist(c, ch);
3220 if (!expr1) {
3221 return NULL;
3222 }
3223 switch (expr1->kind) {
3224 case Name_kind:
3225 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3226 return NULL;
3227 }
3228 expr1->v.Name.ctx = Store;
3229 break;
3230 case Attribute_kind:
3231 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3232 return NULL;
3233 }
3234 expr1->v.Attribute.ctx = Store;
3235 break;
3236 case Subscript_kind:
3237 expr1->v.Subscript.ctx = Store;
3238 break;
3239 case List_kind:
3240 ast_error(c, ch,
3241 "only single target (not list) can be annotated");
3242 return NULL;
3243 case Tuple_kind:
3244 ast_error(c, ch,
3245 "only single target (not tuple) can be annotated");
3246 return NULL;
3247 default:
3248 ast_error(c, ch,
3249 "illegal target for annotation");
3250 return NULL;
3251 }
3252
3253 if (expr1->kind != Name_kind) {
3254 simple = 0;
3255 }
3256 ch = CHILD(ann, 1);
3257 expr2 = ast_for_expr(c, ch);
3258 if (!expr2) {
3259 return NULL;
3260 }
3261 if (NCH(ann) == 2) {
3262 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003263 LINENO(n), n->n_col_offset,
3264 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003265 }
3266 else {
3267 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003268 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003269 expr3 = ast_for_testlist(c, ch);
3270 }
3271 else {
3272 expr3 = ast_for_expr(c, ch);
3273 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003274 if (!expr3) {
3275 return NULL;
3276 }
3277 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003278 LINENO(n), n->n_col_offset,
3279 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003280 }
3281 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003283 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003284 asdl_seq *targets;
3285 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003287 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288
Thomas Wouters89f507f2006-12-13 04:49:30 +00003289 /* a normal assignment */
3290 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003291
3292 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3293 nch_minus_type = num - has_type_comment;
3294
3295 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 if (!targets)
3297 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003298 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 expr_ty e;
3300 node *ch = CHILD(n, i);
3301 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003302 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 return NULL;
3304 }
3305 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003309 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003310 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312
Thomas Wouters89f507f2006-12-13 04:49:30 +00003313 asdl_seq_SET(targets, i / 2, e);
3314 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003315 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003316 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 expression = ast_for_testlist(c, value);
3318 else
3319 expression = ast_for_expr(c, value);
3320 if (!expression)
3321 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003322 if (has_type_comment) {
3323 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3324 if (!type_comment)
3325 return NULL;
3326 }
3327 else
3328 type_comment = NULL;
3329 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003330 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332}
3333
Benjamin Peterson78565b22009-06-28 19:19:51 +00003334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003336ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337{
3338 asdl_seq *seq;
3339 int i;
3340 expr_ty e;
3341
3342 REQ(n, exprlist);
3343
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003344 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003346 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003348 e = ast_for_expr(c, CHILD(n, i));
3349 if (!e)
3350 return NULL;
3351 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003352 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 }
3355 return seq;
3356}
3357
3358static stmt_ty
3359ast_for_del_stmt(struct compiling *c, const node *n)
3360{
3361 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 /* del_stmt: 'del' exprlist */
3364 REQ(n, del_stmt);
3365
3366 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3367 if (!expr_list)
3368 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003369 return Delete(expr_list, LINENO(n), n->n_col_offset,
3370 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371}
3372
3373static stmt_ty
3374ast_for_flow_stmt(struct compiling *c, const node *n)
3375{
3376 /*
3377 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3378 | yield_stmt
3379 break_stmt: 'break'
3380 continue_stmt: 'continue'
3381 return_stmt: 'return' [testlist]
3382 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003383 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 raise_stmt: 'raise' [test [',' test [',' test]]]
3385 */
3386 node *ch;
3387
3388 REQ(n, flow_stmt);
3389 ch = CHILD(n, 0);
3390 switch (TYPE(ch)) {
3391 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003392 return Break(LINENO(n), n->n_col_offset,
3393 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003395 return Continue(LINENO(n), n->n_col_offset,
3396 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003398 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3399 if (!exp)
3400 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003401 return Expr(exp, 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 }
3404 case return_stmt:
3405 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003406 return Return(NULL, LINENO(n), n->n_col_offset,
3407 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003409 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 if (!expression)
3411 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003412 return Return(expression, LINENO(n), n->n_col_offset,
3413 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 }
3415 case raise_stmt:
3416 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003417 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3418 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003419 else if (NCH(ch) >= 2) {
3420 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3422 if (!expression)
3423 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003424 if (NCH(ch) == 4) {
3425 cause = ast_for_expr(c, CHILD(ch, 3));
3426 if (!cause)
3427 return NULL;
3428 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003429 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3430 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003431 }
Stefan Krahf432a322017-08-21 13:09:59 +02003432 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003434 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 "unexpected flow_stmt: %d", TYPE(ch));
3436 return NULL;
3437 }
3438}
3439
3440static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003441alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442{
3443 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003444 import_as_name: NAME ['as' NAME]
3445 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 dotted_name: NAME ('.' NAME)*
3447 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003448 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003449
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 loop:
3451 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003452 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003453 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003454 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003455 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003456 if (!name)
3457 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003458 if (NCH(n) == 3) {
3459 node *str_node = CHILD(n, 2);
3460 str = NEW_IDENTIFIER(str_node);
3461 if (!str)
3462 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003463 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003464 return NULL;
3465 }
3466 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003467 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003468 return NULL;
3469 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003470 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003471 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 case dotted_as_name:
3473 if (NCH(n) == 1) {
3474 n = CHILD(n, 0);
3475 goto loop;
3476 }
3477 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003478 node *asname_node = CHILD(n, 2);
3479 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003480 if (!a)
3481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003483 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003484 if (!a->asname)
3485 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003486 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 return a;
3489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003491 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003492 node *name_node = CHILD(n, 0);
3493 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003494 if (!name)
3495 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003496 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003497 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003498 return alias(name, NULL, c->c_arena);
3499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500 else {
3501 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003502 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003503 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506
3507 len = 0;
3508 for (i = 0; i < NCH(n); i += 2)
3509 /* length of string plus one for the dot */
3510 len += strlen(STR(CHILD(n, i))) + 1;
3511 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003512 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 if (!str)
3514 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003515 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 if (!s)
3517 return NULL;
3518 for (i = 0; i < NCH(n); i += 2) {
3519 char *sch = STR(CHILD(n, i));
3520 strcpy(s, STR(CHILD(n, i)));
3521 s += strlen(sch);
3522 *s++ = '.';
3523 }
3524 --s;
3525 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3527 PyBytes_GET_SIZE(str),
3528 NULL);
3529 Py_DECREF(str);
3530 if (!uni)
3531 return NULL;
3532 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003533 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003534 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3535 Py_DECREF(str);
3536 return NULL;
3537 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003538 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003541 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003542 if (!str)
3543 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003544 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3545 Py_DECREF(str);
3546 return NULL;
3547 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003548 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003550 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 "unexpected import name: %d", TYPE(n));
3552 return NULL;
3553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554}
3555
3556static stmt_ty
3557ast_for_import_stmt(struct compiling *c, const node *n)
3558{
3559 /*
3560 import_stmt: import_name | import_from
3561 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003562 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3563 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003565 int lineno;
3566 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 int i;
3568 asdl_seq *aliases;
3569
3570 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003571 lineno = LINENO(n);
3572 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003574 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003576 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003577 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003578 if (!aliases)
3579 return NULL;
3580 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003581 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003582 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003584 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003586 // Even though n is modified above, the end position is not changed
3587 return Import(aliases, lineno, col_offset,
3588 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003590 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003592 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003593 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003594 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003595 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003597 /* Count the number of dots (for relative imports) and check for the
3598 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003599 for (idx = 1; idx < NCH(n); idx++) {
3600 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003601 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3602 if (!mod)
3603 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 idx++;
3605 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003606 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003608 ndots += 3;
3609 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 } else if (TYPE(CHILD(n, idx)) != DOT) {
3611 break;
3612 }
3613 ndots++;
3614 }
3615 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003616 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003617 case STAR:
3618 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 n = CHILD(n, idx);
3620 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003621 break;
3622 case LPAR:
3623 /* from ... import (x, y, z) */
3624 n = CHILD(n, idx + 1);
3625 n_children = NCH(n);
3626 break;
3627 case import_as_names:
3628 /* from ... import x, y, z */
3629 n = CHILD(n, idx);
3630 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003631 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003632 ast_error(c, n,
3633 "trailing comma not allowed without"
3634 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 return NULL;
3636 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003637 break;
3638 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003639 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003640 return NULL;
3641 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003643 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003644 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646
3647 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003648 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003649 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003650 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003652 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003654 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003656 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003657 if (!import_alias)
3658 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003659 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003660 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003662 if (mod != NULL)
3663 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003664 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003665 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003666 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 }
Neal Norwitz79792652005-11-14 04:25:03 +00003668 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 "unknown import statement: starts with command '%s'",
3670 STR(CHILD(n, 0)));
3671 return NULL;
3672}
3673
3674static stmt_ty
3675ast_for_global_stmt(struct compiling *c, const node *n)
3676{
3677 /* global_stmt: 'global' NAME (',' NAME)* */
3678 identifier name;
3679 asdl_seq *s;
3680 int i;
3681
3682 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003683 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003687 name = NEW_IDENTIFIER(CHILD(n, i));
3688 if (!name)
3689 return NULL;
3690 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003692 return Global(s, LINENO(n), n->n_col_offset,
3693 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694}
3695
3696static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003697ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3698{
3699 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3700 identifier name;
3701 asdl_seq *s;
3702 int i;
3703
3704 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003705 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003706 if (!s)
3707 return NULL;
3708 for (i = 1; i < NCH(n); i += 2) {
3709 name = NEW_IDENTIFIER(CHILD(n, i));
3710 if (!name)
3711 return NULL;
3712 asdl_seq_SET(s, i / 2, name);
3713 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003714 return Nonlocal(s, LINENO(n), n->n_col_offset,
3715 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003716}
3717
3718static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719ast_for_assert_stmt(struct compiling *c, const node *n)
3720{
3721 /* assert_stmt: 'assert' test [',' test] */
3722 REQ(n, assert_stmt);
3723 if (NCH(n) == 2) {
3724 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3725 if (!expression)
3726 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003727 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3728 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 }
3730 else if (NCH(n) == 4) {
3731 expr_ty expr1, expr2;
3732
3733 expr1 = ast_for_expr(c, CHILD(n, 1));
3734 if (!expr1)
3735 return NULL;
3736 expr2 = ast_for_expr(c, CHILD(n, 3));
3737 if (!expr2)
3738 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003740 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3741 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742 }
Neal Norwitz79792652005-11-14 04:25:03 +00003743 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 "improper number of parts to 'assert' statement: %d",
3745 NCH(n));
3746 return NULL;
3747}
3748
3749static asdl_seq *
3750ast_for_suite(struct compiling *c, const node *n)
3751{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003752 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003753 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 stmt_ty s;
3755 int i, total, num, end, pos = 0;
3756 node *ch;
3757
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003758 if (TYPE(n) != func_body_suite) {
3759 REQ(n, suite);
3760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761
3762 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003763 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003767 n = CHILD(n, 0);
3768 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003770 */
3771 end = NCH(n) - 1;
3772 if (TYPE(CHILD(n, end - 1)) == SEMI)
3773 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003775 for (i = 0; i < end; i += 2) {
3776 ch = CHILD(n, i);
3777 s = ast_for_stmt(c, ch);
3778 if (!s)
3779 return NULL;
3780 asdl_seq_SET(seq, pos++, s);
3781 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 }
3783 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003784 i = 2;
3785 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3786 i += 2;
3787 REQ(CHILD(n, 2), NEWLINE);
3788 }
3789
3790 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003791 ch = CHILD(n, i);
3792 REQ(ch, stmt);
3793 num = num_stmts(ch);
3794 if (num == 1) {
3795 /* small_stmt or compound_stmt with only one child */
3796 s = ast_for_stmt(c, ch);
3797 if (!s)
3798 return NULL;
3799 asdl_seq_SET(seq, pos++, s);
3800 }
3801 else {
3802 int j;
3803 ch = CHILD(ch, 0);
3804 REQ(ch, simple_stmt);
3805 for (j = 0; j < NCH(ch); j += 2) {
3806 /* statement terminates with a semi-colon ';' */
3807 if (NCH(CHILD(ch, j)) == 0) {
3808 assert((j + 1) == NCH(ch));
3809 break;
3810 }
3811 s = ast_for_stmt(c, CHILD(ch, j));
3812 if (!s)
3813 return NULL;
3814 asdl_seq_SET(seq, pos++, s);
3815 }
3816 }
3817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 }
3819 assert(pos == seq->size);
3820 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821}
3822
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003823static void
3824get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3825{
Pablo Galindo46a97922019-02-19 22:51:53 +00003826 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003827 // There must be no empty suites.
3828 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003829 stmt_ty last = asdl_seq_GET(s, tot - 1);
3830 *end_lineno = last->end_lineno;
3831 *end_col_offset = last->end_col_offset;
3832}
3833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834static stmt_ty
3835ast_for_if_stmt(struct compiling *c, const node *n)
3836{
3837 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3838 ['else' ':' suite]
3839 */
3840 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003841 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842
3843 REQ(n, if_stmt);
3844
3845 if (NCH(n) == 4) {
3846 expr_ty expression;
3847 asdl_seq *suite_seq;
3848
3849 expression = ast_for_expr(c, CHILD(n, 1));
3850 if (!expression)
3851 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003853 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003855 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856
Guido van Rossumd8faa362007-04-27 19:54:29 +00003857 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003858 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 s = STR(CHILD(n, 4));
3862 /* s[2], the third character in the string, will be
3863 's' for el_s_e, or
3864 'i' for el_i_f
3865 */
3866 if (s[2] == 's') {
3867 expr_ty expression;
3868 asdl_seq *seq1, *seq2;
3869
3870 expression = ast_for_expr(c, CHILD(n, 1));
3871 if (!expression)
3872 return NULL;
3873 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003874 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 return NULL;
3876 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003877 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003879 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880
Guido van Rossumd8faa362007-04-27 19:54:29 +00003881 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003882 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883 }
3884 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003885 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003886 expr_ty expression;
3887 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003888 asdl_seq *orelse = NULL;
3889 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890 /* must reference the child n_elif+1 since 'else' token is third,
3891 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003892 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3893 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3894 has_else = 1;
3895 n_elif -= 3;
3896 }
3897 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898
Thomas Wouters89f507f2006-12-13 04:49:30 +00003899 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003900 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003902 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003903 if (!orelse)
3904 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003906 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003908 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3909 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003911 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3912 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003914 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 asdl_seq_SET(orelse, 0,
3917 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003918 LINENO(CHILD(n, NCH(n) - 7)),
3919 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003920 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003921 /* the just-created orelse handled the last elif */
3922 n_elif--;
3923 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924
Thomas Wouters89f507f2006-12-13 04:49:30 +00003925 for (i = 0; i < n_elif; i++) {
3926 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003927 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 if (!newobj)
3929 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003931 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003934 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003937 if (orelse != NULL) {
3938 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3939 } else {
3940 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3941 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003942 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003944 LINENO(CHILD(n, off - 1)),
3945 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003946 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003947 orelse = newobj;
3948 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003949 expression = ast_for_expr(c, CHILD(n, 1));
3950 if (!expression)
3951 return NULL;
3952 suite_seq = ast_for_suite(c, CHILD(n, 3));
3953 if (!suite_seq)
3954 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003955 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003956 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003957 LINENO(n), n->n_col_offset,
3958 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003960
3961 PyErr_Format(PyExc_SystemError,
3962 "unexpected token in 'if' statement: %s", s);
3963 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964}
3965
3966static stmt_ty
3967ast_for_while_stmt(struct compiling *c, const node *n)
3968{
3969 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3970 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003971 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972
3973 if (NCH(n) == 4) {
3974 expr_ty expression;
3975 asdl_seq *suite_seq;
3976
3977 expression = ast_for_expr(c, CHILD(n, 1));
3978 if (!expression)
3979 return NULL;
3980 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003981 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003983 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3984 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3985 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 }
3987 else if (NCH(n) == 7) {
3988 expr_ty expression;
3989 asdl_seq *seq1, *seq2;
3990
3991 expression = ast_for_expr(c, CHILD(n, 1));
3992 if (!expression)
3993 return NULL;
3994 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003995 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996 return NULL;
3997 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003998 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004000 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004002 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4003 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004005
4006 PyErr_Format(PyExc_SystemError,
4007 "wrong number of tokens for 'while' statement: %d",
4008 NCH(n));
4009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010}
4011
4012static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004013ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014{
guoci90fc8982018-09-11 17:45:45 -04004015 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004016 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004018 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004019 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004020 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004021 int has_type_comment;
4022 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004023
4024 if (is_async && c->c_feature_version < 5) {
4025 ast_error(c, n,
4026 "Async for loops are only supported in Python 3.5 and greater");
4027 return NULL;
4028 }
4029
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004030 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 REQ(n, for_stmt);
4032
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004033 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4034
4035 if (NCH(n) == 9 + has_type_comment) {
4036 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 if (!seq)
4038 return NULL;
4039 }
4040
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004041 node_target = CHILD(n, 1);
4042 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004043 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004045 /* Check the # of children rather than the length of _target, since
4046 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004047 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004048 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004049 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004051 target = Tuple(_target, Store, first->lineno, first->col_offset,
4052 node_target->n_end_lineno, node_target->n_end_col_offset,
4053 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004055 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004056 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004058 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004059 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060 return NULL;
4061
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004062 if (seq != NULL) {
4063 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4064 } else {
4065 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4066 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004067
4068 if (has_type_comment) {
4069 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4070 if (!type_comment)
4071 return NULL;
4072 }
4073 else
4074 type_comment = NULL;
4075
Yury Selivanov75445082015-05-11 22:57:16 -04004076 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004077 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004078 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004079 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004080 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004081 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004082 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004083 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084}
4085
4086static excepthandler_ty
4087ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4088{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004089 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004090 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 REQ(exc, except_clause);
4092 REQ(body, suite);
4093
4094 if (NCH(exc) == 1) {
4095 asdl_seq *suite_seq = ast_for_suite(c, body);
4096 if (!suite_seq)
4097 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004098 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004099
Neal Norwitzad74aa82008-03-31 05:14:30 +00004100 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004101 exc->n_col_offset,
4102 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004103 }
4104 else if (NCH(exc) == 2) {
4105 expr_ty expression;
4106 asdl_seq *suite_seq;
4107
4108 expression = ast_for_expr(c, CHILD(exc, 1));
4109 if (!expression)
4110 return NULL;
4111 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004112 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004113 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004114 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115
Neal Norwitzad74aa82008-03-31 05:14:30 +00004116 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004117 exc->n_col_offset,
4118 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119 }
4120 else if (NCH(exc) == 4) {
4121 asdl_seq *suite_seq;
4122 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004123 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004124 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004126 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004127 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004129 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130 return NULL;
4131 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004132 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004134 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135
Neal Norwitzad74aa82008-03-31 05:14:30 +00004136 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004137 exc->n_col_offset,
4138 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004140
4141 PyErr_Format(PyExc_SystemError,
4142 "wrong number of children for 'except' clause: %d",
4143 NCH(exc));
4144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145}
4146
4147static stmt_ty
4148ast_for_try_stmt(struct compiling *c, const node *n)
4149{
Neal Norwitzf599f422005-12-17 21:33:47 +00004150 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004151 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004152 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004153 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155 REQ(n, try_stmt);
4156
Neal Norwitzf599f422005-12-17 21:33:47 +00004157 body = ast_for_suite(c, CHILD(n, 2));
4158 if (body == NULL)
4159 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004160
Neal Norwitzf599f422005-12-17 21:33:47 +00004161 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4162 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4163 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4164 /* we can assume it's an "else",
4165 because nch >= 9 for try-else-finally and
4166 it would otherwise have a type of except_clause */
4167 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4168 if (orelse == NULL)
4169 return NULL;
4170 n_except--;
4171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172
Neal Norwitzf599f422005-12-17 21:33:47 +00004173 finally = ast_for_suite(c, CHILD(n, nch - 1));
4174 if (finally == NULL)
4175 return NULL;
4176 n_except--;
4177 }
4178 else {
4179 /* we can assume it's an "else",
4180 otherwise it would have a type of except_clause */
4181 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4182 if (orelse == NULL)
4183 return NULL;
4184 n_except--;
4185 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004187 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004188 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 return NULL;
4190 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191
Neal Norwitzf599f422005-12-17 21:33:47 +00004192 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004193 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004194 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004195 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004196 if (handlers == NULL)
4197 return NULL;
4198
4199 for (i = 0; i < n_except; i++) {
4200 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4201 CHILD(n, 5 + i * 3));
4202 if (!e)
4203 return NULL;
4204 asdl_seq_SET(handlers, i, e);
4205 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004206 }
4207
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004208 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004209 if (finally != NULL) {
4210 // finally is always last
4211 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4212 } else if (orelse != NULL) {
4213 // otherwise else is last
4214 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4215 } else {
4216 // inline the get_last_end_pos logic due to layout mismatch
4217 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4218 end_lineno = last_handler->end_lineno;
4219 end_col_offset = last_handler->end_col_offset;
4220 }
4221 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4222 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004223}
4224
Georg Brandl0c315622009-05-25 21:10:36 +00004225/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004226static withitem_ty
4227ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004228{
4229 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004230
Georg Brandl0c315622009-05-25 21:10:36 +00004231 REQ(n, with_item);
4232 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004233 if (!context_expr)
4234 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004235 if (NCH(n) == 3) {
4236 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004237
4238 if (!optional_vars) {
4239 return NULL;
4240 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004241 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004242 return NULL;
4243 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004244 }
4245
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004246 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004247}
4248
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004249/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004250static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004251ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004252{
guoci90fc8982018-09-11 17:45:45 -04004253 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004254 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004255 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004256 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004257
Guido van Rossum495da292019-03-07 12:38:08 -08004258 if (is_async && c->c_feature_version < 5) {
4259 ast_error(c, n,
4260 "Async with statements are only supported in Python 3.5 and greater");
4261 return NULL;
4262 }
4263
Georg Brandl0c315622009-05-25 21:10:36 +00004264 REQ(n, with_stmt);
4265
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004266 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4267 nch_minus_type = NCH(n) - has_type_comment;
4268
4269 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004270 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004271 if (!items)
4272 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004273 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004274 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4275 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004276 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004277 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004278 }
4279
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004280 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4281 if (!body)
4282 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004283 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004284
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004285 if (has_type_comment) {
4286 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4287 if (!type_comment)
4288 return NULL;
4289 }
4290 else
4291 type_comment = NULL;
4292
Yury Selivanov75445082015-05-11 22:57:16 -04004293 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004294 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004295 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004296 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004297 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004298 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004299}
4300
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004301static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004302ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004303{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004304 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004305 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004306 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004307 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004308 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004309
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004310 REQ(n, classdef);
4311
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004312 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004313 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004314 if (!s)
4315 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004316 get_last_end_pos(s, &end_lineno, &end_col_offset);
4317
Benjamin Peterson30760062008-11-25 04:02:28 +00004318 classname = NEW_IDENTIFIER(CHILD(n, 1));
4319 if (!classname)
4320 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004321 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004322 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004323 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004324 LINENO(n), n->n_col_offset,
4325 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004326 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004327
4328 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004329 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004330 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004331 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004332 get_last_end_pos(s, &end_lineno, &end_col_offset);
4333
Benjamin Peterson30760062008-11-25 04:02:28 +00004334 classname = NEW_IDENTIFIER(CHILD(n, 1));
4335 if (!classname)
4336 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004337 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004338 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004339 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004340 LINENO(n), n->n_col_offset,
4341 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004342 }
4343
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004344 /* class NAME '(' arglist ')' ':' suite */
4345 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004346 {
4347 PyObject *dummy_name;
4348 expr_ty dummy;
4349 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4350 if (!dummy_name)
4351 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004352 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4353 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4354 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004355 call = ast_for_call(c, CHILD(n, 3), dummy,
4356 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004357 if (!call)
4358 return NULL;
4359 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004360 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004361 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004362 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004363 get_last_end_pos(s, &end_lineno, &end_col_offset);
4364
Benjamin Peterson30760062008-11-25 04:02:28 +00004365 classname = NEW_IDENTIFIER(CHILD(n, 1));
4366 if (!classname)
4367 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004368 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004369 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004370
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004371 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004372 decorator_seq, LINENO(n), n->n_col_offset,
4373 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004374}
4375
4376static stmt_ty
4377ast_for_stmt(struct compiling *c, const node *n)
4378{
4379 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004380 assert(NCH(n) == 1);
4381 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004382 }
4383 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004384 assert(num_stmts(n) == 1);
4385 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004386 }
4387 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004388 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004389 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4390 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004391 */
4392 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004393 case expr_stmt:
4394 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004395 case del_stmt:
4396 return ast_for_del_stmt(c, n);
4397 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004398 return Pass(LINENO(n), n->n_col_offset,
4399 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004400 case flow_stmt:
4401 return ast_for_flow_stmt(c, n);
4402 case import_stmt:
4403 return ast_for_import_stmt(c, n);
4404 case global_stmt:
4405 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004406 case nonlocal_stmt:
4407 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004408 case assert_stmt:
4409 return ast_for_assert_stmt(c, n);
4410 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004411 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004412 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4413 TYPE(n), NCH(n));
4414 return NULL;
4415 }
4416 }
4417 else {
4418 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004419 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004420 */
4421 node *ch = CHILD(n, 0);
4422 REQ(n, compound_stmt);
4423 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004424 case if_stmt:
4425 return ast_for_if_stmt(c, ch);
4426 case while_stmt:
4427 return ast_for_while_stmt(c, ch);
4428 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004429 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004430 case try_stmt:
4431 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004432 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004433 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004434 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004435 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004436 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004437 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 case decorated:
4439 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004440 case async_stmt:
4441 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004442 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004443 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004444 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445 TYPE(n), NCH(n));
4446 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004447 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004448 }
4449}
4450
4451static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004452parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004453{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004454 const char *end;
4455 long x;
4456 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004457 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004458 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004460 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004461 errno = 0;
4462 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004463 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004464 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004465 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004466 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004467 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004468 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004469 }
4470 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004471 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004472 if (*end == '\0') {
4473 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004474 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004475 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004476 }
4477 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004478 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004479 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004480 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4481 if (compl.imag == -1.0 && PyErr_Occurred())
4482 return NULL;
4483 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004484 }
4485 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004486 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004487 dx = PyOS_string_to_double(s, NULL, NULL);
4488 if (dx == -1.0 && PyErr_Occurred())
4489 return NULL;
4490 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004492}
4493
4494static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004495parsenumber(struct compiling *c, const char *s)
4496{
4497 char *dup, *end;
4498 PyObject *res = NULL;
4499
4500 assert(s != NULL);
4501
4502 if (strchr(s, '_') == NULL) {
4503 return parsenumber_raw(c, s);
4504 }
4505 /* Create a duplicate without underscores. */
4506 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004507 if (dup == NULL) {
4508 return PyErr_NoMemory();
4509 }
Brett Cannona721aba2016-09-09 14:57:09 -07004510 end = dup;
4511 for (; *s; s++) {
4512 if (*s != '_') {
4513 *end++ = *s;
4514 }
4515 }
4516 *end = '\0';
4517 res = parsenumber_raw(c, dup);
4518 PyMem_Free(dup);
4519 return res;
4520}
4521
4522static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004523decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004524{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004525 const char *s, *t;
4526 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004527 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4528 while (s < end && (*s & 0x80)) s++;
4529 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004530 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531}
4532
Eric V. Smith56466482016-10-31 14:46:26 -04004533static int
4534warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004535 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004536{
4537 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4538 first_invalid_escape_char);
4539 if (msg == NULL) {
4540 return -1;
4541 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004542 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004543 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004544 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004545 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004546 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4547 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004548 to get a more accurate error report */
4549 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004550 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004551 }
4552 Py_DECREF(msg);
4553 return -1;
4554 }
4555 Py_DECREF(msg);
4556 return 0;
4557}
4558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004559static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004560decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4561 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004562{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004563 PyObject *v, *u;
4564 char *buf;
4565 char *p;
4566 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004567
Benjamin Peterson202803a2016-02-25 22:34:45 -08004568 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004569 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004570 return NULL;
4571 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4572 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4573 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4574 if (u == NULL)
4575 return NULL;
4576 p = buf = PyBytes_AsString(u);
4577 end = s + len;
4578 while (s < end) {
4579 if (*s == '\\') {
4580 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004581 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004582 strcpy(p, "u005c");
4583 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004584 if (s >= end)
4585 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004586 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004587 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004588 if (*s & 0x80) { /* XXX inefficient */
4589 PyObject *w;
4590 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03004591 const void *data;
Benjamin Peterson202803a2016-02-25 22:34:45 -08004592 Py_ssize_t len, i;
4593 w = decode_utf8(c, &s, end);
4594 if (w == NULL) {
4595 Py_DECREF(u);
4596 return NULL;
4597 }
4598 kind = PyUnicode_KIND(w);
4599 data = PyUnicode_DATA(w);
4600 len = PyUnicode_GET_LENGTH(w);
4601 for (i = 0; i < len; i++) {
4602 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4603 sprintf(p, "\\U%08x", chr);
4604 p += 10;
4605 }
4606 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004607 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004608 Py_DECREF(w);
4609 } else {
4610 *p++ = *s++;
4611 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004612 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004613 len = p - buf;
4614 s = buf;
4615
Eric V. Smith56466482016-10-31 14:46:26 -04004616 const char *first_invalid_escape;
4617 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4618
4619 if (v != NULL && first_invalid_escape != NULL) {
4620 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4621 /* We have not decref u before because first_invalid_escape points
4622 inside u. */
4623 Py_XDECREF(u);
4624 Py_DECREF(v);
4625 return NULL;
4626 }
4627 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004628 Py_XDECREF(u);
4629 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004630}
4631
Eric V. Smith56466482016-10-31 14:46:26 -04004632static PyObject *
4633decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4634 size_t len)
4635{
4636 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004637 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004638 &first_invalid_escape);
4639 if (result == NULL)
4640 return NULL;
4641
4642 if (first_invalid_escape != NULL) {
4643 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4644 Py_DECREF(result);
4645 return NULL;
4646 }
4647 }
4648 return result;
4649}
4650
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004651/* Shift locations for the given node and all its children by adding `lineno`
4652 and `col_offset` to existing locations. */
4653static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4654{
4655 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004656 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004657 for (int i = 0; i < NCH(n); ++i) {
4658 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4659 /* Shifting column offsets unnecessary if there's been newlines. */
4660 col_offset = 0;
4661 }
4662 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4663 }
4664 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004665 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004666}
4667
4668/* Fix locations for the given node and its children.
4669
4670 `parent` is the enclosing node.
4671 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004672 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004673*/
4674static void
4675fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4676{
4677 char *substr = NULL;
4678 char *start;
4679 int lines = LINENO(parent) - 1;
4680 int cols = parent->n_col_offset;
4681 /* Find the full fstring to fix location information in `n`. */
4682 while (parent && parent->n_type != STRING)
4683 parent = parent->n_child;
4684 if (parent && parent->n_str) {
4685 substr = strstr(parent->n_str, expr_str);
4686 if (substr) {
4687 start = substr;
4688 while (start > parent->n_str) {
4689 if (start[0] == '\n')
4690 break;
4691 start--;
4692 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004693 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004694 /* adjust the start based on the number of newlines encountered
4695 before the f-string expression */
4696 for (char* p = parent->n_str; p < substr; p++) {
4697 if (*p == '\n') {
4698 lines++;
4699 }
4700 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004701 }
4702 }
4703 fstring_shift_node_locations(n, lines, cols);
4704}
4705
Eric V. Smith451d0e32016-09-09 21:56:20 -04004706/* Compile this expression in to an expr_ty. Add parens around the
4707 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004708static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004709fstring_compile_expr(const char *expr_start, const char *expr_end,
4710 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004711
Eric V. Smith235a6f02015-09-19 14:51:32 -04004712{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004713 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004714 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004715 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004716 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004717 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004718
Eric V. Smith1d44c412015-09-23 07:49:00 -04004719 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004720 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004721 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4722 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004723
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004724 /* If the substring is all whitespace, it's an error. We need to catch this
4725 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4726 because turning the expression '' in to '()' would go from being invalid
4727 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004728 for (s = expr_start; s != expr_end; s++) {
4729 char c = *s;
4730 /* The Python parser ignores only the following whitespace
4731 characters (\r already is converted to \n). */
4732 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004733 break;
4734 }
4735 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004736 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004737 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004738 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004739 }
4740
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 len = expr_end - expr_start;
4742 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4743 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004744 if (str == NULL) {
4745 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004746 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004747 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004748
Eric V. Smith451d0e32016-09-09 21:56:20 -04004749 str[0] = '(';
4750 memcpy(str+1, expr_start, len);
4751 str[len+1] = ')';
4752 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004753
Victor Stinner37d66d72019-06-13 02:16:41 +02004754 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004755 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004756 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4757 Py_eval_input, 0);
4758 if (!mod_n) {
4759 PyMem_RawFree(str);
4760 return NULL;
4761 }
4762 /* Reuse str to find the correct column offset. */
4763 str[0] = '{';
4764 str[len+1] = '}';
4765 fstring_fix_node_location(n, mod_n, str);
4766 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004767 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004768 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004769 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004770 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004771 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004772}
4773
4774/* Return -1 on error.
4775
4776 Return 0 if we reached the end of the literal.
4777
4778 Return 1 if we haven't reached the end of the literal, but we want
4779 the caller to process the literal up to this point. Used for
4780 doubled braces.
4781*/
4782static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004783fstring_find_literal(const char **str, const char *end, int raw,
4784 PyObject **literal, int recurse_lvl,
4785 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004786{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004787 /* Get any literal string. It ends when we hit an un-doubled left
4788 brace (which isn't part of a unicode name escape such as
4789 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004790
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004791 const char *s = *str;
4792 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004793 int result = 0;
4794
Eric V. Smith235a6f02015-09-19 14:51:32 -04004795 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004796 while (s < end) {
4797 char ch = *s++;
4798 if (!raw && ch == '\\' && s < end) {
4799 ch = *s++;
4800 if (ch == 'N') {
4801 if (s < end && *s++ == '{') {
4802 while (s < end && *s++ != '}') {
4803 }
4804 continue;
4805 }
4806 break;
4807 }
4808 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4809 return -1;
4810 }
4811 }
4812 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004813 /* Check for doubled braces, but only at the top level. If
4814 we checked at every level, then f'{0:{3}}' would fail
4815 with the two closing braces. */
4816 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004817 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004818 /* We're going to tell the caller that the literal ends
4819 here, but that they should continue scanning. But also
4820 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004821 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004822 result = 1;
4823 goto done;
4824 }
4825
4826 /* Where a single '{' is the start of a new expression, a
4827 single '}' is not allowed. */
4828 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004829 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004830 ast_error(c, n, "f-string: single '}' is not allowed");
4831 return -1;
4832 }
4833 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004834 /* We're either at a '{', which means we're starting another
4835 expression; or a '}', which means we're at the end of this
4836 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004837 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004838 break;
4839 }
4840 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004841 *str = s;
4842 assert(s <= end);
4843 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004844done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004845 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004846 if (raw)
4847 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004848 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004849 NULL, NULL);
4850 else
Eric V. Smith56466482016-10-31 14:46:26 -04004851 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004852 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004853 if (!*literal)
4854 return -1;
4855 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856 return result;
4857}
4858
4859/* Forward declaration because parsing is recursive. */
4860static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004861fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004862 struct compiling *c, const node *n);
4863
Eric V. Smith451d0e32016-09-09 21:56:20 -04004864/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004865 expression (so it must be a '{'). Returns the FormattedValue node, which
4866 includes the expression, conversion character, format_spec expression, and
4867 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868
4869 Note that I don't do a perfect job here: I don't make sure that a
4870 closing brace doesn't match an opening paren, for example. It
4871 doesn't need to error on all invalid expressions, just correctly
4872 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004873 will be caught when we parse it later.
4874
4875 *expression is set to the expression. For an '=' "debug" expression,
4876 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004877 including the '=' and any whitespace around it, as a string object). If
4878 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004880fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004881 PyObject **expr_text, expr_ty *expression,
4882 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004883{
4884 /* Return -1 on error, else 0. */
4885
Eric V. Smith451d0e32016-09-09 21:56:20 -04004886 const char *expr_start;
4887 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 expr_ty simple_expression;
4889 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004890 int conversion = -1; /* The conversion char. Use default if not
4891 specified, or !r if using = and no format
4892 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004893
4894 /* 0 if we're not in a string, else the quote char we're trying to
4895 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004896 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897
4898 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4899 int string_type = 0;
4900
4901 /* Keep track of nesting level for braces/parens/brackets in
4902 expressions. */
4903 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004904 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004906 *expr_text = NULL;
4907
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908 /* Can only nest one level deep. */
4909 if (recurse_lvl >= 2) {
4910 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004911 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004912 }
4913
4914 /* The first char must be a left brace, or we wouldn't have gotten
4915 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004916 assert(**str == '{');
4917 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918
Eric V. Smith451d0e32016-09-09 21:56:20 -04004919 expr_start = *str;
4920 for (; *str < end; (*str)++) {
4921 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004922
4923 /* Loop invariants. */
4924 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926 if (quote_char)
4927 assert(string_type == 1 || string_type == 3);
4928 else
4929 assert(string_type == 0);
4930
Eric V. Smith451d0e32016-09-09 21:56:20 -04004931 ch = **str;
4932 /* Nowhere inside an expression is a backslash allowed. */
4933 if (ch == '\\') {
4934 /* Error: can't include a backslash character, inside
4935 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004936 ast_error(c, n,
4937 "f-string expression part "
4938 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004939 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004940 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004941 if (quote_char) {
4942 /* We're inside a string. See if we're at the end. */
4943 /* This code needs to implement the same non-error logic
4944 as tok_get from tokenizer.c, at the letter_quote
4945 label. To actually share that code would be a
4946 nightmare. But, it's unlikely to change and is small,
4947 so duplicate it here. Note we don't need to catch all
4948 of the errors, since they'll be caught when parsing the
4949 expression. We just need to match the non-error
4950 cases. Thus we can ignore \n in single-quoted strings,
4951 for example. Or non-terminated strings. */
4952 if (ch == quote_char) {
4953 /* Does this match the string_type (single or triple
4954 quoted)? */
4955 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004956 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004957 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004958 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959 string_type = 0;
4960 quote_char = 0;
4961 continue;
4962 }
4963 } else {
4964 /* We're at the end of a normal string. */
4965 quote_char = 0;
4966 string_type = 0;
4967 continue;
4968 }
4969 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004970 } else if (ch == '\'' || ch == '"') {
4971 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004972 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004973 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004974 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004975 } else {
4976 /* Start of a normal string. */
4977 string_type = 1;
4978 }
4979 /* Start looking for the end of the string. */
4980 quote_char = ch;
4981 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004982 if (nested_depth >= MAXLEVEL) {
4983 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004984 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004985 }
4986 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004988 } else if (ch == '#') {
4989 /* Error: can't include a comment character, inside parens
4990 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004991 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004992 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004994 (ch == '!' || ch == ':' || ch == '}' ||
4995 ch == '=' || ch == '>' || ch == '<')) {
4996 /* See if there's a next character. */
4997 if (*str+1 < end) {
4998 char next = *(*str+1);
4999
5000 /* For "!=". since '=' is not an allowed conversion character,
5001 nothing is lost in this test. */
5002 if ((ch == '!' && next == '=') || /* != */
5003 (ch == '=' && next == '=') || /* == */
5004 (ch == '<' && next == '=') || /* <= */
5005 (ch == '>' && next == '=') /* >= */
5006 ) {
5007 *str += 1;
5008 continue;
5009 }
5010 /* Don't get out of the loop for these, if they're single
5011 chars (not part of 2-char tokens). If by themselves, they
5012 don't end an expression (unlike say '!'). */
5013 if (ch == '>' || ch == '<') {
5014 continue;
5015 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005016 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005017
Eric V. Smith235a6f02015-09-19 14:51:32 -04005018 /* Normal way out of this loop. */
5019 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005020 } else if (ch == ']' || ch == '}' || ch == ')') {
5021 if (!nested_depth) {
5022 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005023 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005024 }
5025 nested_depth--;
5026 int opening = parenstack[nested_depth];
5027 if (!((opening == '(' && ch == ')') ||
5028 (opening == '[' && ch == ']') ||
5029 (opening == '{' && ch == '}')))
5030 {
5031 ast_error(c, n,
5032 "f-string: closing parenthesis '%c' "
5033 "does not match opening parenthesis '%c'",
5034 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005035 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005036 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005037 } else {
5038 /* Just consume this char and loop around. */
5039 }
5040 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005041 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005042 /* If we leave this loop in a string or with mismatched parens, we
5043 don't care. We'll get a syntax error when compiling the
5044 expression. But, we can produce a better error message, so
5045 let's just do that.*/
5046 if (quote_char) {
5047 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005048 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 }
5050 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005051 int opening = parenstack[nested_depth - 1];
5052 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005053 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005054 }
5055
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005058
5059 /* Compile the expression as soon as possible, so we show errors
5060 related to the expression before errors related to the
5061 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005063 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005064 goto error;
5065
5066 /* Check for =, which puts the text value of the expression in
5067 expr_text. */
5068 if (**str == '=') {
5069 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005070
5071 /* Skip over ASCII whitespace. No need to test for end of string
5072 here, since we know there's at least a trailing quote somewhere
5073 ahead. */
5074 while (Py_ISSPACE(**str)) {
5075 *str += 1;
5076 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005077
5078 /* Set *expr_text to the text of the expression. */
5079 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5080 if (!*expr_text) {
5081 goto error;
5082 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005083 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005084
5085 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086 if (**str == '!') {
5087 *str += 1;
5088 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089 goto unexpected_end_of_string;
5090
Eric V. Smith451d0e32016-09-09 21:56:20 -04005091 conversion = **str;
5092 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005093
5094 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005095 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005096 ast_error(c, n,
5097 "f-string: invalid conversion character: "
5098 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005099 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005100 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005101
5102 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005103
5104 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 if (**str == ':') {
5108 *str += 1;
5109 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110 goto unexpected_end_of_string;
5111
5112 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005114 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005115 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116 }
5117
Eric V. Smith451d0e32016-09-09 21:56:20 -04005118 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 goto unexpected_end_of_string;
5120
5121 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 assert(*str < end);
5123 assert(**str == '}');
5124 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005126 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005127 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005128 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005129 conversion = 'r';
5130 }
5131
Eric V. Smith451d0e32016-09-09 21:56:20 -04005132 /* And now create the FormattedValue node that represents this
5133 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005134 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005135 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005136 n->n_col_offset, n->n_end_lineno,
5137 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005139 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005140
5141 return 0;
5142
5143unexpected_end_of_string:
5144 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005145 /* Falls through to error. */
5146
5147error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005148 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005150
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151}
5152
5153/* Return -1 on error.
5154
5155 Return 0 if we have a literal (possible zero length) and an
5156 expression (zero length if at the end of the string.
5157
5158 Return 1 if we have a literal, but no expression, and we want the
5159 caller to call us again. This is used to deal with doubled
5160 braces.
5161
5162 When called multiple times on the string 'a{{b{0}c', this function
5163 will return:
5164
5165 1. the literal 'a{' with no expression, and a return value
5166 of 1. Despite the fact that there's no expression, the return
5167 value of 1 means we're not finished yet.
5168
5169 2. the literal 'b' and the expression '0', with a return value of
5170 0. The fact that there's an expression means we're not finished.
5171
5172 3. literal 'c' with no expression and a return value of 0. The
5173 combination of the return value of 0 with no expression means
5174 we're finished.
5175*/
5176static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005177fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5178 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005179 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005180 struct compiling *c, const node *n)
5181{
5182 int result;
5183
5184 assert(*literal == NULL && *expression == NULL);
5185
5186 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005187 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005188 if (result < 0)
5189 goto error;
5190
5191 assert(result == 0 || result == 1);
5192
5193 if (result == 1)
5194 /* We have a literal, but don't look at the expression. */
5195 return 1;
5196
Eric V. Smith451d0e32016-09-09 21:56:20 -04005197 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005198 /* We're at the end of the string or the end of a nested
5199 f-string: no expression. The top-level error case where we
5200 expect to be at the end of the string but we're at a '}' is
5201 handled later. */
5202 return 0;
5203
5204 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005205 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005206
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005207 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5208 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209 goto error;
5210
5211 return 0;
5212
5213error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005214 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215 return -1;
5216}
5217
5218#define EXPRLIST_N_CACHED 64
5219
5220typedef struct {
5221 /* Incrementally build an array of expr_ty, so be used in an
5222 asdl_seq. Cache some small but reasonably sized number of
5223 expr_ty's, and then after that start dynamically allocating,
5224 doubling the number allocated each time. Note that the f-string
5225 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005226 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005227 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005228
5229 Py_ssize_t allocated; /* Number we've allocated. */
5230 Py_ssize_t size; /* Number we've used. */
5231 expr_ty *p; /* Pointer to the memory we're actually
5232 using. Will point to 'data' until we
5233 start dynamically allocating. */
5234 expr_ty data[EXPRLIST_N_CACHED];
5235} ExprList;
5236
5237#ifdef NDEBUG
5238#define ExprList_check_invariants(l)
5239#else
5240static void
5241ExprList_check_invariants(ExprList *l)
5242{
5243 /* Check our invariants. Make sure this object is "live", and
5244 hasn't been deallocated. */
5245 assert(l->size >= 0);
5246 assert(l->p != NULL);
5247 if (l->size <= EXPRLIST_N_CACHED)
5248 assert(l->data == l->p);
5249}
5250#endif
5251
5252static void
5253ExprList_Init(ExprList *l)
5254{
5255 l->allocated = EXPRLIST_N_CACHED;
5256 l->size = 0;
5257
5258 /* Until we start allocating dynamically, p points to data. */
5259 l->p = l->data;
5260
5261 ExprList_check_invariants(l);
5262}
5263
5264static int
5265ExprList_Append(ExprList *l, expr_ty exp)
5266{
5267 ExprList_check_invariants(l);
5268 if (l->size >= l->allocated) {
5269 /* We need to alloc (or realloc) the memory. */
5270 Py_ssize_t new_size = l->allocated * 2;
5271
5272 /* See if we've ever allocated anything dynamically. */
5273 if (l->p == l->data) {
5274 Py_ssize_t i;
5275 /* We're still using the cached data. Switch to
5276 alloc-ing. */
5277 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5278 if (!l->p)
5279 return -1;
5280 /* Copy the cached data into the new buffer. */
5281 for (i = 0; i < l->size; i++)
5282 l->p[i] = l->data[i];
5283 } else {
5284 /* Just realloc. */
5285 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5286 if (!tmp) {
5287 PyMem_RawFree(l->p);
5288 l->p = NULL;
5289 return -1;
5290 }
5291 l->p = tmp;
5292 }
5293
5294 l->allocated = new_size;
5295 assert(l->allocated == 2 * l->size);
5296 }
5297
5298 l->p[l->size++] = exp;
5299
5300 ExprList_check_invariants(l);
5301 return 0;
5302}
5303
5304static void
5305ExprList_Dealloc(ExprList *l)
5306{
5307 ExprList_check_invariants(l);
5308
5309 /* If there's been an error, or we've never dynamically allocated,
5310 do nothing. */
5311 if (!l->p || l->p == l->data) {
5312 /* Do nothing. */
5313 } else {
5314 /* We have dynamically allocated. Free the memory. */
5315 PyMem_RawFree(l->p);
5316 }
5317 l->p = NULL;
5318 l->size = -1;
5319}
5320
5321static asdl_seq *
5322ExprList_Finish(ExprList *l, PyArena *arena)
5323{
5324 asdl_seq *seq;
5325
5326 ExprList_check_invariants(l);
5327
5328 /* Allocate the asdl_seq and copy the expressions in to it. */
5329 seq = _Py_asdl_seq_new(l->size, arena);
5330 if (seq) {
5331 Py_ssize_t i;
5332 for (i = 0; i < l->size; i++)
5333 asdl_seq_SET(seq, i, l->p[i]);
5334 }
5335 ExprList_Dealloc(l);
5336 return seq;
5337}
5338
5339/* The FstringParser is designed to add a mix of strings and
5340 f-strings, and concat them together as needed. Ultimately, it
5341 generates an expr_ty. */
5342typedef struct {
5343 PyObject *last_str;
5344 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005345 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005346} FstringParser;
5347
5348#ifdef NDEBUG
5349#define FstringParser_check_invariants(state)
5350#else
5351static void
5352FstringParser_check_invariants(FstringParser *state)
5353{
5354 if (state->last_str)
5355 assert(PyUnicode_CheckExact(state->last_str));
5356 ExprList_check_invariants(&state->expr_list);
5357}
5358#endif
5359
5360static void
5361FstringParser_Init(FstringParser *state)
5362{
5363 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005364 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005365 ExprList_Init(&state->expr_list);
5366 FstringParser_check_invariants(state);
5367}
5368
5369static void
5370FstringParser_Dealloc(FstringParser *state)
5371{
5372 FstringParser_check_invariants(state);
5373
5374 Py_XDECREF(state->last_str);
5375 ExprList_Dealloc(&state->expr_list);
5376}
5377
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005378/* Constants for the following */
5379static PyObject *u_kind;
5380
5381/* Compute 'kind' field for string Constant (either 'u' or None) */
5382static PyObject *
5383make_kind(struct compiling *c, const node *n)
5384{
5385 char *s = NULL;
5386 PyObject *kind = NULL;
5387
5388 /* Find the first string literal, if any */
5389 while (TYPE(n) != STRING) {
5390 if (NCH(n) == 0)
5391 return NULL;
5392 n = CHILD(n, 0);
5393 }
5394 REQ(n, STRING);
5395
5396 /* If it starts with 'u', return a PyUnicode "u" string */
5397 s = STR(n);
5398 if (s && *s == 'u') {
5399 if (!u_kind) {
5400 u_kind = PyUnicode_InternFromString("u");
5401 if (!u_kind)
5402 return NULL;
5403 }
5404 kind = u_kind;
5405 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5406 return NULL;
5407 }
5408 Py_INCREF(kind);
5409 }
5410 return kind;
5411}
5412
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005413/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005414static expr_ty
5415make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5416{
5417 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005418 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005419 *str = NULL;
5420 assert(PyUnicode_CheckExact(s));
5421 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5422 Py_DECREF(s);
5423 return NULL;
5424 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005425 kind = make_kind(c, n);
5426 if (kind == NULL && PyErr_Occurred())
5427 return NULL;
5428 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005429 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005430}
5431
5432/* Add a non-f-string (that is, a regular literal string). str is
5433 decref'd. */
5434static int
5435FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5436{
5437 FstringParser_check_invariants(state);
5438
5439 assert(PyUnicode_CheckExact(str));
5440
5441 if (PyUnicode_GET_LENGTH(str) == 0) {
5442 Py_DECREF(str);
5443 return 0;
5444 }
5445
5446 if (!state->last_str) {
5447 /* We didn't have a string before, so just remember this one. */
5448 state->last_str = str;
5449 } else {
5450 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005451 PyUnicode_AppendAndDel(&state->last_str, str);
5452 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005453 return -1;
5454 }
5455 FstringParser_check_invariants(state);
5456 return 0;
5457}
5458
Eric V. Smith451d0e32016-09-09 21:56:20 -04005459/* Parse an f-string. The f-string is in *str to end, with no
5460 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005461static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005462FstringParser_ConcatFstring(FstringParser *state, const char **str,
5463 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005464 struct compiling *c, const node *n)
5465{
5466 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005467 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005468
5469 /* Parse the f-string. */
5470 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005471 PyObject *literal = NULL;
5472 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005473 expr_ty expression = NULL;
5474
5475 /* If there's a zero length literal in front of the
5476 expression, literal will be NULL. If we're at the end of
5477 the f-string, expression will be NULL (unless result == 1,
5478 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005479 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005480 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005481 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005482 if (result < 0)
5483 return -1;
5484
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005485 /* Add the literal, if any. */
5486 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5487 Py_XDECREF(expr_text);
5488 return -1;
5489 }
5490 /* Add the expr_text, if any. */
5491 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5492 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005493 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005494
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005495 /* We've dealt with the literal and expr_text, their ownership has
5496 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005497
5498 /* See if we should just loop around to get the next literal
5499 and expression, while ignoring the expression this
5500 time. This is used for un-doubling braces, as an
5501 optimization. */
5502 if (result == 1)
5503 continue;
5504
5505 if (!expression)
5506 /* We're done with this f-string. */
5507 break;
5508
5509 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005510 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005511 if (!state->last_str) {
5512 /* Do nothing. No previous literal. */
5513 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005514 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005515 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5516 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5517 return -1;
5518 }
5519
5520 if (ExprList_Append(&state->expr_list, expression) < 0)
5521 return -1;
5522 }
5523
Eric V. Smith235a6f02015-09-19 14:51:32 -04005524 /* If recurse_lvl is zero, then we must be at the end of the
5525 string. Otherwise, we must be at a right brace. */
5526
Eric V. Smith451d0e32016-09-09 21:56:20 -04005527 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005528 ast_error(c, n, "f-string: unexpected end of string");
5529 return -1;
5530 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005531 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005532 ast_error(c, n, "f-string: expecting '}'");
5533 return -1;
5534 }
5535
5536 FstringParser_check_invariants(state);
5537 return 0;
5538}
5539
5540/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005541 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005542static expr_ty
5543FstringParser_Finish(FstringParser *state, struct compiling *c,
5544 const node *n)
5545{
5546 asdl_seq *seq;
5547
5548 FstringParser_check_invariants(state);
5549
5550 /* If we're just a constant string with no expressions, return
5551 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005552 if (!state->fmode) {
5553 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005554 if (!state->last_str) {
5555 /* Create a zero length string. */
5556 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5557 if (!state->last_str)
5558 goto error;
5559 }
5560 return make_str_node_and_del(&state->last_str, c, n);
5561 }
5562
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005563 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005564 last node in our expression list. */
5565 if (state->last_str) {
5566 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5567 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5568 goto error;
5569 }
5570 /* This has already been freed. */
5571 assert(state->last_str == NULL);
5572
5573 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5574 if (!seq)
5575 goto error;
5576
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005577 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5578 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005579
5580error:
5581 FstringParser_Dealloc(state);
5582 return NULL;
5583}
5584
Eric V. Smith451d0e32016-09-09 21:56:20 -04005585/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5586 at end, parse it into an expr_ty. Return NULL on error. Adjust
5587 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005588static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005589fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005590 struct compiling *c, const node *n)
5591{
5592 FstringParser state;
5593
5594 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005595 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005596 c, n) < 0) {
5597 FstringParser_Dealloc(&state);
5598 return NULL;
5599 }
5600
5601 return FstringParser_Finish(&state, c, n);
5602}
5603
5604/* n is a Python string literal, including the bracketing quote
5605 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005606 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005607 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005608 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5609 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005610*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005611static int
5612parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5613 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005614{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005615 size_t len;
5616 const char *s = STR(n);
5617 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005618 int fmode = 0;
5619 *bytesmode = 0;
5620 *rawmode = 0;
5621 *result = NULL;
5622 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005623 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005624 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005625 if (quote == 'b' || quote == 'B') {
5626 quote = *++s;
5627 *bytesmode = 1;
5628 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005629 else if (quote == 'u' || quote == 'U') {
5630 quote = *++s;
5631 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005632 else if (quote == 'r' || quote == 'R') {
5633 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005634 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005635 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005636 else if (quote == 'f' || quote == 'F') {
5637 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005638 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005639 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005640 else {
5641 break;
5642 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005643 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005644 }
Guido van Rossum495da292019-03-07 12:38:08 -08005645
5646 /* fstrings are only allowed in Python 3.6 and greater */
5647 if (fmode && c->c_feature_version < 6) {
5648 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5649 return -1;
5650 }
5651
Eric V. Smith451d0e32016-09-09 21:56:20 -04005652 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005653 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005654 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005655 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005656 if (quote != '\'' && quote != '\"') {
5657 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005658 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005659 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005661 s++;
5662 len = strlen(s);
5663 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005664 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005665 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005666 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005667 }
5668 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005669 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005670 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005671 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005672 }
5673 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005674 /* A triple quoted string. We've already skipped one quote at
5675 the start and one at the end of the string. Now skip the
5676 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005677 s += 2;
5678 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005679 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005680 if (s[--len] != quote || s[--len] != quote) {
5681 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005682 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005683 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005684 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005685
Eric V. Smith451d0e32016-09-09 21:56:20 -04005686 if (fmode) {
5687 /* Just return the bytes. The caller will parse the resulting
5688 string. */
5689 *fstr = s;
5690 *fstrlen = len;
5691 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005692 }
5693
Eric V. Smith451d0e32016-09-09 21:56:20 -04005694 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005695 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005696 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005697 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005698 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005699 const char *ch;
5700 for (ch = s; *ch; ch++) {
5701 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005702 ast_error(c, n,
5703 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005704 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005705 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005706 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005707 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005708 if (*rawmode)
5709 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005710 else
Eric V. Smith56466482016-10-31 14:46:26 -04005711 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005712 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005713 if (*rawmode)
5714 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005715 else
Eric V. Smith56466482016-10-31 14:46:26 -04005716 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005717 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005718 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005719}
5720
Eric V. Smith235a6f02015-09-19 14:51:32 -04005721/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5722 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005723 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005724 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005725 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005726 node if there's just an f-string (with no leading or trailing
5727 literals), or a JoinedStr node if there are multiple f-strings or
5728 any literals involved. */
5729static expr_ty
5730parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005731{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005732 int bytesmode = 0;
5733 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005734 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005735
5736 FstringParser state;
5737 FstringParser_Init(&state);
5738
5739 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005740 int this_bytesmode;
5741 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005742 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005743 const char *fstr;
5744 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005745
5746 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005747 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5748 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005749 goto error;
5750
5751 /* Check that we're not mixing bytes with unicode. */
5752 if (i != 0 && bytesmode != this_bytesmode) {
5753 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005754 /* s is NULL if the current string part is an f-string. */
5755 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005756 goto error;
5757 }
5758 bytesmode = this_bytesmode;
5759
Eric V. Smith451d0e32016-09-09 21:56:20 -04005760 if (fstr != NULL) {
5761 int result;
5762 assert(s == NULL && !bytesmode);
5763 /* This is an f-string. Parse and concatenate it. */
5764 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5765 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005766 if (result < 0)
5767 goto error;
5768 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005769 /* A string or byte string. */
5770 assert(s != NULL && fstr == NULL);
5771
Eric V. Smith451d0e32016-09-09 21:56:20 -04005772 assert(bytesmode ? PyBytes_CheckExact(s) :
5773 PyUnicode_CheckExact(s));
5774
Eric V. Smith451d0e32016-09-09 21:56:20 -04005775 if (bytesmode) {
5776 /* For bytes, concat as we go. */
5777 if (i == 0) {
5778 /* First time, just remember this value. */
5779 bytes_str = s;
5780 } else {
5781 PyBytes_ConcatAndDel(&bytes_str, s);
5782 if (!bytes_str)
5783 goto error;
5784 }
5785 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005786 /* This is a regular string. Concatenate it. */
5787 if (FstringParser_ConcatAndDel(&state, s) < 0)
5788 goto error;
5789 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005790 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005791 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005792 if (bytesmode) {
5793 /* Just return the bytes object and we're done. */
5794 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5795 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005796 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005797 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005799
Eric V. Smith235a6f02015-09-19 14:51:32 -04005800 /* We're not a bytes string, bytes_str should never have been set. */
5801 assert(bytes_str == NULL);
5802
5803 return FstringParser_Finish(&state, c, n);
5804
5805error:
5806 Py_XDECREF(bytes_str);
5807 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005808 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005809}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005810
5811PyObject *
5812_PyAST_GetDocString(asdl_seq *body)
5813{
5814 if (!asdl_seq_LEN(body)) {
5815 return NULL;
5816 }
5817 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5818 if (st->kind != Expr_kind) {
5819 return NULL;
5820 }
5821 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005822 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5823 return e->v.Constant.value;
5824 }
5825 return NULL;
5826}