blob: 7eab3c08e7e19f84701685a3740e3ad62262863b [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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
135validate_expr(expr_ty exp, expr_context_ty ctx)
136{
137 int check_ctx = 1;
138 expr_context_ty actual_ctx;
139
140 /* First check expression context. */
141 switch (exp->kind) {
142 case Attribute_kind:
143 actual_ctx = exp->v.Attribute.ctx;
144 break;
145 case Subscript_kind:
146 actual_ctx = exp->v.Subscript.ctx;
147 break;
148 case Starred_kind:
149 actual_ctx = exp->v.Starred.ctx;
150 break;
151 case Name_kind:
152 actual_ctx = exp->v.Name.ctx;
153 break;
154 case List_kind:
155 actual_ctx = exp->v.List.ctx;
156 break;
157 case Tuple_kind:
158 actual_ctx = exp->v.Tuple.ctx;
159 break;
160 default:
161 if (ctx != Load) {
162 PyErr_Format(PyExc_ValueError, "expression which can't be "
163 "assigned to in %s context", expr_context_name(ctx));
164 return 0;
165 }
166 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100167 /* set actual_ctx to prevent gcc warning */
168 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500169 }
170 if (check_ctx && actual_ctx != ctx) {
171 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
172 expr_context_name(ctx), expr_context_name(actual_ctx));
173 return 0;
174 }
175
176 /* Now validate expression. */
177 switch (exp->kind) {
178 case BoolOp_kind:
179 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
180 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
181 return 0;
182 }
183 return validate_exprs(exp->v.BoolOp.values, Load, 0);
184 case BinOp_kind:
185 return validate_expr(exp->v.BinOp.left, Load) &&
186 validate_expr(exp->v.BinOp.right, Load);
187 case UnaryOp_kind:
188 return validate_expr(exp->v.UnaryOp.operand, Load);
189 case Lambda_kind:
190 return validate_arguments(exp->v.Lambda.args) &&
191 validate_expr(exp->v.Lambda.body, Load);
192 case IfExp_kind:
193 return validate_expr(exp->v.IfExp.test, Load) &&
194 validate_expr(exp->v.IfExp.body, Load) &&
195 validate_expr(exp->v.IfExp.orelse, Load);
196 case Dict_kind:
197 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
198 PyErr_SetString(PyExc_ValueError,
199 "Dict doesn't have the same number of keys as values");
200 return 0;
201 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400202 /* null_ok=1 for keys expressions to allow dict unpacking to work in
203 dict literals, i.e. ``{**{a:b}}`` */
204 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
205 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500206 case Set_kind:
207 return validate_exprs(exp->v.Set.elts, Load, 0);
208#define COMP(NAME) \
209 case NAME ## _kind: \
210 return validate_comprehension(exp->v.NAME.generators) && \
211 validate_expr(exp->v.NAME.elt, Load);
212 COMP(ListComp)
213 COMP(SetComp)
214 COMP(GeneratorExp)
215#undef COMP
216 case DictComp_kind:
217 return validate_comprehension(exp->v.DictComp.generators) &&
218 validate_expr(exp->v.DictComp.key, Load) &&
219 validate_expr(exp->v.DictComp.value, Load);
220 case Yield_kind:
221 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500222 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000223 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400224 case Await_kind:
225 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500226 case Compare_kind:
227 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
228 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
229 return 0;
230 }
231 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
232 asdl_seq_LEN(exp->v.Compare.ops)) {
233 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
234 "of comparators and operands");
235 return 0;
236 }
237 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
238 validate_expr(exp->v.Compare.left, Load);
239 case Call_kind:
240 return validate_expr(exp->v.Call.func, Load) &&
241 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400242 validate_keywords(exp->v.Call.keywords);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500243 case Num_kind: {
244 PyObject *n = exp->v.Num.n;
245 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
246 !PyComplex_CheckExact(n)) {
247 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
248 return 0;
249 }
250 return 1;
251 }
252 case Str_kind: {
253 PyObject *s = exp->v.Str.s;
254 if (!PyUnicode_CheckExact(s)) {
255 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
256 return 0;
257 }
258 return 1;
259 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400260 case JoinedStr_kind:
261 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
262 case FormattedValue_kind:
263 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
264 return 0;
265 if (exp->v.FormattedValue.format_spec)
266 return validate_expr(exp->v.FormattedValue.format_spec, Load);
267 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500268 case Bytes_kind: {
269 PyObject *b = exp->v.Bytes.s;
270 if (!PyBytes_CheckExact(b)) {
271 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
272 return 0;
273 }
274 return 1;
275 }
276 case Attribute_kind:
277 return validate_expr(exp->v.Attribute.value, Load);
278 case Subscript_kind:
279 return validate_slice(exp->v.Subscript.slice) &&
280 validate_expr(exp->v.Subscript.value, Load);
281 case Starred_kind:
282 return validate_expr(exp->v.Starred.value, ctx);
283 case List_kind:
284 return validate_exprs(exp->v.List.elts, ctx, 0);
285 case Tuple_kind:
286 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
287 /* These last cases don't have any checking. */
288 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500289 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500290 case Ellipsis_kind:
291 return 1;
292 default:
293 PyErr_SetString(PyExc_SystemError, "unexpected expression");
294 return 0;
295 }
296}
297
298static int
299validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
300{
301 if (asdl_seq_LEN(seq))
302 return 1;
303 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
304 return 0;
305}
306
307static int
308validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
309{
310 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
311 validate_exprs(targets, ctx, 0);
312}
313
314static int
315validate_body(asdl_seq *body, const char *owner)
316{
317 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
318}
319
320static int
321validate_stmt(stmt_ty stmt)
322{
323 int i;
324 switch (stmt->kind) {
325 case FunctionDef_kind:
326 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
327 validate_arguments(stmt->v.FunctionDef.args) &&
328 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
329 (!stmt->v.FunctionDef.returns ||
330 validate_expr(stmt->v.FunctionDef.returns, Load));
331 case ClassDef_kind:
332 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
333 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
334 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400335 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500336 case Return_kind:
337 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
338 case Delete_kind:
339 return validate_assignlist(stmt->v.Delete.targets, Del);
340 case Assign_kind:
341 return validate_assignlist(stmt->v.Assign.targets, Store) &&
342 validate_expr(stmt->v.Assign.value, Load);
343 case AugAssign_kind:
344 return validate_expr(stmt->v.AugAssign.target, Store) &&
345 validate_expr(stmt->v.AugAssign.value, Load);
346 case For_kind:
347 return validate_expr(stmt->v.For.target, Store) &&
348 validate_expr(stmt->v.For.iter, Load) &&
349 validate_body(stmt->v.For.body, "For") &&
350 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400351 case AsyncFor_kind:
352 return validate_expr(stmt->v.AsyncFor.target, Store) &&
353 validate_expr(stmt->v.AsyncFor.iter, Load) &&
354 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
355 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 case While_kind:
357 return validate_expr(stmt->v.While.test, Load) &&
358 validate_body(stmt->v.While.body, "While") &&
359 validate_stmts(stmt->v.While.orelse);
360 case If_kind:
361 return validate_expr(stmt->v.If.test, Load) &&
362 validate_body(stmt->v.If.body, "If") &&
363 validate_stmts(stmt->v.If.orelse);
364 case With_kind:
365 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
366 return 0;
367 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
368 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
369 if (!validate_expr(item->context_expr, Load) ||
370 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
371 return 0;
372 }
373 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400374 case AsyncWith_kind:
375 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
376 return 0;
377 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
378 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
379 if (!validate_expr(item->context_expr, Load) ||
380 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
381 return 0;
382 }
383 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500384 case Raise_kind:
385 if (stmt->v.Raise.exc) {
386 return validate_expr(stmt->v.Raise.exc, Load) &&
387 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
388 }
389 if (stmt->v.Raise.cause) {
390 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
391 return 0;
392 }
393 return 1;
394 case Try_kind:
395 if (!validate_body(stmt->v.Try.body, "Try"))
396 return 0;
397 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
398 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
399 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
400 return 0;
401 }
402 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
403 asdl_seq_LEN(stmt->v.Try.orelse)) {
404 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
405 return 0;
406 }
407 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
408 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
409 if ((handler->v.ExceptHandler.type &&
410 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
411 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
412 return 0;
413 }
414 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
415 validate_stmts(stmt->v.Try.finalbody)) &&
416 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
417 validate_stmts(stmt->v.Try.orelse));
418 case Assert_kind:
419 return validate_expr(stmt->v.Assert.test, Load) &&
420 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
421 case Import_kind:
422 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
423 case ImportFrom_kind:
424 if (stmt->v.ImportFrom.level < -1) {
425 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
426 return 0;
427 }
428 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
429 case Global_kind:
430 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
431 case Nonlocal_kind:
432 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
433 case Expr_kind:
434 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400435 case AsyncFunctionDef_kind:
436 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
437 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
438 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
439 (!stmt->v.AsyncFunctionDef.returns ||
440 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500441 case Pass_kind:
442 case Break_kind:
443 case Continue_kind:
444 return 1;
445 default:
446 PyErr_SetString(PyExc_SystemError, "unexpected statement");
447 return 0;
448 }
449}
450
451static int
452validate_stmts(asdl_seq *seq)
453{
454 int i;
455 for (i = 0; i < asdl_seq_LEN(seq); i++) {
456 stmt_ty stmt = asdl_seq_GET(seq, i);
457 if (stmt) {
458 if (!validate_stmt(stmt))
459 return 0;
460 }
461 else {
462 PyErr_SetString(PyExc_ValueError,
463 "None disallowed in statement list");
464 return 0;
465 }
466 }
467 return 1;
468}
469
470static int
471validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
472{
473 int i;
474 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
475 expr_ty expr = asdl_seq_GET(exprs, i);
476 if (expr) {
477 if (!validate_expr(expr, ctx))
478 return 0;
479 }
480 else if (!null_ok) {
481 PyErr_SetString(PyExc_ValueError,
482 "None disallowed in expression list");
483 return 0;
484 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100485
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500486 }
487 return 1;
488}
489
490int
491PyAST_Validate(mod_ty mod)
492{
493 int res = 0;
494
495 switch (mod->kind) {
496 case Module_kind:
497 res = validate_stmts(mod->v.Module.body);
498 break;
499 case Interactive_kind:
500 res = validate_stmts(mod->v.Interactive.body);
501 break;
502 case Expression_kind:
503 res = validate_expr(mod->v.Expression.body, Load);
504 break;
505 case Suite_kind:
506 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
507 break;
508 default:
509 PyErr_SetString(PyExc_SystemError, "impossible module node");
510 res = 0;
511 break;
512 }
513 return res;
514}
515
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500516/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517#include "grammar.h"
518#include "parsetok.h"
519#include "graminit.h"
520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521/* Data structure used internally */
522struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000523 char *c_encoding; /* source encoding */
Eric V. Smith163b5c62015-08-21 09:40:38 -0400524 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200525 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500526 PyObject *c_normalize; /* Normalization function from unicodedata. */
527 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528};
529
530static asdl_seq *seq_for_testlist(struct compiling *, const node *);
531static expr_ty ast_for_expr(struct compiling *, const node *);
532static stmt_ty ast_for_stmt(struct compiling *, const node *);
533static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000534static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
535 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000536static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000537static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Yury Selivanov75445082015-05-11 22:57:16 -0400539static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
540static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542/* Note different signature for ast_for_call */
543static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
544
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000545static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400546static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Nick Coghlan650f0d02007-04-15 12:05:43 +0000548#define COMP_GENEXP 0
549#define COMP_LISTCOMP 1
550#define COMP_SETCOMP 2
551
Benjamin Peterson55e00432012-01-16 17:22:31 -0500552static int
553init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000554{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500555 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
556 if (!m)
557 return 0;
558 c->c_normalize = PyObject_GetAttrString(m, "normalize");
559 Py_DECREF(m);
560 if (!c->c_normalize)
561 return 0;
562 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500563 if (!c->c_normalize_args) {
564 Py_CLEAR(c->c_normalize);
565 return 0;
566 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200567 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 return 1;
569}
570
571static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400572new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500573{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400574 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500575 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000576 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500577 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500578 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000579 /* Check whether there are non-ASCII characters in the
580 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500581 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500583 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500584 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500586 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500587 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
588 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500589 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 if (!id2)
591 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000593 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000594 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200595 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
596 Py_DECREF(id);
597 return NULL;
598 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000599 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600}
601
Benjamin Peterson55e00432012-01-16 17:22:31 -0500602#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400605ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400607 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608
Victor Stinner14e461d2013-08-26 22:28:21 +0200609 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611 Py_INCREF(Py_None);
612 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200614 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400615 if (!tmp)
616 return 0;
617 errstr = PyUnicode_FromString(errmsg);
618 if (!errstr) {
619 Py_DECREF(tmp);
620 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000621 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000622 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 Py_DECREF(errstr);
624 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400625 if (value) {
626 PyErr_SetObject(PyExc_SyntaxError, value);
627 Py_DECREF(value);
628 }
629 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630}
631
632/* num_stmts() returns number of contained statements.
633
634 Use this routine to determine how big a sequence is needed for
635 the statements in a parse tree. Its raison d'etre is this bit of
636 grammar:
637
638 stmt: simple_stmt | compound_stmt
639 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
640
641 A simple_stmt can contain multiple small_stmt elements joined
642 by semicolons. If the arg is a simple_stmt, the number of
643 small_stmt elements is returned.
644*/
645
646static int
647num_stmts(const node *n)
648{
649 int i, l;
650 node *ch;
651
652 switch (TYPE(n)) {
653 case single_input:
654 if (TYPE(CHILD(n, 0)) == NEWLINE)
655 return 0;
656 else
657 return num_stmts(CHILD(n, 0));
658 case file_input:
659 l = 0;
660 for (i = 0; i < NCH(n); i++) {
661 ch = CHILD(n, i);
662 if (TYPE(ch) == stmt)
663 l += num_stmts(ch);
664 }
665 return l;
666 case stmt:
667 return num_stmts(CHILD(n, 0));
668 case compound_stmt:
669 return 1;
670 case simple_stmt:
671 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
672 case suite:
673 if (NCH(n) == 1)
674 return num_stmts(CHILD(n, 0));
675 else {
676 l = 0;
677 for (i = 2; i < (NCH(n) - 1); i++)
678 l += num_stmts(CHILD(n, i));
679 return l;
680 }
681 default: {
682 char buf[128];
683
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000684 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 TYPE(n), NCH(n));
686 Py_FatalError(buf);
687 }
688 }
689 assert(0);
690 return 0;
691}
692
693/* Transform the CST rooted at node * to the appropriate AST
694*/
695
696mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200697PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
698 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000700 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 asdl_seq *stmts = NULL;
702 stmt_ty s;
703 node *ch;
704 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500705 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400707 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200708 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400709 c.c_filename = filename;
710 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000712 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000714#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400715 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500716 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000717#endif
718 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 } else if (TYPE(n) == encoding_decl) {
721 c.c_encoding = STR(n);
722 n = CHILD(n, 0);
723 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000725 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 }
727
Jeremy Hyltona8293132006-02-28 17:58:27 +0000728 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 switch (TYPE(n)) {
730 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200731 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500733 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 for (i = 0; i < NCH(n) - 1; i++) {
735 ch = CHILD(n, i);
736 if (TYPE(ch) == NEWLINE)
737 continue;
738 REQ(ch, stmt);
739 num = num_stmts(ch);
740 if (num == 1) {
741 s = ast_for_stmt(&c, ch);
742 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500743 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000744 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 }
746 else {
747 ch = CHILD(ch, 0);
748 REQ(ch, simple_stmt);
749 for (j = 0; j < num; j++) {
750 s = ast_for_stmt(&c, CHILD(ch, j * 2));
751 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500752 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000753 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 }
755 }
756 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 res = Module(stmts, arena);
758 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 case eval_input: {
760 expr_ty testlist_ast;
761
Nick Coghlan650f0d02007-04-15 12:05:43 +0000762 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000763 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500765 goto out;
766 res = Expression(testlist_ast, arena);
767 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 }
769 case single_input:
770 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200771 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500773 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000774 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
775 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000776 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500777 goto out;
778 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 }
780 else {
781 n = CHILD(n, 0);
782 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200783 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500785 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000787 s = ast_for_stmt(&c, n);
788 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500789 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 asdl_seq_SET(stmts, 0, s);
791 }
792 else {
793 /* Only a simple_stmt can contain multiple statements. */
794 REQ(n, simple_stmt);
795 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 if (TYPE(CHILD(n, i)) == NEWLINE)
797 break;
798 s = ast_for_stmt(&c, CHILD(n, i));
799 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500800 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 asdl_seq_SET(stmts, i / 2, s);
802 }
803 }
804
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500807 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000809 PyErr_Format(PyExc_SystemError,
810 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500813 out:
814 if (c.c_normalize) {
815 Py_DECREF(c.c_normalize);
816 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
817 Py_DECREF(c.c_normalize_args);
818 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820}
821
Victor Stinner14e461d2013-08-26 22:28:21 +0200822mod_ty
823PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
824 PyArena *arena)
825{
826 mod_ty mod;
827 PyObject *filename;
828 filename = PyUnicode_DecodeFSDefault(filename_str);
829 if (filename == NULL)
830 return NULL;
831 mod = PyAST_FromNodeObject(n, flags, filename, arena);
832 Py_DECREF(filename);
833 return mod;
834
835}
836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
838*/
839
840static operator_ty
841get_operator(const node *n)
842{
843 switch (TYPE(n)) {
844 case VBAR:
845 return BitOr;
846 case CIRCUMFLEX:
847 return BitXor;
848 case AMPER:
849 return BitAnd;
850 case LEFTSHIFT:
851 return LShift;
852 case RIGHTSHIFT:
853 return RShift;
854 case PLUS:
855 return Add;
856 case MINUS:
857 return Sub;
858 case STAR:
859 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400860 case AT:
861 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 case SLASH:
863 return Div;
864 case DOUBLESLASH:
865 return FloorDiv;
866 case PERCENT:
867 return Mod;
868 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 }
871}
872
Guido van Rossume7ba4952007-06-06 23:52:48 +0000873static const char* FORBIDDEN[] = {
874 "None",
875 "True",
876 "False",
877 NULL,
878};
879
880static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400881forbidden_name(struct compiling *c, identifier name, const node *n,
882 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000883{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000884 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000885 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400886 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000887 return 1;
888 }
889 if (full_checks) {
890 const char **p;
891 for (p = FORBIDDEN; *p; p++) {
892 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400893 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000894 return 1;
895 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000896 }
897 }
898 return 0;
899}
900
Jeremy Hyltona8293132006-02-28 17:58:27 +0000901/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902
903 Only sets context for expr kinds that "can appear in assignment context"
904 (according to ../Parser/Python.asdl). For other expr kinds, it sets
905 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906*/
907
908static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000909set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910{
911 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000912 /* If a particular expression type can't be used for assign / delete,
913 set expr_name to its name and an error message will be generated.
914 */
915 const char* expr_name = NULL;
916
917 /* The ast defines augmented store and load contexts, but the
918 implementation here doesn't actually use them. The code may be
919 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000920 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000921 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000922 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000923 */
924 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925
926 switch (e->kind) {
927 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000928 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400929 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000930 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000931 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000933 e->v.Subscript.ctx = ctx;
934 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000935 case Starred_kind:
936 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000937 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000938 return 0;
939 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000941 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500942 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000943 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000944 }
945 e->v.Name.ctx = ctx;
946 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000948 e->v.List.ctx = ctx;
949 s = e->v.List.elts;
950 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000952 if (asdl_seq_LEN(e->v.Tuple.elts)) {
953 e->v.Tuple.ctx = ctx;
954 s = e->v.Tuple.elts;
955 }
956 else {
957 expr_name = "()";
958 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000959 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000960 case Lambda_kind:
961 expr_name = "lambda";
962 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000965 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000966 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000968 case UnaryOp_kind:
969 expr_name = "operator";
970 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000972 expr_name = "generator expression";
973 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000974 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500975 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000976 expr_name = "yield expression";
977 break;
Yury Selivanov75445082015-05-11 22:57:16 -0400978 case Await_kind:
979 expr_name = "await expression";
980 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000981 case ListComp_kind:
982 expr_name = "list comprehension";
983 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000984 case SetComp_kind:
985 expr_name = "set comprehension";
986 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000987 case DictComp_kind:
988 expr_name = "dict comprehension";
989 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000990 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000991 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 case Num_kind:
993 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500994 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -0400995 case JoinedStr_kind:
996 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000997 expr_name = "literal";
998 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500999 case NameConstant_kind:
1000 expr_name = "keyword";
1001 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001002 case Ellipsis_kind:
1003 expr_name = "Ellipsis";
1004 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 case Compare_kind:
1006 expr_name = "comparison";
1007 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008 case IfExp_kind:
1009 expr_name = "conditional expression";
1010 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyErr_Format(PyExc_SystemError,
1013 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001014 e->kind, e->lineno);
1015 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001017 /* Check for error string set by switch */
1018 if (expr_name) {
1019 char buf[300];
1020 PyOS_snprintf(buf, sizeof(buf),
1021 "can't %s %s",
1022 ctx == Store ? "assign to" : "delete",
1023 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001024 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001025 }
1026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 */
1030 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001031 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032
Thomas Wouters89f507f2006-12-13 04:49:30 +00001033 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001034 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001035 return 0;
1036 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 }
1038 return 1;
1039}
1040
1041static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001042ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043{
1044 REQ(n, augassign);
1045 n = CHILD(n, 0);
1046 switch (STR(n)[0]) {
1047 case '+':
1048 return Add;
1049 case '-':
1050 return Sub;
1051 case '/':
1052 if (STR(n)[1] == '/')
1053 return FloorDiv;
1054 else
1055 return Div;
1056 case '%':
1057 return Mod;
1058 case '<':
1059 return LShift;
1060 case '>':
1061 return RShift;
1062 case '&':
1063 return BitAnd;
1064 case '^':
1065 return BitXor;
1066 case '|':
1067 return BitOr;
1068 case '*':
1069 if (STR(n)[1] == '*')
1070 return Pow;
1071 else
1072 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001073 case '@':
1074 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001076 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001077 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 }
1079}
1080
1081static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001082ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001084 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 |'is' 'not'
1086 */
1087 REQ(n, comp_op);
1088 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001089 n = CHILD(n, 0);
1090 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 case LESS:
1092 return Lt;
1093 case GREATER:
1094 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001095 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 return Eq;
1097 case LESSEQUAL:
1098 return LtE;
1099 case GREATEREQUAL:
1100 return GtE;
1101 case NOTEQUAL:
1102 return NotEq;
1103 case NAME:
1104 if (strcmp(STR(n), "in") == 0)
1105 return In;
1106 if (strcmp(STR(n), "is") == 0)
1107 return Is;
1108 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001109 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 }
1114 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001115 /* handle "not in" and "is not" */
1116 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 case NAME:
1118 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1119 return NotIn;
1120 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1121 return IsNot;
1122 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001123 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001126 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 }
Neal Norwitz79792652005-11-14 04:25:03 +00001128 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001130 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131}
1132
1133static asdl_seq *
1134seq_for_testlist(struct compiling *c, const node *n)
1135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001137 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1138 */
Armin Rigo31441302005-10-21 12:57:31 +00001139 asdl_seq *seq;
1140 expr_ty expression;
1141 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001142 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001144 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 if (!seq)
1146 return NULL;
1147
1148 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001150 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151
Benjamin Peterson4905e802009-09-27 02:43:28 +00001152 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001153 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155
1156 assert(i / 2 < seq->size);
1157 asdl_seq_SET(seq, i / 2, expression);
1158 }
1159 return seq;
1160}
1161
Neal Norwitzc1505362006-12-28 06:47:50 +00001162static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001163ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001164{
1165 identifier name;
1166 expr_ty annotation = NULL;
1167 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001168 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001169
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001170 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001171 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001172 name = NEW_IDENTIFIER(ch);
1173 if (!name)
1174 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001175 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001176 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001177
1178 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1179 annotation = ast_for_expr(c, CHILD(n, 2));
1180 if (!annotation)
1181 return NULL;
1182 }
1183
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001184 ret = arg(name, annotation, c->c_arena);
1185 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001186 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001187 ret->lineno = LINENO(n);
1188 ret->col_offset = n->n_col_offset;
1189 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190}
1191
Guido van Rossum4f72a782006-10-27 23:31:49 +00001192/* returns -1 if failed to handle keyword only arguments
1193 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001194 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001195 ^^^
1196 start pointing here
1197 */
1198static int
1199handle_keywordonly_args(struct compiling *c, const node *n, int start,
1200 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1201{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001202 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001203 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001204 expr_ty expression, annotation;
1205 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001206 int i = start;
1207 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001208
1209 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001210 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001211 return -1;
1212 }
1213 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001214 while (i < NCH(n)) {
1215 ch = CHILD(n, i);
1216 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 case vfpdef:
1218 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001219 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001220 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001221 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001222 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001223 asdl_seq_SET(kwdefaults, j, expression);
1224 i += 2; /* '=' and test */
1225 }
1226 else { /* setting NULL if no default value exists */
1227 asdl_seq_SET(kwdefaults, j, NULL);
1228 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001229 if (NCH(ch) == 3) {
1230 /* ch is NAME ':' test */
1231 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001232 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001233 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001234 }
1235 else {
1236 annotation = NULL;
1237 }
1238 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001239 argname = NEW_IDENTIFIER(ch);
1240 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001241 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001242 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001243 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001244 arg = arg(argname, annotation, c->c_arena);
1245 if (!arg)
1246 goto error;
Benjamin Peterson0714b8b2014-02-13 19:22:14 -05001247 arg->lineno = LINENO(ch);
1248 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001250 i += 2; /* the name and the comma */
1251 break;
1252 case DOUBLESTAR:
1253 return i;
1254 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001255 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001256 goto error;
1257 }
1258 }
1259 return i;
1260 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263
Jeremy Hyltona8293132006-02-28 17:58:27 +00001264/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265
1266static arguments_ty
1267ast_for_arguments(struct compiling *c, const node *n)
1268{
Neal Norwitzc1505362006-12-28 06:47:50 +00001269 /* This function handles both typedargslist (function definition)
1270 and varargslist (lambda definition).
1271
1272 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001273 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1274 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1275 | '**' tfpdef [',']]]
1276 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1277 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001278 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001279 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1280 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1281 | '**' vfpdef [',']]]
1282 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1283 | '**' vfpdef [',']
1284 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001285 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001286
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1289 int nposdefaults = 0, found_default = 0;
1290 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001291 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001292 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 node *ch;
1294
1295 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001296 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001297 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001298 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301
Jeremy Hyltone921e022008-07-17 16:37:17 +00001302 /* First count the number of positional args & defaults. The
1303 variable i is the loop index for this for loop and the next.
1304 The next loop picks up where the first leaves off.
1305 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001307 ch = CHILD(n, i);
1308 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001309 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001310 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001311 if (i < NCH(n) && /* skip argument following star */
1312 (TYPE(CHILD(n, i)) == tfpdef ||
1313 TYPE(CHILD(n, i)) == vfpdef)) {
1314 i++;
1315 }
1316 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001317 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001318 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001319 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001323 defaults for keyword only args */
1324 for ( ; i < NCH(n); ++i) {
1325 ch = CHILD(n, i);
1326 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001327 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001329 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001330 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001331 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001333 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001335 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001337 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001339 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001341 since we set NULL as default for keyword only argument w/o default
1342 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001343 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001344 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001346 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347
1348 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001349 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001353 /* tfpdef: NAME [':' test]
1354 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 */
1356 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001357 j = 0; /* index for defaults */
1358 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 ch = CHILD(n, i);
1361 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001362 case tfpdef:
1363 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1365 anything other than EQUAL or a comma? */
1366 /* XXX Should NCH(n) check be made a separate check? */
1367 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001368 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1369 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001370 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 assert(posdefaults != NULL);
1372 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001377 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001379 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001381 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001382 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001383 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 i += 2; /* the name and the comma */
1386 break;
1387 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001388 if (i+1 >= NCH(n) ||
1389 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001390 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001391 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001392 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001395 if (TYPE(ch) == COMMA) {
1396 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 i += 2; /* now follows keyword only arguments */
1398 res = handle_keywordonly_args(c, n, i,
1399 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001400 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401 i = res; /* res has new position to process */
1402 }
1403 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001404 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001405 if (!vararg)
1406 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001407
Guido van Rossum4f72a782006-10-27 23:31:49 +00001408 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001409 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1410 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 int res = 0;
1412 res = handle_keywordonly_args(c, n, i,
1413 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001414 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 i = res; /* res has new position to process */
1416 }
1417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 break;
1419 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001420 ch = CHILD(n, i+1); /* tfpdef */
1421 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001422 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001423 if (!kwarg)
1424 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 i += 3;
1426 break;
1427 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001428 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 "unexpected node in varargslist: %d @ %d",
1430 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001431 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001434 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435}
1436
1437static expr_ty
1438ast_for_dotted_name(struct compiling *c, const node *n)
1439{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001440 expr_ty e;
1441 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001442 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 int i;
1444
1445 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001446
1447 lineno = LINENO(n);
1448 col_offset = n->n_col_offset;
1449
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 id = NEW_IDENTIFIER(CHILD(n, 0));
1451 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001452 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001453 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456
1457 for (i = 2; i < NCH(n); i+=2) {
1458 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001459 if (!id)
1460 return NULL;
1461 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1462 if (!e)
1463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 }
1465
1466 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467}
1468
1469static expr_ty
1470ast_for_decorator(struct compiling *c, const node *n)
1471{
1472 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1473 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001474 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001477 REQ(CHILD(n, 0), AT);
1478 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1481 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001482 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001485 d = name_expr;
1486 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 }
1488 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001489 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001491 if (!d)
1492 return NULL;
1493 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 }
1495 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001496 d = ast_for_call(c, CHILD(n, 3), name_expr);
1497 if (!d)
1498 return NULL;
1499 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 }
1501
1502 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503}
1504
1505static asdl_seq*
1506ast_for_decorators(struct compiling *c, const node *n)
1507{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001508 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001509 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001513 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 if (!decorator_seq)
1515 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001518 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001519 if (!d)
1520 return NULL;
1521 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 }
1523 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524}
1525
1526static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001527ast_for_funcdef_impl(struct compiling *c, const node *n,
1528 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001530 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001531 identifier name;
1532 arguments_ty args;
1533 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001534 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001535 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536
1537 REQ(n, funcdef);
1538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 name = NEW_IDENTIFIER(CHILD(n, name_i));
1540 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001541 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001542 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001543 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1545 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001546 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001547 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1548 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1549 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001550 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001551 name_i += 2;
1552 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 body = ast_for_suite(c, CHILD(n, name_i + 3));
1554 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001555 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556
Yury Selivanov75445082015-05-11 22:57:16 -04001557 if (is_async)
1558 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1559 LINENO(n),
1560 n->n_col_offset, c->c_arena);
1561 else
1562 return FunctionDef(name, args, body, decorator_seq, returns,
1563 LINENO(n),
1564 n->n_col_offset, c->c_arena);
1565}
1566
1567static stmt_ty
1568ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1569{
1570 /* async_funcdef: ASYNC funcdef */
1571 REQ(n, async_funcdef);
1572 REQ(CHILD(n, 0), ASYNC);
1573 REQ(CHILD(n, 1), funcdef);
1574
1575 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1576 1 /* is_async */);
1577}
1578
1579static stmt_ty
1580ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1581{
1582 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1583 return ast_for_funcdef_impl(c, n, decorator_seq,
1584 0 /* is_async */);
1585}
1586
1587
1588static stmt_ty
1589ast_for_async_stmt(struct compiling *c, const node *n)
1590{
1591 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1592 REQ(n, async_stmt);
1593 REQ(CHILD(n, 0), ASYNC);
1594
1595 switch (TYPE(CHILD(n, 1))) {
1596 case funcdef:
1597 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1598 1 /* is_async */);
1599 case with_stmt:
1600 return ast_for_with_stmt(c, CHILD(n, 1),
1601 1 /* is_async */);
1602
1603 case for_stmt:
1604 return ast_for_for_stmt(c, CHILD(n, 1),
1605 1 /* is_async */);
1606
1607 default:
1608 PyErr_Format(PyExc_SystemError,
1609 "invalid async stament: %s",
1610 STR(CHILD(n, 1)));
1611 return NULL;
1612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613}
1614
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001615static stmt_ty
1616ast_for_decorated(struct compiling *c, const node *n)
1617{
Yury Selivanov75445082015-05-11 22:57:16 -04001618 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001619 stmt_ty thing = NULL;
1620 asdl_seq *decorator_seq = NULL;
1621
1622 REQ(n, decorated);
1623
1624 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1625 if (!decorator_seq)
1626 return NULL;
1627
1628 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001629 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001631
1632 if (TYPE(CHILD(n, 1)) == funcdef) {
1633 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1634 } else if (TYPE(CHILD(n, 1)) == classdef) {
1635 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001636 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1637 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001638 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001639 /* we count the decorators in when talking about the class' or
1640 * function's line number */
1641 if (thing) {
1642 thing->lineno = LINENO(n);
1643 thing->col_offset = n->n_col_offset;
1644 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001645 return thing;
1646}
1647
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648static expr_ty
1649ast_for_lambdef(struct compiling *c, const node *n)
1650{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001651 /* lambdef: 'lambda' [varargslist] ':' test
1652 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 arguments_ty args;
1654 expr_ty expression;
1655
1656 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001657 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 if (!args)
1659 return NULL;
1660 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001661 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 }
1664 else {
1665 args = ast_for_arguments(c, CHILD(n, 1));
1666 if (!args)
1667 return NULL;
1668 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001669 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 }
1672
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674}
1675
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001676static expr_ty
1677ast_for_ifexpr(struct compiling *c, const node *n)
1678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001680 expr_ty expression, body, orelse;
1681
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001682 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001683 body = ast_for_expr(c, CHILD(n, 0));
1684 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001685 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001686 expression = ast_for_expr(c, CHILD(n, 2));
1687 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001688 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001689 orelse = ast_for_expr(c, CHILD(n, 4));
1690 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001691 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1693 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001694}
1695
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001697 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698
Nick Coghlan650f0d02007-04-15 12:05:43 +00001699 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700*/
1701
1702static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001703count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001705 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706
Guido van Rossumd8faa362007-04-27 19:54:29 +00001707 count_comp_for:
1708 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001709 REQ(n, comp_for);
1710 if (NCH(n) == 5)
1711 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001712 else
1713 return n_fors;
1714 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001715 REQ(n, comp_iter);
1716 n = CHILD(n, 0);
1717 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001718 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001719 else if (TYPE(n) == comp_if) {
1720 if (NCH(n) == 3) {
1721 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001722 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001723 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001724 else
1725 return n_fors;
1726 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001727
Guido van Rossumd8faa362007-04-27 19:54:29 +00001728 /* Should never be reached */
1729 PyErr_SetString(PyExc_SystemError,
1730 "logic error in count_comp_fors");
1731 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732}
1733
Nick Coghlan650f0d02007-04-15 12:05:43 +00001734/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735
Nick Coghlan650f0d02007-04-15 12:05:43 +00001736 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737*/
1738
1739static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001740count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001742 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743
Guido van Rossumd8faa362007-04-27 19:54:29 +00001744 while (1) {
1745 REQ(n, comp_iter);
1746 if (TYPE(CHILD(n, 0)) == comp_for)
1747 return n_ifs;
1748 n = CHILD(n, 0);
1749 REQ(n, comp_if);
1750 n_ifs++;
1751 if (NCH(n) == 2)
1752 return n_ifs;
1753 n = CHILD(n, 2);
1754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755}
1756
Guido van Rossum992d4a32007-07-11 13:09:30 +00001757static asdl_seq *
1758ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001761 asdl_seq *comps;
1762
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001763 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 if (n_fors == -1)
1765 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001766
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001767 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001768 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001772 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001774 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001775 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776
Guido van Rossum992d4a32007-07-11 13:09:30 +00001777 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778
Guido van Rossum992d4a32007-07-11 13:09:30 +00001779 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001780 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001781 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001783 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001784 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001786
Thomas Wouters89f507f2006-12-13 04:49:30 +00001787 /* Check the # of children rather than the length of t, since
1788 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001789 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001790 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001791 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001793 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1794 c->c_arena),
1795 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001796 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001798
Guido van Rossum992d4a32007-07-11 13:09:30 +00001799 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 int j, n_ifs;
1801 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802
Guido van Rossum992d4a32007-07-11 13:09:30 +00001803 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001804 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001805 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001807
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001808 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001809 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001813 REQ(n, comp_iter);
1814 n = CHILD(n, 0);
1815 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816
Guido van Rossum992d4a32007-07-11 13:09:30 +00001817 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001818 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001819 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001820 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001821 if (NCH(n) == 3)
1822 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001824 /* on exit, must guarantee that n is a comp_for */
1825 if (TYPE(n) == comp_iter)
1826 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001827 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001829 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001831 return comps;
1832}
1833
1834static expr_ty
1835ast_for_itercomp(struct compiling *c, const node *n, int type)
1836{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001837 /* testlist_comp: (test|star_expr)
1838 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001839 expr_ty elt;
1840 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001841 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842
Guido van Rossum992d4a32007-07-11 13:09:30 +00001843 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001845 ch = CHILD(n, 0);
1846 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001847 if (!elt)
1848 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001849 if (elt->kind == Starred_kind) {
1850 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1851 return NULL;
1852 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853
Guido van Rossum992d4a32007-07-11 13:09:30 +00001854 comps = ast_for_comprehension(c, CHILD(n, 1));
1855 if (!comps)
1856 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001857
1858 if (type == COMP_GENEXP)
1859 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1860 else if (type == COMP_LISTCOMP)
1861 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1862 else if (type == COMP_SETCOMP)
1863 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1864 else
1865 /* Should never happen */
1866 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867}
1868
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001869/* Fills in the key, value pair corresponding to the dict element. In case
1870 * of an unpacking, key is NULL. *i is advanced by the number of ast
1871 * elements. Iff successful, nonzero is returned.
1872 */
1873static int
1874ast_for_dictelement(struct compiling *c, const node *n, int *i,
1875 expr_ty *key, expr_ty *value)
1876{
1877 expr_ty expression;
1878 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1879 assert(NCH(n) - *i >= 2);
1880
1881 expression = ast_for_expr(c, CHILD(n, *i + 1));
1882 if (!expression)
1883 return 0;
1884 *key = NULL;
1885 *value = expression;
1886
1887 *i += 2;
1888 }
1889 else {
1890 assert(NCH(n) - *i >= 3);
1891
1892 expression = ast_for_expr(c, CHILD(n, *i));
1893 if (!expression)
1894 return 0;
1895 *key = expression;
1896
1897 REQ(CHILD(n, *i + 1), COLON);
1898
1899 expression = ast_for_expr(c, CHILD(n, *i + 2));
1900 if (!expression)
1901 return 0;
1902 *value = expression;
1903
1904 *i += 3;
1905 }
1906 return 1;
1907}
1908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001910ast_for_dictcomp(struct compiling *c, const node *n)
1911{
1912 expr_ty key, value;
1913 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001916 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001917 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918 assert(key);
1919 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001921 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001922 if (!comps)
1923 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924
Guido van Rossum992d4a32007-07-11 13:09:30 +00001925 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1926}
1927
1928static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001929ast_for_dictdisplay(struct compiling *c, const node *n)
1930{
1931 int i;
1932 int j;
1933 int size;
1934 asdl_seq *keys, *values;
1935
1936 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1937 keys = _Py_asdl_seq_new(size, c->c_arena);
1938 if (!keys)
1939 return NULL;
1940
1941 values = _Py_asdl_seq_new(size, c->c_arena);
1942 if (!values)
1943 return NULL;
1944
1945 j = 0;
1946 for (i = 0; i < NCH(n); i++) {
1947 expr_ty key, value;
1948
1949 if (!ast_for_dictelement(c, n, &i, &key, &value))
1950 return NULL;
1951 asdl_seq_SET(keys, j, key);
1952 asdl_seq_SET(values, j, value);
1953
1954 j++;
1955 }
1956 keys->size = j;
1957 values->size = j;
1958 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1959}
1960
1961static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001962ast_for_genexp(struct compiling *c, const node *n)
1963{
1964 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001965 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001966}
1967
1968static expr_ty
1969ast_for_listcomp(struct compiling *c, const node *n)
1970{
1971 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001972 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001973}
1974
1975static expr_ty
1976ast_for_setcomp(struct compiling *c, const node *n)
1977{
1978 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001979 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001980}
1981
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001982static expr_ty
1983ast_for_setdisplay(struct compiling *c, const node *n)
1984{
1985 int i;
1986 int size;
1987 asdl_seq *elts;
1988
1989 assert(TYPE(n) == (dictorsetmaker));
1990 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
1991 elts = _Py_asdl_seq_new(size, c->c_arena);
1992 if (!elts)
1993 return NULL;
1994 for (i = 0; i < NCH(n); i += 2) {
1995 expr_ty expression;
1996 expression = ast_for_expr(c, CHILD(n, i));
1997 if (!expression)
1998 return NULL;
1999 asdl_seq_SET(elts, i / 2, expression);
2000 }
2001 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2002}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002003
2004static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005ast_for_atom(struct compiling *c, const node *n)
2006{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002007 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2008 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002009 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 */
2011 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002014 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002015 PyObject *name;
2016 const char *s = STR(ch);
2017 size_t len = strlen(s);
2018 if (len >= 4 && len <= 5) {
2019 if (!strcmp(s, "None"))
2020 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2021 if (!strcmp(s, "True"))
2022 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2023 if (!strcmp(s, "False"))
2024 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2025 }
2026 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002027 if (!name)
2028 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002029 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002030 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2031 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002033 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002034 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002035 const char *errtype = NULL;
2036 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2037 errtype = "unicode error";
2038 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2039 errtype = "value error";
2040 if (errtype) {
2041 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002042 PyObject *type, *value, *tback, *errstr;
2043 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002044 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002045 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002046 char *s = _PyUnicode_AsString(errstr);
2047 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002048 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002049 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002050 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002051 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002052 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002053 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002054 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002055 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002056 Py_XDECREF(tback);
2057 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002058 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002059 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002060 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 }
2062 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002063 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002064 if (!pynum)
2065 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002066
Victor Stinner43d81952013-07-17 00:57:58 +02002067 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2068 Py_DECREF(pynum);
2069 return NULL;
2070 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002071 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 }
Georg Brandldde00282007-03-18 19:01:53 +00002073 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002074 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002076 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 if (TYPE(ch) == RPAR)
2079 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080
Thomas Wouters89f507f2006-12-13 04:49:30 +00002081 if (TYPE(ch) == yield_expr)
2082 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002085 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002086 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002087
Nick Coghlan650f0d02007-04-15 12:05:43 +00002088 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002090 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091
Thomas Wouters89f507f2006-12-13 04:49:30 +00002092 if (TYPE(ch) == RSQB)
2093 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094
Nick Coghlan650f0d02007-04-15 12:05:43 +00002095 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002096 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2097 asdl_seq *elts = seq_for_testlist(c, ch);
2098 if (!elts)
2099 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002100
Thomas Wouters89f507f2006-12-13 04:49:30 +00002101 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2102 }
2103 else
2104 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002106 /* dictorsetmaker: ( ((test ':' test | '**' test)
2107 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2108 * ((test | '*' test)
2109 * (comp_for | (',' (test | '*' test))* [','])) ) */
Neal Norwitzc1505362006-12-28 06:47:50 +00002110 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002111 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002112 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002113 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002114 }
2115 else {
2116 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2117 if (NCH(ch) == 1 ||
2118 (NCH(ch) > 1 &&
2119 TYPE(CHILD(ch, 1)) == COMMA)) {
2120 /* It's a set display. */
2121 return ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002122 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002123 else if (NCH(ch) > 1 &&
2124 TYPE(CHILD(ch, 1)) == comp_for) {
2125 /* It's a set comprehension. */
2126 return ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002127 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002128 else if (NCH(ch) > 3 - is_dict &&
2129 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2130 /* It's a dictionary comprehension. */
2131 if (is_dict) {
2132 ast_error(c, n, "dict unpacking cannot be used in "
2133 "dict comprehension");
2134 return NULL;
2135 }
2136 return ast_for_dictcomp(c, ch);
2137 }
2138 else {
2139 /* It's a dictionary display. */
2140 return ast_for_dictdisplay(c, ch);
2141 }
Guido van Rossum86e58e22006-08-28 15:27:34 +00002142 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002145 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 }
2148}
2149
2150static slice_ty
2151ast_for_slice(struct compiling *c, const node *n)
2152{
2153 node *ch;
2154 expr_ty lower = NULL, upper = NULL, step = NULL;
2155
2156 REQ(n, subscript);
2157
2158 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002159 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 sliceop: ':' [test]
2161 */
2162 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 if (NCH(n) == 1 && TYPE(ch) == test) {
2164 /* 'step' variable hold no significance in terms of being used over
2165 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 if (!step)
2168 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169
Thomas Wouters89f507f2006-12-13 04:49:30 +00002170 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 }
2172
2173 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002174 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 if (!lower)
2176 return NULL;
2177 }
2178
2179 /* If there's an upper bound it's in the second or third position. */
2180 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 if (NCH(n) > 1) {
2182 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183
Thomas Wouters89f507f2006-12-13 04:49:30 +00002184 if (TYPE(n2) == test) {
2185 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 if (!upper)
2187 return NULL;
2188 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002189 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002191 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192
Thomas Wouters89f507f2006-12-13 04:49:30 +00002193 if (TYPE(n2) == test) {
2194 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 if (!upper)
2196 return NULL;
2197 }
2198 }
2199
2200 ch = CHILD(n, NCH(n) - 1);
2201 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002202 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002203 ch = CHILD(ch, 1);
2204 if (TYPE(ch) == test) {
2205 step = ast_for_expr(c, ch);
2206 if (!step)
2207 return NULL;
2208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 }
2210 }
2211
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002212 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213}
2214
2215static expr_ty
2216ast_for_binop(struct compiling *c, const node *n)
2217{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002218 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002220 BinOp(BinOp(A, op, B), op, C).
2221 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Guido van Rossumd8faa362007-04-27 19:54:29 +00002223 int i, nops;
2224 expr_ty expr1, expr2, result;
2225 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226
Guido van Rossumd8faa362007-04-27 19:54:29 +00002227 expr1 = ast_for_expr(c, CHILD(n, 0));
2228 if (!expr1)
2229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230
Guido van Rossumd8faa362007-04-27 19:54:29 +00002231 expr2 = ast_for_expr(c, CHILD(n, 2));
2232 if (!expr2)
2233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234
Guido van Rossumd8faa362007-04-27 19:54:29 +00002235 newoperator = get_operator(CHILD(n, 1));
2236 if (!newoperator)
2237 return NULL;
2238
2239 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2240 c->c_arena);
2241 if (!result)
2242 return NULL;
2243
2244 nops = (NCH(n) - 1) / 2;
2245 for (i = 1; i < nops; i++) {
2246 expr_ty tmp_result, tmp;
2247 const node* next_oper = CHILD(n, i * 2 + 1);
2248
2249 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002250 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 return NULL;
2252
Guido van Rossumd8faa362007-04-27 19:54:29 +00002253 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2254 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 return NULL;
2256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002258 LINENO(next_oper), next_oper->n_col_offset,
2259 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002261 return NULL;
2262 result = tmp_result;
2263 }
2264 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265}
2266
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002267static expr_ty
2268ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002271 subscriptlist: subscript (',' subscript)* [',']
2272 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2273 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002274 REQ(n, trailer);
2275 if (TYPE(CHILD(n, 0)) == LPAR) {
2276 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002277 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002278 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002279 else
2280 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002281 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002282 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002283 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2284 if (!attr_id)
2285 return NULL;
2286 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002287 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002288 }
2289 else {
2290 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002291 REQ(CHILD(n, 2), RSQB);
2292 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002293 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002294 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2295 if (!slc)
2296 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002297 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2298 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002299 }
2300 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002302 by treating the sequence as a tuple literal if there are
2303 no slice features.
2304 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002305 int j;
2306 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002307 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002308 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002309 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002310 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002311 if (!slices)
2312 return NULL;
2313 for (j = 0; j < NCH(n); j += 2) {
2314 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002315 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002316 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002317 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002318 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002319 asdl_seq_SET(slices, j / 2, slc);
2320 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002321 if (!simple) {
2322 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002323 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002324 }
2325 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002326 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002327 if (!elts)
2328 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002329 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2330 slc = (slice_ty)asdl_seq_GET(slices, j);
2331 assert(slc->kind == Index_kind && slc->v.Index.value);
2332 asdl_seq_SET(elts, j, slc->v.Index.value);
2333 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002334 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002335 if (!e)
2336 return NULL;
2337 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002338 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002339 }
2340 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002341}
2342
2343static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002344ast_for_factor(struct compiling *c, const node *n)
2345{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002346 expr_ty expression;
2347
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002348 expression = ast_for_expr(c, CHILD(n, 1));
2349 if (!expression)
2350 return NULL;
2351
2352 switch (TYPE(CHILD(n, 0))) {
2353 case PLUS:
2354 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2355 c->c_arena);
2356 case MINUS:
2357 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2358 c->c_arena);
2359 case TILDE:
2360 return UnaryOp(Invert, expression, LINENO(n),
2361 n->n_col_offset, c->c_arena);
2362 }
2363 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2364 TYPE(CHILD(n, 0)));
2365 return NULL;
2366}
2367
2368static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002369ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002370{
Yury Selivanov75445082015-05-11 22:57:16 -04002371 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002373
2374 REQ(n, atom_expr);
2375 nch = NCH(n);
2376
2377 if (TYPE(CHILD(n, 0)) == AWAIT) {
2378 start = 1;
2379 assert(nch > 1);
2380 }
2381
2382 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002383 if (!e)
2384 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002385 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002387 if (start && nch == 2) {
2388 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2389 }
2390
2391 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 node *ch = CHILD(n, i);
2393 if (TYPE(ch) != trailer)
2394 break;
2395 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002396 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002397 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002398 tmp->lineno = e->lineno;
2399 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002400 e = tmp;
2401 }
Yury Selivanov75445082015-05-11 22:57:16 -04002402
2403 if (start) {
2404 /* there was an AWAIT */
2405 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2406 }
2407 else {
2408 return e;
2409 }
2410}
2411
2412static expr_ty
2413ast_for_power(struct compiling *c, const node *n)
2414{
2415 /* power: atom trailer* ('**' factor)*
2416 */
2417 expr_ty e;
2418 REQ(n, power);
2419 e = ast_for_atom_expr(c, CHILD(n, 0));
2420 if (!e)
2421 return NULL;
2422 if (NCH(n) == 1)
2423 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002424 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2425 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002426 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002427 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002428 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002429 }
2430 return e;
2431}
2432
Guido van Rossum0368b722007-05-11 16:50:42 +00002433static expr_ty
2434ast_for_starred(struct compiling *c, const node *n)
2435{
2436 expr_ty tmp;
2437 REQ(n, star_expr);
2438
2439 tmp = ast_for_expr(c, CHILD(n, 1));
2440 if (!tmp)
2441 return NULL;
2442
2443 /* The Load context is changed later. */
2444 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2445}
2446
2447
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448/* Do not name a variable 'expr'! Will cause a compile error.
2449*/
2450
2451static expr_ty
2452ast_for_expr(struct compiling *c, const node *n)
2453{
2454 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002455 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002456 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 and_test: not_test ('and' not_test)*
2459 not_test: 'not' not_test | comparison
2460 comparison: expr (comp_op expr)*
2461 expr: xor_expr ('|' xor_expr)*
2462 xor_expr: and_expr ('^' and_expr)*
2463 and_expr: shift_expr ('&' shift_expr)*
2464 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2465 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002466 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002468 power: atom_expr ['**' factor]
2469 atom_expr: [AWAIT] atom trailer*
2470 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 */
2472
2473 asdl_seq *seq;
2474 int i;
2475
2476 loop:
2477 switch (TYPE(n)) {
2478 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002479 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002480 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002481 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002483 else if (NCH(n) > 1)
2484 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002485 /* Fallthrough */
2486 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 case and_test:
2488 if (NCH(n) == 1) {
2489 n = CHILD(n, 0);
2490 goto loop;
2491 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002492 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 if (!seq)
2494 return NULL;
2495 for (i = 0; i < NCH(n); i += 2) {
2496 expr_ty e = ast_for_expr(c, CHILD(n, i));
2497 if (!e)
2498 return NULL;
2499 asdl_seq_SET(seq, i / 2, e);
2500 }
2501 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002502 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2503 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002504 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002505 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 case not_test:
2507 if (NCH(n) == 1) {
2508 n = CHILD(n, 0);
2509 goto loop;
2510 }
2511 else {
2512 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2513 if (!expression)
2514 return NULL;
2515
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002516 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2517 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 }
2519 case comparison:
2520 if (NCH(n) == 1) {
2521 n = CHILD(n, 0);
2522 goto loop;
2523 }
2524 else {
2525 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002526 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002527 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002528 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 if (!ops)
2530 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002531 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 return NULL;
2534 }
2535 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002536 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002538 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002539 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002541 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542
2543 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002544 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002546 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002548 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 asdl_seq_SET(cmps, i / 2, expression);
2550 }
2551 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002552 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002556 return Compare(expression, ops, cmps, LINENO(n),
2557 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 }
2559 break;
2560
Guido van Rossum0368b722007-05-11 16:50:42 +00002561 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 /* The next five cases all handle BinOps. The main body of code
2564 is the same in each case, but the switch turned inside out to
2565 reuse the code for each type of operator.
2566 */
2567 case expr:
2568 case xor_expr:
2569 case and_expr:
2570 case shift_expr:
2571 case arith_expr:
2572 case term:
2573 if (NCH(n) == 1) {
2574 n = CHILD(n, 0);
2575 goto loop;
2576 }
2577 return ast_for_binop(c, n);
2578 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002579 node *an = NULL;
2580 node *en = NULL;
2581 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002582 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002583 if (NCH(n) > 1)
2584 an = CHILD(n, 1); /* yield_arg */
2585 if (an) {
2586 en = CHILD(an, NCH(an) - 1);
2587 if (NCH(an) == 2) {
2588 is_from = 1;
2589 exp = ast_for_expr(c, en);
2590 }
2591 else
2592 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002593 if (!exp)
2594 return NULL;
2595 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002596 if (is_from)
2597 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2598 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002599 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002600 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 if (NCH(n) == 1) {
2602 n = CHILD(n, 0);
2603 goto loop;
2604 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002605 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002606 case power:
2607 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002609 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 return NULL;
2611 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002612 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 return NULL;
2614}
2615
2616static expr_ty
2617ast_for_call(struct compiling *c, const node *n, expr_ty func)
2618{
2619 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002620 arglist: argument (',' argument)* [',']
2621 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 */
2623
2624 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002625 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002626 asdl_seq *args;
2627 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
2629 REQ(n, arglist);
2630
2631 nargs = 0;
2632 nkeywords = 0;
2633 ngens = 0;
2634 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002635 node *ch = CHILD(n, i);
2636 if (TYPE(ch) == argument) {
2637 if (NCH(ch) == 1)
2638 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002639 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002640 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002641 else if (TYPE(CHILD(ch, 0)) == STAR)
2642 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002645 nkeywords++;
2646 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 }
2648 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002649 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002650 "if not sole argument");
2651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
2653
2654 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002655 ast_error(c, n, "more than 255 arguments");
2656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 }
2658
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002659 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002661 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002662 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002664 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002665
2666 nargs = 0; /* positional arguments + iterable argument unpackings */
2667 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2668 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002670 node *ch = CHILD(n, i);
2671 if (TYPE(ch) == argument) {
2672 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002673 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002675 /* a positional argument */
2676 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002677 if (ndoublestars) {
2678 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002679 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002680 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002681 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002682 else {
2683 ast_error(c, chch,
2684 "positional argument follows "
2685 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002686 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002687 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002688 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002689 e = ast_for_expr(c, chch);
2690 if (!e)
2691 return NULL;
2692 asdl_seq_SET(args, nargs++, e);
2693 }
2694 else if (TYPE(chch) == STAR) {
2695 /* an iterable argument unpacking */
2696 expr_ty starred;
2697 if (ndoublestars) {
2698 ast_error(c, chch,
2699 "iterable argument unpacking follows "
2700 "keyword argument unpacking");
2701 return NULL;
2702 }
2703 e = ast_for_expr(c, CHILD(ch, 1));
2704 if (!e)
2705 return NULL;
2706 starred = Starred(e, Load, LINENO(chch),
2707 chch->n_col_offset,
2708 c->c_arena);
2709 if (!starred)
2710 return NULL;
2711 asdl_seq_SET(args, nargs++, starred);
2712
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 }
2714 else if (TYPE(chch) == DOUBLESTAR) {
2715 /* a keyword argument unpacking */
2716 keyword_ty kw;
2717 i++;
2718 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002720 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002721 kw = keyword(NULL, e, c->c_arena);
2722 asdl_seq_SET(keywords, nkeywords++, kw);
2723 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002725 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002729 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002730 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002732 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002733 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002734 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002735 identifier key, tmp;
2736 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002738 /* chch is test, but must be an identifier? */
2739 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 /* f(lambda x: x[0] = 3) ends up getting parsed with
2743 * LHS test = lambda x: x[0], and RHS test = 3.
2744 * SF bug 132313 points out that complaining about a keyword
2745 * then is very confusing.
2746 */
2747 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002748 ast_error(c, chch,
2749 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002750 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002751 }
2752 else if (e->kind != Name_kind) {
2753 ast_error(c, chch,
2754 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002755 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002756 }
2757 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002758 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002760 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002761 for (k = 0; k < nkeywords; k++) {
2762 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 if (tmp && !PyUnicode_Compare(tmp, key)) {
2764 ast_error(c, chch,
2765 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002766 return NULL;
2767 }
2768 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002769 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002771 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002772 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002774 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002775 asdl_seq_SET(keywords, nkeywords++, kw);
2776 }
2777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 }
2779
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002780 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781}
2782
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002784ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002786 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002787 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002790 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002791 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002792 }
2793 else {
2794 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002795 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002798 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 else {
2800 asdl_seq *tmp = seq_for_testlist(c, n);
2801 if (!tmp)
2802 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002803 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002805}
2806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807static stmt_ty
2808ast_for_expr_stmt(struct compiling *c, const node *n)
2809{
2810 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002813 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002814 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 test: ... here starts the operator precendence dance
2817 */
2818
2819 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002820 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 if (!e)
2822 return NULL;
2823
Thomas Wouters89f507f2006-12-13 04:49:30 +00002824 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 }
2826 else if (TYPE(CHILD(n, 1)) == augassign) {
2827 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002828 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002829 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830
Thomas Wouters89f507f2006-12-13 04:49:30 +00002831 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 if (!expr1)
2833 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002834 if(!set_context(c, expr1, Store, ch))
2835 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002836 /* set_context checks that most expressions are not the left side.
2837 Augmented assignments can only have a name, a subscript, or an
2838 attribute on the left, though, so we have to explicitly check for
2839 those. */
2840 switch (expr1->kind) {
2841 case Name_kind:
2842 case Attribute_kind:
2843 case Subscript_kind:
2844 break;
2845 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002846 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002847 return NULL;
2848 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849
Thomas Wouters89f507f2006-12-13 04:49:30 +00002850 ch = CHILD(n, 2);
2851 if (TYPE(ch) == testlist)
2852 expr2 = ast_for_testlist(c, ch);
2853 else
2854 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002855 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 return NULL;
2857
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002858 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002859 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 return NULL;
2861
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 }
2864 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002865 int i;
2866 asdl_seq *targets;
2867 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 expr_ty expression;
2869
Thomas Wouters89f507f2006-12-13 04:49:30 +00002870 /* a normal assignment */
2871 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002872 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 if (!targets)
2874 return NULL;
2875 for (i = 0; i < NCH(n) - 2; i += 2) {
2876 expr_ty e;
2877 node *ch = CHILD(n, i);
2878 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002879 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002880 return NULL;
2881 }
2882 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002886 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002887 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889
Thomas Wouters89f507f2006-12-13 04:49:30 +00002890 asdl_seq_SET(targets, i / 2, e);
2891 }
2892 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002893 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002894 expression = ast_for_testlist(c, value);
2895 else
2896 expression = ast_for_expr(c, value);
2897 if (!expression)
2898 return NULL;
2899 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901}
2902
Benjamin Peterson78565b22009-06-28 19:19:51 +00002903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002905ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906{
2907 asdl_seq *seq;
2908 int i;
2909 expr_ty e;
2910
2911 REQ(n, exprlist);
2912
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002913 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 e = ast_for_expr(c, CHILD(n, i));
2918 if (!e)
2919 return NULL;
2920 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002921 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002922 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 }
2924 return seq;
2925}
2926
2927static stmt_ty
2928ast_for_del_stmt(struct compiling *c, const node *n)
2929{
2930 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932 /* del_stmt: 'del' exprlist */
2933 REQ(n, del_stmt);
2934
2935 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2936 if (!expr_list)
2937 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002938 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939}
2940
2941static stmt_ty
2942ast_for_flow_stmt(struct compiling *c, const node *n)
2943{
2944 /*
2945 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2946 | yield_stmt
2947 break_stmt: 'break'
2948 continue_stmt: 'continue'
2949 return_stmt: 'return' [testlist]
2950 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002951 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 raise_stmt: 'raise' [test [',' test [',' test]]]
2953 */
2954 node *ch;
2955
2956 REQ(n, flow_stmt);
2957 ch = CHILD(n, 0);
2958 switch (TYPE(ch)) {
2959 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002960 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002962 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002964 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2965 if (!exp)
2966 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002967 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 }
2969 case return_stmt:
2970 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002971 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002973 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 if (!expression)
2975 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002976 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 }
2978 case raise_stmt:
2979 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002980 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2981 else if (NCH(ch) >= 2) {
2982 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2984 if (!expression)
2985 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002986 if (NCH(ch) == 4) {
2987 cause = ast_for_expr(c, CHILD(ch, 3));
2988 if (!cause)
2989 return NULL;
2990 }
2991 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 }
2993 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002994 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 "unexpected flow_stmt: %d", TYPE(ch));
2996 return NULL;
2997 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002998
2999 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
3000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001}
3002
3003static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003004alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005{
3006 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003007 import_as_name: NAME ['as' NAME]
3008 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 dotted_name: NAME ('.' NAME)*
3010 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003011 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 loop:
3014 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003015 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003016 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003017 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003018 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003019 if (!name)
3020 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003021 if (NCH(n) == 3) {
3022 node *str_node = CHILD(n, 2);
3023 str = NEW_IDENTIFIER(str_node);
3024 if (!str)
3025 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003026 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003027 return NULL;
3028 }
3029 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003030 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003031 return NULL;
3032 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003033 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003034 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 case dotted_as_name:
3036 if (NCH(n) == 1) {
3037 n = CHILD(n, 0);
3038 goto loop;
3039 }
3040 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003041 node *asname_node = CHILD(n, 2);
3042 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003043 if (!a)
3044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003046 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003047 if (!a->asname)
3048 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003049 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003050 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 return a;
3052 }
3053 break;
3054 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003055 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003056 node *name_node = CHILD(n, 0);
3057 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003058 if (!name)
3059 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003060 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003061 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003062 return alias(name, NULL, c->c_arena);
3063 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 else {
3065 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003066 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003067 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070
3071 len = 0;
3072 for (i = 0; i < NCH(n); i += 2)
3073 /* length of string plus one for the dot */
3074 len += strlen(STR(CHILD(n, i))) + 1;
3075 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003076 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 if (!str)
3078 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003079 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 if (!s)
3081 return NULL;
3082 for (i = 0; i < NCH(n); i += 2) {
3083 char *sch = STR(CHILD(n, i));
3084 strcpy(s, STR(CHILD(n, i)));
3085 s += strlen(sch);
3086 *s++ = '.';
3087 }
3088 --s;
3089 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3091 PyBytes_GET_SIZE(str),
3092 NULL);
3093 Py_DECREF(str);
3094 if (!uni)
3095 return NULL;
3096 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003097 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003098 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3099 Py_DECREF(str);
3100 return NULL;
3101 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003102 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 }
3104 break;
3105 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003106 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003107 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3108 Py_DECREF(str);
3109 return NULL;
3110 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003111 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003113 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114 "unexpected import name: %d", TYPE(n));
3115 return NULL;
3116 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003117
3118 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 return NULL;
3120}
3121
3122static stmt_ty
3123ast_for_import_stmt(struct compiling *c, const node *n)
3124{
3125 /*
3126 import_stmt: import_name | import_from
3127 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003128 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3129 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003131 int lineno;
3132 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 int i;
3134 asdl_seq *aliases;
3135
3136 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003137 lineno = LINENO(n);
3138 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003140 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003142 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003143 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003144 if (!aliases)
3145 return NULL;
3146 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003147 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003148 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003150 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003152 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003154 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003156 int idx, ndots = 0;
3157 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003158 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003160 /* Count the number of dots (for relative imports) and check for the
3161 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003162 for (idx = 1; idx < NCH(n); idx++) {
3163 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003164 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3165 if (!mod)
3166 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003167 idx++;
3168 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003169 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003171 ndots += 3;
3172 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003173 } else if (TYPE(CHILD(n, idx)) != DOT) {
3174 break;
3175 }
3176 ndots++;
3177 }
3178 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003179 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003180 case STAR:
3181 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003182 n = CHILD(n, idx);
3183 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003184 break;
3185 case LPAR:
3186 /* from ... import (x, y, z) */
3187 n = CHILD(n, idx + 1);
3188 n_children = NCH(n);
3189 break;
3190 case import_as_names:
3191 /* from ... import x, y, z */
3192 n = CHILD(n, idx);
3193 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003194 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003195 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 " surrounding parentheses");
3197 return NULL;
3198 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199 break;
3200 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003201 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003202 return NULL;
3203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003205 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003206 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208
3209 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003210 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003211 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003212 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003214 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003216 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003217 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003218 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003219 if (!import_alias)
3220 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003221 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003222 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003224 if (mod != NULL)
3225 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003226 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003227 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 }
Neal Norwitz79792652005-11-14 04:25:03 +00003229 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 "unknown import statement: starts with command '%s'",
3231 STR(CHILD(n, 0)));
3232 return NULL;
3233}
3234
3235static stmt_ty
3236ast_for_global_stmt(struct compiling *c, const node *n)
3237{
3238 /* global_stmt: 'global' NAME (',' NAME)* */
3239 identifier name;
3240 asdl_seq *s;
3241 int i;
3242
3243 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003244 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003246 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003248 name = NEW_IDENTIFIER(CHILD(n, i));
3249 if (!name)
3250 return NULL;
3251 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003253 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254}
3255
3256static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003257ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3258{
3259 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3260 identifier name;
3261 asdl_seq *s;
3262 int i;
3263
3264 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003265 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003266 if (!s)
3267 return NULL;
3268 for (i = 1; i < NCH(n); i += 2) {
3269 name = NEW_IDENTIFIER(CHILD(n, i));
3270 if (!name)
3271 return NULL;
3272 asdl_seq_SET(s, i / 2, name);
3273 }
3274 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3275}
3276
3277static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278ast_for_assert_stmt(struct compiling *c, const node *n)
3279{
3280 /* assert_stmt: 'assert' test [',' test] */
3281 REQ(n, assert_stmt);
3282 if (NCH(n) == 2) {
3283 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3284 if (!expression)
3285 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003286 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 }
3288 else if (NCH(n) == 4) {
3289 expr_ty expr1, expr2;
3290
3291 expr1 = ast_for_expr(c, CHILD(n, 1));
3292 if (!expr1)
3293 return NULL;
3294 expr2 = ast_for_expr(c, CHILD(n, 3));
3295 if (!expr2)
3296 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 }
Neal Norwitz79792652005-11-14 04:25:03 +00003300 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 "improper number of parts to 'assert' statement: %d",
3302 NCH(n));
3303 return NULL;
3304}
3305
3306static asdl_seq *
3307ast_for_suite(struct compiling *c, const node *n)
3308{
3309 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003310 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 stmt_ty s;
3312 int i, total, num, end, pos = 0;
3313 node *ch;
3314
3315 REQ(n, suite);
3316
3317 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003318 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 n = CHILD(n, 0);
3323 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 */
3326 end = NCH(n) - 1;
3327 if (TYPE(CHILD(n, end - 1)) == SEMI)
3328 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003330 for (i = 0; i < end; i += 2) {
3331 ch = CHILD(n, i);
3332 s = ast_for_stmt(c, ch);
3333 if (!s)
3334 return NULL;
3335 asdl_seq_SET(seq, pos++, s);
3336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337 }
3338 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003339 for (i = 2; i < (NCH(n) - 1); i++) {
3340 ch = CHILD(n, i);
3341 REQ(ch, stmt);
3342 num = num_stmts(ch);
3343 if (num == 1) {
3344 /* small_stmt or compound_stmt with only one child */
3345 s = ast_for_stmt(c, ch);
3346 if (!s)
3347 return NULL;
3348 asdl_seq_SET(seq, pos++, s);
3349 }
3350 else {
3351 int j;
3352 ch = CHILD(ch, 0);
3353 REQ(ch, simple_stmt);
3354 for (j = 0; j < NCH(ch); j += 2) {
3355 /* statement terminates with a semi-colon ';' */
3356 if (NCH(CHILD(ch, j)) == 0) {
3357 assert((j + 1) == NCH(ch));
3358 break;
3359 }
3360 s = ast_for_stmt(c, CHILD(ch, j));
3361 if (!s)
3362 return NULL;
3363 asdl_seq_SET(seq, pos++, s);
3364 }
3365 }
3366 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 }
3368 assert(pos == seq->size);
3369 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370}
3371
3372static stmt_ty
3373ast_for_if_stmt(struct compiling *c, const node *n)
3374{
3375 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3376 ['else' ':' suite]
3377 */
3378 char *s;
3379
3380 REQ(n, if_stmt);
3381
3382 if (NCH(n) == 4) {
3383 expr_ty expression;
3384 asdl_seq *suite_seq;
3385
3386 expression = ast_for_expr(c, CHILD(n, 1));
3387 if (!expression)
3388 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003390 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392
Guido van Rossumd8faa362007-04-27 19:54:29 +00003393 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3394 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003396
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 s = STR(CHILD(n, 4));
3398 /* s[2], the third character in the string, will be
3399 's' for el_s_e, or
3400 'i' for el_i_f
3401 */
3402 if (s[2] == 's') {
3403 expr_ty expression;
3404 asdl_seq *seq1, *seq2;
3405
3406 expression = ast_for_expr(c, CHILD(n, 1));
3407 if (!expression)
3408 return NULL;
3409 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003410 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 return NULL;
3412 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003413 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 return NULL;
3415
Guido van Rossumd8faa362007-04-27 19:54:29 +00003416 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3417 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003418 }
3419 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003420 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003421 expr_ty expression;
3422 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003423 asdl_seq *orelse = NULL;
3424 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425 /* must reference the child n_elif+1 since 'else' token is third,
3426 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003427 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3428 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3429 has_else = 1;
3430 n_elif -= 3;
3431 }
3432 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433
Thomas Wouters89f507f2006-12-13 04:49:30 +00003434 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003435 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003437 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003438 if (!orelse)
3439 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003441 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003443 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3444 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003446 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3447 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 asdl_seq_SET(orelse, 0,
3451 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003452 LINENO(CHILD(n, NCH(n) - 6)),
3453 CHILD(n, NCH(n) - 6)->n_col_offset,
3454 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003455 /* the just-created orelse handled the last elif */
3456 n_elif--;
3457 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458
Thomas Wouters89f507f2006-12-13 04:49:30 +00003459 for (i = 0; i < n_elif; i++) {
3460 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003461 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003462 if (!newobj)
3463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003465 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003468 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470
Thomas Wouters89f507f2006-12-13 04:49:30 +00003471 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003473 LINENO(CHILD(n, off)),
3474 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003475 orelse = newobj;
3476 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003477 expression = ast_for_expr(c, CHILD(n, 1));
3478 if (!expression)
3479 return NULL;
3480 suite_seq = ast_for_suite(c, CHILD(n, 3));
3481 if (!suite_seq)
3482 return NULL;
3483 return If(expression, suite_seq, orelse,
3484 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003486
3487 PyErr_Format(PyExc_SystemError,
3488 "unexpected token in 'if' statement: %s", s);
3489 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490}
3491
3492static stmt_ty
3493ast_for_while_stmt(struct compiling *c, const node *n)
3494{
3495 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3496 REQ(n, while_stmt);
3497
3498 if (NCH(n) == 4) {
3499 expr_ty expression;
3500 asdl_seq *suite_seq;
3501
3502 expression = ast_for_expr(c, CHILD(n, 1));
3503 if (!expression)
3504 return NULL;
3505 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003506 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003508 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 }
3510 else if (NCH(n) == 7) {
3511 expr_ty expression;
3512 asdl_seq *seq1, *seq2;
3513
3514 expression = ast_for_expr(c, CHILD(n, 1));
3515 if (!expression)
3516 return NULL;
3517 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003518 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 return NULL;
3520 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003521 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 return NULL;
3523
Thomas Wouters89f507f2006-12-13 04:49:30 +00003524 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003526
3527 PyErr_Format(PyExc_SystemError,
3528 "wrong number of tokens for 'while' statement: %d",
3529 NCH(n));
3530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531}
3532
3533static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003534ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003536 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003538 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003539 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3541 REQ(n, for_stmt);
3542
3543 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003544 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 if (!seq)
3546 return NULL;
3547 }
3548
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003549 node_target = CHILD(n, 1);
3550 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003551 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003553 /* Check the # of children rather than the length of _target, since
3554 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003555 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003556 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003557 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003559 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003561 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003562 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 return NULL;
3564 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003565 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 return NULL;
3567
Yury Selivanov75445082015-05-11 22:57:16 -04003568 if (is_async)
3569 return AsyncFor(target, expression, suite_seq, seq,
3570 LINENO(n), n->n_col_offset,
3571 c->c_arena);
3572 else
3573 return For(target, expression, suite_seq, seq,
3574 LINENO(n), n->n_col_offset,
3575 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576}
3577
3578static excepthandler_ty
3579ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3580{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003581 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 REQ(exc, except_clause);
3583 REQ(body, suite);
3584
3585 if (NCH(exc) == 1) {
3586 asdl_seq *suite_seq = ast_for_suite(c, body);
3587 if (!suite_seq)
3588 return NULL;
3589
Neal Norwitzad74aa82008-03-31 05:14:30 +00003590 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003591 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 }
3593 else if (NCH(exc) == 2) {
3594 expr_ty expression;
3595 asdl_seq *suite_seq;
3596
3597 expression = ast_for_expr(c, CHILD(exc, 1));
3598 if (!expression)
3599 return NULL;
3600 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003601 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 return NULL;
3603
Neal Norwitzad74aa82008-03-31 05:14:30 +00003604 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003605 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 }
3607 else if (NCH(exc) == 4) {
3608 asdl_seq *suite_seq;
3609 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003610 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003611 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003613 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003616 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 return NULL;
3618 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003619 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return NULL;
3621
Neal Norwitzad74aa82008-03-31 05:14:30 +00003622 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003623 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003625
3626 PyErr_Format(PyExc_SystemError,
3627 "wrong number of children for 'except' clause: %d",
3628 NCH(exc));
3629 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630}
3631
3632static stmt_ty
3633ast_for_try_stmt(struct compiling *c, const node *n)
3634{
Neal Norwitzf599f422005-12-17 21:33:47 +00003635 const int nch = NCH(n);
3636 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003637 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639 REQ(n, try_stmt);
3640
Neal Norwitzf599f422005-12-17 21:33:47 +00003641 body = ast_for_suite(c, CHILD(n, 2));
3642 if (body == NULL)
3643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644
Neal Norwitzf599f422005-12-17 21:33:47 +00003645 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3646 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3647 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3648 /* we can assume it's an "else",
3649 because nch >= 9 for try-else-finally and
3650 it would otherwise have a type of except_clause */
3651 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3652 if (orelse == NULL)
3653 return NULL;
3654 n_except--;
3655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656
Neal Norwitzf599f422005-12-17 21:33:47 +00003657 finally = ast_for_suite(c, CHILD(n, nch - 1));
3658 if (finally == NULL)
3659 return NULL;
3660 n_except--;
3661 }
3662 else {
3663 /* we can assume it's an "else",
3664 otherwise it would have a type of except_clause */
3665 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3666 if (orelse == NULL)
3667 return NULL;
3668 n_except--;
3669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003671 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003672 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 return NULL;
3674 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675
Neal Norwitzf599f422005-12-17 21:33:47 +00003676 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003677 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003678 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003679 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003680 if (handlers == NULL)
3681 return NULL;
3682
3683 for (i = 0; i < n_except; i++) {
3684 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3685 CHILD(n, 5 + i * 3));
3686 if (!e)
3687 return NULL;
3688 asdl_seq_SET(handlers, i, e);
3689 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003690 }
3691
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003692 assert(finally != NULL || asdl_seq_LEN(handlers));
3693 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694}
3695
Georg Brandl0c315622009-05-25 21:10:36 +00003696/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003697static withitem_ty
3698ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003699{
3700 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003701
Georg Brandl0c315622009-05-25 21:10:36 +00003702 REQ(n, with_item);
3703 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003704 if (!context_expr)
3705 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003706 if (NCH(n) == 3) {
3707 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003708
3709 if (!optional_vars) {
3710 return NULL;
3711 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003712 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003713 return NULL;
3714 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003715 }
3716
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003717 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003718}
3719
Georg Brandl0c315622009-05-25 21:10:36 +00003720/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3721static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003722ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003723{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003724 int i, n_items;
3725 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003726
3727 REQ(n, with_stmt);
3728
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003729 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003730 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003731 if (!items)
3732 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003733 for (i = 1; i < NCH(n) - 2; i += 2) {
3734 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3735 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003736 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003737 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003738 }
3739
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003740 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3741 if (!body)
3742 return NULL;
3743
Yury Selivanov75445082015-05-11 22:57:16 -04003744 if (is_async)
3745 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3746 else
3747 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003748}
3749
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003751ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003753 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003754 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003755 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003756 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003757
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 REQ(n, classdef);
3759
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003760 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761 s = ast_for_suite(c, CHILD(n, 3));
3762 if (!s)
3763 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003764 classname = NEW_IDENTIFIER(CHILD(n, 1));
3765 if (!classname)
3766 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003767 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003768 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003769 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3770 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003772
3773 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003774 s = ast_for_suite(c, CHILD(n,5));
3775 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003776 return NULL;
3777 classname = NEW_IDENTIFIER(CHILD(n, 1));
3778 if (!classname)
3779 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003780 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003781 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003782 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3783 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 }
3785
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003786 /* class NAME '(' arglist ')' ':' suite */
3787 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003788 {
3789 PyObject *dummy_name;
3790 expr_ty dummy;
3791 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3792 if (!dummy_name)
3793 return NULL;
3794 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3795 call = ast_for_call(c, CHILD(n, 3), dummy);
3796 if (!call)
3797 return NULL;
3798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003800 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003802 classname = NEW_IDENTIFIER(CHILD(n, 1));
3803 if (!classname)
3804 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003805 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003806 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003807
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003808 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003809 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810}
3811
3812static stmt_ty
3813ast_for_stmt(struct compiling *c, const node *n)
3814{
3815 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003816 assert(NCH(n) == 1);
3817 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 }
3819 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003820 assert(num_stmts(n) == 1);
3821 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 }
3823 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003824 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003825 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3826 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003827 */
3828 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 case expr_stmt:
3830 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 case del_stmt:
3832 return ast_for_del_stmt(c, n);
3833 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003834 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 case flow_stmt:
3836 return ast_for_flow_stmt(c, n);
3837 case import_stmt:
3838 return ast_for_import_stmt(c, n);
3839 case global_stmt:
3840 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003841 case nonlocal_stmt:
3842 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 case assert_stmt:
3844 return ast_for_assert_stmt(c, n);
3845 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003846 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3848 TYPE(n), NCH(n));
3849 return NULL;
3850 }
3851 }
3852 else {
3853 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003854 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003855 */
3856 node *ch = CHILD(n, 0);
3857 REQ(n, compound_stmt);
3858 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 case if_stmt:
3860 return ast_for_if_stmt(c, ch);
3861 case while_stmt:
3862 return ast_for_while_stmt(c, ch);
3863 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003864 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 case try_stmt:
3866 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003867 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003868 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003869 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003870 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003872 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 case decorated:
3874 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003875 case async_stmt:
3876 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003878 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3880 TYPE(n), NCH(n));
3881 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003882 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883 }
3884}
3885
3886static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003887parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003889 const char *end;
3890 long x;
3891 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003892 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003893 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003895 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003896 errno = 0;
3897 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003898 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003899 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003900 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003901 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003902 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003903 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003904 }
3905 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003906 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003907 if (*end == '\0') {
3908 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003909 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003910 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003911 }
3912 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003913 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003914 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003915 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3916 if (compl.imag == -1.0 && PyErr_Occurred())
3917 return NULL;
3918 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003919 }
3920 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003921 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003922 dx = PyOS_string_to_double(s, NULL, NULL);
3923 if (dx == -1.0 && PyErr_Occurred())
3924 return NULL;
3925 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003926 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927}
3928
3929static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003930decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003932 const char *s, *t;
3933 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003934 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3935 while (s < end && (*s & 0x80)) s++;
3936 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003937 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938}
3939
3940static PyObject *
Eric V. Smith5567f892015-09-21 13:36:09 -04003941decode_unicode(struct compiling *c, const char *s, size_t len, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003943 PyObject *v, *u;
3944 char *buf;
3945 char *p;
3946 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003947
Guido van Rossumd8faa362007-04-27 19:54:29 +00003948 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003949 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003950 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003951 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003952 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003953 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003954 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3955 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3956 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003957 if (u == NULL)
3958 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003959 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003960 end = s + len;
3961 while (s < end) {
3962 if (*s == '\\') {
3963 *p++ = *s++;
3964 if (*s & 0x80) {
3965 strcpy(p, "u005c");
3966 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003967 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003968 }
3969 if (*s & 0x80) { /* XXX inefficient */
3970 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003971 int kind;
3972 void *data;
3973 Py_ssize_t len, i;
3974 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003975 if (w == NULL) {
3976 Py_DECREF(u);
3977 return NULL;
3978 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003979 kind = PyUnicode_KIND(w);
3980 data = PyUnicode_DATA(w);
3981 len = PyUnicode_GET_LENGTH(w);
3982 for (i = 0; i < len; i++) {
3983 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3984 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003985 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003986 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003987 /* Should be impossible to overflow */
3988 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003989 Py_DECREF(w);
3990 } else {
3991 *p++ = *s++;
3992 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003993 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003994 len = p - buf;
3995 s = buf;
3996 }
Eric V. Smith5567f892015-09-21 13:36:09 -04003997 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003998 Py_XDECREF(u);
3999 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000}
4001
Eric V. Smith235a6f02015-09-19 14:51:32 -04004002/* Compile this expression in to an expr_ty. We know that we can
4003 temporarily modify the character before the start of this string
4004 (it's '{'), and we know we can temporarily modify the character
4005 after this string (it is a '}'). Leverage this to create a
4006 sub-string with enough room for us to add parens around the
4007 expression. This is to allow strings with embedded newlines, for
4008 example. */
4009static expr_ty
Eric V. Smith1d44c412015-09-23 07:49:00 -04004010fstring_compile_expr(PyObject *str, Py_ssize_t expr_start,
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004011 Py_ssize_t expr_end, struct compiling *c, const node *n)
4012
Eric V. Smith235a6f02015-09-19 14:51:32 -04004013{
4014 PyCompilerFlags cf;
4015 mod_ty mod;
4016 char *utf_expr;
4017 Py_ssize_t i;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004018 Py_UCS4 end_ch = -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004019 int all_whitespace;
4020 PyObject *sub = NULL;
4021
4022 /* We only decref sub if we allocated it with a PyUnicode_Substring.
4023 decref_sub records that. */
4024 int decref_sub = 0;
4025
4026 assert(str);
4027
Eric V. Smith1d44c412015-09-23 07:49:00 -04004028 assert(expr_start >= 0 && expr_start < PyUnicode_GET_LENGTH(str));
4029 assert(expr_end >= 0 && expr_end < PyUnicode_GET_LENGTH(str));
4030 assert(expr_end >= expr_start);
4031
4032 /* There has to be at least on character on each side of the
4033 expression inside this str. This will have been caught before
4034 we're called. */
4035 assert(expr_start >= 1);
4036 assert(expr_end <= PyUnicode_GET_LENGTH(str)-1);
4037
Eric V. Smith235a6f02015-09-19 14:51:32 -04004038 /* If the substring is all whitespace, it's an error. We need to
4039 catch this here, and not when we call PyParser_ASTFromString,
4040 because turning the expression '' in to '()' would go from
4041 being invalid to valid. */
4042 /* Note that this code says an empty string is all
4043 whitespace. That's important. There's a test for it: f'{}'. */
4044 all_whitespace = 1;
4045 for (i = expr_start; i < expr_end; i++) {
4046 if (!Py_UNICODE_ISSPACE(PyUnicode_READ_CHAR(str, i))) {
4047 all_whitespace = 0;
4048 break;
4049 }
4050 }
4051 if (all_whitespace) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004052 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004053 goto error;
4054 }
4055
4056 /* If the substring will be the entire source string, we can't use
4057 PyUnicode_Substring, since it will return another reference to
4058 our original string. Because we're modifying the string in
4059 place, that's a no-no. So, detect that case and just use our
4060 string directly. */
4061
4062 if (expr_start-1 == 0 && expr_end+1 == PyUnicode_GET_LENGTH(str)) {
Eric V. Smith1d44c412015-09-23 07:49:00 -04004063 /* If str is well formed, then the first and last chars must
4064 be '{' and '}', respectively. But, if there's a syntax
4065 error, for example f'{3!', then the last char won't be a
4066 closing brace. So, remember the last character we read in
4067 order for us to restore it. */
4068 end_ch = PyUnicode_ReadChar(str, expr_end-expr_start+1);
4069 assert(end_ch != (Py_UCS4)-1);
4070
4071 /* In all cases, however, start_ch must be '{'. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004072 assert(PyUnicode_ReadChar(str, 0) == '{');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004073
Eric V. Smith235a6f02015-09-19 14:51:32 -04004074 sub = str;
4075 } else {
4076 /* Create a substring object. It must be a new object, with
4077 refcount==1, so that we can modify it. */
4078 sub = PyUnicode_Substring(str, expr_start-1, expr_end+1);
4079 if (!sub)
4080 goto error;
4081 assert(sub != str); /* Make sure it's a new string. */
4082 decref_sub = 1; /* Remember to deallocate it on error. */
4083 }
4084
Eric V. Smith1d44c412015-09-23 07:49:00 -04004085 /* Put () around the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004086 if (PyUnicode_WriteChar(sub, 0, '(') < 0 ||
4087 PyUnicode_WriteChar(sub, expr_end-expr_start+1, ')') < 0)
4088 goto error;
4089
Eric V. Smith235a6f02015-09-19 14:51:32 -04004090 /* No need to free the memory returned here: it's managed by the
4091 string. */
4092 utf_expr = PyUnicode_AsUTF8(sub);
4093 if (!utf_expr)
4094 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004095
4096 cf.cf_flags = PyCF_ONLY_AST;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004097 mod = PyParser_ASTFromString(utf_expr, "<fstring>",
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004098 Py_eval_input, &cf, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004099 if (!mod)
4100 goto error;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004101
Eric V. Smith235a6f02015-09-19 14:51:32 -04004102 if (sub != str)
4103 /* Clear instead of decref in case we ever modify this code to change
4104 the error handling: this is safest because the XDECREF won't try
4105 and decref it when it's NULL. */
4106 /* No need to restore the chars in sub, since we know it's getting
4107 ready to get deleted (refcount must be 1, since we got a new string
4108 in PyUnicode_Substring). */
4109 Py_CLEAR(sub);
4110 else {
4111 assert(!decref_sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004112 assert(end_ch != (Py_UCS4)-1);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004113 /* Restore str, which we earlier modified directly. */
4114 if (PyUnicode_WriteChar(str, 0, '{') < 0 ||
Eric V. Smith1d44c412015-09-23 07:49:00 -04004115 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004116 goto error;
4117 }
4118 return mod->v.Expression.body;
4119
4120error:
4121 /* Only decref sub if it was the result of a call to SubString. */
4122 if (decref_sub)
4123 Py_XDECREF(sub);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004124
4125 if (end_ch != (Py_UCS4)-1) {
4126 /* We only get here if we modified str. Make sure that's the
4127 case: str will be equal to sub. */
4128 if (str == sub) {
4129 /* Don't check the error, because we've already set the
4130 error state (that's why we're in 'error', after
4131 all). */
4132 PyUnicode_WriteChar(str, 0, '{');
4133 PyUnicode_WriteChar(str, expr_end-expr_start+1, end_ch);
4134 }
4135 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004136 return NULL;
4137}
4138
4139/* Return -1 on error.
4140
4141 Return 0 if we reached the end of the literal.
4142
4143 Return 1 if we haven't reached the end of the literal, but we want
4144 the caller to process the literal up to this point. Used for
4145 doubled braces.
4146*/
4147static int
4148fstring_find_literal(PyObject *str, Py_ssize_t *ofs, PyObject **literal,
4149 int recurse_lvl, struct compiling *c, const node *n)
4150{
4151 /* Get any literal string. It ends when we hit an un-doubled brace, or the
4152 end of the string. */
4153
4154 Py_ssize_t literal_start, literal_end;
4155 int result = 0;
4156
4157 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4158 void *data = PyUnicode_DATA(str);
4159
4160 assert(*literal == NULL);
4161
4162 literal_start = *ofs;
4163 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4164 Py_UCS4 ch = PyUnicode_READ(kind, data, *ofs);
4165 if (ch == '{' || ch == '}') {
4166 /* Check for doubled braces, but only at the top level. If
4167 we checked at every level, then f'{0:{3}}' would fail
4168 with the two closing braces. */
4169 if (recurse_lvl == 0) {
4170 if (*ofs + 1 < PyUnicode_GET_LENGTH(str) &&
4171 PyUnicode_READ(kind, data, *ofs + 1) == ch) {
4172 /* We're going to tell the caller that the literal ends
4173 here, but that they should continue scanning. But also
4174 skip over the second brace when we resume scanning. */
4175 literal_end = *ofs + 1;
4176 *ofs += 2;
4177 result = 1;
4178 goto done;
4179 }
4180
4181 /* Where a single '{' is the start of a new expression, a
4182 single '}' is not allowed. */
4183 if (ch == '}') {
4184 ast_error(c, n, "f-string: single '}' is not allowed");
4185 return -1;
4186 }
4187 }
4188
4189 /* We're either at a '{', which means we're starting another
4190 expression; or a '}', which means we're at the end of this
4191 f-string (for a nested format_spec). */
4192 break;
4193 }
4194 }
4195 literal_end = *ofs;
4196
4197 assert(*ofs == PyUnicode_GET_LENGTH(str) ||
4198 PyUnicode_READ(kind, data, *ofs) == '{' ||
4199 PyUnicode_READ(kind, data, *ofs) == '}');
4200done:
4201 if (literal_start != literal_end) {
4202 *literal = PyUnicode_Substring(str, literal_start, literal_end);
4203 if (!*literal)
4204 return -1;
4205 }
4206
4207 return result;
4208}
4209
4210/* Forward declaration because parsing is recursive. */
4211static expr_ty
4212fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4213 struct compiling *c, const node *n);
4214
4215/* Parse the f-string str, starting at ofs. We know *ofs starts an
4216 expression (so it must be a '{'). Returns the FormattedValue node,
4217 which includes the expression, conversion character, and
4218 format_spec expression.
4219
4220 Note that I don't do a perfect job here: I don't make sure that a
4221 closing brace doesn't match an opening paren, for example. It
4222 doesn't need to error on all invalid expressions, just correctly
4223 find the end of all valid ones. Any errors inside the expression
4224 will be caught when we parse it later. */
4225static int
4226fstring_find_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4227 expr_ty *expression, struct compiling *c, const node *n)
4228{
4229 /* Return -1 on error, else 0. */
4230
4231 Py_ssize_t expr_start;
4232 Py_ssize_t expr_end;
4233 expr_ty simple_expression;
4234 expr_ty format_spec = NULL; /* Optional format specifier. */
4235 Py_UCS4 conversion = -1; /* The conversion char. -1 if not specified. */
4236
4237 enum PyUnicode_Kind kind = PyUnicode_KIND(str);
4238 void *data = PyUnicode_DATA(str);
4239
4240 /* 0 if we're not in a string, else the quote char we're trying to
4241 match (single or double quote). */
4242 Py_UCS4 quote_char = 0;
4243
4244 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4245 int string_type = 0;
4246
4247 /* Keep track of nesting level for braces/parens/brackets in
4248 expressions. */
4249 Py_ssize_t nested_depth = 0;
4250
4251 /* Can only nest one level deep. */
4252 if (recurse_lvl >= 2) {
4253 ast_error(c, n, "f-string: expressions nested too deeply");
4254 return -1;
4255 }
4256
4257 /* The first char must be a left brace, or we wouldn't have gotten
4258 here. Skip over it. */
4259 assert(PyUnicode_READ(kind, data, *ofs) == '{');
4260 *ofs += 1;
4261
4262 expr_start = *ofs;
4263 for (; *ofs < PyUnicode_GET_LENGTH(str); *ofs += 1) {
4264 Py_UCS4 ch;
4265
4266 /* Loop invariants. */
4267 assert(nested_depth >= 0);
4268 assert(*ofs >= expr_start);
4269 if (quote_char)
4270 assert(string_type == 1 || string_type == 3);
4271 else
4272 assert(string_type == 0);
4273
4274 ch = PyUnicode_READ(kind, data, *ofs);
4275 if (quote_char) {
4276 /* We're inside a string. See if we're at the end. */
4277 /* This code needs to implement the same non-error logic
4278 as tok_get from tokenizer.c, at the letter_quote
4279 label. To actually share that code would be a
4280 nightmare. But, it's unlikely to change and is small,
4281 so duplicate it here. Note we don't need to catch all
4282 of the errors, since they'll be caught when parsing the
4283 expression. We just need to match the non-error
4284 cases. Thus we can ignore \n in single-quoted strings,
4285 for example. Or non-terminated strings. */
4286 if (ch == quote_char) {
4287 /* Does this match the string_type (single or triple
4288 quoted)? */
4289 if (string_type == 3) {
4290 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4291 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4292 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4293 /* We're at the end of a triple quoted string. */
4294 *ofs += 2;
4295 string_type = 0;
4296 quote_char = 0;
4297 continue;
4298 }
4299 } else {
4300 /* We're at the end of a normal string. */
4301 quote_char = 0;
4302 string_type = 0;
4303 continue;
4304 }
4305 }
4306 /* We're inside a string, and not finished with the
4307 string. If this is a backslash, skip the next char (it
4308 might be an end quote that needs skipping). Otherwise,
4309 just consume this character normally. */
4310 if (ch == '\\' && *ofs+1 < PyUnicode_GET_LENGTH(str)) {
4311 /* Just skip the next char, whatever it is. */
4312 *ofs += 1;
4313 }
4314 } else if (ch == '\'' || ch == '"') {
4315 /* Is this a triple quoted string? */
4316 if (*ofs+2 < PyUnicode_GET_LENGTH(str) &&
4317 PyUnicode_READ(kind, data, *ofs+1) == ch &&
4318 PyUnicode_READ(kind, data, *ofs+2) == ch) {
4319 string_type = 3;
4320 *ofs += 2;
4321 } else {
4322 /* Start of a normal string. */
4323 string_type = 1;
4324 }
4325 /* Start looking for the end of the string. */
4326 quote_char = ch;
4327 } else if (ch == '[' || ch == '{' || ch == '(') {
4328 nested_depth++;
4329 } else if (nested_depth != 0 &&
4330 (ch == ']' || ch == '}' || ch == ')')) {
4331 nested_depth--;
4332 } else if (ch == '#') {
4333 /* Error: can't include a comment character, inside parens
4334 or not. */
4335 ast_error(c, n, "f-string cannot include '#'");
4336 return -1;
4337 } else if (nested_depth == 0 &&
4338 (ch == '!' || ch == ':' || ch == '}')) {
4339 /* First, test for the special case of "!=". Since '=' is
4340 not an allowed conversion character, nothing is lost in
4341 this test. */
4342 if (ch == '!' && *ofs+1 < PyUnicode_GET_LENGTH(str) &&
4343 PyUnicode_READ(kind, data, *ofs+1) == '=')
4344 /* This isn't a conversion character, just continue. */
4345 continue;
4346
4347 /* Normal way out of this loop. */
4348 break;
4349 } else {
4350 /* Just consume this char and loop around. */
4351 }
4352 }
4353 expr_end = *ofs;
4354 /* If we leave this loop in a string or with mismatched parens, we
4355 don't care. We'll get a syntax error when compiling the
4356 expression. But, we can produce a better error message, so
4357 let's just do that.*/
4358 if (quote_char) {
4359 ast_error(c, n, "f-string: unterminated string");
4360 return -1;
4361 }
4362 if (nested_depth) {
4363 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4364 return -1;
4365 }
4366
Eric V. Smith235a6f02015-09-19 14:51:32 -04004367 if (*ofs >= PyUnicode_GET_LENGTH(str))
4368 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004369
4370 /* Compile the expression as soon as possible, so we show errors
4371 related to the expression before errors related to the
4372 conversion or format_spec. */
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004373 simple_expression = fstring_compile_expr(str, expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004374 if (!simple_expression)
4375 return -1;
4376
4377 /* Check for a conversion char, if present. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004378 if (PyUnicode_READ(kind, data, *ofs) == '!') {
4379 *ofs += 1;
4380 if (*ofs >= PyUnicode_GET_LENGTH(str))
4381 goto unexpected_end_of_string;
4382
4383 conversion = PyUnicode_READ(kind, data, *ofs);
4384 *ofs += 1;
4385
4386 /* Validate the conversion. */
4387 if (!(conversion == 's' || conversion == 'r'
4388 || conversion == 'a')) {
4389 ast_error(c, n, "f-string: invalid conversion character: "
4390 "expected 's', 'r', or 'a'");
4391 return -1;
4392 }
4393 }
4394
4395 /* Check for the format spec, if present. */
4396 if (*ofs >= PyUnicode_GET_LENGTH(str))
4397 goto unexpected_end_of_string;
4398 if (PyUnicode_READ(kind, data, *ofs) == ':') {
4399 *ofs += 1;
4400 if (*ofs >= PyUnicode_GET_LENGTH(str))
4401 goto unexpected_end_of_string;
4402
4403 /* Parse the format spec. */
4404 format_spec = fstring_parse(str, ofs, recurse_lvl+1, c, n);
4405 if (!format_spec)
4406 return -1;
4407 }
4408
4409 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4410 PyUnicode_READ(kind, data, *ofs) != '}')
4411 goto unexpected_end_of_string;
4412
4413 /* We're at a right brace. Consume it. */
4414 assert(*ofs < PyUnicode_GET_LENGTH(str));
4415 assert(PyUnicode_READ(kind, data, *ofs) == '}');
4416 *ofs += 1;
4417
Eric V. Smith235a6f02015-09-19 14:51:32 -04004418 /* And now create the FormattedValue node that represents this entire
4419 expression with the conversion and format spec. */
4420 *expression = FormattedValue(simple_expression, (int)conversion,
4421 format_spec, LINENO(n), n->n_col_offset,
4422 c->c_arena);
4423 if (!*expression)
4424 return -1;
4425
4426 return 0;
4427
4428unexpected_end_of_string:
4429 ast_error(c, n, "f-string: expecting '}'");
4430 return -1;
4431}
4432
4433/* Return -1 on error.
4434
4435 Return 0 if we have a literal (possible zero length) and an
4436 expression (zero length if at the end of the string.
4437
4438 Return 1 if we have a literal, but no expression, and we want the
4439 caller to call us again. This is used to deal with doubled
4440 braces.
4441
4442 When called multiple times on the string 'a{{b{0}c', this function
4443 will return:
4444
4445 1. the literal 'a{' with no expression, and a return value
4446 of 1. Despite the fact that there's no expression, the return
4447 value of 1 means we're not finished yet.
4448
4449 2. the literal 'b' and the expression '0', with a return value of
4450 0. The fact that there's an expression means we're not finished.
4451
4452 3. literal 'c' with no expression and a return value of 0. The
4453 combination of the return value of 0 with no expression means
4454 we're finished.
4455*/
4456static int
4457fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4458 PyObject **literal, expr_ty *expression,
4459 struct compiling *c, const node *n)
4460{
4461 int result;
4462
4463 assert(*literal == NULL && *expression == NULL);
4464
4465 /* Get any literal string. */
4466 result = fstring_find_literal(str, ofs, literal, recurse_lvl, c, n);
4467 if (result < 0)
4468 goto error;
4469
4470 assert(result == 0 || result == 1);
4471
4472 if (result == 1)
4473 /* We have a literal, but don't look at the expression. */
4474 return 1;
4475
4476 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4477
4478 if (*ofs >= PyUnicode_GET_LENGTH(str) ||
4479 PyUnicode_READ_CHAR(str, *ofs) == '}')
4480 /* We're at the end of the string or the end of a nested
4481 f-string: no expression. The top-level error case where we
4482 expect to be at the end of the string but we're at a '}' is
4483 handled later. */
4484 return 0;
4485
4486 /* We must now be the start of an expression, on a '{'. */
4487 assert(*ofs < PyUnicode_GET_LENGTH(str) &&
4488 PyUnicode_READ_CHAR(str, *ofs) == '{');
4489
4490 if (fstring_find_expr(str, ofs, recurse_lvl, expression, c, n) < 0)
4491 goto error;
4492
4493 return 0;
4494
4495error:
4496 Py_XDECREF(*literal);
4497 *literal = NULL;
4498 return -1;
4499}
4500
4501#define EXPRLIST_N_CACHED 64
4502
4503typedef struct {
4504 /* Incrementally build an array of expr_ty, so be used in an
4505 asdl_seq. Cache some small but reasonably sized number of
4506 expr_ty's, and then after that start dynamically allocating,
4507 doubling the number allocated each time. Note that the f-string
4508 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4509 Str for the literal 'a'. So you add expr_ty's about twice as
4510 fast as you add exressions in an f-string. */
4511
4512 Py_ssize_t allocated; /* Number we've allocated. */
4513 Py_ssize_t size; /* Number we've used. */
4514 expr_ty *p; /* Pointer to the memory we're actually
4515 using. Will point to 'data' until we
4516 start dynamically allocating. */
4517 expr_ty data[EXPRLIST_N_CACHED];
4518} ExprList;
4519
4520#ifdef NDEBUG
4521#define ExprList_check_invariants(l)
4522#else
4523static void
4524ExprList_check_invariants(ExprList *l)
4525{
4526 /* Check our invariants. Make sure this object is "live", and
4527 hasn't been deallocated. */
4528 assert(l->size >= 0);
4529 assert(l->p != NULL);
4530 if (l->size <= EXPRLIST_N_CACHED)
4531 assert(l->data == l->p);
4532}
4533#endif
4534
4535static void
4536ExprList_Init(ExprList *l)
4537{
4538 l->allocated = EXPRLIST_N_CACHED;
4539 l->size = 0;
4540
4541 /* Until we start allocating dynamically, p points to data. */
4542 l->p = l->data;
4543
4544 ExprList_check_invariants(l);
4545}
4546
4547static int
4548ExprList_Append(ExprList *l, expr_ty exp)
4549{
4550 ExprList_check_invariants(l);
4551 if (l->size >= l->allocated) {
4552 /* We need to alloc (or realloc) the memory. */
4553 Py_ssize_t new_size = l->allocated * 2;
4554
4555 /* See if we've ever allocated anything dynamically. */
4556 if (l->p == l->data) {
4557 Py_ssize_t i;
4558 /* We're still using the cached data. Switch to
4559 alloc-ing. */
4560 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4561 if (!l->p)
4562 return -1;
4563 /* Copy the cached data into the new buffer. */
4564 for (i = 0; i < l->size; i++)
4565 l->p[i] = l->data[i];
4566 } else {
4567 /* Just realloc. */
4568 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4569 if (!tmp) {
4570 PyMem_RawFree(l->p);
4571 l->p = NULL;
4572 return -1;
4573 }
4574 l->p = tmp;
4575 }
4576
4577 l->allocated = new_size;
4578 assert(l->allocated == 2 * l->size);
4579 }
4580
4581 l->p[l->size++] = exp;
4582
4583 ExprList_check_invariants(l);
4584 return 0;
4585}
4586
4587static void
4588ExprList_Dealloc(ExprList *l)
4589{
4590 ExprList_check_invariants(l);
4591
4592 /* If there's been an error, or we've never dynamically allocated,
4593 do nothing. */
4594 if (!l->p || l->p == l->data) {
4595 /* Do nothing. */
4596 } else {
4597 /* We have dynamically allocated. Free the memory. */
4598 PyMem_RawFree(l->p);
4599 }
4600 l->p = NULL;
4601 l->size = -1;
4602}
4603
4604static asdl_seq *
4605ExprList_Finish(ExprList *l, PyArena *arena)
4606{
4607 asdl_seq *seq;
4608
4609 ExprList_check_invariants(l);
4610
4611 /* Allocate the asdl_seq and copy the expressions in to it. */
4612 seq = _Py_asdl_seq_new(l->size, arena);
4613 if (seq) {
4614 Py_ssize_t i;
4615 for (i = 0; i < l->size; i++)
4616 asdl_seq_SET(seq, i, l->p[i]);
4617 }
4618 ExprList_Dealloc(l);
4619 return seq;
4620}
4621
4622/* The FstringParser is designed to add a mix of strings and
4623 f-strings, and concat them together as needed. Ultimately, it
4624 generates an expr_ty. */
4625typedef struct {
4626 PyObject *last_str;
4627 ExprList expr_list;
4628} FstringParser;
4629
4630#ifdef NDEBUG
4631#define FstringParser_check_invariants(state)
4632#else
4633static void
4634FstringParser_check_invariants(FstringParser *state)
4635{
4636 if (state->last_str)
4637 assert(PyUnicode_CheckExact(state->last_str));
4638 ExprList_check_invariants(&state->expr_list);
4639}
4640#endif
4641
4642static void
4643FstringParser_Init(FstringParser *state)
4644{
4645 state->last_str = NULL;
4646 ExprList_Init(&state->expr_list);
4647 FstringParser_check_invariants(state);
4648}
4649
4650static void
4651FstringParser_Dealloc(FstringParser *state)
4652{
4653 FstringParser_check_invariants(state);
4654
4655 Py_XDECREF(state->last_str);
4656 ExprList_Dealloc(&state->expr_list);
4657}
4658
4659/* Make a Str node, but decref the PyUnicode object being added. */
4660static expr_ty
4661make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4662{
4663 PyObject *s = *str;
4664 *str = NULL;
4665 assert(PyUnicode_CheckExact(s));
4666 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4667 Py_DECREF(s);
4668 return NULL;
4669 }
4670 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4671}
4672
4673/* Add a non-f-string (that is, a regular literal string). str is
4674 decref'd. */
4675static int
4676FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4677{
4678 FstringParser_check_invariants(state);
4679
4680 assert(PyUnicode_CheckExact(str));
4681
4682 if (PyUnicode_GET_LENGTH(str) == 0) {
4683 Py_DECREF(str);
4684 return 0;
4685 }
4686
4687 if (!state->last_str) {
4688 /* We didn't have a string before, so just remember this one. */
4689 state->last_str = str;
4690 } else {
4691 /* Concatenate this with the previous string. */
4692 PyObject *temp = PyUnicode_Concat(state->last_str, str);
4693 Py_DECREF(state->last_str);
4694 Py_DECREF(str);
4695 state->last_str = temp;
4696 if (!temp)
4697 return -1;
4698 }
4699 FstringParser_check_invariants(state);
4700 return 0;
4701}
4702
4703/* Parse an f-string. The f-string is in str, starting at ofs, with no 'f'
4704 or quotes. str is not decref'd, since we don't know if it's used elsewhere.
4705 And if we're only looking at a part of a string, then decref'ing is
4706 definitely not the right thing to do! */
4707static int
4708FstringParser_ConcatFstring(FstringParser *state, PyObject *str,
4709 Py_ssize_t *ofs, int recurse_lvl,
4710 struct compiling *c, const node *n)
4711{
4712 FstringParser_check_invariants(state);
4713
4714 /* Parse the f-string. */
4715 while (1) {
4716 PyObject *literal = NULL;
4717 expr_ty expression = NULL;
4718
4719 /* If there's a zero length literal in front of the
4720 expression, literal will be NULL. If we're at the end of
4721 the f-string, expression will be NULL (unless result == 1,
4722 see below). */
4723 int result = fstring_find_literal_and_expr(str, ofs, recurse_lvl,
4724 &literal, &expression,
4725 c, n);
4726 if (result < 0)
4727 return -1;
4728
4729 /* Add the literal, if any. */
4730 if (!literal) {
4731 /* Do nothing. Just leave last_str alone (and possibly
4732 NULL). */
4733 } else if (!state->last_str) {
4734 state->last_str = literal;
4735 literal = NULL;
4736 } else {
4737 /* We have a literal, concatenate it. */
4738 assert(PyUnicode_GET_LENGTH(literal) != 0);
4739 if (FstringParser_ConcatAndDel(state, literal) < 0)
4740 return -1;
4741 literal = NULL;
4742 }
4743 assert(!state->last_str ||
4744 PyUnicode_GET_LENGTH(state->last_str) != 0);
4745
4746 /* We've dealt with the literal now. It can't be leaked on further
4747 errors. */
4748 assert(literal == NULL);
4749
4750 /* See if we should just loop around to get the next literal
4751 and expression, while ignoring the expression this
4752 time. This is used for un-doubling braces, as an
4753 optimization. */
4754 if (result == 1)
4755 continue;
4756
4757 if (!expression)
4758 /* We're done with this f-string. */
4759 break;
4760
4761 /* We know we have an expression. Convert any existing string
4762 to a Str node. */
4763 if (!state->last_str) {
4764 /* Do nothing. No previous literal. */
4765 } else {
4766 /* Convert the existing last_str literal to a Str node. */
4767 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4768 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4769 return -1;
4770 }
4771
4772 if (ExprList_Append(&state->expr_list, expression) < 0)
4773 return -1;
4774 }
4775
4776 assert(*ofs <= PyUnicode_GET_LENGTH(str));
4777
4778 /* If recurse_lvl is zero, then we must be at the end of the
4779 string. Otherwise, we must be at a right brace. */
4780
4781 if (recurse_lvl == 0 && *ofs < PyUnicode_GET_LENGTH(str)) {
4782 ast_error(c, n, "f-string: unexpected end of string");
4783 return -1;
4784 }
4785 if (recurse_lvl != 0 && PyUnicode_READ_CHAR(str, *ofs) != '}') {
4786 ast_error(c, n, "f-string: expecting '}'");
4787 return -1;
4788 }
4789
4790 FstringParser_check_invariants(state);
4791 return 0;
4792}
4793
4794/* Convert the partial state reflected in last_str and expr_list to an
4795 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
4796static expr_ty
4797FstringParser_Finish(FstringParser *state, struct compiling *c,
4798 const node *n)
4799{
4800 asdl_seq *seq;
4801
4802 FstringParser_check_invariants(state);
4803
4804 /* If we're just a constant string with no expressions, return
4805 that. */
4806 if(state->expr_list.size == 0) {
4807 if (!state->last_str) {
4808 /* Create a zero length string. */
4809 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
4810 if (!state->last_str)
4811 goto error;
4812 }
4813 return make_str_node_and_del(&state->last_str, c, n);
4814 }
4815
4816 /* Create a Str node out of last_str, if needed. It will be the
4817 last node in our expression list. */
4818 if (state->last_str) {
4819 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4820 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4821 goto error;
4822 }
4823 /* This has already been freed. */
4824 assert(state->last_str == NULL);
4825
4826 seq = ExprList_Finish(&state->expr_list, c->c_arena);
4827 if (!seq)
4828 goto error;
4829
4830 /* If there's only one expression, return it. Otherwise, we need
4831 to join them together. */
4832 if (seq->size == 1)
4833 return seq->elements[0];
4834
4835 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
4836
4837error:
4838 FstringParser_Dealloc(state);
4839 return NULL;
4840}
4841
4842/* Given an f-string (with no 'f' or quotes) that's in str starting at
4843 ofs, parse it into an expr_ty. Return NULL on error. Does not
4844 decref str. */
4845static expr_ty
4846fstring_parse(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
4847 struct compiling *c, const node *n)
4848{
4849 FstringParser state;
4850
4851 FstringParser_Init(&state);
4852 if (FstringParser_ConcatFstring(&state, str, ofs, recurse_lvl,
4853 c, n) < 0) {
4854 FstringParser_Dealloc(&state);
4855 return NULL;
4856 }
4857
4858 return FstringParser_Finish(&state, c, n);
4859}
4860
4861/* n is a Python string literal, including the bracketing quote
4862 characters, and r, b, u, &/or f prefixes (if any), and embedded
4863 escape sequences (if any). parsestr parses it, and returns the
4864 decoded Python string object. If the string is an f-string, set
4865 *fmode and return the unparsed string object.
4866*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004867static PyObject *
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868parsestr(struct compiling *c, const node *n, int *bytesmode, int *fmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004869{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004870 size_t len;
4871 const char *s = STR(n);
4872 int quote = Py_CHARMASK(*s);
4873 int rawmode = 0;
4874 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004875 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004876 while (!*bytesmode || !rawmode) {
4877 if (quote == 'b' || quote == 'B') {
4878 quote = *++s;
4879 *bytesmode = 1;
4880 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004881 else if (quote == 'u' || quote == 'U') {
4882 quote = *++s;
4883 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004884 else if (quote == 'r' || quote == 'R') {
4885 quote = *++s;
4886 rawmode = 1;
4887 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 else if (quote == 'f' || quote == 'F') {
4889 quote = *++s;
4890 *fmode = 1;
4891 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004892 else {
4893 break;
4894 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004895 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004896 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 if (*fmode && *bytesmode) {
4898 PyErr_BadInternalCall();
4899 return NULL;
4900 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004901 if (quote != '\'' && quote != '\"') {
4902 PyErr_BadInternalCall();
4903 return NULL;
4904 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004906 s++;
4907 len = strlen(s);
4908 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004910 "string to parse is too long");
4911 return NULL;
4912 }
4913 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004915 PyErr_BadInternalCall();
4916 return NULL;
4917 }
4918 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919 /* A triple quoted string. We've already skipped one quote at
4920 the start and one at the end of the string. Now skip the
4921 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004922 s += 2;
4923 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004925 if (s[--len] != quote || s[--len] != quote) {
4926 PyErr_BadInternalCall();
4927 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004928 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004929 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004930 if (!*bytesmode && !rawmode) {
Eric V. Smith5567f892015-09-21 13:36:09 -04004931 return decode_unicode(c, s, len, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004932 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004933 if (*bytesmode) {
4934 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004935 const char *ch;
4936 for (ch = s; *ch; ch++) {
4937 if (Py_CHARMASK(*ch) >= 0x80) {
4938 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004939 "literal characters.");
4940 return NULL;
4941 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004942 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004943 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004944 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004945 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004946 if (rawmode || strchr(s, '\\') == NULL) {
4947 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004948 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004949 if (u == NULL || !*bytesmode)
4950 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004951 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004952 Py_DECREF(u);
4953 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004954 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00004955 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004956 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004957 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004959 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004960 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004961 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004962 return PyBytes_DecodeEscape(s, len, NULL, 1,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004964}
4965
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
4967 each STRING atom, and process it as needed. For bytes, just
4968 concatenate them together, and the result will be a Bytes node. For
4969 normal strings and f-strings, concatenate them together. The result
4970 will be a Str node if there were no f-strings; a FormattedValue
4971 node if there's just an f-string (with no leading or trailing
4972 literals), or a JoinedStr node if there are multiple f-strings or
4973 any literals involved. */
4974static expr_ty
4975parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004976{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 int bytesmode = 0;
4978 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004979 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980
4981 FstringParser state;
4982 FstringParser_Init(&state);
4983
4984 for (i = 0; i < NCH(n); i++) {
4985 int this_bytesmode = 0;
4986 int this_fmode = 0;
4987 PyObject *s;
4988
4989 REQ(CHILD(n, i), STRING);
4990 s = parsestr(c, CHILD(n, i), &this_bytesmode, &this_fmode);
4991 if (!s)
4992 goto error;
4993
4994 /* Check that we're not mixing bytes with unicode. */
4995 if (i != 0 && bytesmode != this_bytesmode) {
4996 ast_error(c, n, "cannot mix bytes and nonbytes literals");
4997 Py_DECREF(s);
4998 goto error;
4999 }
5000 bytesmode = this_bytesmode;
5001
5002 assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
5003
5004 if (bytesmode) {
5005 /* For bytes, concat as we go. */
5006 if (i == 0) {
5007 /* First time, just remember this value. */
5008 bytes_str = s;
5009 } else {
5010 PyBytes_ConcatAndDel(&bytes_str, s);
5011 if (!bytes_str)
5012 goto error;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005013 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014 } else if (this_fmode) {
5015 /* This is an f-string. Concatenate and decref it. */
5016 Py_ssize_t ofs = 0;
5017 int result = FstringParser_ConcatFstring(&state, s, &ofs, 0, c, n);
5018 Py_DECREF(s);
5019 if (result < 0)
5020 goto error;
5021 } else {
5022 /* This is a regular string. Concatenate it. */
5023 if (FstringParser_ConcatAndDel(&state, s) < 0)
5024 goto error;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005025 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005026 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 if (bytesmode) {
5028 /* Just return the bytes object and we're done. */
5029 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5030 goto error;
5031 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5032 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005033
Eric V. Smith235a6f02015-09-19 14:51:32 -04005034 /* We're not a bytes string, bytes_str should never have been set. */
5035 assert(bytes_str == NULL);
5036
5037 return FstringParser_Finish(&state, c, n);
5038
5039error:
5040 Py_XDECREF(bytes_str);
5041 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005043}