blob: 12f24f2c22ab96dff4ea475f5831b8585b4e038c [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",
297 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100298 return 0;
299 }
300 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400301 case JoinedStr_kind:
302 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
303 case FormattedValue_kind:
304 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
305 return 0;
306 if (exp->v.FormattedValue.format_spec)
307 return validate_expr(exp->v.FormattedValue.format_spec, Load);
308 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500309 case Attribute_kind:
310 return validate_expr(exp->v.Attribute.value, Load);
311 case Subscript_kind:
312 return validate_slice(exp->v.Subscript.slice) &&
313 validate_expr(exp->v.Subscript.value, Load);
314 case Starred_kind:
315 return validate_expr(exp->v.Starred.value, ctx);
316 case List_kind:
317 return validate_exprs(exp->v.List.elts, ctx, 0);
318 case Tuple_kind:
319 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000320 case NamedExpr_kind:
321 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300322 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500324 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000326 PyErr_SetString(PyExc_SystemError, "unexpected expression");
327 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800569 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570};
571
572static asdl_seq *seq_for_testlist(struct compiling *, const node *);
573static expr_ty ast_for_expr(struct compiling *, const node *);
574static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300575static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000576static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
577 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000578static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000579static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
guoci90fc8982018-09-11 17:45:45 -0400581static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
582static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200585static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000586 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000588static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400589static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000590static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Nick Coghlan650f0d02007-04-15 12:05:43 +0000592#define COMP_GENEXP 0
593#define COMP_LISTCOMP 1
594#define COMP_SETCOMP 2
595
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596static int
597init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000598{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500599 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
600 if (!m)
601 return 0;
602 c->c_normalize = PyObject_GetAttrString(m, "normalize");
603 Py_DECREF(m);
604 if (!c->c_normalize)
605 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500606 return 1;
607}
608
609static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400610new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500611{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400612 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000614 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500616 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000617 /* Check whether there are non-ASCII characters in the
618 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500619 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300621 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500622 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500623 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200624 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500625 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300626 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
627 if (form == NULL) {
628 Py_DECREF(id);
629 return NULL;
630 }
631 PyObject *args[2] = {form, id};
632 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500633 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200634 if (!id2)
635 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300636 if (!PyUnicode_Check(id2)) {
637 PyErr_Format(PyExc_TypeError,
638 "unicodedata.normalize() must return a string, not "
639 "%.200s",
640 Py_TYPE(id2)->tp_name);
641 Py_DECREF(id2);
642 return NULL;
643 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000645 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000646 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200647 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
648 Py_DECREF(id);
649 return NULL;
650 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000651 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652}
653
Benjamin Peterson55e00432012-01-16 17:22:31 -0500654#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200657ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400659 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200660 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200662 va_start(va, errmsg);
663 errstr = PyUnicode_FromFormatV(errmsg, va);
664 va_end(va);
665 if (!errstr) {
666 return 0;
667 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200668 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000670 Py_INCREF(Py_None);
671 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 }
Ammar Askar025eb982018-09-24 17:12:49 -0400673 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200674 if (!tmp) {
675 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400676 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000677 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 Py_DECREF(errstr);
680 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400681 if (value) {
682 PyErr_SetObject(PyExc_SyntaxError, value);
683 Py_DECREF(value);
684 }
685 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686}
687
688/* num_stmts() returns number of contained statements.
689
690 Use this routine to determine how big a sequence is needed for
691 the statements in a parse tree. Its raison d'etre is this bit of
692 grammar:
693
694 stmt: simple_stmt | compound_stmt
695 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
696
697 A simple_stmt can contain multiple small_stmt elements joined
698 by semicolons. If the arg is a simple_stmt, the number of
699 small_stmt elements is returned.
700*/
701
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800702static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800703new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800704{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800705 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800706 if (res == NULL)
707 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800708 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
709 Py_DECREF(res);
710 return NULL;
711 }
712 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800713}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800714#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800715
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716static int
717num_stmts(const node *n)
718{
719 int i, l;
720 node *ch;
721
722 switch (TYPE(n)) {
723 case single_input:
724 if (TYPE(CHILD(n, 0)) == NEWLINE)
725 return 0;
726 else
727 return num_stmts(CHILD(n, 0));
728 case file_input:
729 l = 0;
730 for (i = 0; i < NCH(n); i++) {
731 ch = CHILD(n, i);
732 if (TYPE(ch) == stmt)
733 l += num_stmts(ch);
734 }
735 return l;
736 case stmt:
737 return num_stmts(CHILD(n, 0));
738 case compound_stmt:
739 return 1;
740 case simple_stmt:
741 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
742 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800743 case func_body_suite:
744 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
745 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 if (NCH(n) == 1)
747 return num_stmts(CHILD(n, 0));
748 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800749 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800751 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
752 i += 2;
753 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 l += num_stmts(CHILD(n, i));
755 return l;
756 }
757 default: {
758 char buf[128];
759
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000760 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 TYPE(n), NCH(n));
762 Py_FatalError(buf);
763 }
764 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700765 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766}
767
768/* Transform the CST rooted at node * to the appropriate AST
769*/
770
771mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200772PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
773 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000775 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800777 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 stmt_ty s;
779 node *ch;
780 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800782 asdl_seq *argtypes = NULL;
783 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200786 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400787 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700789 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800790
791 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 switch (TYPE(n)) {
796 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200797 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500799 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 for (i = 0; i < NCH(n) - 1; i++) {
801 ch = CHILD(n, i);
802 if (TYPE(ch) == NEWLINE)
803 continue;
804 REQ(ch, stmt);
805 num = num_stmts(ch);
806 if (num == 1) {
807 s = ast_for_stmt(&c, ch);
808 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000810 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 else {
813 ch = CHILD(ch, 0);
814 REQ(ch, simple_stmt);
815 for (j = 0; j < num; j++) {
816 s = ast_for_stmt(&c, CHILD(ch, j * 2));
817 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000819 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 }
822 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800823
824 /* Type ignores are stored under the ENDMARKER in file_input. */
825 ch = CHILD(n, NCH(n) - 1);
826 REQ(ch, ENDMARKER);
827 num = NCH(ch);
828 type_ignores = _Py_asdl_seq_new(num, arena);
829 if (!type_ignores)
830 goto out;
831
832 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700833 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
834 if (!type_comment)
835 goto out;
836 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800837 if (!ti)
838 goto out;
839 asdl_seq_SET(type_ignores, i, ti);
840 }
841
842 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 case eval_input: {
845 expr_ty testlist_ast;
846
Nick Coghlan650f0d02007-04-15 12:05:43 +0000847 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000848 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
851 res = Expression(testlist_ast, arena);
852 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 case single_input:
855 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200856 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000860 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000862 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
864 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
866 else {
867 n = CHILD(n, 0);
868 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200869 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000873 s = ast_for_stmt(&c, n);
874 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500875 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 asdl_seq_SET(stmts, 0, s);
877 }
878 else {
879 /* Only a simple_stmt can contain multiple statements. */
880 REQ(n, simple_stmt);
881 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 if (TYPE(CHILD(n, i)) == NEWLINE)
883 break;
884 s = ast_for_stmt(&c, CHILD(n, i));
885 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500886 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 asdl_seq_SET(stmts, i / 2, s);
888 }
889 }
890
Benjamin Peterson55e00432012-01-16 17:22:31 -0500891 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500893 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800894 case func_type_input:
895 n = CHILD(n, 0);
896 REQ(n, func_type);
897
898 if (TYPE(CHILD(n, 1)) == typelist) {
899 ch = CHILD(n, 1);
900 /* this is overly permissive -- we don't pay any attention to
901 * stars on the args -- just parse them into an ordered list */
902 num = 0;
903 for (i = 0; i < NCH(ch); i++) {
904 if (TYPE(CHILD(ch, i)) == test) {
905 num++;
906 }
907 }
908
909 argtypes = _Py_asdl_seq_new(num, arena);
910 if (!argtypes)
911 goto out;
912
913 j = 0;
914 for (i = 0; i < NCH(ch); i++) {
915 if (TYPE(CHILD(ch, i)) == test) {
916 arg = ast_for_expr(&c, CHILD(ch, i));
917 if (!arg)
918 goto out;
919 asdl_seq_SET(argtypes, j++, arg);
920 }
921 }
922 }
923 else {
924 argtypes = _Py_asdl_seq_new(0, arena);
925 if (!argtypes)
926 goto out;
927 }
928
929 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
930 if (!ret)
931 goto out;
932 res = FunctionType(argtypes, ret, arena);
933 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000935 PyErr_Format(PyExc_SystemError,
936 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500937 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500939 out:
940 if (c.c_normalize) {
941 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500942 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500943 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944}
945
Victor Stinner14e461d2013-08-26 22:28:21 +0200946mod_ty
947PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
948 PyArena *arena)
949{
950 mod_ty mod;
951 PyObject *filename;
952 filename = PyUnicode_DecodeFSDefault(filename_str);
953 if (filename == NULL)
954 return NULL;
955 mod = PyAST_FromNodeObject(n, flags, filename, arena);
956 Py_DECREF(filename);
957 return mod;
958
959}
960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
962*/
963
964static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800965get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 switch (TYPE(n)) {
968 case VBAR:
969 return BitOr;
970 case CIRCUMFLEX:
971 return BitXor;
972 case AMPER:
973 return BitAnd;
974 case LEFTSHIFT:
975 return LShift;
976 case RIGHTSHIFT:
977 return RShift;
978 case PLUS:
979 return Add;
980 case MINUS:
981 return Sub;
982 case STAR:
983 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400984 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800985 if (c->c_feature_version < 5) {
986 ast_error(c, n,
987 "The '@' operator is only supported in Python 3.5 and greater");
988 return (operator_ty)0;
989 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400990 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 case SLASH:
992 return Div;
993 case DOUBLESLASH:
994 return FloorDiv;
995 case PERCENT:
996 return Mod;
997 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 }
1000}
1001
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001002static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001003 "None",
1004 "True",
1005 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001006 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001007 NULL,
1008};
1009
1010static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001011forbidden_name(struct compiling *c, identifier name, const node *n,
1012 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001013{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001014 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001015 const char * const *p = FORBIDDEN;
1016 if (!full_checks) {
1017 /* In most cases, the parser will protect True, False, and None
1018 from being assign to. */
1019 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001020 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001021 for (; *p; p++) {
1022 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1023 ast_error(c, n, "cannot assign to %U", name);
1024 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001025 }
1026 }
1027 return 0;
1028}
1029
Serhiy Storchakab619b092018-11-27 09:40:29 +02001030static expr_ty
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08001031copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001032{
1033 if (e) {
1034 e->lineno = LINENO(n);
1035 e->col_offset = n->n_col_offset;
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08001036 e->end_lineno = end->n_end_lineno;
1037 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001038 }
1039 return e;
1040}
1041
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001042static const char *
1043get_expr_name(expr_ty e)
1044{
1045 switch (e->kind) {
1046 case Attribute_kind:
1047 return "attribute";
1048 case Subscript_kind:
1049 return "subscript";
1050 case Starred_kind:
1051 return "starred";
1052 case Name_kind:
1053 return "name";
1054 case List_kind:
1055 return "list";
1056 case Tuple_kind:
1057 return "tuple";
1058 case Lambda_kind:
1059 return "lambda";
1060 case Call_kind:
1061 return "function call";
1062 case BoolOp_kind:
1063 case BinOp_kind:
1064 case UnaryOp_kind:
1065 return "operator";
1066 case GeneratorExp_kind:
1067 return "generator expression";
1068 case Yield_kind:
1069 case YieldFrom_kind:
1070 return "yield expression";
1071 case Await_kind:
1072 return "await expression";
1073 case ListComp_kind:
1074 return "list comprehension";
1075 case SetComp_kind:
1076 return "set comprehension";
1077 case DictComp_kind:
1078 return "dict comprehension";
1079 case Dict_kind:
1080 return "dict display";
1081 case Set_kind:
1082 return "set display";
1083 case JoinedStr_kind:
1084 case FormattedValue_kind:
1085 return "f-string expression";
1086 case Constant_kind: {
1087 PyObject *value = e->v.Constant.value;
1088 if (value == Py_None) {
1089 return "None";
1090 }
1091 if (value == Py_False) {
1092 return "False";
1093 }
1094 if (value == Py_True) {
1095 return "True";
1096 }
1097 if (value == Py_Ellipsis) {
1098 return "Ellipsis";
1099 }
1100 return "literal";
1101 }
1102 case Compare_kind:
1103 return "comparison";
1104 case IfExp_kind:
1105 return "conditional expression";
1106 case NamedExpr_kind:
1107 return "named expression";
1108 default:
1109 PyErr_Format(PyExc_SystemError,
1110 "unexpected expression in assignment %d (line %d)",
1111 e->kind, e->lineno);
1112 return NULL;
1113 }
1114}
1115
Jeremy Hyltona8293132006-02-28 17:58:27 +00001116/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117
1118 Only sets context for expr kinds that "can appear in assignment context"
1119 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1120 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121*/
1122
1123static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001124set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125{
1126 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001127
1128 /* The ast defines augmented store and load contexts, but the
1129 implementation here doesn't actually use them. The code may be
1130 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001131 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001132 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001133 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001134 */
1135 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
1137 switch (e->kind) {
1138 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001140 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001141 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001142 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001144 e->v.Subscript.ctx = ctx;
1145 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001146 case Starred_kind:
1147 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001148 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001149 return 0;
1150 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001152 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001153 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001154 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001155 }
1156 e->v.Name.ctx = ctx;
1157 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 e->v.List.ctx = ctx;
1160 s = e->v.List.elts;
1161 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001163 e->v.Tuple.ctx = ctx;
1164 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001166 default: {
1167 const char *expr_name = get_expr_name(e);
1168 if (expr_name != NULL) {
1169 ast_error(c, n, "cannot %s %s",
1170 ctx == Store ? "assign to" : "delete",
1171 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001172 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001173 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001174 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001175 }
1176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 */
1180 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001181 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182
Thomas Wouters89f507f2006-12-13 04:49:30 +00001183 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001184 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001185 return 0;
1186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 }
1188 return 1;
1189}
1190
1191static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001192ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193{
1194 REQ(n, augassign);
1195 n = CHILD(n, 0);
1196 switch (STR(n)[0]) {
1197 case '+':
1198 return Add;
1199 case '-':
1200 return Sub;
1201 case '/':
1202 if (STR(n)[1] == '/')
1203 return FloorDiv;
1204 else
1205 return Div;
1206 case '%':
1207 return Mod;
1208 case '<':
1209 return LShift;
1210 case '>':
1211 return RShift;
1212 case '&':
1213 return BitAnd;
1214 case '^':
1215 return BitXor;
1216 case '|':
1217 return BitOr;
1218 case '*':
1219 if (STR(n)[1] == '*')
1220 return Pow;
1221 else
1222 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001223 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001224 if (c->c_feature_version < 5) {
1225 ast_error(c, n,
1226 "The '@' operator is only supported in Python 3.5 and greater");
1227 return (operator_ty)0;
1228 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001229 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001231 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 }
1234}
1235
1236static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001237ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001239 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 |'is' 'not'
1241 */
1242 REQ(n, comp_op);
1243 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001244 n = CHILD(n, 0);
1245 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 case LESS:
1247 return Lt;
1248 case GREATER:
1249 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001250 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return Eq;
1252 case LESSEQUAL:
1253 return LtE;
1254 case GREATEREQUAL:
1255 return GtE;
1256 case NOTEQUAL:
1257 return NotEq;
1258 case NAME:
1259 if (strcmp(STR(n), "in") == 0)
1260 return In;
1261 if (strcmp(STR(n), "is") == 0)
1262 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001263 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001265 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
1270 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001271 /* handle "not in" and "is not" */
1272 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 case NAME:
1274 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1275 return NotIn;
1276 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1277 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001278 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001280 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001282 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001283 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 }
Neal Norwitz79792652005-11-14 04:25:03 +00001285 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288}
1289
1290static asdl_seq *
1291seq_for_testlist(struct compiling *c, const node *n)
1292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001294 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1295 */
Armin Rigo31441302005-10-21 12:57:31 +00001296 asdl_seq *seq;
1297 expr_ty expression;
1298 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001299 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001301 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 if (!seq)
1303 return NULL;
1304
1305 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001307 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308
Benjamin Peterson4905e802009-09-27 02:43:28 +00001309 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001310 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312
1313 assert(i / 2 < seq->size);
1314 asdl_seq_SET(seq, i / 2, expression);
1315 }
1316 return seq;
1317}
1318
Neal Norwitzc1505362006-12-28 06:47:50 +00001319static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001320ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001321{
1322 identifier name;
1323 expr_ty annotation = NULL;
1324 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001325 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001326
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001327 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001329 name = NEW_IDENTIFIER(ch);
1330 if (!name)
1331 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001332 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001333 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001334
1335 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1336 annotation = ast_for_expr(c, CHILD(n, 2));
1337 if (!annotation)
1338 return NULL;
1339 }
1340
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001341 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001342 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001343 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001344 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001345 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346}
1347
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348/* returns -1 if failed to handle keyword only arguments
1349 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001350 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 ^^^
1352 start pointing here
1353 */
1354static int
1355handle_keywordonly_args(struct compiling *c, const node *n, int start,
1356 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1357{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001358 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001360 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001361 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 int i = start;
1363 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001364
1365 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001366 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001367 return -1;
1368 }
1369 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 while (i < NCH(n)) {
1371 ch = CHILD(n, i);
1372 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001373 case vfpdef:
1374 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001378 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001379 asdl_seq_SET(kwdefaults, j, expression);
1380 i += 2; /* '=' and test */
1381 }
1382 else { /* setting NULL if no default value exists */
1383 asdl_seq_SET(kwdefaults, j, NULL);
1384 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 if (NCH(ch) == 3) {
1386 /* ch is NAME ':' test */
1387 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001388 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001389 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001390 }
1391 else {
1392 annotation = NULL;
1393 }
1394 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001395 argname = NEW_IDENTIFIER(ch);
1396 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001398 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001399 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001400 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001401 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001402 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001403 if (!arg)
1404 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001405 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001406 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001407 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001408 i += 1; /* the comma, if present */
1409 break;
1410 case TYPE_COMMENT:
1411 /* arg will be equal to the last argument processed */
1412 arg->type_comment = NEW_TYPE_COMMENT(ch);
1413 if (!arg->type_comment)
1414 goto error;
1415 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 break;
1417 case DOUBLESTAR:
1418 return i;
1419 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001420 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 goto error;
1422 }
1423 }
1424 return i;
1425 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428
Jeremy Hyltona8293132006-02-28 17:58:27 +00001429/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430
1431static arguments_ty
1432ast_for_arguments(struct compiling *c, const node *n)
1433{
Neal Norwitzc1505362006-12-28 06:47:50 +00001434 /* This function handles both typedargslist (function definition)
1435 and varargslist (lambda definition).
1436
1437 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001438
1439 The following definition for typedarglist is equivalent to this set of rules:
1440
1441 arguments = argument (',' [TYPE_COMMENT] argument)*
1442 argument = tfpdef ['=' test]
1443 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1444 args = '*' [tfpdef]
1445 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1446 [TYPE_COMMENT] [kwargs]])
1447 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1448 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1449 [TYPE_COMMENT] [args_kwonly_kwargs]])
1450 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1451 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1452 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1453
1454 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1455 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1456 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1457 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1458 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1459 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1460 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1461 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1462 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1463 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1464 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1465 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1466 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1467 '**' tfpdef [','] [TYPE_COMMENT]))
1468
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001469 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001470
1471 The following definition for varargslist is equivalent to this set of rules:
1472
1473 arguments = argument (',' argument )*
1474 argument = vfpdef ['=' test]
1475 kwargs = '**' vfpdef [',']
1476 args = '*' [vfpdef]
1477 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1478 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1479 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1480 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1481 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1482 (vararglist_no_posonly)
1483
1484 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1485 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1486 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1487 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1488 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1489 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1490 [',']]] | '**' vfpdef [','])
1491
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001492 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001495 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001497 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001498 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001499 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 node *ch;
1501
1502 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001504 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001507 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
Jeremy Hyltone921e022008-07-17 16:37:17 +00001509 /* First count the number of positional args & defaults. The
1510 variable i is the loop index for this for loop and the next.
1511 The next loop picks up where the first leaves off.
1512 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001514 ch = CHILD(n, i);
1515 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001516 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001517 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001518 if (i < NCH(n) && /* skip argument following star */
1519 (TYPE(CHILD(n, i)) == tfpdef ||
1520 TYPE(CHILD(n, i)) == vfpdef)) {
1521 i++;
1522 }
1523 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001524 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001525 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001526 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001527 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001528 if (TYPE(ch) == SLASH ) {
1529 nposonlyargs = nposargs;
1530 nposargs = 0;
1531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001534 defaults for keyword only args */
1535 for ( ; i < NCH(n); ++i) {
1536 ch = CHILD(n, i);
1537 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001538 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001539 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001540 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1541 if (!posonlyargs && nposonlyargs) {
1542 return NULL;
1543 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001544 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001545 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001546 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001548 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001549 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001550 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001552 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001553 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001554 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 since we set NULL as default for keyword only argument w/o default
1557 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001558 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001559 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001560 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001561 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001562
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001563 /* tfpdef: NAME [':' test]
1564 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 */
1566 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001567 j = 0; /* index for defaults */
1568 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001569 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001571 ch = CHILD(n, i);
1572 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001573 case tfpdef:
1574 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1576 anything other than EQUAL or a comma? */
1577 /* XXX Should NCH(n) check be made a separate check? */
1578 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001579 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1580 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001581 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001582 assert(posdefaults != NULL);
1583 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001585 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001587 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001588 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001589 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001590 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001591 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001592 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001593 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001594 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001595 if (l < nposonlyargs) {
1596 asdl_seq_SET(posonlyargs, l++, arg);
1597 } else {
1598 asdl_seq_SET(posargs, k++, arg);
1599 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001600 i += 1; /* the name */
1601 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1602 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001604 case SLASH:
1605 /* Advance the slash and the comma. If there are more names
1606 * after the slash there will be a comma so we are advancing
1607 * the correct number of nodes. If the slash is the last item,
1608 * we will be advancing an extra token but then * i > NCH(n)
1609 * and the enclosing while will finish correctly. */
1610 i += 2;
1611 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001613 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001614 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1615 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001616 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001617 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001618 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001619 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001620 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001621 if (TYPE(ch) == COMMA) {
1622 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001623 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001624
1625 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1626 ast_error(c, CHILD(n, i),
1627 "bare * has associated type comment");
1628 return NULL;
1629 }
1630
Guido van Rossum4f72a782006-10-27 23:31:49 +00001631 res = handle_keywordonly_args(c, n, i,
1632 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001633 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001634 i = res; /* res has new position to process */
1635 }
1636 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001637 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001638 if (!vararg)
1639 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001640
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001641 i += 2; /* the star and the name */
1642 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1643 i += 1; /* the comma, if present */
1644
1645 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1646 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1647 if (!vararg->type_comment)
1648 return NULL;
1649 i += 1;
1650 }
1651
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001652 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1653 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001654 int res = 0;
1655 res = handle_keywordonly_args(c, n, i,
1656 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001657 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001658 i = res; /* res has new position to process */
1659 }
1660 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 break;
1662 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001663 ch = CHILD(n, i+1); /* tfpdef */
1664 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001665 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001666 if (!kwarg)
1667 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001668 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001669 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001670 i += 1; /* the comma, if present */
1671 break;
1672 case TYPE_COMMENT:
1673 assert(i);
1674
1675 if (kwarg)
1676 arg = kwarg;
1677
1678 /* arg will be equal to the last argument processed */
1679 arg->type_comment = NEW_TYPE_COMMENT(ch);
1680 if (!arg->type_comment)
1681 return NULL;
1682 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 break;
1684 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001685 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 "unexpected node in varargslist: %d @ %d",
1687 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001688 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001689 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 }
Miss Islington (bot)cf9a63c2019-07-14 16:49:52 -07001691 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692}
1693
1694static expr_ty
1695ast_for_dotted_name(struct compiling *c, const node *n)
1696{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001697 expr_ty e;
1698 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001699 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001701 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702
1703 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001704
1705 lineno = LINENO(n);
1706 col_offset = n->n_col_offset;
1707
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001708 ch = CHILD(n, 0);
1709 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001711 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001712 e = Name(id, Load, lineno, col_offset,
1713 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716
1717 for (i = 2; i < NCH(n); i+=2) {
Lysandros Nikolaou8b9cebc2020-02-08 01:21:38 +01001718 const node *child = CHILD(n, i);
1719 id = NEW_IDENTIFIER(child);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001720 if (!id)
1721 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001722 e = Attribute(e, id, Load, lineno, col_offset,
Lysandros Nikolaou8b9cebc2020-02-08 01:21:38 +01001723 child->n_end_lineno, child->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001724 if (!e)
1725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 }
1727
1728 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729}
1730
1731static expr_ty
1732ast_for_decorator(struct compiling *c, const node *n)
1733{
1734 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1735 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001736 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001739 REQ(CHILD(n, 0), AT);
1740 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1743 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001744 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001747 d = name_expr;
1748 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 }
1750 else if (NCH(n) == 5) { /* Call with no arguments */
Miss Skeleton (bot)ba3a5662019-10-26 07:06:40 -07001751 d = Call(name_expr, NULL, NULL,
1752 name_expr->lineno, name_expr->col_offset,
1753 CHILD(n, 3)->n_end_lineno, CHILD(n, 3)->n_end_col_offset,
1754 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 if (!d)
1756 return NULL;
1757 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 }
1759 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001760 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001761 if (!d)
1762 return NULL;
1763 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 }
1765
1766 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767}
1768
1769static asdl_seq*
1770ast_for_decorators(struct compiling *c, const node *n)
1771{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001772 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001773 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001777 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778 if (!decorator_seq)
1779 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001782 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001783 if (!d)
1784 return NULL;
1785 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 }
1787 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788}
1789
1790static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001791ast_for_funcdef_impl(struct compiling *c, const node *n0,
1792 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001794 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001795 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001796 identifier name;
1797 arguments_ty args;
1798 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001799 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001800 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001801 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001802 node *tc;
1803 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804
Guido van Rossum495da292019-03-07 12:38:08 -08001805 if (is_async && c->c_feature_version < 5) {
1806 ast_error(c, n,
1807 "Async functions are only supported in Python 3.5 and greater");
1808 return NULL;
1809 }
1810
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 REQ(n, funcdef);
1812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 name = NEW_IDENTIFIER(CHILD(n, name_i));
1814 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001815 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001816 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1819 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001820 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001821 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1822 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1823 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001824 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001825 name_i += 2;
1826 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001827 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1828 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1829 if (!type_comment)
1830 return NULL;
1831 name_i += 1;
1832 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001833 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001835 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001836 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001838 if (NCH(CHILD(n, name_i + 3)) > 1) {
1839 /* Check if the suite has a type comment in it. */
1840 tc = CHILD(CHILD(n, name_i + 3), 1);
1841
1842 if (TYPE(tc) == TYPE_COMMENT) {
1843 if (type_comment != NULL) {
1844 ast_error(c, n, "Cannot have two type comments on def");
1845 return NULL;
1846 }
1847 type_comment = NEW_TYPE_COMMENT(tc);
1848 if (!type_comment)
1849 return NULL;
1850 }
1851 }
1852
Yury Selivanov75445082015-05-11 22:57:16 -04001853 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001854 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001855 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001856 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001857 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001858 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001859}
1860
1861static stmt_ty
1862ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1863{
Guido van Rossum495da292019-03-07 12:38:08 -08001864 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001865 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001866 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001867 REQ(CHILD(n, 1), funcdef);
1868
guoci90fc8982018-09-11 17:45:45 -04001869 return ast_for_funcdef_impl(c, n, decorator_seq,
1870 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001871}
1872
1873static stmt_ty
1874ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1875{
1876 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1877 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001878 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001879}
1880
1881
1882static stmt_ty
1883ast_for_async_stmt(struct compiling *c, const node *n)
1884{
Guido van Rossum495da292019-03-07 12:38:08 -08001885 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001886 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001887 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001888
1889 switch (TYPE(CHILD(n, 1))) {
1890 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001891 return ast_for_funcdef_impl(c, n, NULL,
1892 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001893 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001894 return ast_for_with_stmt(c, n,
1895 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001896
1897 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001898 return ast_for_for_stmt(c, n,
1899 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001900
1901 default:
1902 PyErr_Format(PyExc_SystemError,
1903 "invalid async stament: %s",
1904 STR(CHILD(n, 1)));
1905 return NULL;
1906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907}
1908
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001909static stmt_ty
1910ast_for_decorated(struct compiling *c, const node *n)
1911{
Yury Selivanov75445082015-05-11 22:57:16 -04001912 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001913 stmt_ty thing = NULL;
1914 asdl_seq *decorator_seq = NULL;
1915
1916 REQ(n, decorated);
1917
1918 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1919 if (!decorator_seq)
1920 return NULL;
1921
1922 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001923 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001925
1926 if (TYPE(CHILD(n, 1)) == funcdef) {
1927 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1928 } else if (TYPE(CHILD(n, 1)) == classdef) {
1929 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001930 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1931 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001932 }
1933 return thing;
1934}
1935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001937ast_for_namedexpr(struct compiling *c, const node *n)
1938{
Miss Islington (bot)cd968de2019-12-15 12:04:07 -08001939 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001940 argument: ( test [comp_for] |
1941 test ':=' test |
1942 test '=' test |
1943 '**' test |
1944 '*' test )
1945 */
1946 expr_ty target, value;
1947
1948 target = ast_for_expr(c, CHILD(n, 0));
1949 if (!target)
1950 return NULL;
1951
1952 value = ast_for_expr(c, CHILD(n, 2));
1953 if (!value)
1954 return NULL;
1955
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001956 if (target->kind != Name_kind) {
1957 const char *expr_name = get_expr_name(target);
1958 if (expr_name != NULL) {
Miss Islington (bot)6c004952019-12-31 19:28:08 -08001959 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001960 }
1961 return NULL;
1962 }
1963
1964 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001965 return NULL;
1966
1967 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1968 n->n_end_col_offset, c->c_arena);
1969}
1970
1971static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972ast_for_lambdef(struct compiling *c, const node *n)
1973{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001974 /* lambdef: 'lambda' [varargslist] ':' test
1975 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 arguments_ty args;
1977 expr_ty expression;
1978
1979 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001980 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 if (!args)
1982 return NULL;
1983 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001984 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 }
1987 else {
1988 args = ast_for_arguments(c, CHILD(n, 1));
1989 if (!args)
1990 return NULL;
1991 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001992 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 }
1995
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001996 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1997 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998}
1999
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002000static expr_ty
2001ast_for_ifexpr(struct compiling *c, const node *n)
2002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002004 expr_ty expression, body, orelse;
2005
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00002006 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002007 body = ast_for_expr(c, CHILD(n, 0));
2008 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002009 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002010 expression = ast_for_expr(c, CHILD(n, 2));
2011 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002012 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002013 orelse = ast_for_expr(c, CHILD(n, 4));
2014 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002015 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002016 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002017 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002018 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002019}
2020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002022 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023
Nick Coghlan650f0d02007-04-15 12:05:43 +00002024 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025*/
2026
2027static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002028count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002030 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031
Guido van Rossumd8faa362007-04-27 19:54:29 +00002032 count_comp_for:
2033 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002034 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08002036 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002037 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002038 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002039 else if (NCH(n) == 1) {
2040 n = CHILD(n, 0);
2041 }
2042 else {
2043 goto error;
2044 }
2045 if (NCH(n) == (5)) {
2046 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002047 }
2048 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002049 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002050 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002051 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002052 REQ(n, comp_iter);
2053 n = CHILD(n, 0);
2054 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002055 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002056 else if (TYPE(n) == comp_if) {
2057 if (NCH(n) == 3) {
2058 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002059 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002060 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002061 else
2062 return n_fors;
2063 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002064
Jelle Zijlstraac317702017-10-05 20:24:46 -07002065 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002066 /* Should never be reached */
2067 PyErr_SetString(PyExc_SystemError,
2068 "logic error in count_comp_fors");
2069 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070}
2071
Nick Coghlan650f0d02007-04-15 12:05:43 +00002072/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073
Nick Coghlan650f0d02007-04-15 12:05:43 +00002074 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075*/
2076
2077static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002078count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002080 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081
Guido van Rossumd8faa362007-04-27 19:54:29 +00002082 while (1) {
2083 REQ(n, comp_iter);
2084 if (TYPE(CHILD(n, 0)) == comp_for)
2085 return n_ifs;
2086 n = CHILD(n, 0);
2087 REQ(n, comp_if);
2088 n_ifs++;
2089 if (NCH(n) == 2)
2090 return n_ifs;
2091 n = CHILD(n, 2);
2092 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093}
2094
Guido van Rossum992d4a32007-07-11 13:09:30 +00002095static asdl_seq *
2096ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002099 asdl_seq *comps;
2100
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002101 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 if (n_fors == -1)
2103 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002105 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002106 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002110 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002112 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002113 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002114 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002115 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116
Guido van Rossum992d4a32007-07-11 13:09:30 +00002117 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118
Jelle Zijlstraac317702017-10-05 20:24:46 -07002119 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002120 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002121 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002122 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002123 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002124 else {
2125 sync_n = CHILD(n, 0);
2126 }
2127 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002128
Guido van Rossum495da292019-03-07 12:38:08 -08002129 /* Async comprehensions only allowed in Python 3.6 and greater */
2130 if (is_async && c->c_feature_version < 6) {
2131 ast_error(c, n,
2132 "Async comprehensions are only supported in Python 3.6 and greater");
2133 return NULL;
2134 }
2135
Jelle Zijlstraac317702017-10-05 20:24:46 -07002136 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002138 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002140 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002141 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002143
Thomas Wouters89f507f2006-12-13 04:49:30 +00002144 /* Check the # of children rather than the length of t, since
2145 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002146 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002147 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002148 comp = comprehension(first, expression, NULL,
2149 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002151 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2152 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2153 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002154 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002155 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002157
Jelle Zijlstraac317702017-10-05 20:24:46 -07002158 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 int j, n_ifs;
2160 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161
Jelle Zijlstraac317702017-10-05 20:24:46 -07002162 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002163 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002167 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002168 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002172 REQ(n, comp_iter);
2173 n = CHILD(n, 0);
2174 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175
Guido van Rossum992d4a32007-07-11 13:09:30 +00002176 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002177 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002178 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002179 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002180 if (NCH(n) == 3)
2181 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002183 /* on exit, must guarantee that n is a comp_for */
2184 if (TYPE(n) == comp_iter)
2185 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002186 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002188 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002190 return comps;
2191}
2192
2193static expr_ty
2194ast_for_itercomp(struct compiling *c, const node *n, int type)
2195{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002196 /* testlist_comp: (test|star_expr)
2197 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002198 expr_ty elt;
2199 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201
Guido van Rossum992d4a32007-07-11 13:09:30 +00002202 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002204 ch = CHILD(n, 0);
2205 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002206 if (!elt)
2207 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002208 if (elt->kind == Starred_kind) {
2209 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2210 return NULL;
2211 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212
Guido van Rossum992d4a32007-07-11 13:09:30 +00002213 comps = ast_for_comprehension(c, CHILD(n, 1));
2214 if (!comps)
2215 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002216
2217 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002218 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2219 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002220 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002221 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2222 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002223 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002224 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2225 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002226 else
2227 /* Should never happen */
2228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229}
2230
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002231/* Fills in the key, value pair corresponding to the dict element. In case
2232 * of an unpacking, key is NULL. *i is advanced by the number of ast
2233 * elements. Iff successful, nonzero is returned.
2234 */
2235static int
2236ast_for_dictelement(struct compiling *c, const node *n, int *i,
2237 expr_ty *key, expr_ty *value)
2238{
2239 expr_ty expression;
2240 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2241 assert(NCH(n) - *i >= 2);
2242
2243 expression = ast_for_expr(c, CHILD(n, *i + 1));
2244 if (!expression)
2245 return 0;
2246 *key = NULL;
2247 *value = expression;
2248
2249 *i += 2;
2250 }
2251 else {
2252 assert(NCH(n) - *i >= 3);
2253
2254 expression = ast_for_expr(c, CHILD(n, *i));
2255 if (!expression)
2256 return 0;
2257 *key = expression;
2258
2259 REQ(CHILD(n, *i + 1), COLON);
2260
2261 expression = ast_for_expr(c, CHILD(n, *i + 2));
2262 if (!expression)
2263 return 0;
2264 *value = expression;
2265
2266 *i += 3;
2267 }
2268 return 1;
2269}
2270
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002272ast_for_dictcomp(struct compiling *c, const node *n)
2273{
2274 expr_ty key, value;
2275 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002276 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002278 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002279 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002280 assert(key);
2281 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002283 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002284 if (!comps)
2285 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002287 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002289}
2290
2291static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002292ast_for_dictdisplay(struct compiling *c, const node *n)
2293{
2294 int i;
2295 int j;
2296 int size;
2297 asdl_seq *keys, *values;
2298
2299 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2300 keys = _Py_asdl_seq_new(size, c->c_arena);
2301 if (!keys)
2302 return NULL;
2303
2304 values = _Py_asdl_seq_new(size, c->c_arena);
2305 if (!values)
2306 return NULL;
2307
2308 j = 0;
2309 for (i = 0; i < NCH(n); i++) {
2310 expr_ty key, value;
2311
2312 if (!ast_for_dictelement(c, n, &i, &key, &value))
2313 return NULL;
2314 asdl_seq_SET(keys, j, key);
2315 asdl_seq_SET(values, j, value);
2316
2317 j++;
2318 }
2319 keys->size = j;
2320 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002321 return Dict(keys, values, LINENO(n), n->n_col_offset,
2322 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002323}
2324
2325static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002326ast_for_genexp(struct compiling *c, const node *n)
2327{
2328 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002329 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002330}
2331
2332static expr_ty
2333ast_for_listcomp(struct compiling *c, const node *n)
2334{
2335 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002336 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002337}
2338
2339static expr_ty
2340ast_for_setcomp(struct compiling *c, const node *n)
2341{
2342 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002343 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002344}
2345
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002346static expr_ty
2347ast_for_setdisplay(struct compiling *c, const node *n)
2348{
2349 int i;
2350 int size;
2351 asdl_seq *elts;
2352
2353 assert(TYPE(n) == (dictorsetmaker));
2354 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2355 elts = _Py_asdl_seq_new(size, c->c_arena);
2356 if (!elts)
2357 return NULL;
2358 for (i = 0; i < NCH(n); i += 2) {
2359 expr_ty expression;
2360 expression = ast_for_expr(c, CHILD(n, i));
2361 if (!expression)
2362 return NULL;
2363 asdl_seq_SET(elts, i / 2, expression);
2364 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002365 return Set(elts, LINENO(n), n->n_col_offset,
2366 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002367}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002368
2369static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370ast_for_atom(struct compiling *c, const node *n)
2371{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002372 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2373 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002374 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 */
2376 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002379 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002380 PyObject *name;
2381 const char *s = STR(ch);
2382 size_t len = strlen(s);
2383 if (len >= 4 && len <= 5) {
2384 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002385 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002386 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002387 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002388 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002389 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002390 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002391 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002392 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002393 }
2394 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002395 if (!name)
2396 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002397 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002398 return Name(name, Load, LINENO(n), n->n_col_offset,
2399 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002400 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002402 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002403 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002404 const char *errtype = NULL;
2405 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2406 errtype = "unicode error";
2407 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2408 errtype = "value error";
2409 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002410 PyObject *type, *value, *tback, *errstr;
2411 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002412 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002413 if (errstr) {
2414 ast_error(c, n, "(%s) %U", errtype, errstr);
2415 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002416 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002417 else {
2418 PyErr_Clear();
2419 ast_error(c, n, "(%s) unknown error", errtype);
2420 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002421 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002422 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002423 Py_XDECREF(tback);
2424 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002425 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002426 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002427 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 }
2429 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002430 PyObject *pynum;
2431 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2432 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2433 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2434 ast_error(c, ch,
2435 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2436 return NULL;
2437 }
2438 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002439 if (!pynum)
2440 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002441
Victor Stinner43d81952013-07-17 00:57:58 +02002442 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2443 Py_DECREF(pynum);
2444 return NULL;
2445 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002446 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002447 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 }
Georg Brandldde00282007-03-18 19:01:53 +00002449 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002450 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002451 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002453 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454
Thomas Wouters89f507f2006-12-13 04:49:30 +00002455 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002456 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2457 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458
Thomas Wouters89f507f2006-12-13 04:49:30 +00002459 if (TYPE(ch) == yield_expr)
2460 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002463 if (NCH(ch) == 1) {
2464 return ast_for_testlist(c, ch);
2465 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002466
Serhiy Storchakab619b092018-11-27 09:40:29 +02002467 if (TYPE(CHILD(ch, 1)) == comp_for) {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002468 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002469 }
2470 else {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002471 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002474 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475
Thomas Wouters89f507f2006-12-13 04:49:30 +00002476 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002477 return List(NULL, Load, LINENO(n), n->n_col_offset,
2478 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479
Nick Coghlan650f0d02007-04-15 12:05:43 +00002480 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002481 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2482 asdl_seq *elts = seq_for_testlist(c, ch);
2483 if (!elts)
2484 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002485
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002486 return List(elts, Load, LINENO(n), n->n_col_offset,
2487 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002488 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002489 else {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002490 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002493 /* dictorsetmaker: ( ((test ':' test | '**' test)
2494 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2495 * ((test | '*' test)
2496 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002497 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002498 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002499 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002500 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002501 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2502 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002503 }
2504 else {
2505 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2506 if (NCH(ch) == 1 ||
2507 (NCH(ch) > 1 &&
2508 TYPE(CHILD(ch, 1)) == COMMA)) {
2509 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002510 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002511 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002512 else if (NCH(ch) > 1 &&
2513 TYPE(CHILD(ch, 1)) == comp_for) {
2514 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002515 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002516 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002517 else if (NCH(ch) > 3 - is_dict &&
2518 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2519 /* It's a dictionary comprehension. */
2520 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002521 ast_error(c, n,
2522 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002523 return NULL;
2524 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002525 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 }
2527 else {
2528 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002529 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002530 }
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002531 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002532 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002535 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2536 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 }
2538}
2539
2540static slice_ty
2541ast_for_slice(struct compiling *c, const node *n)
2542{
2543 node *ch;
2544 expr_ty lower = NULL, upper = NULL, step = NULL;
2545
2546 REQ(n, subscript);
2547
2548 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002549 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 sliceop: ':' [test]
2551 */
2552 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 if (NCH(n) == 1 && TYPE(ch) == test) {
2554 /* 'step' variable hold no significance in terms of being used over
2555 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 if (!step)
2558 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559
Thomas Wouters89f507f2006-12-13 04:49:30 +00002560 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562
2563 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002564 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 if (!lower)
2566 return NULL;
2567 }
2568
2569 /* If there's an upper bound it's in the second or third position. */
2570 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002571 if (NCH(n) > 1) {
2572 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573
Thomas Wouters89f507f2006-12-13 04:49:30 +00002574 if (TYPE(n2) == test) {
2575 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (!upper)
2577 return NULL;
2578 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002579 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002581 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582
Thomas Wouters89f507f2006-12-13 04:49:30 +00002583 if (TYPE(n2) == test) {
2584 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 if (!upper)
2586 return NULL;
2587 }
2588 }
2589
2590 ch = CHILD(n, NCH(n) - 1);
2591 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002592 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 ch = CHILD(ch, 1);
2594 if (TYPE(ch) == test) {
2595 step = ast_for_expr(c, ch);
2596 if (!step)
2597 return NULL;
2598 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 }
2600 }
2601
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002602 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603}
2604
2605static expr_ty
2606ast_for_binop(struct compiling *c, const node *n)
2607{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002608 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002610 BinOp(BinOp(A, op, B), op, C).
2611 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
Guido van Rossumd8faa362007-04-27 19:54:29 +00002613 int i, nops;
2614 expr_ty expr1, expr2, result;
2615 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616
Guido van Rossumd8faa362007-04-27 19:54:29 +00002617 expr1 = ast_for_expr(c, CHILD(n, 0));
2618 if (!expr1)
2619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
Guido van Rossumd8faa362007-04-27 19:54:29 +00002621 expr2 = ast_for_expr(c, CHILD(n, 2));
2622 if (!expr2)
2623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624
Guido van Rossum495da292019-03-07 12:38:08 -08002625 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002626 if (!newoperator)
2627 return NULL;
2628
2629 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002630 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002631 c->c_arena);
2632 if (!result)
2633 return NULL;
2634
2635 nops = (NCH(n) - 1) / 2;
2636 for (i = 1; i < nops; i++) {
2637 expr_ty tmp_result, tmp;
2638 const node* next_oper = CHILD(n, i * 2 + 1);
2639
Guido van Rossum495da292019-03-07 12:38:08 -08002640 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002641 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 return NULL;
2643
Guido van Rossumd8faa362007-04-27 19:54:29 +00002644 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2645 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 return NULL;
2647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 tmp_result = BinOp(result, newoperator, tmp,
Miss Islington (bot)c7be35c2019-07-08 14:41:34 -07002649 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002650 CHILD(n, i * 2 + 2)->n_end_lineno,
2651 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002652 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002654 return NULL;
2655 result = tmp_result;
2656 }
2657 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658}
2659
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002660static expr_ty
2661ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002664 subscriptlist: subscript (',' subscript)* [',']
2665 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2666 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002667 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002668 REQ(n, trailer);
2669 if (TYPE(CHILD(n, 0)) == LPAR) {
2670 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002671 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2672 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002673 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002674 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002675 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002676 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002677 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2678 if (!attr_id)
2679 return NULL;
2680 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002681 LINENO(n), n->n_col_offset,
2682 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002683 }
2684 else {
2685 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002686 REQ(CHILD(n, 2), RSQB);
2687 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002688 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002689 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2690 if (!slc)
2691 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002692 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002693 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002694 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002695 }
2696 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002698 by treating the sequence as a tuple literal if there are
2699 no slice features.
2700 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002701 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002702 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002703 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002704 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002705 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002706 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002707 if (!slices)
2708 return NULL;
2709 for (j = 0; j < NCH(n); j += 2) {
2710 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002711 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002712 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002713 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002714 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002715 asdl_seq_SET(slices, j / 2, slc);
2716 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002717 if (!simple) {
2718 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002719 Load, LINENO(n), n->n_col_offset,
2720 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002721 }
2722 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002723 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002724 if (!elts)
2725 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002726 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2727 slc = (slice_ty)asdl_seq_GET(slices, j);
2728 assert(slc->kind == Index_kind && slc->v.Index.value);
2729 asdl_seq_SET(elts, j, slc->v.Index.value);
2730 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002731 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2732 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002733 if (!e)
2734 return NULL;
2735 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002736 Load, LINENO(n), n->n_col_offset,
2737 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002738 }
2739 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002740}
2741
2742static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002743ast_for_factor(struct compiling *c, const node *n)
2744{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002745 expr_ty expression;
2746
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002747 expression = ast_for_expr(c, CHILD(n, 1));
2748 if (!expression)
2749 return NULL;
2750
2751 switch (TYPE(CHILD(n, 0))) {
2752 case PLUS:
2753 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002754 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002755 c->c_arena);
2756 case MINUS:
2757 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002758 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002759 c->c_arena);
2760 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002761 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2762 n->n_end_lineno, n->n_end_col_offset,
2763 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002764 }
2765 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2766 TYPE(CHILD(n, 0)));
2767 return NULL;
2768}
2769
2770static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002771ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002772{
Yury Selivanov75445082015-05-11 22:57:16 -04002773 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002774 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002775
2776 REQ(n, atom_expr);
2777 nch = NCH(n);
2778
Guido van Rossum495da292019-03-07 12:38:08 -08002779 if (TYPE(CHILD(n, 0)) == AWAIT) {
2780 if (c->c_feature_version < 5) {
2781 ast_error(c, n,
2782 "Await expressions are only supported in Python 3.5 and greater");
2783 return NULL;
2784 }
Yury Selivanov75445082015-05-11 22:57:16 -04002785 start = 1;
2786 assert(nch > 1);
2787 }
2788
2789 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002790 if (!e)
2791 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002792 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002793 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002794 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002795 return Await(e, LINENO(n), n->n_col_offset,
2796 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002797 }
2798
2799 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002800 node *ch = CHILD(n, i);
2801 if (TYPE(ch) != trailer)
2802 break;
2803 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002804 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002805 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002806 tmp->lineno = e->lineno;
2807 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002808 e = tmp;
2809 }
Yury Selivanov75445082015-05-11 22:57:16 -04002810
2811 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002812 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002813 return Await(e, LINENO(n), n->n_col_offset,
2814 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002815 }
2816 else {
2817 return e;
2818 }
2819}
2820
2821static expr_ty
2822ast_for_power(struct compiling *c, const node *n)
2823{
2824 /* power: atom trailer* ('**' factor)*
2825 */
2826 expr_ty e;
2827 REQ(n, power);
2828 e = ast_for_atom_expr(c, CHILD(n, 0));
2829 if (!e)
2830 return NULL;
2831 if (NCH(n) == 1)
2832 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002833 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2834 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002835 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002836 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002837 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2838 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002839 }
2840 return e;
2841}
2842
Guido van Rossum0368b722007-05-11 16:50:42 +00002843static expr_ty
2844ast_for_starred(struct compiling *c, const node *n)
2845{
2846 expr_ty tmp;
2847 REQ(n, star_expr);
2848
2849 tmp = ast_for_expr(c, CHILD(n, 1));
2850 if (!tmp)
2851 return NULL;
2852
2853 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002854 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2855 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002856}
2857
2858
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859/* Do not name a variable 'expr'! Will cause a compile error.
2860*/
2861
2862static expr_ty
2863ast_for_expr(struct compiling *c, const node *n)
2864{
2865 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002866 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002867 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002868 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870 and_test: not_test ('and' not_test)*
2871 not_test: 'not' not_test | comparison
2872 comparison: expr (comp_op expr)*
2873 expr: xor_expr ('|' xor_expr)*
2874 xor_expr: and_expr ('^' and_expr)*
2875 and_expr: shift_expr ('&' shift_expr)*
2876 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2877 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002878 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002880 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002881 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002882 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 */
2884
2885 asdl_seq *seq;
2886 int i;
2887
2888 loop:
2889 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002890 case namedexpr_test:
2891 if (NCH(n) == 3)
2892 return ast_for_namedexpr(c, n);
2893 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002895 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002896 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002897 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002899 else if (NCH(n) > 1)
2900 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002901 /* Fallthrough */
2902 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 case and_test:
2904 if (NCH(n) == 1) {
2905 n = CHILD(n, 0);
2906 goto loop;
2907 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002908 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 if (!seq)
2910 return NULL;
2911 for (i = 0; i < NCH(n); i += 2) {
2912 expr_ty e = ast_for_expr(c, CHILD(n, i));
2913 if (!e)
2914 return NULL;
2915 asdl_seq_SET(seq, i / 2, e);
2916 }
2917 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002918 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002919 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002920 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002921 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002922 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2923 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 case not_test:
2925 if (NCH(n) == 1) {
2926 n = CHILD(n, 0);
2927 goto loop;
2928 }
2929 else {
2930 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2931 if (!expression)
2932 return NULL;
2933
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002934 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002935 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002936 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937 }
2938 case comparison:
2939 if (NCH(n) == 1) {
2940 n = CHILD(n, 0);
2941 goto loop;
2942 }
2943 else {
2944 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002946 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002947 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 if (!ops)
2949 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002950 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 return NULL;
2953 }
2954 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002955 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002957 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002958 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002960 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961
2962 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002963 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002965 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002967 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 asdl_seq_SET(cmps, i / 2, expression);
2969 }
2970 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002971 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002975 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2976 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978
Guido van Rossum0368b722007-05-11 16:50:42 +00002979 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981 /* The next five cases all handle BinOps. The main body of code
2982 is the same in each case, but the switch turned inside out to
2983 reuse the code for each type of operator.
2984 */
2985 case expr:
2986 case xor_expr:
2987 case and_expr:
2988 case shift_expr:
2989 case arith_expr:
2990 case term:
2991 if (NCH(n) == 1) {
2992 n = CHILD(n, 0);
2993 goto loop;
2994 }
2995 return ast_for_binop(c, n);
2996 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002997 node *an = NULL;
2998 node *en = NULL;
2999 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003000 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003001 if (NCH(n) > 1)
3002 an = CHILD(n, 1); /* yield_arg */
3003 if (an) {
3004 en = CHILD(an, NCH(an) - 1);
3005 if (NCH(an) == 2) {
3006 is_from = 1;
3007 exp = ast_for_expr(c, en);
3008 }
3009 else
3010 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003011 if (!exp)
3012 return NULL;
3013 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05003014 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003015 return YieldFrom(exp, LINENO(n), n->n_col_offset,
3016 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
3017 return Yield(exp, LINENO(n), n->n_col_offset,
3018 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003019 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003020 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 if (NCH(n) == 1) {
3022 n = CHILD(n, 0);
3023 goto loop;
3024 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003025 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00003026 case power:
3027 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003029 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 return NULL;
3031 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003032 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 return NULL;
3034}
3035
3036static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02003037ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003038 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039{
3040 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003041 arglist: argument (',' argument)* [',']
3042 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 */
3044
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003045 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003046 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003047 asdl_seq *args;
3048 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049
3050 REQ(n, arglist);
3051
3052 nargs = 0;
3053 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003055 node *ch = CHILD(n, i);
3056 if (TYPE(ch) == argument) {
3057 if (NCH(ch) == 1)
3058 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003059 else if (TYPE(CHILD(ch, 1)) == comp_for) {
3060 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02003061 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003062 ast_error(c, ch, "invalid syntax");
3063 return NULL;
3064 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003065 if (NCH(n) > 1) {
3066 ast_error(c, ch, "Generator expression must be parenthesized");
3067 return NULL;
3068 }
3069 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003070 else if (TYPE(CHILD(ch, 0)) == STAR)
3071 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003072 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3073 nargs++;
3074 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003076 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003077 nkeywords++;
3078 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003081 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003083 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003084 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003086 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003087
3088 nargs = 0; /* positional arguments + iterable argument unpackings */
3089 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3090 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003092 node *ch = CHILD(n, i);
3093 if (TYPE(ch) == argument) {
3094 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003095 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003096 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003097 /* a positional argument */
3098 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003099 if (ndoublestars) {
3100 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003101 "positional argument follows "
3102 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003103 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003104 else {
3105 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003106 "positional argument follows "
3107 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003108 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003109 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003110 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003111 e = ast_for_expr(c, chch);
3112 if (!e)
3113 return NULL;
3114 asdl_seq_SET(args, nargs++, e);
3115 }
3116 else if (TYPE(chch) == STAR) {
3117 /* an iterable argument unpacking */
3118 expr_ty starred;
3119 if (ndoublestars) {
3120 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003121 "iterable argument unpacking follows "
3122 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003123 return NULL;
3124 }
3125 e = ast_for_expr(c, CHILD(ch, 1));
3126 if (!e)
3127 return NULL;
3128 starred = Starred(e, Load, LINENO(chch),
3129 chch->n_col_offset,
Pablo Galindob1f20442019-12-18 01:41:58 +00003130 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003131 c->c_arena);
3132 if (!starred)
3133 return NULL;
3134 asdl_seq_SET(args, nargs++, starred);
3135
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003136 }
3137 else if (TYPE(chch) == DOUBLESTAR) {
3138 /* a keyword argument unpacking */
3139 keyword_ty kw;
3140 i++;
3141 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003143 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003144 kw = keyword(NULL, e, c->c_arena);
3145 asdl_seq_SET(keywords, nkeywords++, kw);
3146 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003148 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003149 /* the lone generator expression */
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08003150 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003152 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003153 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003155 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3156 /* treat colon equal as positional argument */
3157 if (nkeywords) {
3158 if (ndoublestars) {
3159 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003160 "positional argument follows "
3161 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003162 }
3163 else {
3164 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003165 "positional argument follows "
3166 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003167 }
3168 return NULL;
3169 }
3170 e = ast_for_namedexpr(c, ch);
3171 if (!e)
3172 return NULL;
3173 asdl_seq_SET(args, nargs++, e);
3174 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003175 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003176 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003177 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003178 identifier key, tmp;
3179 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003181 // To remain LL(1), the grammar accepts any test (basically, any
3182 // expression) in the keyword slot of a call site. So, we need
3183 // to manually enforce that the keyword is a NAME here.
3184 static const int name_tree[] = {
3185 test,
3186 or_test,
3187 and_test,
3188 not_test,
3189 comparison,
3190 expr,
3191 xor_expr,
3192 and_expr,
3193 shift_expr,
3194 arith_expr,
3195 term,
3196 factor,
3197 power,
3198 atom_expr,
3199 atom,
3200 0,
3201 };
3202 node *expr_node = chch;
3203 for (int i = 0; name_tree[i]; i++) {
3204 if (TYPE(expr_node) != name_tree[i])
3205 break;
3206 if (NCH(expr_node) != 1)
3207 break;
3208 expr_node = CHILD(expr_node, 0);
3209 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003210 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003211 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003212 "expression cannot contain assignment, "
3213 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003214 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003215 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003216 key = new_identifier(STR(expr_node), c);
3217 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003218 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003220 if (forbidden_name(c, key, chch, 1)) {
3221 return NULL;
3222 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003223 for (k = 0; k < nkeywords; k++) {
3224 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003225 if (tmp && !PyUnicode_Compare(tmp, key)) {
3226 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003227 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003228 return NULL;
3229 }
3230 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003231 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003233 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003234 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003236 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003237 asdl_seq_SET(keywords, nkeywords++, kw);
3238 }
3239 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 }
3241
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003242 return Call(func, args, keywords, func->lineno, func->col_offset,
3243 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244}
3245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003247ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003249 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003250 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003252 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003253 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003254 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003255 }
3256 else {
3257 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003258 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003261 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 else {
3263 asdl_seq *tmp = seq_for_testlist(c, n);
3264 if (!tmp)
3265 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003266 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3267 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003269}
3270
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271static stmt_ty
3272ast_for_expr_stmt(struct compiling *c, const node *n)
3273{
3274 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003275 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003276 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3277 annassign: ':' test ['=' (yield_expr|testlist)]
3278 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3279 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3280 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003281 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003283 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003285 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003286 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 if (!e)
3288 return NULL;
3289
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003290 return Expr(e, LINENO(n), n->n_col_offset,
3291 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 }
3293 else if (TYPE(CHILD(n, 1)) == augassign) {
3294 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003295 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 if (!expr1)
3300 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003301 if(!set_context(c, expr1, Store, ch))
3302 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003303 /* set_context checks that most expressions are not the left side.
3304 Augmented assignments can only have a name, a subscript, or an
3305 attribute on the left, though, so we have to explicitly check for
3306 those. */
3307 switch (expr1->kind) {
3308 case Name_kind:
3309 case Attribute_kind:
3310 case Subscript_kind:
3311 break;
3312 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003313 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003314 return NULL;
3315 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 ch = CHILD(n, 2);
3318 if (TYPE(ch) == testlist)
3319 expr2 = ast_for_testlist(c, ch);
3320 else
3321 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003322 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323 return NULL;
3324
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003325 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003326 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 return NULL;
3328
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003329 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3330 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003332 else if (TYPE(CHILD(n, 1)) == annassign) {
3333 expr_ty expr1, expr2, expr3;
3334 node *ch = CHILD(n, 0);
3335 node *deep, *ann = CHILD(n, 1);
3336 int simple = 1;
3337
Guido van Rossum495da292019-03-07 12:38:08 -08003338 /* AnnAssigns are only allowed in Python 3.6 or greater */
3339 if (c->c_feature_version < 6) {
3340 ast_error(c, ch,
3341 "Variable annotation syntax is only supported in Python 3.6 and greater");
3342 return NULL;
3343 }
3344
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003345 /* we keep track of parens to qualify (x) as expression not name */
3346 deep = ch;
3347 while (NCH(deep) == 1) {
3348 deep = CHILD(deep, 0);
3349 }
3350 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3351 simple = 0;
3352 }
3353 expr1 = ast_for_testlist(c, ch);
3354 if (!expr1) {
3355 return NULL;
3356 }
3357 switch (expr1->kind) {
3358 case Name_kind:
3359 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3360 return NULL;
3361 }
3362 expr1->v.Name.ctx = Store;
3363 break;
3364 case Attribute_kind:
3365 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3366 return NULL;
3367 }
3368 expr1->v.Attribute.ctx = Store;
3369 break;
3370 case Subscript_kind:
3371 expr1->v.Subscript.ctx = Store;
3372 break;
3373 case List_kind:
3374 ast_error(c, ch,
3375 "only single target (not list) can be annotated");
3376 return NULL;
3377 case Tuple_kind:
3378 ast_error(c, ch,
3379 "only single target (not tuple) can be annotated");
3380 return NULL;
3381 default:
3382 ast_error(c, ch,
3383 "illegal target for annotation");
3384 return NULL;
3385 }
3386
3387 if (expr1->kind != Name_kind) {
3388 simple = 0;
3389 }
3390 ch = CHILD(ann, 1);
3391 expr2 = ast_for_expr(c, ch);
3392 if (!expr2) {
3393 return NULL;
3394 }
3395 if (NCH(ann) == 2) {
3396 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003397 LINENO(n), n->n_col_offset,
3398 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003399 }
3400 else {
3401 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003402 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003403 expr3 = ast_for_testlist(c, ch);
3404 }
3405 else {
3406 expr3 = ast_for_expr(c, ch);
3407 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003408 if (!expr3) {
3409 return NULL;
3410 }
3411 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003412 LINENO(n), n->n_col_offset,
3413 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003414 }
3415 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003416 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003417 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003418 asdl_seq *targets;
3419 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003421 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422
Thomas Wouters89f507f2006-12-13 04:49:30 +00003423 /* a normal assignment */
3424 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003425
3426 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3427 nch_minus_type = num - has_type_comment;
3428
3429 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003430 if (!targets)
3431 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003432 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003433 expr_ty e;
3434 node *ch = CHILD(n, i);
3435 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003436 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003437 return NULL;
3438 }
3439 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003443 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003444 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446
Thomas Wouters89f507f2006-12-13 04:49:30 +00003447 asdl_seq_SET(targets, i / 2, e);
3448 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003449 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003450 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003451 expression = ast_for_testlist(c, value);
3452 else
3453 expression = ast_for_expr(c, value);
3454 if (!expression)
3455 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003456 if (has_type_comment) {
3457 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3458 if (!type_comment)
3459 return NULL;
3460 }
3461 else
3462 type_comment = NULL;
3463 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003464 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466}
3467
Benjamin Peterson78565b22009-06-28 19:19:51 +00003468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003470ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471{
3472 asdl_seq *seq;
3473 int i;
3474 expr_ty e;
3475
3476 REQ(n, exprlist);
3477
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003478 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003482 e = ast_for_expr(c, CHILD(n, i));
3483 if (!e)
3484 return NULL;
3485 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003486 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003487 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 }
3489 return seq;
3490}
3491
3492static stmt_ty
3493ast_for_del_stmt(struct compiling *c, const node *n)
3494{
3495 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 /* del_stmt: 'del' exprlist */
3498 REQ(n, del_stmt);
3499
3500 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3501 if (!expr_list)
3502 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003503 return Delete(expr_list, LINENO(n), n->n_col_offset,
3504 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505}
3506
3507static stmt_ty
3508ast_for_flow_stmt(struct compiling *c, const node *n)
3509{
3510 /*
3511 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3512 | yield_stmt
3513 break_stmt: 'break'
3514 continue_stmt: 'continue'
3515 return_stmt: 'return' [testlist]
3516 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003517 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518 raise_stmt: 'raise' [test [',' test [',' test]]]
3519 */
3520 node *ch;
3521
3522 REQ(n, flow_stmt);
3523 ch = CHILD(n, 0);
3524 switch (TYPE(ch)) {
3525 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003526 return Break(LINENO(n), n->n_col_offset,
3527 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003529 return Continue(LINENO(n), n->n_col_offset,
3530 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003532 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3533 if (!exp)
3534 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003535 return Expr(exp, LINENO(n), n->n_col_offset,
3536 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 }
3538 case return_stmt:
3539 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003540 return Return(NULL, LINENO(n), n->n_col_offset,
3541 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003543 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003544 if (!expression)
3545 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003546 return Return(expression, LINENO(n), n->n_col_offset,
3547 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 }
3549 case raise_stmt:
3550 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003551 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3552 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003553 else if (NCH(ch) >= 2) {
3554 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3556 if (!expression)
3557 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003558 if (NCH(ch) == 4) {
3559 cause = ast_for_expr(c, CHILD(ch, 3));
3560 if (!cause)
3561 return NULL;
3562 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003563 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3564 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 }
Stefan Krahf432a322017-08-21 13:09:59 +02003566 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003568 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 "unexpected flow_stmt: %d", TYPE(ch));
3570 return NULL;
3571 }
3572}
3573
3574static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003575alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576{
3577 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003578 import_as_name: NAME ['as' NAME]
3579 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 dotted_name: NAME ('.' NAME)*
3581 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003582 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 loop:
3585 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003586 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003587 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003588 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003589 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003590 if (!name)
3591 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003592 if (NCH(n) == 3) {
3593 node *str_node = CHILD(n, 2);
3594 str = NEW_IDENTIFIER(str_node);
3595 if (!str)
3596 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003597 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003598 return NULL;
3599 }
3600 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003601 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003602 return NULL;
3603 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003604 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 case dotted_as_name:
3607 if (NCH(n) == 1) {
3608 n = CHILD(n, 0);
3609 goto loop;
3610 }
3611 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003612 node *asname_node = CHILD(n, 2);
3613 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003614 if (!a)
3615 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003617 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003618 if (!a->asname)
3619 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003620 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 return a;
3623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003625 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003626 node *name_node = CHILD(n, 0);
3627 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003628 if (!name)
3629 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003630 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003631 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003632 return alias(name, NULL, c->c_arena);
3633 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 else {
3635 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003636 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003637 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640
3641 len = 0;
3642 for (i = 0; i < NCH(n); i += 2)
3643 /* length of string plus one for the dot */
3644 len += strlen(STR(CHILD(n, i))) + 1;
3645 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003646 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 if (!str)
3648 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003649 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 if (!s)
3651 return NULL;
3652 for (i = 0; i < NCH(n); i += 2) {
3653 char *sch = STR(CHILD(n, i));
3654 strcpy(s, STR(CHILD(n, i)));
3655 s += strlen(sch);
3656 *s++ = '.';
3657 }
3658 --s;
3659 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3661 PyBytes_GET_SIZE(str),
3662 NULL);
3663 Py_DECREF(str);
3664 if (!uni)
3665 return NULL;
3666 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003667 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003668 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3669 Py_DECREF(str);
3670 return NULL;
3671 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003672 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003675 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003676 if (!str)
3677 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003678 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3679 Py_DECREF(str);
3680 return NULL;
3681 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003682 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003684 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685 "unexpected import name: %d", TYPE(n));
3686 return NULL;
3687 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003688
3689 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 return NULL;
3691}
3692
3693static stmt_ty
3694ast_for_import_stmt(struct compiling *c, const node *n)
3695{
3696 /*
3697 import_stmt: import_name | import_from
3698 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003699 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3700 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003702 int lineno;
3703 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 int i;
3705 asdl_seq *aliases;
3706
3707 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003708 lineno = LINENO(n);
3709 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003711 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003713 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003714 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003715 if (!aliases)
3716 return NULL;
3717 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003718 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003719 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003721 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003723 // Even though n is modified above, the end position is not changed
3724 return Import(aliases, lineno, col_offset,
3725 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003727 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003729 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003730 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003731 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003732 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003734 /* Count the number of dots (for relative imports) and check for the
3735 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003736 for (idx = 1; idx < NCH(n); idx++) {
3737 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003738 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3739 if (!mod)
3740 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003741 idx++;
3742 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003743 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003745 ndots += 3;
3746 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003747 } else if (TYPE(CHILD(n, idx)) != DOT) {
3748 break;
3749 }
3750 ndots++;
3751 }
3752 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003753 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003754 case STAR:
3755 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003756 n = CHILD(n, idx);
3757 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003758 break;
3759 case LPAR:
3760 /* from ... import (x, y, z) */
3761 n = CHILD(n, idx + 1);
3762 n_children = NCH(n);
3763 break;
3764 case import_as_names:
3765 /* from ... import x, y, z */
3766 n = CHILD(n, idx);
3767 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003768 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003769 ast_error(c, n,
3770 "trailing comma not allowed without"
3771 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 return NULL;
3773 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003774 break;
3775 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003776 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003777 return NULL;
3778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003780 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003781 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783
3784 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003785 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003786 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003787 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003789 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003791 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003792 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003793 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003794 if (!import_alias)
3795 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003796 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003799 if (mod != NULL)
3800 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003801 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003802 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003803 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 }
Neal Norwitz79792652005-11-14 04:25:03 +00003805 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 "unknown import statement: starts with command '%s'",
3807 STR(CHILD(n, 0)));
3808 return NULL;
3809}
3810
3811static stmt_ty
3812ast_for_global_stmt(struct compiling *c, const node *n)
3813{
3814 /* global_stmt: 'global' NAME (',' NAME)* */
3815 identifier name;
3816 asdl_seq *s;
3817 int i;
3818
3819 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003820 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003822 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003824 name = NEW_IDENTIFIER(CHILD(n, i));
3825 if (!name)
3826 return NULL;
3827 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003829 return Global(s, LINENO(n), n->n_col_offset,
3830 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831}
3832
3833static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003834ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3835{
3836 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3837 identifier name;
3838 asdl_seq *s;
3839 int i;
3840
3841 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003842 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003843 if (!s)
3844 return NULL;
3845 for (i = 1; i < NCH(n); i += 2) {
3846 name = NEW_IDENTIFIER(CHILD(n, i));
3847 if (!name)
3848 return NULL;
3849 asdl_seq_SET(s, i / 2, name);
3850 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003851 return Nonlocal(s, LINENO(n), n->n_col_offset,
3852 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003853}
3854
3855static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856ast_for_assert_stmt(struct compiling *c, const node *n)
3857{
3858 /* assert_stmt: 'assert' test [',' test] */
3859 REQ(n, assert_stmt);
3860 if (NCH(n) == 2) {
3861 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3862 if (!expression)
3863 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003864 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3865 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 }
3867 else if (NCH(n) == 4) {
3868 expr_ty expr1, expr2;
3869
3870 expr1 = ast_for_expr(c, CHILD(n, 1));
3871 if (!expr1)
3872 return NULL;
3873 expr2 = ast_for_expr(c, CHILD(n, 3));
3874 if (!expr2)
3875 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003877 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3878 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 }
Neal Norwitz79792652005-11-14 04:25:03 +00003880 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 "improper number of parts to 'assert' statement: %d",
3882 NCH(n));
3883 return NULL;
3884}
3885
3886static asdl_seq *
3887ast_for_suite(struct compiling *c, const node *n)
3888{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003889 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003890 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 stmt_ty s;
3892 int i, total, num, end, pos = 0;
3893 node *ch;
3894
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003895 if (TYPE(n) != func_body_suite) {
3896 REQ(n, suite);
3897 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898
3899 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003900 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003902 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003904 n = CHILD(n, 0);
3905 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003907 */
3908 end = NCH(n) - 1;
3909 if (TYPE(CHILD(n, end - 1)) == SEMI)
3910 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003912 for (i = 0; i < end; i += 2) {
3913 ch = CHILD(n, i);
3914 s = ast_for_stmt(c, ch);
3915 if (!s)
3916 return NULL;
3917 asdl_seq_SET(seq, pos++, s);
3918 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 }
3920 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003921 i = 2;
3922 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3923 i += 2;
3924 REQ(CHILD(n, 2), NEWLINE);
3925 }
3926
3927 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 ch = CHILD(n, i);
3929 REQ(ch, stmt);
3930 num = num_stmts(ch);
3931 if (num == 1) {
3932 /* small_stmt or compound_stmt with only one child */
3933 s = ast_for_stmt(c, ch);
3934 if (!s)
3935 return NULL;
3936 asdl_seq_SET(seq, pos++, s);
3937 }
3938 else {
3939 int j;
3940 ch = CHILD(ch, 0);
3941 REQ(ch, simple_stmt);
3942 for (j = 0; j < NCH(ch); j += 2) {
3943 /* statement terminates with a semi-colon ';' */
3944 if (NCH(CHILD(ch, j)) == 0) {
3945 assert((j + 1) == NCH(ch));
3946 break;
3947 }
3948 s = ast_for_stmt(c, CHILD(ch, j));
3949 if (!s)
3950 return NULL;
3951 asdl_seq_SET(seq, pos++, s);
3952 }
3953 }
3954 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 }
3956 assert(pos == seq->size);
3957 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958}
3959
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003960static void
3961get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3962{
Pablo Galindo46a97922019-02-19 22:51:53 +00003963 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003964 // There must be no empty suites.
3965 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003966 stmt_ty last = asdl_seq_GET(s, tot - 1);
3967 *end_lineno = last->end_lineno;
3968 *end_col_offset = last->end_col_offset;
3969}
3970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971static stmt_ty
3972ast_for_if_stmt(struct compiling *c, const node *n)
3973{
3974 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3975 ['else' ':' suite]
3976 */
3977 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003978 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979
3980 REQ(n, if_stmt);
3981
3982 if (NCH(n) == 4) {
3983 expr_ty expression;
3984 asdl_seq *suite_seq;
3985
3986 expression = ast_for_expr(c, CHILD(n, 1));
3987 if (!expression)
3988 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003990 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003992 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993
Guido van Rossumd8faa362007-04-27 19:54:29 +00003994 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003995 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003997
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998 s = STR(CHILD(n, 4));
3999 /* s[2], the third character in the string, will be
4000 's' for el_s_e, or
4001 'i' for el_i_f
4002 */
4003 if (s[2] == 's') {
4004 expr_ty expression;
4005 asdl_seq *seq1, *seq2;
4006
4007 expression = ast_for_expr(c, CHILD(n, 1));
4008 if (!expression)
4009 return NULL;
4010 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004011 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 return NULL;
4013 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004014 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004016 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017
Guido van Rossumd8faa362007-04-27 19:54:29 +00004018 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004019 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 }
4021 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004022 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004023 expr_ty expression;
4024 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004025 asdl_seq *orelse = NULL;
4026 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 /* must reference the child n_elif+1 since 'else' token is third,
4028 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004029 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
4030 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
4031 has_else = 1;
4032 n_elif -= 3;
4033 }
4034 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035
Thomas Wouters89f507f2006-12-13 04:49:30 +00004036 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004037 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004039 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004040 if (!orelse)
4041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004043 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004045 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
4046 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004048 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4049 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004051 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 asdl_seq_SET(orelse, 0,
4054 If(expression, suite_seq, suite_seq2,
Miss Islington (bot)ce333cd2019-12-14 02:43:42 -08004055 LINENO(CHILD(n, NCH(n) - 7)),
4056 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004057 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004058 /* the just-created orelse handled the last elif */
4059 n_elif--;
4060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061
Thomas Wouters89f507f2006-12-13 04:49:30 +00004062 for (i = 0; i < n_elif; i++) {
4063 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004064 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004065 if (!newobj)
4066 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004068 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004071 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004074 if (orelse != NULL) {
4075 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4076 } else {
4077 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4078 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004079 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 If(expression, suite_seq, orelse,
Miss Islington (bot)3b18b172019-12-13 08:21:54 -08004081 LINENO(CHILD(n, off - 1)),
4082 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004083 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004084 orelse = newobj;
4085 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 expression = ast_for_expr(c, CHILD(n, 1));
4087 if (!expression)
4088 return NULL;
4089 suite_seq = ast_for_suite(c, CHILD(n, 3));
4090 if (!suite_seq)
4091 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004092 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004093 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004094 LINENO(n), n->n_col_offset,
4095 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004097
4098 PyErr_Format(PyExc_SystemError,
4099 "unexpected token in 'if' statement: %s", s);
4100 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004101}
4102
4103static stmt_ty
4104ast_for_while_stmt(struct compiling *c, const node *n)
4105{
4106 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4107 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004108 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109
4110 if (NCH(n) == 4) {
4111 expr_ty expression;
4112 asdl_seq *suite_seq;
4113
4114 expression = ast_for_expr(c, CHILD(n, 1));
4115 if (!expression)
4116 return NULL;
4117 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004118 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004120 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4121 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4122 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 }
4124 else if (NCH(n) == 7) {
4125 expr_ty expression;
4126 asdl_seq *seq1, *seq2;
4127
4128 expression = ast_for_expr(c, CHILD(n, 1));
4129 if (!expression)
4130 return NULL;
4131 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004132 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 return NULL;
4134 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004135 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004137 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004139 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4140 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004142
4143 PyErr_Format(PyExc_SystemError,
4144 "wrong number of tokens for 'while' statement: %d",
4145 NCH(n));
4146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004147}
4148
4149static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004150ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151{
guoci90fc8982018-09-11 17:45:45 -04004152 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004153 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004155 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004156 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004157 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004158 int has_type_comment;
4159 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004160
4161 if (is_async && c->c_feature_version < 5) {
4162 ast_error(c, n,
4163 "Async for loops are only supported in Python 3.5 and greater");
4164 return NULL;
4165 }
4166
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004167 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004168 REQ(n, for_stmt);
4169
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004170 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4171
4172 if (NCH(n) == 9 + has_type_comment) {
4173 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004174 if (!seq)
4175 return NULL;
4176 }
4177
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004178 node_target = CHILD(n, 1);
4179 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004180 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004182 /* Check the # of children rather than the length of _target, since
4183 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004184 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004185 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004186 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004187 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004188 target = Tuple(_target, Store, first->lineno, first->col_offset,
4189 node_target->n_end_lineno, node_target->n_end_col_offset,
4190 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004192 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004193 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004194 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004195 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004196 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004197 return NULL;
4198
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004199 if (seq != NULL) {
4200 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4201 } else {
4202 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4203 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004204
4205 if (has_type_comment) {
4206 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4207 if (!type_comment)
4208 return NULL;
4209 }
4210 else
4211 type_comment = NULL;
4212
Yury Selivanov75445082015-05-11 22:57:16 -04004213 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004214 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004215 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004216 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004217 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004218 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004219 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004220 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004221}
4222
4223static excepthandler_ty
4224ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4225{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004226 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004227 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004228 REQ(exc, except_clause);
4229 REQ(body, suite);
4230
4231 if (NCH(exc) == 1) {
4232 asdl_seq *suite_seq = ast_for_suite(c, body);
4233 if (!suite_seq)
4234 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004235 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004236
Neal Norwitzad74aa82008-03-31 05:14:30 +00004237 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004238 exc->n_col_offset,
4239 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004240 }
4241 else if (NCH(exc) == 2) {
4242 expr_ty expression;
4243 asdl_seq *suite_seq;
4244
4245 expression = ast_for_expr(c, CHILD(exc, 1));
4246 if (!expression)
4247 return NULL;
4248 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004249 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004250 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004251 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004252
Neal Norwitzad74aa82008-03-31 05:14:30 +00004253 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004254 exc->n_col_offset,
4255 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004256 }
4257 else if (NCH(exc) == 4) {
4258 asdl_seq *suite_seq;
4259 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004260 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004261 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004262 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004263 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004264 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004265 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004266 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004267 return NULL;
4268 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004269 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004270 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004271 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004272
Neal Norwitzad74aa82008-03-31 05:14:30 +00004273 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004274 exc->n_col_offset,
4275 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004276 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004277
4278 PyErr_Format(PyExc_SystemError,
4279 "wrong number of children for 'except' clause: %d",
4280 NCH(exc));
4281 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004282}
4283
4284static stmt_ty
4285ast_for_try_stmt(struct compiling *c, const node *n)
4286{
Neal Norwitzf599f422005-12-17 21:33:47 +00004287 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004288 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004289 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004290 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004291
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004292 REQ(n, try_stmt);
4293
Neal Norwitzf599f422005-12-17 21:33:47 +00004294 body = ast_for_suite(c, CHILD(n, 2));
4295 if (body == NULL)
4296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004297
Neal Norwitzf599f422005-12-17 21:33:47 +00004298 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4299 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4300 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4301 /* we can assume it's an "else",
4302 because nch >= 9 for try-else-finally and
4303 it would otherwise have a type of except_clause */
4304 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4305 if (orelse == NULL)
4306 return NULL;
4307 n_except--;
4308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004309
Neal Norwitzf599f422005-12-17 21:33:47 +00004310 finally = ast_for_suite(c, CHILD(n, nch - 1));
4311 if (finally == NULL)
4312 return NULL;
4313 n_except--;
4314 }
4315 else {
4316 /* we can assume it's an "else",
4317 otherwise it would have a type of except_clause */
4318 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4319 if (orelse == NULL)
4320 return NULL;
4321 n_except--;
4322 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004323 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004324 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004325 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004326 return NULL;
4327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328
Neal Norwitzf599f422005-12-17 21:33:47 +00004329 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004330 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004331 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004332 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004333 if (handlers == NULL)
4334 return NULL;
4335
4336 for (i = 0; i < n_except; i++) {
4337 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4338 CHILD(n, 5 + i * 3));
4339 if (!e)
4340 return NULL;
4341 asdl_seq_SET(handlers, i, e);
4342 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004343 }
4344
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004345 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004346 if (finally != NULL) {
4347 // finally is always last
4348 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4349 } else if (orelse != NULL) {
4350 // otherwise else is last
4351 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4352 } else {
4353 // inline the get_last_end_pos logic due to layout mismatch
4354 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4355 end_lineno = last_handler->end_lineno;
4356 end_col_offset = last_handler->end_col_offset;
4357 }
4358 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4359 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004360}
4361
Georg Brandl0c315622009-05-25 21:10:36 +00004362/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004363static withitem_ty
4364ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004365{
4366 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004367
Georg Brandl0c315622009-05-25 21:10:36 +00004368 REQ(n, with_item);
4369 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004370 if (!context_expr)
4371 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004372 if (NCH(n) == 3) {
4373 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004374
4375 if (!optional_vars) {
4376 return NULL;
4377 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004378 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004379 return NULL;
4380 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004381 }
4382
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004383 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004384}
4385
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004386/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004387static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004388ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004389{
guoci90fc8982018-09-11 17:45:45 -04004390 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004391 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004392 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004393 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004394
Guido van Rossum495da292019-03-07 12:38:08 -08004395 if (is_async && c->c_feature_version < 5) {
4396 ast_error(c, n,
4397 "Async with statements are only supported in Python 3.5 and greater");
4398 return NULL;
4399 }
4400
Georg Brandl0c315622009-05-25 21:10:36 +00004401 REQ(n, with_stmt);
4402
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004403 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4404 nch_minus_type = NCH(n) - has_type_comment;
4405
4406 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004407 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004408 if (!items)
4409 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004410 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004411 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4412 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004413 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004414 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004415 }
4416
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004417 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4418 if (!body)
4419 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004420 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004421
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004422 if (has_type_comment) {
4423 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4424 if (!type_comment)
4425 return NULL;
4426 }
4427 else
4428 type_comment = NULL;
4429
Yury Selivanov75445082015-05-11 22:57:16 -04004430 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004431 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004432 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004433 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004434 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004435 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004436}
4437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004438static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004439ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004440{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004441 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004442 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004443 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004444 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004445 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447 REQ(n, classdef);
4448
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004449 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004450 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451 if (!s)
4452 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004453 get_last_end_pos(s, &end_lineno, &end_col_offset);
4454
Benjamin Peterson30760062008-11-25 04:02:28 +00004455 classname = NEW_IDENTIFIER(CHILD(n, 1));
4456 if (!classname)
4457 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004458 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004459 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004460 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004461 LINENO(n), n->n_col_offset,
4462 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004463 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004464
4465 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004466 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004467 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004468 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004469 get_last_end_pos(s, &end_lineno, &end_col_offset);
4470
Benjamin Peterson30760062008-11-25 04:02:28 +00004471 classname = NEW_IDENTIFIER(CHILD(n, 1));
4472 if (!classname)
4473 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004474 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004475 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004476 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004477 LINENO(n), n->n_col_offset,
4478 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004479 }
4480
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004481 /* class NAME '(' arglist ')' ':' suite */
4482 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004483 {
4484 PyObject *dummy_name;
4485 expr_ty dummy;
4486 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4487 if (!dummy_name)
4488 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004489 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4490 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4491 c->c_arena);
4492 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004493 if (!call)
4494 return NULL;
4495 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004496 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004497 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004498 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004499 get_last_end_pos(s, &end_lineno, &end_col_offset);
4500
Benjamin Peterson30760062008-11-25 04:02:28 +00004501 classname = NEW_IDENTIFIER(CHILD(n, 1));
4502 if (!classname)
4503 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004504 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004505 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004506
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004507 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004508 decorator_seq, LINENO(n), n->n_col_offset,
4509 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004510}
4511
4512static stmt_ty
4513ast_for_stmt(struct compiling *c, const node *n)
4514{
4515 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004516 assert(NCH(n) == 1);
4517 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004518 }
4519 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004520 assert(num_stmts(n) == 1);
4521 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004522 }
4523 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004524 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004525 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4526 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004527 */
4528 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004529 case expr_stmt:
4530 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531 case del_stmt:
4532 return ast_for_del_stmt(c, n);
4533 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004534 return Pass(LINENO(n), n->n_col_offset,
4535 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004536 case flow_stmt:
4537 return ast_for_flow_stmt(c, n);
4538 case import_stmt:
4539 return ast_for_import_stmt(c, n);
4540 case global_stmt:
4541 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004542 case nonlocal_stmt:
4543 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004544 case assert_stmt:
4545 return ast_for_assert_stmt(c, n);
4546 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004547 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004548 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4549 TYPE(n), NCH(n));
4550 return NULL;
4551 }
4552 }
4553 else {
4554 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004555 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004556 */
4557 node *ch = CHILD(n, 0);
4558 REQ(n, compound_stmt);
4559 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004560 case if_stmt:
4561 return ast_for_if_stmt(c, ch);
4562 case while_stmt:
4563 return ast_for_while_stmt(c, ch);
4564 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004565 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004566 case try_stmt:
4567 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004568 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004569 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004570 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004571 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004572 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004573 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 case decorated:
4575 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004576 case async_stmt:
4577 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004578 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004579 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004580 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004581 TYPE(n), NCH(n));
4582 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004583 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004584 }
4585}
4586
4587static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004588parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004589{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004590 const char *end;
4591 long x;
4592 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004593 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004594 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004596 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004597 errno = 0;
4598 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004599 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004600 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004601 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004602 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004603 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004604 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004605 }
4606 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004607 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004608 if (*end == '\0') {
4609 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004610 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004611 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004612 }
4613 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004614 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004615 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004616 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4617 if (compl.imag == -1.0 && PyErr_Occurred())
4618 return NULL;
4619 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004620 }
4621 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004622 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004623 dx = PyOS_string_to_double(s, NULL, NULL);
4624 if (dx == -1.0 && PyErr_Occurred())
4625 return NULL;
4626 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004628}
4629
4630static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004631parsenumber(struct compiling *c, const char *s)
4632{
4633 char *dup, *end;
4634 PyObject *res = NULL;
4635
4636 assert(s != NULL);
4637
4638 if (strchr(s, '_') == NULL) {
4639 return parsenumber_raw(c, s);
4640 }
4641 /* Create a duplicate without underscores. */
4642 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004643 if (dup == NULL) {
4644 return PyErr_NoMemory();
4645 }
Brett Cannona721aba2016-09-09 14:57:09 -07004646 end = dup;
4647 for (; *s; s++) {
4648 if (*s != '_') {
4649 *end++ = *s;
4650 }
4651 }
4652 *end = '\0';
4653 res = parsenumber_raw(c, dup);
4654 PyMem_Free(dup);
4655 return res;
4656}
4657
4658static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004659decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004660{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004661 const char *s, *t;
4662 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004663 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4664 while (s < end && (*s & 0x80)) s++;
4665 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004666 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004667}
4668
Eric V. Smith56466482016-10-31 14:46:26 -04004669static int
4670warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004671 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004672{
4673 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4674 first_invalid_escape_char);
4675 if (msg == NULL) {
4676 return -1;
4677 }
Serhiy Storchaka4c5b6ba2019-08-10 01:34:22 +03004678 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004679 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004680 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004681 {
Serhiy Storchaka4c5b6ba2019-08-10 01:34:22 +03004682 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4683 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004684 to get a more accurate error report */
4685 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004686 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004687 }
4688 Py_DECREF(msg);
4689 return -1;
4690 }
4691 Py_DECREF(msg);
4692 return 0;
4693}
4694
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004695static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004696decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4697 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004698{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004699 PyObject *v, *u;
4700 char *buf;
4701 char *p;
4702 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004703
Benjamin Peterson202803a2016-02-25 22:34:45 -08004704 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004705 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004706 return NULL;
4707 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4708 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4709 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4710 if (u == NULL)
4711 return NULL;
4712 p = buf = PyBytes_AsString(u);
4713 end = s + len;
4714 while (s < end) {
4715 if (*s == '\\') {
4716 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004717 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004718 strcpy(p, "u005c");
4719 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004720 if (s >= end)
4721 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004722 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004723 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004724 if (*s & 0x80) { /* XXX inefficient */
4725 PyObject *w;
4726 int kind;
4727 void *data;
4728 Py_ssize_t len, i;
4729 w = decode_utf8(c, &s, end);
4730 if (w == NULL) {
4731 Py_DECREF(u);
4732 return NULL;
4733 }
4734 kind = PyUnicode_KIND(w);
4735 data = PyUnicode_DATA(w);
4736 len = PyUnicode_GET_LENGTH(w);
4737 for (i = 0; i < len; i++) {
4738 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4739 sprintf(p, "\\U%08x", chr);
4740 p += 10;
4741 }
4742 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004743 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004744 Py_DECREF(w);
4745 } else {
4746 *p++ = *s++;
4747 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004748 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004749 len = p - buf;
4750 s = buf;
4751
Eric V. Smith56466482016-10-31 14:46:26 -04004752 const char *first_invalid_escape;
4753 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4754
4755 if (v != NULL && first_invalid_escape != NULL) {
4756 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4757 /* We have not decref u before because first_invalid_escape points
4758 inside u. */
4759 Py_XDECREF(u);
4760 Py_DECREF(v);
4761 return NULL;
4762 }
4763 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004764 Py_XDECREF(u);
4765 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004766}
4767
Eric V. Smith56466482016-10-31 14:46:26 -04004768static PyObject *
4769decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4770 size_t len)
4771{
4772 const char *first_invalid_escape;
4773 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4774 &first_invalid_escape);
4775 if (result == NULL)
4776 return NULL;
4777
4778 if (first_invalid_escape != NULL) {
4779 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4780 Py_DECREF(result);
4781 return NULL;
4782 }
4783 }
4784 return result;
4785}
4786
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004787/* Shift locations for the given node and all its children by adding `lineno`
4788 and `col_offset` to existing locations. */
4789static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4790{
4791 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004792 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004793 for (int i = 0; i < NCH(n); ++i) {
4794 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4795 /* Shifting column offsets unnecessary if there's been newlines. */
4796 col_offset = 0;
4797 }
4798 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4799 }
4800 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004801 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004802}
4803
4804/* Fix locations for the given node and its children.
4805
4806 `parent` is the enclosing node.
4807 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004808 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004809*/
4810static void
4811fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4812{
4813 char *substr = NULL;
4814 char *start;
4815 int lines = LINENO(parent) - 1;
4816 int cols = parent->n_col_offset;
4817 /* Find the full fstring to fix location information in `n`. */
4818 while (parent && parent->n_type != STRING)
4819 parent = parent->n_child;
4820 if (parent && parent->n_str) {
4821 substr = strstr(parent->n_str, expr_str);
4822 if (substr) {
4823 start = substr;
4824 while (start > parent->n_str) {
4825 if (start[0] == '\n')
4826 break;
4827 start--;
4828 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004829 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004830 /* adjust the start based on the number of newlines encountered
4831 before the f-string expression */
4832 for (char* p = parent->n_str; p < substr; p++) {
4833 if (*p == '\n') {
4834 lines++;
4835 }
4836 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004837 }
4838 }
4839 fstring_shift_node_locations(n, lines, cols);
4840}
4841
Eric V. Smith451d0e32016-09-09 21:56:20 -04004842/* Compile this expression in to an expr_ty. Add parens around the
4843 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004844static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004845fstring_compile_expr(const char *expr_start, const char *expr_end,
4846 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004847
Eric V. Smith235a6f02015-09-19 14:51:32 -04004848{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004849 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004851 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004852 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004853 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004854
Eric V. Smith1d44c412015-09-23 07:49:00 -04004855 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004856 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004857 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4858 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004859
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004860 /* If the substring is all whitespace, it's an error. We need to catch this
4861 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4862 because turning the expression '' in to '()' would go from being invalid
4863 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004864 for (s = expr_start; s != expr_end; s++) {
4865 char c = *s;
4866 /* The Python parser ignores only the following whitespace
4867 characters (\r already is converted to \n). */
4868 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004869 break;
4870 }
4871 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004872 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004873 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004874 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004875 }
4876
Eric V. Smith451d0e32016-09-09 21:56:20 -04004877 len = expr_end - expr_start;
4878 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4879 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004880 if (str == NULL) {
4881 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004882 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004883 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004884
Eric V. Smith451d0e32016-09-09 21:56:20 -04004885 str[0] = '(';
4886 memcpy(str+1, expr_start, len);
4887 str[len+1] = ')';
4888 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004889
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07004890 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004891 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004892 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4893 Py_eval_input, 0);
4894 if (!mod_n) {
4895 PyMem_RawFree(str);
4896 return NULL;
4897 }
4898 /* Reuse str to find the correct column offset. */
4899 str[0] = '{';
4900 str[len+1] = '}';
4901 fstring_fix_node_location(n, mod_n, str);
4902 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004903 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004904 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004906 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004907 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908}
4909
4910/* Return -1 on error.
4911
4912 Return 0 if we reached the end of the literal.
4913
4914 Return 1 if we haven't reached the end of the literal, but we want
4915 the caller to process the literal up to this point. Used for
4916 doubled braces.
4917*/
4918static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004919fstring_find_literal(const char **str, const char *end, int raw,
4920 PyObject **literal, int recurse_lvl,
4921 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004922{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004923 /* Get any literal string. It ends when we hit an un-doubled left
4924 brace (which isn't part of a unicode name escape such as
4925 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004927 const char *s = *str;
4928 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 int result = 0;
4930
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004932 while (s < end) {
4933 char ch = *s++;
4934 if (!raw && ch == '\\' && s < end) {
4935 ch = *s++;
4936 if (ch == 'N') {
4937 if (s < end && *s++ == '{') {
4938 while (s < end && *s++ != '}') {
4939 }
4940 continue;
4941 }
4942 break;
4943 }
4944 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4945 return -1;
4946 }
4947 }
4948 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004949 /* Check for doubled braces, but only at the top level. If
4950 we checked at every level, then f'{0:{3}}' would fail
4951 with the two closing braces. */
4952 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004953 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954 /* We're going to tell the caller that the literal ends
4955 here, but that they should continue scanning. But also
4956 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004957 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004958 result = 1;
4959 goto done;
4960 }
4961
4962 /* Where a single '{' is the start of a new expression, a
4963 single '}' is not allowed. */
4964 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004965 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 ast_error(c, n, "f-string: single '}' is not allowed");
4967 return -1;
4968 }
4969 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004970 /* We're either at a '{', which means we're starting another
4971 expression; or a '}', which means we're at the end of this
4972 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004973 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004974 break;
4975 }
4976 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004977 *str = s;
4978 assert(s <= end);
4979 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004981 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004982 if (raw)
4983 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004984 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004985 NULL, NULL);
4986 else
Eric V. Smith56466482016-10-31 14:46:26 -04004987 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004988 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004989 if (!*literal)
4990 return -1;
4991 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004992 return result;
4993}
4994
4995/* Forward declaration because parsing is recursive. */
4996static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004997fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004998 struct compiling *c, const node *n);
4999
Eric V. Smith451d0e32016-09-09 21:56:20 -04005000/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005001 expression (so it must be a '{'). Returns the FormattedValue node, which
5002 includes the expression, conversion character, format_spec expression, and
5003 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04005004
5005 Note that I don't do a perfect job here: I don't make sure that a
5006 closing brace doesn't match an opening paren, for example. It
5007 doesn't need to error on all invalid expressions, just correctly
5008 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005009 will be caught when we parse it later.
5010
5011 *expression is set to the expression. For an '=' "debug" expression,
5012 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005013 including the '=' and any whitespace around it, as a string object). If
5014 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005015static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005016fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005017 PyObject **expr_text, expr_ty *expression,
5018 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019{
5020 /* Return -1 on error, else 0. */
5021
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022 const char *expr_start;
5023 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 expr_ty simple_expression;
5025 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005026 int conversion = -1; /* The conversion char. Use default if not
5027 specified, or !r if using = and no format
5028 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005029
5030 /* 0 if we're not in a string, else the quote char we're trying to
5031 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005032 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005033
5034 /* If we're inside a string, 1=normal, 3=triple-quoted. */
5035 int string_type = 0;
5036
5037 /* Keep track of nesting level for braces/parens/brackets in
5038 expressions. */
5039 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005040 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04005041
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005042 *expr_text = NULL;
5043
Eric V. Smith235a6f02015-09-19 14:51:32 -04005044 /* Can only nest one level deep. */
5045 if (recurse_lvl >= 2) {
5046 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005047 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005048 }
5049
5050 /* The first char must be a left brace, or we wouldn't have gotten
5051 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005052 assert(**str == '{');
5053 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005054
Eric V. Smith451d0e32016-09-09 21:56:20 -04005055 expr_start = *str;
5056 for (; *str < end; (*str)++) {
5057 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058
5059 /* Loop invariants. */
5060 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 if (quote_char)
5063 assert(string_type == 1 || string_type == 3);
5064 else
5065 assert(string_type == 0);
5066
Eric V. Smith451d0e32016-09-09 21:56:20 -04005067 ch = **str;
5068 /* Nowhere inside an expression is a backslash allowed. */
5069 if (ch == '\\') {
5070 /* Error: can't include a backslash character, inside
5071 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005072 ast_error(c, n,
5073 "f-string expression part "
5074 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005075 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005076 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005077 if (quote_char) {
5078 /* We're inside a string. See if we're at the end. */
5079 /* This code needs to implement the same non-error logic
5080 as tok_get from tokenizer.c, at the letter_quote
5081 label. To actually share that code would be a
5082 nightmare. But, it's unlikely to change and is small,
5083 so duplicate it here. Note we don't need to catch all
5084 of the errors, since they'll be caught when parsing the
5085 expression. We just need to match the non-error
5086 cases. Thus we can ignore \n in single-quoted strings,
5087 for example. Or non-terminated strings. */
5088 if (ch == quote_char) {
5089 /* Does this match the string_type (single or triple
5090 quoted)? */
5091 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005093 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005094 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095 string_type = 0;
5096 quote_char = 0;
5097 continue;
5098 }
5099 } else {
5100 /* We're at the end of a normal string. */
5101 quote_char = 0;
5102 string_type = 0;
5103 continue;
5104 }
5105 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106 } else if (ch == '\'' || ch == '"') {
5107 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005111 } else {
5112 /* Start of a normal string. */
5113 string_type = 1;
5114 }
5115 /* Start looking for the end of the string. */
5116 quote_char = ch;
5117 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005118 if (nested_depth >= MAXLEVEL) {
5119 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005120 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005121 }
5122 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 } else if (ch == '#') {
5125 /* Error: can't include a comment character, inside parens
5126 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005127 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005128 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005129 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005130 (ch == '!' || ch == ':' || ch == '}' ||
5131 ch == '=' || ch == '>' || ch == '<')) {
5132 /* See if there's a next character. */
5133 if (*str+1 < end) {
5134 char next = *(*str+1);
5135
5136 /* For "!=". since '=' is not an allowed conversion character,
5137 nothing is lost in this test. */
5138 if ((ch == '!' && next == '=') || /* != */
5139 (ch == '=' && next == '=') || /* == */
5140 (ch == '<' && next == '=') || /* <= */
5141 (ch == '>' && next == '=') /* >= */
5142 ) {
5143 *str += 1;
5144 continue;
5145 }
5146 /* Don't get out of the loop for these, if they're single
5147 chars (not part of 2-char tokens). If by themselves, they
5148 don't end an expression (unlike say '!'). */
5149 if (ch == '>' || ch == '<') {
5150 continue;
5151 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005152 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005153
Eric V. Smith235a6f02015-09-19 14:51:32 -04005154 /* Normal way out of this loop. */
5155 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005156 } else if (ch == ']' || ch == '}' || ch == ')') {
5157 if (!nested_depth) {
5158 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005159 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005160 }
5161 nested_depth--;
5162 int opening = parenstack[nested_depth];
5163 if (!((opening == '(' && ch == ')') ||
5164 (opening == '[' && ch == ']') ||
5165 (opening == '{' && ch == '}')))
5166 {
5167 ast_error(c, n,
5168 "f-string: closing parenthesis '%c' "
5169 "does not match opening parenthesis '%c'",
5170 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005171 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005172 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005173 } else {
5174 /* Just consume this char and loop around. */
5175 }
5176 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005177 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005178 /* If we leave this loop in a string or with mismatched parens, we
5179 don't care. We'll get a syntax error when compiling the
5180 expression. But, we can produce a better error message, so
5181 let's just do that.*/
5182 if (quote_char) {
5183 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005184 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 }
5186 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005187 int opening = parenstack[nested_depth - 1];
5188 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005189 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005190 }
5191
Eric V. Smith451d0e32016-09-09 21:56:20 -04005192 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005193 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005194
5195 /* Compile the expression as soon as possible, so we show errors
5196 related to the expression before errors related to the
5197 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005198 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005199 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005200 goto error;
5201
5202 /* Check for =, which puts the text value of the expression in
5203 expr_text. */
5204 if (**str == '=') {
5205 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005206
5207 /* Skip over ASCII whitespace. No need to test for end of string
5208 here, since we know there's at least a trailing quote somewhere
5209 ahead. */
5210 while (Py_ISSPACE(**str)) {
5211 *str += 1;
5212 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005213
5214 /* Set *expr_text to the text of the expression. */
5215 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5216 if (!*expr_text) {
5217 goto error;
5218 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005219 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005220
5221 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005222 if (**str == '!') {
5223 *str += 1;
5224 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005225 goto unexpected_end_of_string;
5226
Eric V. Smith451d0e32016-09-09 21:56:20 -04005227 conversion = **str;
5228 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229
5230 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005231 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005232 ast_error(c, n,
5233 "f-string: invalid conversion character: "
5234 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005235 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005236 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005237
5238 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005239
5240 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005241 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005242 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005243 if (**str == ':') {
5244 *str += 1;
5245 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005246 goto unexpected_end_of_string;
5247
5248 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005249 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005250 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005251 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005252 }
5253
Eric V. Smith451d0e32016-09-09 21:56:20 -04005254 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005255 goto unexpected_end_of_string;
5256
5257 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005258 assert(*str < end);
5259 assert(**str == '}');
5260 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005261
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005262 /* If we're in = mode (detected by non-NULL expr_text), and have no format
5263 spec and no explict conversion, set the conversion to 'r'. */
5264 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005265 conversion = 'r';
5266 }
5267
Eric V. Smith451d0e32016-09-09 21:56:20 -04005268 /* And now create the FormattedValue node that represents this
5269 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005270 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005271 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005272 n->n_col_offset, n->n_end_lineno,
5273 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005274 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005275 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005276
5277 return 0;
5278
5279unexpected_end_of_string:
5280 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005281 /* Falls through to error. */
5282
5283error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005284 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005285 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005286
Eric V. Smith235a6f02015-09-19 14:51:32 -04005287}
5288
5289/* Return -1 on error.
5290
5291 Return 0 if we have a literal (possible zero length) and an
5292 expression (zero length if at the end of the string.
5293
5294 Return 1 if we have a literal, but no expression, and we want the
5295 caller to call us again. This is used to deal with doubled
5296 braces.
5297
5298 When called multiple times on the string 'a{{b{0}c', this function
5299 will return:
5300
5301 1. the literal 'a{' with no expression, and a return value
5302 of 1. Despite the fact that there's no expression, the return
5303 value of 1 means we're not finished yet.
5304
5305 2. the literal 'b' and the expression '0', with a return value of
5306 0. The fact that there's an expression means we're not finished.
5307
5308 3. literal 'c' with no expression and a return value of 0. The
5309 combination of the return value of 0 with no expression means
5310 we're finished.
5311*/
5312static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005313fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5314 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005315 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005316 struct compiling *c, const node *n)
5317{
5318 int result;
5319
5320 assert(*literal == NULL && *expression == NULL);
5321
5322 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005323 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005324 if (result < 0)
5325 goto error;
5326
5327 assert(result == 0 || result == 1);
5328
5329 if (result == 1)
5330 /* We have a literal, but don't look at the expression. */
5331 return 1;
5332
Eric V. Smith451d0e32016-09-09 21:56:20 -04005333 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005334 /* We're at the end of the string or the end of a nested
5335 f-string: no expression. The top-level error case where we
5336 expect to be at the end of the string but we're at a '}' is
5337 handled later. */
5338 return 0;
5339
5340 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005341 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005342
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005343 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5344 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005345 goto error;
5346
5347 return 0;
5348
5349error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005350 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005351 return -1;
5352}
5353
5354#define EXPRLIST_N_CACHED 64
5355
5356typedef struct {
5357 /* Incrementally build an array of expr_ty, so be used in an
5358 asdl_seq. Cache some small but reasonably sized number of
5359 expr_ty's, and then after that start dynamically allocating,
5360 doubling the number allocated each time. Note that the f-string
5361 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005362 Constant for the literal 'a'. So you add expr_ty's about twice as
Miss Islington (bot)4bd1d052019-08-30 13:42:54 -07005363 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005364
5365 Py_ssize_t allocated; /* Number we've allocated. */
5366 Py_ssize_t size; /* Number we've used. */
5367 expr_ty *p; /* Pointer to the memory we're actually
5368 using. Will point to 'data' until we
5369 start dynamically allocating. */
5370 expr_ty data[EXPRLIST_N_CACHED];
5371} ExprList;
5372
5373#ifdef NDEBUG
5374#define ExprList_check_invariants(l)
5375#else
5376static void
5377ExprList_check_invariants(ExprList *l)
5378{
5379 /* Check our invariants. Make sure this object is "live", and
5380 hasn't been deallocated. */
5381 assert(l->size >= 0);
5382 assert(l->p != NULL);
5383 if (l->size <= EXPRLIST_N_CACHED)
5384 assert(l->data == l->p);
5385}
5386#endif
5387
5388static void
5389ExprList_Init(ExprList *l)
5390{
5391 l->allocated = EXPRLIST_N_CACHED;
5392 l->size = 0;
5393
5394 /* Until we start allocating dynamically, p points to data. */
5395 l->p = l->data;
5396
5397 ExprList_check_invariants(l);
5398}
5399
5400static int
5401ExprList_Append(ExprList *l, expr_ty exp)
5402{
5403 ExprList_check_invariants(l);
5404 if (l->size >= l->allocated) {
5405 /* We need to alloc (or realloc) the memory. */
5406 Py_ssize_t new_size = l->allocated * 2;
5407
5408 /* See if we've ever allocated anything dynamically. */
5409 if (l->p == l->data) {
5410 Py_ssize_t i;
5411 /* We're still using the cached data. Switch to
5412 alloc-ing. */
5413 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5414 if (!l->p)
5415 return -1;
5416 /* Copy the cached data into the new buffer. */
5417 for (i = 0; i < l->size; i++)
5418 l->p[i] = l->data[i];
5419 } else {
5420 /* Just realloc. */
5421 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5422 if (!tmp) {
5423 PyMem_RawFree(l->p);
5424 l->p = NULL;
5425 return -1;
5426 }
5427 l->p = tmp;
5428 }
5429
5430 l->allocated = new_size;
5431 assert(l->allocated == 2 * l->size);
5432 }
5433
5434 l->p[l->size++] = exp;
5435
5436 ExprList_check_invariants(l);
5437 return 0;
5438}
5439
5440static void
5441ExprList_Dealloc(ExprList *l)
5442{
5443 ExprList_check_invariants(l);
5444
5445 /* If there's been an error, or we've never dynamically allocated,
5446 do nothing. */
5447 if (!l->p || l->p == l->data) {
5448 /* Do nothing. */
5449 } else {
5450 /* We have dynamically allocated. Free the memory. */
5451 PyMem_RawFree(l->p);
5452 }
5453 l->p = NULL;
5454 l->size = -1;
5455}
5456
5457static asdl_seq *
5458ExprList_Finish(ExprList *l, PyArena *arena)
5459{
5460 asdl_seq *seq;
5461
5462 ExprList_check_invariants(l);
5463
5464 /* Allocate the asdl_seq and copy the expressions in to it. */
5465 seq = _Py_asdl_seq_new(l->size, arena);
5466 if (seq) {
5467 Py_ssize_t i;
5468 for (i = 0; i < l->size; i++)
5469 asdl_seq_SET(seq, i, l->p[i]);
5470 }
5471 ExprList_Dealloc(l);
5472 return seq;
5473}
5474
5475/* The FstringParser is designed to add a mix of strings and
5476 f-strings, and concat them together as needed. Ultimately, it
5477 generates an expr_ty. */
5478typedef struct {
5479 PyObject *last_str;
5480 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005481 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005482} FstringParser;
5483
5484#ifdef NDEBUG
5485#define FstringParser_check_invariants(state)
5486#else
5487static void
5488FstringParser_check_invariants(FstringParser *state)
5489{
5490 if (state->last_str)
5491 assert(PyUnicode_CheckExact(state->last_str));
5492 ExprList_check_invariants(&state->expr_list);
5493}
5494#endif
5495
5496static void
5497FstringParser_Init(FstringParser *state)
5498{
5499 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005500 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005501 ExprList_Init(&state->expr_list);
5502 FstringParser_check_invariants(state);
5503}
5504
5505static void
5506FstringParser_Dealloc(FstringParser *state)
5507{
5508 FstringParser_check_invariants(state);
5509
5510 Py_XDECREF(state->last_str);
5511 ExprList_Dealloc(&state->expr_list);
5512}
5513
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005514/* Constants for the following */
5515static PyObject *u_kind;
5516
5517/* Compute 'kind' field for string Constant (either 'u' or None) */
5518static PyObject *
5519make_kind(struct compiling *c, const node *n)
5520{
5521 char *s = NULL;
5522 PyObject *kind = NULL;
5523
5524 /* Find the first string literal, if any */
5525 while (TYPE(n) != STRING) {
5526 if (NCH(n) == 0)
5527 return NULL;
5528 n = CHILD(n, 0);
5529 }
5530 REQ(n, STRING);
5531
5532 /* If it starts with 'u', return a PyUnicode "u" string */
5533 s = STR(n);
5534 if (s && *s == 'u') {
5535 if (!u_kind) {
5536 u_kind = PyUnicode_InternFromString("u");
5537 if (!u_kind)
5538 return NULL;
5539 }
5540 kind = u_kind;
5541 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5542 return NULL;
5543 }
5544 Py_INCREF(kind);
5545 }
5546 return kind;
5547}
5548
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005549/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005550static expr_ty
5551make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5552{
5553 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005554 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005555 *str = NULL;
5556 assert(PyUnicode_CheckExact(s));
5557 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5558 Py_DECREF(s);
5559 return NULL;
5560 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005561 kind = make_kind(c, n);
5562 if (kind == NULL && PyErr_Occurred())
5563 return NULL;
5564 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005565 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005566}
5567
5568/* Add a non-f-string (that is, a regular literal string). str is
5569 decref'd. */
5570static int
5571FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5572{
5573 FstringParser_check_invariants(state);
5574
5575 assert(PyUnicode_CheckExact(str));
5576
5577 if (PyUnicode_GET_LENGTH(str) == 0) {
5578 Py_DECREF(str);
5579 return 0;
5580 }
5581
5582 if (!state->last_str) {
5583 /* We didn't have a string before, so just remember this one. */
5584 state->last_str = str;
5585 } else {
5586 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005587 PyUnicode_AppendAndDel(&state->last_str, str);
5588 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005589 return -1;
5590 }
5591 FstringParser_check_invariants(state);
5592 return 0;
5593}
5594
Eric V. Smith451d0e32016-09-09 21:56:20 -04005595/* Parse an f-string. The f-string is in *str to end, with no
5596 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005597static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005598FstringParser_ConcatFstring(FstringParser *state, const char **str,
5599 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005600 struct compiling *c, const node *n)
5601{
5602 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005603 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005604
5605 /* Parse the f-string. */
5606 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005607 PyObject *literal = NULL;
5608 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005609 expr_ty expression = NULL;
5610
5611 /* If there's a zero length literal in front of the
5612 expression, literal will be NULL. If we're at the end of
5613 the f-string, expression will be NULL (unless result == 1,
5614 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005616 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005617 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005618 if (result < 0)
5619 return -1;
5620
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005621 /* Add the literal, if any. */
5622 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5623 Py_XDECREF(expr_text);
5624 return -1;
5625 }
5626 /* Add the expr_text, if any. */
5627 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5628 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005629 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005630
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005631 /* We've dealt with the literal and expr_text, their ownership has
5632 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005633
5634 /* See if we should just loop around to get the next literal
5635 and expression, while ignoring the expression this
5636 time. This is used for un-doubling braces, as an
5637 optimization. */
5638 if (result == 1)
5639 continue;
5640
5641 if (!expression)
5642 /* We're done with this f-string. */
5643 break;
5644
5645 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005646 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005647 if (!state->last_str) {
5648 /* Do nothing. No previous literal. */
5649 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005650 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005651 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5652 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5653 return -1;
5654 }
5655
5656 if (ExprList_Append(&state->expr_list, expression) < 0)
5657 return -1;
5658 }
5659
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660 /* If recurse_lvl is zero, then we must be at the end of the
5661 string. Otherwise, we must be at a right brace. */
5662
Eric V. Smith451d0e32016-09-09 21:56:20 -04005663 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005664 ast_error(c, n, "f-string: unexpected end of string");
5665 return -1;
5666 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005667 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005668 ast_error(c, n, "f-string: expecting '}'");
5669 return -1;
5670 }
5671
5672 FstringParser_check_invariants(state);
5673 return 0;
5674}
5675
5676/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005677 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005678static expr_ty
5679FstringParser_Finish(FstringParser *state, struct compiling *c,
5680 const node *n)
5681{
5682 asdl_seq *seq;
5683
5684 FstringParser_check_invariants(state);
5685
5686 /* If we're just a constant string with no expressions, return
5687 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005688 if (!state->fmode) {
5689 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005690 if (!state->last_str) {
5691 /* Create a zero length string. */
5692 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5693 if (!state->last_str)
5694 goto error;
5695 }
5696 return make_str_node_and_del(&state->last_str, c, n);
5697 }
5698
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005699 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005700 last node in our expression list. */
5701 if (state->last_str) {
5702 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5703 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5704 goto error;
5705 }
5706 /* This has already been freed. */
5707 assert(state->last_str == NULL);
5708
5709 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5710 if (!seq)
5711 goto error;
5712
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005713 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5714 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005715
5716error:
5717 FstringParser_Dealloc(state);
5718 return NULL;
5719}
5720
Eric V. Smith451d0e32016-09-09 21:56:20 -04005721/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5722 at end, parse it into an expr_ty. Return NULL on error. Adjust
5723 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005724static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005725fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005726 struct compiling *c, const node *n)
5727{
5728 FstringParser state;
5729
5730 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005731 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005732 c, n) < 0) {
5733 FstringParser_Dealloc(&state);
5734 return NULL;
5735 }
5736
5737 return FstringParser_Finish(&state, c, n);
5738}
5739
5740/* n is a Python string literal, including the bracketing quote
5741 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005742 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005743 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005744 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5745 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005746*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005747static int
5748parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5749 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005750{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005751 size_t len;
5752 const char *s = STR(n);
5753 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005754 int fmode = 0;
5755 *bytesmode = 0;
5756 *rawmode = 0;
5757 *result = NULL;
5758 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005759 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005760 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005761 if (quote == 'b' || quote == 'B') {
5762 quote = *++s;
5763 *bytesmode = 1;
5764 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005765 else if (quote == 'u' || quote == 'U') {
5766 quote = *++s;
5767 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005768 else if (quote == 'r' || quote == 'R') {
5769 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005770 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005771 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005772 else if (quote == 'f' || quote == 'F') {
5773 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005774 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005775 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005776 else {
5777 break;
5778 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005779 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005780 }
Guido van Rossum495da292019-03-07 12:38:08 -08005781
5782 /* fstrings are only allowed in Python 3.6 and greater */
5783 if (fmode && c->c_feature_version < 6) {
5784 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5785 return -1;
5786 }
5787
Eric V. Smith451d0e32016-09-09 21:56:20 -04005788 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005789 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005790 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005791 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005792 if (quote != '\'' && quote != '\"') {
5793 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005794 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005795 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005796 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005797 s++;
5798 len = strlen(s);
5799 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005800 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005801 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005802 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005803 }
5804 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005805 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005806 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005807 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005808 }
5809 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005810 /* A triple quoted string. We've already skipped one quote at
5811 the start and one at the end of the string. Now skip the
5812 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005813 s += 2;
5814 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005815 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005816 if (s[--len] != quote || s[--len] != quote) {
5817 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005818 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005819 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005820 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005821
Eric V. Smith451d0e32016-09-09 21:56:20 -04005822 if (fmode) {
5823 /* Just return the bytes. The caller will parse the resulting
5824 string. */
5825 *fstr = s;
5826 *fstrlen = len;
5827 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005828 }
5829
Eric V. Smith451d0e32016-09-09 21:56:20 -04005830 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005831 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005832 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005833 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005834 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005835 const char *ch;
5836 for (ch = s; *ch; ch++) {
5837 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005838 ast_error(c, n,
5839 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005840 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005841 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005842 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005843 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005844 if (*rawmode)
5845 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005846 else
Eric V. Smith56466482016-10-31 14:46:26 -04005847 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005848 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005849 if (*rawmode)
5850 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005851 else
Eric V. Smith56466482016-10-31 14:46:26 -04005852 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005853 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005854 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005855}
5856
Eric V. Smith235a6f02015-09-19 14:51:32 -04005857/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5858 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005859 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005860 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005861 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005862 node if there's just an f-string (with no leading or trailing
5863 literals), or a JoinedStr node if there are multiple f-strings or
5864 any literals involved. */
5865static expr_ty
5866parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005867{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005868 int bytesmode = 0;
5869 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005870 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005871
5872 FstringParser state;
5873 FstringParser_Init(&state);
5874
5875 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005876 int this_bytesmode;
5877 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005878 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005879 const char *fstr;
5880 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005881
5882 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005883 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5884 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005885 goto error;
5886
5887 /* Check that we're not mixing bytes with unicode. */
5888 if (i != 0 && bytesmode != this_bytesmode) {
5889 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005890 /* s is NULL if the current string part is an f-string. */
5891 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005892 goto error;
5893 }
5894 bytesmode = this_bytesmode;
5895
Eric V. Smith451d0e32016-09-09 21:56:20 -04005896 if (fstr != NULL) {
5897 int result;
5898 assert(s == NULL && !bytesmode);
5899 /* This is an f-string. Parse and concatenate it. */
5900 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5901 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005902 if (result < 0)
5903 goto error;
5904 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005905 /* A string or byte string. */
5906 assert(s != NULL && fstr == NULL);
5907
Eric V. Smith451d0e32016-09-09 21:56:20 -04005908 assert(bytesmode ? PyBytes_CheckExact(s) :
5909 PyUnicode_CheckExact(s));
5910
Eric V. Smith451d0e32016-09-09 21:56:20 -04005911 if (bytesmode) {
5912 /* For bytes, concat as we go. */
5913 if (i == 0) {
5914 /* First time, just remember this value. */
5915 bytes_str = s;
5916 } else {
5917 PyBytes_ConcatAndDel(&bytes_str, s);
5918 if (!bytes_str)
5919 goto error;
5920 }
5921 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005922 /* This is a regular string. Concatenate it. */
5923 if (FstringParser_ConcatAndDel(&state, s) < 0)
5924 goto error;
5925 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005926 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005927 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005928 if (bytesmode) {
5929 /* Just return the bytes object and we're done. */
5930 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5931 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005932 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005933 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005935
Eric V. Smith235a6f02015-09-19 14:51:32 -04005936 /* We're not a bytes string, bytes_str should never have been set. */
5937 assert(bytes_str == NULL);
5938
5939 return FstringParser_Finish(&state, c, n);
5940
5941error:
5942 Py_XDECREF(bytes_str);
5943 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005945}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005946
5947PyObject *
5948_PyAST_GetDocString(asdl_seq *body)
5949{
5950 if (!asdl_seq_LEN(body)) {
5951 return NULL;
5952 }
5953 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5954 if (st->kind != Expr_kind) {
5955 return NULL;
5956 }
5957 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005958 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5959 return e->v.Constant.value;
5960 }
5961 return NULL;
5962}