blob: 43bd786015a7b420a9dfbda3efffa4afefc381ee [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;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500298 case Num_kind: {
299 PyObject *n = exp->v.Num.n;
300 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
301 !PyComplex_CheckExact(n)) {
302 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
303 return 0;
304 }
305 return 1;
306 }
307 case Str_kind: {
308 PyObject *s = exp->v.Str.s;
309 if (!PyUnicode_CheckExact(s)) {
310 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
311 return 0;
312 }
313 return 1;
314 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400315 case JoinedStr_kind:
316 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
317 case FormattedValue_kind:
318 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
319 return 0;
320 if (exp->v.FormattedValue.format_spec)
321 return validate_expr(exp->v.FormattedValue.format_spec, Load);
322 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 case Bytes_kind: {
324 PyObject *b = exp->v.Bytes.s;
325 if (!PyBytes_CheckExact(b)) {
326 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
327 return 0;
328 }
329 return 1;
330 }
331 case Attribute_kind:
332 return validate_expr(exp->v.Attribute.value, Load);
333 case Subscript_kind:
334 return validate_slice(exp->v.Subscript.slice) &&
335 validate_expr(exp->v.Subscript.value, Load);
336 case Starred_kind:
337 return validate_expr(exp->v.Starred.value, ctx);
338 case List_kind:
339 return validate_exprs(exp->v.List.elts, ctx, 0);
340 case Tuple_kind:
341 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
342 /* These last cases don't have any checking. */
343 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500344 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500345 case Ellipsis_kind:
346 return 1;
347 default:
348 PyErr_SetString(PyExc_SystemError, "unexpected expression");
349 return 0;
350 }
351}
352
353static int
354validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
355{
356 if (asdl_seq_LEN(seq))
357 return 1;
358 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
359 return 0;
360}
361
362static int
363validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
364{
365 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
366 validate_exprs(targets, ctx, 0);
367}
368
369static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300370validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500371{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300372 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500373}
374
375static int
376validate_stmt(stmt_ty stmt)
377{
378 int i;
379 switch (stmt->kind) {
380 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300381 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500382 validate_arguments(stmt->v.FunctionDef.args) &&
383 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
384 (!stmt->v.FunctionDef.returns ||
385 validate_expr(stmt->v.FunctionDef.returns, Load));
386 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300387 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500388 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
389 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400390 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500391 case Return_kind:
392 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
393 case Delete_kind:
394 return validate_assignlist(stmt->v.Delete.targets, Del);
395 case Assign_kind:
396 return validate_assignlist(stmt->v.Assign.targets, Store) &&
397 validate_expr(stmt->v.Assign.value, Load);
398 case AugAssign_kind:
399 return validate_expr(stmt->v.AugAssign.target, Store) &&
400 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700401 case AnnAssign_kind:
402 if (stmt->v.AnnAssign.target->kind != Name_kind &&
403 stmt->v.AnnAssign.simple) {
404 PyErr_SetString(PyExc_TypeError,
405 "AnnAssign with simple non-Name target");
406 return 0;
407 }
408 return validate_expr(stmt->v.AnnAssign.target, Store) &&
409 (!stmt->v.AnnAssign.value ||
410 validate_expr(stmt->v.AnnAssign.value, Load)) &&
411 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500412 case For_kind:
413 return validate_expr(stmt->v.For.target, Store) &&
414 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300415 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500416 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncFor_kind:
418 return validate_expr(stmt->v.AsyncFor.target, Store) &&
419 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300420 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400421 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500422 case While_kind:
423 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300424 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500425 validate_stmts(stmt->v.While.orelse);
426 case If_kind:
427 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300428 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500429 validate_stmts(stmt->v.If.orelse);
430 case With_kind:
431 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
432 return 0;
433 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
434 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
435 if (!validate_expr(item->context_expr, Load) ||
436 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
437 return 0;
438 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300439 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400440 case AsyncWith_kind:
441 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
442 return 0;
443 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
444 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
445 if (!validate_expr(item->context_expr, Load) ||
446 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
447 return 0;
448 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300449 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500450 case Raise_kind:
451 if (stmt->v.Raise.exc) {
452 return validate_expr(stmt->v.Raise.exc, Load) &&
453 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
454 }
455 if (stmt->v.Raise.cause) {
456 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
457 return 0;
458 }
459 return 1;
460 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300461 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500462 return 0;
463 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
464 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
465 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
466 return 0;
467 }
468 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
469 asdl_seq_LEN(stmt->v.Try.orelse)) {
470 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
471 return 0;
472 }
473 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
474 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
475 if ((handler->v.ExceptHandler.type &&
476 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300477 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500478 return 0;
479 }
480 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
481 validate_stmts(stmt->v.Try.finalbody)) &&
482 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
483 validate_stmts(stmt->v.Try.orelse));
484 case Assert_kind:
485 return validate_expr(stmt->v.Assert.test, Load) &&
486 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
487 case Import_kind:
488 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
489 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300490 if (stmt->v.ImportFrom.level < 0) {
491 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500492 return 0;
493 }
494 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
495 case Global_kind:
496 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
497 case Nonlocal_kind:
498 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
499 case Expr_kind:
500 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400501 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300502 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400503 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
504 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
505 (!stmt->v.AsyncFunctionDef.returns ||
506 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500507 case Pass_kind:
508 case Break_kind:
509 case Continue_kind:
510 return 1;
511 default:
512 PyErr_SetString(PyExc_SystemError, "unexpected statement");
513 return 0;
514 }
515}
516
517static int
518validate_stmts(asdl_seq *seq)
519{
520 int i;
521 for (i = 0; i < asdl_seq_LEN(seq); i++) {
522 stmt_ty stmt = asdl_seq_GET(seq, i);
523 if (stmt) {
524 if (!validate_stmt(stmt))
525 return 0;
526 }
527 else {
528 PyErr_SetString(PyExc_ValueError,
529 "None disallowed in statement list");
530 return 0;
531 }
532 }
533 return 1;
534}
535
536static int
537validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
538{
539 int i;
540 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
541 expr_ty expr = asdl_seq_GET(exprs, i);
542 if (expr) {
543 if (!validate_expr(expr, ctx))
544 return 0;
545 }
546 else if (!null_ok) {
547 PyErr_SetString(PyExc_ValueError,
548 "None disallowed in expression list");
549 return 0;
550 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100551
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500552 }
553 return 1;
554}
555
556int
557PyAST_Validate(mod_ty mod)
558{
559 int res = 0;
560
561 switch (mod->kind) {
562 case Module_kind:
563 res = validate_stmts(mod->v.Module.body);
564 break;
565 case Interactive_kind:
566 res = validate_stmts(mod->v.Interactive.body);
567 break;
568 case Expression_kind:
569 res = validate_expr(mod->v.Expression.body, Load);
570 break;
571 case Suite_kind:
572 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
573 break;
574 default:
575 PyErr_SetString(PyExc_SystemError, "impossible module node");
576 res = 0;
577 break;
578 }
579 return res;
580}
581
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500582/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500583#include "grammar.h"
584#include "parsetok.h"
585#include "graminit.h"
586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587/* Data structure used internally */
588struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400589 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200590 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500591 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592};
593
594static asdl_seq *seq_for_testlist(struct compiling *, const node *);
595static expr_ty ast_for_expr(struct compiling *, const node *);
596static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300597static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
599 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000600static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000601static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Yury Selivanov75445082015-05-11 22:57:16 -0400603static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
604static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606/* Note different signature for ast_for_call */
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200607static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000609static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400610static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Nick Coghlan650f0d02007-04-15 12:05:43 +0000612#define COMP_GENEXP 0
613#define COMP_LISTCOMP 1
614#define COMP_SETCOMP 2
615
Benjamin Peterson55e00432012-01-16 17:22:31 -0500616static int
617init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000618{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500619 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
620 if (!m)
621 return 0;
622 c->c_normalize = PyObject_GetAttrString(m, "normalize");
623 Py_DECREF(m);
624 if (!c->c_normalize)
625 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500626 return 1;
627}
628
629static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400630new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500631{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400632 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500633 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000634 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500635 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500636 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000637 /* Check whether there are non-ASCII characters in the
638 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500639 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300641 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500642 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500643 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500645 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300646 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
647 if (form == NULL) {
648 Py_DECREF(id);
649 return NULL;
650 }
651 PyObject *args[2] = {form, id};
652 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500653 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (!id2)
655 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300656 if (!PyUnicode_Check(id2)) {
657 PyErr_Format(PyExc_TypeError,
658 "unicodedata.normalize() must return a string, not "
659 "%.200s",
660 Py_TYPE(id2)->tp_name);
661 Py_DECREF(id2);
662 return NULL;
663 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200664 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000665 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000666 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200667 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
668 Py_DECREF(id);
669 return NULL;
670 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000671 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672}
673
Benjamin Peterson55e00432012-01-16 17:22:31 -0500674#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400677ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
Victor Stinner14e461d2013-08-26 22:28:21 +0200681 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000683 Py_INCREF(Py_None);
684 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200686 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400687 if (!tmp)
688 return 0;
689 errstr = PyUnicode_FromString(errmsg);
690 if (!errstr) {
691 Py_DECREF(tmp);
692 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000693 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 Py_DECREF(errstr);
696 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400697 if (value) {
698 PyErr_SetObject(PyExc_SyntaxError, value);
699 Py_DECREF(value);
700 }
701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702}
703
704/* num_stmts() returns number of contained statements.
705
706 Use this routine to determine how big a sequence is needed for
707 the statements in a parse tree. Its raison d'etre is this bit of
708 grammar:
709
710 stmt: simple_stmt | compound_stmt
711 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
712
713 A simple_stmt can contain multiple small_stmt elements joined
714 by semicolons. If the arg is a simple_stmt, the number of
715 small_stmt elements is returned.
716*/
717
718static int
719num_stmts(const node *n)
720{
721 int i, l;
722 node *ch;
723
724 switch (TYPE(n)) {
725 case single_input:
726 if (TYPE(CHILD(n, 0)) == NEWLINE)
727 return 0;
728 else
729 return num_stmts(CHILD(n, 0));
730 case file_input:
731 l = 0;
732 for (i = 0; i < NCH(n); i++) {
733 ch = CHILD(n, i);
734 if (TYPE(ch) == stmt)
735 l += num_stmts(ch);
736 }
737 return l;
738 case stmt:
739 return num_stmts(CHILD(n, 0));
740 case compound_stmt:
741 return 1;
742 case simple_stmt:
743 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
744 case suite:
745 if (NCH(n) == 1)
746 return num_stmts(CHILD(n, 0));
747 else {
748 l = 0;
749 for (i = 2; i < (NCH(n) - 1); i++)
750 l += num_stmts(CHILD(n, i));
751 return l;
752 }
753 default: {
754 char buf[128];
755
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000756 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 TYPE(n), NCH(n));
758 Py_FatalError(buf);
759 }
760 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700761 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762}
763
764/* Transform the CST rooted at node * to the appropriate AST
765*/
766
767mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200768PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
769 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000771 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 asdl_seq *stmts = NULL;
773 stmt_ty s;
774 node *ch;
775 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500776 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400778 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200779 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400780 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800781 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800782
783 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
Jeremy Hyltona8293132006-02-28 17:58:27 +0000786 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 switch (TYPE(n)) {
788 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200789 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500791 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 for (i = 0; i < NCH(n) - 1; i++) {
793 ch = CHILD(n, i);
794 if (TYPE(ch) == NEWLINE)
795 continue;
796 REQ(ch, stmt);
797 num = num_stmts(ch);
798 if (num == 1) {
799 s = ast_for_stmt(&c, ch);
800 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500801 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000802 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 }
804 else {
805 ch = CHILD(ch, 0);
806 REQ(ch, simple_stmt);
807 for (j = 0; j < num; j++) {
808 s = ast_for_stmt(&c, CHILD(ch, j * 2));
809 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500810 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000811 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 }
813 }
814 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300815 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500816 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 case eval_input: {
818 expr_ty testlist_ast;
819
Nick Coghlan650f0d02007-04-15 12:05:43 +0000820 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000821 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500823 goto out;
824 res = Expression(testlist_ast, arena);
825 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 }
827 case single_input:
828 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200829 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000832 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
833 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000834 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500835 goto out;
836 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 }
838 else {
839 n = CHILD(n, 0);
840 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200841 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000845 s = ast_for_stmt(&c, n);
846 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 asdl_seq_SET(stmts, 0, s);
849 }
850 else {
851 /* Only a simple_stmt can contain multiple statements. */
852 REQ(n, simple_stmt);
853 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 if (TYPE(CHILD(n, i)) == NEWLINE)
855 break;
856 s = ast_for_stmt(&c, CHILD(n, i));
857 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 asdl_seq_SET(stmts, i / 2, s);
860 }
861 }
862
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500865 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000867 PyErr_Format(PyExc_SystemError,
868 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500869 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 out:
872 if (c.c_normalize) {
873 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500874 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500875 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876}
877
Victor Stinner14e461d2013-08-26 22:28:21 +0200878mod_ty
879PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
880 PyArena *arena)
881{
882 mod_ty mod;
883 PyObject *filename;
884 filename = PyUnicode_DecodeFSDefault(filename_str);
885 if (filename == NULL)
886 return NULL;
887 mod = PyAST_FromNodeObject(n, flags, filename, arena);
888 Py_DECREF(filename);
889 return mod;
890
891}
892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
894*/
895
896static operator_ty
897get_operator(const node *n)
898{
899 switch (TYPE(n)) {
900 case VBAR:
901 return BitOr;
902 case CIRCUMFLEX:
903 return BitXor;
904 case AMPER:
905 return BitAnd;
906 case LEFTSHIFT:
907 return LShift;
908 case RIGHTSHIFT:
909 return RShift;
910 case PLUS:
911 return Add;
912 case MINUS:
913 return Sub;
914 case STAR:
915 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400916 case AT:
917 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918 case SLASH:
919 return Div;
920 case DOUBLESLASH:
921 return FloorDiv;
922 case PERCENT:
923 return Mod;
924 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 }
927}
928
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200929static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000930 "None",
931 "True",
932 "False",
933 NULL,
934};
935
936static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400937forbidden_name(struct compiling *c, identifier name, const node *n,
938 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000939{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000940 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200941 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400942 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000943 return 1;
944 }
945 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200946 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000947 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200948 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400949 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000950 return 1;
951 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000952 }
953 }
954 return 0;
955}
956
Jeremy Hyltona8293132006-02-28 17:58:27 +0000957/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958
959 Only sets context for expr kinds that "can appear in assignment context"
960 (according to ../Parser/Python.asdl). For other expr kinds, it sets
961 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962*/
963
964static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000965set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000968 /* If a particular expression type can't be used for assign / delete,
969 set expr_name to its name and an error message will be generated.
970 */
971 const char* expr_name = NULL;
972
973 /* The ast defines augmented store and load contexts, but the
974 implementation here doesn't actually use them. The code may be
975 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000976 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000977 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000978 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000979 */
980 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
982 switch (e->kind) {
983 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000984 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400985 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000986 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000987 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000989 e->v.Subscript.ctx = ctx;
990 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000991 case Starred_kind:
992 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000993 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000994 return 0;
995 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000997 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500998 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000999 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 }
1001 e->v.Name.ctx = ctx;
1002 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001004 e->v.List.ctx = ctx;
1005 s = e->v.List.elts;
1006 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001008 e->v.Tuple.ctx = ctx;
1009 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001010 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 case Lambda_kind:
1012 expr_name = "lambda";
1013 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001015 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001017 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001019 case UnaryOp_kind:
1020 expr_name = "operator";
1021 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001023 expr_name = "generator expression";
1024 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001025 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001026 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001027 expr_name = "yield expression";
1028 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001029 case Await_kind:
1030 expr_name = "await expression";
1031 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001032 case ListComp_kind:
1033 expr_name = "list comprehension";
1034 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001035 case SetComp_kind:
1036 expr_name = "set comprehension";
1037 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001038 case DictComp_kind:
1039 expr_name = "dict comprehension";
1040 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001041 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001042 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 case Num_kind:
1044 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001045 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001046 case JoinedStr_kind:
1047 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 expr_name = "literal";
1049 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001050 case NameConstant_kind:
1051 expr_name = "keyword";
1052 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001053 case Ellipsis_kind:
1054 expr_name = "Ellipsis";
1055 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001056 case Compare_kind:
1057 expr_name = "comparison";
1058 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001059 case IfExp_kind:
1060 expr_name = "conditional expression";
1061 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001062 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyErr_Format(PyExc_SystemError,
1064 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001065 e->kind, e->lineno);
1066 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001068 /* Check for error string set by switch */
1069 if (expr_name) {
1070 char buf[300];
1071 PyOS_snprintf(buf, sizeof(buf),
1072 "can't %s %s",
1073 ctx == Store ? "assign to" : "delete",
1074 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001075 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001076 }
1077
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 */
1081 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001082 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083
Thomas Wouters89f507f2006-12-13 04:49:30 +00001084 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001085 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001086 return 0;
1087 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 }
1089 return 1;
1090}
1091
1092static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001093ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094{
1095 REQ(n, augassign);
1096 n = CHILD(n, 0);
1097 switch (STR(n)[0]) {
1098 case '+':
1099 return Add;
1100 case '-':
1101 return Sub;
1102 case '/':
1103 if (STR(n)[1] == '/')
1104 return FloorDiv;
1105 else
1106 return Div;
1107 case '%':
1108 return Mod;
1109 case '<':
1110 return LShift;
1111 case '>':
1112 return RShift;
1113 case '&':
1114 return BitAnd;
1115 case '^':
1116 return BitXor;
1117 case '|':
1118 return BitOr;
1119 case '*':
1120 if (STR(n)[1] == '*')
1121 return Pow;
1122 else
1123 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001124 case '@':
1125 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001127 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001128 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 }
1130}
1131
1132static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001133ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001135 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 |'is' 'not'
1137 */
1138 REQ(n, comp_op);
1139 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001140 n = CHILD(n, 0);
1141 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 case LESS:
1143 return Lt;
1144 case GREATER:
1145 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001146 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return Eq;
1148 case LESSEQUAL:
1149 return LtE;
1150 case GREATEREQUAL:
1151 return GtE;
1152 case NOTEQUAL:
1153 return NotEq;
1154 case NAME:
1155 if (strcmp(STR(n), "in") == 0)
1156 return In;
1157 if (strcmp(STR(n), "is") == 0)
1158 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001159 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001161 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001164 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 }
1166 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001167 /* handle "not in" and "is not" */
1168 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 case NAME:
1170 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1171 return NotIn;
1172 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1173 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001174 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001176 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001178 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 }
Neal Norwitz79792652005-11-14 04:25:03 +00001181 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001183 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184}
1185
1186static asdl_seq *
1187seq_for_testlist(struct compiling *c, const node *n)
1188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001190 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1191 */
Armin Rigo31441302005-10-21 12:57:31 +00001192 asdl_seq *seq;
1193 expr_ty expression;
1194 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001195 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001197 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 if (!seq)
1199 return NULL;
1200
1201 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001203 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204
Benjamin Peterson4905e802009-09-27 02:43:28 +00001205 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208
1209 assert(i / 2 < seq->size);
1210 asdl_seq_SET(seq, i / 2, expression);
1211 }
1212 return seq;
1213}
1214
Neal Norwitzc1505362006-12-28 06:47:50 +00001215static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001216ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001217{
1218 identifier name;
1219 expr_ty annotation = NULL;
1220 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001221 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001222
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001223 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001224 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001225 name = NEW_IDENTIFIER(ch);
1226 if (!name)
1227 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001228 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001229 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001230
1231 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1232 annotation = ast_for_expr(c, CHILD(n, 2));
1233 if (!annotation)
1234 return NULL;
1235 }
1236
Victor Stinnerc106c682015-11-06 17:01:48 +01001237 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001238 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001239 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001240 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
Guido van Rossum4f72a782006-10-27 23:31:49 +00001243/* returns -1 if failed to handle keyword only arguments
1244 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001245 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 ^^^
1247 start pointing here
1248 */
1249static int
1250handle_keywordonly_args(struct compiling *c, const node *n, int start,
1251 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1252{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001253 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001254 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001255 expr_ty expression, annotation;
1256 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001257 int i = start;
1258 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001259
1260 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001261 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001262 return -1;
1263 }
1264 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001265 while (i < NCH(n)) {
1266 ch = CHILD(n, i);
1267 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001268 case vfpdef:
1269 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001270 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001271 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001272 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001273 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001274 asdl_seq_SET(kwdefaults, j, expression);
1275 i += 2; /* '=' and test */
1276 }
1277 else { /* setting NULL if no default value exists */
1278 asdl_seq_SET(kwdefaults, j, NULL);
1279 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001280 if (NCH(ch) == 3) {
1281 /* ch is NAME ':' test */
1282 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001283 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001284 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001285 }
1286 else {
1287 annotation = NULL;
1288 }
1289 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001290 argname = NEW_IDENTIFIER(ch);
1291 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001293 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001294 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001295 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1296 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001297 if (!arg)
1298 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 i += 2; /* the name and the comma */
1301 break;
1302 case DOUBLESTAR:
1303 return i;
1304 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001305 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 goto error;
1307 }
1308 }
1309 return i;
1310 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001312}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313
Jeremy Hyltona8293132006-02-28 17:58:27 +00001314/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315
1316static arguments_ty
1317ast_for_arguments(struct compiling *c, const node *n)
1318{
Neal Norwitzc1505362006-12-28 06:47:50 +00001319 /* This function handles both typedargslist (function definition)
1320 and varargslist (lambda definition).
1321
1322 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001323 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1324 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1325 | '**' tfpdef [',']]]
1326 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1327 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001328 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001329 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1330 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1331 | '**' vfpdef [',']]]
1332 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1333 | '**' vfpdef [',']
1334 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1339 int nposdefaults = 0, found_default = 0;
1340 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001342 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 node *ch;
1344
1345 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001347 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351
Jeremy Hyltone921e022008-07-17 16:37:17 +00001352 /* First count the number of positional args & defaults. The
1353 variable i is the loop index for this for loop and the next.
1354 The next loop picks up where the first leaves off.
1355 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 ch = CHILD(n, i);
1358 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001359 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001360 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001361 if (i < NCH(n) && /* skip argument following star */
1362 (TYPE(CHILD(n, i)) == tfpdef ||
1363 TYPE(CHILD(n, i)) == vfpdef)) {
1364 i++;
1365 }
1366 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 defaults for keyword only args */
1374 for ( ; i < NCH(n); ++i) {
1375 ch = CHILD(n, i);
1376 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001377 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001379 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001381 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001383 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001387 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001388 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001389 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 since we set NULL as default for keyword only argument w/o default
1392 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001393 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001394 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001396 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001398 /* tfpdef: NAME [':' test]
1399 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 */
1401 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001402 j = 0; /* index for defaults */
1403 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 ch = CHILD(n, i);
1406 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001407 case tfpdef:
1408 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1410 anything other than EQUAL or a comma? */
1411 /* XXX Should NCH(n) check be made a separate check? */
1412 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001413 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1414 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001415 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 assert(posdefaults != NULL);
1417 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001422 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001424 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001425 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001426 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001427 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001428 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 i += 2; /* the name and the comma */
1431 break;
1432 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001433 if (i+1 >= NCH(n) ||
1434 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001435 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001436 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001437 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001439 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001440 if (TYPE(ch) == COMMA) {
1441 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001442 i += 2; /* now follows keyword only arguments */
1443 res = handle_keywordonly_args(c, n, i,
1444 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001445 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 i = res; /* res has new position to process */
1447 }
1448 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001449 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001450 if (!vararg)
1451 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001452
Guido van Rossum4f72a782006-10-27 23:31:49 +00001453 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001454 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1455 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001456 int res = 0;
1457 res = handle_keywordonly_args(c, n, i,
1458 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001459 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 i = res; /* res has new position to process */
1461 }
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 break;
1464 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001465 ch = CHILD(n, i+1); /* tfpdef */
1466 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001467 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001468 if (!kwarg)
1469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 i += 3;
1471 break;
1472 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001473 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 "unexpected node in varargslist: %d @ %d",
1475 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001476 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001479 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480}
1481
1482static expr_ty
1483ast_for_dotted_name(struct compiling *c, const node *n)
1484{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001485 expr_ty e;
1486 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001487 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 int i;
1489
1490 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001491
1492 lineno = LINENO(n);
1493 col_offset = n->n_col_offset;
1494
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 id = NEW_IDENTIFIER(CHILD(n, 0));
1496 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001497 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001498 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501
1502 for (i = 2; i < NCH(n); i+=2) {
1503 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001504 if (!id)
1505 return NULL;
1506 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1507 if (!e)
1508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 }
1510
1511 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512}
1513
1514static expr_ty
1515ast_for_decorator(struct compiling *c, const node *n)
1516{
1517 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1518 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001519 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001522 REQ(CHILD(n, 0), AT);
1523 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1526 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001527 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001530 d = name_expr;
1531 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 }
1533 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001534 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 if (!d)
1537 return NULL;
1538 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 }
1540 else {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02001541 d = ast_for_call(c, CHILD(n, 3), name_expr, true);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 if (!d)
1543 return NULL;
1544 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 }
1546
1547 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548}
1549
1550static asdl_seq*
1551ast_for_decorators(struct compiling *c, const node *n)
1552{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001553 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001554 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001558 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 if (!decorator_seq)
1560 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001563 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001564 if (!d)
1565 return NULL;
1566 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 }
1568 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569}
1570
1571static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001572ast_for_funcdef_impl(struct compiling *c, const node *n,
1573 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001575 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001576 identifier name;
1577 arguments_ty args;
1578 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001579 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001580 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581
1582 REQ(n, funcdef);
1583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 name = NEW_IDENTIFIER(CHILD(n, name_i));
1585 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001586 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001587 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001588 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1590 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001591 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001592 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1593 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1594 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001595 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001596 name_i += 2;
1597 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001598 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001600 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601
Yury Selivanov75445082015-05-11 22:57:16 -04001602 if (is_async)
1603 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001604 LINENO(n), n->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001605 else
1606 return FunctionDef(name, args, body, decorator_seq, returns,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001607 LINENO(n), n->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001608}
1609
1610static stmt_ty
1611ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1612{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001613 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001614 REQ(n, async_funcdef);
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 REQ(CHILD(n, 1), funcdef);
1618
1619 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1620 1 /* is_async */);
1621}
1622
1623static stmt_ty
1624ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1625{
1626 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1627 return ast_for_funcdef_impl(c, n, decorator_seq,
1628 0 /* is_async */);
1629}
1630
1631
1632static stmt_ty
1633ast_for_async_stmt(struct compiling *c, const node *n)
1634{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001635 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001636 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001637 REQ(CHILD(n, 0), NAME);
1638 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001639
1640 switch (TYPE(CHILD(n, 1))) {
1641 case funcdef:
1642 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1643 1 /* is_async */);
1644 case with_stmt:
1645 return ast_for_with_stmt(c, CHILD(n, 1),
1646 1 /* is_async */);
1647
1648 case for_stmt:
1649 return ast_for_for_stmt(c, CHILD(n, 1),
1650 1 /* is_async */);
1651
1652 default:
1653 PyErr_Format(PyExc_SystemError,
1654 "invalid async stament: %s",
1655 STR(CHILD(n, 1)));
1656 return NULL;
1657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658}
1659
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001660static stmt_ty
1661ast_for_decorated(struct compiling *c, const node *n)
1662{
Yury Selivanov75445082015-05-11 22:57:16 -04001663 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001664 stmt_ty thing = NULL;
1665 asdl_seq *decorator_seq = NULL;
1666
1667 REQ(n, decorated);
1668
1669 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1670 if (!decorator_seq)
1671 return NULL;
1672
1673 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001674 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001676
1677 if (TYPE(CHILD(n, 1)) == funcdef) {
1678 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1679 } else if (TYPE(CHILD(n, 1)) == classdef) {
1680 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001681 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1682 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001683 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001684 /* we count the decorators in when talking about the class' or
1685 * function's line number */
1686 if (thing) {
1687 thing->lineno = LINENO(n);
1688 thing->col_offset = n->n_col_offset;
1689 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001690 return thing;
1691}
1692
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693static expr_ty
1694ast_for_lambdef(struct compiling *c, const node *n)
1695{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001696 /* lambdef: 'lambda' [varargslist] ':' test
1697 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 arguments_ty args;
1699 expr_ty expression;
1700
1701 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001702 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 if (!args)
1704 return NULL;
1705 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 }
1709 else {
1710 args = ast_for_arguments(c, CHILD(n, 1));
1711 if (!args)
1712 return NULL;
1713 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001714 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 }
1717
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001718 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719}
1720
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001721static expr_ty
1722ast_for_ifexpr(struct compiling *c, const node *n)
1723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001725 expr_ty expression, body, orelse;
1726
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001727 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001728 body = ast_for_expr(c, CHILD(n, 0));
1729 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001730 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001731 expression = ast_for_expr(c, CHILD(n, 2));
1732 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001733 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001734 orelse = ast_for_expr(c, CHILD(n, 4));
1735 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001736 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001737 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1738 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001739}
1740
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001742 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743
Nick Coghlan650f0d02007-04-15 12:05:43 +00001744 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745*/
1746
1747static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001748count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001750 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751
Guido van Rossumd8faa362007-04-27 19:54:29 +00001752 count_comp_for:
1753 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001754 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001755 if (NCH(n) == 2) {
1756 REQ(CHILD(n, 0), NAME);
1757 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1758 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001759 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001760 else if (NCH(n) == 1) {
1761 n = CHILD(n, 0);
1762 }
1763 else {
1764 goto error;
1765 }
1766 if (NCH(n) == (5)) {
1767 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001768 }
1769 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001770 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001771 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001772 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001773 REQ(n, comp_iter);
1774 n = CHILD(n, 0);
1775 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001776 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001777 else if (TYPE(n) == comp_if) {
1778 if (NCH(n) == 3) {
1779 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001781 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001782 else
1783 return n_fors;
1784 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001785
Jelle Zijlstraac317702017-10-05 20:24:46 -07001786 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001787 /* Should never be reached */
1788 PyErr_SetString(PyExc_SystemError,
1789 "logic error in count_comp_fors");
1790 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791}
1792
Nick Coghlan650f0d02007-04-15 12:05:43 +00001793/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794
Nick Coghlan650f0d02007-04-15 12:05:43 +00001795 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796*/
1797
1798static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001799count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001801 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802
Guido van Rossumd8faa362007-04-27 19:54:29 +00001803 while (1) {
1804 REQ(n, comp_iter);
1805 if (TYPE(CHILD(n, 0)) == comp_for)
1806 return n_ifs;
1807 n = CHILD(n, 0);
1808 REQ(n, comp_if);
1809 n_ifs++;
1810 if (NCH(n) == 2)
1811 return n_ifs;
1812 n = CHILD(n, 2);
1813 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814}
1815
Guido van Rossum992d4a32007-07-11 13:09:30 +00001816static asdl_seq *
1817ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001820 asdl_seq *comps;
1821
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001822 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 if (n_fors == -1)
1824 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001826 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001827 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001829
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001831 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001833 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001834 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001835 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001836 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837
Guido van Rossum992d4a32007-07-11 13:09:30 +00001838 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839
Jelle Zijlstraac317702017-10-05 20:24:46 -07001840 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001841 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001842 REQ(CHILD(n, 0), NAME);
1843 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1844 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001845 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001846 else {
1847 sync_n = CHILD(n, 0);
1848 }
1849 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001850
Jelle Zijlstraac317702017-10-05 20:24:46 -07001851 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001852 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001853 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001855 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858
Thomas Wouters89f507f2006-12-13 04:49:30 +00001859 /* Check the # of children rather than the length of t, since
1860 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001861 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001863 comp = comprehension(first, expression, NULL,
1864 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001866 comp = comprehension(Tuple(t, Store, first->lineno,
1867 first->col_offset, c->c_arena),
1868 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001869 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871
Jelle Zijlstraac317702017-10-05 20:24:46 -07001872 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 int j, n_ifs;
1874 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875
Jelle Zijlstraac317702017-10-05 20:24:46 -07001876 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001877 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001878 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001880
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001881 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001886 REQ(n, comp_iter);
1887 n = CHILD(n, 0);
1888 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889
Guido van Rossum992d4a32007-07-11 13:09:30 +00001890 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001892 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001893 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001894 if (NCH(n) == 3)
1895 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001897 /* on exit, must guarantee that n is a comp_for */
1898 if (TYPE(n) == comp_iter)
1899 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001900 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001902 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 return comps;
1905}
1906
1907static expr_ty
1908ast_for_itercomp(struct compiling *c, const node *n, int type)
1909{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001910 /* testlist_comp: (test|star_expr)
1911 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001912 expr_ty elt;
1913 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915
Guido van Rossum992d4a32007-07-11 13:09:30 +00001916 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918 ch = CHILD(n, 0);
1919 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001920 if (!elt)
1921 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001922 if (elt->kind == Starred_kind) {
1923 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1924 return NULL;
1925 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926
Guido van Rossum992d4a32007-07-11 13:09:30 +00001927 comps = ast_for_comprehension(c, CHILD(n, 1));
1928 if (!comps)
1929 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001930
1931 if (type == COMP_GENEXP)
1932 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1933 else if (type == COMP_LISTCOMP)
1934 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1935 else if (type == COMP_SETCOMP)
1936 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1937 else
1938 /* Should never happen */
1939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940}
1941
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001942/* Fills in the key, value pair corresponding to the dict element. In case
1943 * of an unpacking, key is NULL. *i is advanced by the number of ast
1944 * elements. Iff successful, nonzero is returned.
1945 */
1946static int
1947ast_for_dictelement(struct compiling *c, const node *n, int *i,
1948 expr_ty *key, expr_ty *value)
1949{
1950 expr_ty expression;
1951 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1952 assert(NCH(n) - *i >= 2);
1953
1954 expression = ast_for_expr(c, CHILD(n, *i + 1));
1955 if (!expression)
1956 return 0;
1957 *key = NULL;
1958 *value = expression;
1959
1960 *i += 2;
1961 }
1962 else {
1963 assert(NCH(n) - *i >= 3);
1964
1965 expression = ast_for_expr(c, CHILD(n, *i));
1966 if (!expression)
1967 return 0;
1968 *key = expression;
1969
1970 REQ(CHILD(n, *i + 1), COLON);
1971
1972 expression = ast_for_expr(c, CHILD(n, *i + 2));
1973 if (!expression)
1974 return 0;
1975 *value = expression;
1976
1977 *i += 3;
1978 }
1979 return 1;
1980}
1981
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001983ast_for_dictcomp(struct compiling *c, const node *n)
1984{
1985 expr_ty key, value;
1986 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001987 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001989 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001990 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001991 assert(key);
1992 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001994 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001995 if (!comps)
1996 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997
Guido van Rossum992d4a32007-07-11 13:09:30 +00001998 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1999}
2000
2001static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002002ast_for_dictdisplay(struct compiling *c, const node *n)
2003{
2004 int i;
2005 int j;
2006 int size;
2007 asdl_seq *keys, *values;
2008
2009 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2010 keys = _Py_asdl_seq_new(size, c->c_arena);
2011 if (!keys)
2012 return NULL;
2013
2014 values = _Py_asdl_seq_new(size, c->c_arena);
2015 if (!values)
2016 return NULL;
2017
2018 j = 0;
2019 for (i = 0; i < NCH(n); i++) {
2020 expr_ty key, value;
2021
2022 if (!ast_for_dictelement(c, n, &i, &key, &value))
2023 return NULL;
2024 asdl_seq_SET(keys, j, key);
2025 asdl_seq_SET(values, j, value);
2026
2027 j++;
2028 }
2029 keys->size = j;
2030 values->size = j;
2031 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2032}
2033
2034static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002035ast_for_genexp(struct compiling *c, const node *n)
2036{
2037 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002038 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002039}
2040
2041static expr_ty
2042ast_for_listcomp(struct compiling *c, const node *n)
2043{
2044 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002045 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002046}
2047
2048static expr_ty
2049ast_for_setcomp(struct compiling *c, const node *n)
2050{
2051 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002052 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002053}
2054
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002055static expr_ty
2056ast_for_setdisplay(struct compiling *c, const node *n)
2057{
2058 int i;
2059 int size;
2060 asdl_seq *elts;
2061
2062 assert(TYPE(n) == (dictorsetmaker));
2063 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2064 elts = _Py_asdl_seq_new(size, c->c_arena);
2065 if (!elts)
2066 return NULL;
2067 for (i = 0; i < NCH(n); i += 2) {
2068 expr_ty expression;
2069 expression = ast_for_expr(c, CHILD(n, i));
2070 if (!expression)
2071 return NULL;
2072 asdl_seq_SET(elts, i / 2, expression);
2073 }
2074 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2075}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002076
2077static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078ast_for_atom(struct compiling *c, const node *n)
2079{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2081 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002082 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 */
2084 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002087 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002088 PyObject *name;
2089 const char *s = STR(ch);
2090 size_t len = strlen(s);
2091 if (len >= 4 && len <= 5) {
2092 if (!strcmp(s, "None"))
2093 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2094 if (!strcmp(s, "True"))
2095 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2096 if (!strcmp(s, "False"))
2097 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2098 }
2099 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002100 if (!name)
2101 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002102 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002103 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2104 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002106 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002107 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002108 const char *errtype = NULL;
2109 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2110 errtype = "unicode error";
2111 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2112 errtype = "value error";
2113 if (errtype) {
2114 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002115 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002116 PyObject *type, *value, *tback, *errstr;
2117 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002118 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002119 if (errstr)
2120 s = PyUnicode_AsUTF8(errstr);
2121 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002122 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002123 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002124 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002125 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002126 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002127 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002128 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002129 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002130 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002131 Py_XDECREF(tback);
2132 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002133 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002134 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002135 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 }
2137 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002138 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 if (!pynum)
2140 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002141
Victor Stinner43d81952013-07-17 00:57:58 +02002142 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2143 Py_DECREF(pynum);
2144 return NULL;
2145 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002146 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 }
Georg Brandldde00282007-03-18 19:01:53 +00002148 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002149 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002151 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152
Thomas Wouters89f507f2006-12-13 04:49:30 +00002153 if (TYPE(ch) == RPAR)
2154 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155
Thomas Wouters89f507f2006-12-13 04:49:30 +00002156 if (TYPE(ch) == yield_expr)
2157 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002160 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002162
Nick Coghlan650f0d02007-04-15 12:05:43 +00002163 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 if (TYPE(ch) == RSQB)
2168 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169
Nick Coghlan650f0d02007-04-15 12:05:43 +00002170 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2172 asdl_seq *elts = seq_for_testlist(c, ch);
2173 if (!elts)
2174 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002175
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2177 }
2178 else
2179 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002181 /* dictorsetmaker: ( ((test ':' test | '**' test)
2182 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2183 * ((test | '*' test)
2184 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002185 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002186 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002187 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002188 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002189 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002190 }
2191 else {
2192 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2193 if (NCH(ch) == 1 ||
2194 (NCH(ch) > 1 &&
2195 TYPE(CHILD(ch, 1)) == COMMA)) {
2196 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002197 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002198 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002199 else if (NCH(ch) > 1 &&
2200 TYPE(CHILD(ch, 1)) == comp_for) {
2201 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002202 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002203 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002204 else if (NCH(ch) > 3 - is_dict &&
2205 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2206 /* It's a dictionary comprehension. */
2207 if (is_dict) {
2208 ast_error(c, n, "dict unpacking cannot be used in "
2209 "dict comprehension");
2210 return NULL;
2211 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002212 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002213 }
2214 else {
2215 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002216 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002217 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002218 if (res) {
2219 res->lineno = LINENO(n);
2220 res->col_offset = n->n_col_offset;
2221 }
2222 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002223 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002226 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2227 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 }
2229}
2230
2231static slice_ty
2232ast_for_slice(struct compiling *c, const node *n)
2233{
2234 node *ch;
2235 expr_ty lower = NULL, upper = NULL, step = NULL;
2236
2237 REQ(n, subscript);
2238
2239 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002240 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 sliceop: ':' [test]
2242 */
2243 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 if (NCH(n) == 1 && TYPE(ch) == test) {
2245 /* 'step' variable hold no significance in terms of being used over
2246 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 if (!step)
2249 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250
Thomas Wouters89f507f2006-12-13 04:49:30 +00002251 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 }
2253
2254 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002255 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 if (!lower)
2257 return NULL;
2258 }
2259
2260 /* If there's an upper bound it's in the second or third position. */
2261 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002262 if (NCH(n) > 1) {
2263 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264
Thomas Wouters89f507f2006-12-13 04:49:30 +00002265 if (TYPE(n2) == test) {
2266 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 if (!upper)
2268 return NULL;
2269 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273
Thomas Wouters89f507f2006-12-13 04:49:30 +00002274 if (TYPE(n2) == test) {
2275 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 if (!upper)
2277 return NULL;
2278 }
2279 }
2280
2281 ch = CHILD(n, NCH(n) - 1);
2282 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002283 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002284 ch = CHILD(ch, 1);
2285 if (TYPE(ch) == test) {
2286 step = ast_for_expr(c, ch);
2287 if (!step)
2288 return NULL;
2289 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 }
2291 }
2292
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002293 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294}
2295
2296static expr_ty
2297ast_for_binop(struct compiling *c, const node *n)
2298{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002299 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002301 BinOp(BinOp(A, op, B), op, C).
2302 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303
Guido van Rossumd8faa362007-04-27 19:54:29 +00002304 int i, nops;
2305 expr_ty expr1, expr2, result;
2306 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307
Guido van Rossumd8faa362007-04-27 19:54:29 +00002308 expr1 = ast_for_expr(c, CHILD(n, 0));
2309 if (!expr1)
2310 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311
Guido van Rossumd8faa362007-04-27 19:54:29 +00002312 expr2 = ast_for_expr(c, CHILD(n, 2));
2313 if (!expr2)
2314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315
Guido van Rossumd8faa362007-04-27 19:54:29 +00002316 newoperator = get_operator(CHILD(n, 1));
2317 if (!newoperator)
2318 return NULL;
2319
2320 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2321 c->c_arena);
2322 if (!result)
2323 return NULL;
2324
2325 nops = (NCH(n) - 1) / 2;
2326 for (i = 1; i < nops; i++) {
2327 expr_ty tmp_result, tmp;
2328 const node* next_oper = CHILD(n, i * 2 + 1);
2329
2330 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002331 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 return NULL;
2333
Guido van Rossumd8faa362007-04-27 19:54:29 +00002334 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2335 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 return NULL;
2337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002339 LINENO(next_oper), next_oper->n_col_offset,
2340 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002342 return NULL;
2343 result = tmp_result;
2344 }
2345 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346}
2347
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002348static expr_ty
2349ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002352 subscriptlist: subscript (',' subscript)* [',']
2353 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2354 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002355 REQ(n, trailer);
2356 if (TYPE(CHILD(n, 0)) == LPAR) {
2357 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002358 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002360 else
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002361 return ast_for_call(c, CHILD(n, 1), left_expr, true);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002362 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002363 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002364 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2365 if (!attr_id)
2366 return NULL;
2367 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002368 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002369 }
2370 else {
2371 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 REQ(CHILD(n, 2), RSQB);
2373 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2376 if (!slc)
2377 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002378 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2379 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002380 }
2381 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002383 by treating the sequence as a tuple literal if there are
2384 no slice features.
2385 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 int j;
2387 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002388 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002389 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002390 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002391 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 if (!slices)
2393 return NULL;
2394 for (j = 0; j < NCH(n); j += 2) {
2395 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002396 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002397 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002399 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002400 asdl_seq_SET(slices, j / 2, slc);
2401 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002402 if (!simple) {
2403 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002404 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002405 }
2406 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002407 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002408 if (!elts)
2409 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002410 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2411 slc = (slice_ty)asdl_seq_GET(slices, j);
2412 assert(slc->kind == Index_kind && slc->v.Index.value);
2413 asdl_seq_SET(elts, j, slc->v.Index.value);
2414 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002415 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002416 if (!e)
2417 return NULL;
2418 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002419 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002420 }
2421 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002422}
2423
2424static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002425ast_for_factor(struct compiling *c, const node *n)
2426{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002427 expr_ty expression;
2428
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002429 expression = ast_for_expr(c, CHILD(n, 1));
2430 if (!expression)
2431 return NULL;
2432
2433 switch (TYPE(CHILD(n, 0))) {
2434 case PLUS:
2435 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2436 c->c_arena);
2437 case MINUS:
2438 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2439 c->c_arena);
2440 case TILDE:
2441 return UnaryOp(Invert, expression, LINENO(n),
2442 n->n_col_offset, c->c_arena);
2443 }
2444 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2445 TYPE(CHILD(n, 0)));
2446 return NULL;
2447}
2448
2449static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002450ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002451{
Yury Selivanov75445082015-05-11 22:57:16 -04002452 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002453 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002454
2455 REQ(n, atom_expr);
2456 nch = NCH(n);
2457
Jelle Zijlstraac317702017-10-05 20:24:46 -07002458 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002459 start = 1;
2460 assert(nch > 1);
2461 }
2462
2463 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002464 if (!e)
2465 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002466 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002467 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002468 if (start && nch == 2) {
2469 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2470 }
2471
2472 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002473 node *ch = CHILD(n, i);
2474 if (TYPE(ch) != trailer)
2475 break;
2476 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002477 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002478 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002479 tmp->lineno = e->lineno;
2480 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002481 e = tmp;
2482 }
Yury Selivanov75445082015-05-11 22:57:16 -04002483
2484 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002485 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002486 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2487 }
2488 else {
2489 return e;
2490 }
2491}
2492
2493static expr_ty
2494ast_for_power(struct compiling *c, const node *n)
2495{
2496 /* power: atom trailer* ('**' factor)*
2497 */
2498 expr_ty e;
2499 REQ(n, power);
2500 e = ast_for_atom_expr(c, CHILD(n, 0));
2501 if (!e)
2502 return NULL;
2503 if (NCH(n) == 1)
2504 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002505 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2506 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002507 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002508 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002509 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002510 }
2511 return e;
2512}
2513
Guido van Rossum0368b722007-05-11 16:50:42 +00002514static expr_ty
2515ast_for_starred(struct compiling *c, const node *n)
2516{
2517 expr_ty tmp;
2518 REQ(n, star_expr);
2519
2520 tmp = ast_for_expr(c, CHILD(n, 1));
2521 if (!tmp)
2522 return NULL;
2523
2524 /* The Load context is changed later. */
2525 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2526}
2527
2528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529/* Do not name a variable 'expr'! Will cause a compile error.
2530*/
2531
2532static expr_ty
2533ast_for_expr(struct compiling *c, const node *n)
2534{
2535 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002536 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002537 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 and_test: not_test ('and' not_test)*
2540 not_test: 'not' not_test | comparison
2541 comparison: expr (comp_op expr)*
2542 expr: xor_expr ('|' xor_expr)*
2543 xor_expr: and_expr ('^' and_expr)*
2544 and_expr: shift_expr ('&' shift_expr)*
2545 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2546 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002547 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002549 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002550 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002551 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 */
2553
2554 asdl_seq *seq;
2555 int i;
2556
2557 loop:
2558 switch (TYPE(n)) {
2559 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002560 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002561 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002562 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002564 else if (NCH(n) > 1)
2565 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002566 /* Fallthrough */
2567 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 case and_test:
2569 if (NCH(n) == 1) {
2570 n = CHILD(n, 0);
2571 goto loop;
2572 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002573 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 if (!seq)
2575 return NULL;
2576 for (i = 0; i < NCH(n); i += 2) {
2577 expr_ty e = ast_for_expr(c, CHILD(n, i));
2578 if (!e)
2579 return NULL;
2580 asdl_seq_SET(seq, i / 2, e);
2581 }
2582 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002583 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2584 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002585 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002586 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 case not_test:
2588 if (NCH(n) == 1) {
2589 n = CHILD(n, 0);
2590 goto loop;
2591 }
2592 else {
2593 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2594 if (!expression)
2595 return NULL;
2596
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002597 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2598 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 }
2600 case comparison:
2601 if (NCH(n) == 1) {
2602 n = CHILD(n, 0);
2603 goto loop;
2604 }
2605 else {
2606 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002608 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002609 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 if (!ops)
2611 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002612 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 return NULL;
2615 }
2616 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002617 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002619 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002620 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623
2624 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002625 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002627 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002629 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 asdl_seq_SET(cmps, i / 2, expression);
2631 }
2632 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002633 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002637 return Compare(expression, ops, cmps, LINENO(n),
2638 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 }
2640 break;
2641
Guido van Rossum0368b722007-05-11 16:50:42 +00002642 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 /* The next five cases all handle BinOps. The main body of code
2645 is the same in each case, but the switch turned inside out to
2646 reuse the code for each type of operator.
2647 */
2648 case expr:
2649 case xor_expr:
2650 case and_expr:
2651 case shift_expr:
2652 case arith_expr:
2653 case term:
2654 if (NCH(n) == 1) {
2655 n = CHILD(n, 0);
2656 goto loop;
2657 }
2658 return ast_for_binop(c, n);
2659 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002660 node *an = NULL;
2661 node *en = NULL;
2662 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002663 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002664 if (NCH(n) > 1)
2665 an = CHILD(n, 1); /* yield_arg */
2666 if (an) {
2667 en = CHILD(an, NCH(an) - 1);
2668 if (NCH(an) == 2) {
2669 is_from = 1;
2670 exp = ast_for_expr(c, en);
2671 }
2672 else
2673 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 if (!exp)
2675 return NULL;
2676 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002677 if (is_from)
2678 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2679 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002680 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002681 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 if (NCH(n) == 1) {
2683 n = CHILD(n, 0);
2684 goto loop;
2685 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002686 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002687 case power:
2688 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002690 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 return NULL;
2692 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002693 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 return NULL;
2695}
2696
2697static expr_ty
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002698ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699{
2700 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002701 arglist: argument (',' argument)* [',']
2702 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 */
2704
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002705 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002706 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002707 asdl_seq *args;
2708 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709
2710 REQ(n, arglist);
2711
2712 nargs = 0;
2713 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002715 node *ch = CHILD(n, i);
2716 if (TYPE(ch) == argument) {
2717 if (NCH(ch) == 1)
2718 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002719 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2720 nargs++;
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002721 if (!allowgen) {
2722 ast_error(c, ch, "invalid syntax");
2723 return NULL;
2724 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002725 if (NCH(n) > 1) {
2726 ast_error(c, ch, "Generator expression must be parenthesized");
2727 return NULL;
2728 }
2729 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 else if (TYPE(CHILD(ch, 0)) == STAR)
2731 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002733 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002734 nkeywords++;
2735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002738 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002740 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002741 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002743 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744
2745 nargs = 0; /* positional arguments + iterable argument unpackings */
2746 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2747 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002749 node *ch = CHILD(n, i);
2750 if (TYPE(ch) == argument) {
2751 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002753 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002754 /* a positional argument */
2755 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002756 if (ndoublestars) {
2757 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002758 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002759 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002761 else {
2762 ast_error(c, chch,
2763 "positional argument follows "
2764 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002766 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002767 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002768 e = ast_for_expr(c, chch);
2769 if (!e)
2770 return NULL;
2771 asdl_seq_SET(args, nargs++, e);
2772 }
2773 else if (TYPE(chch) == STAR) {
2774 /* an iterable argument unpacking */
2775 expr_ty starred;
2776 if (ndoublestars) {
2777 ast_error(c, chch,
2778 "iterable argument unpacking follows "
2779 "keyword argument unpacking");
2780 return NULL;
2781 }
2782 e = ast_for_expr(c, CHILD(ch, 1));
2783 if (!e)
2784 return NULL;
2785 starred = Starred(e, Load, LINENO(chch),
2786 chch->n_col_offset,
2787 c->c_arena);
2788 if (!starred)
2789 return NULL;
2790 asdl_seq_SET(args, nargs++, starred);
2791
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002792 }
2793 else if (TYPE(chch) == DOUBLESTAR) {
2794 /* a keyword argument unpacking */
2795 keyword_ty kw;
2796 i++;
2797 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002799 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 kw = keyword(NULL, e, c->c_arena);
2801 asdl_seq_SET(keywords, nkeywords++, kw);
2802 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002804 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002805 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002806 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002808 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002809 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002811 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002814 identifier key, tmp;
2815 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002817 /* chch is test, but must be an identifier? */
2818 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 /* f(lambda x: x[0] = 3) ends up getting parsed with
2822 * LHS test = lambda x: x[0], and RHS test = 3.
2823 * SF bug 132313 points out that complaining about a keyword
2824 * then is very confusing.
2825 */
2826 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002827 ast_error(c, chch,
2828 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002829 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002830 }
2831 else if (e->kind != Name_kind) {
2832 ast_error(c, chch,
2833 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002834 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002835 }
2836 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002837 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002839 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002840 for (k = 0; k < nkeywords; k++) {
2841 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 if (tmp && !PyUnicode_Compare(tmp, key)) {
2843 ast_error(c, chch,
2844 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002845 return NULL;
2846 }
2847 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002848 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002850 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002851 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002853 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 asdl_seq_SET(keywords, nkeywords++, kw);
2855 }
2856 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
2858
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002859 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860}
2861
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002863ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002865 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002866 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002868 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002869 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002870 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002871 }
2872 else {
2873 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002874 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 else {
2879 asdl_seq *tmp = seq_for_testlist(c, n);
2880 if (!tmp)
2881 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002884}
2885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886static stmt_ty
2887ast_for_expr_stmt(struct compiling *c, const node *n)
2888{
2889 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002890 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2891 ('=' (yield_expr|testlist_star_expr))*)
2892 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002893 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002894 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002895 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002896 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 */
2898
2899 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002900 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 if (!e)
2902 return NULL;
2903
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 }
2906 else if (TYPE(CHILD(n, 1)) == augassign) {
2907 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 if (!expr1)
2913 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002914 if(!set_context(c, expr1, Store, ch))
2915 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002916 /* set_context checks that most expressions are not the left side.
2917 Augmented assignments can only have a name, a subscript, or an
2918 attribute on the left, though, so we have to explicitly check for
2919 those. */
2920 switch (expr1->kind) {
2921 case Name_kind:
2922 case Attribute_kind:
2923 case Subscript_kind:
2924 break;
2925 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002926 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002927 return NULL;
2928 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929
Thomas Wouters89f507f2006-12-13 04:49:30 +00002930 ch = CHILD(n, 2);
2931 if (TYPE(ch) == testlist)
2932 expr2 = ast_for_testlist(c, ch);
2933 else
2934 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002935 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 return NULL;
2937
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002938 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002939 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 return NULL;
2941
Thomas Wouters89f507f2006-12-13 04:49:30 +00002942 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002944 else if (TYPE(CHILD(n, 1)) == annassign) {
2945 expr_ty expr1, expr2, expr3;
2946 node *ch = CHILD(n, 0);
2947 node *deep, *ann = CHILD(n, 1);
2948 int simple = 1;
2949
2950 /* we keep track of parens to qualify (x) as expression not name */
2951 deep = ch;
2952 while (NCH(deep) == 1) {
2953 deep = CHILD(deep, 0);
2954 }
2955 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2956 simple = 0;
2957 }
2958 expr1 = ast_for_testlist(c, ch);
2959 if (!expr1) {
2960 return NULL;
2961 }
2962 switch (expr1->kind) {
2963 case Name_kind:
2964 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2965 return NULL;
2966 }
2967 expr1->v.Name.ctx = Store;
2968 break;
2969 case Attribute_kind:
2970 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2971 return NULL;
2972 }
2973 expr1->v.Attribute.ctx = Store;
2974 break;
2975 case Subscript_kind:
2976 expr1->v.Subscript.ctx = Store;
2977 break;
2978 case List_kind:
2979 ast_error(c, ch,
2980 "only single target (not list) can be annotated");
2981 return NULL;
2982 case Tuple_kind:
2983 ast_error(c, ch,
2984 "only single target (not tuple) can be annotated");
2985 return NULL;
2986 default:
2987 ast_error(c, ch,
2988 "illegal target for annotation");
2989 return NULL;
2990 }
2991
2992 if (expr1->kind != Name_kind) {
2993 simple = 0;
2994 }
2995 ch = CHILD(ann, 1);
2996 expr2 = ast_for_expr(c, ch);
2997 if (!expr2) {
2998 return NULL;
2999 }
3000 if (NCH(ann) == 2) {
3001 return AnnAssign(expr1, expr2, NULL, simple,
3002 LINENO(n), n->n_col_offset, c->c_arena);
3003 }
3004 else {
3005 ch = CHILD(ann, 3);
3006 expr3 = ast_for_expr(c, ch);
3007 if (!expr3) {
3008 return NULL;
3009 }
3010 return AnnAssign(expr1, expr2, expr3, simple,
3011 LINENO(n), n->n_col_offset, c->c_arena);
3012 }
3013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003015 int i;
3016 asdl_seq *targets;
3017 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 expr_ty expression;
3019
Thomas Wouters89f507f2006-12-13 04:49:30 +00003020 /* a normal assignment */
3021 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003022 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003023 if (!targets)
3024 return NULL;
3025 for (i = 0; i < NCH(n) - 2; i += 2) {
3026 expr_ty e;
3027 node *ch = CHILD(n, i);
3028 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003029 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003030 return NULL;
3031 }
3032 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003036 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003037 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039
Thomas Wouters89f507f2006-12-13 04:49:30 +00003040 asdl_seq_SET(targets, i / 2, e);
3041 }
3042 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003043 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003044 expression = ast_for_testlist(c, value);
3045 else
3046 expression = ast_for_expr(c, value);
3047 if (!expression)
3048 return NULL;
3049 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051}
3052
Benjamin Peterson78565b22009-06-28 19:19:51 +00003053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003055ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056{
3057 asdl_seq *seq;
3058 int i;
3059 expr_ty e;
3060
3061 REQ(n, exprlist);
3062
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003063 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003065 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003067 e = ast_for_expr(c, CHILD(n, i));
3068 if (!e)
3069 return NULL;
3070 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003071 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 }
3074 return seq;
3075}
3076
3077static stmt_ty
3078ast_for_del_stmt(struct compiling *c, const node *n)
3079{
3080 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 /* del_stmt: 'del' exprlist */
3083 REQ(n, del_stmt);
3084
3085 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3086 if (!expr_list)
3087 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003088 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089}
3090
3091static stmt_ty
3092ast_for_flow_stmt(struct compiling *c, const node *n)
3093{
3094 /*
3095 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3096 | yield_stmt
3097 break_stmt: 'break'
3098 continue_stmt: 'continue'
3099 return_stmt: 'return' [testlist]
3100 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003101 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 raise_stmt: 'raise' [test [',' test [',' test]]]
3103 */
3104 node *ch;
3105
3106 REQ(n, flow_stmt);
3107 ch = CHILD(n, 0);
3108 switch (TYPE(ch)) {
3109 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003110 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003112 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003114 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3115 if (!exp)
3116 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003117 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 }
3119 case return_stmt:
3120 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003121 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003123 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 if (!expression)
3125 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003126 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 }
3128 case raise_stmt:
3129 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003130 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3131 else if (NCH(ch) >= 2) {
3132 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3134 if (!expression)
3135 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003136 if (NCH(ch) == 4) {
3137 cause = ast_for_expr(c, CHILD(ch, 3));
3138 if (!cause)
3139 return NULL;
3140 }
3141 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 }
Stefan Krahf432a322017-08-21 13:09:59 +02003143 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003145 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 "unexpected flow_stmt: %d", TYPE(ch));
3147 return NULL;
3148 }
3149}
3150
3151static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003152alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153{
3154 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003155 import_as_name: NAME ['as' NAME]
3156 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 dotted_name: NAME ('.' NAME)*
3158 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003159 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 loop:
3162 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003163 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003164 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003165 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003166 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003167 if (!name)
3168 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003169 if (NCH(n) == 3) {
3170 node *str_node = CHILD(n, 2);
3171 str = NEW_IDENTIFIER(str_node);
3172 if (!str)
3173 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003174 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 return NULL;
3176 }
3177 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003178 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003179 return NULL;
3180 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003181 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003182 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 case dotted_as_name:
3184 if (NCH(n) == 1) {
3185 n = CHILD(n, 0);
3186 goto loop;
3187 }
3188 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003189 node *asname_node = CHILD(n, 2);
3190 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003191 if (!a)
3192 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003194 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003195 if (!a->asname)
3196 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003197 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 return a;
3200 }
3201 break;
3202 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003203 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 node *name_node = CHILD(n, 0);
3205 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003206 if (!name)
3207 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003208 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003209 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003210 return alias(name, NULL, c->c_arena);
3211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 else {
3213 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003214 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003215 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218
3219 len = 0;
3220 for (i = 0; i < NCH(n); i += 2)
3221 /* length of string plus one for the dot */
3222 len += strlen(STR(CHILD(n, i))) + 1;
3223 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003224 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 if (!str)
3226 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003227 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 if (!s)
3229 return NULL;
3230 for (i = 0; i < NCH(n); i += 2) {
3231 char *sch = STR(CHILD(n, i));
3232 strcpy(s, STR(CHILD(n, i)));
3233 s += strlen(sch);
3234 *s++ = '.';
3235 }
3236 --s;
3237 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3239 PyBytes_GET_SIZE(str),
3240 NULL);
3241 Py_DECREF(str);
3242 if (!uni)
3243 return NULL;
3244 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003245 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003246 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3247 Py_DECREF(str);
3248 return NULL;
3249 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003250 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 }
3252 break;
3253 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003254 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003255 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3256 Py_DECREF(str);
3257 return NULL;
3258 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003259 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003261 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 "unexpected import name: %d", TYPE(n));
3263 return NULL;
3264 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003265
3266 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 return NULL;
3268}
3269
3270static stmt_ty
3271ast_for_import_stmt(struct compiling *c, const node *n)
3272{
3273 /*
3274 import_stmt: import_name | import_from
3275 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003276 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3277 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003279 int lineno;
3280 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281 int i;
3282 asdl_seq *aliases;
3283
3284 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003285 lineno = LINENO(n);
3286 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003288 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003290 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003291 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003292 if (!aliases)
3293 return NULL;
3294 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003295 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003296 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003302 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003304 int idx, ndots = 0;
3305 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003306 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003308 /* Count the number of dots (for relative imports) and check for the
3309 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 for (idx = 1; idx < NCH(n); idx++) {
3311 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003312 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3313 if (!mod)
3314 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003315 idx++;
3316 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003317 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003319 ndots += 3;
3320 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 } else if (TYPE(CHILD(n, idx)) != DOT) {
3322 break;
3323 }
3324 ndots++;
3325 }
3326 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003327 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003328 case STAR:
3329 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003330 n = CHILD(n, idx);
3331 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332 break;
3333 case LPAR:
3334 /* from ... import (x, y, z) */
3335 n = CHILD(n, idx + 1);
3336 n_children = NCH(n);
3337 break;
3338 case import_as_names:
3339 /* from ... import x, y, z */
3340 n = CHILD(n, idx);
3341 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003342 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003343 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344 " surrounding parentheses");
3345 return NULL;
3346 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003347 break;
3348 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003349 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003350 return NULL;
3351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003353 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003354 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356
3357 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003358 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003359 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003360 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003362 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003364 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003365 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003366 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003367 if (!import_alias)
3368 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003369 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003372 if (mod != NULL)
3373 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003374 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003375 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376 }
Neal Norwitz79792652005-11-14 04:25:03 +00003377 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378 "unknown import statement: starts with command '%s'",
3379 STR(CHILD(n, 0)));
3380 return NULL;
3381}
3382
3383static stmt_ty
3384ast_for_global_stmt(struct compiling *c, const node *n)
3385{
3386 /* global_stmt: 'global' NAME (',' NAME)* */
3387 identifier name;
3388 asdl_seq *s;
3389 int i;
3390
3391 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003392 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003396 name = NEW_IDENTIFIER(CHILD(n, i));
3397 if (!name)
3398 return NULL;
3399 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003401 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402}
3403
3404static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003405ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3406{
3407 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3408 identifier name;
3409 asdl_seq *s;
3410 int i;
3411
3412 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003413 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003414 if (!s)
3415 return NULL;
3416 for (i = 1; i < NCH(n); i += 2) {
3417 name = NEW_IDENTIFIER(CHILD(n, i));
3418 if (!name)
3419 return NULL;
3420 asdl_seq_SET(s, i / 2, name);
3421 }
3422 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3423}
3424
3425static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426ast_for_assert_stmt(struct compiling *c, const node *n)
3427{
3428 /* assert_stmt: 'assert' test [',' test] */
3429 REQ(n, assert_stmt);
3430 if (NCH(n) == 2) {
3431 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3432 if (!expression)
3433 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003434 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 }
3436 else if (NCH(n) == 4) {
3437 expr_ty expr1, expr2;
3438
3439 expr1 = ast_for_expr(c, CHILD(n, 1));
3440 if (!expr1)
3441 return NULL;
3442 expr2 = ast_for_expr(c, CHILD(n, 3));
3443 if (!expr2)
3444 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445
Thomas Wouters89f507f2006-12-13 04:49:30 +00003446 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 }
Neal Norwitz79792652005-11-14 04:25:03 +00003448 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449 "improper number of parts to 'assert' statement: %d",
3450 NCH(n));
3451 return NULL;
3452}
3453
3454static asdl_seq *
3455ast_for_suite(struct compiling *c, const node *n)
3456{
3457 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003458 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 stmt_ty s;
3460 int i, total, num, end, pos = 0;
3461 node *ch;
3462
3463 REQ(n, suite);
3464
3465 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003466 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003470 n = CHILD(n, 0);
3471 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003473 */
3474 end = NCH(n) - 1;
3475 if (TYPE(CHILD(n, end - 1)) == SEMI)
3476 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 for (i = 0; i < end; i += 2) {
3479 ch = CHILD(n, i);
3480 s = ast_for_stmt(c, ch);
3481 if (!s)
3482 return NULL;
3483 asdl_seq_SET(seq, pos++, s);
3484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 }
3486 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003487 for (i = 2; i < (NCH(n) - 1); i++) {
3488 ch = CHILD(n, i);
3489 REQ(ch, stmt);
3490 num = num_stmts(ch);
3491 if (num == 1) {
3492 /* small_stmt or compound_stmt with only one child */
3493 s = ast_for_stmt(c, ch);
3494 if (!s)
3495 return NULL;
3496 asdl_seq_SET(seq, pos++, s);
3497 }
3498 else {
3499 int j;
3500 ch = CHILD(ch, 0);
3501 REQ(ch, simple_stmt);
3502 for (j = 0; j < NCH(ch); j += 2) {
3503 /* statement terminates with a semi-colon ';' */
3504 if (NCH(CHILD(ch, j)) == 0) {
3505 assert((j + 1) == NCH(ch));
3506 break;
3507 }
3508 s = ast_for_stmt(c, CHILD(ch, j));
3509 if (!s)
3510 return NULL;
3511 asdl_seq_SET(seq, pos++, s);
3512 }
3513 }
3514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 }
3516 assert(pos == seq->size);
3517 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518}
3519
3520static stmt_ty
3521ast_for_if_stmt(struct compiling *c, const node *n)
3522{
3523 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3524 ['else' ':' suite]
3525 */
3526 char *s;
3527
3528 REQ(n, if_stmt);
3529
3530 if (NCH(n) == 4) {
3531 expr_ty expression;
3532 asdl_seq *suite_seq;
3533
3534 expression = ast_for_expr(c, CHILD(n, 1));
3535 if (!expression)
3536 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003538 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540
Guido van Rossumd8faa362007-04-27 19:54:29 +00003541 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3542 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 s = STR(CHILD(n, 4));
3546 /* s[2], the third character in the string, will be
3547 's' for el_s_e, or
3548 'i' for el_i_f
3549 */
3550 if (s[2] == 's') {
3551 expr_ty expression;
3552 asdl_seq *seq1, *seq2;
3553
3554 expression = ast_for_expr(c, CHILD(n, 1));
3555 if (!expression)
3556 return NULL;
3557 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003558 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 return NULL;
3560 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003561 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562 return NULL;
3563
Guido van Rossumd8faa362007-04-27 19:54:29 +00003564 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3565 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 }
3567 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003568 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003569 expr_ty expression;
3570 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003571 asdl_seq *orelse = NULL;
3572 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 /* must reference the child n_elif+1 since 'else' token is third,
3574 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003575 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3576 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3577 has_else = 1;
3578 n_elif -= 3;
3579 }
3580 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581
Thomas Wouters89f507f2006-12-13 04:49:30 +00003582 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003583 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003585 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003586 if (!orelse)
3587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003589 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003591 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3592 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003594 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3595 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 asdl_seq_SET(orelse, 0,
3599 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003600 LINENO(CHILD(n, NCH(n) - 6)),
3601 CHILD(n, NCH(n) - 6)->n_col_offset,
3602 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 /* the just-created orelse handled the last elif */
3604 n_elif--;
3605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606
Thomas Wouters89f507f2006-12-13 04:49:30 +00003607 for (i = 0; i < n_elif; i++) {
3608 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003609 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 if (!newobj)
3611 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003613 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003616 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003621 LINENO(CHILD(n, off)),
3622 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003623 orelse = newobj;
3624 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003625 expression = ast_for_expr(c, CHILD(n, 1));
3626 if (!expression)
3627 return NULL;
3628 suite_seq = ast_for_suite(c, CHILD(n, 3));
3629 if (!suite_seq)
3630 return NULL;
3631 return If(expression, suite_seq, orelse,
3632 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003634
3635 PyErr_Format(PyExc_SystemError,
3636 "unexpected token in 'if' statement: %s", s);
3637 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638}
3639
3640static stmt_ty
3641ast_for_while_stmt(struct compiling *c, const node *n)
3642{
3643 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3644 REQ(n, while_stmt);
3645
3646 if (NCH(n) == 4) {
3647 expr_ty expression;
3648 asdl_seq *suite_seq;
3649
3650 expression = ast_for_expr(c, CHILD(n, 1));
3651 if (!expression)
3652 return NULL;
3653 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003654 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003656 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657 }
3658 else if (NCH(n) == 7) {
3659 expr_ty expression;
3660 asdl_seq *seq1, *seq2;
3661
3662 expression = ast_for_expr(c, CHILD(n, 1));
3663 if (!expression)
3664 return NULL;
3665 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003666 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 return NULL;
3668 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003669 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 return NULL;
3671
Thomas Wouters89f507f2006-12-13 04:49:30 +00003672 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003674
3675 PyErr_Format(PyExc_SystemError,
3676 "wrong number of tokens for 'while' statement: %d",
3677 NCH(n));
3678 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679}
3680
3681static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003682ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003684 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003686 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003687 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3689 REQ(n, for_stmt);
3690
3691 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003692 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 if (!seq)
3694 return NULL;
3695 }
3696
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003697 node_target = CHILD(n, 1);
3698 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003699 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003701 /* Check the # of children rather than the length of _target, since
3702 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003703 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003704 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003705 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003707 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003709 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003710 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 return NULL;
3712 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003713 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 return NULL;
3715
Yury Selivanov75445082015-05-11 22:57:16 -04003716 if (is_async)
3717 return AsyncFor(target, expression, suite_seq, seq,
3718 LINENO(n), n->n_col_offset,
3719 c->c_arena);
3720 else
3721 return For(target, expression, suite_seq, seq,
3722 LINENO(n), n->n_col_offset,
3723 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724}
3725
3726static excepthandler_ty
3727ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3728{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003729 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730 REQ(exc, except_clause);
3731 REQ(body, suite);
3732
3733 if (NCH(exc) == 1) {
3734 asdl_seq *suite_seq = ast_for_suite(c, body);
3735 if (!suite_seq)
3736 return NULL;
3737
Neal Norwitzad74aa82008-03-31 05:14:30 +00003738 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003739 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740 }
3741 else if (NCH(exc) == 2) {
3742 expr_ty expression;
3743 asdl_seq *suite_seq;
3744
3745 expression = ast_for_expr(c, CHILD(exc, 1));
3746 if (!expression)
3747 return NULL;
3748 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003749 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 return NULL;
3751
Neal Norwitzad74aa82008-03-31 05:14:30 +00003752 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003753 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 }
3755 else if (NCH(exc) == 4) {
3756 asdl_seq *suite_seq;
3757 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003758 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003759 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003761 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003762 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003764 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 return NULL;
3766 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003767 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 return NULL;
3769
Neal Norwitzad74aa82008-03-31 05:14:30 +00003770 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003771 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003773
3774 PyErr_Format(PyExc_SystemError,
3775 "wrong number of children for 'except' clause: %d",
3776 NCH(exc));
3777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778}
3779
3780static stmt_ty
3781ast_for_try_stmt(struct compiling *c, const node *n)
3782{
Neal Norwitzf599f422005-12-17 21:33:47 +00003783 const int nch = NCH(n);
3784 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003785 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003786
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787 REQ(n, try_stmt);
3788
Neal Norwitzf599f422005-12-17 21:33:47 +00003789 body = ast_for_suite(c, CHILD(n, 2));
3790 if (body == NULL)
3791 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792
Neal Norwitzf599f422005-12-17 21:33:47 +00003793 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3794 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3795 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3796 /* we can assume it's an "else",
3797 because nch >= 9 for try-else-finally and
3798 it would otherwise have a type of except_clause */
3799 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3800 if (orelse == NULL)
3801 return NULL;
3802 n_except--;
3803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804
Neal Norwitzf599f422005-12-17 21:33:47 +00003805 finally = ast_for_suite(c, CHILD(n, nch - 1));
3806 if (finally == NULL)
3807 return NULL;
3808 n_except--;
3809 }
3810 else {
3811 /* we can assume it's an "else",
3812 otherwise it would have a type of except_clause */
3813 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3814 if (orelse == NULL)
3815 return NULL;
3816 n_except--;
3817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003819 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003820 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 return NULL;
3822 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823
Neal Norwitzf599f422005-12-17 21:33:47 +00003824 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003825 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003826 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003827 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003828 if (handlers == NULL)
3829 return NULL;
3830
3831 for (i = 0; i < n_except; i++) {
3832 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3833 CHILD(n, 5 + i * 3));
3834 if (!e)
3835 return NULL;
3836 asdl_seq_SET(handlers, i, e);
3837 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003838 }
3839
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003840 assert(finally != NULL || asdl_seq_LEN(handlers));
3841 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842}
3843
Georg Brandl0c315622009-05-25 21:10:36 +00003844/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003845static withitem_ty
3846ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003847{
3848 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003849
Georg Brandl0c315622009-05-25 21:10:36 +00003850 REQ(n, with_item);
3851 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003852 if (!context_expr)
3853 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003854 if (NCH(n) == 3) {
3855 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003856
3857 if (!optional_vars) {
3858 return NULL;
3859 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003860 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003861 return NULL;
3862 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003863 }
3864
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003865 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003866}
3867
Georg Brandl0c315622009-05-25 21:10:36 +00003868/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3869static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003870ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003871{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003872 int i, n_items;
3873 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003874
3875 REQ(n, with_stmt);
3876
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003877 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003878 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003879 if (!items)
3880 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003881 for (i = 1; i < NCH(n) - 2; i += 2) {
3882 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3883 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003884 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003885 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003886 }
3887
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003888 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3889 if (!body)
3890 return NULL;
3891
Yury Selivanov75445082015-05-11 22:57:16 -04003892 if (is_async)
3893 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3894 else
3895 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003896}
3897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003899ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003901 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003902 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003903 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003904 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 REQ(n, classdef);
3907
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003908 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003909 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 if (!s)
3911 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003912 classname = NEW_IDENTIFIER(CHILD(n, 1));
3913 if (!classname)
3914 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003915 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003916 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003917 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003918 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003920
3921 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003922 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003923 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003924 return NULL;
3925 classname = NEW_IDENTIFIER(CHILD(n, 1));
3926 if (!classname)
3927 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003928 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003929 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003930 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003931 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 }
3933
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003934 /* class NAME '(' arglist ')' ':' suite */
3935 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003936 {
3937 PyObject *dummy_name;
3938 expr_ty dummy;
3939 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3940 if (!dummy_name)
3941 return NULL;
3942 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003943 call = ast_for_call(c, CHILD(n, 3), dummy, false);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003944 if (!call)
3945 return NULL;
3946 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003947 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003948 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003950 classname = NEW_IDENTIFIER(CHILD(n, 1));
3951 if (!classname)
3952 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003953 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003954 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003955
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003956 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003957 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958}
3959
3960static stmt_ty
3961ast_for_stmt(struct compiling *c, const node *n)
3962{
3963 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003964 assert(NCH(n) == 1);
3965 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 }
3967 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003968 assert(num_stmts(n) == 1);
3969 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970 }
3971 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003972 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003973 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3974 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003975 */
3976 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 case expr_stmt:
3978 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 case del_stmt:
3980 return ast_for_del_stmt(c, n);
3981 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003982 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 case flow_stmt:
3984 return ast_for_flow_stmt(c, n);
3985 case import_stmt:
3986 return ast_for_import_stmt(c, n);
3987 case global_stmt:
3988 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003989 case nonlocal_stmt:
3990 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 case assert_stmt:
3992 return ast_for_assert_stmt(c, n);
3993 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003994 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3996 TYPE(n), NCH(n));
3997 return NULL;
3998 }
3999 }
4000 else {
4001 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004002 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004003 */
4004 node *ch = CHILD(n, 0);
4005 REQ(n, compound_stmt);
4006 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 case if_stmt:
4008 return ast_for_if_stmt(c, ch);
4009 case while_stmt:
4010 return ast_for_while_stmt(c, ch);
4011 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004012 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 case try_stmt:
4014 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004015 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004016 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004018 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004020 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 case decorated:
4022 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004023 case async_stmt:
4024 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004026 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004027 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 TYPE(n), NCH(n));
4029 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004030 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 }
4032}
4033
4034static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004035parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004037 const char *end;
4038 long x;
4039 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004040 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004041 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004043 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004044 errno = 0;
4045 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004046 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004048 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004049 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004050 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004051 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 }
4053 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004054 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 if (*end == '\0') {
4056 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004057 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004058 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004059 }
4060 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004061 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004062 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004063 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4064 if (compl.imag == -1.0 && PyErr_Occurred())
4065 return NULL;
4066 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004067 }
4068 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004069 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004070 dx = PyOS_string_to_double(s, NULL, NULL);
4071 if (dx == -1.0 && PyErr_Occurred())
4072 return NULL;
4073 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004074 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075}
4076
4077static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004078parsenumber(struct compiling *c, const char *s)
4079{
4080 char *dup, *end;
4081 PyObject *res = NULL;
4082
4083 assert(s != NULL);
4084
4085 if (strchr(s, '_') == NULL) {
4086 return parsenumber_raw(c, s);
4087 }
4088 /* Create a duplicate without underscores. */
4089 dup = PyMem_Malloc(strlen(s) + 1);
4090 end = dup;
4091 for (; *s; s++) {
4092 if (*s != '_') {
4093 *end++ = *s;
4094 }
4095 }
4096 *end = '\0';
4097 res = parsenumber_raw(c, dup);
4098 PyMem_Free(dup);
4099 return res;
4100}
4101
4102static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004103decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004105 const char *s, *t;
4106 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004107 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4108 while (s < end && (*s & 0x80)) s++;
4109 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004110 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111}
4112
Eric V. Smith56466482016-10-31 14:46:26 -04004113static int
4114warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004115 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004116{
4117 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4118 first_invalid_escape_char);
4119 if (msg == NULL) {
4120 return -1;
4121 }
4122 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4123 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004124 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004125 {
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004126 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4127 const char *s;
Victor Stinnerf9cca362016-11-15 09:12:10 +01004128
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004129 /* Replace the DeprecationWarning exception with a SyntaxError
4130 to get a more accurate error report */
4131 PyErr_Clear();
Victor Stinnerf9cca362016-11-15 09:12:10 +01004132
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004133 s = PyUnicode_AsUTF8(msg);
4134 if (s != NULL) {
4135 ast_error(c, n, s);
4136 }
Eric V. Smith56466482016-10-31 14:46:26 -04004137 }
4138 Py_DECREF(msg);
4139 return -1;
4140 }
4141 Py_DECREF(msg);
4142 return 0;
4143}
4144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004146decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4147 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004149 PyObject *v, *u;
4150 char *buf;
4151 char *p;
4152 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004153
Benjamin Peterson202803a2016-02-25 22:34:45 -08004154 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004155 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004156 return NULL;
4157 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4158 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4159 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4160 if (u == NULL)
4161 return NULL;
4162 p = buf = PyBytes_AsString(u);
4163 end = s + len;
4164 while (s < end) {
4165 if (*s == '\\') {
4166 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004167 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004168 strcpy(p, "u005c");
4169 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004170 if (s >= end)
4171 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004172 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004173 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004174 if (*s & 0x80) { /* XXX inefficient */
4175 PyObject *w;
4176 int kind;
4177 void *data;
4178 Py_ssize_t len, i;
4179 w = decode_utf8(c, &s, end);
4180 if (w == NULL) {
4181 Py_DECREF(u);
4182 return NULL;
4183 }
4184 kind = PyUnicode_KIND(w);
4185 data = PyUnicode_DATA(w);
4186 len = PyUnicode_GET_LENGTH(w);
4187 for (i = 0; i < len; i++) {
4188 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4189 sprintf(p, "\\U%08x", chr);
4190 p += 10;
4191 }
4192 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004193 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004194 Py_DECREF(w);
4195 } else {
4196 *p++ = *s++;
4197 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004198 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004199 len = p - buf;
4200 s = buf;
4201
Eric V. Smith56466482016-10-31 14:46:26 -04004202 const char *first_invalid_escape;
4203 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4204
4205 if (v != NULL && first_invalid_escape != NULL) {
4206 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4207 /* We have not decref u before because first_invalid_escape points
4208 inside u. */
4209 Py_XDECREF(u);
4210 Py_DECREF(v);
4211 return NULL;
4212 }
4213 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004214 Py_XDECREF(u);
4215 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004216}
4217
Eric V. Smith56466482016-10-31 14:46:26 -04004218static PyObject *
4219decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4220 size_t len)
4221{
4222 const char *first_invalid_escape;
4223 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4224 &first_invalid_escape);
4225 if (result == NULL)
4226 return NULL;
4227
4228 if (first_invalid_escape != NULL) {
4229 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4230 Py_DECREF(result);
4231 return NULL;
4232 }
4233 }
4234 return result;
4235}
4236
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004237/* Shift locations for the given node and all its children by adding `lineno`
4238 and `col_offset` to existing locations. */
4239static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4240{
4241 n->n_col_offset = n->n_col_offset + col_offset;
4242 for (int i = 0; i < NCH(n); ++i) {
4243 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4244 /* Shifting column offsets unnecessary if there's been newlines. */
4245 col_offset = 0;
4246 }
4247 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4248 }
4249 n->n_lineno = n->n_lineno + lineno;
4250}
4251
4252/* Fix locations for the given node and its children.
4253
4254 `parent` is the enclosing node.
4255 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004256 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004257*/
4258static void
4259fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4260{
4261 char *substr = NULL;
4262 char *start;
4263 int lines = LINENO(parent) - 1;
4264 int cols = parent->n_col_offset;
4265 /* Find the full fstring to fix location information in `n`. */
4266 while (parent && parent->n_type != STRING)
4267 parent = parent->n_child;
4268 if (parent && parent->n_str) {
4269 substr = strstr(parent->n_str, expr_str);
4270 if (substr) {
4271 start = substr;
4272 while (start > parent->n_str) {
4273 if (start[0] == '\n')
4274 break;
4275 start--;
4276 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004277 cols += (int)(substr - start);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004278 /* Fix lineno in mulitline strings. */
4279 while ((substr = strchr(substr + 1, '\n')))
4280 lines--;
4281 }
4282 }
4283 fstring_shift_node_locations(n, lines, cols);
4284}
4285
Eric V. Smith451d0e32016-09-09 21:56:20 -04004286/* Compile this expression in to an expr_ty. Add parens around the
4287 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004288static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004289fstring_compile_expr(const char *expr_start, const char *expr_end,
4290 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004291
Eric V. Smith235a6f02015-09-19 14:51:32 -04004292{
4293 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004294 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004295 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004296 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004297 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004298 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004299
Eric V. Smith1d44c412015-09-23 07:49:00 -04004300 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004301 assert(*(expr_start-1) == '{');
4302 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004303
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004304 /* If the substring is all whitespace, it's an error. We need to catch this
4305 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4306 because turning the expression '' in to '()' would go from being invalid
4307 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004308 for (s = expr_start; s != expr_end; s++) {
4309 char c = *s;
4310 /* The Python parser ignores only the following whitespace
4311 characters (\r already is converted to \n). */
4312 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004313 break;
4314 }
4315 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004316 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004317 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004318 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004319 }
4320
Eric V. Smith451d0e32016-09-09 21:56:20 -04004321 len = expr_end - expr_start;
4322 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4323 str = PyMem_RawMalloc(len + 3);
4324 if (str == NULL)
4325 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004326
Eric V. Smith451d0e32016-09-09 21:56:20 -04004327 str[0] = '(';
4328 memcpy(str+1, expr_start, len);
4329 str[len+1] = ')';
4330 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004331
4332 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004333 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4334 Py_eval_input, 0);
4335 if (!mod_n) {
4336 PyMem_RawFree(str);
4337 return NULL;
4338 }
4339 /* Reuse str to find the correct column offset. */
4340 str[0] = '{';
4341 str[len+1] = '}';
4342 fstring_fix_node_location(n, mod_n, str);
4343 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004344 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004345 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004346 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004347 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004348 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004349}
4350
4351/* Return -1 on error.
4352
4353 Return 0 if we reached the end of the literal.
4354
4355 Return 1 if we haven't reached the end of the literal, but we want
4356 the caller to process the literal up to this point. Used for
4357 doubled braces.
4358*/
4359static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004360fstring_find_literal(const char **str, const char *end, int raw,
4361 PyObject **literal, int recurse_lvl,
4362 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004363{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004364 /* Get any literal string. It ends when we hit an un-doubled left
4365 brace (which isn't part of a unicode name escape such as
4366 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004367
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004368 const char *s = *str;
4369 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004370 int result = 0;
4371
Eric V. Smith235a6f02015-09-19 14:51:32 -04004372 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004373 while (s < end) {
4374 char ch = *s++;
4375 if (!raw && ch == '\\' && s < end) {
4376 ch = *s++;
4377 if (ch == 'N') {
4378 if (s < end && *s++ == '{') {
4379 while (s < end && *s++ != '}') {
4380 }
4381 continue;
4382 }
4383 break;
4384 }
4385 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4386 return -1;
4387 }
4388 }
4389 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004390 /* Check for doubled braces, but only at the top level. If
4391 we checked at every level, then f'{0:{3}}' would fail
4392 with the two closing braces. */
4393 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004394 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004395 /* We're going to tell the caller that the literal ends
4396 here, but that they should continue scanning. But also
4397 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004398 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004399 result = 1;
4400 goto done;
4401 }
4402
4403 /* Where a single '{' is the start of a new expression, a
4404 single '}' is not allowed. */
4405 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004406 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004407 ast_error(c, n, "f-string: single '}' is not allowed");
4408 return -1;
4409 }
4410 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004411 /* We're either at a '{', which means we're starting another
4412 expression; or a '}', which means we're at the end of this
4413 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004414 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004415 break;
4416 }
4417 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004418 *str = s;
4419 assert(s <= end);
4420 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004421done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004422 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004423 if (raw)
4424 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004425 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004426 NULL, NULL);
4427 else
Eric V. Smith56466482016-10-31 14:46:26 -04004428 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004429 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004430 if (!*literal)
4431 return -1;
4432 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004433 return result;
4434}
4435
4436/* Forward declaration because parsing is recursive. */
4437static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004438fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004439 struct compiling *c, const node *n);
4440
Eric V. Smith451d0e32016-09-09 21:56:20 -04004441/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004442 expression (so it must be a '{'). Returns the FormattedValue node,
4443 which includes the expression, conversion character, and
4444 format_spec expression.
4445
4446 Note that I don't do a perfect job here: I don't make sure that a
4447 closing brace doesn't match an opening paren, for example. It
4448 doesn't need to error on all invalid expressions, just correctly
4449 find the end of all valid ones. Any errors inside the expression
4450 will be caught when we parse it later. */
4451static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004452fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004453 expr_ty *expression, struct compiling *c, const node *n)
4454{
4455 /* Return -1 on error, else 0. */
4456
Eric V. Smith451d0e32016-09-09 21:56:20 -04004457 const char *expr_start;
4458 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004459 expr_ty simple_expression;
4460 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004461 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004462
4463 /* 0 if we're not in a string, else the quote char we're trying to
4464 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004465 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004466
4467 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4468 int string_type = 0;
4469
4470 /* Keep track of nesting level for braces/parens/brackets in
4471 expressions. */
4472 Py_ssize_t nested_depth = 0;
4473
4474 /* Can only nest one level deep. */
4475 if (recurse_lvl >= 2) {
4476 ast_error(c, n, "f-string: expressions nested too deeply");
4477 return -1;
4478 }
4479
4480 /* The first char must be a left brace, or we wouldn't have gotten
4481 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004482 assert(**str == '{');
4483 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004484
Eric V. Smith451d0e32016-09-09 21:56:20 -04004485 expr_start = *str;
4486 for (; *str < end; (*str)++) {
4487 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004488
4489 /* Loop invariants. */
4490 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004491 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004492 if (quote_char)
4493 assert(string_type == 1 || string_type == 3);
4494 else
4495 assert(string_type == 0);
4496
Eric V. Smith451d0e32016-09-09 21:56:20 -04004497 ch = **str;
4498 /* Nowhere inside an expression is a backslash allowed. */
4499 if (ch == '\\') {
4500 /* Error: can't include a backslash character, inside
4501 parens or strings or not. */
4502 ast_error(c, n, "f-string expression part "
4503 "cannot include a backslash");
4504 return -1;
4505 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004506 if (quote_char) {
4507 /* We're inside a string. See if we're at the end. */
4508 /* This code needs to implement the same non-error logic
4509 as tok_get from tokenizer.c, at the letter_quote
4510 label. To actually share that code would be a
4511 nightmare. But, it's unlikely to change and is small,
4512 so duplicate it here. Note we don't need to catch all
4513 of the errors, since they'll be caught when parsing the
4514 expression. We just need to match the non-error
4515 cases. Thus we can ignore \n in single-quoted strings,
4516 for example. Or non-terminated strings. */
4517 if (ch == quote_char) {
4518 /* Does this match the string_type (single or triple
4519 quoted)? */
4520 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004521 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004522 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004523 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004524 string_type = 0;
4525 quote_char = 0;
4526 continue;
4527 }
4528 } else {
4529 /* We're at the end of a normal string. */
4530 quote_char = 0;
4531 string_type = 0;
4532 continue;
4533 }
4534 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004535 } else if (ch == '\'' || ch == '"') {
4536 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004537 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004538 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004539 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004540 } else {
4541 /* Start of a normal string. */
4542 string_type = 1;
4543 }
4544 /* Start looking for the end of the string. */
4545 quote_char = ch;
4546 } else if (ch == '[' || ch == '{' || ch == '(') {
4547 nested_depth++;
4548 } else if (nested_depth != 0 &&
4549 (ch == ']' || ch == '}' || ch == ')')) {
4550 nested_depth--;
4551 } else if (ch == '#') {
4552 /* Error: can't include a comment character, inside parens
4553 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004554 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004555 return -1;
4556 } else if (nested_depth == 0 &&
4557 (ch == '!' || ch == ':' || ch == '}')) {
4558 /* First, test for the special case of "!=". Since '=' is
4559 not an allowed conversion character, nothing is lost in
4560 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004561 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004562 /* This isn't a conversion character, just continue. */
4563 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004564 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004565 /* Normal way out of this loop. */
4566 break;
4567 } else {
4568 /* Just consume this char and loop around. */
4569 }
4570 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004571 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004572 /* If we leave this loop in a string or with mismatched parens, we
4573 don't care. We'll get a syntax error when compiling the
4574 expression. But, we can produce a better error message, so
4575 let's just do that.*/
4576 if (quote_char) {
4577 ast_error(c, n, "f-string: unterminated string");
4578 return -1;
4579 }
4580 if (nested_depth) {
4581 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4582 return -1;
4583 }
4584
Eric V. Smith451d0e32016-09-09 21:56:20 -04004585 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004586 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004587
4588 /* Compile the expression as soon as possible, so we show errors
4589 related to the expression before errors related to the
4590 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004591 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004592 if (!simple_expression)
4593 return -1;
4594
4595 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004596 if (**str == '!') {
4597 *str += 1;
4598 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004599 goto unexpected_end_of_string;
4600
Eric V. Smith451d0e32016-09-09 21:56:20 -04004601 conversion = **str;
4602 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004603
4604 /* Validate the conversion. */
4605 if (!(conversion == 's' || conversion == 'r'
4606 || conversion == 'a')) {
4607 ast_error(c, n, "f-string: invalid conversion character: "
4608 "expected 's', 'r', or 'a'");
4609 return -1;
4610 }
4611 }
4612
4613 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004614 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004615 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004616 if (**str == ':') {
4617 *str += 1;
4618 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004619 goto unexpected_end_of_string;
4620
4621 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004622 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004623 if (!format_spec)
4624 return -1;
4625 }
4626
Eric V. Smith451d0e32016-09-09 21:56:20 -04004627 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004628 goto unexpected_end_of_string;
4629
4630 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004631 assert(*str < end);
4632 assert(**str == '}');
4633 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004634
Eric V. Smith451d0e32016-09-09 21:56:20 -04004635 /* And now create the FormattedValue node that represents this
4636 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004637 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638 format_spec, LINENO(n), n->n_col_offset,
4639 c->c_arena);
4640 if (!*expression)
4641 return -1;
4642
4643 return 0;
4644
4645unexpected_end_of_string:
4646 ast_error(c, n, "f-string: expecting '}'");
4647 return -1;
4648}
4649
4650/* Return -1 on error.
4651
4652 Return 0 if we have a literal (possible zero length) and an
4653 expression (zero length if at the end of the string.
4654
4655 Return 1 if we have a literal, but no expression, and we want the
4656 caller to call us again. This is used to deal with doubled
4657 braces.
4658
4659 When called multiple times on the string 'a{{b{0}c', this function
4660 will return:
4661
4662 1. the literal 'a{' with no expression, and a return value
4663 of 1. Despite the fact that there's no expression, the return
4664 value of 1 means we're not finished yet.
4665
4666 2. the literal 'b' and the expression '0', with a return value of
4667 0. The fact that there's an expression means we're not finished.
4668
4669 3. literal 'c' with no expression and a return value of 0. The
4670 combination of the return value of 0 with no expression means
4671 we're finished.
4672*/
4673static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004674fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4675 int recurse_lvl, PyObject **literal,
4676 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004677 struct compiling *c, const node *n)
4678{
4679 int result;
4680
4681 assert(*literal == NULL && *expression == NULL);
4682
4683 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004684 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004685 if (result < 0)
4686 goto error;
4687
4688 assert(result == 0 || result == 1);
4689
4690 if (result == 1)
4691 /* We have a literal, but don't look at the expression. */
4692 return 1;
4693
Eric V. Smith451d0e32016-09-09 21:56:20 -04004694 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004695 /* We're at the end of the string or the end of a nested
4696 f-string: no expression. The top-level error case where we
4697 expect to be at the end of the string but we're at a '}' is
4698 handled later. */
4699 return 0;
4700
4701 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004702 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004703
Eric V. Smith451d0e32016-09-09 21:56:20 -04004704 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004705 goto error;
4706
4707 return 0;
4708
4709error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004710 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004711 return -1;
4712}
4713
4714#define EXPRLIST_N_CACHED 64
4715
4716typedef struct {
4717 /* Incrementally build an array of expr_ty, so be used in an
4718 asdl_seq. Cache some small but reasonably sized number of
4719 expr_ty's, and then after that start dynamically allocating,
4720 doubling the number allocated each time. Note that the f-string
4721 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4722 Str for the literal 'a'. So you add expr_ty's about twice as
4723 fast as you add exressions in an f-string. */
4724
4725 Py_ssize_t allocated; /* Number we've allocated. */
4726 Py_ssize_t size; /* Number we've used. */
4727 expr_ty *p; /* Pointer to the memory we're actually
4728 using. Will point to 'data' until we
4729 start dynamically allocating. */
4730 expr_ty data[EXPRLIST_N_CACHED];
4731} ExprList;
4732
4733#ifdef NDEBUG
4734#define ExprList_check_invariants(l)
4735#else
4736static void
4737ExprList_check_invariants(ExprList *l)
4738{
4739 /* Check our invariants. Make sure this object is "live", and
4740 hasn't been deallocated. */
4741 assert(l->size >= 0);
4742 assert(l->p != NULL);
4743 if (l->size <= EXPRLIST_N_CACHED)
4744 assert(l->data == l->p);
4745}
4746#endif
4747
4748static void
4749ExprList_Init(ExprList *l)
4750{
4751 l->allocated = EXPRLIST_N_CACHED;
4752 l->size = 0;
4753
4754 /* Until we start allocating dynamically, p points to data. */
4755 l->p = l->data;
4756
4757 ExprList_check_invariants(l);
4758}
4759
4760static int
4761ExprList_Append(ExprList *l, expr_ty exp)
4762{
4763 ExprList_check_invariants(l);
4764 if (l->size >= l->allocated) {
4765 /* We need to alloc (or realloc) the memory. */
4766 Py_ssize_t new_size = l->allocated * 2;
4767
4768 /* See if we've ever allocated anything dynamically. */
4769 if (l->p == l->data) {
4770 Py_ssize_t i;
4771 /* We're still using the cached data. Switch to
4772 alloc-ing. */
4773 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4774 if (!l->p)
4775 return -1;
4776 /* Copy the cached data into the new buffer. */
4777 for (i = 0; i < l->size; i++)
4778 l->p[i] = l->data[i];
4779 } else {
4780 /* Just realloc. */
4781 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4782 if (!tmp) {
4783 PyMem_RawFree(l->p);
4784 l->p = NULL;
4785 return -1;
4786 }
4787 l->p = tmp;
4788 }
4789
4790 l->allocated = new_size;
4791 assert(l->allocated == 2 * l->size);
4792 }
4793
4794 l->p[l->size++] = exp;
4795
4796 ExprList_check_invariants(l);
4797 return 0;
4798}
4799
4800static void
4801ExprList_Dealloc(ExprList *l)
4802{
4803 ExprList_check_invariants(l);
4804
4805 /* If there's been an error, or we've never dynamically allocated,
4806 do nothing. */
4807 if (!l->p || l->p == l->data) {
4808 /* Do nothing. */
4809 } else {
4810 /* We have dynamically allocated. Free the memory. */
4811 PyMem_RawFree(l->p);
4812 }
4813 l->p = NULL;
4814 l->size = -1;
4815}
4816
4817static asdl_seq *
4818ExprList_Finish(ExprList *l, PyArena *arena)
4819{
4820 asdl_seq *seq;
4821
4822 ExprList_check_invariants(l);
4823
4824 /* Allocate the asdl_seq and copy the expressions in to it. */
4825 seq = _Py_asdl_seq_new(l->size, arena);
4826 if (seq) {
4827 Py_ssize_t i;
4828 for (i = 0; i < l->size; i++)
4829 asdl_seq_SET(seq, i, l->p[i]);
4830 }
4831 ExprList_Dealloc(l);
4832 return seq;
4833}
4834
4835/* The FstringParser is designed to add a mix of strings and
4836 f-strings, and concat them together as needed. Ultimately, it
4837 generates an expr_ty. */
4838typedef struct {
4839 PyObject *last_str;
4840 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004841 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004842} FstringParser;
4843
4844#ifdef NDEBUG
4845#define FstringParser_check_invariants(state)
4846#else
4847static void
4848FstringParser_check_invariants(FstringParser *state)
4849{
4850 if (state->last_str)
4851 assert(PyUnicode_CheckExact(state->last_str));
4852 ExprList_check_invariants(&state->expr_list);
4853}
4854#endif
4855
4856static void
4857FstringParser_Init(FstringParser *state)
4858{
4859 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004860 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004861 ExprList_Init(&state->expr_list);
4862 FstringParser_check_invariants(state);
4863}
4864
4865static void
4866FstringParser_Dealloc(FstringParser *state)
4867{
4868 FstringParser_check_invariants(state);
4869
4870 Py_XDECREF(state->last_str);
4871 ExprList_Dealloc(&state->expr_list);
4872}
4873
4874/* Make a Str node, but decref the PyUnicode object being added. */
4875static expr_ty
4876make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4877{
4878 PyObject *s = *str;
4879 *str = NULL;
4880 assert(PyUnicode_CheckExact(s));
4881 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4882 Py_DECREF(s);
4883 return NULL;
4884 }
4885 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4886}
4887
4888/* Add a non-f-string (that is, a regular literal string). str is
4889 decref'd. */
4890static int
4891FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4892{
4893 FstringParser_check_invariants(state);
4894
4895 assert(PyUnicode_CheckExact(str));
4896
4897 if (PyUnicode_GET_LENGTH(str) == 0) {
4898 Py_DECREF(str);
4899 return 0;
4900 }
4901
4902 if (!state->last_str) {
4903 /* We didn't have a string before, so just remember this one. */
4904 state->last_str = str;
4905 } else {
4906 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004907 PyUnicode_AppendAndDel(&state->last_str, str);
4908 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004909 return -1;
4910 }
4911 FstringParser_check_invariants(state);
4912 return 0;
4913}
4914
Eric V. Smith451d0e32016-09-09 21:56:20 -04004915/* Parse an f-string. The f-string is in *str to end, with no
4916 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004918FstringParser_ConcatFstring(FstringParser *state, const char **str,
4919 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004920 struct compiling *c, const node *n)
4921{
4922 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004923 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924
4925 /* Parse the f-string. */
4926 while (1) {
4927 PyObject *literal = NULL;
4928 expr_ty expression = NULL;
4929
4930 /* If there's a zero length literal in front of the
4931 expression, literal will be NULL. If we're at the end of
4932 the f-string, expression will be NULL (unless result == 1,
4933 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004934 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004935 &literal, &expression,
4936 c, n);
4937 if (result < 0)
4938 return -1;
4939
4940 /* Add the literal, if any. */
4941 if (!literal) {
4942 /* Do nothing. Just leave last_str alone (and possibly
4943 NULL). */
4944 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004945 /* Note that the literal can be zero length, if the
4946 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947 state->last_str = literal;
4948 literal = NULL;
4949 } else {
4950 /* We have a literal, concatenate it. */
4951 assert(PyUnicode_GET_LENGTH(literal) != 0);
4952 if (FstringParser_ConcatAndDel(state, literal) < 0)
4953 return -1;
4954 literal = NULL;
4955 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956
4957 /* We've dealt with the literal now. It can't be leaked on further
4958 errors. */
4959 assert(literal == NULL);
4960
4961 /* See if we should just loop around to get the next literal
4962 and expression, while ignoring the expression this
4963 time. This is used for un-doubling braces, as an
4964 optimization. */
4965 if (result == 1)
4966 continue;
4967
4968 if (!expression)
4969 /* We're done with this f-string. */
4970 break;
4971
4972 /* We know we have an expression. Convert any existing string
4973 to a Str node. */
4974 if (!state->last_str) {
4975 /* Do nothing. No previous literal. */
4976 } else {
4977 /* Convert the existing last_str literal to a Str node. */
4978 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4979 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4980 return -1;
4981 }
4982
4983 if (ExprList_Append(&state->expr_list, expression) < 0)
4984 return -1;
4985 }
4986
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 /* If recurse_lvl is zero, then we must be at the end of the
4988 string. Otherwise, we must be at a right brace. */
4989
Eric V. Smith451d0e32016-09-09 21:56:20 -04004990 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991 ast_error(c, n, "f-string: unexpected end of string");
4992 return -1;
4993 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004994 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 ast_error(c, n, "f-string: expecting '}'");
4996 return -1;
4997 }
4998
4999 FstringParser_check_invariants(state);
5000 return 0;
5001}
5002
5003/* Convert the partial state reflected in last_str and expr_list to an
5004 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5005static expr_ty
5006FstringParser_Finish(FstringParser *state, struct compiling *c,
5007 const node *n)
5008{
5009 asdl_seq *seq;
5010
5011 FstringParser_check_invariants(state);
5012
5013 /* If we're just a constant string with no expressions, return
5014 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005015 if (!state->fmode) {
5016 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005017 if (!state->last_str) {
5018 /* Create a zero length string. */
5019 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5020 if (!state->last_str)
5021 goto error;
5022 }
5023 return make_str_node_and_del(&state->last_str, c, n);
5024 }
5025
5026 /* Create a Str node out of last_str, if needed. It will be the
5027 last node in our expression list. */
5028 if (state->last_str) {
5029 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5030 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5031 goto error;
5032 }
5033 /* This has already been freed. */
5034 assert(state->last_str == NULL);
5035
5036 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5037 if (!seq)
5038 goto error;
5039
Eric V. Smith235a6f02015-09-19 14:51:32 -04005040 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5041
5042error:
5043 FstringParser_Dealloc(state);
5044 return NULL;
5045}
5046
Eric V. Smith451d0e32016-09-09 21:56:20 -04005047/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5048 at end, parse it into an expr_ty. Return NULL on error. Adjust
5049 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005050static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005051fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052 struct compiling *c, const node *n)
5053{
5054 FstringParser state;
5055
5056 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005057 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058 c, n) < 0) {
5059 FstringParser_Dealloc(&state);
5060 return NULL;
5061 }
5062
5063 return FstringParser_Finish(&state, c, n);
5064}
5065
5066/* n is a Python string literal, including the bracketing quote
5067 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005068 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005069 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005070 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5071 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005072*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005073static int
5074parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5075 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005076{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005077 size_t len;
5078 const char *s = STR(n);
5079 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080 int fmode = 0;
5081 *bytesmode = 0;
5082 *rawmode = 0;
5083 *result = NULL;
5084 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005085 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005087 if (quote == 'b' || quote == 'B') {
5088 quote = *++s;
5089 *bytesmode = 1;
5090 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005091 else if (quote == 'u' || quote == 'U') {
5092 quote = *++s;
5093 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005094 else if (quote == 'r' || quote == 'R') {
5095 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005097 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005098 else if (quote == 'f' || quote == 'F') {
5099 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005102 else {
5103 break;
5104 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005105 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005106 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005111 if (quote != '\'' && quote != '\"') {
5112 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005114 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005116 s++;
5117 len = strlen(s);
5118 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005120 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005121 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005122 }
5123 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005125 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005126 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005127 }
5128 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005129 /* A triple quoted string. We've already skipped one quote at
5130 the start and one at the end of the string. Now skip the
5131 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005132 s += 2;
5133 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005135 if (s[--len] != quote || s[--len] != quote) {
5136 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005137 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005138 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005139 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005140
Eric V. Smith451d0e32016-09-09 21:56:20 -04005141 if (fmode) {
5142 /* Just return the bytes. The caller will parse the resulting
5143 string. */
5144 *fstr = s;
5145 *fstrlen = len;
5146 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005147 }
5148
Eric V. Smith451d0e32016-09-09 21:56:20 -04005149 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005150 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005151 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005152 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005153 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005154 const char *ch;
5155 for (ch = s; *ch; ch++) {
5156 if (Py_CHARMASK(*ch) >= 0x80) {
5157 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005158 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005159 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005160 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005161 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005162 if (*rawmode)
5163 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005164 else
Eric V. Smith56466482016-10-31 14:46:26 -04005165 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005166 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005167 if (*rawmode)
5168 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005169 else
Eric V. Smith56466482016-10-31 14:46:26 -04005170 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005171 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005172 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005173}
5174
Eric V. Smith235a6f02015-09-19 14:51:32 -04005175/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5176 each STRING atom, and process it as needed. For bytes, just
5177 concatenate them together, and the result will be a Bytes node. For
5178 normal strings and f-strings, concatenate them together. The result
5179 will be a Str node if there were no f-strings; a FormattedValue
5180 node if there's just an f-string (with no leading or trailing
5181 literals), or a JoinedStr node if there are multiple f-strings or
5182 any literals involved. */
5183static expr_ty
5184parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005185{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005186 int bytesmode = 0;
5187 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005188 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005189
5190 FstringParser state;
5191 FstringParser_Init(&state);
5192
5193 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005194 int this_bytesmode;
5195 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005196 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005197 const char *fstr;
5198 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005199
5200 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5202 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005203 goto error;
5204
5205 /* Check that we're not mixing bytes with unicode. */
5206 if (i != 0 && bytesmode != this_bytesmode) {
5207 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005208 /* s is NULL if the current string part is an f-string. */
5209 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005210 goto error;
5211 }
5212 bytesmode = this_bytesmode;
5213
Eric V. Smith451d0e32016-09-09 21:56:20 -04005214 if (fstr != NULL) {
5215 int result;
5216 assert(s == NULL && !bytesmode);
5217 /* This is an f-string. Parse and concatenate it. */
5218 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5219 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005220 if (result < 0)
5221 goto error;
5222 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005223 /* A string or byte string. */
5224 assert(s != NULL && fstr == NULL);
5225
Eric V. Smith451d0e32016-09-09 21:56:20 -04005226 assert(bytesmode ? PyBytes_CheckExact(s) :
5227 PyUnicode_CheckExact(s));
5228
Eric V. Smith451d0e32016-09-09 21:56:20 -04005229 if (bytesmode) {
5230 /* For bytes, concat as we go. */
5231 if (i == 0) {
5232 /* First time, just remember this value. */
5233 bytes_str = s;
5234 } else {
5235 PyBytes_ConcatAndDel(&bytes_str, s);
5236 if (!bytes_str)
5237 goto error;
5238 }
5239 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005240 /* This is a regular string. Concatenate it. */
5241 if (FstringParser_ConcatAndDel(&state, s) < 0)
5242 goto error;
5243 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005244 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005245 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005246 if (bytesmode) {
5247 /* Just return the bytes object and we're done. */
5248 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5249 goto error;
5250 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005252
Eric V. Smith235a6f02015-09-19 14:51:32 -04005253 /* We're not a bytes string, bytes_str should never have been set. */
5254 assert(bytes_str == NULL);
5255
5256 return FstringParser_Finish(&state, c, n);
5257
5258error:
5259 Py_XDECREF(bytes_str);
5260 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005261 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005262}