blob: 417b347f987417b43ec37f45e73dae6db2a748e2 [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
43validate_slice(slice_ty slice)
44{
45 switch (slice->kind) {
46 case Slice_kind:
47 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
48 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
49 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
50 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010051 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050052 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
53 return 0;
54 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
55 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
56 return 0;
57 return 1;
58 }
59 case Index_kind:
60 return validate_expr(slice->v.Index.value, Load);
61 default:
62 PyErr_SetString(PyExc_SystemError, "unknown slice node");
63 return 0;
64 }
65}
66
67static int
68validate_keywords(asdl_seq *keywords)
69{
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 for (i = 0; i < asdl_seq_LEN(keywords); i++)
72 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
73 return 0;
74 return 1;
75}
76
77static int
78validate_args(asdl_seq *args)
79{
Victor Stinner4d73ae72018-11-22 14:45:16 +010080 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050081 for (i = 0; i < asdl_seq_LEN(args); i++) {
82 arg_ty arg = asdl_seq_GET(args, i);
83 if (arg->annotation && !validate_expr(arg->annotation, Load))
84 return 0;
85 }
86 return 1;
87}
88
89static const char *
90expr_context_name(expr_context_ty ctx)
91{
92 switch (ctx) {
93 case Load:
94 return "Load";
95 case Store:
96 return "Store";
97 case Del:
98 return "Del";
99 case AugLoad:
100 return "AugLoad";
101 case AugStore:
102 return "AugStore";
103 case Param:
104 return "Param";
105 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700106 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500107 }
108}
109
110static int
111validate_arguments(arguments_ty args)
112{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100113 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100115 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700116 if (args->vararg && args->vararg->annotation
117 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500118 return 0;
119 }
120 if (!validate_args(args->kwonlyargs))
121 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100122 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700123 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500124 return 0;
125 }
Pablo Galindo2f58a842019-05-31 14:09:49 +0100126 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500127 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
128 return 0;
129 }
130 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
131 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
132 "kw_defaults on arguments");
133 return 0;
134 }
135 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
136}
137
138static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100139validate_constant(PyObject *value)
140{
141 if (value == Py_None || value == Py_Ellipsis)
142 return 1;
143
144 if (PyLong_CheckExact(value)
145 || PyFloat_CheckExact(value)
146 || PyComplex_CheckExact(value)
147 || PyBool_Check(value)
148 || PyUnicode_CheckExact(value)
149 || PyBytes_CheckExact(value))
150 return 1;
151
152 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
153 PyObject *it;
154
155 it = PyObject_GetIter(value);
156 if (it == NULL)
157 return 0;
158
159 while (1) {
160 PyObject *item = PyIter_Next(it);
161 if (item == NULL) {
162 if (PyErr_Occurred()) {
163 Py_DECREF(it);
164 return 0;
165 }
166 break;
167 }
168
169 if (!validate_constant(item)) {
170 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 return 0;
173 }
Victor Stinner726f6902016-01-27 00:11:47 +0100174 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100175 }
176
177 Py_DECREF(it);
178 return 1;
179 }
180
181 return 0;
182}
183
184static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500185validate_expr(expr_ty exp, expr_context_ty ctx)
186{
187 int check_ctx = 1;
188 expr_context_ty actual_ctx;
189
190 /* First check expression context. */
191 switch (exp->kind) {
192 case Attribute_kind:
193 actual_ctx = exp->v.Attribute.ctx;
194 break;
195 case Subscript_kind:
196 actual_ctx = exp->v.Subscript.ctx;
197 break;
198 case Starred_kind:
199 actual_ctx = exp->v.Starred.ctx;
200 break;
201 case Name_kind:
202 actual_ctx = exp->v.Name.ctx;
203 break;
204 case List_kind:
205 actual_ctx = exp->v.List.ctx;
206 break;
207 case Tuple_kind:
208 actual_ctx = exp->v.Tuple.ctx;
209 break;
210 default:
211 if (ctx != Load) {
212 PyErr_Format(PyExc_ValueError, "expression which can't be "
213 "assigned to in %s context", expr_context_name(ctx));
214 return 0;
215 }
216 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100217 /* set actual_ctx to prevent gcc warning */
218 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500219 }
220 if (check_ctx && actual_ctx != ctx) {
221 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
222 expr_context_name(ctx), expr_context_name(actual_ctx));
223 return 0;
224 }
225
226 /* Now validate expression. */
227 switch (exp->kind) {
228 case BoolOp_kind:
229 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
230 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
231 return 0;
232 }
233 return validate_exprs(exp->v.BoolOp.values, Load, 0);
234 case BinOp_kind:
235 return validate_expr(exp->v.BinOp.left, Load) &&
236 validate_expr(exp->v.BinOp.right, Load);
237 case UnaryOp_kind:
238 return validate_expr(exp->v.UnaryOp.operand, Load);
239 case Lambda_kind:
240 return validate_arguments(exp->v.Lambda.args) &&
241 validate_expr(exp->v.Lambda.body, Load);
242 case IfExp_kind:
243 return validate_expr(exp->v.IfExp.test, Load) &&
244 validate_expr(exp->v.IfExp.body, Load) &&
245 validate_expr(exp->v.IfExp.orelse, Load);
246 case Dict_kind:
247 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
248 PyErr_SetString(PyExc_ValueError,
249 "Dict doesn't have the same number of keys as values");
250 return 0;
251 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400252 /* null_ok=1 for keys expressions to allow dict unpacking to work in
253 dict literals, i.e. ``{**{a:b}}`` */
254 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
255 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500256 case Set_kind:
257 return validate_exprs(exp->v.Set.elts, Load, 0);
258#define COMP(NAME) \
259 case NAME ## _kind: \
260 return validate_comprehension(exp->v.NAME.generators) && \
261 validate_expr(exp->v.NAME.elt, Load);
262 COMP(ListComp)
263 COMP(SetComp)
264 COMP(GeneratorExp)
265#undef COMP
266 case DictComp_kind:
267 return validate_comprehension(exp->v.DictComp.generators) &&
268 validate_expr(exp->v.DictComp.key, Load) &&
269 validate_expr(exp->v.DictComp.value, Load);
270 case Yield_kind:
271 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500272 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000273 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400274 case Await_kind:
275 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500276 case Compare_kind:
277 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
278 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
279 return 0;
280 }
281 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
282 asdl_seq_LEN(exp->v.Compare.ops)) {
283 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
284 "of comparators and operands");
285 return 0;
286 }
287 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
288 validate_expr(exp->v.Compare.left, Load);
289 case Call_kind:
290 return validate_expr(exp->v.Call.func, Load) &&
291 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400292 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100293 case Constant_kind:
294 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100295 PyErr_Format(PyExc_TypeError,
296 "got an invalid type in Constant: %s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700297 _PyType_Name(Py_TYPE(exp->v.Constant.value)));
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100298 return 0;
299 }
300 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400301 case JoinedStr_kind:
302 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
303 case FormattedValue_kind:
304 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
305 return 0;
306 if (exp->v.FormattedValue.format_spec)
307 return validate_expr(exp->v.FormattedValue.format_spec, Load);
308 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500309 case Attribute_kind:
310 return validate_expr(exp->v.Attribute.value, Load);
311 case Subscript_kind:
312 return validate_slice(exp->v.Subscript.slice) &&
313 validate_expr(exp->v.Subscript.value, Load);
314 case Starred_kind:
315 return validate_expr(exp->v.Starred.value, ctx);
316 case List_kind:
317 return validate_exprs(exp->v.List.elts, ctx, 0);
318 case Tuple_kind:
319 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000320 case NamedExpr_kind:
321 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300322 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500324 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000326 PyErr_SetString(PyExc_SystemError, "unexpected expression");
327 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800569 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570};
571
572static asdl_seq *seq_for_testlist(struct compiling *, const node *);
573static expr_ty ast_for_expr(struct compiling *, const node *);
574static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300575static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000576static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
577 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000578static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000579static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
guoci90fc8982018-09-11 17:45:45 -0400581static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
582static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200585static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000586 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000588static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400589static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000590static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Nick Coghlan650f0d02007-04-15 12:05:43 +0000592#define COMP_GENEXP 0
593#define COMP_LISTCOMP 1
594#define COMP_SETCOMP 2
595
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596static int
597init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000598{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500599 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
600 if (!m)
601 return 0;
602 c->c_normalize = PyObject_GetAttrString(m, "normalize");
603 Py_DECREF(m);
604 if (!c->c_normalize)
605 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500606 return 1;
607}
608
609static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400610new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500611{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400612 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000614 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500616 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000617 /* Check whether there are non-ASCII characters in the
618 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500619 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500624 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700625 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300626 if (form == NULL) {
627 Py_DECREF(id);
628 return NULL;
629 }
630 PyObject *args[2] = {form, id};
631 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500632 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100633 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200634 if (!id2)
635 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300636 if (!PyUnicode_Check(id2)) {
637 PyErr_Format(PyExc_TypeError,
638 "unicodedata.normalize() must return a string, not "
639 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700640 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300641 Py_DECREF(id2);
642 return NULL;
643 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000645 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000646 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200647 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
648 Py_DECREF(id);
649 return NULL;
650 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000651 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652}
653
Benjamin Peterson55e00432012-01-16 17:22:31 -0500654#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200657ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400659 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200660 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200662 va_start(va, errmsg);
663 errstr = PyUnicode_FromFormatV(errmsg, va);
664 va_end(va);
665 if (!errstr) {
666 return 0;
667 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200668 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000670 Py_INCREF(Py_None);
671 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 }
Ammar Askar025eb982018-09-24 17:12:49 -0400673 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200674 if (!tmp) {
675 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400676 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000677 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 Py_DECREF(errstr);
680 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400681 if (value) {
682 PyErr_SetObject(PyExc_SyntaxError, value);
683 Py_DECREF(value);
684 }
685 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686}
687
688/* num_stmts() returns number of contained statements.
689
690 Use this routine to determine how big a sequence is needed for
691 the statements in a parse tree. Its raison d'etre is this bit of
692 grammar:
693
694 stmt: simple_stmt | compound_stmt
695 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
696
697 A simple_stmt can contain multiple small_stmt elements joined
698 by semicolons. If the arg is a simple_stmt, the number of
699 small_stmt elements is returned.
700*/
701
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800702static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800703new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800704{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800705 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800706 if (res == NULL)
707 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800708 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
709 Py_DECREF(res);
710 return NULL;
711 }
712 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800713}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800714#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800715
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716static int
717num_stmts(const node *n)
718{
719 int i, l;
720 node *ch;
721
722 switch (TYPE(n)) {
723 case single_input:
724 if (TYPE(CHILD(n, 0)) == NEWLINE)
725 return 0;
726 else
727 return num_stmts(CHILD(n, 0));
728 case file_input:
729 l = 0;
730 for (i = 0; i < NCH(n); i++) {
731 ch = CHILD(n, i);
732 if (TYPE(ch) == stmt)
733 l += num_stmts(ch);
734 }
735 return l;
736 case stmt:
737 return num_stmts(CHILD(n, 0));
738 case compound_stmt:
739 return 1;
740 case simple_stmt:
741 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
742 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800743 case func_body_suite:
744 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
745 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 if (NCH(n) == 1)
747 return num_stmts(CHILD(n, 0));
748 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800749 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800751 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
752 i += 2;
753 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 l += num_stmts(CHILD(n, i));
755 return l;
756 }
757 default: {
758 char buf[128];
759
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000760 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 TYPE(n), NCH(n));
762 Py_FatalError(buf);
763 }
764 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700765 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766}
767
768/* Transform the CST rooted at node * to the appropriate AST
769*/
770
771mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200772PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
773 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000775 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800777 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 stmt_ty s;
779 node *ch;
780 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800782 asdl_seq *argtypes = NULL;
783 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200786 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400787 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700789 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800790
791 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 switch (TYPE(n)) {
796 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200797 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500799 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 for (i = 0; i < NCH(n) - 1; i++) {
801 ch = CHILD(n, i);
802 if (TYPE(ch) == NEWLINE)
803 continue;
804 REQ(ch, stmt);
805 num = num_stmts(ch);
806 if (num == 1) {
807 s = ast_for_stmt(&c, ch);
808 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000810 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 else {
813 ch = CHILD(ch, 0);
814 REQ(ch, simple_stmt);
815 for (j = 0; j < num; j++) {
816 s = ast_for_stmt(&c, CHILD(ch, j * 2));
817 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000819 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 }
822 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800823
824 /* Type ignores are stored under the ENDMARKER in file_input. */
825 ch = CHILD(n, NCH(n) - 1);
826 REQ(ch, ENDMARKER);
827 num = NCH(ch);
828 type_ignores = _Py_asdl_seq_new(num, arena);
829 if (!type_ignores)
830 goto out;
831
832 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700833 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
834 if (!type_comment)
835 goto out;
836 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800837 if (!ti)
838 goto out;
839 asdl_seq_SET(type_ignores, i, ti);
840 }
841
842 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 case eval_input: {
845 expr_ty testlist_ast;
846
Nick Coghlan650f0d02007-04-15 12:05:43 +0000847 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000848 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
851 res = Expression(testlist_ast, arena);
852 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 case single_input:
855 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200856 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000860 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000862 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
864 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
866 else {
867 n = CHILD(n, 0);
868 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200869 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000873 s = ast_for_stmt(&c, n);
874 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500875 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 asdl_seq_SET(stmts, 0, s);
877 }
878 else {
879 /* Only a simple_stmt can contain multiple statements. */
880 REQ(n, simple_stmt);
881 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 if (TYPE(CHILD(n, i)) == NEWLINE)
883 break;
884 s = ast_for_stmt(&c, CHILD(n, i));
885 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500886 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 asdl_seq_SET(stmts, i / 2, s);
888 }
889 }
890
Benjamin Peterson55e00432012-01-16 17:22:31 -0500891 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500893 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800894 case func_type_input:
895 n = CHILD(n, 0);
896 REQ(n, func_type);
897
898 if (TYPE(CHILD(n, 1)) == typelist) {
899 ch = CHILD(n, 1);
900 /* this is overly permissive -- we don't pay any attention to
901 * stars on the args -- just parse them into an ordered list */
902 num = 0;
903 for (i = 0; i < NCH(ch); i++) {
904 if (TYPE(CHILD(ch, i)) == test) {
905 num++;
906 }
907 }
908
909 argtypes = _Py_asdl_seq_new(num, arena);
910 if (!argtypes)
911 goto out;
912
913 j = 0;
914 for (i = 0; i < NCH(ch); i++) {
915 if (TYPE(CHILD(ch, i)) == test) {
916 arg = ast_for_expr(&c, CHILD(ch, i));
917 if (!arg)
918 goto out;
919 asdl_seq_SET(argtypes, j++, arg);
920 }
921 }
922 }
923 else {
924 argtypes = _Py_asdl_seq_new(0, arena);
925 if (!argtypes)
926 goto out;
927 }
928
929 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
930 if (!ret)
931 goto out;
932 res = FunctionType(argtypes, ret, arena);
933 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000935 PyErr_Format(PyExc_SystemError,
936 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500937 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500939 out:
940 if (c.c_normalize) {
941 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500942 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500943 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944}
945
Victor Stinner14e461d2013-08-26 22:28:21 +0200946mod_ty
947PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
948 PyArena *arena)
949{
950 mod_ty mod;
951 PyObject *filename;
952 filename = PyUnicode_DecodeFSDefault(filename_str);
953 if (filename == NULL)
954 return NULL;
955 mod = PyAST_FromNodeObject(n, flags, filename, arena);
956 Py_DECREF(filename);
957 return mod;
958
959}
960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
962*/
963
964static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800965get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 switch (TYPE(n)) {
968 case VBAR:
969 return BitOr;
970 case CIRCUMFLEX:
971 return BitXor;
972 case AMPER:
973 return BitAnd;
974 case LEFTSHIFT:
975 return LShift;
976 case RIGHTSHIFT:
977 return RShift;
978 case PLUS:
979 return Add;
980 case MINUS:
981 return Sub;
982 case STAR:
983 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400984 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800985 if (c->c_feature_version < 5) {
986 ast_error(c, n,
987 "The '@' operator is only supported in Python 3.5 and greater");
988 return (operator_ty)0;
989 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400990 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 case SLASH:
992 return Div;
993 case DOUBLESLASH:
994 return FloorDiv;
995 case PERCENT:
996 return Mod;
997 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 }
1000}
1001
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001002static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001003 "None",
1004 "True",
1005 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001006 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001007 NULL,
1008};
1009
1010static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001011forbidden_name(struct compiling *c, identifier name, const node *n,
1012 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001013{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001014 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001015 const char * const *p = FORBIDDEN;
1016 if (!full_checks) {
1017 /* In most cases, the parser will protect True, False, and None
1018 from being assign to. */
1019 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001020 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001021 for (; *p; p++) {
1022 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1023 ast_error(c, n, "cannot assign to %U", name);
1024 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001025 }
1026 }
1027 return 0;
1028}
1029
Serhiy Storchakab619b092018-11-27 09:40:29 +02001030static expr_ty
1031copy_location(expr_ty e, const node *n)
1032{
1033 if (e) {
1034 e->lineno = LINENO(n);
1035 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001036 e->end_lineno = n->n_end_lineno;
1037 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001038 }
1039 return e;
1040}
1041
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001042static const char *
1043get_expr_name(expr_ty e)
1044{
1045 switch (e->kind) {
1046 case Attribute_kind:
1047 return "attribute";
1048 case Subscript_kind:
1049 return "subscript";
1050 case Starred_kind:
1051 return "starred";
1052 case Name_kind:
1053 return "name";
1054 case List_kind:
1055 return "list";
1056 case Tuple_kind:
1057 return "tuple";
1058 case Lambda_kind:
1059 return "lambda";
1060 case Call_kind:
1061 return "function call";
1062 case BoolOp_kind:
1063 case BinOp_kind:
1064 case UnaryOp_kind:
1065 return "operator";
1066 case GeneratorExp_kind:
1067 return "generator expression";
1068 case Yield_kind:
1069 case YieldFrom_kind:
1070 return "yield expression";
1071 case Await_kind:
1072 return "await expression";
1073 case ListComp_kind:
1074 return "list comprehension";
1075 case SetComp_kind:
1076 return "set comprehension";
1077 case DictComp_kind:
1078 return "dict comprehension";
1079 case Dict_kind:
1080 return "dict display";
1081 case Set_kind:
1082 return "set display";
1083 case JoinedStr_kind:
1084 case FormattedValue_kind:
1085 return "f-string expression";
1086 case Constant_kind: {
1087 PyObject *value = e->v.Constant.value;
1088 if (value == Py_None) {
1089 return "None";
1090 }
1091 if (value == Py_False) {
1092 return "False";
1093 }
1094 if (value == Py_True) {
1095 return "True";
1096 }
1097 if (value == Py_Ellipsis) {
1098 return "Ellipsis";
1099 }
1100 return "literal";
1101 }
1102 case Compare_kind:
1103 return "comparison";
1104 case IfExp_kind:
1105 return "conditional expression";
1106 case NamedExpr_kind:
1107 return "named expression";
1108 default:
1109 PyErr_Format(PyExc_SystemError,
1110 "unexpected expression in assignment %d (line %d)",
1111 e->kind, e->lineno);
1112 return NULL;
1113 }
1114}
1115
Jeremy Hyltona8293132006-02-28 17:58:27 +00001116/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117
1118 Only sets context for expr kinds that "can appear in assignment context"
1119 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1120 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121*/
1122
1123static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001124set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125{
1126 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001127
1128 /* The ast defines augmented store and load contexts, but the
1129 implementation here doesn't actually use them. The code may be
1130 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001131 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001132 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001133 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001134 */
1135 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
1137 switch (e->kind) {
1138 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001140 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001141 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001142 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001144 e->v.Subscript.ctx = ctx;
1145 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001146 case Starred_kind:
1147 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001148 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001149 return 0;
1150 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001152 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001153 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001154 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001155 }
1156 e->v.Name.ctx = ctx;
1157 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 e->v.List.ctx = ctx;
1160 s = e->v.List.elts;
1161 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001163 e->v.Tuple.ctx = ctx;
1164 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001166 default: {
1167 const char *expr_name = get_expr_name(e);
1168 if (expr_name != NULL) {
1169 ast_error(c, n, "cannot %s %s",
1170 ctx == Store ? "assign to" : "delete",
1171 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001172 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001173 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001174 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001175 }
1176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 */
1180 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001181 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182
Thomas Wouters89f507f2006-12-13 04:49:30 +00001183 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001184 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001185 return 0;
1186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 }
1188 return 1;
1189}
1190
1191static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001192ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193{
1194 REQ(n, augassign);
1195 n = CHILD(n, 0);
1196 switch (STR(n)[0]) {
1197 case '+':
1198 return Add;
1199 case '-':
1200 return Sub;
1201 case '/':
1202 if (STR(n)[1] == '/')
1203 return FloorDiv;
1204 else
1205 return Div;
1206 case '%':
1207 return Mod;
1208 case '<':
1209 return LShift;
1210 case '>':
1211 return RShift;
1212 case '&':
1213 return BitAnd;
1214 case '^':
1215 return BitXor;
1216 case '|':
1217 return BitOr;
1218 case '*':
1219 if (STR(n)[1] == '*')
1220 return Pow;
1221 else
1222 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001223 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001224 if (c->c_feature_version < 5) {
1225 ast_error(c, n,
1226 "The '@' operator is only supported in Python 3.5 and greater");
1227 return (operator_ty)0;
1228 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001229 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001231 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 }
1234}
1235
1236static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001237ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001239 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 |'is' 'not'
1241 */
1242 REQ(n, comp_op);
1243 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001244 n = CHILD(n, 0);
1245 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 case LESS:
1247 return Lt;
1248 case GREATER:
1249 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001250 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return Eq;
1252 case LESSEQUAL:
1253 return LtE;
1254 case GREATEREQUAL:
1255 return GtE;
1256 case NOTEQUAL:
1257 return NotEq;
1258 case NAME:
1259 if (strcmp(STR(n), "in") == 0)
1260 return In;
1261 if (strcmp(STR(n), "is") == 0)
1262 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001263 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001265 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
1270 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001271 /* handle "not in" and "is not" */
1272 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 case NAME:
1274 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1275 return NotIn;
1276 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1277 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001278 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001280 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001282 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001283 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 }
Neal Norwitz79792652005-11-14 04:25:03 +00001285 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288}
1289
1290static asdl_seq *
1291seq_for_testlist(struct compiling *c, const node *n)
1292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001294 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1295 */
Armin Rigo31441302005-10-21 12:57:31 +00001296 asdl_seq *seq;
1297 expr_ty expression;
1298 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001299 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001301 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 if (!seq)
1303 return NULL;
1304
1305 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001307 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308
Benjamin Peterson4905e802009-09-27 02:43:28 +00001309 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001310 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312
1313 assert(i / 2 < seq->size);
1314 asdl_seq_SET(seq, i / 2, expression);
1315 }
1316 return seq;
1317}
1318
Neal Norwitzc1505362006-12-28 06:47:50 +00001319static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001320ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001321{
1322 identifier name;
1323 expr_ty annotation = NULL;
1324 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001325 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001326
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001327 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001329 name = NEW_IDENTIFIER(ch);
1330 if (!name)
1331 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001332 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001333 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001334
1335 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1336 annotation = ast_for_expr(c, CHILD(n, 2));
1337 if (!annotation)
1338 return NULL;
1339 }
1340
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001341 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001342 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001343 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001344 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001345 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346}
1347
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348/* returns -1 if failed to handle keyword only arguments
1349 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001350 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 ^^^
1352 start pointing here
1353 */
1354static int
1355handle_keywordonly_args(struct compiling *c, const node *n, int start,
1356 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1357{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001358 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001360 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001361 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 int i = start;
1363 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001364
1365 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001366 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001367 return -1;
1368 }
1369 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 while (i < NCH(n)) {
1371 ch = CHILD(n, i);
1372 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001373 case vfpdef:
1374 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001378 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001379 asdl_seq_SET(kwdefaults, j, expression);
1380 i += 2; /* '=' and test */
1381 }
1382 else { /* setting NULL if no default value exists */
1383 asdl_seq_SET(kwdefaults, j, NULL);
1384 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 if (NCH(ch) == 3) {
1386 /* ch is NAME ':' test */
1387 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001388 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001389 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001390 }
1391 else {
1392 annotation = NULL;
1393 }
1394 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001395 argname = NEW_IDENTIFIER(ch);
1396 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001398 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001399 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001400 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001401 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001402 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001403 if (!arg)
1404 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001405 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001406 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001407 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001408 i += 1; /* the comma, if present */
1409 break;
1410 case TYPE_COMMENT:
1411 /* arg will be equal to the last argument processed */
1412 arg->type_comment = NEW_TYPE_COMMENT(ch);
1413 if (!arg->type_comment)
1414 goto error;
1415 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 break;
1417 case DOUBLESTAR:
1418 return i;
1419 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001420 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 goto error;
1422 }
1423 }
1424 return i;
1425 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428
Jeremy Hyltona8293132006-02-28 17:58:27 +00001429/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430
1431static arguments_ty
1432ast_for_arguments(struct compiling *c, const node *n)
1433{
Neal Norwitzc1505362006-12-28 06:47:50 +00001434 /* This function handles both typedargslist (function definition)
1435 and varargslist (lambda definition).
1436
1437 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001438
1439 The following definition for typedarglist is equivalent to this set of rules:
1440
1441 arguments = argument (',' [TYPE_COMMENT] argument)*
1442 argument = tfpdef ['=' test]
1443 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1444 args = '*' [tfpdef]
1445 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1446 [TYPE_COMMENT] [kwargs]])
1447 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1448 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1449 [TYPE_COMMENT] [args_kwonly_kwargs]])
1450 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1451 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1452 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1453
1454 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1455 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1456 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1457 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1458 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1459 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1460 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1461 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1462 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1463 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1464 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1465 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1466 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1467 '**' tfpdef [','] [TYPE_COMMENT]))
1468
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001469 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001470
1471 The following definition for varargslist is equivalent to this set of rules:
1472
1473 arguments = argument (',' argument )*
1474 argument = vfpdef ['=' test]
1475 kwargs = '**' vfpdef [',']
1476 args = '*' [vfpdef]
1477 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1478 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1479 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1480 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1481 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1482 (vararglist_no_posonly)
1483
1484 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1485 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1486 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1487 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1488 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1489 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1490 [',']]] | '**' vfpdef [','])
1491
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001492 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001495 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001497 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001498 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001499 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 node *ch;
1501
1502 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001504 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001507 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
Jeremy Hyltone921e022008-07-17 16:37:17 +00001509 /* First count the number of positional args & defaults. The
1510 variable i is the loop index for this for loop and the next.
1511 The next loop picks up where the first leaves off.
1512 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001514 ch = CHILD(n, i);
1515 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001516 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001517 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001518 if (i < NCH(n) && /* skip argument following star */
1519 (TYPE(CHILD(n, i)) == tfpdef ||
1520 TYPE(CHILD(n, i)) == vfpdef)) {
1521 i++;
1522 }
1523 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001524 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001525 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001526 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001527 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001528 if (TYPE(ch) == SLASH ) {
1529 nposonlyargs = nposargs;
1530 nposargs = 0;
1531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001534 defaults for keyword only args */
1535 for ( ; i < NCH(n); ++i) {
1536 ch = CHILD(n, i);
1537 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001538 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001539 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001540 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1541 if (!posonlyargs && nposonlyargs) {
1542 return NULL;
1543 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001544 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001545 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001546 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001548 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001549 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001550 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001552 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001553 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001554 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 since we set NULL as default for keyword only argument w/o default
1557 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001558 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001559 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001560 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001561 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001562
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001563 /* tfpdef: NAME [':' test]
1564 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 */
1566 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001567 j = 0; /* index for defaults */
1568 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001569 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001571 ch = CHILD(n, i);
1572 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001573 case tfpdef:
1574 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1576 anything other than EQUAL or a comma? */
1577 /* XXX Should NCH(n) check be made a separate check? */
1578 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001579 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1580 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001581 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001582 assert(posdefaults != NULL);
1583 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001585 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001587 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001588 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001589 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001590 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001591 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001592 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001593 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001594 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001595 if (l < nposonlyargs) {
1596 asdl_seq_SET(posonlyargs, l++, arg);
1597 } else {
1598 asdl_seq_SET(posargs, k++, arg);
1599 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001600 i += 1; /* the name */
1601 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1602 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001604 case SLASH:
1605 /* Advance the slash and the comma. If there are more names
1606 * after the slash there will be a comma so we are advancing
1607 * the correct number of nodes. If the slash is the last item,
1608 * we will be advancing an extra token but then * i > NCH(n)
1609 * and the enclosing while will finish correctly. */
1610 i += 2;
1611 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001613 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001614 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1615 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001616 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001617 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001618 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001619 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001620 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001621 if (TYPE(ch) == COMMA) {
1622 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001623 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001624
1625 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1626 ast_error(c, CHILD(n, i),
1627 "bare * has associated type comment");
1628 return NULL;
1629 }
1630
Guido van Rossum4f72a782006-10-27 23:31:49 +00001631 res = handle_keywordonly_args(c, n, i,
1632 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001633 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001634 i = res; /* res has new position to process */
1635 }
1636 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001637 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001638 if (!vararg)
1639 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001640
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001641 i += 2; /* the star and the name */
1642 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1643 i += 1; /* the comma, if present */
1644
1645 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1646 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1647 if (!vararg->type_comment)
1648 return NULL;
1649 i += 1;
1650 }
1651
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001652 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1653 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001654 int res = 0;
1655 res = handle_keywordonly_args(c, n, i,
1656 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001657 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001658 i = res; /* res has new position to process */
1659 }
1660 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 break;
1662 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001663 ch = CHILD(n, i+1); /* tfpdef */
1664 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001665 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001666 if (!kwarg)
1667 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001668 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001669 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001670 i += 1; /* the comma, if present */
1671 break;
1672 case TYPE_COMMENT:
1673 assert(i);
1674
1675 if (kwarg)
1676 arg = kwarg;
1677
1678 /* arg will be equal to the last argument processed */
1679 arg->type_comment = NEW_TYPE_COMMENT(ch);
1680 if (!arg->type_comment)
1681 return NULL;
1682 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 break;
1684 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001685 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 "unexpected node in varargslist: %d @ %d",
1687 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001688 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001689 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001691 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692}
1693
1694static expr_ty
1695ast_for_dotted_name(struct compiling *c, const node *n)
1696{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001697 expr_ty e;
1698 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001699 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001701 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702
1703 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001704
1705 lineno = LINENO(n);
1706 col_offset = n->n_col_offset;
1707
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001708 ch = CHILD(n, 0);
1709 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001711 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001712 e = Name(id, Load, lineno, col_offset,
1713 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716
1717 for (i = 2; i < NCH(n); i+=2) {
1718 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001719 if (!id)
1720 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001721 e = Attribute(e, id, Load, lineno, col_offset,
1722 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001723 if (!e)
1724 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 }
1726
1727 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728}
1729
1730static expr_ty
1731ast_for_decorator(struct compiling *c, const node *n)
1732{
1733 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1734 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001735 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001738 REQ(CHILD(n, 0), AT);
1739 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1742 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001743 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 d = name_expr;
1747 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 }
1749 else if (NCH(n) == 5) { /* Call with no arguments */
Serhiy Storchaka26ae9f62019-10-26 16:46:05 +03001750 d = Call(name_expr, NULL, NULL,
1751 name_expr->lineno, name_expr->col_offset,
1752 CHILD(n, 3)->n_end_lineno, CHILD(n, 3)->n_end_col_offset,
1753 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001754 if (!d)
1755 return NULL;
1756 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 }
1758 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001759 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 if (!d)
1761 return NULL;
1762 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 }
1764
1765 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766}
1767
1768static asdl_seq*
1769ast_for_decorators(struct compiling *c, const node *n)
1770{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001771 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001772 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001776 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 if (!decorator_seq)
1778 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001781 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001782 if (!d)
1783 return NULL;
1784 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 }
1786 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787}
1788
1789static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001790ast_for_funcdef_impl(struct compiling *c, const node *n0,
1791 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001793 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001794 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001795 identifier name;
1796 arguments_ty args;
1797 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001798 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001799 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001800 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001801 node *tc;
1802 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803
Guido van Rossum495da292019-03-07 12:38:08 -08001804 if (is_async && c->c_feature_version < 5) {
1805 ast_error(c, n,
1806 "Async functions are only supported in Python 3.5 and greater");
1807 return NULL;
1808 }
1809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 REQ(n, funcdef);
1811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 name = NEW_IDENTIFIER(CHILD(n, name_i));
1813 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001814 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001815 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1818 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001819 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001820 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1821 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1822 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001823 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001824 name_i += 2;
1825 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001826 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1827 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1828 if (!type_comment)
1829 return NULL;
1830 name_i += 1;
1831 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001832 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001834 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001835 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001837 if (NCH(CHILD(n, name_i + 3)) > 1) {
1838 /* Check if the suite has a type comment in it. */
1839 tc = CHILD(CHILD(n, name_i + 3), 1);
1840
1841 if (TYPE(tc) == TYPE_COMMENT) {
1842 if (type_comment != NULL) {
1843 ast_error(c, n, "Cannot have two type comments on def");
1844 return NULL;
1845 }
1846 type_comment = NEW_TYPE_COMMENT(tc);
1847 if (!type_comment)
1848 return NULL;
1849 }
1850 }
1851
Yury Selivanov75445082015-05-11 22:57:16 -04001852 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001853 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001854 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001855 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001856 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001857 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001858}
1859
1860static stmt_ty
1861ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1862{
Guido van Rossum495da292019-03-07 12:38:08 -08001863 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001864 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001865 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001866 REQ(CHILD(n, 1), funcdef);
1867
guoci90fc8982018-09-11 17:45:45 -04001868 return ast_for_funcdef_impl(c, n, decorator_seq,
1869 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001870}
1871
1872static stmt_ty
1873ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1874{
1875 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1876 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001877 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001878}
1879
1880
1881static stmt_ty
1882ast_for_async_stmt(struct compiling *c, const node *n)
1883{
Guido van Rossum495da292019-03-07 12:38:08 -08001884 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001885 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001886 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001887
1888 switch (TYPE(CHILD(n, 1))) {
1889 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001890 return ast_for_funcdef_impl(c, n, NULL,
1891 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001892 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001893 return ast_for_with_stmt(c, n,
1894 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001895
1896 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001897 return ast_for_for_stmt(c, n,
1898 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001899
1900 default:
1901 PyErr_Format(PyExc_SystemError,
1902 "invalid async stament: %s",
1903 STR(CHILD(n, 1)));
1904 return NULL;
1905 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906}
1907
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001908static stmt_ty
1909ast_for_decorated(struct compiling *c, const node *n)
1910{
Yury Selivanov75445082015-05-11 22:57:16 -04001911 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001912 stmt_ty thing = NULL;
1913 asdl_seq *decorator_seq = NULL;
1914
1915 REQ(n, decorated);
1916
1917 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1918 if (!decorator_seq)
1919 return NULL;
1920
1921 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001922 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001924
1925 if (TYPE(CHILD(n, 1)) == funcdef) {
1926 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1927 } else if (TYPE(CHILD(n, 1)) == classdef) {
1928 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001929 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1930 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001931 }
1932 return thing;
1933}
1934
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001936ast_for_namedexpr(struct compiling *c, const node *n)
1937{
1938 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1939 ['else' ':' suite]
1940 namedexpr_test: test [':=' test]
1941 argument: ( test [comp_for] |
1942 test ':=' test |
1943 test '=' test |
1944 '**' test |
1945 '*' test )
1946 */
1947 expr_ty target, value;
1948
1949 target = ast_for_expr(c, CHILD(n, 0));
1950 if (!target)
1951 return NULL;
1952
1953 value = ast_for_expr(c, CHILD(n, 2));
1954 if (!value)
1955 return NULL;
1956
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001957 if (target->kind != Name_kind) {
1958 const char *expr_name = get_expr_name(target);
1959 if (expr_name != NULL) {
1960 ast_error(c, n, "cannot use named assignment with %s", expr_name);
1961 }
1962 return NULL;
1963 }
1964
1965 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001966 return NULL;
1967
1968 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1969 n->n_end_col_offset, c->c_arena);
1970}
1971
1972static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973ast_for_lambdef(struct compiling *c, const node *n)
1974{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001975 /* lambdef: 'lambda' [varargslist] ':' test
1976 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 arguments_ty args;
1978 expr_ty expression;
1979
1980 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001981 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 if (!args)
1983 return NULL;
1984 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 }
1988 else {
1989 args = ast_for_arguments(c, CHILD(n, 1));
1990 if (!args)
1991 return NULL;
1992 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 }
1996
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001997 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1998 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002001static expr_ty
2002ast_for_ifexpr(struct compiling *c, const node *n)
2003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002005 expr_ty expression, body, orelse;
2006
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00002007 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002008 body = ast_for_expr(c, CHILD(n, 0));
2009 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002010 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002011 expression = ast_for_expr(c, CHILD(n, 2));
2012 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002013 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002014 orelse = ast_for_expr(c, CHILD(n, 4));
2015 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002016 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002017 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002018 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002019 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002020}
2021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002023 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024
Nick Coghlan650f0d02007-04-15 12:05:43 +00002025 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026*/
2027
2028static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002029count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002031 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032
Guido van Rossumd8faa362007-04-27 19:54:29 +00002033 count_comp_for:
2034 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002035 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002036 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08002037 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002038 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002039 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002040 else if (NCH(n) == 1) {
2041 n = CHILD(n, 0);
2042 }
2043 else {
2044 goto error;
2045 }
2046 if (NCH(n) == (5)) {
2047 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002048 }
2049 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002050 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002051 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002052 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002053 REQ(n, comp_iter);
2054 n = CHILD(n, 0);
2055 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002056 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002057 else if (TYPE(n) == comp_if) {
2058 if (NCH(n) == 3) {
2059 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002060 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002061 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002062 else
2063 return n_fors;
2064 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002065
Jelle Zijlstraac317702017-10-05 20:24:46 -07002066 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002067 /* Should never be reached */
2068 PyErr_SetString(PyExc_SystemError,
2069 "logic error in count_comp_fors");
2070 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071}
2072
Nick Coghlan650f0d02007-04-15 12:05:43 +00002073/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074
Nick Coghlan650f0d02007-04-15 12:05:43 +00002075 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076*/
2077
2078static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002079count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002081 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082
Guido van Rossumd8faa362007-04-27 19:54:29 +00002083 while (1) {
2084 REQ(n, comp_iter);
2085 if (TYPE(CHILD(n, 0)) == comp_for)
2086 return n_ifs;
2087 n = CHILD(n, 0);
2088 REQ(n, comp_if);
2089 n_ifs++;
2090 if (NCH(n) == 2)
2091 return n_ifs;
2092 n = CHILD(n, 2);
2093 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094}
2095
Guido van Rossum992d4a32007-07-11 13:09:30 +00002096static asdl_seq *
2097ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002100 asdl_seq *comps;
2101
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002102 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 if (n_fors == -1)
2104 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002105
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002106 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002107 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002111 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002113 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002114 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002115 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002116 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117
Guido van Rossum992d4a32007-07-11 13:09:30 +00002118 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119
Jelle Zijlstraac317702017-10-05 20:24:46 -07002120 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002121 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002122 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002123 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002124 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002125 else {
2126 sync_n = CHILD(n, 0);
2127 }
2128 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002129
Guido van Rossum495da292019-03-07 12:38:08 -08002130 /* Async comprehensions only allowed in Python 3.6 and greater */
2131 if (is_async && c->c_feature_version < 6) {
2132 ast_error(c, n,
2133 "Async comprehensions are only supported in Python 3.6 and greater");
2134 return NULL;
2135 }
2136
Jelle Zijlstraac317702017-10-05 20:24:46 -07002137 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002138 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002141 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002142 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002144
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 /* Check the # of children rather than the length of t, since
2146 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002147 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002149 comp = comprehension(first, expression, NULL,
2150 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002152 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2153 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2154 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002155 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002156 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002158
Jelle Zijlstraac317702017-10-05 20:24:46 -07002159 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 int j, n_ifs;
2161 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162
Jelle Zijlstraac317702017-10-05 20:24:46 -07002163 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002164 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002165 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002167
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002168 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002169 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002173 REQ(n, comp_iter);
2174 n = CHILD(n, 0);
2175 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Guido van Rossum992d4a32007-07-11 13:09:30 +00002177 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002178 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002179 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002180 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002181 if (NCH(n) == 3)
2182 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002184 /* on exit, must guarantee that n is a comp_for */
2185 if (TYPE(n) == comp_iter)
2186 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002187 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002189 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002191 return comps;
2192}
2193
2194static expr_ty
2195ast_for_itercomp(struct compiling *c, const node *n, int type)
2196{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002197 /* testlist_comp: (test|star_expr)
2198 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002199 expr_ty elt;
2200 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202
Guido van Rossum992d4a32007-07-11 13:09:30 +00002203 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 ch = CHILD(n, 0);
2206 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002207 if (!elt)
2208 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 if (elt->kind == Starred_kind) {
2210 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2211 return NULL;
2212 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213
Guido van Rossum992d4a32007-07-11 13:09:30 +00002214 comps = ast_for_comprehension(c, CHILD(n, 1));
2215 if (!comps)
2216 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002217
2218 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002219 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2220 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002221 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002222 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2223 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002224 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002225 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2226 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002227 else
2228 /* Should never happen */
2229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230}
2231
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002232/* Fills in the key, value pair corresponding to the dict element. In case
2233 * of an unpacking, key is NULL. *i is advanced by the number of ast
2234 * elements. Iff successful, nonzero is returned.
2235 */
2236static int
2237ast_for_dictelement(struct compiling *c, const node *n, int *i,
2238 expr_ty *key, expr_ty *value)
2239{
2240 expr_ty expression;
2241 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2242 assert(NCH(n) - *i >= 2);
2243
2244 expression = ast_for_expr(c, CHILD(n, *i + 1));
2245 if (!expression)
2246 return 0;
2247 *key = NULL;
2248 *value = expression;
2249
2250 *i += 2;
2251 }
2252 else {
2253 assert(NCH(n) - *i >= 3);
2254
2255 expression = ast_for_expr(c, CHILD(n, *i));
2256 if (!expression)
2257 return 0;
2258 *key = expression;
2259
2260 REQ(CHILD(n, *i + 1), COLON);
2261
2262 expression = ast_for_expr(c, CHILD(n, *i + 2));
2263 if (!expression)
2264 return 0;
2265 *value = expression;
2266
2267 *i += 3;
2268 }
2269 return 1;
2270}
2271
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002273ast_for_dictcomp(struct compiling *c, const node *n)
2274{
2275 expr_ty key, value;
2276 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002277 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002279 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002280 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002281 assert(key);
2282 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002284 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002285 if (!comps)
2286 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002288 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2289 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002290}
2291
2292static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002293ast_for_dictdisplay(struct compiling *c, const node *n)
2294{
2295 int i;
2296 int j;
2297 int size;
2298 asdl_seq *keys, *values;
2299
2300 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2301 keys = _Py_asdl_seq_new(size, c->c_arena);
2302 if (!keys)
2303 return NULL;
2304
2305 values = _Py_asdl_seq_new(size, c->c_arena);
2306 if (!values)
2307 return NULL;
2308
2309 j = 0;
2310 for (i = 0; i < NCH(n); i++) {
2311 expr_ty key, value;
2312
2313 if (!ast_for_dictelement(c, n, &i, &key, &value))
2314 return NULL;
2315 asdl_seq_SET(keys, j, key);
2316 asdl_seq_SET(values, j, value);
2317
2318 j++;
2319 }
2320 keys->size = j;
2321 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002322 return Dict(keys, values, LINENO(n), n->n_col_offset,
2323 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002324}
2325
2326static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002327ast_for_genexp(struct compiling *c, const node *n)
2328{
2329 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002330 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002331}
2332
2333static expr_ty
2334ast_for_listcomp(struct compiling *c, const node *n)
2335{
2336 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002337 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002338}
2339
2340static expr_ty
2341ast_for_setcomp(struct compiling *c, const node *n)
2342{
2343 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002344 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002345}
2346
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002347static expr_ty
2348ast_for_setdisplay(struct compiling *c, const node *n)
2349{
2350 int i;
2351 int size;
2352 asdl_seq *elts;
2353
2354 assert(TYPE(n) == (dictorsetmaker));
2355 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2356 elts = _Py_asdl_seq_new(size, c->c_arena);
2357 if (!elts)
2358 return NULL;
2359 for (i = 0; i < NCH(n); i += 2) {
2360 expr_ty expression;
2361 expression = ast_for_expr(c, CHILD(n, i));
2362 if (!expression)
2363 return NULL;
2364 asdl_seq_SET(elts, i / 2, expression);
2365 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002366 return Set(elts, LINENO(n), n->n_col_offset,
2367 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002368}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002369
2370static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371ast_for_atom(struct compiling *c, const node *n)
2372{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002373 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2374 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002375 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 */
2377 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002380 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002381 PyObject *name;
2382 const char *s = STR(ch);
2383 size_t len = strlen(s);
2384 if (len >= 4 && len <= 5) {
2385 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002386 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002387 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002388 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002389 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002390 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002391 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002392 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002393 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002394 }
2395 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002396 if (!name)
2397 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002398 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002399 return Name(name, Load, LINENO(n), n->n_col_offset,
2400 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002401 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002403 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002404 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002405 const char *errtype = NULL;
2406 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2407 errtype = "unicode error";
2408 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2409 errtype = "value error";
2410 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002411 PyObject *type, *value, *tback, *errstr;
2412 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002413 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002414 if (errstr) {
2415 ast_error(c, n, "(%s) %U", errtype, errstr);
2416 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002417 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002418 else {
2419 PyErr_Clear();
2420 ast_error(c, n, "(%s) unknown error", errtype);
2421 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002422 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002423 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002424 Py_XDECREF(tback);
2425 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002426 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002427 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002428 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 }
2430 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002431 PyObject *pynum;
2432 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2433 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2434 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2435 ast_error(c, ch,
2436 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2437 return NULL;
2438 }
2439 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002440 if (!pynum)
2441 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002442
Victor Stinner43d81952013-07-17 00:57:58 +02002443 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2444 Py_DECREF(pynum);
2445 return NULL;
2446 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002447 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002448 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 }
Georg Brandldde00282007-03-18 19:01:53 +00002450 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002451 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002452 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002454 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455
Thomas Wouters89f507f2006-12-13 04:49:30 +00002456 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002457 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2458 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459
Thomas Wouters89f507f2006-12-13 04:49:30 +00002460 if (TYPE(ch) == yield_expr)
2461 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002464 if (NCH(ch) == 1) {
2465 return ast_for_testlist(c, ch);
2466 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002467
Serhiy Storchakab619b092018-11-27 09:40:29 +02002468 if (TYPE(CHILD(ch, 1)) == comp_for) {
2469 return copy_location(ast_for_genexp(c, ch), n);
2470 }
2471 else {
2472 return copy_location(ast_for_testlist(c, ch), n);
2473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002475 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002478 return List(NULL, Load, LINENO(n), n->n_col_offset,
2479 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480
Nick Coghlan650f0d02007-04-15 12:05:43 +00002481 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002482 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2483 asdl_seq *elts = seq_for_testlist(c, ch);
2484 if (!elts)
2485 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002486
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002487 return List(elts, Load, LINENO(n), n->n_col_offset,
2488 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002489 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002490 else {
2491 return copy_location(ast_for_listcomp(c, ch), n);
2492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002494 /* dictorsetmaker: ( ((test ':' test | '**' test)
2495 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2496 * ((test | '*' test)
2497 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002498 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002499 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002500 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002501 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002502 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2503 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002504 }
2505 else {
2506 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2507 if (NCH(ch) == 1 ||
2508 (NCH(ch) > 1 &&
2509 TYPE(CHILD(ch, 1)) == COMMA)) {
2510 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002511 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002512 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002513 else if (NCH(ch) > 1 &&
2514 TYPE(CHILD(ch, 1)) == comp_for) {
2515 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002516 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002517 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002518 else if (NCH(ch) > 3 - is_dict &&
2519 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2520 /* It's a dictionary comprehension. */
2521 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002522 ast_error(c, n,
2523 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002524 return NULL;
2525 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002526 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 }
2528 else {
2529 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002530 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002531 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002532 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002536 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 }
2539}
2540
2541static slice_ty
2542ast_for_slice(struct compiling *c, const node *n)
2543{
2544 node *ch;
2545 expr_ty lower = NULL, upper = NULL, step = NULL;
2546
2547 REQ(n, subscript);
2548
2549 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002550 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 sliceop: ':' [test]
2552 */
2553 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 if (NCH(n) == 1 && TYPE(ch) == test) {
2555 /* 'step' variable hold no significance in terms of being used over
2556 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 if (!step)
2559 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560
Thomas Wouters89f507f2006-12-13 04:49:30 +00002561 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 }
2563
2564 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002565 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 if (!lower)
2567 return NULL;
2568 }
2569
2570 /* If there's an upper bound it's in the second or third position. */
2571 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002572 if (NCH(n) > 1) {
2573 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574
Thomas Wouters89f507f2006-12-13 04:49:30 +00002575 if (TYPE(n2) == test) {
2576 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 if (!upper)
2578 return NULL;
2579 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002582 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583
Thomas Wouters89f507f2006-12-13 04:49:30 +00002584 if (TYPE(n2) == test) {
2585 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 if (!upper)
2587 return NULL;
2588 }
2589 }
2590
2591 ch = CHILD(n, NCH(n) - 1);
2592 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002593 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002594 ch = CHILD(ch, 1);
2595 if (TYPE(ch) == test) {
2596 step = ast_for_expr(c, ch);
2597 if (!step)
2598 return NULL;
2599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 }
2601 }
2602
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002603 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604}
2605
2606static expr_ty
2607ast_for_binop(struct compiling *c, const node *n)
2608{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002609 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002611 BinOp(BinOp(A, op, B), op, C).
2612 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613
Guido van Rossumd8faa362007-04-27 19:54:29 +00002614 int i, nops;
2615 expr_ty expr1, expr2, result;
2616 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617
Guido van Rossumd8faa362007-04-27 19:54:29 +00002618 expr1 = ast_for_expr(c, CHILD(n, 0));
2619 if (!expr1)
2620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621
Guido van Rossumd8faa362007-04-27 19:54:29 +00002622 expr2 = ast_for_expr(c, CHILD(n, 2));
2623 if (!expr2)
2624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
Guido van Rossum495da292019-03-07 12:38:08 -08002626 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002627 if (!newoperator)
2628 return NULL;
2629
2630 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002631 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002632 c->c_arena);
2633 if (!result)
2634 return NULL;
2635
2636 nops = (NCH(n) - 1) / 2;
2637 for (i = 1; i < nops; i++) {
2638 expr_ty tmp_result, tmp;
2639 const node* next_oper = CHILD(n, i * 2 + 1);
2640
Guido van Rossum495da292019-03-07 12:38:08 -08002641 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002642 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 return NULL;
2644
Guido van Rossumd8faa362007-04-27 19:54:29 +00002645 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2646 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 return NULL;
2648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002650 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002651 CHILD(n, i * 2 + 2)->n_end_lineno,
2652 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002653 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002655 return NULL;
2656 result = tmp_result;
2657 }
2658 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659}
2660
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002661static expr_ty
2662ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002665 subscriptlist: subscript (',' subscript)* [',']
2666 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2667 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002668 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002669 REQ(n, trailer);
2670 if (TYPE(CHILD(n, 0)) == LPAR) {
2671 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002672 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2673 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002674 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002675 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002676 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002677 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002678 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2679 if (!attr_id)
2680 return NULL;
2681 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002682 LINENO(n), n->n_col_offset,
2683 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002684 }
2685 else {
2686 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002687 REQ(CHILD(n, 2), RSQB);
2688 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002689 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002690 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2691 if (!slc)
2692 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002693 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002694 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002695 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002696 }
2697 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002699 by treating the sequence as a tuple literal if there are
2700 no slice features.
2701 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002702 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002703 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002704 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002705 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002706 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002707 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002708 if (!slices)
2709 return NULL;
2710 for (j = 0; j < NCH(n); j += 2) {
2711 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002712 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002713 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002714 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002715 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002716 asdl_seq_SET(slices, j / 2, slc);
2717 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002718 if (!simple) {
2719 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002720 Load, LINENO(n), n->n_col_offset,
2721 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002722 }
2723 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002724 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002725 if (!elts)
2726 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002727 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2728 slc = (slice_ty)asdl_seq_GET(slices, j);
2729 assert(slc->kind == Index_kind && slc->v.Index.value);
2730 asdl_seq_SET(elts, j, slc->v.Index.value);
2731 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002732 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2733 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002734 if (!e)
2735 return NULL;
2736 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002737 Load, LINENO(n), n->n_col_offset,
2738 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002739 }
2740 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002741}
2742
2743static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002744ast_for_factor(struct compiling *c, const node *n)
2745{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002746 expr_ty expression;
2747
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002748 expression = ast_for_expr(c, CHILD(n, 1));
2749 if (!expression)
2750 return NULL;
2751
2752 switch (TYPE(CHILD(n, 0))) {
2753 case PLUS:
2754 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002755 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002756 c->c_arena);
2757 case MINUS:
2758 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002759 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002760 c->c_arena);
2761 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002762 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2763 n->n_end_lineno, n->n_end_col_offset,
2764 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002765 }
2766 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2767 TYPE(CHILD(n, 0)));
2768 return NULL;
2769}
2770
2771static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002772ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002773{
Yury Selivanov75445082015-05-11 22:57:16 -04002774 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002775 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002776
2777 REQ(n, atom_expr);
2778 nch = NCH(n);
2779
Guido van Rossum495da292019-03-07 12:38:08 -08002780 if (TYPE(CHILD(n, 0)) == AWAIT) {
2781 if (c->c_feature_version < 5) {
2782 ast_error(c, n,
2783 "Await expressions are only supported in Python 3.5 and greater");
2784 return NULL;
2785 }
Yury Selivanov75445082015-05-11 22:57:16 -04002786 start = 1;
2787 assert(nch > 1);
2788 }
2789
2790 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002791 if (!e)
2792 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002793 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002794 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002795 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002796 return Await(e, LINENO(n), n->n_col_offset,
2797 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002798 }
2799
2800 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002801 node *ch = CHILD(n, i);
2802 if (TYPE(ch) != trailer)
2803 break;
2804 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002805 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002806 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002807 tmp->lineno = e->lineno;
2808 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002809 e = tmp;
2810 }
Yury Selivanov75445082015-05-11 22:57:16 -04002811
2812 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002813 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002814 return Await(e, LINENO(n), n->n_col_offset,
2815 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002816 }
2817 else {
2818 return e;
2819 }
2820}
2821
2822static expr_ty
2823ast_for_power(struct compiling *c, const node *n)
2824{
2825 /* power: atom trailer* ('**' factor)*
2826 */
2827 expr_ty e;
2828 REQ(n, power);
2829 e = ast_for_atom_expr(c, CHILD(n, 0));
2830 if (!e)
2831 return NULL;
2832 if (NCH(n) == 1)
2833 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002834 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2835 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002836 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002837 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002838 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2839 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002840 }
2841 return e;
2842}
2843
Guido van Rossum0368b722007-05-11 16:50:42 +00002844static expr_ty
2845ast_for_starred(struct compiling *c, const node *n)
2846{
2847 expr_ty tmp;
2848 REQ(n, star_expr);
2849
2850 tmp = ast_for_expr(c, CHILD(n, 1));
2851 if (!tmp)
2852 return NULL;
2853
2854 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002855 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2856 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002857}
2858
2859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860/* Do not name a variable 'expr'! Will cause a compile error.
2861*/
2862
2863static expr_ty
2864ast_for_expr(struct compiling *c, const node *n)
2865{
2866 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002867 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002868 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002869 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 and_test: not_test ('and' not_test)*
2872 not_test: 'not' not_test | comparison
2873 comparison: expr (comp_op expr)*
2874 expr: xor_expr ('|' xor_expr)*
2875 xor_expr: and_expr ('^' and_expr)*
2876 and_expr: shift_expr ('&' shift_expr)*
2877 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2878 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002879 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002881 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002882 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002883 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 */
2885
2886 asdl_seq *seq;
2887 int i;
2888
2889 loop:
2890 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002891 case namedexpr_test:
2892 if (NCH(n) == 3)
2893 return ast_for_namedexpr(c, n);
2894 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002896 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002897 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002898 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002900 else if (NCH(n) > 1)
2901 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002902 /* Fallthrough */
2903 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 case and_test:
2905 if (NCH(n) == 1) {
2906 n = CHILD(n, 0);
2907 goto loop;
2908 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002909 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 if (!seq)
2911 return NULL;
2912 for (i = 0; i < NCH(n); i += 2) {
2913 expr_ty e = ast_for_expr(c, CHILD(n, i));
2914 if (!e)
2915 return NULL;
2916 asdl_seq_SET(seq, i / 2, e);
2917 }
2918 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002919 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002920 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002921 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002922 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002923 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2924 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 case not_test:
2926 if (NCH(n) == 1) {
2927 n = CHILD(n, 0);
2928 goto loop;
2929 }
2930 else {
2931 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2932 if (!expression)
2933 return NULL;
2934
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002935 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002936 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002937 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 }
2939 case comparison:
2940 if (NCH(n) == 1) {
2941 n = CHILD(n, 0);
2942 goto loop;
2943 }
2944 else {
2945 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002946 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002947 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002948 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 if (!ops)
2950 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002951 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 return NULL;
2954 }
2955 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002956 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002958 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002959 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002961 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962
2963 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002964 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002968 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 asdl_seq_SET(cmps, i / 2, expression);
2970 }
2971 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002972 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002976 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2977 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979
Guido van Rossum0368b722007-05-11 16:50:42 +00002980 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 /* The next five cases all handle BinOps. The main body of code
2983 is the same in each case, but the switch turned inside out to
2984 reuse the code for each type of operator.
2985 */
2986 case expr:
2987 case xor_expr:
2988 case and_expr:
2989 case shift_expr:
2990 case arith_expr:
2991 case term:
2992 if (NCH(n) == 1) {
2993 n = CHILD(n, 0);
2994 goto loop;
2995 }
2996 return ast_for_binop(c, n);
2997 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002998 node *an = NULL;
2999 node *en = NULL;
3000 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003001 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003002 if (NCH(n) > 1)
3003 an = CHILD(n, 1); /* yield_arg */
3004 if (an) {
3005 en = CHILD(an, NCH(an) - 1);
3006 if (NCH(an) == 2) {
3007 is_from = 1;
3008 exp = ast_for_expr(c, en);
3009 }
3010 else
3011 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003012 if (!exp)
3013 return NULL;
3014 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05003015 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003016 return YieldFrom(exp, LINENO(n), n->n_col_offset,
3017 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
3018 return Yield(exp, LINENO(n), n->n_col_offset,
3019 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003020 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003021 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 if (NCH(n) == 1) {
3023 n = CHILD(n, 0);
3024 goto loop;
3025 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003026 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00003027 case power:
3028 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003030 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 return NULL;
3032 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003033 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 return NULL;
3035}
3036
3037static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02003038ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003039 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040{
3041 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003042 arglist: argument (',' argument)* [',']
3043 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 */
3045
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003046 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003047 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003048 asdl_seq *args;
3049 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050
3051 REQ(n, arglist);
3052
3053 nargs = 0;
3054 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003056 node *ch = CHILD(n, i);
3057 if (TYPE(ch) == argument) {
3058 if (NCH(ch) == 1)
3059 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003060 else if (TYPE(CHILD(ch, 1)) == comp_for) {
3061 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02003062 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003063 ast_error(c, ch, "invalid syntax");
3064 return NULL;
3065 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003066 if (NCH(n) > 1) {
3067 ast_error(c, ch, "Generator expression must be parenthesized");
3068 return NULL;
3069 }
3070 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003071 else if (TYPE(CHILD(ch, 0)) == STAR)
3072 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003073 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3074 nargs++;
3075 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003077 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003078 nkeywords++;
3079 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003082 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003084 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003085 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003087 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003088
3089 nargs = 0; /* positional arguments + iterable argument unpackings */
3090 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3091 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003093 node *ch = CHILD(n, i);
3094 if (TYPE(ch) == argument) {
3095 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003096 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003097 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003098 /* a positional argument */
3099 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003100 if (ndoublestars) {
3101 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003102 "positional argument follows "
3103 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003104 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003105 else {
3106 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003107 "positional argument follows "
3108 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003110 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003111 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003112 e = ast_for_expr(c, chch);
3113 if (!e)
3114 return NULL;
3115 asdl_seq_SET(args, nargs++, e);
3116 }
3117 else if (TYPE(chch) == STAR) {
3118 /* an iterable argument unpacking */
3119 expr_ty starred;
3120 if (ndoublestars) {
3121 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003122 "iterable argument unpacking follows "
3123 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003124 return NULL;
3125 }
3126 e = ast_for_expr(c, CHILD(ch, 1));
3127 if (!e)
3128 return NULL;
3129 starred = Starred(e, Load, LINENO(chch),
3130 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003131 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003132 c->c_arena);
3133 if (!starred)
3134 return NULL;
3135 asdl_seq_SET(args, nargs++, starred);
3136
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003137 }
3138 else if (TYPE(chch) == DOUBLESTAR) {
3139 /* a keyword argument unpacking */
3140 keyword_ty kw;
3141 i++;
3142 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003144 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003145 kw = keyword(NULL, e, c->c_arena);
3146 asdl_seq_SET(keywords, nkeywords++, kw);
3147 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003149 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003150 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003151 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003153 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003154 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003156 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3157 /* treat colon equal as positional argument */
3158 if (nkeywords) {
3159 if (ndoublestars) {
3160 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003161 "positional argument follows "
3162 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003163 }
3164 else {
3165 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003166 "positional argument follows "
3167 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003168 }
3169 return NULL;
3170 }
3171 e = ast_for_namedexpr(c, ch);
3172 if (!e)
3173 return NULL;
3174 asdl_seq_SET(args, nargs++, e);
3175 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003176 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003177 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003178 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003179 identifier key, tmp;
3180 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003182 // To remain LL(1), the grammar accepts any test (basically, any
3183 // expression) in the keyword slot of a call site. So, we need
3184 // to manually enforce that the keyword is a NAME here.
3185 static const int name_tree[] = {
3186 test,
3187 or_test,
3188 and_test,
3189 not_test,
3190 comparison,
3191 expr,
3192 xor_expr,
3193 and_expr,
3194 shift_expr,
3195 arith_expr,
3196 term,
3197 factor,
3198 power,
3199 atom_expr,
3200 atom,
3201 0,
3202 };
3203 node *expr_node = chch;
3204 for (int i = 0; name_tree[i]; i++) {
3205 if (TYPE(expr_node) != name_tree[i])
3206 break;
3207 if (NCH(expr_node) != 1)
3208 break;
3209 expr_node = CHILD(expr_node, 0);
3210 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003211 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003212 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003213 "expression cannot contain assignment, "
3214 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003215 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003216 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003217 key = new_identifier(STR(expr_node), c);
3218 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003219 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003221 if (forbidden_name(c, key, chch, 1)) {
3222 return NULL;
3223 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003224 for (k = 0; k < nkeywords; k++) {
3225 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003226 if (tmp && !PyUnicode_Compare(tmp, key)) {
3227 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003228 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003229 return NULL;
3230 }
3231 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003232 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003234 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003235 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003237 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003238 asdl_seq_SET(keywords, nkeywords++, kw);
3239 }
3240 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 }
3242
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003243 return Call(func, args, keywords, func->lineno, func->col_offset,
3244 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245}
3246
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003248ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003250 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003251 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003253 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003254 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003255 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003256 }
3257 else {
3258 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003259 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003262 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 else {
3264 asdl_seq *tmp = seq_for_testlist(c, n);
3265 if (!tmp)
3266 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003267 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3268 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003270}
3271
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272static stmt_ty
3273ast_for_expr_stmt(struct compiling *c, const node *n)
3274{
3275 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003276 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003277 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3278 annassign: ':' test ['=' (yield_expr|testlist)]
3279 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3280 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3281 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003282 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003284 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003286 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003287 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 if (!e)
3289 return NULL;
3290
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003291 return Expr(e, LINENO(n), n->n_col_offset,
3292 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 }
3294 else if (TYPE(CHILD(n, 1)) == augassign) {
3295 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003296 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003297 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 if (!expr1)
3301 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003302 if(!set_context(c, expr1, Store, ch))
3303 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003304 /* set_context checks that most expressions are not the left side.
3305 Augmented assignments can only have a name, a subscript, or an
3306 attribute on the left, though, so we have to explicitly check for
3307 those. */
3308 switch (expr1->kind) {
3309 case Name_kind:
3310 case Attribute_kind:
3311 case Subscript_kind:
3312 break;
3313 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003314 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003315 return NULL;
3316 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 ch = CHILD(n, 2);
3319 if (TYPE(ch) == testlist)
3320 expr2 = ast_for_testlist(c, ch);
3321 else
3322 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003323 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 return NULL;
3325
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003326 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003327 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328 return NULL;
3329
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003330 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3331 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003333 else if (TYPE(CHILD(n, 1)) == annassign) {
3334 expr_ty expr1, expr2, expr3;
3335 node *ch = CHILD(n, 0);
3336 node *deep, *ann = CHILD(n, 1);
3337 int simple = 1;
3338
Guido van Rossum495da292019-03-07 12:38:08 -08003339 /* AnnAssigns are only allowed in Python 3.6 or greater */
3340 if (c->c_feature_version < 6) {
3341 ast_error(c, ch,
3342 "Variable annotation syntax is only supported in Python 3.6 and greater");
3343 return NULL;
3344 }
3345
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003346 /* we keep track of parens to qualify (x) as expression not name */
3347 deep = ch;
3348 while (NCH(deep) == 1) {
3349 deep = CHILD(deep, 0);
3350 }
3351 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3352 simple = 0;
3353 }
3354 expr1 = ast_for_testlist(c, ch);
3355 if (!expr1) {
3356 return NULL;
3357 }
3358 switch (expr1->kind) {
3359 case Name_kind:
3360 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3361 return NULL;
3362 }
3363 expr1->v.Name.ctx = Store;
3364 break;
3365 case Attribute_kind:
3366 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3367 return NULL;
3368 }
3369 expr1->v.Attribute.ctx = Store;
3370 break;
3371 case Subscript_kind:
3372 expr1->v.Subscript.ctx = Store;
3373 break;
3374 case List_kind:
3375 ast_error(c, ch,
3376 "only single target (not list) can be annotated");
3377 return NULL;
3378 case Tuple_kind:
3379 ast_error(c, ch,
3380 "only single target (not tuple) can be annotated");
3381 return NULL;
3382 default:
3383 ast_error(c, ch,
3384 "illegal target for annotation");
3385 return NULL;
3386 }
3387
3388 if (expr1->kind != Name_kind) {
3389 simple = 0;
3390 }
3391 ch = CHILD(ann, 1);
3392 expr2 = ast_for_expr(c, ch);
3393 if (!expr2) {
3394 return NULL;
3395 }
3396 if (NCH(ann) == 2) {
3397 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003398 LINENO(n), n->n_col_offset,
3399 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003400 }
3401 else {
3402 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003403 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003404 expr3 = ast_for_testlist(c, ch);
3405 }
3406 else {
3407 expr3 = ast_for_expr(c, ch);
3408 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003409 if (!expr3) {
3410 return NULL;
3411 }
3412 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003413 LINENO(n), n->n_col_offset,
3414 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003415 }
3416 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003418 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003419 asdl_seq *targets;
3420 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003422 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423
Thomas Wouters89f507f2006-12-13 04:49:30 +00003424 /* a normal assignment */
3425 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003426
3427 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3428 nch_minus_type = num - has_type_comment;
3429
3430 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003431 if (!targets)
3432 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003433 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003434 expr_ty e;
3435 node *ch = CHILD(n, i);
3436 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003437 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003438 return NULL;
3439 }
3440 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003444 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003445 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003446 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447
Thomas Wouters89f507f2006-12-13 04:49:30 +00003448 asdl_seq_SET(targets, i / 2, e);
3449 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003450 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003451 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 expression = ast_for_testlist(c, value);
3453 else
3454 expression = ast_for_expr(c, value);
3455 if (!expression)
3456 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003457 if (has_type_comment) {
3458 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3459 if (!type_comment)
3460 return NULL;
3461 }
3462 else
3463 type_comment = NULL;
3464 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003465 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467}
3468
Benjamin Peterson78565b22009-06-28 19:19:51 +00003469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003471ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472{
3473 asdl_seq *seq;
3474 int i;
3475 expr_ty e;
3476
3477 REQ(n, exprlist);
3478
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003479 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003483 e = ast_for_expr(c, CHILD(n, i));
3484 if (!e)
3485 return NULL;
3486 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003487 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 }
3490 return seq;
3491}
3492
3493static stmt_ty
3494ast_for_del_stmt(struct compiling *c, const node *n)
3495{
3496 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 /* del_stmt: 'del' exprlist */
3499 REQ(n, del_stmt);
3500
3501 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3502 if (!expr_list)
3503 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003504 return Delete(expr_list, LINENO(n), n->n_col_offset,
3505 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506}
3507
3508static stmt_ty
3509ast_for_flow_stmt(struct compiling *c, const node *n)
3510{
3511 /*
3512 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3513 | yield_stmt
3514 break_stmt: 'break'
3515 continue_stmt: 'continue'
3516 return_stmt: 'return' [testlist]
3517 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003518 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 raise_stmt: 'raise' [test [',' test [',' test]]]
3520 */
3521 node *ch;
3522
3523 REQ(n, flow_stmt);
3524 ch = CHILD(n, 0);
3525 switch (TYPE(ch)) {
3526 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003527 return Break(LINENO(n), n->n_col_offset,
3528 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003530 return Continue(LINENO(n), n->n_col_offset,
3531 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003533 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3534 if (!exp)
3535 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003536 return Expr(exp, LINENO(n), n->n_col_offset,
3537 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 }
3539 case return_stmt:
3540 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003541 return Return(NULL, LINENO(n), n->n_col_offset,
3542 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003544 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 if (!expression)
3546 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003547 return Return(expression, LINENO(n), n->n_col_offset,
3548 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 }
3550 case raise_stmt:
3551 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003552 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3553 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003554 else if (NCH(ch) >= 2) {
3555 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3557 if (!expression)
3558 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003559 if (NCH(ch) == 4) {
3560 cause = ast_for_expr(c, CHILD(ch, 3));
3561 if (!cause)
3562 return NULL;
3563 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003564 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3565 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 }
Stefan Krahf432a322017-08-21 13:09:59 +02003567 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003569 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 "unexpected flow_stmt: %d", TYPE(ch));
3571 return NULL;
3572 }
3573}
3574
3575static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003576alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577{
3578 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003579 import_as_name: NAME ['as' NAME]
3580 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 dotted_name: NAME ('.' NAME)*
3582 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003583 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003584
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 loop:
3586 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003587 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003588 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003589 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003590 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003591 if (!name)
3592 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003593 if (NCH(n) == 3) {
3594 node *str_node = CHILD(n, 2);
3595 str = NEW_IDENTIFIER(str_node);
3596 if (!str)
3597 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003598 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003599 return NULL;
3600 }
3601 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003602 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003603 return NULL;
3604 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003605 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003606 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 case dotted_as_name:
3608 if (NCH(n) == 1) {
3609 n = CHILD(n, 0);
3610 goto loop;
3611 }
3612 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003613 node *asname_node = CHILD(n, 2);
3614 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003615 if (!a)
3616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003618 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003619 if (!a->asname)
3620 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003621 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 return a;
3624 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003626 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003627 node *name_node = CHILD(n, 0);
3628 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003629 if (!name)
3630 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003631 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003632 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003633 return alias(name, NULL, c->c_arena);
3634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 else {
3636 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003637 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003638 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641
3642 len = 0;
3643 for (i = 0; i < NCH(n); i += 2)
3644 /* length of string plus one for the dot */
3645 len += strlen(STR(CHILD(n, i))) + 1;
3646 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003647 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 if (!str)
3649 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003650 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 if (!s)
3652 return NULL;
3653 for (i = 0; i < NCH(n); i += 2) {
3654 char *sch = STR(CHILD(n, i));
3655 strcpy(s, STR(CHILD(n, i)));
3656 s += strlen(sch);
3657 *s++ = '.';
3658 }
3659 --s;
3660 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3662 PyBytes_GET_SIZE(str),
3663 NULL);
3664 Py_DECREF(str);
3665 if (!uni)
3666 return NULL;
3667 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003668 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003669 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3670 Py_DECREF(str);
3671 return NULL;
3672 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003673 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003676 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003677 if (!str)
3678 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003679 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3680 Py_DECREF(str);
3681 return NULL;
3682 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003683 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003685 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686 "unexpected import name: %d", TYPE(n));
3687 return NULL;
3688 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689}
3690
3691static stmt_ty
3692ast_for_import_stmt(struct compiling *c, const node *n)
3693{
3694 /*
3695 import_stmt: import_name | import_from
3696 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003697 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3698 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003700 int lineno;
3701 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 int i;
3703 asdl_seq *aliases;
3704
3705 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003706 lineno = LINENO(n);
3707 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003709 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003711 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003712 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003713 if (!aliases)
3714 return NULL;
3715 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003716 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003717 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003719 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003721 // Even though n is modified above, the end position is not changed
3722 return Import(aliases, lineno, col_offset,
3723 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003725 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003727 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003728 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003729 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003730 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003732 /* Count the number of dots (for relative imports) and check for the
3733 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003734 for (idx = 1; idx < NCH(n); idx++) {
3735 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003736 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3737 if (!mod)
3738 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003739 idx++;
3740 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003741 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003743 ndots += 3;
3744 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003745 } else if (TYPE(CHILD(n, idx)) != DOT) {
3746 break;
3747 }
3748 ndots++;
3749 }
3750 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003751 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003752 case STAR:
3753 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003754 n = CHILD(n, idx);
3755 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003756 break;
3757 case LPAR:
3758 /* from ... import (x, y, z) */
3759 n = CHILD(n, idx + 1);
3760 n_children = NCH(n);
3761 break;
3762 case import_as_names:
3763 /* from ... import x, y, z */
3764 n = CHILD(n, idx);
3765 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003766 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003767 ast_error(c, n,
3768 "trailing comma not allowed without"
3769 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 return NULL;
3771 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003772 break;
3773 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003774 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003775 return NULL;
3776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003778 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003779 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781
3782 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003783 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003784 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003785 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003787 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003789 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003790 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003791 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003792 if (!import_alias)
3793 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003794 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003797 if (mod != NULL)
3798 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003799 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003800 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003801 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 }
Neal Norwitz79792652005-11-14 04:25:03 +00003803 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 "unknown import statement: starts with command '%s'",
3805 STR(CHILD(n, 0)));
3806 return NULL;
3807}
3808
3809static stmt_ty
3810ast_for_global_stmt(struct compiling *c, const node *n)
3811{
3812 /* global_stmt: 'global' NAME (',' NAME)* */
3813 identifier name;
3814 asdl_seq *s;
3815 int i;
3816
3817 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003818 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003822 name = NEW_IDENTIFIER(CHILD(n, i));
3823 if (!name)
3824 return NULL;
3825 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003827 return Global(s, LINENO(n), n->n_col_offset,
3828 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829}
3830
3831static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003832ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3833{
3834 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3835 identifier name;
3836 asdl_seq *s;
3837 int i;
3838
3839 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003840 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003841 if (!s)
3842 return NULL;
3843 for (i = 1; i < NCH(n); i += 2) {
3844 name = NEW_IDENTIFIER(CHILD(n, i));
3845 if (!name)
3846 return NULL;
3847 asdl_seq_SET(s, i / 2, name);
3848 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003849 return Nonlocal(s, LINENO(n), n->n_col_offset,
3850 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003851}
3852
3853static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854ast_for_assert_stmt(struct compiling *c, const node *n)
3855{
3856 /* assert_stmt: 'assert' test [',' test] */
3857 REQ(n, assert_stmt);
3858 if (NCH(n) == 2) {
3859 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3860 if (!expression)
3861 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003862 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3863 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 }
3865 else if (NCH(n) == 4) {
3866 expr_ty expr1, expr2;
3867
3868 expr1 = ast_for_expr(c, CHILD(n, 1));
3869 if (!expr1)
3870 return NULL;
3871 expr2 = ast_for_expr(c, CHILD(n, 3));
3872 if (!expr2)
3873 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003875 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3876 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 }
Neal Norwitz79792652005-11-14 04:25:03 +00003878 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 "improper number of parts to 'assert' statement: %d",
3880 NCH(n));
3881 return NULL;
3882}
3883
3884static asdl_seq *
3885ast_for_suite(struct compiling *c, const node *n)
3886{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003887 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003888 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 stmt_ty s;
3890 int i, total, num, end, pos = 0;
3891 node *ch;
3892
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003893 if (TYPE(n) != func_body_suite) {
3894 REQ(n, suite);
3895 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896
3897 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003898 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003902 n = CHILD(n, 0);
3903 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003905 */
3906 end = NCH(n) - 1;
3907 if (TYPE(CHILD(n, end - 1)) == SEMI)
3908 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003910 for (i = 0; i < end; i += 2) {
3911 ch = CHILD(n, i);
3912 s = ast_for_stmt(c, ch);
3913 if (!s)
3914 return NULL;
3915 asdl_seq_SET(seq, pos++, s);
3916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 }
3918 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003919 i = 2;
3920 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3921 i += 2;
3922 REQ(CHILD(n, 2), NEWLINE);
3923 }
3924
3925 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003926 ch = CHILD(n, i);
3927 REQ(ch, stmt);
3928 num = num_stmts(ch);
3929 if (num == 1) {
3930 /* small_stmt or compound_stmt with only one child */
3931 s = ast_for_stmt(c, ch);
3932 if (!s)
3933 return NULL;
3934 asdl_seq_SET(seq, pos++, s);
3935 }
3936 else {
3937 int j;
3938 ch = CHILD(ch, 0);
3939 REQ(ch, simple_stmt);
3940 for (j = 0; j < NCH(ch); j += 2) {
3941 /* statement terminates with a semi-colon ';' */
3942 if (NCH(CHILD(ch, j)) == 0) {
3943 assert((j + 1) == NCH(ch));
3944 break;
3945 }
3946 s = ast_for_stmt(c, CHILD(ch, j));
3947 if (!s)
3948 return NULL;
3949 asdl_seq_SET(seq, pos++, s);
3950 }
3951 }
3952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 }
3954 assert(pos == seq->size);
3955 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956}
3957
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003958static void
3959get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3960{
Pablo Galindo46a97922019-02-19 22:51:53 +00003961 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003962 // There must be no empty suites.
3963 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003964 stmt_ty last = asdl_seq_GET(s, tot - 1);
3965 *end_lineno = last->end_lineno;
3966 *end_col_offset = last->end_col_offset;
3967}
3968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969static stmt_ty
3970ast_for_if_stmt(struct compiling *c, const node *n)
3971{
3972 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3973 ['else' ':' suite]
3974 */
3975 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003976 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977
3978 REQ(n, if_stmt);
3979
3980 if (NCH(n) == 4) {
3981 expr_ty expression;
3982 asdl_seq *suite_seq;
3983
3984 expression = ast_for_expr(c, CHILD(n, 1));
3985 if (!expression)
3986 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003988 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003990 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991
Guido van Rossumd8faa362007-04-27 19:54:29 +00003992 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003993 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003994 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003995
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996 s = STR(CHILD(n, 4));
3997 /* s[2], the third character in the string, will be
3998 's' for el_s_e, or
3999 'i' for el_i_f
4000 */
4001 if (s[2] == 's') {
4002 expr_ty expression;
4003 asdl_seq *seq1, *seq2;
4004
4005 expression = ast_for_expr(c, CHILD(n, 1));
4006 if (!expression)
4007 return NULL;
4008 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004009 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 return NULL;
4011 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004012 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004014 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015
Guido van Rossumd8faa362007-04-27 19:54:29 +00004016 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004017 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004018 }
4019 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004020 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004021 expr_ty expression;
4022 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004023 asdl_seq *orelse = NULL;
4024 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 /* must reference the child n_elif+1 since 'else' token is third,
4026 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004027 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
4028 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
4029 has_else = 1;
4030 n_elif -= 3;
4031 }
4032 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033
Thomas Wouters89f507f2006-12-13 04:49:30 +00004034 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004035 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004037 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004038 if (!orelse)
4039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004041 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004043 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
4044 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004046 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4047 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004049 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 asdl_seq_SET(orelse, 0,
4052 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 LINENO(CHILD(n, NCH(n) - 6)),
4054 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004055 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004056 /* the just-created orelse handled the last elif */
4057 n_elif--;
4058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059
Thomas Wouters89f507f2006-12-13 04:49:30 +00004060 for (i = 0; i < n_elif; i++) {
4061 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004062 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004063 if (!newobj)
4064 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004066 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004069 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004072 if (orelse != NULL) {
4073 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4074 } else {
4075 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4076 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004077 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004079 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004080 CHILD(n, off)->n_col_offset,
4081 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004082 orelse = newobj;
4083 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004084 expression = ast_for_expr(c, CHILD(n, 1));
4085 if (!expression)
4086 return NULL;
4087 suite_seq = ast_for_suite(c, CHILD(n, 3));
4088 if (!suite_seq)
4089 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004090 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004091 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004092 LINENO(n), n->n_col_offset,
4093 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004095
4096 PyErr_Format(PyExc_SystemError,
4097 "unexpected token in 'if' statement: %s", s);
4098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004099}
4100
4101static stmt_ty
4102ast_for_while_stmt(struct compiling *c, const node *n)
4103{
4104 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4105 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004106 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004107
4108 if (NCH(n) == 4) {
4109 expr_ty expression;
4110 asdl_seq *suite_seq;
4111
4112 expression = ast_for_expr(c, CHILD(n, 1));
4113 if (!expression)
4114 return NULL;
4115 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004116 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004118 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4119 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4120 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121 }
4122 else if (NCH(n) == 7) {
4123 expr_ty expression;
4124 asdl_seq *seq1, *seq2;
4125
4126 expression = ast_for_expr(c, CHILD(n, 1));
4127 if (!expression)
4128 return NULL;
4129 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004130 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 return NULL;
4132 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004133 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004135 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004137 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4138 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004140
4141 PyErr_Format(PyExc_SystemError,
4142 "wrong number of tokens for 'while' statement: %d",
4143 NCH(n));
4144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145}
4146
4147static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004148ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004149{
guoci90fc8982018-09-11 17:45:45 -04004150 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004151 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004152 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004153 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004154 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004155 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004156 int has_type_comment;
4157 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004158
4159 if (is_async && c->c_feature_version < 5) {
4160 ast_error(c, n,
4161 "Async for loops are only supported in Python 3.5 and greater");
4162 return NULL;
4163 }
4164
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004165 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004166 REQ(n, for_stmt);
4167
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004168 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4169
4170 if (NCH(n) == 9 + has_type_comment) {
4171 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172 if (!seq)
4173 return NULL;
4174 }
4175
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004176 node_target = CHILD(n, 1);
4177 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004178 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004180 /* Check the # of children rather than the length of _target, since
4181 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004182 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004183 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004184 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004186 target = Tuple(_target, Store, first->lineno, first->col_offset,
4187 node_target->n_end_lineno, node_target->n_end_col_offset,
4188 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004190 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004191 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004193 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004194 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 return NULL;
4196
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004197 if (seq != NULL) {
4198 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4199 } else {
4200 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4201 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004202
4203 if (has_type_comment) {
4204 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4205 if (!type_comment)
4206 return NULL;
4207 }
4208 else
4209 type_comment = NULL;
4210
Yury Selivanov75445082015-05-11 22:57:16 -04004211 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004212 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004213 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004214 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004215 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004216 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004217 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004218 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004219}
4220
4221static excepthandler_ty
4222ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4223{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004224 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004225 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004226 REQ(exc, except_clause);
4227 REQ(body, suite);
4228
4229 if (NCH(exc) == 1) {
4230 asdl_seq *suite_seq = ast_for_suite(c, body);
4231 if (!suite_seq)
4232 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004233 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004234
Neal Norwitzad74aa82008-03-31 05:14:30 +00004235 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004236 exc->n_col_offset,
4237 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004238 }
4239 else if (NCH(exc) == 2) {
4240 expr_ty expression;
4241 asdl_seq *suite_seq;
4242
4243 expression = ast_for_expr(c, CHILD(exc, 1));
4244 if (!expression)
4245 return NULL;
4246 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004247 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004248 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004249 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004250
Neal Norwitzad74aa82008-03-31 05:14:30 +00004251 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004252 exc->n_col_offset,
4253 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004254 }
4255 else if (NCH(exc) == 4) {
4256 asdl_seq *suite_seq;
4257 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004258 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004259 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004260 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004261 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004262 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004263 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004264 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004265 return NULL;
4266 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004267 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004268 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004269 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004270
Neal Norwitzad74aa82008-03-31 05:14:30 +00004271 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004272 exc->n_col_offset,
4273 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004274 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004275
4276 PyErr_Format(PyExc_SystemError,
4277 "wrong number of children for 'except' clause: %d",
4278 NCH(exc));
4279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004280}
4281
4282static stmt_ty
4283ast_for_try_stmt(struct compiling *c, const node *n)
4284{
Neal Norwitzf599f422005-12-17 21:33:47 +00004285 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004286 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004287 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004288 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004289
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004290 REQ(n, try_stmt);
4291
Neal Norwitzf599f422005-12-17 21:33:47 +00004292 body = ast_for_suite(c, CHILD(n, 2));
4293 if (body == NULL)
4294 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004295
Neal Norwitzf599f422005-12-17 21:33:47 +00004296 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4297 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4298 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4299 /* we can assume it's an "else",
4300 because nch >= 9 for try-else-finally and
4301 it would otherwise have a type of except_clause */
4302 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4303 if (orelse == NULL)
4304 return NULL;
4305 n_except--;
4306 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004307
Neal Norwitzf599f422005-12-17 21:33:47 +00004308 finally = ast_for_suite(c, CHILD(n, nch - 1));
4309 if (finally == NULL)
4310 return NULL;
4311 n_except--;
4312 }
4313 else {
4314 /* we can assume it's an "else",
4315 otherwise it would have a type of except_clause */
4316 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4317 if (orelse == NULL)
4318 return NULL;
4319 n_except--;
4320 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004321 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004322 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004323 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004324 return NULL;
4325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326
Neal Norwitzf599f422005-12-17 21:33:47 +00004327 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004328 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004329 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004330 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004331 if (handlers == NULL)
4332 return NULL;
4333
4334 for (i = 0; i < n_except; i++) {
4335 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4336 CHILD(n, 5 + i * 3));
4337 if (!e)
4338 return NULL;
4339 asdl_seq_SET(handlers, i, e);
4340 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004341 }
4342
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004343 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004344 if (finally != NULL) {
4345 // finally is always last
4346 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4347 } else if (orelse != NULL) {
4348 // otherwise else is last
4349 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4350 } else {
4351 // inline the get_last_end_pos logic due to layout mismatch
4352 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4353 end_lineno = last_handler->end_lineno;
4354 end_col_offset = last_handler->end_col_offset;
4355 }
4356 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4357 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004358}
4359
Georg Brandl0c315622009-05-25 21:10:36 +00004360/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004361static withitem_ty
4362ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004363{
4364 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004365
Georg Brandl0c315622009-05-25 21:10:36 +00004366 REQ(n, with_item);
4367 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004368 if (!context_expr)
4369 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004370 if (NCH(n) == 3) {
4371 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004372
4373 if (!optional_vars) {
4374 return NULL;
4375 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004376 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004377 return NULL;
4378 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004379 }
4380
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004381 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004382}
4383
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004384/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004385static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004386ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004387{
guoci90fc8982018-09-11 17:45:45 -04004388 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004389 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004390 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004391 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004392
Guido van Rossum495da292019-03-07 12:38:08 -08004393 if (is_async && c->c_feature_version < 5) {
4394 ast_error(c, n,
4395 "Async with statements are only supported in Python 3.5 and greater");
4396 return NULL;
4397 }
4398
Georg Brandl0c315622009-05-25 21:10:36 +00004399 REQ(n, with_stmt);
4400
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004401 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4402 nch_minus_type = NCH(n) - has_type_comment;
4403
4404 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004405 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004406 if (!items)
4407 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004408 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004409 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4410 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004411 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004412 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004413 }
4414
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004415 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4416 if (!body)
4417 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004418 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004419
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004420 if (has_type_comment) {
4421 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4422 if (!type_comment)
4423 return NULL;
4424 }
4425 else
4426 type_comment = NULL;
4427
Yury Selivanov75445082015-05-11 22:57:16 -04004428 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004429 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004430 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004431 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004432 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004433 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004434}
4435
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004436static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004437ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004438{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004439 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004440 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004441 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004442 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004443 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004444
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445 REQ(n, classdef);
4446
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004447 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004448 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004449 if (!s)
4450 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004451 get_last_end_pos(s, &end_lineno, &end_col_offset);
4452
Benjamin Peterson30760062008-11-25 04:02:28 +00004453 classname = NEW_IDENTIFIER(CHILD(n, 1));
4454 if (!classname)
4455 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004456 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004457 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004458 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004459 LINENO(n), n->n_col_offset,
4460 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004462
4463 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004464 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004465 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004466 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004467 get_last_end_pos(s, &end_lineno, &end_col_offset);
4468
Benjamin Peterson30760062008-11-25 04:02:28 +00004469 classname = NEW_IDENTIFIER(CHILD(n, 1));
4470 if (!classname)
4471 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004472 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004473 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004474 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004475 LINENO(n), n->n_col_offset,
4476 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004477 }
4478
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004479 /* class NAME '(' arglist ')' ':' suite */
4480 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004481 {
4482 PyObject *dummy_name;
4483 expr_ty dummy;
4484 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4485 if (!dummy_name)
4486 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004487 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4488 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4489 c->c_arena);
4490 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004491 if (!call)
4492 return NULL;
4493 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004494 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004495 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004496 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004497 get_last_end_pos(s, &end_lineno, &end_col_offset);
4498
Benjamin Peterson30760062008-11-25 04:02:28 +00004499 classname = NEW_IDENTIFIER(CHILD(n, 1));
4500 if (!classname)
4501 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004502 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004503 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004504
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004505 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004506 decorator_seq, LINENO(n), n->n_col_offset,
4507 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004508}
4509
4510static stmt_ty
4511ast_for_stmt(struct compiling *c, const node *n)
4512{
4513 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004514 assert(NCH(n) == 1);
4515 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004516 }
4517 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004518 assert(num_stmts(n) == 1);
4519 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004520 }
4521 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004522 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004523 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4524 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004525 */
4526 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004527 case expr_stmt:
4528 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004529 case del_stmt:
4530 return ast_for_del_stmt(c, n);
4531 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004532 return Pass(LINENO(n), n->n_col_offset,
4533 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004534 case flow_stmt:
4535 return ast_for_flow_stmt(c, n);
4536 case import_stmt:
4537 return ast_for_import_stmt(c, n);
4538 case global_stmt:
4539 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004540 case nonlocal_stmt:
4541 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004542 case assert_stmt:
4543 return ast_for_assert_stmt(c, n);
4544 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004545 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004546 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4547 TYPE(n), NCH(n));
4548 return NULL;
4549 }
4550 }
4551 else {
4552 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004553 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004554 */
4555 node *ch = CHILD(n, 0);
4556 REQ(n, compound_stmt);
4557 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004558 case if_stmt:
4559 return ast_for_if_stmt(c, ch);
4560 case while_stmt:
4561 return ast_for_while_stmt(c, ch);
4562 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004563 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004564 case try_stmt:
4565 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004566 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004567 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004568 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004569 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004570 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004571 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 case decorated:
4573 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004574 case async_stmt:
4575 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004576 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004577 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004578 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004579 TYPE(n), NCH(n));
4580 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004582 }
4583}
4584
4585static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004586parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004587{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004588 const char *end;
4589 long x;
4590 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004591 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004592 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004593
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004594 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004595 errno = 0;
4596 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004597 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004598 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004599 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004600 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004601 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004602 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004603 }
4604 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004605 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004606 if (*end == '\0') {
4607 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004608 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004609 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004610 }
4611 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004612 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004613 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004614 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4615 if (compl.imag == -1.0 && PyErr_Occurred())
4616 return NULL;
4617 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004618 }
4619 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004620 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004621 dx = PyOS_string_to_double(s, NULL, NULL);
4622 if (dx == -1.0 && PyErr_Occurred())
4623 return NULL;
4624 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004625 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004626}
4627
4628static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004629parsenumber(struct compiling *c, const char *s)
4630{
4631 char *dup, *end;
4632 PyObject *res = NULL;
4633
4634 assert(s != NULL);
4635
4636 if (strchr(s, '_') == NULL) {
4637 return parsenumber_raw(c, s);
4638 }
4639 /* Create a duplicate without underscores. */
4640 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004641 if (dup == NULL) {
4642 return PyErr_NoMemory();
4643 }
Brett Cannona721aba2016-09-09 14:57:09 -07004644 end = dup;
4645 for (; *s; s++) {
4646 if (*s != '_') {
4647 *end++ = *s;
4648 }
4649 }
4650 *end = '\0';
4651 res = parsenumber_raw(c, dup);
4652 PyMem_Free(dup);
4653 return res;
4654}
4655
4656static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004657decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004658{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004659 const char *s, *t;
4660 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004661 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4662 while (s < end && (*s & 0x80)) s++;
4663 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004664 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004665}
4666
Eric V. Smith56466482016-10-31 14:46:26 -04004667static int
4668warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004669 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004670{
4671 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4672 first_invalid_escape_char);
4673 if (msg == NULL) {
4674 return -1;
4675 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004676 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004677 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004678 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004679 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004680 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4681 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004682 to get a more accurate error report */
4683 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004684 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004685 }
4686 Py_DECREF(msg);
4687 return -1;
4688 }
4689 Py_DECREF(msg);
4690 return 0;
4691}
4692
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004693static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004694decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4695 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004696{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004697 PyObject *v, *u;
4698 char *buf;
4699 char *p;
4700 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004701
Benjamin Peterson202803a2016-02-25 22:34:45 -08004702 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004703 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004704 return NULL;
4705 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4706 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4707 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4708 if (u == NULL)
4709 return NULL;
4710 p = buf = PyBytes_AsString(u);
4711 end = s + len;
4712 while (s < end) {
4713 if (*s == '\\') {
4714 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004715 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004716 strcpy(p, "u005c");
4717 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004718 if (s >= end)
4719 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004720 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004721 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004722 if (*s & 0x80) { /* XXX inefficient */
4723 PyObject *w;
4724 int kind;
4725 void *data;
4726 Py_ssize_t len, i;
4727 w = decode_utf8(c, &s, end);
4728 if (w == NULL) {
4729 Py_DECREF(u);
4730 return NULL;
4731 }
4732 kind = PyUnicode_KIND(w);
4733 data = PyUnicode_DATA(w);
4734 len = PyUnicode_GET_LENGTH(w);
4735 for (i = 0; i < len; i++) {
4736 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4737 sprintf(p, "\\U%08x", chr);
4738 p += 10;
4739 }
4740 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004741 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004742 Py_DECREF(w);
4743 } else {
4744 *p++ = *s++;
4745 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004746 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004747 len = p - buf;
4748 s = buf;
4749
Eric V. Smith56466482016-10-31 14:46:26 -04004750 const char *first_invalid_escape;
4751 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4752
4753 if (v != NULL && first_invalid_escape != NULL) {
4754 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4755 /* We have not decref u before because first_invalid_escape points
4756 inside u. */
4757 Py_XDECREF(u);
4758 Py_DECREF(v);
4759 return NULL;
4760 }
4761 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004762 Py_XDECREF(u);
4763 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004764}
4765
Eric V. Smith56466482016-10-31 14:46:26 -04004766static PyObject *
4767decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4768 size_t len)
4769{
4770 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004771 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004772 &first_invalid_escape);
4773 if (result == NULL)
4774 return NULL;
4775
4776 if (first_invalid_escape != NULL) {
4777 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4778 Py_DECREF(result);
4779 return NULL;
4780 }
4781 }
4782 return result;
4783}
4784
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004785/* Shift locations for the given node and all its children by adding `lineno`
4786 and `col_offset` to existing locations. */
4787static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4788{
4789 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004790 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004791 for (int i = 0; i < NCH(n); ++i) {
4792 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4793 /* Shifting column offsets unnecessary if there's been newlines. */
4794 col_offset = 0;
4795 }
4796 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4797 }
4798 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004799 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004800}
4801
4802/* Fix locations for the given node and its children.
4803
4804 `parent` is the enclosing node.
4805 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004806 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004807*/
4808static void
4809fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4810{
4811 char *substr = NULL;
4812 char *start;
4813 int lines = LINENO(parent) - 1;
4814 int cols = parent->n_col_offset;
4815 /* Find the full fstring to fix location information in `n`. */
4816 while (parent && parent->n_type != STRING)
4817 parent = parent->n_child;
4818 if (parent && parent->n_str) {
4819 substr = strstr(parent->n_str, expr_str);
4820 if (substr) {
4821 start = substr;
4822 while (start > parent->n_str) {
4823 if (start[0] == '\n')
4824 break;
4825 start--;
4826 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004827 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004828 /* adjust the start based on the number of newlines encountered
4829 before the f-string expression */
4830 for (char* p = parent->n_str; p < substr; p++) {
4831 if (*p == '\n') {
4832 lines++;
4833 }
4834 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004835 }
4836 }
4837 fstring_shift_node_locations(n, lines, cols);
4838}
4839
Eric V. Smith451d0e32016-09-09 21:56:20 -04004840/* Compile this expression in to an expr_ty. Add parens around the
4841 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004842static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004843fstring_compile_expr(const char *expr_start, const char *expr_end,
4844 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004845
Eric V. Smith235a6f02015-09-19 14:51:32 -04004846{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004847 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004848 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004849 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004850 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004851 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852
Eric V. Smith1d44c412015-09-23 07:49:00 -04004853 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004854 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004855 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4856 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004857
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004858 /* If the substring is all whitespace, it's an error. We need to catch this
4859 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4860 because turning the expression '' in to '()' would go from being invalid
4861 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004862 for (s = expr_start; s != expr_end; s++) {
4863 char c = *s;
4864 /* The Python parser ignores only the following whitespace
4865 characters (\r already is converted to \n). */
4866 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004867 break;
4868 }
4869 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004870 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004871 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004872 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004873 }
4874
Eric V. Smith451d0e32016-09-09 21:56:20 -04004875 len = expr_end - expr_start;
4876 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4877 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004878 if (str == NULL) {
4879 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004880 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004881 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004882
Eric V. Smith451d0e32016-09-09 21:56:20 -04004883 str[0] = '(';
4884 memcpy(str+1, expr_start, len);
4885 str[len+1] = ')';
4886 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004887
Victor Stinner37d66d72019-06-13 02:16:41 +02004888 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004889 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004890 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4891 Py_eval_input, 0);
4892 if (!mod_n) {
4893 PyMem_RawFree(str);
4894 return NULL;
4895 }
4896 /* Reuse str to find the correct column offset. */
4897 str[0] = '{';
4898 str[len+1] = '}';
4899 fstring_fix_node_location(n, mod_n, str);
4900 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004901 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004902 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004903 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004904 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906}
4907
4908/* Return -1 on error.
4909
4910 Return 0 if we reached the end of the literal.
4911
4912 Return 1 if we haven't reached the end of the literal, but we want
4913 the caller to process the literal up to this point. Used for
4914 doubled braces.
4915*/
4916static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004917fstring_find_literal(const char **str, const char *end, int raw,
4918 PyObject **literal, int recurse_lvl,
4919 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004920{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004921 /* Get any literal string. It ends when we hit an un-doubled left
4922 brace (which isn't part of a unicode name escape such as
4923 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004925 const char *s = *str;
4926 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927 int result = 0;
4928
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004930 while (s < end) {
4931 char ch = *s++;
4932 if (!raw && ch == '\\' && s < end) {
4933 ch = *s++;
4934 if (ch == 'N') {
4935 if (s < end && *s++ == '{') {
4936 while (s < end && *s++ != '}') {
4937 }
4938 continue;
4939 }
4940 break;
4941 }
4942 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4943 return -1;
4944 }
4945 }
4946 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947 /* Check for doubled braces, but only at the top level. If
4948 we checked at every level, then f'{0:{3}}' would fail
4949 with the two closing braces. */
4950 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004951 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004952 /* We're going to tell the caller that the literal ends
4953 here, but that they should continue scanning. But also
4954 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004955 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956 result = 1;
4957 goto done;
4958 }
4959
4960 /* Where a single '{' is the start of a new expression, a
4961 single '}' is not allowed. */
4962 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004963 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004964 ast_error(c, n, "f-string: single '}' is not allowed");
4965 return -1;
4966 }
4967 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004968 /* We're either at a '{', which means we're starting another
4969 expression; or a '}', which means we're at the end of this
4970 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004971 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004972 break;
4973 }
4974 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004975 *str = s;
4976 assert(s <= end);
4977 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004978done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004979 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004980 if (raw)
4981 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004982 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 NULL, NULL);
4984 else
Eric V. Smith56466482016-10-31 14:46:26 -04004985 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004986 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 if (!*literal)
4988 return -1;
4989 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990 return result;
4991}
4992
4993/* Forward declaration because parsing is recursive. */
4994static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004995fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 struct compiling *c, const node *n);
4997
Eric V. Smith451d0e32016-09-09 21:56:20 -04004998/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004999 expression (so it must be a '{'). Returns the FormattedValue node, which
5000 includes the expression, conversion character, format_spec expression, and
5001 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04005002
5003 Note that I don't do a perfect job here: I don't make sure that a
5004 closing brace doesn't match an opening paren, for example. It
5005 doesn't need to error on all invalid expressions, just correctly
5006 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005007 will be caught when we parse it later.
5008
5009 *expression is set to the expression. For an '=' "debug" expression,
5010 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005011 including the '=' and any whitespace around it, as a string object). If
5012 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005014fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005015 PyObject **expr_text, expr_ty *expression,
5016 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005017{
5018 /* Return -1 on error, else 0. */
5019
Eric V. Smith451d0e32016-09-09 21:56:20 -04005020 const char *expr_start;
5021 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005022 expr_ty simple_expression;
5023 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005024 int conversion = -1; /* The conversion char. Use default if not
5025 specified, or !r if using = and no format
5026 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027
5028 /* 0 if we're not in a string, else the quote char we're trying to
5029 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005030 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005031
5032 /* If we're inside a string, 1=normal, 3=triple-quoted. */
5033 int string_type = 0;
5034
5035 /* Keep track of nesting level for braces/parens/brackets in
5036 expressions. */
5037 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005038 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04005039
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005040 *expr_text = NULL;
5041
Eric V. Smith235a6f02015-09-19 14:51:32 -04005042 /* Can only nest one level deep. */
5043 if (recurse_lvl >= 2) {
5044 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005045 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 }
5047
5048 /* The first char must be a left brace, or we wouldn't have gotten
5049 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 assert(**str == '{');
5051 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052
Eric V. Smith451d0e32016-09-09 21:56:20 -04005053 expr_start = *str;
5054 for (; *str < end; (*str)++) {
5055 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056
5057 /* Loop invariants. */
5058 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005060 if (quote_char)
5061 assert(string_type == 1 || string_type == 3);
5062 else
5063 assert(string_type == 0);
5064
Eric V. Smith451d0e32016-09-09 21:56:20 -04005065 ch = **str;
5066 /* Nowhere inside an expression is a backslash allowed. */
5067 if (ch == '\\') {
5068 /* Error: can't include a backslash character, inside
5069 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005070 ast_error(c, n,
5071 "f-string expression part "
5072 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005073 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005074 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075 if (quote_char) {
5076 /* We're inside a string. See if we're at the end. */
5077 /* This code needs to implement the same non-error logic
5078 as tok_get from tokenizer.c, at the letter_quote
5079 label. To actually share that code would be a
5080 nightmare. But, it's unlikely to change and is small,
5081 so duplicate it here. Note we don't need to catch all
5082 of the errors, since they'll be caught when parsing the
5083 expression. We just need to match the non-error
5084 cases. Thus we can ignore \n in single-quoted strings,
5085 for example. Or non-terminated strings. */
5086 if (ch == quote_char) {
5087 /* Does this match the string_type (single or triple
5088 quoted)? */
5089 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005091 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005093 string_type = 0;
5094 quote_char = 0;
5095 continue;
5096 }
5097 } else {
5098 /* We're at the end of a normal string. */
5099 quote_char = 0;
5100 string_type = 0;
5101 continue;
5102 }
5103 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 } else if (ch == '\'' || ch == '"') {
5105 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005106 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 } else {
5110 /* Start of a normal string. */
5111 string_type = 1;
5112 }
5113 /* Start looking for the end of the string. */
5114 quote_char = ch;
5115 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005116 if (nested_depth >= MAXLEVEL) {
5117 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005118 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005119 }
5120 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005121 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005122 } else if (ch == '#') {
5123 /* Error: can't include a comment character, inside parens
5124 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005125 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005126 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005127 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005128 (ch == '!' || ch == ':' || ch == '}' ||
5129 ch == '=' || ch == '>' || ch == '<')) {
5130 /* See if there's a next character. */
5131 if (*str+1 < end) {
5132 char next = *(*str+1);
5133
5134 /* For "!=". since '=' is not an allowed conversion character,
5135 nothing is lost in this test. */
5136 if ((ch == '!' && next == '=') || /* != */
5137 (ch == '=' && next == '=') || /* == */
5138 (ch == '<' && next == '=') || /* <= */
5139 (ch == '>' && next == '=') /* >= */
5140 ) {
5141 *str += 1;
5142 continue;
5143 }
5144 /* Don't get out of the loop for these, if they're single
5145 chars (not part of 2-char tokens). If by themselves, they
5146 don't end an expression (unlike say '!'). */
5147 if (ch == '>' || ch == '<') {
5148 continue;
5149 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005151
Eric V. Smith235a6f02015-09-19 14:51:32 -04005152 /* Normal way out of this loop. */
5153 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005154 } else if (ch == ']' || ch == '}' || ch == ')') {
5155 if (!nested_depth) {
5156 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005157 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005158 }
5159 nested_depth--;
5160 int opening = parenstack[nested_depth];
5161 if (!((opening == '(' && ch == ')') ||
5162 (opening == '[' && ch == ']') ||
5163 (opening == '{' && ch == '}')))
5164 {
5165 ast_error(c, n,
5166 "f-string: closing parenthesis '%c' "
5167 "does not match opening parenthesis '%c'",
5168 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005169 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005170 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005171 } else {
5172 /* Just consume this char and loop around. */
5173 }
5174 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005175 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005176 /* If we leave this loop in a string or with mismatched parens, we
5177 don't care. We'll get a syntax error when compiling the
5178 expression. But, we can produce a better error message, so
5179 let's just do that.*/
5180 if (quote_char) {
5181 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005182 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005183 }
5184 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005185 int opening = parenstack[nested_depth - 1];
5186 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005187 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005188 }
5189
Eric V. Smith451d0e32016-09-09 21:56:20 -04005190 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005191 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005192
5193 /* Compile the expression as soon as possible, so we show errors
5194 related to the expression before errors related to the
5195 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005196 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005197 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005198 goto error;
5199
5200 /* Check for =, which puts the text value of the expression in
5201 expr_text. */
5202 if (**str == '=') {
5203 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005204
5205 /* Skip over ASCII whitespace. No need to test for end of string
5206 here, since we know there's at least a trailing quote somewhere
5207 ahead. */
5208 while (Py_ISSPACE(**str)) {
5209 *str += 1;
5210 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005211
5212 /* Set *expr_text to the text of the expression. */
5213 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5214 if (!*expr_text) {
5215 goto error;
5216 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005217 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005218
5219 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005220 if (**str == '!') {
5221 *str += 1;
5222 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005223 goto unexpected_end_of_string;
5224
Eric V. Smith451d0e32016-09-09 21:56:20 -04005225 conversion = **str;
5226 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005227
5228 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005229 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005230 ast_error(c, n,
5231 "f-string: invalid conversion character: "
5232 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005233 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005234 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005235
5236 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005237
5238 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005239 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005240 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005241 if (**str == ':') {
5242 *str += 1;
5243 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005244 goto unexpected_end_of_string;
5245
5246 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005247 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005248 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005249 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005250 }
5251
Eric V. Smith451d0e32016-09-09 21:56:20 -04005252 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005253 goto unexpected_end_of_string;
5254
5255 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005256 assert(*str < end);
5257 assert(**str == '}');
5258 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005259
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005260 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005261 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005262 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005263 conversion = 'r';
5264 }
5265
Eric V. Smith451d0e32016-09-09 21:56:20 -04005266 /* And now create the FormattedValue node that represents this
5267 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005268 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005269 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005270 n->n_col_offset, n->n_end_lineno,
5271 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005272 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005273 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005274
5275 return 0;
5276
5277unexpected_end_of_string:
5278 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005279 /* Falls through to error. */
5280
5281error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005282 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005283 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005284
Eric V. Smith235a6f02015-09-19 14:51:32 -04005285}
5286
5287/* Return -1 on error.
5288
5289 Return 0 if we have a literal (possible zero length) and an
5290 expression (zero length if at the end of the string.
5291
5292 Return 1 if we have a literal, but no expression, and we want the
5293 caller to call us again. This is used to deal with doubled
5294 braces.
5295
5296 When called multiple times on the string 'a{{b{0}c', this function
5297 will return:
5298
5299 1. the literal 'a{' with no expression, and a return value
5300 of 1. Despite the fact that there's no expression, the return
5301 value of 1 means we're not finished yet.
5302
5303 2. the literal 'b' and the expression '0', with a return value of
5304 0. The fact that there's an expression means we're not finished.
5305
5306 3. literal 'c' with no expression and a return value of 0. The
5307 combination of the return value of 0 with no expression means
5308 we're finished.
5309*/
5310static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005311fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5312 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005313 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005314 struct compiling *c, const node *n)
5315{
5316 int result;
5317
5318 assert(*literal == NULL && *expression == NULL);
5319
5320 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005321 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005322 if (result < 0)
5323 goto error;
5324
5325 assert(result == 0 || result == 1);
5326
5327 if (result == 1)
5328 /* We have a literal, but don't look at the expression. */
5329 return 1;
5330
Eric V. Smith451d0e32016-09-09 21:56:20 -04005331 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005332 /* We're at the end of the string or the end of a nested
5333 f-string: no expression. The top-level error case where we
5334 expect to be at the end of the string but we're at a '}' is
5335 handled later. */
5336 return 0;
5337
5338 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005339 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005340
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005341 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5342 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005343 goto error;
5344
5345 return 0;
5346
5347error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005348 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005349 return -1;
5350}
5351
5352#define EXPRLIST_N_CACHED 64
5353
5354typedef struct {
5355 /* Incrementally build an array of expr_ty, so be used in an
5356 asdl_seq. Cache some small but reasonably sized number of
5357 expr_ty's, and then after that start dynamically allocating,
5358 doubling the number allocated each time. Note that the f-string
5359 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005360 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005361 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005362
5363 Py_ssize_t allocated; /* Number we've allocated. */
5364 Py_ssize_t size; /* Number we've used. */
5365 expr_ty *p; /* Pointer to the memory we're actually
5366 using. Will point to 'data' until we
5367 start dynamically allocating. */
5368 expr_ty data[EXPRLIST_N_CACHED];
5369} ExprList;
5370
5371#ifdef NDEBUG
5372#define ExprList_check_invariants(l)
5373#else
5374static void
5375ExprList_check_invariants(ExprList *l)
5376{
5377 /* Check our invariants. Make sure this object is "live", and
5378 hasn't been deallocated. */
5379 assert(l->size >= 0);
5380 assert(l->p != NULL);
5381 if (l->size <= EXPRLIST_N_CACHED)
5382 assert(l->data == l->p);
5383}
5384#endif
5385
5386static void
5387ExprList_Init(ExprList *l)
5388{
5389 l->allocated = EXPRLIST_N_CACHED;
5390 l->size = 0;
5391
5392 /* Until we start allocating dynamically, p points to data. */
5393 l->p = l->data;
5394
5395 ExprList_check_invariants(l);
5396}
5397
5398static int
5399ExprList_Append(ExprList *l, expr_ty exp)
5400{
5401 ExprList_check_invariants(l);
5402 if (l->size >= l->allocated) {
5403 /* We need to alloc (or realloc) the memory. */
5404 Py_ssize_t new_size = l->allocated * 2;
5405
5406 /* See if we've ever allocated anything dynamically. */
5407 if (l->p == l->data) {
5408 Py_ssize_t i;
5409 /* We're still using the cached data. Switch to
5410 alloc-ing. */
5411 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5412 if (!l->p)
5413 return -1;
5414 /* Copy the cached data into the new buffer. */
5415 for (i = 0; i < l->size; i++)
5416 l->p[i] = l->data[i];
5417 } else {
5418 /* Just realloc. */
5419 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5420 if (!tmp) {
5421 PyMem_RawFree(l->p);
5422 l->p = NULL;
5423 return -1;
5424 }
5425 l->p = tmp;
5426 }
5427
5428 l->allocated = new_size;
5429 assert(l->allocated == 2 * l->size);
5430 }
5431
5432 l->p[l->size++] = exp;
5433
5434 ExprList_check_invariants(l);
5435 return 0;
5436}
5437
5438static void
5439ExprList_Dealloc(ExprList *l)
5440{
5441 ExprList_check_invariants(l);
5442
5443 /* If there's been an error, or we've never dynamically allocated,
5444 do nothing. */
5445 if (!l->p || l->p == l->data) {
5446 /* Do nothing. */
5447 } else {
5448 /* We have dynamically allocated. Free the memory. */
5449 PyMem_RawFree(l->p);
5450 }
5451 l->p = NULL;
5452 l->size = -1;
5453}
5454
5455static asdl_seq *
5456ExprList_Finish(ExprList *l, PyArena *arena)
5457{
5458 asdl_seq *seq;
5459
5460 ExprList_check_invariants(l);
5461
5462 /* Allocate the asdl_seq and copy the expressions in to it. */
5463 seq = _Py_asdl_seq_new(l->size, arena);
5464 if (seq) {
5465 Py_ssize_t i;
5466 for (i = 0; i < l->size; i++)
5467 asdl_seq_SET(seq, i, l->p[i]);
5468 }
5469 ExprList_Dealloc(l);
5470 return seq;
5471}
5472
5473/* The FstringParser is designed to add a mix of strings and
5474 f-strings, and concat them together as needed. Ultimately, it
5475 generates an expr_ty. */
5476typedef struct {
5477 PyObject *last_str;
5478 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005479 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005480} FstringParser;
5481
5482#ifdef NDEBUG
5483#define FstringParser_check_invariants(state)
5484#else
5485static void
5486FstringParser_check_invariants(FstringParser *state)
5487{
5488 if (state->last_str)
5489 assert(PyUnicode_CheckExact(state->last_str));
5490 ExprList_check_invariants(&state->expr_list);
5491}
5492#endif
5493
5494static void
5495FstringParser_Init(FstringParser *state)
5496{
5497 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005498 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005499 ExprList_Init(&state->expr_list);
5500 FstringParser_check_invariants(state);
5501}
5502
5503static void
5504FstringParser_Dealloc(FstringParser *state)
5505{
5506 FstringParser_check_invariants(state);
5507
5508 Py_XDECREF(state->last_str);
5509 ExprList_Dealloc(&state->expr_list);
5510}
5511
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005512/* Constants for the following */
5513static PyObject *u_kind;
5514
5515/* Compute 'kind' field for string Constant (either 'u' or None) */
5516static PyObject *
5517make_kind(struct compiling *c, const node *n)
5518{
5519 char *s = NULL;
5520 PyObject *kind = NULL;
5521
5522 /* Find the first string literal, if any */
5523 while (TYPE(n) != STRING) {
5524 if (NCH(n) == 0)
5525 return NULL;
5526 n = CHILD(n, 0);
5527 }
5528 REQ(n, STRING);
5529
5530 /* If it starts with 'u', return a PyUnicode "u" string */
5531 s = STR(n);
5532 if (s && *s == 'u') {
5533 if (!u_kind) {
5534 u_kind = PyUnicode_InternFromString("u");
5535 if (!u_kind)
5536 return NULL;
5537 }
5538 kind = u_kind;
5539 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5540 return NULL;
5541 }
5542 Py_INCREF(kind);
5543 }
5544 return kind;
5545}
5546
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005547/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005548static expr_ty
5549make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5550{
5551 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005552 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005553 *str = NULL;
5554 assert(PyUnicode_CheckExact(s));
5555 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5556 Py_DECREF(s);
5557 return NULL;
5558 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005559 kind = make_kind(c, n);
5560 if (kind == NULL && PyErr_Occurred())
5561 return NULL;
5562 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005563 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005564}
5565
5566/* Add a non-f-string (that is, a regular literal string). str is
5567 decref'd. */
5568static int
5569FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5570{
5571 FstringParser_check_invariants(state);
5572
5573 assert(PyUnicode_CheckExact(str));
5574
5575 if (PyUnicode_GET_LENGTH(str) == 0) {
5576 Py_DECREF(str);
5577 return 0;
5578 }
5579
5580 if (!state->last_str) {
5581 /* We didn't have a string before, so just remember this one. */
5582 state->last_str = str;
5583 } else {
5584 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005585 PyUnicode_AppendAndDel(&state->last_str, str);
5586 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005587 return -1;
5588 }
5589 FstringParser_check_invariants(state);
5590 return 0;
5591}
5592
Eric V. Smith451d0e32016-09-09 21:56:20 -04005593/* Parse an f-string. The f-string is in *str to end, with no
5594 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005595static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005596FstringParser_ConcatFstring(FstringParser *state, const char **str,
5597 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005598 struct compiling *c, const node *n)
5599{
5600 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005601 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005602
5603 /* Parse the f-string. */
5604 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005605 PyObject *literal = NULL;
5606 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005607 expr_ty expression = NULL;
5608
5609 /* If there's a zero length literal in front of the
5610 expression, literal will be NULL. If we're at the end of
5611 the f-string, expression will be NULL (unless result == 1,
5612 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005613 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005614 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005615 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005616 if (result < 0)
5617 return -1;
5618
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005619 /* Add the literal, if any. */
5620 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5621 Py_XDECREF(expr_text);
5622 return -1;
5623 }
5624 /* Add the expr_text, if any. */
5625 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5626 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005627 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005628
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005629 /* We've dealt with the literal and expr_text, their ownership has
5630 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005631
5632 /* See if we should just loop around to get the next literal
5633 and expression, while ignoring the expression this
5634 time. This is used for un-doubling braces, as an
5635 optimization. */
5636 if (result == 1)
5637 continue;
5638
5639 if (!expression)
5640 /* We're done with this f-string. */
5641 break;
5642
5643 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005644 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005645 if (!state->last_str) {
5646 /* Do nothing. No previous literal. */
5647 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005648 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005649 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5650 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5651 return -1;
5652 }
5653
5654 if (ExprList_Append(&state->expr_list, expression) < 0)
5655 return -1;
5656 }
5657
Eric V. Smith235a6f02015-09-19 14:51:32 -04005658 /* If recurse_lvl is zero, then we must be at the end of the
5659 string. Otherwise, we must be at a right brace. */
5660
Eric V. Smith451d0e32016-09-09 21:56:20 -04005661 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 ast_error(c, n, "f-string: unexpected end of string");
5663 return -1;
5664 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005665 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005666 ast_error(c, n, "f-string: expecting '}'");
5667 return -1;
5668 }
5669
5670 FstringParser_check_invariants(state);
5671 return 0;
5672}
5673
5674/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005675 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005676static expr_ty
5677FstringParser_Finish(FstringParser *state, struct compiling *c,
5678 const node *n)
5679{
5680 asdl_seq *seq;
5681
5682 FstringParser_check_invariants(state);
5683
5684 /* If we're just a constant string with no expressions, return
5685 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005686 if (!state->fmode) {
5687 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005688 if (!state->last_str) {
5689 /* Create a zero length string. */
5690 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5691 if (!state->last_str)
5692 goto error;
5693 }
5694 return make_str_node_and_del(&state->last_str, c, n);
5695 }
5696
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005697 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005698 last node in our expression list. */
5699 if (state->last_str) {
5700 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5701 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5702 goto error;
5703 }
5704 /* This has already been freed. */
5705 assert(state->last_str == NULL);
5706
5707 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5708 if (!seq)
5709 goto error;
5710
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005711 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5712 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005713
5714error:
5715 FstringParser_Dealloc(state);
5716 return NULL;
5717}
5718
Eric V. Smith451d0e32016-09-09 21:56:20 -04005719/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5720 at end, parse it into an expr_ty. Return NULL on error. Adjust
5721 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005722static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005723fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005724 struct compiling *c, const node *n)
5725{
5726 FstringParser state;
5727
5728 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005729 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005730 c, n) < 0) {
5731 FstringParser_Dealloc(&state);
5732 return NULL;
5733 }
5734
5735 return FstringParser_Finish(&state, c, n);
5736}
5737
5738/* n is a Python string literal, including the bracketing quote
5739 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005740 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005742 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5743 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005745static int
5746parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5747 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005748{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005749 size_t len;
5750 const char *s = STR(n);
5751 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005752 int fmode = 0;
5753 *bytesmode = 0;
5754 *rawmode = 0;
5755 *result = NULL;
5756 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005757 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005758 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005759 if (quote == 'b' || quote == 'B') {
5760 quote = *++s;
5761 *bytesmode = 1;
5762 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005763 else if (quote == 'u' || quote == 'U') {
5764 quote = *++s;
5765 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005766 else if (quote == 'r' || quote == 'R') {
5767 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005768 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005769 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005770 else if (quote == 'f' || quote == 'F') {
5771 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005772 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005773 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005774 else {
5775 break;
5776 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005777 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005778 }
Guido van Rossum495da292019-03-07 12:38:08 -08005779
5780 /* fstrings are only allowed in Python 3.6 and greater */
5781 if (fmode && c->c_feature_version < 6) {
5782 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5783 return -1;
5784 }
5785
Eric V. Smith451d0e32016-09-09 21:56:20 -04005786 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005787 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005788 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005789 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005790 if (quote != '\'' && quote != '\"') {
5791 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005792 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005793 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005794 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005795 s++;
5796 len = strlen(s);
5797 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005798 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005799 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005800 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005801 }
5802 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005803 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005804 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005805 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005806 }
5807 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005808 /* A triple quoted string. We've already skipped one quote at
5809 the start and one at the end of the string. Now skip the
5810 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005811 s += 2;
5812 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005813 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005814 if (s[--len] != quote || s[--len] != quote) {
5815 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005816 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005817 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005818 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005819
Eric V. Smith451d0e32016-09-09 21:56:20 -04005820 if (fmode) {
5821 /* Just return the bytes. The caller will parse the resulting
5822 string. */
5823 *fstr = s;
5824 *fstrlen = len;
5825 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005826 }
5827
Eric V. Smith451d0e32016-09-09 21:56:20 -04005828 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005829 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005830 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005831 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005832 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005833 const char *ch;
5834 for (ch = s; *ch; ch++) {
5835 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005836 ast_error(c, n,
5837 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005838 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005839 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005840 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005841 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005842 if (*rawmode)
5843 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005844 else
Eric V. Smith56466482016-10-31 14:46:26 -04005845 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005846 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005847 if (*rawmode)
5848 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005849 else
Eric V. Smith56466482016-10-31 14:46:26 -04005850 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005851 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005852 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005853}
5854
Eric V. Smith235a6f02015-09-19 14:51:32 -04005855/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5856 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005857 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005858 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005859 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005860 node if there's just an f-string (with no leading or trailing
5861 literals), or a JoinedStr node if there are multiple f-strings or
5862 any literals involved. */
5863static expr_ty
5864parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005865{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005866 int bytesmode = 0;
5867 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005868 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005869
5870 FstringParser state;
5871 FstringParser_Init(&state);
5872
5873 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005874 int this_bytesmode;
5875 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005876 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005877 const char *fstr;
5878 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005879
5880 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005881 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5882 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005883 goto error;
5884
5885 /* Check that we're not mixing bytes with unicode. */
5886 if (i != 0 && bytesmode != this_bytesmode) {
5887 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005888 /* s is NULL if the current string part is an f-string. */
5889 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005890 goto error;
5891 }
5892 bytesmode = this_bytesmode;
5893
Eric V. Smith451d0e32016-09-09 21:56:20 -04005894 if (fstr != NULL) {
5895 int result;
5896 assert(s == NULL && !bytesmode);
5897 /* This is an f-string. Parse and concatenate it. */
5898 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5899 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005900 if (result < 0)
5901 goto error;
5902 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005903 /* A string or byte string. */
5904 assert(s != NULL && fstr == NULL);
5905
Eric V. Smith451d0e32016-09-09 21:56:20 -04005906 assert(bytesmode ? PyBytes_CheckExact(s) :
5907 PyUnicode_CheckExact(s));
5908
Eric V. Smith451d0e32016-09-09 21:56:20 -04005909 if (bytesmode) {
5910 /* For bytes, concat as we go. */
5911 if (i == 0) {
5912 /* First time, just remember this value. */
5913 bytes_str = s;
5914 } else {
5915 PyBytes_ConcatAndDel(&bytes_str, s);
5916 if (!bytes_str)
5917 goto error;
5918 }
5919 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005920 /* This is a regular string. Concatenate it. */
5921 if (FstringParser_ConcatAndDel(&state, s) < 0)
5922 goto error;
5923 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005924 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005925 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005926 if (bytesmode) {
5927 /* Just return the bytes object and we're done. */
5928 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5929 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005930 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005931 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005932 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005933
Eric V. Smith235a6f02015-09-19 14:51:32 -04005934 /* We're not a bytes string, bytes_str should never have been set. */
5935 assert(bytes_str == NULL);
5936
5937 return FstringParser_Finish(&state, c, n);
5938
5939error:
5940 Py_XDECREF(bytes_str);
5941 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005942 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005943}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005944
5945PyObject *
5946_PyAST_GetDocString(asdl_seq *body)
5947{
5948 if (!asdl_seq_LEN(body)) {
5949 return NULL;
5950 }
5951 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5952 if (st->kind != Expr_kind) {
5953 return NULL;
5954 }
5955 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005956 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5957 return e->v.Constant.value;
5958 }
5959 return NULL;
5960}