blob: 0a999fcca43a8e2d1e730d4af44bec634992dc81 [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
Miss Islington (bot)83a9ba42020-06-06 10:04:47 -070025validate_name(PyObject *name)
26{
27 assert(PyUnicode_Check(name));
28 static const char * const forbidden[] = {
29 "None",
30 "True",
31 "False",
32 NULL
33 };
34 for (int i = 0; forbidden[i] != NULL; i++) {
35 if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
36 PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]);
37 return 0;
38 }
39 }
40 return 1;
41}
42
43static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -050044validate_comprehension(asdl_seq *gens)
45{
Victor Stinner4d73ae72018-11-22 14:45:16 +010046 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050047 if (!asdl_seq_LEN(gens)) {
48 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
49 return 0;
50 }
51 for (i = 0; i < asdl_seq_LEN(gens); i++) {
52 comprehension_ty comp = asdl_seq_GET(gens, i);
53 if (!validate_expr(comp->target, Store) ||
54 !validate_expr(comp->iter, Load) ||
55 !validate_exprs(comp->ifs, Load, 0))
56 return 0;
57 }
58 return 1;
59}
60
61static int
62validate_slice(slice_ty slice)
63{
64 switch (slice->kind) {
65 case Slice_kind:
66 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
67 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
68 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
69 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
72 return 0;
73 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
74 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
75 return 0;
76 return 1;
77 }
78 case Index_kind:
79 return validate_expr(slice->v.Index.value, Load);
80 default:
81 PyErr_SetString(PyExc_SystemError, "unknown slice node");
82 return 0;
83 }
84}
85
86static int
87validate_keywords(asdl_seq *keywords)
88{
Victor Stinner4d73ae72018-11-22 14:45:16 +010089 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050090 for (i = 0; i < asdl_seq_LEN(keywords); i++)
91 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
92 return 0;
93 return 1;
94}
95
96static int
97validate_args(asdl_seq *args)
98{
Victor Stinner4d73ae72018-11-22 14:45:16 +010099 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500100 for (i = 0; i < asdl_seq_LEN(args); i++) {
101 arg_ty arg = asdl_seq_GET(args, i);
102 if (arg->annotation && !validate_expr(arg->annotation, Load))
103 return 0;
104 }
105 return 1;
106}
107
108static const char *
109expr_context_name(expr_context_ty ctx)
110{
111 switch (ctx) {
112 case Load:
113 return "Load";
114 case Store:
115 return "Store";
116 case Del:
117 return "Del";
118 case AugLoad:
119 return "AugLoad";
120 case AugStore:
121 return "AugStore";
122 case Param:
123 return "Param";
124 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700125 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500126 }
127}
128
129static int
130validate_arguments(arguments_ty args)
131{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100132 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500133 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100134 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700135 if (args->vararg && args->vararg->annotation
136 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500137 return 0;
138 }
139 if (!validate_args(args->kwonlyargs))
140 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100141 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700142 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500143 return 0;
144 }
Pablo Galindo2f58a842019-05-31 14:09:49 +0100145 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500146 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
147 return 0;
148 }
149 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
150 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
151 "kw_defaults on arguments");
152 return 0;
153 }
154 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
155}
156
157static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100158validate_constant(PyObject *value)
159{
160 if (value == Py_None || value == Py_Ellipsis)
161 return 1;
162
163 if (PyLong_CheckExact(value)
164 || PyFloat_CheckExact(value)
165 || PyComplex_CheckExact(value)
166 || PyBool_Check(value)
167 || PyUnicode_CheckExact(value)
168 || PyBytes_CheckExact(value))
169 return 1;
170
171 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
172 PyObject *it;
173
174 it = PyObject_GetIter(value);
175 if (it == NULL)
176 return 0;
177
178 while (1) {
179 PyObject *item = PyIter_Next(it);
180 if (item == NULL) {
181 if (PyErr_Occurred()) {
182 Py_DECREF(it);
183 return 0;
184 }
185 break;
186 }
187
188 if (!validate_constant(item)) {
189 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100190 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100191 return 0;
192 }
Victor Stinner726f6902016-01-27 00:11:47 +0100193 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100194 }
195
196 Py_DECREF(it);
197 return 1;
198 }
199
200 return 0;
201}
202
203static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500204validate_expr(expr_ty exp, expr_context_ty ctx)
205{
206 int check_ctx = 1;
207 expr_context_ty actual_ctx;
208
209 /* First check expression context. */
210 switch (exp->kind) {
211 case Attribute_kind:
212 actual_ctx = exp->v.Attribute.ctx;
213 break;
214 case Subscript_kind:
215 actual_ctx = exp->v.Subscript.ctx;
216 break;
217 case Starred_kind:
218 actual_ctx = exp->v.Starred.ctx;
219 break;
220 case Name_kind:
Miss Islington (bot)83a9ba42020-06-06 10:04:47 -0700221 if (!validate_name(exp->v.Name.id)) {
222 return 0;
223 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500224 actual_ctx = exp->v.Name.ctx;
225 break;
226 case List_kind:
227 actual_ctx = exp->v.List.ctx;
228 break;
229 case Tuple_kind:
230 actual_ctx = exp->v.Tuple.ctx;
231 break;
232 default:
233 if (ctx != Load) {
234 PyErr_Format(PyExc_ValueError, "expression which can't be "
235 "assigned to in %s context", expr_context_name(ctx));
236 return 0;
237 }
238 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100239 /* set actual_ctx to prevent gcc warning */
240 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500241 }
242 if (check_ctx && actual_ctx != ctx) {
243 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
244 expr_context_name(ctx), expr_context_name(actual_ctx));
245 return 0;
246 }
247
248 /* Now validate expression. */
249 switch (exp->kind) {
250 case BoolOp_kind:
251 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
252 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
253 return 0;
254 }
255 return validate_exprs(exp->v.BoolOp.values, Load, 0);
256 case BinOp_kind:
257 return validate_expr(exp->v.BinOp.left, Load) &&
258 validate_expr(exp->v.BinOp.right, Load);
259 case UnaryOp_kind:
260 return validate_expr(exp->v.UnaryOp.operand, Load);
261 case Lambda_kind:
262 return validate_arguments(exp->v.Lambda.args) &&
263 validate_expr(exp->v.Lambda.body, Load);
264 case IfExp_kind:
265 return validate_expr(exp->v.IfExp.test, Load) &&
266 validate_expr(exp->v.IfExp.body, Load) &&
267 validate_expr(exp->v.IfExp.orelse, Load);
268 case Dict_kind:
269 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
270 PyErr_SetString(PyExc_ValueError,
271 "Dict doesn't have the same number of keys as values");
272 return 0;
273 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400274 /* null_ok=1 for keys expressions to allow dict unpacking to work in
275 dict literals, i.e. ``{**{a:b}}`` */
276 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
277 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500278 case Set_kind:
279 return validate_exprs(exp->v.Set.elts, Load, 0);
280#define COMP(NAME) \
281 case NAME ## _kind: \
282 return validate_comprehension(exp->v.NAME.generators) && \
283 validate_expr(exp->v.NAME.elt, Load);
284 COMP(ListComp)
285 COMP(SetComp)
286 COMP(GeneratorExp)
287#undef COMP
288 case DictComp_kind:
289 return validate_comprehension(exp->v.DictComp.generators) &&
290 validate_expr(exp->v.DictComp.key, Load) &&
291 validate_expr(exp->v.DictComp.value, Load);
292 case Yield_kind:
293 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500294 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000295 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400296 case Await_kind:
297 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500298 case Compare_kind:
299 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
300 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
301 return 0;
302 }
303 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
304 asdl_seq_LEN(exp->v.Compare.ops)) {
305 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
306 "of comparators and operands");
307 return 0;
308 }
309 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
310 validate_expr(exp->v.Compare.left, Load);
311 case Call_kind:
312 return validate_expr(exp->v.Call.func, Load) &&
313 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400314 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100315 case Constant_kind:
316 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100317 PyErr_Format(PyExc_TypeError,
318 "got an invalid type in Constant: %s",
319 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100320 return 0;
321 }
322 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400323 case JoinedStr_kind:
324 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
325 case FormattedValue_kind:
326 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
327 return 0;
328 if (exp->v.FormattedValue.format_spec)
329 return validate_expr(exp->v.FormattedValue.format_spec, Load);
330 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500331 case Attribute_kind:
332 return validate_expr(exp->v.Attribute.value, Load);
333 case Subscript_kind:
334 return validate_slice(exp->v.Subscript.slice) &&
335 validate_expr(exp->v.Subscript.value, Load);
336 case Starred_kind:
337 return validate_expr(exp->v.Starred.value, ctx);
338 case List_kind:
339 return validate_exprs(exp->v.List.elts, ctx, 0);
340 case Tuple_kind:
341 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000342 case NamedExpr_kind:
343 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300344 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500345 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500347 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000348 PyErr_SetString(PyExc_SystemError, "unexpected expression");
349 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300369validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500370{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300371 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500372}
373
374static int
375validate_stmt(stmt_ty stmt)
376{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100377 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500378 switch (stmt->kind) {
379 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300380 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500381 validate_arguments(stmt->v.FunctionDef.args) &&
382 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
383 (!stmt->v.FunctionDef.returns ||
384 validate_expr(stmt->v.FunctionDef.returns, Load));
385 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300386 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500387 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
388 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400389 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500390 case Return_kind:
391 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
392 case Delete_kind:
393 return validate_assignlist(stmt->v.Delete.targets, Del);
394 case Assign_kind:
395 return validate_assignlist(stmt->v.Assign.targets, Store) &&
396 validate_expr(stmt->v.Assign.value, Load);
397 case AugAssign_kind:
398 return validate_expr(stmt->v.AugAssign.target, Store) &&
399 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700400 case AnnAssign_kind:
401 if (stmt->v.AnnAssign.target->kind != Name_kind &&
402 stmt->v.AnnAssign.simple) {
403 PyErr_SetString(PyExc_TypeError,
404 "AnnAssign with simple non-Name target");
405 return 0;
406 }
407 return validate_expr(stmt->v.AnnAssign.target, Store) &&
408 (!stmt->v.AnnAssign.value ||
409 validate_expr(stmt->v.AnnAssign.value, Load)) &&
410 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500411 case For_kind:
412 return validate_expr(stmt->v.For.target, Store) &&
413 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300414 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500415 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400416 case AsyncFor_kind:
417 return validate_expr(stmt->v.AsyncFor.target, Store) &&
418 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300419 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400420 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500421 case While_kind:
422 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300423 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500424 validate_stmts(stmt->v.While.orelse);
425 case If_kind:
426 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300427 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500428 validate_stmts(stmt->v.If.orelse);
429 case With_kind:
430 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
431 return 0;
432 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
433 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
434 if (!validate_expr(item->context_expr, Load) ||
435 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
436 return 0;
437 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400439 case AsyncWith_kind:
440 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
441 return 0;
442 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
443 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
444 if (!validate_expr(item->context_expr, Load) ||
445 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
446 return 0;
447 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300448 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500449 case Raise_kind:
450 if (stmt->v.Raise.exc) {
451 return validate_expr(stmt->v.Raise.exc, Load) &&
452 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
453 }
454 if (stmt->v.Raise.cause) {
455 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
456 return 0;
457 }
458 return 1;
459 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300460 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500461 return 0;
462 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
463 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
464 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
465 return 0;
466 }
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 asdl_seq_LEN(stmt->v.Try.orelse)) {
469 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
470 return 0;
471 }
472 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
473 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
474 if ((handler->v.ExceptHandler.type &&
475 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300476 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500477 return 0;
478 }
479 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
480 validate_stmts(stmt->v.Try.finalbody)) &&
481 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
482 validate_stmts(stmt->v.Try.orelse));
483 case Assert_kind:
484 return validate_expr(stmt->v.Assert.test, Load) &&
485 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
486 case Import_kind:
487 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
488 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300489 if (stmt->v.ImportFrom.level < 0) {
490 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500491 return 0;
492 }
493 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
494 case Global_kind:
495 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
496 case Nonlocal_kind:
497 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
498 case Expr_kind:
499 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400500 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300501 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400502 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
503 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
504 (!stmt->v.AsyncFunctionDef.returns ||
505 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500506 case Pass_kind:
507 case Break_kind:
508 case Continue_kind:
509 return 1;
510 default:
511 PyErr_SetString(PyExc_SystemError, "unexpected statement");
512 return 0;
513 }
514}
515
516static int
517validate_stmts(asdl_seq *seq)
518{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100519 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500520 for (i = 0; i < asdl_seq_LEN(seq); i++) {
521 stmt_ty stmt = asdl_seq_GET(seq, i);
522 if (stmt) {
523 if (!validate_stmt(stmt))
524 return 0;
525 }
526 else {
527 PyErr_SetString(PyExc_ValueError,
528 "None disallowed in statement list");
529 return 0;
530 }
531 }
532 return 1;
533}
534
535static int
536validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
537{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100538 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500539 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
540 expr_ty expr = asdl_seq_GET(exprs, i);
541 if (expr) {
542 if (!validate_expr(expr, ctx))
543 return 0;
544 }
545 else if (!null_ok) {
546 PyErr_SetString(PyExc_ValueError,
547 "None disallowed in expression list");
548 return 0;
549 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100550
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500551 }
552 return 1;
553}
554
555int
556PyAST_Validate(mod_ty mod)
557{
558 int res = 0;
559
560 switch (mod->kind) {
561 case Module_kind:
562 res = validate_stmts(mod->v.Module.body);
563 break;
564 case Interactive_kind:
565 res = validate_stmts(mod->v.Interactive.body);
566 break;
567 case Expression_kind:
568 res = validate_expr(mod->v.Expression.body, Load);
569 break;
570 case Suite_kind:
571 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
572 break;
573 default:
574 PyErr_SetString(PyExc_SystemError, "impossible module node");
575 res = 0;
576 break;
577 }
578 return res;
579}
580
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500581/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500582#include "grammar.h"
583#include "parsetok.h"
584#include "graminit.h"
585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586/* Data structure used internally */
587struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400588 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200589 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500590 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800591 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592};
593
594static asdl_seq *seq_for_testlist(struct compiling *, const node *);
595static expr_ty ast_for_expr(struct compiling *, const node *);
596static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300597static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
599 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000600static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000601static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
guoci90fc8982018-09-11 17:45:45 -0400603static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
604static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200607static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -0800608 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000610static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400611static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000612static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
Nick Coghlan650f0d02007-04-15 12:05:43 +0000614#define COMP_GENEXP 0
615#define COMP_LISTCOMP 1
616#define COMP_SETCOMP 2
617
Benjamin Peterson55e00432012-01-16 17:22:31 -0500618static int
619init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000620{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
622 if (!m)
623 return 0;
624 c->c_normalize = PyObject_GetAttrString(m, "normalize");
625 Py_DECREF(m);
626 if (!c->c_normalize)
627 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500628 return 1;
629}
630
631static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400632new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400634 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500635 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000636 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500637 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500638 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000639 /* Check whether there are non-ASCII characters in the
640 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500641 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200642 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300643 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500644 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500645 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500647 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300648 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
649 if (form == NULL) {
650 Py_DECREF(id);
651 return NULL;
652 }
653 PyObject *args[2] = {form, id};
654 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500655 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200656 if (!id2)
657 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300658 if (!PyUnicode_Check(id2)) {
659 PyErr_Format(PyExc_TypeError,
660 "unicodedata.normalize() must return a string, not "
661 "%.200s",
662 Py_TYPE(id2)->tp_name);
663 Py_DECREF(id2);
664 return NULL;
665 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200666 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000667 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000668 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200669 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
670 Py_DECREF(id);
671 return NULL;
672 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000673 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674}
675
Benjamin Peterson55e00432012-01-16 17:22:31 -0500676#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200679ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400681 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200682 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200684 va_start(va, errmsg);
685 errstr = PyUnicode_FromFormatV(errmsg, va);
686 va_end(va);
687 if (!errstr) {
688 return 0;
689 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200690 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000692 Py_INCREF(Py_None);
693 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 }
Ammar Askar025eb982018-09-24 17:12:49 -0400695 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200696 if (!tmp) {
697 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400698 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000699 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 Py_DECREF(errstr);
702 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400703 if (value) {
704 PyErr_SetObject(PyExc_SyntaxError, value);
705 Py_DECREF(value);
706 }
707 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708}
709
710/* num_stmts() returns number of contained statements.
711
712 Use this routine to determine how big a sequence is needed for
713 the statements in a parse tree. Its raison d'etre is this bit of
714 grammar:
715
716 stmt: simple_stmt | compound_stmt
717 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
718
719 A simple_stmt can contain multiple small_stmt elements joined
720 by semicolons. If the arg is a simple_stmt, the number of
721 small_stmt elements is returned.
722*/
723
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800724static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800725new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800726{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800727 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800728 if (res == NULL)
729 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800730 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
731 Py_DECREF(res);
732 return NULL;
733 }
734 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800735}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800736#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800737
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738static int
739num_stmts(const node *n)
740{
741 int i, l;
742 node *ch;
743
744 switch (TYPE(n)) {
745 case single_input:
746 if (TYPE(CHILD(n, 0)) == NEWLINE)
747 return 0;
748 else
749 return num_stmts(CHILD(n, 0));
750 case file_input:
751 l = 0;
752 for (i = 0; i < NCH(n); i++) {
753 ch = CHILD(n, i);
754 if (TYPE(ch) == stmt)
755 l += num_stmts(ch);
756 }
757 return l;
758 case stmt:
759 return num_stmts(CHILD(n, 0));
760 case compound_stmt:
761 return 1;
762 case simple_stmt:
763 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
764 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800765 case func_body_suite:
766 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
767 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 if (NCH(n) == 1)
769 return num_stmts(CHILD(n, 0));
770 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800771 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800773 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
774 i += 2;
775 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 l += num_stmts(CHILD(n, i));
777 return l;
778 }
779 default: {
780 char buf[128];
781
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000782 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 TYPE(n), NCH(n));
784 Py_FatalError(buf);
785 }
786 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700787 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788}
789
790/* Transform the CST rooted at node * to the appropriate AST
791*/
792
793mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200794PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
795 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000797 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800799 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 stmt_ty s;
801 node *ch;
802 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500803 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800804 asdl_seq *argtypes = NULL;
805 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400807 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200808 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400809 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800810 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700811 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800812
813 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
Jeremy Hyltona8293132006-02-28 17:58:27 +0000816 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 switch (TYPE(n)) {
818 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200819 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500821 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 for (i = 0; i < NCH(n) - 1; i++) {
823 ch = CHILD(n, i);
824 if (TYPE(ch) == NEWLINE)
825 continue;
826 REQ(ch, stmt);
827 num = num_stmts(ch);
828 if (num == 1) {
829 s = ast_for_stmt(&c, ch);
830 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000832 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 else {
835 ch = CHILD(ch, 0);
836 REQ(ch, simple_stmt);
837 for (j = 0; j < num; j++) {
838 s = ast_for_stmt(&c, CHILD(ch, j * 2));
839 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500840 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000841 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 }
843 }
844 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800845
846 /* Type ignores are stored under the ENDMARKER in file_input. */
847 ch = CHILD(n, NCH(n) - 1);
848 REQ(ch, ENDMARKER);
849 num = NCH(ch);
850 type_ignores = _Py_asdl_seq_new(num, arena);
851 if (!type_ignores)
852 goto out;
853
854 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700855 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
856 if (!type_comment)
857 goto out;
858 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800859 if (!ti)
860 goto out;
861 asdl_seq_SET(type_ignores, i, ti);
862 }
863
864 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 case eval_input: {
867 expr_ty testlist_ast;
868
Nick Coghlan650f0d02007-04-15 12:05:43 +0000869 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000870 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 goto out;
873 res = Expression(testlist_ast, arena);
874 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 }
876 case single_input:
877 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200878 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500880 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000881 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000882 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000884 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500885 goto out;
886 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 }
888 else {
889 n = CHILD(n, 0);
890 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200891 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500893 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000895 s = ast_for_stmt(&c, n);
896 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500897 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 asdl_seq_SET(stmts, 0, s);
899 }
900 else {
901 /* Only a simple_stmt can contain multiple statements. */
902 REQ(n, simple_stmt);
903 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 if (TYPE(CHILD(n, i)) == NEWLINE)
905 break;
906 s = ast_for_stmt(&c, CHILD(n, i));
907 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500908 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 asdl_seq_SET(stmts, i / 2, s);
910 }
911 }
912
Benjamin Peterson55e00432012-01-16 17:22:31 -0500913 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500915 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800916 case func_type_input:
917 n = CHILD(n, 0);
918 REQ(n, func_type);
919
920 if (TYPE(CHILD(n, 1)) == typelist) {
921 ch = CHILD(n, 1);
922 /* this is overly permissive -- we don't pay any attention to
923 * stars on the args -- just parse them into an ordered list */
924 num = 0;
925 for (i = 0; i < NCH(ch); i++) {
926 if (TYPE(CHILD(ch, i)) == test) {
927 num++;
928 }
929 }
930
931 argtypes = _Py_asdl_seq_new(num, arena);
932 if (!argtypes)
933 goto out;
934
935 j = 0;
936 for (i = 0; i < NCH(ch); i++) {
937 if (TYPE(CHILD(ch, i)) == test) {
938 arg = ast_for_expr(&c, CHILD(ch, i));
939 if (!arg)
940 goto out;
941 asdl_seq_SET(argtypes, j++, arg);
942 }
943 }
944 }
945 else {
946 argtypes = _Py_asdl_seq_new(0, arena);
947 if (!argtypes)
948 goto out;
949 }
950
951 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
952 if (!ret)
953 goto out;
954 res = FunctionType(argtypes, ret, arena);
955 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000957 PyErr_Format(PyExc_SystemError,
958 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500959 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500961 out:
962 if (c.c_normalize) {
963 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500964 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500965 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966}
967
Victor Stinner14e461d2013-08-26 22:28:21 +0200968mod_ty
969PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
970 PyArena *arena)
971{
972 mod_ty mod;
973 PyObject *filename;
974 filename = PyUnicode_DecodeFSDefault(filename_str);
975 if (filename == NULL)
976 return NULL;
977 mod = PyAST_FromNodeObject(n, flags, filename, arena);
978 Py_DECREF(filename);
979 return mod;
980
981}
982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
984*/
985
986static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800987get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988{
989 switch (TYPE(n)) {
990 case VBAR:
991 return BitOr;
992 case CIRCUMFLEX:
993 return BitXor;
994 case AMPER:
995 return BitAnd;
996 case LEFTSHIFT:
997 return LShift;
998 case RIGHTSHIFT:
999 return RShift;
1000 case PLUS:
1001 return Add;
1002 case MINUS:
1003 return Sub;
1004 case STAR:
1005 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001006 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -08001007 if (c->c_feature_version < 5) {
1008 ast_error(c, n,
1009 "The '@' operator is only supported in Python 3.5 and greater");
1010 return (operator_ty)0;
1011 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001012 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 case SLASH:
1014 return Div;
1015 case DOUBLESLASH:
1016 return FloorDiv;
1017 case PERCENT:
1018 return Mod;
1019 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 }
1022}
1023
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001024static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001025 "None",
1026 "True",
1027 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001028 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001029 NULL,
1030};
1031
1032static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001033forbidden_name(struct compiling *c, identifier name, const node *n,
1034 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001035{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001036 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001037 const char * const *p = FORBIDDEN;
1038 if (!full_checks) {
1039 /* In most cases, the parser will protect True, False, and None
1040 from being assign to. */
1041 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001042 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001043 for (; *p; p++) {
1044 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1045 ast_error(c, n, "cannot assign to %U", name);
1046 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001047 }
1048 }
1049 return 0;
1050}
1051
Serhiy Storchakab619b092018-11-27 09:40:29 +02001052static expr_ty
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08001053copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001054{
1055 if (e) {
1056 e->lineno = LINENO(n);
1057 e->col_offset = n->n_col_offset;
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08001058 e->end_lineno = end->n_end_lineno;
1059 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001060 }
1061 return e;
1062}
1063
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001064static const char *
1065get_expr_name(expr_ty e)
1066{
1067 switch (e->kind) {
1068 case Attribute_kind:
1069 return "attribute";
1070 case Subscript_kind:
1071 return "subscript";
1072 case Starred_kind:
1073 return "starred";
1074 case Name_kind:
1075 return "name";
1076 case List_kind:
1077 return "list";
1078 case Tuple_kind:
1079 return "tuple";
1080 case Lambda_kind:
1081 return "lambda";
1082 case Call_kind:
1083 return "function call";
1084 case BoolOp_kind:
1085 case BinOp_kind:
1086 case UnaryOp_kind:
1087 return "operator";
1088 case GeneratorExp_kind:
1089 return "generator expression";
1090 case Yield_kind:
1091 case YieldFrom_kind:
1092 return "yield expression";
1093 case Await_kind:
1094 return "await expression";
1095 case ListComp_kind:
1096 return "list comprehension";
1097 case SetComp_kind:
1098 return "set comprehension";
1099 case DictComp_kind:
1100 return "dict comprehension";
1101 case Dict_kind:
1102 return "dict display";
1103 case Set_kind:
1104 return "set display";
1105 case JoinedStr_kind:
1106 case FormattedValue_kind:
1107 return "f-string expression";
1108 case Constant_kind: {
1109 PyObject *value = e->v.Constant.value;
1110 if (value == Py_None) {
1111 return "None";
1112 }
1113 if (value == Py_False) {
1114 return "False";
1115 }
1116 if (value == Py_True) {
1117 return "True";
1118 }
1119 if (value == Py_Ellipsis) {
1120 return "Ellipsis";
1121 }
1122 return "literal";
1123 }
1124 case Compare_kind:
1125 return "comparison";
1126 case IfExp_kind:
1127 return "conditional expression";
1128 case NamedExpr_kind:
1129 return "named expression";
1130 default:
1131 PyErr_Format(PyExc_SystemError,
1132 "unexpected expression in assignment %d (line %d)",
1133 e->kind, e->lineno);
1134 return NULL;
1135 }
1136}
1137
Jeremy Hyltona8293132006-02-28 17:58:27 +00001138/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139
1140 Only sets context for expr kinds that "can appear in assignment context"
1141 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1142 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143*/
1144
1145static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001146set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147{
1148 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001149
1150 /* The ast defines augmented store and load contexts, but the
1151 implementation here doesn't actually use them. The code may be
1152 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001153 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001154 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001155 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001156 */
1157 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158
1159 switch (e->kind) {
1160 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001161 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001162 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001163 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001164 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001166 e->v.Subscript.ctx = ctx;
1167 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001168 case Starred_kind:
1169 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001170 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001171 return 0;
1172 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001174 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001175 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001176 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001177 }
1178 e->v.Name.ctx = ctx;
1179 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181 e->v.List.ctx = ctx;
1182 s = e->v.List.elts;
1183 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001185 e->v.Tuple.ctx = ctx;
1186 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001187 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001188 default: {
1189 const char *expr_name = get_expr_name(e);
1190 if (expr_name != NULL) {
1191 ast_error(c, n, "cannot %s %s",
1192 ctx == Store ? "assign to" : "delete",
1193 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001194 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001195 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001196 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001197 }
1198
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 */
1202 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001203 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204
Thomas Wouters89f507f2006-12-13 04:49:30 +00001205 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001206 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001207 return 0;
1208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 }
1210 return 1;
1211}
1212
1213static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001214ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215{
1216 REQ(n, augassign);
1217 n = CHILD(n, 0);
1218 switch (STR(n)[0]) {
1219 case '+':
1220 return Add;
1221 case '-':
1222 return Sub;
1223 case '/':
1224 if (STR(n)[1] == '/')
1225 return FloorDiv;
1226 else
1227 return Div;
1228 case '%':
1229 return Mod;
1230 case '<':
1231 return LShift;
1232 case '>':
1233 return RShift;
1234 case '&':
1235 return BitAnd;
1236 case '^':
1237 return BitXor;
1238 case '|':
1239 return BitOr;
1240 case '*':
1241 if (STR(n)[1] == '*')
1242 return Pow;
1243 else
1244 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001245 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001246 if (c->c_feature_version < 5) {
1247 ast_error(c, n,
1248 "The '@' operator is only supported in Python 3.5 and greater");
1249 return (operator_ty)0;
1250 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001251 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001253 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 }
1256}
1257
1258static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001259ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001261 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 |'is' 'not'
1263 */
1264 REQ(n, comp_op);
1265 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001266 n = CHILD(n, 0);
1267 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 case LESS:
1269 return Lt;
1270 case GREATER:
1271 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001272 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return Eq;
1274 case LESSEQUAL:
1275 return LtE;
1276 case GREATEREQUAL:
1277 return GtE;
1278 case NOTEQUAL:
1279 return NotEq;
1280 case NAME:
1281 if (strcmp(STR(n), "in") == 0)
1282 return In;
1283 if (strcmp(STR(n), "is") == 0)
1284 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001285 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001287 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 }
1292 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001293 /* handle "not in" and "is not" */
1294 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 case NAME:
1296 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1297 return NotIn;
1298 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1299 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001300 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001302 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001304 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001305 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 }
Neal Norwitz79792652005-11-14 04:25:03 +00001307 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001309 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310}
1311
1312static asdl_seq *
1313seq_for_testlist(struct compiling *c, const node *n)
1314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001316 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1317 */
Armin Rigo31441302005-10-21 12:57:31 +00001318 asdl_seq *seq;
1319 expr_ty expression;
1320 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001321 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001323 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 if (!seq)
1325 return NULL;
1326
1327 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001329 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330
Benjamin Peterson4905e802009-09-27 02:43:28 +00001331 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001332 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334
1335 assert(i / 2 < seq->size);
1336 asdl_seq_SET(seq, i / 2, expression);
1337 }
1338 return seq;
1339}
1340
Neal Norwitzc1505362006-12-28 06:47:50 +00001341static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001342ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001343{
1344 identifier name;
1345 expr_ty annotation = NULL;
1346 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001347 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001348
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001351 name = NEW_IDENTIFIER(ch);
1352 if (!name)
1353 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001354 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001355 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001356
1357 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1358 annotation = ast_for_expr(c, CHILD(n, 2));
1359 if (!annotation)
1360 return NULL;
1361 }
1362
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001363 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001364 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001365 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001366 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001367 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368}
1369
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370/* returns -1 if failed to handle keyword only arguments
1371 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001372 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 ^^^
1374 start pointing here
1375 */
1376static int
1377handle_keywordonly_args(struct compiling *c, const node *n, int start,
1378 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1379{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001380 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001381 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001382 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001383 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 int i = start;
1385 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001386
1387 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001388 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001389 return -1;
1390 }
1391 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 while (i < NCH(n)) {
1393 ch = CHILD(n, i);
1394 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001395 case vfpdef:
1396 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001398 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001399 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001400 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401 asdl_seq_SET(kwdefaults, j, expression);
1402 i += 2; /* '=' and test */
1403 }
1404 else { /* setting NULL if no default value exists */
1405 asdl_seq_SET(kwdefaults, j, NULL);
1406 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001407 if (NCH(ch) == 3) {
1408 /* ch is NAME ':' test */
1409 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001410 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001411 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001412 }
1413 else {
1414 annotation = NULL;
1415 }
1416 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001417 argname = NEW_IDENTIFIER(ch);
1418 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001420 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001421 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001422 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001423 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001424 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001425 if (!arg)
1426 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001427 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001428 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001429 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001430 i += 1; /* the comma, if present */
1431 break;
1432 case TYPE_COMMENT:
1433 /* arg will be equal to the last argument processed */
1434 arg->type_comment = NEW_TYPE_COMMENT(ch);
1435 if (!arg->type_comment)
1436 goto error;
1437 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 break;
1439 case DOUBLESTAR:
1440 return i;
1441 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001442 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001443 goto error;
1444 }
1445 }
1446 return i;
1447 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Jeremy Hyltona8293132006-02-28 17:58:27 +00001451/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452
1453static arguments_ty
1454ast_for_arguments(struct compiling *c, const node *n)
1455{
Neal Norwitzc1505362006-12-28 06:47:50 +00001456 /* This function handles both typedargslist (function definition)
1457 and varargslist (lambda definition).
1458
1459 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001460
1461 The following definition for typedarglist is equivalent to this set of rules:
1462
1463 arguments = argument (',' [TYPE_COMMENT] argument)*
1464 argument = tfpdef ['=' test]
1465 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1466 args = '*' [tfpdef]
1467 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1468 [TYPE_COMMENT] [kwargs]])
1469 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1470 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1471 [TYPE_COMMENT] [args_kwonly_kwargs]])
1472 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1473 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1474 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1475
1476 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1477 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1478 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1479 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1480 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1481 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1482 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1483 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1484 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1485 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1486 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1487 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1488 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1489 '**' tfpdef [','] [TYPE_COMMENT]))
1490
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001492
1493 The following definition for varargslist is equivalent to this set of rules:
1494
1495 arguments = argument (',' argument )*
1496 argument = vfpdef ['=' test]
1497 kwargs = '**' vfpdef [',']
1498 args = '*' [vfpdef]
1499 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1500 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1501 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1502 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1503 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1504 (vararglist_no_posonly)
1505
1506 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1507 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1508 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1509 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1510 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1511 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1512 [',']]] | '**' vfpdef [','])
1513
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001514 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001517 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001518 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001519 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001520 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001521 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 node *ch;
1523
1524 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001525 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001526 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001527 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001529 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530
Jeremy Hyltone921e022008-07-17 16:37:17 +00001531 /* First count the number of positional args & defaults. The
1532 variable i is the loop index for this for loop and the next.
1533 The next loop picks up where the first leaves off.
1534 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536 ch = CHILD(n, i);
1537 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001538 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001539 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001540 if (i < NCH(n) && /* skip argument following star */
1541 (TYPE(CHILD(n, i)) == tfpdef ||
1542 TYPE(CHILD(n, i)) == vfpdef)) {
1543 i++;
1544 }
1545 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001547 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001548 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001549 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001550 if (TYPE(ch) == SLASH ) {
1551 nposonlyargs = nposargs;
1552 nposargs = 0;
1553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 defaults for keyword only args */
1557 for ( ; i < NCH(n); ++i) {
1558 ch = CHILD(n, i);
1559 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001560 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001561 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001562 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1563 if (!posonlyargs && nposonlyargs) {
1564 return NULL;
1565 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001566 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001567 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001568 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001569 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001570 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001571 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001572 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001574 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001575 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001576 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001578 since we set NULL as default for keyword only argument w/o default
1579 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001580 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001581 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001582 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001583 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001584
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001585 /* tfpdef: NAME [':' test]
1586 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 */
1588 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001589 j = 0; /* index for defaults */
1590 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001591 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001593 ch = CHILD(n, i);
1594 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001595 case tfpdef:
1596 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1598 anything other than EQUAL or a comma? */
1599 /* XXX Should NCH(n) check be made a separate check? */
1600 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001601 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1602 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001603 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001604 assert(posdefaults != NULL);
1605 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001607 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001609 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001610 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001611 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001612 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001613 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001614 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001615 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001616 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001617 if (l < nposonlyargs) {
1618 asdl_seq_SET(posonlyargs, l++, arg);
1619 } else {
1620 asdl_seq_SET(posargs, k++, arg);
1621 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001622 i += 1; /* the name */
1623 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1624 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001626 case SLASH:
1627 /* Advance the slash and the comma. If there are more names
1628 * after the slash there will be a comma so we are advancing
1629 * the correct number of nodes. If the slash is the last item,
1630 * we will be advancing an extra token but then * i > NCH(n)
1631 * and the enclosing while will finish correctly. */
1632 i += 2;
1633 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001635 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001636 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1637 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001638 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001639 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001640 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001641 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001642 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001643 if (TYPE(ch) == COMMA) {
1644 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001645 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001646
1647 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1648 ast_error(c, CHILD(n, i),
1649 "bare * has associated type comment");
1650 return NULL;
1651 }
1652
Guido van Rossum4f72a782006-10-27 23:31:49 +00001653 res = handle_keywordonly_args(c, n, i,
1654 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001655 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001656 i = res; /* res has new position to process */
1657 }
1658 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001659 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001660 if (!vararg)
1661 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001662
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001663 i += 2; /* the star and the name */
1664 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1665 i += 1; /* the comma, if present */
1666
1667 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1668 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1669 if (!vararg->type_comment)
1670 return NULL;
1671 i += 1;
1672 }
1673
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001674 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1675 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001676 int res = 0;
1677 res = handle_keywordonly_args(c, n, i,
1678 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001679 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001680 i = res; /* res has new position to process */
1681 }
1682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 break;
1684 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001685 ch = CHILD(n, i+1); /* tfpdef */
1686 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001687 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001688 if (!kwarg)
1689 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001690 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001691 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001692 i += 1; /* the comma, if present */
1693 break;
1694 case TYPE_COMMENT:
1695 assert(i);
1696
1697 if (kwarg)
1698 arg = kwarg;
1699
1700 /* arg will be equal to the last argument processed */
1701 arg->type_comment = NEW_TYPE_COMMENT(ch);
1702 if (!arg->type_comment)
1703 return NULL;
1704 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 break;
1706 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001707 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 "unexpected node in varargslist: %d @ %d",
1709 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001710 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 }
Miss Islington (bot)cf9a63c2019-07-14 16:49:52 -07001713 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714}
1715
1716static expr_ty
1717ast_for_dotted_name(struct compiling *c, const node *n)
1718{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001719 expr_ty e;
1720 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001721 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001723 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724
1725 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001726
1727 lineno = LINENO(n);
1728 col_offset = n->n_col_offset;
1729
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001730 ch = CHILD(n, 0);
1731 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001734 e = Name(id, Load, lineno, col_offset,
1735 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738
1739 for (i = 2; i < NCH(n); i+=2) {
Lysandros Nikolaou8b9cebc2020-02-08 01:21:38 +01001740 const node *child = CHILD(n, i);
1741 id = NEW_IDENTIFIER(child);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001742 if (!id)
1743 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001744 e = Attribute(e, id, Load, lineno, col_offset,
Lysandros Nikolaou8b9cebc2020-02-08 01:21:38 +01001745 child->n_end_lineno, child->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 if (!e)
1747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 }
1749
1750 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751}
1752
1753static expr_ty
1754ast_for_decorator(struct compiling *c, const node *n)
1755{
1756 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1757 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001758 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001761 REQ(CHILD(n, 0), AT);
1762 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1765 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001766 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001769 d = name_expr;
1770 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 }
1772 else if (NCH(n) == 5) { /* Call with no arguments */
Miss Skeleton (bot)ba3a5662019-10-26 07:06:40 -07001773 d = Call(name_expr, NULL, NULL,
1774 name_expr->lineno, name_expr->col_offset,
1775 CHILD(n, 3)->n_end_lineno, CHILD(n, 3)->n_end_col_offset,
1776 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001777 if (!d)
1778 return NULL;
1779 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 }
1781 else {
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08001782 d = ast_for_call(c, CHILD(n, 3), name_expr,
1783 CHILD(n, 1), CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001784 if (!d)
1785 return NULL;
1786 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 }
1788
1789 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790}
1791
1792static asdl_seq*
1793ast_for_decorators(struct compiling *c, const node *n)
1794{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001795 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001796 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001800 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 if (!decorator_seq)
1802 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001805 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001806 if (!d)
1807 return NULL;
1808 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 }
1810 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811}
1812
1813static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001814ast_for_funcdef_impl(struct compiling *c, const node *n0,
1815 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001817 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001818 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001819 identifier name;
1820 arguments_ty args;
1821 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001822 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001823 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001824 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001825 node *tc;
1826 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827
Guido van Rossum495da292019-03-07 12:38:08 -08001828 if (is_async && c->c_feature_version < 5) {
1829 ast_error(c, n,
1830 "Async functions are only supported in Python 3.5 and greater");
1831 return NULL;
1832 }
1833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 REQ(n, funcdef);
1835
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 name = NEW_IDENTIFIER(CHILD(n, name_i));
1837 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001838 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001839 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001840 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1842 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001843 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001844 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1845 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1846 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001847 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001848 name_i += 2;
1849 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001850 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1851 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1852 if (!type_comment)
1853 return NULL;
1854 name_i += 1;
1855 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001856 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001858 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001859 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001861 if (NCH(CHILD(n, name_i + 3)) > 1) {
1862 /* Check if the suite has a type comment in it. */
1863 tc = CHILD(CHILD(n, name_i + 3), 1);
1864
1865 if (TYPE(tc) == TYPE_COMMENT) {
1866 if (type_comment != NULL) {
1867 ast_error(c, n, "Cannot have two type comments on def");
1868 return NULL;
1869 }
1870 type_comment = NEW_TYPE_COMMENT(tc);
1871 if (!type_comment)
1872 return NULL;
1873 }
1874 }
1875
Yury Selivanov75445082015-05-11 22:57:16 -04001876 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001877 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001878 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001879 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001880 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001881 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001882}
1883
1884static stmt_ty
1885ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1886{
Guido van Rossum495da292019-03-07 12:38:08 -08001887 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001888 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001889 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001890 REQ(CHILD(n, 1), funcdef);
1891
guoci90fc8982018-09-11 17:45:45 -04001892 return ast_for_funcdef_impl(c, n, decorator_seq,
1893 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001894}
1895
1896static stmt_ty
1897ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1898{
1899 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1900 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001901 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001902}
1903
1904
1905static stmt_ty
1906ast_for_async_stmt(struct compiling *c, const node *n)
1907{
Guido van Rossum495da292019-03-07 12:38:08 -08001908 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001909 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001910 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001911
1912 switch (TYPE(CHILD(n, 1))) {
1913 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001914 return ast_for_funcdef_impl(c, n, NULL,
1915 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001916 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001917 return ast_for_with_stmt(c, n,
1918 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001919
1920 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001921 return ast_for_for_stmt(c, n,
1922 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001923
1924 default:
1925 PyErr_Format(PyExc_SystemError,
1926 "invalid async stament: %s",
1927 STR(CHILD(n, 1)));
1928 return NULL;
1929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930}
1931
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001932static stmt_ty
1933ast_for_decorated(struct compiling *c, const node *n)
1934{
Yury Selivanov75445082015-05-11 22:57:16 -04001935 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001936 stmt_ty thing = NULL;
1937 asdl_seq *decorator_seq = NULL;
1938
1939 REQ(n, decorated);
1940
1941 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1942 if (!decorator_seq)
1943 return NULL;
1944
1945 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001946 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001948
1949 if (TYPE(CHILD(n, 1)) == funcdef) {
1950 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1951 } else if (TYPE(CHILD(n, 1)) == classdef) {
1952 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001953 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1954 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001955 }
1956 return thing;
1957}
1958
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001960ast_for_namedexpr(struct compiling *c, const node *n)
1961{
Miss Islington (bot)cd968de2019-12-15 12:04:07 -08001962 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001963 argument: ( test [comp_for] |
1964 test ':=' test |
1965 test '=' test |
1966 '**' test |
1967 '*' test )
1968 */
1969 expr_ty target, value;
1970
1971 target = ast_for_expr(c, CHILD(n, 0));
1972 if (!target)
1973 return NULL;
1974
1975 value = ast_for_expr(c, CHILD(n, 2));
1976 if (!value)
1977 return NULL;
1978
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001979 if (target->kind != Name_kind) {
1980 const char *expr_name = get_expr_name(target);
1981 if (expr_name != NULL) {
Miss Islington (bot)6c004952019-12-31 19:28:08 -08001982 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001983 }
1984 return NULL;
1985 }
1986
1987 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001988 return NULL;
1989
1990 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1991 n->n_end_col_offset, c->c_arena);
1992}
1993
1994static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995ast_for_lambdef(struct compiling *c, const node *n)
1996{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001997 /* lambdef: 'lambda' [varargslist] ':' test
1998 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 arguments_ty args;
2000 expr_ty expression;
2001
2002 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002003 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 if (!args)
2005 return NULL;
2006 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002007 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 }
2010 else {
2011 args = ast_for_arguments(c, CHILD(n, 1));
2012 if (!args)
2013 return NULL;
2014 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002015 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 }
2018
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002019 return Lambda(args, expression, LINENO(n), n->n_col_offset,
2020 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021}
2022
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002023static expr_ty
2024ast_for_ifexpr(struct compiling *c, const node *n)
2025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002027 expr_ty expression, body, orelse;
2028
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00002029 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002030 body = ast_for_expr(c, CHILD(n, 0));
2031 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002032 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002033 expression = ast_for_expr(c, CHILD(n, 2));
2034 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002035 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002036 orelse = ast_for_expr(c, CHILD(n, 4));
2037 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002038 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002039 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002040 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002041 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002042}
2043
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002045 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046
Nick Coghlan650f0d02007-04-15 12:05:43 +00002047 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048*/
2049
2050static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002051count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002053 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054
Guido van Rossumd8faa362007-04-27 19:54:29 +00002055 count_comp_for:
2056 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002057 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002058 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08002059 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002060 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002061 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002062 else if (NCH(n) == 1) {
2063 n = CHILD(n, 0);
2064 }
2065 else {
2066 goto error;
2067 }
2068 if (NCH(n) == (5)) {
2069 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002070 }
2071 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002072 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002073 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002074 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002075 REQ(n, comp_iter);
2076 n = CHILD(n, 0);
2077 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002078 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002079 else if (TYPE(n) == comp_if) {
2080 if (NCH(n) == 3) {
2081 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002082 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002083 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002084 else
2085 return n_fors;
2086 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002087
Jelle Zijlstraac317702017-10-05 20:24:46 -07002088 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002089 /* Should never be reached */
2090 PyErr_SetString(PyExc_SystemError,
2091 "logic error in count_comp_fors");
2092 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093}
2094
Nick Coghlan650f0d02007-04-15 12:05:43 +00002095/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096
Nick Coghlan650f0d02007-04-15 12:05:43 +00002097 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098*/
2099
2100static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002101count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002103 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104
Guido van Rossumd8faa362007-04-27 19:54:29 +00002105 while (1) {
2106 REQ(n, comp_iter);
2107 if (TYPE(CHILD(n, 0)) == comp_for)
2108 return n_ifs;
2109 n = CHILD(n, 0);
2110 REQ(n, comp_if);
2111 n_ifs++;
2112 if (NCH(n) == 2)
2113 return n_ifs;
2114 n = CHILD(n, 2);
2115 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116}
2117
Guido van Rossum992d4a32007-07-11 13:09:30 +00002118static asdl_seq *
2119ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002122 asdl_seq *comps;
2123
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002124 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 if (n_fors == -1)
2126 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002127
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002128 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002129 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002133 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002135 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002136 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002137 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002138 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139
Guido van Rossum992d4a32007-07-11 13:09:30 +00002140 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141
Jelle Zijlstraac317702017-10-05 20:24:46 -07002142 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002143 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002144 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002145 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002146 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002147 else {
2148 sync_n = CHILD(n, 0);
2149 }
2150 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002151
Guido van Rossum495da292019-03-07 12:38:08 -08002152 /* Async comprehensions only allowed in Python 3.6 and greater */
2153 if (is_async && c->c_feature_version < 6) {
2154 ast_error(c, n,
2155 "Async comprehensions are only supported in Python 3.6 and greater");
2156 return NULL;
2157 }
2158
Jelle Zijlstraac317702017-10-05 20:24:46 -07002159 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002160 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002161 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002163 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 /* Check the # of children rather than the length of t, since
2168 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002169 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002170 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002171 comp = comprehension(first, expression, NULL,
2172 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002174 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2175 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2176 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002177 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002178 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002180
Jelle Zijlstraac317702017-10-05 20:24:46 -07002181 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 int j, n_ifs;
2183 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184
Jelle Zijlstraac317702017-10-05 20:24:46 -07002185 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002186 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002187 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002189
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002190 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002191 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002193
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002195 REQ(n, comp_iter);
2196 n = CHILD(n, 0);
2197 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198
Guido van Rossum992d4a32007-07-11 13:09:30 +00002199 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002200 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002201 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002202 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002203 if (NCH(n) == 3)
2204 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002206 /* on exit, must guarantee that n is a comp_for */
2207 if (TYPE(n) == comp_iter)
2208 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002209 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002211 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002213 return comps;
2214}
2215
2216static expr_ty
2217ast_for_itercomp(struct compiling *c, const node *n, int type)
2218{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002219 /* testlist_comp: (test|star_expr)
2220 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002221 expr_ty elt;
2222 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002223 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224
Guido van Rossum992d4a32007-07-11 13:09:30 +00002225 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002227 ch = CHILD(n, 0);
2228 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002229 if (!elt)
2230 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002231 if (elt->kind == Starred_kind) {
2232 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2233 return NULL;
2234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235
Guido van Rossum992d4a32007-07-11 13:09:30 +00002236 comps = ast_for_comprehension(c, CHILD(n, 1));
2237 if (!comps)
2238 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002239
2240 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002241 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2242 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002243 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002244 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2245 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002246 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002247 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2248 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002249 else
2250 /* Should never happen */
2251 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252}
2253
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002254/* Fills in the key, value pair corresponding to the dict element. In case
2255 * of an unpacking, key is NULL. *i is advanced by the number of ast
2256 * elements. Iff successful, nonzero is returned.
2257 */
2258static int
2259ast_for_dictelement(struct compiling *c, const node *n, int *i,
2260 expr_ty *key, expr_ty *value)
2261{
2262 expr_ty expression;
2263 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2264 assert(NCH(n) - *i >= 2);
2265
2266 expression = ast_for_expr(c, CHILD(n, *i + 1));
2267 if (!expression)
2268 return 0;
2269 *key = NULL;
2270 *value = expression;
2271
2272 *i += 2;
2273 }
2274 else {
2275 assert(NCH(n) - *i >= 3);
2276
2277 expression = ast_for_expr(c, CHILD(n, *i));
2278 if (!expression)
2279 return 0;
2280 *key = expression;
2281
2282 REQ(CHILD(n, *i + 1), COLON);
2283
2284 expression = ast_for_expr(c, CHILD(n, *i + 2));
2285 if (!expression)
2286 return 0;
2287 *value = expression;
2288
2289 *i += 3;
2290 }
2291 return 1;
2292}
2293
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002295ast_for_dictcomp(struct compiling *c, const node *n)
2296{
2297 expr_ty key, value;
2298 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002299 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002301 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002302 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002303 assert(key);
2304 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002306 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002307 if (!comps)
2308 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002310 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2311 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002312}
2313
2314static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002315ast_for_dictdisplay(struct compiling *c, const node *n)
2316{
2317 int i;
2318 int j;
2319 int size;
2320 asdl_seq *keys, *values;
2321
2322 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2323 keys = _Py_asdl_seq_new(size, c->c_arena);
2324 if (!keys)
2325 return NULL;
2326
2327 values = _Py_asdl_seq_new(size, c->c_arena);
2328 if (!values)
2329 return NULL;
2330
2331 j = 0;
2332 for (i = 0; i < NCH(n); i++) {
2333 expr_ty key, value;
2334
2335 if (!ast_for_dictelement(c, n, &i, &key, &value))
2336 return NULL;
2337 asdl_seq_SET(keys, j, key);
2338 asdl_seq_SET(values, j, value);
2339
2340 j++;
2341 }
2342 keys->size = j;
2343 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002344 return Dict(keys, values, LINENO(n), n->n_col_offset,
2345 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002346}
2347
2348static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002349ast_for_genexp(struct compiling *c, const node *n)
2350{
2351 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002352 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002353}
2354
2355static expr_ty
2356ast_for_listcomp(struct compiling *c, const node *n)
2357{
2358 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002359 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002360}
2361
2362static expr_ty
2363ast_for_setcomp(struct compiling *c, const node *n)
2364{
2365 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002366 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002367}
2368
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002369static expr_ty
2370ast_for_setdisplay(struct compiling *c, const node *n)
2371{
2372 int i;
2373 int size;
2374 asdl_seq *elts;
2375
2376 assert(TYPE(n) == (dictorsetmaker));
2377 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2378 elts = _Py_asdl_seq_new(size, c->c_arena);
2379 if (!elts)
2380 return NULL;
2381 for (i = 0; i < NCH(n); i += 2) {
2382 expr_ty expression;
2383 expression = ast_for_expr(c, CHILD(n, i));
2384 if (!expression)
2385 return NULL;
2386 asdl_seq_SET(elts, i / 2, expression);
2387 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002388 return Set(elts, LINENO(n), n->n_col_offset,
2389 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002390}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002391
2392static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393ast_for_atom(struct compiling *c, const node *n)
2394{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002395 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2396 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002397 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 */
2399 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002402 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002403 PyObject *name;
2404 const char *s = STR(ch);
2405 size_t len = strlen(s);
2406 if (len >= 4 && len <= 5) {
2407 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002408 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002409 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002410 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002411 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002412 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002413 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002414 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002415 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002416 }
2417 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002418 if (!name)
2419 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002420 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002421 return Name(name, Load, LINENO(n), n->n_col_offset,
2422 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002423 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002425 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002426 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002427 const char *errtype = NULL;
2428 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2429 errtype = "unicode error";
2430 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2431 errtype = "value error";
2432 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002433 PyObject *type, *value, *tback, *errstr;
2434 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002435 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002436 if (errstr) {
2437 ast_error(c, n, "(%s) %U", errtype, errstr);
2438 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002439 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002440 else {
2441 PyErr_Clear();
2442 ast_error(c, n, "(%s) unknown error", errtype);
2443 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002444 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002445 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002446 Py_XDECREF(tback);
2447 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002448 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002449 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002450 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 }
2452 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002453 PyObject *pynum;
2454 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2455 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2456 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2457 ast_error(c, ch,
2458 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2459 return NULL;
2460 }
2461 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002462 if (!pynum)
2463 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002464
Victor Stinner43d81952013-07-17 00:57:58 +02002465 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2466 Py_DECREF(pynum);
2467 return NULL;
2468 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002469 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002470 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 }
Georg Brandldde00282007-03-18 19:01:53 +00002472 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002473 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002474 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002476 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477
Thomas Wouters89f507f2006-12-13 04:49:30 +00002478 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002479 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2480 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481
Thomas Wouters89f507f2006-12-13 04:49:30 +00002482 if (TYPE(ch) == yield_expr)
2483 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002486 if (NCH(ch) == 1) {
2487 return ast_for_testlist(c, ch);
2488 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002489
Serhiy Storchakab619b092018-11-27 09:40:29 +02002490 if (TYPE(CHILD(ch, 1)) == comp_for) {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002491 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002492 }
2493 else {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002494 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002495 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002497 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498
Thomas Wouters89f507f2006-12-13 04:49:30 +00002499 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002500 return List(NULL, Load, LINENO(n), n->n_col_offset,
2501 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502
Nick Coghlan650f0d02007-04-15 12:05:43 +00002503 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002504 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2505 asdl_seq *elts = seq_for_testlist(c, ch);
2506 if (!elts)
2507 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002508
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002509 return List(elts, Load, LINENO(n), n->n_col_offset,
2510 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002511 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002512 else {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002513 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002516 /* dictorsetmaker: ( ((test ':' test | '**' test)
2517 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2518 * ((test | '*' test)
2519 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002520 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002521 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002522 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002523 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002524 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2525 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 }
2527 else {
2528 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2529 if (NCH(ch) == 1 ||
2530 (NCH(ch) > 1 &&
2531 TYPE(CHILD(ch, 1)) == COMMA)) {
2532 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002533 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002534 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002535 else if (NCH(ch) > 1 &&
2536 TYPE(CHILD(ch, 1)) == comp_for) {
2537 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002538 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002539 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002540 else if (NCH(ch) > 3 - is_dict &&
2541 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2542 /* It's a dictionary comprehension. */
2543 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002544 ast_error(c, n,
2545 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002546 return NULL;
2547 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002548 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002549 }
2550 else {
2551 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002552 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002553 }
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002554 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002555 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002558 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2559 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 }
2561}
2562
2563static slice_ty
2564ast_for_slice(struct compiling *c, const node *n)
2565{
2566 node *ch;
2567 expr_ty lower = NULL, upper = NULL, step = NULL;
2568
2569 REQ(n, subscript);
2570
2571 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002572 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 sliceop: ':' [test]
2574 */
2575 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 if (NCH(n) == 1 && TYPE(ch) == test) {
2577 /* 'step' variable hold no significance in terms of being used over
2578 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 if (!step)
2581 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582
Thomas Wouters89f507f2006-12-13 04:49:30 +00002583 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 }
2585
2586 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002587 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 if (!lower)
2589 return NULL;
2590 }
2591
2592 /* If there's an upper bound it's in the second or third position. */
2593 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002594 if (NCH(n) > 1) {
2595 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596
Thomas Wouters89f507f2006-12-13 04:49:30 +00002597 if (TYPE(n2) == test) {
2598 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 if (!upper)
2600 return NULL;
2601 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002604 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605
Thomas Wouters89f507f2006-12-13 04:49:30 +00002606 if (TYPE(n2) == test) {
2607 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 if (!upper)
2609 return NULL;
2610 }
2611 }
2612
2613 ch = CHILD(n, NCH(n) - 1);
2614 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002615 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002616 ch = CHILD(ch, 1);
2617 if (TYPE(ch) == test) {
2618 step = ast_for_expr(c, ch);
2619 if (!step)
2620 return NULL;
2621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 }
2623 }
2624
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002625 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626}
2627
2628static expr_ty
2629ast_for_binop(struct compiling *c, const node *n)
2630{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002631 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002633 BinOp(BinOp(A, op, B), op, C).
2634 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635
Guido van Rossumd8faa362007-04-27 19:54:29 +00002636 int i, nops;
2637 expr_ty expr1, expr2, result;
2638 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639
Guido van Rossumd8faa362007-04-27 19:54:29 +00002640 expr1 = ast_for_expr(c, CHILD(n, 0));
2641 if (!expr1)
2642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643
Guido van Rossumd8faa362007-04-27 19:54:29 +00002644 expr2 = ast_for_expr(c, CHILD(n, 2));
2645 if (!expr2)
2646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647
Guido van Rossum495da292019-03-07 12:38:08 -08002648 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002649 if (!newoperator)
2650 return NULL;
2651
2652 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002653 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002654 c->c_arena);
2655 if (!result)
2656 return NULL;
2657
2658 nops = (NCH(n) - 1) / 2;
2659 for (i = 1; i < nops; i++) {
2660 expr_ty tmp_result, tmp;
2661 const node* next_oper = CHILD(n, i * 2 + 1);
2662
Guido van Rossum495da292019-03-07 12:38:08 -08002663 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002664 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 return NULL;
2666
Guido van Rossumd8faa362007-04-27 19:54:29 +00002667 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2668 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 return NULL;
2670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 tmp_result = BinOp(result, newoperator, tmp,
Miss Islington (bot)c7be35c2019-07-08 14:41:34 -07002672 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002673 CHILD(n, i * 2 + 2)->n_end_lineno,
2674 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002675 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002677 return NULL;
2678 result = tmp_result;
2679 }
2680 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681}
2682
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002683static expr_ty
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002684ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002687 subscriptlist: subscript (',' subscript)* [',']
2688 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2689 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002690 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002691 REQ(n, trailer);
2692 if (TYPE(CHILD(n, 0)) == LPAR) {
2693 if (NCH(n) == 2)
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002694 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002695 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002696 else
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002697 return ast_for_call(c, CHILD(n, 1), left_expr,
2698 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002699 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002700 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002701 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2702 if (!attr_id)
2703 return NULL;
2704 return Attribute(left_expr, attr_id, Load,
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002705 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002706 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002707 }
2708 else {
2709 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002710 REQ(CHILD(n, 2), RSQB);
2711 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002712 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002713 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2714 if (!slc)
2715 return NULL;
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002716 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002717 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002718 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002719 }
2720 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002722 by treating the sequence as a tuple literal if there are
2723 no slice features.
2724 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002725 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002726 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002727 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002728 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002729 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002730 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002731 if (!slices)
2732 return NULL;
2733 for (j = 0; j < NCH(n); j += 2) {
2734 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002735 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002736 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002737 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002738 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002739 asdl_seq_SET(slices, j / 2, slc);
2740 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002741 if (!simple) {
2742 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002743 Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002744 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002745 }
2746 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002747 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002748 if (!elts)
2749 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002750 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2751 slc = (slice_ty)asdl_seq_GET(slices, j);
2752 assert(slc->kind == Index_kind && slc->v.Index.value);
2753 asdl_seq_SET(elts, j, slc->v.Index.value);
2754 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002755 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2756 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002757 if (!e)
2758 return NULL;
2759 return Subscript(left_expr, Index(e, c->c_arena),
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002760 Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002761 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002762 }
2763 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002764}
2765
2766static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002767ast_for_factor(struct compiling *c, const node *n)
2768{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002769 expr_ty expression;
2770
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002771 expression = ast_for_expr(c, CHILD(n, 1));
2772 if (!expression)
2773 return NULL;
2774
2775 switch (TYPE(CHILD(n, 0))) {
2776 case PLUS:
2777 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002778 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002779 c->c_arena);
2780 case MINUS:
2781 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002782 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002783 c->c_arena);
2784 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002785 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2786 n->n_end_lineno, n->n_end_col_offset,
2787 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002788 }
2789 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2790 TYPE(CHILD(n, 0)));
2791 return NULL;
2792}
2793
2794static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002795ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002796{
Yury Selivanov75445082015-05-11 22:57:16 -04002797 int i, nch, start = 0;
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002798 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002799
2800 REQ(n, atom_expr);
2801 nch = NCH(n);
2802
Guido van Rossum495da292019-03-07 12:38:08 -08002803 if (TYPE(CHILD(n, 0)) == AWAIT) {
2804 if (c->c_feature_version < 5) {
2805 ast_error(c, n,
2806 "Await expressions are only supported in Python 3.5 and greater");
2807 return NULL;
2808 }
Yury Selivanov75445082015-05-11 22:57:16 -04002809 start = 1;
2810 assert(nch > 1);
2811 }
2812
2813 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002814 if (!e)
2815 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002816 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002817 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002818 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002819 return Await(e, LINENO(n), n->n_col_offset,
2820 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002821 }
2822
2823 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002824 node *ch = CHILD(n, i);
2825 if (TYPE(ch) != trailer)
2826 break;
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002827 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2828 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002829 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002830 }
Yury Selivanov75445082015-05-11 22:57:16 -04002831
2832 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002833 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002834 return Await(e, LINENO(n), n->n_col_offset,
2835 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002836 }
2837 else {
2838 return e;
2839 }
2840}
2841
2842static expr_ty
2843ast_for_power(struct compiling *c, const node *n)
2844{
2845 /* power: atom trailer* ('**' factor)*
2846 */
2847 expr_ty e;
2848 REQ(n, power);
2849 e = ast_for_atom_expr(c, CHILD(n, 0));
2850 if (!e)
2851 return NULL;
2852 if (NCH(n) == 1)
2853 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002854 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2855 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002856 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002857 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002858 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2859 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002860 }
2861 return e;
2862}
2863
Guido van Rossum0368b722007-05-11 16:50:42 +00002864static expr_ty
2865ast_for_starred(struct compiling *c, const node *n)
2866{
2867 expr_ty tmp;
2868 REQ(n, star_expr);
2869
2870 tmp = ast_for_expr(c, CHILD(n, 1));
2871 if (!tmp)
2872 return NULL;
2873
2874 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002875 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2876 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002877}
2878
2879
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880/* Do not name a variable 'expr'! Will cause a compile error.
2881*/
2882
2883static expr_ty
2884ast_for_expr(struct compiling *c, const node *n)
2885{
2886 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002887 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002888 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002889 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 and_test: not_test ('and' not_test)*
2892 not_test: 'not' not_test | comparison
2893 comparison: expr (comp_op expr)*
2894 expr: xor_expr ('|' xor_expr)*
2895 xor_expr: and_expr ('^' and_expr)*
2896 and_expr: shift_expr ('&' shift_expr)*
2897 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2898 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002899 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002901 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002902 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002903 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 */
2905
2906 asdl_seq *seq;
2907 int i;
2908
2909 loop:
2910 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002911 case namedexpr_test:
2912 if (NCH(n) == 3)
2913 return ast_for_namedexpr(c, n);
2914 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002916 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002917 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002918 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002920 else if (NCH(n) > 1)
2921 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002922 /* Fallthrough */
2923 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 case and_test:
2925 if (NCH(n) == 1) {
2926 n = CHILD(n, 0);
2927 goto loop;
2928 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002929 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930 if (!seq)
2931 return NULL;
2932 for (i = 0; i < NCH(n); i += 2) {
2933 expr_ty e = ast_for_expr(c, CHILD(n, i));
2934 if (!e)
2935 return NULL;
2936 asdl_seq_SET(seq, i / 2, e);
2937 }
2938 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002939 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002940 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002941 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002942 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002943 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2944 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 case not_test:
2946 if (NCH(n) == 1) {
2947 n = CHILD(n, 0);
2948 goto loop;
2949 }
2950 else {
2951 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2952 if (!expression)
2953 return NULL;
2954
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002955 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002956 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002957 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 }
2959 case comparison:
2960 if (NCH(n) == 1) {
2961 n = CHILD(n, 0);
2962 goto loop;
2963 }
2964 else {
2965 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002966 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002967 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002968 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 if (!ops)
2970 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002971 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 return NULL;
2974 }
2975 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002976 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002978 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002979 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002981 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982
2983 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002984 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002986 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002988 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 asdl_seq_SET(cmps, i / 2, expression);
2990 }
2991 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002992 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002994 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002996 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2997 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999
Guido van Rossum0368b722007-05-11 16:50:42 +00003000 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 /* The next five cases all handle BinOps. The main body of code
3003 is the same in each case, but the switch turned inside out to
3004 reuse the code for each type of operator.
3005 */
3006 case expr:
3007 case xor_expr:
3008 case and_expr:
3009 case shift_expr:
3010 case arith_expr:
3011 case term:
3012 if (NCH(n) == 1) {
3013 n = CHILD(n, 0);
3014 goto loop;
3015 }
3016 return ast_for_binop(c, n);
3017 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003018 node *an = NULL;
3019 node *en = NULL;
3020 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003021 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003022 if (NCH(n) > 1)
3023 an = CHILD(n, 1); /* yield_arg */
3024 if (an) {
3025 en = CHILD(an, NCH(an) - 1);
3026 if (NCH(an) == 2) {
3027 is_from = 1;
3028 exp = ast_for_expr(c, en);
3029 }
3030 else
3031 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003032 if (!exp)
3033 return NULL;
3034 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05003035 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003036 return YieldFrom(exp, LINENO(n), n->n_col_offset,
3037 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
3038 return Yield(exp, LINENO(n), n->n_col_offset,
3039 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003040 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003041 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 if (NCH(n) == 1) {
3043 n = CHILD(n, 0);
3044 goto loop;
3045 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003046 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00003047 case power:
3048 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003050 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 return NULL;
3052 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003053 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 return NULL;
3055}
3056
3057static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02003058ast_for_call(struct compiling *c, const node *n, expr_ty func,
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08003059 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060{
3061 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003062 arglist: argument (',' argument)* [',']
3063 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 */
3065
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003066 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003067 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003068 asdl_seq *args;
3069 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070
3071 REQ(n, arglist);
3072
3073 nargs = 0;
3074 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003076 node *ch = CHILD(n, i);
3077 if (TYPE(ch) == argument) {
3078 if (NCH(ch) == 1)
3079 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003080 else if (TYPE(CHILD(ch, 1)) == comp_for) {
3081 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02003082 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003083 ast_error(c, ch, "invalid syntax");
3084 return NULL;
3085 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003086 if (NCH(n) > 1) {
3087 ast_error(c, ch, "Generator expression must be parenthesized");
3088 return NULL;
3089 }
3090 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003091 else if (TYPE(CHILD(ch, 0)) == STAR)
3092 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003093 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3094 nargs++;
3095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003097 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003098 nkeywords++;
3099 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003102 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003104 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003105 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003107 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003108
3109 nargs = 0; /* positional arguments + iterable argument unpackings */
3110 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3111 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003113 node *ch = CHILD(n, i);
3114 if (TYPE(ch) == argument) {
3115 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003116 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003117 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003118 /* a positional argument */
3119 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003120 if (ndoublestars) {
3121 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003122 "positional argument follows "
3123 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003124 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003125 else {
3126 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003127 "positional argument follows "
3128 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003129 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003130 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003131 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003132 e = ast_for_expr(c, chch);
3133 if (!e)
3134 return NULL;
3135 asdl_seq_SET(args, nargs++, e);
3136 }
3137 else if (TYPE(chch) == STAR) {
3138 /* an iterable argument unpacking */
3139 expr_ty starred;
3140 if (ndoublestars) {
3141 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003142 "iterable argument unpacking follows "
3143 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003144 return NULL;
3145 }
3146 e = ast_for_expr(c, CHILD(ch, 1));
3147 if (!e)
3148 return NULL;
3149 starred = Starred(e, Load, LINENO(chch),
3150 chch->n_col_offset,
Pablo Galindob1f20442019-12-18 01:41:58 +00003151 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003152 c->c_arena);
3153 if (!starred)
3154 return NULL;
3155 asdl_seq_SET(args, nargs++, starred);
3156
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003157 }
3158 else if (TYPE(chch) == DOUBLESTAR) {
3159 /* a keyword argument unpacking */
3160 keyword_ty kw;
3161 i++;
3162 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003164 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003165 kw = keyword(NULL, e, c->c_arena);
3166 asdl_seq_SET(keywords, nkeywords++, kw);
3167 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003169 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003170 /* the lone generator expression */
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08003171 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003173 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003174 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003176 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3177 /* treat colon equal as positional argument */
3178 if (nkeywords) {
3179 if (ndoublestars) {
3180 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003181 "positional argument follows "
3182 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003183 }
3184 else {
3185 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003186 "positional argument follows "
3187 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003188 }
3189 return NULL;
3190 }
3191 e = ast_for_namedexpr(c, ch);
3192 if (!e)
3193 return NULL;
3194 asdl_seq_SET(args, nargs++, e);
3195 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003197 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003198 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003199 identifier key, tmp;
3200 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003202 // To remain LL(1), the grammar accepts any test (basically, any
3203 // expression) in the keyword slot of a call site. So, we need
3204 // to manually enforce that the keyword is a NAME here.
3205 static const int name_tree[] = {
3206 test,
3207 or_test,
3208 and_test,
3209 not_test,
3210 comparison,
3211 expr,
3212 xor_expr,
3213 and_expr,
3214 shift_expr,
3215 arith_expr,
3216 term,
3217 factor,
3218 power,
3219 atom_expr,
3220 atom,
3221 0,
3222 };
3223 node *expr_node = chch;
3224 for (int i = 0; name_tree[i]; i++) {
3225 if (TYPE(expr_node) != name_tree[i])
3226 break;
3227 if (NCH(expr_node) != 1)
3228 break;
3229 expr_node = CHILD(expr_node, 0);
3230 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003231 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003232 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003233 "expression cannot contain assignment, "
3234 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003235 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003236 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003237 key = new_identifier(STR(expr_node), c);
3238 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003239 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003241 if (forbidden_name(c, key, chch, 1)) {
3242 return NULL;
3243 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003244 for (k = 0; k < nkeywords; k++) {
3245 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003246 if (tmp && !PyUnicode_Compare(tmp, key)) {
3247 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003248 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003249 return NULL;
3250 }
3251 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003252 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003254 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003255 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003257 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003258 asdl_seq_SET(keywords, nkeywords++, kw);
3259 }
3260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 }
3262
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08003263 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003264 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265}
3266
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003268ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003270 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003271 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003273 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003274 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003275 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003276 }
3277 else {
3278 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003279 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003282 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283 else {
3284 asdl_seq *tmp = seq_for_testlist(c, n);
3285 if (!tmp)
3286 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003287 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003290}
3291
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292static stmt_ty
3293ast_for_expr_stmt(struct compiling *c, const node *n)
3294{
3295 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003296 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003297 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3298 annassign: ':' test ['=' (yield_expr|testlist)]
3299 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3300 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3301 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003302 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003304 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003306 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 if (!e)
3309 return NULL;
3310
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003311 return Expr(e, LINENO(n), n->n_col_offset,
3312 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 }
3314 else if (TYPE(CHILD(n, 1)) == augassign) {
3315 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003316 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318
Thomas Wouters89f507f2006-12-13 04:49:30 +00003319 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320 if (!expr1)
3321 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003322 if(!set_context(c, expr1, Store, ch))
3323 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003324 /* set_context checks that most expressions are not the left side.
3325 Augmented assignments can only have a name, a subscript, or an
3326 attribute on the left, though, so we have to explicitly check for
3327 those. */
3328 switch (expr1->kind) {
3329 case Name_kind:
3330 case Attribute_kind:
3331 case Subscript_kind:
3332 break;
3333 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003334 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003335 return NULL;
3336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 ch = CHILD(n, 2);
3339 if (TYPE(ch) == testlist)
3340 expr2 = ast_for_testlist(c, ch);
3341 else
3342 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003343 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344 return NULL;
3345
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003346 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003347 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 return NULL;
3349
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003350 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3351 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003353 else if (TYPE(CHILD(n, 1)) == annassign) {
3354 expr_ty expr1, expr2, expr3;
3355 node *ch = CHILD(n, 0);
3356 node *deep, *ann = CHILD(n, 1);
3357 int simple = 1;
3358
Guido van Rossum495da292019-03-07 12:38:08 -08003359 /* AnnAssigns are only allowed in Python 3.6 or greater */
3360 if (c->c_feature_version < 6) {
3361 ast_error(c, ch,
3362 "Variable annotation syntax is only supported in Python 3.6 and greater");
3363 return NULL;
3364 }
3365
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003366 /* we keep track of parens to qualify (x) as expression not name */
3367 deep = ch;
3368 while (NCH(deep) == 1) {
3369 deep = CHILD(deep, 0);
3370 }
3371 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3372 simple = 0;
3373 }
3374 expr1 = ast_for_testlist(c, ch);
3375 if (!expr1) {
3376 return NULL;
3377 }
3378 switch (expr1->kind) {
3379 case Name_kind:
3380 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3381 return NULL;
3382 }
3383 expr1->v.Name.ctx = Store;
3384 break;
3385 case Attribute_kind:
3386 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3387 return NULL;
3388 }
3389 expr1->v.Attribute.ctx = Store;
3390 break;
3391 case Subscript_kind:
3392 expr1->v.Subscript.ctx = Store;
3393 break;
3394 case List_kind:
3395 ast_error(c, ch,
3396 "only single target (not list) can be annotated");
3397 return NULL;
3398 case Tuple_kind:
3399 ast_error(c, ch,
3400 "only single target (not tuple) can be annotated");
3401 return NULL;
3402 default:
3403 ast_error(c, ch,
3404 "illegal target for annotation");
3405 return NULL;
3406 }
3407
3408 if (expr1->kind != Name_kind) {
3409 simple = 0;
3410 }
3411 ch = CHILD(ann, 1);
3412 expr2 = ast_for_expr(c, ch);
3413 if (!expr2) {
3414 return NULL;
3415 }
3416 if (NCH(ann) == 2) {
3417 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003418 LINENO(n), n->n_col_offset,
3419 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003420 }
3421 else {
3422 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003423 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003424 expr3 = ast_for_testlist(c, ch);
3425 }
3426 else {
3427 expr3 = ast_for_expr(c, ch);
3428 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003429 if (!expr3) {
3430 return NULL;
3431 }
3432 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003433 LINENO(n), n->n_col_offset,
3434 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003435 }
3436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003438 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003439 asdl_seq *targets;
3440 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003442 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443
Thomas Wouters89f507f2006-12-13 04:49:30 +00003444 /* a normal assignment */
3445 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003446
3447 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3448 nch_minus_type = num - has_type_comment;
3449
3450 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003451 if (!targets)
3452 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003453 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003454 expr_ty e;
3455 node *ch = CHILD(n, i);
3456 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003457 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003458 return NULL;
3459 }
3460 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003462 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003464 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003465 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467
Thomas Wouters89f507f2006-12-13 04:49:30 +00003468 asdl_seq_SET(targets, i / 2, e);
3469 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003470 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003471 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003472 expression = ast_for_testlist(c, value);
3473 else
3474 expression = ast_for_expr(c, value);
3475 if (!expression)
3476 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003477 if (has_type_comment) {
3478 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3479 if (!type_comment)
3480 return NULL;
3481 }
3482 else
3483 type_comment = NULL;
3484 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003485 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487}
3488
Benjamin Peterson78565b22009-06-28 19:19:51 +00003489
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003491ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492{
3493 asdl_seq *seq;
3494 int i;
3495 expr_ty e;
3496
3497 REQ(n, exprlist);
3498
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003499 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003503 e = ast_for_expr(c, CHILD(n, i));
3504 if (!e)
3505 return NULL;
3506 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003507 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 }
3510 return seq;
3511}
3512
3513static stmt_ty
3514ast_for_del_stmt(struct compiling *c, const node *n)
3515{
3516 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518 /* del_stmt: 'del' exprlist */
3519 REQ(n, del_stmt);
3520
3521 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3522 if (!expr_list)
3523 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003524 return Delete(expr_list, LINENO(n), n->n_col_offset,
3525 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
3528static stmt_ty
3529ast_for_flow_stmt(struct compiling *c, const node *n)
3530{
3531 /*
3532 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3533 | yield_stmt
3534 break_stmt: 'break'
3535 continue_stmt: 'continue'
3536 return_stmt: 'return' [testlist]
3537 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003538 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 raise_stmt: 'raise' [test [',' test [',' test]]]
3540 */
3541 node *ch;
3542
3543 REQ(n, flow_stmt);
3544 ch = CHILD(n, 0);
3545 switch (TYPE(ch)) {
3546 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003547 return Break(LINENO(n), n->n_col_offset,
3548 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003550 return Continue(LINENO(n), n->n_col_offset,
3551 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003553 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3554 if (!exp)
3555 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003556 return Expr(exp, LINENO(n), n->n_col_offset,
3557 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 }
3559 case return_stmt:
3560 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003561 return Return(NULL, LINENO(n), n->n_col_offset,
3562 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003564 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 if (!expression)
3566 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003567 return Return(expression, LINENO(n), n->n_col_offset,
3568 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 }
3570 case raise_stmt:
3571 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003572 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3573 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003574 else if (NCH(ch) >= 2) {
3575 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3577 if (!expression)
3578 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003579 if (NCH(ch) == 4) {
3580 cause = ast_for_expr(c, CHILD(ch, 3));
3581 if (!cause)
3582 return NULL;
3583 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003584 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3585 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 }
Stefan Krahf432a322017-08-21 13:09:59 +02003587 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003589 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 "unexpected flow_stmt: %d", TYPE(ch));
3591 return NULL;
3592 }
3593}
3594
3595static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003596alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597{
3598 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003599 import_as_name: NAME ['as' NAME]
3600 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 dotted_name: NAME ('.' NAME)*
3602 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003603 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003604
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 loop:
3606 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003607 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003608 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003609 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003610 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003611 if (!name)
3612 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003613 if (NCH(n) == 3) {
3614 node *str_node = CHILD(n, 2);
3615 str = NEW_IDENTIFIER(str_node);
3616 if (!str)
3617 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003618 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003619 return NULL;
3620 }
3621 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003622 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003623 return NULL;
3624 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003625 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003626 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 case dotted_as_name:
3628 if (NCH(n) == 1) {
3629 n = CHILD(n, 0);
3630 goto loop;
3631 }
3632 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003633 node *asname_node = CHILD(n, 2);
3634 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003635 if (!a)
3636 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003638 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003639 if (!a->asname)
3640 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003641 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 return a;
3644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003646 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003647 node *name_node = CHILD(n, 0);
3648 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003649 if (!name)
3650 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003651 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003652 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003653 return alias(name, NULL, c->c_arena);
3654 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 else {
3656 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003657 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003658 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661
3662 len = 0;
3663 for (i = 0; i < NCH(n); i += 2)
3664 /* length of string plus one for the dot */
3665 len += strlen(STR(CHILD(n, i))) + 1;
3666 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003667 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668 if (!str)
3669 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003670 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 if (!s)
3672 return NULL;
3673 for (i = 0; i < NCH(n); i += 2) {
3674 char *sch = STR(CHILD(n, i));
3675 strcpy(s, STR(CHILD(n, i)));
3676 s += strlen(sch);
3677 *s++ = '.';
3678 }
3679 --s;
3680 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3682 PyBytes_GET_SIZE(str),
3683 NULL);
3684 Py_DECREF(str);
3685 if (!uni)
3686 return NULL;
3687 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003688 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003689 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3690 Py_DECREF(str);
3691 return NULL;
3692 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003693 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003696 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003697 if (!str)
3698 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003699 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3700 Py_DECREF(str);
3701 return NULL;
3702 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003703 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003705 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 "unexpected import name: %d", TYPE(n));
3707 return NULL;
3708 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003709
3710 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 return NULL;
3712}
3713
3714static stmt_ty
3715ast_for_import_stmt(struct compiling *c, const node *n)
3716{
3717 /*
3718 import_stmt: import_name | import_from
3719 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003720 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3721 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003723 int lineno;
3724 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 int i;
3726 asdl_seq *aliases;
3727
3728 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003729 lineno = LINENO(n);
3730 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003732 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003734 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003735 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003736 if (!aliases)
3737 return NULL;
3738 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003739 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003740 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003742 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003744 // Even though n is modified above, the end position is not changed
3745 return Import(aliases, lineno, col_offset,
3746 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003748 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003750 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003751 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003752 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003753 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003755 /* Count the number of dots (for relative imports) and check for the
3756 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003757 for (idx = 1; idx < NCH(n); idx++) {
3758 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003759 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3760 if (!mod)
3761 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003762 idx++;
3763 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003764 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003766 ndots += 3;
3767 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003768 } else if (TYPE(CHILD(n, idx)) != DOT) {
3769 break;
3770 }
3771 ndots++;
3772 }
3773 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003774 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003775 case STAR:
3776 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003777 n = CHILD(n, idx);
3778 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003779 break;
3780 case LPAR:
3781 /* from ... import (x, y, z) */
3782 n = CHILD(n, idx + 1);
3783 n_children = NCH(n);
3784 break;
3785 case import_as_names:
3786 /* from ... import x, y, z */
3787 n = CHILD(n, idx);
3788 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003789 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003790 ast_error(c, n,
3791 "trailing comma not allowed without"
3792 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 return NULL;
3794 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003795 break;
3796 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003797 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003798 return NULL;
3799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003801 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003802 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804
3805 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003806 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003807 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003808 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003810 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003812 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003813 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003814 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003815 if (!import_alias)
3816 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003817 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003820 if (mod != NULL)
3821 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003822 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003823 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003824 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 }
Neal Norwitz79792652005-11-14 04:25:03 +00003826 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 "unknown import statement: starts with command '%s'",
3828 STR(CHILD(n, 0)));
3829 return NULL;
3830}
3831
3832static stmt_ty
3833ast_for_global_stmt(struct compiling *c, const node *n)
3834{
3835 /* global_stmt: 'global' NAME (',' NAME)* */
3836 identifier name;
3837 asdl_seq *s;
3838 int i;
3839
3840 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003841 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003845 name = NEW_IDENTIFIER(CHILD(n, i));
3846 if (!name)
3847 return NULL;
3848 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003850 return Global(s, LINENO(n), n->n_col_offset,
3851 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852}
3853
3854static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003855ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3856{
3857 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3858 identifier name;
3859 asdl_seq *s;
3860 int i;
3861
3862 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003863 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003864 if (!s)
3865 return NULL;
3866 for (i = 1; i < NCH(n); i += 2) {
3867 name = NEW_IDENTIFIER(CHILD(n, i));
3868 if (!name)
3869 return NULL;
3870 asdl_seq_SET(s, i / 2, name);
3871 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003872 return Nonlocal(s, LINENO(n), n->n_col_offset,
3873 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003874}
3875
3876static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877ast_for_assert_stmt(struct compiling *c, const node *n)
3878{
3879 /* assert_stmt: 'assert' test [',' test] */
3880 REQ(n, assert_stmt);
3881 if (NCH(n) == 2) {
3882 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3883 if (!expression)
3884 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003885 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3886 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 }
3888 else if (NCH(n) == 4) {
3889 expr_ty expr1, expr2;
3890
3891 expr1 = ast_for_expr(c, CHILD(n, 1));
3892 if (!expr1)
3893 return NULL;
3894 expr2 = ast_for_expr(c, CHILD(n, 3));
3895 if (!expr2)
3896 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003898 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3899 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 }
Neal Norwitz79792652005-11-14 04:25:03 +00003901 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003902 "improper number of parts to 'assert' statement: %d",
3903 NCH(n));
3904 return NULL;
3905}
3906
3907static asdl_seq *
3908ast_for_suite(struct compiling *c, const node *n)
3909{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003910 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003911 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912 stmt_ty s;
3913 int i, total, num, end, pos = 0;
3914 node *ch;
3915
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003916 if (TYPE(n) != func_body_suite) {
3917 REQ(n, suite);
3918 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919
3920 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003921 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003925 n = CHILD(n, 0);
3926 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 */
3929 end = NCH(n) - 1;
3930 if (TYPE(CHILD(n, end - 1)) == SEMI)
3931 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003933 for (i = 0; i < end; i += 2) {
3934 ch = CHILD(n, i);
3935 s = ast_for_stmt(c, ch);
3936 if (!s)
3937 return NULL;
3938 asdl_seq_SET(seq, pos++, s);
3939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 }
3941 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003942 i = 2;
3943 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3944 i += 2;
3945 REQ(CHILD(n, 2), NEWLINE);
3946 }
3947
3948 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003949 ch = CHILD(n, i);
3950 REQ(ch, stmt);
3951 num = num_stmts(ch);
3952 if (num == 1) {
3953 /* small_stmt or compound_stmt with only one child */
3954 s = ast_for_stmt(c, ch);
3955 if (!s)
3956 return NULL;
3957 asdl_seq_SET(seq, pos++, s);
3958 }
3959 else {
3960 int j;
3961 ch = CHILD(ch, 0);
3962 REQ(ch, simple_stmt);
3963 for (j = 0; j < NCH(ch); j += 2) {
3964 /* statement terminates with a semi-colon ';' */
3965 if (NCH(CHILD(ch, j)) == 0) {
3966 assert((j + 1) == NCH(ch));
3967 break;
3968 }
3969 s = ast_for_stmt(c, CHILD(ch, j));
3970 if (!s)
3971 return NULL;
3972 asdl_seq_SET(seq, pos++, s);
3973 }
3974 }
3975 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 }
3977 assert(pos == seq->size);
3978 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979}
3980
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003981static void
3982get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3983{
Pablo Galindo46a97922019-02-19 22:51:53 +00003984 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003985 // There must be no empty suites.
3986 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003987 stmt_ty last = asdl_seq_GET(s, tot - 1);
3988 *end_lineno = last->end_lineno;
3989 *end_col_offset = last->end_col_offset;
3990}
3991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992static stmt_ty
3993ast_for_if_stmt(struct compiling *c, const node *n)
3994{
3995 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3996 ['else' ':' suite]
3997 */
3998 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003999 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000
4001 REQ(n, if_stmt);
4002
4003 if (NCH(n) == 4) {
4004 expr_ty expression;
4005 asdl_seq *suite_seq;
4006
4007 expression = ast_for_expr(c, CHILD(n, 1));
4008 if (!expression)
4009 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004011 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004013 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014
Guido van Rossumd8faa362007-04-27 19:54:29 +00004015 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004016 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 s = STR(CHILD(n, 4));
4020 /* s[2], the third character in the string, will be
4021 's' for el_s_e, or
4022 'i' for el_i_f
4023 */
4024 if (s[2] == 's') {
4025 expr_ty expression;
4026 asdl_seq *seq1, *seq2;
4027
4028 expression = ast_for_expr(c, CHILD(n, 1));
4029 if (!expression)
4030 return NULL;
4031 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004032 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033 return NULL;
4034 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004035 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004037 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038
Guido van Rossumd8faa362007-04-27 19:54:29 +00004039 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004040 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 }
4042 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004043 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004044 expr_ty expression;
4045 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004046 asdl_seq *orelse = NULL;
4047 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048 /* must reference the child n_elif+1 since 'else' token is third,
4049 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004050 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
4051 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
4052 has_else = 1;
4053 n_elif -= 3;
4054 }
4055 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056
Thomas Wouters89f507f2006-12-13 04:49:30 +00004057 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004060 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004061 if (!orelse)
4062 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004064 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004066 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
4067 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004069 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4070 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004072 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 asdl_seq_SET(orelse, 0,
4075 If(expression, suite_seq, suite_seq2,
Miss Islington (bot)ce333cd2019-12-14 02:43:42 -08004076 LINENO(CHILD(n, NCH(n) - 7)),
4077 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004078 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004079 /* the just-created orelse handled the last elif */
4080 n_elif--;
4081 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082
Thomas Wouters89f507f2006-12-13 04:49:30 +00004083 for (i = 0; i < n_elif; i++) {
4084 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004085 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004086 if (!newobj)
4087 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004088 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004089 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004092 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004095 if (orelse != NULL) {
4096 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4097 } else {
4098 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4099 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004100 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 If(expression, suite_seq, orelse,
Miss Islington (bot)3b18b172019-12-13 08:21:54 -08004102 LINENO(CHILD(n, off - 1)),
4103 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004104 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004105 orelse = newobj;
4106 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004107 expression = ast_for_expr(c, CHILD(n, 1));
4108 if (!expression)
4109 return NULL;
4110 suite_seq = ast_for_suite(c, CHILD(n, 3));
4111 if (!suite_seq)
4112 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004113 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004114 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004115 LINENO(n), n->n_col_offset,
4116 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004118
4119 PyErr_Format(PyExc_SystemError,
4120 "unexpected token in 'if' statement: %s", s);
4121 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122}
4123
4124static stmt_ty
4125ast_for_while_stmt(struct compiling *c, const node *n)
4126{
4127 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4128 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004129 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130
4131 if (NCH(n) == 4) {
4132 expr_ty expression;
4133 asdl_seq *suite_seq;
4134
4135 expression = ast_for_expr(c, CHILD(n, 1));
4136 if (!expression)
4137 return NULL;
4138 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004139 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004141 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4142 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4143 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144 }
4145 else if (NCH(n) == 7) {
4146 expr_ty expression;
4147 asdl_seq *seq1, *seq2;
4148
4149 expression = ast_for_expr(c, CHILD(n, 1));
4150 if (!expression)
4151 return NULL;
4152 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004153 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154 return NULL;
4155 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004156 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004158 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4161 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004163
4164 PyErr_Format(PyExc_SystemError,
4165 "wrong number of tokens for 'while' statement: %d",
4166 NCH(n));
4167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004168}
4169
4170static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004171ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172{
guoci90fc8982018-09-11 17:45:45 -04004173 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004174 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004176 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004177 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004178 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004179 int has_type_comment;
4180 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004181
4182 if (is_async && c->c_feature_version < 5) {
4183 ast_error(c, n,
4184 "Async for loops are only supported in Python 3.5 and greater");
4185 return NULL;
4186 }
4187
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004188 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 REQ(n, for_stmt);
4190
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004191 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4192
4193 if (NCH(n) == 9 + has_type_comment) {
4194 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 if (!seq)
4196 return NULL;
4197 }
4198
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004199 node_target = CHILD(n, 1);
4200 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004201 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004202 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004203 /* Check the # of children rather than the length of _target, since
4204 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004205 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004206 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004207 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004208 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004209 target = Tuple(_target, Store, first->lineno, first->col_offset,
4210 node_target->n_end_lineno, node_target->n_end_col_offset,
4211 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004212
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004213 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004214 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004215 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004216 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004217 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004218 return NULL;
4219
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004220 if (seq != NULL) {
4221 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4222 } else {
4223 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4224 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004225
4226 if (has_type_comment) {
4227 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4228 if (!type_comment)
4229 return NULL;
4230 }
4231 else
4232 type_comment = NULL;
4233
Yury Selivanov75445082015-05-11 22:57:16 -04004234 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004235 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004236 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004237 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004238 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004239 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004240 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004241 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004242}
4243
4244static excepthandler_ty
4245ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4246{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004247 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004248 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249 REQ(exc, except_clause);
4250 REQ(body, suite);
4251
4252 if (NCH(exc) == 1) {
4253 asdl_seq *suite_seq = ast_for_suite(c, body);
4254 if (!suite_seq)
4255 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004256 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004257
Neal Norwitzad74aa82008-03-31 05:14:30 +00004258 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004259 exc->n_col_offset,
4260 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004261 }
4262 else if (NCH(exc) == 2) {
4263 expr_ty expression;
4264 asdl_seq *suite_seq;
4265
4266 expression = ast_for_expr(c, CHILD(exc, 1));
4267 if (!expression)
4268 return NULL;
4269 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004270 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004271 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004272 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004273
Neal Norwitzad74aa82008-03-31 05:14:30 +00004274 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004275 exc->n_col_offset,
4276 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004277 }
4278 else if (NCH(exc) == 4) {
4279 asdl_seq *suite_seq;
4280 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004281 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004282 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004283 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004284 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004285 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004286 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004287 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004288 return NULL;
4289 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004290 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004291 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004292 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004293
Neal Norwitzad74aa82008-03-31 05:14:30 +00004294 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004295 exc->n_col_offset,
4296 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004297 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004298
4299 PyErr_Format(PyExc_SystemError,
4300 "wrong number of children for 'except' clause: %d",
4301 NCH(exc));
4302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004303}
4304
4305static stmt_ty
4306ast_for_try_stmt(struct compiling *c, const node *n)
4307{
Neal Norwitzf599f422005-12-17 21:33:47 +00004308 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004309 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004310 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004311 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004312
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004313 REQ(n, try_stmt);
4314
Neal Norwitzf599f422005-12-17 21:33:47 +00004315 body = ast_for_suite(c, CHILD(n, 2));
4316 if (body == NULL)
4317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004318
Neal Norwitzf599f422005-12-17 21:33:47 +00004319 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4320 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4321 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4322 /* we can assume it's an "else",
4323 because nch >= 9 for try-else-finally and
4324 it would otherwise have a type of except_clause */
4325 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4326 if (orelse == NULL)
4327 return NULL;
4328 n_except--;
4329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004330
Neal Norwitzf599f422005-12-17 21:33:47 +00004331 finally = ast_for_suite(c, CHILD(n, nch - 1));
4332 if (finally == NULL)
4333 return NULL;
4334 n_except--;
4335 }
4336 else {
4337 /* we can assume it's an "else",
4338 otherwise it would have a type of except_clause */
4339 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4340 if (orelse == NULL)
4341 return NULL;
4342 n_except--;
4343 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004344 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004345 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004346 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004347 return NULL;
4348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349
Neal Norwitzf599f422005-12-17 21:33:47 +00004350 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004351 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004352 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004353 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004354 if (handlers == NULL)
4355 return NULL;
4356
4357 for (i = 0; i < n_except; i++) {
4358 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4359 CHILD(n, 5 + i * 3));
4360 if (!e)
4361 return NULL;
4362 asdl_seq_SET(handlers, i, e);
4363 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004364 }
4365
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004366 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004367 if (finally != NULL) {
4368 // finally is always last
4369 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4370 } else if (orelse != NULL) {
4371 // otherwise else is last
4372 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4373 } else {
4374 // inline the get_last_end_pos logic due to layout mismatch
4375 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4376 end_lineno = last_handler->end_lineno;
4377 end_col_offset = last_handler->end_col_offset;
4378 }
4379 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4380 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004381}
4382
Georg Brandl0c315622009-05-25 21:10:36 +00004383/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004384static withitem_ty
4385ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004386{
4387 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004388
Georg Brandl0c315622009-05-25 21:10:36 +00004389 REQ(n, with_item);
4390 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004391 if (!context_expr)
4392 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004393 if (NCH(n) == 3) {
4394 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004395
4396 if (!optional_vars) {
4397 return NULL;
4398 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004399 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004400 return NULL;
4401 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004402 }
4403
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004404 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004405}
4406
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004407/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004408static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004409ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004410{
guoci90fc8982018-09-11 17:45:45 -04004411 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004412 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004413 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004414 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004415
Guido van Rossum495da292019-03-07 12:38:08 -08004416 if (is_async && c->c_feature_version < 5) {
4417 ast_error(c, n,
4418 "Async with statements are only supported in Python 3.5 and greater");
4419 return NULL;
4420 }
4421
Georg Brandl0c315622009-05-25 21:10:36 +00004422 REQ(n, with_stmt);
4423
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004424 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4425 nch_minus_type = NCH(n) - has_type_comment;
4426
4427 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004428 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004429 if (!items)
4430 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004431 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004432 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4433 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004434 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004435 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004436 }
4437
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004438 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4439 if (!body)
4440 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004441 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004442
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004443 if (has_type_comment) {
4444 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4445 if (!type_comment)
4446 return NULL;
4447 }
4448 else
4449 type_comment = NULL;
4450
Yury Selivanov75445082015-05-11 22:57:16 -04004451 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004452 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004453 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004454 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004455 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004456 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004457}
4458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004460ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004462 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004463 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004464 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004465 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004466 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004467
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004468 REQ(n, classdef);
4469
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004470 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004471 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004472 if (!s)
4473 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004474 get_last_end_pos(s, &end_lineno, &end_col_offset);
4475
Benjamin Peterson30760062008-11-25 04:02:28 +00004476 classname = NEW_IDENTIFIER(CHILD(n, 1));
4477 if (!classname)
4478 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004479 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004480 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004481 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004482 LINENO(n), n->n_col_offset,
4483 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004484 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004485
4486 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004487 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004488 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004489 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004490 get_last_end_pos(s, &end_lineno, &end_col_offset);
4491
Benjamin Peterson30760062008-11-25 04:02:28 +00004492 classname = NEW_IDENTIFIER(CHILD(n, 1));
4493 if (!classname)
4494 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004495 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004496 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004497 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004498 LINENO(n), n->n_col_offset,
4499 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004500 }
4501
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004502 /* class NAME '(' arglist ')' ':' suite */
4503 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004504 {
4505 PyObject *dummy_name;
4506 expr_ty dummy;
4507 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4508 if (!dummy_name)
4509 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004510 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4511 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4512 c->c_arena);
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08004513 call = ast_for_call(c, CHILD(n, 3), dummy,
4514 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004515 if (!call)
4516 return NULL;
4517 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004518 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004519 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004520 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004521 get_last_end_pos(s, &end_lineno, &end_col_offset);
4522
Benjamin Peterson30760062008-11-25 04:02:28 +00004523 classname = NEW_IDENTIFIER(CHILD(n, 1));
4524 if (!classname)
4525 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004526 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004527 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004528
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004529 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004530 decorator_seq, LINENO(n), n->n_col_offset,
4531 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004532}
4533
4534static stmt_ty
4535ast_for_stmt(struct compiling *c, const node *n)
4536{
4537 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004538 assert(NCH(n) == 1);
4539 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540 }
4541 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004542 assert(num_stmts(n) == 1);
4543 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004544 }
4545 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004546 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004547 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4548 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004549 */
4550 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004551 case expr_stmt:
4552 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004553 case del_stmt:
4554 return ast_for_del_stmt(c, n);
4555 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004556 return Pass(LINENO(n), n->n_col_offset,
4557 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004558 case flow_stmt:
4559 return ast_for_flow_stmt(c, n);
4560 case import_stmt:
4561 return ast_for_import_stmt(c, n);
4562 case global_stmt:
4563 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004564 case nonlocal_stmt:
4565 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004566 case assert_stmt:
4567 return ast_for_assert_stmt(c, n);
4568 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004569 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004570 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4571 TYPE(n), NCH(n));
4572 return NULL;
4573 }
4574 }
4575 else {
4576 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004577 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004578 */
4579 node *ch = CHILD(n, 0);
4580 REQ(n, compound_stmt);
4581 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004582 case if_stmt:
4583 return ast_for_if_stmt(c, ch);
4584 case while_stmt:
4585 return ast_for_while_stmt(c, ch);
4586 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004587 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004588 case try_stmt:
4589 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004590 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004591 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004592 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004593 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004594 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004595 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 case decorated:
4597 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004598 case async_stmt:
4599 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004600 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004601 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004602 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004603 TYPE(n), NCH(n));
4604 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004606 }
4607}
4608
4609static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004610parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004611{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004612 const char *end;
4613 long x;
4614 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004615 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004616 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004617
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004618 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004619 errno = 0;
4620 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004621 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004622 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004623 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004624 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004625 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004626 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004627 }
4628 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004629 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004630 if (*end == '\0') {
4631 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004632 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004633 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004634 }
4635 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004636 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004637 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004638 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4639 if (compl.imag == -1.0 && PyErr_Occurred())
4640 return NULL;
4641 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004642 }
4643 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004644 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004645 dx = PyOS_string_to_double(s, NULL, NULL);
4646 if (dx == -1.0 && PyErr_Occurred())
4647 return NULL;
4648 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004650}
4651
4652static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004653parsenumber(struct compiling *c, const char *s)
4654{
4655 char *dup, *end;
4656 PyObject *res = NULL;
4657
4658 assert(s != NULL);
4659
4660 if (strchr(s, '_') == NULL) {
4661 return parsenumber_raw(c, s);
4662 }
4663 /* Create a duplicate without underscores. */
4664 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004665 if (dup == NULL) {
4666 return PyErr_NoMemory();
4667 }
Brett Cannona721aba2016-09-09 14:57:09 -07004668 end = dup;
4669 for (; *s; s++) {
4670 if (*s != '_') {
4671 *end++ = *s;
4672 }
4673 }
4674 *end = '\0';
4675 res = parsenumber_raw(c, dup);
4676 PyMem_Free(dup);
4677 return res;
4678}
4679
4680static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004681decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004682{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004683 const char *s, *t;
4684 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004685 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4686 while (s < end && (*s & 0x80)) s++;
4687 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004688 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004689}
4690
Eric V. Smith56466482016-10-31 14:46:26 -04004691static int
4692warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004693 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004694{
4695 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4696 first_invalid_escape_char);
4697 if (msg == NULL) {
4698 return -1;
4699 }
Serhiy Storchaka4c5b6ba2019-08-10 01:34:22 +03004700 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004701 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004702 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004703 {
Serhiy Storchaka4c5b6ba2019-08-10 01:34:22 +03004704 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4705 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004706 to get a more accurate error report */
4707 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004708 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004709 }
4710 Py_DECREF(msg);
4711 return -1;
4712 }
4713 Py_DECREF(msg);
4714 return 0;
4715}
4716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004717static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004718decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4719 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004720{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004721 PyObject *v, *u;
4722 char *buf;
4723 char *p;
4724 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004725
Benjamin Peterson202803a2016-02-25 22:34:45 -08004726 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004727 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004728 return NULL;
4729 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4730 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4731 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4732 if (u == NULL)
4733 return NULL;
4734 p = buf = PyBytes_AsString(u);
4735 end = s + len;
4736 while (s < end) {
4737 if (*s == '\\') {
4738 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004739 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004740 strcpy(p, "u005c");
4741 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004742 if (s >= end)
4743 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004744 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004745 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004746 if (*s & 0x80) { /* XXX inefficient */
4747 PyObject *w;
4748 int kind;
4749 void *data;
4750 Py_ssize_t len, i;
4751 w = decode_utf8(c, &s, end);
4752 if (w == NULL) {
4753 Py_DECREF(u);
4754 return NULL;
4755 }
4756 kind = PyUnicode_KIND(w);
4757 data = PyUnicode_DATA(w);
4758 len = PyUnicode_GET_LENGTH(w);
4759 for (i = 0; i < len; i++) {
4760 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4761 sprintf(p, "\\U%08x", chr);
4762 p += 10;
4763 }
4764 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004765 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004766 Py_DECREF(w);
4767 } else {
4768 *p++ = *s++;
4769 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004770 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004771 len = p - buf;
4772 s = buf;
4773
Eric V. Smith56466482016-10-31 14:46:26 -04004774 const char *first_invalid_escape;
4775 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4776
4777 if (v != NULL && first_invalid_escape != NULL) {
4778 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4779 /* We have not decref u before because first_invalid_escape points
4780 inside u. */
4781 Py_XDECREF(u);
4782 Py_DECREF(v);
4783 return NULL;
4784 }
4785 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004786 Py_XDECREF(u);
4787 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004788}
4789
Eric V. Smith56466482016-10-31 14:46:26 -04004790static PyObject *
4791decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4792 size_t len)
4793{
4794 const char *first_invalid_escape;
4795 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4796 &first_invalid_escape);
4797 if (result == NULL)
4798 return NULL;
4799
4800 if (first_invalid_escape != NULL) {
4801 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4802 Py_DECREF(result);
4803 return NULL;
4804 }
4805 }
4806 return result;
4807}
4808
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004809/* Shift locations for the given node and all its children by adding `lineno`
4810 and `col_offset` to existing locations. */
4811static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4812{
4813 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004814 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004815 for (int i = 0; i < NCH(n); ++i) {
4816 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4817 /* Shifting column offsets unnecessary if there's been newlines. */
4818 col_offset = 0;
4819 }
4820 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4821 }
4822 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004823 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004824}
4825
4826/* Fix locations for the given node and its children.
4827
4828 `parent` is the enclosing node.
4829 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004830 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004831*/
4832static void
4833fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4834{
4835 char *substr = NULL;
4836 char *start;
4837 int lines = LINENO(parent) - 1;
4838 int cols = parent->n_col_offset;
4839 /* Find the full fstring to fix location information in `n`. */
4840 while (parent && parent->n_type != STRING)
4841 parent = parent->n_child;
4842 if (parent && parent->n_str) {
4843 substr = strstr(parent->n_str, expr_str);
4844 if (substr) {
4845 start = substr;
4846 while (start > parent->n_str) {
4847 if (start[0] == '\n')
4848 break;
4849 start--;
4850 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004851 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004852 /* adjust the start based on the number of newlines encountered
4853 before the f-string expression */
4854 for (char* p = parent->n_str; p < substr; p++) {
4855 if (*p == '\n') {
4856 lines++;
4857 }
4858 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004859 }
4860 }
4861 fstring_shift_node_locations(n, lines, cols);
4862}
4863
Eric V. Smith451d0e32016-09-09 21:56:20 -04004864/* Compile this expression in to an expr_ty. Add parens around the
4865 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004866static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004867fstring_compile_expr(const char *expr_start, const char *expr_end,
4868 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004869
Eric V. Smith235a6f02015-09-19 14:51:32 -04004870{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004871 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004872 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004873 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004874 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004875 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876
Eric V. Smith1d44c412015-09-23 07:49:00 -04004877 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004878 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004879 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4880 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004881
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004882 /* If the substring is all whitespace, it's an error. We need to catch this
4883 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4884 because turning the expression '' in to '()' would go from being invalid
4885 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004886 for (s = expr_start; s != expr_end; s++) {
4887 char c = *s;
4888 /* The Python parser ignores only the following whitespace
4889 characters (\r already is converted to \n). */
4890 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004891 break;
4892 }
4893 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004894 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004895 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004896 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 }
4898
Eric V. Smith451d0e32016-09-09 21:56:20 -04004899 len = expr_end - expr_start;
4900 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4901 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004902 if (str == NULL) {
4903 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004904 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004905 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906
Eric V. Smith451d0e32016-09-09 21:56:20 -04004907 str[0] = '(';
4908 memcpy(str+1, expr_start, len);
4909 str[len+1] = ')';
4910 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004911
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07004912 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004913 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004914 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4915 Py_eval_input, 0);
4916 if (!mod_n) {
4917 PyMem_RawFree(str);
4918 return NULL;
4919 }
4920 /* Reuse str to find the correct column offset. */
4921 str[0] = '{';
4922 str[len+1] = '}';
4923 fstring_fix_node_location(n, mod_n, str);
4924 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004926 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004928 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004930}
4931
4932/* Return -1 on error.
4933
4934 Return 0 if we reached the end of the literal.
4935
4936 Return 1 if we haven't reached the end of the literal, but we want
4937 the caller to process the literal up to this point. Used for
4938 doubled braces.
4939*/
4940static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004941fstring_find_literal(const char **str, const char *end, int raw,
4942 PyObject **literal, int recurse_lvl,
4943 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004945 /* Get any literal string. It ends when we hit an un-doubled left
4946 brace (which isn't part of a unicode name escape such as
4947 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004949 const char *s = *str;
4950 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004951 int result = 0;
4952
Eric V. Smith235a6f02015-09-19 14:51:32 -04004953 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004954 while (s < end) {
4955 char ch = *s++;
4956 if (!raw && ch == '\\' && s < end) {
4957 ch = *s++;
4958 if (ch == 'N') {
4959 if (s < end && *s++ == '{') {
4960 while (s < end && *s++ != '}') {
4961 }
4962 continue;
4963 }
4964 break;
4965 }
4966 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4967 return -1;
4968 }
4969 }
4970 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004971 /* Check for doubled braces, but only at the top level. If
4972 we checked at every level, then f'{0:{3}}' would fail
4973 with the two closing braces. */
4974 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004975 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004976 /* We're going to tell the caller that the literal ends
4977 here, but that they should continue scanning. But also
4978 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004979 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980 result = 1;
4981 goto done;
4982 }
4983
4984 /* Where a single '{' is the start of a new expression, a
4985 single '}' is not allowed. */
4986 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004987 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004988 ast_error(c, n, "f-string: single '}' is not allowed");
4989 return -1;
4990 }
4991 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004992 /* We're either at a '{', which means we're starting another
4993 expression; or a '}', which means we're at the end of this
4994 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004995 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 break;
4997 }
4998 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004999 *str = s;
5000 assert(s <= end);
5001 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005002done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005003 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005004 if (raw)
5005 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005006 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04005007 NULL, NULL);
5008 else
Eric V. Smith56466482016-10-31 14:46:26 -04005009 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005010 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005011 if (!*literal)
5012 return -1;
5013 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014 return result;
5015}
5016
5017/* Forward declaration because parsing is recursive. */
5018static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005019fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005020 struct compiling *c, const node *n);
5021
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005023 expression (so it must be a '{'). Returns the FormattedValue node, which
5024 includes the expression, conversion character, format_spec expression, and
5025 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026
5027 Note that I don't do a perfect job here: I don't make sure that a
5028 closing brace doesn't match an opening paren, for example. It
5029 doesn't need to error on all invalid expressions, just correctly
5030 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005031 will be caught when we parse it later.
5032
5033 *expression is set to the expression. For an '=' "debug" expression,
5034 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005035 including the '=' and any whitespace around it, as a string object). If
5036 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005037static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005038fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005039 PyObject **expr_text, expr_ty *expression,
5040 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005041{
5042 /* Return -1 on error, else 0. */
5043
Eric V. Smith451d0e32016-09-09 21:56:20 -04005044 const char *expr_start;
5045 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 expr_ty simple_expression;
5047 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005048 int conversion = -1; /* The conversion char. Use default if not
5049 specified, or !r if using = and no format
5050 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051
5052 /* 0 if we're not in a string, else the quote char we're trying to
5053 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005054 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005055
5056 /* If we're inside a string, 1=normal, 3=triple-quoted. */
5057 int string_type = 0;
5058
5059 /* Keep track of nesting level for braces/parens/brackets in
5060 expressions. */
5061 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005062 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005064 *expr_text = NULL;
5065
Eric V. Smith235a6f02015-09-19 14:51:32 -04005066 /* Can only nest one level deep. */
5067 if (recurse_lvl >= 2) {
5068 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005069 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005070 }
5071
5072 /* The first char must be a left brace, or we wouldn't have gotten
5073 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005074 assert(**str == '{');
5075 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076
Eric V. Smith451d0e32016-09-09 21:56:20 -04005077 expr_start = *str;
5078 for (; *str < end; (*str)++) {
5079 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005080
5081 /* Loop invariants. */
5082 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005084 if (quote_char)
5085 assert(string_type == 1 || string_type == 3);
5086 else
5087 assert(string_type == 0);
5088
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 ch = **str;
5090 /* Nowhere inside an expression is a backslash allowed. */
5091 if (ch == '\\') {
5092 /* Error: can't include a backslash character, inside
5093 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005094 ast_error(c, n,
5095 "f-string expression part "
5096 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005097 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005098 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 if (quote_char) {
5100 /* We're inside a string. See if we're at the end. */
5101 /* This code needs to implement the same non-error logic
5102 as tok_get from tokenizer.c, at the letter_quote
5103 label. To actually share that code would be a
5104 nightmare. But, it's unlikely to change and is small,
5105 so duplicate it here. Note we don't need to catch all
5106 of the errors, since they'll be caught when parsing the
5107 expression. We just need to match the non-error
5108 cases. Thus we can ignore \n in single-quoted strings,
5109 for example. Or non-terminated strings. */
5110 if (ch == quote_char) {
5111 /* Does this match the string_type (single or triple
5112 quoted)? */
5113 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005114 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 string_type = 0;
5118 quote_char = 0;
5119 continue;
5120 }
5121 } else {
5122 /* We're at the end of a normal string. */
5123 quote_char = 0;
5124 string_type = 0;
5125 continue;
5126 }
5127 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005128 } else if (ch == '\'' || ch == '"') {
5129 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005132 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005133 } else {
5134 /* Start of a normal string. */
5135 string_type = 1;
5136 }
5137 /* Start looking for the end of the string. */
5138 quote_char = ch;
5139 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005140 if (nested_depth >= MAXLEVEL) {
5141 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005142 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005143 }
5144 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005146 } else if (ch == '#') {
5147 /* Error: can't include a comment character, inside parens
5148 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005149 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005150 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005152 (ch == '!' || ch == ':' || ch == '}' ||
5153 ch == '=' || ch == '>' || ch == '<')) {
5154 /* See if there's a next character. */
5155 if (*str+1 < end) {
5156 char next = *(*str+1);
5157
5158 /* For "!=". since '=' is not an allowed conversion character,
5159 nothing is lost in this test. */
5160 if ((ch == '!' && next == '=') || /* != */
5161 (ch == '=' && next == '=') || /* == */
5162 (ch == '<' && next == '=') || /* <= */
5163 (ch == '>' && next == '=') /* >= */
5164 ) {
5165 *str += 1;
5166 continue;
5167 }
5168 /* Don't get out of the loop for these, if they're single
5169 chars (not part of 2-char tokens). If by themselves, they
5170 don't end an expression (unlike say '!'). */
5171 if (ch == '>' || ch == '<') {
5172 continue;
5173 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005174 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005175
Eric V. Smith235a6f02015-09-19 14:51:32 -04005176 /* Normal way out of this loop. */
5177 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005178 } else if (ch == ']' || ch == '}' || ch == ')') {
5179 if (!nested_depth) {
5180 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005181 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005182 }
5183 nested_depth--;
5184 int opening = parenstack[nested_depth];
5185 if (!((opening == '(' && ch == ')') ||
5186 (opening == '[' && ch == ']') ||
5187 (opening == '{' && ch == '}')))
5188 {
5189 ast_error(c, n,
5190 "f-string: closing parenthesis '%c' "
5191 "does not match opening parenthesis '%c'",
5192 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005193 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005194 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005195 } else {
5196 /* Just consume this char and loop around. */
5197 }
5198 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005199 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005200 /* If we leave this loop in a string or with mismatched parens, we
5201 don't care. We'll get a syntax error when compiling the
5202 expression. But, we can produce a better error message, so
5203 let's just do that.*/
5204 if (quote_char) {
5205 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005206 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005207 }
5208 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005209 int opening = parenstack[nested_depth - 1];
5210 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005211 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 }
5213
Eric V. Smith451d0e32016-09-09 21:56:20 -04005214 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005216
5217 /* Compile the expression as soon as possible, so we show errors
5218 related to the expression before errors related to the
5219 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005220 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005221 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005222 goto error;
5223
5224 /* Check for =, which puts the text value of the expression in
5225 expr_text. */
5226 if (**str == '=') {
Shantanuf7ed4d42020-06-06 03:08:48 -07005227 if (c->c_feature_version < 8) {
5228 ast_error(c, n,
5229 "f-string: self documenting expressions are "
5230 "only supported in Python 3.8 and greater");
5231 goto error;
5232 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005233 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005234
5235 /* Skip over ASCII whitespace. No need to test for end of string
5236 here, since we know there's at least a trailing quote somewhere
5237 ahead. */
5238 while (Py_ISSPACE(**str)) {
5239 *str += 1;
5240 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005241
5242 /* Set *expr_text to the text of the expression. */
5243 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5244 if (!*expr_text) {
5245 goto error;
5246 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005247 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005248
5249 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005250 if (**str == '!') {
5251 *str += 1;
5252 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005253 goto unexpected_end_of_string;
5254
Eric V. Smith451d0e32016-09-09 21:56:20 -04005255 conversion = **str;
5256 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005257
5258 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005259 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005260 ast_error(c, n,
5261 "f-string: invalid conversion character: "
5262 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005263 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005264 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005265
5266 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005267
5268 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005269 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005270 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005271 if (**str == ':') {
5272 *str += 1;
5273 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005274 goto unexpected_end_of_string;
5275
5276 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005277 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005278 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005279 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005280 }
5281
Eric V. Smith451d0e32016-09-09 21:56:20 -04005282 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005283 goto unexpected_end_of_string;
5284
5285 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005286 assert(*str < end);
5287 assert(**str == '}');
5288 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005289
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005290 /* If we're in = mode (detected by non-NULL expr_text), and have no format
5291 spec and no explict conversion, set the conversion to 'r'. */
5292 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005293 conversion = 'r';
5294 }
5295
Eric V. Smith451d0e32016-09-09 21:56:20 -04005296 /* And now create the FormattedValue node that represents this
5297 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005298 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005299 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005300 n->n_col_offset, n->n_end_lineno,
5301 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005302 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005303 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005304
5305 return 0;
5306
5307unexpected_end_of_string:
5308 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005309 /* Falls through to error. */
5310
5311error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005312 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005313 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005314
Eric V. Smith235a6f02015-09-19 14:51:32 -04005315}
5316
5317/* Return -1 on error.
5318
5319 Return 0 if we have a literal (possible zero length) and an
5320 expression (zero length if at the end of the string.
5321
5322 Return 1 if we have a literal, but no expression, and we want the
5323 caller to call us again. This is used to deal with doubled
5324 braces.
5325
5326 When called multiple times on the string 'a{{b{0}c', this function
5327 will return:
5328
5329 1. the literal 'a{' with no expression, and a return value
5330 of 1. Despite the fact that there's no expression, the return
5331 value of 1 means we're not finished yet.
5332
5333 2. the literal 'b' and the expression '0', with a return value of
5334 0. The fact that there's an expression means we're not finished.
5335
5336 3. literal 'c' with no expression and a return value of 0. The
5337 combination of the return value of 0 with no expression means
5338 we're finished.
5339*/
5340static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005341fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5342 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005343 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005344 struct compiling *c, const node *n)
5345{
5346 int result;
5347
5348 assert(*literal == NULL && *expression == NULL);
5349
5350 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005351 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005352 if (result < 0)
5353 goto error;
5354
5355 assert(result == 0 || result == 1);
5356
5357 if (result == 1)
5358 /* We have a literal, but don't look at the expression. */
5359 return 1;
5360
Eric V. Smith451d0e32016-09-09 21:56:20 -04005361 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005362 /* We're at the end of the string or the end of a nested
5363 f-string: no expression. The top-level error case where we
5364 expect to be at the end of the string but we're at a '}' is
5365 handled later. */
5366 return 0;
5367
5368 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005369 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005370
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005371 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5372 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005373 goto error;
5374
5375 return 0;
5376
5377error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005378 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005379 return -1;
5380}
5381
5382#define EXPRLIST_N_CACHED 64
5383
5384typedef struct {
5385 /* Incrementally build an array of expr_ty, so be used in an
5386 asdl_seq. Cache some small but reasonably sized number of
5387 expr_ty's, and then after that start dynamically allocating,
5388 doubling the number allocated each time. Note that the f-string
5389 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005390 Constant for the literal 'a'. So you add expr_ty's about twice as
Miss Islington (bot)4bd1d052019-08-30 13:42:54 -07005391 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005392
5393 Py_ssize_t allocated; /* Number we've allocated. */
5394 Py_ssize_t size; /* Number we've used. */
5395 expr_ty *p; /* Pointer to the memory we're actually
5396 using. Will point to 'data' until we
5397 start dynamically allocating. */
5398 expr_ty data[EXPRLIST_N_CACHED];
5399} ExprList;
5400
5401#ifdef NDEBUG
5402#define ExprList_check_invariants(l)
5403#else
5404static void
5405ExprList_check_invariants(ExprList *l)
5406{
5407 /* Check our invariants. Make sure this object is "live", and
5408 hasn't been deallocated. */
5409 assert(l->size >= 0);
5410 assert(l->p != NULL);
5411 if (l->size <= EXPRLIST_N_CACHED)
5412 assert(l->data == l->p);
5413}
5414#endif
5415
5416static void
5417ExprList_Init(ExprList *l)
5418{
5419 l->allocated = EXPRLIST_N_CACHED;
5420 l->size = 0;
5421
5422 /* Until we start allocating dynamically, p points to data. */
5423 l->p = l->data;
5424
5425 ExprList_check_invariants(l);
5426}
5427
5428static int
5429ExprList_Append(ExprList *l, expr_ty exp)
5430{
5431 ExprList_check_invariants(l);
5432 if (l->size >= l->allocated) {
5433 /* We need to alloc (or realloc) the memory. */
5434 Py_ssize_t new_size = l->allocated * 2;
5435
5436 /* See if we've ever allocated anything dynamically. */
5437 if (l->p == l->data) {
5438 Py_ssize_t i;
5439 /* We're still using the cached data. Switch to
5440 alloc-ing. */
5441 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5442 if (!l->p)
5443 return -1;
5444 /* Copy the cached data into the new buffer. */
5445 for (i = 0; i < l->size; i++)
5446 l->p[i] = l->data[i];
5447 } else {
5448 /* Just realloc. */
5449 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5450 if (!tmp) {
5451 PyMem_RawFree(l->p);
5452 l->p = NULL;
5453 return -1;
5454 }
5455 l->p = tmp;
5456 }
5457
5458 l->allocated = new_size;
5459 assert(l->allocated == 2 * l->size);
5460 }
5461
5462 l->p[l->size++] = exp;
5463
5464 ExprList_check_invariants(l);
5465 return 0;
5466}
5467
5468static void
5469ExprList_Dealloc(ExprList *l)
5470{
5471 ExprList_check_invariants(l);
5472
5473 /* If there's been an error, or we've never dynamically allocated,
5474 do nothing. */
5475 if (!l->p || l->p == l->data) {
5476 /* Do nothing. */
5477 } else {
5478 /* We have dynamically allocated. Free the memory. */
5479 PyMem_RawFree(l->p);
5480 }
5481 l->p = NULL;
5482 l->size = -1;
5483}
5484
5485static asdl_seq *
5486ExprList_Finish(ExprList *l, PyArena *arena)
5487{
5488 asdl_seq *seq;
5489
5490 ExprList_check_invariants(l);
5491
5492 /* Allocate the asdl_seq and copy the expressions in to it. */
5493 seq = _Py_asdl_seq_new(l->size, arena);
5494 if (seq) {
5495 Py_ssize_t i;
5496 for (i = 0; i < l->size; i++)
5497 asdl_seq_SET(seq, i, l->p[i]);
5498 }
5499 ExprList_Dealloc(l);
5500 return seq;
5501}
5502
5503/* The FstringParser is designed to add a mix of strings and
5504 f-strings, and concat them together as needed. Ultimately, it
5505 generates an expr_ty. */
5506typedef struct {
5507 PyObject *last_str;
5508 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005509 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005510} FstringParser;
5511
5512#ifdef NDEBUG
5513#define FstringParser_check_invariants(state)
5514#else
5515static void
5516FstringParser_check_invariants(FstringParser *state)
5517{
5518 if (state->last_str)
5519 assert(PyUnicode_CheckExact(state->last_str));
5520 ExprList_check_invariants(&state->expr_list);
5521}
5522#endif
5523
5524static void
5525FstringParser_Init(FstringParser *state)
5526{
5527 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005528 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005529 ExprList_Init(&state->expr_list);
5530 FstringParser_check_invariants(state);
5531}
5532
5533static void
5534FstringParser_Dealloc(FstringParser *state)
5535{
5536 FstringParser_check_invariants(state);
5537
5538 Py_XDECREF(state->last_str);
5539 ExprList_Dealloc(&state->expr_list);
5540}
5541
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005542/* Constants for the following */
5543static PyObject *u_kind;
5544
5545/* Compute 'kind' field for string Constant (either 'u' or None) */
5546static PyObject *
5547make_kind(struct compiling *c, const node *n)
5548{
5549 char *s = NULL;
5550 PyObject *kind = NULL;
5551
5552 /* Find the first string literal, if any */
5553 while (TYPE(n) != STRING) {
5554 if (NCH(n) == 0)
5555 return NULL;
5556 n = CHILD(n, 0);
5557 }
5558 REQ(n, STRING);
5559
5560 /* If it starts with 'u', return a PyUnicode "u" string */
5561 s = STR(n);
5562 if (s && *s == 'u') {
5563 if (!u_kind) {
5564 u_kind = PyUnicode_InternFromString("u");
5565 if (!u_kind)
5566 return NULL;
5567 }
5568 kind = u_kind;
5569 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5570 return NULL;
5571 }
5572 Py_INCREF(kind);
5573 }
5574 return kind;
5575}
5576
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005577/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005578static expr_ty
5579make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5580{
5581 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005582 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005583 *str = NULL;
5584 assert(PyUnicode_CheckExact(s));
5585 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5586 Py_DECREF(s);
5587 return NULL;
5588 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005589 kind = make_kind(c, n);
5590 if (kind == NULL && PyErr_Occurred())
5591 return NULL;
5592 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005593 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005594}
5595
5596/* Add a non-f-string (that is, a regular literal string). str is
5597 decref'd. */
5598static int
5599FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5600{
5601 FstringParser_check_invariants(state);
5602
5603 assert(PyUnicode_CheckExact(str));
5604
5605 if (PyUnicode_GET_LENGTH(str) == 0) {
5606 Py_DECREF(str);
5607 return 0;
5608 }
5609
5610 if (!state->last_str) {
5611 /* We didn't have a string before, so just remember this one. */
5612 state->last_str = str;
5613 } else {
5614 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005615 PyUnicode_AppendAndDel(&state->last_str, str);
5616 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005617 return -1;
5618 }
5619 FstringParser_check_invariants(state);
5620 return 0;
5621}
5622
Eric V. Smith451d0e32016-09-09 21:56:20 -04005623/* Parse an f-string. The f-string is in *str to end, with no
5624 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005625static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005626FstringParser_ConcatFstring(FstringParser *state, const char **str,
5627 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005628 struct compiling *c, const node *n)
5629{
5630 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005631 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005632
5633 /* Parse the f-string. */
5634 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005635 PyObject *literal = NULL;
5636 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005637 expr_ty expression = NULL;
5638
5639 /* If there's a zero length literal in front of the
5640 expression, literal will be NULL. If we're at the end of
5641 the f-string, expression will be NULL (unless result == 1,
5642 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005643 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005644 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005645 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005646 if (result < 0)
5647 return -1;
5648
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005649 /* Add the literal, if any. */
5650 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5651 Py_XDECREF(expr_text);
5652 return -1;
5653 }
5654 /* Add the expr_text, if any. */
5655 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5656 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005657 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005658
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005659 /* We've dealt with the literal and expr_text, their ownership has
5660 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005661
5662 /* See if we should just loop around to get the next literal
5663 and expression, while ignoring the expression this
5664 time. This is used for un-doubling braces, as an
5665 optimization. */
5666 if (result == 1)
5667 continue;
5668
5669 if (!expression)
5670 /* We're done with this f-string. */
5671 break;
5672
5673 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005674 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005675 if (!state->last_str) {
5676 /* Do nothing. No previous literal. */
5677 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005678 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005679 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5680 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5681 return -1;
5682 }
5683
5684 if (ExprList_Append(&state->expr_list, expression) < 0)
5685 return -1;
5686 }
5687
Eric V. Smith235a6f02015-09-19 14:51:32 -04005688 /* If recurse_lvl is zero, then we must be at the end of the
5689 string. Otherwise, we must be at a right brace. */
5690
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005692 ast_error(c, n, "f-string: unexpected end of string");
5693 return -1;
5694 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005695 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005696 ast_error(c, n, "f-string: expecting '}'");
5697 return -1;
5698 }
5699
5700 FstringParser_check_invariants(state);
5701 return 0;
5702}
5703
5704/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005705 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005706static expr_ty
5707FstringParser_Finish(FstringParser *state, struct compiling *c,
5708 const node *n)
5709{
5710 asdl_seq *seq;
5711
5712 FstringParser_check_invariants(state);
5713
5714 /* If we're just a constant string with no expressions, return
5715 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005716 if (!state->fmode) {
5717 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005718 if (!state->last_str) {
5719 /* Create a zero length string. */
5720 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5721 if (!state->last_str)
5722 goto error;
5723 }
5724 return make_str_node_and_del(&state->last_str, c, n);
5725 }
5726
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005727 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005728 last node in our expression list. */
5729 if (state->last_str) {
5730 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5731 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5732 goto error;
5733 }
5734 /* This has already been freed. */
5735 assert(state->last_str == NULL);
5736
5737 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5738 if (!seq)
5739 goto error;
5740
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005741 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5742 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005743
5744error:
5745 FstringParser_Dealloc(state);
5746 return NULL;
5747}
5748
Eric V. Smith451d0e32016-09-09 21:56:20 -04005749/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5750 at end, parse it into an expr_ty. Return NULL on error. Adjust
5751 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005752static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005753fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005754 struct compiling *c, const node *n)
5755{
5756 FstringParser state;
5757
5758 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005759 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005760 c, n) < 0) {
5761 FstringParser_Dealloc(&state);
5762 return NULL;
5763 }
5764
5765 return FstringParser_Finish(&state, c, n);
5766}
5767
5768/* n is a Python string literal, including the bracketing quote
5769 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005770 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005771 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005772 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5773 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005774*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005775static int
5776parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5777 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005778{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005779 size_t len;
5780 const char *s = STR(n);
5781 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005782 int fmode = 0;
5783 *bytesmode = 0;
5784 *rawmode = 0;
5785 *result = NULL;
5786 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005787 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005788 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005789 if (quote == 'b' || quote == 'B') {
5790 quote = *++s;
5791 *bytesmode = 1;
5792 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005793 else if (quote == 'u' || quote == 'U') {
5794 quote = *++s;
5795 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005796 else if (quote == 'r' || quote == 'R') {
5797 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005798 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005799 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005800 else if (quote == 'f' || quote == 'F') {
5801 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005802 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005803 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005804 else {
5805 break;
5806 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005807 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005808 }
Guido van Rossum495da292019-03-07 12:38:08 -08005809
5810 /* fstrings are only allowed in Python 3.6 and greater */
5811 if (fmode && c->c_feature_version < 6) {
5812 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5813 return -1;
5814 }
5815
Eric V. Smith451d0e32016-09-09 21:56:20 -04005816 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005817 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005818 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005819 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005820 if (quote != '\'' && quote != '\"') {
5821 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005822 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005823 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005824 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005825 s++;
5826 len = strlen(s);
5827 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005828 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005829 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005830 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005831 }
5832 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005833 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005834 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005835 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005836 }
5837 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005838 /* A triple quoted string. We've already skipped one quote at
5839 the start and one at the end of the string. Now skip the
5840 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005841 s += 2;
5842 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005843 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005844 if (s[--len] != quote || s[--len] != quote) {
5845 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005846 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005847 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005848 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005849
Eric V. Smith451d0e32016-09-09 21:56:20 -04005850 if (fmode) {
5851 /* Just return the bytes. The caller will parse the resulting
5852 string. */
5853 *fstr = s;
5854 *fstrlen = len;
5855 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005856 }
5857
Eric V. Smith451d0e32016-09-09 21:56:20 -04005858 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005859 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005860 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005861 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005862 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005863 const char *ch;
5864 for (ch = s; *ch; ch++) {
5865 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005866 ast_error(c, n,
5867 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005868 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005869 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005870 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005871 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005872 if (*rawmode)
5873 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005874 else
Eric V. Smith56466482016-10-31 14:46:26 -04005875 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005876 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005877 if (*rawmode)
5878 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005879 else
Eric V. Smith56466482016-10-31 14:46:26 -04005880 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005881 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005882 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005883}
5884
Eric V. Smith235a6f02015-09-19 14:51:32 -04005885/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5886 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005887 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005888 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005889 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005890 node if there's just an f-string (with no leading or trailing
5891 literals), or a JoinedStr node if there are multiple f-strings or
5892 any literals involved. */
5893static expr_ty
5894parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005895{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005896 int bytesmode = 0;
5897 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005898 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005899
5900 FstringParser state;
5901 FstringParser_Init(&state);
5902
5903 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005904 int this_bytesmode;
5905 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005906 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005907 const char *fstr;
5908 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005909
5910 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005911 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5912 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005913 goto error;
5914
5915 /* Check that we're not mixing bytes with unicode. */
5916 if (i != 0 && bytesmode != this_bytesmode) {
5917 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005918 /* s is NULL if the current string part is an f-string. */
5919 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005920 goto error;
5921 }
5922 bytesmode = this_bytesmode;
5923
Eric V. Smith451d0e32016-09-09 21:56:20 -04005924 if (fstr != NULL) {
5925 int result;
5926 assert(s == NULL && !bytesmode);
5927 /* This is an f-string. Parse and concatenate it. */
5928 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5929 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005930 if (result < 0)
5931 goto error;
5932 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005933 /* A string or byte string. */
5934 assert(s != NULL && fstr == NULL);
5935
Eric V. Smith451d0e32016-09-09 21:56:20 -04005936 assert(bytesmode ? PyBytes_CheckExact(s) :
5937 PyUnicode_CheckExact(s));
5938
Eric V. Smith451d0e32016-09-09 21:56:20 -04005939 if (bytesmode) {
5940 /* For bytes, concat as we go. */
5941 if (i == 0) {
5942 /* First time, just remember this value. */
5943 bytes_str = s;
5944 } else {
5945 PyBytes_ConcatAndDel(&bytes_str, s);
5946 if (!bytes_str)
5947 goto error;
5948 }
5949 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005950 /* This is a regular string. Concatenate it. */
5951 if (FstringParser_ConcatAndDel(&state, s) < 0)
5952 goto error;
5953 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005954 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005955 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005956 if (bytesmode) {
5957 /* Just return the bytes object and we're done. */
5958 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5959 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005960 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005961 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005962 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005963
Eric V. Smith235a6f02015-09-19 14:51:32 -04005964 /* We're not a bytes string, bytes_str should never have been set. */
5965 assert(bytes_str == NULL);
5966
5967 return FstringParser_Finish(&state, c, n);
5968
5969error:
5970 Py_XDECREF(bytes_str);
5971 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005973}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005974
5975PyObject *
5976_PyAST_GetDocString(asdl_seq *body)
5977{
5978 if (!asdl_seq_LEN(body)) {
5979 return NULL;
5980 }
5981 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5982 if (st->kind != Expr_kind) {
5983 return NULL;
5984 }
5985 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005986 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5987 return e->v.Constant.value;
5988 }
5989 return NULL;
5990}