blob: 46815c271b62d5ab174adf928237126f6639f9e6 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
43validate_slice(slice_ty slice)
44{
45 switch (slice->kind) {
46 case Slice_kind:
47 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
48 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
49 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
50 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010051 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050052 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
53 return 0;
54 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
55 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
56 return 0;
57 return 1;
58 }
59 case Index_kind:
60 return validate_expr(slice->v.Index.value, Load);
61 default:
62 PyErr_SetString(PyExc_SystemError, "unknown slice node");
63 return 0;
64 }
65}
66
67static int
68validate_keywords(asdl_seq *keywords)
69{
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 for (i = 0; i < asdl_seq_LEN(keywords); i++)
72 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
73 return 0;
74 return 1;
75}
76
77static int
78validate_args(asdl_seq *args)
79{
Victor Stinner4d73ae72018-11-22 14:45:16 +010080 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050081 for (i = 0; i < asdl_seq_LEN(args); i++) {
82 arg_ty arg = asdl_seq_GET(args, i);
83 if (arg->annotation && !validate_expr(arg->annotation, Load))
84 return 0;
85 }
86 return 1;
87}
88
89static const char *
90expr_context_name(expr_context_ty ctx)
91{
92 switch (ctx) {
93 case Load:
94 return "Load";
95 case Store:
96 return "Store";
97 case Del:
98 return "Del";
99 case AugLoad:
100 return "AugLoad";
101 case AugStore:
102 return "AugStore";
103 case Param:
104 return "Param";
105 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700106 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500107 }
108}
109
110static int
111validate_arguments(arguments_ty args)
112{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100113 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100115 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700116 if (args->vararg && args->vararg->annotation
117 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500118 return 0;
119 }
120 if (!validate_args(args->kwonlyargs))
121 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100122 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700123 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500124 return 0;
125 }
Pablo Galindo2f58a842019-05-31 14:09:49 +0100126 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500127 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
128 return 0;
129 }
130 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
131 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
132 "kw_defaults on arguments");
133 return 0;
134 }
135 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
136}
137
138static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100139validate_constant(PyObject *value)
140{
141 if (value == Py_None || value == Py_Ellipsis)
142 return 1;
143
144 if (PyLong_CheckExact(value)
145 || PyFloat_CheckExact(value)
146 || PyComplex_CheckExact(value)
147 || PyBool_Check(value)
148 || PyUnicode_CheckExact(value)
149 || PyBytes_CheckExact(value))
150 return 1;
151
152 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
153 PyObject *it;
154
155 it = PyObject_GetIter(value);
156 if (it == NULL)
157 return 0;
158
159 while (1) {
160 PyObject *item = PyIter_Next(it);
161 if (item == NULL) {
162 if (PyErr_Occurred()) {
163 Py_DECREF(it);
164 return 0;
165 }
166 break;
167 }
168
169 if (!validate_constant(item)) {
170 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 return 0;
173 }
Victor Stinner726f6902016-01-27 00:11:47 +0100174 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100175 }
176
177 Py_DECREF(it);
178 return 1;
179 }
180
181 return 0;
182}
183
184static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500185validate_expr(expr_ty exp, expr_context_ty ctx)
186{
187 int check_ctx = 1;
188 expr_context_ty actual_ctx;
189
190 /* First check expression context. */
191 switch (exp->kind) {
192 case Attribute_kind:
193 actual_ctx = exp->v.Attribute.ctx;
194 break;
195 case Subscript_kind:
196 actual_ctx = exp->v.Subscript.ctx;
197 break;
198 case Starred_kind:
199 actual_ctx = exp->v.Starred.ctx;
200 break;
201 case Name_kind:
202 actual_ctx = exp->v.Name.ctx;
203 break;
204 case List_kind:
205 actual_ctx = exp->v.List.ctx;
206 break;
207 case Tuple_kind:
208 actual_ctx = exp->v.Tuple.ctx;
209 break;
210 default:
211 if (ctx != Load) {
212 PyErr_Format(PyExc_ValueError, "expression which can't be "
213 "assigned to in %s context", expr_context_name(ctx));
214 return 0;
215 }
216 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100217 /* set actual_ctx to prevent gcc warning */
218 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500219 }
220 if (check_ctx && actual_ctx != ctx) {
221 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
222 expr_context_name(ctx), expr_context_name(actual_ctx));
223 return 0;
224 }
225
226 /* Now validate expression. */
227 switch (exp->kind) {
228 case BoolOp_kind:
229 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
230 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
231 return 0;
232 }
233 return validate_exprs(exp->v.BoolOp.values, Load, 0);
234 case BinOp_kind:
235 return validate_expr(exp->v.BinOp.left, Load) &&
236 validate_expr(exp->v.BinOp.right, Load);
237 case UnaryOp_kind:
238 return validate_expr(exp->v.UnaryOp.operand, Load);
239 case Lambda_kind:
240 return validate_arguments(exp->v.Lambda.args) &&
241 validate_expr(exp->v.Lambda.body, Load);
242 case IfExp_kind:
243 return validate_expr(exp->v.IfExp.test, Load) &&
244 validate_expr(exp->v.IfExp.body, Load) &&
245 validate_expr(exp->v.IfExp.orelse, Load);
246 case Dict_kind:
247 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
248 PyErr_SetString(PyExc_ValueError,
249 "Dict doesn't have the same number of keys as values");
250 return 0;
251 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400252 /* null_ok=1 for keys expressions to allow dict unpacking to work in
253 dict literals, i.e. ``{**{a:b}}`` */
254 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
255 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500256 case Set_kind:
257 return validate_exprs(exp->v.Set.elts, Load, 0);
258#define COMP(NAME) \
259 case NAME ## _kind: \
260 return validate_comprehension(exp->v.NAME.generators) && \
261 validate_expr(exp->v.NAME.elt, Load);
262 COMP(ListComp)
263 COMP(SetComp)
264 COMP(GeneratorExp)
265#undef COMP
266 case DictComp_kind:
267 return validate_comprehension(exp->v.DictComp.generators) &&
268 validate_expr(exp->v.DictComp.key, Load) &&
269 validate_expr(exp->v.DictComp.value, Load);
270 case Yield_kind:
271 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500272 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000273 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400274 case Await_kind:
275 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500276 case Compare_kind:
277 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
278 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
279 return 0;
280 }
281 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
282 asdl_seq_LEN(exp->v.Compare.ops)) {
283 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
284 "of comparators and operands");
285 return 0;
286 }
287 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
288 validate_expr(exp->v.Compare.left, Load);
289 case Call_kind:
290 return validate_expr(exp->v.Call.func, Load) &&
291 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400292 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100293 case Constant_kind:
294 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100295 PyErr_Format(PyExc_TypeError,
296 "got an invalid type in Constant: %s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700297 _PyType_Name(Py_TYPE(exp->v.Constant.value)));
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100298 return 0;
299 }
300 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400301 case JoinedStr_kind:
302 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
303 case FormattedValue_kind:
304 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
305 return 0;
306 if (exp->v.FormattedValue.format_spec)
307 return validate_expr(exp->v.FormattedValue.format_spec, Load);
308 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500309 case Attribute_kind:
310 return validate_expr(exp->v.Attribute.value, Load);
311 case Subscript_kind:
312 return validate_slice(exp->v.Subscript.slice) &&
313 validate_expr(exp->v.Subscript.value, Load);
314 case Starred_kind:
315 return validate_expr(exp->v.Starred.value, ctx);
316 case List_kind:
317 return validate_exprs(exp->v.List.elts, ctx, 0);
318 case Tuple_kind:
319 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000320 case NamedExpr_kind:
321 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300322 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500324 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000326 PyErr_SetString(PyExc_SystemError, "unexpected expression");
327 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800569 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570};
571
572static asdl_seq *seq_for_testlist(struct compiling *, const node *);
573static expr_ty ast_for_expr(struct compiling *, const node *);
574static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300575static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000576static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
577 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000578static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000579static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
guoci90fc8982018-09-11 17:45:45 -0400581static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
582static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200585static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000586 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000588static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400589static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000590static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Nick Coghlan650f0d02007-04-15 12:05:43 +0000592#define COMP_GENEXP 0
593#define COMP_LISTCOMP 1
594#define COMP_SETCOMP 2
595
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596static int
597init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000598{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500599 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
600 if (!m)
601 return 0;
602 c->c_normalize = PyObject_GetAttrString(m, "normalize");
603 Py_DECREF(m);
604 if (!c->c_normalize)
605 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500606 return 1;
607}
608
609static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400610new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500611{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400612 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000614 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500616 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000617 /* Check whether there are non-ASCII characters in the
618 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500619 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500624 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700625 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300626 if (form == NULL) {
627 Py_DECREF(id);
628 return NULL;
629 }
630 PyObject *args[2] = {form, id};
631 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500632 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 if (!id2)
634 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300635 if (!PyUnicode_Check(id2)) {
636 PyErr_Format(PyExc_TypeError,
637 "unicodedata.normalize() must return a string, not "
638 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700639 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300640 Py_DECREF(id2);
641 return NULL;
642 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000645 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200646 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
647 Py_DECREF(id);
648 return NULL;
649 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000650 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651}
652
Benjamin Peterson55e00432012-01-16 17:22:31 -0500653#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400658 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200661 va_start(va, errmsg);
662 errstr = PyUnicode_FromFormatV(errmsg, va);
663 va_end(va);
664 if (!errstr) {
665 return 0;
666 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200667 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000669 Py_INCREF(Py_None);
670 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 }
Ammar Askar025eb982018-09-24 17:12:49 -0400672 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200673 if (!tmp) {
674 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000676 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 Py_DECREF(errstr);
679 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 if (value) {
681 PyErr_SetObject(PyExc_SyntaxError, value);
682 Py_DECREF(value);
683 }
684 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685}
686
687/* num_stmts() returns number of contained statements.
688
689 Use this routine to determine how big a sequence is needed for
690 the statements in a parse tree. Its raison d'etre is this bit of
691 grammar:
692
693 stmt: simple_stmt | compound_stmt
694 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
695
696 A simple_stmt can contain multiple small_stmt elements joined
697 by semicolons. If the arg is a simple_stmt, the number of
698 small_stmt elements is returned.
699*/
700
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800701static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800702new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800703{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800704 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800705 if (res == NULL)
706 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800707 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
708 Py_DECREF(res);
709 return NULL;
710 }
711 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800712}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800713#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715static int
716num_stmts(const node *n)
717{
718 int i, l;
719 node *ch;
720
721 switch (TYPE(n)) {
722 case single_input:
723 if (TYPE(CHILD(n, 0)) == NEWLINE)
724 return 0;
725 else
726 return num_stmts(CHILD(n, 0));
727 case file_input:
728 l = 0;
729 for (i = 0; i < NCH(n); i++) {
730 ch = CHILD(n, i);
731 if (TYPE(ch) == stmt)
732 l += num_stmts(ch);
733 }
734 return l;
735 case stmt:
736 return num_stmts(CHILD(n, 0));
737 case compound_stmt:
738 return 1;
739 case simple_stmt:
740 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
741 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800742 case func_body_suite:
743 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
744 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 if (NCH(n) == 1)
746 return num_stmts(CHILD(n, 0));
747 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800748 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800750 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
751 i += 2;
752 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 l += num_stmts(CHILD(n, i));
754 return l;
755 }
756 default: {
757 char buf[128];
758
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000759 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 TYPE(n), NCH(n));
761 Py_FatalError(buf);
762 }
763 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700764 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767/* Transform the CST rooted at node * to the appropriate AST
768*/
769
770mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200771PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
772 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000774 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800776 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 stmt_ty s;
778 node *ch;
779 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500780 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800781 asdl_seq *argtypes = NULL;
782 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400784 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200785 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400786 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800787 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700788 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800789
790 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 switch (TYPE(n)) {
795 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200796 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500798 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 for (i = 0; i < NCH(n) - 1; i++) {
800 ch = CHILD(n, i);
801 if (TYPE(ch) == NEWLINE)
802 continue;
803 REQ(ch, stmt);
804 num = num_stmts(ch);
805 if (num == 1) {
806 s = ast_for_stmt(&c, ch);
807 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000809 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 else {
812 ch = CHILD(ch, 0);
813 REQ(ch, simple_stmt);
814 for (j = 0; j < num; j++) {
815 s = ast_for_stmt(&c, CHILD(ch, j * 2));
816 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000818 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820 }
821 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800822
823 /* Type ignores are stored under the ENDMARKER in file_input. */
824 ch = CHILD(n, NCH(n) - 1);
825 REQ(ch, ENDMARKER);
826 num = NCH(ch);
827 type_ignores = _Py_asdl_seq_new(num, arena);
828 if (!type_ignores)
829 goto out;
830
831 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700832 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
833 if (!type_comment)
834 goto out;
835 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800836 if (!ti)
837 goto out;
838 asdl_seq_SET(type_ignores, i, ti);
839 }
840
841 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 case eval_input: {
844 expr_ty testlist_ast;
845
Nick Coghlan650f0d02007-04-15 12:05:43 +0000846 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000847 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500849 goto out;
850 res = Expression(testlist_ast, arena);
851 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 }
853 case single_input:
854 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200855 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500857 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000859 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000861 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500862 goto out;
863 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
865 else {
866 n = CHILD(n, 0);
867 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200868 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000872 s = ast_for_stmt(&c, n);
873 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500874 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 asdl_seq_SET(stmts, 0, s);
876 }
877 else {
878 /* Only a simple_stmt can contain multiple statements. */
879 REQ(n, simple_stmt);
880 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 if (TYPE(CHILD(n, i)) == NEWLINE)
882 break;
883 s = ast_for_stmt(&c, CHILD(n, i));
884 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500885 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 asdl_seq_SET(stmts, i / 2, s);
887 }
888 }
889
Benjamin Peterson55e00432012-01-16 17:22:31 -0500890 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500892 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800893 case func_type_input:
894 n = CHILD(n, 0);
895 REQ(n, func_type);
896
897 if (TYPE(CHILD(n, 1)) == typelist) {
898 ch = CHILD(n, 1);
899 /* this is overly permissive -- we don't pay any attention to
900 * stars on the args -- just parse them into an ordered list */
901 num = 0;
902 for (i = 0; i < NCH(ch); i++) {
903 if (TYPE(CHILD(ch, i)) == test) {
904 num++;
905 }
906 }
907
908 argtypes = _Py_asdl_seq_new(num, arena);
909 if (!argtypes)
910 goto out;
911
912 j = 0;
913 for (i = 0; i < NCH(ch); i++) {
914 if (TYPE(CHILD(ch, i)) == test) {
915 arg = ast_for_expr(&c, CHILD(ch, i));
916 if (!arg)
917 goto out;
918 asdl_seq_SET(argtypes, j++, arg);
919 }
920 }
921 }
922 else {
923 argtypes = _Py_asdl_seq_new(0, arena);
924 if (!argtypes)
925 goto out;
926 }
927
928 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
929 if (!ret)
930 goto out;
931 res = FunctionType(argtypes, ret, arena);
932 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000934 PyErr_Format(PyExc_SystemError,
935 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500936 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500938 out:
939 if (c.c_normalize) {
940 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500941 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500942 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943}
944
Victor Stinner14e461d2013-08-26 22:28:21 +0200945mod_ty
946PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
947 PyArena *arena)
948{
949 mod_ty mod;
950 PyObject *filename;
951 filename = PyUnicode_DecodeFSDefault(filename_str);
952 if (filename == NULL)
953 return NULL;
954 mod = PyAST_FromNodeObject(n, flags, filename, arena);
955 Py_DECREF(filename);
956 return mod;
957
958}
959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
961*/
962
963static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800964get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965{
966 switch (TYPE(n)) {
967 case VBAR:
968 return BitOr;
969 case CIRCUMFLEX:
970 return BitXor;
971 case AMPER:
972 return BitAnd;
973 case LEFTSHIFT:
974 return LShift;
975 case RIGHTSHIFT:
976 return RShift;
977 case PLUS:
978 return Add;
979 case MINUS:
980 return Sub;
981 case STAR:
982 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400983 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800984 if (c->c_feature_version < 5) {
985 ast_error(c, n,
986 "The '@' operator is only supported in Python 3.5 and greater");
987 return (operator_ty)0;
988 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400989 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 case SLASH:
991 return Div;
992 case DOUBLESLASH:
993 return FloorDiv;
994 case PERCENT:
995 return Mod;
996 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 }
999}
1000
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001001static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001002 "None",
1003 "True",
1004 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001005 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001006 NULL,
1007};
1008
1009static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001010forbidden_name(struct compiling *c, identifier name, const node *n,
1011 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001012{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001013 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001014 const char * const *p = FORBIDDEN;
1015 if (!full_checks) {
1016 /* In most cases, the parser will protect True, False, and None
1017 from being assign to. */
1018 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001019 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001020 for (; *p; p++) {
1021 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1022 ast_error(c, n, "cannot assign to %U", name);
1023 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001024 }
1025 }
1026 return 0;
1027}
1028
Serhiy Storchakab619b092018-11-27 09:40:29 +02001029static expr_ty
1030copy_location(expr_ty e, const node *n)
1031{
1032 if (e) {
1033 e->lineno = LINENO(n);
1034 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001035 e->end_lineno = n->n_end_lineno;
1036 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001037 }
1038 return e;
1039}
1040
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001041static const char *
1042get_expr_name(expr_ty e)
1043{
1044 switch (e->kind) {
1045 case Attribute_kind:
1046 return "attribute";
1047 case Subscript_kind:
1048 return "subscript";
1049 case Starred_kind:
1050 return "starred";
1051 case Name_kind:
1052 return "name";
1053 case List_kind:
1054 return "list";
1055 case Tuple_kind:
1056 return "tuple";
1057 case Lambda_kind:
1058 return "lambda";
1059 case Call_kind:
1060 return "function call";
1061 case BoolOp_kind:
1062 case BinOp_kind:
1063 case UnaryOp_kind:
1064 return "operator";
1065 case GeneratorExp_kind:
1066 return "generator expression";
1067 case Yield_kind:
1068 case YieldFrom_kind:
1069 return "yield expression";
1070 case Await_kind:
1071 return "await expression";
1072 case ListComp_kind:
1073 return "list comprehension";
1074 case SetComp_kind:
1075 return "set comprehension";
1076 case DictComp_kind:
1077 return "dict comprehension";
1078 case Dict_kind:
1079 return "dict display";
1080 case Set_kind:
1081 return "set display";
1082 case JoinedStr_kind:
1083 case FormattedValue_kind:
1084 return "f-string expression";
1085 case Constant_kind: {
1086 PyObject *value = e->v.Constant.value;
1087 if (value == Py_None) {
1088 return "None";
1089 }
1090 if (value == Py_False) {
1091 return "False";
1092 }
1093 if (value == Py_True) {
1094 return "True";
1095 }
1096 if (value == Py_Ellipsis) {
1097 return "Ellipsis";
1098 }
1099 return "literal";
1100 }
1101 case Compare_kind:
1102 return "comparison";
1103 case IfExp_kind:
1104 return "conditional expression";
1105 case NamedExpr_kind:
1106 return "named expression";
1107 default:
1108 PyErr_Format(PyExc_SystemError,
1109 "unexpected expression in assignment %d (line %d)",
1110 e->kind, e->lineno);
1111 return NULL;
1112 }
1113}
1114
Jeremy Hyltona8293132006-02-28 17:58:27 +00001115/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116
1117 Only sets context for expr kinds that "can appear in assignment context"
1118 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1119 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120*/
1121
1122static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001123set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124{
1125 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001126
1127 /* The ast defines augmented store and load contexts, but the
1128 implementation here doesn't actually use them. The code may be
1129 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001130 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001131 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001132 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001133 */
1134 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135
1136 switch (e->kind) {
1137 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001138 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001139 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001140 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001143 e->v.Subscript.ctx = ctx;
1144 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001145 case Starred_kind:
1146 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001147 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001148 return 0;
1149 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001151 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001152 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001153 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001154 }
1155 e->v.Name.ctx = ctx;
1156 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001158 e->v.List.ctx = ctx;
1159 s = e->v.List.elts;
1160 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001162 e->v.Tuple.ctx = ctx;
1163 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001164 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001165 default: {
1166 const char *expr_name = get_expr_name(e);
1167 if (expr_name != NULL) {
1168 ast_error(c, n, "cannot %s %s",
1169 ctx == Store ? "assign to" : "delete",
1170 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001171 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001172 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001173 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001174 }
1175
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 */
1179 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001180 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001183 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001184 return 0;
1185 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 }
1187 return 1;
1188}
1189
1190static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001191ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192{
1193 REQ(n, augassign);
1194 n = CHILD(n, 0);
1195 switch (STR(n)[0]) {
1196 case '+':
1197 return Add;
1198 case '-':
1199 return Sub;
1200 case '/':
1201 if (STR(n)[1] == '/')
1202 return FloorDiv;
1203 else
1204 return Div;
1205 case '%':
1206 return Mod;
1207 case '<':
1208 return LShift;
1209 case '>':
1210 return RShift;
1211 case '&':
1212 return BitAnd;
1213 case '^':
1214 return BitXor;
1215 case '|':
1216 return BitOr;
1217 case '*':
1218 if (STR(n)[1] == '*')
1219 return Pow;
1220 else
1221 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001222 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001223 if (c->c_feature_version < 5) {
1224 ast_error(c, n,
1225 "The '@' operator is only supported in Python 3.5 and greater");
1226 return (operator_ty)0;
1227 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001228 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001230 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001231 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 }
1233}
1234
1235static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001236ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001238 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 |'is' 'not'
1240 */
1241 REQ(n, comp_op);
1242 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001243 n = CHILD(n, 0);
1244 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 case LESS:
1246 return Lt;
1247 case GREATER:
1248 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001249 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 return Eq;
1251 case LESSEQUAL:
1252 return LtE;
1253 case GREATEREQUAL:
1254 return GtE;
1255 case NOTEQUAL:
1256 return NotEq;
1257 case NAME:
1258 if (strcmp(STR(n), "in") == 0)
1259 return In;
1260 if (strcmp(STR(n), "is") == 0)
1261 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001262 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001264 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 }
1269 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001270 /* handle "not in" and "is not" */
1271 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 case NAME:
1273 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1274 return NotIn;
1275 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1276 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001277 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001279 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001281 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 }
Neal Norwitz79792652005-11-14 04:25:03 +00001284 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001286 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287}
1288
1289static asdl_seq *
1290seq_for_testlist(struct compiling *c, const node *n)
1291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001293 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1294 */
Armin Rigo31441302005-10-21 12:57:31 +00001295 asdl_seq *seq;
1296 expr_ty expression;
1297 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001298 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001300 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 if (!seq)
1302 return NULL;
1303
1304 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001306 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307
Benjamin Peterson4905e802009-09-27 02:43:28 +00001308 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001309 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311
1312 assert(i / 2 < seq->size);
1313 asdl_seq_SET(seq, i / 2, expression);
1314 }
1315 return seq;
1316}
1317
Neal Norwitzc1505362006-12-28 06:47:50 +00001318static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001319ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001320{
1321 identifier name;
1322 expr_ty annotation = NULL;
1323 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001324 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001325
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001326 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001327 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 name = NEW_IDENTIFIER(ch);
1329 if (!name)
1330 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001331 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001332 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001333
1334 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1335 annotation = ast_for_expr(c, CHILD(n, 2));
1336 if (!annotation)
1337 return NULL;
1338 }
1339
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001340 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001341 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001342 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001343 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001344 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345}
1346
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347/* returns -1 if failed to handle keyword only arguments
1348 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001350 ^^^
1351 start pointing here
1352 */
1353static int
1354handle_keywordonly_args(struct compiling *c, const node *n, int start,
1355 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1356{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001357 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001358 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001359 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001360 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 int i = start;
1362 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001363
1364 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001365 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001366 return -1;
1367 }
1368 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001369 while (i < NCH(n)) {
1370 ch = CHILD(n, i);
1371 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001372 case vfpdef:
1373 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001375 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001376 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001377 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 asdl_seq_SET(kwdefaults, j, expression);
1379 i += 2; /* '=' and test */
1380 }
1381 else { /* setting NULL if no default value exists */
1382 asdl_seq_SET(kwdefaults, j, NULL);
1383 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 if (NCH(ch) == 3) {
1385 /* ch is NAME ':' test */
1386 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001387 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001388 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001389 }
1390 else {
1391 annotation = NULL;
1392 }
1393 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001394 argname = NEW_IDENTIFIER(ch);
1395 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001397 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001398 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001399 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001400 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001401 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001402 if (!arg)
1403 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001404 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001405 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001406 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001407 i += 1; /* the comma, if present */
1408 break;
1409 case TYPE_COMMENT:
1410 /* arg will be equal to the last argument processed */
1411 arg->type_comment = NEW_TYPE_COMMENT(ch);
1412 if (!arg->type_comment)
1413 goto error;
1414 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 break;
1416 case DOUBLESTAR:
1417 return i;
1418 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001419 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 goto error;
1421 }
1422 }
1423 return i;
1424 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001426}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427
Jeremy Hyltona8293132006-02-28 17:58:27 +00001428/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429
1430static arguments_ty
1431ast_for_arguments(struct compiling *c, const node *n)
1432{
Neal Norwitzc1505362006-12-28 06:47:50 +00001433 /* This function handles both typedargslist (function definition)
1434 and varargslist (lambda definition).
1435
1436 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001437
1438 The following definition for typedarglist is equivalent to this set of rules:
1439
1440 arguments = argument (',' [TYPE_COMMENT] argument)*
1441 argument = tfpdef ['=' test]
1442 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1443 args = '*' [tfpdef]
1444 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1445 [TYPE_COMMENT] [kwargs]])
1446 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1447 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1448 [TYPE_COMMENT] [args_kwonly_kwargs]])
1449 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1450 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1451 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1452
1453 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1454 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1455 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1456 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1457 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1458 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1459 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1460 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1461 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1462 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1463 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1464 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1465 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1466 '**' tfpdef [','] [TYPE_COMMENT]))
1467
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001468 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001469
1470 The following definition for varargslist is equivalent to this set of rules:
1471
1472 arguments = argument (',' argument )*
1473 argument = vfpdef ['=' test]
1474 kwargs = '**' vfpdef [',']
1475 args = '*' [vfpdef]
1476 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1477 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1478 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1479 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1480 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1481 (vararglist_no_posonly)
1482
1483 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1484 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1485 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1486 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1487 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1488 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1489 [',']]] | '**' vfpdef [','])
1490
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001492
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001494 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001495 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001496 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001497 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001498 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 node *ch;
1500
1501 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001503 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001504 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001506 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507
Jeremy Hyltone921e022008-07-17 16:37:17 +00001508 /* First count the number of positional args & defaults. The
1509 variable i is the loop index for this for loop and the next.
1510 The next loop picks up where the first leaves off.
1511 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001513 ch = CHILD(n, i);
1514 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001515 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001516 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001517 if (i < NCH(n) && /* skip argument following star */
1518 (TYPE(CHILD(n, i)) == tfpdef ||
1519 TYPE(CHILD(n, i)) == vfpdef)) {
1520 i++;
1521 }
1522 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001523 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001524 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001525 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001526 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001527 if (TYPE(ch) == SLASH ) {
1528 nposonlyargs = nposargs;
1529 nposargs = 0;
1530 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001533 defaults for keyword only args */
1534 for ( ; i < NCH(n); ++i) {
1535 ch = CHILD(n, i);
1536 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001537 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001538 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001539 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1540 if (!posonlyargs && nposonlyargs) {
1541 return NULL;
1542 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001543 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001545 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001547 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001548 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001549 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001551 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001552 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001553 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001555 since we set NULL as default for keyword only argument w/o default
1556 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001557 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001558 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001559 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001560 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001561
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001562 /* tfpdef: NAME [':' test]
1563 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 */
1565 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001566 j = 0; /* index for defaults */
1567 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001568 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001570 ch = CHILD(n, i);
1571 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001572 case tfpdef:
1573 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1575 anything other than EQUAL or a comma? */
1576 /* XXX Should NCH(n) check be made a separate check? */
1577 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001578 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1579 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001580 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001581 assert(posdefaults != NULL);
1582 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001584 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001586 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001587 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001588 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001589 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001590 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001591 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001592 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001593 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001594 if (l < nposonlyargs) {
1595 asdl_seq_SET(posonlyargs, l++, arg);
1596 } else {
1597 asdl_seq_SET(posargs, k++, arg);
1598 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001599 i += 1; /* the name */
1600 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1601 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001603 case SLASH:
1604 /* Advance the slash and the comma. If there are more names
1605 * after the slash there will be a comma so we are advancing
1606 * the correct number of nodes. If the slash is the last item,
1607 * we will be advancing an extra token but then * i > NCH(n)
1608 * and the enclosing while will finish correctly. */
1609 i += 2;
1610 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001612 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001613 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1614 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001615 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001616 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001617 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001618 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001619 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001620 if (TYPE(ch) == COMMA) {
1621 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001622 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001623
1624 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1625 ast_error(c, CHILD(n, i),
1626 "bare * has associated type comment");
1627 return NULL;
1628 }
1629
Guido van Rossum4f72a782006-10-27 23:31:49 +00001630 res = handle_keywordonly_args(c, n, i,
1631 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001632 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001633 i = res; /* res has new position to process */
1634 }
1635 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001636 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001637 if (!vararg)
1638 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001639
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001640 i += 2; /* the star and the name */
1641 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1642 i += 1; /* the comma, if present */
1643
1644 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1645 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1646 if (!vararg->type_comment)
1647 return NULL;
1648 i += 1;
1649 }
1650
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001651 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1652 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001653 int res = 0;
1654 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 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 break;
1661 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001662 ch = CHILD(n, i+1); /* tfpdef */
1663 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001664 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001665 if (!kwarg)
1666 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001667 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001668 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001669 i += 1; /* the comma, if present */
1670 break;
1671 case TYPE_COMMENT:
1672 assert(i);
1673
1674 if (kwarg)
1675 arg = kwarg;
1676
1677 /* arg will be equal to the last argument processed */
1678 arg->type_comment = NEW_TYPE_COMMENT(ch);
1679 if (!arg->type_comment)
1680 return NULL;
1681 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 break;
1683 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001684 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 "unexpected node in varargslist: %d @ %d",
1686 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001687 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001688 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001690 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691}
1692
1693static expr_ty
1694ast_for_dotted_name(struct compiling *c, const node *n)
1695{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001696 expr_ty e;
1697 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001698 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001700 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701
1702 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001703
1704 lineno = LINENO(n);
1705 col_offset = n->n_col_offset;
1706
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001707 ch = CHILD(n, 0);
1708 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001710 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001711 e = Name(id, Load, lineno, col_offset,
1712 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
1716 for (i = 2; i < NCH(n); i+=2) {
1717 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001718 if (!id)
1719 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001720 e = Attribute(e, id, Load, lineno, col_offset,
1721 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001722 if (!e)
1723 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 }
1725
1726 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727}
1728
1729static expr_ty
1730ast_for_decorator(struct compiling *c, const node *n)
1731{
1732 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1733 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001734 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001737 REQ(CHILD(n, 0), AT);
1738 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1741 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001742 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001745 d = name_expr;
1746 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 }
1748 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001749 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001750 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001751 if (!d)
1752 return NULL;
1753 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 }
1755 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001756 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 if (!d)
1758 return NULL;
1759 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 }
1761
1762 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763}
1764
1765static asdl_seq*
1766ast_for_decorators(struct compiling *c, const node *n)
1767{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001768 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001769 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001773 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 if (!decorator_seq)
1775 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001778 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001779 if (!d)
1780 return NULL;
1781 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 }
1783 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784}
1785
1786static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001787ast_for_funcdef_impl(struct compiling *c, const node *n0,
1788 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001790 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001791 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001792 identifier name;
1793 arguments_ty args;
1794 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001795 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001796 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001797 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001798 node *tc;
1799 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800
Guido van Rossum495da292019-03-07 12:38:08 -08001801 if (is_async && c->c_feature_version < 5) {
1802 ast_error(c, n,
1803 "Async functions are only supported in Python 3.5 and greater");
1804 return NULL;
1805 }
1806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 REQ(n, funcdef);
1808
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 name = NEW_IDENTIFIER(CHILD(n, name_i));
1810 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001811 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001812 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1815 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001816 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001817 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1818 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1819 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001820 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001821 name_i += 2;
1822 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001823 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1824 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1825 if (!type_comment)
1826 return NULL;
1827 name_i += 1;
1828 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001829 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001831 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001832 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001834 if (NCH(CHILD(n, name_i + 3)) > 1) {
1835 /* Check if the suite has a type comment in it. */
1836 tc = CHILD(CHILD(n, name_i + 3), 1);
1837
1838 if (TYPE(tc) == TYPE_COMMENT) {
1839 if (type_comment != NULL) {
1840 ast_error(c, n, "Cannot have two type comments on def");
1841 return NULL;
1842 }
1843 type_comment = NEW_TYPE_COMMENT(tc);
1844 if (!type_comment)
1845 return NULL;
1846 }
1847 }
1848
Yury Selivanov75445082015-05-11 22:57:16 -04001849 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001850 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001851 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001853 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001854 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001855}
1856
1857static stmt_ty
1858ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1859{
Guido van Rossum495da292019-03-07 12:38:08 -08001860 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001861 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001862 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001863 REQ(CHILD(n, 1), funcdef);
1864
guoci90fc8982018-09-11 17:45:45 -04001865 return ast_for_funcdef_impl(c, n, decorator_seq,
1866 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001867}
1868
1869static stmt_ty
1870ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1871{
1872 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1873 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001874 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001875}
1876
1877
1878static stmt_ty
1879ast_for_async_stmt(struct compiling *c, const node *n)
1880{
Guido van Rossum495da292019-03-07 12:38:08 -08001881 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001882 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001883 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001884
1885 switch (TYPE(CHILD(n, 1))) {
1886 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001887 return ast_for_funcdef_impl(c, n, NULL,
1888 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001889 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001890 return ast_for_with_stmt(c, n,
1891 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001892
1893 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001894 return ast_for_for_stmt(c, n,
1895 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001896
1897 default:
1898 PyErr_Format(PyExc_SystemError,
1899 "invalid async stament: %s",
1900 STR(CHILD(n, 1)));
1901 return NULL;
1902 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903}
1904
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001905static stmt_ty
1906ast_for_decorated(struct compiling *c, const node *n)
1907{
Yury Selivanov75445082015-05-11 22:57:16 -04001908 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001909 stmt_ty thing = NULL;
1910 asdl_seq *decorator_seq = NULL;
1911
1912 REQ(n, decorated);
1913
1914 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1915 if (!decorator_seq)
1916 return NULL;
1917
1918 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001919 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001921
1922 if (TYPE(CHILD(n, 1)) == funcdef) {
1923 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1924 } else if (TYPE(CHILD(n, 1)) == classdef) {
1925 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001926 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1927 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001928 }
1929 return thing;
1930}
1931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001933ast_for_namedexpr(struct compiling *c, const node *n)
1934{
1935 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1936 ['else' ':' suite]
1937 namedexpr_test: test [':=' test]
1938 argument: ( test [comp_for] |
1939 test ':=' test |
1940 test '=' test |
1941 '**' test |
1942 '*' test )
1943 */
1944 expr_ty target, value;
1945
1946 target = ast_for_expr(c, CHILD(n, 0));
1947 if (!target)
1948 return NULL;
1949
1950 value = ast_for_expr(c, CHILD(n, 2));
1951 if (!value)
1952 return NULL;
1953
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001954 if (target->kind != Name_kind) {
1955 const char *expr_name = get_expr_name(target);
1956 if (expr_name != NULL) {
1957 ast_error(c, n, "cannot use named assignment with %s", expr_name);
1958 }
1959 return NULL;
1960 }
1961
1962 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001963 return NULL;
1964
1965 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1966 n->n_end_col_offset, c->c_arena);
1967}
1968
1969static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970ast_for_lambdef(struct compiling *c, const node *n)
1971{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001972 /* lambdef: 'lambda' [varargslist] ':' test
1973 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 arguments_ty args;
1975 expr_ty expression;
1976
1977 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001978 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 if (!args)
1980 return NULL;
1981 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 }
1985 else {
1986 args = ast_for_arguments(c, CHILD(n, 1));
1987 if (!args)
1988 return NULL;
1989 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001990 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 }
1993
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001994 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1995 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996}
1997
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001998static expr_ty
1999ast_for_ifexpr(struct compiling *c, const node *n)
2000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002002 expr_ty expression, body, orelse;
2003
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00002004 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002005 body = ast_for_expr(c, CHILD(n, 0));
2006 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002007 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002008 expression = ast_for_expr(c, CHILD(n, 2));
2009 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002010 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002011 orelse = ast_for_expr(c, CHILD(n, 4));
2012 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002013 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002014 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002015 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002016 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002017}
2018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002020 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021
Nick Coghlan650f0d02007-04-15 12:05:43 +00002022 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023*/
2024
2025static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002026count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002028 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029
Guido van Rossumd8faa362007-04-27 19:54:29 +00002030 count_comp_for:
2031 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002032 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002033 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08002034 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002036 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002037 else if (NCH(n) == 1) {
2038 n = CHILD(n, 0);
2039 }
2040 else {
2041 goto error;
2042 }
2043 if (NCH(n) == (5)) {
2044 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002045 }
2046 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002047 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002048 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002049 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002050 REQ(n, comp_iter);
2051 n = CHILD(n, 0);
2052 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002053 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002054 else if (TYPE(n) == comp_if) {
2055 if (NCH(n) == 3) {
2056 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002057 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002058 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002059 else
2060 return n_fors;
2061 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002062
Jelle Zijlstraac317702017-10-05 20:24:46 -07002063 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002064 /* Should never be reached */
2065 PyErr_SetString(PyExc_SystemError,
2066 "logic error in count_comp_fors");
2067 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068}
2069
Nick Coghlan650f0d02007-04-15 12:05:43 +00002070/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071
Nick Coghlan650f0d02007-04-15 12:05:43 +00002072 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073*/
2074
2075static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002076count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002078 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079
Guido van Rossumd8faa362007-04-27 19:54:29 +00002080 while (1) {
2081 REQ(n, comp_iter);
2082 if (TYPE(CHILD(n, 0)) == comp_for)
2083 return n_ifs;
2084 n = CHILD(n, 0);
2085 REQ(n, comp_if);
2086 n_ifs++;
2087 if (NCH(n) == 2)
2088 return n_ifs;
2089 n = CHILD(n, 2);
2090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091}
2092
Guido van Rossum992d4a32007-07-11 13:09:30 +00002093static asdl_seq *
2094ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002097 asdl_seq *comps;
2098
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002099 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 if (n_fors == -1)
2101 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002103 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002104 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002108 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002110 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002111 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002112 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002113 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114
Guido van Rossum992d4a32007-07-11 13:09:30 +00002115 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116
Jelle Zijlstraac317702017-10-05 20:24:46 -07002117 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002118 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002119 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002120 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002121 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002122 else {
2123 sync_n = CHILD(n, 0);
2124 }
2125 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002126
Guido van Rossum495da292019-03-07 12:38:08 -08002127 /* Async comprehensions only allowed in Python 3.6 and greater */
2128 if (is_async && c->c_feature_version < 6) {
2129 ast_error(c, n,
2130 "Async comprehensions are only supported in Python 3.6 and greater");
2131 return NULL;
2132 }
2133
Jelle Zijlstraac317702017-10-05 20:24:46 -07002134 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002135 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002136 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002138 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002141
Thomas Wouters89f507f2006-12-13 04:49:30 +00002142 /* Check the # of children rather than the length of t, since
2143 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002144 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002146 comp = comprehension(first, expression, NULL,
2147 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002149 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2150 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2151 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002152 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002153 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002155
Jelle Zijlstraac317702017-10-05 20:24:46 -07002156 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 int j, n_ifs;
2158 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159
Jelle Zijlstraac317702017-10-05 20:24:46 -07002160 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002161 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002162 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002165 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002170 REQ(n, comp_iter);
2171 n = CHILD(n, 0);
2172 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173
Guido van Rossum992d4a32007-07-11 13:09:30 +00002174 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002175 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002176 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002177 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002178 if (NCH(n) == 3)
2179 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002181 /* on exit, must guarantee that n is a comp_for */
2182 if (TYPE(n) == comp_iter)
2183 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002184 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002186 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002188 return comps;
2189}
2190
2191static expr_ty
2192ast_for_itercomp(struct compiling *c, const node *n, int type)
2193{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002194 /* testlist_comp: (test|star_expr)
2195 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002196 expr_ty elt;
2197 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002198 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199
Guido van Rossum992d4a32007-07-11 13:09:30 +00002200 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002202 ch = CHILD(n, 0);
2203 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002204 if (!elt)
2205 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002206 if (elt->kind == Starred_kind) {
2207 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2208 return NULL;
2209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210
Guido van Rossum992d4a32007-07-11 13:09:30 +00002211 comps = ast_for_comprehension(c, CHILD(n, 1));
2212 if (!comps)
2213 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002214
2215 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002216 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2217 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002218 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002219 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2220 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002221 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002222 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2223 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002224 else
2225 /* Should never happen */
2226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227}
2228
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002229/* Fills in the key, value pair corresponding to the dict element. In case
2230 * of an unpacking, key is NULL. *i is advanced by the number of ast
2231 * elements. Iff successful, nonzero is returned.
2232 */
2233static int
2234ast_for_dictelement(struct compiling *c, const node *n, int *i,
2235 expr_ty *key, expr_ty *value)
2236{
2237 expr_ty expression;
2238 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2239 assert(NCH(n) - *i >= 2);
2240
2241 expression = ast_for_expr(c, CHILD(n, *i + 1));
2242 if (!expression)
2243 return 0;
2244 *key = NULL;
2245 *value = expression;
2246
2247 *i += 2;
2248 }
2249 else {
2250 assert(NCH(n) - *i >= 3);
2251
2252 expression = ast_for_expr(c, CHILD(n, *i));
2253 if (!expression)
2254 return 0;
2255 *key = expression;
2256
2257 REQ(CHILD(n, *i + 1), COLON);
2258
2259 expression = ast_for_expr(c, CHILD(n, *i + 2));
2260 if (!expression)
2261 return 0;
2262 *value = expression;
2263
2264 *i += 3;
2265 }
2266 return 1;
2267}
2268
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002270ast_for_dictcomp(struct compiling *c, const node *n)
2271{
2272 expr_ty key, value;
2273 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002274 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002276 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002277 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002278 assert(key);
2279 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002281 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002282 if (!comps)
2283 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002285 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2286 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002287}
2288
2289static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002290ast_for_dictdisplay(struct compiling *c, const node *n)
2291{
2292 int i;
2293 int j;
2294 int size;
2295 asdl_seq *keys, *values;
2296
2297 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2298 keys = _Py_asdl_seq_new(size, c->c_arena);
2299 if (!keys)
2300 return NULL;
2301
2302 values = _Py_asdl_seq_new(size, c->c_arena);
2303 if (!values)
2304 return NULL;
2305
2306 j = 0;
2307 for (i = 0; i < NCH(n); i++) {
2308 expr_ty key, value;
2309
2310 if (!ast_for_dictelement(c, n, &i, &key, &value))
2311 return NULL;
2312 asdl_seq_SET(keys, j, key);
2313 asdl_seq_SET(values, j, value);
2314
2315 j++;
2316 }
2317 keys->size = j;
2318 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002319 return Dict(keys, values, LINENO(n), n->n_col_offset,
2320 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002321}
2322
2323static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002324ast_for_genexp(struct compiling *c, const node *n)
2325{
2326 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002327 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002328}
2329
2330static expr_ty
2331ast_for_listcomp(struct compiling *c, const node *n)
2332{
2333 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002334 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002335}
2336
2337static expr_ty
2338ast_for_setcomp(struct compiling *c, const node *n)
2339{
2340 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002341 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002342}
2343
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002344static expr_ty
2345ast_for_setdisplay(struct compiling *c, const node *n)
2346{
2347 int i;
2348 int size;
2349 asdl_seq *elts;
2350
2351 assert(TYPE(n) == (dictorsetmaker));
2352 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2353 elts = _Py_asdl_seq_new(size, c->c_arena);
2354 if (!elts)
2355 return NULL;
2356 for (i = 0; i < NCH(n); i += 2) {
2357 expr_ty expression;
2358 expression = ast_for_expr(c, CHILD(n, i));
2359 if (!expression)
2360 return NULL;
2361 asdl_seq_SET(elts, i / 2, expression);
2362 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002363 return Set(elts, LINENO(n), n->n_col_offset,
2364 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002365}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002366
2367static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368ast_for_atom(struct compiling *c, const node *n)
2369{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002370 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2371 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002372 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 */
2374 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002377 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002378 PyObject *name;
2379 const char *s = STR(ch);
2380 size_t len = strlen(s);
2381 if (len >= 4 && len <= 5) {
2382 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002383 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002384 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002385 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002386 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002387 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002388 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002389 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002390 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002391 }
2392 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002393 if (!name)
2394 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002395 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002396 return Name(name, Load, LINENO(n), n->n_col_offset,
2397 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002400 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002401 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002402 const char *errtype = NULL;
2403 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2404 errtype = "unicode error";
2405 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2406 errtype = "value error";
2407 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002408 PyObject *type, *value, *tback, *errstr;
2409 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002410 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002411 if (errstr) {
2412 ast_error(c, n, "(%s) %U", errtype, errstr);
2413 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002414 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002415 else {
2416 PyErr_Clear();
2417 ast_error(c, n, "(%s) unknown error", errtype);
2418 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002419 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002420 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002421 Py_XDECREF(tback);
2422 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002423 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002424 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002425 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 }
2427 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002428 PyObject *pynum;
2429 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2430 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2431 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2432 ast_error(c, ch,
2433 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2434 return NULL;
2435 }
2436 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002437 if (!pynum)
2438 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002439
Victor Stinner43d81952013-07-17 00:57:58 +02002440 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2441 Py_DECREF(pynum);
2442 return NULL;
2443 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002444 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002445 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 }
Georg Brandldde00282007-03-18 19:01:53 +00002447 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002448 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002449 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002451 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452
Thomas Wouters89f507f2006-12-13 04:49:30 +00002453 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002454 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2455 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456
Thomas Wouters89f507f2006-12-13 04:49:30 +00002457 if (TYPE(ch) == yield_expr)
2458 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002461 if (NCH(ch) == 1) {
2462 return ast_for_testlist(c, ch);
2463 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002464
Serhiy Storchakab619b092018-11-27 09:40:29 +02002465 if (TYPE(CHILD(ch, 1)) == comp_for) {
2466 return copy_location(ast_for_genexp(c, ch), n);
2467 }
2468 else {
2469 return copy_location(ast_for_testlist(c, ch), n);
2470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002472 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473
Thomas Wouters89f507f2006-12-13 04:49:30 +00002474 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002475 return List(NULL, Load, LINENO(n), n->n_col_offset,
2476 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477
Nick Coghlan650f0d02007-04-15 12:05:43 +00002478 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2480 asdl_seq *elts = seq_for_testlist(c, ch);
2481 if (!elts)
2482 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002483
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002484 return List(elts, Load, LINENO(n), n->n_col_offset,
2485 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002486 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002487 else {
2488 return copy_location(ast_for_listcomp(c, ch), n);
2489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002491 /* dictorsetmaker: ( ((test ':' test | '**' test)
2492 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2493 * ((test | '*' test)
2494 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002495 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002496 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002497 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002498 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002499 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2500 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002501 }
2502 else {
2503 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2504 if (NCH(ch) == 1 ||
2505 (NCH(ch) > 1 &&
2506 TYPE(CHILD(ch, 1)) == COMMA)) {
2507 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002508 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002509 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002510 else if (NCH(ch) > 1 &&
2511 TYPE(CHILD(ch, 1)) == comp_for) {
2512 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002513 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002514 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002515 else if (NCH(ch) > 3 - is_dict &&
2516 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2517 /* It's a dictionary comprehension. */
2518 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002519 ast_error(c, n,
2520 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002521 return NULL;
2522 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002523 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002524 }
2525 else {
2526 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002527 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002528 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002529 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002530 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002533 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2534 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 }
2536}
2537
2538static slice_ty
2539ast_for_slice(struct compiling *c, const node *n)
2540{
2541 node *ch;
2542 expr_ty lower = NULL, upper = NULL, step = NULL;
2543
2544 REQ(n, subscript);
2545
2546 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002547 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 sliceop: ':' [test]
2549 */
2550 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 if (NCH(n) == 1 && TYPE(ch) == test) {
2552 /* 'step' variable hold no significance in terms of being used over
2553 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 if (!step)
2556 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557
Thomas Wouters89f507f2006-12-13 04:49:30 +00002558 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 }
2560
2561 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002562 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 if (!lower)
2564 return NULL;
2565 }
2566
2567 /* If there's an upper bound it's in the second or third position. */
2568 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002569 if (NCH(n) > 1) {
2570 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571
Thomas Wouters89f507f2006-12-13 04:49:30 +00002572 if (TYPE(n2) == test) {
2573 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 if (!upper)
2575 return NULL;
2576 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002579 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580
Thomas Wouters89f507f2006-12-13 04:49:30 +00002581 if (TYPE(n2) == test) {
2582 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 if (!upper)
2584 return NULL;
2585 }
2586 }
2587
2588 ch = CHILD(n, NCH(n) - 1);
2589 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002590 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002591 ch = CHILD(ch, 1);
2592 if (TYPE(ch) == test) {
2593 step = ast_for_expr(c, ch);
2594 if (!step)
2595 return NULL;
2596 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 }
2598 }
2599
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002600 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601}
2602
2603static expr_ty
2604ast_for_binop(struct compiling *c, const node *n)
2605{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002606 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002608 BinOp(BinOp(A, op, B), op, C).
2609 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610
Guido van Rossumd8faa362007-04-27 19:54:29 +00002611 int i, nops;
2612 expr_ty expr1, expr2, result;
2613 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614
Guido van Rossumd8faa362007-04-27 19:54:29 +00002615 expr1 = ast_for_expr(c, CHILD(n, 0));
2616 if (!expr1)
2617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Guido van Rossumd8faa362007-04-27 19:54:29 +00002619 expr2 = ast_for_expr(c, CHILD(n, 2));
2620 if (!expr2)
2621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622
Guido van Rossum495da292019-03-07 12:38:08 -08002623 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002624 if (!newoperator)
2625 return NULL;
2626
2627 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002628 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002629 c->c_arena);
2630 if (!result)
2631 return NULL;
2632
2633 nops = (NCH(n) - 1) / 2;
2634 for (i = 1; i < nops; i++) {
2635 expr_ty tmp_result, tmp;
2636 const node* next_oper = CHILD(n, i * 2 + 1);
2637
Guido van Rossum495da292019-03-07 12:38:08 -08002638 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 return NULL;
2641
Guido van Rossumd8faa362007-04-27 19:54:29 +00002642 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2643 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 return NULL;
2645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002647 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002648 CHILD(n, i * 2 + 2)->n_end_lineno,
2649 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002650 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002652 return NULL;
2653 result = tmp_result;
2654 }
2655 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656}
2657
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002658static expr_ty
2659ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002662 subscriptlist: subscript (',' subscript)* [',']
2663 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2664 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002665 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002666 REQ(n, trailer);
2667 if (TYPE(CHILD(n, 0)) == LPAR) {
2668 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002669 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2670 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002671 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002672 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002673 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002674 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002675 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2676 if (!attr_id)
2677 return NULL;
2678 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002679 LINENO(n), n->n_col_offset,
2680 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002681 }
2682 else {
2683 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002684 REQ(CHILD(n, 2), RSQB);
2685 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002686 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002687 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2688 if (!slc)
2689 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002690 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002691 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002692 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002693 }
2694 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002696 by treating the sequence as a tuple literal if there are
2697 no slice features.
2698 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002699 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002700 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002701 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002702 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002703 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002704 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002705 if (!slices)
2706 return NULL;
2707 for (j = 0; j < NCH(n); j += 2) {
2708 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002709 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002710 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002711 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002712 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002713 asdl_seq_SET(slices, j / 2, slc);
2714 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002715 if (!simple) {
2716 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002717 Load, LINENO(n), n->n_col_offset,
2718 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002719 }
2720 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002721 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002722 if (!elts)
2723 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002724 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2725 slc = (slice_ty)asdl_seq_GET(slices, j);
2726 assert(slc->kind == Index_kind && slc->v.Index.value);
2727 asdl_seq_SET(elts, j, slc->v.Index.value);
2728 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002729 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2730 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002731 if (!e)
2732 return NULL;
2733 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002734 Load, LINENO(n), n->n_col_offset,
2735 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002736 }
2737 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002738}
2739
2740static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002741ast_for_factor(struct compiling *c, const node *n)
2742{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002743 expr_ty expression;
2744
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002745 expression = ast_for_expr(c, CHILD(n, 1));
2746 if (!expression)
2747 return NULL;
2748
2749 switch (TYPE(CHILD(n, 0))) {
2750 case PLUS:
2751 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002752 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002753 c->c_arena);
2754 case MINUS:
2755 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002756 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002757 c->c_arena);
2758 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002759 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2760 n->n_end_lineno, n->n_end_col_offset,
2761 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002762 }
2763 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2764 TYPE(CHILD(n, 0)));
2765 return NULL;
2766}
2767
2768static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002769ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002770{
Yury Selivanov75445082015-05-11 22:57:16 -04002771 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002772 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002773
2774 REQ(n, atom_expr);
2775 nch = NCH(n);
2776
Guido van Rossum495da292019-03-07 12:38:08 -08002777 if (TYPE(CHILD(n, 0)) == AWAIT) {
2778 if (c->c_feature_version < 5) {
2779 ast_error(c, n,
2780 "Await expressions are only supported in Python 3.5 and greater");
2781 return NULL;
2782 }
Yury Selivanov75445082015-05-11 22:57:16 -04002783 start = 1;
2784 assert(nch > 1);
2785 }
2786
2787 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002788 if (!e)
2789 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002790 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002791 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002792 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002793 return Await(e, LINENO(n), n->n_col_offset,
2794 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002795 }
2796
2797 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002798 node *ch = CHILD(n, i);
2799 if (TYPE(ch) != trailer)
2800 break;
2801 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002802 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002803 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002804 tmp->lineno = e->lineno;
2805 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002806 e = tmp;
2807 }
Yury Selivanov75445082015-05-11 22:57:16 -04002808
2809 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002810 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002811 return Await(e, LINENO(n), n->n_col_offset,
2812 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002813 }
2814 else {
2815 return e;
2816 }
2817}
2818
2819static expr_ty
2820ast_for_power(struct compiling *c, const node *n)
2821{
2822 /* power: atom trailer* ('**' factor)*
2823 */
2824 expr_ty e;
2825 REQ(n, power);
2826 e = ast_for_atom_expr(c, CHILD(n, 0));
2827 if (!e)
2828 return NULL;
2829 if (NCH(n) == 1)
2830 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002831 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2832 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002833 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002834 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002835 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2836 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002837 }
2838 return e;
2839}
2840
Guido van Rossum0368b722007-05-11 16:50:42 +00002841static expr_ty
2842ast_for_starred(struct compiling *c, const node *n)
2843{
2844 expr_ty tmp;
2845 REQ(n, star_expr);
2846
2847 tmp = ast_for_expr(c, CHILD(n, 1));
2848 if (!tmp)
2849 return NULL;
2850
2851 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002852 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2853 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002854}
2855
2856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857/* Do not name a variable 'expr'! Will cause a compile error.
2858*/
2859
2860static expr_ty
2861ast_for_expr(struct compiling *c, const node *n)
2862{
2863 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002864 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002865 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002866 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 and_test: not_test ('and' not_test)*
2869 not_test: 'not' not_test | comparison
2870 comparison: expr (comp_op expr)*
2871 expr: xor_expr ('|' xor_expr)*
2872 xor_expr: and_expr ('^' and_expr)*
2873 and_expr: shift_expr ('&' shift_expr)*
2874 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2875 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002876 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002878 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002879 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002880 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 */
2882
2883 asdl_seq *seq;
2884 int i;
2885
2886 loop:
2887 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002888 case namedexpr_test:
2889 if (NCH(n) == 3)
2890 return ast_for_namedexpr(c, n);
2891 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002893 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002894 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002895 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002897 else if (NCH(n) > 1)
2898 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002899 /* Fallthrough */
2900 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 case and_test:
2902 if (NCH(n) == 1) {
2903 n = CHILD(n, 0);
2904 goto loop;
2905 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002906 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 if (!seq)
2908 return NULL;
2909 for (i = 0; i < NCH(n); i += 2) {
2910 expr_ty e = ast_for_expr(c, CHILD(n, i));
2911 if (!e)
2912 return NULL;
2913 asdl_seq_SET(seq, i / 2, e);
2914 }
2915 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002917 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002918 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002919 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002920 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2921 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 case not_test:
2923 if (NCH(n) == 1) {
2924 n = CHILD(n, 0);
2925 goto loop;
2926 }
2927 else {
2928 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2929 if (!expression)
2930 return NULL;
2931
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002932 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002933 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002934 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 }
2936 case comparison:
2937 if (NCH(n) == 1) {
2938 n = CHILD(n, 0);
2939 goto loop;
2940 }
2941 else {
2942 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002943 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002944 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002945 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 if (!ops)
2947 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002948 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 return NULL;
2951 }
2952 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002953 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002955 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002956 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002958 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959
2960 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002961 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002965 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 asdl_seq_SET(cmps, i / 2, expression);
2967 }
2968 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002969 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002973 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2974 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976
Guido van Rossum0368b722007-05-11 16:50:42 +00002977 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 /* The next five cases all handle BinOps. The main body of code
2980 is the same in each case, but the switch turned inside out to
2981 reuse the code for each type of operator.
2982 */
2983 case expr:
2984 case xor_expr:
2985 case and_expr:
2986 case shift_expr:
2987 case arith_expr:
2988 case term:
2989 if (NCH(n) == 1) {
2990 n = CHILD(n, 0);
2991 goto loop;
2992 }
2993 return ast_for_binop(c, n);
2994 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002995 node *an = NULL;
2996 node *en = NULL;
2997 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002998 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002999 if (NCH(n) > 1)
3000 an = CHILD(n, 1); /* yield_arg */
3001 if (an) {
3002 en = CHILD(an, NCH(an) - 1);
3003 if (NCH(an) == 2) {
3004 is_from = 1;
3005 exp = ast_for_expr(c, en);
3006 }
3007 else
3008 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 if (!exp)
3010 return NULL;
3011 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05003012 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003013 return YieldFrom(exp, LINENO(n), n->n_col_offset,
3014 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
3015 return Yield(exp, LINENO(n), n->n_col_offset,
3016 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003017 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003018 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 if (NCH(n) == 1) {
3020 n = CHILD(n, 0);
3021 goto loop;
3022 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003023 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00003024 case power:
3025 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003027 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 return NULL;
3029 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003030 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 return NULL;
3032}
3033
3034static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02003035ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003036 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037{
3038 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003039 arglist: argument (',' argument)* [',']
3040 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 */
3042
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003043 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003044 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003045 asdl_seq *args;
3046 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047
3048 REQ(n, arglist);
3049
3050 nargs = 0;
3051 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003053 node *ch = CHILD(n, i);
3054 if (TYPE(ch) == argument) {
3055 if (NCH(ch) == 1)
3056 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003057 else if (TYPE(CHILD(ch, 1)) == comp_for) {
3058 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02003059 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003060 ast_error(c, ch, "invalid syntax");
3061 return NULL;
3062 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003063 if (NCH(n) > 1) {
3064 ast_error(c, ch, "Generator expression must be parenthesized");
3065 return NULL;
3066 }
3067 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003068 else if (TYPE(CHILD(ch, 0)) == STAR)
3069 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003070 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3071 nargs++;
3072 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003074 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003075 nkeywords++;
3076 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003079 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003081 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003082 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003084 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003085
3086 nargs = 0; /* positional arguments + iterable argument unpackings */
3087 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3088 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003090 node *ch = CHILD(n, i);
3091 if (TYPE(ch) == argument) {
3092 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003093 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003094 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003095 /* a positional argument */
3096 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003097 if (ndoublestars) {
3098 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003099 "positional argument follows "
3100 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003101 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003102 else {
3103 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003104 "positional argument follows "
3105 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003106 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003107 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003108 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003109 e = ast_for_expr(c, chch);
3110 if (!e)
3111 return NULL;
3112 asdl_seq_SET(args, nargs++, e);
3113 }
3114 else if (TYPE(chch) == STAR) {
3115 /* an iterable argument unpacking */
3116 expr_ty starred;
3117 if (ndoublestars) {
3118 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003119 "iterable argument unpacking follows "
3120 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003121 return NULL;
3122 }
3123 e = ast_for_expr(c, CHILD(ch, 1));
3124 if (!e)
3125 return NULL;
3126 starred = Starred(e, Load, LINENO(chch),
3127 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003128 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003129 c->c_arena);
3130 if (!starred)
3131 return NULL;
3132 asdl_seq_SET(args, nargs++, starred);
3133
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003134 }
3135 else if (TYPE(chch) == DOUBLESTAR) {
3136 /* a keyword argument unpacking */
3137 keyword_ty kw;
3138 i++;
3139 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003141 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003142 kw = keyword(NULL, e, c->c_arena);
3143 asdl_seq_SET(keywords, nkeywords++, kw);
3144 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003146 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003147 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003148 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003150 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003151 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003153 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3154 /* treat colon equal as positional argument */
3155 if (nkeywords) {
3156 if (ndoublestars) {
3157 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003158 "positional argument follows "
3159 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003160 }
3161 else {
3162 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003163 "positional argument follows "
3164 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003165 }
3166 return NULL;
3167 }
3168 e = ast_for_namedexpr(c, ch);
3169 if (!e)
3170 return NULL;
3171 asdl_seq_SET(args, nargs++, e);
3172 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003173 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003174 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003175 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003176 identifier key, tmp;
3177 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003179 // To remain LL(1), the grammar accepts any test (basically, any
3180 // expression) in the keyword slot of a call site. So, we need
3181 // to manually enforce that the keyword is a NAME here.
3182 static const int name_tree[] = {
3183 test,
3184 or_test,
3185 and_test,
3186 not_test,
3187 comparison,
3188 expr,
3189 xor_expr,
3190 and_expr,
3191 shift_expr,
3192 arith_expr,
3193 term,
3194 factor,
3195 power,
3196 atom_expr,
3197 atom,
3198 0,
3199 };
3200 node *expr_node = chch;
3201 for (int i = 0; name_tree[i]; i++) {
3202 if (TYPE(expr_node) != name_tree[i])
3203 break;
3204 if (NCH(expr_node) != 1)
3205 break;
3206 expr_node = CHILD(expr_node, 0);
3207 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003208 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003209 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003210 "expression cannot contain assignment, "
3211 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003212 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003213 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003214 key = new_identifier(STR(expr_node), c);
3215 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003216 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003218 if (forbidden_name(c, key, chch, 1)) {
3219 return NULL;
3220 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003221 for (k = 0; k < nkeywords; k++) {
3222 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003223 if (tmp && !PyUnicode_Compare(tmp, key)) {
3224 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003225 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003226 return NULL;
3227 }
3228 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003229 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003231 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003232 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003234 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003235 asdl_seq_SET(keywords, nkeywords++, kw);
3236 }
3237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 }
3239
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003240 return Call(func, args, keywords, func->lineno, func->col_offset,
3241 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242}
3243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003245ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003247 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003248 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003250 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003251 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003252 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003253 }
3254 else {
3255 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003256 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003259 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 else {
3261 asdl_seq *tmp = seq_for_testlist(c, n);
3262 if (!tmp)
3263 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003264 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3265 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003267}
3268
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269static stmt_ty
3270ast_for_expr_stmt(struct compiling *c, const node *n)
3271{
3272 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003273 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003274 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3275 annassign: ':' test ['=' (yield_expr|testlist)]
3276 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3277 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3278 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003279 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003281 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003283 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003284 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 if (!e)
3286 return NULL;
3287
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003288 return Expr(e, 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 }
3291 else if (TYPE(CHILD(n, 1)) == augassign) {
3292 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003293 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003294 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 if (!expr1)
3298 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003299 if(!set_context(c, expr1, Store, ch))
3300 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003301 /* set_context checks that most expressions are not the left side.
3302 Augmented assignments can only have a name, a subscript, or an
3303 attribute on the left, though, so we have to explicitly check for
3304 those. */
3305 switch (expr1->kind) {
3306 case Name_kind:
3307 case Attribute_kind:
3308 case Subscript_kind:
3309 break;
3310 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003311 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003312 return NULL;
3313 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314
Thomas Wouters89f507f2006-12-13 04:49:30 +00003315 ch = CHILD(n, 2);
3316 if (TYPE(ch) == testlist)
3317 expr2 = ast_for_testlist(c, ch);
3318 else
3319 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003320 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 return NULL;
3322
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003323 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003324 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325 return NULL;
3326
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003327 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3328 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003330 else if (TYPE(CHILD(n, 1)) == annassign) {
3331 expr_ty expr1, expr2, expr3;
3332 node *ch = CHILD(n, 0);
3333 node *deep, *ann = CHILD(n, 1);
3334 int simple = 1;
3335
Guido van Rossum495da292019-03-07 12:38:08 -08003336 /* AnnAssigns are only allowed in Python 3.6 or greater */
3337 if (c->c_feature_version < 6) {
3338 ast_error(c, ch,
3339 "Variable annotation syntax is only supported in Python 3.6 and greater");
3340 return NULL;
3341 }
3342
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003343 /* we keep track of parens to qualify (x) as expression not name */
3344 deep = ch;
3345 while (NCH(deep) == 1) {
3346 deep = CHILD(deep, 0);
3347 }
3348 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3349 simple = 0;
3350 }
3351 expr1 = ast_for_testlist(c, ch);
3352 if (!expr1) {
3353 return NULL;
3354 }
3355 switch (expr1->kind) {
3356 case Name_kind:
3357 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3358 return NULL;
3359 }
3360 expr1->v.Name.ctx = Store;
3361 break;
3362 case Attribute_kind:
3363 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3364 return NULL;
3365 }
3366 expr1->v.Attribute.ctx = Store;
3367 break;
3368 case Subscript_kind:
3369 expr1->v.Subscript.ctx = Store;
3370 break;
3371 case List_kind:
3372 ast_error(c, ch,
3373 "only single target (not list) can be annotated");
3374 return NULL;
3375 case Tuple_kind:
3376 ast_error(c, ch,
3377 "only single target (not tuple) can be annotated");
3378 return NULL;
3379 default:
3380 ast_error(c, ch,
3381 "illegal target for annotation");
3382 return NULL;
3383 }
3384
3385 if (expr1->kind != Name_kind) {
3386 simple = 0;
3387 }
3388 ch = CHILD(ann, 1);
3389 expr2 = ast_for_expr(c, ch);
3390 if (!expr2) {
3391 return NULL;
3392 }
3393 if (NCH(ann) == 2) {
3394 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003395 LINENO(n), n->n_col_offset,
3396 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003397 }
3398 else {
3399 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003400 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003401 expr3 = ast_for_testlist(c, ch);
3402 }
3403 else {
3404 expr3 = ast_for_expr(c, ch);
3405 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003406 if (!expr3) {
3407 return NULL;
3408 }
3409 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003410 LINENO(n), n->n_col_offset,
3411 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003412 }
3413 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003415 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003416 asdl_seq *targets;
3417 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003418 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003419 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420
Thomas Wouters89f507f2006-12-13 04:49:30 +00003421 /* a normal assignment */
3422 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003423
3424 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3425 nch_minus_type = num - has_type_comment;
3426
3427 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003428 if (!targets)
3429 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003430 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003431 expr_ty e;
3432 node *ch = CHILD(n, i);
3433 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003434 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003435 return NULL;
3436 }
3437 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003439 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003441 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003442 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003443 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444
Thomas Wouters89f507f2006-12-13 04:49:30 +00003445 asdl_seq_SET(targets, i / 2, e);
3446 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003447 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003448 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003449 expression = ast_for_testlist(c, value);
3450 else
3451 expression = ast_for_expr(c, value);
3452 if (!expression)
3453 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003454 if (has_type_comment) {
3455 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3456 if (!type_comment)
3457 return NULL;
3458 }
3459 else
3460 type_comment = NULL;
3461 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003462 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464}
3465
Benjamin Peterson78565b22009-06-28 19:19:51 +00003466
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003468ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469{
3470 asdl_seq *seq;
3471 int i;
3472 expr_ty e;
3473
3474 REQ(n, exprlist);
3475
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003476 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 e = ast_for_expr(c, CHILD(n, i));
3481 if (!e)
3482 return NULL;
3483 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003484 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003485 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 }
3487 return seq;
3488}
3489
3490static stmt_ty
3491ast_for_del_stmt(struct compiling *c, const node *n)
3492{
3493 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 /* del_stmt: 'del' exprlist */
3496 REQ(n, del_stmt);
3497
3498 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3499 if (!expr_list)
3500 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003501 return Delete(expr_list, LINENO(n), n->n_col_offset,
3502 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503}
3504
3505static stmt_ty
3506ast_for_flow_stmt(struct compiling *c, const node *n)
3507{
3508 /*
3509 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3510 | yield_stmt
3511 break_stmt: 'break'
3512 continue_stmt: 'continue'
3513 return_stmt: 'return' [testlist]
3514 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003515 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 raise_stmt: 'raise' [test [',' test [',' test]]]
3517 */
3518 node *ch;
3519
3520 REQ(n, flow_stmt);
3521 ch = CHILD(n, 0);
3522 switch (TYPE(ch)) {
3523 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003524 return Break(LINENO(n), n->n_col_offset,
3525 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003527 return Continue(LINENO(n), n->n_col_offset,
3528 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003530 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3531 if (!exp)
3532 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003533 return Expr(exp, LINENO(n), n->n_col_offset,
3534 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535 }
3536 case return_stmt:
3537 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003538 return Return(NULL, LINENO(n), n->n_col_offset,
3539 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003541 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 if (!expression)
3543 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003544 return Return(expression, LINENO(n), n->n_col_offset,
3545 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 }
3547 case raise_stmt:
3548 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003549 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3550 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003551 else if (NCH(ch) >= 2) {
3552 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3554 if (!expression)
3555 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003556 if (NCH(ch) == 4) {
3557 cause = ast_for_expr(c, CHILD(ch, 3));
3558 if (!cause)
3559 return NULL;
3560 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003561 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3562 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 }
Stefan Krahf432a322017-08-21 13:09:59 +02003564 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003566 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 "unexpected flow_stmt: %d", TYPE(ch));
3568 return NULL;
3569 }
3570}
3571
3572static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003573alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574{
3575 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003576 import_as_name: NAME ['as' NAME]
3577 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 dotted_name: NAME ('.' NAME)*
3579 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003580 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 loop:
3583 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003584 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003585 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003586 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003587 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003588 if (!name)
3589 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003590 if (NCH(n) == 3) {
3591 node *str_node = CHILD(n, 2);
3592 str = NEW_IDENTIFIER(str_node);
3593 if (!str)
3594 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003595 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003596 return NULL;
3597 }
3598 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003599 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003600 return NULL;
3601 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003602 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 case dotted_as_name:
3605 if (NCH(n) == 1) {
3606 n = CHILD(n, 0);
3607 goto loop;
3608 }
3609 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003610 node *asname_node = CHILD(n, 2);
3611 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003612 if (!a)
3613 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003615 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003616 if (!a->asname)
3617 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003618 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return a;
3621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003623 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003624 node *name_node = CHILD(n, 0);
3625 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003626 if (!name)
3627 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003628 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003629 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003630 return alias(name, NULL, c->c_arena);
3631 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 else {
3633 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003634 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003635 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638
3639 len = 0;
3640 for (i = 0; i < NCH(n); i += 2)
3641 /* length of string plus one for the dot */
3642 len += strlen(STR(CHILD(n, i))) + 1;
3643 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003644 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 if (!str)
3646 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003647 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 if (!s)
3649 return NULL;
3650 for (i = 0; i < NCH(n); i += 2) {
3651 char *sch = STR(CHILD(n, i));
3652 strcpy(s, STR(CHILD(n, i)));
3653 s += strlen(sch);
3654 *s++ = '.';
3655 }
3656 --s;
3657 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3659 PyBytes_GET_SIZE(str),
3660 NULL);
3661 Py_DECREF(str);
3662 if (!uni)
3663 return NULL;
3664 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003665 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003666 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3667 Py_DECREF(str);
3668 return NULL;
3669 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003670 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003673 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003674 if (!str)
3675 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003676 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3677 Py_DECREF(str);
3678 return NULL;
3679 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003680 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003682 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 "unexpected import name: %d", TYPE(n));
3684 return NULL;
3685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686}
3687
3688static stmt_ty
3689ast_for_import_stmt(struct compiling *c, const node *n)
3690{
3691 /*
3692 import_stmt: import_name | import_from
3693 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003694 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3695 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003697 int lineno;
3698 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 int i;
3700 asdl_seq *aliases;
3701
3702 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003703 lineno = LINENO(n);
3704 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003706 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003708 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003709 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003710 if (!aliases)
3711 return NULL;
3712 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003713 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003714 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003716 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003718 // Even though n is modified above, the end position is not changed
3719 return Import(aliases, lineno, col_offset,
3720 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003722 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003724 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003725 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003726 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003727 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003729 /* Count the number of dots (for relative imports) and check for the
3730 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003731 for (idx = 1; idx < NCH(n); idx++) {
3732 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003733 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3734 if (!mod)
3735 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003736 idx++;
3737 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003738 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003740 ndots += 3;
3741 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003742 } else if (TYPE(CHILD(n, idx)) != DOT) {
3743 break;
3744 }
3745 ndots++;
3746 }
3747 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003748 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003749 case STAR:
3750 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003751 n = CHILD(n, idx);
3752 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003753 break;
3754 case LPAR:
3755 /* from ... import (x, y, z) */
3756 n = CHILD(n, idx + 1);
3757 n_children = NCH(n);
3758 break;
3759 case import_as_names:
3760 /* from ... import x, y, z */
3761 n = CHILD(n, idx);
3762 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003763 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003764 ast_error(c, n,
3765 "trailing comma not allowed without"
3766 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 return NULL;
3768 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003769 break;
3770 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003771 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003772 return NULL;
3773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003775 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003776 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778
3779 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003780 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003781 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003782 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003784 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003786 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003787 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003788 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003789 if (!import_alias)
3790 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003791 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003794 if (mod != NULL)
3795 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003796 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003797 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003798 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799 }
Neal Norwitz79792652005-11-14 04:25:03 +00003800 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 "unknown import statement: starts with command '%s'",
3802 STR(CHILD(n, 0)));
3803 return NULL;
3804}
3805
3806static stmt_ty
3807ast_for_global_stmt(struct compiling *c, const node *n)
3808{
3809 /* global_stmt: 'global' NAME (',' NAME)* */
3810 identifier name;
3811 asdl_seq *s;
3812 int i;
3813
3814 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003815 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003819 name = NEW_IDENTIFIER(CHILD(n, i));
3820 if (!name)
3821 return NULL;
3822 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003824 return Global(s, LINENO(n), n->n_col_offset,
3825 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826}
3827
3828static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003829ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3830{
3831 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3832 identifier name;
3833 asdl_seq *s;
3834 int i;
3835
3836 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003837 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003838 if (!s)
3839 return NULL;
3840 for (i = 1; i < NCH(n); i += 2) {
3841 name = NEW_IDENTIFIER(CHILD(n, i));
3842 if (!name)
3843 return NULL;
3844 asdl_seq_SET(s, i / 2, name);
3845 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003846 return Nonlocal(s, LINENO(n), n->n_col_offset,
3847 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003848}
3849
3850static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851ast_for_assert_stmt(struct compiling *c, const node *n)
3852{
3853 /* assert_stmt: 'assert' test [',' test] */
3854 REQ(n, assert_stmt);
3855 if (NCH(n) == 2) {
3856 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3857 if (!expression)
3858 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003859 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3860 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 }
3862 else if (NCH(n) == 4) {
3863 expr_ty expr1, expr2;
3864
3865 expr1 = ast_for_expr(c, CHILD(n, 1));
3866 if (!expr1)
3867 return NULL;
3868 expr2 = ast_for_expr(c, CHILD(n, 3));
3869 if (!expr2)
3870 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003872 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3873 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874 }
Neal Norwitz79792652005-11-14 04:25:03 +00003875 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876 "improper number of parts to 'assert' statement: %d",
3877 NCH(n));
3878 return NULL;
3879}
3880
3881static asdl_seq *
3882ast_for_suite(struct compiling *c, const node *n)
3883{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003884 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003885 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 stmt_ty s;
3887 int i, total, num, end, pos = 0;
3888 node *ch;
3889
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003890 if (TYPE(n) != func_body_suite) {
3891 REQ(n, suite);
3892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893
3894 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003895 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003897 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003899 n = CHILD(n, 0);
3900 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003902 */
3903 end = NCH(n) - 1;
3904 if (TYPE(CHILD(n, end - 1)) == SEMI)
3905 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003907 for (i = 0; i < end; i += 2) {
3908 ch = CHILD(n, i);
3909 s = ast_for_stmt(c, ch);
3910 if (!s)
3911 return NULL;
3912 asdl_seq_SET(seq, pos++, s);
3913 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 }
3915 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003916 i = 2;
3917 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3918 i += 2;
3919 REQ(CHILD(n, 2), NEWLINE);
3920 }
3921
3922 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003923 ch = CHILD(n, i);
3924 REQ(ch, stmt);
3925 num = num_stmts(ch);
3926 if (num == 1) {
3927 /* small_stmt or compound_stmt with only one child */
3928 s = ast_for_stmt(c, ch);
3929 if (!s)
3930 return NULL;
3931 asdl_seq_SET(seq, pos++, s);
3932 }
3933 else {
3934 int j;
3935 ch = CHILD(ch, 0);
3936 REQ(ch, simple_stmt);
3937 for (j = 0; j < NCH(ch); j += 2) {
3938 /* statement terminates with a semi-colon ';' */
3939 if (NCH(CHILD(ch, j)) == 0) {
3940 assert((j + 1) == NCH(ch));
3941 break;
3942 }
3943 s = ast_for_stmt(c, CHILD(ch, j));
3944 if (!s)
3945 return NULL;
3946 asdl_seq_SET(seq, pos++, s);
3947 }
3948 }
3949 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 }
3951 assert(pos == seq->size);
3952 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953}
3954
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003955static void
3956get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3957{
Pablo Galindo46a97922019-02-19 22:51:53 +00003958 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003959 // There must be no empty suites.
3960 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003961 stmt_ty last = asdl_seq_GET(s, tot - 1);
3962 *end_lineno = last->end_lineno;
3963 *end_col_offset = last->end_col_offset;
3964}
3965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966static stmt_ty
3967ast_for_if_stmt(struct compiling *c, const node *n)
3968{
3969 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3970 ['else' ':' suite]
3971 */
3972 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003973 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974
3975 REQ(n, if_stmt);
3976
3977 if (NCH(n) == 4) {
3978 expr_ty expression;
3979 asdl_seq *suite_seq;
3980
3981 expression = ast_for_expr(c, CHILD(n, 1));
3982 if (!expression)
3983 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003985 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003987 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988
Guido van Rossumd8faa362007-04-27 19:54:29 +00003989 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003990 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 s = STR(CHILD(n, 4));
3994 /* s[2], the third character in the string, will be
3995 's' for el_s_e, or
3996 'i' for el_i_f
3997 */
3998 if (s[2] == 's') {
3999 expr_ty expression;
4000 asdl_seq *seq1, *seq2;
4001
4002 expression = ast_for_expr(c, CHILD(n, 1));
4003 if (!expression)
4004 return NULL;
4005 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004006 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 return NULL;
4008 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004009 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004011 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012
Guido van Rossumd8faa362007-04-27 19:54:29 +00004013 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004014 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 }
4016 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004017 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004018 expr_ty expression;
4019 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004020 asdl_seq *orelse = NULL;
4021 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 /* must reference the child n_elif+1 since 'else' token is third,
4023 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004024 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
4025 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
4026 has_else = 1;
4027 n_elif -= 3;
4028 }
4029 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030
Thomas Wouters89f507f2006-12-13 04:49:30 +00004031 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004032 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004034 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004035 if (!orelse)
4036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004038 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004040 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
4041 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004043 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4044 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004046 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 asdl_seq_SET(orelse, 0,
4049 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004050 LINENO(CHILD(n, NCH(n) - 6)),
4051 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004052 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004053 /* the just-created orelse handled the last elif */
4054 n_elif--;
4055 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056
Thomas Wouters89f507f2006-12-13 04:49:30 +00004057 for (i = 0; i < n_elif; i++) {
4058 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004059 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004060 if (!newobj)
4061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004063 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004066 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004069 if (orelse != NULL) {
4070 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4071 } else {
4072 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4073 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004074 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004076 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004077 CHILD(n, off)->n_col_offset,
4078 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004079 orelse = newobj;
4080 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 expression = ast_for_expr(c, CHILD(n, 1));
4082 if (!expression)
4083 return NULL;
4084 suite_seq = ast_for_suite(c, CHILD(n, 3));
4085 if (!suite_seq)
4086 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004087 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004088 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004089 LINENO(n), n->n_col_offset,
4090 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004092
4093 PyErr_Format(PyExc_SystemError,
4094 "unexpected token in 'if' statement: %s", s);
4095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096}
4097
4098static stmt_ty
4099ast_for_while_stmt(struct compiling *c, const node *n)
4100{
4101 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4102 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004103 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104
4105 if (NCH(n) == 4) {
4106 expr_ty expression;
4107 asdl_seq *suite_seq;
4108
4109 expression = ast_for_expr(c, CHILD(n, 1));
4110 if (!expression)
4111 return NULL;
4112 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004113 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004115 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4116 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4117 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 }
4119 else if (NCH(n) == 7) {
4120 expr_ty expression;
4121 asdl_seq *seq1, *seq2;
4122
4123 expression = ast_for_expr(c, CHILD(n, 1));
4124 if (!expression)
4125 return NULL;
4126 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004127 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 return NULL;
4129 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004130 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004132 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004134 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4135 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004137
4138 PyErr_Format(PyExc_SystemError,
4139 "wrong number of tokens for 'while' statement: %d",
4140 NCH(n));
4141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142}
4143
4144static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004145ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146{
guoci90fc8982018-09-11 17:45:45 -04004147 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004148 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004149 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004150 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004151 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004152 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004153 int has_type_comment;
4154 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004155
4156 if (is_async && c->c_feature_version < 5) {
4157 ast_error(c, n,
4158 "Async for loops are only supported in Python 3.5 and greater");
4159 return NULL;
4160 }
4161
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004162 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163 REQ(n, for_stmt);
4164
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004165 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4166
4167 if (NCH(n) == 9 + has_type_comment) {
4168 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004169 if (!seq)
4170 return NULL;
4171 }
4172
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004173 node_target = CHILD(n, 1);
4174 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004175 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004176 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004177 /* Check the # of children rather than the length of _target, since
4178 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004179 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004180 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004181 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004182 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004183 target = Tuple(_target, Store, first->lineno, first->col_offset,
4184 node_target->n_end_lineno, node_target->n_end_col_offset,
4185 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004187 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004188 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004190 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004191 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 return NULL;
4193
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004194 if (seq != NULL) {
4195 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4196 } else {
4197 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4198 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004199
4200 if (has_type_comment) {
4201 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4202 if (!type_comment)
4203 return NULL;
4204 }
4205 else
4206 type_comment = NULL;
4207
Yury Selivanov75445082015-05-11 22:57:16 -04004208 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004209 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004210 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004211 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004212 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004213 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004214 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004215 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004216}
4217
4218static excepthandler_ty
4219ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4220{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004221 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004222 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004223 REQ(exc, except_clause);
4224 REQ(body, suite);
4225
4226 if (NCH(exc) == 1) {
4227 asdl_seq *suite_seq = ast_for_suite(c, body);
4228 if (!suite_seq)
4229 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004230 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004231
Neal Norwitzad74aa82008-03-31 05:14:30 +00004232 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004233 exc->n_col_offset,
4234 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004235 }
4236 else if (NCH(exc) == 2) {
4237 expr_ty expression;
4238 asdl_seq *suite_seq;
4239
4240 expression = ast_for_expr(c, CHILD(exc, 1));
4241 if (!expression)
4242 return NULL;
4243 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004244 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004245 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004246 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004247
Neal Norwitzad74aa82008-03-31 05:14:30 +00004248 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004249 exc->n_col_offset,
4250 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004251 }
4252 else if (NCH(exc) == 4) {
4253 asdl_seq *suite_seq;
4254 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004255 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004256 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004257 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004258 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004259 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004260 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004261 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004262 return NULL;
4263 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004264 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004265 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004266 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004267
Neal Norwitzad74aa82008-03-31 05:14:30 +00004268 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004269 exc->n_col_offset,
4270 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004271 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004272
4273 PyErr_Format(PyExc_SystemError,
4274 "wrong number of children for 'except' clause: %d",
4275 NCH(exc));
4276 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004277}
4278
4279static stmt_ty
4280ast_for_try_stmt(struct compiling *c, const node *n)
4281{
Neal Norwitzf599f422005-12-17 21:33:47 +00004282 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004283 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004284 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004285 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004286
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004287 REQ(n, try_stmt);
4288
Neal Norwitzf599f422005-12-17 21:33:47 +00004289 body = ast_for_suite(c, CHILD(n, 2));
4290 if (body == NULL)
4291 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004292
Neal Norwitzf599f422005-12-17 21:33:47 +00004293 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4294 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4295 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4296 /* we can assume it's an "else",
4297 because nch >= 9 for try-else-finally and
4298 it would otherwise have a type of except_clause */
4299 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4300 if (orelse == NULL)
4301 return NULL;
4302 n_except--;
4303 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004304
Neal Norwitzf599f422005-12-17 21:33:47 +00004305 finally = ast_for_suite(c, CHILD(n, nch - 1));
4306 if (finally == NULL)
4307 return NULL;
4308 n_except--;
4309 }
4310 else {
4311 /* we can assume it's an "else",
4312 otherwise it would have a type of except_clause */
4313 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4314 if (orelse == NULL)
4315 return NULL;
4316 n_except--;
4317 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004318 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004319 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004320 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004321 return NULL;
4322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323
Neal Norwitzf599f422005-12-17 21:33:47 +00004324 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004325 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004326 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004327 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004328 if (handlers == NULL)
4329 return NULL;
4330
4331 for (i = 0; i < n_except; i++) {
4332 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4333 CHILD(n, 5 + i * 3));
4334 if (!e)
4335 return NULL;
4336 asdl_seq_SET(handlers, i, e);
4337 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004338 }
4339
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004340 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004341 if (finally != NULL) {
4342 // finally is always last
4343 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4344 } else if (orelse != NULL) {
4345 // otherwise else is last
4346 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4347 } else {
4348 // inline the get_last_end_pos logic due to layout mismatch
4349 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4350 end_lineno = last_handler->end_lineno;
4351 end_col_offset = last_handler->end_col_offset;
4352 }
4353 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4354 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004355}
4356
Georg Brandl0c315622009-05-25 21:10:36 +00004357/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004358static withitem_ty
4359ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004360{
4361 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004362
Georg Brandl0c315622009-05-25 21:10:36 +00004363 REQ(n, with_item);
4364 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004365 if (!context_expr)
4366 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004367 if (NCH(n) == 3) {
4368 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004369
4370 if (!optional_vars) {
4371 return NULL;
4372 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004373 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004374 return NULL;
4375 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004376 }
4377
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004378 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004379}
4380
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004381/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004382static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004383ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004384{
guoci90fc8982018-09-11 17:45:45 -04004385 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004386 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004387 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004388 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004389
Guido van Rossum495da292019-03-07 12:38:08 -08004390 if (is_async && c->c_feature_version < 5) {
4391 ast_error(c, n,
4392 "Async with statements are only supported in Python 3.5 and greater");
4393 return NULL;
4394 }
4395
Georg Brandl0c315622009-05-25 21:10:36 +00004396 REQ(n, with_stmt);
4397
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004398 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4399 nch_minus_type = NCH(n) - has_type_comment;
4400
4401 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004402 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004403 if (!items)
4404 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004405 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004406 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4407 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004408 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004409 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004410 }
4411
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004412 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4413 if (!body)
4414 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004415 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004416
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004417 if (has_type_comment) {
4418 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4419 if (!type_comment)
4420 return NULL;
4421 }
4422 else
4423 type_comment = NULL;
4424
Yury Selivanov75445082015-05-11 22:57:16 -04004425 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004426 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004427 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004428 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004429 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004430 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004431}
4432
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004434ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004435{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004436 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004437 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004438 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004439 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004440 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004442 REQ(n, classdef);
4443
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004444 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004445 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004446 if (!s)
4447 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004448 get_last_end_pos(s, &end_lineno, &end_col_offset);
4449
Benjamin Peterson30760062008-11-25 04:02:28 +00004450 classname = NEW_IDENTIFIER(CHILD(n, 1));
4451 if (!classname)
4452 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004453 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004454 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004455 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004456 LINENO(n), n->n_col_offset,
4457 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004458 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004459
4460 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004461 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004462 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004463 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004464 get_last_end_pos(s, &end_lineno, &end_col_offset);
4465
Benjamin Peterson30760062008-11-25 04:02:28 +00004466 classname = NEW_IDENTIFIER(CHILD(n, 1));
4467 if (!classname)
4468 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004469 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004470 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004471 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004472 LINENO(n), n->n_col_offset,
4473 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004474 }
4475
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004476 /* class NAME '(' arglist ')' ':' suite */
4477 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004478 {
4479 PyObject *dummy_name;
4480 expr_ty dummy;
4481 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4482 if (!dummy_name)
4483 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004484 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4485 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4486 c->c_arena);
4487 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004488 if (!call)
4489 return NULL;
4490 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004491 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004492 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004493 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004494 get_last_end_pos(s, &end_lineno, &end_col_offset);
4495
Benjamin Peterson30760062008-11-25 04:02:28 +00004496 classname = NEW_IDENTIFIER(CHILD(n, 1));
4497 if (!classname)
4498 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004499 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004500 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004501
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004502 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004503 decorator_seq, LINENO(n), n->n_col_offset,
4504 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004505}
4506
4507static stmt_ty
4508ast_for_stmt(struct compiling *c, const node *n)
4509{
4510 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004511 assert(NCH(n) == 1);
4512 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004513 }
4514 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004515 assert(num_stmts(n) == 1);
4516 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004517 }
4518 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004519 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004520 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4521 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004522 */
4523 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004524 case expr_stmt:
4525 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004526 case del_stmt:
4527 return ast_for_del_stmt(c, n);
4528 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004529 return Pass(LINENO(n), n->n_col_offset,
4530 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531 case flow_stmt:
4532 return ast_for_flow_stmt(c, n);
4533 case import_stmt:
4534 return ast_for_import_stmt(c, n);
4535 case global_stmt:
4536 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004537 case nonlocal_stmt:
4538 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004539 case assert_stmt:
4540 return ast_for_assert_stmt(c, n);
4541 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004542 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004543 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4544 TYPE(n), NCH(n));
4545 return NULL;
4546 }
4547 }
4548 else {
4549 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004550 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004551 */
4552 node *ch = CHILD(n, 0);
4553 REQ(n, compound_stmt);
4554 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004555 case if_stmt:
4556 return ast_for_if_stmt(c, ch);
4557 case while_stmt:
4558 return ast_for_while_stmt(c, ch);
4559 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004560 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004561 case try_stmt:
4562 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004563 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004564 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004565 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004566 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004567 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004568 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 case decorated:
4570 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004571 case async_stmt:
4572 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004573 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004574 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004575 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004576 TYPE(n), NCH(n));
4577 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004578 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004579 }
4580}
4581
4582static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004583parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004584{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004585 const char *end;
4586 long x;
4587 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004588 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004589 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004590
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004591 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004592 errno = 0;
4593 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004594 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004595 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004596 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004597 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004598 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004599 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004600 }
4601 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004602 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004603 if (*end == '\0') {
4604 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004605 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004606 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004607 }
4608 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004609 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004610 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004611 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4612 if (compl.imag == -1.0 && PyErr_Occurred())
4613 return NULL;
4614 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004615 }
4616 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004617 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004618 dx = PyOS_string_to_double(s, NULL, NULL);
4619 if (dx == -1.0 && PyErr_Occurred())
4620 return NULL;
4621 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004623}
4624
4625static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004626parsenumber(struct compiling *c, const char *s)
4627{
4628 char *dup, *end;
4629 PyObject *res = NULL;
4630
4631 assert(s != NULL);
4632
4633 if (strchr(s, '_') == NULL) {
4634 return parsenumber_raw(c, s);
4635 }
4636 /* Create a duplicate without underscores. */
4637 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004638 if (dup == NULL) {
4639 return PyErr_NoMemory();
4640 }
Brett Cannona721aba2016-09-09 14:57:09 -07004641 end = dup;
4642 for (; *s; s++) {
4643 if (*s != '_') {
4644 *end++ = *s;
4645 }
4646 }
4647 *end = '\0';
4648 res = parsenumber_raw(c, dup);
4649 PyMem_Free(dup);
4650 return res;
4651}
4652
4653static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004654decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004655{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004656 const char *s, *t;
4657 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004658 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4659 while (s < end && (*s & 0x80)) s++;
4660 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004661 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004662}
4663
Eric V. Smith56466482016-10-31 14:46:26 -04004664static int
4665warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004666 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004667{
4668 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4669 first_invalid_escape_char);
4670 if (msg == NULL) {
4671 return -1;
4672 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004673 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004674 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004675 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004676 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004677 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4678 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004679 to get a more accurate error report */
4680 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004681 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004682 }
4683 Py_DECREF(msg);
4684 return -1;
4685 }
4686 Py_DECREF(msg);
4687 return 0;
4688}
4689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004690static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004691decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4692 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004693{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004694 PyObject *v, *u;
4695 char *buf;
4696 char *p;
4697 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004698
Benjamin Peterson202803a2016-02-25 22:34:45 -08004699 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004700 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004701 return NULL;
4702 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4703 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4704 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4705 if (u == NULL)
4706 return NULL;
4707 p = buf = PyBytes_AsString(u);
4708 end = s + len;
4709 while (s < end) {
4710 if (*s == '\\') {
4711 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004712 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004713 strcpy(p, "u005c");
4714 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004715 if (s >= end)
4716 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004717 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004718 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004719 if (*s & 0x80) { /* XXX inefficient */
4720 PyObject *w;
4721 int kind;
4722 void *data;
4723 Py_ssize_t len, i;
4724 w = decode_utf8(c, &s, end);
4725 if (w == NULL) {
4726 Py_DECREF(u);
4727 return NULL;
4728 }
4729 kind = PyUnicode_KIND(w);
4730 data = PyUnicode_DATA(w);
4731 len = PyUnicode_GET_LENGTH(w);
4732 for (i = 0; i < len; i++) {
4733 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4734 sprintf(p, "\\U%08x", chr);
4735 p += 10;
4736 }
4737 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004738 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004739 Py_DECREF(w);
4740 } else {
4741 *p++ = *s++;
4742 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004743 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004744 len = p - buf;
4745 s = buf;
4746
Eric V. Smith56466482016-10-31 14:46:26 -04004747 const char *first_invalid_escape;
4748 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4749
4750 if (v != NULL && first_invalid_escape != NULL) {
4751 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4752 /* We have not decref u before because first_invalid_escape points
4753 inside u. */
4754 Py_XDECREF(u);
4755 Py_DECREF(v);
4756 return NULL;
4757 }
4758 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004759 Py_XDECREF(u);
4760 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004761}
4762
Eric V. Smith56466482016-10-31 14:46:26 -04004763static PyObject *
4764decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4765 size_t len)
4766{
4767 const char *first_invalid_escape;
4768 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4769 &first_invalid_escape);
4770 if (result == NULL)
4771 return NULL;
4772
4773 if (first_invalid_escape != NULL) {
4774 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4775 Py_DECREF(result);
4776 return NULL;
4777 }
4778 }
4779 return result;
4780}
4781
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004782/* Shift locations for the given node and all its children by adding `lineno`
4783 and `col_offset` to existing locations. */
4784static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4785{
4786 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004787 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004788 for (int i = 0; i < NCH(n); ++i) {
4789 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4790 /* Shifting column offsets unnecessary if there's been newlines. */
4791 col_offset = 0;
4792 }
4793 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4794 }
4795 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004796 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004797}
4798
4799/* Fix locations for the given node and its children.
4800
4801 `parent` is the enclosing node.
4802 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004803 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004804*/
4805static void
4806fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4807{
4808 char *substr = NULL;
4809 char *start;
4810 int lines = LINENO(parent) - 1;
4811 int cols = parent->n_col_offset;
4812 /* Find the full fstring to fix location information in `n`. */
4813 while (parent && parent->n_type != STRING)
4814 parent = parent->n_child;
4815 if (parent && parent->n_str) {
4816 substr = strstr(parent->n_str, expr_str);
4817 if (substr) {
4818 start = substr;
4819 while (start > parent->n_str) {
4820 if (start[0] == '\n')
4821 break;
4822 start--;
4823 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004824 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004825 /* adjust the start based on the number of newlines encountered
4826 before the f-string expression */
4827 for (char* p = parent->n_str; p < substr; p++) {
4828 if (*p == '\n') {
4829 lines++;
4830 }
4831 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004832 }
4833 }
4834 fstring_shift_node_locations(n, lines, cols);
4835}
4836
Eric V. Smith451d0e32016-09-09 21:56:20 -04004837/* Compile this expression in to an expr_ty. Add parens around the
4838 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004839static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004840fstring_compile_expr(const char *expr_start, const char *expr_end,
4841 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004842
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004844 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004845 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004846 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004847 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004848 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004849
Eric V. Smith1d44c412015-09-23 07:49:00 -04004850 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004851 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004852 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4853 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004854
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004855 /* If the substring is all whitespace, it's an error. We need to catch this
4856 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4857 because turning the expression '' in to '()' would go from being invalid
4858 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004859 for (s = expr_start; s != expr_end; s++) {
4860 char c = *s;
4861 /* The Python parser ignores only the following whitespace
4862 characters (\r already is converted to \n). */
4863 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864 break;
4865 }
4866 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004867 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004868 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004869 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004870 }
4871
Eric V. Smith451d0e32016-09-09 21:56:20 -04004872 len = expr_end - expr_start;
4873 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4874 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004875 if (str == NULL) {
4876 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004877 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004878 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879
Eric V. Smith451d0e32016-09-09 21:56:20 -04004880 str[0] = '(';
4881 memcpy(str+1, expr_start, len);
4882 str[len+1] = ')';
4883 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004884
Victor Stinner37d66d72019-06-13 02:16:41 +02004885 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004886 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004887 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4888 Py_eval_input, 0);
4889 if (!mod_n) {
4890 PyMem_RawFree(str);
4891 return NULL;
4892 }
4893 /* Reuse str to find the correct column offset. */
4894 str[0] = '{';
4895 str[len+1] = '}';
4896 fstring_fix_node_location(n, mod_n, str);
4897 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004898 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004899 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004901 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004902 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004903}
4904
4905/* Return -1 on error.
4906
4907 Return 0 if we reached the end of the literal.
4908
4909 Return 1 if we haven't reached the end of the literal, but we want
4910 the caller to process the literal up to this point. Used for
4911 doubled braces.
4912*/
4913static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004914fstring_find_literal(const char **str, const char *end, int raw,
4915 PyObject **literal, int recurse_lvl,
4916 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004918 /* Get any literal string. It ends when we hit an un-doubled left
4919 brace (which isn't part of a unicode name escape such as
4920 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004922 const char *s = *str;
4923 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 int result = 0;
4925
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004927 while (s < end) {
4928 char ch = *s++;
4929 if (!raw && ch == '\\' && s < end) {
4930 ch = *s++;
4931 if (ch == 'N') {
4932 if (s < end && *s++ == '{') {
4933 while (s < end && *s++ != '}') {
4934 }
4935 continue;
4936 }
4937 break;
4938 }
4939 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4940 return -1;
4941 }
4942 }
4943 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 /* Check for doubled braces, but only at the top level. If
4945 we checked at every level, then f'{0:{3}}' would fail
4946 with the two closing braces. */
4947 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004948 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004949 /* We're going to tell the caller that the literal ends
4950 here, but that they should continue scanning. But also
4951 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004952 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004953 result = 1;
4954 goto done;
4955 }
4956
4957 /* Where a single '{' is the start of a new expression, a
4958 single '}' is not allowed. */
4959 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004960 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004961 ast_error(c, n, "f-string: single '}' is not allowed");
4962 return -1;
4963 }
4964 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965 /* We're either at a '{', which means we're starting another
4966 expression; or a '}', which means we're at the end of this
4967 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004968 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004969 break;
4970 }
4971 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004972 *str = s;
4973 assert(s <= end);
4974 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004975done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004976 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004977 if (raw)
4978 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004979 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004980 NULL, NULL);
4981 else
Eric V. Smith56466482016-10-31 14:46:26 -04004982 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004983 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 if (!*literal)
4985 return -1;
4986 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 return result;
4988}
4989
4990/* Forward declaration because parsing is recursive. */
4991static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 struct compiling *c, const node *n);
4994
Eric V. Smith451d0e32016-09-09 21:56:20 -04004995/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004996 expression (so it must be a '{'). Returns the FormattedValue node, which
4997 includes the expression, conversion character, format_spec expression, and
4998 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999
5000 Note that I don't do a perfect job here: I don't make sure that a
5001 closing brace doesn't match an opening paren, for example. It
5002 doesn't need to error on all invalid expressions, just correctly
5003 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005004 will be caught when we parse it later.
5005
5006 *expression is set to the expression. For an '=' "debug" expression,
5007 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005008 including the '=' and any whitespace around it, as a string object). If
5009 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005010static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005011fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005012 PyObject **expr_text, expr_ty *expression,
5013 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014{
5015 /* Return -1 on error, else 0. */
5016
Eric V. Smith451d0e32016-09-09 21:56:20 -04005017 const char *expr_start;
5018 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019 expr_ty simple_expression;
5020 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005021 int conversion = -1; /* The conversion char. Use default if not
5022 specified, or !r if using = and no format
5023 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024
5025 /* 0 if we're not in a string, else the quote char we're trying to
5026 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005027 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005028
5029 /* If we're inside a string, 1=normal, 3=triple-quoted. */
5030 int string_type = 0;
5031
5032 /* Keep track of nesting level for braces/parens/brackets in
5033 expressions. */
5034 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005035 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04005036
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005037 *expr_text = NULL;
5038
Eric V. Smith235a6f02015-09-19 14:51:32 -04005039 /* Can only nest one level deep. */
5040 if (recurse_lvl >= 2) {
5041 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005042 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005043 }
5044
5045 /* The first char must be a left brace, or we wouldn't have gotten
5046 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005047 assert(**str == '{');
5048 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 expr_start = *str;
5051 for (; *str < end; (*str)++) {
5052 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005053
5054 /* Loop invariants. */
5055 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057 if (quote_char)
5058 assert(string_type == 1 || string_type == 3);
5059 else
5060 assert(string_type == 0);
5061
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062 ch = **str;
5063 /* Nowhere inside an expression is a backslash allowed. */
5064 if (ch == '\\') {
5065 /* Error: can't include a backslash character, inside
5066 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005067 ast_error(c, n,
5068 "f-string expression part "
5069 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005070 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005071 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005072 if (quote_char) {
5073 /* We're inside a string. See if we're at the end. */
5074 /* This code needs to implement the same non-error logic
5075 as tok_get from tokenizer.c, at the letter_quote
5076 label. To actually share that code would be a
5077 nightmare. But, it's unlikely to change and is small,
5078 so duplicate it here. Note we don't need to catch all
5079 of the errors, since they'll be caught when parsing the
5080 expression. We just need to match the non-error
5081 cases. Thus we can ignore \n in single-quoted strings,
5082 for example. Or non-terminated strings. */
5083 if (ch == quote_char) {
5084 /* Does this match the string_type (single or triple
5085 quoted)? */
5086 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005087 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005088 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005090 string_type = 0;
5091 quote_char = 0;
5092 continue;
5093 }
5094 } else {
5095 /* We're at the end of a normal string. */
5096 quote_char = 0;
5097 string_type = 0;
5098 continue;
5099 }
5100 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 } else if (ch == '\'' || ch == '"') {
5102 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106 } else {
5107 /* Start of a normal string. */
5108 string_type = 1;
5109 }
5110 /* Start looking for the end of the string. */
5111 quote_char = ch;
5112 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005113 if (nested_depth >= MAXLEVEL) {
5114 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005115 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005116 }
5117 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 } else if (ch == '#') {
5120 /* Error: can't include a comment character, inside parens
5121 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005122 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005123 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005125 (ch == '!' || ch == ':' || ch == '}' ||
5126 ch == '=' || ch == '>' || ch == '<')) {
5127 /* See if there's a next character. */
5128 if (*str+1 < end) {
5129 char next = *(*str+1);
5130
5131 /* For "!=". since '=' is not an allowed conversion character,
5132 nothing is lost in this test. */
5133 if ((ch == '!' && next == '=') || /* != */
5134 (ch == '=' && next == '=') || /* == */
5135 (ch == '<' && next == '=') || /* <= */
5136 (ch == '>' && next == '=') /* >= */
5137 ) {
5138 *str += 1;
5139 continue;
5140 }
5141 /* Don't get out of the loop for these, if they're single
5142 chars (not part of 2-char tokens). If by themselves, they
5143 don't end an expression (unlike say '!'). */
5144 if (ch == '>' || ch == '<') {
5145 continue;
5146 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005147 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005148
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149 /* Normal way out of this loop. */
5150 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005151 } else if (ch == ']' || ch == '}' || ch == ')') {
5152 if (!nested_depth) {
5153 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005154 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005155 }
5156 nested_depth--;
5157 int opening = parenstack[nested_depth];
5158 if (!((opening == '(' && ch == ')') ||
5159 (opening == '[' && ch == ']') ||
5160 (opening == '{' && ch == '}')))
5161 {
5162 ast_error(c, n,
5163 "f-string: closing parenthesis '%c' "
5164 "does not match opening parenthesis '%c'",
5165 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005166 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005167 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005168 } else {
5169 /* Just consume this char and loop around. */
5170 }
5171 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005172 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005173 /* If we leave this loop in a string or with mismatched parens, we
5174 don't care. We'll get a syntax error when compiling the
5175 expression. But, we can produce a better error message, so
5176 let's just do that.*/
5177 if (quote_char) {
5178 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005179 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005180 }
5181 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005182 int opening = parenstack[nested_depth - 1];
5183 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005184 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 }
5186
Eric V. Smith451d0e32016-09-09 21:56:20 -04005187 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005188 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005189
5190 /* Compile the expression as soon as possible, so we show errors
5191 related to the expression before errors related to the
5192 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005193 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005194 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005195 goto error;
5196
5197 /* Check for =, which puts the text value of the expression in
5198 expr_text. */
5199 if (**str == '=') {
5200 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005201
5202 /* Skip over ASCII whitespace. No need to test for end of string
5203 here, since we know there's at least a trailing quote somewhere
5204 ahead. */
5205 while (Py_ISSPACE(**str)) {
5206 *str += 1;
5207 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005208
5209 /* Set *expr_text to the text of the expression. */
5210 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5211 if (!*expr_text) {
5212 goto error;
5213 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005214 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005215
5216 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005217 if (**str == '!') {
5218 *str += 1;
5219 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005220 goto unexpected_end_of_string;
5221
Eric V. Smith451d0e32016-09-09 21:56:20 -04005222 conversion = **str;
5223 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005224
5225 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005226 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005227 ast_error(c, n,
5228 "f-string: invalid conversion character: "
5229 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005230 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005231 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005232
5233 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005234
5235 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005237 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005238 if (**str == ':') {
5239 *str += 1;
5240 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005241 goto unexpected_end_of_string;
5242
5243 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005244 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005245 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005246 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005247 }
5248
Eric V. Smith451d0e32016-09-09 21:56:20 -04005249 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005250 goto unexpected_end_of_string;
5251
5252 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005253 assert(*str < end);
5254 assert(**str == '}');
5255 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005256
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005257 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005258 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005259 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005260 conversion = 'r';
5261 }
5262
Eric V. Smith451d0e32016-09-09 21:56:20 -04005263 /* And now create the FormattedValue node that represents this
5264 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005265 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005266 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005267 n->n_col_offset, n->n_end_lineno,
5268 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005269 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005270 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005271
5272 return 0;
5273
5274unexpected_end_of_string:
5275 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005276 /* Falls through to error. */
5277
5278error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005279 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005280 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005281
Eric V. Smith235a6f02015-09-19 14:51:32 -04005282}
5283
5284/* Return -1 on error.
5285
5286 Return 0 if we have a literal (possible zero length) and an
5287 expression (zero length if at the end of the string.
5288
5289 Return 1 if we have a literal, but no expression, and we want the
5290 caller to call us again. This is used to deal with doubled
5291 braces.
5292
5293 When called multiple times on the string 'a{{b{0}c', this function
5294 will return:
5295
5296 1. the literal 'a{' with no expression, and a return value
5297 of 1. Despite the fact that there's no expression, the return
5298 value of 1 means we're not finished yet.
5299
5300 2. the literal 'b' and the expression '0', with a return value of
5301 0. The fact that there's an expression means we're not finished.
5302
5303 3. literal 'c' with no expression and a return value of 0. The
5304 combination of the return value of 0 with no expression means
5305 we're finished.
5306*/
5307static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005308fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5309 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005310 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005311 struct compiling *c, const node *n)
5312{
5313 int result;
5314
5315 assert(*literal == NULL && *expression == NULL);
5316
5317 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005318 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005319 if (result < 0)
5320 goto error;
5321
5322 assert(result == 0 || result == 1);
5323
5324 if (result == 1)
5325 /* We have a literal, but don't look at the expression. */
5326 return 1;
5327
Eric V. Smith451d0e32016-09-09 21:56:20 -04005328 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005329 /* We're at the end of the string or the end of a nested
5330 f-string: no expression. The top-level error case where we
5331 expect to be at the end of the string but we're at a '}' is
5332 handled later. */
5333 return 0;
5334
5335 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005336 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005337
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005338 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5339 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005340 goto error;
5341
5342 return 0;
5343
5344error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005345 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005346 return -1;
5347}
5348
5349#define EXPRLIST_N_CACHED 64
5350
5351typedef struct {
5352 /* Incrementally build an array of expr_ty, so be used in an
5353 asdl_seq. Cache some small but reasonably sized number of
5354 expr_ty's, and then after that start dynamically allocating,
5355 doubling the number allocated each time. Note that the f-string
5356 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005357 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005358 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005359
5360 Py_ssize_t allocated; /* Number we've allocated. */
5361 Py_ssize_t size; /* Number we've used. */
5362 expr_ty *p; /* Pointer to the memory we're actually
5363 using. Will point to 'data' until we
5364 start dynamically allocating. */
5365 expr_ty data[EXPRLIST_N_CACHED];
5366} ExprList;
5367
5368#ifdef NDEBUG
5369#define ExprList_check_invariants(l)
5370#else
5371static void
5372ExprList_check_invariants(ExprList *l)
5373{
5374 /* Check our invariants. Make sure this object is "live", and
5375 hasn't been deallocated. */
5376 assert(l->size >= 0);
5377 assert(l->p != NULL);
5378 if (l->size <= EXPRLIST_N_CACHED)
5379 assert(l->data == l->p);
5380}
5381#endif
5382
5383static void
5384ExprList_Init(ExprList *l)
5385{
5386 l->allocated = EXPRLIST_N_CACHED;
5387 l->size = 0;
5388
5389 /* Until we start allocating dynamically, p points to data. */
5390 l->p = l->data;
5391
5392 ExprList_check_invariants(l);
5393}
5394
5395static int
5396ExprList_Append(ExprList *l, expr_ty exp)
5397{
5398 ExprList_check_invariants(l);
5399 if (l->size >= l->allocated) {
5400 /* We need to alloc (or realloc) the memory. */
5401 Py_ssize_t new_size = l->allocated * 2;
5402
5403 /* See if we've ever allocated anything dynamically. */
5404 if (l->p == l->data) {
5405 Py_ssize_t i;
5406 /* We're still using the cached data. Switch to
5407 alloc-ing. */
5408 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5409 if (!l->p)
5410 return -1;
5411 /* Copy the cached data into the new buffer. */
5412 for (i = 0; i < l->size; i++)
5413 l->p[i] = l->data[i];
5414 } else {
5415 /* Just realloc. */
5416 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5417 if (!tmp) {
5418 PyMem_RawFree(l->p);
5419 l->p = NULL;
5420 return -1;
5421 }
5422 l->p = tmp;
5423 }
5424
5425 l->allocated = new_size;
5426 assert(l->allocated == 2 * l->size);
5427 }
5428
5429 l->p[l->size++] = exp;
5430
5431 ExprList_check_invariants(l);
5432 return 0;
5433}
5434
5435static void
5436ExprList_Dealloc(ExprList *l)
5437{
5438 ExprList_check_invariants(l);
5439
5440 /* If there's been an error, or we've never dynamically allocated,
5441 do nothing. */
5442 if (!l->p || l->p == l->data) {
5443 /* Do nothing. */
5444 } else {
5445 /* We have dynamically allocated. Free the memory. */
5446 PyMem_RawFree(l->p);
5447 }
5448 l->p = NULL;
5449 l->size = -1;
5450}
5451
5452static asdl_seq *
5453ExprList_Finish(ExprList *l, PyArena *arena)
5454{
5455 asdl_seq *seq;
5456
5457 ExprList_check_invariants(l);
5458
5459 /* Allocate the asdl_seq and copy the expressions in to it. */
5460 seq = _Py_asdl_seq_new(l->size, arena);
5461 if (seq) {
5462 Py_ssize_t i;
5463 for (i = 0; i < l->size; i++)
5464 asdl_seq_SET(seq, i, l->p[i]);
5465 }
5466 ExprList_Dealloc(l);
5467 return seq;
5468}
5469
5470/* The FstringParser is designed to add a mix of strings and
5471 f-strings, and concat them together as needed. Ultimately, it
5472 generates an expr_ty. */
5473typedef struct {
5474 PyObject *last_str;
5475 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005476 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005477} FstringParser;
5478
5479#ifdef NDEBUG
5480#define FstringParser_check_invariants(state)
5481#else
5482static void
5483FstringParser_check_invariants(FstringParser *state)
5484{
5485 if (state->last_str)
5486 assert(PyUnicode_CheckExact(state->last_str));
5487 ExprList_check_invariants(&state->expr_list);
5488}
5489#endif
5490
5491static void
5492FstringParser_Init(FstringParser *state)
5493{
5494 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005495 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005496 ExprList_Init(&state->expr_list);
5497 FstringParser_check_invariants(state);
5498}
5499
5500static void
5501FstringParser_Dealloc(FstringParser *state)
5502{
5503 FstringParser_check_invariants(state);
5504
5505 Py_XDECREF(state->last_str);
5506 ExprList_Dealloc(&state->expr_list);
5507}
5508
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005509/* Constants for the following */
5510static PyObject *u_kind;
5511
5512/* Compute 'kind' field for string Constant (either 'u' or None) */
5513static PyObject *
5514make_kind(struct compiling *c, const node *n)
5515{
5516 char *s = NULL;
5517 PyObject *kind = NULL;
5518
5519 /* Find the first string literal, if any */
5520 while (TYPE(n) != STRING) {
5521 if (NCH(n) == 0)
5522 return NULL;
5523 n = CHILD(n, 0);
5524 }
5525 REQ(n, STRING);
5526
5527 /* If it starts with 'u', return a PyUnicode "u" string */
5528 s = STR(n);
5529 if (s && *s == 'u') {
5530 if (!u_kind) {
5531 u_kind = PyUnicode_InternFromString("u");
5532 if (!u_kind)
5533 return NULL;
5534 }
5535 kind = u_kind;
5536 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5537 return NULL;
5538 }
5539 Py_INCREF(kind);
5540 }
5541 return kind;
5542}
5543
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005544/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005545static expr_ty
5546make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5547{
5548 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005549 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005550 *str = NULL;
5551 assert(PyUnicode_CheckExact(s));
5552 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5553 Py_DECREF(s);
5554 return NULL;
5555 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005556 kind = make_kind(c, n);
5557 if (kind == NULL && PyErr_Occurred())
5558 return NULL;
5559 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005560 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005561}
5562
5563/* Add a non-f-string (that is, a regular literal string). str is
5564 decref'd. */
5565static int
5566FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5567{
5568 FstringParser_check_invariants(state);
5569
5570 assert(PyUnicode_CheckExact(str));
5571
5572 if (PyUnicode_GET_LENGTH(str) == 0) {
5573 Py_DECREF(str);
5574 return 0;
5575 }
5576
5577 if (!state->last_str) {
5578 /* We didn't have a string before, so just remember this one. */
5579 state->last_str = str;
5580 } else {
5581 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005582 PyUnicode_AppendAndDel(&state->last_str, str);
5583 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005584 return -1;
5585 }
5586 FstringParser_check_invariants(state);
5587 return 0;
5588}
5589
Eric V. Smith451d0e32016-09-09 21:56:20 -04005590/* Parse an f-string. The f-string is in *str to end, with no
5591 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005592static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005593FstringParser_ConcatFstring(FstringParser *state, const char **str,
5594 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005595 struct compiling *c, const node *n)
5596{
5597 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005598 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599
5600 /* Parse the f-string. */
5601 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005602 PyObject *literal = NULL;
5603 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005604 expr_ty expression = NULL;
5605
5606 /* If there's a zero length literal in front of the
5607 expression, literal will be NULL. If we're at the end of
5608 the f-string, expression will be NULL (unless result == 1,
5609 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005610 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005611 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005612 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005613 if (result < 0)
5614 return -1;
5615
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005616 /* Add the literal, if any. */
5617 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5618 Py_XDECREF(expr_text);
5619 return -1;
5620 }
5621 /* Add the expr_text, if any. */
5622 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5623 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005624 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005625
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005626 /* We've dealt with the literal and expr_text, their ownership has
5627 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005628
5629 /* See if we should just loop around to get the next literal
5630 and expression, while ignoring the expression this
5631 time. This is used for un-doubling braces, as an
5632 optimization. */
5633 if (result == 1)
5634 continue;
5635
5636 if (!expression)
5637 /* We're done with this f-string. */
5638 break;
5639
5640 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005641 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005642 if (!state->last_str) {
5643 /* Do nothing. No previous literal. */
5644 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005645 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005646 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5647 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5648 return -1;
5649 }
5650
5651 if (ExprList_Append(&state->expr_list, expression) < 0)
5652 return -1;
5653 }
5654
Eric V. Smith235a6f02015-09-19 14:51:32 -04005655 /* If recurse_lvl is zero, then we must be at the end of the
5656 string. Otherwise, we must be at a right brace. */
5657
Eric V. Smith451d0e32016-09-09 21:56:20 -04005658 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005659 ast_error(c, n, "f-string: unexpected end of string");
5660 return -1;
5661 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005662 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005663 ast_error(c, n, "f-string: expecting '}'");
5664 return -1;
5665 }
5666
5667 FstringParser_check_invariants(state);
5668 return 0;
5669}
5670
5671/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005672 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673static expr_ty
5674FstringParser_Finish(FstringParser *state, struct compiling *c,
5675 const node *n)
5676{
5677 asdl_seq *seq;
5678
5679 FstringParser_check_invariants(state);
5680
5681 /* If we're just a constant string with no expressions, return
5682 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005683 if (!state->fmode) {
5684 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005685 if (!state->last_str) {
5686 /* Create a zero length string. */
5687 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5688 if (!state->last_str)
5689 goto error;
5690 }
5691 return make_str_node_and_del(&state->last_str, c, n);
5692 }
5693
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005694 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005695 last node in our expression list. */
5696 if (state->last_str) {
5697 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5698 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5699 goto error;
5700 }
5701 /* This has already been freed. */
5702 assert(state->last_str == NULL);
5703
5704 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5705 if (!seq)
5706 goto error;
5707
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005708 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5709 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005710
5711error:
5712 FstringParser_Dealloc(state);
5713 return NULL;
5714}
5715
Eric V. Smith451d0e32016-09-09 21:56:20 -04005716/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5717 at end, parse it into an expr_ty. Return NULL on error. Adjust
5718 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005719static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005720fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005721 struct compiling *c, const node *n)
5722{
5723 FstringParser state;
5724
5725 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005726 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005727 c, n) < 0) {
5728 FstringParser_Dealloc(&state);
5729 return NULL;
5730 }
5731
5732 return FstringParser_Finish(&state, c, n);
5733}
5734
5735/* n is a Python string literal, including the bracketing quote
5736 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005737 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005738 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005739 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5740 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005742static int
5743parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5744 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005745{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005746 size_t len;
5747 const char *s = STR(n);
5748 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005749 int fmode = 0;
5750 *bytesmode = 0;
5751 *rawmode = 0;
5752 *result = NULL;
5753 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005754 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005755 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005756 if (quote == 'b' || quote == 'B') {
5757 quote = *++s;
5758 *bytesmode = 1;
5759 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005760 else if (quote == 'u' || quote == 'U') {
5761 quote = *++s;
5762 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005763 else if (quote == 'r' || quote == 'R') {
5764 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005765 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005766 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005767 else if (quote == 'f' || quote == 'F') {
5768 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005769 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005770 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005771 else {
5772 break;
5773 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005774 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005775 }
Guido van Rossum495da292019-03-07 12:38:08 -08005776
5777 /* fstrings are only allowed in Python 3.6 and greater */
5778 if (fmode && c->c_feature_version < 6) {
5779 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5780 return -1;
5781 }
5782
Eric V. Smith451d0e32016-09-09 21:56:20 -04005783 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005784 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005785 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005786 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005787 if (quote != '\'' && quote != '\"') {
5788 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005789 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005790 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005791 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005792 s++;
5793 len = strlen(s);
5794 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005795 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005796 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005797 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005798 }
5799 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005800 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005801 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005802 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005803 }
5804 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005805 /* A triple quoted string. We've already skipped one quote at
5806 the start and one at the end of the string. Now skip the
5807 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005808 s += 2;
5809 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005810 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005811 if (s[--len] != quote || s[--len] != quote) {
5812 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005813 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005814 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005815 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005816
Eric V. Smith451d0e32016-09-09 21:56:20 -04005817 if (fmode) {
5818 /* Just return the bytes. The caller will parse the resulting
5819 string. */
5820 *fstr = s;
5821 *fstrlen = len;
5822 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005823 }
5824
Eric V. Smith451d0e32016-09-09 21:56:20 -04005825 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005826 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005827 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005828 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005829 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005830 const char *ch;
5831 for (ch = s; *ch; ch++) {
5832 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005833 ast_error(c, n,
5834 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005835 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005836 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005837 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005838 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005839 if (*rawmode)
5840 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005841 else
Eric V. Smith56466482016-10-31 14:46:26 -04005842 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005843 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005844 if (*rawmode)
5845 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005846 else
Eric V. Smith56466482016-10-31 14:46:26 -04005847 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005848 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005849 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005850}
5851
Eric V. Smith235a6f02015-09-19 14:51:32 -04005852/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5853 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005854 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005855 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005856 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005857 node if there's just an f-string (with no leading or trailing
5858 literals), or a JoinedStr node if there are multiple f-strings or
5859 any literals involved. */
5860static expr_ty
5861parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005862{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005863 int bytesmode = 0;
5864 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005865 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005866
5867 FstringParser state;
5868 FstringParser_Init(&state);
5869
5870 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005871 int this_bytesmode;
5872 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005873 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005874 const char *fstr;
5875 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005876
5877 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005878 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5879 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005880 goto error;
5881
5882 /* Check that we're not mixing bytes with unicode. */
5883 if (i != 0 && bytesmode != this_bytesmode) {
5884 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005885 /* s is NULL if the current string part is an f-string. */
5886 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005887 goto error;
5888 }
5889 bytesmode = this_bytesmode;
5890
Eric V. Smith451d0e32016-09-09 21:56:20 -04005891 if (fstr != NULL) {
5892 int result;
5893 assert(s == NULL && !bytesmode);
5894 /* This is an f-string. Parse and concatenate it. */
5895 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5896 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005897 if (result < 0)
5898 goto error;
5899 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005900 /* A string or byte string. */
5901 assert(s != NULL && fstr == NULL);
5902
Eric V. Smith451d0e32016-09-09 21:56:20 -04005903 assert(bytesmode ? PyBytes_CheckExact(s) :
5904 PyUnicode_CheckExact(s));
5905
Eric V. Smith451d0e32016-09-09 21:56:20 -04005906 if (bytesmode) {
5907 /* For bytes, concat as we go. */
5908 if (i == 0) {
5909 /* First time, just remember this value. */
5910 bytes_str = s;
5911 } else {
5912 PyBytes_ConcatAndDel(&bytes_str, s);
5913 if (!bytes_str)
5914 goto error;
5915 }
5916 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005917 /* This is a regular string. Concatenate it. */
5918 if (FstringParser_ConcatAndDel(&state, s) < 0)
5919 goto error;
5920 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005921 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005922 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005923 if (bytesmode) {
5924 /* Just return the bytes object and we're done. */
5925 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5926 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005927 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005928 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005930
Eric V. Smith235a6f02015-09-19 14:51:32 -04005931 /* We're not a bytes string, bytes_str should never have been set. */
5932 assert(bytes_str == NULL);
5933
5934 return FstringParser_Finish(&state, c, n);
5935
5936error:
5937 Py_XDECREF(bytes_str);
5938 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005940}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005941
5942PyObject *
5943_PyAST_GetDocString(asdl_seq *body)
5944{
5945 if (!asdl_seq_LEN(body)) {
5946 return NULL;
5947 }
5948 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5949 if (st->kind != Expr_kind) {
5950 return NULL;
5951 }
5952 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005953 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5954 return e->v.Constant.value;
5955 }
5956 return NULL;
5957}