blob: fb23c026e8c0024ed9a952bcf14a6cb79a1fb373 [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;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003016 kw = keyword(NULL, e, c->c_arena);
3017 asdl_seq_SET(keywords, nkeywords++, kw);
3018 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003020 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003021 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003022 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003024 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003025 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003027 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3028 /* treat colon equal as positional argument */
3029 if (nkeywords) {
3030 if (ndoublestars) {
3031 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003032 "positional argument follows "
3033 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003034 }
3035 else {
3036 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003037 "positional argument follows "
3038 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003039 }
3040 return NULL;
3041 }
3042 e = ast_for_namedexpr(c, ch);
3043 if (!e)
3044 return NULL;
3045 asdl_seq_SET(args, nargs++, e);
3046 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003047 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003048 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003050 identifier key, tmp;
3051 int k;
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 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003095 for (k = 0; k < nkeywords; k++) {
3096 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003097 if (tmp && !PyUnicode_Compare(tmp, key)) {
3098 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003099 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003100 return NULL;
3101 }
3102 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003103 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003105 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003106 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003108 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003109 asdl_seq_SET(keywords, nkeywords++, kw);
3110 }
3111 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 }
3113
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003114 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003115 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116}
3117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003119ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003121 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003122 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003124 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003125 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003126 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003127 }
3128 else {
3129 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003130 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003131 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003133 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 else {
3135 asdl_seq *tmp = seq_for_testlist(c, n);
3136 if (!tmp)
3137 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003138 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3139 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003141}
3142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143static stmt_ty
3144ast_for_expr_stmt(struct compiling *c, const node *n)
3145{
3146 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003147 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003148 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3149 annassign: ':' test ['=' (yield_expr|testlist)]
3150 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3151 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3152 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003153 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003155 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003157 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003158 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 if (!e)
3160 return NULL;
3161
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003162 return Expr(e, LINENO(n), n->n_col_offset,
3163 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 }
3165 else if (TYPE(CHILD(n, 1)) == augassign) {
3166 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003168 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169
Thomas Wouters89f507f2006-12-13 04:49:30 +00003170 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 if (!expr1)
3172 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003173 if(!set_context(c, expr1, Store, ch))
3174 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003175 /* set_context checks that most expressions are not the left side.
3176 Augmented assignments can only have a name, a subscript, or an
3177 attribute on the left, though, so we have to explicitly check for
3178 those. */
3179 switch (expr1->kind) {
3180 case Name_kind:
3181 case Attribute_kind:
3182 case Subscript_kind:
3183 break;
3184 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003185 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003186 return NULL;
3187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188
Thomas Wouters89f507f2006-12-13 04:49:30 +00003189 ch = CHILD(n, 2);
3190 if (TYPE(ch) == testlist)
3191 expr2 = ast_for_testlist(c, ch);
3192 else
3193 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003194 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 return NULL;
3196
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003197 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003198 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 return NULL;
3200
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003201 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3202 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003204 else if (TYPE(CHILD(n, 1)) == annassign) {
3205 expr_ty expr1, expr2, expr3;
3206 node *ch = CHILD(n, 0);
3207 node *deep, *ann = CHILD(n, 1);
3208 int simple = 1;
3209
Guido van Rossum495da292019-03-07 12:38:08 -08003210 /* AnnAssigns are only allowed in Python 3.6 or greater */
3211 if (c->c_feature_version < 6) {
3212 ast_error(c, ch,
3213 "Variable annotation syntax is only supported in Python 3.6 and greater");
3214 return NULL;
3215 }
3216
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003217 /* we keep track of parens to qualify (x) as expression not name */
3218 deep = ch;
3219 while (NCH(deep) == 1) {
3220 deep = CHILD(deep, 0);
3221 }
3222 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3223 simple = 0;
3224 }
3225 expr1 = ast_for_testlist(c, ch);
3226 if (!expr1) {
3227 return NULL;
3228 }
3229 switch (expr1->kind) {
3230 case Name_kind:
3231 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3232 return NULL;
3233 }
3234 expr1->v.Name.ctx = Store;
3235 break;
3236 case Attribute_kind:
3237 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3238 return NULL;
3239 }
3240 expr1->v.Attribute.ctx = Store;
3241 break;
3242 case Subscript_kind:
3243 expr1->v.Subscript.ctx = Store;
3244 break;
3245 case List_kind:
3246 ast_error(c, ch,
3247 "only single target (not list) can be annotated");
3248 return NULL;
3249 case Tuple_kind:
3250 ast_error(c, ch,
3251 "only single target (not tuple) can be annotated");
3252 return NULL;
3253 default:
3254 ast_error(c, ch,
3255 "illegal target for annotation");
3256 return NULL;
3257 }
3258
3259 if (expr1->kind != Name_kind) {
3260 simple = 0;
3261 }
3262 ch = CHILD(ann, 1);
3263 expr2 = ast_for_expr(c, ch);
3264 if (!expr2) {
3265 return NULL;
3266 }
3267 if (NCH(ann) == 2) {
3268 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003269 LINENO(n), n->n_col_offset,
3270 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003271 }
3272 else {
3273 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003274 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003275 expr3 = ast_for_testlist(c, ch);
3276 }
3277 else {
3278 expr3 = ast_for_expr(c, ch);
3279 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003280 if (!expr3) {
3281 return NULL;
3282 }
3283 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003284 LINENO(n), n->n_col_offset,
3285 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003286 }
3287 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003289 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003290 asdl_seq *targets;
3291 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003293 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294
Thomas Wouters89f507f2006-12-13 04:49:30 +00003295 /* a normal assignment */
3296 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003297
3298 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3299 nch_minus_type = num - has_type_comment;
3300
3301 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003302 if (!targets)
3303 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003304 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 expr_ty e;
3306 node *ch = CHILD(n, i);
3307 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003308 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 return NULL;
3310 }
3311 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003313 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003315 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003316 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318
Thomas Wouters89f507f2006-12-13 04:49:30 +00003319 asdl_seq_SET(targets, i / 2, e);
3320 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003321 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003322 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 expression = ast_for_testlist(c, value);
3324 else
3325 expression = ast_for_expr(c, value);
3326 if (!expression)
3327 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003328 if (has_type_comment) {
3329 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3330 if (!type_comment)
3331 return NULL;
3332 }
3333 else
3334 type_comment = NULL;
3335 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003336 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338}
3339
Benjamin Peterson78565b22009-06-28 19:19:51 +00003340
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003342ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343{
3344 asdl_seq *seq;
3345 int i;
3346 expr_ty e;
3347
3348 REQ(n, exprlist);
3349
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003350 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003352 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003354 e = ast_for_expr(c, CHILD(n, i));
3355 if (!e)
3356 return NULL;
3357 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003358 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360 }
3361 return seq;
3362}
3363
3364static stmt_ty
3365ast_for_del_stmt(struct compiling *c, const node *n)
3366{
3367 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 /* del_stmt: 'del' exprlist */
3370 REQ(n, del_stmt);
3371
3372 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3373 if (!expr_list)
3374 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003375 return Delete(expr_list, LINENO(n), n->n_col_offset,
3376 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377}
3378
3379static stmt_ty
3380ast_for_flow_stmt(struct compiling *c, const node *n)
3381{
3382 /*
3383 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3384 | yield_stmt
3385 break_stmt: 'break'
3386 continue_stmt: 'continue'
3387 return_stmt: 'return' [testlist]
3388 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003389 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 raise_stmt: 'raise' [test [',' test [',' test]]]
3391 */
3392 node *ch;
3393
3394 REQ(n, flow_stmt);
3395 ch = CHILD(n, 0);
3396 switch (TYPE(ch)) {
3397 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003398 return Break(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 continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003401 return Continue(LINENO(n), n->n_col_offset,
3402 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003404 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3405 if (!exp)
3406 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003407 return Expr(exp, LINENO(n), n->n_col_offset,
3408 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409 }
3410 case return_stmt:
3411 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003412 return Return(NULL, LINENO(n), n->n_col_offset,
3413 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003415 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003416 if (!expression)
3417 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003418 return Return(expression, LINENO(n), n->n_col_offset,
3419 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
3421 case raise_stmt:
3422 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003423 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3424 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003425 else if (NCH(ch) >= 2) {
3426 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3428 if (!expression)
3429 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003430 if (NCH(ch) == 4) {
3431 cause = ast_for_expr(c, CHILD(ch, 3));
3432 if (!cause)
3433 return NULL;
3434 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003435 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3436 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 }
Stefan Krahf432a322017-08-21 13:09:59 +02003438 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003440 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 "unexpected flow_stmt: %d", TYPE(ch));
3442 return NULL;
3443 }
3444}
3445
3446static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003447alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448{
3449 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003450 import_as_name: NAME ['as' NAME]
3451 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 dotted_name: NAME ('.' NAME)*
3453 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003454 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003455
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 loop:
3457 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003458 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003459 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003460 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003461 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003462 if (!name)
3463 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003464 if (NCH(n) == 3) {
3465 node *str_node = CHILD(n, 2);
3466 str = NEW_IDENTIFIER(str_node);
3467 if (!str)
3468 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003469 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003470 return NULL;
3471 }
3472 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003473 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003474 return NULL;
3475 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003476 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 case dotted_as_name:
3479 if (NCH(n) == 1) {
3480 n = CHILD(n, 0);
3481 goto loop;
3482 }
3483 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003484 node *asname_node = CHILD(n, 2);
3485 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003486 if (!a)
3487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003489 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003490 if (!a->asname)
3491 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003492 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494 return a;
3495 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003497 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003498 node *name_node = CHILD(n, 0);
3499 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003500 if (!name)
3501 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003502 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003503 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003504 return alias(name, NULL, c->c_arena);
3505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 else {
3507 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003508 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003509 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512
3513 len = 0;
3514 for (i = 0; i < NCH(n); i += 2)
3515 /* length of string plus one for the dot */
3516 len += strlen(STR(CHILD(n, i))) + 1;
3517 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003518 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 if (!str)
3520 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003521 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 if (!s)
3523 return NULL;
3524 for (i = 0; i < NCH(n); i += 2) {
3525 char *sch = STR(CHILD(n, i));
3526 strcpy(s, STR(CHILD(n, i)));
3527 s += strlen(sch);
3528 *s++ = '.';
3529 }
3530 --s;
3531 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3533 PyBytes_GET_SIZE(str),
3534 NULL);
3535 Py_DECREF(str);
3536 if (!uni)
3537 return NULL;
3538 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003539 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003540 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3541 Py_DECREF(str);
3542 return NULL;
3543 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003544 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003547 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003548 if (!str)
3549 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003550 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3551 Py_DECREF(str);
3552 return NULL;
3553 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003554 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003556 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557 "unexpected import name: %d", TYPE(n));
3558 return NULL;
3559 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560}
3561
3562static stmt_ty
3563ast_for_import_stmt(struct compiling *c, const node *n)
3564{
3565 /*
3566 import_stmt: import_name | import_from
3567 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003568 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3569 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003571 int lineno;
3572 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 int i;
3574 asdl_seq *aliases;
3575
3576 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003577 lineno = LINENO(n);
3578 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003580 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003582 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003583 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003584 if (!aliases)
3585 return NULL;
3586 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003587 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003588 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003590 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003592 // Even though n is modified above, the end position is not changed
3593 return Import(aliases, lineno, col_offset,
3594 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003596 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003598 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003599 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003600 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003601 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003603 /* Count the number of dots (for relative imports) and check for the
3604 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003605 for (idx = 1; idx < NCH(n); idx++) {
3606 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003607 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3608 if (!mod)
3609 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 idx++;
3611 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003612 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003614 ndots += 3;
3615 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003616 } else if (TYPE(CHILD(n, idx)) != DOT) {
3617 break;
3618 }
3619 ndots++;
3620 }
3621 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003622 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003623 case STAR:
3624 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003625 n = CHILD(n, idx);
3626 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003627 break;
3628 case LPAR:
3629 /* from ... import (x, y, z) */
3630 n = CHILD(n, idx + 1);
3631 n_children = NCH(n);
3632 break;
3633 case import_as_names:
3634 /* from ... import x, y, z */
3635 n = CHILD(n, idx);
3636 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003637 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003638 ast_error(c, n,
3639 "trailing comma not allowed without"
3640 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 return NULL;
3642 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003643 break;
3644 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003645 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 return NULL;
3647 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003649 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003650 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652
3653 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003654 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003655 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003656 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003658 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003660 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003661 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003662 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003663 if (!import_alias)
3664 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003665 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003666 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003668 if (mod != NULL)
3669 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003670 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003671 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003672 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 }
Neal Norwitz79792652005-11-14 04:25:03 +00003674 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 "unknown import statement: starts with command '%s'",
3676 STR(CHILD(n, 0)));
3677 return NULL;
3678}
3679
3680static stmt_ty
3681ast_for_global_stmt(struct compiling *c, const node *n)
3682{
3683 /* global_stmt: 'global' NAME (',' NAME)* */
3684 identifier name;
3685 asdl_seq *s;
3686 int i;
3687
3688 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003689 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003693 name = NEW_IDENTIFIER(CHILD(n, i));
3694 if (!name)
3695 return NULL;
3696 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003698 return Global(s, LINENO(n), n->n_col_offset,
3699 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700}
3701
3702static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003703ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3704{
3705 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3706 identifier name;
3707 asdl_seq *s;
3708 int i;
3709
3710 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003711 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003712 if (!s)
3713 return NULL;
3714 for (i = 1; i < NCH(n); i += 2) {
3715 name = NEW_IDENTIFIER(CHILD(n, i));
3716 if (!name)
3717 return NULL;
3718 asdl_seq_SET(s, i / 2, name);
3719 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003720 return Nonlocal(s, LINENO(n), n->n_col_offset,
3721 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003722}
3723
3724static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725ast_for_assert_stmt(struct compiling *c, const node *n)
3726{
3727 /* assert_stmt: 'assert' test [',' test] */
3728 REQ(n, assert_stmt);
3729 if (NCH(n) == 2) {
3730 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3731 if (!expression)
3732 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003733 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3734 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 }
3736 else if (NCH(n) == 4) {
3737 expr_ty expr1, expr2;
3738
3739 expr1 = ast_for_expr(c, CHILD(n, 1));
3740 if (!expr1)
3741 return NULL;
3742 expr2 = ast_for_expr(c, CHILD(n, 3));
3743 if (!expr2)
3744 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003746 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3747 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 }
Neal Norwitz79792652005-11-14 04:25:03 +00003749 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 "improper number of parts to 'assert' statement: %d",
3751 NCH(n));
3752 return NULL;
3753}
3754
3755static asdl_seq *
3756ast_for_suite(struct compiling *c, const node *n)
3757{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003758 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003759 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 stmt_ty s;
3761 int i, total, num, end, pos = 0;
3762 node *ch;
3763
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003764 if (TYPE(n) != func_body_suite) {
3765 REQ(n, suite);
3766 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767
3768 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003769 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003773 n = CHILD(n, 0);
3774 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003776 */
3777 end = NCH(n) - 1;
3778 if (TYPE(CHILD(n, end - 1)) == SEMI)
3779 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003781 for (i = 0; i < end; i += 2) {
3782 ch = CHILD(n, i);
3783 s = ast_for_stmt(c, ch);
3784 if (!s)
3785 return NULL;
3786 asdl_seq_SET(seq, pos++, s);
3787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 }
3789 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003790 i = 2;
3791 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3792 i += 2;
3793 REQ(CHILD(n, 2), NEWLINE);
3794 }
3795
3796 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003797 ch = CHILD(n, i);
3798 REQ(ch, stmt);
3799 num = num_stmts(ch);
3800 if (num == 1) {
3801 /* small_stmt or compound_stmt with only one child */
3802 s = ast_for_stmt(c, ch);
3803 if (!s)
3804 return NULL;
3805 asdl_seq_SET(seq, pos++, s);
3806 }
3807 else {
3808 int j;
3809 ch = CHILD(ch, 0);
3810 REQ(ch, simple_stmt);
3811 for (j = 0; j < NCH(ch); j += 2) {
3812 /* statement terminates with a semi-colon ';' */
3813 if (NCH(CHILD(ch, j)) == 0) {
3814 assert((j + 1) == NCH(ch));
3815 break;
3816 }
3817 s = ast_for_stmt(c, CHILD(ch, j));
3818 if (!s)
3819 return NULL;
3820 asdl_seq_SET(seq, pos++, s);
3821 }
3822 }
3823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 }
3825 assert(pos == seq->size);
3826 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827}
3828
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003829static void
3830get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3831{
Pablo Galindo46a97922019-02-19 22:51:53 +00003832 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003833 // There must be no empty suites.
3834 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003835 stmt_ty last = asdl_seq_GET(s, tot - 1);
3836 *end_lineno = last->end_lineno;
3837 *end_col_offset = last->end_col_offset;
3838}
3839
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840static stmt_ty
3841ast_for_if_stmt(struct compiling *c, const node *n)
3842{
3843 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3844 ['else' ':' suite]
3845 */
3846 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003847 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848
3849 REQ(n, if_stmt);
3850
3851 if (NCH(n) == 4) {
3852 expr_ty expression;
3853 asdl_seq *suite_seq;
3854
3855 expression = ast_for_expr(c, CHILD(n, 1));
3856 if (!expression)
3857 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003859 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003861 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862
Guido van Rossumd8faa362007-04-27 19:54:29 +00003863 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003864 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003866
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867 s = STR(CHILD(n, 4));
3868 /* s[2], the third character in the string, will be
3869 's' for el_s_e, or
3870 'i' for el_i_f
3871 */
3872 if (s[2] == 's') {
3873 expr_ty expression;
3874 asdl_seq *seq1, *seq2;
3875
3876 expression = ast_for_expr(c, CHILD(n, 1));
3877 if (!expression)
3878 return NULL;
3879 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003880 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 return NULL;
3882 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003883 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003885 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886
Guido van Rossumd8faa362007-04-27 19:54:29 +00003887 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003888 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 }
3890 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003891 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003892 expr_ty expression;
3893 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003894 asdl_seq *orelse = NULL;
3895 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896 /* must reference the child n_elif+1 since 'else' token is third,
3897 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003898 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3899 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3900 has_else = 1;
3901 n_elif -= 3;
3902 }
3903 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904
Thomas Wouters89f507f2006-12-13 04:49:30 +00003905 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003906 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003908 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003909 if (!orelse)
3910 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003912 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003914 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3915 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003917 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3918 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003920 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 asdl_seq_SET(orelse, 0,
3923 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003924 LINENO(CHILD(n, NCH(n) - 7)),
3925 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003926 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003927 /* the just-created orelse handled the last elif */
3928 n_elif--;
3929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930
Thomas Wouters89f507f2006-12-13 04:49:30 +00003931 for (i = 0; i < n_elif; i++) {
3932 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003933 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003934 if (!newobj)
3935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003937 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003940 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003943 if (orelse != NULL) {
3944 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3945 } else {
3946 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3947 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003948 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003950 LINENO(CHILD(n, off - 1)),
3951 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003952 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003953 orelse = newobj;
3954 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003955 expression = ast_for_expr(c, CHILD(n, 1));
3956 if (!expression)
3957 return NULL;
3958 suite_seq = ast_for_suite(c, CHILD(n, 3));
3959 if (!suite_seq)
3960 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003961 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003962 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003963 LINENO(n), n->n_col_offset,
3964 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003966
3967 PyErr_Format(PyExc_SystemError,
3968 "unexpected token in 'if' statement: %s", s);
3969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970}
3971
3972static stmt_ty
3973ast_for_while_stmt(struct compiling *c, const node *n)
3974{
3975 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3976 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003977 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978
3979 if (NCH(n) == 4) {
3980 expr_ty expression;
3981 asdl_seq *suite_seq;
3982
3983 expression = ast_for_expr(c, CHILD(n, 1));
3984 if (!expression)
3985 return NULL;
3986 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003987 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003989 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3990 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3991 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 }
3993 else if (NCH(n) == 7) {
3994 expr_ty expression;
3995 asdl_seq *seq1, *seq2;
3996
3997 expression = ast_for_expr(c, CHILD(n, 1));
3998 if (!expression)
3999 return NULL;
4000 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004001 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 return NULL;
4003 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004004 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004006 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004008 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4009 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004011
4012 PyErr_Format(PyExc_SystemError,
4013 "wrong number of tokens for 'while' statement: %d",
4014 NCH(n));
4015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016}
4017
4018static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004019ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020{
guoci90fc8982018-09-11 17:45:45 -04004021 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004022 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004024 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004025 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004026 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004027 int has_type_comment;
4028 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004029
4030 if (is_async && c->c_feature_version < 5) {
4031 ast_error(c, n,
4032 "Async for loops are only supported in Python 3.5 and greater");
4033 return NULL;
4034 }
4035
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004036 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 REQ(n, for_stmt);
4038
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004039 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4040
4041 if (NCH(n) == 9 + has_type_comment) {
4042 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 if (!seq)
4044 return NULL;
4045 }
4046
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004047 node_target = CHILD(n, 1);
4048 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004049 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004051 /* Check the # of children rather than the length of _target, since
4052 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004053 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004054 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004055 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004057 target = Tuple(_target, Store, first->lineno, first->col_offset,
4058 node_target->n_end_lineno, node_target->n_end_col_offset,
4059 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004061 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004062 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004064 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004065 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 return NULL;
4067
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004068 if (seq != NULL) {
4069 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4070 } else {
4071 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4072 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004073
4074 if (has_type_comment) {
4075 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4076 if (!type_comment)
4077 return NULL;
4078 }
4079 else
4080 type_comment = NULL;
4081
Yury Selivanov75445082015-05-11 22:57:16 -04004082 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004083 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004084 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004085 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004086 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004087 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004088 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004089 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090}
4091
4092static excepthandler_ty
4093ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4094{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004095 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004096 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097 REQ(exc, except_clause);
4098 REQ(body, suite);
4099
4100 if (NCH(exc) == 1) {
4101 asdl_seq *suite_seq = ast_for_suite(c, body);
4102 if (!suite_seq)
4103 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004104 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004105
Neal Norwitzad74aa82008-03-31 05:14:30 +00004106 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004107 exc->n_col_offset,
4108 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109 }
4110 else if (NCH(exc) == 2) {
4111 expr_ty expression;
4112 asdl_seq *suite_seq;
4113
4114 expression = ast_for_expr(c, CHILD(exc, 1));
4115 if (!expression)
4116 return NULL;
4117 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004118 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004120 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121
Neal Norwitzad74aa82008-03-31 05:14:30 +00004122 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004123 exc->n_col_offset,
4124 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125 }
4126 else if (NCH(exc) == 4) {
4127 asdl_seq *suite_seq;
4128 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004129 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004130 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004132 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004135 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 return NULL;
4137 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004138 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004140 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141
Neal Norwitzad74aa82008-03-31 05:14:30 +00004142 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004143 exc->n_col_offset,
4144 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004146
4147 PyErr_Format(PyExc_SystemError,
4148 "wrong number of children for 'except' clause: %d",
4149 NCH(exc));
4150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151}
4152
4153static stmt_ty
4154ast_for_try_stmt(struct compiling *c, const node *n)
4155{
Neal Norwitzf599f422005-12-17 21:33:47 +00004156 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004157 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004158 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004159 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161 REQ(n, try_stmt);
4162
Neal Norwitzf599f422005-12-17 21:33:47 +00004163 body = ast_for_suite(c, CHILD(n, 2));
4164 if (body == NULL)
4165 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004166
Neal Norwitzf599f422005-12-17 21:33:47 +00004167 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4168 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4169 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4170 /* we can assume it's an "else",
4171 because nch >= 9 for try-else-finally and
4172 it would otherwise have a type of except_clause */
4173 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4174 if (orelse == NULL)
4175 return NULL;
4176 n_except--;
4177 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178
Neal Norwitzf599f422005-12-17 21:33:47 +00004179 finally = ast_for_suite(c, CHILD(n, nch - 1));
4180 if (finally == NULL)
4181 return NULL;
4182 n_except--;
4183 }
4184 else {
4185 /* we can assume it's an "else",
4186 otherwise it would have a type of except_clause */
4187 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4188 if (orelse == NULL)
4189 return NULL;
4190 n_except--;
4191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004193 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004194 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 return NULL;
4196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197
Neal Norwitzf599f422005-12-17 21:33:47 +00004198 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004199 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004200 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004201 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004202 if (handlers == NULL)
4203 return NULL;
4204
4205 for (i = 0; i < n_except; i++) {
4206 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4207 CHILD(n, 5 + i * 3));
4208 if (!e)
4209 return NULL;
4210 asdl_seq_SET(handlers, i, e);
4211 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004212 }
4213
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004214 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004215 if (finally != NULL) {
4216 // finally is always last
4217 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4218 } else if (orelse != NULL) {
4219 // otherwise else is last
4220 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4221 } else {
4222 // inline the get_last_end_pos logic due to layout mismatch
4223 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4224 end_lineno = last_handler->end_lineno;
4225 end_col_offset = last_handler->end_col_offset;
4226 }
4227 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4228 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004229}
4230
Georg Brandl0c315622009-05-25 21:10:36 +00004231/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004232static withitem_ty
4233ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004234{
4235 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004236
Georg Brandl0c315622009-05-25 21:10:36 +00004237 REQ(n, with_item);
4238 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004239 if (!context_expr)
4240 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004241 if (NCH(n) == 3) {
4242 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004243
4244 if (!optional_vars) {
4245 return NULL;
4246 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004247 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004248 return NULL;
4249 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004250 }
4251
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004252 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004253}
4254
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004255/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004256static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004257ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004258{
guoci90fc8982018-09-11 17:45:45 -04004259 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004260 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004261 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004262 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004263
Guido van Rossum495da292019-03-07 12:38:08 -08004264 if (is_async && c->c_feature_version < 5) {
4265 ast_error(c, n,
4266 "Async with statements are only supported in Python 3.5 and greater");
4267 return NULL;
4268 }
4269
Georg Brandl0c315622009-05-25 21:10:36 +00004270 REQ(n, with_stmt);
4271
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004272 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4273 nch_minus_type = NCH(n) - has_type_comment;
4274
4275 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004276 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004277 if (!items)
4278 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004279 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004280 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4281 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004282 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004283 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004284 }
4285
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004286 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4287 if (!body)
4288 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004289 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004290
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004291 if (has_type_comment) {
4292 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4293 if (!type_comment)
4294 return NULL;
4295 }
4296 else
4297 type_comment = NULL;
4298
Yury Selivanov75445082015-05-11 22:57:16 -04004299 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004300 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004301 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004302 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004303 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004304 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004305}
4306
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004307static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004308ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004309{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004310 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004311 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004312 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004313 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004314 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004315
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004316 REQ(n, classdef);
4317
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004318 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004319 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004320 if (!s)
4321 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004322 get_last_end_pos(s, &end_lineno, &end_col_offset);
4323
Benjamin Peterson30760062008-11-25 04:02:28 +00004324 classname = NEW_IDENTIFIER(CHILD(n, 1));
4325 if (!classname)
4326 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004327 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004328 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004329 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004330 LINENO(n), n->n_col_offset,
4331 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004332 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004333
4334 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004335 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004336 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004337 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004338 get_last_end_pos(s, &end_lineno, &end_col_offset);
4339
Benjamin Peterson30760062008-11-25 04:02:28 +00004340 classname = NEW_IDENTIFIER(CHILD(n, 1));
4341 if (!classname)
4342 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004343 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004344 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004345 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004346 LINENO(n), n->n_col_offset,
4347 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004348 }
4349
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004350 /* class NAME '(' arglist ')' ':' suite */
4351 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004352 {
4353 PyObject *dummy_name;
4354 expr_ty dummy;
4355 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4356 if (!dummy_name)
4357 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004358 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4359 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4360 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004361 call = ast_for_call(c, CHILD(n, 3), dummy,
4362 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004363 if (!call)
4364 return NULL;
4365 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004366 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004367 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004368 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004369 get_last_end_pos(s, &end_lineno, &end_col_offset);
4370
Benjamin Peterson30760062008-11-25 04:02:28 +00004371 classname = NEW_IDENTIFIER(CHILD(n, 1));
4372 if (!classname)
4373 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004374 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004375 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004376
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004377 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004378 decorator_seq, LINENO(n), n->n_col_offset,
4379 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004380}
4381
4382static stmt_ty
4383ast_for_stmt(struct compiling *c, const node *n)
4384{
4385 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004386 assert(NCH(n) == 1);
4387 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004388 }
4389 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004390 assert(num_stmts(n) == 1);
4391 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004392 }
4393 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004394 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004395 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4396 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004397 */
4398 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004399 case expr_stmt:
4400 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004401 case del_stmt:
4402 return ast_for_del_stmt(c, n);
4403 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004404 return Pass(LINENO(n), n->n_col_offset,
4405 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004406 case flow_stmt:
4407 return ast_for_flow_stmt(c, n);
4408 case import_stmt:
4409 return ast_for_import_stmt(c, n);
4410 case global_stmt:
4411 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004412 case nonlocal_stmt:
4413 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004414 case assert_stmt:
4415 return ast_for_assert_stmt(c, n);
4416 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004417 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4419 TYPE(n), NCH(n));
4420 return NULL;
4421 }
4422 }
4423 else {
4424 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004425 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004426 */
4427 node *ch = CHILD(n, 0);
4428 REQ(n, compound_stmt);
4429 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004430 case if_stmt:
4431 return ast_for_if_stmt(c, ch);
4432 case while_stmt:
4433 return ast_for_while_stmt(c, ch);
4434 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004435 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004436 case try_stmt:
4437 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004438 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004439 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004440 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004441 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004442 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004443 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 case decorated:
4445 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004446 case async_stmt:
4447 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004448 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004449 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004450 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451 TYPE(n), NCH(n));
4452 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454 }
4455}
4456
4457static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004458parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004460 const char *end;
4461 long x;
4462 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004463 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004464 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004465
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004466 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004467 errno = 0;
4468 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004469 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004470 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004471 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004472 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004473 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004474 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004475 }
4476 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004477 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004478 if (*end == '\0') {
4479 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004480 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004481 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004482 }
4483 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004484 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004485 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004486 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4487 if (compl.imag == -1.0 && PyErr_Occurred())
4488 return NULL;
4489 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004490 }
4491 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004492 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004493 dx = PyOS_string_to_double(s, NULL, NULL);
4494 if (dx == -1.0 && PyErr_Occurred())
4495 return NULL;
4496 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004498}
4499
4500static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004501parsenumber(struct compiling *c, const char *s)
4502{
4503 char *dup, *end;
4504 PyObject *res = NULL;
4505
4506 assert(s != NULL);
4507
4508 if (strchr(s, '_') == NULL) {
4509 return parsenumber_raw(c, s);
4510 }
4511 /* Create a duplicate without underscores. */
4512 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004513 if (dup == NULL) {
4514 return PyErr_NoMemory();
4515 }
Brett Cannona721aba2016-09-09 14:57:09 -07004516 end = dup;
4517 for (; *s; s++) {
4518 if (*s != '_') {
4519 *end++ = *s;
4520 }
4521 }
4522 *end = '\0';
4523 res = parsenumber_raw(c, dup);
4524 PyMem_Free(dup);
4525 return res;
4526}
4527
4528static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004529decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004530{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004531 const char *s, *t;
4532 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004533 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4534 while (s < end && (*s & 0x80)) s++;
4535 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004536 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004537}
4538
Eric V. Smith56466482016-10-31 14:46:26 -04004539static int
4540warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004541 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004542{
4543 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4544 first_invalid_escape_char);
4545 if (msg == NULL) {
4546 return -1;
4547 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004548 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004549 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004550 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004551 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004552 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4553 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004554 to get a more accurate error report */
4555 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004556 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004557 }
4558 Py_DECREF(msg);
4559 return -1;
4560 }
4561 Py_DECREF(msg);
4562 return 0;
4563}
4564
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004565static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004566decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4567 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004568{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004569 PyObject *v, *u;
4570 char *buf;
4571 char *p;
4572 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004573
Benjamin Peterson202803a2016-02-25 22:34:45 -08004574 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004575 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004576 return NULL;
4577 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4578 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4579 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4580 if (u == NULL)
4581 return NULL;
4582 p = buf = PyBytes_AsString(u);
4583 end = s + len;
4584 while (s < end) {
4585 if (*s == '\\') {
4586 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004587 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004588 strcpy(p, "u005c");
4589 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004590 if (s >= end)
4591 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004592 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004593 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004594 if (*s & 0x80) { /* XXX inefficient */
4595 PyObject *w;
4596 int kind;
4597 void *data;
4598 Py_ssize_t len, i;
4599 w = decode_utf8(c, &s, end);
4600 if (w == NULL) {
4601 Py_DECREF(u);
4602 return NULL;
4603 }
4604 kind = PyUnicode_KIND(w);
4605 data = PyUnicode_DATA(w);
4606 len = PyUnicode_GET_LENGTH(w);
4607 for (i = 0; i < len; i++) {
4608 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4609 sprintf(p, "\\U%08x", chr);
4610 p += 10;
4611 }
4612 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004613 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004614 Py_DECREF(w);
4615 } else {
4616 *p++ = *s++;
4617 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004618 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004619 len = p - buf;
4620 s = buf;
4621
Eric V. Smith56466482016-10-31 14:46:26 -04004622 const char *first_invalid_escape;
4623 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4624
4625 if (v != NULL && first_invalid_escape != NULL) {
4626 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4627 /* We have not decref u before because first_invalid_escape points
4628 inside u. */
4629 Py_XDECREF(u);
4630 Py_DECREF(v);
4631 return NULL;
4632 }
4633 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004634 Py_XDECREF(u);
4635 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004636}
4637
Eric V. Smith56466482016-10-31 14:46:26 -04004638static PyObject *
4639decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4640 size_t len)
4641{
4642 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004643 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004644 &first_invalid_escape);
4645 if (result == NULL)
4646 return NULL;
4647
4648 if (first_invalid_escape != NULL) {
4649 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4650 Py_DECREF(result);
4651 return NULL;
4652 }
4653 }
4654 return result;
4655}
4656
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004657/* Shift locations for the given node and all its children by adding `lineno`
4658 and `col_offset` to existing locations. */
4659static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4660{
4661 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004662 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004663 for (int i = 0; i < NCH(n); ++i) {
4664 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4665 /* Shifting column offsets unnecessary if there's been newlines. */
4666 col_offset = 0;
4667 }
4668 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4669 }
4670 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004671 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004672}
4673
4674/* Fix locations for the given node and its children.
4675
4676 `parent` is the enclosing node.
4677 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004678 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004679*/
4680static void
4681fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4682{
4683 char *substr = NULL;
4684 char *start;
4685 int lines = LINENO(parent) - 1;
4686 int cols = parent->n_col_offset;
4687 /* Find the full fstring to fix location information in `n`. */
4688 while (parent && parent->n_type != STRING)
4689 parent = parent->n_child;
4690 if (parent && parent->n_str) {
4691 substr = strstr(parent->n_str, expr_str);
4692 if (substr) {
4693 start = substr;
4694 while (start > parent->n_str) {
4695 if (start[0] == '\n')
4696 break;
4697 start--;
4698 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004699 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004700 /* adjust the start based on the number of newlines encountered
4701 before the f-string expression */
4702 for (char* p = parent->n_str; p < substr; p++) {
4703 if (*p == '\n') {
4704 lines++;
4705 }
4706 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004707 }
4708 }
4709 fstring_shift_node_locations(n, lines, cols);
4710}
4711
Eric V. Smith451d0e32016-09-09 21:56:20 -04004712/* Compile this expression in to an expr_ty. Add parens around the
4713 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004714static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004715fstring_compile_expr(const char *expr_start, const char *expr_end,
4716 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004717
Eric V. Smith235a6f02015-09-19 14:51:32 -04004718{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004719 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004720 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004721 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004722 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004723 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004724
Eric V. Smith1d44c412015-09-23 07:49:00 -04004725 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004726 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004727 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4728 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004729
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004730 /* If the substring is all whitespace, it's an error. We need to catch this
4731 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4732 because turning the expression '' in to '()' would go from being invalid
4733 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004734 for (s = expr_start; s != expr_end; s++) {
4735 char c = *s;
4736 /* The Python parser ignores only the following whitespace
4737 characters (\r already is converted to \n). */
4738 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004739 break;
4740 }
4741 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004742 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004743 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004744 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004745 }
4746
Eric V. Smith451d0e32016-09-09 21:56:20 -04004747 len = expr_end - expr_start;
4748 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4749 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004750 if (str == NULL) {
4751 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004752 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004753 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004754
Eric V. Smith451d0e32016-09-09 21:56:20 -04004755 str[0] = '(';
4756 memcpy(str+1, expr_start, len);
4757 str[len+1] = ')';
4758 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004759
Victor Stinner37d66d72019-06-13 02:16:41 +02004760 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004761 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004762 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4763 Py_eval_input, 0);
4764 if (!mod_n) {
4765 PyMem_RawFree(str);
4766 return NULL;
4767 }
4768 /* Reuse str to find the correct column offset. */
4769 str[0] = '{';
4770 str[len+1] = '}';
4771 fstring_fix_node_location(n, mod_n, str);
4772 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004773 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004774 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004775 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004776 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004777 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004778}
4779
4780/* Return -1 on error.
4781
4782 Return 0 if we reached the end of the literal.
4783
4784 Return 1 if we haven't reached the end of the literal, but we want
4785 the caller to process the literal up to this point. Used for
4786 doubled braces.
4787*/
4788static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004789fstring_find_literal(const char **str, const char *end, int raw,
4790 PyObject **literal, int recurse_lvl,
4791 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004792{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004793 /* Get any literal string. It ends when we hit an un-doubled left
4794 brace (which isn't part of a unicode name escape such as
4795 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004796
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004797 const char *s = *str;
4798 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004799 int result = 0;
4800
Eric V. Smith235a6f02015-09-19 14:51:32 -04004801 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004802 while (s < end) {
4803 char ch = *s++;
4804 if (!raw && ch == '\\' && s < end) {
4805 ch = *s++;
4806 if (ch == 'N') {
4807 if (s < end && *s++ == '{') {
4808 while (s < end && *s++ != '}') {
4809 }
4810 continue;
4811 }
4812 break;
4813 }
4814 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4815 return -1;
4816 }
4817 }
4818 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004819 /* Check for doubled braces, but only at the top level. If
4820 we checked at every level, then f'{0:{3}}' would fail
4821 with the two closing braces. */
4822 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004823 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004824 /* We're going to tell the caller that the literal ends
4825 here, but that they should continue scanning. But also
4826 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004827 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004828 result = 1;
4829 goto done;
4830 }
4831
4832 /* Where a single '{' is the start of a new expression, a
4833 single '}' is not allowed. */
4834 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004835 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004836 ast_error(c, n, "f-string: single '}' is not allowed");
4837 return -1;
4838 }
4839 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004840 /* We're either at a '{', which means we're starting another
4841 expression; or a '}', which means we're at the end of this
4842 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004843 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004844 break;
4845 }
4846 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004847 *str = s;
4848 assert(s <= end);
4849 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004851 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004852 if (raw)
4853 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004854 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004855 NULL, NULL);
4856 else
Eric V. Smith56466482016-10-31 14:46:26 -04004857 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004858 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004859 if (!*literal)
4860 return -1;
4861 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004862 return result;
4863}
4864
4865/* Forward declaration because parsing is recursive. */
4866static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004867fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 struct compiling *c, const node *n);
4869
Eric V. Smith451d0e32016-09-09 21:56:20 -04004870/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004871 expression (so it must be a '{'). Returns the FormattedValue node, which
4872 includes the expression, conversion character, format_spec expression, and
4873 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004874
4875 Note that I don't do a perfect job here: I don't make sure that a
4876 closing brace doesn't match an opening paren, for example. It
4877 doesn't need to error on all invalid expressions, just correctly
4878 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004879 will be caught when we parse it later.
4880
4881 *expression is set to the expression. For an '=' "debug" expression,
4882 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004883 including the '=' and any whitespace around it, as a string object). If
4884 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004885static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004886fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004887 PyObject **expr_text, expr_ty *expression,
4888 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004889{
4890 /* Return -1 on error, else 0. */
4891
Eric V. Smith451d0e32016-09-09 21:56:20 -04004892 const char *expr_start;
4893 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004894 expr_ty simple_expression;
4895 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004896 int conversion = -1; /* The conversion char. Use default if not
4897 specified, or !r if using = and no format
4898 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004899
4900 /* 0 if we're not in a string, else the quote char we're trying to
4901 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004902 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004903
4904 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4905 int string_type = 0;
4906
4907 /* Keep track of nesting level for braces/parens/brackets in
4908 expressions. */
4909 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004910 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004911
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004912 *expr_text = NULL;
4913
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914 /* Can only nest one level deep. */
4915 if (recurse_lvl >= 2) {
4916 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004917 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918 }
4919
4920 /* The first char must be a left brace, or we wouldn't have gotten
4921 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922 assert(**str == '{');
4923 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925 expr_start = *str;
4926 for (; *str < end; (*str)++) {
4927 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928
4929 /* Loop invariants. */
4930 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004931 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004932 if (quote_char)
4933 assert(string_type == 1 || string_type == 3);
4934 else
4935 assert(string_type == 0);
4936
Eric V. Smith451d0e32016-09-09 21:56:20 -04004937 ch = **str;
4938 /* Nowhere inside an expression is a backslash allowed. */
4939 if (ch == '\\') {
4940 /* Error: can't include a backslash character, inside
4941 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004942 ast_error(c, n,
4943 "f-string expression part "
4944 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004945 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004946 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947 if (quote_char) {
4948 /* We're inside a string. See if we're at the end. */
4949 /* This code needs to implement the same non-error logic
4950 as tok_get from tokenizer.c, at the letter_quote
4951 label. To actually share that code would be a
4952 nightmare. But, it's unlikely to change and is small,
4953 so duplicate it here. Note we don't need to catch all
4954 of the errors, since they'll be caught when parsing the
4955 expression. We just need to match the non-error
4956 cases. Thus we can ignore \n in single-quoted strings,
4957 for example. Or non-terminated strings. */
4958 if (ch == quote_char) {
4959 /* Does this match the string_type (single or triple
4960 quoted)? */
4961 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004962 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004964 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965 string_type = 0;
4966 quote_char = 0;
4967 continue;
4968 }
4969 } else {
4970 /* We're at the end of a normal string. */
4971 quote_char = 0;
4972 string_type = 0;
4973 continue;
4974 }
4975 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004976 } else if (ch == '\'' || ch == '"') {
4977 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004978 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004980 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004981 } else {
4982 /* Start of a normal string. */
4983 string_type = 1;
4984 }
4985 /* Start looking for the end of the string. */
4986 quote_char = ch;
4987 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004988 if (nested_depth >= MAXLEVEL) {
4989 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004990 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004991 }
4992 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004994 } else if (ch == '#') {
4995 /* Error: can't include a comment character, inside parens
4996 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004997 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004998 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005000 (ch == '!' || ch == ':' || ch == '}' ||
5001 ch == '=' || ch == '>' || ch == '<')) {
5002 /* See if there's a next character. */
5003 if (*str+1 < end) {
5004 char next = *(*str+1);
5005
5006 /* For "!=". since '=' is not an allowed conversion character,
5007 nothing is lost in this test. */
5008 if ((ch == '!' && next == '=') || /* != */
5009 (ch == '=' && next == '=') || /* == */
5010 (ch == '<' && next == '=') || /* <= */
5011 (ch == '>' && next == '=') /* >= */
5012 ) {
5013 *str += 1;
5014 continue;
5015 }
5016 /* Don't get out of the loop for these, if they're single
5017 chars (not part of 2-char tokens). If by themselves, they
5018 don't end an expression (unlike say '!'). */
5019 if (ch == '>' || ch == '<') {
5020 continue;
5021 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005023
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 /* Normal way out of this loop. */
5025 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005026 } else if (ch == ']' || ch == '}' || ch == ')') {
5027 if (!nested_depth) {
5028 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005029 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005030 }
5031 nested_depth--;
5032 int opening = parenstack[nested_depth];
5033 if (!((opening == '(' && ch == ')') ||
5034 (opening == '[' && ch == ']') ||
5035 (opening == '{' && ch == '}')))
5036 {
5037 ast_error(c, n,
5038 "f-string: closing parenthesis '%c' "
5039 "does not match opening parenthesis '%c'",
5040 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005041 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005042 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005043 } else {
5044 /* Just consume this char and loop around. */
5045 }
5046 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005047 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005048 /* If we leave this loop in a string or with mismatched parens, we
5049 don't care. We'll get a syntax error when compiling the
5050 expression. But, we can produce a better error message, so
5051 let's just do that.*/
5052 if (quote_char) {
5053 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005054 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005055 }
5056 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005057 int opening = parenstack[nested_depth - 1];
5058 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005059 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005060 }
5061
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005064
5065 /* Compile the expression as soon as possible, so we show errors
5066 related to the expression before errors related to the
5067 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005068 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005069 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005070 goto error;
5071
5072 /* Check for =, which puts the text value of the expression in
5073 expr_text. */
5074 if (**str == '=') {
5075 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005076
5077 /* Skip over ASCII whitespace. No need to test for end of string
5078 here, since we know there's at least a trailing quote somewhere
5079 ahead. */
5080 while (Py_ISSPACE(**str)) {
5081 *str += 1;
5082 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005083
5084 /* Set *expr_text to the text of the expression. */
5085 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5086 if (!*expr_text) {
5087 goto error;
5088 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005089 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005090
5091 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 if (**str == '!') {
5093 *str += 1;
5094 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095 goto unexpected_end_of_string;
5096
Eric V. Smith451d0e32016-09-09 21:56:20 -04005097 conversion = **str;
5098 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099
5100 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005101 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005102 ast_error(c, n,
5103 "f-string: invalid conversion character: "
5104 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005105 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005107
5108 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109
5110 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 if (**str == ':') {
5114 *str += 1;
5115 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116 goto unexpected_end_of_string;
5117
5118 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005119 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005120 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005121 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005122 }
5123
Eric V. Smith451d0e32016-09-09 21:56:20 -04005124 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125 goto unexpected_end_of_string;
5126
5127 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005128 assert(*str < end);
5129 assert(**str == '}');
5130 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005132 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005133 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005134 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005135 conversion = 'r';
5136 }
5137
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 /* And now create the FormattedValue node that represents this
5139 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005140 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005141 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005142 n->n_col_offset, n->n_end_lineno,
5143 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005144 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005145 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005146
5147 return 0;
5148
5149unexpected_end_of_string:
5150 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005151 /* Falls through to error. */
5152
5153error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005154 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005155 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005156
Eric V. Smith235a6f02015-09-19 14:51:32 -04005157}
5158
5159/* Return -1 on error.
5160
5161 Return 0 if we have a literal (possible zero length) and an
5162 expression (zero length if at the end of the string.
5163
5164 Return 1 if we have a literal, but no expression, and we want the
5165 caller to call us again. This is used to deal with doubled
5166 braces.
5167
5168 When called multiple times on the string 'a{{b{0}c', this function
5169 will return:
5170
5171 1. the literal 'a{' with no expression, and a return value
5172 of 1. Despite the fact that there's no expression, the return
5173 value of 1 means we're not finished yet.
5174
5175 2. the literal 'b' and the expression '0', with a return value of
5176 0. The fact that there's an expression means we're not finished.
5177
5178 3. literal 'c' with no expression and a return value of 0. The
5179 combination of the return value of 0 with no expression means
5180 we're finished.
5181*/
5182static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5184 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005185 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005186 struct compiling *c, const node *n)
5187{
5188 int result;
5189
5190 assert(*literal == NULL && *expression == NULL);
5191
5192 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005193 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005194 if (result < 0)
5195 goto error;
5196
5197 assert(result == 0 || result == 1);
5198
5199 if (result == 1)
5200 /* We have a literal, but don't look at the expression. */
5201 return 1;
5202
Eric V. Smith451d0e32016-09-09 21:56:20 -04005203 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005204 /* We're at the end of the string or the end of a nested
5205 f-string: no expression. The top-level error case where we
5206 expect to be at the end of the string but we're at a '}' is
5207 handled later. */
5208 return 0;
5209
5210 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005211 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005213 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5214 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215 goto error;
5216
5217 return 0;
5218
5219error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005220 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005221 return -1;
5222}
5223
5224#define EXPRLIST_N_CACHED 64
5225
5226typedef struct {
5227 /* Incrementally build an array of expr_ty, so be used in an
5228 asdl_seq. Cache some small but reasonably sized number of
5229 expr_ty's, and then after that start dynamically allocating,
5230 doubling the number allocated each time. Note that the f-string
5231 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005232 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005233 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005234
5235 Py_ssize_t allocated; /* Number we've allocated. */
5236 Py_ssize_t size; /* Number we've used. */
5237 expr_ty *p; /* Pointer to the memory we're actually
5238 using. Will point to 'data' until we
5239 start dynamically allocating. */
5240 expr_ty data[EXPRLIST_N_CACHED];
5241} ExprList;
5242
5243#ifdef NDEBUG
5244#define ExprList_check_invariants(l)
5245#else
5246static void
5247ExprList_check_invariants(ExprList *l)
5248{
5249 /* Check our invariants. Make sure this object is "live", and
5250 hasn't been deallocated. */
5251 assert(l->size >= 0);
5252 assert(l->p != NULL);
5253 if (l->size <= EXPRLIST_N_CACHED)
5254 assert(l->data == l->p);
5255}
5256#endif
5257
5258static void
5259ExprList_Init(ExprList *l)
5260{
5261 l->allocated = EXPRLIST_N_CACHED;
5262 l->size = 0;
5263
5264 /* Until we start allocating dynamically, p points to data. */
5265 l->p = l->data;
5266
5267 ExprList_check_invariants(l);
5268}
5269
5270static int
5271ExprList_Append(ExprList *l, expr_ty exp)
5272{
5273 ExprList_check_invariants(l);
5274 if (l->size >= l->allocated) {
5275 /* We need to alloc (or realloc) the memory. */
5276 Py_ssize_t new_size = l->allocated * 2;
5277
5278 /* See if we've ever allocated anything dynamically. */
5279 if (l->p == l->data) {
5280 Py_ssize_t i;
5281 /* We're still using the cached data. Switch to
5282 alloc-ing. */
5283 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5284 if (!l->p)
5285 return -1;
5286 /* Copy the cached data into the new buffer. */
5287 for (i = 0; i < l->size; i++)
5288 l->p[i] = l->data[i];
5289 } else {
5290 /* Just realloc. */
5291 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5292 if (!tmp) {
5293 PyMem_RawFree(l->p);
5294 l->p = NULL;
5295 return -1;
5296 }
5297 l->p = tmp;
5298 }
5299
5300 l->allocated = new_size;
5301 assert(l->allocated == 2 * l->size);
5302 }
5303
5304 l->p[l->size++] = exp;
5305
5306 ExprList_check_invariants(l);
5307 return 0;
5308}
5309
5310static void
5311ExprList_Dealloc(ExprList *l)
5312{
5313 ExprList_check_invariants(l);
5314
5315 /* If there's been an error, or we've never dynamically allocated,
5316 do nothing. */
5317 if (!l->p || l->p == l->data) {
5318 /* Do nothing. */
5319 } else {
5320 /* We have dynamically allocated. Free the memory. */
5321 PyMem_RawFree(l->p);
5322 }
5323 l->p = NULL;
5324 l->size = -1;
5325}
5326
5327static asdl_seq *
5328ExprList_Finish(ExprList *l, PyArena *arena)
5329{
5330 asdl_seq *seq;
5331
5332 ExprList_check_invariants(l);
5333
5334 /* Allocate the asdl_seq and copy the expressions in to it. */
5335 seq = _Py_asdl_seq_new(l->size, arena);
5336 if (seq) {
5337 Py_ssize_t i;
5338 for (i = 0; i < l->size; i++)
5339 asdl_seq_SET(seq, i, l->p[i]);
5340 }
5341 ExprList_Dealloc(l);
5342 return seq;
5343}
5344
5345/* The FstringParser is designed to add a mix of strings and
5346 f-strings, and concat them together as needed. Ultimately, it
5347 generates an expr_ty. */
5348typedef struct {
5349 PyObject *last_str;
5350 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005351 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005352} FstringParser;
5353
5354#ifdef NDEBUG
5355#define FstringParser_check_invariants(state)
5356#else
5357static void
5358FstringParser_check_invariants(FstringParser *state)
5359{
5360 if (state->last_str)
5361 assert(PyUnicode_CheckExact(state->last_str));
5362 ExprList_check_invariants(&state->expr_list);
5363}
5364#endif
5365
5366static void
5367FstringParser_Init(FstringParser *state)
5368{
5369 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005370 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005371 ExprList_Init(&state->expr_list);
5372 FstringParser_check_invariants(state);
5373}
5374
5375static void
5376FstringParser_Dealloc(FstringParser *state)
5377{
5378 FstringParser_check_invariants(state);
5379
5380 Py_XDECREF(state->last_str);
5381 ExprList_Dealloc(&state->expr_list);
5382}
5383
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005384/* Constants for the following */
5385static PyObject *u_kind;
5386
5387/* Compute 'kind' field for string Constant (either 'u' or None) */
5388static PyObject *
5389make_kind(struct compiling *c, const node *n)
5390{
5391 char *s = NULL;
5392 PyObject *kind = NULL;
5393
5394 /* Find the first string literal, if any */
5395 while (TYPE(n) != STRING) {
5396 if (NCH(n) == 0)
5397 return NULL;
5398 n = CHILD(n, 0);
5399 }
5400 REQ(n, STRING);
5401
5402 /* If it starts with 'u', return a PyUnicode "u" string */
5403 s = STR(n);
5404 if (s && *s == 'u') {
5405 if (!u_kind) {
5406 u_kind = PyUnicode_InternFromString("u");
5407 if (!u_kind)
5408 return NULL;
5409 }
5410 kind = u_kind;
5411 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5412 return NULL;
5413 }
5414 Py_INCREF(kind);
5415 }
5416 return kind;
5417}
5418
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005419/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005420static expr_ty
5421make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5422{
5423 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005424 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005425 *str = NULL;
5426 assert(PyUnicode_CheckExact(s));
5427 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5428 Py_DECREF(s);
5429 return NULL;
5430 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005431 kind = make_kind(c, n);
5432 if (kind == NULL && PyErr_Occurred())
5433 return NULL;
5434 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005435 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005436}
5437
5438/* Add a non-f-string (that is, a regular literal string). str is
5439 decref'd. */
5440static int
5441FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5442{
5443 FstringParser_check_invariants(state);
5444
5445 assert(PyUnicode_CheckExact(str));
5446
5447 if (PyUnicode_GET_LENGTH(str) == 0) {
5448 Py_DECREF(str);
5449 return 0;
5450 }
5451
5452 if (!state->last_str) {
5453 /* We didn't have a string before, so just remember this one. */
5454 state->last_str = str;
5455 } else {
5456 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005457 PyUnicode_AppendAndDel(&state->last_str, str);
5458 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005459 return -1;
5460 }
5461 FstringParser_check_invariants(state);
5462 return 0;
5463}
5464
Eric V. Smith451d0e32016-09-09 21:56:20 -04005465/* Parse an f-string. The f-string is in *str to end, with no
5466 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005467static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005468FstringParser_ConcatFstring(FstringParser *state, const char **str,
5469 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005470 struct compiling *c, const node *n)
5471{
5472 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005473 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005474
5475 /* Parse the f-string. */
5476 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005477 PyObject *literal = NULL;
5478 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005479 expr_ty expression = NULL;
5480
5481 /* If there's a zero length literal in front of the
5482 expression, literal will be NULL. If we're at the end of
5483 the f-string, expression will be NULL (unless result == 1,
5484 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005485 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005486 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005487 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005488 if (result < 0)
5489 return -1;
5490
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005491 /* Add the literal, if any. */
5492 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5493 Py_XDECREF(expr_text);
5494 return -1;
5495 }
5496 /* Add the expr_text, if any. */
5497 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5498 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005499 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005500
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005501 /* We've dealt with the literal and expr_text, their ownership has
5502 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005503
5504 /* See if we should just loop around to get the next literal
5505 and expression, while ignoring the expression this
5506 time. This is used for un-doubling braces, as an
5507 optimization. */
5508 if (result == 1)
5509 continue;
5510
5511 if (!expression)
5512 /* We're done with this f-string. */
5513 break;
5514
5515 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005516 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005517 if (!state->last_str) {
5518 /* Do nothing. No previous literal. */
5519 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005520 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005521 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5522 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5523 return -1;
5524 }
5525
5526 if (ExprList_Append(&state->expr_list, expression) < 0)
5527 return -1;
5528 }
5529
Eric V. Smith235a6f02015-09-19 14:51:32 -04005530 /* If recurse_lvl is zero, then we must be at the end of the
5531 string. Otherwise, we must be at a right brace. */
5532
Eric V. Smith451d0e32016-09-09 21:56:20 -04005533 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005534 ast_error(c, n, "f-string: unexpected end of string");
5535 return -1;
5536 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005537 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005538 ast_error(c, n, "f-string: expecting '}'");
5539 return -1;
5540 }
5541
5542 FstringParser_check_invariants(state);
5543 return 0;
5544}
5545
5546/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005547 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005548static expr_ty
5549FstringParser_Finish(FstringParser *state, struct compiling *c,
5550 const node *n)
5551{
5552 asdl_seq *seq;
5553
5554 FstringParser_check_invariants(state);
5555
5556 /* If we're just a constant string with no expressions, return
5557 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005558 if (!state->fmode) {
5559 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005560 if (!state->last_str) {
5561 /* Create a zero length string. */
5562 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5563 if (!state->last_str)
5564 goto error;
5565 }
5566 return make_str_node_and_del(&state->last_str, c, n);
5567 }
5568
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005569 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005570 last node in our expression list. */
5571 if (state->last_str) {
5572 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5573 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5574 goto error;
5575 }
5576 /* This has already been freed. */
5577 assert(state->last_str == NULL);
5578
5579 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5580 if (!seq)
5581 goto error;
5582
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005583 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5584 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005585
5586error:
5587 FstringParser_Dealloc(state);
5588 return NULL;
5589}
5590
Eric V. Smith451d0e32016-09-09 21:56:20 -04005591/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5592 at end, parse it into an expr_ty. Return NULL on error. Adjust
5593 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005594static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005595fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005596 struct compiling *c, const node *n)
5597{
5598 FstringParser state;
5599
5600 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005601 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005602 c, n) < 0) {
5603 FstringParser_Dealloc(&state);
5604 return NULL;
5605 }
5606
5607 return FstringParser_Finish(&state, c, n);
5608}
5609
5610/* n is a Python string literal, including the bracketing quote
5611 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005612 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005613 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005614 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5615 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005616*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005617static int
5618parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5619 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005620{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005621 size_t len;
5622 const char *s = STR(n);
5623 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005624 int fmode = 0;
5625 *bytesmode = 0;
5626 *rawmode = 0;
5627 *result = NULL;
5628 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005629 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005630 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005631 if (quote == 'b' || quote == 'B') {
5632 quote = *++s;
5633 *bytesmode = 1;
5634 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005635 else if (quote == 'u' || quote == 'U') {
5636 quote = *++s;
5637 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005638 else if (quote == 'r' || quote == 'R') {
5639 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005640 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005641 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005642 else if (quote == 'f' || quote == 'F') {
5643 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005644 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005645 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005646 else {
5647 break;
5648 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005649 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005650 }
Guido van Rossum495da292019-03-07 12:38:08 -08005651
5652 /* fstrings are only allowed in Python 3.6 and greater */
5653 if (fmode && c->c_feature_version < 6) {
5654 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5655 return -1;
5656 }
5657
Eric V. Smith451d0e32016-09-09 21:56:20 -04005658 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005659 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005660 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005661 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005662 if (quote != '\'' && quote != '\"') {
5663 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005664 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005665 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005666 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005667 s++;
5668 len = strlen(s);
5669 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005670 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005671 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005672 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005673 }
5674 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005675 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005676 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005677 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005678 }
5679 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005680 /* A triple quoted string. We've already skipped one quote at
5681 the start and one at the end of the string. Now skip the
5682 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005683 s += 2;
5684 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005685 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005686 if (s[--len] != quote || s[--len] != quote) {
5687 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005688 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005689 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005690 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005691
Eric V. Smith451d0e32016-09-09 21:56:20 -04005692 if (fmode) {
5693 /* Just return the bytes. The caller will parse the resulting
5694 string. */
5695 *fstr = s;
5696 *fstrlen = len;
5697 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005698 }
5699
Eric V. Smith451d0e32016-09-09 21:56:20 -04005700 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005701 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005702 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005703 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005704 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005705 const char *ch;
5706 for (ch = s; *ch; ch++) {
5707 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005708 ast_error(c, n,
5709 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005710 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005711 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005712 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005713 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 if (*rawmode)
5715 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005716 else
Eric V. Smith56466482016-10-31 14:46:26 -04005717 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005718 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005719 if (*rawmode)
5720 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005721 else
Eric V. Smith56466482016-10-31 14:46:26 -04005722 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005723 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005724 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005725}
5726
Eric V. Smith235a6f02015-09-19 14:51:32 -04005727/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5728 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005729 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005730 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005731 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005732 node if there's just an f-string (with no leading or trailing
5733 literals), or a JoinedStr node if there are multiple f-strings or
5734 any literals involved. */
5735static expr_ty
5736parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005737{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005738 int bytesmode = 0;
5739 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005740 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741
5742 FstringParser state;
5743 FstringParser_Init(&state);
5744
5745 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005746 int this_bytesmode;
5747 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005748 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005749 const char *fstr;
5750 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005751
5752 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005753 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5754 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005755 goto error;
5756
5757 /* Check that we're not mixing bytes with unicode. */
5758 if (i != 0 && bytesmode != this_bytesmode) {
5759 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005760 /* s is NULL if the current string part is an f-string. */
5761 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005762 goto error;
5763 }
5764 bytesmode = this_bytesmode;
5765
Eric V. Smith451d0e32016-09-09 21:56:20 -04005766 if (fstr != NULL) {
5767 int result;
5768 assert(s == NULL && !bytesmode);
5769 /* This is an f-string. Parse and concatenate it. */
5770 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5771 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005772 if (result < 0)
5773 goto error;
5774 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005775 /* A string or byte string. */
5776 assert(s != NULL && fstr == NULL);
5777
Eric V. Smith451d0e32016-09-09 21:56:20 -04005778 assert(bytesmode ? PyBytes_CheckExact(s) :
5779 PyUnicode_CheckExact(s));
5780
Eric V. Smith451d0e32016-09-09 21:56:20 -04005781 if (bytesmode) {
5782 /* For bytes, concat as we go. */
5783 if (i == 0) {
5784 /* First time, just remember this value. */
5785 bytes_str = s;
5786 } else {
5787 PyBytes_ConcatAndDel(&bytes_str, s);
5788 if (!bytes_str)
5789 goto error;
5790 }
5791 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005792 /* This is a regular string. Concatenate it. */
5793 if (FstringParser_ConcatAndDel(&state, s) < 0)
5794 goto error;
5795 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005796 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005797 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005798 if (bytesmode) {
5799 /* Just return the bytes object and we're done. */
5800 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5801 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005802 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005803 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005804 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005805
Eric V. Smith235a6f02015-09-19 14:51:32 -04005806 /* We're not a bytes string, bytes_str should never have been set. */
5807 assert(bytes_str == NULL);
5808
5809 return FstringParser_Finish(&state, c, n);
5810
5811error:
5812 Py_XDECREF(bytes_str);
5813 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005815}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005816
5817PyObject *
5818_PyAST_GetDocString(asdl_seq *body)
5819{
5820 if (!asdl_seq_LEN(body)) {
5821 return NULL;
5822 }
5823 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5824 if (st->kind != Expr_kind) {
5825 return NULL;
5826 }
5827 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005828 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5829 return e->v.Constant.value;
5830 }
5831 return NULL;
5832}