blob: 43b50c5dd4cc2be083afd31ae348cd716d8e063a [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;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500548 default:
549 PyErr_SetString(PyExc_SystemError, "impossible module node");
550 res = 0;
551 break;
552 }
553 return res;
554}
555
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500556/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500557#include "grammar.h"
558#include "parsetok.h"
559#include "graminit.h"
560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561/* Data structure used internally */
562struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400563 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200564 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500565 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800566 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567};
568
569static asdl_seq *seq_for_testlist(struct compiling *, const node *);
570static expr_ty ast_for_expr(struct compiling *, const node *);
571static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300572static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000573static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
574 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000575static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000576static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577
guoci90fc8982018-09-11 17:45:45 -0400578static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
579static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200582static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +0200583 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000585static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400586static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000587static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Nick Coghlan650f0d02007-04-15 12:05:43 +0000589#define COMP_GENEXP 0
590#define COMP_LISTCOMP 1
591#define COMP_SETCOMP 2
592
Benjamin Peterson55e00432012-01-16 17:22:31 -0500593static int
594init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000595{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
597 if (!m)
598 return 0;
599 c->c_normalize = PyObject_GetAttrString(m, "normalize");
600 Py_DECREF(m);
601 if (!c->c_normalize)
602 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500603 return 1;
604}
605
606static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400607new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500608{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400609 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500610 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000611 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500612 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000614 /* Check whether there are non-ASCII characters in the
615 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500616 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500618 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500619 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500621 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700622 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300623 if (form == NULL) {
624 Py_DECREF(id);
625 return NULL;
626 }
627 PyObject *args[2] = {form, id};
628 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500629 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100630 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200631 if (!id2)
632 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300633 if (!PyUnicode_Check(id2)) {
634 PyErr_Format(PyExc_TypeError,
635 "unicodedata.normalize() must return a string, not "
636 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700637 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300638 Py_DECREF(id2);
639 return NULL;
640 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200641 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000642 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000643 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200644 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
645 Py_DECREF(id);
646 return NULL;
647 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000648 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649}
650
Benjamin Peterson55e00432012-01-16 17:22:31 -0500651#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200654ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400656 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200657 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_start(va, errmsg);
660 errstr = PyUnicode_FromFormatV(errmsg, va);
661 va_end(va);
662 if (!errstr) {
663 return 0;
664 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200665 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000667 Py_INCREF(Py_None);
668 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 }
Ammar Askar025eb982018-09-24 17:12:49 -0400670 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200671 if (!tmp) {
672 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400673 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000674 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 Py_DECREF(errstr);
677 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400678 if (value) {
679 PyErr_SetObject(PyExc_SyntaxError, value);
680 Py_DECREF(value);
681 }
682 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683}
684
685/* num_stmts() returns number of contained statements.
686
687 Use this routine to determine how big a sequence is needed for
688 the statements in a parse tree. Its raison d'etre is this bit of
689 grammar:
690
691 stmt: simple_stmt | compound_stmt
692 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
693
694 A simple_stmt can contain multiple small_stmt elements joined
695 by semicolons. If the arg is a simple_stmt, the number of
696 small_stmt elements is returned.
697*/
698
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800699static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800700new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800701{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800702 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800703 if (res == NULL)
704 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800705 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
706 Py_DECREF(res);
707 return NULL;
708 }
709 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800710}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800711#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713static int
714num_stmts(const node *n)
715{
716 int i, l;
717 node *ch;
718
719 switch (TYPE(n)) {
720 case single_input:
721 if (TYPE(CHILD(n, 0)) == NEWLINE)
722 return 0;
723 else
724 return num_stmts(CHILD(n, 0));
725 case file_input:
726 l = 0;
727 for (i = 0; i < NCH(n); i++) {
728 ch = CHILD(n, i);
729 if (TYPE(ch) == stmt)
730 l += num_stmts(ch);
731 }
732 return l;
733 case stmt:
734 return num_stmts(CHILD(n, 0));
735 case compound_stmt:
736 return 1;
737 case simple_stmt:
738 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
739 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800740 case func_body_suite:
741 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
742 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 if (NCH(n) == 1)
744 return num_stmts(CHILD(n, 0));
745 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800746 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800748 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
749 i += 2;
750 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 l += num_stmts(CHILD(n, i));
752 return l;
753 }
754 default: {
755 char buf[128];
756
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000757 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 TYPE(n), NCH(n));
759 Py_FatalError(buf);
760 }
761 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700762 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763}
764
765/* Transform the CST rooted at node * to the appropriate AST
766*/
767
768mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200769PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
770 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000772 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800774 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 stmt_ty s;
776 node *ch;
777 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500778 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800779 asdl_seq *argtypes = NULL;
780 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400782 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200783 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400784 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800785 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700786 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800787
788 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Jeremy Hyltona8293132006-02-28 17:58:27 +0000791 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 switch (TYPE(n)) {
793 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200794 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500796 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 for (i = 0; i < NCH(n) - 1; i++) {
798 ch = CHILD(n, i);
799 if (TYPE(ch) == NEWLINE)
800 continue;
801 REQ(ch, stmt);
802 num = num_stmts(ch);
803 if (num == 1) {
804 s = ast_for_stmt(&c, ch);
805 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500806 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000807 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 }
809 else {
810 ch = CHILD(ch, 0);
811 REQ(ch, simple_stmt);
812 for (j = 0; j < num; j++) {
813 s = ast_for_stmt(&c, CHILD(ch, j * 2));
814 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500815 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000816 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 }
818 }
819 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800820
821 /* Type ignores are stored under the ENDMARKER in file_input. */
822 ch = CHILD(n, NCH(n) - 1);
823 REQ(ch, ENDMARKER);
824 num = NCH(ch);
825 type_ignores = _Py_asdl_seq_new(num, arena);
826 if (!type_ignores)
827 goto out;
828
829 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700830 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
831 if (!type_comment)
832 goto out;
833 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800834 if (!ti)
835 goto out;
836 asdl_seq_SET(type_ignores, i, ti);
837 }
838
839 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500840 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 case eval_input: {
842 expr_ty testlist_ast;
843
Nick Coghlan650f0d02007-04-15 12:05:43 +0000844 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000845 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 goto out;
848 res = Expression(testlist_ast, arena);
849 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851 case single_input:
852 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200853 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000857 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000859 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500860 goto out;
861 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
863 else {
864 n = CHILD(n, 0);
865 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200866 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500868 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870 s = ast_for_stmt(&c, n);
871 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 asdl_seq_SET(stmts, 0, s);
874 }
875 else {
876 /* Only a simple_stmt can contain multiple statements. */
877 REQ(n, simple_stmt);
878 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 if (TYPE(CHILD(n, i)) == NEWLINE)
880 break;
881 s = ast_for_stmt(&c, CHILD(n, i));
882 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500883 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 asdl_seq_SET(stmts, i / 2, s);
885 }
886 }
887
Benjamin Peterson55e00432012-01-16 17:22:31 -0500888 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500890 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800891 case func_type_input:
892 n = CHILD(n, 0);
893 REQ(n, func_type);
894
895 if (TYPE(CHILD(n, 1)) == typelist) {
896 ch = CHILD(n, 1);
897 /* this is overly permissive -- we don't pay any attention to
898 * stars on the args -- just parse them into an ordered list */
899 num = 0;
900 for (i = 0; i < NCH(ch); i++) {
901 if (TYPE(CHILD(ch, i)) == test) {
902 num++;
903 }
904 }
905
906 argtypes = _Py_asdl_seq_new(num, arena);
907 if (!argtypes)
908 goto out;
909
910 j = 0;
911 for (i = 0; i < NCH(ch); i++) {
912 if (TYPE(CHILD(ch, i)) == test) {
913 arg = ast_for_expr(&c, CHILD(ch, i));
914 if (!arg)
915 goto out;
916 asdl_seq_SET(argtypes, j++, arg);
917 }
918 }
919 }
920 else {
921 argtypes = _Py_asdl_seq_new(0, arena);
922 if (!argtypes)
923 goto out;
924 }
925
926 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
927 if (!ret)
928 goto out;
929 res = FunctionType(argtypes, ret, arena);
930 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000932 PyErr_Format(PyExc_SystemError,
933 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500934 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500936 out:
937 if (c.c_normalize) {
938 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500939 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500940 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941}
942
Victor Stinner14e461d2013-08-26 22:28:21 +0200943mod_ty
944PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
945 PyArena *arena)
946{
947 mod_ty mod;
948 PyObject *filename;
949 filename = PyUnicode_DecodeFSDefault(filename_str);
950 if (filename == NULL)
951 return NULL;
952 mod = PyAST_FromNodeObject(n, flags, filename, arena);
953 Py_DECREF(filename);
954 return mod;
955
956}
957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
959*/
960
961static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800962get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963{
964 switch (TYPE(n)) {
965 case VBAR:
966 return BitOr;
967 case CIRCUMFLEX:
968 return BitXor;
969 case AMPER:
970 return BitAnd;
971 case LEFTSHIFT:
972 return LShift;
973 case RIGHTSHIFT:
974 return RShift;
975 case PLUS:
976 return Add;
977 case MINUS:
978 return Sub;
979 case STAR:
980 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400981 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800982 if (c->c_feature_version < 5) {
983 ast_error(c, n,
984 "The '@' operator is only supported in Python 3.5 and greater");
985 return (operator_ty)0;
986 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400987 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 case SLASH:
989 return Div;
990 case DOUBLESLASH:
991 return FloorDiv;
992 case PERCENT:
993 return Mod;
994 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 }
997}
998
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200999static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001000 "None",
1001 "True",
1002 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001003 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001004 NULL,
1005};
1006
1007static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001008forbidden_name(struct compiling *c, identifier name, const node *n,
1009 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001010{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001011 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001012 const char * const *p = FORBIDDEN;
1013 if (!full_checks) {
1014 /* In most cases, the parser will protect True, False, and None
1015 from being assign to. */
1016 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001017 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001018 for (; *p; p++) {
1019 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1020 ast_error(c, n, "cannot assign to %U", name);
1021 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001022 }
1023 }
1024 return 0;
1025}
1026
Serhiy Storchakab619b092018-11-27 09:40:29 +02001027static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001028copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001029{
1030 if (e) {
1031 e->lineno = LINENO(n);
1032 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001033 e->end_lineno = end->n_end_lineno;
1034 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001035 }
1036 return e;
1037}
1038
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001039static const char *
1040get_expr_name(expr_ty e)
1041{
1042 switch (e->kind) {
1043 case Attribute_kind:
1044 return "attribute";
1045 case Subscript_kind:
1046 return "subscript";
1047 case Starred_kind:
1048 return "starred";
1049 case Name_kind:
1050 return "name";
1051 case List_kind:
1052 return "list";
1053 case Tuple_kind:
1054 return "tuple";
1055 case Lambda_kind:
1056 return "lambda";
1057 case Call_kind:
1058 return "function call";
1059 case BoolOp_kind:
1060 case BinOp_kind:
1061 case UnaryOp_kind:
1062 return "operator";
1063 case GeneratorExp_kind:
1064 return "generator expression";
1065 case Yield_kind:
1066 case YieldFrom_kind:
1067 return "yield expression";
1068 case Await_kind:
1069 return "await expression";
1070 case ListComp_kind:
1071 return "list comprehension";
1072 case SetComp_kind:
1073 return "set comprehension";
1074 case DictComp_kind:
1075 return "dict comprehension";
1076 case Dict_kind:
1077 return "dict display";
1078 case Set_kind:
1079 return "set display";
1080 case JoinedStr_kind:
1081 case FormattedValue_kind:
1082 return "f-string expression";
1083 case Constant_kind: {
1084 PyObject *value = e->v.Constant.value;
1085 if (value == Py_None) {
1086 return "None";
1087 }
1088 if (value == Py_False) {
1089 return "False";
1090 }
1091 if (value == Py_True) {
1092 return "True";
1093 }
1094 if (value == Py_Ellipsis) {
1095 return "Ellipsis";
1096 }
1097 return "literal";
1098 }
1099 case Compare_kind:
1100 return "comparison";
1101 case IfExp_kind:
1102 return "conditional expression";
1103 case NamedExpr_kind:
1104 return "named expression";
1105 default:
1106 PyErr_Format(PyExc_SystemError,
1107 "unexpected expression in assignment %d (line %d)",
1108 e->kind, e->lineno);
1109 return NULL;
1110 }
1111}
1112
Jeremy Hyltona8293132006-02-28 17:58:27 +00001113/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114
1115 Only sets context for expr kinds that "can appear in assignment context"
1116 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1117 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118*/
1119
1120static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001121set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
1123 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001124
1125 /* The ast defines augmented store and load contexts, but the
1126 implementation here doesn't actually use them. The code may be
1127 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001128 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001129 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001130 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001131 */
1132 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
1134 switch (e->kind) {
1135 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001136 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001137 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001138 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 e->v.Subscript.ctx = ctx;
1142 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001143 case Starred_kind:
1144 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001145 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001146 return 0;
1147 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001149 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001150 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001151 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001152 }
1153 e->v.Name.ctx = ctx;
1154 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 e->v.List.ctx = ctx;
1157 s = e->v.List.elts;
1158 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001160 e->v.Tuple.ctx = ctx;
1161 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001163 default: {
1164 const char *expr_name = get_expr_name(e);
1165 if (expr_name != NULL) {
1166 ast_error(c, n, "cannot %s %s",
1167 ctx == Store ? "assign to" : "delete",
1168 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001169 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001170 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001171 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001172 }
1173
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 */
1177 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001178 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179
Thomas Wouters89f507f2006-12-13 04:49:30 +00001180 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001181 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 return 0;
1183 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 }
1185 return 1;
1186}
1187
1188static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001189ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190{
1191 REQ(n, augassign);
1192 n = CHILD(n, 0);
1193 switch (STR(n)[0]) {
1194 case '+':
1195 return Add;
1196 case '-':
1197 return Sub;
1198 case '/':
1199 if (STR(n)[1] == '/')
1200 return FloorDiv;
1201 else
1202 return Div;
1203 case '%':
1204 return Mod;
1205 case '<':
1206 return LShift;
1207 case '>':
1208 return RShift;
1209 case '&':
1210 return BitAnd;
1211 case '^':
1212 return BitXor;
1213 case '|':
1214 return BitOr;
1215 case '*':
1216 if (STR(n)[1] == '*')
1217 return Pow;
1218 else
1219 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001220 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001221 if (c->c_feature_version < 5) {
1222 ast_error(c, n,
1223 "The '@' operator is only supported in Python 3.5 and greater");
1224 return (operator_ty)0;
1225 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001226 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001228 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 }
1231}
1232
1233static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001234ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001236 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 |'is' 'not'
1238 */
1239 REQ(n, comp_op);
1240 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001241 n = CHILD(n, 0);
1242 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 case LESS:
1244 return Lt;
1245 case GREATER:
1246 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001247 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return Eq;
1249 case LESSEQUAL:
1250 return LtE;
1251 case GREATEREQUAL:
1252 return GtE;
1253 case NOTEQUAL:
1254 return NotEq;
1255 case NAME:
1256 if (strcmp(STR(n), "in") == 0)
1257 return In;
1258 if (strcmp(STR(n), "is") == 0)
1259 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001260 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001262 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 }
1267 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001268 /* handle "not in" and "is not" */
1269 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 case NAME:
1271 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1272 return NotIn;
1273 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1274 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001275 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001277 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 }
Neal Norwitz79792652005-11-14 04:25:03 +00001282 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285}
1286
1287static asdl_seq *
1288seq_for_testlist(struct compiling *c, const node *n)
1289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001291 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1292 */
Armin Rigo31441302005-10-21 12:57:31 +00001293 asdl_seq *seq;
1294 expr_ty expression;
1295 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001296 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001298 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 if (!seq)
1300 return NULL;
1301
1302 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001304 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305
Benjamin Peterson4905e802009-09-27 02:43:28 +00001306 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001307 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309
1310 assert(i / 2 < seq->size);
1311 asdl_seq_SET(seq, i / 2, expression);
1312 }
1313 return seq;
1314}
1315
Neal Norwitzc1505362006-12-28 06:47:50 +00001316static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001317ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001318{
1319 identifier name;
1320 expr_ty annotation = NULL;
1321 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001322 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001323
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 name = NEW_IDENTIFIER(ch);
1327 if (!name)
1328 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001329 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001330 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001331
1332 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1333 annotation = ast_for_expr(c, CHILD(n, 2));
1334 if (!annotation)
1335 return NULL;
1336 }
1337
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001338 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001339 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001340 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001342 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343}
1344
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345/* returns -1 if failed to handle keyword only arguments
1346 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001347 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 ^^^
1349 start pointing here
1350 */
1351static int
1352handle_keywordonly_args(struct compiling *c, const node *n, int start,
1353 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1354{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001355 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001356 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001358 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 int i = start;
1360 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001361
1362 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001363 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001364 return -1;
1365 }
1366 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 while (i < NCH(n)) {
1368 ch = CHILD(n, i);
1369 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001370 case vfpdef:
1371 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001373 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001374 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001375 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 asdl_seq_SET(kwdefaults, j, expression);
1377 i += 2; /* '=' and test */
1378 }
1379 else { /* setting NULL if no default value exists */
1380 asdl_seq_SET(kwdefaults, j, NULL);
1381 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001382 if (NCH(ch) == 3) {
1383 /* ch is NAME ':' test */
1384 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001387 }
1388 else {
1389 annotation = NULL;
1390 }
1391 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001392 argname = NEW_IDENTIFIER(ch);
1393 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001395 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001396 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001397 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001398 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001399 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001400 if (!arg)
1401 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001402 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001403 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001404 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001405 i += 1; /* the comma, if present */
1406 break;
1407 case TYPE_COMMENT:
1408 /* arg will be equal to the last argument processed */
1409 arg->type_comment = NEW_TYPE_COMMENT(ch);
1410 if (!arg->type_comment)
1411 goto error;
1412 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 break;
1414 case DOUBLESTAR:
1415 return i;
1416 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001417 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 goto error;
1419 }
1420 }
1421 return i;
1422 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425
Jeremy Hyltona8293132006-02-28 17:58:27 +00001426/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427
1428static arguments_ty
1429ast_for_arguments(struct compiling *c, const node *n)
1430{
Neal Norwitzc1505362006-12-28 06:47:50 +00001431 /* This function handles both typedargslist (function definition)
1432 and varargslist (lambda definition).
1433
1434 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001435
1436 The following definition for typedarglist is equivalent to this set of rules:
1437
1438 arguments = argument (',' [TYPE_COMMENT] argument)*
1439 argument = tfpdef ['=' test]
1440 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1441 args = '*' [tfpdef]
1442 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1443 [TYPE_COMMENT] [kwargs]])
1444 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1445 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1446 [TYPE_COMMENT] [args_kwonly_kwargs]])
1447 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1448 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1449 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1450
1451 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1452 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1453 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1454 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1455 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1456 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1457 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1458 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1459 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1460 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1461 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1462 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1463 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1464 '**' tfpdef [','] [TYPE_COMMENT]))
1465
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001466 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001467
1468 The following definition for varargslist is equivalent to this set of rules:
1469
1470 arguments = argument (',' argument )*
1471 argument = vfpdef ['=' test]
1472 kwargs = '**' vfpdef [',']
1473 args = '*' [vfpdef]
1474 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1475 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1476 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1477 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1478 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1479 (vararglist_no_posonly)
1480
1481 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1482 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1483 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1484 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1485 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1486 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1487 [',']]] | '**' vfpdef [','])
1488
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001489 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001490
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001492 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001494 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001495 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001496 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 node *ch;
1498
1499 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001501 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001504 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
Jeremy Hyltone921e022008-07-17 16:37:17 +00001506 /* First count the number of positional args & defaults. The
1507 variable i is the loop index for this for loop and the next.
1508 The next loop picks up where the first leaves off.
1509 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001511 ch = CHILD(n, i);
1512 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001513 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001514 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001515 if (i < NCH(n) && /* skip argument following star */
1516 (TYPE(CHILD(n, i)) == tfpdef ||
1517 TYPE(CHILD(n, i)) == vfpdef)) {
1518 i++;
1519 }
1520 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001521 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001522 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001523 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001524 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001525 if (TYPE(ch) == SLASH ) {
1526 nposonlyargs = nposargs;
1527 nposargs = 0;
1528 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001531 defaults for keyword only args */
1532 for ( ; i < NCH(n); ++i) {
1533 ch = CHILD(n, i);
1534 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001535 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001537 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1538 if (!posonlyargs && nposonlyargs) {
1539 return NULL;
1540 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001541 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001542 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001543 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001545 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001549 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001550 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001551 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001553 since we set NULL as default for keyword only argument w/o default
1554 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001555 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001556 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001557 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001558 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001559
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001560 /* tfpdef: NAME [':' test]
1561 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 */
1563 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001564 j = 0; /* index for defaults */
1565 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001566 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001568 ch = CHILD(n, i);
1569 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001570 case tfpdef:
1571 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1573 anything other than EQUAL or a comma? */
1574 /* XXX Should NCH(n) check be made a separate check? */
1575 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001576 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1577 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001578 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001579 assert(posdefaults != NULL);
1580 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001582 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001584 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001585 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001586 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001587 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001588 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001589 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001590 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001591 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001592 if (l < nposonlyargs) {
1593 asdl_seq_SET(posonlyargs, l++, arg);
1594 } else {
1595 asdl_seq_SET(posargs, k++, arg);
1596 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001597 i += 1; /* the name */
1598 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1599 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001601 case SLASH:
1602 /* Advance the slash and the comma. If there are more names
1603 * after the slash there will be a comma so we are advancing
1604 * the correct number of nodes. If the slash is the last item,
1605 * we will be advancing an extra token but then * i > NCH(n)
1606 * and the enclosing while will finish correctly. */
1607 i += 2;
1608 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001610 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001611 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1612 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001613 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001614 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001615 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001616 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001617 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001618 if (TYPE(ch) == COMMA) {
1619 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001620 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001621
1622 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1623 ast_error(c, CHILD(n, i),
1624 "bare * has associated type comment");
1625 return NULL;
1626 }
1627
Guido van Rossum4f72a782006-10-27 23:31:49 +00001628 res = handle_keywordonly_args(c, n, i,
1629 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001630 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001631 i = res; /* res has new position to process */
1632 }
1633 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001634 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001635 if (!vararg)
1636 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001637
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001638 i += 2; /* the star and the name */
1639 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1640 i += 1; /* the comma, if present */
1641
1642 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1643 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1644 if (!vararg->type_comment)
1645 return NULL;
1646 i += 1;
1647 }
1648
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001649 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1650 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001651 int res = 0;
1652 res = handle_keywordonly_args(c, n, i,
1653 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001654 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001655 i = res; /* res has new position to process */
1656 }
1657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 break;
1659 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001660 ch = CHILD(n, i+1); /* tfpdef */
1661 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001662 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001663 if (!kwarg)
1664 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001665 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001666 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001667 i += 1; /* the comma, if present */
1668 break;
1669 case TYPE_COMMENT:
1670 assert(i);
1671
1672 if (kwarg)
1673 arg = kwarg;
1674
1675 /* arg will be equal to the last argument processed */
1676 arg->type_comment = NEW_TYPE_COMMENT(ch);
1677 if (!arg->type_comment)
1678 return NULL;
1679 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 break;
1681 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001682 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 "unexpected node in varargslist: %d @ %d",
1684 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001685 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001686 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001688 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689}
1690
1691static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692ast_for_decorator(struct compiling *c, const node *n)
1693{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001694 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001697 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001698 REQ(CHILD(n, 2), NEWLINE);
1699
1700 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701}
1702
1703static asdl_seq*
1704ast_for_decorators(struct compiling *c, const node *n)
1705{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001706 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001707 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001711 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 if (!decorator_seq)
1713 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001716 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001717 if (!d)
1718 return NULL;
1719 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 }
1721 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722}
1723
1724static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001725ast_for_funcdef_impl(struct compiling *c, const node *n0,
1726 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001728 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001729 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001730 identifier name;
1731 arguments_ty args;
1732 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001733 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001734 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001735 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001736 node *tc;
1737 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738
Guido van Rossum495da292019-03-07 12:38:08 -08001739 if (is_async && c->c_feature_version < 5) {
1740 ast_error(c, n,
1741 "Async functions are only supported in Python 3.5 and greater");
1742 return NULL;
1743 }
1744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 REQ(n, funcdef);
1746
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 name = NEW_IDENTIFIER(CHILD(n, name_i));
1748 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001750 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1753 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001754 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001755 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1756 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1757 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001758 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001759 name_i += 2;
1760 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001761 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1762 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1763 if (!type_comment)
1764 return NULL;
1765 name_i += 1;
1766 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001767 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001769 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001770 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001772 if (NCH(CHILD(n, name_i + 3)) > 1) {
1773 /* Check if the suite has a type comment in it. */
1774 tc = CHILD(CHILD(n, name_i + 3), 1);
1775
1776 if (TYPE(tc) == TYPE_COMMENT) {
1777 if (type_comment != NULL) {
1778 ast_error(c, n, "Cannot have two type comments on def");
1779 return NULL;
1780 }
1781 type_comment = NEW_TYPE_COMMENT(tc);
1782 if (!type_comment)
1783 return NULL;
1784 }
1785 }
1786
Yury Selivanov75445082015-05-11 22:57:16 -04001787 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001788 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001789 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001790 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001791 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001792 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001793}
1794
1795static stmt_ty
1796ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1797{
Guido van Rossum495da292019-03-07 12:38:08 -08001798 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001799 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001800 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001801 REQ(CHILD(n, 1), funcdef);
1802
guoci90fc8982018-09-11 17:45:45 -04001803 return ast_for_funcdef_impl(c, n, decorator_seq,
1804 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001805}
1806
1807static stmt_ty
1808ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1809{
1810 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1811 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001812 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001813}
1814
1815
1816static stmt_ty
1817ast_for_async_stmt(struct compiling *c, const node *n)
1818{
Guido van Rossum495da292019-03-07 12:38:08 -08001819 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001820 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001821 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001822
1823 switch (TYPE(CHILD(n, 1))) {
1824 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001825 return ast_for_funcdef_impl(c, n, NULL,
1826 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001827 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001828 return ast_for_with_stmt(c, n,
1829 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001830
1831 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001832 return ast_for_for_stmt(c, n,
1833 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001834
1835 default:
1836 PyErr_Format(PyExc_SystemError,
1837 "invalid async stament: %s",
1838 STR(CHILD(n, 1)));
1839 return NULL;
1840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841}
1842
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001843static stmt_ty
1844ast_for_decorated(struct compiling *c, const node *n)
1845{
Yury Selivanov75445082015-05-11 22:57:16 -04001846 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001847 stmt_ty thing = NULL;
1848 asdl_seq *decorator_seq = NULL;
1849
1850 REQ(n, decorated);
1851
1852 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1853 if (!decorator_seq)
1854 return NULL;
1855
1856 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001857 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001859
1860 if (TYPE(CHILD(n, 1)) == funcdef) {
1861 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1862 } else if (TYPE(CHILD(n, 1)) == classdef) {
1863 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001864 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1865 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001866 }
1867 return thing;
1868}
1869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001871ast_for_namedexpr(struct compiling *c, const node *n)
1872{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001873 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001874 argument: ( test [comp_for] |
1875 test ':=' test |
1876 test '=' test |
1877 '**' test |
1878 '*' test )
1879 */
1880 expr_ty target, value;
1881
1882 target = ast_for_expr(c, CHILD(n, 0));
1883 if (!target)
1884 return NULL;
1885
1886 value = ast_for_expr(c, CHILD(n, 2));
1887 if (!value)
1888 return NULL;
1889
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001890 if (target->kind != Name_kind) {
1891 const char *expr_name = get_expr_name(target);
1892 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001893 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001894 }
1895 return NULL;
1896 }
1897
1898 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001899 return NULL;
1900
1901 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1902 n->n_end_col_offset, c->c_arena);
1903}
1904
1905static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906ast_for_lambdef(struct compiling *c, const node *n)
1907{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001908 /* lambdef: 'lambda' [varargslist] ':' test
1909 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 arguments_ty args;
1911 expr_ty expression;
1912
1913 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001914 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 if (!args)
1916 return NULL;
1917 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001918 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 }
1921 else {
1922 args = ast_for_arguments(c, CHILD(n, 1));
1923 if (!args)
1924 return NULL;
1925 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001926 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 }
1929
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001930 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1931 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932}
1933
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001934static expr_ty
1935ast_for_ifexpr(struct compiling *c, const node *n)
1936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001938 expr_ty expression, body, orelse;
1939
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001940 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001941 body = ast_for_expr(c, CHILD(n, 0));
1942 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001943 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001944 expression = ast_for_expr(c, CHILD(n, 2));
1945 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001946 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001947 orelse = ast_for_expr(c, CHILD(n, 4));
1948 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001949 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001950 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001951 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001952 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001953}
1954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001956 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957
Nick Coghlan650f0d02007-04-15 12:05:43 +00001958 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959*/
1960
1961static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001962count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001964 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965
Guido van Rossumd8faa362007-04-27 19:54:29 +00001966 count_comp_for:
1967 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001968 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001969 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001970 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001971 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001972 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001973 else if (NCH(n) == 1) {
1974 n = CHILD(n, 0);
1975 }
1976 else {
1977 goto error;
1978 }
1979 if (NCH(n) == (5)) {
1980 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001981 }
1982 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001983 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001984 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001985 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001986 REQ(n, comp_iter);
1987 n = CHILD(n, 0);
1988 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001989 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001990 else if (TYPE(n) == comp_if) {
1991 if (NCH(n) == 3) {
1992 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001993 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001994 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001995 else
1996 return n_fors;
1997 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001998
Jelle Zijlstraac317702017-10-05 20:24:46 -07001999 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002000 /* Should never be reached */
2001 PyErr_SetString(PyExc_SystemError,
2002 "logic error in count_comp_fors");
2003 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004}
2005
Nick Coghlan650f0d02007-04-15 12:05:43 +00002006/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007
Nick Coghlan650f0d02007-04-15 12:05:43 +00002008 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009*/
2010
2011static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002012count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002014 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015
Guido van Rossumd8faa362007-04-27 19:54:29 +00002016 while (1) {
2017 REQ(n, comp_iter);
2018 if (TYPE(CHILD(n, 0)) == comp_for)
2019 return n_ifs;
2020 n = CHILD(n, 0);
2021 REQ(n, comp_if);
2022 n_ifs++;
2023 if (NCH(n) == 2)
2024 return n_ifs;
2025 n = CHILD(n, 2);
2026 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027}
2028
Guido van Rossum992d4a32007-07-11 13:09:30 +00002029static asdl_seq *
2030ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002033 asdl_seq *comps;
2034
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002035 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 if (n_fors == -1)
2037 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002039 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002040 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002044 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002046 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002047 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002048 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002049 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050
Guido van Rossum992d4a32007-07-11 13:09:30 +00002051 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052
Jelle Zijlstraac317702017-10-05 20:24:46 -07002053 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002054 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002055 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002056 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002057 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002058 else {
2059 sync_n = CHILD(n, 0);
2060 }
2061 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002062
Guido van Rossum495da292019-03-07 12:38:08 -08002063 /* Async comprehensions only allowed in Python 3.6 and greater */
2064 if (is_async && c->c_feature_version < 6) {
2065 ast_error(c, n,
2066 "Async comprehensions are only supported in Python 3.6 and greater");
2067 return NULL;
2068 }
2069
Jelle Zijlstraac317702017-10-05 20:24:46 -07002070 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002071 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002072 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002074 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002075 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002077
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 /* Check the # of children rather than the length of t, since
2079 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002080 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002081 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002082 comp = comprehension(first, expression, NULL,
2083 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002085 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2086 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2087 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002088 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002089 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002091
Jelle Zijlstraac317702017-10-05 20:24:46 -07002092 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 int j, n_ifs;
2094 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095
Jelle Zijlstraac317702017-10-05 20:24:46 -07002096 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002097 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002098 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002100
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002101 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002106 REQ(n, comp_iter);
2107 n = CHILD(n, 0);
2108 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109
Guido van Rossum992d4a32007-07-11 13:09:30 +00002110 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002111 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002112 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002113 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002114 if (NCH(n) == 3)
2115 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002117 /* on exit, must guarantee that n is a comp_for */
2118 if (TYPE(n) == comp_iter)
2119 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002120 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002122 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002124 return comps;
2125}
2126
2127static expr_ty
2128ast_for_itercomp(struct compiling *c, const node *n, int type)
2129{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002130 /* testlist_comp: (test|star_expr)
2131 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002132 expr_ty elt;
2133 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002134 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135
Guido van Rossum992d4a32007-07-11 13:09:30 +00002136 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002138 ch = CHILD(n, 0);
2139 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002140 if (!elt)
2141 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002142 if (elt->kind == Starred_kind) {
2143 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2144 return NULL;
2145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146
Guido van Rossum992d4a32007-07-11 13:09:30 +00002147 comps = ast_for_comprehension(c, CHILD(n, 1));
2148 if (!comps)
2149 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002150
2151 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002152 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2153 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002154 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002155 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2156 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002157 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002158 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2159 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002160 else
2161 /* Should never happen */
2162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163}
2164
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002165/* Fills in the key, value pair corresponding to the dict element. In case
2166 * of an unpacking, key is NULL. *i is advanced by the number of ast
2167 * elements. Iff successful, nonzero is returned.
2168 */
2169static int
2170ast_for_dictelement(struct compiling *c, const node *n, int *i,
2171 expr_ty *key, expr_ty *value)
2172{
2173 expr_ty expression;
2174 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2175 assert(NCH(n) - *i >= 2);
2176
2177 expression = ast_for_expr(c, CHILD(n, *i + 1));
2178 if (!expression)
2179 return 0;
2180 *key = NULL;
2181 *value = expression;
2182
2183 *i += 2;
2184 }
2185 else {
2186 assert(NCH(n) - *i >= 3);
2187
2188 expression = ast_for_expr(c, CHILD(n, *i));
2189 if (!expression)
2190 return 0;
2191 *key = expression;
2192
2193 REQ(CHILD(n, *i + 1), COLON);
2194
2195 expression = ast_for_expr(c, CHILD(n, *i + 2));
2196 if (!expression)
2197 return 0;
2198 *value = expression;
2199
2200 *i += 3;
2201 }
2202 return 1;
2203}
2204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002206ast_for_dictcomp(struct compiling *c, const node *n)
2207{
2208 expr_ty key, value;
2209 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002210 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002212 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002213 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 assert(key);
2215 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002217 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002218 if (!comps)
2219 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002221 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2222 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002223}
2224
2225static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002226ast_for_dictdisplay(struct compiling *c, const node *n)
2227{
2228 int i;
2229 int j;
2230 int size;
2231 asdl_seq *keys, *values;
2232
2233 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2234 keys = _Py_asdl_seq_new(size, c->c_arena);
2235 if (!keys)
2236 return NULL;
2237
2238 values = _Py_asdl_seq_new(size, c->c_arena);
2239 if (!values)
2240 return NULL;
2241
2242 j = 0;
2243 for (i = 0; i < NCH(n); i++) {
2244 expr_ty key, value;
2245
2246 if (!ast_for_dictelement(c, n, &i, &key, &value))
2247 return NULL;
2248 asdl_seq_SET(keys, j, key);
2249 asdl_seq_SET(values, j, value);
2250
2251 j++;
2252 }
2253 keys->size = j;
2254 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002255 return Dict(keys, values, LINENO(n), n->n_col_offset,
2256 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002257}
2258
2259static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002260ast_for_genexp(struct compiling *c, const node *n)
2261{
2262 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002263 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002264}
2265
2266static expr_ty
2267ast_for_listcomp(struct compiling *c, const node *n)
2268{
2269 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002270 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002271}
2272
2273static expr_ty
2274ast_for_setcomp(struct compiling *c, const node *n)
2275{
2276 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002277 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002278}
2279
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002280static expr_ty
2281ast_for_setdisplay(struct compiling *c, const node *n)
2282{
2283 int i;
2284 int size;
2285 asdl_seq *elts;
2286
2287 assert(TYPE(n) == (dictorsetmaker));
2288 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2289 elts = _Py_asdl_seq_new(size, c->c_arena);
2290 if (!elts)
2291 return NULL;
2292 for (i = 0; i < NCH(n); i += 2) {
2293 expr_ty expression;
2294 expression = ast_for_expr(c, CHILD(n, i));
2295 if (!expression)
2296 return NULL;
2297 asdl_seq_SET(elts, i / 2, expression);
2298 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002299 return Set(elts, LINENO(n), n->n_col_offset,
2300 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002301}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002302
2303static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304ast_for_atom(struct compiling *c, const node *n)
2305{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002306 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2307 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002308 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 */
2310 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002313 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002314 PyObject *name;
2315 const char *s = STR(ch);
2316 size_t len = strlen(s);
2317 if (len >= 4 && len <= 5) {
2318 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002319 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002320 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002321 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002322 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002323 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002324 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002325 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002326 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002327 }
2328 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002329 if (!name)
2330 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002331 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002332 return Name(name, Load, LINENO(n), n->n_col_offset,
2333 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002334 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002336 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002337 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002338 const char *errtype = NULL;
2339 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2340 errtype = "unicode error";
2341 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2342 errtype = "value error";
2343 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002344 PyObject *type, *value, *tback, *errstr;
2345 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002346 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002347 if (errstr) {
2348 ast_error(c, n, "(%s) %U", errtype, errstr);
2349 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002350 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002351 else {
2352 PyErr_Clear();
2353 ast_error(c, n, "(%s) unknown error", errtype);
2354 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002355 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002356 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002357 Py_XDECREF(tback);
2358 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002359 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002360 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002361 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362 }
2363 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002364 PyObject *pynum;
2365 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2366 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2367 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2368 ast_error(c, ch,
2369 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2370 return NULL;
2371 }
2372 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002373 if (!pynum)
2374 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002375
Victor Stinner43d81952013-07-17 00:57:58 +02002376 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2377 Py_DECREF(pynum);
2378 return NULL;
2379 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002380 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002381 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 }
Georg Brandldde00282007-03-18 19:01:53 +00002383 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002384 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002385 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002387 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388
Thomas Wouters89f507f2006-12-13 04:49:30 +00002389 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002390 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2391 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392
Thomas Wouters89f507f2006-12-13 04:49:30 +00002393 if (TYPE(ch) == yield_expr)
2394 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002397 if (NCH(ch) == 1) {
2398 return ast_for_testlist(c, ch);
2399 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002400
Serhiy Storchakab619b092018-11-27 09:40:29 +02002401 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002402 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002403 }
2404 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002405 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002408 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409
Thomas Wouters89f507f2006-12-13 04:49:30 +00002410 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002411 return List(NULL, Load, LINENO(n), n->n_col_offset,
2412 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413
Nick Coghlan650f0d02007-04-15 12:05:43 +00002414 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002415 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2416 asdl_seq *elts = seq_for_testlist(c, ch);
2417 if (!elts)
2418 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002419
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002420 return List(elts, Load, LINENO(n), n->n_col_offset,
2421 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002422 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002423 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002424 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002425 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002427 /* dictorsetmaker: ( ((test ':' test | '**' test)
2428 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2429 * ((test | '*' test)
2430 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002431 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002432 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002433 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002434 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002435 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2436 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002437 }
2438 else {
2439 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2440 if (NCH(ch) == 1 ||
2441 (NCH(ch) > 1 &&
2442 TYPE(CHILD(ch, 1)) == COMMA)) {
2443 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002444 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002445 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002446 else if (NCH(ch) > 1 &&
2447 TYPE(CHILD(ch, 1)) == comp_for) {
2448 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002449 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002450 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002451 else if (NCH(ch) > 3 - is_dict &&
2452 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2453 /* It's a dictionary comprehension. */
2454 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002455 ast_error(c, n,
2456 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002457 return NULL;
2458 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002459 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002460 }
2461 else {
2462 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002463 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002464 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002465 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002469 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2470 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 }
2472}
2473
2474static slice_ty
2475ast_for_slice(struct compiling *c, const node *n)
2476{
2477 node *ch;
2478 expr_ty lower = NULL, upper = NULL, step = NULL;
2479
2480 REQ(n, subscript);
2481
2482 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002483 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 sliceop: ':' [test]
2485 */
2486 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 if (NCH(n) == 1 && TYPE(ch) == test) {
2488 /* 'step' variable hold no significance in terms of being used over
2489 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 if (!step)
2492 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493
Thomas Wouters89f507f2006-12-13 04:49:30 +00002494 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
2496
2497 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002498 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 if (!lower)
2500 return NULL;
2501 }
2502
2503 /* If there's an upper bound it's in the second or third position. */
2504 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002505 if (NCH(n) > 1) {
2506 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507
Thomas Wouters89f507f2006-12-13 04:49:30 +00002508 if (TYPE(n2) == test) {
2509 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 if (!upper)
2511 return NULL;
2512 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002515 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516
Thomas Wouters89f507f2006-12-13 04:49:30 +00002517 if (TYPE(n2) == test) {
2518 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 if (!upper)
2520 return NULL;
2521 }
2522 }
2523
2524 ch = CHILD(n, NCH(n) - 1);
2525 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002526 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002527 ch = CHILD(ch, 1);
2528 if (TYPE(ch) == test) {
2529 step = ast_for_expr(c, ch);
2530 if (!step)
2531 return NULL;
2532 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 }
2534 }
2535
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002536 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537}
2538
2539static expr_ty
2540ast_for_binop(struct compiling *c, const node *n)
2541{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002542 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002544 BinOp(BinOp(A, op, B), op, C).
2545 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546
Guido van Rossumd8faa362007-04-27 19:54:29 +00002547 int i, nops;
2548 expr_ty expr1, expr2, result;
2549 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550
Guido van Rossumd8faa362007-04-27 19:54:29 +00002551 expr1 = ast_for_expr(c, CHILD(n, 0));
2552 if (!expr1)
2553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554
Guido van Rossumd8faa362007-04-27 19:54:29 +00002555 expr2 = ast_for_expr(c, CHILD(n, 2));
2556 if (!expr2)
2557 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558
Guido van Rossum495da292019-03-07 12:38:08 -08002559 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002560 if (!newoperator)
2561 return NULL;
2562
2563 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002564 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002565 c->c_arena);
2566 if (!result)
2567 return NULL;
2568
2569 nops = (NCH(n) - 1) / 2;
2570 for (i = 1; i < nops; i++) {
2571 expr_ty tmp_result, tmp;
2572 const node* next_oper = CHILD(n, i * 2 + 1);
2573
Guido van Rossum495da292019-03-07 12:38:08 -08002574 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002575 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 return NULL;
2577
Guido van Rossumd8faa362007-04-27 19:54:29 +00002578 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2579 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 return NULL;
2581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002583 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002584 CHILD(n, i * 2 + 2)->n_end_lineno,
2585 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002586 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002588 return NULL;
2589 result = tmp_result;
2590 }
2591 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592}
2593
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002594static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002595ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002598 subscriptlist: subscript (',' subscript)* [',']
2599 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2600 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002601 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002602 REQ(n, trailer);
2603 if (TYPE(CHILD(n, 0)) == LPAR) {
2604 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002605 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002606 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002607 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002608 return ast_for_call(c, CHILD(n, 1), left_expr,
2609 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002610 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002611 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002612 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2613 if (!attr_id)
2614 return NULL;
2615 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002616 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002617 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002618 }
2619 else {
2620 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002621 REQ(CHILD(n, 2), RSQB);
2622 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002623 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002624 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2625 if (!slc)
2626 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002627 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002628 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002629 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002630 }
2631 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002633 by treating the sequence as a tuple literal if there are
2634 no slice features.
2635 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002636 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002637 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002638 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002639 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002640 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002641 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002642 if (!slices)
2643 return NULL;
2644 for (j = 0; j < NCH(n); j += 2) {
2645 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002646 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002647 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002648 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002649 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002650 asdl_seq_SET(slices, j / 2, slc);
2651 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002652 if (!simple) {
2653 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002654 Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002655 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002656 }
2657 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002658 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002659 if (!elts)
2660 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002661 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2662 slc = (slice_ty)asdl_seq_GET(slices, j);
2663 assert(slc->kind == Index_kind && slc->v.Index.value);
2664 asdl_seq_SET(elts, j, slc->v.Index.value);
2665 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002666 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2667 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002668 if (!e)
2669 return NULL;
2670 return Subscript(left_expr, Index(e, c->c_arena),
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002671 Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002672 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002673 }
2674 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002675}
2676
2677static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002678ast_for_factor(struct compiling *c, const node *n)
2679{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002680 expr_ty expression;
2681
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002682 expression = ast_for_expr(c, CHILD(n, 1));
2683 if (!expression)
2684 return NULL;
2685
2686 switch (TYPE(CHILD(n, 0))) {
2687 case PLUS:
2688 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002689 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002690 c->c_arena);
2691 case MINUS:
2692 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002693 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002694 c->c_arena);
2695 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002696 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2697 n->n_end_lineno, n->n_end_col_offset,
2698 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002699 }
2700 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2701 TYPE(CHILD(n, 0)));
2702 return NULL;
2703}
2704
2705static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002706ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002707{
Yury Selivanov75445082015-05-11 22:57:16 -04002708 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002709 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002710
2711 REQ(n, atom_expr);
2712 nch = NCH(n);
2713
Guido van Rossum495da292019-03-07 12:38:08 -08002714 if (TYPE(CHILD(n, 0)) == AWAIT) {
2715 if (c->c_feature_version < 5) {
2716 ast_error(c, n,
2717 "Await expressions are only supported in Python 3.5 and greater");
2718 return NULL;
2719 }
Yury Selivanov75445082015-05-11 22:57:16 -04002720 start = 1;
2721 assert(nch > 1);
2722 }
2723
2724 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002725 if (!e)
2726 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002727 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002728 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002729 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002730 return Await(e, LINENO(n), n->n_col_offset,
2731 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002732 }
2733
2734 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002735 node *ch = CHILD(n, i);
2736 if (TYPE(ch) != trailer)
2737 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002738 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2739 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002740 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002741 }
Yury Selivanov75445082015-05-11 22:57:16 -04002742
2743 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002744 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002745 return Await(e, LINENO(n), n->n_col_offset,
2746 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002747 }
2748 else {
2749 return e;
2750 }
2751}
2752
2753static expr_ty
2754ast_for_power(struct compiling *c, const node *n)
2755{
2756 /* power: atom trailer* ('**' factor)*
2757 */
2758 expr_ty e;
2759 REQ(n, power);
2760 e = ast_for_atom_expr(c, CHILD(n, 0));
2761 if (!e)
2762 return NULL;
2763 if (NCH(n) == 1)
2764 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002765 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2766 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002767 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002768 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002769 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2770 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002771 }
2772 return e;
2773}
2774
Guido van Rossum0368b722007-05-11 16:50:42 +00002775static expr_ty
2776ast_for_starred(struct compiling *c, const node *n)
2777{
2778 expr_ty tmp;
2779 REQ(n, star_expr);
2780
2781 tmp = ast_for_expr(c, CHILD(n, 1));
2782 if (!tmp)
2783 return NULL;
2784
2785 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002786 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2787 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002788}
2789
2790
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791/* Do not name a variable 'expr'! Will cause a compile error.
2792*/
2793
2794static expr_ty
2795ast_for_expr(struct compiling *c, const node *n)
2796{
2797 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002798 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002799 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002800 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 and_test: not_test ('and' not_test)*
2803 not_test: 'not' not_test | comparison
2804 comparison: expr (comp_op expr)*
2805 expr: xor_expr ('|' xor_expr)*
2806 xor_expr: and_expr ('^' and_expr)*
2807 and_expr: shift_expr ('&' shift_expr)*
2808 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2809 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002810 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002812 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002813 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002814 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 */
2816
2817 asdl_seq *seq;
2818 int i;
2819
2820 loop:
2821 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002822 case namedexpr_test:
2823 if (NCH(n) == 3)
2824 return ast_for_namedexpr(c, n);
2825 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002827 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002828 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002829 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002831 else if (NCH(n) > 1)
2832 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002833 /* Fallthrough */
2834 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 case and_test:
2836 if (NCH(n) == 1) {
2837 n = CHILD(n, 0);
2838 goto loop;
2839 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002840 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 if (!seq)
2842 return NULL;
2843 for (i = 0; i < NCH(n); i += 2) {
2844 expr_ty e = ast_for_expr(c, CHILD(n, i));
2845 if (!e)
2846 return NULL;
2847 asdl_seq_SET(seq, i / 2, e);
2848 }
2849 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002850 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002851 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002853 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002854 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2855 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 case not_test:
2857 if (NCH(n) == 1) {
2858 n = CHILD(n, 0);
2859 goto loop;
2860 }
2861 else {
2862 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2863 if (!expression)
2864 return NULL;
2865
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002866 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002867 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002868 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
2870 case comparison:
2871 if (NCH(n) == 1) {
2872 n = CHILD(n, 0);
2873 goto loop;
2874 }
2875 else {
2876 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002877 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002878 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002879 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 if (!ops)
2881 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002882 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 return NULL;
2885 }
2886 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002887 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002889 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002890 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893
2894 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002895 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002899 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 asdl_seq_SET(cmps, i / 2, expression);
2901 }
2902 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002903 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002907 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2908 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910
Guido van Rossum0368b722007-05-11 16:50:42 +00002911 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 /* The next five cases all handle BinOps. The main body of code
2914 is the same in each case, but the switch turned inside out to
2915 reuse the code for each type of operator.
2916 */
2917 case expr:
2918 case xor_expr:
2919 case and_expr:
2920 case shift_expr:
2921 case arith_expr:
2922 case term:
2923 if (NCH(n) == 1) {
2924 n = CHILD(n, 0);
2925 goto loop;
2926 }
2927 return ast_for_binop(c, n);
2928 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002929 node *an = NULL;
2930 node *en = NULL;
2931 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002932 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002933 if (NCH(n) > 1)
2934 an = CHILD(n, 1); /* yield_arg */
2935 if (an) {
2936 en = CHILD(an, NCH(an) - 1);
2937 if (NCH(an) == 2) {
2938 is_from = 1;
2939 exp = ast_for_expr(c, en);
2940 }
2941 else
2942 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002943 if (!exp)
2944 return NULL;
2945 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002946 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002947 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2948 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2949 return Yield(exp, LINENO(n), n->n_col_offset,
2950 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002951 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002952 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 if (NCH(n) == 1) {
2954 n = CHILD(n, 0);
2955 goto loop;
2956 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002957 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002958 case power:
2959 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002961 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 return NULL;
2963 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002964 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 return NULL;
2966}
2967
2968static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002969ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002970 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971{
2972 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002973 arglist: argument (',' argument)* [',']
2974 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 */
2976
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002977 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002978 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002979 asdl_seq *args;
2980 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981
2982 REQ(n, arglist);
2983
2984 nargs = 0;
2985 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002987 node *ch = CHILD(n, i);
2988 if (TYPE(ch) == argument) {
2989 if (NCH(ch) == 1)
2990 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002991 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2992 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002993 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002994 ast_error(c, ch, "invalid syntax");
2995 return NULL;
2996 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002997 if (NCH(n) > 1) {
2998 ast_error(c, ch, "Generator expression must be parenthesized");
2999 return NULL;
3000 }
3001 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003002 else if (TYPE(CHILD(ch, 0)) == STAR)
3003 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003004 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3005 nargs++;
3006 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003008 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 nkeywords++;
3010 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003013 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003015 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003016 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003018 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003019
3020 nargs = 0; /* positional arguments + iterable argument unpackings */
3021 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3022 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 node *ch = CHILD(n, i);
3025 if (TYPE(ch) == argument) {
3026 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003027 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003029 /* a positional argument */
3030 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003031 if (ndoublestars) {
3032 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003033 "positional argument follows "
3034 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003035 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003036 else {
3037 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003038 "positional argument follows "
3039 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003040 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003041 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003042 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003043 e = ast_for_expr(c, chch);
3044 if (!e)
3045 return NULL;
3046 asdl_seq_SET(args, nargs++, e);
3047 }
3048 else if (TYPE(chch) == STAR) {
3049 /* an iterable argument unpacking */
3050 expr_ty starred;
3051 if (ndoublestars) {
3052 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003053 "iterable argument unpacking follows "
3054 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003055 return NULL;
3056 }
3057 e = ast_for_expr(c, CHILD(ch, 1));
3058 if (!e)
3059 return NULL;
3060 starred = Starred(e, Load, LINENO(chch),
3061 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003062 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003063 c->c_arena);
3064 if (!starred)
3065 return NULL;
3066 asdl_seq_SET(args, nargs++, starred);
3067
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003068 }
3069 else if (TYPE(chch) == DOUBLESTAR) {
3070 /* a keyword argument unpacking */
3071 keyword_ty kw;
3072 i++;
3073 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003075 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003076 kw = keyword(NULL, e, c->c_arena);
3077 asdl_seq_SET(keywords, nkeywords++, kw);
3078 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003080 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003081 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003082 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003084 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003085 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003087 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3088 /* treat colon equal as positional argument */
3089 if (nkeywords) {
3090 if (ndoublestars) {
3091 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003092 "positional argument follows "
3093 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003094 }
3095 else {
3096 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003097 "positional argument follows "
3098 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003099 }
3100 return NULL;
3101 }
3102 e = ast_for_namedexpr(c, ch);
3103 if (!e)
3104 return NULL;
3105 asdl_seq_SET(args, nargs++, e);
3106 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003107 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003108 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003109 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003110 identifier key, tmp;
3111 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003113 // To remain LL(1), the grammar accepts any test (basically, any
3114 // expression) in the keyword slot of a call site. So, we need
3115 // to manually enforce that the keyword is a NAME here.
3116 static const int name_tree[] = {
3117 test,
3118 or_test,
3119 and_test,
3120 not_test,
3121 comparison,
3122 expr,
3123 xor_expr,
3124 and_expr,
3125 shift_expr,
3126 arith_expr,
3127 term,
3128 factor,
3129 power,
3130 atom_expr,
3131 atom,
3132 0,
3133 };
3134 node *expr_node = chch;
3135 for (int i = 0; name_tree[i]; i++) {
3136 if (TYPE(expr_node) != name_tree[i])
3137 break;
3138 if (NCH(expr_node) != 1)
3139 break;
3140 expr_node = CHILD(expr_node, 0);
3141 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003142 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003143 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003144 "expression cannot contain assignment, "
3145 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003146 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003147 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003148 key = new_identifier(STR(expr_node), c);
3149 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003150 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003152 if (forbidden_name(c, key, chch, 1)) {
3153 return NULL;
3154 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003155 for (k = 0; k < nkeywords; k++) {
3156 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003157 if (tmp && !PyUnicode_Compare(tmp, key)) {
3158 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003159 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003160 return NULL;
3161 }
3162 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003163 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003165 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003166 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003168 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003169 asdl_seq_SET(keywords, nkeywords++, kw);
3170 }
3171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 }
3173
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003174 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003175 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176}
3177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003179ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003181 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003182 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003184 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003185 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003186 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003187 }
3188 else {
3189 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003190 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003193 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 else {
3195 asdl_seq *tmp = seq_for_testlist(c, n);
3196 if (!tmp)
3197 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003198 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3199 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003201}
3202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203static stmt_ty
3204ast_for_expr_stmt(struct compiling *c, const node *n)
3205{
3206 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003207 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003208 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3209 annassign: ':' test ['=' (yield_expr|testlist)]
3210 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3211 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3212 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003213 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003215 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003217 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003218 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 if (!e)
3220 return NULL;
3221
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003222 return Expr(e, LINENO(n), n->n_col_offset,
3223 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 }
3225 else if (TYPE(CHILD(n, 1)) == augassign) {
3226 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003227 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003228 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229
Thomas Wouters89f507f2006-12-13 04:49:30 +00003230 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!expr1)
3232 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003233 if(!set_context(c, expr1, Store, ch))
3234 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003235 /* set_context checks that most expressions are not the left side.
3236 Augmented assignments can only have a name, a subscript, or an
3237 attribute on the left, though, so we have to explicitly check for
3238 those. */
3239 switch (expr1->kind) {
3240 case Name_kind:
3241 case Attribute_kind:
3242 case Subscript_kind:
3243 break;
3244 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003245 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003246 return NULL;
3247 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248
Thomas Wouters89f507f2006-12-13 04:49:30 +00003249 ch = CHILD(n, 2);
3250 if (TYPE(ch) == testlist)
3251 expr2 = ast_for_testlist(c, ch);
3252 else
3253 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003254 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255 return NULL;
3256
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003257 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003258 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 return NULL;
3260
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003261 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3262 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003264 else if (TYPE(CHILD(n, 1)) == annassign) {
3265 expr_ty expr1, expr2, expr3;
3266 node *ch = CHILD(n, 0);
3267 node *deep, *ann = CHILD(n, 1);
3268 int simple = 1;
3269
Guido van Rossum495da292019-03-07 12:38:08 -08003270 /* AnnAssigns are only allowed in Python 3.6 or greater */
3271 if (c->c_feature_version < 6) {
3272 ast_error(c, ch,
3273 "Variable annotation syntax is only supported in Python 3.6 and greater");
3274 return NULL;
3275 }
3276
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003277 /* we keep track of parens to qualify (x) as expression not name */
3278 deep = ch;
3279 while (NCH(deep) == 1) {
3280 deep = CHILD(deep, 0);
3281 }
3282 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3283 simple = 0;
3284 }
3285 expr1 = ast_for_testlist(c, ch);
3286 if (!expr1) {
3287 return NULL;
3288 }
3289 switch (expr1->kind) {
3290 case Name_kind:
3291 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3292 return NULL;
3293 }
3294 expr1->v.Name.ctx = Store;
3295 break;
3296 case Attribute_kind:
3297 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3298 return NULL;
3299 }
3300 expr1->v.Attribute.ctx = Store;
3301 break;
3302 case Subscript_kind:
3303 expr1->v.Subscript.ctx = Store;
3304 break;
3305 case List_kind:
3306 ast_error(c, ch,
3307 "only single target (not list) can be annotated");
3308 return NULL;
3309 case Tuple_kind:
3310 ast_error(c, ch,
3311 "only single target (not tuple) can be annotated");
3312 return NULL;
3313 default:
3314 ast_error(c, ch,
3315 "illegal target for annotation");
3316 return NULL;
3317 }
3318
3319 if (expr1->kind != Name_kind) {
3320 simple = 0;
3321 }
3322 ch = CHILD(ann, 1);
3323 expr2 = ast_for_expr(c, ch);
3324 if (!expr2) {
3325 return NULL;
3326 }
3327 if (NCH(ann) == 2) {
3328 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003329 LINENO(n), n->n_col_offset,
3330 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003331 }
3332 else {
3333 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003334 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003335 expr3 = ast_for_testlist(c, ch);
3336 }
3337 else {
3338 expr3 = ast_for_expr(c, ch);
3339 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003340 if (!expr3) {
3341 return NULL;
3342 }
3343 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003344 LINENO(n), n->n_col_offset,
3345 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003346 }
3347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003349 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003350 asdl_seq *targets;
3351 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003353 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 /* a normal assignment */
3356 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003357
3358 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3359 nch_minus_type = num - has_type_comment;
3360
3361 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 if (!targets)
3363 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003364 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003365 expr_ty e;
3366 node *ch = CHILD(n, i);
3367 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003368 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003369 return NULL;
3370 }
3371 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003375 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003376 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003377 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378
Thomas Wouters89f507f2006-12-13 04:49:30 +00003379 asdl_seq_SET(targets, i / 2, e);
3380 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003381 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003382 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003383 expression = ast_for_testlist(c, value);
3384 else
3385 expression = ast_for_expr(c, value);
3386 if (!expression)
3387 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003388 if (has_type_comment) {
3389 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3390 if (!type_comment)
3391 return NULL;
3392 }
3393 else
3394 type_comment = NULL;
3395 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003396 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398}
3399
Benjamin Peterson78565b22009-06-28 19:19:51 +00003400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003402ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403{
3404 asdl_seq *seq;
3405 int i;
3406 expr_ty e;
3407
3408 REQ(n, exprlist);
3409
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003410 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003412 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003414 e = ast_for_expr(c, CHILD(n, i));
3415 if (!e)
3416 return NULL;
3417 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003418 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
3421 return seq;
3422}
3423
3424static stmt_ty
3425ast_for_del_stmt(struct compiling *c, const node *n)
3426{
3427 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429 /* del_stmt: 'del' exprlist */
3430 REQ(n, del_stmt);
3431
3432 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3433 if (!expr_list)
3434 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003435 return Delete(expr_list, LINENO(n), n->n_col_offset,
3436 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437}
3438
3439static stmt_ty
3440ast_for_flow_stmt(struct compiling *c, const node *n)
3441{
3442 /*
3443 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3444 | yield_stmt
3445 break_stmt: 'break'
3446 continue_stmt: 'continue'
3447 return_stmt: 'return' [testlist]
3448 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003449 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 raise_stmt: 'raise' [test [',' test [',' test]]]
3451 */
3452 node *ch;
3453
3454 REQ(n, flow_stmt);
3455 ch = CHILD(n, 0);
3456 switch (TYPE(ch)) {
3457 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003458 return Break(LINENO(n), n->n_col_offset,
3459 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003461 return Continue(LINENO(n), n->n_col_offset,
3462 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003464 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3465 if (!exp)
3466 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003467 return Expr(exp, LINENO(n), n->n_col_offset,
3468 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 }
3470 case return_stmt:
3471 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003472 return Return(NULL, LINENO(n), n->n_col_offset,
3473 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003475 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 if (!expression)
3477 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003478 return Return(expression, LINENO(n), n->n_col_offset,
3479 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 }
3481 case raise_stmt:
3482 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003483 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3484 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003485 else if (NCH(ch) >= 2) {
3486 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3488 if (!expression)
3489 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003490 if (NCH(ch) == 4) {
3491 cause = ast_for_expr(c, CHILD(ch, 3));
3492 if (!cause)
3493 return NULL;
3494 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003495 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3496 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 }
Stefan Krahf432a322017-08-21 13:09:59 +02003498 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003500 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 "unexpected flow_stmt: %d", TYPE(ch));
3502 return NULL;
3503 }
3504}
3505
3506static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003507alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508{
3509 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003510 import_as_name: NAME ['as' NAME]
3511 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 dotted_name: NAME ('.' NAME)*
3513 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003514 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 loop:
3517 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003518 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003519 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003520 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003521 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003522 if (!name)
3523 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003524 if (NCH(n) == 3) {
3525 node *str_node = CHILD(n, 2);
3526 str = NEW_IDENTIFIER(str_node);
3527 if (!str)
3528 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003529 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003530 return NULL;
3531 }
3532 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003533 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003534 return NULL;
3535 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003536 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 case dotted_as_name:
3539 if (NCH(n) == 1) {
3540 n = CHILD(n, 0);
3541 goto loop;
3542 }
3543 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003544 node *asname_node = CHILD(n, 2);
3545 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003546 if (!a)
3547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003549 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003550 if (!a->asname)
3551 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003552 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554 return a;
3555 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003557 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003558 node *name_node = CHILD(n, 0);
3559 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003560 if (!name)
3561 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003562 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003563 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003564 return alias(name, NULL, c->c_arena);
3565 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 else {
3567 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003568 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003569 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572
3573 len = 0;
3574 for (i = 0; i < NCH(n); i += 2)
3575 /* length of string plus one for the dot */
3576 len += strlen(STR(CHILD(n, i))) + 1;
3577 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003578 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 if (!str)
3580 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003581 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 if (!s)
3583 return NULL;
3584 for (i = 0; i < NCH(n); i += 2) {
3585 char *sch = STR(CHILD(n, i));
3586 strcpy(s, STR(CHILD(n, i)));
3587 s += strlen(sch);
3588 *s++ = '.';
3589 }
3590 --s;
3591 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3593 PyBytes_GET_SIZE(str),
3594 NULL);
3595 Py_DECREF(str);
3596 if (!uni)
3597 return NULL;
3598 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003599 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003600 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3601 Py_DECREF(str);
3602 return NULL;
3603 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003604 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003607 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003608 if (!str)
3609 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003610 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3611 Py_DECREF(str);
3612 return NULL;
3613 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003614 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003616 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 "unexpected import name: %d", TYPE(n));
3618 return NULL;
3619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620}
3621
3622static stmt_ty
3623ast_for_import_stmt(struct compiling *c, const node *n)
3624{
3625 /*
3626 import_stmt: import_name | import_from
3627 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003628 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3629 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003631 int lineno;
3632 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 int i;
3634 asdl_seq *aliases;
3635
3636 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003637 lineno = LINENO(n);
3638 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003640 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003642 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003643 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003644 if (!aliases)
3645 return NULL;
3646 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003647 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003648 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003650 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003652 // Even though n is modified above, the end position is not changed
3653 return Import(aliases, lineno, col_offset,
3654 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003656 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003658 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003659 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003660 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003661 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003663 /* Count the number of dots (for relative imports) and check for the
3664 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003665 for (idx = 1; idx < NCH(n); idx++) {
3666 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003667 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3668 if (!mod)
3669 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003670 idx++;
3671 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003672 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003674 ndots += 3;
3675 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003676 } else if (TYPE(CHILD(n, idx)) != DOT) {
3677 break;
3678 }
3679 ndots++;
3680 }
3681 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003682 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003683 case STAR:
3684 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003685 n = CHILD(n, idx);
3686 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003687 break;
3688 case LPAR:
3689 /* from ... import (x, y, z) */
3690 n = CHILD(n, idx + 1);
3691 n_children = NCH(n);
3692 break;
3693 case import_as_names:
3694 /* from ... import x, y, z */
3695 n = CHILD(n, idx);
3696 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003697 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003698 ast_error(c, n,
3699 "trailing comma not allowed without"
3700 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 return NULL;
3702 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003703 break;
3704 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003705 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003706 return NULL;
3707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003709 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003710 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712
3713 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003714 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003715 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003716 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003718 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003720 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003721 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003722 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003723 if (!import_alias)
3724 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003725 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003726 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003728 if (mod != NULL)
3729 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003730 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003731 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003732 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 }
Neal Norwitz79792652005-11-14 04:25:03 +00003734 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 "unknown import statement: starts with command '%s'",
3736 STR(CHILD(n, 0)));
3737 return NULL;
3738}
3739
3740static stmt_ty
3741ast_for_global_stmt(struct compiling *c, const node *n)
3742{
3743 /* global_stmt: 'global' NAME (',' NAME)* */
3744 identifier name;
3745 asdl_seq *s;
3746 int i;
3747
3748 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003749 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003751 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003753 name = NEW_IDENTIFIER(CHILD(n, i));
3754 if (!name)
3755 return NULL;
3756 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003758 return Global(s, LINENO(n), n->n_col_offset,
3759 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760}
3761
3762static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003763ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3764{
3765 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3766 identifier name;
3767 asdl_seq *s;
3768 int i;
3769
3770 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003771 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003772 if (!s)
3773 return NULL;
3774 for (i = 1; i < NCH(n); i += 2) {
3775 name = NEW_IDENTIFIER(CHILD(n, i));
3776 if (!name)
3777 return NULL;
3778 asdl_seq_SET(s, i / 2, name);
3779 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003780 return Nonlocal(s, LINENO(n), n->n_col_offset,
3781 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003782}
3783
3784static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785ast_for_assert_stmt(struct compiling *c, const node *n)
3786{
3787 /* assert_stmt: 'assert' test [',' test] */
3788 REQ(n, assert_stmt);
3789 if (NCH(n) == 2) {
3790 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3791 if (!expression)
3792 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003793 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3794 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 }
3796 else if (NCH(n) == 4) {
3797 expr_ty expr1, expr2;
3798
3799 expr1 = ast_for_expr(c, CHILD(n, 1));
3800 if (!expr1)
3801 return NULL;
3802 expr2 = ast_for_expr(c, CHILD(n, 3));
3803 if (!expr2)
3804 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003806 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3807 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 }
Neal Norwitz79792652005-11-14 04:25:03 +00003809 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 "improper number of parts to 'assert' statement: %d",
3811 NCH(n));
3812 return NULL;
3813}
3814
3815static asdl_seq *
3816ast_for_suite(struct compiling *c, const node *n)
3817{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003818 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003819 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 stmt_ty s;
3821 int i, total, num, end, pos = 0;
3822 node *ch;
3823
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003824 if (TYPE(n) != func_body_suite) {
3825 REQ(n, suite);
3826 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827
3828 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003829 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003833 n = CHILD(n, 0);
3834 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003836 */
3837 end = NCH(n) - 1;
3838 if (TYPE(CHILD(n, end - 1)) == SEMI)
3839 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003841 for (i = 0; i < end; i += 2) {
3842 ch = CHILD(n, i);
3843 s = ast_for_stmt(c, ch);
3844 if (!s)
3845 return NULL;
3846 asdl_seq_SET(seq, pos++, s);
3847 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 }
3849 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003850 i = 2;
3851 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3852 i += 2;
3853 REQ(CHILD(n, 2), NEWLINE);
3854 }
3855
3856 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003857 ch = CHILD(n, i);
3858 REQ(ch, stmt);
3859 num = num_stmts(ch);
3860 if (num == 1) {
3861 /* small_stmt or compound_stmt with only one child */
3862 s = ast_for_stmt(c, ch);
3863 if (!s)
3864 return NULL;
3865 asdl_seq_SET(seq, pos++, s);
3866 }
3867 else {
3868 int j;
3869 ch = CHILD(ch, 0);
3870 REQ(ch, simple_stmt);
3871 for (j = 0; j < NCH(ch); j += 2) {
3872 /* statement terminates with a semi-colon ';' */
3873 if (NCH(CHILD(ch, j)) == 0) {
3874 assert((j + 1) == NCH(ch));
3875 break;
3876 }
3877 s = ast_for_stmt(c, CHILD(ch, j));
3878 if (!s)
3879 return NULL;
3880 asdl_seq_SET(seq, pos++, s);
3881 }
3882 }
3883 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 }
3885 assert(pos == seq->size);
3886 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887}
3888
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003889static void
3890get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3891{
Pablo Galindo46a97922019-02-19 22:51:53 +00003892 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003893 // There must be no empty suites.
3894 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003895 stmt_ty last = asdl_seq_GET(s, tot - 1);
3896 *end_lineno = last->end_lineno;
3897 *end_col_offset = last->end_col_offset;
3898}
3899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900static stmt_ty
3901ast_for_if_stmt(struct compiling *c, const node *n)
3902{
3903 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3904 ['else' ':' suite]
3905 */
3906 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003907 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908
3909 REQ(n, if_stmt);
3910
3911 if (NCH(n) == 4) {
3912 expr_ty expression;
3913 asdl_seq *suite_seq;
3914
3915 expression = ast_for_expr(c, CHILD(n, 1));
3916 if (!expression)
3917 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003919 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003921 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922
Guido van Rossumd8faa362007-04-27 19:54:29 +00003923 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003924 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003926
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 s = STR(CHILD(n, 4));
3928 /* s[2], the third character in the string, will be
3929 's' for el_s_e, or
3930 'i' for el_i_f
3931 */
3932 if (s[2] == 's') {
3933 expr_ty expression;
3934 asdl_seq *seq1, *seq2;
3935
3936 expression = ast_for_expr(c, CHILD(n, 1));
3937 if (!expression)
3938 return NULL;
3939 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003940 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 return NULL;
3942 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003943 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003945 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946
Guido van Rossumd8faa362007-04-27 19:54:29 +00003947 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003948 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949 }
3950 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003951 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 expr_ty expression;
3953 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003954 asdl_seq *orelse = NULL;
3955 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 /* must reference the child n_elif+1 since 'else' token is third,
3957 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003958 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3959 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3960 has_else = 1;
3961 n_elif -= 3;
3962 }
3963 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964
Thomas Wouters89f507f2006-12-13 04:49:30 +00003965 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003966 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003968 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003969 if (!orelse)
3970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003972 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003974 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3975 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003977 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3978 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003980 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 asdl_seq_SET(orelse, 0,
3983 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003984 LINENO(CHILD(n, NCH(n) - 7)),
3985 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003986 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003987 /* the just-created orelse handled the last elif */
3988 n_elif--;
3989 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990
Thomas Wouters89f507f2006-12-13 04:49:30 +00003991 for (i = 0; i < n_elif; i++) {
3992 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003993 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003994 if (!newobj)
3995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003997 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004000 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004003 if (orelse != NULL) {
4004 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4005 } else {
4006 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4007 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004008 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01004010 LINENO(CHILD(n, off - 1)),
4011 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004012 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004013 orelse = newobj;
4014 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004015 expression = ast_for_expr(c, CHILD(n, 1));
4016 if (!expression)
4017 return NULL;
4018 suite_seq = ast_for_suite(c, CHILD(n, 3));
4019 if (!suite_seq)
4020 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004021 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004022 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004023 LINENO(n), n->n_col_offset,
4024 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004026
4027 PyErr_Format(PyExc_SystemError,
4028 "unexpected token in 'if' statement: %s", s);
4029 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030}
4031
4032static stmt_ty
4033ast_for_while_stmt(struct compiling *c, const node *n)
4034{
4035 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4036 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004037 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038
4039 if (NCH(n) == 4) {
4040 expr_ty expression;
4041 asdl_seq *suite_seq;
4042
4043 expression = ast_for_expr(c, CHILD(n, 1));
4044 if (!expression)
4045 return NULL;
4046 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004047 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004049 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4050 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4051 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052 }
4053 else if (NCH(n) == 7) {
4054 expr_ty expression;
4055 asdl_seq *seq1, *seq2;
4056
4057 expression = ast_for_expr(c, CHILD(n, 1));
4058 if (!expression)
4059 return NULL;
4060 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004061 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 return NULL;
4063 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004064 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004066 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004068 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4069 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004071
4072 PyErr_Format(PyExc_SystemError,
4073 "wrong number of tokens for 'while' statement: %d",
4074 NCH(n));
4075 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076}
4077
4078static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004079ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080{
guoci90fc8982018-09-11 17:45:45 -04004081 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004082 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004084 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004085 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004086 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004087 int has_type_comment;
4088 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004089
4090 if (is_async && c->c_feature_version < 5) {
4091 ast_error(c, n,
4092 "Async for loops are only supported in Python 3.5 and greater");
4093 return NULL;
4094 }
4095
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004096 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097 REQ(n, for_stmt);
4098
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004099 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4100
4101 if (NCH(n) == 9 + has_type_comment) {
4102 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004103 if (!seq)
4104 return NULL;
4105 }
4106
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004107 node_target = CHILD(n, 1);
4108 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004109 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004111 /* Check the # of children rather than the length of _target, since
4112 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004113 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004114 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004115 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004117 target = Tuple(_target, Store, first->lineno, first->col_offset,
4118 node_target->n_end_lineno, node_target->n_end_col_offset,
4119 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004121 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004122 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004124 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004125 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126 return NULL;
4127
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004128 if (seq != NULL) {
4129 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4130 } else {
4131 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4132 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004133
4134 if (has_type_comment) {
4135 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4136 if (!type_comment)
4137 return NULL;
4138 }
4139 else
4140 type_comment = NULL;
4141
Yury Selivanov75445082015-05-11 22:57:16 -04004142 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004143 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004144 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004145 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004146 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004147 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004148 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004149 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150}
4151
4152static excepthandler_ty
4153ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4154{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004155 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004156 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157 REQ(exc, except_clause);
4158 REQ(body, suite);
4159
4160 if (NCH(exc) == 1) {
4161 asdl_seq *suite_seq = ast_for_suite(c, body);
4162 if (!suite_seq)
4163 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004164 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004165
Neal Norwitzad74aa82008-03-31 05:14:30 +00004166 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004167 exc->n_col_offset,
4168 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004169 }
4170 else if (NCH(exc) == 2) {
4171 expr_ty expression;
4172 asdl_seq *suite_seq;
4173
4174 expression = ast_for_expr(c, CHILD(exc, 1));
4175 if (!expression)
4176 return NULL;
4177 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004178 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004180 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181
Neal Norwitzad74aa82008-03-31 05:14:30 +00004182 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004183 exc->n_col_offset,
4184 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185 }
4186 else if (NCH(exc) == 4) {
4187 asdl_seq *suite_seq;
4188 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004189 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004190 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004192 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004193 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004194 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004195 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004196 return NULL;
4197 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004198 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004199 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004200 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004201
Neal Norwitzad74aa82008-03-31 05:14:30 +00004202 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004203 exc->n_col_offset,
4204 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004205 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004206
4207 PyErr_Format(PyExc_SystemError,
4208 "wrong number of children for 'except' clause: %d",
4209 NCH(exc));
4210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004211}
4212
4213static stmt_ty
4214ast_for_try_stmt(struct compiling *c, const node *n)
4215{
Neal Norwitzf599f422005-12-17 21:33:47 +00004216 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004217 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004218 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004219 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004221 REQ(n, try_stmt);
4222
Neal Norwitzf599f422005-12-17 21:33:47 +00004223 body = ast_for_suite(c, CHILD(n, 2));
4224 if (body == NULL)
4225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004226
Neal Norwitzf599f422005-12-17 21:33:47 +00004227 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4228 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4229 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4230 /* we can assume it's an "else",
4231 because nch >= 9 for try-else-finally and
4232 it would otherwise have a type of except_clause */
4233 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4234 if (orelse == NULL)
4235 return NULL;
4236 n_except--;
4237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004238
Neal Norwitzf599f422005-12-17 21:33:47 +00004239 finally = ast_for_suite(c, CHILD(n, nch - 1));
4240 if (finally == NULL)
4241 return NULL;
4242 n_except--;
4243 }
4244 else {
4245 /* we can assume it's an "else",
4246 otherwise it would have a type of except_clause */
4247 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4248 if (orelse == NULL)
4249 return NULL;
4250 n_except--;
4251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004252 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004253 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004254 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004255 return NULL;
4256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257
Neal Norwitzf599f422005-12-17 21:33:47 +00004258 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004259 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004260 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004261 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004262 if (handlers == NULL)
4263 return NULL;
4264
4265 for (i = 0; i < n_except; i++) {
4266 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4267 CHILD(n, 5 + i * 3));
4268 if (!e)
4269 return NULL;
4270 asdl_seq_SET(handlers, i, e);
4271 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004272 }
4273
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004274 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004275 if (finally != NULL) {
4276 // finally is always last
4277 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4278 } else if (orelse != NULL) {
4279 // otherwise else is last
4280 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4281 } else {
4282 // inline the get_last_end_pos logic due to layout mismatch
4283 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4284 end_lineno = last_handler->end_lineno;
4285 end_col_offset = last_handler->end_col_offset;
4286 }
4287 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4288 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004289}
4290
Georg Brandl0c315622009-05-25 21:10:36 +00004291/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004292static withitem_ty
4293ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004294{
4295 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004296
Georg Brandl0c315622009-05-25 21:10:36 +00004297 REQ(n, with_item);
4298 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004299 if (!context_expr)
4300 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004301 if (NCH(n) == 3) {
4302 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004303
4304 if (!optional_vars) {
4305 return NULL;
4306 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004307 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004308 return NULL;
4309 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004310 }
4311
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004312 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004313}
4314
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004315/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004316static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004317ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004318{
guoci90fc8982018-09-11 17:45:45 -04004319 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004320 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004321 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004322 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004323
Guido van Rossum495da292019-03-07 12:38:08 -08004324 if (is_async && c->c_feature_version < 5) {
4325 ast_error(c, n,
4326 "Async with statements are only supported in Python 3.5 and greater");
4327 return NULL;
4328 }
4329
Georg Brandl0c315622009-05-25 21:10:36 +00004330 REQ(n, with_stmt);
4331
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004332 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4333 nch_minus_type = NCH(n) - has_type_comment;
4334
4335 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004336 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004337 if (!items)
4338 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004339 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004340 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4341 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004342 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004343 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004344 }
4345
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004346 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4347 if (!body)
4348 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004349 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004350
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004351 if (has_type_comment) {
4352 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4353 if (!type_comment)
4354 return NULL;
4355 }
4356 else
4357 type_comment = NULL;
4358
Yury Selivanov75445082015-05-11 22:57:16 -04004359 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004360 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004361 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004362 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004363 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004364 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004365}
4366
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004367static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004368ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004369{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004370 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004371 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004372 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004373 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004374 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004375
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004376 REQ(n, classdef);
4377
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004378 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004379 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004380 if (!s)
4381 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004382 get_last_end_pos(s, &end_lineno, &end_col_offset);
4383
Benjamin Peterson30760062008-11-25 04:02:28 +00004384 classname = NEW_IDENTIFIER(CHILD(n, 1));
4385 if (!classname)
4386 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004387 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004388 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004389 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004390 LINENO(n), n->n_col_offset,
4391 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004392 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004393
4394 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004395 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004396 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004397 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004398 get_last_end_pos(s, &end_lineno, &end_col_offset);
4399
Benjamin Peterson30760062008-11-25 04:02:28 +00004400 classname = NEW_IDENTIFIER(CHILD(n, 1));
4401 if (!classname)
4402 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004403 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004404 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004405 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004406 LINENO(n), n->n_col_offset,
4407 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004408 }
4409
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004410 /* class NAME '(' arglist ')' ':' suite */
4411 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004412 {
4413 PyObject *dummy_name;
4414 expr_ty dummy;
4415 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4416 if (!dummy_name)
4417 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004418 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4419 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4420 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004421 call = ast_for_call(c, CHILD(n, 3), dummy,
4422 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004423 if (!call)
4424 return NULL;
4425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004426 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004427 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004428 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004429 get_last_end_pos(s, &end_lineno, &end_col_offset);
4430
Benjamin Peterson30760062008-11-25 04:02:28 +00004431 classname = NEW_IDENTIFIER(CHILD(n, 1));
4432 if (!classname)
4433 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004434 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004435 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004436
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004437 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004438 decorator_seq, LINENO(n), n->n_col_offset,
4439 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004440}
4441
4442static stmt_ty
4443ast_for_stmt(struct compiling *c, const node *n)
4444{
4445 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004446 assert(NCH(n) == 1);
4447 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004448 }
4449 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004450 assert(num_stmts(n) == 1);
4451 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452 }
4453 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004454 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004455 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4456 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004457 */
4458 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459 case expr_stmt:
4460 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461 case del_stmt:
4462 return ast_for_del_stmt(c, n);
4463 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004464 return Pass(LINENO(n), n->n_col_offset,
4465 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466 case flow_stmt:
4467 return ast_for_flow_stmt(c, n);
4468 case import_stmt:
4469 return ast_for_import_stmt(c, n);
4470 case global_stmt:
4471 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004472 case nonlocal_stmt:
4473 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004474 case assert_stmt:
4475 return ast_for_assert_stmt(c, n);
4476 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004477 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004478 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4479 TYPE(n), NCH(n));
4480 return NULL;
4481 }
4482 }
4483 else {
4484 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004485 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004486 */
4487 node *ch = CHILD(n, 0);
4488 REQ(n, compound_stmt);
4489 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004490 case if_stmt:
4491 return ast_for_if_stmt(c, ch);
4492 case while_stmt:
4493 return ast_for_while_stmt(c, ch);
4494 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004495 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004496 case try_stmt:
4497 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004498 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004499 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004500 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004501 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004502 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004503 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 case decorated:
4505 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004506 case async_stmt:
4507 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004508 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004509 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004510 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004511 TYPE(n), NCH(n));
4512 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004514 }
4515}
4516
4517static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004518parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004519{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004520 const char *end;
4521 long x;
4522 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004523 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004524 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004525
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004526 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004527 errno = 0;
4528 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004529 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004530 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004531 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004532 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004533 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004534 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004535 }
4536 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004537 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004538 if (*end == '\0') {
4539 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004540 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004541 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004542 }
4543 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004544 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004545 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004546 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4547 if (compl.imag == -1.0 && PyErr_Occurred())
4548 return NULL;
4549 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004550 }
4551 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004552 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004553 dx = PyOS_string_to_double(s, NULL, NULL);
4554 if (dx == -1.0 && PyErr_Occurred())
4555 return NULL;
4556 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004558}
4559
4560static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004561parsenumber(struct compiling *c, const char *s)
4562{
4563 char *dup, *end;
4564 PyObject *res = NULL;
4565
4566 assert(s != NULL);
4567
4568 if (strchr(s, '_') == NULL) {
4569 return parsenumber_raw(c, s);
4570 }
4571 /* Create a duplicate without underscores. */
4572 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004573 if (dup == NULL) {
4574 return PyErr_NoMemory();
4575 }
Brett Cannona721aba2016-09-09 14:57:09 -07004576 end = dup;
4577 for (; *s; s++) {
4578 if (*s != '_') {
4579 *end++ = *s;
4580 }
4581 }
4582 *end = '\0';
4583 res = parsenumber_raw(c, dup);
4584 PyMem_Free(dup);
4585 return res;
4586}
4587
4588static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004589decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004590{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004591 const char *s, *t;
4592 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004593 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4594 while (s < end && (*s & 0x80)) s++;
4595 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004596 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004597}
4598
Eric V. Smith56466482016-10-31 14:46:26 -04004599static int
4600warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004601 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004602{
4603 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4604 first_invalid_escape_char);
4605 if (msg == NULL) {
4606 return -1;
4607 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004608 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004609 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004610 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004611 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004612 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4613 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004614 to get a more accurate error report */
4615 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004616 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004617 }
4618 Py_DECREF(msg);
4619 return -1;
4620 }
4621 Py_DECREF(msg);
4622 return 0;
4623}
4624
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004625static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004626decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4627 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004628{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004629 PyObject *v, *u;
4630 char *buf;
4631 char *p;
4632 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004633
Benjamin Peterson202803a2016-02-25 22:34:45 -08004634 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004635 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004636 return NULL;
4637 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4638 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4639 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4640 if (u == NULL)
4641 return NULL;
4642 p = buf = PyBytes_AsString(u);
4643 end = s + len;
4644 while (s < end) {
4645 if (*s == '\\') {
4646 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004647 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004648 strcpy(p, "u005c");
4649 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004650 if (s >= end)
4651 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004652 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004653 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004654 if (*s & 0x80) { /* XXX inefficient */
4655 PyObject *w;
4656 int kind;
4657 void *data;
4658 Py_ssize_t len, i;
4659 w = decode_utf8(c, &s, end);
4660 if (w == NULL) {
4661 Py_DECREF(u);
4662 return NULL;
4663 }
4664 kind = PyUnicode_KIND(w);
4665 data = PyUnicode_DATA(w);
4666 len = PyUnicode_GET_LENGTH(w);
4667 for (i = 0; i < len; i++) {
4668 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4669 sprintf(p, "\\U%08x", chr);
4670 p += 10;
4671 }
4672 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004673 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004674 Py_DECREF(w);
4675 } else {
4676 *p++ = *s++;
4677 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004678 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004679 len = p - buf;
4680 s = buf;
4681
Eric V. Smith56466482016-10-31 14:46:26 -04004682 const char *first_invalid_escape;
4683 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4684
4685 if (v != NULL && first_invalid_escape != NULL) {
4686 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4687 /* We have not decref u before because first_invalid_escape points
4688 inside u. */
4689 Py_XDECREF(u);
4690 Py_DECREF(v);
4691 return NULL;
4692 }
4693 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004694 Py_XDECREF(u);
4695 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004696}
4697
Eric V. Smith56466482016-10-31 14:46:26 -04004698static PyObject *
4699decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4700 size_t len)
4701{
4702 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004703 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004704 &first_invalid_escape);
4705 if (result == NULL)
4706 return NULL;
4707
4708 if (first_invalid_escape != NULL) {
4709 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4710 Py_DECREF(result);
4711 return NULL;
4712 }
4713 }
4714 return result;
4715}
4716
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004717/* Shift locations for the given node and all its children by adding `lineno`
4718 and `col_offset` to existing locations. */
4719static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4720{
4721 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004722 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004723 for (int i = 0; i < NCH(n); ++i) {
4724 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4725 /* Shifting column offsets unnecessary if there's been newlines. */
4726 col_offset = 0;
4727 }
4728 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4729 }
4730 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004731 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004732}
4733
4734/* Fix locations for the given node and its children.
4735
4736 `parent` is the enclosing node.
4737 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004738 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004739*/
4740static void
4741fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4742{
4743 char *substr = NULL;
4744 char *start;
4745 int lines = LINENO(parent) - 1;
4746 int cols = parent->n_col_offset;
4747 /* Find the full fstring to fix location information in `n`. */
4748 while (parent && parent->n_type != STRING)
4749 parent = parent->n_child;
4750 if (parent && parent->n_str) {
4751 substr = strstr(parent->n_str, expr_str);
4752 if (substr) {
4753 start = substr;
4754 while (start > parent->n_str) {
4755 if (start[0] == '\n')
4756 break;
4757 start--;
4758 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004759 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004760 /* adjust the start based on the number of newlines encountered
4761 before the f-string expression */
4762 for (char* p = parent->n_str; p < substr; p++) {
4763 if (*p == '\n') {
4764 lines++;
4765 }
4766 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004767 }
4768 }
4769 fstring_shift_node_locations(n, lines, cols);
4770}
4771
Eric V. Smith451d0e32016-09-09 21:56:20 -04004772/* Compile this expression in to an expr_ty. Add parens around the
4773 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004774static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004775fstring_compile_expr(const char *expr_start, const char *expr_end,
4776 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004777
Eric V. Smith235a6f02015-09-19 14:51:32 -04004778{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004779 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004780 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004781 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004782 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004783 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004784
Eric V. Smith1d44c412015-09-23 07:49:00 -04004785 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004786 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004787 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4788 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004789
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004790 /* If the substring is all whitespace, it's an error. We need to catch this
4791 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4792 because turning the expression '' in to '()' would go from being invalid
4793 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004794 for (s = expr_start; s != expr_end; s++) {
4795 char c = *s;
4796 /* The Python parser ignores only the following whitespace
4797 characters (\r already is converted to \n). */
4798 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004799 break;
4800 }
4801 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004802 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004803 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004804 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004805 }
4806
Eric V. Smith451d0e32016-09-09 21:56:20 -04004807 len = expr_end - expr_start;
4808 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4809 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004810 if (str == NULL) {
4811 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004812 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004813 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004814
Eric V. Smith451d0e32016-09-09 21:56:20 -04004815 str[0] = '(';
4816 memcpy(str+1, expr_start, len);
4817 str[len+1] = ')';
4818 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004819
Victor Stinner37d66d72019-06-13 02:16:41 +02004820 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004821 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004822 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4823 Py_eval_input, 0);
4824 if (!mod_n) {
4825 PyMem_RawFree(str);
4826 return NULL;
4827 }
4828 /* Reuse str to find the correct column offset. */
4829 str[0] = '{';
4830 str[len+1] = '}';
4831 fstring_fix_node_location(n, mod_n, str);
4832 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004833 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004834 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004835 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004836 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004837 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004838}
4839
4840/* Return -1 on error.
4841
4842 Return 0 if we reached the end of the literal.
4843
4844 Return 1 if we haven't reached the end of the literal, but we want
4845 the caller to process the literal up to this point. Used for
4846 doubled braces.
4847*/
4848static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004849fstring_find_literal(const char **str, const char *end, int raw,
4850 PyObject **literal, int recurse_lvl,
4851 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004853 /* Get any literal string. It ends when we hit an un-doubled left
4854 brace (which isn't part of a unicode name escape such as
4855 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004857 const char *s = *str;
4858 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004859 int result = 0;
4860
Eric V. Smith235a6f02015-09-19 14:51:32 -04004861 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004862 while (s < end) {
4863 char ch = *s++;
4864 if (!raw && ch == '\\' && s < end) {
4865 ch = *s++;
4866 if (ch == 'N') {
4867 if (s < end && *s++ == '{') {
4868 while (s < end && *s++ != '}') {
4869 }
4870 continue;
4871 }
4872 break;
4873 }
4874 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4875 return -1;
4876 }
4877 }
4878 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879 /* Check for doubled braces, but only at the top level. If
4880 we checked at every level, then f'{0:{3}}' would fail
4881 with the two closing braces. */
4882 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004883 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004884 /* We're going to tell the caller that the literal ends
4885 here, but that they should continue scanning. But also
4886 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004887 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 result = 1;
4889 goto done;
4890 }
4891
4892 /* Where a single '{' is the start of a new expression, a
4893 single '}' is not allowed. */
4894 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004895 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004896 ast_error(c, n, "f-string: single '}' is not allowed");
4897 return -1;
4898 }
4899 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900 /* We're either at a '{', which means we're starting another
4901 expression; or a '}', which means we're at the end of this
4902 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004903 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904 break;
4905 }
4906 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004907 *str = s;
4908 assert(s <= end);
4909 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004910done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004911 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004912 if (raw)
4913 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004914 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004915 NULL, NULL);
4916 else
Eric V. Smith56466482016-10-31 14:46:26 -04004917 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004918 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919 if (!*literal)
4920 return -1;
4921 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004922 return result;
4923}
4924
4925/* Forward declaration because parsing is recursive. */
4926static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004927fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928 struct compiling *c, const node *n);
4929
Eric V. Smith451d0e32016-09-09 21:56:20 -04004930/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004931 expression (so it must be a '{'). Returns the FormattedValue node, which
4932 includes the expression, conversion character, format_spec expression, and
4933 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004934
4935 Note that I don't do a perfect job here: I don't make sure that a
4936 closing brace doesn't match an opening paren, for example. It
4937 doesn't need to error on all invalid expressions, just correctly
4938 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004939 will be caught when we parse it later.
4940
4941 *expression is set to the expression. For an '=' "debug" expression,
4942 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004943 including the '=' and any whitespace around it, as a string object). If
4944 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004945static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004946fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004947 PyObject **expr_text, expr_ty *expression,
4948 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004949{
4950 /* Return -1 on error, else 0. */
4951
Eric V. Smith451d0e32016-09-09 21:56:20 -04004952 const char *expr_start;
4953 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954 expr_ty simple_expression;
4955 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004956 int conversion = -1; /* The conversion char. Use default if not
4957 specified, or !r if using = and no format
4958 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959
4960 /* 0 if we're not in a string, else the quote char we're trying to
4961 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004962 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963
4964 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4965 int string_type = 0;
4966
4967 /* Keep track of nesting level for braces/parens/brackets in
4968 expressions. */
4969 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004970 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004971
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004972 *expr_text = NULL;
4973
Eric V. Smith235a6f02015-09-19 14:51:32 -04004974 /* Can only nest one level deep. */
4975 if (recurse_lvl >= 2) {
4976 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004977 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004978 }
4979
4980 /* The first char must be a left brace, or we wouldn't have gotten
4981 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004982 assert(**str == '{');
4983 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984
Eric V. Smith451d0e32016-09-09 21:56:20 -04004985 expr_start = *str;
4986 for (; *str < end; (*str)++) {
4987 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004988
4989 /* Loop invariants. */
4990 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004991 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004992 if (quote_char)
4993 assert(string_type == 1 || string_type == 3);
4994 else
4995 assert(string_type == 0);
4996
Eric V. Smith451d0e32016-09-09 21:56:20 -04004997 ch = **str;
4998 /* Nowhere inside an expression is a backslash allowed. */
4999 if (ch == '\\') {
5000 /* Error: can't include a backslash character, inside
5001 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005002 ast_error(c, n,
5003 "f-string expression part "
5004 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005005 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005006 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005007 if (quote_char) {
5008 /* We're inside a string. See if we're at the end. */
5009 /* This code needs to implement the same non-error logic
5010 as tok_get from tokenizer.c, at the letter_quote
5011 label. To actually share that code would be a
5012 nightmare. But, it's unlikely to change and is small,
5013 so duplicate it here. Note we don't need to catch all
5014 of the errors, since they'll be caught when parsing the
5015 expression. We just need to match the non-error
5016 cases. Thus we can ignore \n in single-quoted strings,
5017 for example. Or non-terminated strings. */
5018 if (ch == quote_char) {
5019 /* Does this match the string_type (single or triple
5020 quoted)? */
5021 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005023 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005024 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005025 string_type = 0;
5026 quote_char = 0;
5027 continue;
5028 }
5029 } else {
5030 /* We're at the end of a normal string. */
5031 quote_char = 0;
5032 string_type = 0;
5033 continue;
5034 }
5035 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005036 } else if (ch == '\'' || ch == '"') {
5037 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005038 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005039 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005040 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005041 } else {
5042 /* Start of a normal string. */
5043 string_type = 1;
5044 }
5045 /* Start looking for the end of the string. */
5046 quote_char = ch;
5047 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005048 if (nested_depth >= MAXLEVEL) {
5049 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005050 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005051 }
5052 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005053 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005054 } else if (ch == '#') {
5055 /* Error: can't include a comment character, inside parens
5056 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005057 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005058 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005060 (ch == '!' || ch == ':' || ch == '}' ||
5061 ch == '=' || ch == '>' || ch == '<')) {
5062 /* See if there's a next character. */
5063 if (*str+1 < end) {
5064 char next = *(*str+1);
5065
5066 /* For "!=". since '=' is not an allowed conversion character,
5067 nothing is lost in this test. */
5068 if ((ch == '!' && next == '=') || /* != */
5069 (ch == '=' && next == '=') || /* == */
5070 (ch == '<' && next == '=') || /* <= */
5071 (ch == '>' && next == '=') /* >= */
5072 ) {
5073 *str += 1;
5074 continue;
5075 }
5076 /* Don't get out of the loop for these, if they're single
5077 chars (not part of 2-char tokens). If by themselves, they
5078 don't end an expression (unlike say '!'). */
5079 if (ch == '>' || ch == '<') {
5080 continue;
5081 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005083
Eric V. Smith235a6f02015-09-19 14:51:32 -04005084 /* Normal way out of this loop. */
5085 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005086 } else if (ch == ']' || ch == '}' || ch == ')') {
5087 if (!nested_depth) {
5088 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005089 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005090 }
5091 nested_depth--;
5092 int opening = parenstack[nested_depth];
5093 if (!((opening == '(' && ch == ')') ||
5094 (opening == '[' && ch == ']') ||
5095 (opening == '{' && ch == '}')))
5096 {
5097 ast_error(c, n,
5098 "f-string: closing parenthesis '%c' "
5099 "does not match opening parenthesis '%c'",
5100 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005101 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005102 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005103 } else {
5104 /* Just consume this char and loop around. */
5105 }
5106 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 /* If we leave this loop in a string or with mismatched parens, we
5109 don't care. We'll get a syntax error when compiling the
5110 expression. But, we can produce a better error message, so
5111 let's just do that.*/
5112 if (quote_char) {
5113 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005114 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 }
5116 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005117 int opening = parenstack[nested_depth - 1];
5118 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005119 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005120 }
5121
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005124
5125 /* Compile the expression as soon as possible, so we show errors
5126 related to the expression before errors related to the
5127 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005128 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005129 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005130 goto error;
5131
5132 /* Check for =, which puts the text value of the expression in
5133 expr_text. */
5134 if (**str == '=') {
5135 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005136
5137 /* Skip over ASCII whitespace. No need to test for end of string
5138 here, since we know there's at least a trailing quote somewhere
5139 ahead. */
5140 while (Py_ISSPACE(**str)) {
5141 *str += 1;
5142 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005143
5144 /* Set *expr_text to the text of the expression. */
5145 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5146 if (!*expr_text) {
5147 goto error;
5148 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005149 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005150
5151 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005152 if (**str == '!') {
5153 *str += 1;
5154 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005155 goto unexpected_end_of_string;
5156
Eric V. Smith451d0e32016-09-09 21:56:20 -04005157 conversion = **str;
5158 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005159
5160 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005161 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005162 ast_error(c, n,
5163 "f-string: invalid conversion character: "
5164 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005165 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005166 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005167
5168 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005169
5170 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005172 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005173 if (**str == ':') {
5174 *str += 1;
5175 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005176 goto unexpected_end_of_string;
5177
5178 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005179 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005180 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005181 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005182 }
5183
Eric V. Smith451d0e32016-09-09 21:56:20 -04005184 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 goto unexpected_end_of_string;
5186
5187 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005188 assert(*str < end);
5189 assert(**str == '}');
5190 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005191
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005192 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005193 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005194 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005195 conversion = 'r';
5196 }
5197
Eric V. Smith451d0e32016-09-09 21:56:20 -04005198 /* And now create the FormattedValue node that represents this
5199 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005200 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005201 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005202 n->n_col_offset, n->n_end_lineno,
5203 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005204 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005205 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005206
5207 return 0;
5208
5209unexpected_end_of_string:
5210 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005211 /* Falls through to error. */
5212
5213error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005214 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005216
Eric V. Smith235a6f02015-09-19 14:51:32 -04005217}
5218
5219/* Return -1 on error.
5220
5221 Return 0 if we have a literal (possible zero length) and an
5222 expression (zero length if at the end of the string.
5223
5224 Return 1 if we have a literal, but no expression, and we want the
5225 caller to call us again. This is used to deal with doubled
5226 braces.
5227
5228 When called multiple times on the string 'a{{b{0}c', this function
5229 will return:
5230
5231 1. the literal 'a{' with no expression, and a return value
5232 of 1. Despite the fact that there's no expression, the return
5233 value of 1 means we're not finished yet.
5234
5235 2. the literal 'b' and the expression '0', with a return value of
5236 0. The fact that there's an expression means we're not finished.
5237
5238 3. literal 'c' with no expression and a return value of 0. The
5239 combination of the return value of 0 with no expression means
5240 we're finished.
5241*/
5242static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005243fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5244 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005245 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005246 struct compiling *c, const node *n)
5247{
5248 int result;
5249
5250 assert(*literal == NULL && *expression == NULL);
5251
5252 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005253 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005254 if (result < 0)
5255 goto error;
5256
5257 assert(result == 0 || result == 1);
5258
5259 if (result == 1)
5260 /* We have a literal, but don't look at the expression. */
5261 return 1;
5262
Eric V. Smith451d0e32016-09-09 21:56:20 -04005263 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005264 /* We're at the end of the string or the end of a nested
5265 f-string: no expression. The top-level error case where we
5266 expect to be at the end of the string but we're at a '}' is
5267 handled later. */
5268 return 0;
5269
5270 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005271 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005272
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005273 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5274 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005275 goto error;
5276
5277 return 0;
5278
5279error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005280 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005281 return -1;
5282}
5283
5284#define EXPRLIST_N_CACHED 64
5285
5286typedef struct {
5287 /* Incrementally build an array of expr_ty, so be used in an
5288 asdl_seq. Cache some small but reasonably sized number of
5289 expr_ty's, and then after that start dynamically allocating,
5290 doubling the number allocated each time. Note that the f-string
5291 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005292 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005293 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005294
5295 Py_ssize_t allocated; /* Number we've allocated. */
5296 Py_ssize_t size; /* Number we've used. */
5297 expr_ty *p; /* Pointer to the memory we're actually
5298 using. Will point to 'data' until we
5299 start dynamically allocating. */
5300 expr_ty data[EXPRLIST_N_CACHED];
5301} ExprList;
5302
5303#ifdef NDEBUG
5304#define ExprList_check_invariants(l)
5305#else
5306static void
5307ExprList_check_invariants(ExprList *l)
5308{
5309 /* Check our invariants. Make sure this object is "live", and
5310 hasn't been deallocated. */
5311 assert(l->size >= 0);
5312 assert(l->p != NULL);
5313 if (l->size <= EXPRLIST_N_CACHED)
5314 assert(l->data == l->p);
5315}
5316#endif
5317
5318static void
5319ExprList_Init(ExprList *l)
5320{
5321 l->allocated = EXPRLIST_N_CACHED;
5322 l->size = 0;
5323
5324 /* Until we start allocating dynamically, p points to data. */
5325 l->p = l->data;
5326
5327 ExprList_check_invariants(l);
5328}
5329
5330static int
5331ExprList_Append(ExprList *l, expr_ty exp)
5332{
5333 ExprList_check_invariants(l);
5334 if (l->size >= l->allocated) {
5335 /* We need to alloc (or realloc) the memory. */
5336 Py_ssize_t new_size = l->allocated * 2;
5337
5338 /* See if we've ever allocated anything dynamically. */
5339 if (l->p == l->data) {
5340 Py_ssize_t i;
5341 /* We're still using the cached data. Switch to
5342 alloc-ing. */
5343 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5344 if (!l->p)
5345 return -1;
5346 /* Copy the cached data into the new buffer. */
5347 for (i = 0; i < l->size; i++)
5348 l->p[i] = l->data[i];
5349 } else {
5350 /* Just realloc. */
5351 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5352 if (!tmp) {
5353 PyMem_RawFree(l->p);
5354 l->p = NULL;
5355 return -1;
5356 }
5357 l->p = tmp;
5358 }
5359
5360 l->allocated = new_size;
5361 assert(l->allocated == 2 * l->size);
5362 }
5363
5364 l->p[l->size++] = exp;
5365
5366 ExprList_check_invariants(l);
5367 return 0;
5368}
5369
5370static void
5371ExprList_Dealloc(ExprList *l)
5372{
5373 ExprList_check_invariants(l);
5374
5375 /* If there's been an error, or we've never dynamically allocated,
5376 do nothing. */
5377 if (!l->p || l->p == l->data) {
5378 /* Do nothing. */
5379 } else {
5380 /* We have dynamically allocated. Free the memory. */
5381 PyMem_RawFree(l->p);
5382 }
5383 l->p = NULL;
5384 l->size = -1;
5385}
5386
5387static asdl_seq *
5388ExprList_Finish(ExprList *l, PyArena *arena)
5389{
5390 asdl_seq *seq;
5391
5392 ExprList_check_invariants(l);
5393
5394 /* Allocate the asdl_seq and copy the expressions in to it. */
5395 seq = _Py_asdl_seq_new(l->size, arena);
5396 if (seq) {
5397 Py_ssize_t i;
5398 for (i = 0; i < l->size; i++)
5399 asdl_seq_SET(seq, i, l->p[i]);
5400 }
5401 ExprList_Dealloc(l);
5402 return seq;
5403}
5404
5405/* The FstringParser is designed to add a mix of strings and
5406 f-strings, and concat them together as needed. Ultimately, it
5407 generates an expr_ty. */
5408typedef struct {
5409 PyObject *last_str;
5410 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005411 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005412} FstringParser;
5413
5414#ifdef NDEBUG
5415#define FstringParser_check_invariants(state)
5416#else
5417static void
5418FstringParser_check_invariants(FstringParser *state)
5419{
5420 if (state->last_str)
5421 assert(PyUnicode_CheckExact(state->last_str));
5422 ExprList_check_invariants(&state->expr_list);
5423}
5424#endif
5425
5426static void
5427FstringParser_Init(FstringParser *state)
5428{
5429 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005430 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005431 ExprList_Init(&state->expr_list);
5432 FstringParser_check_invariants(state);
5433}
5434
5435static void
5436FstringParser_Dealloc(FstringParser *state)
5437{
5438 FstringParser_check_invariants(state);
5439
5440 Py_XDECREF(state->last_str);
5441 ExprList_Dealloc(&state->expr_list);
5442}
5443
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005444/* Constants for the following */
5445static PyObject *u_kind;
5446
5447/* Compute 'kind' field for string Constant (either 'u' or None) */
5448static PyObject *
5449make_kind(struct compiling *c, const node *n)
5450{
5451 char *s = NULL;
5452 PyObject *kind = NULL;
5453
5454 /* Find the first string literal, if any */
5455 while (TYPE(n) != STRING) {
5456 if (NCH(n) == 0)
5457 return NULL;
5458 n = CHILD(n, 0);
5459 }
5460 REQ(n, STRING);
5461
5462 /* If it starts with 'u', return a PyUnicode "u" string */
5463 s = STR(n);
5464 if (s && *s == 'u') {
5465 if (!u_kind) {
5466 u_kind = PyUnicode_InternFromString("u");
5467 if (!u_kind)
5468 return NULL;
5469 }
5470 kind = u_kind;
5471 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5472 return NULL;
5473 }
5474 Py_INCREF(kind);
5475 }
5476 return kind;
5477}
5478
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005479/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005480static expr_ty
5481make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5482{
5483 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005484 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005485 *str = NULL;
5486 assert(PyUnicode_CheckExact(s));
5487 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5488 Py_DECREF(s);
5489 return NULL;
5490 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005491 kind = make_kind(c, n);
5492 if (kind == NULL && PyErr_Occurred())
5493 return NULL;
5494 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005495 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005496}
5497
5498/* Add a non-f-string (that is, a regular literal string). str is
5499 decref'd. */
5500static int
5501FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5502{
5503 FstringParser_check_invariants(state);
5504
5505 assert(PyUnicode_CheckExact(str));
5506
5507 if (PyUnicode_GET_LENGTH(str) == 0) {
5508 Py_DECREF(str);
5509 return 0;
5510 }
5511
5512 if (!state->last_str) {
5513 /* We didn't have a string before, so just remember this one. */
5514 state->last_str = str;
5515 } else {
5516 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005517 PyUnicode_AppendAndDel(&state->last_str, str);
5518 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005519 return -1;
5520 }
5521 FstringParser_check_invariants(state);
5522 return 0;
5523}
5524
Eric V. Smith451d0e32016-09-09 21:56:20 -04005525/* Parse an f-string. The f-string is in *str to end, with no
5526 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005527static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005528FstringParser_ConcatFstring(FstringParser *state, const char **str,
5529 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005530 struct compiling *c, const node *n)
5531{
5532 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005533 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005534
5535 /* Parse the f-string. */
5536 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005537 PyObject *literal = NULL;
5538 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005539 expr_ty expression = NULL;
5540
5541 /* If there's a zero length literal in front of the
5542 expression, literal will be NULL. If we're at the end of
5543 the f-string, expression will be NULL (unless result == 1,
5544 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005545 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005546 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005547 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005548 if (result < 0)
5549 return -1;
5550
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005551 /* Add the literal, if any. */
5552 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5553 Py_XDECREF(expr_text);
5554 return -1;
5555 }
5556 /* Add the expr_text, if any. */
5557 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5558 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005559 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005560
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005561 /* We've dealt with the literal and expr_text, their ownership has
5562 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005563
5564 /* See if we should just loop around to get the next literal
5565 and expression, while ignoring the expression this
5566 time. This is used for un-doubling braces, as an
5567 optimization. */
5568 if (result == 1)
5569 continue;
5570
5571 if (!expression)
5572 /* We're done with this f-string. */
5573 break;
5574
5575 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005576 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005577 if (!state->last_str) {
5578 /* Do nothing. No previous literal. */
5579 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005580 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005581 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5582 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5583 return -1;
5584 }
5585
5586 if (ExprList_Append(&state->expr_list, expression) < 0)
5587 return -1;
5588 }
5589
Eric V. Smith235a6f02015-09-19 14:51:32 -04005590 /* If recurse_lvl is zero, then we must be at the end of the
5591 string. Otherwise, we must be at a right brace. */
5592
Eric V. Smith451d0e32016-09-09 21:56:20 -04005593 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005594 ast_error(c, n, "f-string: unexpected end of string");
5595 return -1;
5596 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005597 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005598 ast_error(c, n, "f-string: expecting '}'");
5599 return -1;
5600 }
5601
5602 FstringParser_check_invariants(state);
5603 return 0;
5604}
5605
5606/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005607 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005608static expr_ty
5609FstringParser_Finish(FstringParser *state, struct compiling *c,
5610 const node *n)
5611{
5612 asdl_seq *seq;
5613
5614 FstringParser_check_invariants(state);
5615
5616 /* If we're just a constant string with no expressions, return
5617 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005618 if (!state->fmode) {
5619 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005620 if (!state->last_str) {
5621 /* Create a zero length string. */
5622 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5623 if (!state->last_str)
5624 goto error;
5625 }
5626 return make_str_node_and_del(&state->last_str, c, n);
5627 }
5628
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005629 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005630 last node in our expression list. */
5631 if (state->last_str) {
5632 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5633 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5634 goto error;
5635 }
5636 /* This has already been freed. */
5637 assert(state->last_str == NULL);
5638
5639 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5640 if (!seq)
5641 goto error;
5642
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005643 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5644 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005645
5646error:
5647 FstringParser_Dealloc(state);
5648 return NULL;
5649}
5650
Eric V. Smith451d0e32016-09-09 21:56:20 -04005651/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5652 at end, parse it into an expr_ty. Return NULL on error. Adjust
5653 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005654static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005655fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005656 struct compiling *c, const node *n)
5657{
5658 FstringParser state;
5659
5660 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005661 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 c, n) < 0) {
5663 FstringParser_Dealloc(&state);
5664 return NULL;
5665 }
5666
5667 return FstringParser_Finish(&state, c, n);
5668}
5669
5670/* n is a Python string literal, including the bracketing quote
5671 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005672 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005674 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5675 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005676*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005677static int
5678parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5679 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005680{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005681 size_t len;
5682 const char *s = STR(n);
5683 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005684 int fmode = 0;
5685 *bytesmode = 0;
5686 *rawmode = 0;
5687 *result = NULL;
5688 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005689 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005690 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005691 if (quote == 'b' || quote == 'B') {
5692 quote = *++s;
5693 *bytesmode = 1;
5694 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005695 else if (quote == 'u' || quote == 'U') {
5696 quote = *++s;
5697 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005698 else if (quote == 'r' || quote == 'R') {
5699 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005700 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005701 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005702 else if (quote == 'f' || quote == 'F') {
5703 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005704 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005705 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005706 else {
5707 break;
5708 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005709 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005710 }
Guido van Rossum495da292019-03-07 12:38:08 -08005711
5712 /* fstrings are only allowed in Python 3.6 and greater */
5713 if (fmode && c->c_feature_version < 6) {
5714 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5715 return -1;
5716 }
5717
Eric V. Smith451d0e32016-09-09 21:56:20 -04005718 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005719 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005720 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005721 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005722 if (quote != '\'' && quote != '\"') {
5723 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005724 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005725 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005726 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005727 s++;
5728 len = strlen(s);
5729 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005730 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005731 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005732 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005733 }
5734 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005735 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005736 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005737 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005738 }
5739 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005740 /* A triple quoted string. We've already skipped one quote at
5741 the start and one at the end of the string. Now skip the
5742 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005743 s += 2;
5744 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005745 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005746 if (s[--len] != quote || s[--len] != quote) {
5747 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005748 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005749 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005750 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005751
Eric V. Smith451d0e32016-09-09 21:56:20 -04005752 if (fmode) {
5753 /* Just return the bytes. The caller will parse the resulting
5754 string. */
5755 *fstr = s;
5756 *fstrlen = len;
5757 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005758 }
5759
Eric V. Smith451d0e32016-09-09 21:56:20 -04005760 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005761 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005762 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005763 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005764 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005765 const char *ch;
5766 for (ch = s; *ch; ch++) {
5767 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005768 ast_error(c, n,
5769 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005770 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005771 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005772 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005773 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005774 if (*rawmode)
5775 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005776 else
Eric V. Smith56466482016-10-31 14:46:26 -04005777 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005778 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005779 if (*rawmode)
5780 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005781 else
Eric V. Smith56466482016-10-31 14:46:26 -04005782 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005783 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005784 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005785}
5786
Eric V. Smith235a6f02015-09-19 14:51:32 -04005787/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5788 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005789 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005790 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005791 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005792 node if there's just an f-string (with no leading or trailing
5793 literals), or a JoinedStr node if there are multiple f-strings or
5794 any literals involved. */
5795static expr_ty
5796parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005797{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005798 int bytesmode = 0;
5799 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005800 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005801
5802 FstringParser state;
5803 FstringParser_Init(&state);
5804
5805 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005806 int this_bytesmode;
5807 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005808 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005809 const char *fstr;
5810 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005811
5812 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005813 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5814 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005815 goto error;
5816
5817 /* Check that we're not mixing bytes with unicode. */
5818 if (i != 0 && bytesmode != this_bytesmode) {
5819 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005820 /* s is NULL if the current string part is an f-string. */
5821 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005822 goto error;
5823 }
5824 bytesmode = this_bytesmode;
5825
Eric V. Smith451d0e32016-09-09 21:56:20 -04005826 if (fstr != NULL) {
5827 int result;
5828 assert(s == NULL && !bytesmode);
5829 /* This is an f-string. Parse and concatenate it. */
5830 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5831 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005832 if (result < 0)
5833 goto error;
5834 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005835 /* A string or byte string. */
5836 assert(s != NULL && fstr == NULL);
5837
Eric V. Smith451d0e32016-09-09 21:56:20 -04005838 assert(bytesmode ? PyBytes_CheckExact(s) :
5839 PyUnicode_CheckExact(s));
5840
Eric V. Smith451d0e32016-09-09 21:56:20 -04005841 if (bytesmode) {
5842 /* For bytes, concat as we go. */
5843 if (i == 0) {
5844 /* First time, just remember this value. */
5845 bytes_str = s;
5846 } else {
5847 PyBytes_ConcatAndDel(&bytes_str, s);
5848 if (!bytes_str)
5849 goto error;
5850 }
5851 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005852 /* This is a regular string. Concatenate it. */
5853 if (FstringParser_ConcatAndDel(&state, s) < 0)
5854 goto error;
5855 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005856 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005857 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005858 if (bytesmode) {
5859 /* Just return the bytes object and we're done. */
5860 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5861 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005862 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005863 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005865
Eric V. Smith235a6f02015-09-19 14:51:32 -04005866 /* We're not a bytes string, bytes_str should never have been set. */
5867 assert(bytes_str == NULL);
5868
5869 return FstringParser_Finish(&state, c, n);
5870
5871error:
5872 Py_XDECREF(bytes_str);
5873 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005875}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005876
5877PyObject *
5878_PyAST_GetDocString(asdl_seq *body)
5879{
5880 if (!asdl_seq_LEN(body)) {
5881 return NULL;
5882 }
5883 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5884 if (st->kind != Expr_kind) {
5885 return NULL;
5886 }
5887 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005888 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5889 return e->v.Constant.value;
5890 }
5891 return NULL;
5892}