blob: 2b74ed4dbdefd6503c58a162534840a69c7167a7 [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: {
730 char buf[128];
731
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000732 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 TYPE(n), NCH(n));
734 Py_FatalError(buf);
735 }
736 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700737 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738}
739
740/* Transform the CST rooted at node * to the appropriate AST
741*/
742
743mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200744PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
745 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000747 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800749 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 stmt_ty s;
751 node *ch;
752 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500753 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800754 asdl_seq *argtypes = NULL;
755 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400757 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200758 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400759 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800760 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700761 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800762
763 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765
Jeremy Hyltona8293132006-02-28 17:58:27 +0000766 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 switch (TYPE(n)) {
768 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200769 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500771 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 for (i = 0; i < NCH(n) - 1; i++) {
773 ch = CHILD(n, i);
774 if (TYPE(ch) == NEWLINE)
775 continue;
776 REQ(ch, stmt);
777 num = num_stmts(ch);
778 if (num == 1) {
779 s = ast_for_stmt(&c, ch);
780 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000782 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 }
784 else {
785 ch = CHILD(ch, 0);
786 REQ(ch, simple_stmt);
787 for (j = 0; j < num; j++) {
788 s = ast_for_stmt(&c, CHILD(ch, j * 2));
789 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500790 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000791 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 }
793 }
794 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800795
796 /* Type ignores are stored under the ENDMARKER in file_input. */
797 ch = CHILD(n, NCH(n) - 1);
798 REQ(ch, ENDMARKER);
799 num = NCH(ch);
800 type_ignores = _Py_asdl_seq_new(num, arena);
801 if (!type_ignores)
802 goto out;
803
804 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700805 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
806 if (!type_comment)
807 goto out;
808 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800809 if (!ti)
810 goto out;
811 asdl_seq_SET(type_ignores, i, ti);
812 }
813
814 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500815 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 case eval_input: {
817 expr_ty testlist_ast;
818
Nick Coghlan650f0d02007-04-15 12:05:43 +0000819 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000820 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500822 goto out;
823 res = Expression(testlist_ast, arena);
824 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 }
826 case single_input:
827 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200828 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000832 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000834 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500835 goto out;
836 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 }
838 else {
839 n = CHILD(n, 0);
840 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200841 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000845 s = ast_for_stmt(&c, n);
846 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 asdl_seq_SET(stmts, 0, s);
849 }
850 else {
851 /* Only a simple_stmt can contain multiple statements. */
852 REQ(n, simple_stmt);
853 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 if (TYPE(CHILD(n, i)) == NEWLINE)
855 break;
856 s = ast_for_stmt(&c, CHILD(n, i));
857 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 asdl_seq_SET(stmts, i / 2, s);
860 }
861 }
862
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500865 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800866 case func_type_input:
867 n = CHILD(n, 0);
868 REQ(n, func_type);
869
870 if (TYPE(CHILD(n, 1)) == typelist) {
871 ch = CHILD(n, 1);
872 /* this is overly permissive -- we don't pay any attention to
873 * stars on the args -- just parse them into an ordered list */
874 num = 0;
875 for (i = 0; i < NCH(ch); i++) {
876 if (TYPE(CHILD(ch, i)) == test) {
877 num++;
878 }
879 }
880
881 argtypes = _Py_asdl_seq_new(num, arena);
882 if (!argtypes)
883 goto out;
884
885 j = 0;
886 for (i = 0; i < NCH(ch); i++) {
887 if (TYPE(CHILD(ch, i)) == test) {
888 arg = ast_for_expr(&c, CHILD(ch, i));
889 if (!arg)
890 goto out;
891 asdl_seq_SET(argtypes, j++, arg);
892 }
893 }
894 }
895 else {
896 argtypes = _Py_asdl_seq_new(0, arena);
897 if (!argtypes)
898 goto out;
899 }
900
901 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
902 if (!ret)
903 goto out;
904 res = FunctionType(argtypes, ret, arena);
905 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000907 PyErr_Format(PyExc_SystemError,
908 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500909 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500911 out:
912 if (c.c_normalize) {
913 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500914 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500915 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916}
917
Victor Stinner14e461d2013-08-26 22:28:21 +0200918mod_ty
919PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
920 PyArena *arena)
921{
922 mod_ty mod;
923 PyObject *filename;
924 filename = PyUnicode_DecodeFSDefault(filename_str);
925 if (filename == NULL)
926 return NULL;
927 mod = PyAST_FromNodeObject(n, flags, filename, arena);
928 Py_DECREF(filename);
929 return mod;
930
931}
932
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
934*/
935
936static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800937get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938{
939 switch (TYPE(n)) {
940 case VBAR:
941 return BitOr;
942 case CIRCUMFLEX:
943 return BitXor;
944 case AMPER:
945 return BitAnd;
946 case LEFTSHIFT:
947 return LShift;
948 case RIGHTSHIFT:
949 return RShift;
950 case PLUS:
951 return Add;
952 case MINUS:
953 return Sub;
954 case STAR:
955 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400956 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800957 if (c->c_feature_version < 5) {
958 ast_error(c, n,
959 "The '@' operator is only supported in Python 3.5 and greater");
960 return (operator_ty)0;
961 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400962 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case SLASH:
964 return Div;
965 case DOUBLESLASH:
966 return FloorDiv;
967 case PERCENT:
968 return Mod;
969 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 }
972}
973
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200974static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000975 "None",
976 "True",
977 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200978 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000979 NULL,
980};
981
982static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400983forbidden_name(struct compiling *c, identifier name, const node *n,
984 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000985{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000986 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200987 const char * const *p = FORBIDDEN;
988 if (!full_checks) {
989 /* In most cases, the parser will protect True, False, and None
990 from being assign to. */
991 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000992 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200993 for (; *p; p++) {
994 if (_PyUnicode_EqualToASCIIString(name, *p)) {
995 ast_error(c, n, "cannot assign to %U", name);
996 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000997 }
998 }
999 return 0;
1000}
1001
Serhiy Storchakab619b092018-11-27 09:40:29 +02001002static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001003copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001004{
1005 if (e) {
1006 e->lineno = LINENO(n);
1007 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001008 e->end_lineno = end->n_end_lineno;
1009 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001010 }
1011 return e;
1012}
1013
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001014static const char *
1015get_expr_name(expr_ty e)
1016{
1017 switch (e->kind) {
1018 case Attribute_kind:
1019 return "attribute";
1020 case Subscript_kind:
1021 return "subscript";
1022 case Starred_kind:
1023 return "starred";
1024 case Name_kind:
1025 return "name";
1026 case List_kind:
1027 return "list";
1028 case Tuple_kind:
1029 return "tuple";
1030 case Lambda_kind:
1031 return "lambda";
1032 case Call_kind:
1033 return "function call";
1034 case BoolOp_kind:
1035 case BinOp_kind:
1036 case UnaryOp_kind:
1037 return "operator";
1038 case GeneratorExp_kind:
1039 return "generator expression";
1040 case Yield_kind:
1041 case YieldFrom_kind:
1042 return "yield expression";
1043 case Await_kind:
1044 return "await expression";
1045 case ListComp_kind:
1046 return "list comprehension";
1047 case SetComp_kind:
1048 return "set comprehension";
1049 case DictComp_kind:
1050 return "dict comprehension";
1051 case Dict_kind:
1052 return "dict display";
1053 case Set_kind:
1054 return "set display";
1055 case JoinedStr_kind:
1056 case FormattedValue_kind:
1057 return "f-string expression";
1058 case Constant_kind: {
1059 PyObject *value = e->v.Constant.value;
1060 if (value == Py_None) {
1061 return "None";
1062 }
1063 if (value == Py_False) {
1064 return "False";
1065 }
1066 if (value == Py_True) {
1067 return "True";
1068 }
1069 if (value == Py_Ellipsis) {
1070 return "Ellipsis";
1071 }
1072 return "literal";
1073 }
1074 case Compare_kind:
1075 return "comparison";
1076 case IfExp_kind:
1077 return "conditional expression";
1078 case NamedExpr_kind:
1079 return "named expression";
1080 default:
1081 PyErr_Format(PyExc_SystemError,
1082 "unexpected expression in assignment %d (line %d)",
1083 e->kind, e->lineno);
1084 return NULL;
1085 }
1086}
1087
Jeremy Hyltona8293132006-02-28 17:58:27 +00001088/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
1090 Only sets context for expr kinds that "can appear in assignment context"
1091 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1092 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093*/
1094
1095static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001096set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097{
1098 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001099
Serhiy Storchaka6b975982020-03-17 23:41:08 +02001100 /* Expressions in an augmented assignment have a Store context. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101
1102 switch (e->kind) {
1103 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001104 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001105 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001106 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001107 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001109 e->v.Subscript.ctx = ctx;
1110 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001111 case Starred_kind:
1112 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001113 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001114 return 0;
1115 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001117 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001118 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001119 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001120 }
1121 e->v.Name.ctx = ctx;
1122 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001124 e->v.List.ctx = ctx;
1125 s = e->v.List.elts;
1126 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001128 e->v.Tuple.ctx = ctx;
1129 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001130 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001131 default: {
1132 const char *expr_name = get_expr_name(e);
1133 if (expr_name != NULL) {
1134 ast_error(c, n, "cannot %s %s",
1135 ctx == Store ? "assign to" : "delete",
1136 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001137 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001138 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001139 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001140 }
1141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 */
1145 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001146 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147
Thomas Wouters89f507f2006-12-13 04:49:30 +00001148 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001149 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001150 return 0;
1151 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 }
1153 return 1;
1154}
1155
1156static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001157ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158{
1159 REQ(n, augassign);
1160 n = CHILD(n, 0);
1161 switch (STR(n)[0]) {
1162 case '+':
1163 return Add;
1164 case '-':
1165 return Sub;
1166 case '/':
1167 if (STR(n)[1] == '/')
1168 return FloorDiv;
1169 else
1170 return Div;
1171 case '%':
1172 return Mod;
1173 case '<':
1174 return LShift;
1175 case '>':
1176 return RShift;
1177 case '&':
1178 return BitAnd;
1179 case '^':
1180 return BitXor;
1181 case '|':
1182 return BitOr;
1183 case '*':
1184 if (STR(n)[1] == '*')
1185 return Pow;
1186 else
1187 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001188 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001189 if (c->c_feature_version < 5) {
1190 ast_error(c, n,
1191 "The '@' operator is only supported in Python 3.5 and greater");
1192 return (operator_ty)0;
1193 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001194 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001196 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 }
1199}
1200
1201static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001202ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001204 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 |'is' 'not'
1206 */
1207 REQ(n, comp_op);
1208 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001209 n = CHILD(n, 0);
1210 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 case LESS:
1212 return Lt;
1213 case GREATER:
1214 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001215 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 return Eq;
1217 case LESSEQUAL:
1218 return LtE;
1219 case GREATEREQUAL:
1220 return GtE;
1221 case NOTEQUAL:
1222 return NotEq;
1223 case NAME:
1224 if (strcmp(STR(n), "in") == 0)
1225 return In;
1226 if (strcmp(STR(n), "is") == 0)
1227 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001228 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001230 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 }
1235 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001236 /* handle "not in" and "is not" */
1237 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 case NAME:
1239 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1240 return NotIn;
1241 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1242 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001243 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001245 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001248 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 }
Neal Norwitz79792652005-11-14 04:25:03 +00001250 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255static asdl_seq *
1256seq_for_testlist(struct compiling *c, const node *n)
1257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001259 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1260 */
Armin Rigo31441302005-10-21 12:57:31 +00001261 asdl_seq *seq;
1262 expr_ty expression;
1263 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001264 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001266 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 if (!seq)
1268 return NULL;
1269
1270 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001272 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273
Benjamin Peterson4905e802009-09-27 02:43:28 +00001274 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277
1278 assert(i / 2 < seq->size);
1279 asdl_seq_SET(seq, i / 2, expression);
1280 }
1281 return seq;
1282}
1283
Neal Norwitzc1505362006-12-28 06:47:50 +00001284static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001285ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001286{
1287 identifier name;
1288 expr_ty annotation = NULL;
1289 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001290 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001291
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001292 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001294 name = NEW_IDENTIFIER(ch);
1295 if (!name)
1296 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001297 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001298 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001299
1300 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1301 annotation = ast_for_expr(c, CHILD(n, 2));
1302 if (!annotation)
1303 return NULL;
1304 }
1305
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001306 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001307 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001308 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001309 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001310 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311}
1312
Guido van Rossum4f72a782006-10-27 23:31:49 +00001313/* returns -1 if failed to handle keyword only arguments
1314 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001315 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001316 ^^^
1317 start pointing here
1318 */
1319static int
1320handle_keywordonly_args(struct compiling *c, const node *n, int start,
1321 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1322{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001323 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001326 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 int i = start;
1328 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001329
1330 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001331 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001332 return -1;
1333 }
1334 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 while (i < NCH(n)) {
1336 ch = CHILD(n, i);
1337 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001338 case vfpdef:
1339 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001341 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001342 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001343 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344 asdl_seq_SET(kwdefaults, j, expression);
1345 i += 2; /* '=' and test */
1346 }
1347 else { /* setting NULL if no default value exists */
1348 asdl_seq_SET(kwdefaults, j, NULL);
1349 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 if (NCH(ch) == 3) {
1351 /* ch is NAME ':' test */
1352 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001353 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001354 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001355 }
1356 else {
1357 annotation = NULL;
1358 }
1359 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001360 argname = NEW_IDENTIFIER(ch);
1361 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001363 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001364 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001365 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001366 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001367 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001368 if (!arg)
1369 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001371 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001372 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001373 i += 1; /* the comma, if present */
1374 break;
1375 case TYPE_COMMENT:
1376 /* arg will be equal to the last argument processed */
1377 arg->type_comment = NEW_TYPE_COMMENT(ch);
1378 if (!arg->type_comment)
1379 goto error;
1380 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 break;
1382 case DOUBLESTAR:
1383 return i;
1384 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001385 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 goto error;
1387 }
1388 }
1389 return i;
1390 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393
Jeremy Hyltona8293132006-02-28 17:58:27 +00001394/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395
1396static arguments_ty
1397ast_for_arguments(struct compiling *c, const node *n)
1398{
Neal Norwitzc1505362006-12-28 06:47:50 +00001399 /* This function handles both typedargslist (function definition)
1400 and varargslist (lambda definition).
1401
1402 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001403
1404 The following definition for typedarglist is equivalent to this set of rules:
1405
1406 arguments = argument (',' [TYPE_COMMENT] argument)*
1407 argument = tfpdef ['=' test]
1408 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1409 args = '*' [tfpdef]
1410 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1411 [TYPE_COMMENT] [kwargs]])
1412 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1413 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1414 [TYPE_COMMENT] [args_kwonly_kwargs]])
1415 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1416 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1417 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1418
1419 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1420 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1421 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1422 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1423 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1424 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1425 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1426 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1427 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1428 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1429 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1430 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1431 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1432 '**' tfpdef [','] [TYPE_COMMENT]))
1433
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001434 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001435
1436 The following definition for varargslist is equivalent to this set of rules:
1437
1438 arguments = argument (',' argument )*
1439 argument = vfpdef ['=' test]
1440 kwargs = '**' vfpdef [',']
1441 args = '*' [vfpdef]
1442 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1443 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1444 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1445 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1446 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1447 (vararglist_no_posonly)
1448
1449 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1450 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1451 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1452 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1453 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1454 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1455 [',']]] | '**' vfpdef [','])
1456
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001457 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001460 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001461 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001462 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001463 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001464 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 node *ch;
1466
1467 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001469 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001470 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001472 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473
Jeremy Hyltone921e022008-07-17 16:37:17 +00001474 /* First count the number of positional args & defaults. The
1475 variable i is the loop index for this for loop and the next.
1476 The next loop picks up where the first leaves off.
1477 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 ch = CHILD(n, i);
1480 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001481 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001482 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001483 if (i < NCH(n) && /* skip argument following star */
1484 (TYPE(CHILD(n, i)) == tfpdef ||
1485 TYPE(CHILD(n, i)) == vfpdef)) {
1486 i++;
1487 }
1488 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001489 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001490 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001491 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001492 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001493 if (TYPE(ch) == SLASH ) {
1494 nposonlyargs = nposargs;
1495 nposargs = 0;
1496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001499 defaults for keyword only args */
1500 for ( ; i < NCH(n); ++i) {
1501 ch = CHILD(n, i);
1502 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001503 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001504 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001505 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1506 if (!posonlyargs && nposonlyargs) {
1507 return NULL;
1508 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001509 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001510 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001511 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001512 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001513 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001514 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001515 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001517 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001518 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001519 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001521 since we set NULL as default for keyword only argument w/o default
1522 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001523 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001524 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001525 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001526 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001527
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001528 /* tfpdef: NAME [':' test]
1529 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 */
1531 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001532 j = 0; /* index for defaults */
1533 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001534 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536 ch = CHILD(n, i);
1537 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001538 case tfpdef:
1539 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1541 anything other than EQUAL or a comma? */
1542 /* XXX Should NCH(n) check be made a separate check? */
1543 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001544 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1545 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001546 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547 assert(posdefaults != NULL);
1548 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001550 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001552 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001553 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001554 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001555 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001557 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001558 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001559 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001560 if (l < nposonlyargs) {
1561 asdl_seq_SET(posonlyargs, l++, arg);
1562 } else {
1563 asdl_seq_SET(posargs, k++, arg);
1564 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001565 i += 1; /* the name */
1566 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1567 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001569 case SLASH:
1570 /* Advance the slash and the comma. If there are more names
1571 * after the slash there will be a comma so we are advancing
1572 * the correct number of nodes. If the slash is the last item,
1573 * we will be advancing an extra token but then * i > NCH(n)
1574 * and the enclosing while will finish correctly. */
1575 i += 2;
1576 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001578 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001579 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1580 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001581 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001582 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001583 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001584 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001585 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001586 if (TYPE(ch) == COMMA) {
1587 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001588 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001589
1590 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1591 ast_error(c, CHILD(n, i),
1592 "bare * has associated type comment");
1593 return NULL;
1594 }
1595
Guido van Rossum4f72a782006-10-27 23:31:49 +00001596 res = handle_keywordonly_args(c, n, i,
1597 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001598 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001599 i = res; /* res has new position to process */
1600 }
1601 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001602 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001603 if (!vararg)
1604 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001605
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001606 i += 2; /* the star and the name */
1607 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1608 i += 1; /* the comma, if present */
1609
1610 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1611 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1612 if (!vararg->type_comment)
1613 return NULL;
1614 i += 1;
1615 }
1616
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001617 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1618 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001619 int res = 0;
1620 res = handle_keywordonly_args(c, n, i,
1621 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001622 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001623 i = res; /* res has new position to process */
1624 }
1625 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 break;
1627 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001628 ch = CHILD(n, i+1); /* tfpdef */
1629 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001630 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001631 if (!kwarg)
1632 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001633 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001634 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001635 i += 1; /* the comma, if present */
1636 break;
1637 case TYPE_COMMENT:
1638 assert(i);
1639
1640 if (kwarg)
1641 arg = kwarg;
1642
1643 /* arg will be equal to the last argument processed */
1644 arg->type_comment = NEW_TYPE_COMMENT(ch);
1645 if (!arg->type_comment)
1646 return NULL;
1647 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 break;
1649 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001650 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 "unexpected node in varargslist: %d @ %d",
1652 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001653 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001654 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001656 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657}
1658
1659static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660ast_for_decorator(struct compiling *c, const node *n)
1661{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001662 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001665 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001666 REQ(CHILD(n, 2), NEWLINE);
1667
1668 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
1670
1671static asdl_seq*
1672ast_for_decorators(struct compiling *c, const node *n)
1673{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001674 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001675 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001679 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 if (!decorator_seq)
1681 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001684 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001685 if (!d)
1686 return NULL;
1687 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 }
1689 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690}
1691
1692static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001693ast_for_funcdef_impl(struct compiling *c, const node *n0,
1694 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001696 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001697 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001698 identifier name;
1699 arguments_ty args;
1700 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001701 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001702 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001703 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001704 node *tc;
1705 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706
Guido van Rossum495da292019-03-07 12:38:08 -08001707 if (is_async && c->c_feature_version < 5) {
1708 ast_error(c, n,
1709 "Async functions are only supported in Python 3.5 and greater");
1710 return NULL;
1711 }
1712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 REQ(n, funcdef);
1714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 name = NEW_IDENTIFIER(CHILD(n, name_i));
1716 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001717 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001718 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1721 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001722 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001723 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1724 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1725 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001726 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001727 name_i += 2;
1728 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001729 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1730 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1731 if (!type_comment)
1732 return NULL;
1733 name_i += 1;
1734 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001735 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001737 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001738 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001740 if (NCH(CHILD(n, name_i + 3)) > 1) {
1741 /* Check if the suite has a type comment in it. */
1742 tc = CHILD(CHILD(n, name_i + 3), 1);
1743
1744 if (TYPE(tc) == TYPE_COMMENT) {
1745 if (type_comment != NULL) {
1746 ast_error(c, n, "Cannot have two type comments on def");
1747 return NULL;
1748 }
1749 type_comment = NEW_TYPE_COMMENT(tc);
1750 if (!type_comment)
1751 return NULL;
1752 }
1753 }
1754
Yury Selivanov75445082015-05-11 22:57:16 -04001755 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001756 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001757 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001758 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001759 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001760 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001761}
1762
1763static stmt_ty
1764ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1765{
Guido van Rossum495da292019-03-07 12:38:08 -08001766 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001767 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001768 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001769 REQ(CHILD(n, 1), funcdef);
1770
guoci90fc8982018-09-11 17:45:45 -04001771 return ast_for_funcdef_impl(c, n, decorator_seq,
1772 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001773}
1774
1775static stmt_ty
1776ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1777{
1778 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1779 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001780 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001781}
1782
1783
1784static stmt_ty
1785ast_for_async_stmt(struct compiling *c, const node *n)
1786{
Guido van Rossum495da292019-03-07 12:38:08 -08001787 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001788 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001789 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001790
1791 switch (TYPE(CHILD(n, 1))) {
1792 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001793 return ast_for_funcdef_impl(c, n, NULL,
1794 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001795 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001796 return ast_for_with_stmt(c, n,
1797 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001798
1799 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001800 return ast_for_for_stmt(c, n,
1801 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001802
1803 default:
1804 PyErr_Format(PyExc_SystemError,
1805 "invalid async stament: %s",
1806 STR(CHILD(n, 1)));
1807 return NULL;
1808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809}
1810
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001811static stmt_ty
1812ast_for_decorated(struct compiling *c, const node *n)
1813{
Yury Selivanov75445082015-05-11 22:57:16 -04001814 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001815 stmt_ty thing = NULL;
1816 asdl_seq *decorator_seq = NULL;
1817
1818 REQ(n, decorated);
1819
1820 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1821 if (!decorator_seq)
1822 return NULL;
1823
1824 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001825 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001827
1828 if (TYPE(CHILD(n, 1)) == funcdef) {
1829 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1830 } else if (TYPE(CHILD(n, 1)) == classdef) {
1831 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001832 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1833 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001834 }
1835 return thing;
1836}
1837
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001839ast_for_namedexpr(struct compiling *c, const node *n)
1840{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001841 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001842 argument: ( test [comp_for] |
1843 test ':=' test |
1844 test '=' test |
1845 '**' test |
1846 '*' test )
1847 */
1848 expr_ty target, value;
1849
1850 target = ast_for_expr(c, CHILD(n, 0));
1851 if (!target)
1852 return NULL;
1853
1854 value = ast_for_expr(c, CHILD(n, 2));
1855 if (!value)
1856 return NULL;
1857
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001858 if (target->kind != Name_kind) {
1859 const char *expr_name = get_expr_name(target);
1860 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001861 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001862 }
1863 return NULL;
1864 }
1865
1866 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001867 return NULL;
1868
1869 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1870 n->n_end_col_offset, c->c_arena);
1871}
1872
1873static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874ast_for_lambdef(struct compiling *c, const node *n)
1875{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001876 /* lambdef: 'lambda' [varargslist] ':' test
1877 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 arguments_ty args;
1879 expr_ty expression;
1880
1881 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001882 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 if (!args)
1884 return NULL;
1885 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 }
1889 else {
1890 args = ast_for_arguments(c, CHILD(n, 1));
1891 if (!args)
1892 return NULL;
1893 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 }
1897
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001898 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1899 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900}
1901
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001902static expr_ty
1903ast_for_ifexpr(struct compiling *c, const node *n)
1904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001906 expr_ty expression, body, orelse;
1907
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001908 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001909 body = ast_for_expr(c, CHILD(n, 0));
1910 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001911 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001912 expression = ast_for_expr(c, CHILD(n, 2));
1913 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001914 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001915 orelse = ast_for_expr(c, CHILD(n, 4));
1916 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001917 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001918 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001919 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001920 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001921}
1922
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001924 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925
Nick Coghlan650f0d02007-04-15 12:05:43 +00001926 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927*/
1928
1929static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001930count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001932 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933
Guido van Rossumd8faa362007-04-27 19:54:29 +00001934 count_comp_for:
1935 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001936 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001937 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001938 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001939 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001940 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001941 else if (NCH(n) == 1) {
1942 n = CHILD(n, 0);
1943 }
1944 else {
1945 goto error;
1946 }
1947 if (NCH(n) == (5)) {
1948 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001949 }
1950 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001951 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001952 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001953 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001954 REQ(n, comp_iter);
1955 n = CHILD(n, 0);
1956 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001957 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001958 else if (TYPE(n) == comp_if) {
1959 if (NCH(n) == 3) {
1960 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001961 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001962 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001963 else
1964 return n_fors;
1965 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001966
Jelle Zijlstraac317702017-10-05 20:24:46 -07001967 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001968 /* Should never be reached */
1969 PyErr_SetString(PyExc_SystemError,
1970 "logic error in count_comp_fors");
1971 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972}
1973
Nick Coghlan650f0d02007-04-15 12:05:43 +00001974/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975
Nick Coghlan650f0d02007-04-15 12:05:43 +00001976 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977*/
1978
1979static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001980count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001982 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983
Guido van Rossumd8faa362007-04-27 19:54:29 +00001984 while (1) {
1985 REQ(n, comp_iter);
1986 if (TYPE(CHILD(n, 0)) == comp_for)
1987 return n_ifs;
1988 n = CHILD(n, 0);
1989 REQ(n, comp_if);
1990 n_ifs++;
1991 if (NCH(n) == 2)
1992 return n_ifs;
1993 n = CHILD(n, 2);
1994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995}
1996
Guido van Rossum992d4a32007-07-11 13:09:30 +00001997static asdl_seq *
1998ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002001 asdl_seq *comps;
2002
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002003 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 if (n_fors == -1)
2005 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002006
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002007 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002008 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002012 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002014 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002015 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002016 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002017 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018
Guido van Rossum992d4a32007-07-11 13:09:30 +00002019 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020
Jelle Zijlstraac317702017-10-05 20:24:46 -07002021 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002022 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002023 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002024 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002025 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002026 else {
2027 sync_n = CHILD(n, 0);
2028 }
2029 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002030
Guido van Rossum495da292019-03-07 12:38:08 -08002031 /* Async comprehensions only allowed in Python 3.6 and greater */
2032 if (is_async && c->c_feature_version < 6) {
2033 ast_error(c, n,
2034 "Async comprehensions are only supported in Python 3.6 and greater");
2035 return NULL;
2036 }
2037
Jelle Zijlstraac317702017-10-05 20:24:46 -07002038 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002039 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002040 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002042 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002043 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002045
Thomas Wouters89f507f2006-12-13 04:49:30 +00002046 /* Check the # of children rather than the length of t, since
2047 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002048 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002049 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002050 comp = comprehension(first, expression, NULL,
2051 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002053 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2054 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2055 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002056 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002057 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002059
Jelle Zijlstraac317702017-10-05 20:24:46 -07002060 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 int j, n_ifs;
2062 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063
Jelle Zijlstraac317702017-10-05 20:24:46 -07002064 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002065 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002066 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002068
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002069 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002074 REQ(n, comp_iter);
2075 n = CHILD(n, 0);
2076 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077
Guido van Rossum992d4a32007-07-11 13:09:30 +00002078 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002080 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002081 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002082 if (NCH(n) == 3)
2083 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002085 /* on exit, must guarantee that n is a comp_for */
2086 if (TYPE(n) == comp_iter)
2087 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002088 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002090 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002092 return comps;
2093}
2094
2095static expr_ty
2096ast_for_itercomp(struct compiling *c, const node *n, int type)
2097{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002098 /* testlist_comp: (test|star_expr)
2099 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002100 expr_ty elt;
2101 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002102 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103
Guido van Rossum992d4a32007-07-11 13:09:30 +00002104 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002106 ch = CHILD(n, 0);
2107 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002108 if (!elt)
2109 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002110 if (elt->kind == Starred_kind) {
2111 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2112 return NULL;
2113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114
Guido van Rossum992d4a32007-07-11 13:09:30 +00002115 comps = ast_for_comprehension(c, CHILD(n, 1));
2116 if (!comps)
2117 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002118
2119 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002120 return GeneratorExp(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_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002123 return ListComp(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 if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002126 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2127 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002128 else
2129 /* Should never happen */
2130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131}
2132
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002133/* Fills in the key, value pair corresponding to the dict element. In case
2134 * of an unpacking, key is NULL. *i is advanced by the number of ast
2135 * elements. Iff successful, nonzero is returned.
2136 */
2137static int
2138ast_for_dictelement(struct compiling *c, const node *n, int *i,
2139 expr_ty *key, expr_ty *value)
2140{
2141 expr_ty expression;
2142 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2143 assert(NCH(n) - *i >= 2);
2144
2145 expression = ast_for_expr(c, CHILD(n, *i + 1));
2146 if (!expression)
2147 return 0;
2148 *key = NULL;
2149 *value = expression;
2150
2151 *i += 2;
2152 }
2153 else {
2154 assert(NCH(n) - *i >= 3);
2155
2156 expression = ast_for_expr(c, CHILD(n, *i));
2157 if (!expression)
2158 return 0;
2159 *key = expression;
2160
2161 REQ(CHILD(n, *i + 1), COLON);
2162
2163 expression = ast_for_expr(c, CHILD(n, *i + 2));
2164 if (!expression)
2165 return 0;
2166 *value = expression;
2167
2168 *i += 3;
2169 }
2170 return 1;
2171}
2172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002174ast_for_dictcomp(struct compiling *c, const node *n)
2175{
2176 expr_ty key, value;
2177 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002178 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002180 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002181 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002182 assert(key);
2183 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002185 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002186 if (!comps)
2187 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002189 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2190 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002191}
2192
2193static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002194ast_for_dictdisplay(struct compiling *c, const node *n)
2195{
2196 int i;
2197 int j;
2198 int size;
2199 asdl_seq *keys, *values;
2200
2201 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2202 keys = _Py_asdl_seq_new(size, c->c_arena);
2203 if (!keys)
2204 return NULL;
2205
2206 values = _Py_asdl_seq_new(size, c->c_arena);
2207 if (!values)
2208 return NULL;
2209
2210 j = 0;
2211 for (i = 0; i < NCH(n); i++) {
2212 expr_ty key, value;
2213
2214 if (!ast_for_dictelement(c, n, &i, &key, &value))
2215 return NULL;
2216 asdl_seq_SET(keys, j, key);
2217 asdl_seq_SET(values, j, value);
2218
2219 j++;
2220 }
2221 keys->size = j;
2222 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002223 return Dict(keys, values, LINENO(n), n->n_col_offset,
2224 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002225}
2226
2227static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002228ast_for_genexp(struct compiling *c, const node *n)
2229{
2230 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002231 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002232}
2233
2234static expr_ty
2235ast_for_listcomp(struct compiling *c, const node *n)
2236{
2237 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002238 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002239}
2240
2241static expr_ty
2242ast_for_setcomp(struct compiling *c, const node *n)
2243{
2244 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002245 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002246}
2247
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002248static expr_ty
2249ast_for_setdisplay(struct compiling *c, const node *n)
2250{
2251 int i;
2252 int size;
2253 asdl_seq *elts;
2254
2255 assert(TYPE(n) == (dictorsetmaker));
2256 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2257 elts = _Py_asdl_seq_new(size, c->c_arena);
2258 if (!elts)
2259 return NULL;
2260 for (i = 0; i < NCH(n); i += 2) {
2261 expr_ty expression;
2262 expression = ast_for_expr(c, CHILD(n, i));
2263 if (!expression)
2264 return NULL;
2265 asdl_seq_SET(elts, i / 2, expression);
2266 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002267 return Set(elts, LINENO(n), n->n_col_offset,
2268 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002269}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002270
2271static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272ast_for_atom(struct compiling *c, const node *n)
2273{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002274 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2275 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002276 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 */
2278 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002281 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002282 PyObject *name;
2283 const char *s = STR(ch);
2284 size_t len = strlen(s);
2285 if (len >= 4 && len <= 5) {
2286 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002287 return Constant(Py_None, 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, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002290 return Constant(Py_True, 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 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002293 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002294 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002295 }
2296 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002297 if (!name)
2298 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002299 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002300 return Name(name, Load, LINENO(n), n->n_col_offset,
2301 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002304 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002305 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002306 const char *errtype = NULL;
2307 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2308 errtype = "unicode error";
2309 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2310 errtype = "value error";
2311 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002312 PyObject *type, *value, *tback, *errstr;
2313 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002314 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002315 if (errstr) {
2316 ast_error(c, n, "(%s) %U", errtype, errstr);
2317 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002318 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002319 else {
2320 PyErr_Clear();
2321 ast_error(c, n, "(%s) unknown error", errtype);
2322 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002323 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002324 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002325 Py_XDECREF(tback);
2326 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002327 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002328 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002329 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 }
2331 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002332 PyObject *pynum;
2333 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2334 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2335 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2336 ast_error(c, ch,
2337 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2338 return NULL;
2339 }
2340 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002341 if (!pynum)
2342 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002343
Victor Stinner43d81952013-07-17 00:57:58 +02002344 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2345 Py_DECREF(pynum);
2346 return NULL;
2347 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002348 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002349 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 }
Georg Brandldde00282007-03-18 19:01:53 +00002351 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002352 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002353 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002355 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356
Thomas Wouters89f507f2006-12-13 04:49:30 +00002357 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002358 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2359 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360
Thomas Wouters89f507f2006-12-13 04:49:30 +00002361 if (TYPE(ch) == yield_expr)
2362 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002365 if (NCH(ch) == 1) {
2366 return ast_for_testlist(c, ch);
2367 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002368
Serhiy Storchakab619b092018-11-27 09:40:29 +02002369 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002370 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002371 }
2372 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002373 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002374 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002376 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377
Thomas Wouters89f507f2006-12-13 04:49:30 +00002378 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002379 return List(NULL, Load, LINENO(n), n->n_col_offset,
2380 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381
Nick Coghlan650f0d02007-04-15 12:05:43 +00002382 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002383 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2384 asdl_seq *elts = seq_for_testlist(c, ch);
2385 if (!elts)
2386 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002387
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002388 return List(elts, Load, LINENO(n), n->n_col_offset,
2389 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002390 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002391 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002392 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002395 /* dictorsetmaker: ( ((test ':' test | '**' test)
2396 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2397 * ((test | '*' test)
2398 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002399 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002400 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002401 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002402 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002403 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2404 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002405 }
2406 else {
2407 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2408 if (NCH(ch) == 1 ||
2409 (NCH(ch) > 1 &&
2410 TYPE(CHILD(ch, 1)) == COMMA)) {
2411 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002412 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002413 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002414 else if (NCH(ch) > 1 &&
2415 TYPE(CHILD(ch, 1)) == comp_for) {
2416 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002417 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002418 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002419 else if (NCH(ch) > 3 - is_dict &&
2420 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2421 /* It's a dictionary comprehension. */
2422 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002423 ast_error(c, n,
2424 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 return NULL;
2426 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002427 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002428 }
2429 else {
2430 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002431 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002432 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002433 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002437 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2438 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 }
2440}
2441
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002442static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443ast_for_slice(struct compiling *c, const node *n)
2444{
2445 node *ch;
2446 expr_ty lower = NULL, upper = NULL, step = NULL;
2447
2448 REQ(n, subscript);
2449
2450 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002451 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 sliceop: ':' [test]
2453 */
2454 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002456 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 }
2458
2459 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002460 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 if (!lower)
2462 return NULL;
2463 }
2464
2465 /* If there's an upper bound it's in the second or third position. */
2466 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002467 if (NCH(n) > 1) {
2468 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469
Thomas Wouters89f507f2006-12-13 04:49:30 +00002470 if (TYPE(n2) == test) {
2471 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 if (!upper)
2473 return NULL;
2474 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002475 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 if (TYPE(n2) == test) {
2480 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 if (!upper)
2482 return NULL;
2483 }
2484 }
2485
2486 ch = CHILD(n, NCH(n) - 1);
2487 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002488 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002489 ch = CHILD(ch, 1);
2490 if (TYPE(ch) == test) {
2491 step = ast_for_expr(c, ch);
2492 if (!step)
2493 return NULL;
2494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
2496 }
2497
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002498 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2499 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500}
2501
2502static expr_ty
2503ast_for_binop(struct compiling *c, const node *n)
2504{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002505 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002507 BinOp(BinOp(A, op, B), op, C).
2508 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509
Guido van Rossumd8faa362007-04-27 19:54:29 +00002510 int i, nops;
2511 expr_ty expr1, expr2, result;
2512 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513
Guido van Rossumd8faa362007-04-27 19:54:29 +00002514 expr1 = ast_for_expr(c, CHILD(n, 0));
2515 if (!expr1)
2516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517
Guido van Rossumd8faa362007-04-27 19:54:29 +00002518 expr2 = ast_for_expr(c, CHILD(n, 2));
2519 if (!expr2)
2520 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521
Guido van Rossum495da292019-03-07 12:38:08 -08002522 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002523 if (!newoperator)
2524 return NULL;
2525
2526 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002527 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002528 c->c_arena);
2529 if (!result)
2530 return NULL;
2531
2532 nops = (NCH(n) - 1) / 2;
2533 for (i = 1; i < nops; i++) {
2534 expr_ty tmp_result, tmp;
2535 const node* next_oper = CHILD(n, i * 2 + 1);
2536
Guido van Rossum495da292019-03-07 12:38:08 -08002537 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002538 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 return NULL;
2540
Guido van Rossumd8faa362007-04-27 19:54:29 +00002541 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2542 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 return NULL;
2544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002546 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002547 CHILD(n, i * 2 + 2)->n_end_lineno,
2548 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002549 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002551 return NULL;
2552 result = tmp_result;
2553 }
2554 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555}
2556
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002557static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002558ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002561 subscriptlist: subscript (',' subscript)* [',']
2562 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2563 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002564 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002565 REQ(n, trailer);
2566 if (TYPE(CHILD(n, 0)) == LPAR) {
2567 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002568 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002569 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002570 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002571 return ast_for_call(c, CHILD(n, 1), left_expr,
2572 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002573 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002574 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002575 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2576 if (!attr_id)
2577 return NULL;
2578 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002579 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002580 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002581 }
2582 else {
2583 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002584 REQ(CHILD(n, 2), RSQB);
2585 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002586 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002587 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002588 if (!slc)
2589 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002590 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002591 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002592 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002593 }
2594 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002595 int j;
2596 expr_ty slc, e;
2597 asdl_seq *elts;
2598 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2599 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002600 return NULL;
2601 for (j = 0; j < NCH(n); j += 2) {
2602 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002603 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002604 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002605 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002606 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002607 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002608 n->n_end_lineno, n->n_end_col_offset,
2609 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002610 if (!e)
2611 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002612 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002613 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002614 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2615 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002616 }
2617 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002618}
2619
2620static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002621ast_for_factor(struct compiling *c, const node *n)
2622{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002623 expr_ty expression;
2624
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002625 expression = ast_for_expr(c, CHILD(n, 1));
2626 if (!expression)
2627 return NULL;
2628
2629 switch (TYPE(CHILD(n, 0))) {
2630 case PLUS:
2631 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002632 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002633 c->c_arena);
2634 case MINUS:
2635 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002636 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002637 c->c_arena);
2638 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002639 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2640 n->n_end_lineno, n->n_end_col_offset,
2641 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002642 }
2643 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2644 TYPE(CHILD(n, 0)));
2645 return NULL;
2646}
2647
2648static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002649ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002650{
Yury Selivanov75445082015-05-11 22:57:16 -04002651 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002652 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002653
2654 REQ(n, atom_expr);
2655 nch = NCH(n);
2656
Guido van Rossum495da292019-03-07 12:38:08 -08002657 if (TYPE(CHILD(n, 0)) == AWAIT) {
2658 if (c->c_feature_version < 5) {
2659 ast_error(c, n,
2660 "Await expressions are only supported in Python 3.5 and greater");
2661 return NULL;
2662 }
Yury Selivanov75445082015-05-11 22:57:16 -04002663 start = 1;
2664 assert(nch > 1);
2665 }
2666
2667 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002668 if (!e)
2669 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002670 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002671 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002672 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002673 return Await(e, LINENO(n), n->n_col_offset,
2674 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002675 }
2676
2677 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002678 node *ch = CHILD(n, i);
2679 if (TYPE(ch) != trailer)
2680 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002681 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2682 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002683 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002684 }
Yury Selivanov75445082015-05-11 22:57:16 -04002685
2686 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002687 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002688 return Await(e, LINENO(n), n->n_col_offset,
2689 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002690 }
2691 else {
2692 return e;
2693 }
2694}
2695
2696static expr_ty
2697ast_for_power(struct compiling *c, const node *n)
2698{
2699 /* power: atom trailer* ('**' factor)*
2700 */
2701 expr_ty e;
2702 REQ(n, power);
2703 e = ast_for_atom_expr(c, CHILD(n, 0));
2704 if (!e)
2705 return NULL;
2706 if (NCH(n) == 1)
2707 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002708 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2709 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002710 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002711 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002712 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2713 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002714 }
2715 return e;
2716}
2717
Guido van Rossum0368b722007-05-11 16:50:42 +00002718static expr_ty
2719ast_for_starred(struct compiling *c, const node *n)
2720{
2721 expr_ty tmp;
2722 REQ(n, star_expr);
2723
2724 tmp = ast_for_expr(c, CHILD(n, 1));
2725 if (!tmp)
2726 return NULL;
2727
2728 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002729 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2730 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002731}
2732
2733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734/* Do not name a variable 'expr'! Will cause a compile error.
2735*/
2736
2737static expr_ty
2738ast_for_expr(struct compiling *c, const node *n)
2739{
2740 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002741 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002742 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002743 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 and_test: not_test ('and' not_test)*
2746 not_test: 'not' not_test | comparison
2747 comparison: expr (comp_op expr)*
2748 expr: xor_expr ('|' xor_expr)*
2749 xor_expr: and_expr ('^' and_expr)*
2750 and_expr: shift_expr ('&' shift_expr)*
2751 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2752 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002753 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002755 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002756 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002757 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 */
2759
2760 asdl_seq *seq;
2761 int i;
2762
2763 loop:
2764 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002765 case namedexpr_test:
2766 if (NCH(n) == 3)
2767 return ast_for_namedexpr(c, n);
2768 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002770 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002771 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002772 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002774 else if (NCH(n) > 1)
2775 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002776 /* Fallthrough */
2777 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 case and_test:
2779 if (NCH(n) == 1) {
2780 n = CHILD(n, 0);
2781 goto loop;
2782 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002783 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 if (!seq)
2785 return NULL;
2786 for (i = 0; i < NCH(n); i += 2) {
2787 expr_ty e = ast_for_expr(c, CHILD(n, i));
2788 if (!e)
2789 return NULL;
2790 asdl_seq_SET(seq, i / 2, e);
2791 }
2792 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002793 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002794 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002795 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002796 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002797 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2798 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 case not_test:
2800 if (NCH(n) == 1) {
2801 n = CHILD(n, 0);
2802 goto loop;
2803 }
2804 else {
2805 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2806 if (!expression)
2807 return NULL;
2808
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002809 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002810 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002811 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 }
2813 case comparison:
2814 if (NCH(n) == 1) {
2815 n = CHILD(n, 0);
2816 goto loop;
2817 }
2818 else {
2819 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002820 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002821 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002822 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!ops)
2824 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002825 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 return NULL;
2828 }
2829 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002830 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002832 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002833 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836
2837 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002838 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002840 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002842 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 asdl_seq_SET(cmps, i / 2, expression);
2844 }
2845 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002846 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002848 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002850 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2851 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853
Guido van Rossum0368b722007-05-11 16:50:42 +00002854 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 /* The next five cases all handle BinOps. The main body of code
2857 is the same in each case, but the switch turned inside out to
2858 reuse the code for each type of operator.
2859 */
2860 case expr:
2861 case xor_expr:
2862 case and_expr:
2863 case shift_expr:
2864 case arith_expr:
2865 case term:
2866 if (NCH(n) == 1) {
2867 n = CHILD(n, 0);
2868 goto loop;
2869 }
2870 return ast_for_binop(c, n);
2871 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002872 node *an = NULL;
2873 node *en = NULL;
2874 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002875 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002876 if (NCH(n) > 1)
2877 an = CHILD(n, 1); /* yield_arg */
2878 if (an) {
2879 en = CHILD(an, NCH(an) - 1);
2880 if (NCH(an) == 2) {
2881 is_from = 1;
2882 exp = ast_for_expr(c, en);
2883 }
2884 else
2885 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 if (!exp)
2887 return NULL;
2888 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002889 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002890 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2891 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2892 return Yield(exp, LINENO(n), n->n_col_offset,
2893 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002894 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002895 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 if (NCH(n) == 1) {
2897 n = CHILD(n, 0);
2898 goto loop;
2899 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002900 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002901 case power:
2902 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002904 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 return NULL;
2906 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002907 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 return NULL;
2909}
2910
2911static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002912ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002913 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914{
2915 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002916 arglist: argument (',' argument)* [',']
2917 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 */
2919
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002920 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002921 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002922 asdl_seq *args;
2923 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924
2925 REQ(n, arglist);
2926
2927 nargs = 0;
2928 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002930 node *ch = CHILD(n, i);
2931 if (TYPE(ch) == argument) {
2932 if (NCH(ch) == 1)
2933 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002934 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2935 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002936 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002937 ast_error(c, ch, "invalid syntax");
2938 return NULL;
2939 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002940 if (NCH(n) > 1) {
2941 ast_error(c, ch, "Generator expression must be parenthesized");
2942 return NULL;
2943 }
2944 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002945 else if (TYPE(CHILD(ch, 0)) == STAR)
2946 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002947 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2948 nargs++;
2949 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002951 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002952 nkeywords++;
2953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002956 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002958 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002959 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002961 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002962
2963 nargs = 0; /* positional arguments + iterable argument unpackings */
2964 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2965 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002967 node *ch = CHILD(n, i);
2968 if (TYPE(ch) == argument) {
2969 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002970 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002971 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002972 /* a positional argument */
2973 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002974 if (ndoublestars) {
2975 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002976 "positional argument follows "
2977 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002978 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002979 else {
2980 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002981 "positional argument follows "
2982 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002983 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002984 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002985 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002986 e = ast_for_expr(c, chch);
2987 if (!e)
2988 return NULL;
2989 asdl_seq_SET(args, nargs++, e);
2990 }
2991 else if (TYPE(chch) == STAR) {
2992 /* an iterable argument unpacking */
2993 expr_ty starred;
2994 if (ndoublestars) {
2995 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002996 "iterable argument unpacking follows "
2997 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002998 return NULL;
2999 }
3000 e = ast_for_expr(c, CHILD(ch, 1));
3001 if (!e)
3002 return NULL;
3003 starred = Starred(e, Load, LINENO(chch),
3004 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003005 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003006 c->c_arena);
3007 if (!starred)
3008 return NULL;
3009 asdl_seq_SET(args, nargs++, starred);
3010
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003011 }
3012 else if (TYPE(chch) == DOUBLESTAR) {
3013 /* a keyword argument unpacking */
3014 keyword_ty kw;
3015 i++;
3016 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003018 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003019 kw = keyword(NULL, e, c->c_arena);
3020 asdl_seq_SET(keywords, nkeywords++, kw);
3021 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003023 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003024 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003025 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003027 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003030 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3031 /* treat colon equal as positional argument */
3032 if (nkeywords) {
3033 if (ndoublestars) {
3034 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003035 "positional argument follows "
3036 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003037 }
3038 else {
3039 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003040 "positional argument follows "
3041 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003042 }
3043 return NULL;
3044 }
3045 e = ast_for_namedexpr(c, ch);
3046 if (!e)
3047 return NULL;
3048 asdl_seq_SET(args, nargs++, e);
3049 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003051 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003052 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003053 identifier key, tmp;
3054 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003056 // To remain LL(1), the grammar accepts any test (basically, any
3057 // expression) in the keyword slot of a call site. So, we need
3058 // to manually enforce that the keyword is a NAME here.
3059 static const int name_tree[] = {
3060 test,
3061 or_test,
3062 and_test,
3063 not_test,
3064 comparison,
3065 expr,
3066 xor_expr,
3067 and_expr,
3068 shift_expr,
3069 arith_expr,
3070 term,
3071 factor,
3072 power,
3073 atom_expr,
3074 atom,
3075 0,
3076 };
3077 node *expr_node = chch;
3078 for (int i = 0; name_tree[i]; i++) {
3079 if (TYPE(expr_node) != name_tree[i])
3080 break;
3081 if (NCH(expr_node) != 1)
3082 break;
3083 expr_node = CHILD(expr_node, 0);
3084 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003085 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003086 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003087 "expression cannot contain assignment, "
3088 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003089 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003090 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003091 key = new_identifier(STR(expr_node), c);
3092 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003093 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003095 if (forbidden_name(c, key, chch, 1)) {
3096 return NULL;
3097 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003098 for (k = 0; k < nkeywords; k++) {
3099 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003100 if (tmp && !PyUnicode_Compare(tmp, key)) {
3101 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003102 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003103 return NULL;
3104 }
3105 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003106 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003108 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003109 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003111 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003112 asdl_seq_SET(keywords, nkeywords++, kw);
3113 }
3114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 }
3116
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003117 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003118 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119}
3120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003122ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003124 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003125 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003127 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003128 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003129 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003130 }
3131 else {
3132 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003133 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003134 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003136 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 else {
3138 asdl_seq *tmp = seq_for_testlist(c, n);
3139 if (!tmp)
3140 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003141 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3142 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003144}
3145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146static stmt_ty
3147ast_for_expr_stmt(struct compiling *c, const node *n)
3148{
3149 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003150 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003151 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3152 annassign: ':' test ['=' (yield_expr|testlist)]
3153 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3154 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3155 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003156 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003158 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003160 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003161 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 if (!e)
3163 return NULL;
3164
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003165 return Expr(e, LINENO(n), n->n_col_offset,
3166 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 }
3168 else if (TYPE(CHILD(n, 1)) == augassign) {
3169 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003170 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003171 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172
Thomas Wouters89f507f2006-12-13 04:49:30 +00003173 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 if (!expr1)
3175 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003176 if(!set_context(c, expr1, Store, ch))
3177 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003178 /* set_context checks that most expressions are not the left side.
3179 Augmented assignments can only have a name, a subscript, or an
3180 attribute on the left, though, so we have to explicitly check for
3181 those. */
3182 switch (expr1->kind) {
3183 case Name_kind:
3184 case Attribute_kind:
3185 case Subscript_kind:
3186 break;
3187 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003188 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003189 return NULL;
3190 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191
Thomas Wouters89f507f2006-12-13 04:49:30 +00003192 ch = CHILD(n, 2);
3193 if (TYPE(ch) == testlist)
3194 expr2 = ast_for_testlist(c, ch);
3195 else
3196 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003197 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 return NULL;
3199
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003200 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003201 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 return NULL;
3203
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003204 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3205 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003207 else if (TYPE(CHILD(n, 1)) == annassign) {
3208 expr_ty expr1, expr2, expr3;
3209 node *ch = CHILD(n, 0);
3210 node *deep, *ann = CHILD(n, 1);
3211 int simple = 1;
3212
Guido van Rossum495da292019-03-07 12:38:08 -08003213 /* AnnAssigns are only allowed in Python 3.6 or greater */
3214 if (c->c_feature_version < 6) {
3215 ast_error(c, ch,
3216 "Variable annotation syntax is only supported in Python 3.6 and greater");
3217 return NULL;
3218 }
3219
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003220 /* we keep track of parens to qualify (x) as expression not name */
3221 deep = ch;
3222 while (NCH(deep) == 1) {
3223 deep = CHILD(deep, 0);
3224 }
3225 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3226 simple = 0;
3227 }
3228 expr1 = ast_for_testlist(c, ch);
3229 if (!expr1) {
3230 return NULL;
3231 }
3232 switch (expr1->kind) {
3233 case Name_kind:
3234 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3235 return NULL;
3236 }
3237 expr1->v.Name.ctx = Store;
3238 break;
3239 case Attribute_kind:
3240 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3241 return NULL;
3242 }
3243 expr1->v.Attribute.ctx = Store;
3244 break;
3245 case Subscript_kind:
3246 expr1->v.Subscript.ctx = Store;
3247 break;
3248 case List_kind:
3249 ast_error(c, ch,
3250 "only single target (not list) can be annotated");
3251 return NULL;
3252 case Tuple_kind:
3253 ast_error(c, ch,
3254 "only single target (not tuple) can be annotated");
3255 return NULL;
3256 default:
3257 ast_error(c, ch,
3258 "illegal target for annotation");
3259 return NULL;
3260 }
3261
3262 if (expr1->kind != Name_kind) {
3263 simple = 0;
3264 }
3265 ch = CHILD(ann, 1);
3266 expr2 = ast_for_expr(c, ch);
3267 if (!expr2) {
3268 return NULL;
3269 }
3270 if (NCH(ann) == 2) {
3271 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003272 LINENO(n), n->n_col_offset,
3273 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003274 }
3275 else {
3276 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003277 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003278 expr3 = ast_for_testlist(c, ch);
3279 }
3280 else {
3281 expr3 = ast_for_expr(c, ch);
3282 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003283 if (!expr3) {
3284 return NULL;
3285 }
3286 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003287 LINENO(n), n->n_col_offset,
3288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003289 }
3290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003292 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003293 asdl_seq *targets;
3294 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003296 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 /* a normal assignment */
3299 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003300
3301 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3302 nch_minus_type = num - has_type_comment;
3303
3304 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 if (!targets)
3306 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003307 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 expr_ty e;
3309 node *ch = CHILD(n, i);
3310 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003311 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 return NULL;
3313 }
3314 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003318 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003319 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 asdl_seq_SET(targets, i / 2, e);
3323 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003324 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003325 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 expression = ast_for_testlist(c, value);
3327 else
3328 expression = ast_for_expr(c, value);
3329 if (!expression)
3330 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003331 if (has_type_comment) {
3332 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3333 if (!type_comment)
3334 return NULL;
3335 }
3336 else
3337 type_comment = NULL;
3338 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003339 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
Benjamin Peterson78565b22009-06-28 19:19:51 +00003343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003345ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346{
3347 asdl_seq *seq;
3348 int i;
3349 expr_ty e;
3350
3351 REQ(n, exprlist);
3352
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003353 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 e = ast_for_expr(c, CHILD(n, i));
3358 if (!e)
3359 return NULL;
3360 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003361 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 }
3364 return seq;
3365}
3366
3367static stmt_ty
3368ast_for_del_stmt(struct compiling *c, const node *n)
3369{
3370 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 /* del_stmt: 'del' exprlist */
3373 REQ(n, del_stmt);
3374
3375 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3376 if (!expr_list)
3377 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003378 return Delete(expr_list, LINENO(n), n->n_col_offset,
3379 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380}
3381
3382static stmt_ty
3383ast_for_flow_stmt(struct compiling *c, const node *n)
3384{
3385 /*
3386 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3387 | yield_stmt
3388 break_stmt: 'break'
3389 continue_stmt: 'continue'
3390 return_stmt: 'return' [testlist]
3391 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003392 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 raise_stmt: 'raise' [test [',' test [',' test]]]
3394 */
3395 node *ch;
3396
3397 REQ(n, flow_stmt);
3398 ch = CHILD(n, 0);
3399 switch (TYPE(ch)) {
3400 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003401 return Break(LINENO(n), n->n_col_offset,
3402 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003404 return Continue(LINENO(n), n->n_col_offset,
3405 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003407 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3408 if (!exp)
3409 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003410 return Expr(exp, LINENO(n), n->n_col_offset,
3411 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 }
3413 case return_stmt:
3414 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003415 return Return(NULL, LINENO(n), n->n_col_offset,
3416 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003418 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 if (!expression)
3420 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003421 return Return(expression, LINENO(n), n->n_col_offset,
3422 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 }
3424 case raise_stmt:
3425 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003426 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3427 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003428 else if (NCH(ch) >= 2) {
3429 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3431 if (!expression)
3432 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003433 if (NCH(ch) == 4) {
3434 cause = ast_for_expr(c, CHILD(ch, 3));
3435 if (!cause)
3436 return NULL;
3437 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003438 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3439 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
Stefan Krahf432a322017-08-21 13:09:59 +02003441 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003443 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 "unexpected flow_stmt: %d", TYPE(ch));
3445 return NULL;
3446 }
3447}
3448
3449static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003450alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451{
3452 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003453 import_as_name: NAME ['as' NAME]
3454 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 dotted_name: NAME ('.' NAME)*
3456 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003457 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 loop:
3460 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003461 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003462 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003463 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003464 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003465 if (!name)
3466 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003467 if (NCH(n) == 3) {
3468 node *str_node = CHILD(n, 2);
3469 str = NEW_IDENTIFIER(str_node);
3470 if (!str)
3471 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003472 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003473 return NULL;
3474 }
3475 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003476 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003477 return NULL;
3478 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003479 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003480 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 case dotted_as_name:
3482 if (NCH(n) == 1) {
3483 n = CHILD(n, 0);
3484 goto loop;
3485 }
3486 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003487 node *asname_node = CHILD(n, 2);
3488 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003489 if (!a)
3490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003492 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003493 if (!a->asname)
3494 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003495 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 return a;
3498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003500 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003501 node *name_node = CHILD(n, 0);
3502 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003503 if (!name)
3504 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003505 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003506 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003507 return alias(name, NULL, c->c_arena);
3508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 else {
3510 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003511 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003512 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515
3516 len = 0;
3517 for (i = 0; i < NCH(n); i += 2)
3518 /* length of string plus one for the dot */
3519 len += strlen(STR(CHILD(n, i))) + 1;
3520 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003521 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 if (!str)
3523 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003524 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 if (!s)
3526 return NULL;
3527 for (i = 0; i < NCH(n); i += 2) {
3528 char *sch = STR(CHILD(n, i));
3529 strcpy(s, STR(CHILD(n, i)));
3530 s += strlen(sch);
3531 *s++ = '.';
3532 }
3533 --s;
3534 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3536 PyBytes_GET_SIZE(str),
3537 NULL);
3538 Py_DECREF(str);
3539 if (!uni)
3540 return NULL;
3541 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003542 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003543 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3544 Py_DECREF(str);
3545 return NULL;
3546 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003547 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003550 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003551 if (!str)
3552 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003553 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3554 Py_DECREF(str);
3555 return NULL;
3556 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003557 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003559 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 "unexpected import name: %d", TYPE(n));
3561 return NULL;
3562 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563}
3564
3565static stmt_ty
3566ast_for_import_stmt(struct compiling *c, const node *n)
3567{
3568 /*
3569 import_stmt: import_name | import_from
3570 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003571 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3572 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003574 int lineno;
3575 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 int i;
3577 asdl_seq *aliases;
3578
3579 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003580 lineno = LINENO(n);
3581 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003583 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003585 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003586 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003587 if (!aliases)
3588 return NULL;
3589 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003590 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003591 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003593 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003595 // Even though n is modified above, the end position is not changed
3596 return Import(aliases, lineno, col_offset,
3597 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003599 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003601 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003602 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003604 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003606 /* Count the number of dots (for relative imports) and check for the
3607 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003608 for (idx = 1; idx < NCH(n); idx++) {
3609 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003610 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3611 if (!mod)
3612 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 idx++;
3614 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003615 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003617 ndots += 3;
3618 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 } else if (TYPE(CHILD(n, idx)) != DOT) {
3620 break;
3621 }
3622 ndots++;
3623 }
3624 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003625 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003626 case STAR:
3627 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003628 n = CHILD(n, idx);
3629 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003630 break;
3631 case LPAR:
3632 /* from ... import (x, y, z) */
3633 n = CHILD(n, idx + 1);
3634 n_children = NCH(n);
3635 break;
3636 case import_as_names:
3637 /* from ... import x, y, z */
3638 n = CHILD(n, idx);
3639 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003640 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003641 ast_error(c, n,
3642 "trailing comma not allowed without"
3643 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 return NULL;
3645 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 break;
3647 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003648 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003649 return NULL;
3650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003652 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003653 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655
3656 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003657 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003658 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003659 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003661 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003663 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003665 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003666 if (!import_alias)
3667 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003668 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003671 if (mod != NULL)
3672 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003673 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003674 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003675 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 }
Neal Norwitz79792652005-11-14 04:25:03 +00003677 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 "unknown import statement: starts with command '%s'",
3679 STR(CHILD(n, 0)));
3680 return NULL;
3681}
3682
3683static stmt_ty
3684ast_for_global_stmt(struct compiling *c, const node *n)
3685{
3686 /* global_stmt: 'global' NAME (',' NAME)* */
3687 identifier name;
3688 asdl_seq *s;
3689 int i;
3690
3691 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003692 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003694 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003696 name = NEW_IDENTIFIER(CHILD(n, i));
3697 if (!name)
3698 return NULL;
3699 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003701 return Global(s, LINENO(n), n->n_col_offset,
3702 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703}
3704
3705static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003706ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3707{
3708 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3709 identifier name;
3710 asdl_seq *s;
3711 int i;
3712
3713 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003714 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003715 if (!s)
3716 return NULL;
3717 for (i = 1; i < NCH(n); i += 2) {
3718 name = NEW_IDENTIFIER(CHILD(n, i));
3719 if (!name)
3720 return NULL;
3721 asdl_seq_SET(s, i / 2, name);
3722 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003723 return Nonlocal(s, LINENO(n), n->n_col_offset,
3724 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003725}
3726
3727static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728ast_for_assert_stmt(struct compiling *c, const node *n)
3729{
3730 /* assert_stmt: 'assert' test [',' test] */
3731 REQ(n, assert_stmt);
3732 if (NCH(n) == 2) {
3733 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3734 if (!expression)
3735 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003736 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3737 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 }
3739 else if (NCH(n) == 4) {
3740 expr_ty expr1, expr2;
3741
3742 expr1 = ast_for_expr(c, CHILD(n, 1));
3743 if (!expr1)
3744 return NULL;
3745 expr2 = ast_for_expr(c, CHILD(n, 3));
3746 if (!expr2)
3747 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003749 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3750 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 }
Neal Norwitz79792652005-11-14 04:25:03 +00003752 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 "improper number of parts to 'assert' statement: %d",
3754 NCH(n));
3755 return NULL;
3756}
3757
3758static asdl_seq *
3759ast_for_suite(struct compiling *c, const node *n)
3760{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003761 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003762 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 stmt_ty s;
3764 int i, total, num, end, pos = 0;
3765 node *ch;
3766
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003767 if (TYPE(n) != func_body_suite) {
3768 REQ(n, suite);
3769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770
3771 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003772 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003776 n = CHILD(n, 0);
3777 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003779 */
3780 end = NCH(n) - 1;
3781 if (TYPE(CHILD(n, end - 1)) == SEMI)
3782 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003784 for (i = 0; i < end; i += 2) {
3785 ch = CHILD(n, i);
3786 s = ast_for_stmt(c, ch);
3787 if (!s)
3788 return NULL;
3789 asdl_seq_SET(seq, pos++, s);
3790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 }
3792 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003793 i = 2;
3794 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3795 i += 2;
3796 REQ(CHILD(n, 2), NEWLINE);
3797 }
3798
3799 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003800 ch = CHILD(n, i);
3801 REQ(ch, stmt);
3802 num = num_stmts(ch);
3803 if (num == 1) {
3804 /* small_stmt or compound_stmt with only one child */
3805 s = ast_for_stmt(c, ch);
3806 if (!s)
3807 return NULL;
3808 asdl_seq_SET(seq, pos++, s);
3809 }
3810 else {
3811 int j;
3812 ch = CHILD(ch, 0);
3813 REQ(ch, simple_stmt);
3814 for (j = 0; j < NCH(ch); j += 2) {
3815 /* statement terminates with a semi-colon ';' */
3816 if (NCH(CHILD(ch, j)) == 0) {
3817 assert((j + 1) == NCH(ch));
3818 break;
3819 }
3820 s = ast_for_stmt(c, CHILD(ch, j));
3821 if (!s)
3822 return NULL;
3823 asdl_seq_SET(seq, pos++, s);
3824 }
3825 }
3826 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 }
3828 assert(pos == seq->size);
3829 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830}
3831
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003832static void
3833get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3834{
Pablo Galindo46a97922019-02-19 22:51:53 +00003835 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003836 // There must be no empty suites.
3837 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003838 stmt_ty last = asdl_seq_GET(s, tot - 1);
3839 *end_lineno = last->end_lineno;
3840 *end_col_offset = last->end_col_offset;
3841}
3842
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843static stmt_ty
3844ast_for_if_stmt(struct compiling *c, const node *n)
3845{
3846 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3847 ['else' ':' suite]
3848 */
3849 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003850 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851
3852 REQ(n, if_stmt);
3853
3854 if (NCH(n) == 4) {
3855 expr_ty expression;
3856 asdl_seq *suite_seq;
3857
3858 expression = ast_for_expr(c, CHILD(n, 1));
3859 if (!expression)
3860 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003862 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003864 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865
Guido van Rossumd8faa362007-04-27 19:54:29 +00003866 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003867 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 s = STR(CHILD(n, 4));
3871 /* s[2], the third character in the string, will be
3872 's' for el_s_e, or
3873 'i' for el_i_f
3874 */
3875 if (s[2] == 's') {
3876 expr_ty expression;
3877 asdl_seq *seq1, *seq2;
3878
3879 expression = ast_for_expr(c, CHILD(n, 1));
3880 if (!expression)
3881 return NULL;
3882 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003883 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 return NULL;
3885 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003886 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003888 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889
Guido van Rossumd8faa362007-04-27 19:54:29 +00003890 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003891 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892 }
3893 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003894 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003895 expr_ty expression;
3896 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003897 asdl_seq *orelse = NULL;
3898 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 /* must reference the child n_elif+1 since 'else' token is third,
3900 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003901 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3902 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3903 has_else = 1;
3904 n_elif -= 3;
3905 }
3906 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907
Thomas Wouters89f507f2006-12-13 04:49:30 +00003908 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003909 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003911 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003912 if (!orelse)
3913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003915 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003917 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3918 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003920 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3921 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003923 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 asdl_seq_SET(orelse, 0,
3926 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003927 LINENO(CHILD(n, NCH(n) - 7)),
3928 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003929 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003930 /* the just-created orelse handled the last elif */
3931 n_elif--;
3932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933
Thomas Wouters89f507f2006-12-13 04:49:30 +00003934 for (i = 0; i < n_elif; i++) {
3935 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003936 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003937 if (!newobj)
3938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003940 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003943 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003946 if (orelse != NULL) {
3947 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3948 } else {
3949 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3950 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003951 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003953 LINENO(CHILD(n, off - 1)),
3954 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003955 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003956 orelse = newobj;
3957 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 expression = ast_for_expr(c, CHILD(n, 1));
3959 if (!expression)
3960 return NULL;
3961 suite_seq = ast_for_suite(c, CHILD(n, 3));
3962 if (!suite_seq)
3963 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003964 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003965 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003966 LINENO(n), n->n_col_offset,
3967 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003969
3970 PyErr_Format(PyExc_SystemError,
3971 "unexpected token in 'if' statement: %s", s);
3972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973}
3974
3975static stmt_ty
3976ast_for_while_stmt(struct compiling *c, const node *n)
3977{
3978 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3979 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003980 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981
3982 if (NCH(n) == 4) {
3983 expr_ty expression;
3984 asdl_seq *suite_seq;
3985
3986 expression = ast_for_expr(c, CHILD(n, 1));
3987 if (!expression)
3988 return NULL;
3989 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003990 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003992 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3993 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3994 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 }
3996 else if (NCH(n) == 7) {
3997 expr_ty expression;
3998 asdl_seq *seq1, *seq2;
3999
4000 expression = ast_for_expr(c, CHILD(n, 1));
4001 if (!expression)
4002 return NULL;
4003 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004004 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 return NULL;
4006 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004007 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004009 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004011 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4012 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004014
4015 PyErr_Format(PyExc_SystemError,
4016 "wrong number of tokens for 'while' statement: %d",
4017 NCH(n));
4018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019}
4020
4021static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004022ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023{
guoci90fc8982018-09-11 17:45:45 -04004024 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004025 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004027 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004028 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004029 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004030 int has_type_comment;
4031 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004032
4033 if (is_async && c->c_feature_version < 5) {
4034 ast_error(c, n,
4035 "Async for loops are only supported in Python 3.5 and greater");
4036 return NULL;
4037 }
4038
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004039 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 REQ(n, for_stmt);
4041
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004042 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4043
4044 if (NCH(n) == 9 + has_type_comment) {
4045 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 if (!seq)
4047 return NULL;
4048 }
4049
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004050 node_target = CHILD(n, 1);
4051 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004052 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004054 /* Check the # of children rather than the length of _target, since
4055 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004056 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004057 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004058 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004060 target = Tuple(_target, Store, first->lineno, first->col_offset,
4061 node_target->n_end_lineno, node_target->n_end_col_offset,
4062 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004064 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004065 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004067 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004068 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 return NULL;
4070
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004071 if (seq != NULL) {
4072 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4073 } else {
4074 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4075 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004076
4077 if (has_type_comment) {
4078 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4079 if (!type_comment)
4080 return NULL;
4081 }
4082 else
4083 type_comment = NULL;
4084
Yury Selivanov75445082015-05-11 22:57:16 -04004085 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004086 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004087 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004088 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004089 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004090 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004091 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004092 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093}
4094
4095static excepthandler_ty
4096ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4097{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004098 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004099 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004100 REQ(exc, except_clause);
4101 REQ(body, suite);
4102
4103 if (NCH(exc) == 1) {
4104 asdl_seq *suite_seq = ast_for_suite(c, body);
4105 if (!suite_seq)
4106 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004107 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108
Neal Norwitzad74aa82008-03-31 05:14:30 +00004109 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004110 exc->n_col_offset,
4111 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112 }
4113 else if (NCH(exc) == 2) {
4114 expr_ty expression;
4115 asdl_seq *suite_seq;
4116
4117 expression = ast_for_expr(c, CHILD(exc, 1));
4118 if (!expression)
4119 return NULL;
4120 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004121 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004123 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124
Neal Norwitzad74aa82008-03-31 05:14:30 +00004125 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004126 exc->n_col_offset,
4127 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 }
4129 else if (NCH(exc) == 4) {
4130 asdl_seq *suite_seq;
4131 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004132 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004133 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004135 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004136 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004138 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 return NULL;
4140 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004141 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004143 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144
Neal Norwitzad74aa82008-03-31 05:14:30 +00004145 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004146 exc->n_col_offset,
4147 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004149
4150 PyErr_Format(PyExc_SystemError,
4151 "wrong number of children for 'except' clause: %d",
4152 NCH(exc));
4153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154}
4155
4156static stmt_ty
4157ast_for_try_stmt(struct compiling *c, const node *n)
4158{
Neal Norwitzf599f422005-12-17 21:33:47 +00004159 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004161 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004162 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004164 REQ(n, try_stmt);
4165
Neal Norwitzf599f422005-12-17 21:33:47 +00004166 body = ast_for_suite(c, CHILD(n, 2));
4167 if (body == NULL)
4168 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004169
Neal Norwitzf599f422005-12-17 21:33:47 +00004170 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4171 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4172 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4173 /* we can assume it's an "else",
4174 because nch >= 9 for try-else-finally and
4175 it would otherwise have a type of except_clause */
4176 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4177 if (orelse == NULL)
4178 return NULL;
4179 n_except--;
4180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181
Neal Norwitzf599f422005-12-17 21:33:47 +00004182 finally = ast_for_suite(c, CHILD(n, nch - 1));
4183 if (finally == NULL)
4184 return NULL;
4185 n_except--;
4186 }
4187 else {
4188 /* we can assume it's an "else",
4189 otherwise it would have a type of except_clause */
4190 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4191 if (orelse == NULL)
4192 return NULL;
4193 n_except--;
4194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004196 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004197 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198 return NULL;
4199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200
Neal Norwitzf599f422005-12-17 21:33:47 +00004201 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004202 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004203 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004204 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004205 if (handlers == NULL)
4206 return NULL;
4207
4208 for (i = 0; i < n_except; i++) {
4209 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4210 CHILD(n, 5 + i * 3));
4211 if (!e)
4212 return NULL;
4213 asdl_seq_SET(handlers, i, e);
4214 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004215 }
4216
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004217 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004218 if (finally != NULL) {
4219 // finally is always last
4220 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4221 } else if (orelse != NULL) {
4222 // otherwise else is last
4223 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4224 } else {
4225 // inline the get_last_end_pos logic due to layout mismatch
4226 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4227 end_lineno = last_handler->end_lineno;
4228 end_col_offset = last_handler->end_col_offset;
4229 }
4230 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4231 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232}
4233
Georg Brandl0c315622009-05-25 21:10:36 +00004234/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004235static withitem_ty
4236ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004237{
4238 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004239
Georg Brandl0c315622009-05-25 21:10:36 +00004240 REQ(n, with_item);
4241 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004242 if (!context_expr)
4243 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004244 if (NCH(n) == 3) {
4245 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004246
4247 if (!optional_vars) {
4248 return NULL;
4249 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004250 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004251 return NULL;
4252 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004253 }
4254
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004255 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004256}
4257
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004258/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004259static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004260ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004261{
guoci90fc8982018-09-11 17:45:45 -04004262 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004263 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004264 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004265 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004266
Guido van Rossum495da292019-03-07 12:38:08 -08004267 if (is_async && c->c_feature_version < 5) {
4268 ast_error(c, n,
4269 "Async with statements are only supported in Python 3.5 and greater");
4270 return NULL;
4271 }
4272
Georg Brandl0c315622009-05-25 21:10:36 +00004273 REQ(n, with_stmt);
4274
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004275 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4276 nch_minus_type = NCH(n) - has_type_comment;
4277
4278 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004279 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004280 if (!items)
4281 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004282 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004283 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4284 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004285 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004286 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004287 }
4288
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004289 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4290 if (!body)
4291 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004292 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004293
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004294 if (has_type_comment) {
4295 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4296 if (!type_comment)
4297 return NULL;
4298 }
4299 else
4300 type_comment = NULL;
4301
Yury Selivanov75445082015-05-11 22:57:16 -04004302 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004303 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004304 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004305 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004306 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004307 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004308}
4309
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004310static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004311ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004312{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004313 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004314 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004315 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004316 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004317 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004318
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004319 REQ(n, classdef);
4320
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004321 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004322 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004323 if (!s)
4324 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004325 get_last_end_pos(s, &end_lineno, &end_col_offset);
4326
Benjamin Peterson30760062008-11-25 04:02:28 +00004327 classname = NEW_IDENTIFIER(CHILD(n, 1));
4328 if (!classname)
4329 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004330 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004331 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004332 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004333 LINENO(n), n->n_col_offset,
4334 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004335 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004336
4337 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004338 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004339 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004340 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004341 get_last_end_pos(s, &end_lineno, &end_col_offset);
4342
Benjamin Peterson30760062008-11-25 04:02:28 +00004343 classname = NEW_IDENTIFIER(CHILD(n, 1));
4344 if (!classname)
4345 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004346 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004347 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004348 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004349 LINENO(n), n->n_col_offset,
4350 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004351 }
4352
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004353 /* class NAME '(' arglist ')' ':' suite */
4354 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004355 {
4356 PyObject *dummy_name;
4357 expr_ty dummy;
4358 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4359 if (!dummy_name)
4360 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004361 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4362 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4363 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004364 call = ast_for_call(c, CHILD(n, 3), dummy,
4365 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004366 if (!call)
4367 return NULL;
4368 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004369 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004370 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004371 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004372 get_last_end_pos(s, &end_lineno, &end_col_offset);
4373
Benjamin Peterson30760062008-11-25 04:02:28 +00004374 classname = NEW_IDENTIFIER(CHILD(n, 1));
4375 if (!classname)
4376 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004377 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004378 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004379
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004380 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004381 decorator_seq, LINENO(n), n->n_col_offset,
4382 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004383}
4384
4385static stmt_ty
4386ast_for_stmt(struct compiling *c, const node *n)
4387{
4388 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004389 assert(NCH(n) == 1);
4390 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004391 }
4392 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004393 assert(num_stmts(n) == 1);
4394 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004395 }
4396 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004397 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004398 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4399 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004400 */
4401 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402 case expr_stmt:
4403 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004404 case del_stmt:
4405 return ast_for_del_stmt(c, n);
4406 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004407 return Pass(LINENO(n), n->n_col_offset,
4408 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004409 case flow_stmt:
4410 return ast_for_flow_stmt(c, n);
4411 case import_stmt:
4412 return ast_for_import_stmt(c, n);
4413 case global_stmt:
4414 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004415 case nonlocal_stmt:
4416 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004417 case assert_stmt:
4418 return ast_for_assert_stmt(c, n);
4419 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004420 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4422 TYPE(n), NCH(n));
4423 return NULL;
4424 }
4425 }
4426 else {
4427 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004428 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004429 */
4430 node *ch = CHILD(n, 0);
4431 REQ(n, compound_stmt);
4432 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433 case if_stmt:
4434 return ast_for_if_stmt(c, ch);
4435 case while_stmt:
4436 return ast_for_while_stmt(c, ch);
4437 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004438 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004439 case try_stmt:
4440 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004441 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004442 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004443 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004444 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004446 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 case decorated:
4448 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004449 case async_stmt:
4450 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004452 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004453 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454 TYPE(n), NCH(n));
4455 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004456 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004457 }
4458}
4459
4460static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004461parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004463 const char *end;
4464 long x;
4465 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004466 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004467 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004468
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004469 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004470 errno = 0;
4471 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004472 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004473 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004474 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004475 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004476 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004477 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004478 }
4479 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004480 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004481 if (*end == '\0') {
4482 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004483 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004484 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004485 }
4486 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004487 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004488 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004489 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4490 if (compl.imag == -1.0 && PyErr_Occurred())
4491 return NULL;
4492 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004493 }
4494 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004495 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004496 dx = PyOS_string_to_double(s, NULL, NULL);
4497 if (dx == -1.0 && PyErr_Occurred())
4498 return NULL;
4499 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004501}
4502
4503static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004504parsenumber(struct compiling *c, const char *s)
4505{
4506 char *dup, *end;
4507 PyObject *res = NULL;
4508
4509 assert(s != NULL);
4510
4511 if (strchr(s, '_') == NULL) {
4512 return parsenumber_raw(c, s);
4513 }
4514 /* Create a duplicate without underscores. */
4515 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004516 if (dup == NULL) {
4517 return PyErr_NoMemory();
4518 }
Brett Cannona721aba2016-09-09 14:57:09 -07004519 end = dup;
4520 for (; *s; s++) {
4521 if (*s != '_') {
4522 *end++ = *s;
4523 }
4524 }
4525 *end = '\0';
4526 res = parsenumber_raw(c, dup);
4527 PyMem_Free(dup);
4528 return res;
4529}
4530
4531static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004532decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004533{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004534 const char *s, *t;
4535 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004536 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4537 while (s < end && (*s & 0x80)) s++;
4538 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004539 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540}
4541
Eric V. Smith56466482016-10-31 14:46:26 -04004542static int
4543warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004544 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004545{
4546 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4547 first_invalid_escape_char);
4548 if (msg == NULL) {
4549 return -1;
4550 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004551 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004552 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004553 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004554 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004555 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4556 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004557 to get a more accurate error report */
4558 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004559 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004560 }
4561 Py_DECREF(msg);
4562 return -1;
4563 }
4564 Py_DECREF(msg);
4565 return 0;
4566}
4567
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004568static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004569decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4570 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004571{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004572 PyObject *v, *u;
4573 char *buf;
4574 char *p;
4575 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004576
Benjamin Peterson202803a2016-02-25 22:34:45 -08004577 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004578 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004579 return NULL;
4580 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4581 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4582 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4583 if (u == NULL)
4584 return NULL;
4585 p = buf = PyBytes_AsString(u);
4586 end = s + len;
4587 while (s < end) {
4588 if (*s == '\\') {
4589 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004590 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004591 strcpy(p, "u005c");
4592 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004593 if (s >= end)
4594 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004595 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004596 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004597 if (*s & 0x80) { /* XXX inefficient */
4598 PyObject *w;
4599 int kind;
4600 void *data;
4601 Py_ssize_t len, i;
4602 w = decode_utf8(c, &s, end);
4603 if (w == NULL) {
4604 Py_DECREF(u);
4605 return NULL;
4606 }
4607 kind = PyUnicode_KIND(w);
4608 data = PyUnicode_DATA(w);
4609 len = PyUnicode_GET_LENGTH(w);
4610 for (i = 0; i < len; i++) {
4611 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4612 sprintf(p, "\\U%08x", chr);
4613 p += 10;
4614 }
4615 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004616 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004617 Py_DECREF(w);
4618 } else {
4619 *p++ = *s++;
4620 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004621 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004622 len = p - buf;
4623 s = buf;
4624
Eric V. Smith56466482016-10-31 14:46:26 -04004625 const char *first_invalid_escape;
4626 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4627
4628 if (v != NULL && first_invalid_escape != NULL) {
4629 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4630 /* We have not decref u before because first_invalid_escape points
4631 inside u. */
4632 Py_XDECREF(u);
4633 Py_DECREF(v);
4634 return NULL;
4635 }
4636 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004637 Py_XDECREF(u);
4638 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004639}
4640
Eric V. Smith56466482016-10-31 14:46:26 -04004641static PyObject *
4642decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4643 size_t len)
4644{
4645 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004646 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004647 &first_invalid_escape);
4648 if (result == NULL)
4649 return NULL;
4650
4651 if (first_invalid_escape != NULL) {
4652 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4653 Py_DECREF(result);
4654 return NULL;
4655 }
4656 }
4657 return result;
4658}
4659
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004660/* Shift locations for the given node and all its children by adding `lineno`
4661 and `col_offset` to existing locations. */
4662static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4663{
4664 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004665 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004666 for (int i = 0; i < NCH(n); ++i) {
4667 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4668 /* Shifting column offsets unnecessary if there's been newlines. */
4669 col_offset = 0;
4670 }
4671 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4672 }
4673 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004674 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004675}
4676
4677/* Fix locations for the given node and its children.
4678
4679 `parent` is the enclosing node.
4680 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004681 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004682*/
4683static void
4684fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4685{
4686 char *substr = NULL;
4687 char *start;
4688 int lines = LINENO(parent) - 1;
4689 int cols = parent->n_col_offset;
4690 /* Find the full fstring to fix location information in `n`. */
4691 while (parent && parent->n_type != STRING)
4692 parent = parent->n_child;
4693 if (parent && parent->n_str) {
4694 substr = strstr(parent->n_str, expr_str);
4695 if (substr) {
4696 start = substr;
4697 while (start > parent->n_str) {
4698 if (start[0] == '\n')
4699 break;
4700 start--;
4701 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004702 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004703 /* adjust the start based on the number of newlines encountered
4704 before the f-string expression */
4705 for (char* p = parent->n_str; p < substr; p++) {
4706 if (*p == '\n') {
4707 lines++;
4708 }
4709 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004710 }
4711 }
4712 fstring_shift_node_locations(n, lines, cols);
4713}
4714
Eric V. Smith451d0e32016-09-09 21:56:20 -04004715/* Compile this expression in to an expr_ty. Add parens around the
4716 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004717static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004718fstring_compile_expr(const char *expr_start, const char *expr_end,
4719 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004720
Eric V. Smith235a6f02015-09-19 14:51:32 -04004721{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004722 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004723 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004724 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004725 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004726 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004727
Eric V. Smith1d44c412015-09-23 07:49:00 -04004728 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004729 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004730 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4731 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004732
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004733 /* If the substring is all whitespace, it's an error. We need to catch this
4734 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4735 because turning the expression '' in to '()' would go from being invalid
4736 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004737 for (s = expr_start; s != expr_end; s++) {
4738 char c = *s;
4739 /* The Python parser ignores only the following whitespace
4740 characters (\r already is converted to \n). */
4741 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742 break;
4743 }
4744 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004745 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004746 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004747 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004748 }
4749
Eric V. Smith451d0e32016-09-09 21:56:20 -04004750 len = expr_end - expr_start;
4751 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4752 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004753 if (str == NULL) {
4754 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004755 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004756 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004757
Eric V. Smith451d0e32016-09-09 21:56:20 -04004758 str[0] = '(';
4759 memcpy(str+1, expr_start, len);
4760 str[len+1] = ')';
4761 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004762
Victor Stinner37d66d72019-06-13 02:16:41 +02004763 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004764 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004765 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4766 Py_eval_input, 0);
4767 if (!mod_n) {
4768 PyMem_RawFree(str);
4769 return NULL;
4770 }
4771 /* Reuse str to find the correct column offset. */
4772 str[0] = '{';
4773 str[len+1] = '}';
4774 fstring_fix_node_location(n, mod_n, str);
4775 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004776 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004777 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004778 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004779 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004780 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004781}
4782
4783/* Return -1 on error.
4784
4785 Return 0 if we reached the end of the literal.
4786
4787 Return 1 if we haven't reached the end of the literal, but we want
4788 the caller to process the literal up to this point. Used for
4789 doubled braces.
4790*/
4791static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004792fstring_find_literal(const char **str, const char *end, int raw,
4793 PyObject **literal, int recurse_lvl,
4794 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004795{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004796 /* Get any literal string. It ends when we hit an un-doubled left
4797 brace (which isn't part of a unicode name escape such as
4798 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004799
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004800 const char *s = *str;
4801 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004802 int result = 0;
4803
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004805 while (s < end) {
4806 char ch = *s++;
4807 if (!raw && ch == '\\' && s < end) {
4808 ch = *s++;
4809 if (ch == 'N') {
4810 if (s < end && *s++ == '{') {
4811 while (s < end && *s++ != '}') {
4812 }
4813 continue;
4814 }
4815 break;
4816 }
4817 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4818 return -1;
4819 }
4820 }
4821 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004822 /* Check for doubled braces, but only at the top level. If
4823 we checked at every level, then f'{0:{3}}' would fail
4824 with the two closing braces. */
4825 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004826 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004827 /* We're going to tell the caller that the literal ends
4828 here, but that they should continue scanning. But also
4829 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004830 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004831 result = 1;
4832 goto done;
4833 }
4834
4835 /* Where a single '{' is the start of a new expression, a
4836 single '}' is not allowed. */
4837 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004838 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004839 ast_error(c, n, "f-string: single '}' is not allowed");
4840 return -1;
4841 }
4842 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843 /* We're either at a '{', which means we're starting another
4844 expression; or a '}', which means we're at the end of this
4845 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004846 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004847 break;
4848 }
4849 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004850 *str = s;
4851 assert(s <= end);
4852 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004853done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004854 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004855 if (raw)
4856 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004857 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004858 NULL, NULL);
4859 else
Eric V. Smith56466482016-10-31 14:46:26 -04004860 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004861 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004862 if (!*literal)
4863 return -1;
4864 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865 return result;
4866}
4867
4868/* Forward declaration because parsing is recursive. */
4869static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004870fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871 struct compiling *c, const node *n);
4872
Eric V. Smith451d0e32016-09-09 21:56:20 -04004873/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004874 expression (so it must be a '{'). Returns the FormattedValue node, which
4875 includes the expression, conversion character, format_spec expression, and
4876 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004877
4878 Note that I don't do a perfect job here: I don't make sure that a
4879 closing brace doesn't match an opening paren, for example. It
4880 doesn't need to error on all invalid expressions, just correctly
4881 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004882 will be caught when we parse it later.
4883
4884 *expression is set to the expression. For an '=' "debug" expression,
4885 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004886 including the '=' and any whitespace around it, as a string object). If
4887 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004889fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004890 PyObject **expr_text, expr_ty *expression,
4891 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892{
4893 /* Return -1 on error, else 0. */
4894
Eric V. Smith451d0e32016-09-09 21:56:20 -04004895 const char *expr_start;
4896 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 expr_ty simple_expression;
4898 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004899 int conversion = -1; /* The conversion char. Use default if not
4900 specified, or !r if using = and no format
4901 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004902
4903 /* 0 if we're not in a string, else the quote char we're trying to
4904 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906
4907 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4908 int string_type = 0;
4909
4910 /* Keep track of nesting level for braces/parens/brackets in
4911 expressions. */
4912 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004913 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004915 *expr_text = NULL;
4916
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917 /* Can only nest one level deep. */
4918 if (recurse_lvl >= 2) {
4919 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004920 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921 }
4922
4923 /* The first char must be a left brace, or we wouldn't have gotten
4924 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925 assert(**str == '{');
4926 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927
Eric V. Smith451d0e32016-09-09 21:56:20 -04004928 expr_start = *str;
4929 for (; *str < end; (*str)++) {
4930 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931
4932 /* Loop invariants. */
4933 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004934 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004935 if (quote_char)
4936 assert(string_type == 1 || string_type == 3);
4937 else
4938 assert(string_type == 0);
4939
Eric V. Smith451d0e32016-09-09 21:56:20 -04004940 ch = **str;
4941 /* Nowhere inside an expression is a backslash allowed. */
4942 if (ch == '\\') {
4943 /* Error: can't include a backslash character, inside
4944 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004945 ast_error(c, n,
4946 "f-string expression part "
4947 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004948 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004949 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950 if (quote_char) {
4951 /* We're inside a string. See if we're at the end. */
4952 /* This code needs to implement the same non-error logic
4953 as tok_get from tokenizer.c, at the letter_quote
4954 label. To actually share that code would be a
4955 nightmare. But, it's unlikely to change and is small,
4956 so duplicate it here. Note we don't need to catch all
4957 of the errors, since they'll be caught when parsing the
4958 expression. We just need to match the non-error
4959 cases. Thus we can ignore \n in single-quoted strings,
4960 for example. Or non-terminated strings. */
4961 if (ch == quote_char) {
4962 /* Does this match the string_type (single or triple
4963 quoted)? */
4964 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004965 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004967 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004968 string_type = 0;
4969 quote_char = 0;
4970 continue;
4971 }
4972 } else {
4973 /* We're at the end of a normal string. */
4974 quote_char = 0;
4975 string_type = 0;
4976 continue;
4977 }
4978 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 } else if (ch == '\'' || ch == '"') {
4980 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004981 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 } else {
4985 /* Start of a normal string. */
4986 string_type = 1;
4987 }
4988 /* Start looking for the end of the string. */
4989 quote_char = ch;
4990 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004991 if (nested_depth >= MAXLEVEL) {
4992 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004993 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004994 }
4995 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 } else if (ch == '#') {
4998 /* Error: can't include a comment character, inside parens
4999 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005000 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005001 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005002 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005003 (ch == '!' || ch == ':' || ch == '}' ||
5004 ch == '=' || ch == '>' || ch == '<')) {
5005 /* See if there's a next character. */
5006 if (*str+1 < end) {
5007 char next = *(*str+1);
5008
5009 /* For "!=". since '=' is not an allowed conversion character,
5010 nothing is lost in this test. */
5011 if ((ch == '!' && next == '=') || /* != */
5012 (ch == '=' && next == '=') || /* == */
5013 (ch == '<' && next == '=') || /* <= */
5014 (ch == '>' && next == '=') /* >= */
5015 ) {
5016 *str += 1;
5017 continue;
5018 }
5019 /* Don't get out of the loop for these, if they're single
5020 chars (not part of 2-char tokens). If by themselves, they
5021 don't end an expression (unlike say '!'). */
5022 if (ch == '>' || ch == '<') {
5023 continue;
5024 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005025 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005026
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 /* Normal way out of this loop. */
5028 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005029 } else if (ch == ']' || ch == '}' || ch == ')') {
5030 if (!nested_depth) {
5031 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005032 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005033 }
5034 nested_depth--;
5035 int opening = parenstack[nested_depth];
5036 if (!((opening == '(' && ch == ')') ||
5037 (opening == '[' && ch == ']') ||
5038 (opening == '{' && ch == '}')))
5039 {
5040 ast_error(c, n,
5041 "f-string: closing parenthesis '%c' "
5042 "does not match opening parenthesis '%c'",
5043 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005044 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005045 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 } else {
5047 /* Just consume this char and loop around. */
5048 }
5049 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 /* If we leave this loop in a string or with mismatched parens, we
5052 don't care. We'll get a syntax error when compiling the
5053 expression. But, we can produce a better error message, so
5054 let's just do that.*/
5055 if (quote_char) {
5056 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005057 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058 }
5059 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005060 int opening = parenstack[nested_depth - 1];
5061 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005062 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 }
5064
Eric V. Smith451d0e32016-09-09 21:56:20 -04005065 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005066 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005067
5068 /* Compile the expression as soon as possible, so we show errors
5069 related to the expression before errors related to the
5070 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005071 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005072 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005073 goto error;
5074
5075 /* Check for =, which puts the text value of the expression in
5076 expr_text. */
5077 if (**str == '=') {
5078 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005079
5080 /* Skip over ASCII whitespace. No need to test for end of string
5081 here, since we know there's at least a trailing quote somewhere
5082 ahead. */
5083 while (Py_ISSPACE(**str)) {
5084 *str += 1;
5085 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005086
5087 /* Set *expr_text to the text of the expression. */
5088 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5089 if (!*expr_text) {
5090 goto error;
5091 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005092 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005093
5094 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005095 if (**str == '!') {
5096 *str += 1;
5097 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005098 goto unexpected_end_of_string;
5099
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 conversion = **str;
5101 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005102
5103 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005104 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005105 ast_error(c, n,
5106 "f-string: invalid conversion character: "
5107 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005108 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005110
5111 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112
5113 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005114 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 if (**str == ':') {
5117 *str += 1;
5118 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 goto unexpected_end_of_string;
5120
5121 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005124 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125 }
5126
Eric V. Smith451d0e32016-09-09 21:56:20 -04005127 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005128 goto unexpected_end_of_string;
5129
5130 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 assert(*str < end);
5132 assert(**str == '}');
5133 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005135 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005136 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005137 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005138 conversion = 'r';
5139 }
5140
Eric V. Smith451d0e32016-09-09 21:56:20 -04005141 /* And now create the FormattedValue node that represents this
5142 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005143 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005144 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005145 n->n_col_offset, n->n_end_lineno,
5146 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005148 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149
5150 return 0;
5151
5152unexpected_end_of_string:
5153 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005154 /* Falls through to error. */
5155
5156error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005157 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005159
Eric V. Smith235a6f02015-09-19 14:51:32 -04005160}
5161
5162/* Return -1 on error.
5163
5164 Return 0 if we have a literal (possible zero length) and an
5165 expression (zero length if at the end of the string.
5166
5167 Return 1 if we have a literal, but no expression, and we want the
5168 caller to call us again. This is used to deal with doubled
5169 braces.
5170
5171 When called multiple times on the string 'a{{b{0}c', this function
5172 will return:
5173
5174 1. the literal 'a{' with no expression, and a return value
5175 of 1. Despite the fact that there's no expression, the return
5176 value of 1 means we're not finished yet.
5177
5178 2. the literal 'b' and the expression '0', with a return value of
5179 0. The fact that there's an expression means we're not finished.
5180
5181 3. literal 'c' with no expression and a return value of 0. The
5182 combination of the return value of 0 with no expression means
5183 we're finished.
5184*/
5185static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5187 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005188 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005189 struct compiling *c, const node *n)
5190{
5191 int result;
5192
5193 assert(*literal == NULL && *expression == NULL);
5194
5195 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005196 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005197 if (result < 0)
5198 goto error;
5199
5200 assert(result == 0 || result == 1);
5201
5202 if (result == 1)
5203 /* We have a literal, but don't look at the expression. */
5204 return 1;
5205
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005207 /* We're at the end of the string or the end of a nested
5208 f-string: no expression. The top-level error case where we
5209 expect to be at the end of the string but we're at a '}' is
5210 handled later. */
5211 return 0;
5212
5213 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005214 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005216 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5217 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005218 goto error;
5219
5220 return 0;
5221
5222error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005223 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005224 return -1;
5225}
5226
5227#define EXPRLIST_N_CACHED 64
5228
5229typedef struct {
5230 /* Incrementally build an array of expr_ty, so be used in an
5231 asdl_seq. Cache some small but reasonably sized number of
5232 expr_ty's, and then after that start dynamically allocating,
5233 doubling the number allocated each time. Note that the f-string
5234 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005235 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005236 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005237
5238 Py_ssize_t allocated; /* Number we've allocated. */
5239 Py_ssize_t size; /* Number we've used. */
5240 expr_ty *p; /* Pointer to the memory we're actually
5241 using. Will point to 'data' until we
5242 start dynamically allocating. */
5243 expr_ty data[EXPRLIST_N_CACHED];
5244} ExprList;
5245
5246#ifdef NDEBUG
5247#define ExprList_check_invariants(l)
5248#else
5249static void
5250ExprList_check_invariants(ExprList *l)
5251{
5252 /* Check our invariants. Make sure this object is "live", and
5253 hasn't been deallocated. */
5254 assert(l->size >= 0);
5255 assert(l->p != NULL);
5256 if (l->size <= EXPRLIST_N_CACHED)
5257 assert(l->data == l->p);
5258}
5259#endif
5260
5261static void
5262ExprList_Init(ExprList *l)
5263{
5264 l->allocated = EXPRLIST_N_CACHED;
5265 l->size = 0;
5266
5267 /* Until we start allocating dynamically, p points to data. */
5268 l->p = l->data;
5269
5270 ExprList_check_invariants(l);
5271}
5272
5273static int
5274ExprList_Append(ExprList *l, expr_ty exp)
5275{
5276 ExprList_check_invariants(l);
5277 if (l->size >= l->allocated) {
5278 /* We need to alloc (or realloc) the memory. */
5279 Py_ssize_t new_size = l->allocated * 2;
5280
5281 /* See if we've ever allocated anything dynamically. */
5282 if (l->p == l->data) {
5283 Py_ssize_t i;
5284 /* We're still using the cached data. Switch to
5285 alloc-ing. */
5286 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5287 if (!l->p)
5288 return -1;
5289 /* Copy the cached data into the new buffer. */
5290 for (i = 0; i < l->size; i++)
5291 l->p[i] = l->data[i];
5292 } else {
5293 /* Just realloc. */
5294 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5295 if (!tmp) {
5296 PyMem_RawFree(l->p);
5297 l->p = NULL;
5298 return -1;
5299 }
5300 l->p = tmp;
5301 }
5302
5303 l->allocated = new_size;
5304 assert(l->allocated == 2 * l->size);
5305 }
5306
5307 l->p[l->size++] = exp;
5308
5309 ExprList_check_invariants(l);
5310 return 0;
5311}
5312
5313static void
5314ExprList_Dealloc(ExprList *l)
5315{
5316 ExprList_check_invariants(l);
5317
5318 /* If there's been an error, or we've never dynamically allocated,
5319 do nothing. */
5320 if (!l->p || l->p == l->data) {
5321 /* Do nothing. */
5322 } else {
5323 /* We have dynamically allocated. Free the memory. */
5324 PyMem_RawFree(l->p);
5325 }
5326 l->p = NULL;
5327 l->size = -1;
5328}
5329
5330static asdl_seq *
5331ExprList_Finish(ExprList *l, PyArena *arena)
5332{
5333 asdl_seq *seq;
5334
5335 ExprList_check_invariants(l);
5336
5337 /* Allocate the asdl_seq and copy the expressions in to it. */
5338 seq = _Py_asdl_seq_new(l->size, arena);
5339 if (seq) {
5340 Py_ssize_t i;
5341 for (i = 0; i < l->size; i++)
5342 asdl_seq_SET(seq, i, l->p[i]);
5343 }
5344 ExprList_Dealloc(l);
5345 return seq;
5346}
5347
5348/* The FstringParser is designed to add a mix of strings and
5349 f-strings, and concat them together as needed. Ultimately, it
5350 generates an expr_ty. */
5351typedef struct {
5352 PyObject *last_str;
5353 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005354 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005355} FstringParser;
5356
5357#ifdef NDEBUG
5358#define FstringParser_check_invariants(state)
5359#else
5360static void
5361FstringParser_check_invariants(FstringParser *state)
5362{
5363 if (state->last_str)
5364 assert(PyUnicode_CheckExact(state->last_str));
5365 ExprList_check_invariants(&state->expr_list);
5366}
5367#endif
5368
5369static void
5370FstringParser_Init(FstringParser *state)
5371{
5372 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005373 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005374 ExprList_Init(&state->expr_list);
5375 FstringParser_check_invariants(state);
5376}
5377
5378static void
5379FstringParser_Dealloc(FstringParser *state)
5380{
5381 FstringParser_check_invariants(state);
5382
5383 Py_XDECREF(state->last_str);
5384 ExprList_Dealloc(&state->expr_list);
5385}
5386
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005387/* Constants for the following */
5388static PyObject *u_kind;
5389
5390/* Compute 'kind' field for string Constant (either 'u' or None) */
5391static PyObject *
5392make_kind(struct compiling *c, const node *n)
5393{
5394 char *s = NULL;
5395 PyObject *kind = NULL;
5396
5397 /* Find the first string literal, if any */
5398 while (TYPE(n) != STRING) {
5399 if (NCH(n) == 0)
5400 return NULL;
5401 n = CHILD(n, 0);
5402 }
5403 REQ(n, STRING);
5404
5405 /* If it starts with 'u', return a PyUnicode "u" string */
5406 s = STR(n);
5407 if (s && *s == 'u') {
5408 if (!u_kind) {
5409 u_kind = PyUnicode_InternFromString("u");
5410 if (!u_kind)
5411 return NULL;
5412 }
5413 kind = u_kind;
5414 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5415 return NULL;
5416 }
5417 Py_INCREF(kind);
5418 }
5419 return kind;
5420}
5421
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005422/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005423static expr_ty
5424make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5425{
5426 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005427 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005428 *str = NULL;
5429 assert(PyUnicode_CheckExact(s));
5430 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5431 Py_DECREF(s);
5432 return NULL;
5433 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005434 kind = make_kind(c, n);
5435 if (kind == NULL && PyErr_Occurred())
5436 return NULL;
5437 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005438 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005439}
5440
5441/* Add a non-f-string (that is, a regular literal string). str is
5442 decref'd. */
5443static int
5444FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5445{
5446 FstringParser_check_invariants(state);
5447
5448 assert(PyUnicode_CheckExact(str));
5449
5450 if (PyUnicode_GET_LENGTH(str) == 0) {
5451 Py_DECREF(str);
5452 return 0;
5453 }
5454
5455 if (!state->last_str) {
5456 /* We didn't have a string before, so just remember this one. */
5457 state->last_str = str;
5458 } else {
5459 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005460 PyUnicode_AppendAndDel(&state->last_str, str);
5461 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005462 return -1;
5463 }
5464 FstringParser_check_invariants(state);
5465 return 0;
5466}
5467
Eric V. Smith451d0e32016-09-09 21:56:20 -04005468/* Parse an f-string. The f-string is in *str to end, with no
5469 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005470static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005471FstringParser_ConcatFstring(FstringParser *state, const char **str,
5472 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005473 struct compiling *c, const node *n)
5474{
5475 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005476 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005477
5478 /* Parse the f-string. */
5479 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005480 PyObject *literal = NULL;
5481 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005482 expr_ty expression = NULL;
5483
5484 /* If there's a zero length literal in front of the
5485 expression, literal will be NULL. If we're at the end of
5486 the f-string, expression will be NULL (unless result == 1,
5487 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005488 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005489 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005490 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005491 if (result < 0)
5492 return -1;
5493
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005494 /* Add the literal, if any. */
5495 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5496 Py_XDECREF(expr_text);
5497 return -1;
5498 }
5499 /* Add the expr_text, if any. */
5500 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5501 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005502 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005503
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005504 /* We've dealt with the literal and expr_text, their ownership has
5505 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005506
5507 /* See if we should just loop around to get the next literal
5508 and expression, while ignoring the expression this
5509 time. This is used for un-doubling braces, as an
5510 optimization. */
5511 if (result == 1)
5512 continue;
5513
5514 if (!expression)
5515 /* We're done with this f-string. */
5516 break;
5517
5518 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005519 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005520 if (!state->last_str) {
5521 /* Do nothing. No previous literal. */
5522 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005523 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005524 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5525 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5526 return -1;
5527 }
5528
5529 if (ExprList_Append(&state->expr_list, expression) < 0)
5530 return -1;
5531 }
5532
Eric V. Smith235a6f02015-09-19 14:51:32 -04005533 /* If recurse_lvl is zero, then we must be at the end of the
5534 string. Otherwise, we must be at a right brace. */
5535
Eric V. Smith451d0e32016-09-09 21:56:20 -04005536 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005537 ast_error(c, n, "f-string: unexpected end of string");
5538 return -1;
5539 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005540 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005541 ast_error(c, n, "f-string: expecting '}'");
5542 return -1;
5543 }
5544
5545 FstringParser_check_invariants(state);
5546 return 0;
5547}
5548
5549/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005550 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005551static expr_ty
5552FstringParser_Finish(FstringParser *state, struct compiling *c,
5553 const node *n)
5554{
5555 asdl_seq *seq;
5556
5557 FstringParser_check_invariants(state);
5558
5559 /* If we're just a constant string with no expressions, return
5560 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005561 if (!state->fmode) {
5562 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005563 if (!state->last_str) {
5564 /* Create a zero length string. */
5565 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5566 if (!state->last_str)
5567 goto error;
5568 }
5569 return make_str_node_and_del(&state->last_str, c, n);
5570 }
5571
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005572 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005573 last node in our expression list. */
5574 if (state->last_str) {
5575 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5576 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5577 goto error;
5578 }
5579 /* This has already been freed. */
5580 assert(state->last_str == NULL);
5581
5582 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5583 if (!seq)
5584 goto error;
5585
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005586 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5587 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005588
5589error:
5590 FstringParser_Dealloc(state);
5591 return NULL;
5592}
5593
Eric V. Smith451d0e32016-09-09 21:56:20 -04005594/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5595 at end, parse it into an expr_ty. Return NULL on error. Adjust
5596 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005597static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005598fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599 struct compiling *c, const node *n)
5600{
5601 FstringParser state;
5602
5603 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005604 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005605 c, n) < 0) {
5606 FstringParser_Dealloc(&state);
5607 return NULL;
5608 }
5609
5610 return FstringParser_Finish(&state, c, n);
5611}
5612
5613/* n is a Python string literal, including the bracketing quote
5614 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005616 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005617 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5618 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005619*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005620static int
5621parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5622 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005623{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005624 size_t len;
5625 const char *s = STR(n);
5626 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627 int fmode = 0;
5628 *bytesmode = 0;
5629 *rawmode = 0;
5630 *result = NULL;
5631 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005632 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005633 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005634 if (quote == 'b' || quote == 'B') {
5635 quote = *++s;
5636 *bytesmode = 1;
5637 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005638 else if (quote == 'u' || quote == 'U') {
5639 quote = *++s;
5640 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005641 else if (quote == 'r' || quote == 'R') {
5642 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005643 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005644 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005645 else if (quote == 'f' || quote == 'F') {
5646 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005647 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005648 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005649 else {
5650 break;
5651 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005652 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005653 }
Guido van Rossum495da292019-03-07 12:38:08 -08005654
5655 /* fstrings are only allowed in Python 3.6 and greater */
5656 if (fmode && c->c_feature_version < 6) {
5657 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5658 return -1;
5659 }
5660
Eric V. Smith451d0e32016-09-09 21:56:20 -04005661 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005663 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005664 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005665 if (quote != '\'' && quote != '\"') {
5666 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005667 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005668 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005669 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005670 s++;
5671 len = strlen(s);
5672 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005673 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005674 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005675 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005676 }
5677 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005678 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005679 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005680 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005681 }
5682 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005683 /* A triple quoted string. We've already skipped one quote at
5684 the start and one at the end of the string. Now skip the
5685 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005686 s += 2;
5687 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005688 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005689 if (s[--len] != quote || s[--len] != quote) {
5690 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005692 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005693 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005694
Eric V. Smith451d0e32016-09-09 21:56:20 -04005695 if (fmode) {
5696 /* Just return the bytes. The caller will parse the resulting
5697 string. */
5698 *fstr = s;
5699 *fstrlen = len;
5700 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005701 }
5702
Eric V. Smith451d0e32016-09-09 21:56:20 -04005703 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005704 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005705 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005706 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005707 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005708 const char *ch;
5709 for (ch = s; *ch; ch++) {
5710 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005711 ast_error(c, n,
5712 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005713 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005715 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005716 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005717 if (*rawmode)
5718 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005719 else
Eric V. Smith56466482016-10-31 14:46:26 -04005720 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005721 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005722 if (*rawmode)
5723 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005724 else
Eric V. Smith56466482016-10-31 14:46:26 -04005725 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005726 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005727 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005728}
5729
Eric V. Smith235a6f02015-09-19 14:51:32 -04005730/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5731 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005732 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005733 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005734 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005735 node if there's just an f-string (with no leading or trailing
5736 literals), or a JoinedStr node if there are multiple f-strings or
5737 any literals involved. */
5738static expr_ty
5739parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005740{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741 int bytesmode = 0;
5742 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005743 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744
5745 FstringParser state;
5746 FstringParser_Init(&state);
5747
5748 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005749 int this_bytesmode;
5750 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005751 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005752 const char *fstr;
5753 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005754
5755 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005756 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5757 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005758 goto error;
5759
5760 /* Check that we're not mixing bytes with unicode. */
5761 if (i != 0 && bytesmode != this_bytesmode) {
5762 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005763 /* s is NULL if the current string part is an f-string. */
5764 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005765 goto error;
5766 }
5767 bytesmode = this_bytesmode;
5768
Eric V. Smith451d0e32016-09-09 21:56:20 -04005769 if (fstr != NULL) {
5770 int result;
5771 assert(s == NULL && !bytesmode);
5772 /* This is an f-string. Parse and concatenate it. */
5773 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5774 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005775 if (result < 0)
5776 goto error;
5777 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005778 /* A string or byte string. */
5779 assert(s != NULL && fstr == NULL);
5780
Eric V. Smith451d0e32016-09-09 21:56:20 -04005781 assert(bytesmode ? PyBytes_CheckExact(s) :
5782 PyUnicode_CheckExact(s));
5783
Eric V. Smith451d0e32016-09-09 21:56:20 -04005784 if (bytesmode) {
5785 /* For bytes, concat as we go. */
5786 if (i == 0) {
5787 /* First time, just remember this value. */
5788 bytes_str = s;
5789 } else {
5790 PyBytes_ConcatAndDel(&bytes_str, s);
5791 if (!bytes_str)
5792 goto error;
5793 }
5794 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005795 /* This is a regular string. Concatenate it. */
5796 if (FstringParser_ConcatAndDel(&state, s) < 0)
5797 goto error;
5798 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005799 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005800 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005801 if (bytesmode) {
5802 /* Just return the bytes object and we're done. */
5803 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5804 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005805 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005806 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005808
Eric V. Smith235a6f02015-09-19 14:51:32 -04005809 /* We're not a bytes string, bytes_str should never have been set. */
5810 assert(bytes_str == NULL);
5811
5812 return FstringParser_Finish(&state, c, n);
5813
5814error:
5815 Py_XDECREF(bytes_str);
5816 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005818}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005819
5820PyObject *
5821_PyAST_GetDocString(asdl_seq *body)
5822{
5823 if (!asdl_seq_LEN(body)) {
5824 return NULL;
5825 }
5826 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5827 if (st->kind != Expr_kind) {
5828 return NULL;
5829 }
5830 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005831 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5832 return e->v.Constant.value;
5833 }
5834 return NULL;
5835}