blob: 7c1d24dea7184fac9619e3f41c1adf171bbdcc66 [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 Rossume6533692020-06-27 17:35:05 -0700811 c.c_feature_version = flags && (flags->cf_flags & PyCF_ONLY_AST) ?
812 flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800813
814 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816
Jeremy Hyltona8293132006-02-28 17:58:27 +0000817 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 switch (TYPE(n)) {
819 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200820 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500822 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 for (i = 0; i < NCH(n) - 1; i++) {
824 ch = CHILD(n, i);
825 if (TYPE(ch) == NEWLINE)
826 continue;
827 REQ(ch, stmt);
828 num = num_stmts(ch);
829 if (num == 1) {
830 s = ast_for_stmt(&c, ch);
831 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500832 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000833 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 }
835 else {
836 ch = CHILD(ch, 0);
837 REQ(ch, simple_stmt);
838 for (j = 0; j < num; j++) {
839 s = ast_for_stmt(&c, CHILD(ch, j * 2));
840 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000842 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
844 }
845 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800846
847 /* Type ignores are stored under the ENDMARKER in file_input. */
848 ch = CHILD(n, NCH(n) - 1);
849 REQ(ch, ENDMARKER);
850 num = NCH(ch);
851 type_ignores = _Py_asdl_seq_new(num, arena);
852 if (!type_ignores)
853 goto out;
854
855 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700856 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
857 if (!type_comment)
858 goto out;
859 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800860 if (!ti)
861 goto out;
862 asdl_seq_SET(type_ignores, i, ti);
863 }
864
865 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 case eval_input: {
868 expr_ty testlist_ast;
869
Nick Coghlan650f0d02007-04-15 12:05:43 +0000870 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000871 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500873 goto out;
874 res = Expression(testlist_ast, arena);
875 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
877 case single_input:
878 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200879 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500881 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000883 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000885 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500886 goto out;
887 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 }
889 else {
890 n = CHILD(n, 0);
891 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200892 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500894 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000896 s = ast_for_stmt(&c, n);
897 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500898 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899 asdl_seq_SET(stmts, 0, s);
900 }
901 else {
902 /* Only a simple_stmt can contain multiple statements. */
903 REQ(n, simple_stmt);
904 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 if (TYPE(CHILD(n, i)) == NEWLINE)
906 break;
907 s = ast_for_stmt(&c, CHILD(n, i));
908 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500909 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 asdl_seq_SET(stmts, i / 2, s);
911 }
912 }
913
Benjamin Peterson55e00432012-01-16 17:22:31 -0500914 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500916 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800917 case func_type_input:
918 n = CHILD(n, 0);
919 REQ(n, func_type);
920
921 if (TYPE(CHILD(n, 1)) == typelist) {
922 ch = CHILD(n, 1);
923 /* this is overly permissive -- we don't pay any attention to
924 * stars on the args -- just parse them into an ordered list */
925 num = 0;
926 for (i = 0; i < NCH(ch); i++) {
927 if (TYPE(CHILD(ch, i)) == test) {
928 num++;
929 }
930 }
931
932 argtypes = _Py_asdl_seq_new(num, arena);
933 if (!argtypes)
934 goto out;
935
936 j = 0;
937 for (i = 0; i < NCH(ch); i++) {
938 if (TYPE(CHILD(ch, i)) == test) {
939 arg = ast_for_expr(&c, CHILD(ch, i));
940 if (!arg)
941 goto out;
942 asdl_seq_SET(argtypes, j++, arg);
943 }
944 }
945 }
946 else {
947 argtypes = _Py_asdl_seq_new(0, arena);
948 if (!argtypes)
949 goto out;
950 }
951
952 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
953 if (!ret)
954 goto out;
955 res = FunctionType(argtypes, ret, arena);
956 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000958 PyErr_Format(PyExc_SystemError,
959 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500960 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500962 out:
963 if (c.c_normalize) {
964 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500965 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500966 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967}
968
Victor Stinner14e461d2013-08-26 22:28:21 +0200969mod_ty
970PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
971 PyArena *arena)
972{
973 mod_ty mod;
974 PyObject *filename;
975 filename = PyUnicode_DecodeFSDefault(filename_str);
976 if (filename == NULL)
977 return NULL;
978 mod = PyAST_FromNodeObject(n, flags, filename, arena);
979 Py_DECREF(filename);
980 return mod;
981
982}
983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
985*/
986
987static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800988get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
990 switch (TYPE(n)) {
991 case VBAR:
992 return BitOr;
993 case CIRCUMFLEX:
994 return BitXor;
995 case AMPER:
996 return BitAnd;
997 case LEFTSHIFT:
998 return LShift;
999 case RIGHTSHIFT:
1000 return RShift;
1001 case PLUS:
1002 return Add;
1003 case MINUS:
1004 return Sub;
1005 case STAR:
1006 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001007 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -08001008 if (c->c_feature_version < 5) {
1009 ast_error(c, n,
1010 "The '@' operator is only supported in Python 3.5 and greater");
1011 return (operator_ty)0;
1012 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001013 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case SLASH:
1015 return Div;
1016 case DOUBLESLASH:
1017 return FloorDiv;
1018 case PERCENT:
1019 return Mod;
1020 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 }
1023}
1024
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001025static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001026 "None",
1027 "True",
1028 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001029 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001030 NULL,
1031};
1032
1033static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001034forbidden_name(struct compiling *c, identifier name, const node *n,
1035 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001036{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001037 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001038 const char * const *p = FORBIDDEN;
1039 if (!full_checks) {
1040 /* In most cases, the parser will protect True, False, and None
1041 from being assign to. */
1042 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001043 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001044 for (; *p; p++) {
1045 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1046 ast_error(c, n, "cannot assign to %U", name);
1047 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001048 }
1049 }
1050 return 0;
1051}
1052
Serhiy Storchakab619b092018-11-27 09:40:29 +02001053static expr_ty
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08001054copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001055{
1056 if (e) {
1057 e->lineno = LINENO(n);
1058 e->col_offset = n->n_col_offset;
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08001059 e->end_lineno = end->n_end_lineno;
1060 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001061 }
1062 return e;
1063}
1064
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001065static const char *
1066get_expr_name(expr_ty e)
1067{
1068 switch (e->kind) {
1069 case Attribute_kind:
1070 return "attribute";
1071 case Subscript_kind:
1072 return "subscript";
1073 case Starred_kind:
1074 return "starred";
1075 case Name_kind:
1076 return "name";
1077 case List_kind:
1078 return "list";
1079 case Tuple_kind:
1080 return "tuple";
1081 case Lambda_kind:
1082 return "lambda";
1083 case Call_kind:
1084 return "function call";
1085 case BoolOp_kind:
1086 case BinOp_kind:
1087 case UnaryOp_kind:
1088 return "operator";
1089 case GeneratorExp_kind:
1090 return "generator expression";
1091 case Yield_kind:
1092 case YieldFrom_kind:
1093 return "yield expression";
1094 case Await_kind:
1095 return "await expression";
1096 case ListComp_kind:
1097 return "list comprehension";
1098 case SetComp_kind:
1099 return "set comprehension";
1100 case DictComp_kind:
1101 return "dict comprehension";
1102 case Dict_kind:
1103 return "dict display";
1104 case Set_kind:
1105 return "set display";
1106 case JoinedStr_kind:
1107 case FormattedValue_kind:
1108 return "f-string expression";
1109 case Constant_kind: {
1110 PyObject *value = e->v.Constant.value;
1111 if (value == Py_None) {
1112 return "None";
1113 }
1114 if (value == Py_False) {
1115 return "False";
1116 }
1117 if (value == Py_True) {
1118 return "True";
1119 }
1120 if (value == Py_Ellipsis) {
1121 return "Ellipsis";
1122 }
1123 return "literal";
1124 }
1125 case Compare_kind:
1126 return "comparison";
1127 case IfExp_kind:
1128 return "conditional expression";
1129 case NamedExpr_kind:
1130 return "named expression";
1131 default:
1132 PyErr_Format(PyExc_SystemError,
1133 "unexpected expression in assignment %d (line %d)",
1134 e->kind, e->lineno);
1135 return NULL;
1136 }
1137}
1138
Jeremy Hyltona8293132006-02-28 17:58:27 +00001139/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140
1141 Only sets context for expr kinds that "can appear in assignment context"
1142 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1143 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144*/
1145
1146static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001147set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148{
1149 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001150
1151 /* The ast defines augmented store and load contexts, but the
1152 implementation here doesn't actually use them. The code may be
1153 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001154 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001155 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001156 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001157 */
1158 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159
1160 switch (e->kind) {
1161 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001163 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001164 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 e->v.Subscript.ctx = ctx;
1168 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001169 case Starred_kind:
1170 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001171 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001172 return 0;
1173 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001175 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001176 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001177 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001178 }
1179 e->v.Name.ctx = ctx;
1180 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 e->v.List.ctx = ctx;
1183 s = e->v.List.elts;
1184 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001186 e->v.Tuple.ctx = ctx;
1187 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001188 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001189 default: {
1190 const char *expr_name = get_expr_name(e);
1191 if (expr_name != NULL) {
1192 ast_error(c, n, "cannot %s %s",
1193 ctx == Store ? "assign to" : "delete",
1194 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001195 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001196 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001197 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001198 }
1199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 */
1203 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001204 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205
Thomas Wouters89f507f2006-12-13 04:49:30 +00001206 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001207 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001208 return 0;
1209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 }
1211 return 1;
1212}
1213
1214static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001215ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216{
1217 REQ(n, augassign);
1218 n = CHILD(n, 0);
1219 switch (STR(n)[0]) {
1220 case '+':
1221 return Add;
1222 case '-':
1223 return Sub;
1224 case '/':
1225 if (STR(n)[1] == '/')
1226 return FloorDiv;
1227 else
1228 return Div;
1229 case '%':
1230 return Mod;
1231 case '<':
1232 return LShift;
1233 case '>':
1234 return RShift;
1235 case '&':
1236 return BitAnd;
1237 case '^':
1238 return BitXor;
1239 case '|':
1240 return BitOr;
1241 case '*':
1242 if (STR(n)[1] == '*')
1243 return Pow;
1244 else
1245 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001246 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001247 if (c->c_feature_version < 5) {
1248 ast_error(c, n,
1249 "The '@' operator is only supported in Python 3.5 and greater");
1250 return (operator_ty)0;
1251 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001252 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001254 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001255 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 }
1257}
1258
1259static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001260ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001262 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 |'is' 'not'
1264 */
1265 REQ(n, comp_op);
1266 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001267 n = CHILD(n, 0);
1268 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 case LESS:
1270 return Lt;
1271 case GREATER:
1272 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001273 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return Eq;
1275 case LESSEQUAL:
1276 return LtE;
1277 case GREATEREQUAL:
1278 return GtE;
1279 case NOTEQUAL:
1280 return NotEq;
1281 case NAME:
1282 if (strcmp(STR(n), "in") == 0)
1283 return In;
1284 if (strcmp(STR(n), "is") == 0)
1285 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001286 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001288 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 }
1293 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001294 /* handle "not in" and "is not" */
1295 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 case NAME:
1297 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1298 return NotIn;
1299 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1300 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001301 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001303 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001305 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001306 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 }
Neal Norwitz79792652005-11-14 04:25:03 +00001308 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001310 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311}
1312
1313static asdl_seq *
1314seq_for_testlist(struct compiling *c, const node *n)
1315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001317 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1318 */
Armin Rigo31441302005-10-21 12:57:31 +00001319 asdl_seq *seq;
1320 expr_ty expression;
1321 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001322 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001324 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 if (!seq)
1326 return NULL;
1327
1328 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001330 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331
Benjamin Peterson4905e802009-09-27 02:43:28 +00001332 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001333 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335
1336 assert(i / 2 < seq->size);
1337 asdl_seq_SET(seq, i / 2, expression);
1338 }
1339 return seq;
1340}
1341
Neal Norwitzc1505362006-12-28 06:47:50 +00001342static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001343ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001344{
1345 identifier name;
1346 expr_ty annotation = NULL;
1347 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001348 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001349
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001350 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001351 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001352 name = NEW_IDENTIFIER(ch);
1353 if (!name)
1354 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001355 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001356 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001357
1358 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1359 annotation = ast_for_expr(c, CHILD(n, 2));
1360 if (!annotation)
1361 return NULL;
1362 }
1363
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001364 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001365 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001366 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001367 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001368 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369}
1370
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371/* returns -1 if failed to handle keyword only arguments
1372 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001373 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 ^^^
1375 start pointing here
1376 */
1377static int
1378handle_keywordonly_args(struct compiling *c, const node *n, int start,
1379 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1380{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001381 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001383 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001384 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 int i = start;
1386 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001387
1388 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001389 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001390 return -1;
1391 }
1392 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 while (i < NCH(n)) {
1394 ch = CHILD(n, i);
1395 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001396 case vfpdef:
1397 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001399 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001400 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001401 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 asdl_seq_SET(kwdefaults, j, expression);
1403 i += 2; /* '=' and test */
1404 }
1405 else { /* setting NULL if no default value exists */
1406 asdl_seq_SET(kwdefaults, j, NULL);
1407 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001408 if (NCH(ch) == 3) {
1409 /* ch is NAME ':' test */
1410 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001411 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001412 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001413 }
1414 else {
1415 annotation = NULL;
1416 }
1417 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001418 argname = NEW_IDENTIFIER(ch);
1419 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001421 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001422 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001423 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001424 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001425 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001426 if (!arg)
1427 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001428 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001429 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001430 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001431 i += 1; /* the comma, if present */
1432 break;
1433 case TYPE_COMMENT:
1434 /* arg will be equal to the last argument processed */
1435 arg->type_comment = NEW_TYPE_COMMENT(ch);
1436 if (!arg->type_comment)
1437 goto error;
1438 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001439 break;
1440 case DOUBLESTAR:
1441 return i;
1442 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001443 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 goto error;
1445 }
1446 }
1447 return i;
1448 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451
Jeremy Hyltona8293132006-02-28 17:58:27 +00001452/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453
1454static arguments_ty
1455ast_for_arguments(struct compiling *c, const node *n)
1456{
Neal Norwitzc1505362006-12-28 06:47:50 +00001457 /* This function handles both typedargslist (function definition)
1458 and varargslist (lambda definition).
1459
1460 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001461
1462 The following definition for typedarglist is equivalent to this set of rules:
1463
1464 arguments = argument (',' [TYPE_COMMENT] argument)*
1465 argument = tfpdef ['=' test]
1466 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1467 args = '*' [tfpdef]
1468 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1469 [TYPE_COMMENT] [kwargs]])
1470 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1471 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1472 [TYPE_COMMENT] [args_kwonly_kwargs]])
1473 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1474 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1475 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1476
1477 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1478 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1479 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1480 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1481 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1482 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1483 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1484 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1485 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1486 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1487 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1488 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1489 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1490 '**' tfpdef [','] [TYPE_COMMENT]))
1491
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001492 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001493
1494 The following definition for varargslist is equivalent to this set of rules:
1495
1496 arguments = argument (',' argument )*
1497 argument = vfpdef ['=' test]
1498 kwargs = '**' vfpdef [',']
1499 args = '*' [vfpdef]
1500 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1501 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1502 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1503 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1504 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1505 (vararglist_no_posonly)
1506
1507 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1508 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1509 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1510 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1511 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1512 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1513 [',']]] | '**' vfpdef [','])
1514
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001515 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001518 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001519 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001520 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001521 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001522 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 node *ch;
1524
1525 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001526 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001527 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001528 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001530 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531
Jeremy Hyltone921e022008-07-17 16:37:17 +00001532 /* First count the number of positional args & defaults. The
1533 variable i is the loop index for this for loop and the next.
1534 The next loop picks up where the first leaves off.
1535 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001537 ch = CHILD(n, i);
1538 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001539 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001540 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001541 if (i < NCH(n) && /* skip argument following star */
1542 (TYPE(CHILD(n, i)) == tfpdef ||
1543 TYPE(CHILD(n, i)) == vfpdef)) {
1544 i++;
1545 }
1546 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001548 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001549 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001550 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001551 if (TYPE(ch) == SLASH ) {
1552 nposonlyargs = nposargs;
1553 nposargs = 0;
1554 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001557 defaults for keyword only args */
1558 for ( ; i < NCH(n); ++i) {
1559 ch = CHILD(n, i);
1560 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001561 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001562 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001563 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1564 if (!posonlyargs && nposonlyargs) {
1565 return NULL;
1566 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001567 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001568 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001569 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001570 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001571 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001572 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001573 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001575 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001576 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001577 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001579 since we set NULL as default for keyword only argument w/o default
1580 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001581 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001582 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001583 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001584 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001585
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001586 /* tfpdef: NAME [':' test]
1587 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 */
1589 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001590 j = 0; /* index for defaults */
1591 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001592 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001594 ch = CHILD(n, i);
1595 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001596 case tfpdef:
1597 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1599 anything other than EQUAL or a comma? */
1600 /* XXX Should NCH(n) check be made a separate check? */
1601 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001602 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1603 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001604 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001605 assert(posdefaults != NULL);
1606 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001608 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001610 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001611 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001612 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001613 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001614 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001615 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001616 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001617 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001618 if (l < nposonlyargs) {
1619 asdl_seq_SET(posonlyargs, l++, arg);
1620 } else {
1621 asdl_seq_SET(posargs, k++, arg);
1622 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001623 i += 1; /* the name */
1624 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1625 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001627 case SLASH:
1628 /* Advance the slash and the comma. If there are more names
1629 * after the slash there will be a comma so we are advancing
1630 * the correct number of nodes. If the slash is the last item,
1631 * we will be advancing an extra token but then * i > NCH(n)
1632 * and the enclosing while will finish correctly. */
1633 i += 2;
1634 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001636 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001637 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1638 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001639 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001640 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001641 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001642 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001643 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001644 if (TYPE(ch) == COMMA) {
1645 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001646 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001647
1648 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1649 ast_error(c, CHILD(n, i),
1650 "bare * has associated type comment");
1651 return NULL;
1652 }
1653
Guido van Rossum4f72a782006-10-27 23:31:49 +00001654 res = handle_keywordonly_args(c, n, i,
1655 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001656 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001657 i = res; /* res has new position to process */
1658 }
1659 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001660 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001661 if (!vararg)
1662 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001663
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001664 i += 2; /* the star and the name */
1665 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1666 i += 1; /* the comma, if present */
1667
1668 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1669 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1670 if (!vararg->type_comment)
1671 return NULL;
1672 i += 1;
1673 }
1674
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001675 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1676 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001677 int res = 0;
1678 res = handle_keywordonly_args(c, n, i,
1679 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001680 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001681 i = res; /* res has new position to process */
1682 }
1683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 break;
1685 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001686 ch = CHILD(n, i+1); /* tfpdef */
1687 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001688 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001689 if (!kwarg)
1690 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001691 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001692 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001693 i += 1; /* the comma, if present */
1694 break;
1695 case TYPE_COMMENT:
1696 assert(i);
1697
1698 if (kwarg)
1699 arg = kwarg;
1700
1701 /* arg will be equal to the last argument processed */
1702 arg->type_comment = NEW_TYPE_COMMENT(ch);
1703 if (!arg->type_comment)
1704 return NULL;
1705 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 break;
1707 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001708 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 "unexpected node in varargslist: %d @ %d",
1710 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001711 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 }
Miss Islington (bot)cf9a63c2019-07-14 16:49:52 -07001714 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715}
1716
1717static expr_ty
1718ast_for_dotted_name(struct compiling *c, const node *n)
1719{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001720 expr_ty e;
1721 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001722 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001724 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725
1726 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001727
1728 lineno = LINENO(n);
1729 col_offset = n->n_col_offset;
1730
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001731 ch = CHILD(n, 0);
1732 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001734 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001735 e = Name(id, Load, lineno, col_offset,
1736 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739
1740 for (i = 2; i < NCH(n); i+=2) {
Lysandros Nikolaou8b9cebc2020-02-08 01:21:38 +01001741 const node *child = CHILD(n, i);
1742 id = NEW_IDENTIFIER(child);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001743 if (!id)
1744 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001745 e = Attribute(e, id, Load, lineno, col_offset,
Lysandros Nikolaou8b9cebc2020-02-08 01:21:38 +01001746 child->n_end_lineno, child->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001747 if (!e)
1748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 }
1750
1751 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752}
1753
1754static expr_ty
1755ast_for_decorator(struct compiling *c, const node *n)
1756{
1757 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1758 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001759 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001762 REQ(CHILD(n, 0), AT);
1763 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1766 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001767 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001770 d = name_expr;
1771 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 }
1773 else if (NCH(n) == 5) { /* Call with no arguments */
Miss Skeleton (bot)ba3a5662019-10-26 07:06:40 -07001774 d = Call(name_expr, NULL, NULL,
1775 name_expr->lineno, name_expr->col_offset,
1776 CHILD(n, 3)->n_end_lineno, CHILD(n, 3)->n_end_col_offset,
1777 c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001778 if (!d)
1779 return NULL;
1780 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 }
1782 else {
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08001783 d = ast_for_call(c, CHILD(n, 3), name_expr,
1784 CHILD(n, 1), CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001785 if (!d)
1786 return NULL;
1787 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 }
1789
1790 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791}
1792
1793static asdl_seq*
1794ast_for_decorators(struct compiling *c, const node *n)
1795{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001796 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001797 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001801 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 if (!decorator_seq)
1803 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001806 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001807 if (!d)
1808 return NULL;
1809 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 }
1811 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812}
1813
1814static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001815ast_for_funcdef_impl(struct compiling *c, const node *n0,
1816 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001818 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001819 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001820 identifier name;
1821 arguments_ty args;
1822 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001823 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001824 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001825 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001826 node *tc;
1827 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828
Guido van Rossum495da292019-03-07 12:38:08 -08001829 if (is_async && c->c_feature_version < 5) {
1830 ast_error(c, n,
1831 "Async functions are only supported in Python 3.5 and greater");
1832 return NULL;
1833 }
1834
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 REQ(n, funcdef);
1836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 name = NEW_IDENTIFIER(CHILD(n, name_i));
1838 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001839 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001840 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001841 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1843 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001844 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001845 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1846 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1847 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001848 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001849 name_i += 2;
1850 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001851 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1852 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1853 if (!type_comment)
1854 return NULL;
1855 name_i += 1;
1856 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001857 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001859 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001860 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001862 if (NCH(CHILD(n, name_i + 3)) > 1) {
1863 /* Check if the suite has a type comment in it. */
1864 tc = CHILD(CHILD(n, name_i + 3), 1);
1865
1866 if (TYPE(tc) == TYPE_COMMENT) {
1867 if (type_comment != NULL) {
1868 ast_error(c, n, "Cannot have two type comments on def");
1869 return NULL;
1870 }
1871 type_comment = NEW_TYPE_COMMENT(tc);
1872 if (!type_comment)
1873 return NULL;
1874 }
1875 }
1876
Yury Selivanov75445082015-05-11 22:57:16 -04001877 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001878 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001879 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001880 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001881 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001882 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001883}
1884
1885static stmt_ty
1886ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1887{
Guido van Rossum495da292019-03-07 12:38:08 -08001888 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001889 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001890 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001891 REQ(CHILD(n, 1), funcdef);
1892
guoci90fc8982018-09-11 17:45:45 -04001893 return ast_for_funcdef_impl(c, n, decorator_seq,
1894 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001895}
1896
1897static stmt_ty
1898ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1899{
1900 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1901 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001902 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001903}
1904
1905
1906static stmt_ty
1907ast_for_async_stmt(struct compiling *c, const node *n)
1908{
Guido van Rossum495da292019-03-07 12:38:08 -08001909 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001910 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001911 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001912
1913 switch (TYPE(CHILD(n, 1))) {
1914 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001915 return ast_for_funcdef_impl(c, n, NULL,
1916 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001917 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001918 return ast_for_with_stmt(c, n,
1919 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001920
1921 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001922 return ast_for_for_stmt(c, n,
1923 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001924
1925 default:
1926 PyErr_Format(PyExc_SystemError,
1927 "invalid async stament: %s",
1928 STR(CHILD(n, 1)));
1929 return NULL;
1930 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931}
1932
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001933static stmt_ty
1934ast_for_decorated(struct compiling *c, const node *n)
1935{
Yury Selivanov75445082015-05-11 22:57:16 -04001936 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001937 stmt_ty thing = NULL;
1938 asdl_seq *decorator_seq = NULL;
1939
1940 REQ(n, decorated);
1941
1942 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1943 if (!decorator_seq)
1944 return NULL;
1945
1946 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001947 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001949
1950 if (TYPE(CHILD(n, 1)) == funcdef) {
1951 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1952 } else if (TYPE(CHILD(n, 1)) == classdef) {
1953 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001954 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1955 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001956 }
1957 return thing;
1958}
1959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001961ast_for_namedexpr(struct compiling *c, const node *n)
1962{
Miss Islington (bot)cd968de2019-12-15 12:04:07 -08001963 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001964 argument: ( test [comp_for] |
1965 test ':=' test |
1966 test '=' test |
1967 '**' test |
1968 '*' test )
1969 */
1970 expr_ty target, value;
1971
1972 target = ast_for_expr(c, CHILD(n, 0));
1973 if (!target)
1974 return NULL;
1975
1976 value = ast_for_expr(c, CHILD(n, 2));
1977 if (!value)
1978 return NULL;
1979
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001980 if (target->kind != Name_kind) {
1981 const char *expr_name = get_expr_name(target);
1982 if (expr_name != NULL) {
Miss Islington (bot)6c004952019-12-31 19:28:08 -08001983 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001984 }
1985 return NULL;
1986 }
1987
1988 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001989 return NULL;
1990
1991 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1992 n->n_end_col_offset, c->c_arena);
1993}
1994
1995static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996ast_for_lambdef(struct compiling *c, const node *n)
1997{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001998 /* lambdef: 'lambda' [varargslist] ':' test
1999 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 arguments_ty args;
2001 expr_ty expression;
2002
2003 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002004 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 if (!args)
2006 return NULL;
2007 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002008 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
2011 else {
2012 args = ast_for_arguments(c, CHILD(n, 1));
2013 if (!args)
2014 return NULL;
2015 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002016 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 }
2019
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002020 return Lambda(args, expression, LINENO(n), n->n_col_offset,
2021 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022}
2023
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002024static expr_ty
2025ast_for_ifexpr(struct compiling *c, const node *n)
2026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002028 expr_ty expression, body, orelse;
2029
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00002030 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002031 body = ast_for_expr(c, CHILD(n, 0));
2032 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002033 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002034 expression = ast_for_expr(c, CHILD(n, 2));
2035 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002036 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002037 orelse = ast_for_expr(c, CHILD(n, 4));
2038 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002039 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002040 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002041 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002042 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002043}
2044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002046 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047
Nick Coghlan650f0d02007-04-15 12:05:43 +00002048 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049*/
2050
2051static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002052count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002054 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055
Guido van Rossumd8faa362007-04-27 19:54:29 +00002056 count_comp_for:
2057 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002058 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002059 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08002060 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002061 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002062 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002063 else if (NCH(n) == 1) {
2064 n = CHILD(n, 0);
2065 }
2066 else {
2067 goto error;
2068 }
2069 if (NCH(n) == (5)) {
2070 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002071 }
2072 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002073 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002074 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002075 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002076 REQ(n, comp_iter);
2077 n = CHILD(n, 0);
2078 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002079 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002080 else if (TYPE(n) == comp_if) {
2081 if (NCH(n) == 3) {
2082 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002083 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002084 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002085 else
2086 return n_fors;
2087 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002088
Jelle Zijlstraac317702017-10-05 20:24:46 -07002089 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002090 /* Should never be reached */
2091 PyErr_SetString(PyExc_SystemError,
2092 "logic error in count_comp_fors");
2093 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094}
2095
Nick Coghlan650f0d02007-04-15 12:05:43 +00002096/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097
Nick Coghlan650f0d02007-04-15 12:05:43 +00002098 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099*/
2100
2101static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002102count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002104 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105
Guido van Rossumd8faa362007-04-27 19:54:29 +00002106 while (1) {
2107 REQ(n, comp_iter);
2108 if (TYPE(CHILD(n, 0)) == comp_for)
2109 return n_ifs;
2110 n = CHILD(n, 0);
2111 REQ(n, comp_if);
2112 n_ifs++;
2113 if (NCH(n) == 2)
2114 return n_ifs;
2115 n = CHILD(n, 2);
2116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117}
2118
Guido van Rossum992d4a32007-07-11 13:09:30 +00002119static asdl_seq *
2120ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002123 asdl_seq *comps;
2124
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002125 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 if (n_fors == -1)
2127 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002128
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002129 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002130 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002134 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002136 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002138 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002139 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140
Guido van Rossum992d4a32007-07-11 13:09:30 +00002141 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142
Jelle Zijlstraac317702017-10-05 20:24:46 -07002143 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002144 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002145 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002146 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002147 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002148 else {
2149 sync_n = CHILD(n, 0);
2150 }
2151 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002152
Guido van Rossum495da292019-03-07 12:38:08 -08002153 /* Async comprehensions only allowed in Python 3.6 and greater */
2154 if (is_async && c->c_feature_version < 6) {
2155 ast_error(c, n,
2156 "Async comprehensions are only supported in Python 3.6 and greater");
2157 return NULL;
2158 }
2159
Jelle Zijlstraac317702017-10-05 20:24:46 -07002160 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002162 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002164 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002165 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002167
Thomas Wouters89f507f2006-12-13 04:49:30 +00002168 /* Check the # of children rather than the length of t, since
2169 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002170 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002172 comp = comprehension(first, expression, NULL,
2173 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002175 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2176 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2177 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002178 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002179 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002181
Jelle Zijlstraac317702017-10-05 20:24:46 -07002182 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 int j, n_ifs;
2184 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185
Jelle Zijlstraac317702017-10-05 20:24:46 -07002186 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002187 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002188 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002190
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002191 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002192 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002196 REQ(n, comp_iter);
2197 n = CHILD(n, 0);
2198 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199
Guido van Rossum992d4a32007-07-11 13:09:30 +00002200 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002201 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002202 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002203 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002204 if (NCH(n) == 3)
2205 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002207 /* on exit, must guarantee that n is a comp_for */
2208 if (TYPE(n) == comp_iter)
2209 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002210 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002212 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002214 return comps;
2215}
2216
2217static expr_ty
2218ast_for_itercomp(struct compiling *c, const node *n, int type)
2219{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002220 /* testlist_comp: (test|star_expr)
2221 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002222 expr_ty elt;
2223 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002224 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225
Guido van Rossum992d4a32007-07-11 13:09:30 +00002226 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002228 ch = CHILD(n, 0);
2229 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002230 if (!elt)
2231 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002232 if (elt->kind == Starred_kind) {
2233 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2234 return NULL;
2235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236
Guido van Rossum992d4a32007-07-11 13:09:30 +00002237 comps = ast_for_comprehension(c, CHILD(n, 1));
2238 if (!comps)
2239 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002240
2241 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002242 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2243 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002244 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002245 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2246 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002247 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002248 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2249 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002250 else
2251 /* Should never happen */
2252 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253}
2254
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002255/* Fills in the key, value pair corresponding to the dict element. In case
2256 * of an unpacking, key is NULL. *i is advanced by the number of ast
2257 * elements. Iff successful, nonzero is returned.
2258 */
2259static int
2260ast_for_dictelement(struct compiling *c, const node *n, int *i,
2261 expr_ty *key, expr_ty *value)
2262{
2263 expr_ty expression;
2264 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2265 assert(NCH(n) - *i >= 2);
2266
2267 expression = ast_for_expr(c, CHILD(n, *i + 1));
2268 if (!expression)
2269 return 0;
2270 *key = NULL;
2271 *value = expression;
2272
2273 *i += 2;
2274 }
2275 else {
2276 assert(NCH(n) - *i >= 3);
2277
2278 expression = ast_for_expr(c, CHILD(n, *i));
2279 if (!expression)
2280 return 0;
2281 *key = expression;
2282
2283 REQ(CHILD(n, *i + 1), COLON);
2284
2285 expression = ast_for_expr(c, CHILD(n, *i + 2));
2286 if (!expression)
2287 return 0;
2288 *value = expression;
2289
2290 *i += 3;
2291 }
2292 return 1;
2293}
2294
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002296ast_for_dictcomp(struct compiling *c, const node *n)
2297{
2298 expr_ty key, value;
2299 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002300 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002302 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002303 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002304 assert(key);
2305 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002307 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002308 if (!comps)
2309 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002311 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2312 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002313}
2314
2315static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002316ast_for_dictdisplay(struct compiling *c, const node *n)
2317{
2318 int i;
2319 int j;
2320 int size;
2321 asdl_seq *keys, *values;
2322
2323 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2324 keys = _Py_asdl_seq_new(size, c->c_arena);
2325 if (!keys)
2326 return NULL;
2327
2328 values = _Py_asdl_seq_new(size, c->c_arena);
2329 if (!values)
2330 return NULL;
2331
2332 j = 0;
2333 for (i = 0; i < NCH(n); i++) {
2334 expr_ty key, value;
2335
2336 if (!ast_for_dictelement(c, n, &i, &key, &value))
2337 return NULL;
2338 asdl_seq_SET(keys, j, key);
2339 asdl_seq_SET(values, j, value);
2340
2341 j++;
2342 }
2343 keys->size = j;
2344 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002345 return Dict(keys, values, LINENO(n), n->n_col_offset,
2346 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002347}
2348
2349static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002350ast_for_genexp(struct compiling *c, const node *n)
2351{
2352 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002353 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002354}
2355
2356static expr_ty
2357ast_for_listcomp(struct compiling *c, const node *n)
2358{
2359 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002360 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002361}
2362
2363static expr_ty
2364ast_for_setcomp(struct compiling *c, const node *n)
2365{
2366 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002367 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002368}
2369
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002370static expr_ty
2371ast_for_setdisplay(struct compiling *c, const node *n)
2372{
2373 int i;
2374 int size;
2375 asdl_seq *elts;
2376
2377 assert(TYPE(n) == (dictorsetmaker));
2378 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2379 elts = _Py_asdl_seq_new(size, c->c_arena);
2380 if (!elts)
2381 return NULL;
2382 for (i = 0; i < NCH(n); i += 2) {
2383 expr_ty expression;
2384 expression = ast_for_expr(c, CHILD(n, i));
2385 if (!expression)
2386 return NULL;
2387 asdl_seq_SET(elts, i / 2, expression);
2388 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002389 return Set(elts, LINENO(n), n->n_col_offset,
2390 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002391}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002392
2393static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394ast_for_atom(struct compiling *c, const node *n)
2395{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002396 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2397 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002398 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 */
2400 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002403 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002404 PyObject *name;
2405 const char *s = STR(ch);
2406 size_t len = strlen(s);
2407 if (len >= 4 && len <= 5) {
2408 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002409 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002410 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002411 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002412 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002413 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002414 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002415 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002416 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002417 }
2418 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002419 if (!name)
2420 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002421 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002422 return Name(name, Load, LINENO(n), n->n_col_offset,
2423 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002424 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002426 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002427 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002428 const char *errtype = NULL;
2429 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2430 errtype = "unicode error";
2431 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2432 errtype = "value error";
2433 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002434 PyObject *type, *value, *tback, *errstr;
2435 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002436 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002437 if (errstr) {
2438 ast_error(c, n, "(%s) %U", errtype, errstr);
2439 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002440 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002441 else {
2442 PyErr_Clear();
2443 ast_error(c, n, "(%s) unknown error", errtype);
2444 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002445 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002446 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002447 Py_XDECREF(tback);
2448 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002449 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002450 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002451 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 }
2453 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002454 PyObject *pynum;
2455 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2456 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2457 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2458 ast_error(c, ch,
2459 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2460 return NULL;
2461 }
2462 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002463 if (!pynum)
2464 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002465
Victor Stinner43d81952013-07-17 00:57:58 +02002466 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2467 Py_DECREF(pynum);
2468 return NULL;
2469 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002470 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002471 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 }
Georg Brandldde00282007-03-18 19:01:53 +00002473 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002474 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002475 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002480 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2481 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482
Thomas Wouters89f507f2006-12-13 04:49:30 +00002483 if (TYPE(ch) == yield_expr)
2484 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002487 if (NCH(ch) == 1) {
2488 return ast_for_testlist(c, ch);
2489 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002490
Serhiy Storchakab619b092018-11-27 09:40:29 +02002491 if (TYPE(CHILD(ch, 1)) == comp_for) {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002492 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002493 }
2494 else {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002495 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002498 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499
Thomas Wouters89f507f2006-12-13 04:49:30 +00002500 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002501 return List(NULL, Load, LINENO(n), n->n_col_offset,
2502 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503
Nick Coghlan650f0d02007-04-15 12:05:43 +00002504 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002505 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2506 asdl_seq *elts = seq_for_testlist(c, ch);
2507 if (!elts)
2508 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002509
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002510 return List(elts, Load, LINENO(n), n->n_col_offset,
2511 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002512 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002513 else {
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002514 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002517 /* dictorsetmaker: ( ((test ':' test | '**' test)
2518 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2519 * ((test | '*' test)
2520 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002521 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002522 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002523 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002524 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002525 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2526 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 }
2528 else {
2529 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2530 if (NCH(ch) == 1 ||
2531 (NCH(ch) > 1 &&
2532 TYPE(CHILD(ch, 1)) == COMMA)) {
2533 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002534 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002535 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002536 else if (NCH(ch) > 1 &&
2537 TYPE(CHILD(ch, 1)) == comp_for) {
2538 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002539 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002540 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002541 else if (NCH(ch) > 3 - is_dict &&
2542 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2543 /* It's a dictionary comprehension. */
2544 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002545 ast_error(c, n,
2546 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002547 return NULL;
2548 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002549 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002550 }
2551 else {
2552 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002553 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002554 }
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08002555 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002559 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2560 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
2562}
2563
2564static slice_ty
2565ast_for_slice(struct compiling *c, const node *n)
2566{
2567 node *ch;
2568 expr_ty lower = NULL, upper = NULL, step = NULL;
2569
2570 REQ(n, subscript);
2571
2572 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002573 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 sliceop: ':' [test]
2575 */
2576 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 if (NCH(n) == 1 && TYPE(ch) == test) {
2578 /* 'step' variable hold no significance in terms of being used over
2579 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 if (!step)
2582 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583
Thomas Wouters89f507f2006-12-13 04:49:30 +00002584 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 }
2586
2587 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002588 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 if (!lower)
2590 return NULL;
2591 }
2592
2593 /* If there's an upper bound it's in the second or third position. */
2594 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002595 if (NCH(n) > 1) {
2596 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597
Thomas Wouters89f507f2006-12-13 04:49:30 +00002598 if (TYPE(n2) == test) {
2599 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 if (!upper)
2601 return NULL;
2602 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002605 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
Thomas Wouters89f507f2006-12-13 04:49:30 +00002607 if (TYPE(n2) == test) {
2608 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 if (!upper)
2610 return NULL;
2611 }
2612 }
2613
2614 ch = CHILD(n, NCH(n) - 1);
2615 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002616 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002617 ch = CHILD(ch, 1);
2618 if (TYPE(ch) == test) {
2619 step = ast_for_expr(c, ch);
2620 if (!step)
2621 return NULL;
2622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 }
2624 }
2625
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002626 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627}
2628
2629static expr_ty
2630ast_for_binop(struct compiling *c, const node *n)
2631{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002632 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002634 BinOp(BinOp(A, op, B), op, C).
2635 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636
Guido van Rossumd8faa362007-04-27 19:54:29 +00002637 int i, nops;
2638 expr_ty expr1, expr2, result;
2639 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640
Guido van Rossumd8faa362007-04-27 19:54:29 +00002641 expr1 = ast_for_expr(c, CHILD(n, 0));
2642 if (!expr1)
2643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644
Guido van Rossumd8faa362007-04-27 19:54:29 +00002645 expr2 = ast_for_expr(c, CHILD(n, 2));
2646 if (!expr2)
2647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648
Guido van Rossum495da292019-03-07 12:38:08 -08002649 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002650 if (!newoperator)
2651 return NULL;
2652
2653 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002654 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002655 c->c_arena);
2656 if (!result)
2657 return NULL;
2658
2659 nops = (NCH(n) - 1) / 2;
2660 for (i = 1; i < nops; i++) {
2661 expr_ty tmp_result, tmp;
2662 const node* next_oper = CHILD(n, i * 2 + 1);
2663
Guido van Rossum495da292019-03-07 12:38:08 -08002664 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002665 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
2667
Guido van Rossumd8faa362007-04-27 19:54:29 +00002668 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2669 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 return NULL;
2671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 tmp_result = BinOp(result, newoperator, tmp,
Miss Islington (bot)c7be35c2019-07-08 14:41:34 -07002673 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002674 CHILD(n, i * 2 + 2)->n_end_lineno,
2675 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002676 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002678 return NULL;
2679 result = tmp_result;
2680 }
2681 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682}
2683
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002684static expr_ty
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002685ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002688 subscriptlist: subscript (',' subscript)* [',']
2689 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2690 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002691 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002692 REQ(n, trailer);
2693 if (TYPE(CHILD(n, 0)) == LPAR) {
2694 if (NCH(n) == 2)
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002695 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002696 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002697 else
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002698 return ast_for_call(c, CHILD(n, 1), left_expr,
2699 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002700 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002701 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002702 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2703 if (!attr_id)
2704 return NULL;
2705 return Attribute(left_expr, attr_id, Load,
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002706 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002707 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002708 }
2709 else {
2710 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002711 REQ(CHILD(n, 2), RSQB);
2712 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002713 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002714 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2715 if (!slc)
2716 return NULL;
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002717 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002718 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002719 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002720 }
2721 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002723 by treating the sequence as a tuple literal if there are
2724 no slice features.
2725 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002726 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002727 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002728 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002729 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002730 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002731 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002732 if (!slices)
2733 return NULL;
2734 for (j = 0; j < NCH(n); j += 2) {
2735 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002736 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002737 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002738 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002739 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002740 asdl_seq_SET(slices, j / 2, slc);
2741 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002742 if (!simple) {
2743 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002744 Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002745 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002746 }
2747 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002748 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002749 if (!elts)
2750 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002751 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2752 slc = (slice_ty)asdl_seq_GET(slices, j);
2753 assert(slc->kind == Index_kind && slc->v.Index.value);
2754 asdl_seq_SET(elts, j, slc->v.Index.value);
2755 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002756 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2757 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002758 if (!e)
2759 return NULL;
2760 return Subscript(left_expr, Index(e, c->c_arena),
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002761 Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002762 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002763 }
2764 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002765}
2766
2767static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002768ast_for_factor(struct compiling *c, const node *n)
2769{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002770 expr_ty expression;
2771
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002772 expression = ast_for_expr(c, CHILD(n, 1));
2773 if (!expression)
2774 return NULL;
2775
2776 switch (TYPE(CHILD(n, 0))) {
2777 case PLUS:
2778 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002779 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002780 c->c_arena);
2781 case MINUS:
2782 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002783 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002784 c->c_arena);
2785 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002786 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2787 n->n_end_lineno, n->n_end_col_offset,
2788 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002789 }
2790 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2791 TYPE(CHILD(n, 0)));
2792 return NULL;
2793}
2794
2795static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002796ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002797{
Yury Selivanov75445082015-05-11 22:57:16 -04002798 int i, nch, start = 0;
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002799 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002800
2801 REQ(n, atom_expr);
2802 nch = NCH(n);
2803
Guido van Rossum495da292019-03-07 12:38:08 -08002804 if (TYPE(CHILD(n, 0)) == AWAIT) {
2805 if (c->c_feature_version < 5) {
2806 ast_error(c, n,
2807 "Await expressions are only supported in Python 3.5 and greater");
2808 return NULL;
2809 }
Yury Selivanov75445082015-05-11 22:57:16 -04002810 start = 1;
2811 assert(nch > 1);
2812 }
2813
2814 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002815 if (!e)
2816 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002817 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002818 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002819 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002820 return Await(e, LINENO(n), n->n_col_offset,
2821 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002822 }
2823
2824 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002825 node *ch = CHILD(n, i);
2826 if (TYPE(ch) != trailer)
2827 break;
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08002828 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2829 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002830 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002831 }
Yury Selivanov75445082015-05-11 22:57:16 -04002832
2833 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002834 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002835 return Await(e, LINENO(n), n->n_col_offset,
2836 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002837 }
2838 else {
2839 return e;
2840 }
2841}
2842
2843static expr_ty
2844ast_for_power(struct compiling *c, const node *n)
2845{
2846 /* power: atom trailer* ('**' factor)*
2847 */
2848 expr_ty e;
2849 REQ(n, power);
2850 e = ast_for_atom_expr(c, CHILD(n, 0));
2851 if (!e)
2852 return NULL;
2853 if (NCH(n) == 1)
2854 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002855 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2856 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002857 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002858 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002859 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2860 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002861 }
2862 return e;
2863}
2864
Guido van Rossum0368b722007-05-11 16:50:42 +00002865static expr_ty
2866ast_for_starred(struct compiling *c, const node *n)
2867{
2868 expr_ty tmp;
2869 REQ(n, star_expr);
2870
2871 tmp = ast_for_expr(c, CHILD(n, 1));
2872 if (!tmp)
2873 return NULL;
2874
2875 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002876 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2877 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002878}
2879
2880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881/* Do not name a variable 'expr'! Will cause a compile error.
2882*/
2883
2884static expr_ty
2885ast_for_expr(struct compiling *c, const node *n)
2886{
2887 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002888 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002889 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002890 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 and_test: not_test ('and' not_test)*
2893 not_test: 'not' not_test | comparison
2894 comparison: expr (comp_op expr)*
2895 expr: xor_expr ('|' xor_expr)*
2896 xor_expr: and_expr ('^' and_expr)*
2897 and_expr: shift_expr ('&' shift_expr)*
2898 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2899 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002900 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002902 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002903 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002904 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 */
2906
2907 asdl_seq *seq;
2908 int i;
2909
2910 loop:
2911 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002912 case namedexpr_test:
2913 if (NCH(n) == 3)
2914 return ast_for_namedexpr(c, n);
2915 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002917 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002918 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002919 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002921 else if (NCH(n) > 1)
2922 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002923 /* Fallthrough */
2924 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 case and_test:
2926 if (NCH(n) == 1) {
2927 n = CHILD(n, 0);
2928 goto loop;
2929 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002930 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 if (!seq)
2932 return NULL;
2933 for (i = 0; i < NCH(n); i += 2) {
2934 expr_ty e = ast_for_expr(c, CHILD(n, i));
2935 if (!e)
2936 return NULL;
2937 asdl_seq_SET(seq, i / 2, e);
2938 }
2939 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002940 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002941 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002942 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002943 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002944 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2945 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 case not_test:
2947 if (NCH(n) == 1) {
2948 n = CHILD(n, 0);
2949 goto loop;
2950 }
2951 else {
2952 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2953 if (!expression)
2954 return NULL;
2955
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002956 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002957 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002958 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 }
2960 case comparison:
2961 if (NCH(n) == 1) {
2962 n = CHILD(n, 0);
2963 goto loop;
2964 }
2965 else {
2966 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002967 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002968 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002969 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 if (!ops)
2971 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002972 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 return NULL;
2975 }
2976 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002977 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002979 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002980 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002982 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983
2984 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002985 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002989 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 asdl_seq_SET(cmps, i / 2, expression);
2991 }
2992 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002993 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002997 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2998 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000
Guido van Rossum0368b722007-05-11 16:50:42 +00003001 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 /* The next five cases all handle BinOps. The main body of code
3004 is the same in each case, but the switch turned inside out to
3005 reuse the code for each type of operator.
3006 */
3007 case expr:
3008 case xor_expr:
3009 case and_expr:
3010 case shift_expr:
3011 case arith_expr:
3012 case term:
3013 if (NCH(n) == 1) {
3014 n = CHILD(n, 0);
3015 goto loop;
3016 }
3017 return ast_for_binop(c, n);
3018 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003019 node *an = NULL;
3020 node *en = NULL;
3021 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003022 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003023 if (NCH(n) > 1)
3024 an = CHILD(n, 1); /* yield_arg */
3025 if (an) {
3026 en = CHILD(an, NCH(an) - 1);
3027 if (NCH(an) == 2) {
3028 is_from = 1;
3029 exp = ast_for_expr(c, en);
3030 }
3031 else
3032 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003033 if (!exp)
3034 return NULL;
3035 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05003036 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003037 return YieldFrom(exp, LINENO(n), n->n_col_offset,
3038 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
3039 return Yield(exp, LINENO(n), n->n_col_offset,
3040 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003041 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003042 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 if (NCH(n) == 1) {
3044 n = CHILD(n, 0);
3045 goto loop;
3046 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003047 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00003048 case power:
3049 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003051 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 return NULL;
3053 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003054 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 return NULL;
3056}
3057
3058static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02003059ast_for_call(struct compiling *c, const node *n, expr_ty func,
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08003060 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061{
3062 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003063 arglist: argument (',' argument)* [',']
3064 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 */
3066
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003067 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003068 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003069 asdl_seq *args;
3070 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071
3072 REQ(n, arglist);
3073
3074 nargs = 0;
3075 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003077 node *ch = CHILD(n, i);
3078 if (TYPE(ch) == argument) {
3079 if (NCH(ch) == 1)
3080 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003081 else if (TYPE(CHILD(ch, 1)) == comp_for) {
3082 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02003083 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003084 ast_error(c, ch, "invalid syntax");
3085 return NULL;
3086 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003087 if (NCH(n) > 1) {
3088 ast_error(c, ch, "Generator expression must be parenthesized");
3089 return NULL;
3090 }
3091 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003092 else if (TYPE(CHILD(ch, 0)) == STAR)
3093 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003094 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3095 nargs++;
3096 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003098 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003099 nkeywords++;
3100 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003103 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003105 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003106 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003108 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109
3110 nargs = 0; /* positional arguments + iterable argument unpackings */
3111 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3112 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003114 node *ch = CHILD(n, i);
3115 if (TYPE(ch) == argument) {
3116 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003117 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003118 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003119 /* a positional argument */
3120 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003121 if (ndoublestars) {
3122 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003123 "positional argument follows "
3124 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003125 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003126 else {
3127 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003128 "positional argument follows "
3129 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003130 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003131 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003132 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003133 e = ast_for_expr(c, chch);
3134 if (!e)
3135 return NULL;
3136 asdl_seq_SET(args, nargs++, e);
3137 }
3138 else if (TYPE(chch) == STAR) {
3139 /* an iterable argument unpacking */
3140 expr_ty starred;
3141 if (ndoublestars) {
3142 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003143 "iterable argument unpacking follows "
3144 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003145 return NULL;
3146 }
3147 e = ast_for_expr(c, CHILD(ch, 1));
3148 if (!e)
3149 return NULL;
3150 starred = Starred(e, Load, LINENO(chch),
3151 chch->n_col_offset,
Pablo Galindob1f20442019-12-18 01:41:58 +00003152 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003153 c->c_arena);
3154 if (!starred)
3155 return NULL;
3156 asdl_seq_SET(args, nargs++, starred);
3157
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003158 }
3159 else if (TYPE(chch) == DOUBLESTAR) {
3160 /* a keyword argument unpacking */
3161 keyword_ty kw;
3162 i++;
3163 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003165 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003166 kw = keyword(NULL, e, c->c_arena);
3167 asdl_seq_SET(keywords, nkeywords++, kw);
3168 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003170 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003171 /* the lone generator expression */
Miss Islington (bot)33e033d2020-01-09 11:39:00 -08003172 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003174 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003175 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003177 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3178 /* treat colon equal as positional argument */
3179 if (nkeywords) {
3180 if (ndoublestars) {
3181 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003182 "positional argument follows "
3183 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003184 }
3185 else {
3186 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003187 "positional argument follows "
3188 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003189 }
3190 return NULL;
3191 }
3192 e = ast_for_namedexpr(c, ch);
3193 if (!e)
3194 return NULL;
3195 asdl_seq_SET(args, nargs++, e);
3196 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003197 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003198 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003200 identifier key, tmp;
3201 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003203 // To remain LL(1), the grammar accepts any test (basically, any
3204 // expression) in the keyword slot of a call site. So, we need
3205 // to manually enforce that the keyword is a NAME here.
3206 static const int name_tree[] = {
3207 test,
3208 or_test,
3209 and_test,
3210 not_test,
3211 comparison,
3212 expr,
3213 xor_expr,
3214 and_expr,
3215 shift_expr,
3216 arith_expr,
3217 term,
3218 factor,
3219 power,
3220 atom_expr,
3221 atom,
3222 0,
3223 };
3224 node *expr_node = chch;
3225 for (int i = 0; name_tree[i]; i++) {
3226 if (TYPE(expr_node) != name_tree[i])
3227 break;
3228 if (NCH(expr_node) != 1)
3229 break;
3230 expr_node = CHILD(expr_node, 0);
3231 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003232 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003233 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003234 "expression cannot contain assignment, "
3235 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003236 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003237 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003238 key = new_identifier(STR(expr_node), c);
3239 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003240 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003242 if (forbidden_name(c, key, chch, 1)) {
3243 return NULL;
3244 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003245 for (k = 0; k < nkeywords; k++) {
3246 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003247 if (tmp && !PyUnicode_Compare(tmp, key)) {
3248 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003249 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003250 return NULL;
3251 }
3252 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003253 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003255 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003256 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003258 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003259 asdl_seq_SET(keywords, nkeywords++, kw);
3260 }
3261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 }
3263
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08003264 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003265 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266}
3267
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003269ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003271 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003272 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003274 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003275 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003276 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003277 }
3278 else {
3279 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003280 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003281 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003283 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 else {
3285 asdl_seq *tmp = seq_for_testlist(c, n);
3286 if (!tmp)
3287 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003288 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3289 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003291}
3292
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293static stmt_ty
3294ast_for_expr_stmt(struct compiling *c, const node *n)
3295{
3296 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003297 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003298 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3299 annassign: ':' test ['=' (yield_expr|testlist)]
3300 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3301 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3302 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003303 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003305 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003307 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 if (!e)
3310 return NULL;
3311
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003312 return Expr(e, LINENO(n), n->n_col_offset,
3313 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314 }
3315 else if (TYPE(CHILD(n, 1)) == augassign) {
3316 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 if (!expr1)
3322 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003323 if(!set_context(c, expr1, Store, ch))
3324 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003325 /* set_context checks that most expressions are not the left side.
3326 Augmented assignments can only have a name, a subscript, or an
3327 attribute on the left, though, so we have to explicitly check for
3328 those. */
3329 switch (expr1->kind) {
3330 case Name_kind:
3331 case Attribute_kind:
3332 case Subscript_kind:
3333 break;
3334 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003335 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003336 return NULL;
3337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 ch = CHILD(n, 2);
3340 if (TYPE(ch) == testlist)
3341 expr2 = ast_for_testlist(c, ch);
3342 else
3343 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003344 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 return NULL;
3346
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003347 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003348 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 return NULL;
3350
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003351 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3352 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003354 else if (TYPE(CHILD(n, 1)) == annassign) {
3355 expr_ty expr1, expr2, expr3;
3356 node *ch = CHILD(n, 0);
3357 node *deep, *ann = CHILD(n, 1);
3358 int simple = 1;
3359
Guido van Rossum495da292019-03-07 12:38:08 -08003360 /* AnnAssigns are only allowed in Python 3.6 or greater */
3361 if (c->c_feature_version < 6) {
3362 ast_error(c, ch,
3363 "Variable annotation syntax is only supported in Python 3.6 and greater");
3364 return NULL;
3365 }
3366
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003367 /* we keep track of parens to qualify (x) as expression not name */
3368 deep = ch;
3369 while (NCH(deep) == 1) {
3370 deep = CHILD(deep, 0);
3371 }
3372 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3373 simple = 0;
3374 }
3375 expr1 = ast_for_testlist(c, ch);
3376 if (!expr1) {
3377 return NULL;
3378 }
3379 switch (expr1->kind) {
3380 case Name_kind:
3381 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3382 return NULL;
3383 }
3384 expr1->v.Name.ctx = Store;
3385 break;
3386 case Attribute_kind:
3387 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3388 return NULL;
3389 }
3390 expr1->v.Attribute.ctx = Store;
3391 break;
3392 case Subscript_kind:
3393 expr1->v.Subscript.ctx = Store;
3394 break;
3395 case List_kind:
3396 ast_error(c, ch,
3397 "only single target (not list) can be annotated");
3398 return NULL;
3399 case Tuple_kind:
3400 ast_error(c, ch,
3401 "only single target (not tuple) can be annotated");
3402 return NULL;
3403 default:
3404 ast_error(c, ch,
3405 "illegal target for annotation");
3406 return NULL;
3407 }
3408
3409 if (expr1->kind != Name_kind) {
3410 simple = 0;
3411 }
3412 ch = CHILD(ann, 1);
3413 expr2 = ast_for_expr(c, ch);
3414 if (!expr2) {
3415 return NULL;
3416 }
3417 if (NCH(ann) == 2) {
3418 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003419 LINENO(n), n->n_col_offset,
3420 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003421 }
3422 else {
3423 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003424 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003425 expr3 = ast_for_testlist(c, ch);
3426 }
3427 else {
3428 expr3 = ast_for_expr(c, ch);
3429 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003430 if (!expr3) {
3431 return NULL;
3432 }
3433 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003434 LINENO(n), n->n_col_offset,
3435 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003436 }
3437 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003439 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003440 asdl_seq *targets;
3441 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003443 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444
Thomas Wouters89f507f2006-12-13 04:49:30 +00003445 /* a normal assignment */
3446 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003447
3448 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3449 nch_minus_type = num - has_type_comment;
3450
3451 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 if (!targets)
3453 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003454 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003455 expr_ty e;
3456 node *ch = CHILD(n, i);
3457 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003458 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003459 return NULL;
3460 }
3461 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003465 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003466 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003467 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468
Thomas Wouters89f507f2006-12-13 04:49:30 +00003469 asdl_seq_SET(targets, i / 2, e);
3470 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003471 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003472 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003473 expression = ast_for_testlist(c, value);
3474 else
3475 expression = ast_for_expr(c, value);
3476 if (!expression)
3477 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003478 if (has_type_comment) {
3479 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3480 if (!type_comment)
3481 return NULL;
3482 }
3483 else
3484 type_comment = NULL;
3485 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003486 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488}
3489
Benjamin Peterson78565b22009-06-28 19:19:51 +00003490
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003492ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493{
3494 asdl_seq *seq;
3495 int i;
3496 expr_ty e;
3497
3498 REQ(n, exprlist);
3499
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003500 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003502 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003504 e = ast_for_expr(c, CHILD(n, i));
3505 if (!e)
3506 return NULL;
3507 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003508 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 }
3511 return seq;
3512}
3513
3514static stmt_ty
3515ast_for_del_stmt(struct compiling *c, const node *n)
3516{
3517 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 /* del_stmt: 'del' exprlist */
3520 REQ(n, del_stmt);
3521
3522 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3523 if (!expr_list)
3524 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003525 return Delete(expr_list, LINENO(n), n->n_col_offset,
3526 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527}
3528
3529static stmt_ty
3530ast_for_flow_stmt(struct compiling *c, const node *n)
3531{
3532 /*
3533 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3534 | yield_stmt
3535 break_stmt: 'break'
3536 continue_stmt: 'continue'
3537 return_stmt: 'return' [testlist]
3538 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003539 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 raise_stmt: 'raise' [test [',' test [',' test]]]
3541 */
3542 node *ch;
3543
3544 REQ(n, flow_stmt);
3545 ch = CHILD(n, 0);
3546 switch (TYPE(ch)) {
3547 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003548 return Break(LINENO(n), n->n_col_offset,
3549 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003551 return Continue(LINENO(n), n->n_col_offset,
3552 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003554 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3555 if (!exp)
3556 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003557 return Expr(exp, LINENO(n), n->n_col_offset,
3558 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 }
3560 case return_stmt:
3561 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003562 return Return(NULL, LINENO(n), n->n_col_offset,
3563 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003565 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 if (!expression)
3567 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003568 return Return(expression, LINENO(n), n->n_col_offset,
3569 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 }
3571 case raise_stmt:
3572 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003573 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3574 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003575 else if (NCH(ch) >= 2) {
3576 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3578 if (!expression)
3579 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003580 if (NCH(ch) == 4) {
3581 cause = ast_for_expr(c, CHILD(ch, 3));
3582 if (!cause)
3583 return NULL;
3584 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003585 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3586 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587 }
Stefan Krahf432a322017-08-21 13:09:59 +02003588 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003590 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 "unexpected flow_stmt: %d", TYPE(ch));
3592 return NULL;
3593 }
3594}
3595
3596static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003597alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598{
3599 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003600 import_as_name: NAME ['as' NAME]
3601 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 dotted_name: NAME ('.' NAME)*
3603 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003604 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 loop:
3607 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003608 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003609 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003610 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003611 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003612 if (!name)
3613 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003614 if (NCH(n) == 3) {
3615 node *str_node = CHILD(n, 2);
3616 str = NEW_IDENTIFIER(str_node);
3617 if (!str)
3618 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003619 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003620 return NULL;
3621 }
3622 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003623 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003624 return NULL;
3625 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003626 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 case dotted_as_name:
3629 if (NCH(n) == 1) {
3630 n = CHILD(n, 0);
3631 goto loop;
3632 }
3633 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003634 node *asname_node = CHILD(n, 2);
3635 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003636 if (!a)
3637 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003639 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003640 if (!a->asname)
3641 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003642 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 return a;
3645 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003647 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003648 node *name_node = CHILD(n, 0);
3649 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003650 if (!name)
3651 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003652 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003653 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003654 return alias(name, NULL, c->c_arena);
3655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 else {
3657 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003658 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003659 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662
3663 len = 0;
3664 for (i = 0; i < NCH(n); i += 2)
3665 /* length of string plus one for the dot */
3666 len += strlen(STR(CHILD(n, i))) + 1;
3667 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003668 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 if (!str)
3670 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003671 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 if (!s)
3673 return NULL;
3674 for (i = 0; i < NCH(n); i += 2) {
3675 char *sch = STR(CHILD(n, i));
3676 strcpy(s, STR(CHILD(n, i)));
3677 s += strlen(sch);
3678 *s++ = '.';
3679 }
3680 --s;
3681 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3683 PyBytes_GET_SIZE(str),
3684 NULL);
3685 Py_DECREF(str);
3686 if (!uni)
3687 return NULL;
3688 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003689 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003690 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3691 Py_DECREF(str);
3692 return NULL;
3693 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003694 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003697 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003698 if (!str)
3699 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003700 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3701 Py_DECREF(str);
3702 return NULL;
3703 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003704 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003706 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 "unexpected import name: %d", TYPE(n));
3708 return NULL;
3709 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003710
3711 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 return NULL;
3713}
3714
3715static stmt_ty
3716ast_for_import_stmt(struct compiling *c, const node *n)
3717{
3718 /*
3719 import_stmt: import_name | import_from
3720 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003721 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3722 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003724 int lineno;
3725 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 int i;
3727 asdl_seq *aliases;
3728
3729 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003730 lineno = LINENO(n);
3731 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003733 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003735 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003736 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003737 if (!aliases)
3738 return NULL;
3739 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003740 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003741 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003743 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003745 // Even though n is modified above, the end position is not changed
3746 return Import(aliases, lineno, col_offset,
3747 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003749 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003751 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003752 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003753 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003754 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003756 /* Count the number of dots (for relative imports) and check for the
3757 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003758 for (idx = 1; idx < NCH(n); idx++) {
3759 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003760 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3761 if (!mod)
3762 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003763 idx++;
3764 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003765 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003767 ndots += 3;
3768 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003769 } else if (TYPE(CHILD(n, idx)) != DOT) {
3770 break;
3771 }
3772 ndots++;
3773 }
3774 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003775 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003776 case STAR:
3777 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003778 n = CHILD(n, idx);
3779 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003780 break;
3781 case LPAR:
3782 /* from ... import (x, y, z) */
3783 n = CHILD(n, idx + 1);
3784 n_children = NCH(n);
3785 break;
3786 case import_as_names:
3787 /* from ... import x, y, z */
3788 n = CHILD(n, idx);
3789 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003790 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003791 ast_error(c, n,
3792 "trailing comma not allowed without"
3793 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 return NULL;
3795 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003796 break;
3797 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003798 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003799 return NULL;
3800 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003802 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003803 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805
3806 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003807 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003808 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003809 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003811 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003813 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003814 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003815 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003816 if (!import_alias)
3817 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003818 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003821 if (mod != NULL)
3822 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003823 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003824 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003825 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 }
Neal Norwitz79792652005-11-14 04:25:03 +00003827 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 "unknown import statement: starts with command '%s'",
3829 STR(CHILD(n, 0)));
3830 return NULL;
3831}
3832
3833static stmt_ty
3834ast_for_global_stmt(struct compiling *c, const node *n)
3835{
3836 /* global_stmt: 'global' NAME (',' NAME)* */
3837 identifier name;
3838 asdl_seq *s;
3839 int i;
3840
3841 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003842 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003846 name = NEW_IDENTIFIER(CHILD(n, i));
3847 if (!name)
3848 return NULL;
3849 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003851 return Global(s, LINENO(n), n->n_col_offset,
3852 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853}
3854
3855static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003856ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3857{
3858 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3859 identifier name;
3860 asdl_seq *s;
3861 int i;
3862
3863 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003864 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003865 if (!s)
3866 return NULL;
3867 for (i = 1; i < NCH(n); i += 2) {
3868 name = NEW_IDENTIFIER(CHILD(n, i));
3869 if (!name)
3870 return NULL;
3871 asdl_seq_SET(s, i / 2, name);
3872 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003873 return Nonlocal(s, LINENO(n), n->n_col_offset,
3874 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003875}
3876
3877static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878ast_for_assert_stmt(struct compiling *c, const node *n)
3879{
3880 /* assert_stmt: 'assert' test [',' test] */
3881 REQ(n, assert_stmt);
3882 if (NCH(n) == 2) {
3883 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3884 if (!expression)
3885 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003886 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3887 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888 }
3889 else if (NCH(n) == 4) {
3890 expr_ty expr1, expr2;
3891
3892 expr1 = ast_for_expr(c, CHILD(n, 1));
3893 if (!expr1)
3894 return NULL;
3895 expr2 = ast_for_expr(c, CHILD(n, 3));
3896 if (!expr2)
3897 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003899 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3900 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 }
Neal Norwitz79792652005-11-14 04:25:03 +00003902 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 "improper number of parts to 'assert' statement: %d",
3904 NCH(n));
3905 return NULL;
3906}
3907
3908static asdl_seq *
3909ast_for_suite(struct compiling *c, const node *n)
3910{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003911 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003912 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 stmt_ty s;
3914 int i, total, num, end, pos = 0;
3915 node *ch;
3916
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003917 if (TYPE(n) != func_body_suite) {
3918 REQ(n, suite);
3919 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920
3921 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003922 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003926 n = CHILD(n, 0);
3927 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003929 */
3930 end = NCH(n) - 1;
3931 if (TYPE(CHILD(n, end - 1)) == SEMI)
3932 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003934 for (i = 0; i < end; i += 2) {
3935 ch = CHILD(n, i);
3936 s = ast_for_stmt(c, ch);
3937 if (!s)
3938 return NULL;
3939 asdl_seq_SET(seq, pos++, s);
3940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 }
3942 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003943 i = 2;
3944 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3945 i += 2;
3946 REQ(CHILD(n, 2), NEWLINE);
3947 }
3948
3949 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003950 ch = CHILD(n, i);
3951 REQ(ch, stmt);
3952 num = num_stmts(ch);
3953 if (num == 1) {
3954 /* small_stmt or compound_stmt with only one child */
3955 s = ast_for_stmt(c, ch);
3956 if (!s)
3957 return NULL;
3958 asdl_seq_SET(seq, pos++, s);
3959 }
3960 else {
3961 int j;
3962 ch = CHILD(ch, 0);
3963 REQ(ch, simple_stmt);
3964 for (j = 0; j < NCH(ch); j += 2) {
3965 /* statement terminates with a semi-colon ';' */
3966 if (NCH(CHILD(ch, j)) == 0) {
3967 assert((j + 1) == NCH(ch));
3968 break;
3969 }
3970 s = ast_for_stmt(c, CHILD(ch, j));
3971 if (!s)
3972 return NULL;
3973 asdl_seq_SET(seq, pos++, s);
3974 }
3975 }
3976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 }
3978 assert(pos == seq->size);
3979 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980}
3981
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003982static void
3983get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3984{
Pablo Galindo46a97922019-02-19 22:51:53 +00003985 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003986 // There must be no empty suites.
3987 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003988 stmt_ty last = asdl_seq_GET(s, tot - 1);
3989 *end_lineno = last->end_lineno;
3990 *end_col_offset = last->end_col_offset;
3991}
3992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993static stmt_ty
3994ast_for_if_stmt(struct compiling *c, const node *n)
3995{
3996 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3997 ['else' ':' suite]
3998 */
3999 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004000 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001
4002 REQ(n, if_stmt);
4003
4004 if (NCH(n) == 4) {
4005 expr_ty expression;
4006 asdl_seq *suite_seq;
4007
4008 expression = ast_for_expr(c, CHILD(n, 1));
4009 if (!expression)
4010 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004012 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004014 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015
Guido van Rossumd8faa362007-04-27 19:54:29 +00004016 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004017 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004018 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 s = STR(CHILD(n, 4));
4021 /* s[2], the third character in the string, will be
4022 's' for el_s_e, or
4023 'i' for el_i_f
4024 */
4025 if (s[2] == 's') {
4026 expr_ty expression;
4027 asdl_seq *seq1, *seq2;
4028
4029 expression = ast_for_expr(c, CHILD(n, 1));
4030 if (!expression)
4031 return NULL;
4032 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004033 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 return NULL;
4035 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004036 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004038 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039
Guido van Rossumd8faa362007-04-27 19:54:29 +00004040 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004041 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 }
4043 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004044 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004045 expr_ty expression;
4046 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004047 asdl_seq *orelse = NULL;
4048 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 /* must reference the child n_elif+1 since 'else' token is third,
4050 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004051 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
4052 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
4053 has_else = 1;
4054 n_elif -= 3;
4055 }
4056 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057
Thomas Wouters89f507f2006-12-13 04:49:30 +00004058 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004059 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004061 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004062 if (!orelse)
4063 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004065 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004067 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
4068 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004070 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4071 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004073 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 asdl_seq_SET(orelse, 0,
4076 If(expression, suite_seq, suite_seq2,
Miss Islington (bot)ce333cd2019-12-14 02:43:42 -08004077 LINENO(CHILD(n, NCH(n) - 7)),
4078 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004079 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004080 /* the just-created orelse handled the last elif */
4081 n_elif--;
4082 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083
Thomas Wouters89f507f2006-12-13 04:49:30 +00004084 for (i = 0; i < n_elif; i++) {
4085 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004086 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004087 if (!newobj)
4088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004089 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004090 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004092 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004093 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004095
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004096 if (orelse != NULL) {
4097 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4098 } else {
4099 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4100 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004101 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 If(expression, suite_seq, orelse,
Miss Islington (bot)3b18b172019-12-13 08:21:54 -08004103 LINENO(CHILD(n, off - 1)),
4104 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004105 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004106 orelse = newobj;
4107 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004108 expression = ast_for_expr(c, CHILD(n, 1));
4109 if (!expression)
4110 return NULL;
4111 suite_seq = ast_for_suite(c, CHILD(n, 3));
4112 if (!suite_seq)
4113 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004114 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004115 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004116 LINENO(n), n->n_col_offset,
4117 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004119
4120 PyErr_Format(PyExc_SystemError,
4121 "unexpected token in 'if' statement: %s", s);
4122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123}
4124
4125static stmt_ty
4126ast_for_while_stmt(struct compiling *c, const node *n)
4127{
4128 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4129 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004130 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131
4132 if (NCH(n) == 4) {
4133 expr_ty expression;
4134 asdl_seq *suite_seq;
4135
4136 expression = ast_for_expr(c, CHILD(n, 1));
4137 if (!expression)
4138 return NULL;
4139 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004140 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004142 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4143 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4144 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145 }
4146 else if (NCH(n) == 7) {
4147 expr_ty expression;
4148 asdl_seq *seq1, *seq2;
4149
4150 expression = ast_for_expr(c, CHILD(n, 1));
4151 if (!expression)
4152 return NULL;
4153 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004154 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155 return NULL;
4156 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004157 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004159 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004160
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004161 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4162 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004164
4165 PyErr_Format(PyExc_SystemError,
4166 "wrong number of tokens for 'while' statement: %d",
4167 NCH(n));
4168 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004169}
4170
4171static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004172ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173{
guoci90fc8982018-09-11 17:45:45 -04004174 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004175 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004176 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004177 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004178 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004179 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004180 int has_type_comment;
4181 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004182
4183 if (is_async && c->c_feature_version < 5) {
4184 ast_error(c, n,
4185 "Async for loops are only supported in Python 3.5 and greater");
4186 return NULL;
4187 }
4188
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004189 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190 REQ(n, for_stmt);
4191
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004192 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4193
4194 if (NCH(n) == 9 + has_type_comment) {
4195 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004196 if (!seq)
4197 return NULL;
4198 }
4199
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004200 node_target = CHILD(n, 1);
4201 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004202 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004203 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004204 /* Check the # of children rather than the length of _target, since
4205 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004206 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004207 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004208 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004209 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004210 target = Tuple(_target, Store, first->lineno, first->col_offset,
4211 node_target->n_end_lineno, node_target->n_end_col_offset,
4212 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004213
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004214 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004215 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004216 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004217 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004218 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004219 return NULL;
4220
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004221 if (seq != NULL) {
4222 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4223 } else {
4224 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4225 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004226
4227 if (has_type_comment) {
4228 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4229 if (!type_comment)
4230 return NULL;
4231 }
4232 else
4233 type_comment = NULL;
4234
Yury Selivanov75445082015-05-11 22:57:16 -04004235 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004236 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004237 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004238 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004239 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004240 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004241 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004242 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004243}
4244
4245static excepthandler_ty
4246ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4247{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004248 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004249 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004250 REQ(exc, except_clause);
4251 REQ(body, suite);
4252
4253 if (NCH(exc) == 1) {
4254 asdl_seq *suite_seq = ast_for_suite(c, body);
4255 if (!suite_seq)
4256 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004257 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004258
Neal Norwitzad74aa82008-03-31 05:14:30 +00004259 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004260 exc->n_col_offset,
4261 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004262 }
4263 else if (NCH(exc) == 2) {
4264 expr_ty expression;
4265 asdl_seq *suite_seq;
4266
4267 expression = ast_for_expr(c, CHILD(exc, 1));
4268 if (!expression)
4269 return NULL;
4270 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004271 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004272 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004273 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004274
Neal Norwitzad74aa82008-03-31 05:14:30 +00004275 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004276 exc->n_col_offset,
4277 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004278 }
4279 else if (NCH(exc) == 4) {
4280 asdl_seq *suite_seq;
4281 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004282 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004283 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004284 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004285 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004287 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004288 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004289 return NULL;
4290 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004291 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004292 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004293 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004294
Neal Norwitzad74aa82008-03-31 05:14:30 +00004295 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004296 exc->n_col_offset,
4297 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004298 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004299
4300 PyErr_Format(PyExc_SystemError,
4301 "wrong number of children for 'except' clause: %d",
4302 NCH(exc));
4303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004304}
4305
4306static stmt_ty
4307ast_for_try_stmt(struct compiling *c, const node *n)
4308{
Neal Norwitzf599f422005-12-17 21:33:47 +00004309 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004310 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004311 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004312 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004313
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004314 REQ(n, try_stmt);
4315
Neal Norwitzf599f422005-12-17 21:33:47 +00004316 body = ast_for_suite(c, CHILD(n, 2));
4317 if (body == NULL)
4318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004319
Neal Norwitzf599f422005-12-17 21:33:47 +00004320 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4321 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4322 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4323 /* we can assume it's an "else",
4324 because nch >= 9 for try-else-finally and
4325 it would otherwise have a type of except_clause */
4326 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4327 if (orelse == NULL)
4328 return NULL;
4329 n_except--;
4330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004331
Neal Norwitzf599f422005-12-17 21:33:47 +00004332 finally = ast_for_suite(c, CHILD(n, nch - 1));
4333 if (finally == NULL)
4334 return NULL;
4335 n_except--;
4336 }
4337 else {
4338 /* we can assume it's an "else",
4339 otherwise it would have a type of except_clause */
4340 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4341 if (orelse == NULL)
4342 return NULL;
4343 n_except--;
4344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004345 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004346 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004347 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004348 return NULL;
4349 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350
Neal Norwitzf599f422005-12-17 21:33:47 +00004351 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004352 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004353 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004354 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004355 if (handlers == NULL)
4356 return NULL;
4357
4358 for (i = 0; i < n_except; i++) {
4359 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4360 CHILD(n, 5 + i * 3));
4361 if (!e)
4362 return NULL;
4363 asdl_seq_SET(handlers, i, e);
4364 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004365 }
4366
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004367 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004368 if (finally != NULL) {
4369 // finally is always last
4370 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4371 } else if (orelse != NULL) {
4372 // otherwise else is last
4373 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4374 } else {
4375 // inline the get_last_end_pos logic due to layout mismatch
4376 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4377 end_lineno = last_handler->end_lineno;
4378 end_col_offset = last_handler->end_col_offset;
4379 }
4380 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4381 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004382}
4383
Georg Brandl0c315622009-05-25 21:10:36 +00004384/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004385static withitem_ty
4386ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004387{
4388 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004389
Georg Brandl0c315622009-05-25 21:10:36 +00004390 REQ(n, with_item);
4391 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004392 if (!context_expr)
4393 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004394 if (NCH(n) == 3) {
4395 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004396
4397 if (!optional_vars) {
4398 return NULL;
4399 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004400 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004401 return NULL;
4402 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004403 }
4404
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004405 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004406}
4407
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004408/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004409static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004410ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004411{
guoci90fc8982018-09-11 17:45:45 -04004412 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004413 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004414 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004415 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004416
Guido van Rossum495da292019-03-07 12:38:08 -08004417 if (is_async && c->c_feature_version < 5) {
4418 ast_error(c, n,
4419 "Async with statements are only supported in Python 3.5 and greater");
4420 return NULL;
4421 }
4422
Georg Brandl0c315622009-05-25 21:10:36 +00004423 REQ(n, with_stmt);
4424
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004425 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4426 nch_minus_type = NCH(n) - has_type_comment;
4427
4428 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004429 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004430 if (!items)
4431 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004432 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004433 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4434 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004435 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004436 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004437 }
4438
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004439 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4440 if (!body)
4441 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004442 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004443
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004444 if (has_type_comment) {
4445 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4446 if (!type_comment)
4447 return NULL;
4448 }
4449 else
4450 type_comment = NULL;
4451
Yury Selivanov75445082015-05-11 22:57:16 -04004452 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004453 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004454 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004455 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004456 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004457 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004458}
4459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004461ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004463 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004464 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004465 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004466 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004467 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004469 REQ(n, classdef);
4470
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004471 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004472 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004473 if (!s)
4474 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004475 get_last_end_pos(s, &end_lineno, &end_col_offset);
4476
Benjamin Peterson30760062008-11-25 04:02:28 +00004477 classname = NEW_IDENTIFIER(CHILD(n, 1));
4478 if (!classname)
4479 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004480 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004481 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004482 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004483 LINENO(n), n->n_col_offset,
4484 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004485 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004486
4487 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004488 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004489 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004490 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004491 get_last_end_pos(s, &end_lineno, &end_col_offset);
4492
Benjamin Peterson30760062008-11-25 04:02:28 +00004493 classname = NEW_IDENTIFIER(CHILD(n, 1));
4494 if (!classname)
4495 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004496 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004497 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004498 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004499 LINENO(n), n->n_col_offset,
4500 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004501 }
4502
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004503 /* class NAME '(' arglist ')' ':' suite */
4504 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004505 {
4506 PyObject *dummy_name;
4507 expr_ty dummy;
4508 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4509 if (!dummy_name)
4510 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004511 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4512 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4513 c->c_arena);
Miss Islington (bot)2076d4f2020-02-12 12:56:44 -08004514 call = ast_for_call(c, CHILD(n, 3), dummy,
4515 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004516 if (!call)
4517 return NULL;
4518 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004519 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004520 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004521 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004522 get_last_end_pos(s, &end_lineno, &end_col_offset);
4523
Benjamin Peterson30760062008-11-25 04:02:28 +00004524 classname = NEW_IDENTIFIER(CHILD(n, 1));
4525 if (!classname)
4526 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004527 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004528 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004529
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004530 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004531 decorator_seq, LINENO(n), n->n_col_offset,
4532 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004533}
4534
4535static stmt_ty
4536ast_for_stmt(struct compiling *c, const node *n)
4537{
4538 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004539 assert(NCH(n) == 1);
4540 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004541 }
4542 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004543 assert(num_stmts(n) == 1);
4544 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004545 }
4546 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004547 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004548 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4549 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004550 */
4551 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004552 case expr_stmt:
4553 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004554 case del_stmt:
4555 return ast_for_del_stmt(c, n);
4556 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004557 return Pass(LINENO(n), n->n_col_offset,
4558 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004559 case flow_stmt:
4560 return ast_for_flow_stmt(c, n);
4561 case import_stmt:
4562 return ast_for_import_stmt(c, n);
4563 case global_stmt:
4564 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004565 case nonlocal_stmt:
4566 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004567 case assert_stmt:
4568 return ast_for_assert_stmt(c, n);
4569 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004570 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004571 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4572 TYPE(n), NCH(n));
4573 return NULL;
4574 }
4575 }
4576 else {
4577 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004578 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004579 */
4580 node *ch = CHILD(n, 0);
4581 REQ(n, compound_stmt);
4582 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004583 case if_stmt:
4584 return ast_for_if_stmt(c, ch);
4585 case while_stmt:
4586 return ast_for_while_stmt(c, ch);
4587 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004588 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004589 case try_stmt:
4590 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004591 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004592 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004593 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004594 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004596 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 case decorated:
4598 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004599 case async_stmt:
4600 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004601 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004602 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004603 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004604 TYPE(n), NCH(n));
4605 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004606 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004607 }
4608}
4609
4610static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004611parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004612{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004613 const char *end;
4614 long x;
4615 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004616 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004617 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004618
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004619 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004620 errno = 0;
4621 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004622 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004623 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004624 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004625 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004626 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004627 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004628 }
4629 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004630 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004631 if (*end == '\0') {
4632 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004633 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004634 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004635 }
4636 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004637 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004638 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004639 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4640 if (compl.imag == -1.0 && PyErr_Occurred())
4641 return NULL;
4642 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004643 }
4644 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004645 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004646 dx = PyOS_string_to_double(s, NULL, NULL);
4647 if (dx == -1.0 && PyErr_Occurred())
4648 return NULL;
4649 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004651}
4652
4653static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004654parsenumber(struct compiling *c, const char *s)
4655{
4656 char *dup, *end;
4657 PyObject *res = NULL;
4658
4659 assert(s != NULL);
4660
4661 if (strchr(s, '_') == NULL) {
4662 return parsenumber_raw(c, s);
4663 }
4664 /* Create a duplicate without underscores. */
4665 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004666 if (dup == NULL) {
4667 return PyErr_NoMemory();
4668 }
Brett Cannona721aba2016-09-09 14:57:09 -07004669 end = dup;
4670 for (; *s; s++) {
4671 if (*s != '_') {
4672 *end++ = *s;
4673 }
4674 }
4675 *end = '\0';
4676 res = parsenumber_raw(c, dup);
4677 PyMem_Free(dup);
4678 return res;
4679}
4680
4681static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004682decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004683{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004684 const char *s, *t;
4685 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004686 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4687 while (s < end && (*s & 0x80)) s++;
4688 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004689 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004690}
4691
Eric V. Smith56466482016-10-31 14:46:26 -04004692static int
4693warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004694 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004695{
4696 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4697 first_invalid_escape_char);
4698 if (msg == NULL) {
4699 return -1;
4700 }
Serhiy Storchaka4c5b6ba2019-08-10 01:34:22 +03004701 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004702 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004703 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004704 {
Serhiy Storchaka4c5b6ba2019-08-10 01:34:22 +03004705 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4706 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004707 to get a more accurate error report */
4708 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004709 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004710 }
4711 Py_DECREF(msg);
4712 return -1;
4713 }
4714 Py_DECREF(msg);
4715 return 0;
4716}
4717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004718static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004719decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4720 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004721{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004722 PyObject *v, *u;
4723 char *buf;
4724 char *p;
4725 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004726
Benjamin Peterson202803a2016-02-25 22:34:45 -08004727 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004728 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004729 return NULL;
4730 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4731 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4732 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4733 if (u == NULL)
4734 return NULL;
4735 p = buf = PyBytes_AsString(u);
4736 end = s + len;
4737 while (s < end) {
4738 if (*s == '\\') {
4739 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004740 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004741 strcpy(p, "u005c");
4742 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004743 if (s >= end)
4744 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004745 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004746 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004747 if (*s & 0x80) { /* XXX inefficient */
4748 PyObject *w;
4749 int kind;
4750 void *data;
4751 Py_ssize_t len, i;
4752 w = decode_utf8(c, &s, end);
4753 if (w == NULL) {
4754 Py_DECREF(u);
4755 return NULL;
4756 }
4757 kind = PyUnicode_KIND(w);
4758 data = PyUnicode_DATA(w);
4759 len = PyUnicode_GET_LENGTH(w);
4760 for (i = 0; i < len; i++) {
4761 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4762 sprintf(p, "\\U%08x", chr);
4763 p += 10;
4764 }
4765 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004766 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004767 Py_DECREF(w);
4768 } else {
4769 *p++ = *s++;
4770 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004771 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004772 len = p - buf;
4773 s = buf;
4774
Eric V. Smith56466482016-10-31 14:46:26 -04004775 const char *first_invalid_escape;
4776 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4777
4778 if (v != NULL && first_invalid_escape != NULL) {
4779 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4780 /* We have not decref u before because first_invalid_escape points
4781 inside u. */
4782 Py_XDECREF(u);
4783 Py_DECREF(v);
4784 return NULL;
4785 }
4786 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004787 Py_XDECREF(u);
4788 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004789}
4790
Eric V. Smith56466482016-10-31 14:46:26 -04004791static PyObject *
4792decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4793 size_t len)
4794{
4795 const char *first_invalid_escape;
4796 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4797 &first_invalid_escape);
4798 if (result == NULL)
4799 return NULL;
4800
4801 if (first_invalid_escape != NULL) {
4802 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4803 Py_DECREF(result);
4804 return NULL;
4805 }
4806 }
4807 return result;
4808}
4809
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004810/* Shift locations for the given node and all its children by adding `lineno`
4811 and `col_offset` to existing locations. */
4812static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4813{
4814 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004815 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004816 for (int i = 0; i < NCH(n); ++i) {
4817 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4818 /* Shifting column offsets unnecessary if there's been newlines. */
4819 col_offset = 0;
4820 }
4821 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4822 }
4823 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004824 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004825}
4826
4827/* Fix locations for the given node and its children.
4828
4829 `parent` is the enclosing node.
4830 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004831 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004832*/
4833static void
4834fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4835{
4836 char *substr = NULL;
4837 char *start;
4838 int lines = LINENO(parent) - 1;
4839 int cols = parent->n_col_offset;
4840 /* Find the full fstring to fix location information in `n`. */
4841 while (parent && parent->n_type != STRING)
4842 parent = parent->n_child;
4843 if (parent && parent->n_str) {
4844 substr = strstr(parent->n_str, expr_str);
4845 if (substr) {
4846 start = substr;
4847 while (start > parent->n_str) {
4848 if (start[0] == '\n')
4849 break;
4850 start--;
4851 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004852 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004853 /* adjust the start based on the number of newlines encountered
4854 before the f-string expression */
4855 for (char* p = parent->n_str; p < substr; p++) {
4856 if (*p == '\n') {
4857 lines++;
4858 }
4859 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004860 }
4861 }
4862 fstring_shift_node_locations(n, lines, cols);
4863}
4864
Eric V. Smith451d0e32016-09-09 21:56:20 -04004865/* Compile this expression in to an expr_ty. Add parens around the
4866 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004867static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004868fstring_compile_expr(const char *expr_start, const char *expr_end,
4869 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004870
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004872 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004873 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004874 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004875 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004876 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004877
Eric V. Smith1d44c412015-09-23 07:49:00 -04004878 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004879 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004880 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4881 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004882
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004883 /* If the substring is all whitespace, it's an error. We need to catch this
4884 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4885 because turning the expression '' in to '()' would go from being invalid
4886 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004887 for (s = expr_start; s != expr_end; s++) {
4888 char c = *s;
4889 /* The Python parser ignores only the following whitespace
4890 characters (\r already is converted to \n). */
4891 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892 break;
4893 }
4894 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004895 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004896 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004897 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004898 }
4899
Eric V. Smith451d0e32016-09-09 21:56:20 -04004900 len = expr_end - expr_start;
4901 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03004902 str = PyMem_Malloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004903 if (str == NULL) {
4904 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004906 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004907
Eric V. Smith451d0e32016-09-09 21:56:20 -04004908 str[0] = '(';
4909 memcpy(str+1, expr_start, len);
4910 str[len+1] = ')';
4911 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004912
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07004913 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004914 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004915 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4916 Py_eval_input, 0);
4917 if (!mod_n) {
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03004918 PyMem_Free(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004919 return NULL;
4920 }
4921 /* Reuse str to find the correct column offset. */
4922 str[0] = '{';
4923 str[len+1] = '}';
4924 fstring_fix_node_location(n, mod_n, str);
4925 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03004926 PyMem_Free(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004927 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004929 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004930 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931}
4932
4933/* Return -1 on error.
4934
4935 Return 0 if we reached the end of the literal.
4936
4937 Return 1 if we haven't reached the end of the literal, but we want
4938 the caller to process the literal up to this point. Used for
4939 doubled braces.
4940*/
4941static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004942fstring_find_literal(const char **str, const char *end, int raw,
4943 PyObject **literal, int recurse_lvl,
4944 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004945{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004946 /* Get any literal string. It ends when we hit an un-doubled left
4947 brace (which isn't part of a unicode name escape such as
4948 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004949
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004950 const char *s = *str;
4951 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004952 int result = 0;
4953
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004955 while (s < end) {
4956 char ch = *s++;
4957 if (!raw && ch == '\\' && s < end) {
4958 ch = *s++;
4959 if (ch == 'N') {
4960 if (s < end && *s++ == '{') {
4961 while (s < end && *s++ != '}') {
4962 }
4963 continue;
4964 }
4965 break;
4966 }
4967 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4968 return -1;
4969 }
4970 }
4971 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004972 /* Check for doubled braces, but only at the top level. If
4973 we checked at every level, then f'{0:{3}}' would fail
4974 with the two closing braces. */
4975 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004976 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 /* We're going to tell the caller that the literal ends
4978 here, but that they should continue scanning. But also
4979 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004980 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004981 result = 1;
4982 goto done;
4983 }
4984
4985 /* Where a single '{' is the start of a new expression, a
4986 single '}' is not allowed. */
4987 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004988 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004989 ast_error(c, n, "f-string: single '}' is not allowed");
4990 return -1;
4991 }
4992 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 /* We're either at a '{', which means we're starting another
4994 expression; or a '}', which means we're at the end of this
4995 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004996 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 break;
4998 }
4999 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005000 *str = s;
5001 assert(s <= end);
5002 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005003done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005004 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005005 if (raw)
5006 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005007 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04005008 NULL, NULL);
5009 else
Eric V. Smith56466482016-10-31 14:46:26 -04005010 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03005011 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005012 if (!*literal)
5013 return -1;
5014 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005015 return result;
5016}
5017
5018/* Forward declaration because parsing is recursive. */
5019static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005020fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005021 struct compiling *c, const node *n);
5022
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005024 expression (so it must be a '{'). Returns the FormattedValue node, which
5025 includes the expression, conversion character, format_spec expression, and
5026 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027
5028 Note that I don't do a perfect job here: I don't make sure that a
5029 closing brace doesn't match an opening paren, for example. It
5030 doesn't need to error on all invalid expressions, just correctly
5031 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005032 will be caught when we parse it later.
5033
5034 *expression is set to the expression. For an '=' "debug" expression,
5035 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005036 including the '=' and any whitespace around it, as a string object). If
5037 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005038static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005039fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005040 PyObject **expr_text, expr_ty *expression,
5041 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005042{
5043 /* Return -1 on error, else 0. */
5044
Eric V. Smith451d0e32016-09-09 21:56:20 -04005045 const char *expr_start;
5046 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047 expr_ty simple_expression;
5048 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005049 int conversion = -1; /* The conversion char. Use default if not
5050 specified, or !r if using = and no format
5051 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052
5053 /* 0 if we're not in a string, else the quote char we're trying to
5054 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005055 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056
5057 /* If we're inside a string, 1=normal, 3=triple-quoted. */
5058 int string_type = 0;
5059
5060 /* Keep track of nesting level for braces/parens/brackets in
5061 expressions. */
5062 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005063 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04005064
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005065 *expr_text = NULL;
5066
Eric V. Smith235a6f02015-09-19 14:51:32 -04005067 /* Can only nest one level deep. */
5068 if (recurse_lvl >= 2) {
5069 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005070 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005071 }
5072
5073 /* The first char must be a left brace, or we wouldn't have gotten
5074 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 assert(**str == '{');
5076 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005077
Eric V. Smith451d0e32016-09-09 21:56:20 -04005078 expr_start = *str;
5079 for (; *str < end; (*str)++) {
5080 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005081
5082 /* Loop invariants. */
5083 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005084 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085 if (quote_char)
5086 assert(string_type == 1 || string_type == 3);
5087 else
5088 assert(string_type == 0);
5089
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 ch = **str;
5091 /* Nowhere inside an expression is a backslash allowed. */
5092 if (ch == '\\') {
5093 /* Error: can't include a backslash character, inside
5094 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005095 ast_error(c, n,
5096 "f-string expression part "
5097 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005098 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005099 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005100 if (quote_char) {
5101 /* We're inside a string. See if we're at the end. */
5102 /* This code needs to implement the same non-error logic
5103 as tok_get from tokenizer.c, at the letter_quote
5104 label. To actually share that code would be a
5105 nightmare. But, it's unlikely to change and is small,
5106 so duplicate it here. Note we don't need to catch all
5107 of the errors, since they'll be caught when parsing the
5108 expression. We just need to match the non-error
5109 cases. Thus we can ignore \n in single-quoted strings,
5110 for example. Or non-terminated strings. */
5111 if (ch == quote_char) {
5112 /* Does this match the string_type (single or triple
5113 quoted)? */
5114 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005115 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005117 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 string_type = 0;
5119 quote_char = 0;
5120 continue;
5121 }
5122 } else {
5123 /* We're at the end of a normal string. */
5124 quote_char = 0;
5125 string_type = 0;
5126 continue;
5127 }
5128 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005129 } else if (ch == '\'' || ch == '"') {
5130 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005133 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 } else {
5135 /* Start of a normal string. */
5136 string_type = 1;
5137 }
5138 /* Start looking for the end of the string. */
5139 quote_char = ch;
5140 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005141 if (nested_depth >= MAXLEVEL) {
5142 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005143 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005144 }
5145 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005146 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 } else if (ch == '#') {
5148 /* Error: can't include a comment character, inside parens
5149 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005150 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005151 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005152 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005153 (ch == '!' || ch == ':' || ch == '}' ||
5154 ch == '=' || ch == '>' || ch == '<')) {
5155 /* See if there's a next character. */
5156 if (*str+1 < end) {
5157 char next = *(*str+1);
5158
5159 /* For "!=". since '=' is not an allowed conversion character,
5160 nothing is lost in this test. */
5161 if ((ch == '!' && next == '=') || /* != */
5162 (ch == '=' && next == '=') || /* == */
5163 (ch == '<' && next == '=') || /* <= */
5164 (ch == '>' && next == '=') /* >= */
5165 ) {
5166 *str += 1;
5167 continue;
5168 }
5169 /* Don't get out of the loop for these, if they're single
5170 chars (not part of 2-char tokens). If by themselves, they
5171 don't end an expression (unlike say '!'). */
5172 if (ch == '>' || ch == '<') {
5173 continue;
5174 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005175 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005176
Eric V. Smith235a6f02015-09-19 14:51:32 -04005177 /* Normal way out of this loop. */
5178 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005179 } else if (ch == ']' || ch == '}' || ch == ')') {
5180 if (!nested_depth) {
5181 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005182 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005183 }
5184 nested_depth--;
5185 int opening = parenstack[nested_depth];
5186 if (!((opening == '(' && ch == ')') ||
5187 (opening == '[' && ch == ']') ||
5188 (opening == '{' && ch == '}')))
5189 {
5190 ast_error(c, n,
5191 "f-string: closing parenthesis '%c' "
5192 "does not match opening parenthesis '%c'",
5193 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005194 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005195 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005196 } else {
5197 /* Just consume this char and loop around. */
5198 }
5199 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005200 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005201 /* If we leave this loop in a string or with mismatched parens, we
5202 don't care. We'll get a syntax error when compiling the
5203 expression. But, we can produce a better error message, so
5204 let's just do that.*/
5205 if (quote_char) {
5206 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005207 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005208 }
5209 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005210 int opening = parenstack[nested_depth - 1];
5211 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005212 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005213 }
5214
Eric V. Smith451d0e32016-09-09 21:56:20 -04005215 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005216 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005217
5218 /* Compile the expression as soon as possible, so we show errors
5219 related to the expression before errors related to the
5220 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005221 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005222 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005223 goto error;
5224
5225 /* Check for =, which puts the text value of the expression in
5226 expr_text. */
5227 if (**str == '=') {
Shantanuf7ed4d42020-06-06 03:08:48 -07005228 if (c->c_feature_version < 8) {
5229 ast_error(c, n,
5230 "f-string: self documenting expressions are "
5231 "only supported in Python 3.8 and greater");
5232 goto error;
5233 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005234 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005235
5236 /* Skip over ASCII whitespace. No need to test for end of string
5237 here, since we know there's at least a trailing quote somewhere
5238 ahead. */
5239 while (Py_ISSPACE(**str)) {
5240 *str += 1;
5241 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005242
5243 /* Set *expr_text to the text of the expression. */
5244 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5245 if (!*expr_text) {
5246 goto error;
5247 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005248 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005249
5250 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005251 if (**str == '!') {
5252 *str += 1;
5253 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005254 goto unexpected_end_of_string;
5255
Eric V. Smith451d0e32016-09-09 21:56:20 -04005256 conversion = **str;
5257 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005258
5259 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005260 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005261 ast_error(c, n,
5262 "f-string: invalid conversion character: "
5263 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005264 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005265 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005266
5267 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005268
5269 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005270 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005271 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005272 if (**str == ':') {
5273 *str += 1;
5274 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005275 goto unexpected_end_of_string;
5276
5277 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005278 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005279 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005280 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005281 }
5282
Eric V. Smith451d0e32016-09-09 21:56:20 -04005283 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005284 goto unexpected_end_of_string;
5285
5286 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005287 assert(*str < end);
5288 assert(**str == '}');
5289 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005290
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005291 /* If we're in = mode (detected by non-NULL expr_text), and have no format
5292 spec and no explict conversion, set the conversion to 'r'. */
5293 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005294 conversion = 'r';
5295 }
5296
Eric V. Smith451d0e32016-09-09 21:56:20 -04005297 /* And now create the FormattedValue node that represents this
5298 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005299 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005300 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005301 n->n_col_offset, n->n_end_lineno,
5302 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005303 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005304 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005305
5306 return 0;
5307
5308unexpected_end_of_string:
5309 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005310 /* Falls through to error. */
5311
5312error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005313 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005314 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005315
Eric V. Smith235a6f02015-09-19 14:51:32 -04005316}
5317
5318/* Return -1 on error.
5319
5320 Return 0 if we have a literal (possible zero length) and an
5321 expression (zero length if at the end of the string.
5322
5323 Return 1 if we have a literal, but no expression, and we want the
5324 caller to call us again. This is used to deal with doubled
5325 braces.
5326
5327 When called multiple times on the string 'a{{b{0}c', this function
5328 will return:
5329
5330 1. the literal 'a{' with no expression, and a return value
5331 of 1. Despite the fact that there's no expression, the return
5332 value of 1 means we're not finished yet.
5333
5334 2. the literal 'b' and the expression '0', with a return value of
5335 0. The fact that there's an expression means we're not finished.
5336
5337 3. literal 'c' with no expression and a return value of 0. The
5338 combination of the return value of 0 with no expression means
5339 we're finished.
5340*/
5341static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005342fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5343 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005344 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005345 struct compiling *c, const node *n)
5346{
5347 int result;
5348
5349 assert(*literal == NULL && *expression == NULL);
5350
5351 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005352 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005353 if (result < 0)
5354 goto error;
5355
5356 assert(result == 0 || result == 1);
5357
5358 if (result == 1)
5359 /* We have a literal, but don't look at the expression. */
5360 return 1;
5361
Eric V. Smith451d0e32016-09-09 21:56:20 -04005362 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005363 /* We're at the end of the string or the end of a nested
5364 f-string: no expression. The top-level error case where we
5365 expect to be at the end of the string but we're at a '}' is
5366 handled later. */
5367 return 0;
5368
5369 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005370 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005371
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005372 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5373 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005374 goto error;
5375
5376 return 0;
5377
5378error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005379 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005380 return -1;
5381}
5382
5383#define EXPRLIST_N_CACHED 64
5384
5385typedef struct {
5386 /* Incrementally build an array of expr_ty, so be used in an
5387 asdl_seq. Cache some small but reasonably sized number of
5388 expr_ty's, and then after that start dynamically allocating,
5389 doubling the number allocated each time. Note that the f-string
5390 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005391 Constant for the literal 'a'. So you add expr_ty's about twice as
Miss Islington (bot)4bd1d052019-08-30 13:42:54 -07005392 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005393
5394 Py_ssize_t allocated; /* Number we've allocated. */
5395 Py_ssize_t size; /* Number we've used. */
5396 expr_ty *p; /* Pointer to the memory we're actually
5397 using. Will point to 'data' until we
5398 start dynamically allocating. */
5399 expr_ty data[EXPRLIST_N_CACHED];
5400} ExprList;
5401
5402#ifdef NDEBUG
5403#define ExprList_check_invariants(l)
5404#else
5405static void
5406ExprList_check_invariants(ExprList *l)
5407{
5408 /* Check our invariants. Make sure this object is "live", and
5409 hasn't been deallocated. */
5410 assert(l->size >= 0);
5411 assert(l->p != NULL);
5412 if (l->size <= EXPRLIST_N_CACHED)
5413 assert(l->data == l->p);
5414}
5415#endif
5416
5417static void
5418ExprList_Init(ExprList *l)
5419{
5420 l->allocated = EXPRLIST_N_CACHED;
5421 l->size = 0;
5422
5423 /* Until we start allocating dynamically, p points to data. */
5424 l->p = l->data;
5425
5426 ExprList_check_invariants(l);
5427}
5428
5429static int
5430ExprList_Append(ExprList *l, expr_ty exp)
5431{
5432 ExprList_check_invariants(l);
5433 if (l->size >= l->allocated) {
5434 /* We need to alloc (or realloc) the memory. */
5435 Py_ssize_t new_size = l->allocated * 2;
5436
5437 /* See if we've ever allocated anything dynamically. */
5438 if (l->p == l->data) {
5439 Py_ssize_t i;
5440 /* We're still using the cached data. Switch to
5441 alloc-ing. */
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03005442 l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005443 if (!l->p)
5444 return -1;
5445 /* Copy the cached data into the new buffer. */
5446 for (i = 0; i < l->size; i++)
5447 l->p[i] = l->data[i];
5448 } else {
5449 /* Just realloc. */
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03005450 expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005451 if (!tmp) {
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03005452 PyMem_Free(l->p);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005453 l->p = NULL;
5454 return -1;
5455 }
5456 l->p = tmp;
5457 }
5458
5459 l->allocated = new_size;
5460 assert(l->allocated == 2 * l->size);
5461 }
5462
5463 l->p[l->size++] = exp;
5464
5465 ExprList_check_invariants(l);
5466 return 0;
5467}
5468
5469static void
5470ExprList_Dealloc(ExprList *l)
5471{
5472 ExprList_check_invariants(l);
5473
5474 /* If there's been an error, or we've never dynamically allocated,
5475 do nothing. */
5476 if (!l->p || l->p == l->data) {
5477 /* Do nothing. */
5478 } else {
5479 /* We have dynamically allocated. Free the memory. */
Lysandros Nikolaou749d3bc2020-06-27 21:43:41 +03005480 PyMem_Free(l->p);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005481 }
5482 l->p = NULL;
5483 l->size = -1;
5484}
5485
5486static asdl_seq *
5487ExprList_Finish(ExprList *l, PyArena *arena)
5488{
5489 asdl_seq *seq;
5490
5491 ExprList_check_invariants(l);
5492
5493 /* Allocate the asdl_seq and copy the expressions in to it. */
5494 seq = _Py_asdl_seq_new(l->size, arena);
5495 if (seq) {
5496 Py_ssize_t i;
5497 for (i = 0; i < l->size; i++)
5498 asdl_seq_SET(seq, i, l->p[i]);
5499 }
5500 ExprList_Dealloc(l);
5501 return seq;
5502}
5503
5504/* The FstringParser is designed to add a mix of strings and
5505 f-strings, and concat them together as needed. Ultimately, it
5506 generates an expr_ty. */
5507typedef struct {
5508 PyObject *last_str;
5509 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005510 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005511} FstringParser;
5512
5513#ifdef NDEBUG
5514#define FstringParser_check_invariants(state)
5515#else
5516static void
5517FstringParser_check_invariants(FstringParser *state)
5518{
5519 if (state->last_str)
5520 assert(PyUnicode_CheckExact(state->last_str));
5521 ExprList_check_invariants(&state->expr_list);
5522}
5523#endif
5524
5525static void
5526FstringParser_Init(FstringParser *state)
5527{
5528 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005529 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005530 ExprList_Init(&state->expr_list);
5531 FstringParser_check_invariants(state);
5532}
5533
5534static void
5535FstringParser_Dealloc(FstringParser *state)
5536{
5537 FstringParser_check_invariants(state);
5538
5539 Py_XDECREF(state->last_str);
5540 ExprList_Dealloc(&state->expr_list);
5541}
5542
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005543/* Constants for the following */
5544static PyObject *u_kind;
5545
5546/* Compute 'kind' field for string Constant (either 'u' or None) */
5547static PyObject *
5548make_kind(struct compiling *c, const node *n)
5549{
5550 char *s = NULL;
5551 PyObject *kind = NULL;
5552
5553 /* Find the first string literal, if any */
5554 while (TYPE(n) != STRING) {
5555 if (NCH(n) == 0)
5556 return NULL;
5557 n = CHILD(n, 0);
5558 }
5559 REQ(n, STRING);
5560
5561 /* If it starts with 'u', return a PyUnicode "u" string */
5562 s = STR(n);
5563 if (s && *s == 'u') {
5564 if (!u_kind) {
5565 u_kind = PyUnicode_InternFromString("u");
5566 if (!u_kind)
5567 return NULL;
5568 }
5569 kind = u_kind;
5570 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5571 return NULL;
5572 }
5573 Py_INCREF(kind);
5574 }
5575 return kind;
5576}
5577
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005578/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005579static expr_ty
5580make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5581{
5582 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005583 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005584 *str = NULL;
5585 assert(PyUnicode_CheckExact(s));
5586 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5587 Py_DECREF(s);
5588 return NULL;
5589 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005590 kind = make_kind(c, n);
5591 if (kind == NULL && PyErr_Occurred())
5592 return NULL;
5593 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005594 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005595}
5596
5597/* Add a non-f-string (that is, a regular literal string). str is
5598 decref'd. */
5599static int
5600FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5601{
5602 FstringParser_check_invariants(state);
5603
5604 assert(PyUnicode_CheckExact(str));
5605
5606 if (PyUnicode_GET_LENGTH(str) == 0) {
5607 Py_DECREF(str);
5608 return 0;
5609 }
5610
5611 if (!state->last_str) {
5612 /* We didn't have a string before, so just remember this one. */
5613 state->last_str = str;
5614 } else {
5615 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005616 PyUnicode_AppendAndDel(&state->last_str, str);
5617 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005618 return -1;
5619 }
5620 FstringParser_check_invariants(state);
5621 return 0;
5622}
5623
Eric V. Smith451d0e32016-09-09 21:56:20 -04005624/* Parse an f-string. The f-string is in *str to end, with no
5625 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005626static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627FstringParser_ConcatFstring(FstringParser *state, const char **str,
5628 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005629 struct compiling *c, const node *n)
5630{
5631 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005632 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005633
5634 /* Parse the f-string. */
5635 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005636 PyObject *literal = NULL;
5637 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005638 expr_ty expression = NULL;
5639
5640 /* If there's a zero length literal in front of the
5641 expression, literal will be NULL. If we're at the end of
5642 the f-string, expression will be NULL (unless result == 1,
5643 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005644 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005645 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005646 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005647 if (result < 0)
5648 return -1;
5649
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005650 /* Add the literal, if any. */
5651 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5652 Py_XDECREF(expr_text);
5653 return -1;
5654 }
5655 /* Add the expr_text, if any. */
5656 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5657 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005658 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005659
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005660 /* We've dealt with the literal and expr_text, their ownership has
5661 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662
5663 /* See if we should just loop around to get the next literal
5664 and expression, while ignoring the expression this
5665 time. This is used for un-doubling braces, as an
5666 optimization. */
5667 if (result == 1)
5668 continue;
5669
5670 if (!expression)
5671 /* We're done with this f-string. */
5672 break;
5673
5674 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005675 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005676 if (!state->last_str) {
5677 /* Do nothing. No previous literal. */
5678 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005679 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005680 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5681 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5682 return -1;
5683 }
5684
5685 if (ExprList_Append(&state->expr_list, expression) < 0)
5686 return -1;
5687 }
5688
Eric V. Smith235a6f02015-09-19 14:51:32 -04005689 /* If recurse_lvl is zero, then we must be at the end of the
5690 string. Otherwise, we must be at a right brace. */
5691
Eric V. Smith451d0e32016-09-09 21:56:20 -04005692 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005693 ast_error(c, n, "f-string: unexpected end of string");
5694 return -1;
5695 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005696 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005697 ast_error(c, n, "f-string: expecting '}'");
5698 return -1;
5699 }
5700
5701 FstringParser_check_invariants(state);
5702 return 0;
5703}
5704
5705/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005706 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005707static expr_ty
5708FstringParser_Finish(FstringParser *state, struct compiling *c,
5709 const node *n)
5710{
5711 asdl_seq *seq;
5712
5713 FstringParser_check_invariants(state);
5714
5715 /* If we're just a constant string with no expressions, return
5716 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005717 if (!state->fmode) {
5718 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005719 if (!state->last_str) {
5720 /* Create a zero length string. */
5721 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5722 if (!state->last_str)
5723 goto error;
5724 }
5725 return make_str_node_and_del(&state->last_str, c, n);
5726 }
5727
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005728 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005729 last node in our expression list. */
5730 if (state->last_str) {
5731 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5732 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5733 goto error;
5734 }
5735 /* This has already been freed. */
5736 assert(state->last_str == NULL);
5737
5738 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5739 if (!seq)
5740 goto error;
5741
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005742 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5743 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744
5745error:
5746 FstringParser_Dealloc(state);
5747 return NULL;
5748}
5749
Eric V. Smith451d0e32016-09-09 21:56:20 -04005750/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5751 at end, parse it into an expr_ty. Return NULL on error. Adjust
5752 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005753static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005754fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005755 struct compiling *c, const node *n)
5756{
5757 FstringParser state;
5758
5759 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005760 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005761 c, n) < 0) {
5762 FstringParser_Dealloc(&state);
5763 return NULL;
5764 }
5765
5766 return FstringParser_Finish(&state, c, n);
5767}
5768
5769/* n is a Python string literal, including the bracketing quote
5770 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005771 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005772 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005773 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5774 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005775*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005776static int
5777parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5778 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005779{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005780 size_t len;
5781 const char *s = STR(n);
5782 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005783 int fmode = 0;
5784 *bytesmode = 0;
5785 *rawmode = 0;
5786 *result = NULL;
5787 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005788 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005789 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005790 if (quote == 'b' || quote == 'B') {
5791 quote = *++s;
5792 *bytesmode = 1;
5793 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005794 else if (quote == 'u' || quote == 'U') {
5795 quote = *++s;
5796 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005797 else if (quote == 'r' || quote == 'R') {
5798 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005799 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005800 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005801 else if (quote == 'f' || quote == 'F') {
5802 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005803 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005804 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005805 else {
5806 break;
5807 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005808 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005809 }
Guido van Rossum495da292019-03-07 12:38:08 -08005810
5811 /* fstrings are only allowed in Python 3.6 and greater */
5812 if (fmode && c->c_feature_version < 6) {
5813 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5814 return -1;
5815 }
5816
Eric V. Smith451d0e32016-09-09 21:56:20 -04005817 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005818 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005819 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005820 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005821 if (quote != '\'' && quote != '\"') {
5822 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005823 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005824 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005825 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005826 s++;
5827 len = strlen(s);
5828 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005830 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005831 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005832 }
5833 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005834 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005835 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005836 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005837 }
5838 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005839 /* A triple quoted string. We've already skipped one quote at
5840 the start and one at the end of the string. Now skip the
5841 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005842 s += 2;
5843 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005844 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005845 if (s[--len] != quote || s[--len] != quote) {
5846 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005847 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005848 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005849 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005850
Eric V. Smith451d0e32016-09-09 21:56:20 -04005851 if (fmode) {
5852 /* Just return the bytes. The caller will parse the resulting
5853 string. */
5854 *fstr = s;
5855 *fstrlen = len;
5856 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005857 }
5858
Eric V. Smith451d0e32016-09-09 21:56:20 -04005859 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005860 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005861 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005862 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005863 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005864 const char *ch;
5865 for (ch = s; *ch; ch++) {
5866 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005867 ast_error(c, n,
5868 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005869 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005870 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005871 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005872 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005873 if (*rawmode)
5874 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005875 else
Eric V. Smith56466482016-10-31 14:46:26 -04005876 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005877 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005878 if (*rawmode)
5879 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005880 else
Eric V. Smith56466482016-10-31 14:46:26 -04005881 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005882 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005883 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005884}
5885
Eric V. Smith235a6f02015-09-19 14:51:32 -04005886/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5887 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005888 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005889 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005890 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005891 node if there's just an f-string (with no leading or trailing
5892 literals), or a JoinedStr node if there are multiple f-strings or
5893 any literals involved. */
5894static expr_ty
5895parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005896{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005897 int bytesmode = 0;
5898 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005899 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005900
5901 FstringParser state;
5902 FstringParser_Init(&state);
5903
5904 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005905 int this_bytesmode;
5906 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005907 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005908 const char *fstr;
5909 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005910
5911 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005912 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5913 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005914 goto error;
5915
5916 /* Check that we're not mixing bytes with unicode. */
5917 if (i != 0 && bytesmode != this_bytesmode) {
5918 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005919 /* s is NULL if the current string part is an f-string. */
5920 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005921 goto error;
5922 }
5923 bytesmode = this_bytesmode;
5924
Eric V. Smith451d0e32016-09-09 21:56:20 -04005925 if (fstr != NULL) {
5926 int result;
5927 assert(s == NULL && !bytesmode);
5928 /* This is an f-string. Parse and concatenate it. */
5929 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5930 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005931 if (result < 0)
5932 goto error;
5933 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005934 /* A string or byte string. */
5935 assert(s != NULL && fstr == NULL);
5936
Eric V. Smith451d0e32016-09-09 21:56:20 -04005937 assert(bytesmode ? PyBytes_CheckExact(s) :
5938 PyUnicode_CheckExact(s));
5939
Eric V. Smith451d0e32016-09-09 21:56:20 -04005940 if (bytesmode) {
5941 /* For bytes, concat as we go. */
5942 if (i == 0) {
5943 /* First time, just remember this value. */
5944 bytes_str = s;
5945 } else {
5946 PyBytes_ConcatAndDel(&bytes_str, s);
5947 if (!bytes_str)
5948 goto error;
5949 }
5950 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005951 /* This is a regular string. Concatenate it. */
5952 if (FstringParser_ConcatAndDel(&state, s) < 0)
5953 goto error;
5954 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005955 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005956 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005957 if (bytesmode) {
5958 /* Just return the bytes object and we're done. */
5959 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5960 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005961 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005962 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005963 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005964
Eric V. Smith235a6f02015-09-19 14:51:32 -04005965 /* We're not a bytes string, bytes_str should never have been set. */
5966 assert(bytes_str == NULL);
5967
5968 return FstringParser_Finish(&state, c, n);
5969
5970error:
5971 Py_XDECREF(bytes_str);
5972 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005973 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005974}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005975
5976PyObject *
5977_PyAST_GetDocString(asdl_seq *body)
5978{
5979 if (!asdl_seq_LEN(body)) {
5980 return NULL;
5981 }
5982 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5983 if (st->kind != Expr_kind) {
5984 return NULL;
5985 }
5986 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005987 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5988 return e->v.Constant.value;
5989 }
5990 return NULL;
5991}