blob: 4fa68a371d402dcb45194619c845e315a0a00a2a [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"
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
14
Benjamin Peterson832bfe22011-08-09 16:15:04 -050015static int validate_stmts(asdl_seq *);
16static int validate_exprs(asdl_seq *, expr_context_ty, int);
17static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
18static int validate_stmt(stmt_ty);
19static int validate_expr(expr_ty, expr_context_ty);
20
21static int
22validate_comprehension(asdl_seq *gens)
23{
24 int i;
25 if (!asdl_seq_LEN(gens)) {
26 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
27 return 0;
28 }
29 for (i = 0; i < asdl_seq_LEN(gens); i++) {
30 comprehension_ty comp = asdl_seq_GET(gens, i);
31 if (!validate_expr(comp->target, Store) ||
32 !validate_expr(comp->iter, Load) ||
33 !validate_exprs(comp->ifs, Load, 0))
34 return 0;
35 }
36 return 1;
37}
38
39static int
40validate_slice(slice_ty slice)
41{
42 switch (slice->kind) {
43 case Slice_kind:
44 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
45 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
46 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
47 case ExtSlice_kind: {
48 int i;
49 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
50 return 0;
51 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
52 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
53 return 0;
54 return 1;
55 }
56 case Index_kind:
57 return validate_expr(slice->v.Index.value, Load);
58 default:
59 PyErr_SetString(PyExc_SystemError, "unknown slice node");
60 return 0;
61 }
62}
63
64static int
65validate_keywords(asdl_seq *keywords)
66{
67 int i;
68 for (i = 0; i < asdl_seq_LEN(keywords); i++)
69 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
70 return 0;
71 return 1;
72}
73
74static int
75validate_args(asdl_seq *args)
76{
77 int i;
78 for (i = 0; i < asdl_seq_LEN(args); i++) {
79 arg_ty arg = asdl_seq_GET(args, i);
80 if (arg->annotation && !validate_expr(arg->annotation, Load))
81 return 0;
82 }
83 return 1;
84}
85
86static const char *
87expr_context_name(expr_context_ty ctx)
88{
89 switch (ctx) {
90 case Load:
91 return "Load";
92 case Store:
93 return "Store";
94 case Del:
95 return "Del";
96 case AugLoad:
97 return "AugLoad";
98 case AugStore:
99 return "AugStore";
100 case Param:
101 return "Param";
102 default:
103 assert(0);
104 return "(unknown)";
105 }
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
370validate_body(asdl_seq *body, const char *owner)
371{
372 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
373}
374
375static int
376validate_stmt(stmt_ty stmt)
377{
378 int i;
379 switch (stmt->kind) {
380 case FunctionDef_kind:
381 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
382 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:
387 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
388 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) &&
415 validate_body(stmt->v.For.body, "For") &&
416 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) &&
420 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
421 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) &&
424 validate_body(stmt->v.While.body, "While") &&
425 validate_stmts(stmt->v.While.orelse);
426 case If_kind:
427 return validate_expr(stmt->v.If.test, Load) &&
428 validate_body(stmt->v.If.body, "If") &&
429 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 }
439 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 }
449 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:
461 if (!validate_body(stmt->v.Try.body, "Try"))
462 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)) ||
477 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
478 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:
502 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
503 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. */
592 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593};
594
595static asdl_seq *seq_for_testlist(struct compiling *, const node *);
596static expr_ty ast_for_expr(struct compiling *, const node *);
597static stmt_ty ast_for_stmt(struct compiling *, const node *);
598static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000599static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
600 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000601static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000602static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603
Yury Selivanov75445082015-05-11 22:57:16 -0400604static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
605static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
606
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607/* Note different signature for ast_for_call */
608static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
609
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000610static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400611static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612
Nick Coghlan650f0d02007-04-15 12:05:43 +0000613#define COMP_GENEXP 0
614#define COMP_LISTCOMP 1
615#define COMP_SETCOMP 2
616
Benjamin Peterson55e00432012-01-16 17:22:31 -0500617static int
618init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000619{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500620 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
621 if (!m)
622 return 0;
623 c->c_normalize = PyObject_GetAttrString(m, "normalize");
624 Py_DECREF(m);
625 if (!c->c_normalize)
626 return 0;
627 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500628 if (!c->c_normalize_args) {
629 Py_CLEAR(c->c_normalize);
630 return 0;
631 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200632 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633 return 1;
634}
635
636static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400637new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500638{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400639 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500640 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000641 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500642 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500643 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 /* Check whether there are non-ASCII characters in the
645 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500646 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500648 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500649 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500651 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500652 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
653 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500654 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200655 if (!id2)
656 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000658 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000659 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200660 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
661 Py_DECREF(id);
662 return NULL;
663 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000664 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665}
666
Benjamin Peterson55e00432012-01-16 17:22:31 -0500667#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400670ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400672 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673
Victor Stinner14e461d2013-08-26 22:28:21 +0200674 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000676 Py_INCREF(Py_None);
677 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200679 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 if (!tmp)
681 return 0;
682 errstr = PyUnicode_FromString(errmsg);
683 if (!errstr) {
684 Py_DECREF(tmp);
685 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000686 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000687 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 Py_DECREF(errstr);
689 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400690 if (value) {
691 PyErr_SetObject(PyExc_SyntaxError, value);
692 Py_DECREF(value);
693 }
694 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695}
696
697/* num_stmts() returns number of contained statements.
698
699 Use this routine to determine how big a sequence is needed for
700 the statements in a parse tree. Its raison d'etre is this bit of
701 grammar:
702
703 stmt: simple_stmt | compound_stmt
704 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
705
706 A simple_stmt can contain multiple small_stmt elements joined
707 by semicolons. If the arg is a simple_stmt, the number of
708 small_stmt elements is returned.
709*/
710
711static int
712num_stmts(const node *n)
713{
714 int i, l;
715 node *ch;
716
717 switch (TYPE(n)) {
718 case single_input:
719 if (TYPE(CHILD(n, 0)) == NEWLINE)
720 return 0;
721 else
722 return num_stmts(CHILD(n, 0));
723 case file_input:
724 l = 0;
725 for (i = 0; i < NCH(n); i++) {
726 ch = CHILD(n, i);
727 if (TYPE(ch) == stmt)
728 l += num_stmts(ch);
729 }
730 return l;
731 case stmt:
732 return num_stmts(CHILD(n, 0));
733 case compound_stmt:
734 return 1;
735 case simple_stmt:
736 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
737 case suite:
738 if (NCH(n) == 1)
739 return num_stmts(CHILD(n, 0));
740 else {
741 l = 0;
742 for (i = 2; i < (NCH(n) - 1); i++)
743 l += num_stmts(CHILD(n, i));
744 return l;
745 }
746 default: {
747 char buf[128];
748
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000749 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 TYPE(n), NCH(n));
751 Py_FatalError(buf);
752 }
753 }
754 assert(0);
755 return 0;
756}
757
758/* Transform the CST rooted at node * to the appropriate AST
759*/
760
761mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200762PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
763 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000765 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 asdl_seq *stmts = NULL;
767 stmt_ty s;
768 node *ch;
769 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500770 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400772 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200773 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400774 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800775 c.c_normalize = NULL;
776 c.c_normalize_args = NULL;
777
778 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
Jeremy Hyltona8293132006-02-28 17:58:27 +0000781 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 switch (TYPE(n)) {
783 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200784 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500786 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 for (i = 0; i < NCH(n) - 1; i++) {
788 ch = CHILD(n, i);
789 if (TYPE(ch) == NEWLINE)
790 continue;
791 REQ(ch, stmt);
792 num = num_stmts(ch);
793 if (num == 1) {
794 s = ast_for_stmt(&c, ch);
795 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500796 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000797 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
799 else {
800 ch = CHILD(ch, 0);
801 REQ(ch, simple_stmt);
802 for (j = 0; j < num; j++) {
803 s = ast_for_stmt(&c, CHILD(ch, j * 2));
804 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000806 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 }
809 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500810 res = Module(stmts, arena);
811 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 case eval_input: {
813 expr_ty testlist_ast;
814
Nick Coghlan650f0d02007-04-15 12:05:43 +0000815 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000816 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500818 goto out;
819 res = Expression(testlist_ast, arena);
820 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822 case single_input:
823 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200824 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500826 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
828 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000829 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
831 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 }
833 else {
834 n = CHILD(n, 0);
835 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200836 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000840 s = ast_for_stmt(&c, n);
841 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 asdl_seq_SET(stmts, 0, s);
844 }
845 else {
846 /* Only a simple_stmt can contain multiple statements. */
847 REQ(n, simple_stmt);
848 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (TYPE(CHILD(n, i)) == NEWLINE)
850 break;
851 s = ast_for_stmt(&c, CHILD(n, i));
852 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 asdl_seq_SET(stmts, i / 2, s);
855 }
856 }
857
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500860 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000862 PyErr_Format(PyExc_SystemError,
863 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500864 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 out:
867 if (c.c_normalize) {
868 Py_DECREF(c.c_normalize);
869 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
870 Py_DECREF(c.c_normalize_args);
871 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
Victor Stinner14e461d2013-08-26 22:28:21 +0200875mod_ty
876PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
877 PyArena *arena)
878{
879 mod_ty mod;
880 PyObject *filename;
881 filename = PyUnicode_DecodeFSDefault(filename_str);
882 if (filename == NULL)
883 return NULL;
884 mod = PyAST_FromNodeObject(n, flags, filename, arena);
885 Py_DECREF(filename);
886 return mod;
887
888}
889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
891*/
892
893static operator_ty
894get_operator(const node *n)
895{
896 switch (TYPE(n)) {
897 case VBAR:
898 return BitOr;
899 case CIRCUMFLEX:
900 return BitXor;
901 case AMPER:
902 return BitAnd;
903 case LEFTSHIFT:
904 return LShift;
905 case RIGHTSHIFT:
906 return RShift;
907 case PLUS:
908 return Add;
909 case MINUS:
910 return Sub;
911 case STAR:
912 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400913 case AT:
914 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 case SLASH:
916 return Div;
917 case DOUBLESLASH:
918 return FloorDiv;
919 case PERCENT:
920 return Mod;
921 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000922 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923 }
924}
925
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200926static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000927 "None",
928 "True",
929 "False",
930 NULL,
931};
932
933static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400934forbidden_name(struct compiling *c, identifier name, const node *n,
935 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000936{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000937 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200938 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400939 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000940 return 1;
941 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200942 if (_PyUnicode_EqualToASCIIString(name, "async") ||
943 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400944 {
945 PyObject *message = PyUnicode_FromString(
946 "'async' and 'await' will become reserved keywords"
947 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500948 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400949 if (message == NULL) {
950 return 1;
951 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500952 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400953 PyExc_DeprecationWarning,
954 message,
955 c->c_filename,
956 LINENO(n),
957 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500958 NULL);
959 Py_DECREF(message);
960 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400961 return 1;
962 }
963 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000964 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200965 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000966 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200967 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400968 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000969 return 1;
970 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000971 }
972 }
973 return 0;
974}
975
Jeremy Hyltona8293132006-02-28 17:58:27 +0000976/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
978 Only sets context for expr kinds that "can appear in assignment context"
979 (according to ../Parser/Python.asdl). For other expr kinds, it sets
980 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981*/
982
983static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000984set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985{
986 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000987 /* If a particular expression type can't be used for assign / delete,
988 set expr_name to its name and an error message will be generated.
989 */
990 const char* expr_name = NULL;
991
992 /* The ast defines augmented store and load contexts, but the
993 implementation here doesn't actually use them. The code may be
994 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000995 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000996 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000997 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000998 */
999 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
1001 switch (e->kind) {
1002 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001004 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001005 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001008 e->v.Subscript.ctx = ctx;
1009 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001010 case Starred_kind:
1011 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001012 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001013 return 0;
1014 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001016 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001017 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001018 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001019 }
1020 e->v.Name.ctx = ctx;
1021 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 e->v.List.ctx = ctx;
1024 s = e->v.List.elts;
1025 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001027 e->v.Tuple.ctx = ctx;
1028 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001029 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001030 case Lambda_kind:
1031 expr_name = "lambda";
1032 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001034 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001035 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001036 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001038 case UnaryOp_kind:
1039 expr_name = "operator";
1040 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001042 expr_name = "generator expression";
1043 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001044 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001045 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001046 expr_name = "yield expression";
1047 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001048 case Await_kind:
1049 expr_name = "await expression";
1050 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001051 case ListComp_kind:
1052 expr_name = "list comprehension";
1053 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001054 case SetComp_kind:
1055 expr_name = "set comprehension";
1056 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001057 case DictComp_kind:
1058 expr_name = "dict comprehension";
1059 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001060 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001061 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 case Num_kind:
1063 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001064 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001065 case JoinedStr_kind:
1066 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001067 expr_name = "literal";
1068 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001069 case NameConstant_kind:
1070 expr_name = "keyword";
1071 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001072 case Ellipsis_kind:
1073 expr_name = "Ellipsis";
1074 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001075 case Compare_kind:
1076 expr_name = "comparison";
1077 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078 case IfExp_kind:
1079 expr_name = "conditional expression";
1080 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001081 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyErr_Format(PyExc_SystemError,
1083 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001084 e->kind, e->lineno);
1085 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001087 /* Check for error string set by switch */
1088 if (expr_name) {
1089 char buf[300];
1090 PyOS_snprintf(buf, sizeof(buf),
1091 "can't %s %s",
1092 ctx == Store ? "assign to" : "delete",
1093 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001094 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001095 }
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 */
1100 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001101 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102
Thomas Wouters89f507f2006-12-13 04:49:30 +00001103 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001104 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105 return 0;
1106 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 }
1108 return 1;
1109}
1110
1111static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001112ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113{
1114 REQ(n, augassign);
1115 n = CHILD(n, 0);
1116 switch (STR(n)[0]) {
1117 case '+':
1118 return Add;
1119 case '-':
1120 return Sub;
1121 case '/':
1122 if (STR(n)[1] == '/')
1123 return FloorDiv;
1124 else
1125 return Div;
1126 case '%':
1127 return Mod;
1128 case '<':
1129 return LShift;
1130 case '>':
1131 return RShift;
1132 case '&':
1133 return BitAnd;
1134 case '^':
1135 return BitXor;
1136 case '|':
1137 return BitOr;
1138 case '*':
1139 if (STR(n)[1] == '*')
1140 return Pow;
1141 else
1142 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001143 case '@':
1144 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001146 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 }
1149}
1150
1151static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001152ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001154 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 |'is' 'not'
1156 */
1157 REQ(n, comp_op);
1158 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 n = CHILD(n, 0);
1160 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 case LESS:
1162 return Lt;
1163 case GREATER:
1164 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 return Eq;
1167 case LESSEQUAL:
1168 return LtE;
1169 case GREATEREQUAL:
1170 return GtE;
1171 case NOTEQUAL:
1172 return NotEq;
1173 case NAME:
1174 if (strcmp(STR(n), "in") == 0)
1175 return In;
1176 if (strcmp(STR(n), "is") == 0)
1177 return Is;
1178 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001179 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001181 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001182 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 }
1184 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001185 /* handle "not in" and "is not" */
1186 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 case NAME:
1188 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1189 return NotIn;
1190 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1191 return IsNot;
1192 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001193 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001195 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 }
Neal Norwitz79792652005-11-14 04:25:03 +00001198 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201}
1202
1203static asdl_seq *
1204seq_for_testlist(struct compiling *c, const node *n)
1205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001207 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1208 */
Armin Rigo31441302005-10-21 12:57:31 +00001209 asdl_seq *seq;
1210 expr_ty expression;
1211 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001212 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001214 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 if (!seq)
1216 return NULL;
1217
1218 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001220 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221
Benjamin Peterson4905e802009-09-27 02:43:28 +00001222 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001223 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225
1226 assert(i / 2 < seq->size);
1227 asdl_seq_SET(seq, i / 2, expression);
1228 }
1229 return seq;
1230}
1231
Neal Norwitzc1505362006-12-28 06:47:50 +00001232static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001233ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001234{
1235 identifier name;
1236 expr_ty annotation = NULL;
1237 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001238 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001239
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001240 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001241 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001242 name = NEW_IDENTIFIER(ch);
1243 if (!name)
1244 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001245 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001246 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001247
1248 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1249 annotation = ast_for_expr(c, CHILD(n, 2));
1250 if (!annotation)
1251 return NULL;
1252 }
1253
Victor Stinnerc106c682015-11-06 17:01:48 +01001254 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001255 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001256 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001257 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258}
1259
Guido van Rossum4f72a782006-10-27 23:31:49 +00001260/* returns -1 if failed to handle keyword only arguments
1261 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001262 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001263 ^^^
1264 start pointing here
1265 */
1266static int
1267handle_keywordonly_args(struct compiling *c, const node *n, int start,
1268 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1269{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001270 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001272 expr_ty expression, annotation;
1273 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001274 int i = start;
1275 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001276
1277 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001278 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001279 return -1;
1280 }
1281 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 while (i < NCH(n)) {
1283 ch = CHILD(n, i);
1284 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001285 case vfpdef:
1286 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001287 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001288 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001289 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001290 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001291 asdl_seq_SET(kwdefaults, j, expression);
1292 i += 2; /* '=' and test */
1293 }
1294 else { /* setting NULL if no default value exists */
1295 asdl_seq_SET(kwdefaults, j, NULL);
1296 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001297 if (NCH(ch) == 3) {
1298 /* ch is NAME ':' test */
1299 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001300 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001301 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001302 }
1303 else {
1304 annotation = NULL;
1305 }
1306 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001307 argname = NEW_IDENTIFIER(ch);
1308 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001309 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001310 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001311 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001312 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1313 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001314 if (!arg)
1315 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001317 i += 2; /* the name and the comma */
1318 break;
1319 case DOUBLESTAR:
1320 return i;
1321 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001322 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001323 goto error;
1324 }
1325 }
1326 return i;
1327 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330
Jeremy Hyltona8293132006-02-28 17:58:27 +00001331/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332
1333static arguments_ty
1334ast_for_arguments(struct compiling *c, const node *n)
1335{
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 /* This function handles both typedargslist (function definition)
1337 and varargslist (lambda definition).
1338
1339 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001340 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1341 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1342 | '**' tfpdef [',']]]
1343 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1344 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001345 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001346 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1347 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1348 | '**' vfpdef [',']]]
1349 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1350 | '**' vfpdef [',']
1351 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001352 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001353
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1356 int nposdefaults = 0, found_default = 0;
1357 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001358 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001359 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 node *ch;
1361
1362 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001363 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001364 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001367 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368
Jeremy Hyltone921e022008-07-17 16:37:17 +00001369 /* First count the number of positional args & defaults. The
1370 variable i is the loop index for this for loop and the next.
1371 The next loop picks up where the first leaves off.
1372 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 ch = CHILD(n, i);
1375 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001376 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001377 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001378 if (i < NCH(n) && /* skip argument following star */
1379 (TYPE(CHILD(n, i)) == tfpdef ||
1380 TYPE(CHILD(n, i)) == vfpdef)) {
1381 i++;
1382 }
1383 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001385 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001390 defaults for keyword only args */
1391 for ( ; i < NCH(n); ++i) {
1392 ch = CHILD(n, i);
1393 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001396 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001398 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001400 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001402 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001404 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001406 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001408 since we set NULL as default for keyword only argument w/o default
1409 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001411 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001413 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001414
1415 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001416 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001417 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001418 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001420 /* tfpdef: NAME [':' test]
1421 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 */
1423 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001424 j = 0; /* index for defaults */
1425 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427 ch = CHILD(n, i);
1428 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 case tfpdef:
1430 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1432 anything other than EQUAL or a comma? */
1433 /* XXX Should NCH(n) check be made a separate check? */
1434 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001435 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1436 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001437 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 assert(posdefaults != NULL);
1439 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001443 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001444 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001446 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001447 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001448 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001449 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001450 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001451 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 i += 2; /* the name and the comma */
1453 break;
1454 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001455 if (i+1 >= NCH(n) ||
1456 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001457 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001458 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001459 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001461 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001462 if (TYPE(ch) == COMMA) {
1463 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001464 i += 2; /* now follows keyword only arguments */
1465 res = handle_keywordonly_args(c, n, i,
1466 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001467 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 i = res; /* res has new position to process */
1469 }
1470 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001471 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001472 if (!vararg)
1473 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001474
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001476 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1477 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001478 int res = 0;
1479 res = handle_keywordonly_args(c, n, i,
1480 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001481 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001482 i = res; /* res has new position to process */
1483 }
1484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 break;
1486 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001487 ch = CHILD(n, i+1); /* tfpdef */
1488 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001489 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001490 if (!kwarg)
1491 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 i += 3;
1493 break;
1494 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001495 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 "unexpected node in varargslist: %d @ %d",
1497 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001498 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001501 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
1503
1504static expr_ty
1505ast_for_dotted_name(struct compiling *c, const node *n)
1506{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001507 expr_ty e;
1508 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001509 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 int i;
1511
1512 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001513
1514 lineno = LINENO(n);
1515 col_offset = n->n_col_offset;
1516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 id = NEW_IDENTIFIER(CHILD(n, 0));
1518 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001519 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001520 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001522 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
1524 for (i = 2; i < NCH(n); i+=2) {
1525 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 if (!id)
1527 return NULL;
1528 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1529 if (!e)
1530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 }
1532
1533 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
1536static expr_ty
1537ast_for_decorator(struct compiling *c, const node *n)
1538{
1539 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1540 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001541 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001544 REQ(CHILD(n, 0), AT);
1545 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1548 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001549 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001552 d = name_expr;
1553 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 }
1555 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001556 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001557 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001558 if (!d)
1559 return NULL;
1560 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 }
1562 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001563 d = ast_for_call(c, CHILD(n, 3), name_expr);
1564 if (!d)
1565 return NULL;
1566 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 }
1568
1569 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570}
1571
1572static asdl_seq*
1573ast_for_decorators(struct compiling *c, const node *n)
1574{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001575 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001576 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001580 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 if (!decorator_seq)
1582 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001585 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001586 if (!d)
1587 return NULL;
1588 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 }
1590 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591}
1592
1593static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001594ast_for_funcdef_impl(struct compiling *c, const node *n,
1595 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001597 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001598 identifier name;
1599 arguments_ty args;
1600 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001601 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001602 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603
1604 REQ(n, funcdef);
1605
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 name = NEW_IDENTIFIER(CHILD(n, name_i));
1607 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001608 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001609 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001610 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1612 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001613 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001614 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1615 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1616 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001617 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001618 name_i += 2;
1619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 body = ast_for_suite(c, CHILD(n, name_i + 3));
1621 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623
Yury Selivanov75445082015-05-11 22:57:16 -04001624 if (is_async)
1625 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1626 LINENO(n),
1627 n->n_col_offset, c->c_arena);
1628 else
1629 return FunctionDef(name, args, body, decorator_seq, returns,
1630 LINENO(n),
1631 n->n_col_offset, c->c_arena);
1632}
1633
1634static stmt_ty
1635ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1636{
1637 /* async_funcdef: ASYNC funcdef */
1638 REQ(n, async_funcdef);
1639 REQ(CHILD(n, 0), ASYNC);
1640 REQ(CHILD(n, 1), funcdef);
1641
1642 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1643 1 /* is_async */);
1644}
1645
1646static stmt_ty
1647ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1648{
1649 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1650 return ast_for_funcdef_impl(c, n, decorator_seq,
1651 0 /* is_async */);
1652}
1653
1654
1655static stmt_ty
1656ast_for_async_stmt(struct compiling *c, const node *n)
1657{
1658 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1659 REQ(n, async_stmt);
1660 REQ(CHILD(n, 0), ASYNC);
1661
1662 switch (TYPE(CHILD(n, 1))) {
1663 case funcdef:
1664 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1665 1 /* is_async */);
1666 case with_stmt:
1667 return ast_for_with_stmt(c, CHILD(n, 1),
1668 1 /* is_async */);
1669
1670 case for_stmt:
1671 return ast_for_for_stmt(c, CHILD(n, 1),
1672 1 /* is_async */);
1673
1674 default:
1675 PyErr_Format(PyExc_SystemError,
1676 "invalid async stament: %s",
1677 STR(CHILD(n, 1)));
1678 return NULL;
1679 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680}
1681
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001682static stmt_ty
1683ast_for_decorated(struct compiling *c, const node *n)
1684{
Yury Selivanov75445082015-05-11 22:57:16 -04001685 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001686 stmt_ty thing = NULL;
1687 asdl_seq *decorator_seq = NULL;
1688
1689 REQ(n, decorated);
1690
1691 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1692 if (!decorator_seq)
1693 return NULL;
1694
1695 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001696 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001698
1699 if (TYPE(CHILD(n, 1)) == funcdef) {
1700 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1701 } else if (TYPE(CHILD(n, 1)) == classdef) {
1702 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001703 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1704 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001705 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001706 /* we count the decorators in when talking about the class' or
1707 * function's line number */
1708 if (thing) {
1709 thing->lineno = LINENO(n);
1710 thing->col_offset = n->n_col_offset;
1711 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001712 return thing;
1713}
1714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715static expr_ty
1716ast_for_lambdef(struct compiling *c, const node *n)
1717{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001718 /* lambdef: 'lambda' [varargslist] ':' test
1719 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 arguments_ty args;
1721 expr_ty expression;
1722
1723 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001724 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 if (!args)
1726 return NULL;
1727 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001728 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 }
1731 else {
1732 args = ast_for_arguments(c, CHILD(n, 1));
1733 if (!args)
1734 return NULL;
1735 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001736 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 }
1739
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001740 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741}
1742
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001743static expr_ty
1744ast_for_ifexpr(struct compiling *c, const node *n)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001747 expr_ty expression, body, orelse;
1748
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001749 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001750 body = ast_for_expr(c, CHILD(n, 0));
1751 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001752 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001753 expression = ast_for_expr(c, CHILD(n, 2));
1754 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001755 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001756 orelse = ast_for_expr(c, CHILD(n, 4));
1757 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001758 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001759 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1760 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001761}
1762
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001764 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765
Nick Coghlan650f0d02007-04-15 12:05:43 +00001766 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767*/
1768
1769static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001770count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001772 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001773 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774
Guido van Rossumd8faa362007-04-27 19:54:29 +00001775 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001776 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001777 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001778 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001779 if (TYPE(CHILD(n, 0)) == ASYNC) {
1780 is_async = 1;
1781 }
1782 if (NCH(n) == (5 + is_async)) {
1783 n = CHILD(n, 4 + is_async);
1784 }
1785 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001786 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001787 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001788 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001789 REQ(n, comp_iter);
1790 n = CHILD(n, 0);
1791 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001793 else if (TYPE(n) == comp_if) {
1794 if (NCH(n) == 3) {
1795 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001796 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001797 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001798 else
1799 return n_fors;
1800 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001801
Guido van Rossumd8faa362007-04-27 19:54:29 +00001802 /* Should never be reached */
1803 PyErr_SetString(PyExc_SystemError,
1804 "logic error in count_comp_fors");
1805 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806}
1807
Nick Coghlan650f0d02007-04-15 12:05:43 +00001808/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809
Nick Coghlan650f0d02007-04-15 12:05:43 +00001810 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811*/
1812
1813static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001814count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001816 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817
Guido van Rossumd8faa362007-04-27 19:54:29 +00001818 while (1) {
1819 REQ(n, comp_iter);
1820 if (TYPE(CHILD(n, 0)) == comp_for)
1821 return n_ifs;
1822 n = CHILD(n, 0);
1823 REQ(n, comp_if);
1824 n_ifs++;
1825 if (NCH(n) == 2)
1826 return n_ifs;
1827 n = CHILD(n, 2);
1828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829}
1830
Guido van Rossum992d4a32007-07-11 13:09:30 +00001831static asdl_seq *
1832ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001835 asdl_seq *comps;
1836
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001837 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 if (n_fors == -1)
1839 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001841 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001842 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001846 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001848 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001849 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001850 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851
Guido van Rossum992d4a32007-07-11 13:09:30 +00001852 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001854 if (TYPE(CHILD(n, 0)) == ASYNC) {
1855 is_async = 1;
1856 }
1857
1858 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001859 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001862 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001865
Thomas Wouters89f507f2006-12-13 04:49:30 +00001866 /* Check the # of children rather than the length of t, since
1867 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001868 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001869 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001870 comp = comprehension(first, expression, NULL,
1871 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001873 comp = comprehension(Tuple(t, Store, first->lineno,
1874 first->col_offset, c->c_arena),
1875 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001876 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001878
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001879 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 int j, n_ifs;
1881 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001883 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001884 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001885 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001887
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001888 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001893 REQ(n, comp_iter);
1894 n = CHILD(n, 0);
1895 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896
Guido van Rossum992d4a32007-07-11 13:09:30 +00001897 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001899 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001900 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 if (NCH(n) == 3)
1902 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 /* on exit, must guarantee that n is a comp_for */
1905 if (TYPE(n) == comp_iter)
1906 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001907 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001909 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001911 return comps;
1912}
1913
1914static expr_ty
1915ast_for_itercomp(struct compiling *c, const node *n, int type)
1916{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001917 /* testlist_comp: (test|star_expr)
1918 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001919 expr_ty elt;
1920 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001921 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922
Guido van Rossum992d4a32007-07-11 13:09:30 +00001923 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001925 ch = CHILD(n, 0);
1926 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001927 if (!elt)
1928 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001929 if (elt->kind == Starred_kind) {
1930 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1931 return NULL;
1932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933
Guido van Rossum992d4a32007-07-11 13:09:30 +00001934 comps = ast_for_comprehension(c, CHILD(n, 1));
1935 if (!comps)
1936 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001937
1938 if (type == COMP_GENEXP)
1939 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1940 else if (type == COMP_LISTCOMP)
1941 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1942 else if (type == COMP_SETCOMP)
1943 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1944 else
1945 /* Should never happen */
1946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947}
1948
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001949/* Fills in the key, value pair corresponding to the dict element. In case
1950 * of an unpacking, key is NULL. *i is advanced by the number of ast
1951 * elements. Iff successful, nonzero is returned.
1952 */
1953static int
1954ast_for_dictelement(struct compiling *c, const node *n, int *i,
1955 expr_ty *key, expr_ty *value)
1956{
1957 expr_ty expression;
1958 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1959 assert(NCH(n) - *i >= 2);
1960
1961 expression = ast_for_expr(c, CHILD(n, *i + 1));
1962 if (!expression)
1963 return 0;
1964 *key = NULL;
1965 *value = expression;
1966
1967 *i += 2;
1968 }
1969 else {
1970 assert(NCH(n) - *i >= 3);
1971
1972 expression = ast_for_expr(c, CHILD(n, *i));
1973 if (!expression)
1974 return 0;
1975 *key = expression;
1976
1977 REQ(CHILD(n, *i + 1), COLON);
1978
1979 expression = ast_for_expr(c, CHILD(n, *i + 2));
1980 if (!expression)
1981 return 0;
1982 *value = expression;
1983
1984 *i += 3;
1985 }
1986 return 1;
1987}
1988
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001990ast_for_dictcomp(struct compiling *c, const node *n)
1991{
1992 expr_ty key, value;
1993 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001994 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001996 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001997 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 assert(key);
1999 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002001 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002002 if (!comps)
2003 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004
Guido van Rossum992d4a32007-07-11 13:09:30 +00002005 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2006}
2007
2008static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002009ast_for_dictdisplay(struct compiling *c, const node *n)
2010{
2011 int i;
2012 int j;
2013 int size;
2014 asdl_seq *keys, *values;
2015
2016 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2017 keys = _Py_asdl_seq_new(size, c->c_arena);
2018 if (!keys)
2019 return NULL;
2020
2021 values = _Py_asdl_seq_new(size, c->c_arena);
2022 if (!values)
2023 return NULL;
2024
2025 j = 0;
2026 for (i = 0; i < NCH(n); i++) {
2027 expr_ty key, value;
2028
2029 if (!ast_for_dictelement(c, n, &i, &key, &value))
2030 return NULL;
2031 asdl_seq_SET(keys, j, key);
2032 asdl_seq_SET(values, j, value);
2033
2034 j++;
2035 }
2036 keys->size = j;
2037 values->size = j;
2038 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2039}
2040
2041static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002042ast_for_genexp(struct compiling *c, const node *n)
2043{
2044 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002045 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002046}
2047
2048static expr_ty
2049ast_for_listcomp(struct compiling *c, const node *n)
2050{
2051 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002052 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002053}
2054
2055static expr_ty
2056ast_for_setcomp(struct compiling *c, const node *n)
2057{
2058 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002059 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002060}
2061
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002062static expr_ty
2063ast_for_setdisplay(struct compiling *c, const node *n)
2064{
2065 int i;
2066 int size;
2067 asdl_seq *elts;
2068
2069 assert(TYPE(n) == (dictorsetmaker));
2070 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2071 elts = _Py_asdl_seq_new(size, c->c_arena);
2072 if (!elts)
2073 return NULL;
2074 for (i = 0; i < NCH(n); i += 2) {
2075 expr_ty expression;
2076 expression = ast_for_expr(c, CHILD(n, i));
2077 if (!expression)
2078 return NULL;
2079 asdl_seq_SET(elts, i / 2, expression);
2080 }
2081 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2082}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002083
2084static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085ast_for_atom(struct compiling *c, const node *n)
2086{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2088 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002089 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 */
2091 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002094 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002095 PyObject *name;
2096 const char *s = STR(ch);
2097 size_t len = strlen(s);
2098 if (len >= 4 && len <= 5) {
2099 if (!strcmp(s, "None"))
2100 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2101 if (!strcmp(s, "True"))
2102 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2103 if (!strcmp(s, "False"))
2104 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2105 }
2106 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002107 if (!name)
2108 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002109 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002110 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2111 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002113 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002114 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002115 const char *errtype = NULL;
2116 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2117 errtype = "unicode error";
2118 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2119 errtype = "value error";
2120 if (errtype) {
2121 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002122 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002123 PyObject *type, *value, *tback, *errstr;
2124 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002125 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002126 if (errstr)
2127 s = PyUnicode_AsUTF8(errstr);
2128 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002129 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002130 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002131 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002132 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002133 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002134 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002135 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002137 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002138 Py_XDECREF(tback);
2139 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002140 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002142 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 }
2144 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002145 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002146 if (!pynum)
2147 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002148
Victor Stinner43d81952013-07-17 00:57:58 +02002149 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2150 Py_DECREF(pynum);
2151 return NULL;
2152 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002153 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 }
Georg Brandldde00282007-03-18 19:01:53 +00002155 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002156 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002158 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159
Thomas Wouters89f507f2006-12-13 04:49:30 +00002160 if (TYPE(ch) == RPAR)
2161 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 if (TYPE(ch) == yield_expr)
2164 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002167 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002168 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002169
Nick Coghlan650f0d02007-04-15 12:05:43 +00002170 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002172 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173
Thomas Wouters89f507f2006-12-13 04:49:30 +00002174 if (TYPE(ch) == RSQB)
2175 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Nick Coghlan650f0d02007-04-15 12:05:43 +00002177 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2179 asdl_seq *elts = seq_for_testlist(c, ch);
2180 if (!elts)
2181 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002182
Thomas Wouters89f507f2006-12-13 04:49:30 +00002183 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2184 }
2185 else
2186 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002188 /* dictorsetmaker: ( ((test ':' test | '**' test)
2189 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2190 * ((test | '*' test)
2191 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002192 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002193 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002194 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002195 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002196 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002197 }
2198 else {
2199 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2200 if (NCH(ch) == 1 ||
2201 (NCH(ch) > 1 &&
2202 TYPE(CHILD(ch, 1)) == COMMA)) {
2203 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002204 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002205 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002206 else if (NCH(ch) > 1 &&
2207 TYPE(CHILD(ch, 1)) == comp_for) {
2208 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002209 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002210 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002211 else if (NCH(ch) > 3 - is_dict &&
2212 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2213 /* It's a dictionary comprehension. */
2214 if (is_dict) {
2215 ast_error(c, n, "dict unpacking cannot be used in "
2216 "dict comprehension");
2217 return NULL;
2218 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002219 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002220 }
2221 else {
2222 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002223 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002224 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002225 if (res) {
2226 res->lineno = LINENO(n);
2227 res->col_offset = n->n_col_offset;
2228 }
2229 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002230 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002233 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2234 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 }
2236}
2237
2238static slice_ty
2239ast_for_slice(struct compiling *c, const node *n)
2240{
2241 node *ch;
2242 expr_ty lower = NULL, upper = NULL, step = NULL;
2243
2244 REQ(n, subscript);
2245
2246 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002247 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 sliceop: ':' [test]
2249 */
2250 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 if (NCH(n) == 1 && TYPE(ch) == test) {
2252 /* 'step' variable hold no significance in terms of being used over
2253 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 if (!step)
2256 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257
Thomas Wouters89f507f2006-12-13 04:49:30 +00002258 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 }
2260
2261 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002262 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 if (!lower)
2264 return NULL;
2265 }
2266
2267 /* If there's an upper bound it's in the second or third position. */
2268 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002269 if (NCH(n) > 1) {
2270 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 if (TYPE(n2) == test) {
2273 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 if (!upper)
2275 return NULL;
2276 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002279 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 if (TYPE(n2) == test) {
2282 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 if (!upper)
2284 return NULL;
2285 }
2286 }
2287
2288 ch = CHILD(n, NCH(n) - 1);
2289 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002290 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002291 ch = CHILD(ch, 1);
2292 if (TYPE(ch) == test) {
2293 step = ast_for_expr(c, ch);
2294 if (!step)
2295 return NULL;
2296 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 }
2298 }
2299
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002300 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301}
2302
2303static expr_ty
2304ast_for_binop(struct compiling *c, const node *n)
2305{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002306 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002308 BinOp(BinOp(A, op, B), op, C).
2309 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 int i, nops;
2312 expr_ty expr1, expr2, result;
2313 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314
Guido van Rossumd8faa362007-04-27 19:54:29 +00002315 expr1 = ast_for_expr(c, CHILD(n, 0));
2316 if (!expr1)
2317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318
Guido van Rossumd8faa362007-04-27 19:54:29 +00002319 expr2 = ast_for_expr(c, CHILD(n, 2));
2320 if (!expr2)
2321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322
Guido van Rossumd8faa362007-04-27 19:54:29 +00002323 newoperator = get_operator(CHILD(n, 1));
2324 if (!newoperator)
2325 return NULL;
2326
2327 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2328 c->c_arena);
2329 if (!result)
2330 return NULL;
2331
2332 nops = (NCH(n) - 1) / 2;
2333 for (i = 1; i < nops; i++) {
2334 expr_ty tmp_result, tmp;
2335 const node* next_oper = CHILD(n, i * 2 + 1);
2336
2337 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002338 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 return NULL;
2340
Guido van Rossumd8faa362007-04-27 19:54:29 +00002341 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2342 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 return NULL;
2344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002346 LINENO(next_oper), next_oper->n_col_offset,
2347 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349 return NULL;
2350 result = tmp_result;
2351 }
2352 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002353}
2354
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002355static expr_ty
2356ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002359 subscriptlist: subscript (',' subscript)* [',']
2360 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2361 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002362 REQ(n, trailer);
2363 if (TYPE(CHILD(n, 0)) == LPAR) {
2364 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002365 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002366 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002367 else
2368 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002369 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002370 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002371 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2372 if (!attr_id)
2373 return NULL;
2374 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002375 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002376 }
2377 else {
2378 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002379 REQ(CHILD(n, 2), RSQB);
2380 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002381 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2383 if (!slc)
2384 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002385 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2386 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002387 }
2388 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002390 by treating the sequence as a tuple literal if there are
2391 no slice features.
2392 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002393 int j;
2394 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002395 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002396 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002397 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002398 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002399 if (!slices)
2400 return NULL;
2401 for (j = 0; j < NCH(n); j += 2) {
2402 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002403 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002404 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002405 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002406 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002407 asdl_seq_SET(slices, j / 2, slc);
2408 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002409 if (!simple) {
2410 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002411 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002412 }
2413 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002414 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002415 if (!elts)
2416 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002417 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2418 slc = (slice_ty)asdl_seq_GET(slices, j);
2419 assert(slc->kind == Index_kind && slc->v.Index.value);
2420 asdl_seq_SET(elts, j, slc->v.Index.value);
2421 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002422 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002423 if (!e)
2424 return NULL;
2425 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002426 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002427 }
2428 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002429}
2430
2431static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002432ast_for_factor(struct compiling *c, const node *n)
2433{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002434 expr_ty expression;
2435
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002436 expression = ast_for_expr(c, CHILD(n, 1));
2437 if (!expression)
2438 return NULL;
2439
2440 switch (TYPE(CHILD(n, 0))) {
2441 case PLUS:
2442 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2443 c->c_arena);
2444 case MINUS:
2445 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2446 c->c_arena);
2447 case TILDE:
2448 return UnaryOp(Invert, expression, LINENO(n),
2449 n->n_col_offset, c->c_arena);
2450 }
2451 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2452 TYPE(CHILD(n, 0)));
2453 return NULL;
2454}
2455
2456static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002457ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002458{
Yury Selivanov75445082015-05-11 22:57:16 -04002459 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002460 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002461
2462 REQ(n, atom_expr);
2463 nch = NCH(n);
2464
2465 if (TYPE(CHILD(n, 0)) == AWAIT) {
2466 start = 1;
2467 assert(nch > 1);
2468 }
2469
2470 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002471 if (!e)
2472 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002473 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002474 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002475 if (start && nch == 2) {
2476 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2477 }
2478
2479 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002480 node *ch = CHILD(n, i);
2481 if (TYPE(ch) != trailer)
2482 break;
2483 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002484 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002485 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002486 tmp->lineno = e->lineno;
2487 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 e = tmp;
2489 }
Yury Selivanov75445082015-05-11 22:57:16 -04002490
2491 if (start) {
2492 /* there was an AWAIT */
2493 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2494 }
2495 else {
2496 return e;
2497 }
2498}
2499
2500static expr_ty
2501ast_for_power(struct compiling *c, const node *n)
2502{
2503 /* power: atom trailer* ('**' factor)*
2504 */
2505 expr_ty e;
2506 REQ(n, power);
2507 e = ast_for_atom_expr(c, CHILD(n, 0));
2508 if (!e)
2509 return NULL;
2510 if (NCH(n) == 1)
2511 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002512 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2513 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002514 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002515 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002516 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002517 }
2518 return e;
2519}
2520
Guido van Rossum0368b722007-05-11 16:50:42 +00002521static expr_ty
2522ast_for_starred(struct compiling *c, const node *n)
2523{
2524 expr_ty tmp;
2525 REQ(n, star_expr);
2526
2527 tmp = ast_for_expr(c, CHILD(n, 1));
2528 if (!tmp)
2529 return NULL;
2530
2531 /* The Load context is changed later. */
2532 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2533}
2534
2535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536/* Do not name a variable 'expr'! Will cause a compile error.
2537*/
2538
2539static expr_ty
2540ast_for_expr(struct compiling *c, const node *n)
2541{
2542 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002543 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002544 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 and_test: not_test ('and' not_test)*
2547 not_test: 'not' not_test | comparison
2548 comparison: expr (comp_op expr)*
2549 expr: xor_expr ('|' xor_expr)*
2550 xor_expr: and_expr ('^' and_expr)*
2551 and_expr: shift_expr ('&' shift_expr)*
2552 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2553 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002554 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002556 power: atom_expr ['**' factor]
2557 atom_expr: [AWAIT] atom trailer*
2558 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 */
2560
2561 asdl_seq *seq;
2562 int i;
2563
2564 loop:
2565 switch (TYPE(n)) {
2566 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002567 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002568 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002569 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002571 else if (NCH(n) > 1)
2572 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002573 /* Fallthrough */
2574 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 case and_test:
2576 if (NCH(n) == 1) {
2577 n = CHILD(n, 0);
2578 goto loop;
2579 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002580 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 if (!seq)
2582 return NULL;
2583 for (i = 0; i < NCH(n); i += 2) {
2584 expr_ty e = ast_for_expr(c, CHILD(n, i));
2585 if (!e)
2586 return NULL;
2587 asdl_seq_SET(seq, i / 2, e);
2588 }
2589 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002590 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2591 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002592 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002593 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 case not_test:
2595 if (NCH(n) == 1) {
2596 n = CHILD(n, 0);
2597 goto loop;
2598 }
2599 else {
2600 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2601 if (!expression)
2602 return NULL;
2603
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002604 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2605 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 }
2607 case comparison:
2608 if (NCH(n) == 1) {
2609 n = CHILD(n, 0);
2610 goto loop;
2611 }
2612 else {
2613 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002614 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002615 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002616 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 if (!ops)
2618 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002619 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 return NULL;
2622 }
2623 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002624 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002626 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002627 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
2631 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002632 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002634 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002636 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 asdl_seq_SET(cmps, i / 2, expression);
2638 }
2639 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002640 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002644 return Compare(expression, ops, cmps, LINENO(n),
2645 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 }
2647 break;
2648
Guido van Rossum0368b722007-05-11 16:50:42 +00002649 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 /* The next five cases all handle BinOps. The main body of code
2652 is the same in each case, but the switch turned inside out to
2653 reuse the code for each type of operator.
2654 */
2655 case expr:
2656 case xor_expr:
2657 case and_expr:
2658 case shift_expr:
2659 case arith_expr:
2660 case term:
2661 if (NCH(n) == 1) {
2662 n = CHILD(n, 0);
2663 goto loop;
2664 }
2665 return ast_for_binop(c, n);
2666 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002667 node *an = NULL;
2668 node *en = NULL;
2669 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002670 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002671 if (NCH(n) > 1)
2672 an = CHILD(n, 1); /* yield_arg */
2673 if (an) {
2674 en = CHILD(an, NCH(an) - 1);
2675 if (NCH(an) == 2) {
2676 is_from = 1;
2677 exp = ast_for_expr(c, en);
2678 }
2679 else
2680 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002681 if (!exp)
2682 return NULL;
2683 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002684 if (is_from)
2685 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2686 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002687 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002688 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 if (NCH(n) == 1) {
2690 n = CHILD(n, 0);
2691 goto loop;
2692 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002693 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002694 case power:
2695 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002697 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 return NULL;
2699 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002700 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 return NULL;
2702}
2703
2704static expr_ty
2705ast_for_call(struct compiling *c, const node *n, expr_ty func)
2706{
2707 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 arglist: argument (',' argument)* [',']
2709 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 */
2711
2712 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002714 asdl_seq *args;
2715 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716
2717 REQ(n, arglist);
2718
2719 nargs = 0;
2720 nkeywords = 0;
2721 ngens = 0;
2722 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723 node *ch = CHILD(n, i);
2724 if (TYPE(ch) == argument) {
2725 if (NCH(ch) == 1)
2726 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002727 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002728 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002729 else if (TYPE(CHILD(ch, 0)) == STAR)
2730 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002733 nkeywords++;
2734 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 }
2736 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002737 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002738 "if not sole argument");
2739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 }
2741
2742 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002743 ast_error(c, n, "more than 255 arguments");
2744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 }
2746
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002747 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002749 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002750 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002752 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002753
2754 nargs = 0; /* positional arguments + iterable argument unpackings */
2755 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2756 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002758 node *ch = CHILD(n, i);
2759 if (TYPE(ch) == argument) {
2760 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002761 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002762 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002763 /* a positional argument */
2764 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 if (ndoublestars) {
2766 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002767 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002768 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002770 else {
2771 ast_error(c, chch,
2772 "positional argument follows "
2773 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002775 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002776 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002777 e = ast_for_expr(c, chch);
2778 if (!e)
2779 return NULL;
2780 asdl_seq_SET(args, nargs++, e);
2781 }
2782 else if (TYPE(chch) == STAR) {
2783 /* an iterable argument unpacking */
2784 expr_ty starred;
2785 if (ndoublestars) {
2786 ast_error(c, chch,
2787 "iterable argument unpacking follows "
2788 "keyword argument unpacking");
2789 return NULL;
2790 }
2791 e = ast_for_expr(c, CHILD(ch, 1));
2792 if (!e)
2793 return NULL;
2794 starred = Starred(e, Load, LINENO(chch),
2795 chch->n_col_offset,
2796 c->c_arena);
2797 if (!starred)
2798 return NULL;
2799 asdl_seq_SET(args, nargs++, starred);
2800
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 }
2802 else if (TYPE(chch) == DOUBLESTAR) {
2803 /* a keyword argument unpacking */
2804 keyword_ty kw;
2805 i++;
2806 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002808 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 kw = keyword(NULL, e, c->c_arena);
2810 asdl_seq_SET(keywords, nkeywords++, kw);
2811 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002813 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002814 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002817 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002821 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002822 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002823 identifier key, tmp;
2824 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002826 /* chch is test, but must be an identifier? */
2827 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 /* f(lambda x: x[0] = 3) ends up getting parsed with
2831 * LHS test = lambda x: x[0], and RHS test = 3.
2832 * SF bug 132313 points out that complaining about a keyword
2833 * then is very confusing.
2834 */
2835 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002836 ast_error(c, chch,
2837 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002838 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 }
2840 else if (e->kind != Name_kind) {
2841 ast_error(c, chch,
2842 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002843 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002844 }
2845 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002846 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002848 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002849 for (k = 0; k < nkeywords; k++) {
2850 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002851 if (tmp && !PyUnicode_Compare(tmp, key)) {
2852 ast_error(c, chch,
2853 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002854 return NULL;
2855 }
2856 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002859 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002862 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002863 asdl_seq_SET(keywords, nkeywords++, kw);
2864 }
2865 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 }
2867
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002868 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869}
2870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002872ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002878 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002879 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002880 }
2881 else {
2882 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002883 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002884 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 else {
2888 asdl_seq *tmp = seq_for_testlist(c, n);
2889 if (!tmp)
2890 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002893}
2894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895static stmt_ty
2896ast_for_expr_stmt(struct compiling *c, const node *n)
2897{
2898 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002899 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2900 ('=' (yield_expr|testlist_star_expr))*)
2901 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002902 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002903 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002905 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 */
2907
2908 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 if (!e)
2911 return NULL;
2912
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 }
2915 else if (TYPE(CHILD(n, 1)) == augassign) {
2916 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002917 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002918 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919
Thomas Wouters89f507f2006-12-13 04:49:30 +00002920 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 if (!expr1)
2922 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002923 if(!set_context(c, expr1, Store, ch))
2924 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002925 /* set_context checks that most expressions are not the left side.
2926 Augmented assignments can only have a name, a subscript, or an
2927 attribute on the left, though, so we have to explicitly check for
2928 those. */
2929 switch (expr1->kind) {
2930 case Name_kind:
2931 case Attribute_kind:
2932 case Subscript_kind:
2933 break;
2934 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002935 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002936 return NULL;
2937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938
Thomas Wouters89f507f2006-12-13 04:49:30 +00002939 ch = CHILD(n, 2);
2940 if (TYPE(ch) == testlist)
2941 expr2 = ast_for_testlist(c, ch);
2942 else
2943 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002944 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 return NULL;
2946
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002947 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002948 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 return NULL;
2950
Thomas Wouters89f507f2006-12-13 04:49:30 +00002951 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002953 else if (TYPE(CHILD(n, 1)) == annassign) {
2954 expr_ty expr1, expr2, expr3;
2955 node *ch = CHILD(n, 0);
2956 node *deep, *ann = CHILD(n, 1);
2957 int simple = 1;
2958
2959 /* we keep track of parens to qualify (x) as expression not name */
2960 deep = ch;
2961 while (NCH(deep) == 1) {
2962 deep = CHILD(deep, 0);
2963 }
2964 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2965 simple = 0;
2966 }
2967 expr1 = ast_for_testlist(c, ch);
2968 if (!expr1) {
2969 return NULL;
2970 }
2971 switch (expr1->kind) {
2972 case Name_kind:
2973 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2974 return NULL;
2975 }
2976 expr1->v.Name.ctx = Store;
2977 break;
2978 case Attribute_kind:
2979 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2980 return NULL;
2981 }
2982 expr1->v.Attribute.ctx = Store;
2983 break;
2984 case Subscript_kind:
2985 expr1->v.Subscript.ctx = Store;
2986 break;
2987 case List_kind:
2988 ast_error(c, ch,
2989 "only single target (not list) can be annotated");
2990 return NULL;
2991 case Tuple_kind:
2992 ast_error(c, ch,
2993 "only single target (not tuple) can be annotated");
2994 return NULL;
2995 default:
2996 ast_error(c, ch,
2997 "illegal target for annotation");
2998 return NULL;
2999 }
3000
3001 if (expr1->kind != Name_kind) {
3002 simple = 0;
3003 }
3004 ch = CHILD(ann, 1);
3005 expr2 = ast_for_expr(c, ch);
3006 if (!expr2) {
3007 return NULL;
3008 }
3009 if (NCH(ann) == 2) {
3010 return AnnAssign(expr1, expr2, NULL, simple,
3011 LINENO(n), n->n_col_offset, c->c_arena);
3012 }
3013 else {
3014 ch = CHILD(ann, 3);
3015 expr3 = ast_for_expr(c, ch);
3016 if (!expr3) {
3017 return NULL;
3018 }
3019 return AnnAssign(expr1, expr2, expr3, simple,
3020 LINENO(n), n->n_col_offset, c->c_arena);
3021 }
3022 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 int i;
3025 asdl_seq *targets;
3026 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 expr_ty expression;
3028
Thomas Wouters89f507f2006-12-13 04:49:30 +00003029 /* a normal assignment */
3030 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003031 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003032 if (!targets)
3033 return NULL;
3034 for (i = 0; i < NCH(n) - 2; i += 2) {
3035 expr_ty e;
3036 node *ch = CHILD(n, i);
3037 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003038 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003039 return NULL;
3040 }
3041 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003045 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003046 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 asdl_seq_SET(targets, i / 2, e);
3050 }
3051 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003052 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003053 expression = ast_for_testlist(c, value);
3054 else
3055 expression = ast_for_expr(c, value);
3056 if (!expression)
3057 return NULL;
3058 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060}
3061
Benjamin Peterson78565b22009-06-28 19:19:51 +00003062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003064ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065{
3066 asdl_seq *seq;
3067 int i;
3068 expr_ty e;
3069
3070 REQ(n, exprlist);
3071
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003072 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003076 e = ast_for_expr(c, CHILD(n, i));
3077 if (!e)
3078 return NULL;
3079 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003080 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 }
3083 return seq;
3084}
3085
3086static stmt_ty
3087ast_for_del_stmt(struct compiling *c, const node *n)
3088{
3089 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 /* del_stmt: 'del' exprlist */
3092 REQ(n, del_stmt);
3093
3094 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3095 if (!expr_list)
3096 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003097 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098}
3099
3100static stmt_ty
3101ast_for_flow_stmt(struct compiling *c, const node *n)
3102{
3103 /*
3104 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3105 | yield_stmt
3106 break_stmt: 'break'
3107 continue_stmt: 'continue'
3108 return_stmt: 'return' [testlist]
3109 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003110 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 raise_stmt: 'raise' [test [',' test [',' test]]]
3112 */
3113 node *ch;
3114
3115 REQ(n, flow_stmt);
3116 ch = CHILD(n, 0);
3117 switch (TYPE(ch)) {
3118 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003121 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003123 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3124 if (!exp)
3125 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003126 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 }
3128 case return_stmt:
3129 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003130 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003132 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 if (!expression)
3134 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003135 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 }
3137 case raise_stmt:
3138 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003139 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3140 else if (NCH(ch) >= 2) {
3141 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3143 if (!expression)
3144 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003145 if (NCH(ch) == 4) {
3146 cause = ast_for_expr(c, CHILD(ch, 3));
3147 if (!cause)
3148 return NULL;
3149 }
3150 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 }
3152 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003153 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 "unexpected flow_stmt: %d", TYPE(ch));
3155 return NULL;
3156 }
3157}
3158
3159static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003160alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161{
3162 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003163 import_as_name: NAME ['as' NAME]
3164 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 dotted_name: NAME ('.' NAME)*
3166 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003167 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 loop:
3170 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003171 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003173 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003174 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003175 if (!name)
3176 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003177 if (NCH(n) == 3) {
3178 node *str_node = CHILD(n, 2);
3179 str = NEW_IDENTIFIER(str_node);
3180 if (!str)
3181 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003182 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003183 return NULL;
3184 }
3185 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003186 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003187 return NULL;
3188 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003189 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003190 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 case dotted_as_name:
3192 if (NCH(n) == 1) {
3193 n = CHILD(n, 0);
3194 goto loop;
3195 }
3196 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003197 node *asname_node = CHILD(n, 2);
3198 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003199 if (!a)
3200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003202 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003203 if (!a->asname)
3204 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003205 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 return a;
3208 }
3209 break;
3210 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003211 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003212 node *name_node = CHILD(n, 0);
3213 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003214 if (!name)
3215 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003216 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003217 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003218 return alias(name, NULL, c->c_arena);
3219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 else {
3221 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003222 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003223 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
3227 len = 0;
3228 for (i = 0; i < NCH(n); i += 2)
3229 /* length of string plus one for the dot */
3230 len += strlen(STR(CHILD(n, i))) + 1;
3231 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003232 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 if (!str)
3234 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003235 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 if (!s)
3237 return NULL;
3238 for (i = 0; i < NCH(n); i += 2) {
3239 char *sch = STR(CHILD(n, i));
3240 strcpy(s, STR(CHILD(n, i)));
3241 s += strlen(sch);
3242 *s++ = '.';
3243 }
3244 --s;
3245 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3247 PyBytes_GET_SIZE(str),
3248 NULL);
3249 Py_DECREF(str);
3250 if (!uni)
3251 return NULL;
3252 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003253 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003254 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3255 Py_DECREF(str);
3256 return NULL;
3257 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003258 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 }
3260 break;
3261 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003262 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003263 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3264 Py_DECREF(str);
3265 return NULL;
3266 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003267 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003269 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 "unexpected import name: %d", TYPE(n));
3271 return NULL;
3272 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003273
3274 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 return NULL;
3276}
3277
3278static stmt_ty
3279ast_for_import_stmt(struct compiling *c, const node *n)
3280{
3281 /*
3282 import_stmt: import_name | import_from
3283 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003284 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3285 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003287 int lineno;
3288 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 int i;
3290 asdl_seq *aliases;
3291
3292 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003293 lineno = LINENO(n);
3294 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003296 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003299 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 if (!aliases)
3301 return NULL;
3302 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003303 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003304 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003310 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 int idx, ndots = 0;
3313 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003314 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003316 /* Count the number of dots (for relative imports) and check for the
3317 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 for (idx = 1; idx < NCH(n); idx++) {
3319 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003320 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3321 if (!mod)
3322 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 idx++;
3324 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003325 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003327 ndots += 3;
3328 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003329 } else if (TYPE(CHILD(n, idx)) != DOT) {
3330 break;
3331 }
3332 ndots++;
3333 }
3334 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003335 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003336 case STAR:
3337 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 n = CHILD(n, idx);
3339 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 break;
3341 case LPAR:
3342 /* from ... import (x, y, z) */
3343 n = CHILD(n, idx + 1);
3344 n_children = NCH(n);
3345 break;
3346 case import_as_names:
3347 /* from ... import x, y, z */
3348 n = CHILD(n, idx);
3349 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003350 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003351 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 " surrounding parentheses");
3353 return NULL;
3354 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 break;
3356 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003357 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003358 return NULL;
3359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003361 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364
3365 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003366 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003367 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003368 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003370 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003372 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003374 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003375 if (!import_alias)
3376 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003377 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003380 if (mod != NULL)
3381 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003382 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003383 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 }
Neal Norwitz79792652005-11-14 04:25:03 +00003385 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 "unknown import statement: starts with command '%s'",
3387 STR(CHILD(n, 0)));
3388 return NULL;
3389}
3390
3391static stmt_ty
3392ast_for_global_stmt(struct compiling *c, const node *n)
3393{
3394 /* global_stmt: 'global' NAME (',' NAME)* */
3395 identifier name;
3396 asdl_seq *s;
3397 int i;
3398
3399 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003400 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003404 name = NEW_IDENTIFIER(CHILD(n, i));
3405 if (!name)
3406 return NULL;
3407 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003409 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410}
3411
3412static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003413ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3414{
3415 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3416 identifier name;
3417 asdl_seq *s;
3418 int i;
3419
3420 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003421 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003422 if (!s)
3423 return NULL;
3424 for (i = 1; i < NCH(n); i += 2) {
3425 name = NEW_IDENTIFIER(CHILD(n, i));
3426 if (!name)
3427 return NULL;
3428 asdl_seq_SET(s, i / 2, name);
3429 }
3430 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3431}
3432
3433static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434ast_for_assert_stmt(struct compiling *c, const node *n)
3435{
3436 /* assert_stmt: 'assert' test [',' test] */
3437 REQ(n, assert_stmt);
3438 if (NCH(n) == 2) {
3439 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3440 if (!expression)
3441 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003442 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443 }
3444 else if (NCH(n) == 4) {
3445 expr_ty expr1, expr2;
3446
3447 expr1 = ast_for_expr(c, CHILD(n, 1));
3448 if (!expr1)
3449 return NULL;
3450 expr2 = ast_for_expr(c, CHILD(n, 3));
3451 if (!expr2)
3452 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453
Thomas Wouters89f507f2006-12-13 04:49:30 +00003454 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 }
Neal Norwitz79792652005-11-14 04:25:03 +00003456 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 "improper number of parts to 'assert' statement: %d",
3458 NCH(n));
3459 return NULL;
3460}
3461
3462static asdl_seq *
3463ast_for_suite(struct compiling *c, const node *n)
3464{
3465 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003466 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 stmt_ty s;
3468 int i, total, num, end, pos = 0;
3469 node *ch;
3470
3471 REQ(n, suite);
3472
3473 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003474 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 n = CHILD(n, 0);
3479 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003481 */
3482 end = NCH(n) - 1;
3483 if (TYPE(CHILD(n, end - 1)) == SEMI)
3484 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003486 for (i = 0; i < end; i += 2) {
3487 ch = CHILD(n, i);
3488 s = ast_for_stmt(c, ch);
3489 if (!s)
3490 return NULL;
3491 asdl_seq_SET(seq, pos++, s);
3492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 }
3494 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003495 for (i = 2; i < (NCH(n) - 1); i++) {
3496 ch = CHILD(n, i);
3497 REQ(ch, stmt);
3498 num = num_stmts(ch);
3499 if (num == 1) {
3500 /* small_stmt or compound_stmt with only one child */
3501 s = ast_for_stmt(c, ch);
3502 if (!s)
3503 return NULL;
3504 asdl_seq_SET(seq, pos++, s);
3505 }
3506 else {
3507 int j;
3508 ch = CHILD(ch, 0);
3509 REQ(ch, simple_stmt);
3510 for (j = 0; j < NCH(ch); j += 2) {
3511 /* statement terminates with a semi-colon ';' */
3512 if (NCH(CHILD(ch, j)) == 0) {
3513 assert((j + 1) == NCH(ch));
3514 break;
3515 }
3516 s = ast_for_stmt(c, CHILD(ch, j));
3517 if (!s)
3518 return NULL;
3519 asdl_seq_SET(seq, pos++, s);
3520 }
3521 }
3522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 }
3524 assert(pos == seq->size);
3525 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
3528static stmt_ty
3529ast_for_if_stmt(struct compiling *c, const node *n)
3530{
3531 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3532 ['else' ':' suite]
3533 */
3534 char *s;
3535
3536 REQ(n, if_stmt);
3537
3538 if (NCH(n) == 4) {
3539 expr_ty expression;
3540 asdl_seq *suite_seq;
3541
3542 expression = ast_for_expr(c, CHILD(n, 1));
3543 if (!expression)
3544 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003546 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548
Guido van Rossumd8faa362007-04-27 19:54:29 +00003549 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3550 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 s = STR(CHILD(n, 4));
3554 /* s[2], the third character in the string, will be
3555 's' for el_s_e, or
3556 'i' for el_i_f
3557 */
3558 if (s[2] == 's') {
3559 expr_ty expression;
3560 asdl_seq *seq1, *seq2;
3561
3562 expression = ast_for_expr(c, CHILD(n, 1));
3563 if (!expression)
3564 return NULL;
3565 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003566 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 return NULL;
3568 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003569 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 return NULL;
3571
Guido van Rossumd8faa362007-04-27 19:54:29 +00003572 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3573 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 }
3575 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003576 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003577 expr_ty expression;
3578 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579 asdl_seq *orelse = NULL;
3580 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 /* must reference the child n_elif+1 since 'else' token is third,
3582 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003583 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3584 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3585 has_else = 1;
3586 n_elif -= 3;
3587 }
3588 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589
Thomas Wouters89f507f2006-12-13 04:49:30 +00003590 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003591 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003593 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003594 if (!orelse)
3595 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003597 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003599 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3600 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003602 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3603 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 asdl_seq_SET(orelse, 0,
3607 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003608 LINENO(CHILD(n, NCH(n) - 6)),
3609 CHILD(n, NCH(n) - 6)->n_col_offset,
3610 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003611 /* the just-created orelse handled the last elif */
3612 n_elif--;
3613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614
Thomas Wouters89f507f2006-12-13 04:49:30 +00003615 for (i = 0; i < n_elif; i++) {
3616 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003617 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 if (!newobj)
3619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003621 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003624 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626
Thomas Wouters89f507f2006-12-13 04:49:30 +00003627 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003629 LINENO(CHILD(n, off)),
3630 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003631 orelse = newobj;
3632 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003633 expression = ast_for_expr(c, CHILD(n, 1));
3634 if (!expression)
3635 return NULL;
3636 suite_seq = ast_for_suite(c, CHILD(n, 3));
3637 if (!suite_seq)
3638 return NULL;
3639 return If(expression, suite_seq, orelse,
3640 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003642
3643 PyErr_Format(PyExc_SystemError,
3644 "unexpected token in 'if' statement: %s", s);
3645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646}
3647
3648static stmt_ty
3649ast_for_while_stmt(struct compiling *c, const node *n)
3650{
3651 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3652 REQ(n, while_stmt);
3653
3654 if (NCH(n) == 4) {
3655 expr_ty expression;
3656 asdl_seq *suite_seq;
3657
3658 expression = ast_for_expr(c, CHILD(n, 1));
3659 if (!expression)
3660 return NULL;
3661 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003662 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 }
3666 else if (NCH(n) == 7) {
3667 expr_ty expression;
3668 asdl_seq *seq1, *seq2;
3669
3670 expression = ast_for_expr(c, CHILD(n, 1));
3671 if (!expression)
3672 return NULL;
3673 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003674 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 return NULL;
3676 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003677 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 return NULL;
3679
Thomas Wouters89f507f2006-12-13 04:49:30 +00003680 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003682
3683 PyErr_Format(PyExc_SystemError,
3684 "wrong number of tokens for 'while' statement: %d",
3685 NCH(n));
3686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687}
3688
3689static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003690ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003692 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003694 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003695 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3697 REQ(n, for_stmt);
3698
3699 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003700 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 if (!seq)
3702 return NULL;
3703 }
3704
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003705 node_target = CHILD(n, 1);
3706 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003707 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003709 /* Check the # of children rather than the length of _target, since
3710 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003711 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003712 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003713 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003715 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003717 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003718 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 return NULL;
3720 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003721 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 return NULL;
3723
Yury Selivanov75445082015-05-11 22:57:16 -04003724 if (is_async)
3725 return AsyncFor(target, expression, suite_seq, seq,
3726 LINENO(n), n->n_col_offset,
3727 c->c_arena);
3728 else
3729 return For(target, expression, suite_seq, seq,
3730 LINENO(n), n->n_col_offset,
3731 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732}
3733
3734static excepthandler_ty
3735ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3736{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003737 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 REQ(exc, except_clause);
3739 REQ(body, suite);
3740
3741 if (NCH(exc) == 1) {
3742 asdl_seq *suite_seq = ast_for_suite(c, body);
3743 if (!suite_seq)
3744 return NULL;
3745
Neal Norwitzad74aa82008-03-31 05:14:30 +00003746 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003747 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 }
3749 else if (NCH(exc) == 2) {
3750 expr_ty expression;
3751 asdl_seq *suite_seq;
3752
3753 expression = ast_for_expr(c, CHILD(exc, 1));
3754 if (!expression)
3755 return NULL;
3756 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003757 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 return NULL;
3759
Neal Norwitzad74aa82008-03-31 05:14:30 +00003760 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003761 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 }
3763 else if (NCH(exc) == 4) {
3764 asdl_seq *suite_seq;
3765 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003766 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003767 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003769 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003772 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 return NULL;
3774 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003775 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 return NULL;
3777
Neal Norwitzad74aa82008-03-31 05:14:30 +00003778 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003779 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003781
3782 PyErr_Format(PyExc_SystemError,
3783 "wrong number of children for 'except' clause: %d",
3784 NCH(exc));
3785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786}
3787
3788static stmt_ty
3789ast_for_try_stmt(struct compiling *c, const node *n)
3790{
Neal Norwitzf599f422005-12-17 21:33:47 +00003791 const int nch = NCH(n);
3792 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003793 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003794
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 REQ(n, try_stmt);
3796
Neal Norwitzf599f422005-12-17 21:33:47 +00003797 body = ast_for_suite(c, CHILD(n, 2));
3798 if (body == NULL)
3799 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800
Neal Norwitzf599f422005-12-17 21:33:47 +00003801 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3802 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3803 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3804 /* we can assume it's an "else",
3805 because nch >= 9 for try-else-finally and
3806 it would otherwise have a type of except_clause */
3807 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3808 if (orelse == NULL)
3809 return NULL;
3810 n_except--;
3811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812
Neal Norwitzf599f422005-12-17 21:33:47 +00003813 finally = ast_for_suite(c, CHILD(n, nch - 1));
3814 if (finally == NULL)
3815 return NULL;
3816 n_except--;
3817 }
3818 else {
3819 /* we can assume it's an "else",
3820 otherwise it would have a type of except_clause */
3821 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3822 if (orelse == NULL)
3823 return NULL;
3824 n_except--;
3825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003827 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003828 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 return NULL;
3830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831
Neal Norwitzf599f422005-12-17 21:33:47 +00003832 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003833 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003834 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003835 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003836 if (handlers == NULL)
3837 return NULL;
3838
3839 for (i = 0; i < n_except; i++) {
3840 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3841 CHILD(n, 5 + i * 3));
3842 if (!e)
3843 return NULL;
3844 asdl_seq_SET(handlers, i, e);
3845 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003846 }
3847
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003848 assert(finally != NULL || asdl_seq_LEN(handlers));
3849 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850}
3851
Georg Brandl0c315622009-05-25 21:10:36 +00003852/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003853static withitem_ty
3854ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003855{
3856 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003857
Georg Brandl0c315622009-05-25 21:10:36 +00003858 REQ(n, with_item);
3859 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003860 if (!context_expr)
3861 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003862 if (NCH(n) == 3) {
3863 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003864
3865 if (!optional_vars) {
3866 return NULL;
3867 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003868 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003869 return NULL;
3870 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003871 }
3872
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003873 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003874}
3875
Georg Brandl0c315622009-05-25 21:10:36 +00003876/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3877static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003878ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003879{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003880 int i, n_items;
3881 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003882
3883 REQ(n, with_stmt);
3884
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003885 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003886 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003887 if (!items)
3888 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003889 for (i = 1; i < NCH(n) - 2; i += 2) {
3890 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3891 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003892 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003893 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003894 }
3895
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003896 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3897 if (!body)
3898 return NULL;
3899
Yury Selivanov75445082015-05-11 22:57:16 -04003900 if (is_async)
3901 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3902 else
3903 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003904}
3905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003907ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003909 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003910 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003911 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003912 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 REQ(n, classdef);
3915
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003916 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 s = ast_for_suite(c, CHILD(n, 3));
3918 if (!s)
3919 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003920 classname = NEW_IDENTIFIER(CHILD(n, 1));
3921 if (!classname)
3922 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003923 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003924 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003925 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3926 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003928
3929 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003930 s = ast_for_suite(c, CHILD(n,5));
3931 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003932 return NULL;
3933 classname = NEW_IDENTIFIER(CHILD(n, 1));
3934 if (!classname)
3935 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003936 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003937 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003938 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3939 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 }
3941
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003942 /* class NAME '(' arglist ')' ':' suite */
3943 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003944 {
3945 PyObject *dummy_name;
3946 expr_ty dummy;
3947 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3948 if (!dummy_name)
3949 return NULL;
3950 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3951 call = ast_for_call(c, CHILD(n, 3), dummy);
3952 if (!call)
3953 return NULL;
3954 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003956 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003958 classname = NEW_IDENTIFIER(CHILD(n, 1));
3959 if (!classname)
3960 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003961 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003962 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003963
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003964 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003965 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966}
3967
3968static stmt_ty
3969ast_for_stmt(struct compiling *c, const node *n)
3970{
3971 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003972 assert(NCH(n) == 1);
3973 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 }
3975 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003976 assert(num_stmts(n) == 1);
3977 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 }
3979 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003980 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003981 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3982 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003983 */
3984 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 case expr_stmt:
3986 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003987 case del_stmt:
3988 return ast_for_del_stmt(c, n);
3989 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003990 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 case flow_stmt:
3992 return ast_for_flow_stmt(c, n);
3993 case import_stmt:
3994 return ast_for_import_stmt(c, n);
3995 case global_stmt:
3996 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003997 case nonlocal_stmt:
3998 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 case assert_stmt:
4000 return ast_for_assert_stmt(c, n);
4001 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004002 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4004 TYPE(n), NCH(n));
4005 return NULL;
4006 }
4007 }
4008 else {
4009 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004010 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004011 */
4012 node *ch = CHILD(n, 0);
4013 REQ(n, compound_stmt);
4014 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 case if_stmt:
4016 return ast_for_if_stmt(c, ch);
4017 case while_stmt:
4018 return ast_for_while_stmt(c, ch);
4019 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004020 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021 case try_stmt:
4022 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004023 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004024 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004026 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004028 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 case decorated:
4030 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004031 case async_stmt:
4032 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004034 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4036 TYPE(n), NCH(n));
4037 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004038 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039 }
4040}
4041
4042static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004043parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004045 const char *end;
4046 long x;
4047 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004048 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004049 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004051 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 errno = 0;
4053 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004056 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004057 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004058 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004059 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004060 }
4061 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004062 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004063 if (*end == '\0') {
4064 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004065 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004066 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004067 }
4068 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004069 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004070 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004071 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4072 if (compl.imag == -1.0 && PyErr_Occurred())
4073 return NULL;
4074 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004075 }
4076 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004077 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004078 dx = PyOS_string_to_double(s, NULL, NULL);
4079 if (dx == -1.0 && PyErr_Occurred())
4080 return NULL;
4081 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004082 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083}
4084
4085static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004086parsenumber(struct compiling *c, const char *s)
4087{
4088 char *dup, *end;
4089 PyObject *res = NULL;
4090
4091 assert(s != NULL);
4092
4093 if (strchr(s, '_') == NULL) {
4094 return parsenumber_raw(c, s);
4095 }
4096 /* Create a duplicate without underscores. */
4097 dup = PyMem_Malloc(strlen(s) + 1);
4098 end = dup;
4099 for (; *s; s++) {
4100 if (*s != '_') {
4101 *end++ = *s;
4102 }
4103 }
4104 *end = '\0';
4105 res = parsenumber_raw(c, dup);
4106 PyMem_Free(dup);
4107 return res;
4108}
4109
4110static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004111decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004113 const char *s, *t;
4114 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004115 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4116 while (s < end && (*s & 0x80)) s++;
4117 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004118 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119}
4120
Eric V. Smith56466482016-10-31 14:46:26 -04004121static int
4122warn_invalid_escape_sequence(struct compiling *c, const node *n,
4123 char first_invalid_escape_char)
4124{
4125 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4126 first_invalid_escape_char);
4127 if (msg == NULL) {
4128 return -1;
4129 }
4130 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4131 c->c_filename, LINENO(n),
4132 NULL, NULL) < 0 &&
4133 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4134 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004135 const char *s;
4136
4137 /* Replace the DeprecationWarning exception with a SyntaxError
4138 to get a more accurate error report */
4139 PyErr_Clear();
4140
4141 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004142 if (s != NULL) {
4143 ast_error(c, n, s);
4144 }
4145 Py_DECREF(msg);
4146 return -1;
4147 }
4148 Py_DECREF(msg);
4149 return 0;
4150}
4151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004152static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004153decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4154 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004156 PyObject *v, *u;
4157 char *buf;
4158 char *p;
4159 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004160
Benjamin Peterson202803a2016-02-25 22:34:45 -08004161 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004162 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004163 return NULL;
4164 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4165 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4166 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4167 if (u == NULL)
4168 return NULL;
4169 p = buf = PyBytes_AsString(u);
4170 end = s + len;
4171 while (s < end) {
4172 if (*s == '\\') {
4173 *p++ = *s++;
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004174 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004175 strcpy(p, "u005c");
4176 p += 5;
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004177 if (s >= end)
4178 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004179 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004180 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004181 if (*s & 0x80) { /* XXX inefficient */
4182 PyObject *w;
4183 int kind;
4184 void *data;
4185 Py_ssize_t len, i;
4186 w = decode_utf8(c, &s, end);
4187 if (w == NULL) {
4188 Py_DECREF(u);
4189 return NULL;
4190 }
4191 kind = PyUnicode_KIND(w);
4192 data = PyUnicode_DATA(w);
4193 len = PyUnicode_GET_LENGTH(w);
4194 for (i = 0; i < len; i++) {
4195 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4196 sprintf(p, "\\U%08x", chr);
4197 p += 10;
4198 }
4199 /* Should be impossible to overflow */
4200 assert(p - buf <= Py_SIZE(u));
4201 Py_DECREF(w);
4202 } else {
4203 *p++ = *s++;
4204 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004205 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004206 len = p - buf;
4207 s = buf;
4208
Eric V. Smith56466482016-10-31 14:46:26 -04004209 const char *first_invalid_escape;
4210 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4211
4212 if (v != NULL && first_invalid_escape != NULL) {
4213 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4214 /* We have not decref u before because first_invalid_escape points
4215 inside u. */
4216 Py_XDECREF(u);
4217 Py_DECREF(v);
4218 return NULL;
4219 }
4220 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004221 Py_XDECREF(u);
4222 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004223}
4224
Eric V. Smith56466482016-10-31 14:46:26 -04004225static PyObject *
4226decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4227 size_t len)
4228{
4229 const char *first_invalid_escape;
4230 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4231 &first_invalid_escape);
4232 if (result == NULL)
4233 return NULL;
4234
4235 if (first_invalid_escape != NULL) {
4236 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4237 Py_DECREF(result);
4238 return NULL;
4239 }
4240 }
4241 return result;
4242}
4243
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004244/* Shift locations for the given node and all its children by adding `lineno`
4245 and `col_offset` to existing locations. */
4246static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4247{
4248 n->n_col_offset = n->n_col_offset + col_offset;
4249 for (int i = 0; i < NCH(n); ++i) {
4250 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4251 /* Shifting column offsets unnecessary if there's been newlines. */
4252 col_offset = 0;
4253 }
4254 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4255 }
4256 n->n_lineno = n->n_lineno + lineno;
4257}
4258
4259/* Fix locations for the given node and its children.
4260
4261 `parent` is the enclosing node.
4262 `n` is the node which locations are going to be fixed relative to parent.
4263 `expr_str` is the child node's string representation, incuding braces.
4264*/
4265static void
4266fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4267{
4268 char *substr = NULL;
4269 char *start;
4270 int lines = LINENO(parent) - 1;
4271 int cols = parent->n_col_offset;
4272 /* Find the full fstring to fix location information in `n`. */
4273 while (parent && parent->n_type != STRING)
4274 parent = parent->n_child;
4275 if (parent && parent->n_str) {
4276 substr = strstr(parent->n_str, expr_str);
4277 if (substr) {
4278 start = substr;
4279 while (start > parent->n_str) {
4280 if (start[0] == '\n')
4281 break;
4282 start--;
4283 }
4284 cols += substr - start;
4285 /* Fix lineno in mulitline strings. */
4286 while ((substr = strchr(substr + 1, '\n')))
4287 lines--;
4288 }
4289 }
4290 fstring_shift_node_locations(n, lines, cols);
4291}
4292
Eric V. Smith451d0e32016-09-09 21:56:20 -04004293/* Compile this expression in to an expr_ty. Add parens around the
4294 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004295static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004296fstring_compile_expr(const char *expr_start, const char *expr_end,
4297 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004298
Eric V. Smith235a6f02015-09-19 14:51:32 -04004299{
4300 PyCompilerFlags cf;
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004301 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004302 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004303 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004304 Py_ssize_t len;
Serhiy Storchaka570b1c92017-06-09 00:38:06 +03004305 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004306
Eric V. Smith1d44c412015-09-23 07:49:00 -04004307 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004308 assert(*(expr_start-1) == '{');
4309 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004310
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004311 /* If the substring is all whitespace, it's an error. We need to catch this
4312 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4313 because turning the expression '' in to '()' would go from being invalid
4314 to valid. */
Serhiy Storchaka570b1c92017-06-09 00:38:06 +03004315 for (s = expr_start; s != expr_end; s++) {
4316 char c = *s;
4317 /* The Python parser ignores only the following whitespace
4318 characters (\r already is converted to \n). */
4319 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004320 break;
4321 }
4322 }
Serhiy Storchaka570b1c92017-06-09 00:38:06 +03004323 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004324 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004325 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004326 }
4327
Eric V. Smith451d0e32016-09-09 21:56:20 -04004328 len = expr_end - expr_start;
4329 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4330 str = PyMem_RawMalloc(len + 3);
4331 if (str == NULL)
4332 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004333
Eric V. Smith451d0e32016-09-09 21:56:20 -04004334 str[0] = '(';
4335 memcpy(str+1, expr_start, len);
4336 str[len+1] = ')';
4337 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004338
4339 cf.cf_flags = PyCF_ONLY_AST;
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004340 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4341 Py_eval_input, 0);
4342 if (!mod_n) {
4343 PyMem_RawFree(str);
4344 return NULL;
4345 }
4346 /* Reuse str to find the correct column offset. */
4347 str[0] = '{';
4348 str[len+1] = '}';
4349 fstring_fix_node_location(n, mod_n, str);
4350 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004351 PyMem_RawFree(str);
Miss Islington (bot)aa1afc72017-09-06 19:43:04 -07004352 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004353 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004354 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004355 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356}
4357
4358/* Return -1 on error.
4359
4360 Return 0 if we reached the end of the literal.
4361
4362 Return 1 if we haven't reached the end of the literal, but we want
4363 the caller to process the literal up to this point. Used for
4364 doubled braces.
4365*/
4366static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004367fstring_find_literal(const char **str, const char *end, int raw,
4368 PyObject **literal, int recurse_lvl,
4369 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004370{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004371 /* Get any literal string. It ends when we hit an un-doubled left
4372 brace (which isn't part of a unicode name escape such as
4373 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004375 const char *s = *str;
4376 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004377 int result = 0;
4378
Eric V. Smith235a6f02015-09-19 14:51:32 -04004379 assert(*literal == NULL);
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004380 while (s < end) {
4381 char ch = *s++;
4382 if (!raw && ch == '\\' && s < end) {
4383 ch = *s++;
4384 if (ch == 'N') {
4385 if (s < end && *s++ == '{') {
4386 while (s < end && *s++ != '}') {
4387 }
4388 continue;
4389 }
4390 break;
4391 }
4392 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4393 return -1;
4394 }
4395 }
4396 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004397 /* Check for doubled braces, but only at the top level. If
4398 we checked at every level, then f'{0:{3}}' would fail
4399 with the two closing braces. */
4400 if (recurse_lvl == 0) {
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004401 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004402 /* We're going to tell the caller that the literal ends
4403 here, but that they should continue scanning. But also
4404 skip over the second brace when we resume scanning. */
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004405 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004406 result = 1;
4407 goto done;
4408 }
4409
4410 /* Where a single '{' is the start of a new expression, a
4411 single '}' is not allowed. */
4412 if (ch == '}') {
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004413 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004414 ast_error(c, n, "f-string: single '}' is not allowed");
4415 return -1;
4416 }
4417 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004418 /* We're either at a '{', which means we're starting another
4419 expression; or a '}', which means we're at the end of this
4420 f-string (for a nested format_spec). */
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004421 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004422 break;
4423 }
4424 }
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004425 *str = s;
4426 assert(s <= end);
4427 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004428done:
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004429 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004430 if (raw)
4431 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004432 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004433 NULL, NULL);
4434 else
Eric V. Smith56466482016-10-31 14:46:26 -04004435 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka89a31022017-05-25 14:18:55 +03004436 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004437 if (!*literal)
4438 return -1;
4439 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004440 return result;
4441}
4442
4443/* Forward declaration because parsing is recursive. */
4444static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004445fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004446 struct compiling *c, const node *n);
4447
Eric V. Smith451d0e32016-09-09 21:56:20 -04004448/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004449 expression (so it must be a '{'). Returns the FormattedValue node,
4450 which includes the expression, conversion character, and
4451 format_spec expression.
4452
4453 Note that I don't do a perfect job here: I don't make sure that a
4454 closing brace doesn't match an opening paren, for example. It
4455 doesn't need to error on all invalid expressions, just correctly
4456 find the end of all valid ones. Any errors inside the expression
4457 will be caught when we parse it later. */
4458static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004459fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004460 expr_ty *expression, struct compiling *c, const node *n)
4461{
4462 /* Return -1 on error, else 0. */
4463
Eric V. Smith451d0e32016-09-09 21:56:20 -04004464 const char *expr_start;
4465 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004466 expr_ty simple_expression;
4467 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004468 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004469
4470 /* 0 if we're not in a string, else the quote char we're trying to
4471 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004472 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004473
4474 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4475 int string_type = 0;
4476
4477 /* Keep track of nesting level for braces/parens/brackets in
4478 expressions. */
4479 Py_ssize_t nested_depth = 0;
4480
4481 /* Can only nest one level deep. */
4482 if (recurse_lvl >= 2) {
4483 ast_error(c, n, "f-string: expressions nested too deeply");
4484 return -1;
4485 }
4486
4487 /* The first char must be a left brace, or we wouldn't have gotten
4488 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004489 assert(**str == '{');
4490 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004491
Eric V. Smith451d0e32016-09-09 21:56:20 -04004492 expr_start = *str;
4493 for (; *str < end; (*str)++) {
4494 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004495
4496 /* Loop invariants. */
4497 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004498 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004499 if (quote_char)
4500 assert(string_type == 1 || string_type == 3);
4501 else
4502 assert(string_type == 0);
4503
Eric V. Smith451d0e32016-09-09 21:56:20 -04004504 ch = **str;
4505 /* Nowhere inside an expression is a backslash allowed. */
4506 if (ch == '\\') {
4507 /* Error: can't include a backslash character, inside
4508 parens or strings or not. */
4509 ast_error(c, n, "f-string expression part "
4510 "cannot include a backslash");
4511 return -1;
4512 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004513 if (quote_char) {
4514 /* We're inside a string. See if we're at the end. */
4515 /* This code needs to implement the same non-error logic
4516 as tok_get from tokenizer.c, at the letter_quote
4517 label. To actually share that code would be a
4518 nightmare. But, it's unlikely to change and is small,
4519 so duplicate it here. Note we don't need to catch all
4520 of the errors, since they'll be caught when parsing the
4521 expression. We just need to match the non-error
4522 cases. Thus we can ignore \n in single-quoted strings,
4523 for example. Or non-terminated strings. */
4524 if (ch == quote_char) {
4525 /* Does this match the string_type (single or triple
4526 quoted)? */
4527 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004528 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004529 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004530 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004531 string_type = 0;
4532 quote_char = 0;
4533 continue;
4534 }
4535 } else {
4536 /* We're at the end of a normal string. */
4537 quote_char = 0;
4538 string_type = 0;
4539 continue;
4540 }
4541 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004542 } else if (ch == '\'' || ch == '"') {
4543 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004544 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004545 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004546 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004547 } else {
4548 /* Start of a normal string. */
4549 string_type = 1;
4550 }
4551 /* Start looking for the end of the string. */
4552 quote_char = ch;
4553 } else if (ch == '[' || ch == '{' || ch == '(') {
4554 nested_depth++;
4555 } else if (nested_depth != 0 &&
4556 (ch == ']' || ch == '}' || ch == ')')) {
4557 nested_depth--;
4558 } else if (ch == '#') {
4559 /* Error: can't include a comment character, inside parens
4560 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004561 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004562 return -1;
4563 } else if (nested_depth == 0 &&
4564 (ch == '!' || ch == ':' || ch == '}')) {
4565 /* First, test for the special case of "!=". Since '=' is
4566 not an allowed conversion character, nothing is lost in
4567 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004568 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004569 /* This isn't a conversion character, just continue. */
4570 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004571 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004572 /* Normal way out of this loop. */
4573 break;
4574 } else {
4575 /* Just consume this char and loop around. */
4576 }
4577 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004578 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 /* If we leave this loop in a string or with mismatched parens, we
4580 don't care. We'll get a syntax error when compiling the
4581 expression. But, we can produce a better error message, so
4582 let's just do that.*/
4583 if (quote_char) {
4584 ast_error(c, n, "f-string: unterminated string");
4585 return -1;
4586 }
4587 if (nested_depth) {
4588 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4589 return -1;
4590 }
4591
Eric V. Smith451d0e32016-09-09 21:56:20 -04004592 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004593 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004594
4595 /* Compile the expression as soon as possible, so we show errors
4596 related to the expression before errors related to the
4597 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004598 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004599 if (!simple_expression)
4600 return -1;
4601
4602 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004603 if (**str == '!') {
4604 *str += 1;
4605 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004606 goto unexpected_end_of_string;
4607
Eric V. Smith451d0e32016-09-09 21:56:20 -04004608 conversion = **str;
4609 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004610
4611 /* Validate the conversion. */
4612 if (!(conversion == 's' || conversion == 'r'
4613 || conversion == 'a')) {
4614 ast_error(c, n, "f-string: invalid conversion character: "
4615 "expected 's', 'r', or 'a'");
4616 return -1;
4617 }
4618 }
4619
4620 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004621 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004622 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004623 if (**str == ':') {
4624 *str += 1;
4625 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004626 goto unexpected_end_of_string;
4627
4628 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004629 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004630 if (!format_spec)
4631 return -1;
4632 }
4633
Eric V. Smith451d0e32016-09-09 21:56:20 -04004634 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004635 goto unexpected_end_of_string;
4636
4637 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004638 assert(*str < end);
4639 assert(**str == '}');
4640 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004641
Eric V. Smith451d0e32016-09-09 21:56:20 -04004642 /* And now create the FormattedValue node that represents this
4643 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004644 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004645 format_spec, LINENO(n), n->n_col_offset,
4646 c->c_arena);
4647 if (!*expression)
4648 return -1;
4649
4650 return 0;
4651
4652unexpected_end_of_string:
4653 ast_error(c, n, "f-string: expecting '}'");
4654 return -1;
4655}
4656
4657/* Return -1 on error.
4658
4659 Return 0 if we have a literal (possible zero length) and an
4660 expression (zero length if at the end of the string.
4661
4662 Return 1 if we have a literal, but no expression, and we want the
4663 caller to call us again. This is used to deal with doubled
4664 braces.
4665
4666 When called multiple times on the string 'a{{b{0}c', this function
4667 will return:
4668
4669 1. the literal 'a{' with no expression, and a return value
4670 of 1. Despite the fact that there's no expression, the return
4671 value of 1 means we're not finished yet.
4672
4673 2. the literal 'b' and the expression '0', with a return value of
4674 0. The fact that there's an expression means we're not finished.
4675
4676 3. literal 'c' with no expression and a return value of 0. The
4677 combination of the return value of 0 with no expression means
4678 we're finished.
4679*/
4680static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004681fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4682 int recurse_lvl, PyObject **literal,
4683 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004684 struct compiling *c, const node *n)
4685{
4686 int result;
4687
4688 assert(*literal == NULL && *expression == NULL);
4689
4690 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004691 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004692 if (result < 0)
4693 goto error;
4694
4695 assert(result == 0 || result == 1);
4696
4697 if (result == 1)
4698 /* We have a literal, but don't look at the expression. */
4699 return 1;
4700
Eric V. Smith451d0e32016-09-09 21:56:20 -04004701 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004702 /* We're at the end of the string or the end of a nested
4703 f-string: no expression. The top-level error case where we
4704 expect to be at the end of the string but we're at a '}' is
4705 handled later. */
4706 return 0;
4707
4708 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004709 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004710
Eric V. Smith451d0e32016-09-09 21:56:20 -04004711 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004712 goto error;
4713
4714 return 0;
4715
4716error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004717 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004718 return -1;
4719}
4720
4721#define EXPRLIST_N_CACHED 64
4722
4723typedef struct {
4724 /* Incrementally build an array of expr_ty, so be used in an
4725 asdl_seq. Cache some small but reasonably sized number of
4726 expr_ty's, and then after that start dynamically allocating,
4727 doubling the number allocated each time. Note that the f-string
4728 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4729 Str for the literal 'a'. So you add expr_ty's about twice as
4730 fast as you add exressions in an f-string. */
4731
4732 Py_ssize_t allocated; /* Number we've allocated. */
4733 Py_ssize_t size; /* Number we've used. */
4734 expr_ty *p; /* Pointer to the memory we're actually
4735 using. Will point to 'data' until we
4736 start dynamically allocating. */
4737 expr_ty data[EXPRLIST_N_CACHED];
4738} ExprList;
4739
4740#ifdef NDEBUG
4741#define ExprList_check_invariants(l)
4742#else
4743static void
4744ExprList_check_invariants(ExprList *l)
4745{
4746 /* Check our invariants. Make sure this object is "live", and
4747 hasn't been deallocated. */
4748 assert(l->size >= 0);
4749 assert(l->p != NULL);
4750 if (l->size <= EXPRLIST_N_CACHED)
4751 assert(l->data == l->p);
4752}
4753#endif
4754
4755static void
4756ExprList_Init(ExprList *l)
4757{
4758 l->allocated = EXPRLIST_N_CACHED;
4759 l->size = 0;
4760
4761 /* Until we start allocating dynamically, p points to data. */
4762 l->p = l->data;
4763
4764 ExprList_check_invariants(l);
4765}
4766
4767static int
4768ExprList_Append(ExprList *l, expr_ty exp)
4769{
4770 ExprList_check_invariants(l);
4771 if (l->size >= l->allocated) {
4772 /* We need to alloc (or realloc) the memory. */
4773 Py_ssize_t new_size = l->allocated * 2;
4774
4775 /* See if we've ever allocated anything dynamically. */
4776 if (l->p == l->data) {
4777 Py_ssize_t i;
4778 /* We're still using the cached data. Switch to
4779 alloc-ing. */
4780 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4781 if (!l->p)
4782 return -1;
4783 /* Copy the cached data into the new buffer. */
4784 for (i = 0; i < l->size; i++)
4785 l->p[i] = l->data[i];
4786 } else {
4787 /* Just realloc. */
4788 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4789 if (!tmp) {
4790 PyMem_RawFree(l->p);
4791 l->p = NULL;
4792 return -1;
4793 }
4794 l->p = tmp;
4795 }
4796
4797 l->allocated = new_size;
4798 assert(l->allocated == 2 * l->size);
4799 }
4800
4801 l->p[l->size++] = exp;
4802
4803 ExprList_check_invariants(l);
4804 return 0;
4805}
4806
4807static void
4808ExprList_Dealloc(ExprList *l)
4809{
4810 ExprList_check_invariants(l);
4811
4812 /* If there's been an error, or we've never dynamically allocated,
4813 do nothing. */
4814 if (!l->p || l->p == l->data) {
4815 /* Do nothing. */
4816 } else {
4817 /* We have dynamically allocated. Free the memory. */
4818 PyMem_RawFree(l->p);
4819 }
4820 l->p = NULL;
4821 l->size = -1;
4822}
4823
4824static asdl_seq *
4825ExprList_Finish(ExprList *l, PyArena *arena)
4826{
4827 asdl_seq *seq;
4828
4829 ExprList_check_invariants(l);
4830
4831 /* Allocate the asdl_seq and copy the expressions in to it. */
4832 seq = _Py_asdl_seq_new(l->size, arena);
4833 if (seq) {
4834 Py_ssize_t i;
4835 for (i = 0; i < l->size; i++)
4836 asdl_seq_SET(seq, i, l->p[i]);
4837 }
4838 ExprList_Dealloc(l);
4839 return seq;
4840}
4841
4842/* The FstringParser is designed to add a mix of strings and
4843 f-strings, and concat them together as needed. Ultimately, it
4844 generates an expr_ty. */
4845typedef struct {
4846 PyObject *last_str;
4847 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004848 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004849} FstringParser;
4850
4851#ifdef NDEBUG
4852#define FstringParser_check_invariants(state)
4853#else
4854static void
4855FstringParser_check_invariants(FstringParser *state)
4856{
4857 if (state->last_str)
4858 assert(PyUnicode_CheckExact(state->last_str));
4859 ExprList_check_invariants(&state->expr_list);
4860}
4861#endif
4862
4863static void
4864FstringParser_Init(FstringParser *state)
4865{
4866 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004867 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 ExprList_Init(&state->expr_list);
4869 FstringParser_check_invariants(state);
4870}
4871
4872static void
4873FstringParser_Dealloc(FstringParser *state)
4874{
4875 FstringParser_check_invariants(state);
4876
4877 Py_XDECREF(state->last_str);
4878 ExprList_Dealloc(&state->expr_list);
4879}
4880
4881/* Make a Str node, but decref the PyUnicode object being added. */
4882static expr_ty
4883make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4884{
4885 PyObject *s = *str;
4886 *str = NULL;
4887 assert(PyUnicode_CheckExact(s));
4888 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4889 Py_DECREF(s);
4890 return NULL;
4891 }
4892 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4893}
4894
4895/* Add a non-f-string (that is, a regular literal string). str is
4896 decref'd. */
4897static int
4898FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4899{
4900 FstringParser_check_invariants(state);
4901
4902 assert(PyUnicode_CheckExact(str));
4903
4904 if (PyUnicode_GET_LENGTH(str) == 0) {
4905 Py_DECREF(str);
4906 return 0;
4907 }
4908
4909 if (!state->last_str) {
4910 /* We didn't have a string before, so just remember this one. */
4911 state->last_str = str;
4912 } else {
4913 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004914 PyUnicode_AppendAndDel(&state->last_str, str);
4915 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004916 return -1;
4917 }
4918 FstringParser_check_invariants(state);
4919 return 0;
4920}
4921
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922/* Parse an f-string. The f-string is in *str to end, with no
4923 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004925FstringParser_ConcatFstring(FstringParser *state, const char **str,
4926 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927 struct compiling *c, const node *n)
4928{
4929 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004930 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931
4932 /* Parse the f-string. */
4933 while (1) {
4934 PyObject *literal = NULL;
4935 expr_ty expression = NULL;
4936
4937 /* If there's a zero length literal in front of the
4938 expression, literal will be NULL. If we're at the end of
4939 the f-string, expression will be NULL (unless result == 1,
4940 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004941 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004942 &literal, &expression,
4943 c, n);
4944 if (result < 0)
4945 return -1;
4946
4947 /* Add the literal, if any. */
4948 if (!literal) {
4949 /* Do nothing. Just leave last_str alone (and possibly
4950 NULL). */
4951 } else if (!state->last_str) {
Serhiy Storchaka2eca5b42017-06-16 16:29:42 +03004952 /* Note that the literal can be zero length, if the
4953 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004954 state->last_str = literal;
4955 literal = NULL;
4956 } else {
4957 /* We have a literal, concatenate it. */
4958 assert(PyUnicode_GET_LENGTH(literal) != 0);
4959 if (FstringParser_ConcatAndDel(state, literal) < 0)
4960 return -1;
4961 literal = NULL;
4962 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963
4964 /* We've dealt with the literal now. It can't be leaked on further
4965 errors. */
4966 assert(literal == NULL);
4967
4968 /* See if we should just loop around to get the next literal
4969 and expression, while ignoring the expression this
4970 time. This is used for un-doubling braces, as an
4971 optimization. */
4972 if (result == 1)
4973 continue;
4974
4975 if (!expression)
4976 /* We're done with this f-string. */
4977 break;
4978
4979 /* We know we have an expression. Convert any existing string
4980 to a Str node. */
4981 if (!state->last_str) {
4982 /* Do nothing. No previous literal. */
4983 } else {
4984 /* Convert the existing last_str literal to a Str node. */
4985 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4986 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4987 return -1;
4988 }
4989
4990 if (ExprList_Append(&state->expr_list, expression) < 0)
4991 return -1;
4992 }
4993
Eric V. Smith235a6f02015-09-19 14:51:32 -04004994 /* If recurse_lvl is zero, then we must be at the end of the
4995 string. Otherwise, we must be at a right brace. */
4996
Eric V. Smith451d0e32016-09-09 21:56:20 -04004997 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004998 ast_error(c, n, "f-string: unexpected end of string");
4999 return -1;
5000 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005001 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005002 ast_error(c, n, "f-string: expecting '}'");
5003 return -1;
5004 }
5005
5006 FstringParser_check_invariants(state);
5007 return 0;
5008}
5009
5010/* Convert the partial state reflected in last_str and expr_list to an
5011 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5012static expr_ty
5013FstringParser_Finish(FstringParser *state, struct compiling *c,
5014 const node *n)
5015{
5016 asdl_seq *seq;
5017
5018 FstringParser_check_invariants(state);
5019
5020 /* If we're just a constant string with no expressions, return
5021 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005022 if (!state->fmode) {
5023 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 if (!state->last_str) {
5025 /* Create a zero length string. */
5026 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5027 if (!state->last_str)
5028 goto error;
5029 }
5030 return make_str_node_and_del(&state->last_str, c, n);
5031 }
5032
5033 /* Create a Str node out of last_str, if needed. It will be the
5034 last node in our expression list. */
5035 if (state->last_str) {
5036 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5037 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5038 goto error;
5039 }
5040 /* This has already been freed. */
5041 assert(state->last_str == NULL);
5042
5043 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5044 if (!seq)
5045 goto error;
5046
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5048
5049error:
5050 FstringParser_Dealloc(state);
5051 return NULL;
5052}
5053
Eric V. Smith451d0e32016-09-09 21:56:20 -04005054/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5055 at end, parse it into an expr_ty. Return NULL on error. Adjust
5056 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005058fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059 struct compiling *c, const node *n)
5060{
5061 FstringParser state;
5062
5063 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005064 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005065 c, n) < 0) {
5066 FstringParser_Dealloc(&state);
5067 return NULL;
5068 }
5069
5070 return FstringParser_Finish(&state, c, n);
5071}
5072
5073/* n is a Python string literal, including the bracketing quote
5074 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005077 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5078 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080static int
5081parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5082 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005083{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005084 size_t len;
5085 const char *s = STR(n);
5086 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005087 int fmode = 0;
5088 *bytesmode = 0;
5089 *rawmode = 0;
5090 *result = NULL;
5091 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005092 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005093 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005094 if (quote == 'b' || quote == 'B') {
5095 quote = *++s;
5096 *bytesmode = 1;
5097 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005098 else if (quote == 'u' || quote == 'U') {
5099 quote = *++s;
5100 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005101 else if (quote == 'r' || quote == 'R') {
5102 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005104 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005105 else if (quote == 'f' || quote == 'F') {
5106 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005109 else {
5110 break;
5111 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005112 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005113 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005114 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005118 if (quote != '\'' && quote != '\"') {
5119 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005120 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005121 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005122 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005123 s++;
5124 len = strlen(s);
5125 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005127 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005128 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005129 }
5130 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005132 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005133 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005134 }
5135 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005136 /* A triple quoted string. We've already skipped one quote at
5137 the start and one at the end of the string. Now skip the
5138 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005139 s += 2;
5140 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005141 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005142 if (s[--len] != quote || s[--len] != quote) {
5143 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005144 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005145 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005146 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005147
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 if (fmode) {
5149 /* Just return the bytes. The caller will parse the resulting
5150 string. */
5151 *fstr = s;
5152 *fstrlen = len;
5153 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005154 }
5155
Eric V. Smith451d0e32016-09-09 21:56:20 -04005156 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005157 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005158 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005159 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005160 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005161 const char *ch;
5162 for (ch = s; *ch; ch++) {
5163 if (Py_CHARMASK(*ch) >= 0x80) {
5164 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005165 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005166 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005167 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005168 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005169 if (*rawmode)
5170 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005171 else
Eric V. Smith56466482016-10-31 14:46:26 -04005172 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005173 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005174 if (*rawmode)
5175 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005176 else
Eric V. Smith56466482016-10-31 14:46:26 -04005177 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005178 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005179 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005180}
5181
Eric V. Smith235a6f02015-09-19 14:51:32 -04005182/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5183 each STRING atom, and process it as needed. For bytes, just
5184 concatenate them together, and the result will be a Bytes node. For
5185 normal strings and f-strings, concatenate them together. The result
5186 will be a Str node if there were no f-strings; a FormattedValue
5187 node if there's just an f-string (with no leading or trailing
5188 literals), or a JoinedStr node if there are multiple f-strings or
5189 any literals involved. */
5190static expr_ty
5191parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005192{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005193 int bytesmode = 0;
5194 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005195 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005196
5197 FstringParser state;
5198 FstringParser_Init(&state);
5199
5200 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 int this_bytesmode;
5202 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005203 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005204 const char *fstr;
5205 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005206
5207 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005208 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5209 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005210 goto error;
5211
5212 /* Check that we're not mixing bytes with unicode. */
5213 if (i != 0 && bytesmode != this_bytesmode) {
5214 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005215 /* s is NULL if the current string part is an f-string. */
5216 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005217 goto error;
5218 }
5219 bytesmode = this_bytesmode;
5220
Eric V. Smith451d0e32016-09-09 21:56:20 -04005221 if (fstr != NULL) {
5222 int result;
5223 assert(s == NULL && !bytesmode);
5224 /* This is an f-string. Parse and concatenate it. */
5225 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5226 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005227 if (result < 0)
5228 goto error;
5229 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005230 /* A string or byte string. */
5231 assert(s != NULL && fstr == NULL);
5232
Eric V. Smith451d0e32016-09-09 21:56:20 -04005233 assert(bytesmode ? PyBytes_CheckExact(s) :
5234 PyUnicode_CheckExact(s));
5235
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 if (bytesmode) {
5237 /* For bytes, concat as we go. */
5238 if (i == 0) {
5239 /* First time, just remember this value. */
5240 bytes_str = s;
5241 } else {
5242 PyBytes_ConcatAndDel(&bytes_str, s);
5243 if (!bytes_str)
5244 goto error;
5245 }
5246 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005247 /* This is a regular string. Concatenate it. */
5248 if (FstringParser_ConcatAndDel(&state, s) < 0)
5249 goto error;
5250 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005251 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005252 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005253 if (bytesmode) {
5254 /* Just return the bytes object and we're done. */
5255 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5256 goto error;
5257 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5258 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005259
Eric V. Smith235a6f02015-09-19 14:51:32 -04005260 /* We're not a bytes string, bytes_str should never have been set. */
5261 assert(bytes_str == NULL);
5262
5263 return FstringParser_Finish(&state, c, n);
5264
5265error:
5266 Py_XDECREF(bytes_str);
5267 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005269}