blob: bb28437ae2faff6201a1bfb14aed5674b8ccf6a5 [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 }
202 return validate_exprs(exp->v.Dict.keys, Load, 0) &&
203 validate_exprs(exp->v.Dict.values, Load, 0);
204 case Set_kind:
205 return validate_exprs(exp->v.Set.elts, Load, 0);
206#define COMP(NAME) \
207 case NAME ## _kind: \
208 return validate_comprehension(exp->v.NAME.generators) && \
209 validate_expr(exp->v.NAME.elt, Load);
210 COMP(ListComp)
211 COMP(SetComp)
212 COMP(GeneratorExp)
213#undef COMP
214 case DictComp_kind:
215 return validate_comprehension(exp->v.DictComp.generators) &&
216 validate_expr(exp->v.DictComp.key, Load) &&
217 validate_expr(exp->v.DictComp.value, Load);
218 case Yield_kind:
219 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500220 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000221 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400222 case Await_kind:
223 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500224 case Compare_kind:
225 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
226 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
227 return 0;
228 }
229 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
230 asdl_seq_LEN(exp->v.Compare.ops)) {
231 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
232 "of comparators and operands");
233 return 0;
234 }
235 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
236 validate_expr(exp->v.Compare.left, Load);
237 case Call_kind:
238 return validate_expr(exp->v.Call.func, Load) &&
239 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400240 validate_keywords(exp->v.Call.keywords);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500241 case Num_kind: {
242 PyObject *n = exp->v.Num.n;
243 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
244 !PyComplex_CheckExact(n)) {
245 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
246 return 0;
247 }
248 return 1;
249 }
250 case Str_kind: {
251 PyObject *s = exp->v.Str.s;
252 if (!PyUnicode_CheckExact(s)) {
253 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
254 return 0;
255 }
256 return 1;
257 }
258 case Bytes_kind: {
259 PyObject *b = exp->v.Bytes.s;
260 if (!PyBytes_CheckExact(b)) {
261 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
262 return 0;
263 }
264 return 1;
265 }
266 case Attribute_kind:
267 return validate_expr(exp->v.Attribute.value, Load);
268 case Subscript_kind:
269 return validate_slice(exp->v.Subscript.slice) &&
270 validate_expr(exp->v.Subscript.value, Load);
271 case Starred_kind:
272 return validate_expr(exp->v.Starred.value, ctx);
273 case List_kind:
274 return validate_exprs(exp->v.List.elts, ctx, 0);
275 case Tuple_kind:
276 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
277 /* These last cases don't have any checking. */
278 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500279 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500280 case Ellipsis_kind:
281 return 1;
282 default:
283 PyErr_SetString(PyExc_SystemError, "unexpected expression");
284 return 0;
285 }
286}
287
288static int
289validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
290{
291 if (asdl_seq_LEN(seq))
292 return 1;
293 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
294 return 0;
295}
296
297static int
298validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
299{
300 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
301 validate_exprs(targets, ctx, 0);
302}
303
304static int
305validate_body(asdl_seq *body, const char *owner)
306{
307 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
308}
309
310static int
311validate_stmt(stmt_ty stmt)
312{
313 int i;
314 switch (stmt->kind) {
315 case FunctionDef_kind:
316 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
317 validate_arguments(stmt->v.FunctionDef.args) &&
318 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
319 (!stmt->v.FunctionDef.returns ||
320 validate_expr(stmt->v.FunctionDef.returns, Load));
321 case ClassDef_kind:
322 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
323 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
324 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400325 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500326 case Return_kind:
327 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
328 case Delete_kind:
329 return validate_assignlist(stmt->v.Delete.targets, Del);
330 case Assign_kind:
331 return validate_assignlist(stmt->v.Assign.targets, Store) &&
332 validate_expr(stmt->v.Assign.value, Load);
333 case AugAssign_kind:
334 return validate_expr(stmt->v.AugAssign.target, Store) &&
335 validate_expr(stmt->v.AugAssign.value, Load);
336 case For_kind:
337 return validate_expr(stmt->v.For.target, Store) &&
338 validate_expr(stmt->v.For.iter, Load) &&
339 validate_body(stmt->v.For.body, "For") &&
340 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400341 case AsyncFor_kind:
342 return validate_expr(stmt->v.AsyncFor.target, Store) &&
343 validate_expr(stmt->v.AsyncFor.iter, Load) &&
344 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
345 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346 case While_kind:
347 return validate_expr(stmt->v.While.test, Load) &&
348 validate_body(stmt->v.While.body, "While") &&
349 validate_stmts(stmt->v.While.orelse);
350 case If_kind:
351 return validate_expr(stmt->v.If.test, Load) &&
352 validate_body(stmt->v.If.body, "If") &&
353 validate_stmts(stmt->v.If.orelse);
354 case With_kind:
355 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
356 return 0;
357 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
358 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
359 if (!validate_expr(item->context_expr, Load) ||
360 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
361 return 0;
362 }
363 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400364 case AsyncWith_kind:
365 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
366 return 0;
367 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
368 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.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.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500374 case Raise_kind:
375 if (stmt->v.Raise.exc) {
376 return validate_expr(stmt->v.Raise.exc, Load) &&
377 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
378 }
379 if (stmt->v.Raise.cause) {
380 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
381 return 0;
382 }
383 return 1;
384 case Try_kind:
385 if (!validate_body(stmt->v.Try.body, "Try"))
386 return 0;
387 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
388 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
389 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
390 return 0;
391 }
392 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
393 asdl_seq_LEN(stmt->v.Try.orelse)) {
394 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
395 return 0;
396 }
397 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
398 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
399 if ((handler->v.ExceptHandler.type &&
400 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
401 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
402 return 0;
403 }
404 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
405 validate_stmts(stmt->v.Try.finalbody)) &&
406 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
407 validate_stmts(stmt->v.Try.orelse));
408 case Assert_kind:
409 return validate_expr(stmt->v.Assert.test, Load) &&
410 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
411 case Import_kind:
412 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
413 case ImportFrom_kind:
414 if (stmt->v.ImportFrom.level < -1) {
415 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
416 return 0;
417 }
418 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
419 case Global_kind:
420 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
421 case Nonlocal_kind:
422 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
423 case Expr_kind:
424 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400425 case AsyncFunctionDef_kind:
426 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
427 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
428 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
429 (!stmt->v.AsyncFunctionDef.returns ||
430 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500431 case Pass_kind:
432 case Break_kind:
433 case Continue_kind:
434 return 1;
435 default:
436 PyErr_SetString(PyExc_SystemError, "unexpected statement");
437 return 0;
438 }
439}
440
441static int
442validate_stmts(asdl_seq *seq)
443{
444 int i;
445 for (i = 0; i < asdl_seq_LEN(seq); i++) {
446 stmt_ty stmt = asdl_seq_GET(seq, i);
447 if (stmt) {
448 if (!validate_stmt(stmt))
449 return 0;
450 }
451 else {
452 PyErr_SetString(PyExc_ValueError,
453 "None disallowed in statement list");
454 return 0;
455 }
456 }
457 return 1;
458}
459
460static int
461validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
462{
463 int i;
464 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
465 expr_ty expr = asdl_seq_GET(exprs, i);
466 if (expr) {
467 if (!validate_expr(expr, ctx))
468 return 0;
469 }
470 else if (!null_ok) {
471 PyErr_SetString(PyExc_ValueError,
472 "None disallowed in expression list");
473 return 0;
474 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100475
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500476 }
477 return 1;
478}
479
480int
481PyAST_Validate(mod_ty mod)
482{
483 int res = 0;
484
485 switch (mod->kind) {
486 case Module_kind:
487 res = validate_stmts(mod->v.Module.body);
488 break;
489 case Interactive_kind:
490 res = validate_stmts(mod->v.Interactive.body);
491 break;
492 case Expression_kind:
493 res = validate_expr(mod->v.Expression.body, Load);
494 break;
495 case Suite_kind:
496 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
497 break;
498 default:
499 PyErr_SetString(PyExc_SystemError, "impossible module node");
500 res = 0;
501 break;
502 }
503 return res;
504}
505
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500506/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500507#include "grammar.h"
508#include "parsetok.h"
509#include "graminit.h"
510
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511/* Data structure used internally */
512struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000513 char *c_encoding; /* source encoding */
514 PyArena *c_arena; /* arena for allocating memeory */
Victor Stinner14e461d2013-08-26 22:28:21 +0200515 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500516 PyObject *c_normalize; /* Normalization function from unicodedata. */
517 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518};
519
520static asdl_seq *seq_for_testlist(struct compiling *, const node *);
521static expr_ty ast_for_expr(struct compiling *, const node *);
522static stmt_ty ast_for_stmt(struct compiling *, const node *);
523static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
525 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000526static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000527static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Yury Selivanov75445082015-05-11 22:57:16 -0400529static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
530static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532/* Note different signature for ast_for_call */
533static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
534
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000535static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000536static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +0000537static PyObject *parsestrplus(struct compiling *, const node *n,
538 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Nick Coghlan650f0d02007-04-15 12:05:43 +0000540#define COMP_GENEXP 0
541#define COMP_LISTCOMP 1
542#define COMP_SETCOMP 2
543
Benjamin Peterson55e00432012-01-16 17:22:31 -0500544static int
545init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000546{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500547 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
548 if (!m)
549 return 0;
550 c->c_normalize = PyObject_GetAttrString(m, "normalize");
551 Py_DECREF(m);
552 if (!c->c_normalize)
553 return 0;
554 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500555 if (!c->c_normalize_args) {
556 Py_CLEAR(c->c_normalize);
557 return 0;
558 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200559 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500560 return 1;
561}
562
563static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400564new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500565{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400566 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500567 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000568 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500569 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500570 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000571 /* Check whether there are non-ASCII characters in the
572 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500573 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200574 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500575 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500576 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500578 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500579 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
580 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500581 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 if (!id2)
583 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200584 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000585 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000586 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200587 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
588 Py_DECREF(id);
589 return NULL;
590 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000591 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592}
593
Benjamin Peterson55e00432012-01-16 17:22:31 -0500594#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400597ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400599 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Victor Stinner14e461d2013-08-26 22:28:21 +0200601 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000603 Py_INCREF(Py_None);
604 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200606 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400607 if (!tmp)
608 return 0;
609 errstr = PyUnicode_FromString(errmsg);
610 if (!errstr) {
611 Py_DECREF(tmp);
612 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000613 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 Py_DECREF(errstr);
616 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400617 if (value) {
618 PyErr_SetObject(PyExc_SyntaxError, value);
619 Py_DECREF(value);
620 }
621 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622}
623
624/* num_stmts() returns number of contained statements.
625
626 Use this routine to determine how big a sequence is needed for
627 the statements in a parse tree. Its raison d'etre is this bit of
628 grammar:
629
630 stmt: simple_stmt | compound_stmt
631 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
632
633 A simple_stmt can contain multiple small_stmt elements joined
634 by semicolons. If the arg is a simple_stmt, the number of
635 small_stmt elements is returned.
636*/
637
638static int
639num_stmts(const node *n)
640{
641 int i, l;
642 node *ch;
643
644 switch (TYPE(n)) {
645 case single_input:
646 if (TYPE(CHILD(n, 0)) == NEWLINE)
647 return 0;
648 else
649 return num_stmts(CHILD(n, 0));
650 case file_input:
651 l = 0;
652 for (i = 0; i < NCH(n); i++) {
653 ch = CHILD(n, i);
654 if (TYPE(ch) == stmt)
655 l += num_stmts(ch);
656 }
657 return l;
658 case stmt:
659 return num_stmts(CHILD(n, 0));
660 case compound_stmt:
661 return 1;
662 case simple_stmt:
663 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
664 case suite:
665 if (NCH(n) == 1)
666 return num_stmts(CHILD(n, 0));
667 else {
668 l = 0;
669 for (i = 2; i < (NCH(n) - 1); i++)
670 l += num_stmts(CHILD(n, i));
671 return l;
672 }
673 default: {
674 char buf[128];
675
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000676 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 TYPE(n), NCH(n));
678 Py_FatalError(buf);
679 }
680 }
681 assert(0);
682 return 0;
683}
684
685/* Transform the CST rooted at node * to the appropriate AST
686*/
687
688mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200689PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
690 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000692 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 asdl_seq *stmts = NULL;
694 stmt_ty s;
695 node *ch;
696 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500697 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400699 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200700 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400701 c.c_filename = filename;
702 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000704 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000706#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400707 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500708 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000709#endif
710 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 } else if (TYPE(n) == encoding_decl) {
713 c.c_encoding = STR(n);
714 n = CHILD(n, 0);
715 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000717 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 }
719
Jeremy Hyltona8293132006-02-28 17:58:27 +0000720 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 switch (TYPE(n)) {
722 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200723 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500725 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 for (i = 0; i < NCH(n) - 1; i++) {
727 ch = CHILD(n, i);
728 if (TYPE(ch) == NEWLINE)
729 continue;
730 REQ(ch, stmt);
731 num = num_stmts(ch);
732 if (num == 1) {
733 s = ast_for_stmt(&c, ch);
734 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500735 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000736 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 }
738 else {
739 ch = CHILD(ch, 0);
740 REQ(ch, simple_stmt);
741 for (j = 0; j < num; j++) {
742 s = ast_for_stmt(&c, CHILD(ch, j * 2));
743 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500744 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000745 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 }
747 }
748 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500749 res = Module(stmts, arena);
750 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 case eval_input: {
752 expr_ty testlist_ast;
753
Nick Coghlan650f0d02007-04-15 12:05:43 +0000754 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000755 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 goto out;
758 res = Expression(testlist_ast, arena);
759 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 }
761 case single_input:
762 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200763 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500765 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
767 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000768 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 goto out;
770 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 }
772 else {
773 n = CHILD(n, 0);
774 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200775 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500777 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000779 s = ast_for_stmt(&c, n);
780 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 asdl_seq_SET(stmts, 0, s);
783 }
784 else {
785 /* Only a simple_stmt can contain multiple statements. */
786 REQ(n, simple_stmt);
787 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 if (TYPE(CHILD(n, i)) == NEWLINE)
789 break;
790 s = ast_for_stmt(&c, CHILD(n, i));
791 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500792 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 asdl_seq_SET(stmts, i / 2, s);
794 }
795 }
796
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500799 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000801 PyErr_Format(PyExc_SystemError,
802 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500803 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 out:
806 if (c.c_normalize) {
807 Py_DECREF(c.c_normalize);
808 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
809 Py_DECREF(c.c_normalize_args);
810 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
Victor Stinner14e461d2013-08-26 22:28:21 +0200814mod_ty
815PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
816 PyArena *arena)
817{
818 mod_ty mod;
819 PyObject *filename;
820 filename = PyUnicode_DecodeFSDefault(filename_str);
821 if (filename == NULL)
822 return NULL;
823 mod = PyAST_FromNodeObject(n, flags, filename, arena);
824 Py_DECREF(filename);
825 return mod;
826
827}
828
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
830*/
831
832static operator_ty
833get_operator(const node *n)
834{
835 switch (TYPE(n)) {
836 case VBAR:
837 return BitOr;
838 case CIRCUMFLEX:
839 return BitXor;
840 case AMPER:
841 return BitAnd;
842 case LEFTSHIFT:
843 return LShift;
844 case RIGHTSHIFT:
845 return RShift;
846 case PLUS:
847 return Add;
848 case MINUS:
849 return Sub;
850 case STAR:
851 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400852 case AT:
853 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 case SLASH:
855 return Div;
856 case DOUBLESLASH:
857 return FloorDiv;
858 case PERCENT:
859 return Mod;
860 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
863}
864
Guido van Rossume7ba4952007-06-06 23:52:48 +0000865static const char* FORBIDDEN[] = {
866 "None",
867 "True",
868 "False",
869 NULL,
870};
871
872static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400873forbidden_name(struct compiling *c, identifier name, const node *n,
874 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000875{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000876 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000877 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400878 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000879 return 1;
880 }
881 if (full_checks) {
882 const char **p;
883 for (p = FORBIDDEN; *p; p++) {
884 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400885 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000886 return 1;
887 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000888 }
889 }
890 return 0;
891}
892
Jeremy Hyltona8293132006-02-28 17:58:27 +0000893/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
895 Only sets context for expr kinds that "can appear in assignment context"
896 (according to ../Parser/Python.asdl). For other expr kinds, it sets
897 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898*/
899
900static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000901set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902{
903 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000904 /* If a particular expression type can't be used for assign / delete,
905 set expr_name to its name and an error message will be generated.
906 */
907 const char* expr_name = NULL;
908
909 /* The ast defines augmented store and load contexts, but the
910 implementation here doesn't actually use them. The code may be
911 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000912 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000913 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000914 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000915 */
916 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
918 switch (e->kind) {
919 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000920 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400921 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000922 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000923 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000925 e->v.Subscript.ctx = ctx;
926 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000927 case Starred_kind:
928 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000929 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000930 return 0;
931 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000933 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500934 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000936 }
937 e->v.Name.ctx = ctx;
938 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000940 e->v.List.ctx = ctx;
941 s = e->v.List.elts;
942 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000944 if (asdl_seq_LEN(e->v.Tuple.elts)) {
945 e->v.Tuple.ctx = ctx;
946 s = e->v.Tuple.elts;
947 }
948 else {
949 expr_name = "()";
950 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000951 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000952 case Lambda_kind:
953 expr_name = "lambda";
954 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000956 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000957 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000958 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000960 case UnaryOp_kind:
961 expr_name = "operator";
962 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 expr_name = "generator expression";
965 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500967 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000968 expr_name = "yield expression";
969 break;
Yury Selivanov75445082015-05-11 22:57:16 -0400970 case Await_kind:
971 expr_name = "await expression";
972 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 case ListComp_kind:
974 expr_name = "list comprehension";
975 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000976 case SetComp_kind:
977 expr_name = "set comprehension";
978 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000979 case DictComp_kind:
980 expr_name = "dict comprehension";
981 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000982 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000983 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Num_kind:
985 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500986 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000987 expr_name = "literal";
988 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500989 case NameConstant_kind:
990 expr_name = "keyword";
991 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000992 case Ellipsis_kind:
993 expr_name = "Ellipsis";
994 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 case Compare_kind:
996 expr_name = "comparison";
997 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998 case IfExp_kind:
999 expr_name = "conditional expression";
1000 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001001 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyErr_Format(PyExc_SystemError,
1003 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001004 e->kind, e->lineno);
1005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 /* Check for error string set by switch */
1008 if (expr_name) {
1009 char buf[300];
1010 PyOS_snprintf(buf, sizeof(buf),
1011 "can't %s %s",
1012 ctx == Store ? "assign to" : "delete",
1013 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001014 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001015 }
1016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 */
1020 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001021 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001024 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001025 return 0;
1026 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 }
1028 return 1;
1029}
1030
1031static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001032ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033{
1034 REQ(n, augassign);
1035 n = CHILD(n, 0);
1036 switch (STR(n)[0]) {
1037 case '+':
1038 return Add;
1039 case '-':
1040 return Sub;
1041 case '/':
1042 if (STR(n)[1] == '/')
1043 return FloorDiv;
1044 else
1045 return Div;
1046 case '%':
1047 return Mod;
1048 case '<':
1049 return LShift;
1050 case '>':
1051 return RShift;
1052 case '&':
1053 return BitAnd;
1054 case '^':
1055 return BitXor;
1056 case '|':
1057 return BitOr;
1058 case '*':
1059 if (STR(n)[1] == '*')
1060 return Pow;
1061 else
1062 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001063 case '@':
1064 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001066 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 }
1069}
1070
1071static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001072ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001074 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 |'is' 'not'
1076 */
1077 REQ(n, comp_op);
1078 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001079 n = CHILD(n, 0);
1080 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 case LESS:
1082 return Lt;
1083 case GREATER:
1084 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001085 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return Eq;
1087 case LESSEQUAL:
1088 return LtE;
1089 case GREATEREQUAL:
1090 return GtE;
1091 case NOTEQUAL:
1092 return NotEq;
1093 case NAME:
1094 if (strcmp(STR(n), "in") == 0)
1095 return In;
1096 if (strcmp(STR(n), "is") == 0)
1097 return Is;
1098 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001099 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 }
1104 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105 /* handle "not in" and "is not" */
1106 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 case NAME:
1108 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1109 return NotIn;
1110 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1111 return IsNot;
1112 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001113 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 }
Neal Norwitz79792652005-11-14 04:25:03 +00001118 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121}
1122
1123static asdl_seq *
1124seq_for_testlist(struct compiling *c, const node *n)
1125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001127 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1128 */
Armin Rigo31441302005-10-21 12:57:31 +00001129 asdl_seq *seq;
1130 expr_ty expression;
1131 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001132 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001134 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 if (!seq)
1136 return NULL;
1137
1138 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001140 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141
Benjamin Peterson4905e802009-09-27 02:43:28 +00001142 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145
1146 assert(i / 2 < seq->size);
1147 asdl_seq_SET(seq, i / 2, expression);
1148 }
1149 return seq;
1150}
1151
Neal Norwitzc1505362006-12-28 06:47:50 +00001152static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001153ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001154{
1155 identifier name;
1156 expr_ty annotation = NULL;
1157 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001158 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001159
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001160 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001161 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001162 name = NEW_IDENTIFIER(ch);
1163 if (!name)
1164 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001165 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001166 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001167
1168 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1169 annotation = ast_for_expr(c, CHILD(n, 2));
1170 if (!annotation)
1171 return NULL;
1172 }
1173
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001174 ret = arg(name, annotation, c->c_arena);
1175 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001176 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001177 ret->lineno = LINENO(n);
1178 ret->col_offset = n->n_col_offset;
1179 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180}
1181
Guido van Rossum4f72a782006-10-27 23:31:49 +00001182/* returns -1 if failed to handle keyword only arguments
1183 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001184 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001185 ^^^
1186 start pointing here
1187 */
1188static int
1189handle_keywordonly_args(struct compiling *c, const node *n, int start,
1190 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1191{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001192 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001193 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001194 expr_ty expression, annotation;
1195 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001196 int i = start;
1197 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001198
1199 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001200 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001201 return -1;
1202 }
1203 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001204 while (i < NCH(n)) {
1205 ch = CHILD(n, i);
1206 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001207 case vfpdef:
1208 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001209 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001210 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001211 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001212 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001213 asdl_seq_SET(kwdefaults, j, expression);
1214 i += 2; /* '=' and test */
1215 }
1216 else { /* setting NULL if no default value exists */
1217 asdl_seq_SET(kwdefaults, j, NULL);
1218 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 if (NCH(ch) == 3) {
1220 /* ch is NAME ':' test */
1221 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001222 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001223 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001224 }
1225 else {
1226 annotation = NULL;
1227 }
1228 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001229 argname = NEW_IDENTIFIER(ch);
1230 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001231 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001232 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001233 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001234 arg = arg(argname, annotation, c->c_arena);
1235 if (!arg)
1236 goto error;
Benjamin Peterson0714b8b2014-02-13 19:22:14 -05001237 arg->lineno = LINENO(ch);
1238 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001239 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 i += 2; /* the name and the comma */
1241 break;
1242 case DOUBLESTAR:
1243 return i;
1244 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001245 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 goto error;
1247 }
1248 }
1249 return i;
1250 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001252}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253
Jeremy Hyltona8293132006-02-28 17:58:27 +00001254/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255
1256static arguments_ty
1257ast_for_arguments(struct compiling *c, const node *n)
1258{
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 /* This function handles both typedargslist (function definition)
1260 and varargslist (lambda definition).
1261
1262 parameters: '(' [typedargslist] ')'
1263 typedargslist: ((tfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001265 | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001266 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001267 tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001268 varargslist: ((vfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001270 | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001271 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001274 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1275 int nposdefaults = 0, found_default = 0;
1276 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001277 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 node *ch;
1280
1281 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001283 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001286 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287
Jeremy Hyltone921e022008-07-17 16:37:17 +00001288 /* First count the number of positional args & defaults. The
1289 variable i is the loop index for this for loop and the next.
1290 The next loop picks up where the first leaves off.
1291 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 ch = CHILD(n, i);
1294 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001295 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001296 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001297 if (i < NCH(n) && /* skip argument following star */
1298 (TYPE(CHILD(n, i)) == tfpdef ||
1299 TYPE(CHILD(n, i)) == vfpdef)) {
1300 i++;
1301 }
1302 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001303 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001304 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001305 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001309 defaults for keyword only args */
1310 for ( ; i < NCH(n); ++i) {
1311 ch = CHILD(n, i);
1312 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001313 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001315 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001316 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001317 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001319 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001321 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001323 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001325 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 since we set NULL as default for keyword only argument w/o default
1328 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001329 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001330 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001332 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333
1334 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001335 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001339 /* tfpdef: NAME [':' test]
1340 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 */
1342 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001343 j = 0; /* index for defaults */
1344 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 ch = CHILD(n, i);
1347 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001348 case tfpdef:
1349 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1351 anything other than EQUAL or a comma? */
1352 /* XXX Should NCH(n) check be made a separate check? */
1353 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001354 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1355 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001356 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 assert(posdefaults != NULL);
1358 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001363 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001365 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001367 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001369 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 i += 2; /* the name and the comma */
1372 break;
1373 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 if (i+1 >= NCH(n)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001375 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001376 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 if (TYPE(ch) == COMMA) {
1381 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 i += 2; /* now follows keyword only arguments */
1383 res = handle_keywordonly_args(c, n, i,
1384 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 i = res; /* res has new position to process */
1387 }
1388 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001389 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001390 if (!vararg)
1391 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001392
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1395 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 int res = 0;
1397 res = handle_keywordonly_args(c, n, i,
1398 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001399 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 i = res; /* res has new position to process */
1401 }
1402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 break;
1404 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001405 ch = CHILD(n, i+1); /* tfpdef */
1406 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001407 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001408 if (!kwarg)
1409 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 i += 3;
1411 break;
1412 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001413 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 "unexpected node in varargslist: %d @ %d",
1415 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001416 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001419 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420}
1421
1422static expr_ty
1423ast_for_dotted_name(struct compiling *c, const node *n)
1424{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001425 expr_ty e;
1426 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 int i;
1429
1430 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001431
1432 lineno = LINENO(n);
1433 col_offset = n->n_col_offset;
1434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 id = NEW_IDENTIFIER(CHILD(n, 0));
1436 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001437 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001438 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441
1442 for (i = 2; i < NCH(n); i+=2) {
1443 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001444 if (!id)
1445 return NULL;
1446 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1447 if (!e)
1448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 }
1450
1451 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452}
1453
1454static expr_ty
1455ast_for_decorator(struct compiling *c, const node *n)
1456{
1457 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1458 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001459 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001462 REQ(CHILD(n, 0), AT);
1463 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1466 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001467 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001470 d = name_expr;
1471 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 }
1473 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001474 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001476 if (!d)
1477 return NULL;
1478 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 }
1480 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001481 d = ast_for_call(c, CHILD(n, 3), name_expr);
1482 if (!d)
1483 return NULL;
1484 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 }
1486
1487 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490static asdl_seq*
1491ast_for_decorators(struct compiling *c, const node *n)
1492{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001493 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001494 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001498 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 if (!decorator_seq)
1500 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001503 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001504 if (!d)
1505 return NULL;
1506 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 }
1508 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
1511static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001512ast_for_funcdef_impl(struct compiling *c, const node *n,
1513 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001515 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001516 identifier name;
1517 arguments_ty args;
1518 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001519 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001520 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521
1522 REQ(n, funcdef);
1523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 name = NEW_IDENTIFIER(CHILD(n, name_i));
1525 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001527 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1530 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001531 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001532 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1533 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1534 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001535 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001536 name_i += 2;
1537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 body = ast_for_suite(c, CHILD(n, name_i + 3));
1539 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541
Yury Selivanov75445082015-05-11 22:57:16 -04001542 if (is_async)
1543 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1544 LINENO(n),
1545 n->n_col_offset, c->c_arena);
1546 else
1547 return FunctionDef(name, args, body, decorator_seq, returns,
1548 LINENO(n),
1549 n->n_col_offset, c->c_arena);
1550}
1551
1552static stmt_ty
1553ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1554{
1555 /* async_funcdef: ASYNC funcdef */
1556 REQ(n, async_funcdef);
1557 REQ(CHILD(n, 0), ASYNC);
1558 REQ(CHILD(n, 1), funcdef);
1559
1560 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1561 1 /* is_async */);
1562}
1563
1564static stmt_ty
1565ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1566{
1567 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1568 return ast_for_funcdef_impl(c, n, decorator_seq,
1569 0 /* is_async */);
1570}
1571
1572
1573static stmt_ty
1574ast_for_async_stmt(struct compiling *c, const node *n)
1575{
1576 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1577 REQ(n, async_stmt);
1578 REQ(CHILD(n, 0), ASYNC);
1579
1580 switch (TYPE(CHILD(n, 1))) {
1581 case funcdef:
1582 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1583 1 /* is_async */);
1584 case with_stmt:
1585 return ast_for_with_stmt(c, CHILD(n, 1),
1586 1 /* is_async */);
1587
1588 case for_stmt:
1589 return ast_for_for_stmt(c, CHILD(n, 1),
1590 1 /* is_async */);
1591
1592 default:
1593 PyErr_Format(PyExc_SystemError,
1594 "invalid async stament: %s",
1595 STR(CHILD(n, 1)));
1596 return NULL;
1597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001600static stmt_ty
1601ast_for_decorated(struct compiling *c, const node *n)
1602{
Yury Selivanov75445082015-05-11 22:57:16 -04001603 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001604 stmt_ty thing = NULL;
1605 asdl_seq *decorator_seq = NULL;
1606
1607 REQ(n, decorated);
1608
1609 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1610 if (!decorator_seq)
1611 return NULL;
1612
1613 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001614 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001616
1617 if (TYPE(CHILD(n, 1)) == funcdef) {
1618 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1619 } else if (TYPE(CHILD(n, 1)) == classdef) {
1620 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001621 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1622 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001623 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001624 /* we count the decorators in when talking about the class' or
1625 * function's line number */
1626 if (thing) {
1627 thing->lineno = LINENO(n);
1628 thing->col_offset = n->n_col_offset;
1629 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001630 return thing;
1631}
1632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633static expr_ty
1634ast_for_lambdef(struct compiling *c, const node *n)
1635{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001636 /* lambdef: 'lambda' [varargslist] ':' test
1637 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638 arguments_ty args;
1639 expr_ty expression;
1640
1641 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001642 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (!args)
1644 return NULL;
1645 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001646 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 }
1649 else {
1650 args = ast_for_arguments(c, CHILD(n, 1));
1651 if (!args)
1652 return NULL;
1653 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001654 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 }
1657
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001658 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659}
1660
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001661static expr_ty
1662ast_for_ifexpr(struct compiling *c, const node *n)
1663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001665 expr_ty expression, body, orelse;
1666
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001667 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001668 body = ast_for_expr(c, CHILD(n, 0));
1669 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001670 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001671 expression = ast_for_expr(c, CHILD(n, 2));
1672 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001673 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001674 orelse = ast_for_expr(c, CHILD(n, 4));
1675 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001676 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001677 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1678 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001679}
1680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001682 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683
Nick Coghlan650f0d02007-04-15 12:05:43 +00001684 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685*/
1686
1687static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001688count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001690 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
Guido van Rossumd8faa362007-04-27 19:54:29 +00001692 count_comp_for:
1693 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001694 REQ(n, comp_for);
1695 if (NCH(n) == 5)
1696 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001697 else
1698 return n_fors;
1699 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001700 REQ(n, comp_iter);
1701 n = CHILD(n, 0);
1702 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001703 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001704 else if (TYPE(n) == comp_if) {
1705 if (NCH(n) == 3) {
1706 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001707 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001708 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001709 else
1710 return n_fors;
1711 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001712
Guido van Rossumd8faa362007-04-27 19:54:29 +00001713 /* Should never be reached */
1714 PyErr_SetString(PyExc_SystemError,
1715 "logic error in count_comp_fors");
1716 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717}
1718
Nick Coghlan650f0d02007-04-15 12:05:43 +00001719/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720
Nick Coghlan650f0d02007-04-15 12:05:43 +00001721 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722*/
1723
1724static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001725count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001727 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728
Guido van Rossumd8faa362007-04-27 19:54:29 +00001729 while (1) {
1730 REQ(n, comp_iter);
1731 if (TYPE(CHILD(n, 0)) == comp_for)
1732 return n_ifs;
1733 n = CHILD(n, 0);
1734 REQ(n, comp_if);
1735 n_ifs++;
1736 if (NCH(n) == 2)
1737 return n_ifs;
1738 n = CHILD(n, 2);
1739 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740}
1741
Guido van Rossum992d4a32007-07-11 13:09:30 +00001742static asdl_seq *
1743ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001746 asdl_seq *comps;
1747
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001748 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 if (n_fors == -1)
1750 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001751
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001752 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001753 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001755
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001757 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001759 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761
Guido van Rossum992d4a32007-07-11 13:09:30 +00001762 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001765 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001766 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001768 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001769 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001771
Thomas Wouters89f507f2006-12-13 04:49:30 +00001772 /* Check the # of children rather than the length of t, since
1773 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001774 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001775 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001776 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001778 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1779 c->c_arena),
1780 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001781 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001783
Guido van Rossum992d4a32007-07-11 13:09:30 +00001784 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 int j, n_ifs;
1786 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787
Guido van Rossum992d4a32007-07-11 13:09:30 +00001788 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001789 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001790 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001793 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001796
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001798 REQ(n, comp_iter);
1799 n = CHILD(n, 0);
1800 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801
Guido van Rossum992d4a32007-07-11 13:09:30 +00001802 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001803 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001804 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001805 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001806 if (NCH(n) == 3)
1807 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001809 /* on exit, must guarantee that n is a comp_for */
1810 if (TYPE(n) == comp_iter)
1811 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001812 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001814 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001816 return comps;
1817}
1818
1819static expr_ty
1820ast_for_itercomp(struct compiling *c, const node *n, int type)
1821{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001822 /* testlist_comp: (test|star_expr)
1823 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001824 expr_ty elt;
1825 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001826 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827
Guido van Rossum992d4a32007-07-11 13:09:30 +00001828 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001830 ch = CHILD(n, 0);
1831 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001832 if (!elt)
1833 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001834 if (elt->kind == Starred_kind) {
1835 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1836 return NULL;
1837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838
Guido van Rossum992d4a32007-07-11 13:09:30 +00001839 comps = ast_for_comprehension(c, CHILD(n, 1));
1840 if (!comps)
1841 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001842
1843 if (type == COMP_GENEXP)
1844 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1845 else if (type == COMP_LISTCOMP)
1846 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1847 else if (type == COMP_SETCOMP)
1848 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1849 else
1850 /* Should never happen */
1851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852}
1853
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001854/* Fills in the key, value pair corresponding to the dict element. In case
1855 * of an unpacking, key is NULL. *i is advanced by the number of ast
1856 * elements. Iff successful, nonzero is returned.
1857 */
1858static int
1859ast_for_dictelement(struct compiling *c, const node *n, int *i,
1860 expr_ty *key, expr_ty *value)
1861{
1862 expr_ty expression;
1863 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1864 assert(NCH(n) - *i >= 2);
1865
1866 expression = ast_for_expr(c, CHILD(n, *i + 1));
1867 if (!expression)
1868 return 0;
1869 *key = NULL;
1870 *value = expression;
1871
1872 *i += 2;
1873 }
1874 else {
1875 assert(NCH(n) - *i >= 3);
1876
1877 expression = ast_for_expr(c, CHILD(n, *i));
1878 if (!expression)
1879 return 0;
1880 *key = expression;
1881
1882 REQ(CHILD(n, *i + 1), COLON);
1883
1884 expression = ast_for_expr(c, CHILD(n, *i + 2));
1885 if (!expression)
1886 return 0;
1887 *value = expression;
1888
1889 *i += 3;
1890 }
1891 return 1;
1892}
1893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001895ast_for_dictcomp(struct compiling *c, const node *n)
1896{
1897 expr_ty key, value;
1898 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001899 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001901 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001902 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001903 assert(key);
1904 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001906 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 if (!comps)
1908 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909
Guido van Rossum992d4a32007-07-11 13:09:30 +00001910 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1911}
1912
1913static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914ast_for_dictdisplay(struct compiling *c, const node *n)
1915{
1916 int i;
1917 int j;
1918 int size;
1919 asdl_seq *keys, *values;
1920
1921 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1922 keys = _Py_asdl_seq_new(size, c->c_arena);
1923 if (!keys)
1924 return NULL;
1925
1926 values = _Py_asdl_seq_new(size, c->c_arena);
1927 if (!values)
1928 return NULL;
1929
1930 j = 0;
1931 for (i = 0; i < NCH(n); i++) {
1932 expr_ty key, value;
1933
1934 if (!ast_for_dictelement(c, n, &i, &key, &value))
1935 return NULL;
1936 asdl_seq_SET(keys, j, key);
1937 asdl_seq_SET(values, j, value);
1938
1939 j++;
1940 }
1941 keys->size = j;
1942 values->size = j;
1943 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1944}
1945
1946static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001947ast_for_genexp(struct compiling *c, const node *n)
1948{
1949 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001950 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001951}
1952
1953static expr_ty
1954ast_for_listcomp(struct compiling *c, const node *n)
1955{
1956 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001957 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001958}
1959
1960static expr_ty
1961ast_for_setcomp(struct compiling *c, const node *n)
1962{
1963 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001964 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001965}
1966
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001967static expr_ty
1968ast_for_setdisplay(struct compiling *c, const node *n)
1969{
1970 int i;
1971 int size;
1972 asdl_seq *elts;
1973
1974 assert(TYPE(n) == (dictorsetmaker));
1975 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
1976 elts = _Py_asdl_seq_new(size, c->c_arena);
1977 if (!elts)
1978 return NULL;
1979 for (i = 0; i < NCH(n); i += 2) {
1980 expr_ty expression;
1981 expression = ast_for_expr(c, CHILD(n, i));
1982 if (!expression)
1983 return NULL;
1984 asdl_seq_SET(elts, i / 2, expression);
1985 }
1986 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1987}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001988
1989static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990ast_for_atom(struct compiling *c, const node *n)
1991{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001992 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1993 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001994 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 */
1996 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001997 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002000 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002001 PyObject *name;
2002 const char *s = STR(ch);
2003 size_t len = strlen(s);
2004 if (len >= 4 && len <= 5) {
2005 if (!strcmp(s, "None"))
2006 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2007 if (!strcmp(s, "True"))
2008 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2009 if (!strcmp(s, "False"))
2010 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2011 }
2012 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002013 if (!name)
2014 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002015 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002016 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2017 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00002019 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002020 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002021 const char *errtype = NULL;
2022 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2023 errtype = "unicode error";
2024 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2025 errtype = "value error";
2026 if (errtype) {
2027 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002028 PyObject *type, *value, *tback, *errstr;
2029 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002030 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002031 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002032 char *s = _PyUnicode_AsString(errstr);
2033 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002034 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002035 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002036 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002037 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002038 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002039 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002040 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002041 Py_XDECREF(tback);
2042 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002043 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002044 }
Victor Stinner43d81952013-07-17 00:57:58 +02002045 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2046 Py_DECREF(str);
2047 return NULL;
2048 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00002049 if (bytesmode)
2050 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
2051 else
2052 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 }
2054 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002055 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002056 if (!pynum)
2057 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002058
Victor Stinner43d81952013-07-17 00:57:58 +02002059 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2060 Py_DECREF(pynum);
2061 return NULL;
2062 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002063 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 }
Georg Brandldde00282007-03-18 19:01:53 +00002065 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002066 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002068 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069
Thomas Wouters89f507f2006-12-13 04:49:30 +00002070 if (TYPE(ch) == RPAR)
2071 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072
Thomas Wouters89f507f2006-12-13 04:49:30 +00002073 if (TYPE(ch) == yield_expr)
2074 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002079
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002082 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083
Thomas Wouters89f507f2006-12-13 04:49:30 +00002084 if (TYPE(ch) == RSQB)
2085 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002088 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2089 asdl_seq *elts = seq_for_testlist(c, ch);
2090 if (!elts)
2091 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002092
Thomas Wouters89f507f2006-12-13 04:49:30 +00002093 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2094 }
2095 else
2096 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002098 /* dictorsetmaker: ( ((test ':' test | '**' test)
2099 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2100 * ((test | '*' test)
2101 * (comp_for | (',' (test | '*' test))* [','])) ) */
Neal Norwitzc1505362006-12-28 06:47:50 +00002102 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002103 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002104 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002105 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002106 }
2107 else {
2108 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2109 if (NCH(ch) == 1 ||
2110 (NCH(ch) > 1 &&
2111 TYPE(CHILD(ch, 1)) == COMMA)) {
2112 /* It's a set display. */
2113 return ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002114 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002115 else if (NCH(ch) > 1 &&
2116 TYPE(CHILD(ch, 1)) == comp_for) {
2117 /* It's a set comprehension. */
2118 return ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002119 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002120 else if (NCH(ch) > 3 - is_dict &&
2121 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2122 /* It's a dictionary comprehension. */
2123 if (is_dict) {
2124 ast_error(c, n, "dict unpacking cannot be used in "
2125 "dict comprehension");
2126 return NULL;
2127 }
2128 return ast_for_dictcomp(c, ch);
2129 }
2130 else {
2131 /* It's a dictionary display. */
2132 return ast_for_dictdisplay(c, ch);
2133 }
Guido van Rossum86e58e22006-08-28 15:27:34 +00002134 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 }
2140}
2141
2142static slice_ty
2143ast_for_slice(struct compiling *c, const node *n)
2144{
2145 node *ch;
2146 expr_ty lower = NULL, upper = NULL, step = NULL;
2147
2148 REQ(n, subscript);
2149
2150 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002151 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 sliceop: ':' [test]
2153 */
2154 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 if (NCH(n) == 1 && TYPE(ch) == test) {
2156 /* 'step' variable hold no significance in terms of being used over
2157 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 if (!step)
2160 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 }
2164
2165 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 if (!lower)
2168 return NULL;
2169 }
2170
2171 /* If there's an upper bound it's in the second or third position. */
2172 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002173 if (NCH(n) > 1) {
2174 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 if (TYPE(n2) == test) {
2177 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 if (!upper)
2179 return NULL;
2180 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002183 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184
Thomas Wouters89f507f2006-12-13 04:49:30 +00002185 if (TYPE(n2) == test) {
2186 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 if (!upper)
2188 return NULL;
2189 }
2190 }
2191
2192 ch = CHILD(n, NCH(n) - 1);
2193 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002194 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002195 ch = CHILD(ch, 1);
2196 if (TYPE(ch) == test) {
2197 step = ast_for_expr(c, ch);
2198 if (!step)
2199 return NULL;
2200 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 }
2202 }
2203
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002204 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205}
2206
2207static expr_ty
2208ast_for_binop(struct compiling *c, const node *n)
2209{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002210 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002212 BinOp(BinOp(A, op, B), op, C).
2213 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214
Guido van Rossumd8faa362007-04-27 19:54:29 +00002215 int i, nops;
2216 expr_ty expr1, expr2, result;
2217 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218
Guido van Rossumd8faa362007-04-27 19:54:29 +00002219 expr1 = ast_for_expr(c, CHILD(n, 0));
2220 if (!expr1)
2221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Guido van Rossumd8faa362007-04-27 19:54:29 +00002223 expr2 = ast_for_expr(c, CHILD(n, 2));
2224 if (!expr2)
2225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226
Guido van Rossumd8faa362007-04-27 19:54:29 +00002227 newoperator = get_operator(CHILD(n, 1));
2228 if (!newoperator)
2229 return NULL;
2230
2231 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2232 c->c_arena);
2233 if (!result)
2234 return NULL;
2235
2236 nops = (NCH(n) - 1) / 2;
2237 for (i = 1; i < nops; i++) {
2238 expr_ty tmp_result, tmp;
2239 const node* next_oper = CHILD(n, i * 2 + 1);
2240
2241 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 return NULL;
2244
Guido van Rossumd8faa362007-04-27 19:54:29 +00002245 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2246 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 return NULL;
2248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002250 LINENO(next_oper), next_oper->n_col_offset,
2251 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002253 return NULL;
2254 result = tmp_result;
2255 }
2256 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257}
2258
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002259static expr_ty
2260ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002263 subscriptlist: subscript (',' subscript)* [',']
2264 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2265 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002266 REQ(n, trailer);
2267 if (TYPE(CHILD(n, 0)) == LPAR) {
2268 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002269 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002271 else
2272 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002273 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002274 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002275 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2276 if (!attr_id)
2277 return NULL;
2278 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002279 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002280 }
2281 else {
2282 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002283 REQ(CHILD(n, 2), RSQB);
2284 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002285 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002286 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2287 if (!slc)
2288 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2290 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002291 }
2292 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002294 by treating the sequence as a tuple literal if there are
2295 no slice features.
2296 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002297 int j;
2298 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002299 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002300 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002301 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002302 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002303 if (!slices)
2304 return NULL;
2305 for (j = 0; j < NCH(n); j += 2) {
2306 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002307 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002308 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002309 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002310 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002311 asdl_seq_SET(slices, j / 2, slc);
2312 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002313 if (!simple) {
2314 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002315 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002316 }
2317 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002318 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002319 if (!elts)
2320 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002321 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2322 slc = (slice_ty)asdl_seq_GET(slices, j);
2323 assert(slc->kind == Index_kind && slc->v.Index.value);
2324 asdl_seq_SET(elts, j, slc->v.Index.value);
2325 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002326 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002327 if (!e)
2328 return NULL;
2329 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002330 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002331 }
2332 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002333}
2334
2335static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002336ast_for_factor(struct compiling *c, const node *n)
2337{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002338 expr_ty expression;
2339
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002340 expression = ast_for_expr(c, CHILD(n, 1));
2341 if (!expression)
2342 return NULL;
2343
2344 switch (TYPE(CHILD(n, 0))) {
2345 case PLUS:
2346 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2347 c->c_arena);
2348 case MINUS:
2349 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2350 c->c_arena);
2351 case TILDE:
2352 return UnaryOp(Invert, expression, LINENO(n),
2353 n->n_col_offset, c->c_arena);
2354 }
2355 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2356 TYPE(CHILD(n, 0)));
2357 return NULL;
2358}
2359
2360static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002361ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002362{
Yury Selivanov75445082015-05-11 22:57:16 -04002363 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002365
2366 REQ(n, atom_expr);
2367 nch = NCH(n);
2368
2369 if (TYPE(CHILD(n, 0)) == AWAIT) {
2370 start = 1;
2371 assert(nch > 1);
2372 }
2373
2374 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 if (!e)
2376 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002377 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002378 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002379 if (start && nch == 2) {
2380 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2381 }
2382
2383 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002384 node *ch = CHILD(n, i);
2385 if (TYPE(ch) != trailer)
2386 break;
2387 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002388 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002389 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002390 tmp->lineno = e->lineno;
2391 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 e = tmp;
2393 }
Yury Selivanov75445082015-05-11 22:57:16 -04002394
2395 if (start) {
2396 /* there was an AWAIT */
2397 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2398 }
2399 else {
2400 return e;
2401 }
2402}
2403
2404static expr_ty
2405ast_for_power(struct compiling *c, const node *n)
2406{
2407 /* power: atom trailer* ('**' factor)*
2408 */
2409 expr_ty e;
2410 REQ(n, power);
2411 e = ast_for_atom_expr(c, CHILD(n, 0));
2412 if (!e)
2413 return NULL;
2414 if (NCH(n) == 1)
2415 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002416 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2417 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002418 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002419 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002420 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002421 }
2422 return e;
2423}
2424
Guido van Rossum0368b722007-05-11 16:50:42 +00002425static expr_ty
2426ast_for_starred(struct compiling *c, const node *n)
2427{
2428 expr_ty tmp;
2429 REQ(n, star_expr);
2430
2431 tmp = ast_for_expr(c, CHILD(n, 1));
2432 if (!tmp)
2433 return NULL;
2434
2435 /* The Load context is changed later. */
2436 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2437}
2438
2439
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440/* Do not name a variable 'expr'! Will cause a compile error.
2441*/
2442
2443static expr_ty
2444ast_for_expr(struct compiling *c, const node *n)
2445{
2446 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002447 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002448 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 and_test: not_test ('and' not_test)*
2451 not_test: 'not' not_test | comparison
2452 comparison: expr (comp_op expr)*
2453 expr: xor_expr ('|' xor_expr)*
2454 xor_expr: and_expr ('^' and_expr)*
2455 and_expr: shift_expr ('&' shift_expr)*
2456 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2457 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002458 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002460 power: atom_expr ['**' factor]
2461 atom_expr: [AWAIT] atom trailer*
2462 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 */
2464
2465 asdl_seq *seq;
2466 int i;
2467
2468 loop:
2469 switch (TYPE(n)) {
2470 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002471 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002472 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002473 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002475 else if (NCH(n) > 1)
2476 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 /* Fallthrough */
2478 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 case and_test:
2480 if (NCH(n) == 1) {
2481 n = CHILD(n, 0);
2482 goto loop;
2483 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002484 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 if (!seq)
2486 return NULL;
2487 for (i = 0; i < NCH(n); i += 2) {
2488 expr_ty e = ast_for_expr(c, CHILD(n, i));
2489 if (!e)
2490 return NULL;
2491 asdl_seq_SET(seq, i / 2, e);
2492 }
2493 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002494 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2495 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002496 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002497 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 case not_test:
2499 if (NCH(n) == 1) {
2500 n = CHILD(n, 0);
2501 goto loop;
2502 }
2503 else {
2504 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2505 if (!expression)
2506 return NULL;
2507
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002508 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2509 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
2511 case comparison:
2512 if (NCH(n) == 1) {
2513 n = CHILD(n, 0);
2514 goto loop;
2515 }
2516 else {
2517 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002518 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002519 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002520 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 if (!ops)
2522 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002523 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 return NULL;
2526 }
2527 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002528 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002530 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002531 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534
2535 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002536 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002540 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 asdl_seq_SET(cmps, i / 2, expression);
2542 }
2543 expression = ast_for_expr(c, CHILD(n, 0));
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 return Compare(expression, ops, cmps, LINENO(n),
2549 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 }
2551 break;
2552
Guido van Rossum0368b722007-05-11 16:50:42 +00002553 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 /* The next five cases all handle BinOps. The main body of code
2556 is the same in each case, but the switch turned inside out to
2557 reuse the code for each type of operator.
2558 */
2559 case expr:
2560 case xor_expr:
2561 case and_expr:
2562 case shift_expr:
2563 case arith_expr:
2564 case term:
2565 if (NCH(n) == 1) {
2566 n = CHILD(n, 0);
2567 goto loop;
2568 }
2569 return ast_for_binop(c, n);
2570 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002571 node *an = NULL;
2572 node *en = NULL;
2573 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002574 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002575 if (NCH(n) > 1)
2576 an = CHILD(n, 1); /* yield_arg */
2577 if (an) {
2578 en = CHILD(an, NCH(an) - 1);
2579 if (NCH(an) == 2) {
2580 is_from = 1;
2581 exp = ast_for_expr(c, en);
2582 }
2583 else
2584 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002585 if (!exp)
2586 return NULL;
2587 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002588 if (is_from)
2589 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2590 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002591 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002592 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 if (NCH(n) == 1) {
2594 n = CHILD(n, 0);
2595 goto loop;
2596 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002597 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002598 case power:
2599 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002601 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
2603 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002604 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 return NULL;
2606}
2607
2608static expr_ty
2609ast_for_call(struct compiling *c, const node *n, expr_ty func)
2610{
2611 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002612 arglist: argument (',' argument)* [',']
2613 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 */
2615
2616 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002617 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618 asdl_seq *args;
2619 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
2621 REQ(n, arglist);
2622
2623 nargs = 0;
2624 nkeywords = 0;
2625 ngens = 0;
2626 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002627 node *ch = CHILD(n, i);
2628 if (TYPE(ch) == argument) {
2629 if (NCH(ch) == 1)
2630 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002631 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002633 else if (TYPE(CHILD(ch, 0)) == STAR)
2634 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002636 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 nkeywords++;
2638 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 }
2640 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002641 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642 "if not sole argument");
2643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645
2646 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002647 ast_error(c, n, "more than 255 arguments");
2648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002651 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002653 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002654 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002656 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002657
2658 nargs = 0; /* positional arguments + iterable argument unpackings */
2659 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2660 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002662 node *ch = CHILD(n, i);
2663 if (TYPE(ch) == argument) {
2664 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002665 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002666 if (NCH(ch) == 1) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002667 if (TYPE(chch) == star_expr) {
2668 /* an iterable argument unpacking */
2669 expr_ty starred;
2670 if (ndoublestars) {
2671 ast_error(c, chch,
2672 "iterable argument unpacking follows "
2673 "keyword argument unpacking");
2674 return NULL;
2675 }
2676 e = ast_for_expr(c, CHILD(chch, 1));
2677 if (!e)
2678 return NULL;
2679 starred = Starred(e, Load, LINENO(chch),
2680 chch->n_col_offset,
2681 c->c_arena);
2682 if (!starred)
2683 return NULL;
2684 asdl_seq_SET(args, nargs++, starred);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002686 else {
2687 /* a positional argument */
2688 if (nkeywords) {
2689 if (ndoublestars) {
2690 ast_error(c, chch,
2691 "positional argument follows "
2692 "keyword argument unpacking");
2693 }
2694 else {
2695 ast_error(c, chch,
2696 "positional argument follows "
2697 "keyword argument");
2698 }
2699 return NULL;
2700 }
2701 e = ast_for_expr(c, chch);
2702 if (!e)
2703 return NULL;
2704 asdl_seq_SET(args, nargs++, e);
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002705 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002706 }
2707 else if (TYPE(chch) == DOUBLESTAR) {
2708 /* a keyword argument unpacking */
2709 keyword_ty kw;
2710 i++;
2711 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002713 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002714 kw = keyword(NULL, e, c->c_arena);
2715 asdl_seq_SET(keywords, nkeywords++, kw);
2716 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002718 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002720 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002722 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002725 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002728 identifier key, tmp;
2729 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 /* chch is test, but must be an identifier? */
2732 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 /* f(lambda x: x[0] = 3) ends up getting parsed with
2736 * LHS test = lambda x: x[0], and RHS test = 3.
2737 * SF bug 132313 points out that complaining about a keyword
2738 * then is very confusing.
2739 */
2740 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002741 ast_error(c, chch,
2742 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002743 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 }
2745 else if (e->kind != Name_kind) {
2746 ast_error(c, chch,
2747 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002748 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002749 }
2750 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002751 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002753 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002754 for (k = 0; k < nkeywords; k++) {
2755 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002756 if (tmp && !PyUnicode_Compare(tmp, key)) {
2757 ast_error(c, chch,
2758 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002759 return NULL;
2760 }
2761 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002762 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002764 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002765 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002767 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002768 asdl_seq_SET(keywords, nkeywords++, kw);
2769 }
2770 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 }
2772
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774}
2775
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002777ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002779 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002780 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002782 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002783 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002784 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002785 }
2786 else {
2787 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002788 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002791 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 else {
2793 asdl_seq *tmp = seq_for_testlist(c, n);
2794 if (!tmp)
2795 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002796 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002798}
2799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800static stmt_ty
2801ast_for_expr_stmt(struct compiling *c, const node *n)
2802{
2803 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002806 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002807 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002808 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 test: ... here starts the operator precendence dance
2810 */
2811
2812 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002813 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 if (!e)
2815 return NULL;
2816
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 }
2819 else if (TYPE(CHILD(n, 1)) == augassign) {
2820 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002821 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002822 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823
Thomas Wouters89f507f2006-12-13 04:49:30 +00002824 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 if (!expr1)
2826 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002827 if(!set_context(c, expr1, Store, ch))
2828 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002829 /* set_context checks that most expressions are not the left side.
2830 Augmented assignments can only have a name, a subscript, or an
2831 attribute on the left, though, so we have to explicitly check for
2832 those. */
2833 switch (expr1->kind) {
2834 case Name_kind:
2835 case Attribute_kind:
2836 case Subscript_kind:
2837 break;
2838 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002839 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002840 return NULL;
2841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842
Thomas Wouters89f507f2006-12-13 04:49:30 +00002843 ch = CHILD(n, 2);
2844 if (TYPE(ch) == testlist)
2845 expr2 = ast_for_testlist(c, ch);
2846 else
2847 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002848 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 return NULL;
2850
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002851 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 return NULL;
2854
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 }
2857 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002858 int i;
2859 asdl_seq *targets;
2860 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 expr_ty expression;
2862
Thomas Wouters89f507f2006-12-13 04:49:30 +00002863 /* a normal assignment */
2864 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002865 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002866 if (!targets)
2867 return NULL;
2868 for (i = 0; i < NCH(n) - 2; i += 2) {
2869 expr_ty e;
2870 node *ch = CHILD(n, i);
2871 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002872 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 return NULL;
2874 }
2875 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002879 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002880 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Thomas Wouters89f507f2006-12-13 04:49:30 +00002883 asdl_seq_SET(targets, i / 2, e);
2884 }
2885 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002886 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002887 expression = ast_for_testlist(c, value);
2888 else
2889 expression = ast_for_expr(c, value);
2890 if (!expression)
2891 return NULL;
2892 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894}
2895
Benjamin Peterson78565b22009-06-28 19:19:51 +00002896
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002898ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899{
2900 asdl_seq *seq;
2901 int i;
2902 expr_ty e;
2903
2904 REQ(n, exprlist);
2905
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002906 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002910 e = ast_for_expr(c, CHILD(n, i));
2911 if (!e)
2912 return NULL;
2913 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002914 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 }
2917 return seq;
2918}
2919
2920static stmt_ty
2921ast_for_del_stmt(struct compiling *c, const node *n)
2922{
2923 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 /* del_stmt: 'del' exprlist */
2926 REQ(n, del_stmt);
2927
2928 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2929 if (!expr_list)
2930 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002931 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932}
2933
2934static stmt_ty
2935ast_for_flow_stmt(struct compiling *c, const node *n)
2936{
2937 /*
2938 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2939 | yield_stmt
2940 break_stmt: 'break'
2941 continue_stmt: 'continue'
2942 return_stmt: 'return' [testlist]
2943 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002944 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 raise_stmt: 'raise' [test [',' test [',' test]]]
2946 */
2947 node *ch;
2948
2949 REQ(n, flow_stmt);
2950 ch = CHILD(n, 0);
2951 switch (TYPE(ch)) {
2952 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002953 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002955 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002957 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2958 if (!exp)
2959 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002960 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 }
2962 case return_stmt:
2963 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002964 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002966 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967 if (!expression)
2968 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002969 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 }
2971 case raise_stmt:
2972 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002973 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2974 else if (NCH(ch) >= 2) {
2975 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2977 if (!expression)
2978 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002979 if (NCH(ch) == 4) {
2980 cause = ast_for_expr(c, CHILD(ch, 3));
2981 if (!cause)
2982 return NULL;
2983 }
2984 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 }
2986 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002987 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 "unexpected flow_stmt: %d", TYPE(ch));
2989 return NULL;
2990 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002991
2992 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994}
2995
2996static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00002997alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998{
2999 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003000 import_as_name: NAME ['as' NAME]
3001 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 dotted_name: NAME ('.' NAME)*
3003 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003004 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 loop:
3007 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003008 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003009 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003010 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003011 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003012 if (!name)
3013 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003014 if (NCH(n) == 3) {
3015 node *str_node = CHILD(n, 2);
3016 str = NEW_IDENTIFIER(str_node);
3017 if (!str)
3018 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003019 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003020 return NULL;
3021 }
3022 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003023 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003024 return NULL;
3025 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003026 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003027 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 case dotted_as_name:
3029 if (NCH(n) == 1) {
3030 n = CHILD(n, 0);
3031 goto loop;
3032 }
3033 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003034 node *asname_node = CHILD(n, 2);
3035 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003036 if (!a)
3037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003039 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003040 if (!a->asname)
3041 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003042 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 return a;
3045 }
3046 break;
3047 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003048 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003049 node *name_node = CHILD(n, 0);
3050 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003051 if (!name)
3052 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003053 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003054 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003055 return alias(name, NULL, c->c_arena);
3056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 else {
3058 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003059 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003060 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063
3064 len = 0;
3065 for (i = 0; i < NCH(n); i += 2)
3066 /* length of string plus one for the dot */
3067 len += strlen(STR(CHILD(n, i))) + 1;
3068 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003069 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 if (!str)
3071 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003072 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 if (!s)
3074 return NULL;
3075 for (i = 0; i < NCH(n); i += 2) {
3076 char *sch = STR(CHILD(n, i));
3077 strcpy(s, STR(CHILD(n, i)));
3078 s += strlen(sch);
3079 *s++ = '.';
3080 }
3081 --s;
3082 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3084 PyBytes_GET_SIZE(str),
3085 NULL);
3086 Py_DECREF(str);
3087 if (!uni)
3088 return NULL;
3089 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003090 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003091 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3092 Py_DECREF(str);
3093 return NULL;
3094 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003095 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096 }
3097 break;
3098 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003099 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003100 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3101 Py_DECREF(str);
3102 return NULL;
3103 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003104 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003106 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 "unexpected import name: %d", TYPE(n));
3108 return NULL;
3109 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003110
3111 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 return NULL;
3113}
3114
3115static stmt_ty
3116ast_for_import_stmt(struct compiling *c, const node *n)
3117{
3118 /*
3119 import_stmt: import_name | import_from
3120 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003121 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3122 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003124 int lineno;
3125 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 int i;
3127 asdl_seq *aliases;
3128
3129 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003130 lineno = LINENO(n);
3131 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003133 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003135 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003136 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003137 if (!aliases)
3138 return NULL;
3139 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003140 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003141 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003143 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003145 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003147 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003149 int idx, ndots = 0;
3150 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003151 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003153 /* Count the number of dots (for relative imports) and check for the
3154 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003155 for (idx = 1; idx < NCH(n); idx++) {
3156 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003157 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3158 if (!mod)
3159 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003160 idx++;
3161 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003162 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003164 ndots += 3;
3165 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003166 } else if (TYPE(CHILD(n, idx)) != DOT) {
3167 break;
3168 }
3169 ndots++;
3170 }
3171 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003172 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003173 case STAR:
3174 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003175 n = CHILD(n, idx);
3176 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003177 break;
3178 case LPAR:
3179 /* from ... import (x, y, z) */
3180 n = CHILD(n, idx + 1);
3181 n_children = NCH(n);
3182 break;
3183 case import_as_names:
3184 /* from ... import x, y, z */
3185 n = CHILD(n, idx);
3186 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003187 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003188 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 " surrounding parentheses");
3190 return NULL;
3191 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003192 break;
3193 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003194 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003195 return NULL;
3196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003198 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201
3202 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003203 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003205 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003207 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003209 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003210 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003211 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003212 if (!import_alias)
3213 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003214 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003215 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003217 if (mod != NULL)
3218 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003219 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003220 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 }
Neal Norwitz79792652005-11-14 04:25:03 +00003222 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 "unknown import statement: starts with command '%s'",
3224 STR(CHILD(n, 0)));
3225 return NULL;
3226}
3227
3228static stmt_ty
3229ast_for_global_stmt(struct compiling *c, const node *n)
3230{
3231 /* global_stmt: 'global' NAME (',' NAME)* */
3232 identifier name;
3233 asdl_seq *s;
3234 int i;
3235
3236 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003237 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003239 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003241 name = NEW_IDENTIFIER(CHILD(n, i));
3242 if (!name)
3243 return NULL;
3244 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003246 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247}
3248
3249static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003250ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3251{
3252 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3253 identifier name;
3254 asdl_seq *s;
3255 int i;
3256
3257 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003258 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003259 if (!s)
3260 return NULL;
3261 for (i = 1; i < NCH(n); i += 2) {
3262 name = NEW_IDENTIFIER(CHILD(n, i));
3263 if (!name)
3264 return NULL;
3265 asdl_seq_SET(s, i / 2, name);
3266 }
3267 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3268}
3269
3270static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271ast_for_assert_stmt(struct compiling *c, const node *n)
3272{
3273 /* assert_stmt: 'assert' test [',' test] */
3274 REQ(n, assert_stmt);
3275 if (NCH(n) == 2) {
3276 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3277 if (!expression)
3278 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003279 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 }
3281 else if (NCH(n) == 4) {
3282 expr_ty expr1, expr2;
3283
3284 expr1 = ast_for_expr(c, CHILD(n, 1));
3285 if (!expr1)
3286 return NULL;
3287 expr2 = ast_for_expr(c, CHILD(n, 3));
3288 if (!expr2)
3289 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290
Thomas Wouters89f507f2006-12-13 04:49:30 +00003291 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 }
Neal Norwitz79792652005-11-14 04:25:03 +00003293 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 "improper number of parts to 'assert' statement: %d",
3295 NCH(n));
3296 return NULL;
3297}
3298
3299static asdl_seq *
3300ast_for_suite(struct compiling *c, const node *n)
3301{
3302 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003303 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 stmt_ty s;
3305 int i, total, num, end, pos = 0;
3306 node *ch;
3307
3308 REQ(n, suite);
3309
3310 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003311 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003313 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003315 n = CHILD(n, 0);
3316 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 */
3319 end = NCH(n) - 1;
3320 if (TYPE(CHILD(n, end - 1)) == SEMI)
3321 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 for (i = 0; i < end; i += 2) {
3324 ch = CHILD(n, i);
3325 s = ast_for_stmt(c, ch);
3326 if (!s)
3327 return NULL;
3328 asdl_seq_SET(seq, pos++, s);
3329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 }
3331 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332 for (i = 2; i < (NCH(n) - 1); i++) {
3333 ch = CHILD(n, i);
3334 REQ(ch, stmt);
3335 num = num_stmts(ch);
3336 if (num == 1) {
3337 /* small_stmt or compound_stmt with only one child */
3338 s = ast_for_stmt(c, ch);
3339 if (!s)
3340 return NULL;
3341 asdl_seq_SET(seq, pos++, s);
3342 }
3343 else {
3344 int j;
3345 ch = CHILD(ch, 0);
3346 REQ(ch, simple_stmt);
3347 for (j = 0; j < NCH(ch); j += 2) {
3348 /* statement terminates with a semi-colon ';' */
3349 if (NCH(CHILD(ch, j)) == 0) {
3350 assert((j + 1) == NCH(ch));
3351 break;
3352 }
3353 s = ast_for_stmt(c, CHILD(ch, j));
3354 if (!s)
3355 return NULL;
3356 asdl_seq_SET(seq, pos++, s);
3357 }
3358 }
3359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360 }
3361 assert(pos == seq->size);
3362 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363}
3364
3365static stmt_ty
3366ast_for_if_stmt(struct compiling *c, const node *n)
3367{
3368 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3369 ['else' ':' suite]
3370 */
3371 char *s;
3372
3373 REQ(n, if_stmt);
3374
3375 if (NCH(n) == 4) {
3376 expr_ty expression;
3377 asdl_seq *suite_seq;
3378
3379 expression = ast_for_expr(c, CHILD(n, 1));
3380 if (!expression)
3381 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003383 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385
Guido van Rossumd8faa362007-04-27 19:54:29 +00003386 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3387 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003389
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 s = STR(CHILD(n, 4));
3391 /* s[2], the third character in the string, will be
3392 's' for el_s_e, or
3393 'i' for el_i_f
3394 */
3395 if (s[2] == 's') {
3396 expr_ty expression;
3397 asdl_seq *seq1, *seq2;
3398
3399 expression = ast_for_expr(c, CHILD(n, 1));
3400 if (!expression)
3401 return NULL;
3402 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003403 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404 return NULL;
3405 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003406 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 return NULL;
3408
Guido van Rossumd8faa362007-04-27 19:54:29 +00003409 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3410 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 }
3412 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003413 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003414 expr_ty expression;
3415 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003416 asdl_seq *orelse = NULL;
3417 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003418 /* must reference the child n_elif+1 since 'else' token is third,
3419 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003420 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3421 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3422 has_else = 1;
3423 n_elif -= 3;
3424 }
3425 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426
Thomas Wouters89f507f2006-12-13 04:49:30 +00003427 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003428 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003430 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003431 if (!orelse)
3432 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003434 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003436 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3437 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003439 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3440 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 asdl_seq_SET(orelse, 0,
3444 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003445 LINENO(CHILD(n, NCH(n) - 6)),
3446 CHILD(n, NCH(n) - 6)->n_col_offset,
3447 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003448 /* the just-created orelse handled the last elif */
3449 n_elif--;
3450 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 for (i = 0; i < n_elif; i++) {
3453 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003454 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003455 if (!newobj)
3456 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003458 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003461 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463
Thomas Wouters89f507f2006-12-13 04:49:30 +00003464 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003466 LINENO(CHILD(n, off)),
3467 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003468 orelse = newobj;
3469 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003470 expression = ast_for_expr(c, CHILD(n, 1));
3471 if (!expression)
3472 return NULL;
3473 suite_seq = ast_for_suite(c, CHILD(n, 3));
3474 if (!suite_seq)
3475 return NULL;
3476 return If(expression, suite_seq, orelse,
3477 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003479
3480 PyErr_Format(PyExc_SystemError,
3481 "unexpected token in 'if' statement: %s", s);
3482 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483}
3484
3485static stmt_ty
3486ast_for_while_stmt(struct compiling *c, const node *n)
3487{
3488 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3489 REQ(n, while_stmt);
3490
3491 if (NCH(n) == 4) {
3492 expr_ty expression;
3493 asdl_seq *suite_seq;
3494
3495 expression = ast_for_expr(c, CHILD(n, 1));
3496 if (!expression)
3497 return NULL;
3498 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003499 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003501 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 }
3503 else if (NCH(n) == 7) {
3504 expr_ty expression;
3505 asdl_seq *seq1, *seq2;
3506
3507 expression = ast_for_expr(c, CHILD(n, 1));
3508 if (!expression)
3509 return NULL;
3510 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003511 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 return NULL;
3513 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003514 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 return NULL;
3516
Thomas Wouters89f507f2006-12-13 04:49:30 +00003517 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003519
3520 PyErr_Format(PyExc_SystemError,
3521 "wrong number of tokens for 'while' statement: %d",
3522 NCH(n));
3523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
3526static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003527ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003529 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003531 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003532 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3534 REQ(n, for_stmt);
3535
3536 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003537 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 if (!seq)
3539 return NULL;
3540 }
3541
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003542 node_target = CHILD(n, 1);
3543 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003544 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003546 /* Check the # of children rather than the length of _target, since
3547 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003548 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003549 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003550 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003552 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003554 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003555 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 return NULL;
3557 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003558 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 return NULL;
3560
Yury Selivanov75445082015-05-11 22:57:16 -04003561 if (is_async)
3562 return AsyncFor(target, expression, suite_seq, seq,
3563 LINENO(n), n->n_col_offset,
3564 c->c_arena);
3565 else
3566 return For(target, expression, suite_seq, seq,
3567 LINENO(n), n->n_col_offset,
3568 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569}
3570
3571static excepthandler_ty
3572ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3573{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003574 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 REQ(exc, except_clause);
3576 REQ(body, suite);
3577
3578 if (NCH(exc) == 1) {
3579 asdl_seq *suite_seq = ast_for_suite(c, body);
3580 if (!suite_seq)
3581 return NULL;
3582
Neal Norwitzad74aa82008-03-31 05:14:30 +00003583 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003584 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 }
3586 else if (NCH(exc) == 2) {
3587 expr_ty expression;
3588 asdl_seq *suite_seq;
3589
3590 expression = ast_for_expr(c, CHILD(exc, 1));
3591 if (!expression)
3592 return NULL;
3593 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003594 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 return NULL;
3596
Neal Norwitzad74aa82008-03-31 05:14:30 +00003597 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003598 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 }
3600 else if (NCH(exc) == 4) {
3601 asdl_seq *suite_seq;
3602 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003603 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003606 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003609 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 return NULL;
3611 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003612 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 return NULL;
3614
Neal Norwitzad74aa82008-03-31 05:14:30 +00003615 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003616 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003618
3619 PyErr_Format(PyExc_SystemError,
3620 "wrong number of children for 'except' clause: %d",
3621 NCH(exc));
3622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623}
3624
3625static stmt_ty
3626ast_for_try_stmt(struct compiling *c, const node *n)
3627{
Neal Norwitzf599f422005-12-17 21:33:47 +00003628 const int nch = NCH(n);
3629 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003630 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003631
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 REQ(n, try_stmt);
3633
Neal Norwitzf599f422005-12-17 21:33:47 +00003634 body = ast_for_suite(c, CHILD(n, 2));
3635 if (body == NULL)
3636 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637
Neal Norwitzf599f422005-12-17 21:33:47 +00003638 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3639 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3640 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3641 /* we can assume it's an "else",
3642 because nch >= 9 for try-else-finally and
3643 it would otherwise have a type of except_clause */
3644 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3645 if (orelse == NULL)
3646 return NULL;
3647 n_except--;
3648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649
Neal Norwitzf599f422005-12-17 21:33:47 +00003650 finally = ast_for_suite(c, CHILD(n, nch - 1));
3651 if (finally == NULL)
3652 return NULL;
3653 n_except--;
3654 }
3655 else {
3656 /* we can assume it's an "else",
3657 otherwise it would have a type of except_clause */
3658 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3659 if (orelse == NULL)
3660 return NULL;
3661 n_except--;
3662 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003664 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003665 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666 return NULL;
3667 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668
Neal Norwitzf599f422005-12-17 21:33:47 +00003669 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003670 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003671 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003672 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003673 if (handlers == NULL)
3674 return NULL;
3675
3676 for (i = 0; i < n_except; i++) {
3677 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3678 CHILD(n, 5 + i * 3));
3679 if (!e)
3680 return NULL;
3681 asdl_seq_SET(handlers, i, e);
3682 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003683 }
3684
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003685 assert(finally != NULL || asdl_seq_LEN(handlers));
3686 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687}
3688
Georg Brandl0c315622009-05-25 21:10:36 +00003689/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003690static withitem_ty
3691ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003692{
3693 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003694
Georg Brandl0c315622009-05-25 21:10:36 +00003695 REQ(n, with_item);
3696 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003697 if (!context_expr)
3698 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003699 if (NCH(n) == 3) {
3700 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003701
3702 if (!optional_vars) {
3703 return NULL;
3704 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003705 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003706 return NULL;
3707 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003708 }
3709
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003710 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003711}
3712
Georg Brandl0c315622009-05-25 21:10:36 +00003713/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3714static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003715ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003716{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003717 int i, n_items;
3718 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003719
3720 REQ(n, with_stmt);
3721
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003722 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003723 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003724 if (!items)
3725 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003726 for (i = 1; i < NCH(n) - 2; i += 2) {
3727 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3728 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003729 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003730 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003731 }
3732
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003733 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3734 if (!body)
3735 return NULL;
3736
Yury Selivanov75445082015-05-11 22:57:16 -04003737 if (is_async)
3738 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3739 else
3740 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003741}
3742
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003744ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003746 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003747 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003748 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003749 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 REQ(n, classdef);
3752
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003753 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 s = ast_for_suite(c, CHILD(n, 3));
3755 if (!s)
3756 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003757 classname = NEW_IDENTIFIER(CHILD(n, 1));
3758 if (!classname)
3759 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003760 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003761 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003762 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3763 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003765
3766 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003767 s = ast_for_suite(c, CHILD(n,5));
3768 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003769 return NULL;
3770 classname = NEW_IDENTIFIER(CHILD(n, 1));
3771 if (!classname)
3772 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003773 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003774 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003775 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3776 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 }
3778
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003779 /* class NAME '(' arglist ')' ':' suite */
3780 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003781 {
3782 PyObject *dummy_name;
3783 expr_ty dummy;
3784 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3785 if (!dummy_name)
3786 return NULL;
3787 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3788 call = ast_for_call(c, CHILD(n, 3), dummy);
3789 if (!call)
3790 return NULL;
3791 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003793 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003795 classname = NEW_IDENTIFIER(CHILD(n, 1));
3796 if (!classname)
3797 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003798 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003799 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003800
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003801 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003802 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803}
3804
3805static stmt_ty
3806ast_for_stmt(struct compiling *c, const node *n)
3807{
3808 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003809 assert(NCH(n) == 1);
3810 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811 }
3812 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003813 assert(num_stmts(n) == 1);
3814 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 }
3816 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003817 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003818 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3819 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003820 */
3821 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 case expr_stmt:
3823 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 case del_stmt:
3825 return ast_for_del_stmt(c, n);
3826 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003827 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 case flow_stmt:
3829 return ast_for_flow_stmt(c, n);
3830 case import_stmt:
3831 return ast_for_import_stmt(c, n);
3832 case global_stmt:
3833 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003834 case nonlocal_stmt:
3835 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836 case assert_stmt:
3837 return ast_for_assert_stmt(c, n);
3838 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003839 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3841 TYPE(n), NCH(n));
3842 return NULL;
3843 }
3844 }
3845 else {
3846 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003847 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003848 */
3849 node *ch = CHILD(n, 0);
3850 REQ(n, compound_stmt);
3851 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 case if_stmt:
3853 return ast_for_if_stmt(c, ch);
3854 case while_stmt:
3855 return ast_for_while_stmt(c, ch);
3856 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003857 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 case try_stmt:
3859 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003860 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003861 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003863 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003865 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 case decorated:
3867 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003868 case async_stmt:
3869 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003871 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3873 TYPE(n), NCH(n));
3874 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876 }
3877}
3878
3879static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003880parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003882 const char *end;
3883 long x;
3884 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003885 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003886 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003888 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003889 errno = 0;
3890 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003891 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003892 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003893 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003894 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003895 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003896 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003897 }
3898 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003899 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003900 if (*end == '\0') {
3901 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003902 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003903 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003904 }
3905 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003906 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003907 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003908 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3909 if (compl.imag == -1.0 && PyErr_Occurred())
3910 return NULL;
3911 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003912 }
3913 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003914 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003915 dx = PyOS_string_to_double(s, NULL, NULL);
3916 if (dx == -1.0 && PyErr_Occurred())
3917 return NULL;
3918 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003919 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920}
3921
3922static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003923decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003925 const char *s, *t;
3926 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003927 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3928 while (s < end && (*s & 0x80)) s++;
3929 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003930 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931}
3932
3933static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003934decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003936 PyObject *v, *u;
3937 char *buf;
3938 char *p;
3939 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003940
Guido van Rossumd8faa362007-04-27 19:54:29 +00003941 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003942 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003943 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003944 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003945 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003946 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003947 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3948 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3949 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003950 if (u == NULL)
3951 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003952 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003953 end = s + len;
3954 while (s < end) {
3955 if (*s == '\\') {
3956 *p++ = *s++;
3957 if (*s & 0x80) {
3958 strcpy(p, "u005c");
3959 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003960 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003961 }
3962 if (*s & 0x80) { /* XXX inefficient */
3963 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003964 int kind;
3965 void *data;
3966 Py_ssize_t len, i;
3967 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003968 if (w == NULL) {
3969 Py_DECREF(u);
3970 return NULL;
3971 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003972 kind = PyUnicode_KIND(w);
3973 data = PyUnicode_DATA(w);
3974 len = PyUnicode_GET_LENGTH(w);
3975 for (i = 0; i < len; i++) {
3976 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3977 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003978 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003979 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003980 /* Should be impossible to overflow */
3981 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003982 Py_DECREF(w);
3983 } else {
3984 *p++ = *s++;
3985 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003986 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003987 len = p - buf;
3988 s = buf;
3989 }
3990 if (rawmode)
3991 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3992 else
3993 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3994 Py_XDECREF(u);
3995 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996}
3997
3998/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003999 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 * parsestr parses it, and returns the decoded Python string object.
4001 */
4002static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00004003parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004005 size_t len;
4006 const char *s = STR(n);
4007 int quote = Py_CHARMASK(*s);
4008 int rawmode = 0;
4009 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004010 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004011 while (!*bytesmode || !rawmode) {
4012 if (quote == 'b' || quote == 'B') {
4013 quote = *++s;
4014 *bytesmode = 1;
4015 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004016 else if (quote == 'u' || quote == 'U') {
4017 quote = *++s;
4018 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004019 else if (quote == 'r' || quote == 'R') {
4020 quote = *++s;
4021 rawmode = 1;
4022 }
4023 else {
4024 break;
4025 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004026 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004027 }
4028 if (quote != '\'' && quote != '\"') {
4029 PyErr_BadInternalCall();
4030 return NULL;
4031 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004032 s++;
4033 len = strlen(s);
4034 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004036 "string to parse is too long");
4037 return NULL;
4038 }
4039 if (s[--len] != quote) {
4040 PyErr_BadInternalCall();
4041 return NULL;
4042 }
4043 if (len >= 4 && s[0] == quote && s[1] == quote) {
4044 s += 2;
4045 len -= 2;
4046 if (s[--len] != quote || s[--len] != quote) {
4047 PyErr_BadInternalCall();
4048 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004049 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004050 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004051 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004052 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 if (*bytesmode) {
4055 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004056 const char *ch;
4057 for (ch = s; *ch; ch++) {
4058 if (Py_CHARMASK(*ch) >= 0x80) {
4059 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004060 "literal characters.");
4061 return NULL;
4062 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004063 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004065 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004066 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004067 if (rawmode || strchr(s, '\\') == NULL) {
4068 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004069 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004070 if (u == NULL || !*bytesmode)
4071 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004072 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004073 Py_DECREF(u);
4074 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004075 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00004076 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004077 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004078 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004080 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004081 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004082 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004083 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00004084 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085}
4086
Guido van Rossum29fd7122007-11-12 01:13:56 +00004087/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004088 * compile-time literal catenation, calling parsestr() on each piece, and
4089 * pasting the intermediate results together.
4090 */
4091static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00004092parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004094 PyObject *v;
4095 int i;
4096 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004097 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004098 if (v != NULL) {
4099 /* String literal concatenation */
4100 for (i = 1; i < NCH(n); i++) {
4101 PyObject *s;
4102 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004103 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004104 if (s == NULL)
4105 goto onError;
4106 if (*bytesmode != subbm) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004107 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02004108 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004109 goto onError;
4110 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004111 if (PyBytes_Check(v) && PyBytes_Check(s)) {
4112 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004113 if (v == NULL)
4114 goto onError;
4115 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004116 else {
4117 PyObject *temp = PyUnicode_Concat(v, s);
4118 Py_DECREF(s);
4119 Py_DECREF(v);
4120 v = temp;
4121 if (v == NULL)
4122 goto onError;
4123 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004124 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004125 }
4126 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004127
Guido van Rossumd8faa362007-04-27 19:54:29 +00004128 onError:
4129 Py_XDECREF(v);
4130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131}