blob: 184e33b4b5064bdc03d16cbc6d9b006b9973888f [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
Benjamin Peterson832bfe22011-08-09 16:15:04 -050016static int validate_stmts(asdl_seq *);
17static int validate_exprs(asdl_seq *, expr_context_ty, int);
18static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
19static int validate_stmt(stmt_ty);
20static int validate_expr(expr_ty, expr_context_ty);
21
22static int
23validate_comprehension(asdl_seq *gens)
24{
25 int i;
26 if (!asdl_seq_LEN(gens)) {
27 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
28 return 0;
29 }
30 for (i = 0; i < asdl_seq_LEN(gens); i++) {
31 comprehension_ty comp = asdl_seq_GET(gens, i);
32 if (!validate_expr(comp->target, Store) ||
33 !validate_expr(comp->iter, Load) ||
34 !validate_exprs(comp->ifs, Load, 0))
35 return 0;
36 }
37 return 1;
38}
39
40static int
41validate_slice(slice_ty slice)
42{
43 switch (slice->kind) {
44 case Slice_kind:
45 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
46 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
47 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
48 case ExtSlice_kind: {
49 int i;
50 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
51 return 0;
52 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
53 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
54 return 0;
55 return 1;
56 }
57 case Index_kind:
58 return validate_expr(slice->v.Index.value, Load);
59 default:
60 PyErr_SetString(PyExc_SystemError, "unknown slice node");
61 return 0;
62 }
63}
64
65static int
66validate_keywords(asdl_seq *keywords)
67{
68 int i;
69 for (i = 0; i < asdl_seq_LEN(keywords); i++)
70 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
71 return 0;
72 return 1;
73}
74
75static int
76validate_args(asdl_seq *args)
77{
78 int i;
79 for (i = 0; i < asdl_seq_LEN(args); i++) {
80 arg_ty arg = asdl_seq_GET(args, i);
81 if (arg->annotation && !validate_expr(arg->annotation, Load))
82 return 0;
83 }
84 return 1;
85}
86
87static const char *
88expr_context_name(expr_context_ty ctx)
89{
90 switch (ctx) {
91 case Load:
92 return "Load";
93 case Store:
94 return "Store";
95 case Del:
96 return "Del";
97 case AugLoad:
98 return "AugLoad";
99 case AugStore:
100 return "AugStore";
101 case Param:
102 return "Param";
103 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700104 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500105 }
106}
107
108static int
109validate_arguments(arguments_ty args)
110{
111 if (!validate_args(args->args))
112 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700113 if (args->vararg && args->vararg->annotation
114 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500115 return 0;
116 }
117 if (!validate_args(args->kwonlyargs))
118 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100119 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700120 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500121 return 0;
122 }
123 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
124 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
125 return 0;
126 }
127 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
128 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
129 "kw_defaults on arguments");
130 return 0;
131 }
132 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
133}
134
135static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100136validate_constant(PyObject *value)
137{
138 if (value == Py_None || value == Py_Ellipsis)
139 return 1;
140
141 if (PyLong_CheckExact(value)
142 || PyFloat_CheckExact(value)
143 || PyComplex_CheckExact(value)
144 || PyBool_Check(value)
145 || PyUnicode_CheckExact(value)
146 || PyBytes_CheckExact(value))
147 return 1;
148
149 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
150 PyObject *it;
151
152 it = PyObject_GetIter(value);
153 if (it == NULL)
154 return 0;
155
156 while (1) {
157 PyObject *item = PyIter_Next(it);
158 if (item == NULL) {
159 if (PyErr_Occurred()) {
160 Py_DECREF(it);
161 return 0;
162 }
163 break;
164 }
165
166 if (!validate_constant(item)) {
167 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100168 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100169 return 0;
170 }
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 }
173
174 Py_DECREF(it);
175 return 1;
176 }
177
178 return 0;
179}
180
181static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500182validate_expr(expr_ty exp, expr_context_ty ctx)
183{
184 int check_ctx = 1;
185 expr_context_ty actual_ctx;
186
187 /* First check expression context. */
188 switch (exp->kind) {
189 case Attribute_kind:
190 actual_ctx = exp->v.Attribute.ctx;
191 break;
192 case Subscript_kind:
193 actual_ctx = exp->v.Subscript.ctx;
194 break;
195 case Starred_kind:
196 actual_ctx = exp->v.Starred.ctx;
197 break;
198 case Name_kind:
199 actual_ctx = exp->v.Name.ctx;
200 break;
201 case List_kind:
202 actual_ctx = exp->v.List.ctx;
203 break;
204 case Tuple_kind:
205 actual_ctx = exp->v.Tuple.ctx;
206 break;
207 default:
208 if (ctx != Load) {
209 PyErr_Format(PyExc_ValueError, "expression which can't be "
210 "assigned to in %s context", expr_context_name(ctx));
211 return 0;
212 }
213 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100214 /* set actual_ctx to prevent gcc warning */
215 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500216 }
217 if (check_ctx && actual_ctx != ctx) {
218 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
219 expr_context_name(ctx), expr_context_name(actual_ctx));
220 return 0;
221 }
222
223 /* Now validate expression. */
224 switch (exp->kind) {
225 case BoolOp_kind:
226 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
227 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
228 return 0;
229 }
230 return validate_exprs(exp->v.BoolOp.values, Load, 0);
231 case BinOp_kind:
232 return validate_expr(exp->v.BinOp.left, Load) &&
233 validate_expr(exp->v.BinOp.right, Load);
234 case UnaryOp_kind:
235 return validate_expr(exp->v.UnaryOp.operand, Load);
236 case Lambda_kind:
237 return validate_arguments(exp->v.Lambda.args) &&
238 validate_expr(exp->v.Lambda.body, Load);
239 case IfExp_kind:
240 return validate_expr(exp->v.IfExp.test, Load) &&
241 validate_expr(exp->v.IfExp.body, Load) &&
242 validate_expr(exp->v.IfExp.orelse, Load);
243 case Dict_kind:
244 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
245 PyErr_SetString(PyExc_ValueError,
246 "Dict doesn't have the same number of keys as values");
247 return 0;
248 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400249 /* null_ok=1 for keys expressions to allow dict unpacking to work in
250 dict literals, i.e. ``{**{a:b}}`` */
251 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
252 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500253 case Set_kind:
254 return validate_exprs(exp->v.Set.elts, Load, 0);
255#define COMP(NAME) \
256 case NAME ## _kind: \
257 return validate_comprehension(exp->v.NAME.generators) && \
258 validate_expr(exp->v.NAME.elt, Load);
259 COMP(ListComp)
260 COMP(SetComp)
261 COMP(GeneratorExp)
262#undef COMP
263 case DictComp_kind:
264 return validate_comprehension(exp->v.DictComp.generators) &&
265 validate_expr(exp->v.DictComp.key, Load) &&
266 validate_expr(exp->v.DictComp.value, Load);
267 case Yield_kind:
268 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500269 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000270 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400271 case Await_kind:
272 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500273 case Compare_kind:
274 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
275 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
276 return 0;
277 }
278 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
279 asdl_seq_LEN(exp->v.Compare.ops)) {
280 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
281 "of comparators and operands");
282 return 0;
283 }
284 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
285 validate_expr(exp->v.Compare.left, Load);
286 case Call_kind:
287 return validate_expr(exp->v.Call.func, Load) &&
288 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400289 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100290 case Constant_kind:
291 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100292 PyErr_Format(PyExc_TypeError,
293 "got an invalid type in Constant: %s",
294 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100295 return 0;
296 }
297 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400298 case JoinedStr_kind:
299 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
300 case FormattedValue_kind:
301 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
302 return 0;
303 if (exp->v.FormattedValue.format_spec)
304 return validate_expr(exp->v.FormattedValue.format_spec, Load);
305 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500306 case Attribute_kind:
307 return validate_expr(exp->v.Attribute.value, Load);
308 case Subscript_kind:
309 return validate_slice(exp->v.Subscript.slice) &&
310 validate_expr(exp->v.Subscript.value, Load);
311 case Starred_kind:
312 return validate_expr(exp->v.Starred.value, ctx);
313 case List_kind:
314 return validate_exprs(exp->v.List.elts, ctx, 0);
315 case Tuple_kind:
316 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300317 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500318 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500319 return 1;
320 default:
321 PyErr_SetString(PyExc_SystemError, "unexpected expression");
322 return 0;
323 }
324}
325
326static int
327validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
328{
329 if (asdl_seq_LEN(seq))
330 return 1;
331 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
332 return 0;
333}
334
335static int
336validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
337{
338 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
339 validate_exprs(targets, ctx, 0);
340}
341
342static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300343validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300345 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346}
347
348static int
349validate_stmt(stmt_ty stmt)
350{
351 int i;
352 switch (stmt->kind) {
353 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300354 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500355 validate_arguments(stmt->v.FunctionDef.args) &&
356 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
357 (!stmt->v.FunctionDef.returns ||
358 validate_expr(stmt->v.FunctionDef.returns, Load));
359 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300360 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500361 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
362 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400363 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500364 case Return_kind:
365 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
366 case Delete_kind:
367 return validate_assignlist(stmt->v.Delete.targets, Del);
368 case Assign_kind:
369 return validate_assignlist(stmt->v.Assign.targets, Store) &&
370 validate_expr(stmt->v.Assign.value, Load);
371 case AugAssign_kind:
372 return validate_expr(stmt->v.AugAssign.target, Store) &&
373 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700374 case AnnAssign_kind:
375 if (stmt->v.AnnAssign.target->kind != Name_kind &&
376 stmt->v.AnnAssign.simple) {
377 PyErr_SetString(PyExc_TypeError,
378 "AnnAssign with simple non-Name target");
379 return 0;
380 }
381 return validate_expr(stmt->v.AnnAssign.target, Store) &&
382 (!stmt->v.AnnAssign.value ||
383 validate_expr(stmt->v.AnnAssign.value, Load)) &&
384 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 case For_kind:
386 return validate_expr(stmt->v.For.target, Store) &&
387 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300388 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400390 case AsyncFor_kind:
391 return validate_expr(stmt->v.AsyncFor.target, Store) &&
392 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300393 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400394 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500395 case While_kind:
396 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500398 validate_stmts(stmt->v.While.orelse);
399 case If_kind:
400 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.If.orelse);
403 case With_kind:
404 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
405 return 0;
406 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
407 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
408 if (!validate_expr(item->context_expr, Load) ||
409 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
410 return 0;
411 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300412 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400413 case AsyncWith_kind:
414 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
415 return 0;
416 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
417 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
418 if (!validate_expr(item->context_expr, Load) ||
419 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
420 return 0;
421 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300422 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500423 case Raise_kind:
424 if (stmt->v.Raise.exc) {
425 return validate_expr(stmt->v.Raise.exc, Load) &&
426 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
427 }
428 if (stmt->v.Raise.cause) {
429 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
430 return 0;
431 }
432 return 1;
433 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300434 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500435 return 0;
436 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
437 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
438 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
439 return 0;
440 }
441 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
442 asdl_seq_LEN(stmt->v.Try.orelse)) {
443 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
444 return 0;
445 }
446 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
447 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
448 if ((handler->v.ExceptHandler.type &&
449 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300450 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500451 return 0;
452 }
453 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
454 validate_stmts(stmt->v.Try.finalbody)) &&
455 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
456 validate_stmts(stmt->v.Try.orelse));
457 case Assert_kind:
458 return validate_expr(stmt->v.Assert.test, Load) &&
459 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
460 case Import_kind:
461 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
462 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300463 if (stmt->v.ImportFrom.level < 0) {
464 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500465 return 0;
466 }
467 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
468 case Global_kind:
469 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
470 case Nonlocal_kind:
471 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
472 case Expr_kind:
473 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400474 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300475 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400476 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
477 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
478 (!stmt->v.AsyncFunctionDef.returns ||
479 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500480 case Pass_kind:
481 case Break_kind:
482 case Continue_kind:
483 return 1;
484 default:
485 PyErr_SetString(PyExc_SystemError, "unexpected statement");
486 return 0;
487 }
488}
489
490static int
491validate_stmts(asdl_seq *seq)
492{
493 int i;
494 for (i = 0; i < asdl_seq_LEN(seq); i++) {
495 stmt_ty stmt = asdl_seq_GET(seq, i);
496 if (stmt) {
497 if (!validate_stmt(stmt))
498 return 0;
499 }
500 else {
501 PyErr_SetString(PyExc_ValueError,
502 "None disallowed in statement list");
503 return 0;
504 }
505 }
506 return 1;
507}
508
509static int
510validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
511{
512 int i;
513 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
514 expr_ty expr = asdl_seq_GET(exprs, i);
515 if (expr) {
516 if (!validate_expr(expr, ctx))
517 return 0;
518 }
519 else if (!null_ok) {
520 PyErr_SetString(PyExc_ValueError,
521 "None disallowed in expression list");
522 return 0;
523 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100524
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500525 }
526 return 1;
527}
528
529int
530PyAST_Validate(mod_ty mod)
531{
532 int res = 0;
533
534 switch (mod->kind) {
535 case Module_kind:
536 res = validate_stmts(mod->v.Module.body);
537 break;
538 case Interactive_kind:
539 res = validate_stmts(mod->v.Interactive.body);
540 break;
541 case Expression_kind:
542 res = validate_expr(mod->v.Expression.body, Load);
543 break;
544 case Suite_kind:
545 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
546 break;
547 default:
548 PyErr_SetString(PyExc_SystemError, "impossible module node");
549 res = 0;
550 break;
551 }
552 return res;
553}
554
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500555/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500556#include "grammar.h"
557#include "parsetok.h"
558#include "graminit.h"
559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560/* Data structure used internally */
561struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400562 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200563 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500564 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565};
566
567static asdl_seq *seq_for_testlist(struct compiling *, const node *);
568static expr_ty ast_for_expr(struct compiling *, const node *);
569static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300570static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000571static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
572 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000573static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000574static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
guoci90fc8982018-09-11 17:45:45 -0400576static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
577static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579/* Note different signature for ast_for_call */
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200580static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000582static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400583static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Nick Coghlan650f0d02007-04-15 12:05:43 +0000585#define COMP_GENEXP 0
586#define COMP_LISTCOMP 1
587#define COMP_SETCOMP 2
588
Benjamin Peterson55e00432012-01-16 17:22:31 -0500589static int
590init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000591{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500592 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
593 if (!m)
594 return 0;
595 c->c_normalize = PyObject_GetAttrString(m, "normalize");
596 Py_DECREF(m);
597 if (!c->c_normalize)
598 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500599 return 1;
600}
601
602static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400603new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500604{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400605 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500606 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000607 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500608 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500609 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000610 /* Check whether there are non-ASCII characters in the
611 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500612 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200613 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300614 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500616 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500618 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300619 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
620 if (form == NULL) {
621 Py_DECREF(id);
622 return NULL;
623 }
624 PyObject *args[2] = {form, id};
625 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500626 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200627 if (!id2)
628 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300629 if (!PyUnicode_Check(id2)) {
630 PyErr_Format(PyExc_TypeError,
631 "unicodedata.normalize() must return a string, not "
632 "%.200s",
633 Py_TYPE(id2)->tp_name);
634 Py_DECREF(id2);
635 return NULL;
636 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200637 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000638 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000639 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200640 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
641 Py_DECREF(id);
642 return NULL;
643 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000644 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645}
646
Benjamin Peterson55e00432012-01-16 17:22:31 -0500647#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400650ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400652 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653
Victor Stinner14e461d2013-08-26 22:28:21 +0200654 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000656 Py_INCREF(Py_None);
657 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 }
Ammar Askar025eb982018-09-24 17:12:49 -0400659 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400660 if (!tmp)
661 return 0;
662 errstr = PyUnicode_FromString(errmsg);
663 if (!errstr) {
664 Py_DECREF(tmp);
665 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000666 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 Py_DECREF(errstr);
669 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400670 if (value) {
671 PyErr_SetObject(PyExc_SyntaxError, value);
672 Py_DECREF(value);
673 }
674 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675}
676
677/* num_stmts() returns number of contained statements.
678
679 Use this routine to determine how big a sequence is needed for
680 the statements in a parse tree. Its raison d'etre is this bit of
681 grammar:
682
683 stmt: simple_stmt | compound_stmt
684 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
685
686 A simple_stmt can contain multiple small_stmt elements joined
687 by semicolons. If the arg is a simple_stmt, the number of
688 small_stmt elements is returned.
689*/
690
691static int
692num_stmts(const node *n)
693{
694 int i, l;
695 node *ch;
696
697 switch (TYPE(n)) {
698 case single_input:
699 if (TYPE(CHILD(n, 0)) == NEWLINE)
700 return 0;
701 else
702 return num_stmts(CHILD(n, 0));
703 case file_input:
704 l = 0;
705 for (i = 0; i < NCH(n); i++) {
706 ch = CHILD(n, i);
707 if (TYPE(ch) == stmt)
708 l += num_stmts(ch);
709 }
710 return l;
711 case stmt:
712 return num_stmts(CHILD(n, 0));
713 case compound_stmt:
714 return 1;
715 case simple_stmt:
716 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
717 case suite:
718 if (NCH(n) == 1)
719 return num_stmts(CHILD(n, 0));
720 else {
721 l = 0;
722 for (i = 2; i < (NCH(n) - 1); i++)
723 l += num_stmts(CHILD(n, i));
724 return l;
725 }
726 default: {
727 char buf[128];
728
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000729 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 TYPE(n), NCH(n));
731 Py_FatalError(buf);
732 }
733 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700734 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735}
736
737/* Transform the CST rooted at node * to the appropriate AST
738*/
739
740mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200741PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
742 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000744 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 asdl_seq *stmts = NULL;
746 stmt_ty s;
747 node *ch;
748 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500749 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400751 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200752 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400753 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800754 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800755
756 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Jeremy Hyltona8293132006-02-28 17:58:27 +0000759 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 switch (TYPE(n)) {
761 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200762 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500764 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 for (i = 0; i < NCH(n) - 1; i++) {
766 ch = CHILD(n, i);
767 if (TYPE(ch) == NEWLINE)
768 continue;
769 REQ(ch, stmt);
770 num = num_stmts(ch);
771 if (num == 1) {
772 s = ast_for_stmt(&c, ch);
773 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500774 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000775 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 }
777 else {
778 ch = CHILD(ch, 0);
779 REQ(ch, simple_stmt);
780 for (j = 0; j < num; j++) {
781 s = ast_for_stmt(&c, CHILD(ch, j * 2));
782 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000784 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 }
786 }
787 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300788 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500789 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 case eval_input: {
791 expr_ty testlist_ast;
792
Nick Coghlan650f0d02007-04-15 12:05:43 +0000793 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000794 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500796 goto out;
797 res = Expression(testlist_ast, arena);
798 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 }
800 case single_input:
801 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200802 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500804 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
806 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000807 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
809 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 else {
812 n = CHILD(n, 0);
813 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200814 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500816 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000818 s = ast_for_stmt(&c, n);
819 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500820 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 asdl_seq_SET(stmts, 0, s);
822 }
823 else {
824 /* Only a simple_stmt can contain multiple statements. */
825 REQ(n, simple_stmt);
826 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 if (TYPE(CHILD(n, i)) == NEWLINE)
828 break;
829 s = ast_for_stmt(&c, CHILD(n, i));
830 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 asdl_seq_SET(stmts, i / 2, s);
833 }
834 }
835
Benjamin Peterson55e00432012-01-16 17:22:31 -0500836 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500838 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000840 PyErr_Format(PyExc_SystemError,
841 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500844 out:
845 if (c.c_normalize) {
846 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500848 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849}
850
Victor Stinner14e461d2013-08-26 22:28:21 +0200851mod_ty
852PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
853 PyArena *arena)
854{
855 mod_ty mod;
856 PyObject *filename;
857 filename = PyUnicode_DecodeFSDefault(filename_str);
858 if (filename == NULL)
859 return NULL;
860 mod = PyAST_FromNodeObject(n, flags, filename, arena);
861 Py_DECREF(filename);
862 return mod;
863
864}
865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
867*/
868
869static operator_ty
870get_operator(const node *n)
871{
872 switch (TYPE(n)) {
873 case VBAR:
874 return BitOr;
875 case CIRCUMFLEX:
876 return BitXor;
877 case AMPER:
878 return BitAnd;
879 case LEFTSHIFT:
880 return LShift;
881 case RIGHTSHIFT:
882 return RShift;
883 case PLUS:
884 return Add;
885 case MINUS:
886 return Sub;
887 case STAR:
888 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400889 case AT:
890 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 case SLASH:
892 return Div;
893 case DOUBLESLASH:
894 return FloorDiv;
895 case PERCENT:
896 return Mod;
897 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000898 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899 }
900}
901
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200902static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000903 "None",
904 "True",
905 "False",
906 NULL,
907};
908
909static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400910forbidden_name(struct compiling *c, identifier name, const node *n,
911 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000912{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000913 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200914 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400915 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000916 return 1;
917 }
918 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200919 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000920 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200921 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400922 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000923 return 1;
924 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000925 }
926 }
927 return 0;
928}
929
Jeremy Hyltona8293132006-02-28 17:58:27 +0000930/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931
932 Only sets context for expr kinds that "can appear in assignment context"
933 (according to ../Parser/Python.asdl). For other expr kinds, it sets
934 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935*/
936
937static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000938set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939{
940 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000941 /* If a particular expression type can't be used for assign / delete,
942 set expr_name to its name and an error message will be generated.
943 */
944 const char* expr_name = NULL;
945
946 /* The ast defines augmented store and load contexts, but the
947 implementation here doesn't actually use them. The code may be
948 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000949 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000950 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000951 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000952 */
953 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
955 switch (e->kind) {
956 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000957 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400958 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000959 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000960 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000962 e->v.Subscript.ctx = ctx;
963 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000964 case Starred_kind:
965 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000966 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000967 return 0;
968 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000970 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500971 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000972 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000973 }
974 e->v.Name.ctx = ctx;
975 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000977 e->v.List.ctx = ctx;
978 s = e->v.List.elts;
979 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +0300981 e->v.Tuple.ctx = ctx;
982 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000983 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000984 case Lambda_kind:
985 expr_name = "lambda";
986 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000988 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000989 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000990 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000992 case UnaryOp_kind:
993 expr_name = "operator";
994 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000996 expr_name = "generator expression";
997 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000998 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500999 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001000 expr_name = "yield expression";
1001 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001002 case Await_kind:
1003 expr_name = "await expression";
1004 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 case ListComp_kind:
1006 expr_name = "list comprehension";
1007 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001008 case SetComp_kind:
1009 expr_name = "set comprehension";
1010 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001011 case DictComp_kind:
1012 expr_name = "dict comprehension";
1013 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001014 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001015 case Set_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001016 case JoinedStr_kind:
1017 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001018 expr_name = "literal";
1019 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001020 case Constant_kind: {
1021 PyObject *value = e->v.Constant.value;
1022 if (value == Py_None || value == Py_False || value == Py_True) {
1023 expr_name = "keyword";
1024 }
1025 else if (value == Py_Ellipsis) {
1026 expr_name = "Ellipsis";
1027 }
1028 else {
1029 expr_name = "literal";
1030 }
Benjamin Peterson442f2092012-12-06 17:41:04 -05001031 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001032 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001033 case Compare_kind:
1034 expr_name = "comparison";
1035 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001036 case IfExp_kind:
1037 expr_name = "conditional expression";
1038 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001039 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyErr_Format(PyExc_SystemError,
1041 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001042 e->kind, e->lineno);
1043 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001045 /* Check for error string set by switch */
1046 if (expr_name) {
1047 char buf[300];
1048 PyOS_snprintf(buf, sizeof(buf),
1049 "can't %s %s",
1050 ctx == Store ? "assign to" : "delete",
1051 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001052 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001053 }
1054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 */
1058 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001059 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060
Thomas Wouters89f507f2006-12-13 04:49:30 +00001061 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001062 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001063 return 0;
1064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 }
1066 return 1;
1067}
1068
1069static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001070ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071{
1072 REQ(n, augassign);
1073 n = CHILD(n, 0);
1074 switch (STR(n)[0]) {
1075 case '+':
1076 return Add;
1077 case '-':
1078 return Sub;
1079 case '/':
1080 if (STR(n)[1] == '/')
1081 return FloorDiv;
1082 else
1083 return Div;
1084 case '%':
1085 return Mod;
1086 case '<':
1087 return LShift;
1088 case '>':
1089 return RShift;
1090 case '&':
1091 return BitAnd;
1092 case '^':
1093 return BitXor;
1094 case '|':
1095 return BitOr;
1096 case '*':
1097 if (STR(n)[1] == '*')
1098 return Pow;
1099 else
1100 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001101 case '@':
1102 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001104 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001105 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 }
1107}
1108
1109static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001110ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001112 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 |'is' 'not'
1114 */
1115 REQ(n, comp_op);
1116 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001117 n = CHILD(n, 0);
1118 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 case LESS:
1120 return Lt;
1121 case GREATER:
1122 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001123 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return Eq;
1125 case LESSEQUAL:
1126 return LtE;
1127 case GREATEREQUAL:
1128 return GtE;
1129 case NOTEQUAL:
1130 return NotEq;
1131 case NAME:
1132 if (strcmp(STR(n), "in") == 0)
1133 return In;
1134 if (strcmp(STR(n), "is") == 0)
1135 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001136 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001138 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001140 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 }
1143 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001144 /* handle "not in" and "is not" */
1145 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 case NAME:
1147 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1148 return NotIn;
1149 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1150 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001151 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001153 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 }
Neal Norwitz79792652005-11-14 04:25:03 +00001158 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001160 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161}
1162
1163static asdl_seq *
1164seq_for_testlist(struct compiling *c, const node *n)
1165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001167 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1168 */
Armin Rigo31441302005-10-21 12:57:31 +00001169 asdl_seq *seq;
1170 expr_ty expression;
1171 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001172 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001174 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 if (!seq)
1176 return NULL;
1177
1178 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001180 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181
Benjamin Peterson4905e802009-09-27 02:43:28 +00001182 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185
1186 assert(i / 2 < seq->size);
1187 asdl_seq_SET(seq, i / 2, expression);
1188 }
1189 return seq;
1190}
1191
Neal Norwitzc1505362006-12-28 06:47:50 +00001192static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001193ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001194{
1195 identifier name;
1196 expr_ty annotation = NULL;
1197 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001198 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001199
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001200 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001201 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001202 name = NEW_IDENTIFIER(ch);
1203 if (!name)
1204 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001205 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001206 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001207
1208 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1209 annotation = ast_for_expr(c, CHILD(n, 2));
1210 if (!annotation)
1211 return NULL;
1212 }
1213
Victor Stinnerc106c682015-11-06 17:01:48 +01001214 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001215 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001216 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001217 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218}
1219
Guido van Rossum4f72a782006-10-27 23:31:49 +00001220/* returns -1 if failed to handle keyword only arguments
1221 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001222 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001223 ^^^
1224 start pointing here
1225 */
1226static int
1227handle_keywordonly_args(struct compiling *c, const node *n, int start,
1228 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1229{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001230 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001231 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001232 expr_ty expression, annotation;
1233 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001234 int i = start;
1235 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001236
1237 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001238 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001239 return -1;
1240 }
1241 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001242 while (i < NCH(n)) {
1243 ch = CHILD(n, i);
1244 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001245 case vfpdef:
1246 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001247 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001248 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001249 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001250 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251 asdl_seq_SET(kwdefaults, j, expression);
1252 i += 2; /* '=' and test */
1253 }
1254 else { /* setting NULL if no default value exists */
1255 asdl_seq_SET(kwdefaults, j, NULL);
1256 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001257 if (NCH(ch) == 3) {
1258 /* ch is NAME ':' test */
1259 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001260 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001261 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001262 }
1263 else {
1264 annotation = NULL;
1265 }
1266 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001267 argname = NEW_IDENTIFIER(ch);
1268 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001269 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001270 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001271 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001272 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1273 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001274 if (!arg)
1275 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001276 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277 i += 2; /* the name and the comma */
1278 break;
1279 case DOUBLESTAR:
1280 return i;
1281 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001282 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 goto error;
1284 }
1285 }
1286 return i;
1287 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001289}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290
Jeremy Hyltona8293132006-02-28 17:58:27 +00001291/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292
1293static arguments_ty
1294ast_for_arguments(struct compiling *c, const node *n)
1295{
Neal Norwitzc1505362006-12-28 06:47:50 +00001296 /* This function handles both typedargslist (function definition)
1297 and varargslist (lambda definition).
1298
1299 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001300 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1301 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1302 | '**' tfpdef [',']]]
1303 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1304 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001305 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001306 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1307 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1308 | '**' vfpdef [',']]]
1309 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1310 | '**' vfpdef [',']
1311 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001312 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001313
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001315 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1316 int nposdefaults = 0, found_default = 0;
1317 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001318 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001319 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 node *ch;
1321
1322 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001323 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001324 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001327 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328
Jeremy Hyltone921e022008-07-17 16:37:17 +00001329 /* First count the number of positional args & defaults. The
1330 variable i is the loop index for this for loop and the next.
1331 The next loop picks up where the first leaves off.
1332 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 ch = CHILD(n, i);
1335 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001336 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001337 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001338 if (i < NCH(n) && /* skip argument following star */
1339 (TYPE(CHILD(n, i)) == tfpdef ||
1340 TYPE(CHILD(n, i)) == vfpdef)) {
1341 i++;
1342 }
1343 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001345 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001346 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001350 defaults for keyword only args */
1351 for ( ; i < NCH(n); ++i) {
1352 ch = CHILD(n, i);
1353 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001356 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001358 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001360 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001362 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001364 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001366 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 since we set NULL as default for keyword only argument w/o default
1369 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001371 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375 /* tfpdef: NAME [':' test]
1376 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 */
1378 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001379 j = 0; /* index for defaults */
1380 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 ch = CHILD(n, i);
1383 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 case tfpdef:
1385 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1387 anything other than EQUAL or a comma? */
1388 /* XXX Should NCH(n) check be made a separate check? */
1389 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001390 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1391 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001392 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 assert(posdefaults != NULL);
1394 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001399 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001401 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001403 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001404 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001405 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001406 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 i += 2; /* the name and the comma */
1408 break;
1409 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001410 if (i+1 >= NCH(n) ||
1411 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001412 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001413 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001414 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001416 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001417 if (TYPE(ch) == COMMA) {
1418 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 i += 2; /* now follows keyword only arguments */
1420 res = handle_keywordonly_args(c, n, i,
1421 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001422 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 i = res; /* res has new position to process */
1424 }
1425 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001426 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001427 if (!vararg)
1428 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001429
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001431 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1432 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001433 int res = 0;
1434 res = handle_keywordonly_args(c, n, i,
1435 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001436 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437 i = res; /* res has new position to process */
1438 }
1439 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 break;
1441 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001442 ch = CHILD(n, i+1); /* tfpdef */
1443 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001444 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001445 if (!kwarg)
1446 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 i += 3;
1448 break;
1449 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001450 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 "unexpected node in varargslist: %d @ %d",
1452 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001453 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001456 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457}
1458
1459static expr_ty
1460ast_for_dotted_name(struct compiling *c, const node *n)
1461{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001462 expr_ty e;
1463 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001464 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 int i;
1466
1467 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468
1469 lineno = LINENO(n);
1470 col_offset = n->n_col_offset;
1471
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 id = NEW_IDENTIFIER(CHILD(n, 0));
1473 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001474 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001475 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001477 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478
1479 for (i = 2; i < NCH(n); i+=2) {
1480 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001481 if (!id)
1482 return NULL;
1483 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1484 if (!e)
1485 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 }
1487
1488 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489}
1490
1491static expr_ty
1492ast_for_decorator(struct compiling *c, const node *n)
1493{
1494 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1495 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001496 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001499 REQ(CHILD(n, 0), AT);
1500 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1503 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001504 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507 d = name_expr;
1508 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 }
1510 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001511 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001512 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001513 if (!d)
1514 return NULL;
1515 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 }
1517 else {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02001518 d = ast_for_call(c, CHILD(n, 3), name_expr, true);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001519 if (!d)
1520 return NULL;
1521 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 }
1523
1524 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527static asdl_seq*
1528ast_for_decorators(struct compiling *c, const node *n)
1529{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001530 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001531 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001535 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 if (!decorator_seq)
1537 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001540 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001541 if (!d)
1542 return NULL;
1543 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 }
1545 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546}
1547
1548static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001549ast_for_funcdef_impl(struct compiling *c, const node *n0,
1550 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001552 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
guoci90fc8982018-09-11 17:45:45 -04001553 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001554 identifier name;
1555 arguments_ty args;
1556 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001557 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001558 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559
1560 REQ(n, funcdef);
1561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 name = NEW_IDENTIFIER(CHILD(n, name_i));
1563 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001564 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001565 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001566 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1568 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001569 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001570 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1571 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1572 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001573 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001574 name_i += 2;
1575 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001576 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579
Yury Selivanov75445082015-05-11 22:57:16 -04001580 if (is_async)
1581 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07001582 LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001583 else
1584 return FunctionDef(name, args, body, decorator_seq, returns,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001585 LINENO(n), n->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001586}
1587
1588static stmt_ty
1589ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1590{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001591 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001592 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001593 REQ(CHILD(n, 0), NAME);
1594 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001595 REQ(CHILD(n, 1), funcdef);
1596
guoci90fc8982018-09-11 17:45:45 -04001597 return ast_for_funcdef_impl(c, n, decorator_seq,
1598 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001599}
1600
1601static stmt_ty
1602ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1603{
1604 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1605 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001606 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001607}
1608
1609
1610static stmt_ty
1611ast_for_async_stmt(struct compiling *c, const node *n)
1612{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001613 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001614 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001615 REQ(CHILD(n, 0), NAME);
1616 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001617
1618 switch (TYPE(CHILD(n, 1))) {
1619 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001620 return ast_for_funcdef_impl(c, n, NULL,
1621 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001622 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001623 return ast_for_with_stmt(c, n,
1624 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001625
1626 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001627 return ast_for_for_stmt(c, n,
1628 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001629
1630 default:
1631 PyErr_Format(PyExc_SystemError,
1632 "invalid async stament: %s",
1633 STR(CHILD(n, 1)));
1634 return NULL;
1635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636}
1637
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001638static stmt_ty
1639ast_for_decorated(struct compiling *c, const node *n)
1640{
Yury Selivanov75445082015-05-11 22:57:16 -04001641 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001642 stmt_ty thing = NULL;
1643 asdl_seq *decorator_seq = NULL;
1644
1645 REQ(n, decorated);
1646
1647 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1648 if (!decorator_seq)
1649 return NULL;
1650
1651 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001652 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001654
1655 if (TYPE(CHILD(n, 1)) == funcdef) {
1656 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1657 } else if (TYPE(CHILD(n, 1)) == classdef) {
1658 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001659 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1660 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001661 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001662 /* we count the decorators in when talking about the class' or
1663 * function's line number */
1664 if (thing) {
1665 thing->lineno = LINENO(n);
1666 thing->col_offset = n->n_col_offset;
1667 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001668 return thing;
1669}
1670
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671static expr_ty
1672ast_for_lambdef(struct compiling *c, const node *n)
1673{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001674 /* lambdef: 'lambda' [varargslist] ':' test
1675 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 arguments_ty args;
1677 expr_ty expression;
1678
1679 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001680 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 if (!args)
1682 return NULL;
1683 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001684 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 }
1687 else {
1688 args = ast_for_arguments(c, CHILD(n, 1));
1689 if (!args)
1690 return NULL;
1691 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001692 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 }
1695
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697}
1698
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001699static expr_ty
1700ast_for_ifexpr(struct compiling *c, const node *n)
1701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001703 expr_ty expression, body, orelse;
1704
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001705 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001706 body = ast_for_expr(c, CHILD(n, 0));
1707 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001708 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001709 expression = ast_for_expr(c, CHILD(n, 2));
1710 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001711 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001712 orelse = ast_for_expr(c, CHILD(n, 4));
1713 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001714 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001715 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1716 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001717}
1718
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001720 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721
Nick Coghlan650f0d02007-04-15 12:05:43 +00001722 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723*/
1724
1725static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001726count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001728 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729
Guido van Rossumd8faa362007-04-27 19:54:29 +00001730 count_comp_for:
1731 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001732 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001733 if (NCH(n) == 2) {
1734 REQ(CHILD(n, 0), NAME);
1735 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1736 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001737 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001738 else if (NCH(n) == 1) {
1739 n = CHILD(n, 0);
1740 }
1741 else {
1742 goto error;
1743 }
1744 if (NCH(n) == (5)) {
1745 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001746 }
1747 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001748 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001749 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001750 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001751 REQ(n, comp_iter);
1752 n = CHILD(n, 0);
1753 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001754 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001755 else if (TYPE(n) == comp_if) {
1756 if (NCH(n) == 3) {
1757 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001758 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001759 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001760 else
1761 return n_fors;
1762 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001763
Jelle Zijlstraac317702017-10-05 20:24:46 -07001764 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001765 /* Should never be reached */
1766 PyErr_SetString(PyExc_SystemError,
1767 "logic error in count_comp_fors");
1768 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769}
1770
Nick Coghlan650f0d02007-04-15 12:05:43 +00001771/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772
Nick Coghlan650f0d02007-04-15 12:05:43 +00001773 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774*/
1775
1776static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001777count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780
Guido van Rossumd8faa362007-04-27 19:54:29 +00001781 while (1) {
1782 REQ(n, comp_iter);
1783 if (TYPE(CHILD(n, 0)) == comp_for)
1784 return n_ifs;
1785 n = CHILD(n, 0);
1786 REQ(n, comp_if);
1787 n_ifs++;
1788 if (NCH(n) == 2)
1789 return n_ifs;
1790 n = CHILD(n, 2);
1791 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792}
1793
Guido van Rossum992d4a32007-07-11 13:09:30 +00001794static asdl_seq *
1795ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001798 asdl_seq *comps;
1799
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001800 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 if (n_fors == -1)
1802 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001803
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001804 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001805 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001807
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001809 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001811 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001812 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001813 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001814 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815
Guido van Rossum992d4a32007-07-11 13:09:30 +00001816 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817
Jelle Zijlstraac317702017-10-05 20:24:46 -07001818 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001819 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001820 REQ(CHILD(n, 0), NAME);
1821 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1822 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001823 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001824 else {
1825 sync_n = CHILD(n, 0);
1826 }
1827 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001828
Jelle Zijlstraac317702017-10-05 20:24:46 -07001829 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001830 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001831 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001833 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001836
Thomas Wouters89f507f2006-12-13 04:49:30 +00001837 /* Check the # of children rather than the length of t, since
1838 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001839 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001840 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001841 comp = comprehension(first, expression, NULL,
1842 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001844 comp = comprehension(Tuple(t, Store, first->lineno,
1845 first->col_offset, c->c_arena),
1846 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001847 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849
Jelle Zijlstraac317702017-10-05 20:24:46 -07001850 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 int j, n_ifs;
1852 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853
Jelle Zijlstraac317702017-10-05 20:24:46 -07001854 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001855 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001859 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001862
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001864 REQ(n, comp_iter);
1865 n = CHILD(n, 0);
1866 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867
Guido van Rossum992d4a32007-07-11 13:09:30 +00001868 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001870 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001871 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001872 if (NCH(n) == 3)
1873 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001875 /* on exit, must guarantee that n is a comp_for */
1876 if (TYPE(n) == comp_iter)
1877 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001878 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001880 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001882 return comps;
1883}
1884
1885static expr_ty
1886ast_for_itercomp(struct compiling *c, const node *n, int type)
1887{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001888 /* testlist_comp: (test|star_expr)
1889 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 expr_ty elt;
1891 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001892 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001896 ch = CHILD(n, 0);
1897 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001898 if (!elt)
1899 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001900 if (elt->kind == Starred_kind) {
1901 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1902 return NULL;
1903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 comps = ast_for_comprehension(c, CHILD(n, 1));
1906 if (!comps)
1907 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001908
1909 if (type == COMP_GENEXP)
1910 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1911 else if (type == COMP_LISTCOMP)
1912 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1913 else if (type == COMP_SETCOMP)
1914 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1915 else
1916 /* Should never happen */
1917 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918}
1919
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001920/* Fills in the key, value pair corresponding to the dict element. In case
1921 * of an unpacking, key is NULL. *i is advanced by the number of ast
1922 * elements. Iff successful, nonzero is returned.
1923 */
1924static int
1925ast_for_dictelement(struct compiling *c, const node *n, int *i,
1926 expr_ty *key, expr_ty *value)
1927{
1928 expr_ty expression;
1929 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1930 assert(NCH(n) - *i >= 2);
1931
1932 expression = ast_for_expr(c, CHILD(n, *i + 1));
1933 if (!expression)
1934 return 0;
1935 *key = NULL;
1936 *value = expression;
1937
1938 *i += 2;
1939 }
1940 else {
1941 assert(NCH(n) - *i >= 3);
1942
1943 expression = ast_for_expr(c, CHILD(n, *i));
1944 if (!expression)
1945 return 0;
1946 *key = expression;
1947
1948 REQ(CHILD(n, *i + 1), COLON);
1949
1950 expression = ast_for_expr(c, CHILD(n, *i + 2));
1951 if (!expression)
1952 return 0;
1953 *value = expression;
1954
1955 *i += 3;
1956 }
1957 return 1;
1958}
1959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001961ast_for_dictcomp(struct compiling *c, const node *n)
1962{
1963 expr_ty key, value;
1964 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001965 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001967 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001968 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001969 assert(key);
1970 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001972 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001973 if (!comps)
1974 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975
Guido van Rossum992d4a32007-07-11 13:09:30 +00001976 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1977}
1978
1979static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001980ast_for_dictdisplay(struct compiling *c, const node *n)
1981{
1982 int i;
1983 int j;
1984 int size;
1985 asdl_seq *keys, *values;
1986
1987 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1988 keys = _Py_asdl_seq_new(size, c->c_arena);
1989 if (!keys)
1990 return NULL;
1991
1992 values = _Py_asdl_seq_new(size, c->c_arena);
1993 if (!values)
1994 return NULL;
1995
1996 j = 0;
1997 for (i = 0; i < NCH(n); i++) {
1998 expr_ty key, value;
1999
2000 if (!ast_for_dictelement(c, n, &i, &key, &value))
2001 return NULL;
2002 asdl_seq_SET(keys, j, key);
2003 asdl_seq_SET(values, j, value);
2004
2005 j++;
2006 }
2007 keys->size = j;
2008 values->size = j;
2009 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2010}
2011
2012static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002013ast_for_genexp(struct compiling *c, const node *n)
2014{
2015 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002016 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002017}
2018
2019static expr_ty
2020ast_for_listcomp(struct compiling *c, const node *n)
2021{
2022 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002023 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002024}
2025
2026static expr_ty
2027ast_for_setcomp(struct compiling *c, const node *n)
2028{
2029 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002030 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002031}
2032
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002033static expr_ty
2034ast_for_setdisplay(struct compiling *c, const node *n)
2035{
2036 int i;
2037 int size;
2038 asdl_seq *elts;
2039
2040 assert(TYPE(n) == (dictorsetmaker));
2041 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2042 elts = _Py_asdl_seq_new(size, c->c_arena);
2043 if (!elts)
2044 return NULL;
2045 for (i = 0; i < NCH(n); i += 2) {
2046 expr_ty expression;
2047 expression = ast_for_expr(c, CHILD(n, i));
2048 if (!expression)
2049 return NULL;
2050 asdl_seq_SET(elts, i / 2, expression);
2051 }
2052 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2053}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002054
2055static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056ast_for_atom(struct compiling *c, const node *n)
2057{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002058 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2059 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002060 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 */
2062 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002065 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002066 PyObject *name;
2067 const char *s = STR(ch);
2068 size_t len = strlen(s);
2069 if (len >= 4 && len <= 5) {
2070 if (!strcmp(s, "None"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002071 return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002072 if (!strcmp(s, "True"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002073 return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002074 if (!strcmp(s, "False"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002075 return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002076 }
2077 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002078 if (!name)
2079 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002080 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002081 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2082 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002084 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002085 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002086 const char *errtype = NULL;
2087 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2088 errtype = "unicode error";
2089 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2090 errtype = "value error";
2091 if (errtype) {
2092 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002093 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002094 PyObject *type, *value, *tback, *errstr;
2095 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002096 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002097 if (errstr)
2098 s = PyUnicode_AsUTF8(errstr);
2099 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002100 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002101 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002102 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002103 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002104 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002105 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002106 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002107 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002108 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002109 Py_XDECREF(tback);
2110 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002111 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002112 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002113 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
2115 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002116 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002117 if (!pynum)
2118 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002119
Victor Stinner43d81952013-07-17 00:57:58 +02002120 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2121 Py_DECREF(pynum);
2122 return NULL;
2123 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002124 return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 }
Georg Brandldde00282007-03-18 19:01:53 +00002126 case ELLIPSIS: /* Ellipsis */
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002127 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002129 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130
Thomas Wouters89f507f2006-12-13 04:49:30 +00002131 if (TYPE(ch) == RPAR)
2132 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 if (TYPE(ch) == yield_expr)
2135 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002138 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002140
Nick Coghlan650f0d02007-04-15 12:05:43 +00002141 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 if (TYPE(ch) == RSQB)
2146 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147
Nick Coghlan650f0d02007-04-15 12:05:43 +00002148 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002149 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2150 asdl_seq *elts = seq_for_testlist(c, ch);
2151 if (!elts)
2152 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002153
Thomas Wouters89f507f2006-12-13 04:49:30 +00002154 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2155 }
2156 else
2157 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002159 /* dictorsetmaker: ( ((test ':' test | '**' test)
2160 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2161 * ((test | '*' test)
2162 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002163 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002164 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002165 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002166 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002167 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002168 }
2169 else {
2170 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2171 if (NCH(ch) == 1 ||
2172 (NCH(ch) > 1 &&
2173 TYPE(CHILD(ch, 1)) == COMMA)) {
2174 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002175 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002176 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002177 else if (NCH(ch) > 1 &&
2178 TYPE(CHILD(ch, 1)) == comp_for) {
2179 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002180 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002181 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002182 else if (NCH(ch) > 3 - is_dict &&
2183 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2184 /* It's a dictionary comprehension. */
2185 if (is_dict) {
2186 ast_error(c, n, "dict unpacking cannot be used in "
2187 "dict comprehension");
2188 return NULL;
2189 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002190 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 }
2192 else {
2193 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002194 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002195 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002196 if (res) {
2197 res->lineno = LINENO(n);
2198 res->col_offset = n->n_col_offset;
2199 }
2200 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002204 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 }
2207}
2208
2209static slice_ty
2210ast_for_slice(struct compiling *c, const node *n)
2211{
2212 node *ch;
2213 expr_ty lower = NULL, upper = NULL, step = NULL;
2214
2215 REQ(n, subscript);
2216
2217 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002218 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 sliceop: ':' [test]
2220 */
2221 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222 if (NCH(n) == 1 && TYPE(ch) == test) {
2223 /* 'step' variable hold no significance in terms of being used over
2224 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 if (!step)
2227 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228
Thomas Wouters89f507f2006-12-13 04:49:30 +00002229 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 }
2231
2232 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002233 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 if (!lower)
2235 return NULL;
2236 }
2237
2238 /* If there's an upper bound it's in the second or third position. */
2239 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002240 if (NCH(n) > 1) {
2241 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242
Thomas Wouters89f507f2006-12-13 04:49:30 +00002243 if (TYPE(n2) == test) {
2244 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 if (!upper)
2246 return NULL;
2247 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002248 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002250 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251
Thomas Wouters89f507f2006-12-13 04:49:30 +00002252 if (TYPE(n2) == test) {
2253 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 if (!upper)
2255 return NULL;
2256 }
2257 }
2258
2259 ch = CHILD(n, NCH(n) - 1);
2260 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002261 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002262 ch = CHILD(ch, 1);
2263 if (TYPE(ch) == test) {
2264 step = ast_for_expr(c, ch);
2265 if (!step)
2266 return NULL;
2267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 }
2269 }
2270
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002271 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272}
2273
2274static expr_ty
2275ast_for_binop(struct compiling *c, const node *n)
2276{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002277 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002279 BinOp(BinOp(A, op, B), op, C).
2280 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281
Guido van Rossumd8faa362007-04-27 19:54:29 +00002282 int i, nops;
2283 expr_ty expr1, expr2, result;
2284 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285
Guido van Rossumd8faa362007-04-27 19:54:29 +00002286 expr1 = ast_for_expr(c, CHILD(n, 0));
2287 if (!expr1)
2288 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289
Guido van Rossumd8faa362007-04-27 19:54:29 +00002290 expr2 = ast_for_expr(c, CHILD(n, 2));
2291 if (!expr2)
2292 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293
Guido van Rossumd8faa362007-04-27 19:54:29 +00002294 newoperator = get_operator(CHILD(n, 1));
2295 if (!newoperator)
2296 return NULL;
2297
2298 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2299 c->c_arena);
2300 if (!result)
2301 return NULL;
2302
2303 nops = (NCH(n) - 1) / 2;
2304 for (i = 1; i < nops; i++) {
2305 expr_ty tmp_result, tmp;
2306 const node* next_oper = CHILD(n, i * 2 + 1);
2307
2308 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002309 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 return NULL;
2311
Guido van Rossumd8faa362007-04-27 19:54:29 +00002312 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2313 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 return NULL;
2315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002317 LINENO(next_oper), next_oper->n_col_offset,
2318 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002320 return NULL;
2321 result = tmp_result;
2322 }
2323 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324}
2325
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002326static expr_ty
2327ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002330 subscriptlist: subscript (',' subscript)* [',']
2331 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2332 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002333 REQ(n, trailer);
2334 if (TYPE(CHILD(n, 0)) == LPAR) {
2335 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002336 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002337 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002338 else
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002339 return ast_for_call(c, CHILD(n, 1), left_expr, true);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002340 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002341 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002342 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2343 if (!attr_id)
2344 return NULL;
2345 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002346 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002347 }
2348 else {
2349 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002350 REQ(CHILD(n, 2), RSQB);
2351 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002352 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002353 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2354 if (!slc)
2355 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002356 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2357 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358 }
2359 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002361 by treating the sequence as a tuple literal if there are
2362 no slice features.
2363 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 int j;
2365 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002366 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002367 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002368 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002369 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002370 if (!slices)
2371 return NULL;
2372 for (j = 0; j < NCH(n); j += 2) {
2373 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002374 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002376 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002377 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002378 asdl_seq_SET(slices, j / 2, slc);
2379 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002380 if (!simple) {
2381 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002382 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002383 }
2384 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002385 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002386 if (!elts)
2387 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002388 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2389 slc = (slice_ty)asdl_seq_GET(slices, j);
2390 assert(slc->kind == Index_kind && slc->v.Index.value);
2391 asdl_seq_SET(elts, j, slc->v.Index.value);
2392 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002393 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002394 if (!e)
2395 return NULL;
2396 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002397 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002398 }
2399 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002400}
2401
2402static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002403ast_for_factor(struct compiling *c, const node *n)
2404{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002405 expr_ty expression;
2406
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002407 expression = ast_for_expr(c, CHILD(n, 1));
2408 if (!expression)
2409 return NULL;
2410
2411 switch (TYPE(CHILD(n, 0))) {
2412 case PLUS:
2413 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2414 c->c_arena);
2415 case MINUS:
2416 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2417 c->c_arena);
2418 case TILDE:
2419 return UnaryOp(Invert, expression, LINENO(n),
2420 n->n_col_offset, c->c_arena);
2421 }
2422 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2423 TYPE(CHILD(n, 0)));
2424 return NULL;
2425}
2426
2427static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002428ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002429{
Yury Selivanov75445082015-05-11 22:57:16 -04002430 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002431 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002432
2433 REQ(n, atom_expr);
2434 nch = NCH(n);
2435
Jelle Zijlstraac317702017-10-05 20:24:46 -07002436 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002437 start = 1;
2438 assert(nch > 1);
2439 }
2440
2441 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002442 if (!e)
2443 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002444 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002445 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002446 if (start && nch == 2) {
2447 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2448 }
2449
2450 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002451 node *ch = CHILD(n, i);
2452 if (TYPE(ch) != trailer)
2453 break;
2454 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002455 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002456 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002457 tmp->lineno = e->lineno;
2458 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002459 e = tmp;
2460 }
Yury Selivanov75445082015-05-11 22:57:16 -04002461
2462 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002463 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002464 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2465 }
2466 else {
2467 return e;
2468 }
2469}
2470
2471static expr_ty
2472ast_for_power(struct compiling *c, const node *n)
2473{
2474 /* power: atom trailer* ('**' factor)*
2475 */
2476 expr_ty e;
2477 REQ(n, power);
2478 e = ast_for_atom_expr(c, CHILD(n, 0));
2479 if (!e)
2480 return NULL;
2481 if (NCH(n) == 1)
2482 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2484 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002485 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002486 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002487 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 }
2489 return e;
2490}
2491
Guido van Rossum0368b722007-05-11 16:50:42 +00002492static expr_ty
2493ast_for_starred(struct compiling *c, const node *n)
2494{
2495 expr_ty tmp;
2496 REQ(n, star_expr);
2497
2498 tmp = ast_for_expr(c, CHILD(n, 1));
2499 if (!tmp)
2500 return NULL;
2501
2502 /* The Load context is changed later. */
2503 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2504}
2505
2506
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507/* Do not name a variable 'expr'! Will cause a compile error.
2508*/
2509
2510static expr_ty
2511ast_for_expr(struct compiling *c, const node *n)
2512{
2513 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002514 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002515 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 and_test: not_test ('and' not_test)*
2518 not_test: 'not' not_test | comparison
2519 comparison: expr (comp_op expr)*
2520 expr: xor_expr ('|' xor_expr)*
2521 xor_expr: and_expr ('^' and_expr)*
2522 and_expr: shift_expr ('&' shift_expr)*
2523 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2524 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002525 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002527 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002528 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002529 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 */
2531
2532 asdl_seq *seq;
2533 int i;
2534
2535 loop:
2536 switch (TYPE(n)) {
2537 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002538 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002539 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002540 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002542 else if (NCH(n) > 1)
2543 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002544 /* Fallthrough */
2545 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 case and_test:
2547 if (NCH(n) == 1) {
2548 n = CHILD(n, 0);
2549 goto loop;
2550 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002551 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 if (!seq)
2553 return NULL;
2554 for (i = 0; i < NCH(n); i += 2) {
2555 expr_ty e = ast_for_expr(c, CHILD(n, i));
2556 if (!e)
2557 return NULL;
2558 asdl_seq_SET(seq, i / 2, e);
2559 }
2560 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002561 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2562 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002563 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002564 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 case not_test:
2566 if (NCH(n) == 1) {
2567 n = CHILD(n, 0);
2568 goto loop;
2569 }
2570 else {
2571 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2572 if (!expression)
2573 return NULL;
2574
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002575 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2576 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 }
2578 case comparison:
2579 if (NCH(n) == 1) {
2580 n = CHILD(n, 0);
2581 goto loop;
2582 }
2583 else {
2584 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002585 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002586 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002587 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 if (!ops)
2589 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002590 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 return NULL;
2593 }
2594 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002595 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002597 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002598 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002600 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601
2602 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002603 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002605 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 asdl_seq_SET(cmps, i / 2, expression);
2609 }
2610 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002611 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002615 return Compare(expression, ops, cmps, LINENO(n),
2616 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 }
2618 break;
2619
Guido van Rossum0368b722007-05-11 16:50:42 +00002620 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 /* The next five cases all handle BinOps. The main body of code
2623 is the same in each case, but the switch turned inside out to
2624 reuse the code for each type of operator.
2625 */
2626 case expr:
2627 case xor_expr:
2628 case and_expr:
2629 case shift_expr:
2630 case arith_expr:
2631 case term:
2632 if (NCH(n) == 1) {
2633 n = CHILD(n, 0);
2634 goto loop;
2635 }
2636 return ast_for_binop(c, n);
2637 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002638 node *an = NULL;
2639 node *en = NULL;
2640 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002641 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002642 if (NCH(n) > 1)
2643 an = CHILD(n, 1); /* yield_arg */
2644 if (an) {
2645 en = CHILD(an, NCH(an) - 1);
2646 if (NCH(an) == 2) {
2647 is_from = 1;
2648 exp = ast_for_expr(c, en);
2649 }
2650 else
2651 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002652 if (!exp)
2653 return NULL;
2654 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002655 if (is_from)
2656 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2657 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002658 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002659 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 if (NCH(n) == 1) {
2661 n = CHILD(n, 0);
2662 goto loop;
2663 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002664 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002665 case power:
2666 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002668 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 return NULL;
2670 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002671 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 return NULL;
2673}
2674
2675static expr_ty
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002676ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677{
2678 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002679 arglist: argument (',' argument)* [',']
2680 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 */
2682
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002683 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002684 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002685 asdl_seq *args;
2686 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687
2688 REQ(n, arglist);
2689
2690 nargs = 0;
2691 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002693 node *ch = CHILD(n, i);
2694 if (TYPE(ch) == argument) {
2695 if (NCH(ch) == 1)
2696 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002697 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2698 nargs++;
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002699 if (!allowgen) {
2700 ast_error(c, ch, "invalid syntax");
2701 return NULL;
2702 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002703 if (NCH(n) > 1) {
2704 ast_error(c, ch, "Generator expression must be parenthesized");
2705 return NULL;
2706 }
2707 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 else if (TYPE(CHILD(ch, 0)) == STAR)
2709 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002711 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002712 nkeywords++;
2713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002716 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002718 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002719 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002721 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002722
2723 nargs = 0; /* positional arguments + iterable argument unpackings */
2724 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2725 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 node *ch = CHILD(n, i);
2728 if (TYPE(ch) == argument) {
2729 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002732 /* a positional argument */
2733 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002734 if (ndoublestars) {
2735 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002736 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002737 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002738 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002739 else {
2740 ast_error(c, chch,
2741 "positional argument follows "
2742 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002744 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002745 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002746 e = ast_for_expr(c, chch);
2747 if (!e)
2748 return NULL;
2749 asdl_seq_SET(args, nargs++, e);
2750 }
2751 else if (TYPE(chch) == STAR) {
2752 /* an iterable argument unpacking */
2753 expr_ty starred;
2754 if (ndoublestars) {
2755 ast_error(c, chch,
2756 "iterable argument unpacking follows "
2757 "keyword argument unpacking");
2758 return NULL;
2759 }
2760 e = ast_for_expr(c, CHILD(ch, 1));
2761 if (!e)
2762 return NULL;
2763 starred = Starred(e, Load, LINENO(chch),
2764 chch->n_col_offset,
2765 c->c_arena);
2766 if (!starred)
2767 return NULL;
2768 asdl_seq_SET(args, nargs++, starred);
2769
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 }
2771 else if (TYPE(chch) == DOUBLESTAR) {
2772 /* a keyword argument unpacking */
2773 keyword_ty kw;
2774 i++;
2775 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002777 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002778 kw = keyword(NULL, e, c->c_arena);
2779 asdl_seq_SET(keywords, nkeywords++, kw);
2780 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002782 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002783 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002784 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002786 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002787 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002789 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002791 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002792 identifier key, tmp;
2793 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002795 // To remain LL(1), the grammar accepts any test (basically, any
2796 // expression) in the keyword slot of a call site. So, we need
2797 // to manually enforce that the keyword is a NAME here.
2798 static const int name_tree[] = {
2799 test,
2800 or_test,
2801 and_test,
2802 not_test,
2803 comparison,
2804 expr,
2805 xor_expr,
2806 and_expr,
2807 shift_expr,
2808 arith_expr,
2809 term,
2810 factor,
2811 power,
2812 atom_expr,
2813 atom,
2814 0,
2815 };
2816 node *expr_node = chch;
2817 for (int i = 0; name_tree[i]; i++) {
2818 if (TYPE(expr_node) != name_tree[i])
2819 break;
2820 if (NCH(expr_node) != 1)
2821 break;
2822 expr_node = CHILD(expr_node, 0);
2823 }
2824 if (TYPE(expr_node) == lambdef) {
2825 // f(lambda x: x[0] = 3) ends up getting parsed with LHS
2826 // test = lambda x: x[0], and RHS test = 3. Issue #132313
2827 // points out that complaining about a keyword then is very
2828 // confusing.
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002829 ast_error(c, chch,
2830 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002831 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002832 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002833 else if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 ast_error(c, chch,
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002835 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002836 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002838 key = new_identifier(STR(expr_node), c);
2839 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002840 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002842 if (forbidden_name(c, key, chch, 1)) {
2843 return NULL;
2844 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002845 for (k = 0; k < nkeywords; k++) {
2846 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002847 if (tmp && !PyUnicode_Compare(tmp, key)) {
2848 ast_error(c, chch,
2849 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002850 return NULL;
2851 }
2852 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002853 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002855 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002858 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 asdl_seq_SET(keywords, nkeywords++, kw);
2860 }
2861 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 }
2863
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002864 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865}
2866
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002868ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002870 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002871 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002873 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002874 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002875 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002876 }
2877 else {
2878 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002879 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 else {
2884 asdl_seq *tmp = seq_for_testlist(c, n);
2885 if (!tmp)
2886 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002887 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002889}
2890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891static stmt_ty
2892ast_for_expr_stmt(struct compiling *c, const node *n)
2893{
2894 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002895 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2896 ('=' (yield_expr|testlist_star_expr))*)
2897 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002898 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002899 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002900 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002901 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 */
2903
2904 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 if (!e)
2907 return NULL;
2908
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 }
2911 else if (TYPE(CHILD(n, 1)) == augassign) {
2912 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002913 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915
Thomas Wouters89f507f2006-12-13 04:49:30 +00002916 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 if (!expr1)
2918 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002919 if(!set_context(c, expr1, Store, ch))
2920 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002921 /* set_context checks that most expressions are not the left side.
2922 Augmented assignments can only have a name, a subscript, or an
2923 attribute on the left, though, so we have to explicitly check for
2924 those. */
2925 switch (expr1->kind) {
2926 case Name_kind:
2927 case Attribute_kind:
2928 case Subscript_kind:
2929 break;
2930 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002931 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002932 return NULL;
2933 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934
Thomas Wouters89f507f2006-12-13 04:49:30 +00002935 ch = CHILD(n, 2);
2936 if (TYPE(ch) == testlist)
2937 expr2 = ast_for_testlist(c, ch);
2938 else
2939 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002940 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941 return NULL;
2942
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002943 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002944 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 return NULL;
2946
Thomas Wouters89f507f2006-12-13 04:49:30 +00002947 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002949 else if (TYPE(CHILD(n, 1)) == annassign) {
2950 expr_ty expr1, expr2, expr3;
2951 node *ch = CHILD(n, 0);
2952 node *deep, *ann = CHILD(n, 1);
2953 int simple = 1;
2954
2955 /* we keep track of parens to qualify (x) as expression not name */
2956 deep = ch;
2957 while (NCH(deep) == 1) {
2958 deep = CHILD(deep, 0);
2959 }
2960 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2961 simple = 0;
2962 }
2963 expr1 = ast_for_testlist(c, ch);
2964 if (!expr1) {
2965 return NULL;
2966 }
2967 switch (expr1->kind) {
2968 case Name_kind:
2969 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2970 return NULL;
2971 }
2972 expr1->v.Name.ctx = Store;
2973 break;
2974 case Attribute_kind:
2975 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2976 return NULL;
2977 }
2978 expr1->v.Attribute.ctx = Store;
2979 break;
2980 case Subscript_kind:
2981 expr1->v.Subscript.ctx = Store;
2982 break;
2983 case List_kind:
2984 ast_error(c, ch,
2985 "only single target (not list) can be annotated");
2986 return NULL;
2987 case Tuple_kind:
2988 ast_error(c, ch,
2989 "only single target (not tuple) can be annotated");
2990 return NULL;
2991 default:
2992 ast_error(c, ch,
2993 "illegal target for annotation");
2994 return NULL;
2995 }
2996
2997 if (expr1->kind != Name_kind) {
2998 simple = 0;
2999 }
3000 ch = CHILD(ann, 1);
3001 expr2 = ast_for_expr(c, ch);
3002 if (!expr2) {
3003 return NULL;
3004 }
3005 if (NCH(ann) == 2) {
3006 return AnnAssign(expr1, expr2, NULL, simple,
3007 LINENO(n), n->n_col_offset, c->c_arena);
3008 }
3009 else {
3010 ch = CHILD(ann, 3);
3011 expr3 = ast_for_expr(c, ch);
3012 if (!expr3) {
3013 return NULL;
3014 }
3015 return AnnAssign(expr1, expr2, expr3, simple,
3016 LINENO(n), n->n_col_offset, c->c_arena);
3017 }
3018 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003020 int i;
3021 asdl_seq *targets;
3022 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 expr_ty expression;
3024
Thomas Wouters89f507f2006-12-13 04:49:30 +00003025 /* a normal assignment */
3026 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003027 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 if (!targets)
3029 return NULL;
3030 for (i = 0; i < NCH(n) - 2; i += 2) {
3031 expr_ty e;
3032 node *ch = CHILD(n, i);
3033 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003034 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003035 return NULL;
3036 }
3037 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003041 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003042 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044
Thomas Wouters89f507f2006-12-13 04:49:30 +00003045 asdl_seq_SET(targets, i / 2, e);
3046 }
3047 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003048 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 expression = ast_for_testlist(c, value);
3050 else
3051 expression = ast_for_expr(c, value);
3052 if (!expression)
3053 return NULL;
3054 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056}
3057
Benjamin Peterson78565b22009-06-28 19:19:51 +00003058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003060ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061{
3062 asdl_seq *seq;
3063 int i;
3064 expr_ty e;
3065
3066 REQ(n, exprlist);
3067
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003068 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003070 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003072 e = ast_for_expr(c, CHILD(n, i));
3073 if (!e)
3074 return NULL;
3075 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003076 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 }
3079 return seq;
3080}
3081
3082static stmt_ty
3083ast_for_del_stmt(struct compiling *c, const node *n)
3084{
3085 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087 /* del_stmt: 'del' exprlist */
3088 REQ(n, del_stmt);
3089
3090 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3091 if (!expr_list)
3092 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003093 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094}
3095
3096static stmt_ty
3097ast_for_flow_stmt(struct compiling *c, const node *n)
3098{
3099 /*
3100 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3101 | yield_stmt
3102 break_stmt: 'break'
3103 continue_stmt: 'continue'
3104 return_stmt: 'return' [testlist]
3105 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003106 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 raise_stmt: 'raise' [test [',' test [',' test]]]
3108 */
3109 node *ch;
3110
3111 REQ(n, flow_stmt);
3112 ch = CHILD(n, 0);
3113 switch (TYPE(ch)) {
3114 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003115 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003117 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003119 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3120 if (!exp)
3121 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003122 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 }
3124 case return_stmt:
3125 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003126 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003128 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 if (!expression)
3130 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003131 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 }
3133 case raise_stmt:
3134 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003135 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3136 else if (NCH(ch) >= 2) {
3137 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3139 if (!expression)
3140 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003141 if (NCH(ch) == 4) {
3142 cause = ast_for_expr(c, CHILD(ch, 3));
3143 if (!cause)
3144 return NULL;
3145 }
3146 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 }
Stefan Krahf432a322017-08-21 13:09:59 +02003148 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003150 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 "unexpected flow_stmt: %d", TYPE(ch));
3152 return NULL;
3153 }
3154}
3155
3156static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003157alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158{
3159 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003160 import_as_name: NAME ['as' NAME]
3161 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 dotted_name: NAME ('.' NAME)*
3163 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003164 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 loop:
3167 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003168 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003169 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003170 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003171 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003172 if (!name)
3173 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003174 if (NCH(n) == 3) {
3175 node *str_node = CHILD(n, 2);
3176 str = NEW_IDENTIFIER(str_node);
3177 if (!str)
3178 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003179 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003180 return NULL;
3181 }
3182 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003183 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003184 return NULL;
3185 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003186 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188 case dotted_as_name:
3189 if (NCH(n) == 1) {
3190 n = CHILD(n, 0);
3191 goto loop;
3192 }
3193 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003194 node *asname_node = CHILD(n, 2);
3195 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003196 if (!a)
3197 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003199 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003200 if (!a->asname)
3201 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003202 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 return a;
3205 }
3206 break;
3207 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003208 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003209 node *name_node = CHILD(n, 0);
3210 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003211 if (!name)
3212 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003213 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003214 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003215 return alias(name, NULL, c->c_arena);
3216 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217 else {
3218 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003219 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003220 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223
3224 len = 0;
3225 for (i = 0; i < NCH(n); i += 2)
3226 /* length of string plus one for the dot */
3227 len += strlen(STR(CHILD(n, i))) + 1;
3228 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003229 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 if (!str)
3231 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003232 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 if (!s)
3234 return NULL;
3235 for (i = 0; i < NCH(n); i += 2) {
3236 char *sch = STR(CHILD(n, i));
3237 strcpy(s, STR(CHILD(n, i)));
3238 s += strlen(sch);
3239 *s++ = '.';
3240 }
3241 --s;
3242 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3244 PyBytes_GET_SIZE(str),
3245 NULL);
3246 Py_DECREF(str);
3247 if (!uni)
3248 return NULL;
3249 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003250 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003251 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3252 Py_DECREF(str);
3253 return NULL;
3254 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003255 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 }
3257 break;
3258 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003259 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003260 if (!str)
3261 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003262 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3263 Py_DECREF(str);
3264 return NULL;
3265 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003266 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003268 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269 "unexpected import name: %d", TYPE(n));
3270 return NULL;
3271 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003272
3273 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 return NULL;
3275}
3276
3277static stmt_ty
3278ast_for_import_stmt(struct compiling *c, const node *n)
3279{
3280 /*
3281 import_stmt: import_name | import_from
3282 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003283 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3284 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003286 int lineno;
3287 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 int i;
3289 asdl_seq *aliases;
3290
3291 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003292 lineno = LINENO(n);
3293 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003295 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003297 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003298 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003299 if (!aliases)
3300 return NULL;
3301 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003302 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003303 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003305 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003309 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003311 int idx, ndots = 0;
3312 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003313 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003315 /* Count the number of dots (for relative imports) and check for the
3316 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 for (idx = 1; idx < NCH(n); idx++) {
3318 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003319 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3320 if (!mod)
3321 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 idx++;
3323 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003324 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003326 ndots += 3;
3327 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003328 } else if (TYPE(CHILD(n, idx)) != DOT) {
3329 break;
3330 }
3331 ndots++;
3332 }
3333 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003334 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003335 case STAR:
3336 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 n = CHILD(n, idx);
3338 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 break;
3340 case LPAR:
3341 /* from ... import (x, y, z) */
3342 n = CHILD(n, idx + 1);
3343 n_children = NCH(n);
3344 break;
3345 case import_as_names:
3346 /* from ... import x, y, z */
3347 n = CHILD(n, idx);
3348 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003349 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003350 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 " surrounding parentheses");
3352 return NULL;
3353 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003354 break;
3355 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003356 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 return NULL;
3358 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003360 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003361 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363
3364 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003365 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003366 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003367 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003369 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003371 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003372 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);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003374 if (!import_alias)
3375 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003376 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003379 if (mod != NULL)
3380 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003381 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003382 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 }
Neal Norwitz79792652005-11-14 04:25:03 +00003384 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385 "unknown import statement: starts with command '%s'",
3386 STR(CHILD(n, 0)));
3387 return NULL;
3388}
3389
3390static stmt_ty
3391ast_for_global_stmt(struct compiling *c, const node *n)
3392{
3393 /* global_stmt: 'global' NAME (',' NAME)* */
3394 identifier name;
3395 asdl_seq *s;
3396 int i;
3397
3398 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003399 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003401 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003403 name = NEW_IDENTIFIER(CHILD(n, i));
3404 if (!name)
3405 return NULL;
3406 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003408 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409}
3410
3411static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003412ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3413{
3414 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3415 identifier name;
3416 asdl_seq *s;
3417 int i;
3418
3419 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003420 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003421 if (!s)
3422 return NULL;
3423 for (i = 1; i < NCH(n); i += 2) {
3424 name = NEW_IDENTIFIER(CHILD(n, i));
3425 if (!name)
3426 return NULL;
3427 asdl_seq_SET(s, i / 2, name);
3428 }
3429 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3430}
3431
3432static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433ast_for_assert_stmt(struct compiling *c, const node *n)
3434{
3435 /* assert_stmt: 'assert' test [',' test] */
3436 REQ(n, assert_stmt);
3437 if (NCH(n) == 2) {
3438 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3439 if (!expression)
3440 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003441 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 }
3443 else if (NCH(n) == 4) {
3444 expr_ty expr1, expr2;
3445
3446 expr1 = ast_for_expr(c, CHILD(n, 1));
3447 if (!expr1)
3448 return NULL;
3449 expr2 = ast_for_expr(c, CHILD(n, 3));
3450 if (!expr2)
3451 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452
Thomas Wouters89f507f2006-12-13 04:49:30 +00003453 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454 }
Neal Norwitz79792652005-11-14 04:25:03 +00003455 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 "improper number of parts to 'assert' statement: %d",
3457 NCH(n));
3458 return NULL;
3459}
3460
3461static asdl_seq *
3462ast_for_suite(struct compiling *c, const node *n)
3463{
3464 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003465 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 stmt_ty s;
3467 int i, total, num, end, pos = 0;
3468 node *ch;
3469
3470 REQ(n, suite);
3471
3472 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003473 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003477 n = CHILD(n, 0);
3478 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 */
3481 end = NCH(n) - 1;
3482 if (TYPE(CHILD(n, end - 1)) == SEMI)
3483 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003485 for (i = 0; i < end; i += 2) {
3486 ch = CHILD(n, i);
3487 s = ast_for_stmt(c, ch);
3488 if (!s)
3489 return NULL;
3490 asdl_seq_SET(seq, pos++, s);
3491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 }
3493 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003494 for (i = 2; i < (NCH(n) - 1); i++) {
3495 ch = CHILD(n, i);
3496 REQ(ch, stmt);
3497 num = num_stmts(ch);
3498 if (num == 1) {
3499 /* small_stmt or compound_stmt with only one child */
3500 s = ast_for_stmt(c, ch);
3501 if (!s)
3502 return NULL;
3503 asdl_seq_SET(seq, pos++, s);
3504 }
3505 else {
3506 int j;
3507 ch = CHILD(ch, 0);
3508 REQ(ch, simple_stmt);
3509 for (j = 0; j < NCH(ch); j += 2) {
3510 /* statement terminates with a semi-colon ';' */
3511 if (NCH(CHILD(ch, j)) == 0) {
3512 assert((j + 1) == NCH(ch));
3513 break;
3514 }
3515 s = ast_for_stmt(c, CHILD(ch, j));
3516 if (!s)
3517 return NULL;
3518 asdl_seq_SET(seq, pos++, s);
3519 }
3520 }
3521 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 }
3523 assert(pos == seq->size);
3524 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525}
3526
3527static stmt_ty
3528ast_for_if_stmt(struct compiling *c, const node *n)
3529{
3530 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3531 ['else' ':' suite]
3532 */
3533 char *s;
3534
3535 REQ(n, if_stmt);
3536
3537 if (NCH(n) == 4) {
3538 expr_ty expression;
3539 asdl_seq *suite_seq;
3540
3541 expression = ast_for_expr(c, CHILD(n, 1));
3542 if (!expression)
3543 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003545 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547
Guido van Rossumd8faa362007-04-27 19:54:29 +00003548 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3549 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 s = STR(CHILD(n, 4));
3553 /* s[2], the third character in the string, will be
3554 's' for el_s_e, or
3555 'i' for el_i_f
3556 */
3557 if (s[2] == 's') {
3558 expr_ty expression;
3559 asdl_seq *seq1, *seq2;
3560
3561 expression = ast_for_expr(c, CHILD(n, 1));
3562 if (!expression)
3563 return NULL;
3564 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003565 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 return NULL;
3567 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003568 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 return NULL;
3570
Guido van Rossumd8faa362007-04-27 19:54:29 +00003571 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3572 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 }
3574 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003575 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003576 expr_ty expression;
3577 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003578 asdl_seq *orelse = NULL;
3579 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 /* must reference the child n_elif+1 since 'else' token is third,
3581 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003582 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3583 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3584 has_else = 1;
3585 n_elif -= 3;
3586 }
3587 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588
Thomas Wouters89f507f2006-12-13 04:49:30 +00003589 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003590 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003592 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003593 if (!orelse)
3594 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003596 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003598 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3599 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003601 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3602 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 asdl_seq_SET(orelse, 0,
3606 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003607 LINENO(CHILD(n, NCH(n) - 6)),
3608 CHILD(n, NCH(n) - 6)->n_col_offset,
3609 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 /* the just-created orelse handled the last elif */
3611 n_elif--;
3612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 for (i = 0; i < n_elif; i++) {
3615 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003616 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003617 if (!newobj)
3618 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003620 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003623 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625
Thomas Wouters89f507f2006-12-13 04:49:30 +00003626 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003628 LINENO(CHILD(n, off)),
3629 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003630 orelse = newobj;
3631 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003632 expression = ast_for_expr(c, CHILD(n, 1));
3633 if (!expression)
3634 return NULL;
3635 suite_seq = ast_for_suite(c, CHILD(n, 3));
3636 if (!suite_seq)
3637 return NULL;
3638 return If(expression, suite_seq, orelse,
3639 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003641
3642 PyErr_Format(PyExc_SystemError,
3643 "unexpected token in 'if' statement: %s", s);
3644 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645}
3646
3647static stmt_ty
3648ast_for_while_stmt(struct compiling *c, const node *n)
3649{
3650 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3651 REQ(n, while_stmt);
3652
3653 if (NCH(n) == 4) {
3654 expr_ty expression;
3655 asdl_seq *suite_seq;
3656
3657 expression = ast_for_expr(c, CHILD(n, 1));
3658 if (!expression)
3659 return NULL;
3660 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003661 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003663 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 }
3665 else if (NCH(n) == 7) {
3666 expr_ty expression;
3667 asdl_seq *seq1, *seq2;
3668
3669 expression = ast_for_expr(c, CHILD(n, 1));
3670 if (!expression)
3671 return NULL;
3672 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003673 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 return NULL;
3675 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003676 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 return NULL;
3678
Thomas Wouters89f507f2006-12-13 04:49:30 +00003679 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003680 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003681
3682 PyErr_Format(PyExc_SystemError,
3683 "wrong number of tokens for 'while' statement: %d",
3684 NCH(n));
3685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686}
3687
3688static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003689ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690{
guoci90fc8982018-09-11 17:45:45 -04003691 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003692 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003694 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003695 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3697 REQ(n, for_stmt);
3698
3699 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003700 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 if (!seq)
3702 return NULL;
3703 }
3704
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003705 node_target = CHILD(n, 1);
3706 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003707 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003709 /* Check the # of children rather than the length of _target, since
3710 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003711 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003712 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003713 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003715 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003717 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003718 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 return NULL;
3720 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003721 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 return NULL;
3723
Yury Selivanov75445082015-05-11 22:57:16 -04003724 if (is_async)
3725 return AsyncFor(target, expression, suite_seq, seq,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003726 LINENO(n0), n0->n_col_offset,
Yury Selivanov75445082015-05-11 22:57:16 -04003727 c->c_arena);
3728 else
3729 return For(target, expression, suite_seq, seq,
3730 LINENO(n), n->n_col_offset,
3731 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732}
3733
3734static excepthandler_ty
3735ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3736{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003737 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 REQ(exc, except_clause);
3739 REQ(body, suite);
3740
3741 if (NCH(exc) == 1) {
3742 asdl_seq *suite_seq = ast_for_suite(c, body);
3743 if (!suite_seq)
3744 return NULL;
3745
Neal Norwitzad74aa82008-03-31 05:14:30 +00003746 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003747 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 }
3749 else if (NCH(exc) == 2) {
3750 expr_ty expression;
3751 asdl_seq *suite_seq;
3752
3753 expression = ast_for_expr(c, CHILD(exc, 1));
3754 if (!expression)
3755 return NULL;
3756 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003757 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 return NULL;
3759
Neal Norwitzad74aa82008-03-31 05:14:30 +00003760 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003761 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 }
3763 else if (NCH(exc) == 4) {
3764 asdl_seq *suite_seq;
3765 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003766 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003767 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003769 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003772 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 return NULL;
3774 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003775 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 return NULL;
3777
Neal Norwitzad74aa82008-03-31 05:14:30 +00003778 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003779 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003781
3782 PyErr_Format(PyExc_SystemError,
3783 "wrong number of children for 'except' clause: %d",
3784 NCH(exc));
3785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786}
3787
3788static stmt_ty
3789ast_for_try_stmt(struct compiling *c, const node *n)
3790{
Neal Norwitzf599f422005-12-17 21:33:47 +00003791 const int nch = NCH(n);
3792 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003793 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003794
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 REQ(n, try_stmt);
3796
Neal Norwitzf599f422005-12-17 21:33:47 +00003797 body = ast_for_suite(c, CHILD(n, 2));
3798 if (body == NULL)
3799 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800
Neal Norwitzf599f422005-12-17 21:33:47 +00003801 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3802 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3803 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3804 /* we can assume it's an "else",
3805 because nch >= 9 for try-else-finally and
3806 it would otherwise have a type of except_clause */
3807 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3808 if (orelse == NULL)
3809 return NULL;
3810 n_except--;
3811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812
Neal Norwitzf599f422005-12-17 21:33:47 +00003813 finally = ast_for_suite(c, CHILD(n, nch - 1));
3814 if (finally == NULL)
3815 return NULL;
3816 n_except--;
3817 }
3818 else {
3819 /* we can assume it's an "else",
3820 otherwise it would have a type of except_clause */
3821 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3822 if (orelse == NULL)
3823 return NULL;
3824 n_except--;
3825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003827 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003828 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 return NULL;
3830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831
Neal Norwitzf599f422005-12-17 21:33:47 +00003832 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003833 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003834 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003835 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003836 if (handlers == NULL)
3837 return NULL;
3838
3839 for (i = 0; i < n_except; i++) {
3840 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3841 CHILD(n, 5 + i * 3));
3842 if (!e)
3843 return NULL;
3844 asdl_seq_SET(handlers, i, e);
3845 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003846 }
3847
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003848 assert(finally != NULL || asdl_seq_LEN(handlers));
3849 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850}
3851
Georg Brandl0c315622009-05-25 21:10:36 +00003852/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003853static withitem_ty
3854ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003855{
3856 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003857
Georg Brandl0c315622009-05-25 21:10:36 +00003858 REQ(n, with_item);
3859 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003860 if (!context_expr)
3861 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003862 if (NCH(n) == 3) {
3863 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003864
3865 if (!optional_vars) {
3866 return NULL;
3867 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003868 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003869 return NULL;
3870 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003871 }
3872
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003873 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003874}
3875
Georg Brandl0c315622009-05-25 21:10:36 +00003876/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3877static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003878ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003879{
guoci90fc8982018-09-11 17:45:45 -04003880 const node * const n = is_async ? CHILD(n0, 1) : n0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003881 int i, n_items;
3882 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003883
3884 REQ(n, with_stmt);
3885
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003886 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003887 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003888 if (!items)
3889 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003890 for (i = 1; i < NCH(n) - 2; i += 2) {
3891 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3892 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003893 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003894 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003895 }
3896
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003897 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3898 if (!body)
3899 return NULL;
3900
Yury Selivanov75445082015-05-11 22:57:16 -04003901 if (is_async)
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003902 return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04003903 else
3904 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003905}
3906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003908ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003910 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003911 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003912 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003913 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003914
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915 REQ(n, classdef);
3916
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003917 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003918 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 if (!s)
3920 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003921 classname = NEW_IDENTIFIER(CHILD(n, 1));
3922 if (!classname)
3923 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003924 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003925 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003926 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003927 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003929
3930 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003931 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003932 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003933 return NULL;
3934 classname = NEW_IDENTIFIER(CHILD(n, 1));
3935 if (!classname)
3936 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003937 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003938 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003939 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003940 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 }
3942
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003943 /* class NAME '(' arglist ')' ':' suite */
3944 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003945 {
3946 PyObject *dummy_name;
3947 expr_ty dummy;
3948 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3949 if (!dummy_name)
3950 return NULL;
3951 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003952 call = ast_for_call(c, CHILD(n, 3), dummy, false);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003953 if (!call)
3954 return NULL;
3955 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003956 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003957 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003959 classname = NEW_IDENTIFIER(CHILD(n, 1));
3960 if (!classname)
3961 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003962 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003963 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003964
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003965 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003966 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967}
3968
3969static stmt_ty
3970ast_for_stmt(struct compiling *c, const node *n)
3971{
3972 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003973 assert(NCH(n) == 1);
3974 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 }
3976 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003977 assert(num_stmts(n) == 1);
3978 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 }
3980 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003981 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003982 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3983 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003984 */
3985 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 case expr_stmt:
3987 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 case del_stmt:
3989 return ast_for_del_stmt(c, n);
3990 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003991 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 case flow_stmt:
3993 return ast_for_flow_stmt(c, n);
3994 case import_stmt:
3995 return ast_for_import_stmt(c, n);
3996 case global_stmt:
3997 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003998 case nonlocal_stmt:
3999 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 case assert_stmt:
4001 return ast_for_assert_stmt(c, n);
4002 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004003 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4005 TYPE(n), NCH(n));
4006 return NULL;
4007 }
4008 }
4009 else {
4010 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004011 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004012 */
4013 node *ch = CHILD(n, 0);
4014 REQ(n, compound_stmt);
4015 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 case if_stmt:
4017 return ast_for_if_stmt(c, ch);
4018 case while_stmt:
4019 return ast_for_while_stmt(c, ch);
4020 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004021 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 case try_stmt:
4023 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004024 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004025 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004027 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004029 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 case decorated:
4031 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004032 case async_stmt:
4033 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004035 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004036 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 TYPE(n), NCH(n));
4038 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004039 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 }
4041}
4042
4043static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004044parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004046 const char *end;
4047 long x;
4048 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004049 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004050 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004052 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 errno = 0;
4054 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004056 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004057 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004059 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004060 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004061 }
4062 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004063 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 if (*end == '\0') {
4065 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004066 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004067 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004068 }
4069 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004070 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004071 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004072 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4073 if (compl.imag == -1.0 && PyErr_Occurred())
4074 return NULL;
4075 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004076 }
4077 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004078 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004079 dx = PyOS_string_to_double(s, NULL, NULL);
4080 if (dx == -1.0 && PyErr_Occurred())
4081 return NULL;
4082 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084}
4085
4086static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004087parsenumber(struct compiling *c, const char *s)
4088{
4089 char *dup, *end;
4090 PyObject *res = NULL;
4091
4092 assert(s != NULL);
4093
4094 if (strchr(s, '_') == NULL) {
4095 return parsenumber_raw(c, s);
4096 }
4097 /* Create a duplicate without underscores. */
4098 dup = PyMem_Malloc(strlen(s) + 1);
4099 end = dup;
4100 for (; *s; s++) {
4101 if (*s != '_') {
4102 *end++ = *s;
4103 }
4104 }
4105 *end = '\0';
4106 res = parsenumber_raw(c, dup);
4107 PyMem_Free(dup);
4108 return res;
4109}
4110
4111static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004112decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004113{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004114 const char *s, *t;
4115 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004116 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4117 while (s < end && (*s & 0x80)) s++;
4118 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004119 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120}
4121
Eric V. Smith56466482016-10-31 14:46:26 -04004122static int
4123warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004124 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004125{
4126 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4127 first_invalid_escape_char);
4128 if (msg == NULL) {
4129 return -1;
4130 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004131 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004132 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004133 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004134 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004135 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004136 const char *s;
Victor Stinnerf9cca362016-11-15 09:12:10 +01004137
Serhiy Storchaka65439122018-10-19 17:42:06 +03004138 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004139 to get a more accurate error report */
4140 PyErr_Clear();
Victor Stinnerf9cca362016-11-15 09:12:10 +01004141
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004142 s = PyUnicode_AsUTF8(msg);
4143 if (s != NULL) {
4144 ast_error(c, n, s);
4145 }
Eric V. Smith56466482016-10-31 14:46:26 -04004146 }
4147 Py_DECREF(msg);
4148 return -1;
4149 }
4150 Py_DECREF(msg);
4151 return 0;
4152}
4153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004155decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4156 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004158 PyObject *v, *u;
4159 char *buf;
4160 char *p;
4161 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004162
Benjamin Peterson202803a2016-02-25 22:34:45 -08004163 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004164 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004165 return NULL;
4166 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4167 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4168 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4169 if (u == NULL)
4170 return NULL;
4171 p = buf = PyBytes_AsString(u);
4172 end = s + len;
4173 while (s < end) {
4174 if (*s == '\\') {
4175 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004176 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004177 strcpy(p, "u005c");
4178 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004179 if (s >= end)
4180 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004181 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004182 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004183 if (*s & 0x80) { /* XXX inefficient */
4184 PyObject *w;
4185 int kind;
4186 void *data;
4187 Py_ssize_t len, i;
4188 w = decode_utf8(c, &s, end);
4189 if (w == NULL) {
4190 Py_DECREF(u);
4191 return NULL;
4192 }
4193 kind = PyUnicode_KIND(w);
4194 data = PyUnicode_DATA(w);
4195 len = PyUnicode_GET_LENGTH(w);
4196 for (i = 0; i < len; i++) {
4197 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4198 sprintf(p, "\\U%08x", chr);
4199 p += 10;
4200 }
4201 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004202 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004203 Py_DECREF(w);
4204 } else {
4205 *p++ = *s++;
4206 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004207 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004208 len = p - buf;
4209 s = buf;
4210
Eric V. Smith56466482016-10-31 14:46:26 -04004211 const char *first_invalid_escape;
4212 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4213
4214 if (v != NULL && first_invalid_escape != NULL) {
4215 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4216 /* We have not decref u before because first_invalid_escape points
4217 inside u. */
4218 Py_XDECREF(u);
4219 Py_DECREF(v);
4220 return NULL;
4221 }
4222 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004223 Py_XDECREF(u);
4224 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004225}
4226
Eric V. Smith56466482016-10-31 14:46:26 -04004227static PyObject *
4228decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4229 size_t len)
4230{
4231 const char *first_invalid_escape;
4232 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4233 &first_invalid_escape);
4234 if (result == NULL)
4235 return NULL;
4236
4237 if (first_invalid_escape != NULL) {
4238 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4239 Py_DECREF(result);
4240 return NULL;
4241 }
4242 }
4243 return result;
4244}
4245
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004246/* Shift locations for the given node and all its children by adding `lineno`
4247 and `col_offset` to existing locations. */
4248static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4249{
4250 n->n_col_offset = n->n_col_offset + col_offset;
4251 for (int i = 0; i < NCH(n); ++i) {
4252 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4253 /* Shifting column offsets unnecessary if there's been newlines. */
4254 col_offset = 0;
4255 }
4256 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4257 }
4258 n->n_lineno = n->n_lineno + lineno;
4259}
4260
4261/* Fix locations for the given node and its children.
4262
4263 `parent` is the enclosing node.
4264 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004265 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004266*/
4267static void
4268fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4269{
4270 char *substr = NULL;
4271 char *start;
4272 int lines = LINENO(parent) - 1;
4273 int cols = parent->n_col_offset;
4274 /* Find the full fstring to fix location information in `n`. */
4275 while (parent && parent->n_type != STRING)
4276 parent = parent->n_child;
4277 if (parent && parent->n_str) {
4278 substr = strstr(parent->n_str, expr_str);
4279 if (substr) {
4280 start = substr;
4281 while (start > parent->n_str) {
4282 if (start[0] == '\n')
4283 break;
4284 start--;
4285 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004286 cols += (int)(substr - start);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004287 /* Fix lineno in mulitline strings. */
4288 while ((substr = strchr(substr + 1, '\n')))
4289 lines--;
4290 }
4291 }
4292 fstring_shift_node_locations(n, lines, cols);
4293}
4294
Eric V. Smith451d0e32016-09-09 21:56:20 -04004295/* Compile this expression in to an expr_ty. Add parens around the
4296 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004297static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004298fstring_compile_expr(const char *expr_start, const char *expr_end,
4299 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004300
Eric V. Smith235a6f02015-09-19 14:51:32 -04004301{
4302 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004303 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004304 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004305 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004306 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004307 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004308
Eric V. Smith1d44c412015-09-23 07:49:00 -04004309 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004310 assert(*(expr_start-1) == '{');
4311 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004312
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004313 /* If the substring is all whitespace, it's an error. We need to catch this
4314 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4315 because turning the expression '' in to '()' would go from being invalid
4316 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004317 for (s = expr_start; s != expr_end; s++) {
4318 char c = *s;
4319 /* The Python parser ignores only the following whitespace
4320 characters (\r already is converted to \n). */
4321 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004322 break;
4323 }
4324 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004325 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004326 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004327 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004328 }
4329
Eric V. Smith451d0e32016-09-09 21:56:20 -04004330 len = expr_end - expr_start;
4331 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4332 str = PyMem_RawMalloc(len + 3);
4333 if (str == NULL)
4334 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 str[0] = '(';
4337 memcpy(str+1, expr_start, len);
4338 str[len+1] = ')';
4339 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004340
4341 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004342 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4343 Py_eval_input, 0);
4344 if (!mod_n) {
4345 PyMem_RawFree(str);
4346 return NULL;
4347 }
4348 /* Reuse str to find the correct column offset. */
4349 str[0] = '{';
4350 str[len+1] = '}';
4351 fstring_fix_node_location(n, mod_n, str);
4352 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004354 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004355 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004356 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004357 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004358}
4359
4360/* Return -1 on error.
4361
4362 Return 0 if we reached the end of the literal.
4363
4364 Return 1 if we haven't reached the end of the literal, but we want
4365 the caller to process the literal up to this point. Used for
4366 doubled braces.
4367*/
4368static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004369fstring_find_literal(const char **str, const char *end, int raw,
4370 PyObject **literal, int recurse_lvl,
4371 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004372{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004373 /* Get any literal string. It ends when we hit an un-doubled left
4374 brace (which isn't part of a unicode name escape such as
4375 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004376
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004377 const char *s = *str;
4378 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004379 int result = 0;
4380
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004382 while (s < end) {
4383 char ch = *s++;
4384 if (!raw && ch == '\\' && s < end) {
4385 ch = *s++;
4386 if (ch == 'N') {
4387 if (s < end && *s++ == '{') {
4388 while (s < end && *s++ != '}') {
4389 }
4390 continue;
4391 }
4392 break;
4393 }
4394 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4395 return -1;
4396 }
4397 }
4398 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004399 /* Check for doubled braces, but only at the top level. If
4400 we checked at every level, then f'{0:{3}}' would fail
4401 with the two closing braces. */
4402 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004403 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004404 /* We're going to tell the caller that the literal ends
4405 here, but that they should continue scanning. But also
4406 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004408 result = 1;
4409 goto done;
4410 }
4411
4412 /* Where a single '{' is the start of a new expression, a
4413 single '}' is not allowed. */
4414 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004415 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004416 ast_error(c, n, "f-string: single '}' is not allowed");
4417 return -1;
4418 }
4419 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004420 /* We're either at a '{', which means we're starting another
4421 expression; or a '}', which means we're at the end of this
4422 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004423 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004424 break;
4425 }
4426 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004427 *str = s;
4428 assert(s <= end);
4429 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004430done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004431 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004432 if (raw)
4433 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004434 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004435 NULL, NULL);
4436 else
Eric V. Smith56466482016-10-31 14:46:26 -04004437 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004438 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004439 if (!*literal)
4440 return -1;
4441 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004442 return result;
4443}
4444
4445/* Forward declaration because parsing is recursive. */
4446static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004447fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004448 struct compiling *c, const node *n);
4449
Eric V. Smith451d0e32016-09-09 21:56:20 -04004450/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004451 expression (so it must be a '{'). Returns the FormattedValue node,
4452 which includes the expression, conversion character, and
4453 format_spec expression.
4454
4455 Note that I don't do a perfect job here: I don't make sure that a
4456 closing brace doesn't match an opening paren, for example. It
4457 doesn't need to error on all invalid expressions, just correctly
4458 find the end of all valid ones. Any errors inside the expression
4459 will be caught when we parse it later. */
4460static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004461fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004462 expr_ty *expression, struct compiling *c, const node *n)
4463{
4464 /* Return -1 on error, else 0. */
4465
Eric V. Smith451d0e32016-09-09 21:56:20 -04004466 const char *expr_start;
4467 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004468 expr_ty simple_expression;
4469 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004470 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004471
4472 /* 0 if we're not in a string, else the quote char we're trying to
4473 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004474 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004475
4476 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4477 int string_type = 0;
4478
4479 /* Keep track of nesting level for braces/parens/brackets in
4480 expressions. */
4481 Py_ssize_t nested_depth = 0;
4482
4483 /* Can only nest one level deep. */
4484 if (recurse_lvl >= 2) {
4485 ast_error(c, n, "f-string: expressions nested too deeply");
4486 return -1;
4487 }
4488
4489 /* The first char must be a left brace, or we wouldn't have gotten
4490 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004491 assert(**str == '{');
4492 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004493
Eric V. Smith451d0e32016-09-09 21:56:20 -04004494 expr_start = *str;
4495 for (; *str < end; (*str)++) {
4496 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004497
4498 /* Loop invariants. */
4499 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004500 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004501 if (quote_char)
4502 assert(string_type == 1 || string_type == 3);
4503 else
4504 assert(string_type == 0);
4505
Eric V. Smith451d0e32016-09-09 21:56:20 -04004506 ch = **str;
4507 /* Nowhere inside an expression is a backslash allowed. */
4508 if (ch == '\\') {
4509 /* Error: can't include a backslash character, inside
4510 parens or strings or not. */
4511 ast_error(c, n, "f-string expression part "
4512 "cannot include a backslash");
4513 return -1;
4514 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004515 if (quote_char) {
4516 /* We're inside a string. See if we're at the end. */
4517 /* This code needs to implement the same non-error logic
4518 as tok_get from tokenizer.c, at the letter_quote
4519 label. To actually share that code would be a
4520 nightmare. But, it's unlikely to change and is small,
4521 so duplicate it here. Note we don't need to catch all
4522 of the errors, since they'll be caught when parsing the
4523 expression. We just need to match the non-error
4524 cases. Thus we can ignore \n in single-quoted strings,
4525 for example. Or non-terminated strings. */
4526 if (ch == quote_char) {
4527 /* Does this match the string_type (single or triple
4528 quoted)? */
4529 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004530 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004531 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004532 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004533 string_type = 0;
4534 quote_char = 0;
4535 continue;
4536 }
4537 } else {
4538 /* We're at the end of a normal string. */
4539 quote_char = 0;
4540 string_type = 0;
4541 continue;
4542 }
4543 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004544 } else if (ch == '\'' || ch == '"') {
4545 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004546 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004547 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004548 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004549 } else {
4550 /* Start of a normal string. */
4551 string_type = 1;
4552 }
4553 /* Start looking for the end of the string. */
4554 quote_char = ch;
4555 } else if (ch == '[' || ch == '{' || ch == '(') {
4556 nested_depth++;
4557 } else if (nested_depth != 0 &&
4558 (ch == ']' || ch == '}' || ch == ')')) {
4559 nested_depth--;
4560 } else if (ch == '#') {
4561 /* Error: can't include a comment character, inside parens
4562 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004563 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004564 return -1;
4565 } else if (nested_depth == 0 &&
4566 (ch == '!' || ch == ':' || ch == '}')) {
4567 /* First, test for the special case of "!=". Since '=' is
4568 not an allowed conversion character, nothing is lost in
4569 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004570 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004571 /* This isn't a conversion character, just continue. */
4572 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004573 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004574 /* Normal way out of this loop. */
4575 break;
4576 } else {
4577 /* Just consume this char and loop around. */
4578 }
4579 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004580 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004581 /* If we leave this loop in a string or with mismatched parens, we
4582 don't care. We'll get a syntax error when compiling the
4583 expression. But, we can produce a better error message, so
4584 let's just do that.*/
4585 if (quote_char) {
4586 ast_error(c, n, "f-string: unterminated string");
4587 return -1;
4588 }
4589 if (nested_depth) {
4590 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4591 return -1;
4592 }
4593
Eric V. Smith451d0e32016-09-09 21:56:20 -04004594 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004595 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004596
4597 /* Compile the expression as soon as possible, so we show errors
4598 related to the expression before errors related to the
4599 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004600 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004601 if (!simple_expression)
4602 return -1;
4603
4604 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004605 if (**str == '!') {
4606 *str += 1;
4607 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004608 goto unexpected_end_of_string;
4609
Eric V. Smith451d0e32016-09-09 21:56:20 -04004610 conversion = **str;
4611 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004612
4613 /* Validate the conversion. */
4614 if (!(conversion == 's' || conversion == 'r'
4615 || conversion == 'a')) {
4616 ast_error(c, n, "f-string: invalid conversion character: "
4617 "expected 's', 'r', or 'a'");
4618 return -1;
4619 }
4620 }
4621
4622 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004623 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004624 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004625 if (**str == ':') {
4626 *str += 1;
4627 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004628 goto unexpected_end_of_string;
4629
4630 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004631 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004632 if (!format_spec)
4633 return -1;
4634 }
4635
Eric V. Smith451d0e32016-09-09 21:56:20 -04004636 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004637 goto unexpected_end_of_string;
4638
4639 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004640 assert(*str < end);
4641 assert(**str == '}');
4642 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004643
Eric V. Smith451d0e32016-09-09 21:56:20 -04004644 /* And now create the FormattedValue node that represents this
4645 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004646 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004647 format_spec, LINENO(n), n->n_col_offset,
4648 c->c_arena);
4649 if (!*expression)
4650 return -1;
4651
4652 return 0;
4653
4654unexpected_end_of_string:
4655 ast_error(c, n, "f-string: expecting '}'");
4656 return -1;
4657}
4658
4659/* Return -1 on error.
4660
4661 Return 0 if we have a literal (possible zero length) and an
4662 expression (zero length if at the end of the string.
4663
4664 Return 1 if we have a literal, but no expression, and we want the
4665 caller to call us again. This is used to deal with doubled
4666 braces.
4667
4668 When called multiple times on the string 'a{{b{0}c', this function
4669 will return:
4670
4671 1. the literal 'a{' with no expression, and a return value
4672 of 1. Despite the fact that there's no expression, the return
4673 value of 1 means we're not finished yet.
4674
4675 2. the literal 'b' and the expression '0', with a return value of
4676 0. The fact that there's an expression means we're not finished.
4677
4678 3. literal 'c' with no expression and a return value of 0. The
4679 combination of the return value of 0 with no expression means
4680 we're finished.
4681*/
4682static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004683fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4684 int recurse_lvl, PyObject **literal,
4685 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004686 struct compiling *c, const node *n)
4687{
4688 int result;
4689
4690 assert(*literal == NULL && *expression == NULL);
4691
4692 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004693 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004694 if (result < 0)
4695 goto error;
4696
4697 assert(result == 0 || result == 1);
4698
4699 if (result == 1)
4700 /* We have a literal, but don't look at the expression. */
4701 return 1;
4702
Eric V. Smith451d0e32016-09-09 21:56:20 -04004703 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004704 /* We're at the end of the string or the end of a nested
4705 f-string: no expression. The top-level error case where we
4706 expect to be at the end of the string but we're at a '}' is
4707 handled later. */
4708 return 0;
4709
4710 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004711 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004712
Eric V. Smith451d0e32016-09-09 21:56:20 -04004713 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004714 goto error;
4715
4716 return 0;
4717
4718error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004719 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004720 return -1;
4721}
4722
4723#define EXPRLIST_N_CACHED 64
4724
4725typedef struct {
4726 /* Incrementally build an array of expr_ty, so be used in an
4727 asdl_seq. Cache some small but reasonably sized number of
4728 expr_ty's, and then after that start dynamically allocating,
4729 doubling the number allocated each time. Note that the f-string
4730 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004731 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04004732 fast as you add exressions in an f-string. */
4733
4734 Py_ssize_t allocated; /* Number we've allocated. */
4735 Py_ssize_t size; /* Number we've used. */
4736 expr_ty *p; /* Pointer to the memory we're actually
4737 using. Will point to 'data' until we
4738 start dynamically allocating. */
4739 expr_ty data[EXPRLIST_N_CACHED];
4740} ExprList;
4741
4742#ifdef NDEBUG
4743#define ExprList_check_invariants(l)
4744#else
4745static void
4746ExprList_check_invariants(ExprList *l)
4747{
4748 /* Check our invariants. Make sure this object is "live", and
4749 hasn't been deallocated. */
4750 assert(l->size >= 0);
4751 assert(l->p != NULL);
4752 if (l->size <= EXPRLIST_N_CACHED)
4753 assert(l->data == l->p);
4754}
4755#endif
4756
4757static void
4758ExprList_Init(ExprList *l)
4759{
4760 l->allocated = EXPRLIST_N_CACHED;
4761 l->size = 0;
4762
4763 /* Until we start allocating dynamically, p points to data. */
4764 l->p = l->data;
4765
4766 ExprList_check_invariants(l);
4767}
4768
4769static int
4770ExprList_Append(ExprList *l, expr_ty exp)
4771{
4772 ExprList_check_invariants(l);
4773 if (l->size >= l->allocated) {
4774 /* We need to alloc (or realloc) the memory. */
4775 Py_ssize_t new_size = l->allocated * 2;
4776
4777 /* See if we've ever allocated anything dynamically. */
4778 if (l->p == l->data) {
4779 Py_ssize_t i;
4780 /* We're still using the cached data. Switch to
4781 alloc-ing. */
4782 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4783 if (!l->p)
4784 return -1;
4785 /* Copy the cached data into the new buffer. */
4786 for (i = 0; i < l->size; i++)
4787 l->p[i] = l->data[i];
4788 } else {
4789 /* Just realloc. */
4790 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4791 if (!tmp) {
4792 PyMem_RawFree(l->p);
4793 l->p = NULL;
4794 return -1;
4795 }
4796 l->p = tmp;
4797 }
4798
4799 l->allocated = new_size;
4800 assert(l->allocated == 2 * l->size);
4801 }
4802
4803 l->p[l->size++] = exp;
4804
4805 ExprList_check_invariants(l);
4806 return 0;
4807}
4808
4809static void
4810ExprList_Dealloc(ExprList *l)
4811{
4812 ExprList_check_invariants(l);
4813
4814 /* If there's been an error, or we've never dynamically allocated,
4815 do nothing. */
4816 if (!l->p || l->p == l->data) {
4817 /* Do nothing. */
4818 } else {
4819 /* We have dynamically allocated. Free the memory. */
4820 PyMem_RawFree(l->p);
4821 }
4822 l->p = NULL;
4823 l->size = -1;
4824}
4825
4826static asdl_seq *
4827ExprList_Finish(ExprList *l, PyArena *arena)
4828{
4829 asdl_seq *seq;
4830
4831 ExprList_check_invariants(l);
4832
4833 /* Allocate the asdl_seq and copy the expressions in to it. */
4834 seq = _Py_asdl_seq_new(l->size, arena);
4835 if (seq) {
4836 Py_ssize_t i;
4837 for (i = 0; i < l->size; i++)
4838 asdl_seq_SET(seq, i, l->p[i]);
4839 }
4840 ExprList_Dealloc(l);
4841 return seq;
4842}
4843
4844/* The FstringParser is designed to add a mix of strings and
4845 f-strings, and concat them together as needed. Ultimately, it
4846 generates an expr_ty. */
4847typedef struct {
4848 PyObject *last_str;
4849 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004850 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004851} FstringParser;
4852
4853#ifdef NDEBUG
4854#define FstringParser_check_invariants(state)
4855#else
4856static void
4857FstringParser_check_invariants(FstringParser *state)
4858{
4859 if (state->last_str)
4860 assert(PyUnicode_CheckExact(state->last_str));
4861 ExprList_check_invariants(&state->expr_list);
4862}
4863#endif
4864
4865static void
4866FstringParser_Init(FstringParser *state)
4867{
4868 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004869 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004870 ExprList_Init(&state->expr_list);
4871 FstringParser_check_invariants(state);
4872}
4873
4874static void
4875FstringParser_Dealloc(FstringParser *state)
4876{
4877 FstringParser_check_invariants(state);
4878
4879 Py_XDECREF(state->last_str);
4880 ExprList_Dealloc(&state->expr_list);
4881}
4882
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004883/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004884static expr_ty
4885make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4886{
4887 PyObject *s = *str;
4888 *str = NULL;
4889 assert(PyUnicode_CheckExact(s));
4890 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4891 Py_DECREF(s);
4892 return NULL;
4893 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004894 return Constant(s, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004895}
4896
4897/* Add a non-f-string (that is, a regular literal string). str is
4898 decref'd. */
4899static int
4900FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4901{
4902 FstringParser_check_invariants(state);
4903
4904 assert(PyUnicode_CheckExact(str));
4905
4906 if (PyUnicode_GET_LENGTH(str) == 0) {
4907 Py_DECREF(str);
4908 return 0;
4909 }
4910
4911 if (!state->last_str) {
4912 /* We didn't have a string before, so just remember this one. */
4913 state->last_str = str;
4914 } else {
4915 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004916 PyUnicode_AppendAndDel(&state->last_str, str);
4917 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918 return -1;
4919 }
4920 FstringParser_check_invariants(state);
4921 return 0;
4922}
4923
Eric V. Smith451d0e32016-09-09 21:56:20 -04004924/* Parse an f-string. The f-string is in *str to end, with no
4925 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004927FstringParser_ConcatFstring(FstringParser *state, const char **str,
4928 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 struct compiling *c, const node *n)
4930{
4931 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004932 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004933
4934 /* Parse the f-string. */
4935 while (1) {
4936 PyObject *literal = NULL;
4937 expr_ty expression = NULL;
4938
4939 /* If there's a zero length literal in front of the
4940 expression, literal will be NULL. If we're at the end of
4941 the f-string, expression will be NULL (unless result == 1,
4942 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004943 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 &literal, &expression,
4945 c, n);
4946 if (result < 0)
4947 return -1;
4948
4949 /* Add the literal, if any. */
4950 if (!literal) {
4951 /* Do nothing. Just leave last_str alone (and possibly
4952 NULL). */
4953 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004954 /* Note that the literal can be zero length, if the
4955 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956 state->last_str = literal;
4957 literal = NULL;
4958 } else {
4959 /* We have a literal, concatenate it. */
4960 assert(PyUnicode_GET_LENGTH(literal) != 0);
4961 if (FstringParser_ConcatAndDel(state, literal) < 0)
4962 return -1;
4963 literal = NULL;
4964 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965
4966 /* We've dealt with the literal now. It can't be leaked on further
4967 errors. */
4968 assert(literal == NULL);
4969
4970 /* See if we should just loop around to get the next literal
4971 and expression, while ignoring the expression this
4972 time. This is used for un-doubling braces, as an
4973 optimization. */
4974 if (result == 1)
4975 continue;
4976
4977 if (!expression)
4978 /* We're done with this f-string. */
4979 break;
4980
4981 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004982 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004983 if (!state->last_str) {
4984 /* Do nothing. No previous literal. */
4985 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004986 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4988 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4989 return -1;
4990 }
4991
4992 if (ExprList_Append(&state->expr_list, expression) < 0)
4993 return -1;
4994 }
4995
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 /* If recurse_lvl is zero, then we must be at the end of the
4997 string. Otherwise, we must be at a right brace. */
4998
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005000 ast_error(c, n, "f-string: unexpected end of string");
5001 return -1;
5002 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005003 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005004 ast_error(c, n, "f-string: expecting '}'");
5005 return -1;
5006 }
5007
5008 FstringParser_check_invariants(state);
5009 return 0;
5010}
5011
5012/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005013 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014static expr_ty
5015FstringParser_Finish(FstringParser *state, struct compiling *c,
5016 const node *n)
5017{
5018 asdl_seq *seq;
5019
5020 FstringParser_check_invariants(state);
5021
5022 /* If we're just a constant string with no expressions, return
5023 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005024 if (!state->fmode) {
5025 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 if (!state->last_str) {
5027 /* Create a zero length string. */
5028 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5029 if (!state->last_str)
5030 goto error;
5031 }
5032 return make_str_node_and_del(&state->last_str, c, n);
5033 }
5034
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005035 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005036 last node in our expression list. */
5037 if (state->last_str) {
5038 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5039 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5040 goto error;
5041 }
5042 /* This has already been freed. */
5043 assert(state->last_str == NULL);
5044
5045 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5046 if (!seq)
5047 goto error;
5048
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5050
5051error:
5052 FstringParser_Dealloc(state);
5053 return NULL;
5054}
5055
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5057 at end, parse it into an expr_ty. Return NULL on error. Adjust
5058 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005060fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061 struct compiling *c, const node *n)
5062{
5063 FstringParser state;
5064
5065 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005066 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005067 c, n) < 0) {
5068 FstringParser_Dealloc(&state);
5069 return NULL;
5070 }
5071
5072 return FstringParser_Finish(&state, c, n);
5073}
5074
5075/* n is a Python string literal, including the bracketing quote
5076 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005077 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005078 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005079 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5080 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005081*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082static int
5083parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5084 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005085{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005086 size_t len;
5087 const char *s = STR(n);
5088 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 int fmode = 0;
5090 *bytesmode = 0;
5091 *rawmode = 0;
5092 *result = NULL;
5093 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005094 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005095 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005096 if (quote == 'b' || quote == 'B') {
5097 quote = *++s;
5098 *bytesmode = 1;
5099 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005100 else if (quote == 'u' || quote == 'U') {
5101 quote = *++s;
5102 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005103 else if (quote == 'r' || quote == 'R') {
5104 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005106 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107 else if (quote == 'f' || quote == 'F') {
5108 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005111 else {
5112 break;
5113 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005114 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005115 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005118 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005120 if (quote != '\'' && quote != '\"') {
5121 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005123 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005125 s++;
5126 len = strlen(s);
5127 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005129 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005131 }
5132 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005133 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005134 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005136 }
5137 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 /* A triple quoted string. We've already skipped one quote at
5139 the start and one at the end of the string. Now skip the
5140 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005141 s += 2;
5142 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005143 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005144 if (s[--len] != quote || s[--len] != quote) {
5145 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005147 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005148 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005149
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 if (fmode) {
5151 /* Just return the bytes. The caller will parse the resulting
5152 string. */
5153 *fstr = s;
5154 *fstrlen = len;
5155 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005156 }
5157
Eric V. Smith451d0e32016-09-09 21:56:20 -04005158 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005159 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005160 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005161 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005162 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005163 const char *ch;
5164 for (ch = s; *ch; ch++) {
5165 if (Py_CHARMASK(*ch) >= 0x80) {
5166 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005167 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005168 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005169 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005170 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 if (*rawmode)
5172 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005173 else
Eric V. Smith56466482016-10-31 14:46:26 -04005174 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005175 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 if (*rawmode)
5177 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005178 else
Eric V. Smith56466482016-10-31 14:46:26 -04005179 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005180 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005182}
5183
Eric V. Smith235a6f02015-09-19 14:51:32 -04005184/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5185 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005186 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005187 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005188 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005189 node if there's just an f-string (with no leading or trailing
5190 literals), or a JoinedStr node if there are multiple f-strings or
5191 any literals involved. */
5192static expr_ty
5193parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005194{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005195 int bytesmode = 0;
5196 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005197 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005198
5199 FstringParser state;
5200 FstringParser_Init(&state);
5201
5202 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005203 int this_bytesmode;
5204 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005205 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 const char *fstr;
5207 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005208
5209 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005210 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5211 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 goto error;
5213
5214 /* Check that we're not mixing bytes with unicode. */
5215 if (i != 0 && bytesmode != this_bytesmode) {
5216 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005217 /* s is NULL if the current string part is an f-string. */
5218 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005219 goto error;
5220 }
5221 bytesmode = this_bytesmode;
5222
Eric V. Smith451d0e32016-09-09 21:56:20 -04005223 if (fstr != NULL) {
5224 int result;
5225 assert(s == NULL && !bytesmode);
5226 /* This is an f-string. Parse and concatenate it. */
5227 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5228 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229 if (result < 0)
5230 goto error;
5231 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005232 /* A string or byte string. */
5233 assert(s != NULL && fstr == NULL);
5234
Eric V. Smith451d0e32016-09-09 21:56:20 -04005235 assert(bytesmode ? PyBytes_CheckExact(s) :
5236 PyUnicode_CheckExact(s));
5237
Eric V. Smith451d0e32016-09-09 21:56:20 -04005238 if (bytesmode) {
5239 /* For bytes, concat as we go. */
5240 if (i == 0) {
5241 /* First time, just remember this value. */
5242 bytes_str = s;
5243 } else {
5244 PyBytes_ConcatAndDel(&bytes_str, s);
5245 if (!bytes_str)
5246 goto error;
5247 }
5248 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005249 /* This is a regular string. Concatenate it. */
5250 if (FstringParser_ConcatAndDel(&state, s) < 0)
5251 goto error;
5252 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005253 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005254 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005255 if (bytesmode) {
5256 /* Just return the bytes object and we're done. */
5257 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5258 goto error;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005259 return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005261
Eric V. Smith235a6f02015-09-19 14:51:32 -04005262 /* We're not a bytes string, bytes_str should never have been set. */
5263 assert(bytes_str == NULL);
5264
5265 return FstringParser_Finish(&state, c, n);
5266
5267error:
5268 Py_XDECREF(bytes_str);
5269 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005270 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005271}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005272
5273PyObject *
5274_PyAST_GetDocString(asdl_seq *body)
5275{
5276 if (!asdl_seq_LEN(body)) {
5277 return NULL;
5278 }
5279 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5280 if (st->kind != Expr_kind) {
5281 return NULL;
5282 }
5283 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005284 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5285 return e->v.Constant.value;
5286 }
5287 return NULL;
5288}