blob: 2d20ca62aa83780ffc5622b3370b1cc164301dfd [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;
Pablo Galindo16ab0702020-05-15 02:04:52 +01003167 /* Augmented assignments can only have a name, a subscript, or an
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003168 attribute on the left, though, so we have to explicitly check for
3169 those. */
3170 switch (expr1->kind) {
3171 case Name_kind:
3172 case Attribute_kind:
3173 case Subscript_kind:
3174 break;
3175 default:
Pablo Galindo16ab0702020-05-15 02:04:52 +01003176 ast_error(c, ch, "'%s' is an illegal expression for augmented assignment",
3177 get_expr_name(expr1));
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003178 return NULL;
3179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180
Pablo Galindo16ab0702020-05-15 02:04:52 +01003181 /* set_context checks that most expressions are not the left side. */
3182 if(!set_context(c, expr1, Store, ch)) {
3183 return NULL;
3184 }
3185
Thomas Wouters89f507f2006-12-13 04:49:30 +00003186 ch = CHILD(n, 2);
3187 if (TYPE(ch) == testlist)
3188 expr2 = ast_for_testlist(c, ch);
3189 else
3190 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003191 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 return NULL;
3193
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003194 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003195 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 return NULL;
3197
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003198 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3199 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003201 else if (TYPE(CHILD(n, 1)) == annassign) {
3202 expr_ty expr1, expr2, expr3;
3203 node *ch = CHILD(n, 0);
3204 node *deep, *ann = CHILD(n, 1);
3205 int simple = 1;
3206
Guido van Rossum495da292019-03-07 12:38:08 -08003207 /* AnnAssigns are only allowed in Python 3.6 or greater */
3208 if (c->c_feature_version < 6) {
3209 ast_error(c, ch,
3210 "Variable annotation syntax is only supported in Python 3.6 and greater");
3211 return NULL;
3212 }
3213
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003214 /* we keep track of parens to qualify (x) as expression not name */
3215 deep = ch;
3216 while (NCH(deep) == 1) {
3217 deep = CHILD(deep, 0);
3218 }
3219 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3220 simple = 0;
3221 }
3222 expr1 = ast_for_testlist(c, ch);
3223 if (!expr1) {
3224 return NULL;
3225 }
3226 switch (expr1->kind) {
3227 case Name_kind:
3228 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3229 return NULL;
3230 }
3231 expr1->v.Name.ctx = Store;
3232 break;
3233 case Attribute_kind:
3234 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3235 return NULL;
3236 }
3237 expr1->v.Attribute.ctx = Store;
3238 break;
3239 case Subscript_kind:
3240 expr1->v.Subscript.ctx = Store;
3241 break;
3242 case List_kind:
3243 ast_error(c, ch,
3244 "only single target (not list) can be annotated");
3245 return NULL;
3246 case Tuple_kind:
3247 ast_error(c, ch,
3248 "only single target (not tuple) can be annotated");
3249 return NULL;
3250 default:
3251 ast_error(c, ch,
3252 "illegal target for annotation");
3253 return NULL;
3254 }
3255
3256 if (expr1->kind != Name_kind) {
3257 simple = 0;
3258 }
3259 ch = CHILD(ann, 1);
3260 expr2 = ast_for_expr(c, ch);
3261 if (!expr2) {
3262 return NULL;
3263 }
3264 if (NCH(ann) == 2) {
3265 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003266 LINENO(n), n->n_col_offset,
3267 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003268 }
3269 else {
3270 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003271 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003272 expr3 = ast_for_testlist(c, ch);
3273 }
3274 else {
3275 expr3 = ast_for_expr(c, ch);
3276 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003277 if (!expr3) {
3278 return NULL;
3279 }
3280 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003281 LINENO(n), n->n_col_offset,
3282 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003283 }
3284 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003286 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003287 asdl_seq *targets;
3288 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003290 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291
Thomas Wouters89f507f2006-12-13 04:49:30 +00003292 /* a normal assignment */
3293 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003294
3295 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3296 nch_minus_type = num - has_type_comment;
3297
3298 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 if (!targets)
3300 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003301 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003302 expr_ty e;
3303 node *ch = CHILD(n, i);
3304 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003305 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 return NULL;
3307 }
3308 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003312 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003313 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 asdl_seq_SET(targets, i / 2, e);
3317 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003318 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003319 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 expression = ast_for_testlist(c, value);
3321 else
3322 expression = ast_for_expr(c, value);
3323 if (!expression)
3324 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003325 if (has_type_comment) {
3326 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3327 if (!type_comment)
3328 return NULL;
3329 }
3330 else
3331 type_comment = NULL;
3332 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003333 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335}
3336
Benjamin Peterson78565b22009-06-28 19:19:51 +00003337
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003339ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340{
3341 asdl_seq *seq;
3342 int i;
3343 expr_ty e;
3344
3345 REQ(n, exprlist);
3346
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003347 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003349 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351 e = ast_for_expr(c, CHILD(n, i));
3352 if (!e)
3353 return NULL;
3354 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003355 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 }
3358 return seq;
3359}
3360
3361static stmt_ty
3362ast_for_del_stmt(struct compiling *c, const node *n)
3363{
3364 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 /* del_stmt: 'del' exprlist */
3367 REQ(n, del_stmt);
3368
3369 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3370 if (!expr_list)
3371 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003372 return Delete(expr_list, LINENO(n), n->n_col_offset,
3373 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374}
3375
3376static stmt_ty
3377ast_for_flow_stmt(struct compiling *c, const node *n)
3378{
3379 /*
3380 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3381 | yield_stmt
3382 break_stmt: 'break'
3383 continue_stmt: 'continue'
3384 return_stmt: 'return' [testlist]
3385 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003386 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387 raise_stmt: 'raise' [test [',' test [',' test]]]
3388 */
3389 node *ch;
3390
3391 REQ(n, flow_stmt);
3392 ch = CHILD(n, 0);
3393 switch (TYPE(ch)) {
3394 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003395 return Break(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 continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003398 return Continue(LINENO(n), n->n_col_offset,
3399 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003401 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3402 if (!exp)
3403 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003404 return Expr(exp, LINENO(n), n->n_col_offset,
3405 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 }
3407 case return_stmt:
3408 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003409 return Return(NULL, LINENO(n), n->n_col_offset,
3410 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003412 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413 if (!expression)
3414 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003415 return Return(expression, LINENO(n), n->n_col_offset,
3416 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 }
3418 case raise_stmt:
3419 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003420 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3421 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003422 else if (NCH(ch) >= 2) {
3423 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3425 if (!expression)
3426 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003427 if (NCH(ch) == 4) {
3428 cause = ast_for_expr(c, CHILD(ch, 3));
3429 if (!cause)
3430 return NULL;
3431 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003432 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3433 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 }
Stefan Krahf432a322017-08-21 13:09:59 +02003435 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003437 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 "unexpected flow_stmt: %d", TYPE(ch));
3439 return NULL;
3440 }
3441}
3442
3443static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003444alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445{
3446 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003447 import_as_name: NAME ['as' NAME]
3448 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449 dotted_name: NAME ('.' NAME)*
3450 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003451 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003452
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 loop:
3454 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003455 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003456 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003457 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003458 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003459 if (!name)
3460 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003461 if (NCH(n) == 3) {
3462 node *str_node = CHILD(n, 2);
3463 str = NEW_IDENTIFIER(str_node);
3464 if (!str)
3465 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003466 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003467 return NULL;
3468 }
3469 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003470 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003471 return NULL;
3472 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003473 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003474 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 case dotted_as_name:
3476 if (NCH(n) == 1) {
3477 n = CHILD(n, 0);
3478 goto loop;
3479 }
3480 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003481 node *asname_node = CHILD(n, 2);
3482 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003483 if (!a)
3484 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003486 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003487 if (!a->asname)
3488 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003489 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 return a;
3492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003494 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003495 node *name_node = CHILD(n, 0);
3496 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003497 if (!name)
3498 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003499 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003500 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003501 return alias(name, NULL, c->c_arena);
3502 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503 else {
3504 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003505 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003506 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509
3510 len = 0;
3511 for (i = 0; i < NCH(n); i += 2)
3512 /* length of string plus one for the dot */
3513 len += strlen(STR(CHILD(n, i))) + 1;
3514 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003515 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 if (!str)
3517 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003518 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 if (!s)
3520 return NULL;
3521 for (i = 0; i < NCH(n); i += 2) {
3522 char *sch = STR(CHILD(n, i));
3523 strcpy(s, STR(CHILD(n, i)));
3524 s += strlen(sch);
3525 *s++ = '.';
3526 }
3527 --s;
3528 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3530 PyBytes_GET_SIZE(str),
3531 NULL);
3532 Py_DECREF(str);
3533 if (!uni)
3534 return NULL;
3535 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003536 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003537 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3538 Py_DECREF(str);
3539 return NULL;
3540 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003541 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003544 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003545 if (!str)
3546 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003547 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3548 Py_DECREF(str);
3549 return NULL;
3550 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003551 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003553 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554 "unexpected import name: %d", TYPE(n));
3555 return NULL;
3556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557}
3558
3559static stmt_ty
3560ast_for_import_stmt(struct compiling *c, const node *n)
3561{
3562 /*
3563 import_stmt: import_name | import_from
3564 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003565 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3566 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003568 int lineno;
3569 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 int i;
3571 asdl_seq *aliases;
3572
3573 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003574 lineno = LINENO(n);
3575 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003577 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003580 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003581 if (!aliases)
3582 return NULL;
3583 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003584 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003585 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003587 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003589 // Even though n is modified above, the end position is not changed
3590 return Import(aliases, lineno, col_offset,
3591 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003593 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003595 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003596 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003597 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003598 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003600 /* Count the number of dots (for relative imports) and check for the
3601 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003602 for (idx = 1; idx < NCH(n); idx++) {
3603 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003604 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3605 if (!mod)
3606 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003607 idx++;
3608 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003609 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003611 ndots += 3;
3612 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 } else if (TYPE(CHILD(n, idx)) != DOT) {
3614 break;
3615 }
3616 ndots++;
3617 }
3618 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003619 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003620 case STAR:
3621 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003622 n = CHILD(n, idx);
3623 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003624 break;
3625 case LPAR:
3626 /* from ... import (x, y, z) */
3627 n = CHILD(n, idx + 1);
3628 n_children = NCH(n);
3629 break;
3630 case import_as_names:
3631 /* from ... import x, y, z */
3632 n = CHILD(n, idx);
3633 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003634 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003635 ast_error(c, n,
3636 "trailing comma not allowed without"
3637 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 return NULL;
3639 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003640 break;
3641 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003642 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003643 return NULL;
3644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003646 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003647 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649
3650 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003651 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003652 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003653 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003655 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003657 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003658 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003659 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003660 if (!import_alias)
3661 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003662 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003665 if (mod != NULL)
3666 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003667 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003668 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003669 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
Neal Norwitz79792652005-11-14 04:25:03 +00003671 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 "unknown import statement: starts with command '%s'",
3673 STR(CHILD(n, 0)));
3674 return NULL;
3675}
3676
3677static stmt_ty
3678ast_for_global_stmt(struct compiling *c, const node *n)
3679{
3680 /* global_stmt: 'global' NAME (',' NAME)* */
3681 identifier name;
3682 asdl_seq *s;
3683 int i;
3684
3685 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003686 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003690 name = NEW_IDENTIFIER(CHILD(n, i));
3691 if (!name)
3692 return NULL;
3693 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003695 return Global(s, LINENO(n), n->n_col_offset,
3696 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697}
3698
3699static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003700ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3701{
3702 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3703 identifier name;
3704 asdl_seq *s;
3705 int i;
3706
3707 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003708 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003709 if (!s)
3710 return NULL;
3711 for (i = 1; i < NCH(n); i += 2) {
3712 name = NEW_IDENTIFIER(CHILD(n, i));
3713 if (!name)
3714 return NULL;
3715 asdl_seq_SET(s, i / 2, name);
3716 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003717 return Nonlocal(s, LINENO(n), n->n_col_offset,
3718 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003719}
3720
3721static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722ast_for_assert_stmt(struct compiling *c, const node *n)
3723{
3724 /* assert_stmt: 'assert' test [',' test] */
3725 REQ(n, assert_stmt);
3726 if (NCH(n) == 2) {
3727 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3728 if (!expression)
3729 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003730 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3731 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 }
3733 else if (NCH(n) == 4) {
3734 expr_ty expr1, expr2;
3735
3736 expr1 = ast_for_expr(c, CHILD(n, 1));
3737 if (!expr1)
3738 return NULL;
3739 expr2 = ast_for_expr(c, CHILD(n, 3));
3740 if (!expr2)
3741 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003743 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3744 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 }
Neal Norwitz79792652005-11-14 04:25:03 +00003746 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 "improper number of parts to 'assert' statement: %d",
3748 NCH(n));
3749 return NULL;
3750}
3751
3752static asdl_seq *
3753ast_for_suite(struct compiling *c, const node *n)
3754{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003755 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003756 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 stmt_ty s;
3758 int i, total, num, end, pos = 0;
3759 node *ch;
3760
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003761 if (TYPE(n) != func_body_suite) {
3762 REQ(n, suite);
3763 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764
3765 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003766 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003770 n = CHILD(n, 0);
3771 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003773 */
3774 end = NCH(n) - 1;
3775 if (TYPE(CHILD(n, end - 1)) == SEMI)
3776 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003778 for (i = 0; i < end; i += 2) {
3779 ch = CHILD(n, i);
3780 s = ast_for_stmt(c, ch);
3781 if (!s)
3782 return NULL;
3783 asdl_seq_SET(seq, pos++, s);
3784 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 }
3786 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003787 i = 2;
3788 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3789 i += 2;
3790 REQ(CHILD(n, 2), NEWLINE);
3791 }
3792
3793 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003794 ch = CHILD(n, i);
3795 REQ(ch, stmt);
3796 num = num_stmts(ch);
3797 if (num == 1) {
3798 /* small_stmt or compound_stmt with only one child */
3799 s = ast_for_stmt(c, ch);
3800 if (!s)
3801 return NULL;
3802 asdl_seq_SET(seq, pos++, s);
3803 }
3804 else {
3805 int j;
3806 ch = CHILD(ch, 0);
3807 REQ(ch, simple_stmt);
3808 for (j = 0; j < NCH(ch); j += 2) {
3809 /* statement terminates with a semi-colon ';' */
3810 if (NCH(CHILD(ch, j)) == 0) {
3811 assert((j + 1) == NCH(ch));
3812 break;
3813 }
3814 s = ast_for_stmt(c, CHILD(ch, j));
3815 if (!s)
3816 return NULL;
3817 asdl_seq_SET(seq, pos++, s);
3818 }
3819 }
3820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 }
3822 assert(pos == seq->size);
3823 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824}
3825
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003826static void
3827get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3828{
Pablo Galindo46a97922019-02-19 22:51:53 +00003829 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003830 // There must be no empty suites.
3831 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003832 stmt_ty last = asdl_seq_GET(s, tot - 1);
3833 *end_lineno = last->end_lineno;
3834 *end_col_offset = last->end_col_offset;
3835}
3836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837static stmt_ty
3838ast_for_if_stmt(struct compiling *c, const node *n)
3839{
3840 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3841 ['else' ':' suite]
3842 */
3843 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003844 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845
3846 REQ(n, if_stmt);
3847
3848 if (NCH(n) == 4) {
3849 expr_ty expression;
3850 asdl_seq *suite_seq;
3851
3852 expression = ast_for_expr(c, CHILD(n, 1));
3853 if (!expression)
3854 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003856 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003858 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859
Guido van Rossumd8faa362007-04-27 19:54:29 +00003860 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003861 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 s = STR(CHILD(n, 4));
3865 /* s[2], the third character in the string, will be
3866 's' for el_s_e, or
3867 'i' for el_i_f
3868 */
3869 if (s[2] == 's') {
3870 expr_ty expression;
3871 asdl_seq *seq1, *seq2;
3872
3873 expression = ast_for_expr(c, CHILD(n, 1));
3874 if (!expression)
3875 return NULL;
3876 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003877 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 return NULL;
3879 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003880 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003882 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883
Guido van Rossumd8faa362007-04-27 19:54:29 +00003884 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003885 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 }
3887 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003888 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003889 expr_ty expression;
3890 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003891 asdl_seq *orelse = NULL;
3892 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 /* must reference the child n_elif+1 since 'else' token is third,
3894 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003895 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3896 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3897 has_else = 1;
3898 n_elif -= 3;
3899 }
3900 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901
Thomas Wouters89f507f2006-12-13 04:49:30 +00003902 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003903 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003905 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003906 if (!orelse)
3907 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003909 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003911 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3912 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003914 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3915 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003917 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 asdl_seq_SET(orelse, 0,
3920 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003921 LINENO(CHILD(n, NCH(n) - 7)),
3922 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003923 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003924 /* the just-created orelse handled the last elif */
3925 n_elif--;
3926 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 for (i = 0; i < n_elif; i++) {
3929 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003930 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003931 if (!newobj)
3932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003934 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003937 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003940 if (orelse != NULL) {
3941 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3942 } else {
3943 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3944 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003945 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003947 LINENO(CHILD(n, off - 1)),
3948 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003949 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003950 orelse = newobj;
3951 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 expression = ast_for_expr(c, CHILD(n, 1));
3953 if (!expression)
3954 return NULL;
3955 suite_seq = ast_for_suite(c, CHILD(n, 3));
3956 if (!suite_seq)
3957 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003958 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003959 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003960 LINENO(n), n->n_col_offset,
3961 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003963
3964 PyErr_Format(PyExc_SystemError,
3965 "unexpected token in 'if' statement: %s", s);
3966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967}
3968
3969static stmt_ty
3970ast_for_while_stmt(struct compiling *c, const node *n)
3971{
3972 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3973 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003974 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975
3976 if (NCH(n) == 4) {
3977 expr_ty expression;
3978 asdl_seq *suite_seq;
3979
3980 expression = ast_for_expr(c, CHILD(n, 1));
3981 if (!expression)
3982 return NULL;
3983 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003984 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003986 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3987 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3988 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 }
3990 else if (NCH(n) == 7) {
3991 expr_ty expression;
3992 asdl_seq *seq1, *seq2;
3993
3994 expression = ast_for_expr(c, CHILD(n, 1));
3995 if (!expression)
3996 return NULL;
3997 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003998 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 return NULL;
4000 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004001 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004003 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004005 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4006 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004008
4009 PyErr_Format(PyExc_SystemError,
4010 "wrong number of tokens for 'while' statement: %d",
4011 NCH(n));
4012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013}
4014
4015static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004016ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017{
guoci90fc8982018-09-11 17:45:45 -04004018 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004019 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004021 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004022 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004023 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004024 int has_type_comment;
4025 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004026
4027 if (is_async && c->c_feature_version < 5) {
4028 ast_error(c, n,
4029 "Async for loops are only supported in Python 3.5 and greater");
4030 return NULL;
4031 }
4032
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004033 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 REQ(n, for_stmt);
4035
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004036 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4037
4038 if (NCH(n) == 9 + has_type_comment) {
4039 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 if (!seq)
4041 return NULL;
4042 }
4043
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004044 node_target = CHILD(n, 1);
4045 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004046 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004048 /* Check the # of children rather than the length of _target, since
4049 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004050 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004051 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004052 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004054 target = Tuple(_target, Store, first->lineno, first->col_offset,
4055 node_target->n_end_lineno, node_target->n_end_col_offset,
4056 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004058 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004059 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004061 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004062 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 return NULL;
4064
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004065 if (seq != NULL) {
4066 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4067 } else {
4068 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4069 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004070
4071 if (has_type_comment) {
4072 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4073 if (!type_comment)
4074 return NULL;
4075 }
4076 else
4077 type_comment = NULL;
4078
Yury Selivanov75445082015-05-11 22:57:16 -04004079 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004080 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004081 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004082 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004083 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004084 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004085 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004086 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087}
4088
4089static excepthandler_ty
4090ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4091{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004092 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004093 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094 REQ(exc, except_clause);
4095 REQ(body, suite);
4096
4097 if (NCH(exc) == 1) {
4098 asdl_seq *suite_seq = ast_for_suite(c, body);
4099 if (!suite_seq)
4100 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004101 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102
Neal Norwitzad74aa82008-03-31 05:14:30 +00004103 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004104 exc->n_col_offset,
4105 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004106 }
4107 else if (NCH(exc) == 2) {
4108 expr_ty expression;
4109 asdl_seq *suite_seq;
4110
4111 expression = ast_for_expr(c, CHILD(exc, 1));
4112 if (!expression)
4113 return NULL;
4114 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004115 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004117 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118
Neal Norwitzad74aa82008-03-31 05:14:30 +00004119 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004120 exc->n_col_offset,
4121 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 }
4123 else if (NCH(exc) == 4) {
4124 asdl_seq *suite_seq;
4125 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004126 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004127 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004129 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004132 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 return NULL;
4134 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004135 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004137 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138
Neal Norwitzad74aa82008-03-31 05:14:30 +00004139 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004140 exc->n_col_offset,
4141 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004143
4144 PyErr_Format(PyExc_SystemError,
4145 "wrong number of children for 'except' clause: %d",
4146 NCH(exc));
4147 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148}
4149
4150static stmt_ty
4151ast_for_try_stmt(struct compiling *c, const node *n)
4152{
Neal Norwitzf599f422005-12-17 21:33:47 +00004153 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004154 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004155 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004156 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158 REQ(n, try_stmt);
4159
Neal Norwitzf599f422005-12-17 21:33:47 +00004160 body = ast_for_suite(c, CHILD(n, 2));
4161 if (body == NULL)
4162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163
Neal Norwitzf599f422005-12-17 21:33:47 +00004164 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4165 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4166 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4167 /* we can assume it's an "else",
4168 because nch >= 9 for try-else-finally and
4169 it would otherwise have a type of except_clause */
4170 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4171 if (orelse == NULL)
4172 return NULL;
4173 n_except--;
4174 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175
Neal Norwitzf599f422005-12-17 21:33:47 +00004176 finally = ast_for_suite(c, CHILD(n, nch - 1));
4177 if (finally == NULL)
4178 return NULL;
4179 n_except--;
4180 }
4181 else {
4182 /* we can assume it's an "else",
4183 otherwise it would have a type of except_clause */
4184 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4185 if (orelse == NULL)
4186 return NULL;
4187 n_except--;
4188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004190 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004191 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 return NULL;
4193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194
Neal Norwitzf599f422005-12-17 21:33:47 +00004195 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004196 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004197 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004198 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004199 if (handlers == NULL)
4200 return NULL;
4201
4202 for (i = 0; i < n_except; i++) {
4203 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4204 CHILD(n, 5 + i * 3));
4205 if (!e)
4206 return NULL;
4207 asdl_seq_SET(handlers, i, e);
4208 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004209 }
4210
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004211 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004212 if (finally != NULL) {
4213 // finally is always last
4214 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4215 } else if (orelse != NULL) {
4216 // otherwise else is last
4217 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4218 } else {
4219 // inline the get_last_end_pos logic due to layout mismatch
4220 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4221 end_lineno = last_handler->end_lineno;
4222 end_col_offset = last_handler->end_col_offset;
4223 }
4224 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4225 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004226}
4227
Georg Brandl0c315622009-05-25 21:10:36 +00004228/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004229static withitem_ty
4230ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004231{
4232 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004233
Georg Brandl0c315622009-05-25 21:10:36 +00004234 REQ(n, with_item);
4235 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004236 if (!context_expr)
4237 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004238 if (NCH(n) == 3) {
4239 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004240
4241 if (!optional_vars) {
4242 return NULL;
4243 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004244 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004245 return NULL;
4246 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004247 }
4248
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004249 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004250}
4251
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004252/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004253static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004254ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004255{
guoci90fc8982018-09-11 17:45:45 -04004256 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004257 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004258 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004259 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004260
Guido van Rossum495da292019-03-07 12:38:08 -08004261 if (is_async && c->c_feature_version < 5) {
4262 ast_error(c, n,
4263 "Async with statements are only supported in Python 3.5 and greater");
4264 return NULL;
4265 }
4266
Georg Brandl0c315622009-05-25 21:10:36 +00004267 REQ(n, with_stmt);
4268
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004269 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4270 nch_minus_type = NCH(n) - has_type_comment;
4271
4272 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004273 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004274 if (!items)
4275 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004276 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004277 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4278 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004279 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004280 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004281 }
4282
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004283 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4284 if (!body)
4285 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004286 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004287
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004288 if (has_type_comment) {
4289 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4290 if (!type_comment)
4291 return NULL;
4292 }
4293 else
4294 type_comment = NULL;
4295
Yury Selivanov75445082015-05-11 22:57:16 -04004296 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004297 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004298 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004299 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004300 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004301 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004302}
4303
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004304static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004305ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004306{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004307 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004308 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004309 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004310 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004311 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004312
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004313 REQ(n, classdef);
4314
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004315 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004316 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004317 if (!s)
4318 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004319 get_last_end_pos(s, &end_lineno, &end_col_offset);
4320
Benjamin Peterson30760062008-11-25 04:02:28 +00004321 classname = NEW_IDENTIFIER(CHILD(n, 1));
4322 if (!classname)
4323 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004324 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004325 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004326 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004327 LINENO(n), n->n_col_offset,
4328 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004329 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004330
4331 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004332 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004333 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004334 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004335 get_last_end_pos(s, &end_lineno, &end_col_offset);
4336
Benjamin Peterson30760062008-11-25 04:02:28 +00004337 classname = NEW_IDENTIFIER(CHILD(n, 1));
4338 if (!classname)
4339 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004340 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004341 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004342 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004343 LINENO(n), n->n_col_offset,
4344 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004345 }
4346
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004347 /* class NAME '(' arglist ')' ':' suite */
4348 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004349 {
4350 PyObject *dummy_name;
4351 expr_ty dummy;
4352 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4353 if (!dummy_name)
4354 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004355 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4356 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4357 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004358 call = ast_for_call(c, CHILD(n, 3), dummy,
4359 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004360 if (!call)
4361 return NULL;
4362 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004363 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004364 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004365 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004366 get_last_end_pos(s, &end_lineno, &end_col_offset);
4367
Benjamin Peterson30760062008-11-25 04:02:28 +00004368 classname = NEW_IDENTIFIER(CHILD(n, 1));
4369 if (!classname)
4370 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004371 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004372 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004373
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004374 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004375 decorator_seq, LINENO(n), n->n_col_offset,
4376 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004377}
4378
4379static stmt_ty
4380ast_for_stmt(struct compiling *c, const node *n)
4381{
4382 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004383 assert(NCH(n) == 1);
4384 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004385 }
4386 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004387 assert(num_stmts(n) == 1);
4388 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004389 }
4390 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004391 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004392 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4393 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004394 */
4395 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004396 case expr_stmt:
4397 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398 case del_stmt:
4399 return ast_for_del_stmt(c, n);
4400 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004401 return Pass(LINENO(n), n->n_col_offset,
4402 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004403 case flow_stmt:
4404 return ast_for_flow_stmt(c, n);
4405 case import_stmt:
4406 return ast_for_import_stmt(c, n);
4407 case global_stmt:
4408 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004409 case nonlocal_stmt:
4410 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411 case assert_stmt:
4412 return ast_for_assert_stmt(c, n);
4413 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004414 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004415 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4416 TYPE(n), NCH(n));
4417 return NULL;
4418 }
4419 }
4420 else {
4421 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004422 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004423 */
4424 node *ch = CHILD(n, 0);
4425 REQ(n, compound_stmt);
4426 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004427 case if_stmt:
4428 return ast_for_if_stmt(c, ch);
4429 case while_stmt:
4430 return ast_for_while_stmt(c, ch);
4431 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004432 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433 case try_stmt:
4434 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004435 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004436 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004437 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004438 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004439 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004440 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 case decorated:
4442 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004443 case async_stmt:
4444 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004446 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004447 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004448 TYPE(n), NCH(n));
4449 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004450 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451 }
4452}
4453
4454static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004455parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004457 const char *end;
4458 long x;
4459 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004460 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004461 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004463 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004464 errno = 0;
4465 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004466 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004467 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004468 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004469 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004470 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004471 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004472 }
4473 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004474 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004475 if (*end == '\0') {
4476 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004477 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004478 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004479 }
4480 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004481 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004482 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004483 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4484 if (compl.imag == -1.0 && PyErr_Occurred())
4485 return NULL;
4486 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004487 }
4488 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004489 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004490 dx = PyOS_string_to_double(s, NULL, NULL);
4491 if (dx == -1.0 && PyErr_Occurred())
4492 return NULL;
4493 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004495}
4496
4497static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004498parsenumber(struct compiling *c, const char *s)
4499{
4500 char *dup, *end;
4501 PyObject *res = NULL;
4502
4503 assert(s != NULL);
4504
4505 if (strchr(s, '_') == NULL) {
4506 return parsenumber_raw(c, s);
4507 }
4508 /* Create a duplicate without underscores. */
4509 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004510 if (dup == NULL) {
4511 return PyErr_NoMemory();
4512 }
Brett Cannona721aba2016-09-09 14:57:09 -07004513 end = dup;
4514 for (; *s; s++) {
4515 if (*s != '_') {
4516 *end++ = *s;
4517 }
4518 }
4519 *end = '\0';
4520 res = parsenumber_raw(c, dup);
4521 PyMem_Free(dup);
4522 return res;
4523}
4524
4525static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004526decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004527{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004528 const char *s, *t;
4529 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004530 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4531 while (s < end && (*s & 0x80)) s++;
4532 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004533 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004534}
4535
Eric V. Smith56466482016-10-31 14:46:26 -04004536static int
4537warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004538 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004539{
4540 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4541 first_invalid_escape_char);
4542 if (msg == NULL) {
4543 return -1;
4544 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004545 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004546 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004547 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004548 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004549 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4550 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004551 to get a more accurate error report */
4552 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004553 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004554 }
4555 Py_DECREF(msg);
4556 return -1;
4557 }
4558 Py_DECREF(msg);
4559 return 0;
4560}
4561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004562static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004563decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4564 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004565{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004566 PyObject *v, *u;
4567 char *buf;
4568 char *p;
4569 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004570
Benjamin Peterson202803a2016-02-25 22:34:45 -08004571 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004572 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004573 return NULL;
4574 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4575 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4576 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4577 if (u == NULL)
4578 return NULL;
4579 p = buf = PyBytes_AsString(u);
4580 end = s + len;
4581 while (s < end) {
4582 if (*s == '\\') {
4583 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004584 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004585 strcpy(p, "u005c");
4586 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004587 if (s >= end)
4588 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004589 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004590 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004591 if (*s & 0x80) { /* XXX inefficient */
4592 PyObject *w;
4593 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03004594 const void *data;
Benjamin Peterson202803a2016-02-25 22:34:45 -08004595 Py_ssize_t len, i;
4596 w = decode_utf8(c, &s, end);
4597 if (w == NULL) {
4598 Py_DECREF(u);
4599 return NULL;
4600 }
4601 kind = PyUnicode_KIND(w);
4602 data = PyUnicode_DATA(w);
4603 len = PyUnicode_GET_LENGTH(w);
4604 for (i = 0; i < len; i++) {
4605 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4606 sprintf(p, "\\U%08x", chr);
4607 p += 10;
4608 }
4609 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004610 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004611 Py_DECREF(w);
4612 } else {
4613 *p++ = *s++;
4614 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004615 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004616 len = p - buf;
4617 s = buf;
4618
Eric V. Smith56466482016-10-31 14:46:26 -04004619 const char *first_invalid_escape;
4620 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4621
4622 if (v != NULL && first_invalid_escape != NULL) {
4623 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4624 /* We have not decref u before because first_invalid_escape points
4625 inside u. */
4626 Py_XDECREF(u);
4627 Py_DECREF(v);
4628 return NULL;
4629 }
4630 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004631 Py_XDECREF(u);
4632 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004633}
4634
Eric V. Smith56466482016-10-31 14:46:26 -04004635static PyObject *
4636decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4637 size_t len)
4638{
4639 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004640 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004641 &first_invalid_escape);
4642 if (result == NULL)
4643 return NULL;
4644
4645 if (first_invalid_escape != NULL) {
4646 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4647 Py_DECREF(result);
4648 return NULL;
4649 }
4650 }
4651 return result;
4652}
4653
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004654/* Shift locations for the given node and all its children by adding `lineno`
4655 and `col_offset` to existing locations. */
4656static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4657{
4658 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004659 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004660 for (int i = 0; i < NCH(n); ++i) {
4661 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4662 /* Shifting column offsets unnecessary if there's been newlines. */
4663 col_offset = 0;
4664 }
4665 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4666 }
4667 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004668 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004669}
4670
4671/* Fix locations for the given node and its children.
4672
4673 `parent` is the enclosing node.
4674 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004675 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004676*/
4677static void
4678fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4679{
4680 char *substr = NULL;
4681 char *start;
4682 int lines = LINENO(parent) - 1;
4683 int cols = parent->n_col_offset;
4684 /* Find the full fstring to fix location information in `n`. */
4685 while (parent && parent->n_type != STRING)
4686 parent = parent->n_child;
4687 if (parent && parent->n_str) {
4688 substr = strstr(parent->n_str, expr_str);
4689 if (substr) {
4690 start = substr;
4691 while (start > parent->n_str) {
4692 if (start[0] == '\n')
4693 break;
4694 start--;
4695 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004696 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004697 /* adjust the start based on the number of newlines encountered
4698 before the f-string expression */
4699 for (char* p = parent->n_str; p < substr; p++) {
4700 if (*p == '\n') {
4701 lines++;
4702 }
4703 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004704 }
4705 }
4706 fstring_shift_node_locations(n, lines, cols);
4707}
4708
Eric V. Smith451d0e32016-09-09 21:56:20 -04004709/* Compile this expression in to an expr_ty. Add parens around the
4710 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004711static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004712fstring_compile_expr(const char *expr_start, const char *expr_end,
4713 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004714
Eric V. Smith235a6f02015-09-19 14:51:32 -04004715{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004716 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004717 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004718 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004719 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004720 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004721
Eric V. Smith1d44c412015-09-23 07:49:00 -04004722 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004723 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004724 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4725 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004726
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004727 /* If the substring is all whitespace, it's an error. We need to catch this
4728 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4729 because turning the expression '' in to '()' would go from being invalid
4730 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004731 for (s = expr_start; s != expr_end; s++) {
4732 char c = *s;
4733 /* The Python parser ignores only the following whitespace
4734 characters (\r already is converted to \n). */
4735 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004736 break;
4737 }
4738 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004739 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004740 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742 }
4743
Eric V. Smith451d0e32016-09-09 21:56:20 -04004744 len = expr_end - expr_start;
4745 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4746 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004747 if (str == NULL) {
4748 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004749 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004750 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004751
Eric V. Smith451d0e32016-09-09 21:56:20 -04004752 str[0] = '(';
4753 memcpy(str+1, expr_start, len);
4754 str[len+1] = ')';
4755 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004756
Victor Stinner37d66d72019-06-13 02:16:41 +02004757 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004758 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004759 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4760 Py_eval_input, 0);
4761 if (!mod_n) {
4762 PyMem_RawFree(str);
4763 return NULL;
4764 }
4765 /* Reuse str to find the correct column offset. */
4766 str[0] = '{';
4767 str[len+1] = '}';
4768 fstring_fix_node_location(n, mod_n, str);
4769 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004770 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004771 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004772 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004773 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004774 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004775}
4776
4777/* Return -1 on error.
4778
4779 Return 0 if we reached the end of the literal.
4780
4781 Return 1 if we haven't reached the end of the literal, but we want
4782 the caller to process the literal up to this point. Used for
4783 doubled braces.
4784*/
4785static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004786fstring_find_literal(const char **str, const char *end, int raw,
4787 PyObject **literal, int recurse_lvl,
4788 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004789{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004790 /* Get any literal string. It ends when we hit an un-doubled left
4791 brace (which isn't part of a unicode name escape such as
4792 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004793
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004794 const char *s = *str;
4795 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004796 int result = 0;
4797
Eric V. Smith235a6f02015-09-19 14:51:32 -04004798 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004799 while (s < end) {
4800 char ch = *s++;
4801 if (!raw && ch == '\\' && s < end) {
4802 ch = *s++;
4803 if (ch == 'N') {
4804 if (s < end && *s++ == '{') {
4805 while (s < end && *s++ != '}') {
4806 }
4807 continue;
4808 }
4809 break;
4810 }
4811 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4812 return -1;
4813 }
4814 }
4815 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004816 /* Check for doubled braces, but only at the top level. If
4817 we checked at every level, then f'{0:{3}}' would fail
4818 with the two closing braces. */
4819 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004820 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004821 /* We're going to tell the caller that the literal ends
4822 here, but that they should continue scanning. But also
4823 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004824 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004825 result = 1;
4826 goto done;
4827 }
4828
4829 /* Where a single '{' is the start of a new expression, a
4830 single '}' is not allowed. */
4831 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004832 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004833 ast_error(c, n, "f-string: single '}' is not allowed");
4834 return -1;
4835 }
4836 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004837 /* We're either at a '{', which means we're starting another
4838 expression; or a '}', which means we're at the end of this
4839 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004840 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004841 break;
4842 }
4843 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004844 *str = s;
4845 assert(s <= end);
4846 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004847done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004848 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004849 if (raw)
4850 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004851 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004852 NULL, NULL);
4853 else
Eric V. Smith56466482016-10-31 14:46:26 -04004854 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004855 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856 if (!*literal)
4857 return -1;
4858 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004859 return result;
4860}
4861
4862/* Forward declaration because parsing is recursive. */
4863static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004864fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865 struct compiling *c, const node *n);
4866
Eric V. Smith451d0e32016-09-09 21:56:20 -04004867/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004868 expression (so it must be a '{'). Returns the FormattedValue node, which
4869 includes the expression, conversion character, format_spec expression, and
4870 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871
4872 Note that I don't do a perfect job here: I don't make sure that a
4873 closing brace doesn't match an opening paren, for example. It
4874 doesn't need to error on all invalid expressions, just correctly
4875 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004876 will be caught when we parse it later.
4877
4878 *expression is set to the expression. For an '=' "debug" expression,
4879 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004880 including the '=' and any whitespace around it, as a string object). If
4881 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004882static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004883fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004884 PyObject **expr_text, expr_ty *expression,
4885 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004886{
4887 /* Return -1 on error, else 0. */
4888
Eric V. Smith451d0e32016-09-09 21:56:20 -04004889 const char *expr_start;
4890 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004891 expr_ty simple_expression;
4892 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004893 int conversion = -1; /* The conversion char. Use default if not
4894 specified, or !r if using = and no format
4895 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004896
4897 /* 0 if we're not in a string, else the quote char we're trying to
4898 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004899 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900
4901 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4902 int string_type = 0;
4903
4904 /* Keep track of nesting level for braces/parens/brackets in
4905 expressions. */
4906 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004907 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004909 *expr_text = NULL;
4910
Eric V. Smith235a6f02015-09-19 14:51:32 -04004911 /* Can only nest one level deep. */
4912 if (recurse_lvl >= 2) {
4913 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004914 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004915 }
4916
4917 /* The first char must be a left brace, or we wouldn't have gotten
4918 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004919 assert(**str == '{');
4920 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922 expr_start = *str;
4923 for (; *str < end; (*str)++) {
4924 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004925
4926 /* Loop invariants. */
4927 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004928 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 if (quote_char)
4930 assert(string_type == 1 || string_type == 3);
4931 else
4932 assert(string_type == 0);
4933
Eric V. Smith451d0e32016-09-09 21:56:20 -04004934 ch = **str;
4935 /* Nowhere inside an expression is a backslash allowed. */
4936 if (ch == '\\') {
4937 /* Error: can't include a backslash character, inside
4938 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004939 ast_error(c, n,
4940 "f-string expression part "
4941 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004942 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004943 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 if (quote_char) {
4945 /* We're inside a string. See if we're at the end. */
4946 /* This code needs to implement the same non-error logic
4947 as tok_get from tokenizer.c, at the letter_quote
4948 label. To actually share that code would be a
4949 nightmare. But, it's unlikely to change and is small,
4950 so duplicate it here. Note we don't need to catch all
4951 of the errors, since they'll be caught when parsing the
4952 expression. We just need to match the non-error
4953 cases. Thus we can ignore \n in single-quoted strings,
4954 for example. Or non-terminated strings. */
4955 if (ch == quote_char) {
4956 /* Does this match the string_type (single or triple
4957 quoted)? */
4958 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004959 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004960 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004961 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004962 string_type = 0;
4963 quote_char = 0;
4964 continue;
4965 }
4966 } else {
4967 /* We're at the end of a normal string. */
4968 quote_char = 0;
4969 string_type = 0;
4970 continue;
4971 }
4972 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004973 } else if (ch == '\'' || ch == '"') {
4974 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004975 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004976 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004977 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004978 } else {
4979 /* Start of a normal string. */
4980 string_type = 1;
4981 }
4982 /* Start looking for the end of the string. */
4983 quote_char = ch;
4984 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004985 if (nested_depth >= MAXLEVEL) {
4986 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004987 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004988 }
4989 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991 } else if (ch == '#') {
4992 /* Error: can't include a comment character, inside parens
4993 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004994 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004995 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004997 (ch == '!' || ch == ':' || ch == '}' ||
4998 ch == '=' || ch == '>' || ch == '<')) {
4999 /* See if there's a next character. */
5000 if (*str+1 < end) {
5001 char next = *(*str+1);
5002
5003 /* For "!=". since '=' is not an allowed conversion character,
5004 nothing is lost in this test. */
5005 if ((ch == '!' && next == '=') || /* != */
5006 (ch == '=' && next == '=') || /* == */
5007 (ch == '<' && next == '=') || /* <= */
5008 (ch == '>' && next == '=') /* >= */
5009 ) {
5010 *str += 1;
5011 continue;
5012 }
5013 /* Don't get out of the loop for these, if they're single
5014 chars (not part of 2-char tokens). If by themselves, they
5015 don't end an expression (unlike say '!'). */
5016 if (ch == '>' || ch == '<') {
5017 continue;
5018 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005019 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005020
Eric V. Smith235a6f02015-09-19 14:51:32 -04005021 /* Normal way out of this loop. */
5022 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005023 } else if (ch == ']' || ch == '}' || ch == ')') {
5024 if (!nested_depth) {
5025 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005026 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005027 }
5028 nested_depth--;
5029 int opening = parenstack[nested_depth];
5030 if (!((opening == '(' && ch == ')') ||
5031 (opening == '[' && ch == ']') ||
5032 (opening == '{' && ch == '}')))
5033 {
5034 ast_error(c, n,
5035 "f-string: closing parenthesis '%c' "
5036 "does not match opening parenthesis '%c'",
5037 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005038 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005039 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005040 } else {
5041 /* Just consume this char and loop around. */
5042 }
5043 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005044 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005045 /* If we leave this loop in a string or with mismatched parens, we
5046 don't care. We'll get a syntax error when compiling the
5047 expression. But, we can produce a better error message, so
5048 let's just do that.*/
5049 if (quote_char) {
5050 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005051 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052 }
5053 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005054 int opening = parenstack[nested_depth - 1];
5055 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005056 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057 }
5058
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005060 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005061
5062 /* Compile the expression as soon as possible, so we show errors
5063 related to the expression before errors related to the
5064 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005065 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005066 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005067 goto error;
5068
5069 /* Check for =, which puts the text value of the expression in
5070 expr_text. */
5071 if (**str == '=') {
5072 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005073
5074 /* Skip over ASCII whitespace. No need to test for end of string
5075 here, since we know there's at least a trailing quote somewhere
5076 ahead. */
5077 while (Py_ISSPACE(**str)) {
5078 *str += 1;
5079 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005080
5081 /* Set *expr_text to the text of the expression. */
5082 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5083 if (!*expr_text) {
5084 goto error;
5085 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005086 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005087
5088 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 if (**str == '!') {
5090 *str += 1;
5091 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005092 goto unexpected_end_of_string;
5093
Eric V. Smith451d0e32016-09-09 21:56:20 -04005094 conversion = **str;
5095 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005096
5097 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005098 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005099 ast_error(c, n,
5100 "f-string: invalid conversion character: "
5101 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005102 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005103 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005104
5105 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106
5107 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110 if (**str == ':') {
5111 *str += 1;
5112 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005113 goto unexpected_end_of_string;
5114
5115 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005118 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 }
5120
Eric V. Smith451d0e32016-09-09 21:56:20 -04005121 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005122 goto unexpected_end_of_string;
5123
5124 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 assert(*str < end);
5126 assert(**str == '}');
5127 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005128
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005129 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005130 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005131 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005132 conversion = 'r';
5133 }
5134
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 /* And now create the FormattedValue node that represents this
5136 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005137 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005138 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005139 n->n_col_offset, n->n_end_lineno,
5140 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005141 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005142 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005143
5144 return 0;
5145
5146unexpected_end_of_string:
5147 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005148 /* Falls through to error. */
5149
5150error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005151 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005152 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005153
Eric V. Smith235a6f02015-09-19 14:51:32 -04005154}
5155
5156/* Return -1 on error.
5157
5158 Return 0 if we have a literal (possible zero length) and an
5159 expression (zero length if at the end of the string.
5160
5161 Return 1 if we have a literal, but no expression, and we want the
5162 caller to call us again. This is used to deal with doubled
5163 braces.
5164
5165 When called multiple times on the string 'a{{b{0}c', this function
5166 will return:
5167
5168 1. the literal 'a{' with no expression, and a return value
5169 of 1. Despite the fact that there's no expression, the return
5170 value of 1 means we're not finished yet.
5171
5172 2. the literal 'b' and the expression '0', with a return value of
5173 0. The fact that there's an expression means we're not finished.
5174
5175 3. literal 'c' with no expression and a return value of 0. The
5176 combination of the return value of 0 with no expression means
5177 we're finished.
5178*/
5179static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005180fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5181 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005182 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005183 struct compiling *c, const node *n)
5184{
5185 int result;
5186
5187 assert(*literal == NULL && *expression == NULL);
5188
5189 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005190 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005191 if (result < 0)
5192 goto error;
5193
5194 assert(result == 0 || result == 1);
5195
5196 if (result == 1)
5197 /* We have a literal, but don't look at the expression. */
5198 return 1;
5199
Eric V. Smith451d0e32016-09-09 21:56:20 -04005200 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005201 /* We're at the end of the string or the end of a nested
5202 f-string: no expression. The top-level error case where we
5203 expect to be at the end of the string but we're at a '}' is
5204 handled later. */
5205 return 0;
5206
5207 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005208 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005210 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5211 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 goto error;
5213
5214 return 0;
5215
5216error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005217 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005218 return -1;
5219}
5220
5221#define EXPRLIST_N_CACHED 64
5222
5223typedef struct {
5224 /* Incrementally build an array of expr_ty, so be used in an
5225 asdl_seq. Cache some small but reasonably sized number of
5226 expr_ty's, and then after that start dynamically allocating,
5227 doubling the number allocated each time. Note that the f-string
5228 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005229 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005230 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005231
5232 Py_ssize_t allocated; /* Number we've allocated. */
5233 Py_ssize_t size; /* Number we've used. */
5234 expr_ty *p; /* Pointer to the memory we're actually
5235 using. Will point to 'data' until we
5236 start dynamically allocating. */
5237 expr_ty data[EXPRLIST_N_CACHED];
5238} ExprList;
5239
5240#ifdef NDEBUG
5241#define ExprList_check_invariants(l)
5242#else
5243static void
5244ExprList_check_invariants(ExprList *l)
5245{
5246 /* Check our invariants. Make sure this object is "live", and
5247 hasn't been deallocated. */
5248 assert(l->size >= 0);
5249 assert(l->p != NULL);
5250 if (l->size <= EXPRLIST_N_CACHED)
5251 assert(l->data == l->p);
5252}
5253#endif
5254
5255static void
5256ExprList_Init(ExprList *l)
5257{
5258 l->allocated = EXPRLIST_N_CACHED;
5259 l->size = 0;
5260
5261 /* Until we start allocating dynamically, p points to data. */
5262 l->p = l->data;
5263
5264 ExprList_check_invariants(l);
5265}
5266
5267static int
5268ExprList_Append(ExprList *l, expr_ty exp)
5269{
5270 ExprList_check_invariants(l);
5271 if (l->size >= l->allocated) {
5272 /* We need to alloc (or realloc) the memory. */
5273 Py_ssize_t new_size = l->allocated * 2;
5274
5275 /* See if we've ever allocated anything dynamically. */
5276 if (l->p == l->data) {
5277 Py_ssize_t i;
5278 /* We're still using the cached data. Switch to
5279 alloc-ing. */
5280 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5281 if (!l->p)
5282 return -1;
5283 /* Copy the cached data into the new buffer. */
5284 for (i = 0; i < l->size; i++)
5285 l->p[i] = l->data[i];
5286 } else {
5287 /* Just realloc. */
5288 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5289 if (!tmp) {
5290 PyMem_RawFree(l->p);
5291 l->p = NULL;
5292 return -1;
5293 }
5294 l->p = tmp;
5295 }
5296
5297 l->allocated = new_size;
5298 assert(l->allocated == 2 * l->size);
5299 }
5300
5301 l->p[l->size++] = exp;
5302
5303 ExprList_check_invariants(l);
5304 return 0;
5305}
5306
5307static void
5308ExprList_Dealloc(ExprList *l)
5309{
5310 ExprList_check_invariants(l);
5311
5312 /* If there's been an error, or we've never dynamically allocated,
5313 do nothing. */
5314 if (!l->p || l->p == l->data) {
5315 /* Do nothing. */
5316 } else {
5317 /* We have dynamically allocated. Free the memory. */
5318 PyMem_RawFree(l->p);
5319 }
5320 l->p = NULL;
5321 l->size = -1;
5322}
5323
5324static asdl_seq *
5325ExprList_Finish(ExprList *l, PyArena *arena)
5326{
5327 asdl_seq *seq;
5328
5329 ExprList_check_invariants(l);
5330
5331 /* Allocate the asdl_seq and copy the expressions in to it. */
5332 seq = _Py_asdl_seq_new(l->size, arena);
5333 if (seq) {
5334 Py_ssize_t i;
5335 for (i = 0; i < l->size; i++)
5336 asdl_seq_SET(seq, i, l->p[i]);
5337 }
5338 ExprList_Dealloc(l);
5339 return seq;
5340}
5341
5342/* The FstringParser is designed to add a mix of strings and
5343 f-strings, and concat them together as needed. Ultimately, it
5344 generates an expr_ty. */
5345typedef struct {
5346 PyObject *last_str;
5347 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005348 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005349} FstringParser;
5350
5351#ifdef NDEBUG
5352#define FstringParser_check_invariants(state)
5353#else
5354static void
5355FstringParser_check_invariants(FstringParser *state)
5356{
5357 if (state->last_str)
5358 assert(PyUnicode_CheckExact(state->last_str));
5359 ExprList_check_invariants(&state->expr_list);
5360}
5361#endif
5362
5363static void
5364FstringParser_Init(FstringParser *state)
5365{
5366 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005367 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005368 ExprList_Init(&state->expr_list);
5369 FstringParser_check_invariants(state);
5370}
5371
5372static void
5373FstringParser_Dealloc(FstringParser *state)
5374{
5375 FstringParser_check_invariants(state);
5376
5377 Py_XDECREF(state->last_str);
5378 ExprList_Dealloc(&state->expr_list);
5379}
5380
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005381/* Constants for the following */
5382static PyObject *u_kind;
5383
5384/* Compute 'kind' field for string Constant (either 'u' or None) */
5385static PyObject *
5386make_kind(struct compiling *c, const node *n)
5387{
5388 char *s = NULL;
5389 PyObject *kind = NULL;
5390
5391 /* Find the first string literal, if any */
5392 while (TYPE(n) != STRING) {
5393 if (NCH(n) == 0)
5394 return NULL;
5395 n = CHILD(n, 0);
5396 }
5397 REQ(n, STRING);
5398
5399 /* If it starts with 'u', return a PyUnicode "u" string */
5400 s = STR(n);
5401 if (s && *s == 'u') {
5402 if (!u_kind) {
5403 u_kind = PyUnicode_InternFromString("u");
5404 if (!u_kind)
5405 return NULL;
5406 }
5407 kind = u_kind;
5408 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5409 return NULL;
5410 }
5411 Py_INCREF(kind);
5412 }
5413 return kind;
5414}
5415
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005416/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005417static expr_ty
5418make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5419{
5420 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005421 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005422 *str = NULL;
5423 assert(PyUnicode_CheckExact(s));
5424 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5425 Py_DECREF(s);
5426 return NULL;
5427 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005428 kind = make_kind(c, n);
5429 if (kind == NULL && PyErr_Occurred())
5430 return NULL;
5431 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005432 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005433}
5434
5435/* Add a non-f-string (that is, a regular literal string). str is
5436 decref'd. */
5437static int
5438FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5439{
5440 FstringParser_check_invariants(state);
5441
5442 assert(PyUnicode_CheckExact(str));
5443
5444 if (PyUnicode_GET_LENGTH(str) == 0) {
5445 Py_DECREF(str);
5446 return 0;
5447 }
5448
5449 if (!state->last_str) {
5450 /* We didn't have a string before, so just remember this one. */
5451 state->last_str = str;
5452 } else {
5453 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005454 PyUnicode_AppendAndDel(&state->last_str, str);
5455 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005456 return -1;
5457 }
5458 FstringParser_check_invariants(state);
5459 return 0;
5460}
5461
Eric V. Smith451d0e32016-09-09 21:56:20 -04005462/* Parse an f-string. The f-string is in *str to end, with no
5463 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005464static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005465FstringParser_ConcatFstring(FstringParser *state, const char **str,
5466 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005467 struct compiling *c, const node *n)
5468{
5469 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005470 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005471
5472 /* Parse the f-string. */
5473 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005474 PyObject *literal = NULL;
5475 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005476 expr_ty expression = NULL;
5477
5478 /* If there's a zero length literal in front of the
5479 expression, literal will be NULL. If we're at the end of
5480 the f-string, expression will be NULL (unless result == 1,
5481 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005482 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005483 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005484 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005485 if (result < 0)
5486 return -1;
5487
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005488 /* Add the literal, if any. */
5489 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5490 Py_XDECREF(expr_text);
5491 return -1;
5492 }
5493 /* Add the expr_text, if any. */
5494 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5495 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005496 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005497
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005498 /* We've dealt with the literal and expr_text, their ownership has
5499 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005500
5501 /* See if we should just loop around to get the next literal
5502 and expression, while ignoring the expression this
5503 time. This is used for un-doubling braces, as an
5504 optimization. */
5505 if (result == 1)
5506 continue;
5507
5508 if (!expression)
5509 /* We're done with this f-string. */
5510 break;
5511
5512 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005513 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005514 if (!state->last_str) {
5515 /* Do nothing. No previous literal. */
5516 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005517 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005518 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5519 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5520 return -1;
5521 }
5522
5523 if (ExprList_Append(&state->expr_list, expression) < 0)
5524 return -1;
5525 }
5526
Eric V. Smith235a6f02015-09-19 14:51:32 -04005527 /* If recurse_lvl is zero, then we must be at the end of the
5528 string. Otherwise, we must be at a right brace. */
5529
Eric V. Smith451d0e32016-09-09 21:56:20 -04005530 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531 ast_error(c, n, "f-string: unexpected end of string");
5532 return -1;
5533 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005534 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005535 ast_error(c, n, "f-string: expecting '}'");
5536 return -1;
5537 }
5538
5539 FstringParser_check_invariants(state);
5540 return 0;
5541}
5542
5543/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005544 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005545static expr_ty
5546FstringParser_Finish(FstringParser *state, struct compiling *c,
5547 const node *n)
5548{
5549 asdl_seq *seq;
5550
5551 FstringParser_check_invariants(state);
5552
5553 /* If we're just a constant string with no expressions, return
5554 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005555 if (!state->fmode) {
5556 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005557 if (!state->last_str) {
5558 /* Create a zero length string. */
5559 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5560 if (!state->last_str)
5561 goto error;
5562 }
5563 return make_str_node_and_del(&state->last_str, c, n);
5564 }
5565
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005566 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005567 last node in our expression list. */
5568 if (state->last_str) {
5569 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5570 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5571 goto error;
5572 }
5573 /* This has already been freed. */
5574 assert(state->last_str == NULL);
5575
5576 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5577 if (!seq)
5578 goto error;
5579
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005580 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5581 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005582
5583error:
5584 FstringParser_Dealloc(state);
5585 return NULL;
5586}
5587
Eric V. Smith451d0e32016-09-09 21:56:20 -04005588/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5589 at end, parse it into an expr_ty. Return NULL on error. Adjust
5590 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005591static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005592fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005593 struct compiling *c, const node *n)
5594{
5595 FstringParser state;
5596
5597 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005598 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599 c, n) < 0) {
5600 FstringParser_Dealloc(&state);
5601 return NULL;
5602 }
5603
5604 return FstringParser_Finish(&state, c, n);
5605}
5606
5607/* n is a Python string literal, including the bracketing quote
5608 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005609 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005610 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005611 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5612 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005613*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005614static int
5615parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5616 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005617{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005618 size_t len;
5619 const char *s = STR(n);
5620 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005621 int fmode = 0;
5622 *bytesmode = 0;
5623 *rawmode = 0;
5624 *result = NULL;
5625 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005626 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005628 if (quote == 'b' || quote == 'B') {
5629 quote = *++s;
5630 *bytesmode = 1;
5631 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005632 else if (quote == 'u' || quote == 'U') {
5633 quote = *++s;
5634 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005635 else if (quote == 'r' || quote == 'R') {
5636 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005637 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005638 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005639 else if (quote == 'f' || quote == 'F') {
5640 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005641 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005642 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005643 else {
5644 break;
5645 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005646 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005647 }
Guido van Rossum495da292019-03-07 12:38:08 -08005648
5649 /* fstrings are only allowed in Python 3.6 and greater */
5650 if (fmode && c->c_feature_version < 6) {
5651 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5652 return -1;
5653 }
5654
Eric V. Smith451d0e32016-09-09 21:56:20 -04005655 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005656 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005657 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005658 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005659 if (quote != '\'' && quote != '\"') {
5660 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005661 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005662 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005663 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005664 s++;
5665 len = strlen(s);
5666 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005667 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005668 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005669 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005670 }
5671 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005672 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005673 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005674 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005675 }
5676 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005677 /* A triple quoted string. We've already skipped one quote at
5678 the start and one at the end of the string. Now skip the
5679 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005680 s += 2;
5681 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005682 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005683 if (s[--len] != quote || s[--len] != quote) {
5684 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005685 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005686 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005687 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005688
Eric V. Smith451d0e32016-09-09 21:56:20 -04005689 if (fmode) {
5690 /* Just return the bytes. The caller will parse the resulting
5691 string. */
5692 *fstr = s;
5693 *fstrlen = len;
5694 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005695 }
5696
Eric V. Smith451d0e32016-09-09 21:56:20 -04005697 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005698 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005699 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005700 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005701 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005702 const char *ch;
5703 for (ch = s; *ch; ch++) {
5704 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005705 ast_error(c, n,
5706 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005707 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005708 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005709 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005710 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005711 if (*rawmode)
5712 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005713 else
Eric V. Smith56466482016-10-31 14:46:26 -04005714 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005715 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005716 if (*rawmode)
5717 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005718 else
Eric V. Smith56466482016-10-31 14:46:26 -04005719 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005720 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005721 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005722}
5723
Eric V. Smith235a6f02015-09-19 14:51:32 -04005724/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5725 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005726 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005727 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005728 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005729 node if there's just an f-string (with no leading or trailing
5730 literals), or a JoinedStr node if there are multiple f-strings or
5731 any literals involved. */
5732static expr_ty
5733parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005734{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005735 int bytesmode = 0;
5736 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005737 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005738
5739 FstringParser state;
5740 FstringParser_Init(&state);
5741
5742 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005743 int this_bytesmode;
5744 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005745 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005746 const char *fstr;
5747 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005748
5749 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005750 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5751 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005752 goto error;
5753
5754 /* Check that we're not mixing bytes with unicode. */
5755 if (i != 0 && bytesmode != this_bytesmode) {
5756 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005757 /* s is NULL if the current string part is an f-string. */
5758 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005759 goto error;
5760 }
5761 bytesmode = this_bytesmode;
5762
Eric V. Smith451d0e32016-09-09 21:56:20 -04005763 if (fstr != NULL) {
5764 int result;
5765 assert(s == NULL && !bytesmode);
5766 /* This is an f-string. Parse and concatenate it. */
5767 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5768 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005769 if (result < 0)
5770 goto error;
5771 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005772 /* A string or byte string. */
5773 assert(s != NULL && fstr == NULL);
5774
Eric V. Smith451d0e32016-09-09 21:56:20 -04005775 assert(bytesmode ? PyBytes_CheckExact(s) :
5776 PyUnicode_CheckExact(s));
5777
Eric V. Smith451d0e32016-09-09 21:56:20 -04005778 if (bytesmode) {
5779 /* For bytes, concat as we go. */
5780 if (i == 0) {
5781 /* First time, just remember this value. */
5782 bytes_str = s;
5783 } else {
5784 PyBytes_ConcatAndDel(&bytes_str, s);
5785 if (!bytes_str)
5786 goto error;
5787 }
5788 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005789 /* This is a regular string. Concatenate it. */
5790 if (FstringParser_ConcatAndDel(&state, s) < 0)
5791 goto error;
5792 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005793 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005794 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005795 if (bytesmode) {
5796 /* Just return the bytes object and we're done. */
5797 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5798 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005799 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005800 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005802
Eric V. Smith235a6f02015-09-19 14:51:32 -04005803 /* We're not a bytes string, bytes_str should never have been set. */
5804 assert(bytes_str == NULL);
5805
5806 return FstringParser_Finish(&state, c, n);
5807
5808error:
5809 Py_XDECREF(bytes_str);
5810 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005812}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005813
5814PyObject *
5815_PyAST_GetDocString(asdl_seq *body)
5816{
5817 if (!asdl_seq_LEN(body)) {
5818 return NULL;
5819 }
5820 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5821 if (st->kind != Expr_kind) {
5822 return NULL;
5823 }
5824 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005825 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5826 return e->v.Constant.value;
5827 }
5828 return NULL;
5829}