blob: 855acca29e7c3cf64a96bfd2d6be93a886ce9a7d [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{
113 if (!validate_args(args->args))
114 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700115 if (args->vararg && args->vararg->annotation
116 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500117 return 0;
118 }
119 if (!validate_args(args->kwonlyargs))
120 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100121 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700122 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500123 return 0;
124 }
125 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
126 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
127 return 0;
128 }
129 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
130 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
131 "kw_defaults on arguments");
132 return 0;
133 }
134 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
135}
136
137static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100138validate_constant(PyObject *value)
139{
140 if (value == Py_None || value == Py_Ellipsis)
141 return 1;
142
143 if (PyLong_CheckExact(value)
144 || PyFloat_CheckExact(value)
145 || PyComplex_CheckExact(value)
146 || PyBool_Check(value)
147 || PyUnicode_CheckExact(value)
148 || PyBytes_CheckExact(value))
149 return 1;
150
151 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
152 PyObject *it;
153
154 it = PyObject_GetIter(value);
155 if (it == NULL)
156 return 0;
157
158 while (1) {
159 PyObject *item = PyIter_Next(it);
160 if (item == NULL) {
161 if (PyErr_Occurred()) {
162 Py_DECREF(it);
163 return 0;
164 }
165 break;
166 }
167
168 if (!validate_constant(item)) {
169 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 return 0;
172 }
Victor Stinner726f6902016-01-27 00:11:47 +0100173 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100174 }
175
176 Py_DECREF(it);
177 return 1;
178 }
179
180 return 0;
181}
182
183static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500184validate_expr(expr_ty exp, expr_context_ty ctx)
185{
186 int check_ctx = 1;
187 expr_context_ty actual_ctx;
188
189 /* First check expression context. */
190 switch (exp->kind) {
191 case Attribute_kind:
192 actual_ctx = exp->v.Attribute.ctx;
193 break;
194 case Subscript_kind:
195 actual_ctx = exp->v.Subscript.ctx;
196 break;
197 case Starred_kind:
198 actual_ctx = exp->v.Starred.ctx;
199 break;
200 case Name_kind:
201 actual_ctx = exp->v.Name.ctx;
202 break;
203 case List_kind:
204 actual_ctx = exp->v.List.ctx;
205 break;
206 case Tuple_kind:
207 actual_ctx = exp->v.Tuple.ctx;
208 break;
209 default:
210 if (ctx != Load) {
211 PyErr_Format(PyExc_ValueError, "expression which can't be "
212 "assigned to in %s context", expr_context_name(ctx));
213 return 0;
214 }
215 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100216 /* set actual_ctx to prevent gcc warning */
217 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500218 }
219 if (check_ctx && actual_ctx != ctx) {
220 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
221 expr_context_name(ctx), expr_context_name(actual_ctx));
222 return 0;
223 }
224
225 /* Now validate expression. */
226 switch (exp->kind) {
227 case BoolOp_kind:
228 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
229 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
230 return 0;
231 }
232 return validate_exprs(exp->v.BoolOp.values, Load, 0);
233 case BinOp_kind:
234 return validate_expr(exp->v.BinOp.left, Load) &&
235 validate_expr(exp->v.BinOp.right, Load);
236 case UnaryOp_kind:
237 return validate_expr(exp->v.UnaryOp.operand, Load);
238 case Lambda_kind:
239 return validate_arguments(exp->v.Lambda.args) &&
240 validate_expr(exp->v.Lambda.body, Load);
241 case IfExp_kind:
242 return validate_expr(exp->v.IfExp.test, Load) &&
243 validate_expr(exp->v.IfExp.body, Load) &&
244 validate_expr(exp->v.IfExp.orelse, Load);
245 case Dict_kind:
246 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
247 PyErr_SetString(PyExc_ValueError,
248 "Dict doesn't have the same number of keys as values");
249 return 0;
250 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400251 /* null_ok=1 for keys expressions to allow dict unpacking to work in
252 dict literals, i.e. ``{**{a:b}}`` */
253 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
254 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500255 case Set_kind:
256 return validate_exprs(exp->v.Set.elts, Load, 0);
257#define COMP(NAME) \
258 case NAME ## _kind: \
259 return validate_comprehension(exp->v.NAME.generators) && \
260 validate_expr(exp->v.NAME.elt, Load);
261 COMP(ListComp)
262 COMP(SetComp)
263 COMP(GeneratorExp)
264#undef COMP
265 case DictComp_kind:
266 return validate_comprehension(exp->v.DictComp.generators) &&
267 validate_expr(exp->v.DictComp.key, Load) &&
268 validate_expr(exp->v.DictComp.value, Load);
269 case Yield_kind:
270 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500271 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000272 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400273 case Await_kind:
274 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500275 case Compare_kind:
276 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
277 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
278 return 0;
279 }
280 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
281 asdl_seq_LEN(exp->v.Compare.ops)) {
282 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
283 "of comparators and operands");
284 return 0;
285 }
286 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
287 validate_expr(exp->v.Compare.left, Load);
288 case Call_kind:
289 return validate_expr(exp->v.Call.func, Load) &&
290 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400291 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100292 case Constant_kind:
293 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100294 PyErr_Format(PyExc_TypeError,
295 "got an invalid type in Constant: %s",
296 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100297 return 0;
298 }
299 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400300 case JoinedStr_kind:
301 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
302 case FormattedValue_kind:
303 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
304 return 0;
305 if (exp->v.FormattedValue.format_spec)
306 return validate_expr(exp->v.FormattedValue.format_spec, Load);
307 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500308 case Attribute_kind:
309 return validate_expr(exp->v.Attribute.value, Load);
310 case Subscript_kind:
311 return validate_slice(exp->v.Subscript.slice) &&
312 validate_expr(exp->v.Subscript.value, Load);
313 case Starred_kind:
314 return validate_expr(exp->v.Starred.value, ctx);
315 case List_kind:
316 return validate_exprs(exp->v.List.elts, ctx, 0);
317 case Tuple_kind:
318 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300319 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500320 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500321 return 1;
322 default:
323 PyErr_SetString(PyExc_SystemError, "unexpected expression");
324 return 0;
325 }
326}
327
328static int
329validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
330{
331 if (asdl_seq_LEN(seq))
332 return 1;
333 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
334 return 0;
335}
336
337static int
338validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
339{
340 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
341 validate_exprs(targets, ctx, 0);
342}
343
344static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300345validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348}
349
350static int
351validate_stmt(stmt_ty stmt)
352{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100353 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500354 switch (stmt->kind) {
355 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300356 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500357 validate_arguments(stmt->v.FunctionDef.args) &&
358 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
359 (!stmt->v.FunctionDef.returns ||
360 validate_expr(stmt->v.FunctionDef.returns, Load));
361 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300362 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500363 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
364 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400365 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500366 case Return_kind:
367 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
368 case Delete_kind:
369 return validate_assignlist(stmt->v.Delete.targets, Del);
370 case Assign_kind:
371 return validate_assignlist(stmt->v.Assign.targets, Store) &&
372 validate_expr(stmt->v.Assign.value, Load);
373 case AugAssign_kind:
374 return validate_expr(stmt->v.AugAssign.target, Store) &&
375 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700376 case AnnAssign_kind:
377 if (stmt->v.AnnAssign.target->kind != Name_kind &&
378 stmt->v.AnnAssign.simple) {
379 PyErr_SetString(PyExc_TypeError,
380 "AnnAssign with simple non-Name target");
381 return 0;
382 }
383 return validate_expr(stmt->v.AnnAssign.target, Store) &&
384 (!stmt->v.AnnAssign.value ||
385 validate_expr(stmt->v.AnnAssign.value, Load)) &&
386 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500387 case For_kind:
388 return validate_expr(stmt->v.For.target, Store) &&
389 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300390 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500391 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400392 case AsyncFor_kind:
393 return validate_expr(stmt->v.AsyncFor.target, Store) &&
394 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300395 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400396 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500397 case While_kind:
398 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300399 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500400 validate_stmts(stmt->v.While.orelse);
401 case If_kind:
402 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300403 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500404 validate_stmts(stmt->v.If.orelse);
405 case With_kind:
406 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
407 return 0;
408 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
409 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
410 if (!validate_expr(item->context_expr, Load) ||
411 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
412 return 0;
413 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300414 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400415 case AsyncWith_kind:
416 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
417 return 0;
418 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
419 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
420 if (!validate_expr(item->context_expr, Load) ||
421 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
422 return 0;
423 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300424 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500425 case Raise_kind:
426 if (stmt->v.Raise.exc) {
427 return validate_expr(stmt->v.Raise.exc, Load) &&
428 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
429 }
430 if (stmt->v.Raise.cause) {
431 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
432 return 0;
433 }
434 return 1;
435 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300436 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500437 return 0;
438 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
439 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
440 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
441 return 0;
442 }
443 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
444 asdl_seq_LEN(stmt->v.Try.orelse)) {
445 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
446 return 0;
447 }
448 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
449 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
450 if ((handler->v.ExceptHandler.type &&
451 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300452 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500453 return 0;
454 }
455 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
456 validate_stmts(stmt->v.Try.finalbody)) &&
457 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
458 validate_stmts(stmt->v.Try.orelse));
459 case Assert_kind:
460 return validate_expr(stmt->v.Assert.test, Load) &&
461 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
462 case Import_kind:
463 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
464 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300465 if (stmt->v.ImportFrom.level < 0) {
466 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500467 return 0;
468 }
469 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
470 case Global_kind:
471 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
472 case Nonlocal_kind:
473 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
474 case Expr_kind:
475 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400476 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300477 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400478 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
479 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
480 (!stmt->v.AsyncFunctionDef.returns ||
481 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 case Pass_kind:
483 case Break_kind:
484 case Continue_kind:
485 return 1;
486 default:
487 PyErr_SetString(PyExc_SystemError, "unexpected statement");
488 return 0;
489 }
490}
491
492static int
493validate_stmts(asdl_seq *seq)
494{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100495 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 for (i = 0; i < asdl_seq_LEN(seq); i++) {
497 stmt_ty stmt = asdl_seq_GET(seq, i);
498 if (stmt) {
499 if (!validate_stmt(stmt))
500 return 0;
501 }
502 else {
503 PyErr_SetString(PyExc_ValueError,
504 "None disallowed in statement list");
505 return 0;
506 }
507 }
508 return 1;
509}
510
511static int
512validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
513{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100514 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500515 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
516 expr_ty expr = asdl_seq_GET(exprs, i);
517 if (expr) {
518 if (!validate_expr(expr, ctx))
519 return 0;
520 }
521 else if (!null_ok) {
522 PyErr_SetString(PyExc_ValueError,
523 "None disallowed in expression list");
524 return 0;
525 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100526
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500527 }
528 return 1;
529}
530
531int
532PyAST_Validate(mod_ty mod)
533{
534 int res = 0;
535
536 switch (mod->kind) {
537 case Module_kind:
538 res = validate_stmts(mod->v.Module.body);
539 break;
540 case Interactive_kind:
541 res = validate_stmts(mod->v.Interactive.body);
542 break;
543 case Expression_kind:
544 res = validate_expr(mod->v.Expression.body, Load);
545 break;
546 case Suite_kind:
547 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
548 break;
549 default:
550 PyErr_SetString(PyExc_SystemError, "impossible module node");
551 res = 0;
552 break;
553 }
554 return res;
555}
556
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500557/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500558#include "grammar.h"
559#include "parsetok.h"
560#include "graminit.h"
561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562/* Data structure used internally */
563struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400564 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200565 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500566 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567};
568
569static asdl_seq *seq_for_testlist(struct compiling *, const node *);
570static expr_ty ast_for_expr(struct compiling *, const node *);
571static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300572static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000573static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
574 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000575static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000576static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577
guoci90fc8982018-09-11 17:45:45 -0400578static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
579static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200582static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000583 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000585static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400586static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000587static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Nick Coghlan650f0d02007-04-15 12:05:43 +0000589#define COMP_GENEXP 0
590#define COMP_LISTCOMP 1
591#define COMP_SETCOMP 2
592
Benjamin Peterson55e00432012-01-16 17:22:31 -0500593static int
594init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000595{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
597 if (!m)
598 return 0;
599 c->c_normalize = PyObject_GetAttrString(m, "normalize");
600 Py_DECREF(m);
601 if (!c->c_normalize)
602 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500603 return 1;
604}
605
606static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400607new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500608{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400609 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500610 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000611 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500612 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000614 /* Check whether there are non-ASCII characters in the
615 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500616 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300618 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500619 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500620 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200621 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300623 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
624 if (form == NULL) {
625 Py_DECREF(id);
626 return NULL;
627 }
628 PyObject *args[2] = {form, id};
629 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500630 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200631 if (!id2)
632 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300633 if (!PyUnicode_Check(id2)) {
634 PyErr_Format(PyExc_TypeError,
635 "unicodedata.normalize() must return a string, not "
636 "%.200s",
637 Py_TYPE(id2)->tp_name);
638 Py_DECREF(id2);
639 return NULL;
640 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200641 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000642 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000643 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200644 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
645 Py_DECREF(id);
646 return NULL;
647 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000648 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649}
650
Benjamin Peterson55e00432012-01-16 17:22:31 -0500651#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200654ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400656 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200657 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_start(va, errmsg);
660 errstr = PyUnicode_FromFormatV(errmsg, va);
661 va_end(va);
662 if (!errstr) {
663 return 0;
664 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200665 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000667 Py_INCREF(Py_None);
668 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 }
Ammar Askar025eb982018-09-24 17:12:49 -0400670 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200671 if (!tmp) {
672 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400673 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000674 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 Py_DECREF(errstr);
677 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400678 if (value) {
679 PyErr_SetObject(PyExc_SyntaxError, value);
680 Py_DECREF(value);
681 }
682 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683}
684
685/* num_stmts() returns number of contained statements.
686
687 Use this routine to determine how big a sequence is needed for
688 the statements in a parse tree. Its raison d'etre is this bit of
689 grammar:
690
691 stmt: simple_stmt | compound_stmt
692 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
693
694 A simple_stmt can contain multiple small_stmt elements joined
695 by semicolons. If the arg is a simple_stmt, the number of
696 small_stmt elements is returned.
697*/
698
699static int
700num_stmts(const node *n)
701{
702 int i, l;
703 node *ch;
704
705 switch (TYPE(n)) {
706 case single_input:
707 if (TYPE(CHILD(n, 0)) == NEWLINE)
708 return 0;
709 else
710 return num_stmts(CHILD(n, 0));
711 case file_input:
712 l = 0;
713 for (i = 0; i < NCH(n); i++) {
714 ch = CHILD(n, i);
715 if (TYPE(ch) == stmt)
716 l += num_stmts(ch);
717 }
718 return l;
719 case stmt:
720 return num_stmts(CHILD(n, 0));
721 case compound_stmt:
722 return 1;
723 case simple_stmt:
724 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
725 case suite:
726 if (NCH(n) == 1)
727 return num_stmts(CHILD(n, 0));
728 else {
729 l = 0;
730 for (i = 2; i < (NCH(n) - 1); i++)
731 l += num_stmts(CHILD(n, i));
732 return l;
733 }
734 default: {
735 char buf[128];
736
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000737 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 TYPE(n), NCH(n));
739 Py_FatalError(buf);
740 }
741 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700742 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743}
744
745/* Transform the CST rooted at node * to the appropriate AST
746*/
747
748mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200749PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
750 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000752 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 asdl_seq *stmts = NULL;
754 stmt_ty s;
755 node *ch;
756 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400759 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200760 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400761 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800762 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800763
764 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766
Jeremy Hyltona8293132006-02-28 17:58:27 +0000767 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 switch (TYPE(n)) {
769 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200770 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500772 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 for (i = 0; i < NCH(n) - 1; i++) {
774 ch = CHILD(n, i);
775 if (TYPE(ch) == NEWLINE)
776 continue;
777 REQ(ch, stmt);
778 num = num_stmts(ch);
779 if (num == 1) {
780 s = ast_for_stmt(&c, ch);
781 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500782 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000783 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 }
785 else {
786 ch = CHILD(ch, 0);
787 REQ(ch, simple_stmt);
788 for (j = 0; j < num; j++) {
789 s = ast_for_stmt(&c, CHILD(ch, j * 2));
790 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500791 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000792 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 }
794 }
795 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300796 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 case eval_input: {
799 expr_ty testlist_ast;
800
Nick Coghlan650f0d02007-04-15 12:05:43 +0000801 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000802 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500804 goto out;
805 res = Expression(testlist_ast, arena);
806 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 case single_input:
809 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200810 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500812 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000814 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000816 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
818 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820 else {
821 n = CHILD(n, 0);
822 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200823 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500825 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000827 s = ast_for_stmt(&c, n);
828 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500829 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 asdl_seq_SET(stmts, 0, s);
831 }
832 else {
833 /* Only a simple_stmt can contain multiple statements. */
834 REQ(n, simple_stmt);
835 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 if (TYPE(CHILD(n, i)) == NEWLINE)
837 break;
838 s = ast_for_stmt(&c, CHILD(n, i));
839 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500840 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 asdl_seq_SET(stmts, i / 2, s);
842 }
843 }
844
Benjamin Peterson55e00432012-01-16 17:22:31 -0500845 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500847 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000849 PyErr_Format(PyExc_SystemError,
850 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 out:
854 if (c.c_normalize) {
855 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500856 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500857 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858}
859
Victor Stinner14e461d2013-08-26 22:28:21 +0200860mod_ty
861PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
862 PyArena *arena)
863{
864 mod_ty mod;
865 PyObject *filename;
866 filename = PyUnicode_DecodeFSDefault(filename_str);
867 if (filename == NULL)
868 return NULL;
869 mod = PyAST_FromNodeObject(n, flags, filename, arena);
870 Py_DECREF(filename);
871 return mod;
872
873}
874
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
876*/
877
878static operator_ty
879get_operator(const node *n)
880{
881 switch (TYPE(n)) {
882 case VBAR:
883 return BitOr;
884 case CIRCUMFLEX:
885 return BitXor;
886 case AMPER:
887 return BitAnd;
888 case LEFTSHIFT:
889 return LShift;
890 case RIGHTSHIFT:
891 return RShift;
892 case PLUS:
893 return Add;
894 case MINUS:
895 return Sub;
896 case STAR:
897 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400898 case AT:
899 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900 case SLASH:
901 return Div;
902 case DOUBLESLASH:
903 return FloorDiv;
904 case PERCENT:
905 return Mod;
906 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000907 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 }
909}
910
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200911static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000912 "None",
913 "True",
914 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200915 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000916 NULL,
917};
918
919static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400920forbidden_name(struct compiling *c, identifier name, const node *n,
921 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000922{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000923 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200924 const char * const *p = FORBIDDEN;
925 if (!full_checks) {
926 /* In most cases, the parser will protect True, False, and None
927 from being assign to. */
928 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000929 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200930 for (; *p; p++) {
931 if (_PyUnicode_EqualToASCIIString(name, *p)) {
932 ast_error(c, n, "cannot assign to %U", name);
933 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000934 }
935 }
936 return 0;
937}
938
Serhiy Storchakab619b092018-11-27 09:40:29 +0200939static expr_ty
940copy_location(expr_ty e, const node *n)
941{
942 if (e) {
943 e->lineno = LINENO(n);
944 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000945 e->end_lineno = n->n_end_lineno;
946 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +0200947 }
948 return e;
949}
950
Jeremy Hyltona8293132006-02-28 17:58:27 +0000951/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952
953 Only sets context for expr kinds that "can appear in assignment context"
954 (according to ../Parser/Python.asdl). For other expr kinds, it sets
955 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956*/
957
958static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000959set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960{
961 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000962 /* If a particular expression type can't be used for assign / delete,
963 set expr_name to its name and an error message will be generated.
964 */
965 const char* expr_name = NULL;
966
967 /* The ast defines augmented store and load contexts, but the
968 implementation here doesn't actually use them. The code may be
969 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000971 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000972 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 */
974 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975
976 switch (e->kind) {
977 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000978 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400979 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000980 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000981 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000983 e->v.Subscript.ctx = ctx;
984 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000985 case Starred_kind:
986 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000987 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000988 return 0;
989 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000991 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500992 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000993 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 }
995 e->v.Name.ctx = ctx;
996 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000998 e->v.List.ctx = ctx;
999 s = e->v.List.elts;
1000 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001002 e->v.Tuple.ctx = ctx;
1003 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001004 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 case Lambda_kind:
1006 expr_name = "lambda";
1007 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001009 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001010 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001013 case UnaryOp_kind:
1014 expr_name = "operator";
1015 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001017 expr_name = "generator expression";
1018 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001019 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001020 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001021 expr_name = "yield expression";
1022 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001023 case Await_kind:
1024 expr_name = "await expression";
1025 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001026 case ListComp_kind:
1027 expr_name = "list comprehension";
1028 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001029 case SetComp_kind:
1030 expr_name = "set comprehension";
1031 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001032 case DictComp_kind:
1033 expr_name = "dict comprehension";
1034 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001035 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001036 expr_name = "dict display";
1037 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001038 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001039 expr_name = "set display";
1040 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001041 case JoinedStr_kind:
1042 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001043 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001045 case Constant_kind: {
1046 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001047 if (value == Py_None || value == Py_False || value == Py_True
1048 || value == Py_Ellipsis)
1049 {
1050 return ast_error(c, n, "cannot %s %R",
1051 ctx == Store ? "assign to" : "delete",
1052 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001053 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001054 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001055 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001056 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001057 case Compare_kind:
1058 expr_name = "comparison";
1059 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060 case IfExp_kind:
1061 expr_name = "conditional expression";
1062 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001063 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 PyErr_Format(PyExc_SystemError,
1065 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001066 e->kind, e->lineno);
1067 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001069 /* Check for error string set by switch */
1070 if (expr_name) {
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001071 return ast_error(c, n, "cannot %s %s",
1072 ctx == Store ? "assign to" : "delete",
1073 expr_name);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001074 }
1075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 */
1079 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001080 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081
Thomas Wouters89f507f2006-12-13 04:49:30 +00001082 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001083 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001084 return 0;
1085 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 }
1087 return 1;
1088}
1089
1090static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001091ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092{
1093 REQ(n, augassign);
1094 n = CHILD(n, 0);
1095 switch (STR(n)[0]) {
1096 case '+':
1097 return Add;
1098 case '-':
1099 return Sub;
1100 case '/':
1101 if (STR(n)[1] == '/')
1102 return FloorDiv;
1103 else
1104 return Div;
1105 case '%':
1106 return Mod;
1107 case '<':
1108 return LShift;
1109 case '>':
1110 return RShift;
1111 case '&':
1112 return BitAnd;
1113 case '^':
1114 return BitXor;
1115 case '|':
1116 return BitOr;
1117 case '*':
1118 if (STR(n)[1] == '*')
1119 return Pow;
1120 else
1121 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001122 case '@':
1123 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001125 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 }
1128}
1129
1130static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001131ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001133 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 |'is' 'not'
1135 */
1136 REQ(n, comp_op);
1137 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001138 n = CHILD(n, 0);
1139 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 case LESS:
1141 return Lt;
1142 case GREATER:
1143 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001144 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return Eq;
1146 case LESSEQUAL:
1147 return LtE;
1148 case GREATEREQUAL:
1149 return GtE;
1150 case NOTEQUAL:
1151 return NotEq;
1152 case NAME:
1153 if (strcmp(STR(n), "in") == 0)
1154 return In;
1155 if (strcmp(STR(n), "is") == 0)
1156 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001157 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001159 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001162 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 }
1164 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 /* handle "not in" and "is not" */
1166 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 case NAME:
1168 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1169 return NotIn;
1170 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1171 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001172 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001174 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001176 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001177 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 }
Neal Norwitz79792652005-11-14 04:25:03 +00001179 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182}
1183
1184static asdl_seq *
1185seq_for_testlist(struct compiling *c, const node *n)
1186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001188 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1189 */
Armin Rigo31441302005-10-21 12:57:31 +00001190 asdl_seq *seq;
1191 expr_ty expression;
1192 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001193 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001195 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 if (!seq)
1197 return NULL;
1198
1199 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001201 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202
Benjamin Peterson4905e802009-09-27 02:43:28 +00001203 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206
1207 assert(i / 2 < seq->size);
1208 asdl_seq_SET(seq, i / 2, expression);
1209 }
1210 return seq;
1211}
1212
Neal Norwitzc1505362006-12-28 06:47:50 +00001213static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001214ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001215{
1216 identifier name;
1217 expr_ty annotation = NULL;
1218 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001219 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001220
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001221 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001222 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001223 name = NEW_IDENTIFIER(ch);
1224 if (!name)
1225 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001226 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001227 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001228
1229 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1230 annotation = ast_for_expr(c, CHILD(n, 2));
1231 if (!annotation)
1232 return NULL;
1233 }
1234
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001235 ret = arg(name, annotation, LINENO(n), n->n_col_offset,
1236 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001237 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001238 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001239 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240}
1241
Guido van Rossum4f72a782006-10-27 23:31:49 +00001242/* returns -1 if failed to handle keyword only arguments
1243 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001244 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001245 ^^^
1246 start pointing here
1247 */
1248static int
1249handle_keywordonly_args(struct compiling *c, const node *n, int start,
1250 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1251{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001252 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001253 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001254 expr_ty expression, annotation;
1255 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001256 int i = start;
1257 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001258
1259 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001260 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001261 return -1;
1262 }
1263 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001264 while (i < NCH(n)) {
1265 ch = CHILD(n, i);
1266 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001267 case vfpdef:
1268 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001269 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001270 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001271 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001272 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 asdl_seq_SET(kwdefaults, j, expression);
1274 i += 2; /* '=' and test */
1275 }
1276 else { /* setting NULL if no default value exists */
1277 asdl_seq_SET(kwdefaults, j, NULL);
1278 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 if (NCH(ch) == 3) {
1280 /* ch is NAME ':' test */
1281 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001282 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001283 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001284 }
1285 else {
1286 annotation = NULL;
1287 }
1288 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001289 argname = NEW_IDENTIFIER(ch);
1290 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001291 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001292 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001293 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001294 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001295 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001296 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001297 if (!arg)
1298 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 i += 2; /* the name and the comma */
1301 break;
1302 case DOUBLESTAR:
1303 return i;
1304 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001305 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 goto error;
1307 }
1308 }
1309 return i;
1310 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001312}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313
Jeremy Hyltona8293132006-02-28 17:58:27 +00001314/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315
1316static arguments_ty
1317ast_for_arguments(struct compiling *c, const node *n)
1318{
Neal Norwitzc1505362006-12-28 06:47:50 +00001319 /* This function handles both typedargslist (function definition)
1320 and varargslist (lambda definition).
1321
1322 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001323 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1324 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1325 | '**' tfpdef [',']]]
1326 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1327 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001328 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001329 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1330 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1331 | '**' vfpdef [',']]]
1332 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1333 | '**' vfpdef [',']
1334 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1339 int nposdefaults = 0, found_default = 0;
1340 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001342 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 node *ch;
1344
1345 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001347 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351
Jeremy Hyltone921e022008-07-17 16:37:17 +00001352 /* First count the number of positional args & defaults. The
1353 variable i is the loop index for this for loop and the next.
1354 The next loop picks up where the first leaves off.
1355 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 ch = CHILD(n, i);
1358 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001359 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001360 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001361 if (i < NCH(n) && /* skip argument following star */
1362 (TYPE(CHILD(n, i)) == tfpdef ||
1363 TYPE(CHILD(n, i)) == vfpdef)) {
1364 i++;
1365 }
1366 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 defaults for keyword only args */
1374 for ( ; i < NCH(n); ++i) {
1375 ch = CHILD(n, i);
1376 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001377 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001379 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001381 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001383 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001387 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001388 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001389 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 since we set NULL as default for keyword only argument w/o default
1392 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001393 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001394 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001396 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001398 /* tfpdef: NAME [':' test]
1399 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 */
1401 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001402 j = 0; /* index for defaults */
1403 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 ch = CHILD(n, i);
1406 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001407 case tfpdef:
1408 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1410 anything other than EQUAL or a comma? */
1411 /* XXX Should NCH(n) check be made a separate check? */
1412 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001413 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1414 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001415 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 assert(posdefaults != NULL);
1417 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001422 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001424 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001425 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001426 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001427 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001428 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 i += 2; /* the name and the comma */
1431 break;
1432 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001433 if (i+1 >= NCH(n) ||
1434 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001435 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001436 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001437 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001439 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001440 if (TYPE(ch) == COMMA) {
1441 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001442 i += 2; /* now follows keyword only arguments */
1443 res = handle_keywordonly_args(c, n, i,
1444 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001445 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 i = res; /* res has new position to process */
1447 }
1448 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001449 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001450 if (!vararg)
1451 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001452
Guido van Rossum4f72a782006-10-27 23:31:49 +00001453 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001454 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1455 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001456 int res = 0;
1457 res = handle_keywordonly_args(c, n, i,
1458 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001459 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 i = res; /* res has new position to process */
1461 }
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 break;
1464 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001465 ch = CHILD(n, i+1); /* tfpdef */
1466 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001467 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001468 if (!kwarg)
1469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 i += 3;
1471 break;
1472 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001473 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 "unexpected node in varargslist: %d @ %d",
1475 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001476 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001479 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480}
1481
1482static expr_ty
1483ast_for_dotted_name(struct compiling *c, const node *n)
1484{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001485 expr_ty e;
1486 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001487 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001489 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490
1491 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001492
1493 lineno = LINENO(n);
1494 col_offset = n->n_col_offset;
1495
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001496 ch = CHILD(n, 0);
1497 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001499 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001500 e = Name(id, Load, lineno, col_offset,
1501 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
1505 for (i = 2; i < NCH(n); i+=2) {
1506 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507 if (!id)
1508 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001509 e = Attribute(e, id, Load, lineno, col_offset,
1510 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001511 if (!e)
1512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 }
1514
1515 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516}
1517
1518static expr_ty
1519ast_for_decorator(struct compiling *c, const node *n)
1520{
1521 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1522 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001523 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001526 REQ(CHILD(n, 0), AT);
1527 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1530 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001531 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001534 d = name_expr;
1535 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 }
1537 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001538 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001539 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 if (!d)
1541 return NULL;
1542 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 }
1544 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001545 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001546 if (!d)
1547 return NULL;
1548 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 }
1550
1551 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
1554static asdl_seq*
1555ast_for_decorators(struct compiling *c, const node *n)
1556{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001557 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001558 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001562 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 if (!decorator_seq)
1564 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001567 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001568 if (!d)
1569 return NULL;
1570 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 }
1572 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573}
1574
1575static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001576ast_for_funcdef_impl(struct compiling *c, const node *n0,
1577 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001579 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
guoci90fc8982018-09-11 17:45:45 -04001580 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001581 identifier name;
1582 arguments_ty args;
1583 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001584 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001585 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001586 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587
1588 REQ(n, funcdef);
1589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 name = NEW_IDENTIFIER(CHILD(n, name_i));
1591 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001592 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001593 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001594 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1596 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001597 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001598 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1599 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1600 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001601 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001602 name_i += 2;
1603 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001604 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001606 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001607 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608
Yury Selivanov75445082015-05-11 22:57:16 -04001609 if (is_async)
1610 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001611 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001612 else
1613 return FunctionDef(name, args, body, decorator_seq, returns,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001614 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001615}
1616
1617static stmt_ty
1618ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1619{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001620 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001621 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001622 REQ(CHILD(n, 0), NAME);
1623 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001624 REQ(CHILD(n, 1), funcdef);
1625
guoci90fc8982018-09-11 17:45:45 -04001626 return ast_for_funcdef_impl(c, n, decorator_seq,
1627 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001628}
1629
1630static stmt_ty
1631ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1632{
1633 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1634 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001635 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001636}
1637
1638
1639static stmt_ty
1640ast_for_async_stmt(struct compiling *c, const node *n)
1641{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001642 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001643 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001644 REQ(CHILD(n, 0), NAME);
1645 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001646
1647 switch (TYPE(CHILD(n, 1))) {
1648 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001649 return ast_for_funcdef_impl(c, n, NULL,
1650 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001651 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001652 return ast_for_with_stmt(c, n,
1653 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001654
1655 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001656 return ast_for_for_stmt(c, n,
1657 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001658
1659 default:
1660 PyErr_Format(PyExc_SystemError,
1661 "invalid async stament: %s",
1662 STR(CHILD(n, 1)));
1663 return NULL;
1664 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665}
1666
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001667static stmt_ty
1668ast_for_decorated(struct compiling *c, const node *n)
1669{
Yury Selivanov75445082015-05-11 22:57:16 -04001670 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001671 stmt_ty thing = NULL;
1672 asdl_seq *decorator_seq = NULL;
1673
1674 REQ(n, decorated);
1675
1676 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1677 if (!decorator_seq)
1678 return NULL;
1679
1680 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001681 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001683
1684 if (TYPE(CHILD(n, 1)) == funcdef) {
1685 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1686 } else if (TYPE(CHILD(n, 1)) == classdef) {
1687 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001688 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1689 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001690 }
1691 return thing;
1692}
1693
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694static expr_ty
1695ast_for_lambdef(struct compiling *c, const node *n)
1696{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001697 /* lambdef: 'lambda' [varargslist] ':' test
1698 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 arguments_ty args;
1700 expr_ty expression;
1701
1702 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001703 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 if (!args)
1705 return NULL;
1706 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001707 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 }
1710 else {
1711 args = ast_for_arguments(c, CHILD(n, 1));
1712 if (!args)
1713 return NULL;
1714 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001715 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 }
1718
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001719 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1720 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721}
1722
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001723static expr_ty
1724ast_for_ifexpr(struct compiling *c, const node *n)
1725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001727 expr_ty expression, body, orelse;
1728
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001729 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001730 body = ast_for_expr(c, CHILD(n, 0));
1731 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001732 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001733 expression = ast_for_expr(c, CHILD(n, 2));
1734 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001736 orelse = ast_for_expr(c, CHILD(n, 4));
1737 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001738 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001740 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001741 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001742}
1743
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001745 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746
Nick Coghlan650f0d02007-04-15 12:05:43 +00001747 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748*/
1749
1750static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001751count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001753 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754
Guido van Rossumd8faa362007-04-27 19:54:29 +00001755 count_comp_for:
1756 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001757 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001758 if (NCH(n) == 2) {
1759 REQ(CHILD(n, 0), NAME);
1760 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1761 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001762 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001763 else if (NCH(n) == 1) {
1764 n = CHILD(n, 0);
1765 }
1766 else {
1767 goto error;
1768 }
1769 if (NCH(n) == (5)) {
1770 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001771 }
1772 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001773 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001774 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001776 REQ(n, comp_iter);
1777 n = CHILD(n, 0);
1778 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001780 else if (TYPE(n) == comp_if) {
1781 if (NCH(n) == 3) {
1782 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001784 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001785 else
1786 return n_fors;
1787 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001788
Jelle Zijlstraac317702017-10-05 20:24:46 -07001789 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 /* Should never be reached */
1791 PyErr_SetString(PyExc_SystemError,
1792 "logic error in count_comp_fors");
1793 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794}
1795
Nick Coghlan650f0d02007-04-15 12:05:43 +00001796/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797
Nick Coghlan650f0d02007-04-15 12:05:43 +00001798 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799*/
1800
1801static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001802count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001804 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805
Guido van Rossumd8faa362007-04-27 19:54:29 +00001806 while (1) {
1807 REQ(n, comp_iter);
1808 if (TYPE(CHILD(n, 0)) == comp_for)
1809 return n_ifs;
1810 n = CHILD(n, 0);
1811 REQ(n, comp_if);
1812 n_ifs++;
1813 if (NCH(n) == 2)
1814 return n_ifs;
1815 n = CHILD(n, 2);
1816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817}
1818
Guido van Rossum992d4a32007-07-11 13:09:30 +00001819static asdl_seq *
1820ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001823 asdl_seq *comps;
1824
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001825 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 if (n_fors == -1)
1827 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001828
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001829 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001830 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001834 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001836 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001837 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001838 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001839 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840
Guido van Rossum992d4a32007-07-11 13:09:30 +00001841 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842
Jelle Zijlstraac317702017-10-05 20:24:46 -07001843 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001844 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001845 REQ(CHILD(n, 0), NAME);
1846 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1847 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001848 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001849 else {
1850 sync_n = CHILD(n, 0);
1851 }
1852 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001853
Jelle Zijlstraac317702017-10-05 20:24:46 -07001854 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001855 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001858 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001859 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001861
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 /* Check the # of children rather than the length of t, since
1863 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001864 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001865 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001866 comp = comprehension(first, expression, NULL,
1867 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001869 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1870 for_ch->n_end_lineno, for_ch->n_end_col_offset,
1871 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001872 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001873 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001875
Jelle Zijlstraac317702017-10-05 20:24:46 -07001876 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 int j, n_ifs;
1878 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879
Jelle Zijlstraac317702017-10-05 20:24:46 -07001880 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001881 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001885 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 REQ(n, comp_iter);
1891 n = CHILD(n, 0);
1892 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001896 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001897 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 if (NCH(n) == 3)
1899 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 /* on exit, must guarantee that n is a comp_for */
1902 if (TYPE(n) == comp_iter)
1903 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001904 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001906 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908 return comps;
1909}
1910
1911static expr_ty
1912ast_for_itercomp(struct compiling *c, const node *n, int type)
1913{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914 /* testlist_comp: (test|star_expr)
1915 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001916 expr_ty elt;
1917 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919
Guido van Rossum992d4a32007-07-11 13:09:30 +00001920 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001922 ch = CHILD(n, 0);
1923 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001924 if (!elt)
1925 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001926 if (elt->kind == Starred_kind) {
1927 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1928 return NULL;
1929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930
Guido van Rossum992d4a32007-07-11 13:09:30 +00001931 comps = ast_for_comprehension(c, CHILD(n, 1));
1932 if (!comps)
1933 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001934
1935 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001936 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
1937 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001938 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001939 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
1940 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001941 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001942 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
1943 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001944 else
1945 /* Should never happen */
1946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947}
1948
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001949/* Fills in the key, value pair corresponding to the dict element. In case
1950 * of an unpacking, key is NULL. *i is advanced by the number of ast
1951 * elements. Iff successful, nonzero is returned.
1952 */
1953static int
1954ast_for_dictelement(struct compiling *c, const node *n, int *i,
1955 expr_ty *key, expr_ty *value)
1956{
1957 expr_ty expression;
1958 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1959 assert(NCH(n) - *i >= 2);
1960
1961 expression = ast_for_expr(c, CHILD(n, *i + 1));
1962 if (!expression)
1963 return 0;
1964 *key = NULL;
1965 *value = expression;
1966
1967 *i += 2;
1968 }
1969 else {
1970 assert(NCH(n) - *i >= 3);
1971
1972 expression = ast_for_expr(c, CHILD(n, *i));
1973 if (!expression)
1974 return 0;
1975 *key = expression;
1976
1977 REQ(CHILD(n, *i + 1), COLON);
1978
1979 expression = ast_for_expr(c, CHILD(n, *i + 2));
1980 if (!expression)
1981 return 0;
1982 *value = expression;
1983
1984 *i += 3;
1985 }
1986 return 1;
1987}
1988
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001990ast_for_dictcomp(struct compiling *c, const node *n)
1991{
1992 expr_ty key, value;
1993 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001994 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001996 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001997 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 assert(key);
1999 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002001 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002002 if (!comps)
2003 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002005 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2006 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002007}
2008
2009static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002010ast_for_dictdisplay(struct compiling *c, const node *n)
2011{
2012 int i;
2013 int j;
2014 int size;
2015 asdl_seq *keys, *values;
2016
2017 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2018 keys = _Py_asdl_seq_new(size, c->c_arena);
2019 if (!keys)
2020 return NULL;
2021
2022 values = _Py_asdl_seq_new(size, c->c_arena);
2023 if (!values)
2024 return NULL;
2025
2026 j = 0;
2027 for (i = 0; i < NCH(n); i++) {
2028 expr_ty key, value;
2029
2030 if (!ast_for_dictelement(c, n, &i, &key, &value))
2031 return NULL;
2032 asdl_seq_SET(keys, j, key);
2033 asdl_seq_SET(values, j, value);
2034
2035 j++;
2036 }
2037 keys->size = j;
2038 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002039 return Dict(keys, values, LINENO(n), n->n_col_offset,
2040 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002041}
2042
2043static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002044ast_for_genexp(struct compiling *c, const node *n)
2045{
2046 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002047 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002048}
2049
2050static expr_ty
2051ast_for_listcomp(struct compiling *c, const node *n)
2052{
2053 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002054 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002055}
2056
2057static expr_ty
2058ast_for_setcomp(struct compiling *c, const node *n)
2059{
2060 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002061 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002062}
2063
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002064static expr_ty
2065ast_for_setdisplay(struct compiling *c, const node *n)
2066{
2067 int i;
2068 int size;
2069 asdl_seq *elts;
2070
2071 assert(TYPE(n) == (dictorsetmaker));
2072 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2073 elts = _Py_asdl_seq_new(size, c->c_arena);
2074 if (!elts)
2075 return NULL;
2076 for (i = 0; i < NCH(n); i += 2) {
2077 expr_ty expression;
2078 expression = ast_for_expr(c, CHILD(n, i));
2079 if (!expression)
2080 return NULL;
2081 asdl_seq_SET(elts, i / 2, expression);
2082 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002083 return Set(elts, LINENO(n), n->n_col_offset,
2084 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002085}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086
2087static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088ast_for_atom(struct compiling *c, const node *n)
2089{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002090 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2091 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002092 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 */
2094 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002097 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002098 PyObject *name;
2099 const char *s = STR(ch);
2100 size_t len = strlen(s);
2101 if (len >= 4 && len <= 5) {
2102 if (!strcmp(s, "None"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002103 return Constant(Py_None, LINENO(n), n->n_col_offset,
2104 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002105 if (!strcmp(s, "True"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002106 return Constant(Py_True, LINENO(n), n->n_col_offset,
2107 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002108 if (!strcmp(s, "False"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002109 return Constant(Py_False, LINENO(n), n->n_col_offset,
2110 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002111 }
2112 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002113 if (!name)
2114 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002115 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002116 return Name(name, Load, LINENO(n), n->n_col_offset,
2117 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002118 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002120 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002121 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002122 const char *errtype = NULL;
2123 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2124 errtype = "unicode error";
2125 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2126 errtype = "value error";
2127 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002128 PyObject *type, *value, *tback, *errstr;
2129 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002130 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002131 if (errstr) {
2132 ast_error(c, n, "(%s) %U", errtype, errstr);
2133 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002134 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002135 else {
2136 PyErr_Clear();
2137 ast_error(c, n, "(%s) unknown error", errtype);
2138 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002139 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002140 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 Py_XDECREF(tback);
2142 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002144 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002145 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
2147 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002148 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002149 if (!pynum)
2150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002151
Victor Stinner43d81952013-07-17 00:57:58 +02002152 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2153 Py_DECREF(pynum);
2154 return NULL;
2155 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002156 return Constant(pynum, LINENO(n), n->n_col_offset,
2157 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 }
Georg Brandldde00282007-03-18 19:01:53 +00002159 case ELLIPSIS: /* Ellipsis */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002160 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset,
2161 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002166 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2167 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168
Thomas Wouters89f507f2006-12-13 04:49:30 +00002169 if (TYPE(ch) == yield_expr)
2170 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002173 if (NCH(ch) == 1) {
2174 return ast_for_testlist(c, ch);
2175 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002176
Serhiy Storchakab619b092018-11-27 09:40:29 +02002177 if (TYPE(CHILD(ch, 1)) == comp_for) {
2178 return copy_location(ast_for_genexp(c, ch), n);
2179 }
2180 else {
2181 return copy_location(ast_for_testlist(c, ch), n);
2182 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002184 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185
Thomas Wouters89f507f2006-12-13 04:49:30 +00002186 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002187 return List(NULL, Load, LINENO(n), n->n_col_offset,
2188 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189
Nick Coghlan650f0d02007-04-15 12:05:43 +00002190 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002191 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2192 asdl_seq *elts = seq_for_testlist(c, ch);
2193 if (!elts)
2194 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002195
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002196 return List(elts, Load, LINENO(n), n->n_col_offset,
2197 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002198 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002199 else {
2200 return copy_location(ast_for_listcomp(c, ch), n);
2201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002203 /* dictorsetmaker: ( ((test ':' test | '**' test)
2204 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2205 * ((test | '*' test)
2206 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002207 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002208 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002209 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002210 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002211 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2212 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002213 }
2214 else {
2215 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2216 if (NCH(ch) == 1 ||
2217 (NCH(ch) > 1 &&
2218 TYPE(CHILD(ch, 1)) == COMMA)) {
2219 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002220 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002221 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002222 else if (NCH(ch) > 1 &&
2223 TYPE(CHILD(ch, 1)) == comp_for) {
2224 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002225 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002226 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002227 else if (NCH(ch) > 3 - is_dict &&
2228 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2229 /* It's a dictionary comprehension. */
2230 if (is_dict) {
2231 ast_error(c, n, "dict unpacking cannot be used in "
2232 "dict comprehension");
2233 return NULL;
2234 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002235 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002236 }
2237 else {
2238 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002239 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002240 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002241 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002242 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002245 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2246 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 }
2248}
2249
2250static slice_ty
2251ast_for_slice(struct compiling *c, const node *n)
2252{
2253 node *ch;
2254 expr_ty lower = NULL, upper = NULL, step = NULL;
2255
2256 REQ(n, subscript);
2257
2258 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002259 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 sliceop: ':' [test]
2261 */
2262 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 if (NCH(n) == 1 && TYPE(ch) == test) {
2264 /* 'step' variable hold no significance in terms of being used over
2265 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 if (!step)
2268 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269
Thomas Wouters89f507f2006-12-13 04:49:30 +00002270 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 }
2272
2273 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002274 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 if (!lower)
2276 return NULL;
2277 }
2278
2279 /* If there's an upper bound it's in the second or third position. */
2280 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 if (NCH(n) > 1) {
2282 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Thomas Wouters89f507f2006-12-13 04:49:30 +00002284 if (TYPE(n2) == test) {
2285 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 if (!upper)
2287 return NULL;
2288 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002289 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002291 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292
Thomas Wouters89f507f2006-12-13 04:49:30 +00002293 if (TYPE(n2) == test) {
2294 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 if (!upper)
2296 return NULL;
2297 }
2298 }
2299
2300 ch = CHILD(n, NCH(n) - 1);
2301 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002302 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303 ch = CHILD(ch, 1);
2304 if (TYPE(ch) == test) {
2305 step = ast_for_expr(c, ch);
2306 if (!step)
2307 return NULL;
2308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 }
2310 }
2311
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002312 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313}
2314
2315static expr_ty
2316ast_for_binop(struct compiling *c, const node *n)
2317{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002320 BinOp(BinOp(A, op, B), op, C).
2321 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322
Guido van Rossumd8faa362007-04-27 19:54:29 +00002323 int i, nops;
2324 expr_ty expr1, expr2, result;
2325 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327 expr1 = ast_for_expr(c, CHILD(n, 0));
2328 if (!expr1)
2329 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330
Guido van Rossumd8faa362007-04-27 19:54:29 +00002331 expr2 = ast_for_expr(c, CHILD(n, 2));
2332 if (!expr2)
2333 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334
Guido van Rossumd8faa362007-04-27 19:54:29 +00002335 newoperator = get_operator(CHILD(n, 1));
2336 if (!newoperator)
2337 return NULL;
2338
2339 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002340 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002341 c->c_arena);
2342 if (!result)
2343 return NULL;
2344
2345 nops = (NCH(n) - 1) / 2;
2346 for (i = 1; i < nops; i++) {
2347 expr_ty tmp_result, tmp;
2348 const node* next_oper = CHILD(n, i * 2 + 1);
2349
2350 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002351 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 return NULL;
2353
Guido van Rossumd8faa362007-04-27 19:54:29 +00002354 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2355 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 return NULL;
2357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002359 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002360 CHILD(n, i * 2 + 2)->n_end_lineno,
2361 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002362 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002364 return NULL;
2365 result = tmp_result;
2366 }
2367 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368}
2369
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002370static expr_ty
2371ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 subscriptlist: subscript (',' subscript)* [',']
2375 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2376 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002377 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002378 REQ(n, trailer);
2379 if (TYPE(CHILD(n, 0)) == LPAR) {
2380 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002381 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2382 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002383 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002384 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002386 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002387 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2388 if (!attr_id)
2389 return NULL;
2390 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002391 LINENO(n), n->n_col_offset,
2392 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002393 }
2394 else {
2395 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002396 REQ(CHILD(n, 2), RSQB);
2397 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002399 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2400 if (!slc)
2401 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002402 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002403 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002404 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002405 }
2406 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002408 by treating the sequence as a tuple literal if there are
2409 no slice features.
2410 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002411 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002412 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002413 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002414 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002415 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002416 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002417 if (!slices)
2418 return NULL;
2419 for (j = 0; j < NCH(n); j += 2) {
2420 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002421 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002422 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002423 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002424 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002425 asdl_seq_SET(slices, j / 2, slc);
2426 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002427 if (!simple) {
2428 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002429 Load, LINENO(n), n->n_col_offset,
2430 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002431 }
2432 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002433 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002434 if (!elts)
2435 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002436 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2437 slc = (slice_ty)asdl_seq_GET(slices, j);
2438 assert(slc->kind == Index_kind && slc->v.Index.value);
2439 asdl_seq_SET(elts, j, slc->v.Index.value);
2440 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002441 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2442 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002443 if (!e)
2444 return NULL;
2445 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002446 Load, LINENO(n), n->n_col_offset,
2447 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002448 }
2449 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002450}
2451
2452static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002453ast_for_factor(struct compiling *c, const node *n)
2454{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002455 expr_ty expression;
2456
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002457 expression = ast_for_expr(c, CHILD(n, 1));
2458 if (!expression)
2459 return NULL;
2460
2461 switch (TYPE(CHILD(n, 0))) {
2462 case PLUS:
2463 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002464 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002465 c->c_arena);
2466 case MINUS:
2467 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002468 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002469 c->c_arena);
2470 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002471 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2472 n->n_end_lineno, n->n_end_col_offset,
2473 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002474 }
2475 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2476 TYPE(CHILD(n, 0)));
2477 return NULL;
2478}
2479
2480static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002481ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002482{
Yury Selivanov75445082015-05-11 22:57:16 -04002483 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002484 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002485
2486 REQ(n, atom_expr);
2487 nch = NCH(n);
2488
Jelle Zijlstraac317702017-10-05 20:24:46 -07002489 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002490 start = 1;
2491 assert(nch > 1);
2492 }
2493
2494 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002495 if (!e)
2496 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002497 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002498 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002499 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002500 return Await(e, LINENO(n), n->n_col_offset,
2501 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002502 }
2503
2504 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002505 node *ch = CHILD(n, i);
2506 if (TYPE(ch) != trailer)
2507 break;
2508 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002509 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002510 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002511 tmp->lineno = e->lineno;
2512 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002513 e = tmp;
2514 }
Yury Selivanov75445082015-05-11 22:57:16 -04002515
2516 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002517 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002518 return Await(e, LINENO(n), n->n_col_offset,
2519 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002520 }
2521 else {
2522 return e;
2523 }
2524}
2525
2526static expr_ty
2527ast_for_power(struct compiling *c, const node *n)
2528{
2529 /* power: atom trailer* ('**' factor)*
2530 */
2531 expr_ty e;
2532 REQ(n, power);
2533 e = ast_for_atom_expr(c, CHILD(n, 0));
2534 if (!e)
2535 return NULL;
2536 if (NCH(n) == 1)
2537 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002538 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2539 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002540 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002541 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002542 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2543 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002544 }
2545 return e;
2546}
2547
Guido van Rossum0368b722007-05-11 16:50:42 +00002548static expr_ty
2549ast_for_starred(struct compiling *c, const node *n)
2550{
2551 expr_ty tmp;
2552 REQ(n, star_expr);
2553
2554 tmp = ast_for_expr(c, CHILD(n, 1));
2555 if (!tmp)
2556 return NULL;
2557
2558 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002559 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2560 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002561}
2562
2563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564/* Do not name a variable 'expr'! Will cause a compile error.
2565*/
2566
2567static expr_ty
2568ast_for_expr(struct compiling *c, const node *n)
2569{
2570 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002571 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002572 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 and_test: not_test ('and' not_test)*
2575 not_test: 'not' not_test | comparison
2576 comparison: expr (comp_op expr)*
2577 expr: xor_expr ('|' xor_expr)*
2578 xor_expr: and_expr ('^' and_expr)*
2579 and_expr: shift_expr ('&' shift_expr)*
2580 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2581 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002582 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002584 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002585 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002586 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 */
2588
2589 asdl_seq *seq;
2590 int i;
2591
2592 loop:
2593 switch (TYPE(n)) {
2594 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002595 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002596 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002597 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002599 else if (NCH(n) > 1)
2600 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002601 /* Fallthrough */
2602 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 case and_test:
2604 if (NCH(n) == 1) {
2605 n = CHILD(n, 0);
2606 goto loop;
2607 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002608 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 if (!seq)
2610 return NULL;
2611 for (i = 0; i < NCH(n); i += 2) {
2612 expr_ty e = ast_for_expr(c, CHILD(n, i));
2613 if (!e)
2614 return NULL;
2615 asdl_seq_SET(seq, i / 2, e);
2616 }
2617 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002618 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002619 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002620 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002621 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002622 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2623 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 case not_test:
2625 if (NCH(n) == 1) {
2626 n = CHILD(n, 0);
2627 goto loop;
2628 }
2629 else {
2630 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2631 if (!expression)
2632 return NULL;
2633
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002634 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002635 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002636 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 }
2638 case comparison:
2639 if (NCH(n) == 1) {
2640 n = CHILD(n, 0);
2641 goto loop;
2642 }
2643 else {
2644 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002645 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002646 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002647 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 if (!ops)
2649 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002650 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return NULL;
2653 }
2654 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002655 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002657 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002658 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002660 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661
2662 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002663 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002665 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002667 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 asdl_seq_SET(cmps, i / 2, expression);
2669 }
2670 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002671 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002675 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2676 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 }
2678 break;
2679
Guido van Rossum0368b722007-05-11 16:50:42 +00002680 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 /* The next five cases all handle BinOps. The main body of code
2683 is the same in each case, but the switch turned inside out to
2684 reuse the code for each type of operator.
2685 */
2686 case expr:
2687 case xor_expr:
2688 case and_expr:
2689 case shift_expr:
2690 case arith_expr:
2691 case term:
2692 if (NCH(n) == 1) {
2693 n = CHILD(n, 0);
2694 goto loop;
2695 }
2696 return ast_for_binop(c, n);
2697 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002698 node *an = NULL;
2699 node *en = NULL;
2700 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002701 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002702 if (NCH(n) > 1)
2703 an = CHILD(n, 1); /* yield_arg */
2704 if (an) {
2705 en = CHILD(an, NCH(an) - 1);
2706 if (NCH(an) == 2) {
2707 is_from = 1;
2708 exp = ast_for_expr(c, en);
2709 }
2710 else
2711 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002712 if (!exp)
2713 return NULL;
2714 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002715 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002716 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2717 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2718 return Yield(exp, LINENO(n), n->n_col_offset,
2719 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002720 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002721 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 if (NCH(n) == 1) {
2723 n = CHILD(n, 0);
2724 goto loop;
2725 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002727 case power:
2728 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002730 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 return NULL;
2732 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002733 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 return NULL;
2735}
2736
2737static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002738ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002739 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740{
2741 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002742 arglist: argument (',' argument)* [',']
2743 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744 */
2745
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002746 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002747 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002748 asdl_seq *args;
2749 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750
2751 REQ(n, arglist);
2752
2753 nargs = 0;
2754 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002756 node *ch = CHILD(n, i);
2757 if (TYPE(ch) == argument) {
2758 if (NCH(ch) == 1)
2759 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002760 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2761 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002762 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002763 ast_error(c, ch, "invalid syntax");
2764 return NULL;
2765 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002766 if (NCH(n) > 1) {
2767 ast_error(c, ch, "Generator expression must be parenthesized");
2768 return NULL;
2769 }
2770 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002771 else if (TYPE(CHILD(ch, 0)) == STAR)
2772 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002775 nkeywords++;
2776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002779 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002781 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002782 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002784 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002785
2786 nargs = 0; /* positional arguments + iterable argument unpackings */
2787 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2788 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002790 node *ch = CHILD(n, i);
2791 if (TYPE(ch) == argument) {
2792 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002793 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002794 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002795 /* a positional argument */
2796 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002797 if (ndoublestars) {
2798 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002799 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002802 else {
2803 ast_error(c, chch,
2804 "positional argument follows "
2805 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002806 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002807 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002808 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002809 e = ast_for_expr(c, chch);
2810 if (!e)
2811 return NULL;
2812 asdl_seq_SET(args, nargs++, e);
2813 }
2814 else if (TYPE(chch) == STAR) {
2815 /* an iterable argument unpacking */
2816 expr_ty starred;
2817 if (ndoublestars) {
2818 ast_error(c, chch,
2819 "iterable argument unpacking follows "
2820 "keyword argument unpacking");
2821 return NULL;
2822 }
2823 e = ast_for_expr(c, CHILD(ch, 1));
2824 if (!e)
2825 return NULL;
2826 starred = Starred(e, Load, LINENO(chch),
2827 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002828 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002829 c->c_arena);
2830 if (!starred)
2831 return NULL;
2832 asdl_seq_SET(args, nargs++, starred);
2833
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 }
2835 else if (TYPE(chch) == DOUBLESTAR) {
2836 /* a keyword argument unpacking */
2837 keyword_ty kw;
2838 i++;
2839 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002841 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 kw = keyword(NULL, e, c->c_arena);
2843 asdl_seq_SET(keywords, nkeywords++, kw);
2844 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002846 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002847 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002848 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002850 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002851 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002853 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002854 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002856 identifier key, tmp;
2857 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002859 // To remain LL(1), the grammar accepts any test (basically, any
2860 // expression) in the keyword slot of a call site. So, we need
2861 // to manually enforce that the keyword is a NAME here.
2862 static const int name_tree[] = {
2863 test,
2864 or_test,
2865 and_test,
2866 not_test,
2867 comparison,
2868 expr,
2869 xor_expr,
2870 and_expr,
2871 shift_expr,
2872 arith_expr,
2873 term,
2874 factor,
2875 power,
2876 atom_expr,
2877 atom,
2878 0,
2879 };
2880 node *expr_node = chch;
2881 for (int i = 0; name_tree[i]; i++) {
2882 if (TYPE(expr_node) != name_tree[i])
2883 break;
2884 if (NCH(expr_node) != 1)
2885 break;
2886 expr_node = CHILD(expr_node, 0);
2887 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002888 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002889 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002890 "expression cannot contain assignment, "
2891 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002892 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002893 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002894 key = new_identifier(STR(expr_node), c);
2895 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002896 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002898 if (forbidden_name(c, key, chch, 1)) {
2899 return NULL;
2900 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002901 for (k = 0; k < nkeywords; k++) {
2902 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002903 if (tmp && !PyUnicode_Compare(tmp, key)) {
2904 ast_error(c, chch,
2905 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002906 return NULL;
2907 }
2908 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002911 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002914 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 asdl_seq_SET(keywords, nkeywords++, kw);
2916 }
2917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 }
2919
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002920 return Call(func, args, keywords, func->lineno, func->col_offset,
2921 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922}
2923
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002925ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002927 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002928 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002930 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002931 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002932 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002933 }
2934 else {
2935 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002936 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002939 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 else {
2941 asdl_seq *tmp = seq_for_testlist(c, n);
2942 if (!tmp)
2943 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002944 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
2945 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002947}
2948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949static stmt_ty
2950ast_for_expr_stmt(struct compiling *c, const node *n)
2951{
2952 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002953 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2954 ('=' (yield_expr|testlist_star_expr))*)
2955 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002956 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002957 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002958 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002959 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 */
2961
2962 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002963 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 if (!e)
2965 return NULL;
2966
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002967 return Expr(e, LINENO(n), n->n_col_offset,
2968 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 }
2970 else if (TYPE(CHILD(n, 1)) == augassign) {
2971 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002972 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002973 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974
Thomas Wouters89f507f2006-12-13 04:49:30 +00002975 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 if (!expr1)
2977 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002978 if(!set_context(c, expr1, Store, ch))
2979 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002980 /* set_context checks that most expressions are not the left side.
2981 Augmented assignments can only have a name, a subscript, or an
2982 attribute on the left, though, so we have to explicitly check for
2983 those. */
2984 switch (expr1->kind) {
2985 case Name_kind:
2986 case Attribute_kind:
2987 case Subscript_kind:
2988 break;
2989 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002990 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002991 return NULL;
2992 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993
Thomas Wouters89f507f2006-12-13 04:49:30 +00002994 ch = CHILD(n, 2);
2995 if (TYPE(ch) == testlist)
2996 expr2 = ast_for_testlist(c, ch);
2997 else
2998 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002999 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 return NULL;
3001
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003002 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003003 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 return NULL;
3005
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003006 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3007 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003009 else if (TYPE(CHILD(n, 1)) == annassign) {
3010 expr_ty expr1, expr2, expr3;
3011 node *ch = CHILD(n, 0);
3012 node *deep, *ann = CHILD(n, 1);
3013 int simple = 1;
3014
3015 /* we keep track of parens to qualify (x) as expression not name */
3016 deep = ch;
3017 while (NCH(deep) == 1) {
3018 deep = CHILD(deep, 0);
3019 }
3020 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3021 simple = 0;
3022 }
3023 expr1 = ast_for_testlist(c, ch);
3024 if (!expr1) {
3025 return NULL;
3026 }
3027 switch (expr1->kind) {
3028 case Name_kind:
3029 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3030 return NULL;
3031 }
3032 expr1->v.Name.ctx = Store;
3033 break;
3034 case Attribute_kind:
3035 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3036 return NULL;
3037 }
3038 expr1->v.Attribute.ctx = Store;
3039 break;
3040 case Subscript_kind:
3041 expr1->v.Subscript.ctx = Store;
3042 break;
3043 case List_kind:
3044 ast_error(c, ch,
3045 "only single target (not list) can be annotated");
3046 return NULL;
3047 case Tuple_kind:
3048 ast_error(c, ch,
3049 "only single target (not tuple) can be annotated");
3050 return NULL;
3051 default:
3052 ast_error(c, ch,
3053 "illegal target for annotation");
3054 return NULL;
3055 }
3056
3057 if (expr1->kind != Name_kind) {
3058 simple = 0;
3059 }
3060 ch = CHILD(ann, 1);
3061 expr2 = ast_for_expr(c, ch);
3062 if (!expr2) {
3063 return NULL;
3064 }
3065 if (NCH(ann) == 2) {
3066 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003067 LINENO(n), n->n_col_offset,
3068 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003069 }
3070 else {
3071 ch = CHILD(ann, 3);
3072 expr3 = ast_for_expr(c, ch);
3073 if (!expr3) {
3074 return NULL;
3075 }
3076 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003077 LINENO(n), n->n_col_offset,
3078 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003079 }
3080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003082 int i;
3083 asdl_seq *targets;
3084 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 expr_ty expression;
3086
Thomas Wouters89f507f2006-12-13 04:49:30 +00003087 /* a normal assignment */
3088 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003089 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003090 if (!targets)
3091 return NULL;
3092 for (i = 0; i < NCH(n) - 2; i += 2) {
3093 expr_ty e;
3094 node *ch = CHILD(n, i);
3095 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003096 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003097 return NULL;
3098 }
3099 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003101 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003103 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003104 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003105 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106
Thomas Wouters89f507f2006-12-13 04:49:30 +00003107 asdl_seq_SET(targets, i / 2, e);
3108 }
3109 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003110 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003111 expression = ast_for_testlist(c, value);
3112 else
3113 expression = ast_for_expr(c, value);
3114 if (!expression)
3115 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003116 return Assign(targets, expression, LINENO(n), n->n_col_offset,
3117 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119}
3120
Benjamin Peterson78565b22009-06-28 19:19:51 +00003121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003123ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124{
3125 asdl_seq *seq;
3126 int i;
3127 expr_ty e;
3128
3129 REQ(n, exprlist);
3130
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003131 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003133 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003135 e = ast_for_expr(c, CHILD(n, i));
3136 if (!e)
3137 return NULL;
3138 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003139 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 }
3142 return seq;
3143}
3144
3145static stmt_ty
3146ast_for_del_stmt(struct compiling *c, const node *n)
3147{
3148 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 /* del_stmt: 'del' exprlist */
3151 REQ(n, del_stmt);
3152
3153 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3154 if (!expr_list)
3155 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003156 return Delete(expr_list, LINENO(n), n->n_col_offset,
3157 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158}
3159
3160static stmt_ty
3161ast_for_flow_stmt(struct compiling *c, const node *n)
3162{
3163 /*
3164 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3165 | yield_stmt
3166 break_stmt: 'break'
3167 continue_stmt: 'continue'
3168 return_stmt: 'return' [testlist]
3169 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003170 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 raise_stmt: 'raise' [test [',' test [',' test]]]
3172 */
3173 node *ch;
3174
3175 REQ(n, flow_stmt);
3176 ch = CHILD(n, 0);
3177 switch (TYPE(ch)) {
3178 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003179 return Break(LINENO(n), n->n_col_offset,
3180 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003182 return Continue(LINENO(n), n->n_col_offset,
3183 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003185 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3186 if (!exp)
3187 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003188 return Expr(exp, LINENO(n), n->n_col_offset,
3189 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 }
3191 case return_stmt:
3192 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003193 return Return(NULL, LINENO(n), n->n_col_offset,
3194 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003196 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 if (!expression)
3198 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003199 return Return(expression, LINENO(n), n->n_col_offset,
3200 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 }
3202 case raise_stmt:
3203 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003204 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3205 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003206 else if (NCH(ch) >= 2) {
3207 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3209 if (!expression)
3210 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003211 if (NCH(ch) == 4) {
3212 cause = ast_for_expr(c, CHILD(ch, 3));
3213 if (!cause)
3214 return NULL;
3215 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003216 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3217 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 }
Stefan Krahf432a322017-08-21 13:09:59 +02003219 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003221 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 "unexpected flow_stmt: %d", TYPE(ch));
3223 return NULL;
3224 }
3225}
3226
3227static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003228alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229{
3230 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003231 import_as_name: NAME ['as' NAME]
3232 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 dotted_name: NAME ('.' NAME)*
3234 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003235 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 loop:
3238 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003239 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003240 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003241 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003242 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003243 if (!name)
3244 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003245 if (NCH(n) == 3) {
3246 node *str_node = CHILD(n, 2);
3247 str = NEW_IDENTIFIER(str_node);
3248 if (!str)
3249 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003250 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003251 return NULL;
3252 }
3253 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003254 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003255 return NULL;
3256 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003257 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003258 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 case dotted_as_name:
3260 if (NCH(n) == 1) {
3261 n = CHILD(n, 0);
3262 goto loop;
3263 }
3264 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003265 node *asname_node = CHILD(n, 2);
3266 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003267 if (!a)
3268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003270 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003271 if (!a->asname)
3272 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003273 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 return a;
3276 }
3277 break;
3278 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003279 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003280 node *name_node = CHILD(n, 0);
3281 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003282 if (!name)
3283 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003284 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003285 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003286 return alias(name, NULL, c->c_arena);
3287 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 else {
3289 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003290 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003291 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294
3295 len = 0;
3296 for (i = 0; i < NCH(n); i += 2)
3297 /* length of string plus one for the dot */
3298 len += strlen(STR(CHILD(n, i))) + 1;
3299 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003300 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 if (!str)
3302 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003303 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 if (!s)
3305 return NULL;
3306 for (i = 0; i < NCH(n); i += 2) {
3307 char *sch = STR(CHILD(n, i));
3308 strcpy(s, STR(CHILD(n, i)));
3309 s += strlen(sch);
3310 *s++ = '.';
3311 }
3312 --s;
3313 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3315 PyBytes_GET_SIZE(str),
3316 NULL);
3317 Py_DECREF(str);
3318 if (!uni)
3319 return NULL;
3320 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003321 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003322 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3323 Py_DECREF(str);
3324 return NULL;
3325 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003326 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 }
3328 break;
3329 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003330 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003331 if (!str)
3332 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003333 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3334 Py_DECREF(str);
3335 return NULL;
3336 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003337 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003339 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 "unexpected import name: %d", TYPE(n));
3341 return NULL;
3342 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003343
3344 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 return NULL;
3346}
3347
3348static stmt_ty
3349ast_for_import_stmt(struct compiling *c, const node *n)
3350{
3351 /*
3352 import_stmt: import_name | import_from
3353 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003354 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3355 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003357 int lineno;
3358 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359 int i;
3360 asdl_seq *aliases;
3361
3362 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003363 lineno = LINENO(n);
3364 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003366 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003368 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003369 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003370 if (!aliases)
3371 return NULL;
3372 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003373 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003374 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003376 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003378 // Even though n is modified above, the end position is not changed
3379 return Import(aliases, lineno, col_offset,
3380 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003382 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003384 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003385 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003386 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003387 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003389 /* Count the number of dots (for relative imports) and check for the
3390 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003391 for (idx = 1; idx < NCH(n); idx++) {
3392 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003393 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3394 if (!mod)
3395 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003396 idx++;
3397 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003398 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003400 ndots += 3;
3401 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 } else if (TYPE(CHILD(n, idx)) != DOT) {
3403 break;
3404 }
3405 ndots++;
3406 }
3407 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003408 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003409 case STAR:
3410 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003411 n = CHILD(n, idx);
3412 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003413 break;
3414 case LPAR:
3415 /* from ... import (x, y, z) */
3416 n = CHILD(n, idx + 1);
3417 n_children = NCH(n);
3418 break;
3419 case import_as_names:
3420 /* from ... import x, y, z */
3421 n = CHILD(n, idx);
3422 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003423 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003424 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425 " surrounding parentheses");
3426 return NULL;
3427 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003428 break;
3429 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003430 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003431 return NULL;
3432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003434 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003435 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437
3438 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003439 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003440 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003441 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003443 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003445 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003446 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003447 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003448 if (!import_alias)
3449 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003450 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003451 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003453 if (mod != NULL)
3454 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003455 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003456 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003457 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 }
Neal Norwitz79792652005-11-14 04:25:03 +00003459 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 "unknown import statement: starts with command '%s'",
3461 STR(CHILD(n, 0)));
3462 return NULL;
3463}
3464
3465static stmt_ty
3466ast_for_global_stmt(struct compiling *c, const node *n)
3467{
3468 /* global_stmt: 'global' NAME (',' NAME)* */
3469 identifier name;
3470 asdl_seq *s;
3471 int i;
3472
3473 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003474 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 name = NEW_IDENTIFIER(CHILD(n, i));
3479 if (!name)
3480 return NULL;
3481 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003483 return Global(s, LINENO(n), n->n_col_offset,
3484 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485}
3486
3487static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003488ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3489{
3490 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3491 identifier name;
3492 asdl_seq *s;
3493 int i;
3494
3495 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003496 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003497 if (!s)
3498 return NULL;
3499 for (i = 1; i < NCH(n); i += 2) {
3500 name = NEW_IDENTIFIER(CHILD(n, i));
3501 if (!name)
3502 return NULL;
3503 asdl_seq_SET(s, i / 2, name);
3504 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003505 return Nonlocal(s, LINENO(n), n->n_col_offset,
3506 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003507}
3508
3509static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510ast_for_assert_stmt(struct compiling *c, const node *n)
3511{
3512 /* assert_stmt: 'assert' test [',' test] */
3513 REQ(n, assert_stmt);
3514 if (NCH(n) == 2) {
3515 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3516 if (!expression)
3517 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003518 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3519 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520 }
3521 else if (NCH(n) == 4) {
3522 expr_ty expr1, expr2;
3523
3524 expr1 = ast_for_expr(c, CHILD(n, 1));
3525 if (!expr1)
3526 return NULL;
3527 expr2 = ast_for_expr(c, CHILD(n, 3));
3528 if (!expr2)
3529 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003531 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3532 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533 }
Neal Norwitz79792652005-11-14 04:25:03 +00003534 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535 "improper number of parts to 'assert' statement: %d",
3536 NCH(n));
3537 return NULL;
3538}
3539
3540static asdl_seq *
3541ast_for_suite(struct compiling *c, const node *n)
3542{
3543 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003544 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 stmt_ty s;
3546 int i, total, num, end, pos = 0;
3547 node *ch;
3548
3549 REQ(n, suite);
3550
3551 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003552 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003554 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003556 n = CHILD(n, 0);
3557 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003559 */
3560 end = NCH(n) - 1;
3561 if (TYPE(CHILD(n, end - 1)) == SEMI)
3562 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003564 for (i = 0; i < end; i += 2) {
3565 ch = CHILD(n, i);
3566 s = ast_for_stmt(c, ch);
3567 if (!s)
3568 return NULL;
3569 asdl_seq_SET(seq, pos++, s);
3570 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 }
3572 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003573 for (i = 2; i < (NCH(n) - 1); i++) {
3574 ch = CHILD(n, i);
3575 REQ(ch, stmt);
3576 num = num_stmts(ch);
3577 if (num == 1) {
3578 /* small_stmt or compound_stmt with only one child */
3579 s = ast_for_stmt(c, ch);
3580 if (!s)
3581 return NULL;
3582 asdl_seq_SET(seq, pos++, s);
3583 }
3584 else {
3585 int j;
3586 ch = CHILD(ch, 0);
3587 REQ(ch, simple_stmt);
3588 for (j = 0; j < NCH(ch); j += 2) {
3589 /* statement terminates with a semi-colon ';' */
3590 if (NCH(CHILD(ch, j)) == 0) {
3591 assert((j + 1) == NCH(ch));
3592 break;
3593 }
3594 s = ast_for_stmt(c, CHILD(ch, j));
3595 if (!s)
3596 return NULL;
3597 asdl_seq_SET(seq, pos++, s);
3598 }
3599 }
3600 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 }
3602 assert(pos == seq->size);
3603 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604}
3605
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003606static void
3607get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3608{
3609 int tot = asdl_seq_LEN(s);
3610 // Suite should not be empty, but it is safe to just ignore it
3611 // if it will ever occur.
3612 if (!tot) {
3613 return;
3614 }
3615 stmt_ty last = asdl_seq_GET(s, tot - 1);
3616 *end_lineno = last->end_lineno;
3617 *end_col_offset = last->end_col_offset;
3618}
3619
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620static stmt_ty
3621ast_for_if_stmt(struct compiling *c, const node *n)
3622{
3623 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3624 ['else' ':' suite]
3625 */
3626 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003627 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628
3629 REQ(n, if_stmt);
3630
3631 if (NCH(n) == 4) {
3632 expr_ty expression;
3633 asdl_seq *suite_seq;
3634
3635 expression = ast_for_expr(c, CHILD(n, 1));
3636 if (!expression)
3637 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003639 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003641 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642
Guido van Rossumd8faa362007-04-27 19:54:29 +00003643 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003644 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003646
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 s = STR(CHILD(n, 4));
3648 /* s[2], the third character in the string, will be
3649 's' for el_s_e, or
3650 'i' for el_i_f
3651 */
3652 if (s[2] == 's') {
3653 expr_ty expression;
3654 asdl_seq *seq1, *seq2;
3655
3656 expression = ast_for_expr(c, CHILD(n, 1));
3657 if (!expression)
3658 return NULL;
3659 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003660 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 return NULL;
3662 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003663 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003665 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666
Guido van Rossumd8faa362007-04-27 19:54:29 +00003667 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003668 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 }
3670 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003671 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003672 expr_ty expression;
3673 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003674 asdl_seq *orelse = NULL;
3675 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 /* must reference the child n_elif+1 since 'else' token is third,
3677 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003678 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3679 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3680 has_else = 1;
3681 n_elif -= 3;
3682 }
3683 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684
Thomas Wouters89f507f2006-12-13 04:49:30 +00003685 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003686 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003688 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003689 if (!orelse)
3690 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003692 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003694 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3695 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003697 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3698 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003700 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 asdl_seq_SET(orelse, 0,
3703 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003704 LINENO(CHILD(n, NCH(n) - 6)),
3705 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003706 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003707 /* the just-created orelse handled the last elif */
3708 n_elif--;
3709 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710
Thomas Wouters89f507f2006-12-13 04:49:30 +00003711 for (i = 0; i < n_elif; i++) {
3712 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003713 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003714 if (!newobj)
3715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003717 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003720 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003723 if (orelse != NULL) {
3724 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3725 } else {
3726 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3727 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003728 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003730 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003731 CHILD(n, off)->n_col_offset,
3732 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003733 orelse = newobj;
3734 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003735 expression = ast_for_expr(c, CHILD(n, 1));
3736 if (!expression)
3737 return NULL;
3738 suite_seq = ast_for_suite(c, CHILD(n, 3));
3739 if (!suite_seq)
3740 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003741 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003742 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003743 LINENO(n), n->n_col_offset,
3744 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003746
3747 PyErr_Format(PyExc_SystemError,
3748 "unexpected token in 'if' statement: %s", s);
3749 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750}
3751
3752static stmt_ty
3753ast_for_while_stmt(struct compiling *c, const node *n)
3754{
3755 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3756 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003757 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758
3759 if (NCH(n) == 4) {
3760 expr_ty expression;
3761 asdl_seq *suite_seq;
3762
3763 expression = ast_for_expr(c, CHILD(n, 1));
3764 if (!expression)
3765 return NULL;
3766 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003767 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003769 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3770 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3771 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 }
3773 else if (NCH(n) == 7) {
3774 expr_ty expression;
3775 asdl_seq *seq1, *seq2;
3776
3777 expression = ast_for_expr(c, CHILD(n, 1));
3778 if (!expression)
3779 return NULL;
3780 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003781 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 return NULL;
3783 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003784 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003786 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003788 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3789 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003791
3792 PyErr_Format(PyExc_SystemError,
3793 "wrong number of tokens for 'while' statement: %d",
3794 NCH(n));
3795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796}
3797
3798static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003799ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800{
guoci90fc8982018-09-11 17:45:45 -04003801 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003802 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003804 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003805 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003806 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3808 REQ(n, for_stmt);
3809
3810 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003811 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 if (!seq)
3813 return NULL;
3814 }
3815
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003816 node_target = CHILD(n, 1);
3817 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003818 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003820 /* Check the # of children rather than the length of _target, since
3821 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003822 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003823 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003824 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003826 target = Tuple(_target, Store, first->lineno, first->col_offset,
3827 node_target->n_end_lineno, node_target->n_end_col_offset,
3828 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003830 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003831 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 return NULL;
3833 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003834 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 return NULL;
3836
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003837 if (seq != NULL) {
3838 get_last_end_pos(seq, &end_lineno, &end_col_offset);
3839 } else {
3840 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3841 }
Yury Selivanov75445082015-05-11 22:57:16 -04003842 if (is_async)
3843 return AsyncFor(target, expression, suite_seq, seq,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003844 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003845 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04003846 else
3847 return For(target, expression, suite_seq, seq,
3848 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003849 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850}
3851
3852static excepthandler_ty
3853ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3854{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003855 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003856 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 REQ(exc, except_clause);
3858 REQ(body, suite);
3859
3860 if (NCH(exc) == 1) {
3861 asdl_seq *suite_seq = ast_for_suite(c, body);
3862 if (!suite_seq)
3863 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003864 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865
Neal Norwitzad74aa82008-03-31 05:14:30 +00003866 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003867 exc->n_col_offset,
3868 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003869 }
3870 else if (NCH(exc) == 2) {
3871 expr_ty expression;
3872 asdl_seq *suite_seq;
3873
3874 expression = ast_for_expr(c, CHILD(exc, 1));
3875 if (!expression)
3876 return NULL;
3877 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003878 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003880 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881
Neal Norwitzad74aa82008-03-31 05:14:30 +00003882 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003883 exc->n_col_offset,
3884 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885 }
3886 else if (NCH(exc) == 4) {
3887 asdl_seq *suite_seq;
3888 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003889 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003890 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003892 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003895 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896 return NULL;
3897 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003898 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003900 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901
Neal Norwitzad74aa82008-03-31 05:14:30 +00003902 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003903 exc->n_col_offset,
3904 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003906
3907 PyErr_Format(PyExc_SystemError,
3908 "wrong number of children for 'except' clause: %d",
3909 NCH(exc));
3910 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911}
3912
3913static stmt_ty
3914ast_for_try_stmt(struct compiling *c, const node *n)
3915{
Neal Norwitzf599f422005-12-17 21:33:47 +00003916 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003917 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003918 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003919 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00003920
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921 REQ(n, try_stmt);
3922
Neal Norwitzf599f422005-12-17 21:33:47 +00003923 body = ast_for_suite(c, CHILD(n, 2));
3924 if (body == NULL)
3925 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926
Neal Norwitzf599f422005-12-17 21:33:47 +00003927 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3928 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3929 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3930 /* we can assume it's an "else",
3931 because nch >= 9 for try-else-finally and
3932 it would otherwise have a type of except_clause */
3933 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3934 if (orelse == NULL)
3935 return NULL;
3936 n_except--;
3937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938
Neal Norwitzf599f422005-12-17 21:33:47 +00003939 finally = ast_for_suite(c, CHILD(n, nch - 1));
3940 if (finally == NULL)
3941 return NULL;
3942 n_except--;
3943 }
3944 else {
3945 /* we can assume it's an "else",
3946 otherwise it would have a type of except_clause */
3947 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3948 if (orelse == NULL)
3949 return NULL;
3950 n_except--;
3951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003953 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003954 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 return NULL;
3956 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957
Neal Norwitzf599f422005-12-17 21:33:47 +00003958 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003959 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003960 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003961 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003962 if (handlers == NULL)
3963 return NULL;
3964
3965 for (i = 0; i < n_except; i++) {
3966 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3967 CHILD(n, 5 + i * 3));
3968 if (!e)
3969 return NULL;
3970 asdl_seq_SET(handlers, i, e);
3971 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003972 }
3973
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003974 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003975 if (finally != NULL) {
3976 // finally is always last
3977 get_last_end_pos(finally, &end_lineno, &end_col_offset);
3978 } else if (orelse != NULL) {
3979 // otherwise else is last
3980 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3981 } else {
3982 // inline the get_last_end_pos logic due to layout mismatch
3983 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
3984 end_lineno = last_handler->end_lineno;
3985 end_col_offset = last_handler->end_col_offset;
3986 }
3987 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
3988 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989}
3990
Georg Brandl0c315622009-05-25 21:10:36 +00003991/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003992static withitem_ty
3993ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003994{
3995 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003996
Georg Brandl0c315622009-05-25 21:10:36 +00003997 REQ(n, with_item);
3998 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003999 if (!context_expr)
4000 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004001 if (NCH(n) == 3) {
4002 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004003
4004 if (!optional_vars) {
4005 return NULL;
4006 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004007 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004008 return NULL;
4009 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004010 }
4011
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004012 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004013}
4014
Georg Brandl0c315622009-05-25 21:10:36 +00004015/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
4016static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004017ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004018{
guoci90fc8982018-09-11 17:45:45 -04004019 const node * const n = is_async ? CHILD(n0, 1) : n0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004020 int i, n_items, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004021 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00004022
4023 REQ(n, with_stmt);
4024
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004025 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004026 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004027 if (!items)
4028 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004029 for (i = 1; i < NCH(n) - 2; i += 2) {
4030 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4031 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004032 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004033 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004034 }
4035
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004036 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4037 if (!body)
4038 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004039 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004040
Yury Selivanov75445082015-05-11 22:57:16 -04004041 if (is_async)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004042 return AsyncWith(items, body, LINENO(n0), n0->n_col_offset,
4043 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004044 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004045 return With(items, body, LINENO(n), n->n_col_offset,
4046 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004047}
4048
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004050ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004052 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004053 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004054 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004055 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004056 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004057
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004058 REQ(n, classdef);
4059
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004060 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004061 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 if (!s)
4063 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004064 get_last_end_pos(s, &end_lineno, &end_col_offset);
4065
Benjamin Peterson30760062008-11-25 04:02:28 +00004066 classname = NEW_IDENTIFIER(CHILD(n, 1));
4067 if (!classname)
4068 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004069 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004070 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004071 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004072 LINENO(n), n->n_col_offset,
4073 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004075
4076 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004077 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004078 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004079 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004080 get_last_end_pos(s, &end_lineno, &end_col_offset);
4081
Benjamin Peterson30760062008-11-25 04:02:28 +00004082 classname = NEW_IDENTIFIER(CHILD(n, 1));
4083 if (!classname)
4084 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004085 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004086 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004087 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004088 LINENO(n), n->n_col_offset,
4089 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090 }
4091
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004092 /* class NAME '(' arglist ')' ':' suite */
4093 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004094 {
4095 PyObject *dummy_name;
4096 expr_ty dummy;
4097 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4098 if (!dummy_name)
4099 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004100 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4101 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4102 c->c_arena);
4103 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004104 if (!call)
4105 return NULL;
4106 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004107 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004108 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004110 get_last_end_pos(s, &end_lineno, &end_col_offset);
4111
Benjamin Peterson30760062008-11-25 04:02:28 +00004112 classname = NEW_IDENTIFIER(CHILD(n, 1));
4113 if (!classname)
4114 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004115 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004116 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004117
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004118 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004119 decorator_seq, LINENO(n), n->n_col_offset,
4120 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121}
4122
4123static stmt_ty
4124ast_for_stmt(struct compiling *c, const node *n)
4125{
4126 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004127 assert(NCH(n) == 1);
4128 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004129 }
4130 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004131 assert(num_stmts(n) == 1);
4132 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 }
4134 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004135 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004136 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4137 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004138 */
4139 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140 case expr_stmt:
4141 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142 case del_stmt:
4143 return ast_for_del_stmt(c, n);
4144 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004145 return Pass(LINENO(n), n->n_col_offset,
4146 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004147 case flow_stmt:
4148 return ast_for_flow_stmt(c, n);
4149 case import_stmt:
4150 return ast_for_import_stmt(c, n);
4151 case global_stmt:
4152 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004153 case nonlocal_stmt:
4154 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155 case assert_stmt:
4156 return ast_for_assert_stmt(c, n);
4157 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004158 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4160 TYPE(n), NCH(n));
4161 return NULL;
4162 }
4163 }
4164 else {
4165 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004166 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004167 */
4168 node *ch = CHILD(n, 0);
4169 REQ(n, compound_stmt);
4170 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171 case if_stmt:
4172 return ast_for_if_stmt(c, ch);
4173 case while_stmt:
4174 return ast_for_while_stmt(c, ch);
4175 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004176 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004177 case try_stmt:
4178 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004179 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004180 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004182 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004184 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 case decorated:
4186 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004187 case async_stmt:
4188 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004190 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004191 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 TYPE(n), NCH(n));
4193 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 }
4196}
4197
4198static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004199parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004200{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004201 const char *end;
4202 long x;
4203 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004204 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004205 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004206
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004207 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004208 errno = 0;
4209 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004210 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004211 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004212 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004213 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004214 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004215 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004216 }
4217 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004218 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004219 if (*end == '\0') {
4220 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004221 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004222 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004223 }
4224 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004225 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004226 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004227 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4228 if (compl.imag == -1.0 && PyErr_Occurred())
4229 return NULL;
4230 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004231 }
4232 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004233 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004234 dx = PyOS_string_to_double(s, NULL, NULL);
4235 if (dx == -1.0 && PyErr_Occurred())
4236 return NULL;
4237 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004239}
4240
4241static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004242parsenumber(struct compiling *c, const char *s)
4243{
4244 char *dup, *end;
4245 PyObject *res = NULL;
4246
4247 assert(s != NULL);
4248
4249 if (strchr(s, '_') == NULL) {
4250 return parsenumber_raw(c, s);
4251 }
4252 /* Create a duplicate without underscores. */
4253 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004254 if (dup == NULL) {
4255 return PyErr_NoMemory();
4256 }
Brett Cannona721aba2016-09-09 14:57:09 -07004257 end = dup;
4258 for (; *s; s++) {
4259 if (*s != '_') {
4260 *end++ = *s;
4261 }
4262 }
4263 *end = '\0';
4264 res = parsenumber_raw(c, dup);
4265 PyMem_Free(dup);
4266 return res;
4267}
4268
4269static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004270decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004271{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004272 const char *s, *t;
4273 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004274 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4275 while (s < end && (*s & 0x80)) s++;
4276 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004277 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004278}
4279
Eric V. Smith56466482016-10-31 14:46:26 -04004280static int
4281warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004282 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004283{
4284 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4285 first_invalid_escape_char);
4286 if (msg == NULL) {
4287 return -1;
4288 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004289 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004290 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004291 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004292 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004293 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004294 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004295 to get a more accurate error report */
4296 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004297 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004298 }
4299 Py_DECREF(msg);
4300 return -1;
4301 }
4302 Py_DECREF(msg);
4303 return 0;
4304}
4305
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004306static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004307decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4308 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004309{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004310 PyObject *v, *u;
4311 char *buf;
4312 char *p;
4313 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004314
Benjamin Peterson202803a2016-02-25 22:34:45 -08004315 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004316 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004317 return NULL;
4318 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4319 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4320 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4321 if (u == NULL)
4322 return NULL;
4323 p = buf = PyBytes_AsString(u);
4324 end = s + len;
4325 while (s < end) {
4326 if (*s == '\\') {
4327 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004328 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004329 strcpy(p, "u005c");
4330 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004331 if (s >= end)
4332 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004333 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004334 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004335 if (*s & 0x80) { /* XXX inefficient */
4336 PyObject *w;
4337 int kind;
4338 void *data;
4339 Py_ssize_t len, i;
4340 w = decode_utf8(c, &s, end);
4341 if (w == NULL) {
4342 Py_DECREF(u);
4343 return NULL;
4344 }
4345 kind = PyUnicode_KIND(w);
4346 data = PyUnicode_DATA(w);
4347 len = PyUnicode_GET_LENGTH(w);
4348 for (i = 0; i < len; i++) {
4349 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4350 sprintf(p, "\\U%08x", chr);
4351 p += 10;
4352 }
4353 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004354 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004355 Py_DECREF(w);
4356 } else {
4357 *p++ = *s++;
4358 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004359 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004360 len = p - buf;
4361 s = buf;
4362
Eric V. Smith56466482016-10-31 14:46:26 -04004363 const char *first_invalid_escape;
4364 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4365
4366 if (v != NULL && first_invalid_escape != NULL) {
4367 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4368 /* We have not decref u before because first_invalid_escape points
4369 inside u. */
4370 Py_XDECREF(u);
4371 Py_DECREF(v);
4372 return NULL;
4373 }
4374 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004375 Py_XDECREF(u);
4376 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004377}
4378
Eric V. Smith56466482016-10-31 14:46:26 -04004379static PyObject *
4380decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4381 size_t len)
4382{
4383 const char *first_invalid_escape;
4384 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4385 &first_invalid_escape);
4386 if (result == NULL)
4387 return NULL;
4388
4389 if (first_invalid_escape != NULL) {
4390 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4391 Py_DECREF(result);
4392 return NULL;
4393 }
4394 }
4395 return result;
4396}
4397
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004398/* Shift locations for the given node and all its children by adding `lineno`
4399 and `col_offset` to existing locations. */
4400static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4401{
4402 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004403 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004404 for (int i = 0; i < NCH(n); ++i) {
4405 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4406 /* Shifting column offsets unnecessary if there's been newlines. */
4407 col_offset = 0;
4408 }
4409 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4410 }
4411 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004412 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004413}
4414
4415/* Fix locations for the given node and its children.
4416
4417 `parent` is the enclosing node.
4418 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004419 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004420*/
4421static void
4422fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4423{
4424 char *substr = NULL;
4425 char *start;
4426 int lines = LINENO(parent) - 1;
4427 int cols = parent->n_col_offset;
4428 /* Find the full fstring to fix location information in `n`. */
4429 while (parent && parent->n_type != STRING)
4430 parent = parent->n_child;
4431 if (parent && parent->n_str) {
4432 substr = strstr(parent->n_str, expr_str);
4433 if (substr) {
4434 start = substr;
4435 while (start > parent->n_str) {
4436 if (start[0] == '\n')
4437 break;
4438 start--;
4439 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004440 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004441 /* adjust the start based on the number of newlines encountered
4442 before the f-string expression */
4443 for (char* p = parent->n_str; p < substr; p++) {
4444 if (*p == '\n') {
4445 lines++;
4446 }
4447 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004448 }
4449 }
4450 fstring_shift_node_locations(n, lines, cols);
4451}
4452
Eric V. Smith451d0e32016-09-09 21:56:20 -04004453/* Compile this expression in to an expr_ty. Add parens around the
4454 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004455static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004456fstring_compile_expr(const char *expr_start, const char *expr_end,
4457 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004458
Eric V. Smith235a6f02015-09-19 14:51:32 -04004459{
4460 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004461 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004462 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004463 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004464 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004465 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004466
Eric V. Smith1d44c412015-09-23 07:49:00 -04004467 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004468 assert(*(expr_start-1) == '{');
4469 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004470
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004471 /* If the substring is all whitespace, it's an error. We need to catch this
4472 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4473 because turning the expression '' in to '()' would go from being invalid
4474 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004475 for (s = expr_start; s != expr_end; s++) {
4476 char c = *s;
4477 /* The Python parser ignores only the following whitespace
4478 characters (\r already is converted to \n). */
4479 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004480 break;
4481 }
4482 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004483 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004484 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004485 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004486 }
4487
Eric V. Smith451d0e32016-09-09 21:56:20 -04004488 len = expr_end - expr_start;
4489 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4490 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004491 if (str == NULL) {
4492 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004493 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004494 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004495
Eric V. Smith451d0e32016-09-09 21:56:20 -04004496 str[0] = '(';
4497 memcpy(str+1, expr_start, len);
4498 str[len+1] = ')';
4499 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004500
4501 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004502 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4503 Py_eval_input, 0);
4504 if (!mod_n) {
4505 PyMem_RawFree(str);
4506 return NULL;
4507 }
4508 /* Reuse str to find the correct column offset. */
4509 str[0] = '{';
4510 str[len+1] = '}';
4511 fstring_fix_node_location(n, mod_n, str);
4512 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004513 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004514 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004515 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004516 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004517 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004518}
4519
4520/* Return -1 on error.
4521
4522 Return 0 if we reached the end of the literal.
4523
4524 Return 1 if we haven't reached the end of the literal, but we want
4525 the caller to process the literal up to this point. Used for
4526 doubled braces.
4527*/
4528static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004529fstring_find_literal(const char **str, const char *end, int raw,
4530 PyObject **literal, int recurse_lvl,
4531 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004532{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004533 /* Get any literal string. It ends when we hit an un-doubled left
4534 brace (which isn't part of a unicode name escape such as
4535 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004536
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004537 const char *s = *str;
4538 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004539 int result = 0;
4540
Eric V. Smith235a6f02015-09-19 14:51:32 -04004541 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004542 while (s < end) {
4543 char ch = *s++;
4544 if (!raw && ch == '\\' && s < end) {
4545 ch = *s++;
4546 if (ch == 'N') {
4547 if (s < end && *s++ == '{') {
4548 while (s < end && *s++ != '}') {
4549 }
4550 continue;
4551 }
4552 break;
4553 }
4554 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4555 return -1;
4556 }
4557 }
4558 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004559 /* Check for doubled braces, but only at the top level. If
4560 we checked at every level, then f'{0:{3}}' would fail
4561 with the two closing braces. */
4562 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004563 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004564 /* We're going to tell the caller that the literal ends
4565 here, but that they should continue scanning. But also
4566 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004567 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004568 result = 1;
4569 goto done;
4570 }
4571
4572 /* Where a single '{' is the start of a new expression, a
4573 single '}' is not allowed. */
4574 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004575 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004576 ast_error(c, n, "f-string: single '}' is not allowed");
4577 return -1;
4578 }
4579 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004580 /* We're either at a '{', which means we're starting another
4581 expression; or a '}', which means we're at the end of this
4582 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004583 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004584 break;
4585 }
4586 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004587 *str = s;
4588 assert(s <= end);
4589 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004590done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004591 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004592 if (raw)
4593 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004594 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004595 NULL, NULL);
4596 else
Eric V. Smith56466482016-10-31 14:46:26 -04004597 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004598 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004599 if (!*literal)
4600 return -1;
4601 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004602 return result;
4603}
4604
4605/* Forward declaration because parsing is recursive. */
4606static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004607fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004608 struct compiling *c, const node *n);
4609
Eric V. Smith451d0e32016-09-09 21:56:20 -04004610/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004611 expression (so it must be a '{'). Returns the FormattedValue node,
4612 which includes the expression, conversion character, and
4613 format_spec expression.
4614
4615 Note that I don't do a perfect job here: I don't make sure that a
4616 closing brace doesn't match an opening paren, for example. It
4617 doesn't need to error on all invalid expressions, just correctly
4618 find the end of all valid ones. Any errors inside the expression
4619 will be caught when we parse it later. */
4620static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004621fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004622 expr_ty *expression, struct compiling *c, const node *n)
4623{
4624 /* Return -1 on error, else 0. */
4625
Eric V. Smith451d0e32016-09-09 21:56:20 -04004626 const char *expr_start;
4627 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004628 expr_ty simple_expression;
4629 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004630 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004631
4632 /* 0 if we're not in a string, else the quote char we're trying to
4633 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004634 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004635
4636 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4637 int string_type = 0;
4638
4639 /* Keep track of nesting level for braces/parens/brackets in
4640 expressions. */
4641 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004642 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004643
4644 /* Can only nest one level deep. */
4645 if (recurse_lvl >= 2) {
4646 ast_error(c, n, "f-string: expressions nested too deeply");
4647 return -1;
4648 }
4649
4650 /* The first char must be a left brace, or we wouldn't have gotten
4651 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004652 assert(**str == '{');
4653 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004654
Eric V. Smith451d0e32016-09-09 21:56:20 -04004655 expr_start = *str;
4656 for (; *str < end; (*str)++) {
4657 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004658
4659 /* Loop invariants. */
4660 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004661 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004662 if (quote_char)
4663 assert(string_type == 1 || string_type == 3);
4664 else
4665 assert(string_type == 0);
4666
Eric V. Smith451d0e32016-09-09 21:56:20 -04004667 ch = **str;
4668 /* Nowhere inside an expression is a backslash allowed. */
4669 if (ch == '\\') {
4670 /* Error: can't include a backslash character, inside
4671 parens or strings or not. */
4672 ast_error(c, n, "f-string expression part "
4673 "cannot include a backslash");
4674 return -1;
4675 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004676 if (quote_char) {
4677 /* We're inside a string. See if we're at the end. */
4678 /* This code needs to implement the same non-error logic
4679 as tok_get from tokenizer.c, at the letter_quote
4680 label. To actually share that code would be a
4681 nightmare. But, it's unlikely to change and is small,
4682 so duplicate it here. Note we don't need to catch all
4683 of the errors, since they'll be caught when parsing the
4684 expression. We just need to match the non-error
4685 cases. Thus we can ignore \n in single-quoted strings,
4686 for example. Or non-terminated strings. */
4687 if (ch == quote_char) {
4688 /* Does this match the string_type (single or triple
4689 quoted)? */
4690 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004691 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004692 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004693 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004694 string_type = 0;
4695 quote_char = 0;
4696 continue;
4697 }
4698 } else {
4699 /* We're at the end of a normal string. */
4700 quote_char = 0;
4701 string_type = 0;
4702 continue;
4703 }
4704 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004705 } else if (ch == '\'' || ch == '"') {
4706 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004707 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004708 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004709 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004710 } else {
4711 /* Start of a normal string. */
4712 string_type = 1;
4713 }
4714 /* Start looking for the end of the string. */
4715 quote_char = ch;
4716 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004717 if (nested_depth >= MAXLEVEL) {
4718 ast_error(c, n, "f-string: too many nested parenthesis");
4719 return -1;
4720 }
4721 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004722 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004723 } else if (ch == '#') {
4724 /* Error: can't include a comment character, inside parens
4725 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004726 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004727 return -1;
4728 } else if (nested_depth == 0 &&
4729 (ch == '!' || ch == ':' || ch == '}')) {
4730 /* First, test for the special case of "!=". Since '=' is
4731 not an allowed conversion character, nothing is lost in
4732 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004733 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734 /* This isn't a conversion character, just continue. */
4735 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004736 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004737 /* Normal way out of this loop. */
4738 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004739 } else if (ch == ']' || ch == '}' || ch == ')') {
4740 if (!nested_depth) {
4741 ast_error(c, n, "f-string: unmatched '%c'", ch);
4742 return -1;
4743 }
4744 nested_depth--;
4745 int opening = parenstack[nested_depth];
4746 if (!((opening == '(' && ch == ')') ||
4747 (opening == '[' && ch == ']') ||
4748 (opening == '{' && ch == '}')))
4749 {
4750 ast_error(c, n,
4751 "f-string: closing parenthesis '%c' "
4752 "does not match opening parenthesis '%c'",
4753 ch, opening);
4754 return -1;
4755 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004756 } else {
4757 /* Just consume this char and loop around. */
4758 }
4759 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004760 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004761 /* If we leave this loop in a string or with mismatched parens, we
4762 don't care. We'll get a syntax error when compiling the
4763 expression. But, we can produce a better error message, so
4764 let's just do that.*/
4765 if (quote_char) {
4766 ast_error(c, n, "f-string: unterminated string");
4767 return -1;
4768 }
4769 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004770 int opening = parenstack[nested_depth - 1];
4771 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004772 return -1;
4773 }
4774
Eric V. Smith451d0e32016-09-09 21:56:20 -04004775 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004776 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004777
4778 /* Compile the expression as soon as possible, so we show errors
4779 related to the expression before errors related to the
4780 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004781 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004782 if (!simple_expression)
4783 return -1;
4784
4785 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004786 if (**str == '!') {
4787 *str += 1;
4788 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004789 goto unexpected_end_of_string;
4790
Eric V. Smith451d0e32016-09-09 21:56:20 -04004791 conversion = **str;
4792 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004793
4794 /* Validate the conversion. */
4795 if (!(conversion == 's' || conversion == 'r'
4796 || conversion == 'a')) {
4797 ast_error(c, n, "f-string: invalid conversion character: "
4798 "expected 's', 'r', or 'a'");
4799 return -1;
4800 }
4801 }
4802
4803 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004804 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004805 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004806 if (**str == ':') {
4807 *str += 1;
4808 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004809 goto unexpected_end_of_string;
4810
4811 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004812 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004813 if (!format_spec)
4814 return -1;
4815 }
4816
Eric V. Smith451d0e32016-09-09 21:56:20 -04004817 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004818 goto unexpected_end_of_string;
4819
4820 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004821 assert(*str < end);
4822 assert(**str == '}');
4823 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004824
Eric V. Smith451d0e32016-09-09 21:56:20 -04004825 /* And now create the FormattedValue node that represents this
4826 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004827 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004828 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004829 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004830 c->c_arena);
4831 if (!*expression)
4832 return -1;
4833
4834 return 0;
4835
4836unexpected_end_of_string:
4837 ast_error(c, n, "f-string: expecting '}'");
4838 return -1;
4839}
4840
4841/* Return -1 on error.
4842
4843 Return 0 if we have a literal (possible zero length) and an
4844 expression (zero length if at the end of the string.
4845
4846 Return 1 if we have a literal, but no expression, and we want the
4847 caller to call us again. This is used to deal with doubled
4848 braces.
4849
4850 When called multiple times on the string 'a{{b{0}c', this function
4851 will return:
4852
4853 1. the literal 'a{' with no expression, and a return value
4854 of 1. Despite the fact that there's no expression, the return
4855 value of 1 means we're not finished yet.
4856
4857 2. the literal 'b' and the expression '0', with a return value of
4858 0. The fact that there's an expression means we're not finished.
4859
4860 3. literal 'c' with no expression and a return value of 0. The
4861 combination of the return value of 0 with no expression means
4862 we're finished.
4863*/
4864static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004865fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4866 int recurse_lvl, PyObject **literal,
4867 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 struct compiling *c, const node *n)
4869{
4870 int result;
4871
4872 assert(*literal == NULL && *expression == NULL);
4873
4874 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004875 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876 if (result < 0)
4877 goto error;
4878
4879 assert(result == 0 || result == 1);
4880
4881 if (result == 1)
4882 /* We have a literal, but don't look at the expression. */
4883 return 1;
4884
Eric V. Smith451d0e32016-09-09 21:56:20 -04004885 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004886 /* We're at the end of the string or the end of a nested
4887 f-string: no expression. The top-level error case where we
4888 expect to be at the end of the string but we're at a '}' is
4889 handled later. */
4890 return 0;
4891
4892 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004893 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004894
Eric V. Smith451d0e32016-09-09 21:56:20 -04004895 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004896 goto error;
4897
4898 return 0;
4899
4900error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004901 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004902 return -1;
4903}
4904
4905#define EXPRLIST_N_CACHED 64
4906
4907typedef struct {
4908 /* Incrementally build an array of expr_ty, so be used in an
4909 asdl_seq. Cache some small but reasonably sized number of
4910 expr_ty's, and then after that start dynamically allocating,
4911 doubling the number allocated each time. Note that the f-string
4912 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004913 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914 fast as you add exressions in an f-string. */
4915
4916 Py_ssize_t allocated; /* Number we've allocated. */
4917 Py_ssize_t size; /* Number we've used. */
4918 expr_ty *p; /* Pointer to the memory we're actually
4919 using. Will point to 'data' until we
4920 start dynamically allocating. */
4921 expr_ty data[EXPRLIST_N_CACHED];
4922} ExprList;
4923
4924#ifdef NDEBUG
4925#define ExprList_check_invariants(l)
4926#else
4927static void
4928ExprList_check_invariants(ExprList *l)
4929{
4930 /* Check our invariants. Make sure this object is "live", and
4931 hasn't been deallocated. */
4932 assert(l->size >= 0);
4933 assert(l->p != NULL);
4934 if (l->size <= EXPRLIST_N_CACHED)
4935 assert(l->data == l->p);
4936}
4937#endif
4938
4939static void
4940ExprList_Init(ExprList *l)
4941{
4942 l->allocated = EXPRLIST_N_CACHED;
4943 l->size = 0;
4944
4945 /* Until we start allocating dynamically, p points to data. */
4946 l->p = l->data;
4947
4948 ExprList_check_invariants(l);
4949}
4950
4951static int
4952ExprList_Append(ExprList *l, expr_ty exp)
4953{
4954 ExprList_check_invariants(l);
4955 if (l->size >= l->allocated) {
4956 /* We need to alloc (or realloc) the memory. */
4957 Py_ssize_t new_size = l->allocated * 2;
4958
4959 /* See if we've ever allocated anything dynamically. */
4960 if (l->p == l->data) {
4961 Py_ssize_t i;
4962 /* We're still using the cached data. Switch to
4963 alloc-ing. */
4964 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4965 if (!l->p)
4966 return -1;
4967 /* Copy the cached data into the new buffer. */
4968 for (i = 0; i < l->size; i++)
4969 l->p[i] = l->data[i];
4970 } else {
4971 /* Just realloc. */
4972 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4973 if (!tmp) {
4974 PyMem_RawFree(l->p);
4975 l->p = NULL;
4976 return -1;
4977 }
4978 l->p = tmp;
4979 }
4980
4981 l->allocated = new_size;
4982 assert(l->allocated == 2 * l->size);
4983 }
4984
4985 l->p[l->size++] = exp;
4986
4987 ExprList_check_invariants(l);
4988 return 0;
4989}
4990
4991static void
4992ExprList_Dealloc(ExprList *l)
4993{
4994 ExprList_check_invariants(l);
4995
4996 /* If there's been an error, or we've never dynamically allocated,
4997 do nothing. */
4998 if (!l->p || l->p == l->data) {
4999 /* Do nothing. */
5000 } else {
5001 /* We have dynamically allocated. Free the memory. */
5002 PyMem_RawFree(l->p);
5003 }
5004 l->p = NULL;
5005 l->size = -1;
5006}
5007
5008static asdl_seq *
5009ExprList_Finish(ExprList *l, PyArena *arena)
5010{
5011 asdl_seq *seq;
5012
5013 ExprList_check_invariants(l);
5014
5015 /* Allocate the asdl_seq and copy the expressions in to it. */
5016 seq = _Py_asdl_seq_new(l->size, arena);
5017 if (seq) {
5018 Py_ssize_t i;
5019 for (i = 0; i < l->size; i++)
5020 asdl_seq_SET(seq, i, l->p[i]);
5021 }
5022 ExprList_Dealloc(l);
5023 return seq;
5024}
5025
5026/* The FstringParser is designed to add a mix of strings and
5027 f-strings, and concat them together as needed. Ultimately, it
5028 generates an expr_ty. */
5029typedef struct {
5030 PyObject *last_str;
5031 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005032 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005033} FstringParser;
5034
5035#ifdef NDEBUG
5036#define FstringParser_check_invariants(state)
5037#else
5038static void
5039FstringParser_check_invariants(FstringParser *state)
5040{
5041 if (state->last_str)
5042 assert(PyUnicode_CheckExact(state->last_str));
5043 ExprList_check_invariants(&state->expr_list);
5044}
5045#endif
5046
5047static void
5048FstringParser_Init(FstringParser *state)
5049{
5050 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005051 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052 ExprList_Init(&state->expr_list);
5053 FstringParser_check_invariants(state);
5054}
5055
5056static void
5057FstringParser_Dealloc(FstringParser *state)
5058{
5059 FstringParser_check_invariants(state);
5060
5061 Py_XDECREF(state->last_str);
5062 ExprList_Dealloc(&state->expr_list);
5063}
5064
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005065/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005066static expr_ty
5067make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5068{
5069 PyObject *s = *str;
5070 *str = NULL;
5071 assert(PyUnicode_CheckExact(s));
5072 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5073 Py_DECREF(s);
5074 return NULL;
5075 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005076 return Constant(s, LINENO(n), n->n_col_offset,
5077 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005078}
5079
5080/* Add a non-f-string (that is, a regular literal string). str is
5081 decref'd. */
5082static int
5083FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5084{
5085 FstringParser_check_invariants(state);
5086
5087 assert(PyUnicode_CheckExact(str));
5088
5089 if (PyUnicode_GET_LENGTH(str) == 0) {
5090 Py_DECREF(str);
5091 return 0;
5092 }
5093
5094 if (!state->last_str) {
5095 /* We didn't have a string before, so just remember this one. */
5096 state->last_str = str;
5097 } else {
5098 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005099 PyUnicode_AppendAndDel(&state->last_str, str);
5100 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 return -1;
5102 }
5103 FstringParser_check_invariants(state);
5104 return 0;
5105}
5106
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107/* Parse an f-string. The f-string is in *str to end, with no
5108 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110FstringParser_ConcatFstring(FstringParser *state, const char **str,
5111 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112 struct compiling *c, const node *n)
5113{
5114 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005115 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116
5117 /* Parse the f-string. */
5118 while (1) {
5119 PyObject *literal = NULL;
5120 expr_ty expression = NULL;
5121
5122 /* If there's a zero length literal in front of the
5123 expression, literal will be NULL. If we're at the end of
5124 the f-string, expression will be NULL (unless result == 1,
5125 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005126 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005127 &literal, &expression,
5128 c, n);
5129 if (result < 0)
5130 return -1;
5131
5132 /* Add the literal, if any. */
5133 if (!literal) {
5134 /* Do nothing. Just leave last_str alone (and possibly
5135 NULL). */
5136 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005137 /* Note that the literal can be zero length, if the
5138 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005139 state->last_str = literal;
5140 literal = NULL;
5141 } else {
5142 /* We have a literal, concatenate it. */
5143 assert(PyUnicode_GET_LENGTH(literal) != 0);
5144 if (FstringParser_ConcatAndDel(state, literal) < 0)
5145 return -1;
5146 literal = NULL;
5147 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005148
5149 /* We've dealt with the literal now. It can't be leaked on further
5150 errors. */
5151 assert(literal == NULL);
5152
5153 /* See if we should just loop around to get the next literal
5154 and expression, while ignoring the expression this
5155 time. This is used for un-doubling braces, as an
5156 optimization. */
5157 if (result == 1)
5158 continue;
5159
5160 if (!expression)
5161 /* We're done with this f-string. */
5162 break;
5163
5164 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005165 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005166 if (!state->last_str) {
5167 /* Do nothing. No previous literal. */
5168 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005169 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005170 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5171 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5172 return -1;
5173 }
5174
5175 if (ExprList_Append(&state->expr_list, expression) < 0)
5176 return -1;
5177 }
5178
Eric V. Smith235a6f02015-09-19 14:51:32 -04005179 /* If recurse_lvl is zero, then we must be at the end of the
5180 string. Otherwise, we must be at a right brace. */
5181
Eric V. Smith451d0e32016-09-09 21:56:20 -04005182 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005183 ast_error(c, n, "f-string: unexpected end of string");
5184 return -1;
5185 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005187 ast_error(c, n, "f-string: expecting '}'");
5188 return -1;
5189 }
5190
5191 FstringParser_check_invariants(state);
5192 return 0;
5193}
5194
5195/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005196 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005197static expr_ty
5198FstringParser_Finish(FstringParser *state, struct compiling *c,
5199 const node *n)
5200{
5201 asdl_seq *seq;
5202
5203 FstringParser_check_invariants(state);
5204
5205 /* If we're just a constant string with no expressions, return
5206 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005207 if (!state->fmode) {
5208 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209 if (!state->last_str) {
5210 /* Create a zero length string. */
5211 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5212 if (!state->last_str)
5213 goto error;
5214 }
5215 return make_str_node_and_del(&state->last_str, c, n);
5216 }
5217
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005218 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005219 last node in our expression list. */
5220 if (state->last_str) {
5221 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5222 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5223 goto error;
5224 }
5225 /* This has already been freed. */
5226 assert(state->last_str == NULL);
5227
5228 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5229 if (!seq)
5230 goto error;
5231
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005232 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5233 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005234
5235error:
5236 FstringParser_Dealloc(state);
5237 return NULL;
5238}
5239
Eric V. Smith451d0e32016-09-09 21:56:20 -04005240/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5241 at end, parse it into an expr_ty. Return NULL on error. Adjust
5242 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005243static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005244fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005245 struct compiling *c, const node *n)
5246{
5247 FstringParser state;
5248
5249 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005250 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005251 c, n) < 0) {
5252 FstringParser_Dealloc(&state);
5253 return NULL;
5254 }
5255
5256 return FstringParser_Finish(&state, c, n);
5257}
5258
5259/* n is a Python string literal, including the bracketing quote
5260 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005261 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005262 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005263 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5264 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005265*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005266static int
5267parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5268 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005269{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005270 size_t len;
5271 const char *s = STR(n);
5272 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005273 int fmode = 0;
5274 *bytesmode = 0;
5275 *rawmode = 0;
5276 *result = NULL;
5277 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005278 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005279 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005280 if (quote == 'b' || quote == 'B') {
5281 quote = *++s;
5282 *bytesmode = 1;
5283 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005284 else if (quote == 'u' || quote == 'U') {
5285 quote = *++s;
5286 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005287 else if (quote == 'r' || quote == 'R') {
5288 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005289 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005290 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005291 else if (quote == 'f' || quote == 'F') {
5292 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005293 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005294 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005295 else {
5296 break;
5297 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005298 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005299 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005300 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005301 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005302 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005303 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005304 if (quote != '\'' && quote != '\"') {
5305 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005306 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005307 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005308 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005309 s++;
5310 len = strlen(s);
5311 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005312 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005313 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005314 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005315 }
5316 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005317 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005318 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005319 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005320 }
5321 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005322 /* A triple quoted string. We've already skipped one quote at
5323 the start and one at the end of the string. Now skip the
5324 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005325 s += 2;
5326 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005327 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005328 if (s[--len] != quote || s[--len] != quote) {
5329 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005330 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005331 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005332 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005333
Eric V. Smith451d0e32016-09-09 21:56:20 -04005334 if (fmode) {
5335 /* Just return the bytes. The caller will parse the resulting
5336 string. */
5337 *fstr = s;
5338 *fstrlen = len;
5339 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005340 }
5341
Eric V. Smith451d0e32016-09-09 21:56:20 -04005342 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005343 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005344 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005345 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005346 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005347 const char *ch;
5348 for (ch = s; *ch; ch++) {
5349 if (Py_CHARMASK(*ch) >= 0x80) {
5350 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005351 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005352 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005353 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005354 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005355 if (*rawmode)
5356 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005357 else
Eric V. Smith56466482016-10-31 14:46:26 -04005358 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005359 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005360 if (*rawmode)
5361 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005362 else
Eric V. Smith56466482016-10-31 14:46:26 -04005363 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005364 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005365 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005366}
5367
Eric V. Smith235a6f02015-09-19 14:51:32 -04005368/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5369 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005370 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005371 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005372 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005373 node if there's just an f-string (with no leading or trailing
5374 literals), or a JoinedStr node if there are multiple f-strings or
5375 any literals involved. */
5376static expr_ty
5377parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005378{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005379 int bytesmode = 0;
5380 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005381 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005382
5383 FstringParser state;
5384 FstringParser_Init(&state);
5385
5386 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005387 int this_bytesmode;
5388 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005389 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005390 const char *fstr;
5391 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005392
5393 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005394 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5395 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005396 goto error;
5397
5398 /* Check that we're not mixing bytes with unicode. */
5399 if (i != 0 && bytesmode != this_bytesmode) {
5400 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005401 /* s is NULL if the current string part is an f-string. */
5402 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005403 goto error;
5404 }
5405 bytesmode = this_bytesmode;
5406
Eric V. Smith451d0e32016-09-09 21:56:20 -04005407 if (fstr != NULL) {
5408 int result;
5409 assert(s == NULL && !bytesmode);
5410 /* This is an f-string. Parse and concatenate it. */
5411 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5412 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005413 if (result < 0)
5414 goto error;
5415 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005416 /* A string or byte string. */
5417 assert(s != NULL && fstr == NULL);
5418
Eric V. Smith451d0e32016-09-09 21:56:20 -04005419 assert(bytesmode ? PyBytes_CheckExact(s) :
5420 PyUnicode_CheckExact(s));
5421
Eric V. Smith451d0e32016-09-09 21:56:20 -04005422 if (bytesmode) {
5423 /* For bytes, concat as we go. */
5424 if (i == 0) {
5425 /* First time, just remember this value. */
5426 bytes_str = s;
5427 } else {
5428 PyBytes_ConcatAndDel(&bytes_str, s);
5429 if (!bytes_str)
5430 goto error;
5431 }
5432 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005433 /* This is a regular string. Concatenate it. */
5434 if (FstringParser_ConcatAndDel(&state, s) < 0)
5435 goto error;
5436 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005437 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005438 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005439 if (bytesmode) {
5440 /* Just return the bytes object and we're done. */
5441 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5442 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005443 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5444 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005446
Eric V. Smith235a6f02015-09-19 14:51:32 -04005447 /* We're not a bytes string, bytes_str should never have been set. */
5448 assert(bytes_str == NULL);
5449
5450 return FstringParser_Finish(&state, c, n);
5451
5452error:
5453 Py_XDECREF(bytes_str);
5454 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005456}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005457
5458PyObject *
5459_PyAST_GetDocString(asdl_seq *body)
5460{
5461 if (!asdl_seq_LEN(body)) {
5462 return NULL;
5463 }
5464 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5465 if (st->kind != Expr_kind) {
5466 return NULL;
5467 }
5468 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005469 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5470 return e->v.Constant.value;
5471 }
5472 return NULL;
5473}