blob: 4687f8178b0244b594eca566851f94af9874ad90 [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 }
126 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
127 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
128 return 0;
129 }
130 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
131 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
132 "kw_defaults on arguments");
133 return 0;
134 }
135 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
136}
137
138static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100139validate_constant(PyObject *value)
140{
141 if (value == Py_None || value == Py_Ellipsis)
142 return 1;
143
144 if (PyLong_CheckExact(value)
145 || PyFloat_CheckExact(value)
146 || PyComplex_CheckExact(value)
147 || PyBool_Check(value)
148 || PyUnicode_CheckExact(value)
149 || PyBytes_CheckExact(value))
150 return 1;
151
152 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
153 PyObject *it;
154
155 it = PyObject_GetIter(value);
156 if (it == NULL)
157 return 0;
158
159 while (1) {
160 PyObject *item = PyIter_Next(it);
161 if (item == NULL) {
162 if (PyErr_Occurred()) {
163 Py_DECREF(it);
164 return 0;
165 }
166 break;
167 }
168
169 if (!validate_constant(item)) {
170 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 return 0;
173 }
Victor Stinner726f6902016-01-27 00:11:47 +0100174 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100175 }
176
177 Py_DECREF(it);
178 return 1;
179 }
180
181 return 0;
182}
183
184static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500185validate_expr(expr_ty exp, expr_context_ty ctx)
186{
187 int check_ctx = 1;
188 expr_context_ty actual_ctx;
189
190 /* First check expression context. */
191 switch (exp->kind) {
192 case Attribute_kind:
193 actual_ctx = exp->v.Attribute.ctx;
194 break;
195 case Subscript_kind:
196 actual_ctx = exp->v.Subscript.ctx;
197 break;
198 case Starred_kind:
199 actual_ctx = exp->v.Starred.ctx;
200 break;
201 case Name_kind:
202 actual_ctx = exp->v.Name.ctx;
203 break;
204 case List_kind:
205 actual_ctx = exp->v.List.ctx;
206 break;
207 case Tuple_kind:
208 actual_ctx = exp->v.Tuple.ctx;
209 break;
210 default:
211 if (ctx != Load) {
212 PyErr_Format(PyExc_ValueError, "expression which can't be "
213 "assigned to in %s context", expr_context_name(ctx));
214 return 0;
215 }
216 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100217 /* set actual_ctx to prevent gcc warning */
218 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500219 }
220 if (check_ctx && actual_ctx != ctx) {
221 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
222 expr_context_name(ctx), expr_context_name(actual_ctx));
223 return 0;
224 }
225
226 /* Now validate expression. */
227 switch (exp->kind) {
228 case BoolOp_kind:
229 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
230 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
231 return 0;
232 }
233 return validate_exprs(exp->v.BoolOp.values, Load, 0);
234 case BinOp_kind:
235 return validate_expr(exp->v.BinOp.left, Load) &&
236 validate_expr(exp->v.BinOp.right, Load);
237 case UnaryOp_kind:
238 return validate_expr(exp->v.UnaryOp.operand, Load);
239 case Lambda_kind:
240 return validate_arguments(exp->v.Lambda.args) &&
241 validate_expr(exp->v.Lambda.body, Load);
242 case IfExp_kind:
243 return validate_expr(exp->v.IfExp.test, Load) &&
244 validate_expr(exp->v.IfExp.body, Load) &&
245 validate_expr(exp->v.IfExp.orelse, Load);
246 case Dict_kind:
247 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
248 PyErr_SetString(PyExc_ValueError,
249 "Dict doesn't have the same number of keys as values");
250 return 0;
251 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400252 /* null_ok=1 for keys expressions to allow dict unpacking to work in
253 dict literals, i.e. ``{**{a:b}}`` */
254 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
255 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500256 case Set_kind:
257 return validate_exprs(exp->v.Set.elts, Load, 0);
258#define COMP(NAME) \
259 case NAME ## _kind: \
260 return validate_comprehension(exp->v.NAME.generators) && \
261 validate_expr(exp->v.NAME.elt, Load);
262 COMP(ListComp)
263 COMP(SetComp)
264 COMP(GeneratorExp)
265#undef COMP
266 case DictComp_kind:
267 return validate_comprehension(exp->v.DictComp.generators) &&
268 validate_expr(exp->v.DictComp.key, Load) &&
269 validate_expr(exp->v.DictComp.value, Load);
270 case Yield_kind:
271 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500272 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000273 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400274 case Await_kind:
275 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500276 case Compare_kind:
277 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
278 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
279 return 0;
280 }
281 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
282 asdl_seq_LEN(exp->v.Compare.ops)) {
283 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
284 "of comparators and operands");
285 return 0;
286 }
287 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
288 validate_expr(exp->v.Compare.left, Load);
289 case Call_kind:
290 return validate_expr(exp->v.Call.func, Load) &&
291 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400292 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100293 case Constant_kind:
294 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100295 PyErr_Format(PyExc_TypeError,
296 "got an invalid type in Constant: %s",
297 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100298 return 0;
299 }
300 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400301 case JoinedStr_kind:
302 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
303 case FormattedValue_kind:
304 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
305 return 0;
306 if (exp->v.FormattedValue.format_spec)
307 return validate_expr(exp->v.FormattedValue.format_spec, Load);
308 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500309 case Attribute_kind:
310 return validate_expr(exp->v.Attribute.value, Load);
311 case Subscript_kind:
312 return validate_slice(exp->v.Subscript.slice) &&
313 validate_expr(exp->v.Subscript.value, Load);
314 case Starred_kind:
315 return validate_expr(exp->v.Starred.value, ctx);
316 case List_kind:
317 return validate_exprs(exp->v.List.elts, ctx, 0);
318 case Tuple_kind:
319 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000320 case NamedExpr_kind:
321 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300322 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500324 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000326 PyErr_SetString(PyExc_SystemError, "unexpected expression");
327 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800569 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570};
571
572static asdl_seq *seq_for_testlist(struct compiling *, const node *);
573static expr_ty ast_for_expr(struct compiling *, const node *);
574static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300575static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000576static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
577 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000578static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000579static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
guoci90fc8982018-09-11 17:45:45 -0400581static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
582static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200585static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000586 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000588static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400589static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000590static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Nick Coghlan650f0d02007-04-15 12:05:43 +0000592#define COMP_GENEXP 0
593#define COMP_LISTCOMP 1
594#define COMP_SETCOMP 2
595
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596static int
597init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000598{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500599 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
600 if (!m)
601 return 0;
602 c->c_normalize = PyObject_GetAttrString(m, "normalize");
603 Py_DECREF(m);
604 if (!c->c_normalize)
605 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500606 return 1;
607}
608
609static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400610new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500611{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400612 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000614 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500616 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000617 /* Check whether there are non-ASCII characters in the
618 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500619 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300621 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500622 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500623 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200624 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500625 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300626 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
627 if (form == NULL) {
628 Py_DECREF(id);
629 return NULL;
630 }
631 PyObject *args[2] = {form, id};
632 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500633 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200634 if (!id2)
635 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300636 if (!PyUnicode_Check(id2)) {
637 PyErr_Format(PyExc_TypeError,
638 "unicodedata.normalize() must return a string, not "
639 "%.200s",
640 Py_TYPE(id2)->tp_name);
641 Py_DECREF(id2);
642 return NULL;
643 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000645 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000646 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200647 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
648 Py_DECREF(id);
649 return NULL;
650 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000651 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652}
653
Benjamin Peterson55e00432012-01-16 17:22:31 -0500654#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200657ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400659 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200660 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200662 va_start(va, errmsg);
663 errstr = PyUnicode_FromFormatV(errmsg, va);
664 va_end(va);
665 if (!errstr) {
666 return 0;
667 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200668 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000670 Py_INCREF(Py_None);
671 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 }
Ammar Askar025eb982018-09-24 17:12:49 -0400673 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200674 if (!tmp) {
675 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400676 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000677 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 Py_DECREF(errstr);
680 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400681 if (value) {
682 PyErr_SetObject(PyExc_SyntaxError, value);
683 Py_DECREF(value);
684 }
685 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686}
687
688/* num_stmts() returns number of contained statements.
689
690 Use this routine to determine how big a sequence is needed for
691 the statements in a parse tree. Its raison d'etre is this bit of
692 grammar:
693
694 stmt: simple_stmt | compound_stmt
695 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
696
697 A simple_stmt can contain multiple small_stmt elements joined
698 by semicolons. If the arg is a simple_stmt, the number of
699 small_stmt elements is returned.
700*/
701
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800702static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800703new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800704{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800705 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800706 if (res == NULL)
707 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800708 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
709 Py_DECREF(res);
710 return NULL;
711 }
712 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800713}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800714#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800715
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716static int
717num_stmts(const node *n)
718{
719 int i, l;
720 node *ch;
721
722 switch (TYPE(n)) {
723 case single_input:
724 if (TYPE(CHILD(n, 0)) == NEWLINE)
725 return 0;
726 else
727 return num_stmts(CHILD(n, 0));
728 case file_input:
729 l = 0;
730 for (i = 0; i < NCH(n); i++) {
731 ch = CHILD(n, i);
732 if (TYPE(ch) == stmt)
733 l += num_stmts(ch);
734 }
735 return l;
736 case stmt:
737 return num_stmts(CHILD(n, 0));
738 case compound_stmt:
739 return 1;
740 case simple_stmt:
741 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
742 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800743 case func_body_suite:
744 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
745 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 if (NCH(n) == 1)
747 return num_stmts(CHILD(n, 0));
748 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800749 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800751 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
752 i += 2;
753 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 l += num_stmts(CHILD(n, i));
755 return l;
756 }
757 default: {
758 char buf[128];
759
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000760 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 TYPE(n), NCH(n));
762 Py_FatalError(buf);
763 }
764 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700765 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766}
767
768/* Transform the CST rooted at node * to the appropriate AST
769*/
770
771mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200772PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
773 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000775 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800777 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 stmt_ty s;
779 node *ch;
780 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800782 asdl_seq *argtypes = NULL;
783 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200786 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400787 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788 c.c_normalize = NULL;
Guido van Rossum495da292019-03-07 12:38:08 -0800789 c.c_feature_version = flags->cf_feature_version;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800790
791 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 switch (TYPE(n)) {
796 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200797 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500799 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 for (i = 0; i < NCH(n) - 1; i++) {
801 ch = CHILD(n, i);
802 if (TYPE(ch) == NEWLINE)
803 continue;
804 REQ(ch, stmt);
805 num = num_stmts(ch);
806 if (num == 1) {
807 s = ast_for_stmt(&c, ch);
808 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000810 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 else {
813 ch = CHILD(ch, 0);
814 REQ(ch, simple_stmt);
815 for (j = 0; j < num; j++) {
816 s = ast_for_stmt(&c, CHILD(ch, j * 2));
817 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000819 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 }
822 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800823
824 /* Type ignores are stored under the ENDMARKER in file_input. */
825 ch = CHILD(n, NCH(n) - 1);
826 REQ(ch, ENDMARKER);
827 num = NCH(ch);
828 type_ignores = _Py_asdl_seq_new(num, arena);
829 if (!type_ignores)
830 goto out;
831
832 for (i = 0; i < num; i++) {
833 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
834 if (!ti)
835 goto out;
836 asdl_seq_SET(type_ignores, i, ti);
837 }
838
839 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500840 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 case eval_input: {
842 expr_ty testlist_ast;
843
Nick Coghlan650f0d02007-04-15 12:05:43 +0000844 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000845 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 goto out;
848 res = Expression(testlist_ast, arena);
849 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
851 case single_input:
852 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200853 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000857 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000859 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500860 goto out;
861 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
863 else {
864 n = CHILD(n, 0);
865 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200866 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500868 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870 s = ast_for_stmt(&c, n);
871 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 asdl_seq_SET(stmts, 0, s);
874 }
875 else {
876 /* Only a simple_stmt can contain multiple statements. */
877 REQ(n, simple_stmt);
878 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 if (TYPE(CHILD(n, i)) == NEWLINE)
880 break;
881 s = ast_for_stmt(&c, CHILD(n, i));
882 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500883 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 asdl_seq_SET(stmts, i / 2, s);
885 }
886 }
887
Benjamin Peterson55e00432012-01-16 17:22:31 -0500888 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500890 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800891 case func_type_input:
892 n = CHILD(n, 0);
893 REQ(n, func_type);
894
895 if (TYPE(CHILD(n, 1)) == typelist) {
896 ch = CHILD(n, 1);
897 /* this is overly permissive -- we don't pay any attention to
898 * stars on the args -- just parse them into an ordered list */
899 num = 0;
900 for (i = 0; i < NCH(ch); i++) {
901 if (TYPE(CHILD(ch, i)) == test) {
902 num++;
903 }
904 }
905
906 argtypes = _Py_asdl_seq_new(num, arena);
907 if (!argtypes)
908 goto out;
909
910 j = 0;
911 for (i = 0; i < NCH(ch); i++) {
912 if (TYPE(CHILD(ch, i)) == test) {
913 arg = ast_for_expr(&c, CHILD(ch, i));
914 if (!arg)
915 goto out;
916 asdl_seq_SET(argtypes, j++, arg);
917 }
918 }
919 }
920 else {
921 argtypes = _Py_asdl_seq_new(0, arena);
922 if (!argtypes)
923 goto out;
924 }
925
926 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
927 if (!ret)
928 goto out;
929 res = FunctionType(argtypes, ret, arena);
930 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000932 PyErr_Format(PyExc_SystemError,
933 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500934 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500936 out:
937 if (c.c_normalize) {
938 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500939 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500940 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941}
942
Victor Stinner14e461d2013-08-26 22:28:21 +0200943mod_ty
944PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
945 PyArena *arena)
946{
947 mod_ty mod;
948 PyObject *filename;
949 filename = PyUnicode_DecodeFSDefault(filename_str);
950 if (filename == NULL)
951 return NULL;
952 mod = PyAST_FromNodeObject(n, flags, filename, arena);
953 Py_DECREF(filename);
954 return mod;
955
956}
957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
959*/
960
961static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800962get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963{
964 switch (TYPE(n)) {
965 case VBAR:
966 return BitOr;
967 case CIRCUMFLEX:
968 return BitXor;
969 case AMPER:
970 return BitAnd;
971 case LEFTSHIFT:
972 return LShift;
973 case RIGHTSHIFT:
974 return RShift;
975 case PLUS:
976 return Add;
977 case MINUS:
978 return Sub;
979 case STAR:
980 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400981 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800982 if (c->c_feature_version < 5) {
983 ast_error(c, n,
984 "The '@' operator is only supported in Python 3.5 and greater");
985 return (operator_ty)0;
986 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400987 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 case SLASH:
989 return Div;
990 case DOUBLESLASH:
991 return FloorDiv;
992 case PERCENT:
993 return Mod;
994 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 }
997}
998
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200999static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +00001000 "None",
1001 "True",
1002 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001003 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001004 NULL,
1005};
1006
1007static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001008forbidden_name(struct compiling *c, identifier name, const node *n,
1009 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001010{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001011 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001012 const char * const *p = FORBIDDEN;
1013 if (!full_checks) {
1014 /* In most cases, the parser will protect True, False, and None
1015 from being assign to. */
1016 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001017 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001018 for (; *p; p++) {
1019 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1020 ast_error(c, n, "cannot assign to %U", name);
1021 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001022 }
1023 }
1024 return 0;
1025}
1026
Serhiy Storchakab619b092018-11-27 09:40:29 +02001027static expr_ty
1028copy_location(expr_ty e, const node *n)
1029{
1030 if (e) {
1031 e->lineno = LINENO(n);
1032 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001033 e->end_lineno = n->n_end_lineno;
1034 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001035 }
1036 return e;
1037}
1038
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001039static const char *
1040get_expr_name(expr_ty e)
1041{
1042 switch (e->kind) {
1043 case Attribute_kind:
1044 return "attribute";
1045 case Subscript_kind:
1046 return "subscript";
1047 case Starred_kind:
1048 return "starred";
1049 case Name_kind:
1050 return "name";
1051 case List_kind:
1052 return "list";
1053 case Tuple_kind:
1054 return "tuple";
1055 case Lambda_kind:
1056 return "lambda";
1057 case Call_kind:
1058 return "function call";
1059 case BoolOp_kind:
1060 case BinOp_kind:
1061 case UnaryOp_kind:
1062 return "operator";
1063 case GeneratorExp_kind:
1064 return "generator expression";
1065 case Yield_kind:
1066 case YieldFrom_kind:
1067 return "yield expression";
1068 case Await_kind:
1069 return "await expression";
1070 case ListComp_kind:
1071 return "list comprehension";
1072 case SetComp_kind:
1073 return "set comprehension";
1074 case DictComp_kind:
1075 return "dict comprehension";
1076 case Dict_kind:
1077 return "dict display";
1078 case Set_kind:
1079 return "set display";
1080 case JoinedStr_kind:
1081 case FormattedValue_kind:
1082 return "f-string expression";
1083 case Constant_kind: {
1084 PyObject *value = e->v.Constant.value;
1085 if (value == Py_None) {
1086 return "None";
1087 }
1088 if (value == Py_False) {
1089 return "False";
1090 }
1091 if (value == Py_True) {
1092 return "True";
1093 }
1094 if (value == Py_Ellipsis) {
1095 return "Ellipsis";
1096 }
1097 return "literal";
1098 }
1099 case Compare_kind:
1100 return "comparison";
1101 case IfExp_kind:
1102 return "conditional expression";
1103 case NamedExpr_kind:
1104 return "named expression";
1105 default:
1106 PyErr_Format(PyExc_SystemError,
1107 "unexpected expression in assignment %d (line %d)",
1108 e->kind, e->lineno);
1109 return NULL;
1110 }
1111}
1112
Jeremy Hyltona8293132006-02-28 17:58:27 +00001113/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114
1115 Only sets context for expr kinds that "can appear in assignment context"
1116 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1117 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118*/
1119
1120static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001121set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
1123 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001124
1125 /* The ast defines augmented store and load contexts, but the
1126 implementation here doesn't actually use them. The code may be
1127 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001128 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001129 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001130 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001131 */
1132 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
1134 switch (e->kind) {
1135 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001136 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001137 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001138 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 e->v.Subscript.ctx = ctx;
1142 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001143 case Starred_kind:
1144 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001145 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001146 return 0;
1147 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001149 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001150 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001151 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001152 }
1153 e->v.Name.ctx = ctx;
1154 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 e->v.List.ctx = ctx;
1157 s = e->v.List.elts;
1158 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001160 e->v.Tuple.ctx = ctx;
1161 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001163 default: {
1164 const char *expr_name = get_expr_name(e);
1165 if (expr_name != NULL) {
1166 ast_error(c, n, "cannot %s %s",
1167 ctx == Store ? "assign to" : "delete",
1168 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001169 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001170 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001171 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001172 }
1173
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 */
1177 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001178 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179
Thomas Wouters89f507f2006-12-13 04:49:30 +00001180 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001181 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 return 0;
1183 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 }
1185 return 1;
1186}
1187
1188static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001189ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190{
1191 REQ(n, augassign);
1192 n = CHILD(n, 0);
1193 switch (STR(n)[0]) {
1194 case '+':
1195 return Add;
1196 case '-':
1197 return Sub;
1198 case '/':
1199 if (STR(n)[1] == '/')
1200 return FloorDiv;
1201 else
1202 return Div;
1203 case '%':
1204 return Mod;
1205 case '<':
1206 return LShift;
1207 case '>':
1208 return RShift;
1209 case '&':
1210 return BitAnd;
1211 case '^':
1212 return BitXor;
1213 case '|':
1214 return BitOr;
1215 case '*':
1216 if (STR(n)[1] == '*')
1217 return Pow;
1218 else
1219 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001220 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001221 if (c->c_feature_version < 5) {
1222 ast_error(c, n,
1223 "The '@' operator is only supported in Python 3.5 and greater");
1224 return (operator_ty)0;
1225 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001226 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001228 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 }
1231}
1232
1233static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001234ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001236 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 |'is' 'not'
1238 */
1239 REQ(n, comp_op);
1240 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001241 n = CHILD(n, 0);
1242 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 case LESS:
1244 return Lt;
1245 case GREATER:
1246 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001247 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return Eq;
1249 case LESSEQUAL:
1250 return LtE;
1251 case GREATEREQUAL:
1252 return GtE;
1253 case NOTEQUAL:
1254 return NotEq;
1255 case NAME:
1256 if (strcmp(STR(n), "in") == 0)
1257 return In;
1258 if (strcmp(STR(n), "is") == 0)
1259 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001260 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001262 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 }
1267 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001268 /* handle "not in" and "is not" */
1269 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 case NAME:
1271 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1272 return NotIn;
1273 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1274 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001275 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001277 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 }
Neal Norwitz79792652005-11-14 04:25:03 +00001282 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285}
1286
1287static asdl_seq *
1288seq_for_testlist(struct compiling *c, const node *n)
1289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001291 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1292 */
Armin Rigo31441302005-10-21 12:57:31 +00001293 asdl_seq *seq;
1294 expr_ty expression;
1295 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001296 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001298 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 if (!seq)
1300 return NULL;
1301
1302 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001304 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305
Benjamin Peterson4905e802009-09-27 02:43:28 +00001306 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001307 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309
1310 assert(i / 2 < seq->size);
1311 asdl_seq_SET(seq, i / 2, expression);
1312 }
1313 return seq;
1314}
1315
Neal Norwitzc1505362006-12-28 06:47:50 +00001316static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001317ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001318{
1319 identifier name;
1320 expr_ty annotation = NULL;
1321 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001322 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001323
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 name = NEW_IDENTIFIER(ch);
1327 if (!name)
1328 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001329 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001330 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001331
1332 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1333 annotation = ast_for_expr(c, CHILD(n, 2));
1334 if (!annotation)
1335 return NULL;
1336 }
1337
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001338 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001339 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001340 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001342 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343}
1344
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345/* returns -1 if failed to handle keyword only arguments
1346 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001347 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 ^^^
1349 start pointing here
1350 */
1351static int
1352handle_keywordonly_args(struct compiling *c, const node *n, int start,
1353 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1354{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001355 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001356 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001358 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 int i = start;
1360 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001361
1362 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001363 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001364 return -1;
1365 }
1366 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 while (i < NCH(n)) {
1368 ch = CHILD(n, i);
1369 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001370 case vfpdef:
1371 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001373 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001374 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001375 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 asdl_seq_SET(kwdefaults, j, expression);
1377 i += 2; /* '=' and test */
1378 }
1379 else { /* setting NULL if no default value exists */
1380 asdl_seq_SET(kwdefaults, j, NULL);
1381 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001382 if (NCH(ch) == 3) {
1383 /* ch is NAME ':' test */
1384 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001387 }
1388 else {
1389 annotation = NULL;
1390 }
1391 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001392 argname = NEW_IDENTIFIER(ch);
1393 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001395 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001396 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001397 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001398 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001399 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001400 if (!arg)
1401 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001402 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001403 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001404 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001405 i += 1; /* the comma, if present */
1406 break;
1407 case TYPE_COMMENT:
1408 /* arg will be equal to the last argument processed */
1409 arg->type_comment = NEW_TYPE_COMMENT(ch);
1410 if (!arg->type_comment)
1411 goto error;
1412 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 break;
1414 case DOUBLESTAR:
1415 return i;
1416 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001417 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 goto error;
1419 }
1420 }
1421 return i;
1422 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425
Jeremy Hyltona8293132006-02-28 17:58:27 +00001426/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427
1428static arguments_ty
1429ast_for_arguments(struct compiling *c, const node *n)
1430{
Neal Norwitzc1505362006-12-28 06:47:50 +00001431 /* This function handles both typedargslist (function definition)
1432 and varargslist (lambda definition).
1433
1434 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001435
1436 The following definition for typedarglist is equivalent to this set of rules:
1437
1438 arguments = argument (',' [TYPE_COMMENT] argument)*
1439 argument = tfpdef ['=' test]
1440 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1441 args = '*' [tfpdef]
1442 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1443 [TYPE_COMMENT] [kwargs]])
1444 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1445 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1446 [TYPE_COMMENT] [args_kwonly_kwargs]])
1447 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1448 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1449 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1450
1451 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1452 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1453 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1454 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1455 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1456 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1457 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1458 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1459 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1460 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1461 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1462 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1463 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1464 '**' tfpdef [','] [TYPE_COMMENT]))
1465
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001466 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001467
1468 The following definition for varargslist is equivalent to this set of rules:
1469
1470 arguments = argument (',' argument )*
1471 argument = vfpdef ['=' test]
1472 kwargs = '**' vfpdef [',']
1473 args = '*' [vfpdef]
1474 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1475 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1476 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1477 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1478 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1479 (vararglist_no_posonly)
1480
1481 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1482 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1483 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1484 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1485 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1486 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1487 [',']]] | '**' vfpdef [','])
1488
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001489 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001490
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001492 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001494 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001495 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001496 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 node *ch;
1498
1499 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001501 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001504 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
Jeremy Hyltone921e022008-07-17 16:37:17 +00001506 /* First count the number of positional args & defaults. The
1507 variable i is the loop index for this for loop and the next.
1508 The next loop picks up where the first leaves off.
1509 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001511 ch = CHILD(n, i);
1512 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001513 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001514 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001515 if (i < NCH(n) && /* skip argument following star */
1516 (TYPE(CHILD(n, i)) == tfpdef ||
1517 TYPE(CHILD(n, i)) == vfpdef)) {
1518 i++;
1519 }
1520 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001521 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001522 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001523 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001524 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001525 if (TYPE(ch) == SLASH ) {
1526 nposonlyargs = nposargs;
1527 nposargs = 0;
1528 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001531 defaults for keyword only args */
1532 for ( ; i < NCH(n); ++i) {
1533 ch = CHILD(n, i);
1534 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001535 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001537 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1538 if (!posonlyargs && nposonlyargs) {
1539 return NULL;
1540 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001541 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001542 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001543 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001545 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001549 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001550 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001551 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001553 since we set NULL as default for keyword only argument w/o default
1554 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001555 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001556 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001557 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001558 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001559
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001560 /* tfpdef: NAME [':' test]
1561 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 */
1563 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001564 j = 0; /* index for defaults */
1565 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001566 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001568 ch = CHILD(n, i);
1569 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001570 case tfpdef:
1571 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1573 anything other than EQUAL or a comma? */
1574 /* XXX Should NCH(n) check be made a separate check? */
1575 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001576 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1577 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001578 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001579 assert(posdefaults != NULL);
1580 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001582 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001584 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001585 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001586 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001587 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001588 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001589 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001590 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001591 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001592 if (l < nposonlyargs) {
1593 asdl_seq_SET(posonlyargs, l++, arg);
1594 } else {
1595 asdl_seq_SET(posargs, k++, arg);
1596 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001597 i += 1; /* the name */
1598 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1599 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001601 case SLASH:
1602 /* Advance the slash and the comma. If there are more names
1603 * after the slash there will be a comma so we are advancing
1604 * the correct number of nodes. If the slash is the last item,
1605 * we will be advancing an extra token but then * i > NCH(n)
1606 * and the enclosing while will finish correctly. */
1607 i += 2;
1608 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001610 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001611 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1612 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001613 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001614 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001615 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001616 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001617 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001618 if (TYPE(ch) == COMMA) {
1619 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001620 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001621
1622 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1623 ast_error(c, CHILD(n, i),
1624 "bare * has associated type comment");
1625 return NULL;
1626 }
1627
Guido van Rossum4f72a782006-10-27 23:31:49 +00001628 res = handle_keywordonly_args(c, n, i,
1629 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001630 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001631 i = res; /* res has new position to process */
1632 }
1633 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001634 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001635 if (!vararg)
1636 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001637
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001638 i += 2; /* the star and the name */
1639 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1640 i += 1; /* the comma, if present */
1641
1642 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1643 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1644 if (!vararg->type_comment)
1645 return NULL;
1646 i += 1;
1647 }
1648
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001649 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1650 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001651 int res = 0;
1652 res = handle_keywordonly_args(c, n, i,
1653 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001654 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001655 i = res; /* res has new position to process */
1656 }
1657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 break;
1659 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001660 ch = CHILD(n, i+1); /* tfpdef */
1661 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001662 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001663 if (!kwarg)
1664 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001665 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001666 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001667 i += 1; /* the comma, if present */
1668 break;
1669 case TYPE_COMMENT:
1670 assert(i);
1671
1672 if (kwarg)
1673 arg = kwarg;
1674
1675 /* arg will be equal to the last argument processed */
1676 arg->type_comment = NEW_TYPE_COMMENT(ch);
1677 if (!arg->type_comment)
1678 return NULL;
1679 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 break;
1681 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001682 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 "unexpected node in varargslist: %d @ %d",
1684 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001685 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001686 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001688 return arguments(posargs, posonlyargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689}
1690
1691static expr_ty
1692ast_for_dotted_name(struct compiling *c, const node *n)
1693{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001694 expr_ty e;
1695 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001698 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699
1700 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001701
1702 lineno = LINENO(n);
1703 col_offset = n->n_col_offset;
1704
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001705 ch = CHILD(n, 0);
1706 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001708 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001709 e = Name(id, Load, lineno, col_offset,
1710 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713
1714 for (i = 2; i < NCH(n); i+=2) {
1715 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001716 if (!id)
1717 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001718 e = Attribute(e, id, Load, lineno, col_offset,
1719 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001720 if (!e)
1721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 }
1723
1724 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725}
1726
1727static expr_ty
1728ast_for_decorator(struct compiling *c, const node *n)
1729{
1730 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1731 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001732 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001735 REQ(CHILD(n, 0), AT);
1736 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1739 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001740 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001743 d = name_expr;
1744 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 }
1746 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001747 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001748 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 if (!d)
1750 return NULL;
1751 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 }
1753 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001754 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 if (!d)
1756 return NULL;
1757 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 }
1759
1760 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761}
1762
1763static asdl_seq*
1764ast_for_decorators(struct compiling *c, const node *n)
1765{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001766 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001767 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001771 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 if (!decorator_seq)
1773 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001776 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001777 if (!d)
1778 return NULL;
1779 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 }
1781 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782}
1783
1784static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001785ast_for_funcdef_impl(struct compiling *c, const node *n0,
1786 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001788 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001789 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001790 identifier name;
1791 arguments_ty args;
1792 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001793 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001794 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001795 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001796 node *tc;
1797 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798
Guido van Rossum495da292019-03-07 12:38:08 -08001799 if (is_async && c->c_feature_version < 5) {
1800 ast_error(c, n,
1801 "Async functions are only supported in Python 3.5 and greater");
1802 return NULL;
1803 }
1804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 REQ(n, funcdef);
1806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 name = NEW_IDENTIFIER(CHILD(n, name_i));
1808 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001809 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001810 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1813 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001814 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001815 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1816 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1817 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001818 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001819 name_i += 2;
1820 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001821 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1822 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1823 if (!type_comment)
1824 return NULL;
1825 name_i += 1;
1826 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001827 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001829 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001830 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001832 if (NCH(CHILD(n, name_i + 3)) > 1) {
1833 /* Check if the suite has a type comment in it. */
1834 tc = CHILD(CHILD(n, name_i + 3), 1);
1835
1836 if (TYPE(tc) == TYPE_COMMENT) {
1837 if (type_comment != NULL) {
1838 ast_error(c, n, "Cannot have two type comments on def");
1839 return NULL;
1840 }
1841 type_comment = NEW_TYPE_COMMENT(tc);
1842 if (!type_comment)
1843 return NULL;
1844 }
1845 }
1846
Yury Selivanov75445082015-05-11 22:57:16 -04001847 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001848 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001849 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001850 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001851 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001852 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001853}
1854
1855static stmt_ty
1856ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1857{
Guido van Rossum495da292019-03-07 12:38:08 -08001858 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001859 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001860 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001861 REQ(CHILD(n, 1), funcdef);
1862
guoci90fc8982018-09-11 17:45:45 -04001863 return ast_for_funcdef_impl(c, n, decorator_seq,
1864 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001865}
1866
1867static stmt_ty
1868ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1869{
1870 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1871 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001872 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001873}
1874
1875
1876static stmt_ty
1877ast_for_async_stmt(struct compiling *c, const node *n)
1878{
Guido van Rossum495da292019-03-07 12:38:08 -08001879 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001880 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001881 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001882
1883 switch (TYPE(CHILD(n, 1))) {
1884 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001885 return ast_for_funcdef_impl(c, n, NULL,
1886 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001887 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001888 return ast_for_with_stmt(c, n,
1889 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001890
1891 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001892 return ast_for_for_stmt(c, n,
1893 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001894
1895 default:
1896 PyErr_Format(PyExc_SystemError,
1897 "invalid async stament: %s",
1898 STR(CHILD(n, 1)));
1899 return NULL;
1900 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901}
1902
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001903static stmt_ty
1904ast_for_decorated(struct compiling *c, const node *n)
1905{
Yury Selivanov75445082015-05-11 22:57:16 -04001906 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001907 stmt_ty thing = NULL;
1908 asdl_seq *decorator_seq = NULL;
1909
1910 REQ(n, decorated);
1911
1912 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1913 if (!decorator_seq)
1914 return NULL;
1915
1916 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001917 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001919
1920 if (TYPE(CHILD(n, 1)) == funcdef) {
1921 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1922 } else if (TYPE(CHILD(n, 1)) == classdef) {
1923 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001924 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1925 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001926 }
1927 return thing;
1928}
1929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001931ast_for_namedexpr(struct compiling *c, const node *n)
1932{
1933 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1934 ['else' ':' suite]
1935 namedexpr_test: test [':=' test]
1936 argument: ( test [comp_for] |
1937 test ':=' test |
1938 test '=' test |
1939 '**' test |
1940 '*' test )
1941 */
1942 expr_ty target, value;
1943
1944 target = ast_for_expr(c, CHILD(n, 0));
1945 if (!target)
1946 return NULL;
1947
1948 value = ast_for_expr(c, CHILD(n, 2));
1949 if (!value)
1950 return NULL;
1951
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001952 if (target->kind != Name_kind) {
1953 const char *expr_name = get_expr_name(target);
1954 if (expr_name != NULL) {
1955 ast_error(c, n, "cannot use named assignment with %s", expr_name);
1956 }
1957 return NULL;
1958 }
1959
1960 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001961 return NULL;
1962
1963 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1964 n->n_end_col_offset, c->c_arena);
1965}
1966
1967static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968ast_for_lambdef(struct compiling *c, const node *n)
1969{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001970 /* lambdef: 'lambda' [varargslist] ':' test
1971 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 arguments_ty args;
1973 expr_ty expression;
1974
1975 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001976 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 if (!args)
1978 return NULL;
1979 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001980 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 }
1983 else {
1984 args = ast_for_arguments(c, CHILD(n, 1));
1985 if (!args)
1986 return NULL;
1987 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001988 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 }
1991
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001992 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1993 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994}
1995
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001996static expr_ty
1997ast_for_ifexpr(struct compiling *c, const node *n)
1998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002000 expr_ty expression, body, orelse;
2001
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00002002 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002003 body = ast_for_expr(c, CHILD(n, 0));
2004 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002005 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002006 expression = ast_for_expr(c, CHILD(n, 2));
2007 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002008 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002009 orelse = ast_for_expr(c, CHILD(n, 4));
2010 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002011 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002012 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002013 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002014 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002015}
2016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002018 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019
Nick Coghlan650f0d02007-04-15 12:05:43 +00002020 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021*/
2022
2023static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002024count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002026 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027
Guido van Rossumd8faa362007-04-27 19:54:29 +00002028 count_comp_for:
2029 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002030 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002031 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08002032 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002033 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002034 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 else if (NCH(n) == 1) {
2036 n = CHILD(n, 0);
2037 }
2038 else {
2039 goto error;
2040 }
2041 if (NCH(n) == (5)) {
2042 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002043 }
2044 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00002045 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002046 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002047 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002048 REQ(n, comp_iter);
2049 n = CHILD(n, 0);
2050 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002051 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002052 else if (TYPE(n) == comp_if) {
2053 if (NCH(n) == 3) {
2054 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002055 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002056 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002057 else
2058 return n_fors;
2059 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002060
Jelle Zijlstraac317702017-10-05 20:24:46 -07002061 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002062 /* Should never be reached */
2063 PyErr_SetString(PyExc_SystemError,
2064 "logic error in count_comp_fors");
2065 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066}
2067
Nick Coghlan650f0d02007-04-15 12:05:43 +00002068/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069
Nick Coghlan650f0d02007-04-15 12:05:43 +00002070 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071*/
2072
2073static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002074count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002076 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077
Guido van Rossumd8faa362007-04-27 19:54:29 +00002078 while (1) {
2079 REQ(n, comp_iter);
2080 if (TYPE(CHILD(n, 0)) == comp_for)
2081 return n_ifs;
2082 n = CHILD(n, 0);
2083 REQ(n, comp_if);
2084 n_ifs++;
2085 if (NCH(n) == 2)
2086 return n_ifs;
2087 n = CHILD(n, 2);
2088 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089}
2090
Guido van Rossum992d4a32007-07-11 13:09:30 +00002091static asdl_seq *
2092ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002095 asdl_seq *comps;
2096
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002097 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 if (n_fors == -1)
2099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002100
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002101 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002102 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002106 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002108 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002109 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002110 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002111 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112
Guido van Rossum992d4a32007-07-11 13:09:30 +00002113 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114
Jelle Zijlstraac317702017-10-05 20:24:46 -07002115 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002116 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002117 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002118 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002119 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002120 else {
2121 sync_n = CHILD(n, 0);
2122 }
2123 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002124
Guido van Rossum495da292019-03-07 12:38:08 -08002125 /* Async comprehensions only allowed in Python 3.6 and greater */
2126 if (is_async && c->c_feature_version < 6) {
2127 ast_error(c, n,
2128 "Async comprehensions are only supported in Python 3.6 and greater");
2129 return NULL;
2130 }
2131
Jelle Zijlstraac317702017-10-05 20:24:46 -07002132 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002133 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002134 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002136 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002137 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139
Thomas Wouters89f507f2006-12-13 04:49:30 +00002140 /* Check the # of children rather than the length of t, since
2141 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002142 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002144 comp = comprehension(first, expression, NULL,
2145 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002147 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2148 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2149 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002150 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002151 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002153
Jelle Zijlstraac317702017-10-05 20:24:46 -07002154 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 int j, n_ifs;
2156 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157
Jelle Zijlstraac317702017-10-05 20:24:46 -07002158 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002159 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002160 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002162
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002163 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002168 REQ(n, comp_iter);
2169 n = CHILD(n, 0);
2170 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171
Guido van Rossum992d4a32007-07-11 13:09:30 +00002172 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002173 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002174 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002175 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002176 if (NCH(n) == 3)
2177 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002179 /* on exit, must guarantee that n is a comp_for */
2180 if (TYPE(n) == comp_iter)
2181 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002182 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002184 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002186 return comps;
2187}
2188
2189static expr_ty
2190ast_for_itercomp(struct compiling *c, const node *n, int type)
2191{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002192 /* testlist_comp: (test|star_expr)
2193 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002194 expr_ty elt;
2195 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002196 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197
Guido van Rossum992d4a32007-07-11 13:09:30 +00002198 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 ch = CHILD(n, 0);
2201 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002202 if (!elt)
2203 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002204 if (elt->kind == Starred_kind) {
2205 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2206 return NULL;
2207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208
Guido van Rossum992d4a32007-07-11 13:09:30 +00002209 comps = ast_for_comprehension(c, CHILD(n, 1));
2210 if (!comps)
2211 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002212
2213 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002214 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2215 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002216 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002217 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2218 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002219 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002220 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2221 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002222 else
2223 /* Should never happen */
2224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225}
2226
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002227/* Fills in the key, value pair corresponding to the dict element. In case
2228 * of an unpacking, key is NULL. *i is advanced by the number of ast
2229 * elements. Iff successful, nonzero is returned.
2230 */
2231static int
2232ast_for_dictelement(struct compiling *c, const node *n, int *i,
2233 expr_ty *key, expr_ty *value)
2234{
2235 expr_ty expression;
2236 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2237 assert(NCH(n) - *i >= 2);
2238
2239 expression = ast_for_expr(c, CHILD(n, *i + 1));
2240 if (!expression)
2241 return 0;
2242 *key = NULL;
2243 *value = expression;
2244
2245 *i += 2;
2246 }
2247 else {
2248 assert(NCH(n) - *i >= 3);
2249
2250 expression = ast_for_expr(c, CHILD(n, *i));
2251 if (!expression)
2252 return 0;
2253 *key = expression;
2254
2255 REQ(CHILD(n, *i + 1), COLON);
2256
2257 expression = ast_for_expr(c, CHILD(n, *i + 2));
2258 if (!expression)
2259 return 0;
2260 *value = expression;
2261
2262 *i += 3;
2263 }
2264 return 1;
2265}
2266
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002268ast_for_dictcomp(struct compiling *c, const node *n)
2269{
2270 expr_ty key, value;
2271 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002272 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002274 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002275 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002276 assert(key);
2277 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002279 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002280 if (!comps)
2281 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002283 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2284 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002285}
2286
2287static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002288ast_for_dictdisplay(struct compiling *c, const node *n)
2289{
2290 int i;
2291 int j;
2292 int size;
2293 asdl_seq *keys, *values;
2294
2295 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2296 keys = _Py_asdl_seq_new(size, c->c_arena);
2297 if (!keys)
2298 return NULL;
2299
2300 values = _Py_asdl_seq_new(size, c->c_arena);
2301 if (!values)
2302 return NULL;
2303
2304 j = 0;
2305 for (i = 0; i < NCH(n); i++) {
2306 expr_ty key, value;
2307
2308 if (!ast_for_dictelement(c, n, &i, &key, &value))
2309 return NULL;
2310 asdl_seq_SET(keys, j, key);
2311 asdl_seq_SET(values, j, value);
2312
2313 j++;
2314 }
2315 keys->size = j;
2316 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002317 return Dict(keys, values, LINENO(n), n->n_col_offset,
2318 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002319}
2320
2321static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002322ast_for_genexp(struct compiling *c, const node *n)
2323{
2324 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002325 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002326}
2327
2328static expr_ty
2329ast_for_listcomp(struct compiling *c, const node *n)
2330{
2331 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002332 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002333}
2334
2335static expr_ty
2336ast_for_setcomp(struct compiling *c, const node *n)
2337{
2338 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002339 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002340}
2341
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002342static expr_ty
2343ast_for_setdisplay(struct compiling *c, const node *n)
2344{
2345 int i;
2346 int size;
2347 asdl_seq *elts;
2348
2349 assert(TYPE(n) == (dictorsetmaker));
2350 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2351 elts = _Py_asdl_seq_new(size, c->c_arena);
2352 if (!elts)
2353 return NULL;
2354 for (i = 0; i < NCH(n); i += 2) {
2355 expr_ty expression;
2356 expression = ast_for_expr(c, CHILD(n, i));
2357 if (!expression)
2358 return NULL;
2359 asdl_seq_SET(elts, i / 2, expression);
2360 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002361 return Set(elts, LINENO(n), n->n_col_offset,
2362 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002363}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002364
2365static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366ast_for_atom(struct compiling *c, const node *n)
2367{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002368 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2369 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002370 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 */
2372 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002375 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002376 PyObject *name;
2377 const char *s = STR(ch);
2378 size_t len = strlen(s);
2379 if (len >= 4 && len <= 5) {
2380 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002381 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002382 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002383 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002384 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002385 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002386 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002387 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002388 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002389 }
2390 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002391 if (!name)
2392 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002393 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002394 return Name(name, Load, LINENO(n), n->n_col_offset,
2395 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002398 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002399 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002400 const char *errtype = NULL;
2401 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2402 errtype = "unicode error";
2403 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2404 errtype = "value error";
2405 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002406 PyObject *type, *value, *tback, *errstr;
2407 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002408 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002409 if (errstr) {
2410 ast_error(c, n, "(%s) %U", errtype, errstr);
2411 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002412 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002413 else {
2414 PyErr_Clear();
2415 ast_error(c, n, "(%s) unknown error", errtype);
2416 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002417 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002418 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002419 Py_XDECREF(tback);
2420 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002421 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002422 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002423 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 }
2425 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002426 PyObject *pynum;
2427 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2428 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2429 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2430 ast_error(c, ch,
2431 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2432 return NULL;
2433 }
2434 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002435 if (!pynum)
2436 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002437
Victor Stinner43d81952013-07-17 00:57:58 +02002438 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2439 Py_DECREF(pynum);
2440 return NULL;
2441 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002442 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002443 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 }
Georg Brandldde00282007-03-18 19:01:53 +00002445 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002446 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002447 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002449 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450
Thomas Wouters89f507f2006-12-13 04:49:30 +00002451 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002452 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2453 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454
Thomas Wouters89f507f2006-12-13 04:49:30 +00002455 if (TYPE(ch) == yield_expr)
2456 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002459 if (NCH(ch) == 1) {
2460 return ast_for_testlist(c, ch);
2461 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002462
Serhiy Storchakab619b092018-11-27 09:40:29 +02002463 if (TYPE(CHILD(ch, 1)) == comp_for) {
2464 return copy_location(ast_for_genexp(c, ch), n);
2465 }
2466 else {
2467 return copy_location(ast_for_testlist(c, ch), n);
2468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002470 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471
Thomas Wouters89f507f2006-12-13 04:49:30 +00002472 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002473 return List(NULL, Load, LINENO(n), n->n_col_offset,
2474 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475
Nick Coghlan650f0d02007-04-15 12:05:43 +00002476 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2478 asdl_seq *elts = seq_for_testlist(c, ch);
2479 if (!elts)
2480 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002481
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002482 return List(elts, Load, LINENO(n), n->n_col_offset,
2483 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002484 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002485 else {
2486 return copy_location(ast_for_listcomp(c, ch), n);
2487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002489 /* dictorsetmaker: ( ((test ':' test | '**' test)
2490 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2491 * ((test | '*' test)
2492 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002493 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002494 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002495 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002496 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002497 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2498 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002499 }
2500 else {
2501 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2502 if (NCH(ch) == 1 ||
2503 (NCH(ch) > 1 &&
2504 TYPE(CHILD(ch, 1)) == COMMA)) {
2505 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002506 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002507 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002508 else if (NCH(ch) > 1 &&
2509 TYPE(CHILD(ch, 1)) == comp_for) {
2510 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002511 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002512 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002513 else if (NCH(ch) > 3 - is_dict &&
2514 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2515 /* It's a dictionary comprehension. */
2516 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002517 ast_error(c, n,
2518 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002519 return NULL;
2520 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002521 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002522 }
2523 else {
2524 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002525 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002527 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002528 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002531 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2532 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 }
2534}
2535
2536static slice_ty
2537ast_for_slice(struct compiling *c, const node *n)
2538{
2539 node *ch;
2540 expr_ty lower = NULL, upper = NULL, step = NULL;
2541
2542 REQ(n, subscript);
2543
2544 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002545 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 sliceop: ':' [test]
2547 */
2548 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 if (NCH(n) == 1 && TYPE(ch) == test) {
2550 /* 'step' variable hold no significance in terms of being used over
2551 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 if (!step)
2554 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555
Thomas Wouters89f507f2006-12-13 04:49:30 +00002556 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
2558
2559 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002560 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 if (!lower)
2562 return NULL;
2563 }
2564
2565 /* If there's an upper bound it's in the second or third position. */
2566 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002567 if (NCH(n) > 1) {
2568 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569
Thomas Wouters89f507f2006-12-13 04:49:30 +00002570 if (TYPE(n2) == test) {
2571 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 if (!upper)
2573 return NULL;
2574 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002575 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578
Thomas Wouters89f507f2006-12-13 04:49:30 +00002579 if (TYPE(n2) == test) {
2580 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 if (!upper)
2582 return NULL;
2583 }
2584 }
2585
2586 ch = CHILD(n, NCH(n) - 1);
2587 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002588 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002589 ch = CHILD(ch, 1);
2590 if (TYPE(ch) == test) {
2591 step = ast_for_expr(c, ch);
2592 if (!step)
2593 return NULL;
2594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 }
2596 }
2597
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002598 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599}
2600
2601static expr_ty
2602ast_for_binop(struct compiling *c, const node *n)
2603{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002604 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002606 BinOp(BinOp(A, op, B), op, C).
2607 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608
Guido van Rossumd8faa362007-04-27 19:54:29 +00002609 int i, nops;
2610 expr_ty expr1, expr2, result;
2611 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
Guido van Rossumd8faa362007-04-27 19:54:29 +00002613 expr1 = ast_for_expr(c, CHILD(n, 0));
2614 if (!expr1)
2615 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616
Guido van Rossumd8faa362007-04-27 19:54:29 +00002617 expr2 = ast_for_expr(c, CHILD(n, 2));
2618 if (!expr2)
2619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
Guido van Rossum495da292019-03-07 12:38:08 -08002621 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002622 if (!newoperator)
2623 return NULL;
2624
2625 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002626 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002627 c->c_arena);
2628 if (!result)
2629 return NULL;
2630
2631 nops = (NCH(n) - 1) / 2;
2632 for (i = 1; i < nops; i++) {
2633 expr_ty tmp_result, tmp;
2634 const node* next_oper = CHILD(n, i * 2 + 1);
2635
Guido van Rossum495da292019-03-07 12:38:08 -08002636 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002637 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return NULL;
2639
Guido van Rossumd8faa362007-04-27 19:54:29 +00002640 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2641 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 return NULL;
2643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002645 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002646 CHILD(n, i * 2 + 2)->n_end_lineno,
2647 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002648 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002650 return NULL;
2651 result = tmp_result;
2652 }
2653 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654}
2655
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002656static expr_ty
2657ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002660 subscriptlist: subscript (',' subscript)* [',']
2661 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2662 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002663 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002664 REQ(n, trailer);
2665 if (TYPE(CHILD(n, 0)) == LPAR) {
2666 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002667 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2668 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002669 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002670 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002671 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002672 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002673 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2674 if (!attr_id)
2675 return NULL;
2676 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002677 LINENO(n), n->n_col_offset,
2678 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002679 }
2680 else {
2681 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002682 REQ(CHILD(n, 2), RSQB);
2683 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002684 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002685 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2686 if (!slc)
2687 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002688 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002689 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002690 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002691 }
2692 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002694 by treating the sequence as a tuple literal if there are
2695 no slice features.
2696 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002697 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002698 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002699 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002700 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002701 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002702 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002703 if (!slices)
2704 return NULL;
2705 for (j = 0; j < NCH(n); j += 2) {
2706 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002707 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002708 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002709 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002710 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002711 asdl_seq_SET(slices, j / 2, slc);
2712 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002713 if (!simple) {
2714 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002715 Load, LINENO(n), n->n_col_offset,
2716 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002717 }
2718 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002719 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002720 if (!elts)
2721 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002722 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2723 slc = (slice_ty)asdl_seq_GET(slices, j);
2724 assert(slc->kind == Index_kind && slc->v.Index.value);
2725 asdl_seq_SET(elts, j, slc->v.Index.value);
2726 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002727 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2728 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002729 if (!e)
2730 return NULL;
2731 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002732 Load, LINENO(n), n->n_col_offset,
2733 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002734 }
2735 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002736}
2737
2738static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002739ast_for_factor(struct compiling *c, const node *n)
2740{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002741 expr_ty expression;
2742
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002743 expression = ast_for_expr(c, CHILD(n, 1));
2744 if (!expression)
2745 return NULL;
2746
2747 switch (TYPE(CHILD(n, 0))) {
2748 case PLUS:
2749 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002750 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002751 c->c_arena);
2752 case MINUS:
2753 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002754 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002755 c->c_arena);
2756 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002757 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2758 n->n_end_lineno, n->n_end_col_offset,
2759 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002760 }
2761 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2762 TYPE(CHILD(n, 0)));
2763 return NULL;
2764}
2765
2766static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002767ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002768{
Yury Selivanov75445082015-05-11 22:57:16 -04002769 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002770 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002771
2772 REQ(n, atom_expr);
2773 nch = NCH(n);
2774
Guido van Rossum495da292019-03-07 12:38:08 -08002775 if (TYPE(CHILD(n, 0)) == AWAIT) {
2776 if (c->c_feature_version < 5) {
2777 ast_error(c, n,
2778 "Await expressions are only supported in Python 3.5 and greater");
2779 return NULL;
2780 }
Yury Selivanov75445082015-05-11 22:57:16 -04002781 start = 1;
2782 assert(nch > 1);
2783 }
2784
2785 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002786 if (!e)
2787 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002788 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002789 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002790 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002791 return Await(e, LINENO(n), n->n_col_offset,
2792 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002793 }
2794
2795 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002796 node *ch = CHILD(n, i);
2797 if (TYPE(ch) != trailer)
2798 break;
2799 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002800 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002801 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002802 tmp->lineno = e->lineno;
2803 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002804 e = tmp;
2805 }
Yury Selivanov75445082015-05-11 22:57:16 -04002806
2807 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002808 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002809 return Await(e, LINENO(n), n->n_col_offset,
2810 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002811 }
2812 else {
2813 return e;
2814 }
2815}
2816
2817static expr_ty
2818ast_for_power(struct compiling *c, const node *n)
2819{
2820 /* power: atom trailer* ('**' factor)*
2821 */
2822 expr_ty e;
2823 REQ(n, power);
2824 e = ast_for_atom_expr(c, CHILD(n, 0));
2825 if (!e)
2826 return NULL;
2827 if (NCH(n) == 1)
2828 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002829 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2830 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002831 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002832 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002833 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2834 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002835 }
2836 return e;
2837}
2838
Guido van Rossum0368b722007-05-11 16:50:42 +00002839static expr_ty
2840ast_for_starred(struct compiling *c, const node *n)
2841{
2842 expr_ty tmp;
2843 REQ(n, star_expr);
2844
2845 tmp = ast_for_expr(c, CHILD(n, 1));
2846 if (!tmp)
2847 return NULL;
2848
2849 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002850 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2851 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002852}
2853
2854
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855/* Do not name a variable 'expr'! Will cause a compile error.
2856*/
2857
2858static expr_ty
2859ast_for_expr(struct compiling *c, const node *n)
2860{
2861 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002862 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002863 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002864 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 and_test: not_test ('and' not_test)*
2867 not_test: 'not' not_test | comparison
2868 comparison: expr (comp_op expr)*
2869 expr: xor_expr ('|' xor_expr)*
2870 xor_expr: and_expr ('^' and_expr)*
2871 and_expr: shift_expr ('&' shift_expr)*
2872 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2873 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002874 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002876 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002877 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002878 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 */
2880
2881 asdl_seq *seq;
2882 int i;
2883
2884 loop:
2885 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002886 case namedexpr_test:
2887 if (NCH(n) == 3)
2888 return ast_for_namedexpr(c, n);
2889 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002891 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002892 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002893 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002895 else if (NCH(n) > 1)
2896 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002897 /* Fallthrough */
2898 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 case and_test:
2900 if (NCH(n) == 1) {
2901 n = CHILD(n, 0);
2902 goto loop;
2903 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002904 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 if (!seq)
2906 return NULL;
2907 for (i = 0; i < NCH(n); i += 2) {
2908 expr_ty e = ast_for_expr(c, CHILD(n, i));
2909 if (!e)
2910 return NULL;
2911 asdl_seq_SET(seq, i / 2, e);
2912 }
2913 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002914 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002915 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002917 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002918 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2919 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 case not_test:
2921 if (NCH(n) == 1) {
2922 n = CHILD(n, 0);
2923 goto loop;
2924 }
2925 else {
2926 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2927 if (!expression)
2928 return NULL;
2929
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002930 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002931 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002932 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 }
2934 case comparison:
2935 if (NCH(n) == 1) {
2936 n = CHILD(n, 0);
2937 goto loop;
2938 }
2939 else {
2940 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002941 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002942 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002943 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 if (!ops)
2945 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002946 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
2949 }
2950 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002951 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002953 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002954 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002956 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957
2958 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002959 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002963 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 asdl_seq_SET(cmps, i / 2, expression);
2965 }
2966 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002967 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002969 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002971 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2972 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 }
2974 break;
2975
Guido van Rossum0368b722007-05-11 16:50:42 +00002976 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 /* The next five cases all handle BinOps. The main body of code
2979 is the same in each case, but the switch turned inside out to
2980 reuse the code for each type of operator.
2981 */
2982 case expr:
2983 case xor_expr:
2984 case and_expr:
2985 case shift_expr:
2986 case arith_expr:
2987 case term:
2988 if (NCH(n) == 1) {
2989 n = CHILD(n, 0);
2990 goto loop;
2991 }
2992 return ast_for_binop(c, n);
2993 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002994 node *an = NULL;
2995 node *en = NULL;
2996 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002997 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002998 if (NCH(n) > 1)
2999 an = CHILD(n, 1); /* yield_arg */
3000 if (an) {
3001 en = CHILD(an, NCH(an) - 1);
3002 if (NCH(an) == 2) {
3003 is_from = 1;
3004 exp = ast_for_expr(c, en);
3005 }
3006 else
3007 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003008 if (!exp)
3009 return NULL;
3010 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05003011 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003012 return YieldFrom(exp, LINENO(n), n->n_col_offset,
3013 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
3014 return Yield(exp, LINENO(n), n->n_col_offset,
3015 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003016 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003017 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 if (NCH(n) == 1) {
3019 n = CHILD(n, 0);
3020 goto loop;
3021 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003022 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00003023 case power:
3024 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003026 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 return NULL;
3028 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003029 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 return NULL;
3031}
3032
3033static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02003034ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003035 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036{
3037 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003038 arglist: argument (',' argument)* [',']
3039 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 */
3041
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003042 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003043 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003044 asdl_seq *args;
3045 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046
3047 REQ(n, arglist);
3048
3049 nargs = 0;
3050 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003052 node *ch = CHILD(n, i);
3053 if (TYPE(ch) == argument) {
3054 if (NCH(ch) == 1)
3055 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003056 else if (TYPE(CHILD(ch, 1)) == comp_for) {
3057 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02003058 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003059 ast_error(c, ch, "invalid syntax");
3060 return NULL;
3061 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003062 if (NCH(n) > 1) {
3063 ast_error(c, ch, "Generator expression must be parenthesized");
3064 return NULL;
3065 }
3066 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003067 else if (TYPE(CHILD(ch, 0)) == STAR)
3068 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003069 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3070 nargs++;
3071 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003073 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003074 nkeywords++;
3075 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003078 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003080 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003081 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003083 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003084
3085 nargs = 0; /* positional arguments + iterable argument unpackings */
3086 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3087 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003089 node *ch = CHILD(n, i);
3090 if (TYPE(ch) == argument) {
3091 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003092 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003093 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003094 /* a positional argument */
3095 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003096 if (ndoublestars) {
3097 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003098 "positional argument follows "
3099 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003100 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003101 else {
3102 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003103 "positional argument follows "
3104 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003105 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003106 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003107 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003108 e = ast_for_expr(c, chch);
3109 if (!e)
3110 return NULL;
3111 asdl_seq_SET(args, nargs++, e);
3112 }
3113 else if (TYPE(chch) == STAR) {
3114 /* an iterable argument unpacking */
3115 expr_ty starred;
3116 if (ndoublestars) {
3117 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003118 "iterable argument unpacking follows "
3119 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003120 return NULL;
3121 }
3122 e = ast_for_expr(c, CHILD(ch, 1));
3123 if (!e)
3124 return NULL;
3125 starred = Starred(e, Load, LINENO(chch),
3126 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003127 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003128 c->c_arena);
3129 if (!starred)
3130 return NULL;
3131 asdl_seq_SET(args, nargs++, starred);
3132
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003133 }
3134 else if (TYPE(chch) == DOUBLESTAR) {
3135 /* a keyword argument unpacking */
3136 keyword_ty kw;
3137 i++;
3138 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003140 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003141 kw = keyword(NULL, e, c->c_arena);
3142 asdl_seq_SET(keywords, nkeywords++, kw);
3143 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003145 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003146 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003147 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003149 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003150 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003152 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3153 /* treat colon equal as positional argument */
3154 if (nkeywords) {
3155 if (ndoublestars) {
3156 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003157 "positional argument follows "
3158 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003159 }
3160 else {
3161 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003162 "positional argument follows "
3163 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003164 }
3165 return NULL;
3166 }
3167 e = ast_for_namedexpr(c, ch);
3168 if (!e)
3169 return NULL;
3170 asdl_seq_SET(args, nargs++, e);
3171 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003172 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003173 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003174 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003175 identifier key, tmp;
3176 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003178 // To remain LL(1), the grammar accepts any test (basically, any
3179 // expression) in the keyword slot of a call site. So, we need
3180 // to manually enforce that the keyword is a NAME here.
3181 static const int name_tree[] = {
3182 test,
3183 or_test,
3184 and_test,
3185 not_test,
3186 comparison,
3187 expr,
3188 xor_expr,
3189 and_expr,
3190 shift_expr,
3191 arith_expr,
3192 term,
3193 factor,
3194 power,
3195 atom_expr,
3196 atom,
3197 0,
3198 };
3199 node *expr_node = chch;
3200 for (int i = 0; name_tree[i]; i++) {
3201 if (TYPE(expr_node) != name_tree[i])
3202 break;
3203 if (NCH(expr_node) != 1)
3204 break;
3205 expr_node = CHILD(expr_node, 0);
3206 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003207 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003208 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003209 "expression cannot contain assignment, "
3210 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003211 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003212 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003213 key = new_identifier(STR(expr_node), c);
3214 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003215 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003217 if (forbidden_name(c, key, chch, 1)) {
3218 return NULL;
3219 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003220 for (k = 0; k < nkeywords; k++) {
3221 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003222 if (tmp && !PyUnicode_Compare(tmp, key)) {
3223 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003224 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003225 return NULL;
3226 }
3227 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003228 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003230 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003231 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003233 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003234 asdl_seq_SET(keywords, nkeywords++, kw);
3235 }
3236 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 }
3238
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003239 return Call(func, args, keywords, func->lineno, func->col_offset,
3240 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241}
3242
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003244ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003246 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003247 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003249 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003250 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003251 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003252 }
3253 else {
3254 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003255 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003258 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 else {
3260 asdl_seq *tmp = seq_for_testlist(c, n);
3261 if (!tmp)
3262 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003263 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3264 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003266}
3267
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268static stmt_ty
3269ast_for_expr_stmt(struct compiling *c, const node *n)
3270{
3271 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003272 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003273 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3274 annassign: ':' test ['=' (yield_expr|testlist)]
3275 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3276 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3277 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003278 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003280 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003282 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003283 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 if (!e)
3285 return NULL;
3286
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003287 return Expr(e, LINENO(n), n->n_col_offset,
3288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 }
3290 else if (TYPE(CHILD(n, 1)) == augassign) {
3291 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003292 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003293 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294
Thomas Wouters89f507f2006-12-13 04:49:30 +00003295 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 if (!expr1)
3297 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003298 if(!set_context(c, expr1, Store, ch))
3299 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003300 /* set_context checks that most expressions are not the left side.
3301 Augmented assignments can only have a name, a subscript, or an
3302 attribute on the left, though, so we have to explicitly check for
3303 those. */
3304 switch (expr1->kind) {
3305 case Name_kind:
3306 case Attribute_kind:
3307 case Subscript_kind:
3308 break;
3309 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003310 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003311 return NULL;
3312 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 ch = CHILD(n, 2);
3315 if (TYPE(ch) == testlist)
3316 expr2 = ast_for_testlist(c, ch);
3317 else
3318 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003319 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320 return NULL;
3321
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003322 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003323 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 return NULL;
3325
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003326 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3327 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003329 else if (TYPE(CHILD(n, 1)) == annassign) {
3330 expr_ty expr1, expr2, expr3;
3331 node *ch = CHILD(n, 0);
3332 node *deep, *ann = CHILD(n, 1);
3333 int simple = 1;
3334
Guido van Rossum495da292019-03-07 12:38:08 -08003335 /* AnnAssigns are only allowed in Python 3.6 or greater */
3336 if (c->c_feature_version < 6) {
3337 ast_error(c, ch,
3338 "Variable annotation syntax is only supported in Python 3.6 and greater");
3339 return NULL;
3340 }
3341
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003342 /* we keep track of parens to qualify (x) as expression not name */
3343 deep = ch;
3344 while (NCH(deep) == 1) {
3345 deep = CHILD(deep, 0);
3346 }
3347 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3348 simple = 0;
3349 }
3350 expr1 = ast_for_testlist(c, ch);
3351 if (!expr1) {
3352 return NULL;
3353 }
3354 switch (expr1->kind) {
3355 case Name_kind:
3356 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3357 return NULL;
3358 }
3359 expr1->v.Name.ctx = Store;
3360 break;
3361 case Attribute_kind:
3362 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3363 return NULL;
3364 }
3365 expr1->v.Attribute.ctx = Store;
3366 break;
3367 case Subscript_kind:
3368 expr1->v.Subscript.ctx = Store;
3369 break;
3370 case List_kind:
3371 ast_error(c, ch,
3372 "only single target (not list) can be annotated");
3373 return NULL;
3374 case Tuple_kind:
3375 ast_error(c, ch,
3376 "only single target (not tuple) can be annotated");
3377 return NULL;
3378 default:
3379 ast_error(c, ch,
3380 "illegal target for annotation");
3381 return NULL;
3382 }
3383
3384 if (expr1->kind != Name_kind) {
3385 simple = 0;
3386 }
3387 ch = CHILD(ann, 1);
3388 expr2 = ast_for_expr(c, ch);
3389 if (!expr2) {
3390 return NULL;
3391 }
3392 if (NCH(ann) == 2) {
3393 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003394 LINENO(n), n->n_col_offset,
3395 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003396 }
3397 else {
3398 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003399 if (TYPE(ch) == testlist) {
3400 expr3 = ast_for_testlist(c, ch);
3401 }
3402 else {
3403 expr3 = ast_for_expr(c, ch);
3404 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003405 if (!expr3) {
3406 return NULL;
3407 }
3408 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003409 LINENO(n), n->n_col_offset,
3410 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003411 }
3412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003414 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003415 asdl_seq *targets;
3416 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003418 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419
Thomas Wouters89f507f2006-12-13 04:49:30 +00003420 /* a normal assignment */
3421 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003422
3423 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3424 nch_minus_type = num - has_type_comment;
3425
3426 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003427 if (!targets)
3428 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003429 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003430 expr_ty e;
3431 node *ch = CHILD(n, i);
3432 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003433 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003434 return NULL;
3435 }
3436 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003438 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003440 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003441 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443
Thomas Wouters89f507f2006-12-13 04:49:30 +00003444 asdl_seq_SET(targets, i / 2, e);
3445 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003446 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003447 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003448 expression = ast_for_testlist(c, value);
3449 else
3450 expression = ast_for_expr(c, value);
3451 if (!expression)
3452 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003453 if (has_type_comment) {
3454 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3455 if (!type_comment)
3456 return NULL;
3457 }
3458 else
3459 type_comment = NULL;
3460 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003461 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463}
3464
Benjamin Peterson78565b22009-06-28 19:19:51 +00003465
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003467ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468{
3469 asdl_seq *seq;
3470 int i;
3471 expr_ty e;
3472
3473 REQ(n, exprlist);
3474
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003475 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003479 e = ast_for_expr(c, CHILD(n, i));
3480 if (!e)
3481 return NULL;
3482 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003483 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 }
3486 return seq;
3487}
3488
3489static stmt_ty
3490ast_for_del_stmt(struct compiling *c, const node *n)
3491{
3492 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494 /* del_stmt: 'del' exprlist */
3495 REQ(n, del_stmt);
3496
3497 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3498 if (!expr_list)
3499 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003500 return Delete(expr_list, LINENO(n), n->n_col_offset,
3501 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502}
3503
3504static stmt_ty
3505ast_for_flow_stmt(struct compiling *c, const node *n)
3506{
3507 /*
3508 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3509 | yield_stmt
3510 break_stmt: 'break'
3511 continue_stmt: 'continue'
3512 return_stmt: 'return' [testlist]
3513 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003514 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 raise_stmt: 'raise' [test [',' test [',' test]]]
3516 */
3517 node *ch;
3518
3519 REQ(n, flow_stmt);
3520 ch = CHILD(n, 0);
3521 switch (TYPE(ch)) {
3522 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003523 return Break(LINENO(n), n->n_col_offset,
3524 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003526 return Continue(LINENO(n), n->n_col_offset,
3527 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003529 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3530 if (!exp)
3531 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003532 return Expr(exp, LINENO(n), n->n_col_offset,
3533 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534 }
3535 case return_stmt:
3536 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003537 return Return(NULL, LINENO(n), n->n_col_offset,
3538 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003540 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541 if (!expression)
3542 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003543 return Return(expression, LINENO(n), n->n_col_offset,
3544 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 }
3546 case raise_stmt:
3547 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003548 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3549 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003550 else if (NCH(ch) >= 2) {
3551 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3553 if (!expression)
3554 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003555 if (NCH(ch) == 4) {
3556 cause = ast_for_expr(c, CHILD(ch, 3));
3557 if (!cause)
3558 return NULL;
3559 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003560 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3561 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562 }
Stefan Krahf432a322017-08-21 13:09:59 +02003563 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003565 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 "unexpected flow_stmt: %d", TYPE(ch));
3567 return NULL;
3568 }
3569}
3570
3571static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003572alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573{
3574 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003575 import_as_name: NAME ['as' NAME]
3576 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 dotted_name: NAME ('.' NAME)*
3578 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003579 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 loop:
3582 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003583 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003584 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003585 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003586 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003587 if (!name)
3588 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003589 if (NCH(n) == 3) {
3590 node *str_node = CHILD(n, 2);
3591 str = NEW_IDENTIFIER(str_node);
3592 if (!str)
3593 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003594 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003595 return NULL;
3596 }
3597 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003598 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003599 return NULL;
3600 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003601 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 case dotted_as_name:
3604 if (NCH(n) == 1) {
3605 n = CHILD(n, 0);
3606 goto loop;
3607 }
3608 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003609 node *asname_node = CHILD(n, 2);
3610 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003611 if (!a)
3612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003614 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003615 if (!a->asname)
3616 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003617 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003618 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 return a;
3620 }
3621 break;
3622 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 }
3672 break;
3673 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003674 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003675 if (!str)
3676 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003677 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3678 Py_DECREF(str);
3679 return NULL;
3680 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003681 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003683 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 "unexpected import name: %d", TYPE(n));
3685 return NULL;
3686 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003687
3688 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 return NULL;
3690}
3691
3692static stmt_ty
3693ast_for_import_stmt(struct compiling *c, const node *n)
3694{
3695 /*
3696 import_stmt: import_name | import_from
3697 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003698 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3699 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003701 int lineno;
3702 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 int i;
3704 asdl_seq *aliases;
3705
3706 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003707 lineno = LINENO(n);
3708 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003710 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003712 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003713 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003714 if (!aliases)
3715 return NULL;
3716 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003717 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003718 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003720 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003722 // Even though n is modified above, the end position is not changed
3723 return Import(aliases, lineno, col_offset,
3724 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003726 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003728 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003729 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003730 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003731 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003733 /* Count the number of dots (for relative imports) and check for the
3734 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003735 for (idx = 1; idx < NCH(n); idx++) {
3736 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003737 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3738 if (!mod)
3739 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003740 idx++;
3741 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003742 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003744 ndots += 3;
3745 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003746 } else if (TYPE(CHILD(n, idx)) != DOT) {
3747 break;
3748 }
3749 ndots++;
3750 }
3751 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003752 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003753 case STAR:
3754 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003755 n = CHILD(n, idx);
3756 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003757 break;
3758 case LPAR:
3759 /* from ... import (x, y, z) */
3760 n = CHILD(n, idx + 1);
3761 n_children = NCH(n);
3762 break;
3763 case import_as_names:
3764 /* from ... import x, y, z */
3765 n = CHILD(n, idx);
3766 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003767 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003768 ast_error(c, n,
3769 "trailing comma not allowed without"
3770 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 return NULL;
3772 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003773 break;
3774 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003775 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003776 return NULL;
3777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003779 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003780 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782
3783 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003784 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003785 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003786 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003788 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003790 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003791 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003792 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003793 if (!import_alias)
3794 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003795 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003798 if (mod != NULL)
3799 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003800 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003801 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003802 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 }
Neal Norwitz79792652005-11-14 04:25:03 +00003804 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 "unknown import statement: starts with command '%s'",
3806 STR(CHILD(n, 0)));
3807 return NULL;
3808}
3809
3810static stmt_ty
3811ast_for_global_stmt(struct compiling *c, const node *n)
3812{
3813 /* global_stmt: 'global' NAME (',' NAME)* */
3814 identifier name;
3815 asdl_seq *s;
3816 int i;
3817
3818 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003819 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003821 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003823 name = NEW_IDENTIFIER(CHILD(n, i));
3824 if (!name)
3825 return NULL;
3826 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003828 return Global(s, LINENO(n), n->n_col_offset,
3829 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830}
3831
3832static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003833ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3834{
3835 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3836 identifier name;
3837 asdl_seq *s;
3838 int i;
3839
3840 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003841 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003842 if (!s)
3843 return NULL;
3844 for (i = 1; i < NCH(n); i += 2) {
3845 name = NEW_IDENTIFIER(CHILD(n, i));
3846 if (!name)
3847 return NULL;
3848 asdl_seq_SET(s, i / 2, name);
3849 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003850 return Nonlocal(s, LINENO(n), n->n_col_offset,
3851 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003852}
3853
3854static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855ast_for_assert_stmt(struct compiling *c, const node *n)
3856{
3857 /* assert_stmt: 'assert' test [',' test] */
3858 REQ(n, assert_stmt);
3859 if (NCH(n) == 2) {
3860 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3861 if (!expression)
3862 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003863 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3864 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 }
3866 else if (NCH(n) == 4) {
3867 expr_ty expr1, expr2;
3868
3869 expr1 = ast_for_expr(c, CHILD(n, 1));
3870 if (!expr1)
3871 return NULL;
3872 expr2 = ast_for_expr(c, CHILD(n, 3));
3873 if (!expr2)
3874 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003876 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3877 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 }
Neal Norwitz79792652005-11-14 04:25:03 +00003879 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880 "improper number of parts to 'assert' statement: %d",
3881 NCH(n));
3882 return NULL;
3883}
3884
3885static asdl_seq *
3886ast_for_suite(struct compiling *c, const node *n)
3887{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003888 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003889 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890 stmt_ty s;
3891 int i, total, num, end, pos = 0;
3892 node *ch;
3893
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003894 if (TYPE(n) != func_body_suite) {
3895 REQ(n, suite);
3896 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003897
3898 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003899 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003901 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003902 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003903 n = CHILD(n, 0);
3904 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003906 */
3907 end = NCH(n) - 1;
3908 if (TYPE(CHILD(n, end - 1)) == SEMI)
3909 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003911 for (i = 0; i < end; i += 2) {
3912 ch = CHILD(n, i);
3913 s = ast_for_stmt(c, ch);
3914 if (!s)
3915 return NULL;
3916 asdl_seq_SET(seq, pos++, s);
3917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 }
3919 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003920 i = 2;
3921 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3922 i += 2;
3923 REQ(CHILD(n, 2), NEWLINE);
3924 }
3925
3926 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003927 ch = CHILD(n, i);
3928 REQ(ch, stmt);
3929 num = num_stmts(ch);
3930 if (num == 1) {
3931 /* small_stmt or compound_stmt with only one child */
3932 s = ast_for_stmt(c, ch);
3933 if (!s)
3934 return NULL;
3935 asdl_seq_SET(seq, pos++, s);
3936 }
3937 else {
3938 int j;
3939 ch = CHILD(ch, 0);
3940 REQ(ch, simple_stmt);
3941 for (j = 0; j < NCH(ch); j += 2) {
3942 /* statement terminates with a semi-colon ';' */
3943 if (NCH(CHILD(ch, j)) == 0) {
3944 assert((j + 1) == NCH(ch));
3945 break;
3946 }
3947 s = ast_for_stmt(c, CHILD(ch, j));
3948 if (!s)
3949 return NULL;
3950 asdl_seq_SET(seq, pos++, s);
3951 }
3952 }
3953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 }
3955 assert(pos == seq->size);
3956 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957}
3958
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003959static void
3960get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3961{
Pablo Galindo46a97922019-02-19 22:51:53 +00003962 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003963 // There must be no empty suites.
3964 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003965 stmt_ty last = asdl_seq_GET(s, tot - 1);
3966 *end_lineno = last->end_lineno;
3967 *end_col_offset = last->end_col_offset;
3968}
3969
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970static stmt_ty
3971ast_for_if_stmt(struct compiling *c, const node *n)
3972{
3973 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3974 ['else' ':' suite]
3975 */
3976 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003977 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978
3979 REQ(n, if_stmt);
3980
3981 if (NCH(n) == 4) {
3982 expr_ty expression;
3983 asdl_seq *suite_seq;
3984
3985 expression = ast_for_expr(c, CHILD(n, 1));
3986 if (!expression)
3987 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003989 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003991 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992
Guido van Rossumd8faa362007-04-27 19:54:29 +00003993 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003994 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003996
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997 s = STR(CHILD(n, 4));
3998 /* s[2], the third character in the string, will be
3999 's' for el_s_e, or
4000 'i' for el_i_f
4001 */
4002 if (s[2] == 's') {
4003 expr_ty expression;
4004 asdl_seq *seq1, *seq2;
4005
4006 expression = ast_for_expr(c, CHILD(n, 1));
4007 if (!expression)
4008 return NULL;
4009 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004010 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 return NULL;
4012 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004013 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004015 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016
Guido van Rossumd8faa362007-04-27 19:54:29 +00004017 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004018 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 }
4020 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004021 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004022 expr_ty expression;
4023 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004024 asdl_seq *orelse = NULL;
4025 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 /* must reference the child n_elif+1 since 'else' token is third,
4027 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004028 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
4029 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
4030 has_else = 1;
4031 n_elif -= 3;
4032 }
4033 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034
Thomas Wouters89f507f2006-12-13 04:49:30 +00004035 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004036 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004038 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004039 if (!orelse)
4040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004042 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004044 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
4045 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4048 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004050 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 asdl_seq_SET(orelse, 0,
4053 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 LINENO(CHILD(n, NCH(n) - 6)),
4055 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004056 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004057 /* the just-created orelse handled the last elif */
4058 n_elif--;
4059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060
Thomas Wouters89f507f2006-12-13 04:49:30 +00004061 for (i = 0; i < n_elif; i++) {
4062 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004063 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004064 if (!newobj)
4065 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004067 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004070 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004073 if (orelse != NULL) {
4074 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4075 } else {
4076 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4077 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004078 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004081 CHILD(n, off)->n_col_offset,
4082 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004083 orelse = newobj;
4084 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004085 expression = ast_for_expr(c, CHILD(n, 1));
4086 if (!expression)
4087 return NULL;
4088 suite_seq = ast_for_suite(c, CHILD(n, 3));
4089 if (!suite_seq)
4090 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004091 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004092 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004093 LINENO(n), n->n_col_offset,
4094 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004095 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004096
4097 PyErr_Format(PyExc_SystemError,
4098 "unexpected token in 'if' statement: %s", s);
4099 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004100}
4101
4102static stmt_ty
4103ast_for_while_stmt(struct compiling *c, const node *n)
4104{
4105 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4106 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004107 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108
4109 if (NCH(n) == 4) {
4110 expr_ty expression;
4111 asdl_seq *suite_seq;
4112
4113 expression = ast_for_expr(c, CHILD(n, 1));
4114 if (!expression)
4115 return NULL;
4116 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004117 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004119 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4120 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4121 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 }
4123 else if (NCH(n) == 7) {
4124 expr_ty expression;
4125 asdl_seq *seq1, *seq2;
4126
4127 expression = ast_for_expr(c, CHILD(n, 1));
4128 if (!expression)
4129 return NULL;
4130 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004131 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132 return NULL;
4133 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004134 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004136 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004138 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4139 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004141
4142 PyErr_Format(PyExc_SystemError,
4143 "wrong number of tokens for 'while' statement: %d",
4144 NCH(n));
4145 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146}
4147
4148static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004149ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150{
guoci90fc8982018-09-11 17:45:45 -04004151 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004152 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004154 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004155 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004156 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004157 int has_type_comment;
4158 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004159
4160 if (is_async && c->c_feature_version < 5) {
4161 ast_error(c, n,
4162 "Async for loops are only supported in Python 3.5 and greater");
4163 return NULL;
4164 }
4165
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004166 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004167 REQ(n, for_stmt);
4168
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004169 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4170
4171 if (NCH(n) == 9 + has_type_comment) {
4172 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173 if (!seq)
4174 return NULL;
4175 }
4176
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004177 node_target = CHILD(n, 1);
4178 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004179 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004180 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004181 /* Check the # of children rather than the length of _target, since
4182 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004183 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004184 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004185 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004187 target = Tuple(_target, Store, first->lineno, first->col_offset,
4188 node_target->n_end_lineno, node_target->n_end_col_offset,
4189 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004191 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004192 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004193 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004194 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004195 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004196 return NULL;
4197
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004198 if (seq != NULL) {
4199 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4200 } else {
4201 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4202 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004203
4204 if (has_type_comment) {
4205 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4206 if (!type_comment)
4207 return NULL;
4208 }
4209 else
4210 type_comment = NULL;
4211
Yury Selivanov75445082015-05-11 22:57:16 -04004212 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004213 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004214 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004215 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004216 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004217 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004218 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004219 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004220}
4221
4222static excepthandler_ty
4223ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4224{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004225 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004226 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004227 REQ(exc, except_clause);
4228 REQ(body, suite);
4229
4230 if (NCH(exc) == 1) {
4231 asdl_seq *suite_seq = ast_for_suite(c, body);
4232 if (!suite_seq)
4233 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004234 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004235
Neal Norwitzad74aa82008-03-31 05:14:30 +00004236 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004237 exc->n_col_offset,
4238 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004239 }
4240 else if (NCH(exc) == 2) {
4241 expr_ty expression;
4242 asdl_seq *suite_seq;
4243
4244 expression = ast_for_expr(c, CHILD(exc, 1));
4245 if (!expression)
4246 return NULL;
4247 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004248 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004250 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004251
Neal Norwitzad74aa82008-03-31 05:14:30 +00004252 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004253 exc->n_col_offset,
4254 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004255 }
4256 else if (NCH(exc) == 4) {
4257 asdl_seq *suite_seq;
4258 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004259 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004260 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004261 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004262 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004263 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004264 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004265 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004266 return NULL;
4267 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004268 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004269 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004270 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004271
Neal Norwitzad74aa82008-03-31 05:14:30 +00004272 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004273 exc->n_col_offset,
4274 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004275 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004276
4277 PyErr_Format(PyExc_SystemError,
4278 "wrong number of children for 'except' clause: %d",
4279 NCH(exc));
4280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004281}
4282
4283static stmt_ty
4284ast_for_try_stmt(struct compiling *c, const node *n)
4285{
Neal Norwitzf599f422005-12-17 21:33:47 +00004286 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004287 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004288 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004289 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004290
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004291 REQ(n, try_stmt);
4292
Neal Norwitzf599f422005-12-17 21:33:47 +00004293 body = ast_for_suite(c, CHILD(n, 2));
4294 if (body == NULL)
4295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004296
Neal Norwitzf599f422005-12-17 21:33:47 +00004297 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4298 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4299 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4300 /* we can assume it's an "else",
4301 because nch >= 9 for try-else-finally and
4302 it would otherwise have a type of except_clause */
4303 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4304 if (orelse == NULL)
4305 return NULL;
4306 n_except--;
4307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004308
Neal Norwitzf599f422005-12-17 21:33:47 +00004309 finally = ast_for_suite(c, CHILD(n, nch - 1));
4310 if (finally == NULL)
4311 return NULL;
4312 n_except--;
4313 }
4314 else {
4315 /* we can assume it's an "else",
4316 otherwise it would have a type of except_clause */
4317 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4318 if (orelse == NULL)
4319 return NULL;
4320 n_except--;
4321 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004322 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004323 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004324 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004325 return NULL;
4326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327
Neal Norwitzf599f422005-12-17 21:33:47 +00004328 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004329 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004330 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004331 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004332 if (handlers == NULL)
4333 return NULL;
4334
4335 for (i = 0; i < n_except; i++) {
4336 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4337 CHILD(n, 5 + i * 3));
4338 if (!e)
4339 return NULL;
4340 asdl_seq_SET(handlers, i, e);
4341 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004342 }
4343
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004344 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004345 if (finally != NULL) {
4346 // finally is always last
4347 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4348 } else if (orelse != NULL) {
4349 // otherwise else is last
4350 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4351 } else {
4352 // inline the get_last_end_pos logic due to layout mismatch
4353 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4354 end_lineno = last_handler->end_lineno;
4355 end_col_offset = last_handler->end_col_offset;
4356 }
4357 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4358 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004359}
4360
Georg Brandl0c315622009-05-25 21:10:36 +00004361/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004362static withitem_ty
4363ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004364{
4365 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004366
Georg Brandl0c315622009-05-25 21:10:36 +00004367 REQ(n, with_item);
4368 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004369 if (!context_expr)
4370 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004371 if (NCH(n) == 3) {
4372 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004373
4374 if (!optional_vars) {
4375 return NULL;
4376 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004377 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004378 return NULL;
4379 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004380 }
4381
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004382 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004383}
4384
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004385/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004386static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004387ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004388{
guoci90fc8982018-09-11 17:45:45 -04004389 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004390 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004391 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004392 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004393
Guido van Rossum495da292019-03-07 12:38:08 -08004394 if (is_async && c->c_feature_version < 5) {
4395 ast_error(c, n,
4396 "Async with statements are only supported in Python 3.5 and greater");
4397 return NULL;
4398 }
4399
Georg Brandl0c315622009-05-25 21:10:36 +00004400 REQ(n, with_stmt);
4401
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004402 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4403 nch_minus_type = NCH(n) - has_type_comment;
4404
4405 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004406 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004407 if (!items)
4408 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004409 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004410 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4411 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004412 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004413 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004414 }
4415
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004416 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4417 if (!body)
4418 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004419 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004420
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004421 if (has_type_comment) {
4422 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4423 if (!type_comment)
4424 return NULL;
4425 }
4426 else
4427 type_comment = NULL;
4428
Yury Selivanov75445082015-05-11 22:57:16 -04004429 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004430 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004431 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004432 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004433 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004434 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004435}
4436
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004437static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004438ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004439{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004440 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004441 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004442 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004443 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004444 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004445
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004446 REQ(n, classdef);
4447
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004448 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004449 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004450 if (!s)
4451 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004452 get_last_end_pos(s, &end_lineno, &end_col_offset);
4453
Benjamin Peterson30760062008-11-25 04:02:28 +00004454 classname = NEW_IDENTIFIER(CHILD(n, 1));
4455 if (!classname)
4456 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004457 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004458 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004459 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004460 LINENO(n), n->n_col_offset,
4461 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004463
4464 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004465 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004466 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004467 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004468 get_last_end_pos(s, &end_lineno, &end_col_offset);
4469
Benjamin Peterson30760062008-11-25 04:02:28 +00004470 classname = NEW_IDENTIFIER(CHILD(n, 1));
4471 if (!classname)
4472 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004473 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004474 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004475 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004476 LINENO(n), n->n_col_offset,
4477 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004478 }
4479
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004480 /* class NAME '(' arglist ')' ':' suite */
4481 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004482 {
4483 PyObject *dummy_name;
4484 expr_ty dummy;
4485 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4486 if (!dummy_name)
4487 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004488 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4489 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4490 c->c_arena);
4491 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004492 if (!call)
4493 return NULL;
4494 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004495 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004496 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004497 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004498 get_last_end_pos(s, &end_lineno, &end_col_offset);
4499
Benjamin Peterson30760062008-11-25 04:02:28 +00004500 classname = NEW_IDENTIFIER(CHILD(n, 1));
4501 if (!classname)
4502 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004503 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004504 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004505
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004506 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004507 decorator_seq, LINENO(n), n->n_col_offset,
4508 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004509}
4510
4511static stmt_ty
4512ast_for_stmt(struct compiling *c, const node *n)
4513{
4514 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004515 assert(NCH(n) == 1);
4516 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004517 }
4518 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004519 assert(num_stmts(n) == 1);
4520 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004521 }
4522 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004523 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004524 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4525 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004526 */
4527 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004528 case expr_stmt:
4529 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004530 case del_stmt:
4531 return ast_for_del_stmt(c, n);
4532 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004533 return Pass(LINENO(n), n->n_col_offset,
4534 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004535 case flow_stmt:
4536 return ast_for_flow_stmt(c, n);
4537 case import_stmt:
4538 return ast_for_import_stmt(c, n);
4539 case global_stmt:
4540 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004541 case nonlocal_stmt:
4542 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004543 case assert_stmt:
4544 return ast_for_assert_stmt(c, n);
4545 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004546 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004547 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4548 TYPE(n), NCH(n));
4549 return NULL;
4550 }
4551 }
4552 else {
4553 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004554 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004555 */
4556 node *ch = CHILD(n, 0);
4557 REQ(n, compound_stmt);
4558 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004559 case if_stmt:
4560 return ast_for_if_stmt(c, ch);
4561 case while_stmt:
4562 return ast_for_while_stmt(c, ch);
4563 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004564 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004565 case try_stmt:
4566 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004567 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004568 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004569 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004570 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004571 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004572 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 case decorated:
4574 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004575 case async_stmt:
4576 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004577 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004578 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004579 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004580 TYPE(n), NCH(n));
4581 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004582 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004583 }
4584}
4585
4586static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004587parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004588{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004589 const char *end;
4590 long x;
4591 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004592 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004593 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004594
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004595 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004596 errno = 0;
4597 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004598 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004599 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004600 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004601 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004602 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004603 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004604 }
4605 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004606 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004607 if (*end == '\0') {
4608 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004609 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004610 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004611 }
4612 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004613 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004614 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004615 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4616 if (compl.imag == -1.0 && PyErr_Occurred())
4617 return NULL;
4618 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004619 }
4620 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004621 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004622 dx = PyOS_string_to_double(s, NULL, NULL);
4623 if (dx == -1.0 && PyErr_Occurred())
4624 return NULL;
4625 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004626 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004627}
4628
4629static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004630parsenumber(struct compiling *c, const char *s)
4631{
4632 char *dup, *end;
4633 PyObject *res = NULL;
4634
4635 assert(s != NULL);
4636
4637 if (strchr(s, '_') == NULL) {
4638 return parsenumber_raw(c, s);
4639 }
4640 /* Create a duplicate without underscores. */
4641 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004642 if (dup == NULL) {
4643 return PyErr_NoMemory();
4644 }
Brett Cannona721aba2016-09-09 14:57:09 -07004645 end = dup;
4646 for (; *s; s++) {
4647 if (*s != '_') {
4648 *end++ = *s;
4649 }
4650 }
4651 *end = '\0';
4652 res = parsenumber_raw(c, dup);
4653 PyMem_Free(dup);
4654 return res;
4655}
4656
4657static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004658decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004659{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004660 const char *s, *t;
4661 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004662 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4663 while (s < end && (*s & 0x80)) s++;
4664 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004665 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004666}
4667
Eric V. Smith56466482016-10-31 14:46:26 -04004668static int
4669warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004670 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004671{
4672 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4673 first_invalid_escape_char);
4674 if (msg == NULL) {
4675 return -1;
4676 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004677 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004678 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004679 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004680 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004681 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004682 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004683 to get a more accurate error report */
4684 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004685 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004686 }
4687 Py_DECREF(msg);
4688 return -1;
4689 }
4690 Py_DECREF(msg);
4691 return 0;
4692}
4693
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004694static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004695decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4696 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004697{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004698 PyObject *v, *u;
4699 char *buf;
4700 char *p;
4701 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004702
Benjamin Peterson202803a2016-02-25 22:34:45 -08004703 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004704 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004705 return NULL;
4706 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4707 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4708 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4709 if (u == NULL)
4710 return NULL;
4711 p = buf = PyBytes_AsString(u);
4712 end = s + len;
4713 while (s < end) {
4714 if (*s == '\\') {
4715 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004716 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004717 strcpy(p, "u005c");
4718 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004719 if (s >= end)
4720 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004721 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004722 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004723 if (*s & 0x80) { /* XXX inefficient */
4724 PyObject *w;
4725 int kind;
4726 void *data;
4727 Py_ssize_t len, i;
4728 w = decode_utf8(c, &s, end);
4729 if (w == NULL) {
4730 Py_DECREF(u);
4731 return NULL;
4732 }
4733 kind = PyUnicode_KIND(w);
4734 data = PyUnicode_DATA(w);
4735 len = PyUnicode_GET_LENGTH(w);
4736 for (i = 0; i < len; i++) {
4737 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4738 sprintf(p, "\\U%08x", chr);
4739 p += 10;
4740 }
4741 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004742 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004743 Py_DECREF(w);
4744 } else {
4745 *p++ = *s++;
4746 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004747 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004748 len = p - buf;
4749 s = buf;
4750
Eric V. Smith56466482016-10-31 14:46:26 -04004751 const char *first_invalid_escape;
4752 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4753
4754 if (v != NULL && first_invalid_escape != NULL) {
4755 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4756 /* We have not decref u before because first_invalid_escape points
4757 inside u. */
4758 Py_XDECREF(u);
4759 Py_DECREF(v);
4760 return NULL;
4761 }
4762 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004763 Py_XDECREF(u);
4764 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004765}
4766
Eric V. Smith56466482016-10-31 14:46:26 -04004767static PyObject *
4768decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4769 size_t len)
4770{
4771 const char *first_invalid_escape;
4772 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4773 &first_invalid_escape);
4774 if (result == NULL)
4775 return NULL;
4776
4777 if (first_invalid_escape != NULL) {
4778 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4779 Py_DECREF(result);
4780 return NULL;
4781 }
4782 }
4783 return result;
4784}
4785
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004786/* Shift locations for the given node and all its children by adding `lineno`
4787 and `col_offset` to existing locations. */
4788static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4789{
4790 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004791 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004792 for (int i = 0; i < NCH(n); ++i) {
4793 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4794 /* Shifting column offsets unnecessary if there's been newlines. */
4795 col_offset = 0;
4796 }
4797 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4798 }
4799 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004800 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004801}
4802
4803/* Fix locations for the given node and its children.
4804
4805 `parent` is the enclosing node.
4806 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004807 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004808*/
4809static void
4810fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4811{
4812 char *substr = NULL;
4813 char *start;
4814 int lines = LINENO(parent) - 1;
4815 int cols = parent->n_col_offset;
4816 /* Find the full fstring to fix location information in `n`. */
4817 while (parent && parent->n_type != STRING)
4818 parent = parent->n_child;
4819 if (parent && parent->n_str) {
4820 substr = strstr(parent->n_str, expr_str);
4821 if (substr) {
4822 start = substr;
4823 while (start > parent->n_str) {
4824 if (start[0] == '\n')
4825 break;
4826 start--;
4827 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004828 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004829 /* adjust the start based on the number of newlines encountered
4830 before the f-string expression */
4831 for (char* p = parent->n_str; p < substr; p++) {
4832 if (*p == '\n') {
4833 lines++;
4834 }
4835 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004836 }
4837 }
4838 fstring_shift_node_locations(n, lines, cols);
4839}
4840
Eric V. Smith451d0e32016-09-09 21:56:20 -04004841/* Compile this expression in to an expr_ty. Add parens around the
4842 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004844fstring_compile_expr(const char *expr_start, const char *expr_end,
4845 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004846
Eric V. Smith235a6f02015-09-19 14:51:32 -04004847{
4848 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004849 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004851 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004852 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004853 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004854
Eric V. Smith1d44c412015-09-23 07:49:00 -04004855 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004856 assert(*(expr_start-1) == '{');
4857 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004858
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004859 /* If the substring is all whitespace, it's an error. We need to catch this
4860 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4861 because turning the expression '' in to '()' would go from being invalid
4862 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004863 for (s = expr_start; s != expr_end; s++) {
4864 char c = *s;
4865 /* The Python parser ignores only the following whitespace
4866 characters (\r already is converted to \n). */
4867 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 break;
4869 }
4870 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004871 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004872 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004873 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004874 }
4875
Eric V. Smith451d0e32016-09-09 21:56:20 -04004876 len = expr_end - expr_start;
4877 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4878 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004879 if (str == NULL) {
4880 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004881 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004882 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004883
Eric V. Smith451d0e32016-09-09 21:56:20 -04004884 str[0] = '(';
4885 memcpy(str+1, expr_start, len);
4886 str[len+1] = ')';
4887 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004888
4889 cf.cf_flags = PyCF_ONLY_AST;
Guido van Rossum495da292019-03-07 12:38:08 -08004890 cf.cf_feature_version = PY_MINOR_VERSION;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004891 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4892 Py_eval_input, 0);
4893 if (!mod_n) {
4894 PyMem_RawFree(str);
4895 return NULL;
4896 }
4897 /* Reuse str to find the correct column offset. */
4898 str[0] = '{';
4899 str[len+1] = '}';
4900 fstring_fix_node_location(n, mod_n, str);
4901 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004902 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004903 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004907}
4908
4909/* Return -1 on error.
4910
4911 Return 0 if we reached the end of the literal.
4912
4913 Return 1 if we haven't reached the end of the literal, but we want
4914 the caller to process the literal up to this point. Used for
4915 doubled braces.
4916*/
4917static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004918fstring_find_literal(const char **str, const char *end, int raw,
4919 PyObject **literal, int recurse_lvl,
4920 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922 /* Get any literal string. It ends when we hit an un-doubled left
4923 brace (which isn't part of a unicode name escape such as
4924 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004925
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004926 const char *s = *str;
4927 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928 int result = 0;
4929
Eric V. Smith235a6f02015-09-19 14:51:32 -04004930 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004931 while (s < end) {
4932 char ch = *s++;
4933 if (!raw && ch == '\\' && s < end) {
4934 ch = *s++;
4935 if (ch == 'N') {
4936 if (s < end && *s++ == '{') {
4937 while (s < end && *s++ != '}') {
4938 }
4939 continue;
4940 }
4941 break;
4942 }
4943 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4944 return -1;
4945 }
4946 }
4947 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948 /* Check for doubled braces, but only at the top level. If
4949 we checked at every level, then f'{0:{3}}' would fail
4950 with the two closing braces. */
4951 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004952 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004953 /* We're going to tell the caller that the literal ends
4954 here, but that they should continue scanning. But also
4955 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004956 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004957 result = 1;
4958 goto done;
4959 }
4960
4961 /* Where a single '{' is the start of a new expression, a
4962 single '}' is not allowed. */
4963 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004964 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965 ast_error(c, n, "f-string: single '}' is not allowed");
4966 return -1;
4967 }
4968 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004969 /* We're either at a '{', which means we're starting another
4970 expression; or a '}', which means we're at the end of this
4971 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004972 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004973 break;
4974 }
4975 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004976 *str = s;
4977 assert(s <= end);
4978 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004980 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004981 if (raw)
4982 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004983 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004984 NULL, NULL);
4985 else
Eric V. Smith56466482016-10-31 14:46:26 -04004986 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004987 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004988 if (!*literal)
4989 return -1;
4990 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991 return result;
4992}
4993
4994/* Forward declaration because parsing is recursive. */
4995static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004996fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 struct compiling *c, const node *n);
4998
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04005000 expression (so it must be a '{'). Returns the FormattedValue node,
5001 which includes the expression, conversion character, and
5002 format_spec expression.
5003
5004 Note that I don't do a perfect job here: I don't make sure that a
5005 closing brace doesn't match an opening paren, for example. It
5006 doesn't need to error on all invalid expressions, just correctly
5007 find the end of all valid ones. Any errors inside the expression
5008 will be caught when we parse it later. */
5009static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005010fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005011 expr_ty *expression, struct compiling *c, const node *n)
5012{
5013 /* Return -1 on error, else 0. */
5014
Eric V. Smith451d0e32016-09-09 21:56:20 -04005015 const char *expr_start;
5016 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005017 expr_ty simple_expression;
5018 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005019 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005020
5021 /* 0 if we're not in a string, else the quote char we're trying to
5022 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024
5025 /* If we're inside a string, 1=normal, 3=triple-quoted. */
5026 int string_type = 0;
5027
5028 /* Keep track of nesting level for braces/parens/brackets in
5029 expressions. */
5030 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005031 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04005032
5033 /* Can only nest one level deep. */
5034 if (recurse_lvl >= 2) {
5035 ast_error(c, n, "f-string: expressions nested too deeply");
5036 return -1;
5037 }
5038
5039 /* The first char must be a left brace, or we wouldn't have gotten
5040 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005041 assert(**str == '{');
5042 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005043
Eric V. Smith451d0e32016-09-09 21:56:20 -04005044 expr_start = *str;
5045 for (; *str < end; (*str)++) {
5046 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047
5048 /* Loop invariants. */
5049 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 if (quote_char)
5052 assert(string_type == 1 || string_type == 3);
5053 else
5054 assert(string_type == 0);
5055
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056 ch = **str;
5057 /* Nowhere inside an expression is a backslash allowed. */
5058 if (ch == '\\') {
5059 /* Error: can't include a backslash character, inside
5060 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005061 ast_error(c, n,
5062 "f-string expression part "
5063 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005064 return -1;
5065 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005066 if (quote_char) {
5067 /* We're inside a string. See if we're at the end. */
5068 /* This code needs to implement the same non-error logic
5069 as tok_get from tokenizer.c, at the letter_quote
5070 label. To actually share that code would be a
5071 nightmare. But, it's unlikely to change and is small,
5072 so duplicate it here. Note we don't need to catch all
5073 of the errors, since they'll be caught when parsing the
5074 expression. We just need to match the non-error
5075 cases. Thus we can ignore \n in single-quoted strings,
5076 for example. Or non-terminated strings. */
5077 if (ch == quote_char) {
5078 /* Does this match the string_type (single or triple
5079 quoted)? */
5080 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005081 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005084 string_type = 0;
5085 quote_char = 0;
5086 continue;
5087 }
5088 } else {
5089 /* We're at the end of a normal string. */
5090 quote_char = 0;
5091 string_type = 0;
5092 continue;
5093 }
5094 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095 } else if (ch == '\'' || ch == '"') {
5096 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005097 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005098 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005099 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005100 } else {
5101 /* Start of a normal string. */
5102 string_type = 1;
5103 }
5104 /* Start looking for the end of the string. */
5105 quote_char = ch;
5106 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005107 if (nested_depth >= MAXLEVEL) {
5108 ast_error(c, n, "f-string: too many nested parenthesis");
5109 return -1;
5110 }
5111 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005113 } else if (ch == '#') {
5114 /* Error: can't include a comment character, inside parens
5115 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005116 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 return -1;
5118 } else if (nested_depth == 0 &&
5119 (ch == '!' || ch == ':' || ch == '}')) {
5120 /* First, test for the special case of "!=". Since '=' is
5121 not an allowed conversion character, nothing is lost in
5122 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005123 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 /* This isn't a conversion character, just continue. */
5125 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005126 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005127 /* Normal way out of this loop. */
5128 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005129 } else if (ch == ']' || ch == '}' || ch == ')') {
5130 if (!nested_depth) {
5131 ast_error(c, n, "f-string: unmatched '%c'", ch);
5132 return -1;
5133 }
5134 nested_depth--;
5135 int opening = parenstack[nested_depth];
5136 if (!((opening == '(' && ch == ')') ||
5137 (opening == '[' && ch == ']') ||
5138 (opening == '{' && ch == '}')))
5139 {
5140 ast_error(c, n,
5141 "f-string: closing parenthesis '%c' "
5142 "does not match opening parenthesis '%c'",
5143 ch, opening);
5144 return -1;
5145 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005146 } else {
5147 /* Just consume this char and loop around. */
5148 }
5149 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151 /* If we leave this loop in a string or with mismatched parens, we
5152 don't care. We'll get a syntax error when compiling the
5153 expression. But, we can produce a better error message, so
5154 let's just do that.*/
5155 if (quote_char) {
5156 ast_error(c, n, "f-string: unterminated string");
5157 return -1;
5158 }
5159 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005160 int opening = parenstack[nested_depth - 1];
5161 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005162 return -1;
5163 }
5164
Eric V. Smith451d0e32016-09-09 21:56:20 -04005165 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005166 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005167
5168 /* Compile the expression as soon as possible, so we show errors
5169 related to the expression before errors related to the
5170 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005172 if (!simple_expression)
5173 return -1;
5174
5175 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 if (**str == '!') {
5177 *str += 1;
5178 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005179 goto unexpected_end_of_string;
5180
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 conversion = **str;
5182 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005183
5184 /* Validate the conversion. */
5185 if (!(conversion == 's' || conversion == 'r'
5186 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005187 ast_error(c, n,
5188 "f-string: invalid conversion character: "
5189 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005190 return -1;
5191 }
5192 }
5193
5194 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005195 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005196 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005197 if (**str == ':') {
5198 *str += 1;
5199 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005200 goto unexpected_end_of_string;
5201
5202 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005203 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005204 if (!format_spec)
5205 return -1;
5206 }
5207
Eric V. Smith451d0e32016-09-09 21:56:20 -04005208 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209 goto unexpected_end_of_string;
5210
5211 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005212 assert(*str < end);
5213 assert(**str == '}');
5214 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215
Eric V. Smith451d0e32016-09-09 21:56:20 -04005216 /* And now create the FormattedValue node that represents this
5217 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005218 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005219 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005220 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005221 c->c_arena);
5222 if (!*expression)
5223 return -1;
5224
5225 return 0;
5226
5227unexpected_end_of_string:
5228 ast_error(c, n, "f-string: expecting '}'");
5229 return -1;
5230}
5231
5232/* Return -1 on error.
5233
5234 Return 0 if we have a literal (possible zero length) and an
5235 expression (zero length if at the end of the string.
5236
5237 Return 1 if we have a literal, but no expression, and we want the
5238 caller to call us again. This is used to deal with doubled
5239 braces.
5240
5241 When called multiple times on the string 'a{{b{0}c', this function
5242 will return:
5243
5244 1. the literal 'a{' with no expression, and a return value
5245 of 1. Despite the fact that there's no expression, the return
5246 value of 1 means we're not finished yet.
5247
5248 2. the literal 'b' and the expression '0', with a return value of
5249 0. The fact that there's an expression means we're not finished.
5250
5251 3. literal 'c' with no expression and a return value of 0. The
5252 combination of the return value of 0 with no expression means
5253 we're finished.
5254*/
5255static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005256fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5257 int recurse_lvl, PyObject **literal,
5258 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005259 struct compiling *c, const node *n)
5260{
5261 int result;
5262
5263 assert(*literal == NULL && *expression == NULL);
5264
5265 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005266 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005267 if (result < 0)
5268 goto error;
5269
5270 assert(result == 0 || result == 1);
5271
5272 if (result == 1)
5273 /* We have a literal, but don't look at the expression. */
5274 return 1;
5275
Eric V. Smith451d0e32016-09-09 21:56:20 -04005276 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005277 /* We're at the end of the string or the end of a nested
5278 f-string: no expression. The top-level error case where we
5279 expect to be at the end of the string but we're at a '}' is
5280 handled later. */
5281 return 0;
5282
5283 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005284 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005285
Eric V. Smith451d0e32016-09-09 21:56:20 -04005286 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005287 goto error;
5288
5289 return 0;
5290
5291error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005292 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005293 return -1;
5294}
5295
5296#define EXPRLIST_N_CACHED 64
5297
5298typedef struct {
5299 /* Incrementally build an array of expr_ty, so be used in an
5300 asdl_seq. Cache some small but reasonably sized number of
5301 expr_ty's, and then after that start dynamically allocating,
5302 doubling the number allocated each time. Note that the f-string
5303 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005304 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005305 fast as you add exressions in an f-string. */
5306
5307 Py_ssize_t allocated; /* Number we've allocated. */
5308 Py_ssize_t size; /* Number we've used. */
5309 expr_ty *p; /* Pointer to the memory we're actually
5310 using. Will point to 'data' until we
5311 start dynamically allocating. */
5312 expr_ty data[EXPRLIST_N_CACHED];
5313} ExprList;
5314
5315#ifdef NDEBUG
5316#define ExprList_check_invariants(l)
5317#else
5318static void
5319ExprList_check_invariants(ExprList *l)
5320{
5321 /* Check our invariants. Make sure this object is "live", and
5322 hasn't been deallocated. */
5323 assert(l->size >= 0);
5324 assert(l->p != NULL);
5325 if (l->size <= EXPRLIST_N_CACHED)
5326 assert(l->data == l->p);
5327}
5328#endif
5329
5330static void
5331ExprList_Init(ExprList *l)
5332{
5333 l->allocated = EXPRLIST_N_CACHED;
5334 l->size = 0;
5335
5336 /* Until we start allocating dynamically, p points to data. */
5337 l->p = l->data;
5338
5339 ExprList_check_invariants(l);
5340}
5341
5342static int
5343ExprList_Append(ExprList *l, expr_ty exp)
5344{
5345 ExprList_check_invariants(l);
5346 if (l->size >= l->allocated) {
5347 /* We need to alloc (or realloc) the memory. */
5348 Py_ssize_t new_size = l->allocated * 2;
5349
5350 /* See if we've ever allocated anything dynamically. */
5351 if (l->p == l->data) {
5352 Py_ssize_t i;
5353 /* We're still using the cached data. Switch to
5354 alloc-ing. */
5355 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5356 if (!l->p)
5357 return -1;
5358 /* Copy the cached data into the new buffer. */
5359 for (i = 0; i < l->size; i++)
5360 l->p[i] = l->data[i];
5361 } else {
5362 /* Just realloc. */
5363 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5364 if (!tmp) {
5365 PyMem_RawFree(l->p);
5366 l->p = NULL;
5367 return -1;
5368 }
5369 l->p = tmp;
5370 }
5371
5372 l->allocated = new_size;
5373 assert(l->allocated == 2 * l->size);
5374 }
5375
5376 l->p[l->size++] = exp;
5377
5378 ExprList_check_invariants(l);
5379 return 0;
5380}
5381
5382static void
5383ExprList_Dealloc(ExprList *l)
5384{
5385 ExprList_check_invariants(l);
5386
5387 /* If there's been an error, or we've never dynamically allocated,
5388 do nothing. */
5389 if (!l->p || l->p == l->data) {
5390 /* Do nothing. */
5391 } else {
5392 /* We have dynamically allocated. Free the memory. */
5393 PyMem_RawFree(l->p);
5394 }
5395 l->p = NULL;
5396 l->size = -1;
5397}
5398
5399static asdl_seq *
5400ExprList_Finish(ExprList *l, PyArena *arena)
5401{
5402 asdl_seq *seq;
5403
5404 ExprList_check_invariants(l);
5405
5406 /* Allocate the asdl_seq and copy the expressions in to it. */
5407 seq = _Py_asdl_seq_new(l->size, arena);
5408 if (seq) {
5409 Py_ssize_t i;
5410 for (i = 0; i < l->size; i++)
5411 asdl_seq_SET(seq, i, l->p[i]);
5412 }
5413 ExprList_Dealloc(l);
5414 return seq;
5415}
5416
5417/* The FstringParser is designed to add a mix of strings and
5418 f-strings, and concat them together as needed. Ultimately, it
5419 generates an expr_ty. */
5420typedef struct {
5421 PyObject *last_str;
5422 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005423 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005424} FstringParser;
5425
5426#ifdef NDEBUG
5427#define FstringParser_check_invariants(state)
5428#else
5429static void
5430FstringParser_check_invariants(FstringParser *state)
5431{
5432 if (state->last_str)
5433 assert(PyUnicode_CheckExact(state->last_str));
5434 ExprList_check_invariants(&state->expr_list);
5435}
5436#endif
5437
5438static void
5439FstringParser_Init(FstringParser *state)
5440{
5441 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005442 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005443 ExprList_Init(&state->expr_list);
5444 FstringParser_check_invariants(state);
5445}
5446
5447static void
5448FstringParser_Dealloc(FstringParser *state)
5449{
5450 FstringParser_check_invariants(state);
5451
5452 Py_XDECREF(state->last_str);
5453 ExprList_Dealloc(&state->expr_list);
5454}
5455
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005456/* Constants for the following */
5457static PyObject *u_kind;
5458
5459/* Compute 'kind' field for string Constant (either 'u' or None) */
5460static PyObject *
5461make_kind(struct compiling *c, const node *n)
5462{
5463 char *s = NULL;
5464 PyObject *kind = NULL;
5465
5466 /* Find the first string literal, if any */
5467 while (TYPE(n) != STRING) {
5468 if (NCH(n) == 0)
5469 return NULL;
5470 n = CHILD(n, 0);
5471 }
5472 REQ(n, STRING);
5473
5474 /* If it starts with 'u', return a PyUnicode "u" string */
5475 s = STR(n);
5476 if (s && *s == 'u') {
5477 if (!u_kind) {
5478 u_kind = PyUnicode_InternFromString("u");
5479 if (!u_kind)
5480 return NULL;
5481 }
5482 kind = u_kind;
5483 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5484 return NULL;
5485 }
5486 Py_INCREF(kind);
5487 }
5488 return kind;
5489}
5490
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005491/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005492static expr_ty
5493make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5494{
5495 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005496 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005497 *str = NULL;
5498 assert(PyUnicode_CheckExact(s));
5499 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5500 Py_DECREF(s);
5501 return NULL;
5502 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005503 kind = make_kind(c, n);
5504 if (kind == NULL && PyErr_Occurred())
5505 return NULL;
5506 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005507 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005508}
5509
5510/* Add a non-f-string (that is, a regular literal string). str is
5511 decref'd. */
5512static int
5513FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5514{
5515 FstringParser_check_invariants(state);
5516
5517 assert(PyUnicode_CheckExact(str));
5518
5519 if (PyUnicode_GET_LENGTH(str) == 0) {
5520 Py_DECREF(str);
5521 return 0;
5522 }
5523
5524 if (!state->last_str) {
5525 /* We didn't have a string before, so just remember this one. */
5526 state->last_str = str;
5527 } else {
5528 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005529 PyUnicode_AppendAndDel(&state->last_str, str);
5530 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531 return -1;
5532 }
5533 FstringParser_check_invariants(state);
5534 return 0;
5535}
5536
Eric V. Smith451d0e32016-09-09 21:56:20 -04005537/* Parse an f-string. The f-string is in *str to end, with no
5538 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005539static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005540FstringParser_ConcatFstring(FstringParser *state, const char **str,
5541 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005542 struct compiling *c, const node *n)
5543{
5544 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005545 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005546
5547 /* Parse the f-string. */
5548 while (1) {
5549 PyObject *literal = NULL;
5550 expr_ty expression = NULL;
5551
5552 /* If there's a zero length literal in front of the
5553 expression, literal will be NULL. If we're at the end of
5554 the f-string, expression will be NULL (unless result == 1,
5555 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005556 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005557 &literal, &expression,
5558 c, n);
5559 if (result < 0)
5560 return -1;
5561
5562 /* Add the literal, if any. */
5563 if (!literal) {
5564 /* Do nothing. Just leave last_str alone (and possibly
5565 NULL). */
5566 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005567 /* Note that the literal can be zero length, if the
5568 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005569 state->last_str = literal;
5570 literal = NULL;
5571 } else {
5572 /* We have a literal, concatenate it. */
5573 assert(PyUnicode_GET_LENGTH(literal) != 0);
5574 if (FstringParser_ConcatAndDel(state, literal) < 0)
5575 return -1;
5576 literal = NULL;
5577 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005578
5579 /* We've dealt with the literal now. It can't be leaked on further
5580 errors. */
5581 assert(literal == NULL);
5582
5583 /* See if we should just loop around to get the next literal
5584 and expression, while ignoring the expression this
5585 time. This is used for un-doubling braces, as an
5586 optimization. */
5587 if (result == 1)
5588 continue;
5589
5590 if (!expression)
5591 /* We're done with this f-string. */
5592 break;
5593
5594 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005595 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005596 if (!state->last_str) {
5597 /* Do nothing. No previous literal. */
5598 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005599 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005600 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5601 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5602 return -1;
5603 }
5604
5605 if (ExprList_Append(&state->expr_list, expression) < 0)
5606 return -1;
5607 }
5608
Eric V. Smith235a6f02015-09-19 14:51:32 -04005609 /* If recurse_lvl is zero, then we must be at the end of the
5610 string. Otherwise, we must be at a right brace. */
5611
Eric V. Smith451d0e32016-09-09 21:56:20 -04005612 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005613 ast_error(c, n, "f-string: unexpected end of string");
5614 return -1;
5615 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005616 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005617 ast_error(c, n, "f-string: expecting '}'");
5618 return -1;
5619 }
5620
5621 FstringParser_check_invariants(state);
5622 return 0;
5623}
5624
5625/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005626 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005627static expr_ty
5628FstringParser_Finish(FstringParser *state, struct compiling *c,
5629 const node *n)
5630{
5631 asdl_seq *seq;
5632
5633 FstringParser_check_invariants(state);
5634
5635 /* If we're just a constant string with no expressions, return
5636 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005637 if (!state->fmode) {
5638 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005639 if (!state->last_str) {
5640 /* Create a zero length string. */
5641 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5642 if (!state->last_str)
5643 goto error;
5644 }
5645 return make_str_node_and_del(&state->last_str, c, n);
5646 }
5647
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005648 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005649 last node in our expression list. */
5650 if (state->last_str) {
5651 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5652 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5653 goto error;
5654 }
5655 /* This has already been freed. */
5656 assert(state->last_str == NULL);
5657
5658 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5659 if (!seq)
5660 goto error;
5661
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005662 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5663 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005664
5665error:
5666 FstringParser_Dealloc(state);
5667 return NULL;
5668}
5669
Eric V. Smith451d0e32016-09-09 21:56:20 -04005670/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5671 at end, parse it into an expr_ty. Return NULL on error. Adjust
5672 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005674fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005675 struct compiling *c, const node *n)
5676{
5677 FstringParser state;
5678
5679 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005680 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005681 c, n) < 0) {
5682 FstringParser_Dealloc(&state);
5683 return NULL;
5684 }
5685
5686 return FstringParser_Finish(&state, c, n);
5687}
5688
5689/* n is a Python string literal, including the bracketing quote
5690 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005692 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005693 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5694 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005695*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005696static int
5697parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5698 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005699{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005700 size_t len;
5701 const char *s = STR(n);
5702 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005703 int fmode = 0;
5704 *bytesmode = 0;
5705 *rawmode = 0;
5706 *result = NULL;
5707 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005708 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005709 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005710 if (quote == 'b' || quote == 'B') {
5711 quote = *++s;
5712 *bytesmode = 1;
5713 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005714 else if (quote == 'u' || quote == 'U') {
5715 quote = *++s;
5716 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005717 else if (quote == 'r' || quote == 'R') {
5718 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005719 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005720 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005721 else if (quote == 'f' || quote == 'F') {
5722 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005723 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005724 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005725 else {
5726 break;
5727 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005728 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005729 }
Guido van Rossum495da292019-03-07 12:38:08 -08005730
5731 /* fstrings are only allowed in Python 3.6 and greater */
5732 if (fmode && c->c_feature_version < 6) {
5733 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5734 return -1;
5735 }
5736
Eric V. Smith451d0e32016-09-09 21:56:20 -04005737 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005738 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005739 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005740 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005741 if (quote != '\'' && quote != '\"') {
5742 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005743 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005744 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005745 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005746 s++;
5747 len = strlen(s);
5748 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005749 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005750 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005751 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005752 }
5753 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005754 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005755 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005756 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005757 }
5758 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005759 /* A triple quoted string. We've already skipped one quote at
5760 the start and one at the end of the string. Now skip the
5761 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005762 s += 2;
5763 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005764 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005765 if (s[--len] != quote || s[--len] != quote) {
5766 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005767 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005768 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005769 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005770
Eric V. Smith451d0e32016-09-09 21:56:20 -04005771 if (fmode) {
5772 /* Just return the bytes. The caller will parse the resulting
5773 string. */
5774 *fstr = s;
5775 *fstrlen = len;
5776 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005777 }
5778
Eric V. Smith451d0e32016-09-09 21:56:20 -04005779 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005780 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005781 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005782 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005783 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005784 const char *ch;
5785 for (ch = s; *ch; ch++) {
5786 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005787 ast_error(c, n,
5788 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005789 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005790 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005791 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005792 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005793 if (*rawmode)
5794 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005795 else
Eric V. Smith56466482016-10-31 14:46:26 -04005796 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005797 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005798 if (*rawmode)
5799 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005800 else
Eric V. Smith56466482016-10-31 14:46:26 -04005801 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005802 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005803 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005804}
5805
Eric V. Smith235a6f02015-09-19 14:51:32 -04005806/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5807 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005808 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005809 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005810 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005811 node if there's just an f-string (with no leading or trailing
5812 literals), or a JoinedStr node if there are multiple f-strings or
5813 any literals involved. */
5814static expr_ty
5815parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005816{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005817 int bytesmode = 0;
5818 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005819 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005820
5821 FstringParser state;
5822 FstringParser_Init(&state);
5823
5824 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005825 int this_bytesmode;
5826 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005827 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005828 const char *fstr;
5829 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005830
5831 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005832 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5833 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005834 goto error;
5835
5836 /* Check that we're not mixing bytes with unicode. */
5837 if (i != 0 && bytesmode != this_bytesmode) {
5838 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005839 /* s is NULL if the current string part is an f-string. */
5840 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005841 goto error;
5842 }
5843 bytesmode = this_bytesmode;
5844
Eric V. Smith451d0e32016-09-09 21:56:20 -04005845 if (fstr != NULL) {
5846 int result;
5847 assert(s == NULL && !bytesmode);
5848 /* This is an f-string. Parse and concatenate it. */
5849 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5850 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005851 if (result < 0)
5852 goto error;
5853 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005854 /* A string or byte string. */
5855 assert(s != NULL && fstr == NULL);
5856
Eric V. Smith451d0e32016-09-09 21:56:20 -04005857 assert(bytesmode ? PyBytes_CheckExact(s) :
5858 PyUnicode_CheckExact(s));
5859
Eric V. Smith451d0e32016-09-09 21:56:20 -04005860 if (bytesmode) {
5861 /* For bytes, concat as we go. */
5862 if (i == 0) {
5863 /* First time, just remember this value. */
5864 bytes_str = s;
5865 } else {
5866 PyBytes_ConcatAndDel(&bytes_str, s);
5867 if (!bytes_str)
5868 goto error;
5869 }
5870 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005871 /* This is a regular string. Concatenate it. */
5872 if (FstringParser_ConcatAndDel(&state, s) < 0)
5873 goto error;
5874 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005875 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005876 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005877 if (bytesmode) {
5878 /* Just return the bytes object and we're done. */
5879 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5880 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005881 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005882 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005883 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005884
Eric V. Smith235a6f02015-09-19 14:51:32 -04005885 /* We're not a bytes string, bytes_str should never have been set. */
5886 assert(bytes_str == NULL);
5887
5888 return FstringParser_Finish(&state, c, n);
5889
5890error:
5891 Py_XDECREF(bytes_str);
5892 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005894}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005895
5896PyObject *
5897_PyAST_GetDocString(asdl_seq *body)
5898{
5899 if (!asdl_seq_LEN(body)) {
5900 return NULL;
5901 }
5902 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5903 if (st->kind != Expr_kind) {
5904 return NULL;
5905 }
5906 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005907 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5908 return e->v.Constant.value;
5909 }
5910 return NULL;
5911}