blob: b2f09b9c84b6cbfd5a0b09f5dcce7bcc68d668a5 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
135validate_expr(expr_ty exp, expr_context_ty ctx)
136{
137 int check_ctx = 1;
138 expr_context_ty actual_ctx;
139
140 /* First check expression context. */
141 switch (exp->kind) {
142 case Attribute_kind:
143 actual_ctx = exp->v.Attribute.ctx;
144 break;
145 case Subscript_kind:
146 actual_ctx = exp->v.Subscript.ctx;
147 break;
148 case Starred_kind:
149 actual_ctx = exp->v.Starred.ctx;
150 break;
151 case Name_kind:
152 actual_ctx = exp->v.Name.ctx;
153 break;
154 case List_kind:
155 actual_ctx = exp->v.List.ctx;
156 break;
157 case Tuple_kind:
158 actual_ctx = exp->v.Tuple.ctx;
159 break;
160 default:
161 if (ctx != Load) {
162 PyErr_Format(PyExc_ValueError, "expression which can't be "
163 "assigned to in %s context", expr_context_name(ctx));
164 return 0;
165 }
166 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100167 /* set actual_ctx to prevent gcc warning */
168 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500169 }
170 if (check_ctx && actual_ctx != ctx) {
171 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
172 expr_context_name(ctx), expr_context_name(actual_ctx));
173 return 0;
174 }
175
176 /* Now validate expression. */
177 switch (exp->kind) {
178 case BoolOp_kind:
179 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
180 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
181 return 0;
182 }
183 return validate_exprs(exp->v.BoolOp.values, Load, 0);
184 case BinOp_kind:
185 return validate_expr(exp->v.BinOp.left, Load) &&
186 validate_expr(exp->v.BinOp.right, Load);
187 case UnaryOp_kind:
188 return validate_expr(exp->v.UnaryOp.operand, Load);
189 case Lambda_kind:
190 return validate_arguments(exp->v.Lambda.args) &&
191 validate_expr(exp->v.Lambda.body, Load);
192 case IfExp_kind:
193 return validate_expr(exp->v.IfExp.test, Load) &&
194 validate_expr(exp->v.IfExp.body, Load) &&
195 validate_expr(exp->v.IfExp.orelse, Load);
196 case Dict_kind:
197 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
198 PyErr_SetString(PyExc_ValueError,
199 "Dict doesn't have the same number of keys as values");
200 return 0;
201 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400202 /* null_ok=1 for keys expressions to allow dict unpacking to work in
203 dict literals, i.e. ``{**{a:b}}`` */
204 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
205 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500206 case Set_kind:
207 return validate_exprs(exp->v.Set.elts, Load, 0);
208#define COMP(NAME) \
209 case NAME ## _kind: \
210 return validate_comprehension(exp->v.NAME.generators) && \
211 validate_expr(exp->v.NAME.elt, Load);
212 COMP(ListComp)
213 COMP(SetComp)
214 COMP(GeneratorExp)
215#undef COMP
216 case DictComp_kind:
217 return validate_comprehension(exp->v.DictComp.generators) &&
218 validate_expr(exp->v.DictComp.key, Load) &&
219 validate_expr(exp->v.DictComp.value, Load);
220 case Yield_kind:
221 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500222 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000223 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400224 case Await_kind:
225 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500226 case Compare_kind:
227 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
228 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
229 return 0;
230 }
231 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
232 asdl_seq_LEN(exp->v.Compare.ops)) {
233 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
234 "of comparators and operands");
235 return 0;
236 }
237 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
238 validate_expr(exp->v.Compare.left, Load);
239 case Call_kind:
240 return validate_expr(exp->v.Call.func, Load) &&
241 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400242 validate_keywords(exp->v.Call.keywords);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500243 case Num_kind: {
244 PyObject *n = exp->v.Num.n;
245 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
246 !PyComplex_CheckExact(n)) {
247 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
248 return 0;
249 }
250 return 1;
251 }
252 case Str_kind: {
253 PyObject *s = exp->v.Str.s;
254 if (!PyUnicode_CheckExact(s)) {
255 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
256 return 0;
257 }
258 return 1;
259 }
260 case Bytes_kind: {
261 PyObject *b = exp->v.Bytes.s;
262 if (!PyBytes_CheckExact(b)) {
263 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
264 return 0;
265 }
266 return 1;
267 }
268 case Attribute_kind:
269 return validate_expr(exp->v.Attribute.value, Load);
270 case Subscript_kind:
271 return validate_slice(exp->v.Subscript.slice) &&
272 validate_expr(exp->v.Subscript.value, Load);
273 case Starred_kind:
274 return validate_expr(exp->v.Starred.value, ctx);
275 case List_kind:
276 return validate_exprs(exp->v.List.elts, ctx, 0);
277 case Tuple_kind:
278 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
279 /* These last cases don't have any checking. */
280 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500281 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500282 case Ellipsis_kind:
283 return 1;
284 default:
285 PyErr_SetString(PyExc_SystemError, "unexpected expression");
286 return 0;
287 }
288}
289
290static int
291validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
292{
293 if (asdl_seq_LEN(seq))
294 return 1;
295 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
296 return 0;
297}
298
299static int
300validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
301{
302 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
303 validate_exprs(targets, ctx, 0);
304}
305
306static int
307validate_body(asdl_seq *body, const char *owner)
308{
309 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
310}
311
312static int
313validate_stmt(stmt_ty stmt)
314{
315 int i;
316 switch (stmt->kind) {
317 case FunctionDef_kind:
318 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
319 validate_arguments(stmt->v.FunctionDef.args) &&
320 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
321 (!stmt->v.FunctionDef.returns ||
322 validate_expr(stmt->v.FunctionDef.returns, Load));
323 case ClassDef_kind:
324 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
325 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
326 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400327 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500328 case Return_kind:
329 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
330 case Delete_kind:
331 return validate_assignlist(stmt->v.Delete.targets, Del);
332 case Assign_kind:
333 return validate_assignlist(stmt->v.Assign.targets, Store) &&
334 validate_expr(stmt->v.Assign.value, Load);
335 case AugAssign_kind:
336 return validate_expr(stmt->v.AugAssign.target, Store) &&
337 validate_expr(stmt->v.AugAssign.value, Load);
338 case For_kind:
339 return validate_expr(stmt->v.For.target, Store) &&
340 validate_expr(stmt->v.For.iter, Load) &&
341 validate_body(stmt->v.For.body, "For") &&
342 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400343 case AsyncFor_kind:
344 return validate_expr(stmt->v.AsyncFor.target, Store) &&
345 validate_expr(stmt->v.AsyncFor.iter, Load) &&
346 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
347 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348 case While_kind:
349 return validate_expr(stmt->v.While.test, Load) &&
350 validate_body(stmt->v.While.body, "While") &&
351 validate_stmts(stmt->v.While.orelse);
352 case If_kind:
353 return validate_expr(stmt->v.If.test, Load) &&
354 validate_body(stmt->v.If.body, "If") &&
355 validate_stmts(stmt->v.If.orelse);
356 case With_kind:
357 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
358 return 0;
359 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
360 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
361 if (!validate_expr(item->context_expr, Load) ||
362 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
363 return 0;
364 }
365 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400366 case AsyncWith_kind:
367 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
368 return 0;
369 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
370 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
371 if (!validate_expr(item->context_expr, Load) ||
372 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
373 return 0;
374 }
375 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500376 case Raise_kind:
377 if (stmt->v.Raise.exc) {
378 return validate_expr(stmt->v.Raise.exc, Load) &&
379 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
380 }
381 if (stmt->v.Raise.cause) {
382 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
383 return 0;
384 }
385 return 1;
386 case Try_kind:
387 if (!validate_body(stmt->v.Try.body, "Try"))
388 return 0;
389 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
390 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
391 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
392 return 0;
393 }
394 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
395 asdl_seq_LEN(stmt->v.Try.orelse)) {
396 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
397 return 0;
398 }
399 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
400 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
401 if ((handler->v.ExceptHandler.type &&
402 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
403 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
404 return 0;
405 }
406 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
407 validate_stmts(stmt->v.Try.finalbody)) &&
408 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
409 validate_stmts(stmt->v.Try.orelse));
410 case Assert_kind:
411 return validate_expr(stmt->v.Assert.test, Load) &&
412 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
413 case Import_kind:
414 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
415 case ImportFrom_kind:
416 if (stmt->v.ImportFrom.level < -1) {
417 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
418 return 0;
419 }
420 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
421 case Global_kind:
422 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
423 case Nonlocal_kind:
424 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
425 case Expr_kind:
426 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400427 case AsyncFunctionDef_kind:
428 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
429 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
430 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
431 (!stmt->v.AsyncFunctionDef.returns ||
432 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500433 case Pass_kind:
434 case Break_kind:
435 case Continue_kind:
436 return 1;
437 default:
438 PyErr_SetString(PyExc_SystemError, "unexpected statement");
439 return 0;
440 }
441}
442
443static int
444validate_stmts(asdl_seq *seq)
445{
446 int i;
447 for (i = 0; i < asdl_seq_LEN(seq); i++) {
448 stmt_ty stmt = asdl_seq_GET(seq, i);
449 if (stmt) {
450 if (!validate_stmt(stmt))
451 return 0;
452 }
453 else {
454 PyErr_SetString(PyExc_ValueError,
455 "None disallowed in statement list");
456 return 0;
457 }
458 }
459 return 1;
460}
461
462static int
463validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
464{
465 int i;
466 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
467 expr_ty expr = asdl_seq_GET(exprs, i);
468 if (expr) {
469 if (!validate_expr(expr, ctx))
470 return 0;
471 }
472 else if (!null_ok) {
473 PyErr_SetString(PyExc_ValueError,
474 "None disallowed in expression list");
475 return 0;
476 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100477
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500478 }
479 return 1;
480}
481
482int
483PyAST_Validate(mod_ty mod)
484{
485 int res = 0;
486
487 switch (mod->kind) {
488 case Module_kind:
489 res = validate_stmts(mod->v.Module.body);
490 break;
491 case Interactive_kind:
492 res = validate_stmts(mod->v.Interactive.body);
493 break;
494 case Expression_kind:
495 res = validate_expr(mod->v.Expression.body, Load);
496 break;
497 case Suite_kind:
498 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
499 break;
500 default:
501 PyErr_SetString(PyExc_SystemError, "impossible module node");
502 res = 0;
503 break;
504 }
505 return res;
506}
507
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500508/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500509#include "grammar.h"
510#include "parsetok.h"
511#include "graminit.h"
512
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513/* Data structure used internally */
514struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000515 char *c_encoding; /* source encoding */
516 PyArena *c_arena; /* arena for allocating memeory */
Victor Stinner14e461d2013-08-26 22:28:21 +0200517 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500518 PyObject *c_normalize; /* Normalization function from unicodedata. */
519 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520};
521
522static asdl_seq *seq_for_testlist(struct compiling *, const node *);
523static expr_ty ast_for_expr(struct compiling *, const node *);
524static stmt_ty ast_for_stmt(struct compiling *, const node *);
525static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000526static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
527 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000528static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000529static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530
Yury Selivanov75445082015-05-11 22:57:16 -0400531static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
532static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
533
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534/* Note different signature for ast_for_call */
535static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
536
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000537static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000538static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +0000539static PyObject *parsestrplus(struct compiling *, const node *n,
540 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542#define COMP_GENEXP 0
543#define COMP_LISTCOMP 1
544#define COMP_SETCOMP 2
545
Benjamin Peterson55e00432012-01-16 17:22:31 -0500546static int
547init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000548{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500549 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
550 if (!m)
551 return 0;
552 c->c_normalize = PyObject_GetAttrString(m, "normalize");
553 Py_DECREF(m);
554 if (!c->c_normalize)
555 return 0;
556 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500557 if (!c->c_normalize_args) {
558 Py_CLEAR(c->c_normalize);
559 return 0;
560 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200561 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500562 return 1;
563}
564
565static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400566new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500567{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400568 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500569 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000570 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500571 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500572 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000573 /* Check whether there are non-ASCII characters in the
574 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500575 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200576 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500577 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500578 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200579 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500580 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500581 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
582 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500583 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200584 if (!id2)
585 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200586 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000587 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000588 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200589 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
590 Py_DECREF(id);
591 return NULL;
592 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000593 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594}
595
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400599ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400601 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Victor Stinner14e461d2013-08-26 22:28:21 +0200603 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000605 Py_INCREF(Py_None);
606 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200608 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400609 if (!tmp)
610 return 0;
611 errstr = PyUnicode_FromString(errmsg);
612 if (!errstr) {
613 Py_DECREF(tmp);
614 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000615 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000616 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 Py_DECREF(errstr);
618 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400619 if (value) {
620 PyErr_SetObject(PyExc_SyntaxError, value);
621 Py_DECREF(value);
622 }
623 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624}
625
626/* num_stmts() returns number of contained statements.
627
628 Use this routine to determine how big a sequence is needed for
629 the statements in a parse tree. Its raison d'etre is this bit of
630 grammar:
631
632 stmt: simple_stmt | compound_stmt
633 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
634
635 A simple_stmt can contain multiple small_stmt elements joined
636 by semicolons. If the arg is a simple_stmt, the number of
637 small_stmt elements is returned.
638*/
639
640static int
641num_stmts(const node *n)
642{
643 int i, l;
644 node *ch;
645
646 switch (TYPE(n)) {
647 case single_input:
648 if (TYPE(CHILD(n, 0)) == NEWLINE)
649 return 0;
650 else
651 return num_stmts(CHILD(n, 0));
652 case file_input:
653 l = 0;
654 for (i = 0; i < NCH(n); i++) {
655 ch = CHILD(n, i);
656 if (TYPE(ch) == stmt)
657 l += num_stmts(ch);
658 }
659 return l;
660 case stmt:
661 return num_stmts(CHILD(n, 0));
662 case compound_stmt:
663 return 1;
664 case simple_stmt:
665 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
666 case suite:
667 if (NCH(n) == 1)
668 return num_stmts(CHILD(n, 0));
669 else {
670 l = 0;
671 for (i = 2; i < (NCH(n) - 1); i++)
672 l += num_stmts(CHILD(n, i));
673 return l;
674 }
675 default: {
676 char buf[128];
677
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000678 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 TYPE(n), NCH(n));
680 Py_FatalError(buf);
681 }
682 }
683 assert(0);
684 return 0;
685}
686
687/* Transform the CST rooted at node * to the appropriate AST
688*/
689
690mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200691PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
692 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000694 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 asdl_seq *stmts = NULL;
696 stmt_ty s;
697 node *ch;
698 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500699 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400701 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200702 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400703 c.c_filename = filename;
704 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000706 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000708#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400709 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500710 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000711#endif
712 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 } else if (TYPE(n) == encoding_decl) {
715 c.c_encoding = STR(n);
716 n = CHILD(n, 0);
717 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000719 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 }
721
Jeremy Hyltona8293132006-02-28 17:58:27 +0000722 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 switch (TYPE(n)) {
724 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200725 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500727 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 for (i = 0; i < NCH(n) - 1; i++) {
729 ch = CHILD(n, i);
730 if (TYPE(ch) == NEWLINE)
731 continue;
732 REQ(ch, stmt);
733 num = num_stmts(ch);
734 if (num == 1) {
735 s = ast_for_stmt(&c, ch);
736 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500737 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000738 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 }
740 else {
741 ch = CHILD(ch, 0);
742 REQ(ch, simple_stmt);
743 for (j = 0; j < num; j++) {
744 s = ast_for_stmt(&c, CHILD(ch, j * 2));
745 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500746 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000747 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 }
749 }
750 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500751 res = Module(stmts, arena);
752 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 case eval_input: {
754 expr_ty testlist_ast;
755
Nick Coghlan650f0d02007-04-15 12:05:43 +0000756 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000757 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500759 goto out;
760 res = Expression(testlist_ast, arena);
761 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 }
763 case single_input:
764 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200765 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500767 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
769 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000770 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500771 goto out;
772 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 }
774 else {
775 n = CHILD(n, 0);
776 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200777 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500779 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000781 s = ast_for_stmt(&c, n);
782 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 asdl_seq_SET(stmts, 0, s);
785 }
786 else {
787 /* Only a simple_stmt can contain multiple statements. */
788 REQ(n, simple_stmt);
789 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 if (TYPE(CHILD(n, i)) == NEWLINE)
791 break;
792 s = ast_for_stmt(&c, CHILD(n, i));
793 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500794 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 asdl_seq_SET(stmts, i / 2, s);
796 }
797 }
798
Benjamin Peterson55e00432012-01-16 17:22:31 -0500799 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500801 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000803 PyErr_Format(PyExc_SystemError,
804 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500807 out:
808 if (c.c_normalize) {
809 Py_DECREF(c.c_normalize);
810 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
811 Py_DECREF(c.c_normalize_args);
812 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500813 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814}
815
Victor Stinner14e461d2013-08-26 22:28:21 +0200816mod_ty
817PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
818 PyArena *arena)
819{
820 mod_ty mod;
821 PyObject *filename;
822 filename = PyUnicode_DecodeFSDefault(filename_str);
823 if (filename == NULL)
824 return NULL;
825 mod = PyAST_FromNodeObject(n, flags, filename, arena);
826 Py_DECREF(filename);
827 return mod;
828
829}
830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
832*/
833
834static operator_ty
835get_operator(const node *n)
836{
837 switch (TYPE(n)) {
838 case VBAR:
839 return BitOr;
840 case CIRCUMFLEX:
841 return BitXor;
842 case AMPER:
843 return BitAnd;
844 case LEFTSHIFT:
845 return LShift;
846 case RIGHTSHIFT:
847 return RShift;
848 case PLUS:
849 return Add;
850 case MINUS:
851 return Sub;
852 case STAR:
853 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400854 case AT:
855 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 case SLASH:
857 return Div;
858 case DOUBLESLASH:
859 return FloorDiv;
860 case PERCENT:
861 return Mod;
862 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 }
865}
866
Guido van Rossume7ba4952007-06-06 23:52:48 +0000867static const char* FORBIDDEN[] = {
868 "None",
869 "True",
870 "False",
871 NULL,
872};
873
874static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400875forbidden_name(struct compiling *c, identifier name, const node *n,
876 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000877{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000878 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000879 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400880 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000881 return 1;
882 }
883 if (full_checks) {
884 const char **p;
885 for (p = FORBIDDEN; *p; p++) {
886 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400887 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000888 return 1;
889 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000890 }
891 }
892 return 0;
893}
894
Jeremy Hyltona8293132006-02-28 17:58:27 +0000895/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
897 Only sets context for expr kinds that "can appear in assignment context"
898 (according to ../Parser/Python.asdl). For other expr kinds, it sets
899 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900*/
901
902static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000903set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904{
905 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000906 /* If a particular expression type can't be used for assign / delete,
907 set expr_name to its name and an error message will be generated.
908 */
909 const char* expr_name = NULL;
910
911 /* The ast defines augmented store and load contexts, but the
912 implementation here doesn't actually use them. The code may be
913 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000914 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000915 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000916 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000917 */
918 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
920 switch (e->kind) {
921 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000922 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400923 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000924 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000925 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000927 e->v.Subscript.ctx = ctx;
928 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000929 case Starred_kind:
930 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000931 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000932 return 0;
933 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500936 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000938 }
939 e->v.Name.ctx = ctx;
940 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000942 e->v.List.ctx = ctx;
943 s = e->v.List.elts;
944 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000946 if (asdl_seq_LEN(e->v.Tuple.elts)) {
947 e->v.Tuple.ctx = ctx;
948 s = e->v.Tuple.elts;
949 }
950 else {
951 expr_name = "()";
952 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000953 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000954 case Lambda_kind:
955 expr_name = "lambda";
956 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000958 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000959 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000960 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000962 case UnaryOp_kind:
963 expr_name = "operator";
964 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000966 expr_name = "generator expression";
967 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000968 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500969 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970 expr_name = "yield expression";
971 break;
Yury Selivanov75445082015-05-11 22:57:16 -0400972 case Await_kind:
973 expr_name = "await expression";
974 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 case ListComp_kind:
976 expr_name = "list comprehension";
977 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000978 case SetComp_kind:
979 expr_name = "set comprehension";
980 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000981 case DictComp_kind:
982 expr_name = "dict comprehension";
983 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000984 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000985 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 case Num_kind:
987 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500988 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000989 expr_name = "literal";
990 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500991 case NameConstant_kind:
992 expr_name = "keyword";
993 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000994 case Ellipsis_kind:
995 expr_name = "Ellipsis";
996 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000997 case Compare_kind:
998 expr_name = "comparison";
999 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000 case IfExp_kind:
1001 expr_name = "conditional expression";
1002 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001003 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 PyErr_Format(PyExc_SystemError,
1005 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001006 e->kind, e->lineno);
1007 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001009 /* Check for error string set by switch */
1010 if (expr_name) {
1011 char buf[300];
1012 PyOS_snprintf(buf, sizeof(buf),
1013 "can't %s %s",
1014 ctx == Store ? "assign to" : "delete",
1015 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001016 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001017 }
1018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 */
1022 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Thomas Wouters89f507f2006-12-13 04:49:30 +00001025 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001026 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001027 return 0;
1028 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 }
1030 return 1;
1031}
1032
1033static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001034ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035{
1036 REQ(n, augassign);
1037 n = CHILD(n, 0);
1038 switch (STR(n)[0]) {
1039 case '+':
1040 return Add;
1041 case '-':
1042 return Sub;
1043 case '/':
1044 if (STR(n)[1] == '/')
1045 return FloorDiv;
1046 else
1047 return Div;
1048 case '%':
1049 return Mod;
1050 case '<':
1051 return LShift;
1052 case '>':
1053 return RShift;
1054 case '&':
1055 return BitAnd;
1056 case '^':
1057 return BitXor;
1058 case '|':
1059 return BitOr;
1060 case '*':
1061 if (STR(n)[1] == '*')
1062 return Pow;
1063 else
1064 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001065 case '@':
1066 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001068 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 }
1071}
1072
1073static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001074ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001076 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 |'is' 'not'
1078 */
1079 REQ(n, comp_op);
1080 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001081 n = CHILD(n, 0);
1082 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 case LESS:
1084 return Lt;
1085 case GREATER:
1086 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001087 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return Eq;
1089 case LESSEQUAL:
1090 return LtE;
1091 case GREATEREQUAL:
1092 return GtE;
1093 case NOTEQUAL:
1094 return NotEq;
1095 case NAME:
1096 if (strcmp(STR(n), "in") == 0)
1097 return In;
1098 if (strcmp(STR(n), "is") == 0)
1099 return Is;
1100 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001101 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001103 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001104 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 }
1106 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001107 /* handle "not in" and "is not" */
1108 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 case NAME:
1110 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1111 return NotIn;
1112 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1113 return IsNot;
1114 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001115 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001117 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 }
Neal Norwitz79792652005-11-14 04:25:03 +00001120 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123}
1124
1125static asdl_seq *
1126seq_for_testlist(struct compiling *c, const node *n)
1127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001129 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1130 */
Armin Rigo31441302005-10-21 12:57:31 +00001131 asdl_seq *seq;
1132 expr_ty expression;
1133 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001134 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001136 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 if (!seq)
1138 return NULL;
1139
1140 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001142 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143
Benjamin Peterson4905e802009-09-27 02:43:28 +00001144 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147
1148 assert(i / 2 < seq->size);
1149 asdl_seq_SET(seq, i / 2, expression);
1150 }
1151 return seq;
1152}
1153
Neal Norwitzc1505362006-12-28 06:47:50 +00001154static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001155ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001156{
1157 identifier name;
1158 expr_ty annotation = NULL;
1159 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001160 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001161
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001162 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001163 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001164 name = NEW_IDENTIFIER(ch);
1165 if (!name)
1166 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001167 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001168 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001169
1170 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1171 annotation = ast_for_expr(c, CHILD(n, 2));
1172 if (!annotation)
1173 return NULL;
1174 }
1175
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001176 ret = arg(name, annotation, c->c_arena);
1177 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001178 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001179 ret->lineno = LINENO(n);
1180 ret->col_offset = n->n_col_offset;
1181 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182}
1183
Guido van Rossum4f72a782006-10-27 23:31:49 +00001184/* returns -1 if failed to handle keyword only arguments
1185 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001186 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001187 ^^^
1188 start pointing here
1189 */
1190static int
1191handle_keywordonly_args(struct compiling *c, const node *n, int start,
1192 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1193{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001194 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001195 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001196 expr_ty expression, annotation;
1197 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001198 int i = start;
1199 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001200
1201 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001202 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001203 return -1;
1204 }
1205 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001206 while (i < NCH(n)) {
1207 ch = CHILD(n, i);
1208 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001209 case vfpdef:
1210 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001211 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001212 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001213 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001214 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001215 asdl_seq_SET(kwdefaults, j, expression);
1216 i += 2; /* '=' and test */
1217 }
1218 else { /* setting NULL if no default value exists */
1219 asdl_seq_SET(kwdefaults, j, NULL);
1220 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001221 if (NCH(ch) == 3) {
1222 /* ch is NAME ':' test */
1223 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001224 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001225 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001226 }
1227 else {
1228 annotation = NULL;
1229 }
1230 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001231 argname = NEW_IDENTIFIER(ch);
1232 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001233 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001234 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001235 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001236 arg = arg(argname, annotation, c->c_arena);
1237 if (!arg)
1238 goto error;
Benjamin Peterson0714b8b2014-02-13 19:22:14 -05001239 arg->lineno = LINENO(ch);
1240 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001241 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001242 i += 2; /* the name and the comma */
1243 break;
1244 case DOUBLESTAR:
1245 return i;
1246 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001247 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001248 goto error;
1249 }
1250 }
1251 return i;
1252 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001254}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255
Jeremy Hyltona8293132006-02-28 17:58:27 +00001256/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257
1258static arguments_ty
1259ast_for_arguments(struct compiling *c, const node *n)
1260{
Neal Norwitzc1505362006-12-28 06:47:50 +00001261 /* This function handles both typedargslist (function definition)
1262 and varargslist (lambda definition).
1263
1264 parameters: '(' [typedargslist] ')'
1265 typedargslist: ((tfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001267 | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001268 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001269 tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001270 varargslist: ((vfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272 | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001273 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001274 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001276 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1277 int nposdefaults = 0, found_default = 0;
1278 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001279 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001280 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 node *ch;
1282
1283 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001285 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001288 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289
Jeremy Hyltone921e022008-07-17 16:37:17 +00001290 /* First count the number of positional args & defaults. The
1291 variable i is the loop index for this for loop and the next.
1292 The next loop picks up where the first leaves off.
1293 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295 ch = CHILD(n, i);
1296 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001297 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001298 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001299 if (i < NCH(n) && /* skip argument following star */
1300 (TYPE(CHILD(n, i)) == tfpdef ||
1301 TYPE(CHILD(n, i)) == vfpdef)) {
1302 i++;
1303 }
1304 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001305 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001306 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001308 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001311 defaults for keyword only args */
1312 for ( ; i < NCH(n); ++i) {
1313 ch = CHILD(n, i);
1314 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001315 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001316 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001317 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001319 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001321 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001323 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001325 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001327 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329 since we set NULL as default for keyword only argument w/o default
1330 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001331 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001332 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001334 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335
1336 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001337 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001339 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001341 /* tfpdef: NAME [':' test]
1342 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 */
1344 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001345 j = 0; /* index for defaults */
1346 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 ch = CHILD(n, i);
1349 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 case tfpdef:
1351 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1353 anything other than EQUAL or a comma? */
1354 /* XXX Should NCH(n) check be made a separate check? */
1355 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001356 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1357 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001358 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 assert(posdefaults != NULL);
1360 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001365 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001367 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001369 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001371 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001372 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 i += 2; /* the name and the comma */
1374 break;
1375 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 if (i+1 >= NCH(n)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001377 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001378 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001379 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001381 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001382 if (TYPE(ch) == COMMA) {
1383 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 i += 2; /* now follows keyword only arguments */
1385 res = handle_keywordonly_args(c, n, i,
1386 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001387 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001388 i = res; /* res has new position to process */
1389 }
1390 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001391 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001392 if (!vararg)
1393 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001394
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001396 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1397 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 int res = 0;
1399 res = handle_keywordonly_args(c, n, i,
1400 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001401 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 i = res; /* res has new position to process */
1403 }
1404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 break;
1406 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001407 ch = CHILD(n, i+1); /* tfpdef */
1408 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001409 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001410 if (!kwarg)
1411 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 i += 3;
1413 break;
1414 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001415 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 "unexpected node in varargslist: %d @ %d",
1417 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001418 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001421 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422}
1423
1424static expr_ty
1425ast_for_dotted_name(struct compiling *c, const node *n)
1426{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001427 expr_ty e;
1428 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001429 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 int i;
1431
1432 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001433
1434 lineno = LINENO(n);
1435 col_offset = n->n_col_offset;
1436
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 id = NEW_IDENTIFIER(CHILD(n, 0));
1438 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001439 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001440 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443
1444 for (i = 2; i < NCH(n); i+=2) {
1445 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001446 if (!id)
1447 return NULL;
1448 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1449 if (!e)
1450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 }
1452
1453 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454}
1455
1456static expr_ty
1457ast_for_decorator(struct compiling *c, const node *n)
1458{
1459 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1460 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001461 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001464 REQ(CHILD(n, 0), AT);
1465 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1468 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001469 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001472 d = name_expr;
1473 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 }
1475 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001476 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001478 if (!d)
1479 return NULL;
1480 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 }
1482 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001483 d = ast_for_call(c, CHILD(n, 3), name_expr);
1484 if (!d)
1485 return NULL;
1486 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 }
1488
1489 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490}
1491
1492static asdl_seq*
1493ast_for_decorators(struct compiling *c, const node *n)
1494{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001495 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001496 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001500 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 if (!decorator_seq)
1502 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001505 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001506 if (!d)
1507 return NULL;
1508 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 }
1510 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
1513static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001514ast_for_funcdef_impl(struct compiling *c, const node *n,
1515 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001517 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001518 identifier name;
1519 arguments_ty args;
1520 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001521 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001522 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
1524 REQ(n, funcdef);
1525
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526 name = NEW_IDENTIFIER(CHILD(n, name_i));
1527 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001528 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001529 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1532 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001533 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001534 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1535 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1536 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001537 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001538 name_i += 2;
1539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 body = ast_for_suite(c, CHILD(n, name_i + 3));
1541 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543
Yury Selivanov75445082015-05-11 22:57:16 -04001544 if (is_async)
1545 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1546 LINENO(n),
1547 n->n_col_offset, c->c_arena);
1548 else
1549 return FunctionDef(name, args, body, decorator_seq, returns,
1550 LINENO(n),
1551 n->n_col_offset, c->c_arena);
1552}
1553
1554static stmt_ty
1555ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1556{
1557 /* async_funcdef: ASYNC funcdef */
1558 REQ(n, async_funcdef);
1559 REQ(CHILD(n, 0), ASYNC);
1560 REQ(CHILD(n, 1), funcdef);
1561
1562 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1563 1 /* is_async */);
1564}
1565
1566static stmt_ty
1567ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1568{
1569 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1570 return ast_for_funcdef_impl(c, n, decorator_seq,
1571 0 /* is_async */);
1572}
1573
1574
1575static stmt_ty
1576ast_for_async_stmt(struct compiling *c, const node *n)
1577{
1578 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1579 REQ(n, async_stmt);
1580 REQ(CHILD(n, 0), ASYNC);
1581
1582 switch (TYPE(CHILD(n, 1))) {
1583 case funcdef:
1584 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1585 1 /* is_async */);
1586 case with_stmt:
1587 return ast_for_with_stmt(c, CHILD(n, 1),
1588 1 /* is_async */);
1589
1590 case for_stmt:
1591 return ast_for_for_stmt(c, CHILD(n, 1),
1592 1 /* is_async */);
1593
1594 default:
1595 PyErr_Format(PyExc_SystemError,
1596 "invalid async stament: %s",
1597 STR(CHILD(n, 1)));
1598 return NULL;
1599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001602static stmt_ty
1603ast_for_decorated(struct compiling *c, const node *n)
1604{
Yury Selivanov75445082015-05-11 22:57:16 -04001605 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001606 stmt_ty thing = NULL;
1607 asdl_seq *decorator_seq = NULL;
1608
1609 REQ(n, decorated);
1610
1611 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1612 if (!decorator_seq)
1613 return NULL;
1614
1615 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001616 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001618
1619 if (TYPE(CHILD(n, 1)) == funcdef) {
1620 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1621 } else if (TYPE(CHILD(n, 1)) == classdef) {
1622 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001623 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1624 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001625 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001626 /* we count the decorators in when talking about the class' or
1627 * function's line number */
1628 if (thing) {
1629 thing->lineno = LINENO(n);
1630 thing->col_offset = n->n_col_offset;
1631 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001632 return thing;
1633}
1634
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635static expr_ty
1636ast_for_lambdef(struct compiling *c, const node *n)
1637{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001638 /* lambdef: 'lambda' [varargslist] ':' test
1639 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 arguments_ty args;
1641 expr_ty expression;
1642
1643 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001644 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 if (!args)
1646 return NULL;
1647 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001648 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 }
1651 else {
1652 args = ast_for_arguments(c, CHILD(n, 1));
1653 if (!args)
1654 return NULL;
1655 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001656 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 }
1659
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001660 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661}
1662
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001663static expr_ty
1664ast_for_ifexpr(struct compiling *c, const node *n)
1665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001667 expr_ty expression, body, orelse;
1668
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001669 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001670 body = ast_for_expr(c, CHILD(n, 0));
1671 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001672 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001673 expression = ast_for_expr(c, CHILD(n, 2));
1674 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001675 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001676 orelse = ast_for_expr(c, CHILD(n, 4));
1677 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001678 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001679 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1680 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001681}
1682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001684 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685
Nick Coghlan650f0d02007-04-15 12:05:43 +00001686 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687*/
1688
1689static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001690count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001692 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693
Guido van Rossumd8faa362007-04-27 19:54:29 +00001694 count_comp_for:
1695 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001696 REQ(n, comp_for);
1697 if (NCH(n) == 5)
1698 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001699 else
1700 return n_fors;
1701 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001702 REQ(n, comp_iter);
1703 n = CHILD(n, 0);
1704 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001705 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001706 else if (TYPE(n) == comp_if) {
1707 if (NCH(n) == 3) {
1708 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001709 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001710 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001711 else
1712 return n_fors;
1713 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001714
Guido van Rossumd8faa362007-04-27 19:54:29 +00001715 /* Should never be reached */
1716 PyErr_SetString(PyExc_SystemError,
1717 "logic error in count_comp_fors");
1718 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719}
1720
Nick Coghlan650f0d02007-04-15 12:05:43 +00001721/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722
Nick Coghlan650f0d02007-04-15 12:05:43 +00001723 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724*/
1725
1726static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001727count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001729 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730
Guido van Rossumd8faa362007-04-27 19:54:29 +00001731 while (1) {
1732 REQ(n, comp_iter);
1733 if (TYPE(CHILD(n, 0)) == comp_for)
1734 return n_ifs;
1735 n = CHILD(n, 0);
1736 REQ(n, comp_if);
1737 n_ifs++;
1738 if (NCH(n) == 2)
1739 return n_ifs;
1740 n = CHILD(n, 2);
1741 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742}
1743
Guido van Rossum992d4a32007-07-11 13:09:30 +00001744static asdl_seq *
1745ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001748 asdl_seq *comps;
1749
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001750 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 if (n_fors == -1)
1752 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001753
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001754 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001755 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001757
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001759 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001761 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001762 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765
Guido van Rossum992d4a32007-07-11 13:09:30 +00001766 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001767 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001768 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001770 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001771 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001773
Thomas Wouters89f507f2006-12-13 04:49:30 +00001774 /* Check the # of children rather than the length of t, since
1775 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001776 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001777 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001778 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001780 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1781 c->c_arena),
1782 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001783 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001785
Guido van Rossum992d4a32007-07-11 13:09:30 +00001786 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 int j, n_ifs;
1788 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789
Guido van Rossum992d4a32007-07-11 13:09:30 +00001790 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001791 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001795 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001796 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001798
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001800 REQ(n, comp_iter);
1801 n = CHILD(n, 0);
1802 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803
Guido van Rossum992d4a32007-07-11 13:09:30 +00001804 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001805 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001806 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001807 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001808 if (NCH(n) == 3)
1809 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001811 /* on exit, must guarantee that n is a comp_for */
1812 if (TYPE(n) == comp_iter)
1813 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001814 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001816 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001818 return comps;
1819}
1820
1821static expr_ty
1822ast_for_itercomp(struct compiling *c, const node *n, int type)
1823{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001824 /* testlist_comp: (test|star_expr)
1825 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001826 expr_ty elt;
1827 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001828 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829
Guido van Rossum992d4a32007-07-11 13:09:30 +00001830 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001832 ch = CHILD(n, 0);
1833 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001834 if (!elt)
1835 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001836 if (elt->kind == Starred_kind) {
1837 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1838 return NULL;
1839 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840
Guido van Rossum992d4a32007-07-11 13:09:30 +00001841 comps = ast_for_comprehension(c, CHILD(n, 1));
1842 if (!comps)
1843 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001844
1845 if (type == COMP_GENEXP)
1846 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1847 else if (type == COMP_LISTCOMP)
1848 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1849 else if (type == COMP_SETCOMP)
1850 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1851 else
1852 /* Should never happen */
1853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854}
1855
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001856/* Fills in the key, value pair corresponding to the dict element. In case
1857 * of an unpacking, key is NULL. *i is advanced by the number of ast
1858 * elements. Iff successful, nonzero is returned.
1859 */
1860static int
1861ast_for_dictelement(struct compiling *c, const node *n, int *i,
1862 expr_ty *key, expr_ty *value)
1863{
1864 expr_ty expression;
1865 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1866 assert(NCH(n) - *i >= 2);
1867
1868 expression = ast_for_expr(c, CHILD(n, *i + 1));
1869 if (!expression)
1870 return 0;
1871 *key = NULL;
1872 *value = expression;
1873
1874 *i += 2;
1875 }
1876 else {
1877 assert(NCH(n) - *i >= 3);
1878
1879 expression = ast_for_expr(c, CHILD(n, *i));
1880 if (!expression)
1881 return 0;
1882 *key = expression;
1883
1884 REQ(CHILD(n, *i + 1), COLON);
1885
1886 expression = ast_for_expr(c, CHILD(n, *i + 2));
1887 if (!expression)
1888 return 0;
1889 *value = expression;
1890
1891 *i += 3;
1892 }
1893 return 1;
1894}
1895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001897ast_for_dictcomp(struct compiling *c, const node *n)
1898{
1899 expr_ty key, value;
1900 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001901 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001903 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001905 assert(key);
1906 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001908 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001909 if (!comps)
1910 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911
Guido van Rossum992d4a32007-07-11 13:09:30 +00001912 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1913}
1914
1915static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001916ast_for_dictdisplay(struct compiling *c, const node *n)
1917{
1918 int i;
1919 int j;
1920 int size;
1921 asdl_seq *keys, *values;
1922
1923 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1924 keys = _Py_asdl_seq_new(size, c->c_arena);
1925 if (!keys)
1926 return NULL;
1927
1928 values = _Py_asdl_seq_new(size, c->c_arena);
1929 if (!values)
1930 return NULL;
1931
1932 j = 0;
1933 for (i = 0; i < NCH(n); i++) {
1934 expr_ty key, value;
1935
1936 if (!ast_for_dictelement(c, n, &i, &key, &value))
1937 return NULL;
1938 asdl_seq_SET(keys, j, key);
1939 asdl_seq_SET(values, j, value);
1940
1941 j++;
1942 }
1943 keys->size = j;
1944 values->size = j;
1945 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1946}
1947
1948static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001949ast_for_genexp(struct compiling *c, const node *n)
1950{
1951 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001952 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001953}
1954
1955static expr_ty
1956ast_for_listcomp(struct compiling *c, const node *n)
1957{
1958 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001959 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001960}
1961
1962static expr_ty
1963ast_for_setcomp(struct compiling *c, const node *n)
1964{
1965 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001966 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001967}
1968
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001969static expr_ty
1970ast_for_setdisplay(struct compiling *c, const node *n)
1971{
1972 int i;
1973 int size;
1974 asdl_seq *elts;
1975
1976 assert(TYPE(n) == (dictorsetmaker));
1977 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
1978 elts = _Py_asdl_seq_new(size, c->c_arena);
1979 if (!elts)
1980 return NULL;
1981 for (i = 0; i < NCH(n); i += 2) {
1982 expr_ty expression;
1983 expression = ast_for_expr(c, CHILD(n, i));
1984 if (!expression)
1985 return NULL;
1986 asdl_seq_SET(elts, i / 2, expression);
1987 }
1988 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1989}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001990
1991static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992ast_for_atom(struct compiling *c, const node *n)
1993{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001994 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1995 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001996 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 */
1998 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001999 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002002 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002003 PyObject *name;
2004 const char *s = STR(ch);
2005 size_t len = strlen(s);
2006 if (len >= 4 && len <= 5) {
2007 if (!strcmp(s, "None"))
2008 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2009 if (!strcmp(s, "True"))
2010 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2011 if (!strcmp(s, "False"))
2012 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2013 }
2014 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002015 if (!name)
2016 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002017 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002018 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2019 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00002021 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002022 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002023 const char *errtype = NULL;
2024 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2025 errtype = "unicode error";
2026 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2027 errtype = "value error";
2028 if (errtype) {
2029 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002030 PyObject *type, *value, *tback, *errstr;
2031 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002032 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002033 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002034 char *s = _PyUnicode_AsString(errstr);
2035 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002036 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002037 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002038 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002039 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002040 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002041 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002042 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002043 Py_XDECREF(tback);
2044 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002045 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002046 }
Victor Stinner43d81952013-07-17 00:57:58 +02002047 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2048 Py_DECREF(str);
2049 return NULL;
2050 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00002051 if (bytesmode)
2052 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
2053 else
2054 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 }
2056 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002057 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002058 if (!pynum)
2059 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002060
Victor Stinner43d81952013-07-17 00:57:58 +02002061 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2062 Py_DECREF(pynum);
2063 return NULL;
2064 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002065 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 }
Georg Brandldde00282007-03-18 19:01:53 +00002067 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002068 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002070 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071
Thomas Wouters89f507f2006-12-13 04:49:30 +00002072 if (TYPE(ch) == RPAR)
2073 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 if (TYPE(ch) == yield_expr)
2076 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002079 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002080 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002081
Nick Coghlan650f0d02007-04-15 12:05:43 +00002082 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002084 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085
Thomas Wouters89f507f2006-12-13 04:49:30 +00002086 if (TYPE(ch) == RSQB)
2087 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088
Nick Coghlan650f0d02007-04-15 12:05:43 +00002089 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002090 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2091 asdl_seq *elts = seq_for_testlist(c, ch);
2092 if (!elts)
2093 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002094
Thomas Wouters89f507f2006-12-13 04:49:30 +00002095 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2096 }
2097 else
2098 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002100 /* dictorsetmaker: ( ((test ':' test | '**' test)
2101 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2102 * ((test | '*' test)
2103 * (comp_for | (',' (test | '*' test))* [','])) ) */
Neal Norwitzc1505362006-12-28 06:47:50 +00002104 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002105 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002106 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002107 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002108 }
2109 else {
2110 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2111 if (NCH(ch) == 1 ||
2112 (NCH(ch) > 1 &&
2113 TYPE(CHILD(ch, 1)) == COMMA)) {
2114 /* It's a set display. */
2115 return ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002116 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002117 else if (NCH(ch) > 1 &&
2118 TYPE(CHILD(ch, 1)) == comp_for) {
2119 /* It's a set comprehension. */
2120 return ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002121 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002122 else if (NCH(ch) > 3 - is_dict &&
2123 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2124 /* It's a dictionary comprehension. */
2125 if (is_dict) {
2126 ast_error(c, n, "dict unpacking cannot be used in "
2127 "dict comprehension");
2128 return NULL;
2129 }
2130 return ast_for_dictcomp(c, ch);
2131 }
2132 else {
2133 /* It's a dictionary display. */
2134 return ast_for_dictdisplay(c, ch);
2135 }
Guido van Rossum86e58e22006-08-28 15:27:34 +00002136 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 }
2142}
2143
2144static slice_ty
2145ast_for_slice(struct compiling *c, const node *n)
2146{
2147 node *ch;
2148 expr_ty lower = NULL, upper = NULL, step = NULL;
2149
2150 REQ(n, subscript);
2151
2152 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002153 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 sliceop: ':' [test]
2155 */
2156 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 if (NCH(n) == 1 && TYPE(ch) == test) {
2158 /* 'step' variable hold no significance in terms of being used over
2159 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 if (!step)
2162 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163
Thomas Wouters89f507f2006-12-13 04:49:30 +00002164 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 }
2166
2167 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002168 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 if (!lower)
2170 return NULL;
2171 }
2172
2173 /* If there's an upper bound it's in the second or third position. */
2174 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002175 if (NCH(n) > 1) {
2176 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 if (TYPE(n2) == test) {
2179 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 if (!upper)
2181 return NULL;
2182 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002183 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002185 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186
Thomas Wouters89f507f2006-12-13 04:49:30 +00002187 if (TYPE(n2) == test) {
2188 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 if (!upper)
2190 return NULL;
2191 }
2192 }
2193
2194 ch = CHILD(n, NCH(n) - 1);
2195 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002196 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002197 ch = CHILD(ch, 1);
2198 if (TYPE(ch) == test) {
2199 step = ast_for_expr(c, ch);
2200 if (!step)
2201 return NULL;
2202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 }
2204 }
2205
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002206 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207}
2208
2209static expr_ty
2210ast_for_binop(struct compiling *c, const node *n)
2211{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002212 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002214 BinOp(BinOp(A, op, B), op, C).
2215 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216
Guido van Rossumd8faa362007-04-27 19:54:29 +00002217 int i, nops;
2218 expr_ty expr1, expr2, result;
2219 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220
Guido van Rossumd8faa362007-04-27 19:54:29 +00002221 expr1 = ast_for_expr(c, CHILD(n, 0));
2222 if (!expr1)
2223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224
Guido van Rossumd8faa362007-04-27 19:54:29 +00002225 expr2 = ast_for_expr(c, CHILD(n, 2));
2226 if (!expr2)
2227 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228
Guido van Rossumd8faa362007-04-27 19:54:29 +00002229 newoperator = get_operator(CHILD(n, 1));
2230 if (!newoperator)
2231 return NULL;
2232
2233 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2234 c->c_arena);
2235 if (!result)
2236 return NULL;
2237
2238 nops = (NCH(n) - 1) / 2;
2239 for (i = 1; i < nops; i++) {
2240 expr_ty tmp_result, tmp;
2241 const node* next_oper = CHILD(n, i * 2 + 1);
2242
2243 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002244 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 return NULL;
2246
Guido van Rossumd8faa362007-04-27 19:54:29 +00002247 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2248 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 return NULL;
2250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002252 LINENO(next_oper), next_oper->n_col_offset,
2253 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002255 return NULL;
2256 result = tmp_result;
2257 }
2258 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259}
2260
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002261static expr_ty
2262ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002265 subscriptlist: subscript (',' subscript)* [',']
2266 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2267 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002268 REQ(n, trailer);
2269 if (TYPE(CHILD(n, 0)) == LPAR) {
2270 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002271 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002272 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002273 else
2274 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002275 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002276 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002277 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2278 if (!attr_id)
2279 return NULL;
2280 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002281 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002282 }
2283 else {
2284 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002285 REQ(CHILD(n, 2), RSQB);
2286 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002287 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002288 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2289 if (!slc)
2290 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002291 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2292 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002293 }
2294 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002296 by treating the sequence as a tuple literal if there are
2297 no slice features.
2298 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002299 int j;
2300 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002301 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002302 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002303 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002304 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002305 if (!slices)
2306 return NULL;
2307 for (j = 0; j < NCH(n); j += 2) {
2308 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002309 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002310 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002311 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002312 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002313 asdl_seq_SET(slices, j / 2, slc);
2314 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002315 if (!simple) {
2316 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002317 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002318 }
2319 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002320 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002321 if (!elts)
2322 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002323 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2324 slc = (slice_ty)asdl_seq_GET(slices, j);
2325 assert(slc->kind == Index_kind && slc->v.Index.value);
2326 asdl_seq_SET(elts, j, slc->v.Index.value);
2327 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002328 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002329 if (!e)
2330 return NULL;
2331 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002332 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002333 }
2334 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002335}
2336
2337static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002338ast_for_factor(struct compiling *c, const node *n)
2339{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002340 expr_ty expression;
2341
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002342 expression = ast_for_expr(c, CHILD(n, 1));
2343 if (!expression)
2344 return NULL;
2345
2346 switch (TYPE(CHILD(n, 0))) {
2347 case PLUS:
2348 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2349 c->c_arena);
2350 case MINUS:
2351 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2352 c->c_arena);
2353 case TILDE:
2354 return UnaryOp(Invert, expression, LINENO(n),
2355 n->n_col_offset, c->c_arena);
2356 }
2357 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2358 TYPE(CHILD(n, 0)));
2359 return NULL;
2360}
2361
2362static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002363ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364{
Yury Selivanov75445082015-05-11 22:57:16 -04002365 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002366 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002367
2368 REQ(n, atom_expr);
2369 nch = NCH(n);
2370
2371 if (TYPE(CHILD(n, 0)) == AWAIT) {
2372 start = 1;
2373 assert(nch > 1);
2374 }
2375
2376 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002377 if (!e)
2378 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002379 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002380 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002381 if (start && nch == 2) {
2382 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2383 }
2384
2385 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 node *ch = CHILD(n, i);
2387 if (TYPE(ch) != trailer)
2388 break;
2389 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002390 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002391 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002392 tmp->lineno = e->lineno;
2393 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002394 e = tmp;
2395 }
Yury Selivanov75445082015-05-11 22:57:16 -04002396
2397 if (start) {
2398 /* there was an AWAIT */
2399 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2400 }
2401 else {
2402 return e;
2403 }
2404}
2405
2406static expr_ty
2407ast_for_power(struct compiling *c, const node *n)
2408{
2409 /* power: atom trailer* ('**' factor)*
2410 */
2411 expr_ty e;
2412 REQ(n, power);
2413 e = ast_for_atom_expr(c, CHILD(n, 0));
2414 if (!e)
2415 return NULL;
2416 if (NCH(n) == 1)
2417 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002418 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2419 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002420 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002421 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002422 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002423 }
2424 return e;
2425}
2426
Guido van Rossum0368b722007-05-11 16:50:42 +00002427static expr_ty
2428ast_for_starred(struct compiling *c, const node *n)
2429{
2430 expr_ty tmp;
2431 REQ(n, star_expr);
2432
2433 tmp = ast_for_expr(c, CHILD(n, 1));
2434 if (!tmp)
2435 return NULL;
2436
2437 /* The Load context is changed later. */
2438 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2439}
2440
2441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442/* Do not name a variable 'expr'! Will cause a compile error.
2443*/
2444
2445static expr_ty
2446ast_for_expr(struct compiling *c, const node *n)
2447{
2448 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002449 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002450 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 and_test: not_test ('and' not_test)*
2453 not_test: 'not' not_test | comparison
2454 comparison: expr (comp_op expr)*
2455 expr: xor_expr ('|' xor_expr)*
2456 xor_expr: and_expr ('^' and_expr)*
2457 and_expr: shift_expr ('&' shift_expr)*
2458 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2459 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002460 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002462 power: atom_expr ['**' factor]
2463 atom_expr: [AWAIT] atom trailer*
2464 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 */
2466
2467 asdl_seq *seq;
2468 int i;
2469
2470 loop:
2471 switch (TYPE(n)) {
2472 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002473 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002474 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002475 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002477 else if (NCH(n) > 1)
2478 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 /* Fallthrough */
2480 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 case and_test:
2482 if (NCH(n) == 1) {
2483 n = CHILD(n, 0);
2484 goto loop;
2485 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002486 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 if (!seq)
2488 return NULL;
2489 for (i = 0; i < NCH(n); i += 2) {
2490 expr_ty e = ast_for_expr(c, CHILD(n, i));
2491 if (!e)
2492 return NULL;
2493 asdl_seq_SET(seq, i / 2, e);
2494 }
2495 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002496 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2497 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002498 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002499 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 case not_test:
2501 if (NCH(n) == 1) {
2502 n = CHILD(n, 0);
2503 goto loop;
2504 }
2505 else {
2506 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2507 if (!expression)
2508 return NULL;
2509
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002510 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2511 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 }
2513 case comparison:
2514 if (NCH(n) == 1) {
2515 n = CHILD(n, 0);
2516 goto loop;
2517 }
2518 else {
2519 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002520 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002521 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002522 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 if (!ops)
2524 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002525 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 return NULL;
2528 }
2529 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002530 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002532 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536
2537 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002538 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002540 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002542 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 asdl_seq_SET(cmps, i / 2, expression);
2544 }
2545 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002546 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002550 return Compare(expression, ops, cmps, LINENO(n),
2551 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 }
2553 break;
2554
Guido van Rossum0368b722007-05-11 16:50:42 +00002555 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 /* The next five cases all handle BinOps. The main body of code
2558 is the same in each case, but the switch turned inside out to
2559 reuse the code for each type of operator.
2560 */
2561 case expr:
2562 case xor_expr:
2563 case and_expr:
2564 case shift_expr:
2565 case arith_expr:
2566 case term:
2567 if (NCH(n) == 1) {
2568 n = CHILD(n, 0);
2569 goto loop;
2570 }
2571 return ast_for_binop(c, n);
2572 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002573 node *an = NULL;
2574 node *en = NULL;
2575 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002576 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002577 if (NCH(n) > 1)
2578 an = CHILD(n, 1); /* yield_arg */
2579 if (an) {
2580 en = CHILD(an, NCH(an) - 1);
2581 if (NCH(an) == 2) {
2582 is_from = 1;
2583 exp = ast_for_expr(c, en);
2584 }
2585 else
2586 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002587 if (!exp)
2588 return NULL;
2589 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002590 if (is_from)
2591 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2592 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002593 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002594 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 if (NCH(n) == 1) {
2596 n = CHILD(n, 0);
2597 goto loop;
2598 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002599 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002600 case power:
2601 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002603 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 return NULL;
2605 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002606 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 return NULL;
2608}
2609
2610static expr_ty
2611ast_for_call(struct compiling *c, const node *n, expr_ty func)
2612{
2613 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002614 arglist: argument (',' argument)* [',']
2615 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 */
2617
2618 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002619 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002620 asdl_seq *args;
2621 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622
2623 REQ(n, arglist);
2624
2625 nargs = 0;
2626 nkeywords = 0;
2627 ngens = 0;
2628 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002629 node *ch = CHILD(n, i);
2630 if (TYPE(ch) == argument) {
2631 if (NCH(ch) == 1)
2632 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002633 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002634 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002635 else if (TYPE(CHILD(ch, 0)) == STAR)
2636 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002638 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002639 nkeywords++;
2640 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
2642 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002643 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002644 "if not sole argument");
2645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 }
2647
2648 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002649 ast_error(c, n, "more than 255 arguments");
2650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 }
2652
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002653 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002655 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002656 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002658 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002659
2660 nargs = 0; /* positional arguments + iterable argument unpackings */
2661 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2662 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002664 node *ch = CHILD(n, i);
2665 if (TYPE(ch) == argument) {
2666 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002667 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002668 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002669 /* a positional argument */
2670 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002671 if (ndoublestars) {
2672 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002673 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002674 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002675 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002676 else {
2677 ast_error(c, chch,
2678 "positional argument follows "
2679 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002680 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002681 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002682 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002683 e = ast_for_expr(c, chch);
2684 if (!e)
2685 return NULL;
2686 asdl_seq_SET(args, nargs++, e);
2687 }
2688 else if (TYPE(chch) == STAR) {
2689 /* an iterable argument unpacking */
2690 expr_ty starred;
2691 if (ndoublestars) {
2692 ast_error(c, chch,
2693 "iterable argument unpacking follows "
2694 "keyword argument unpacking");
2695 return NULL;
2696 }
2697 e = ast_for_expr(c, CHILD(ch, 1));
2698 if (!e)
2699 return NULL;
2700 starred = Starred(e, Load, LINENO(chch),
2701 chch->n_col_offset,
2702 c->c_arena);
2703 if (!starred)
2704 return NULL;
2705 asdl_seq_SET(args, nargs++, starred);
2706
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002707 }
2708 else if (TYPE(chch) == DOUBLESTAR) {
2709 /* a keyword argument unpacking */
2710 keyword_ty kw;
2711 i++;
2712 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002714 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002715 kw = keyword(NULL, e, c->c_arena);
2716 asdl_seq_SET(keywords, nkeywords++, kw);
2717 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002719 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002720 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002721 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002723 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002724 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002727 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002728 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002729 identifier key, tmp;
2730 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 /* chch is test, but must be an identifier? */
2733 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 /* f(lambda x: x[0] = 3) ends up getting parsed with
2737 * LHS test = lambda x: x[0], and RHS test = 3.
2738 * SF bug 132313 points out that complaining about a keyword
2739 * then is very confusing.
2740 */
2741 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002742 ast_error(c, chch,
2743 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002744 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002745 }
2746 else if (e->kind != Name_kind) {
2747 ast_error(c, chch,
2748 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002749 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002750 }
2751 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002752 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002754 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002755 for (k = 0; k < nkeywords; k++) {
2756 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757 if (tmp && !PyUnicode_Compare(tmp, key)) {
2758 ast_error(c, chch,
2759 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002760 return NULL;
2761 }
2762 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002763 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002765 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002766 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002768 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002769 asdl_seq_SET(keywords, nkeywords++, kw);
2770 }
2771 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 }
2773
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775}
2776
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002778ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002780 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002781 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002783 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002784 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002785 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002786 }
2787 else {
2788 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002789 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002792 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 else {
2794 asdl_seq *tmp = seq_for_testlist(c, n);
2795 if (!tmp)
2796 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002797 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002799}
2800
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801static stmt_ty
2802ast_for_expr_stmt(struct compiling *c, const node *n)
2803{
2804 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002807 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002808 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002809 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 test: ... here starts the operator precendence dance
2811 */
2812
2813 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002814 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 if (!e)
2816 return NULL;
2817
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 }
2820 else if (TYPE(CHILD(n, 1)) == augassign) {
2821 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002822 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002823 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
Thomas Wouters89f507f2006-12-13 04:49:30 +00002825 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 if (!expr1)
2827 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002828 if(!set_context(c, expr1, Store, ch))
2829 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002830 /* set_context checks that most expressions are not the left side.
2831 Augmented assignments can only have a name, a subscript, or an
2832 attribute on the left, though, so we have to explicitly check for
2833 those. */
2834 switch (expr1->kind) {
2835 case Name_kind:
2836 case Attribute_kind:
2837 case Subscript_kind:
2838 break;
2839 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002840 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002841 return NULL;
2842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Thomas Wouters89f507f2006-12-13 04:49:30 +00002844 ch = CHILD(n, 2);
2845 if (TYPE(ch) == testlist)
2846 expr2 = ast_for_testlist(c, ch);
2847 else
2848 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002849 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 return NULL;
2851
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002852 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002853 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 return NULL;
2855
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 }
2858 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 int i;
2860 asdl_seq *targets;
2861 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 expr_ty expression;
2863
Thomas Wouters89f507f2006-12-13 04:49:30 +00002864 /* a normal assignment */
2865 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002866 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002867 if (!targets)
2868 return NULL;
2869 for (i = 0; i < NCH(n) - 2; i += 2) {
2870 expr_ty e;
2871 node *ch = CHILD(n, i);
2872 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002873 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002874 return NULL;
2875 }
2876 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002878 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002880 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002881 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 asdl_seq_SET(targets, i / 2, e);
2885 }
2886 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002887 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 expression = ast_for_testlist(c, value);
2889 else
2890 expression = ast_for_expr(c, value);
2891 if (!expression)
2892 return NULL;
2893 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895}
2896
Benjamin Peterson78565b22009-06-28 19:19:51 +00002897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002899ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900{
2901 asdl_seq *seq;
2902 int i;
2903 expr_ty e;
2904
2905 REQ(n, exprlist);
2906
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002907 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 e = ast_for_expr(c, CHILD(n, i));
2912 if (!e)
2913 return NULL;
2914 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002915 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 }
2918 return seq;
2919}
2920
2921static stmt_ty
2922ast_for_del_stmt(struct compiling *c, const node *n)
2923{
2924 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 /* del_stmt: 'del' exprlist */
2927 REQ(n, del_stmt);
2928
2929 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2930 if (!expr_list)
2931 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002932 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933}
2934
2935static stmt_ty
2936ast_for_flow_stmt(struct compiling *c, const node *n)
2937{
2938 /*
2939 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2940 | yield_stmt
2941 break_stmt: 'break'
2942 continue_stmt: 'continue'
2943 return_stmt: 'return' [testlist]
2944 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002945 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 raise_stmt: 'raise' [test [',' test [',' test]]]
2947 */
2948 node *ch;
2949
2950 REQ(n, flow_stmt);
2951 ch = CHILD(n, 0);
2952 switch (TYPE(ch)) {
2953 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002954 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002956 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002958 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2959 if (!exp)
2960 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002961 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 }
2963 case return_stmt:
2964 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002965 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002967 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 if (!expression)
2969 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002970 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 }
2972 case raise_stmt:
2973 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002974 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2975 else if (NCH(ch) >= 2) {
2976 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2978 if (!expression)
2979 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002980 if (NCH(ch) == 4) {
2981 cause = ast_for_expr(c, CHILD(ch, 3));
2982 if (!cause)
2983 return NULL;
2984 }
2985 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 }
2987 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002988 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 "unexpected flow_stmt: %d", TYPE(ch));
2990 return NULL;
2991 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002992
2993 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2994 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995}
2996
2997static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00002998alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999{
3000 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003001 import_as_name: NAME ['as' NAME]
3002 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 dotted_name: NAME ('.' NAME)*
3004 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003005 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003006
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 loop:
3008 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003009 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003010 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003011 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003012 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003013 if (!name)
3014 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003015 if (NCH(n) == 3) {
3016 node *str_node = CHILD(n, 2);
3017 str = NEW_IDENTIFIER(str_node);
3018 if (!str)
3019 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003020 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003021 return NULL;
3022 }
3023 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003024 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003025 return NULL;
3026 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003027 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003028 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 case dotted_as_name:
3030 if (NCH(n) == 1) {
3031 n = CHILD(n, 0);
3032 goto loop;
3033 }
3034 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003035 node *asname_node = CHILD(n, 2);
3036 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003037 if (!a)
3038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003040 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003041 if (!a->asname)
3042 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003043 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 return a;
3046 }
3047 break;
3048 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003049 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003050 node *name_node = CHILD(n, 0);
3051 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003052 if (!name)
3053 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003054 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003055 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003056 return alias(name, NULL, c->c_arena);
3057 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 else {
3059 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003060 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003061 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064
3065 len = 0;
3066 for (i = 0; i < NCH(n); i += 2)
3067 /* length of string plus one for the dot */
3068 len += strlen(STR(CHILD(n, i))) + 1;
3069 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003070 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 if (!str)
3072 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003073 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 if (!s)
3075 return NULL;
3076 for (i = 0; i < NCH(n); i += 2) {
3077 char *sch = STR(CHILD(n, i));
3078 strcpy(s, STR(CHILD(n, i)));
3079 s += strlen(sch);
3080 *s++ = '.';
3081 }
3082 --s;
3083 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3085 PyBytes_GET_SIZE(str),
3086 NULL);
3087 Py_DECREF(str);
3088 if (!uni)
3089 return NULL;
3090 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003091 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003092 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3093 Py_DECREF(str);
3094 return NULL;
3095 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003096 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097 }
3098 break;
3099 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003100 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003101 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3102 Py_DECREF(str);
3103 return NULL;
3104 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003105 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003107 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 "unexpected import name: %d", TYPE(n));
3109 return NULL;
3110 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003111
3112 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 return NULL;
3114}
3115
3116static stmt_ty
3117ast_for_import_stmt(struct compiling *c, const node *n)
3118{
3119 /*
3120 import_stmt: import_name | import_from
3121 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003122 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3123 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 int lineno;
3126 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 int i;
3128 asdl_seq *aliases;
3129
3130 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003131 lineno = LINENO(n);
3132 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003134 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003136 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003137 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003138 if (!aliases)
3139 return NULL;
3140 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003141 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003142 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003144 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003146 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003148 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003150 int idx, ndots = 0;
3151 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003152 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003154 /* Count the number of dots (for relative imports) and check for the
3155 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003156 for (idx = 1; idx < NCH(n); idx++) {
3157 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003158 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3159 if (!mod)
3160 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003161 idx++;
3162 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003163 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003165 ndots += 3;
3166 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003167 } else if (TYPE(CHILD(n, idx)) != DOT) {
3168 break;
3169 }
3170 ndots++;
3171 }
3172 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003173 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003174 case STAR:
3175 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003176 n = CHILD(n, idx);
3177 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003178 break;
3179 case LPAR:
3180 /* from ... import (x, y, z) */
3181 n = CHILD(n, idx + 1);
3182 n_children = NCH(n);
3183 break;
3184 case import_as_names:
3185 /* from ... import x, y, z */
3186 n = CHILD(n, idx);
3187 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003188 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003189 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 " surrounding parentheses");
3191 return NULL;
3192 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003193 break;
3194 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003195 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 return NULL;
3197 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003199 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003200 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
3203 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003204 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003205 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003206 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003208 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003210 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003211 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003212 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003213 if (!import_alias)
3214 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003215 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003216 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003218 if (mod != NULL)
3219 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003220 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003221 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 }
Neal Norwitz79792652005-11-14 04:25:03 +00003223 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 "unknown import statement: starts with command '%s'",
3225 STR(CHILD(n, 0)));
3226 return NULL;
3227}
3228
3229static stmt_ty
3230ast_for_global_stmt(struct compiling *c, const node *n)
3231{
3232 /* global_stmt: 'global' NAME (',' NAME)* */
3233 identifier name;
3234 asdl_seq *s;
3235 int i;
3236
3237 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003238 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003242 name = NEW_IDENTIFIER(CHILD(n, i));
3243 if (!name)
3244 return NULL;
3245 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003247 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248}
3249
3250static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003251ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3252{
3253 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3254 identifier name;
3255 asdl_seq *s;
3256 int i;
3257
3258 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003259 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003260 if (!s)
3261 return NULL;
3262 for (i = 1; i < NCH(n); i += 2) {
3263 name = NEW_IDENTIFIER(CHILD(n, i));
3264 if (!name)
3265 return NULL;
3266 asdl_seq_SET(s, i / 2, name);
3267 }
3268 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3269}
3270
3271static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272ast_for_assert_stmt(struct compiling *c, const node *n)
3273{
3274 /* assert_stmt: 'assert' test [',' test] */
3275 REQ(n, assert_stmt);
3276 if (NCH(n) == 2) {
3277 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3278 if (!expression)
3279 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003280 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281 }
3282 else if (NCH(n) == 4) {
3283 expr_ty expr1, expr2;
3284
3285 expr1 = ast_for_expr(c, CHILD(n, 1));
3286 if (!expr1)
3287 return NULL;
3288 expr2 = ast_for_expr(c, CHILD(n, 3));
3289 if (!expr2)
3290 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291
Thomas Wouters89f507f2006-12-13 04:49:30 +00003292 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 }
Neal Norwitz79792652005-11-14 04:25:03 +00003294 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 "improper number of parts to 'assert' statement: %d",
3296 NCH(n));
3297 return NULL;
3298}
3299
3300static asdl_seq *
3301ast_for_suite(struct compiling *c, const node *n)
3302{
3303 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003304 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 stmt_ty s;
3306 int i, total, num, end, pos = 0;
3307 node *ch;
3308
3309 REQ(n, suite);
3310
3311 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003312 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 n = CHILD(n, 0);
3317 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003319 */
3320 end = NCH(n) - 1;
3321 if (TYPE(CHILD(n, end - 1)) == SEMI)
3322 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324 for (i = 0; i < end; i += 2) {
3325 ch = CHILD(n, i);
3326 s = ast_for_stmt(c, ch);
3327 if (!s)
3328 return NULL;
3329 asdl_seq_SET(seq, pos++, s);
3330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331 }
3332 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 for (i = 2; i < (NCH(n) - 1); i++) {
3334 ch = CHILD(n, i);
3335 REQ(ch, stmt);
3336 num = num_stmts(ch);
3337 if (num == 1) {
3338 /* small_stmt or compound_stmt with only one child */
3339 s = ast_for_stmt(c, ch);
3340 if (!s)
3341 return NULL;
3342 asdl_seq_SET(seq, pos++, s);
3343 }
3344 else {
3345 int j;
3346 ch = CHILD(ch, 0);
3347 REQ(ch, simple_stmt);
3348 for (j = 0; j < NCH(ch); j += 2) {
3349 /* statement terminates with a semi-colon ';' */
3350 if (NCH(CHILD(ch, j)) == 0) {
3351 assert((j + 1) == NCH(ch));
3352 break;
3353 }
3354 s = ast_for_stmt(c, CHILD(ch, j));
3355 if (!s)
3356 return NULL;
3357 asdl_seq_SET(seq, pos++, s);
3358 }
3359 }
3360 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 }
3362 assert(pos == seq->size);
3363 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364}
3365
3366static stmt_ty
3367ast_for_if_stmt(struct compiling *c, const node *n)
3368{
3369 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3370 ['else' ':' suite]
3371 */
3372 char *s;
3373
3374 REQ(n, if_stmt);
3375
3376 if (NCH(n) == 4) {
3377 expr_ty expression;
3378 asdl_seq *suite_seq;
3379
3380 expression = ast_for_expr(c, CHILD(n, 1));
3381 if (!expression)
3382 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003384 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386
Guido van Rossumd8faa362007-04-27 19:54:29 +00003387 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3388 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003390
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 s = STR(CHILD(n, 4));
3392 /* s[2], the third character in the string, will be
3393 's' for el_s_e, or
3394 'i' for el_i_f
3395 */
3396 if (s[2] == 's') {
3397 expr_ty expression;
3398 asdl_seq *seq1, *seq2;
3399
3400 expression = ast_for_expr(c, CHILD(n, 1));
3401 if (!expression)
3402 return NULL;
3403 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003404 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 return NULL;
3406 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003407 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 return NULL;
3409
Guido van Rossumd8faa362007-04-27 19:54:29 +00003410 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3411 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 }
3413 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003414 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003415 expr_ty expression;
3416 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003417 asdl_seq *orelse = NULL;
3418 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 /* must reference the child n_elif+1 since 'else' token is third,
3420 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003421 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3422 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3423 has_else = 1;
3424 n_elif -= 3;
3425 }
3426 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427
Thomas Wouters89f507f2006-12-13 04:49:30 +00003428 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003429 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003431 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003432 if (!orelse)
3433 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003435 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003437 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3438 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003440 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3441 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 asdl_seq_SET(orelse, 0,
3445 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003446 LINENO(CHILD(n, NCH(n) - 6)),
3447 CHILD(n, NCH(n) - 6)->n_col_offset,
3448 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003449 /* the just-created orelse handled the last elif */
3450 n_elif--;
3451 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452
Thomas Wouters89f507f2006-12-13 04:49:30 +00003453 for (i = 0; i < n_elif; i++) {
3454 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003455 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003456 if (!newobj)
3457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003459 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003462 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464
Thomas Wouters89f507f2006-12-13 04:49:30 +00003465 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003467 LINENO(CHILD(n, off)),
3468 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003469 orelse = newobj;
3470 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003471 expression = ast_for_expr(c, CHILD(n, 1));
3472 if (!expression)
3473 return NULL;
3474 suite_seq = ast_for_suite(c, CHILD(n, 3));
3475 if (!suite_seq)
3476 return NULL;
3477 return If(expression, suite_seq, orelse,
3478 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003480
3481 PyErr_Format(PyExc_SystemError,
3482 "unexpected token in 'if' statement: %s", s);
3483 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484}
3485
3486static stmt_ty
3487ast_for_while_stmt(struct compiling *c, const node *n)
3488{
3489 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3490 REQ(n, while_stmt);
3491
3492 if (NCH(n) == 4) {
3493 expr_ty expression;
3494 asdl_seq *suite_seq;
3495
3496 expression = ast_for_expr(c, CHILD(n, 1));
3497 if (!expression)
3498 return NULL;
3499 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003500 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003502 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503 }
3504 else if (NCH(n) == 7) {
3505 expr_ty expression;
3506 asdl_seq *seq1, *seq2;
3507
3508 expression = ast_for_expr(c, CHILD(n, 1));
3509 if (!expression)
3510 return NULL;
3511 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003512 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 return NULL;
3514 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003515 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 return NULL;
3517
Thomas Wouters89f507f2006-12-13 04:49:30 +00003518 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003520
3521 PyErr_Format(PyExc_SystemError,
3522 "wrong number of tokens for 'while' statement: %d",
3523 NCH(n));
3524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525}
3526
3527static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003528ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003530 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003532 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003533 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3535 REQ(n, for_stmt);
3536
3537 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003538 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 if (!seq)
3540 return NULL;
3541 }
3542
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003543 node_target = CHILD(n, 1);
3544 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003545 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003547 /* Check the # of children rather than the length of _target, since
3548 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003549 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003550 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003551 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003553 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003555 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003556 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557 return NULL;
3558 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003559 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 return NULL;
3561
Yury Selivanov75445082015-05-11 22:57:16 -04003562 if (is_async)
3563 return AsyncFor(target, expression, suite_seq, seq,
3564 LINENO(n), n->n_col_offset,
3565 c->c_arena);
3566 else
3567 return For(target, expression, suite_seq, seq,
3568 LINENO(n), n->n_col_offset,
3569 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570}
3571
3572static excepthandler_ty
3573ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3574{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003575 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 REQ(exc, except_clause);
3577 REQ(body, suite);
3578
3579 if (NCH(exc) == 1) {
3580 asdl_seq *suite_seq = ast_for_suite(c, body);
3581 if (!suite_seq)
3582 return NULL;
3583
Neal Norwitzad74aa82008-03-31 05:14:30 +00003584 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003585 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 }
3587 else if (NCH(exc) == 2) {
3588 expr_ty expression;
3589 asdl_seq *suite_seq;
3590
3591 expression = ast_for_expr(c, CHILD(exc, 1));
3592 if (!expression)
3593 return NULL;
3594 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003595 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
3597
Neal Norwitzad74aa82008-03-31 05:14:30 +00003598 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003599 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 }
3601 else if (NCH(exc) == 4) {
3602 asdl_seq *suite_seq;
3603 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003604 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003605 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003607 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003610 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 return NULL;
3612 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003613 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 return NULL;
3615
Neal Norwitzad74aa82008-03-31 05:14:30 +00003616 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003617 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003619
3620 PyErr_Format(PyExc_SystemError,
3621 "wrong number of children for 'except' clause: %d",
3622 NCH(exc));
3623 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624}
3625
3626static stmt_ty
3627ast_for_try_stmt(struct compiling *c, const node *n)
3628{
Neal Norwitzf599f422005-12-17 21:33:47 +00003629 const int nch = NCH(n);
3630 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003631 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 REQ(n, try_stmt);
3634
Neal Norwitzf599f422005-12-17 21:33:47 +00003635 body = ast_for_suite(c, CHILD(n, 2));
3636 if (body == NULL)
3637 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638
Neal Norwitzf599f422005-12-17 21:33:47 +00003639 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3640 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3641 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3642 /* we can assume it's an "else",
3643 because nch >= 9 for try-else-finally and
3644 it would otherwise have a type of except_clause */
3645 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3646 if (orelse == NULL)
3647 return NULL;
3648 n_except--;
3649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650
Neal Norwitzf599f422005-12-17 21:33:47 +00003651 finally = ast_for_suite(c, CHILD(n, nch - 1));
3652 if (finally == NULL)
3653 return NULL;
3654 n_except--;
3655 }
3656 else {
3657 /* we can assume it's an "else",
3658 otherwise it would have a type of except_clause */
3659 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3660 if (orelse == NULL)
3661 return NULL;
3662 n_except--;
3663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003665 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003666 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 return NULL;
3668 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669
Neal Norwitzf599f422005-12-17 21:33:47 +00003670 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003671 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003672 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003673 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003674 if (handlers == NULL)
3675 return NULL;
3676
3677 for (i = 0; i < n_except; i++) {
3678 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3679 CHILD(n, 5 + i * 3));
3680 if (!e)
3681 return NULL;
3682 asdl_seq_SET(handlers, i, e);
3683 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003684 }
3685
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003686 assert(finally != NULL || asdl_seq_LEN(handlers));
3687 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688}
3689
Georg Brandl0c315622009-05-25 21:10:36 +00003690/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003691static withitem_ty
3692ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003693{
3694 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003695
Georg Brandl0c315622009-05-25 21:10:36 +00003696 REQ(n, with_item);
3697 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003698 if (!context_expr)
3699 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003700 if (NCH(n) == 3) {
3701 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003702
3703 if (!optional_vars) {
3704 return NULL;
3705 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003706 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003707 return NULL;
3708 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003709 }
3710
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003711 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003712}
3713
Georg Brandl0c315622009-05-25 21:10:36 +00003714/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3715static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003716ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003717{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003718 int i, n_items;
3719 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003720
3721 REQ(n, with_stmt);
3722
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003723 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003724 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003725 if (!items)
3726 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003727 for (i = 1; i < NCH(n) - 2; i += 2) {
3728 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3729 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003730 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003731 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003732 }
3733
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003734 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3735 if (!body)
3736 return NULL;
3737
Yury Selivanov75445082015-05-11 22:57:16 -04003738 if (is_async)
3739 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3740 else
3741 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003742}
3743
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003745ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003747 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003748 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003749 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003750 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 REQ(n, classdef);
3753
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003754 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 s = ast_for_suite(c, CHILD(n, 3));
3756 if (!s)
3757 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003758 classname = NEW_IDENTIFIER(CHILD(n, 1));
3759 if (!classname)
3760 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003761 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003762 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003763 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3764 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003766
3767 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003768 s = ast_for_suite(c, CHILD(n,5));
3769 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003770 return NULL;
3771 classname = NEW_IDENTIFIER(CHILD(n, 1));
3772 if (!classname)
3773 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003774 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003775 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003776 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3777 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778 }
3779
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003780 /* class NAME '(' arglist ')' ':' suite */
3781 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003782 {
3783 PyObject *dummy_name;
3784 expr_ty dummy;
3785 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3786 if (!dummy_name)
3787 return NULL;
3788 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3789 call = ast_for_call(c, CHILD(n, 3), dummy);
3790 if (!call)
3791 return NULL;
3792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003794 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003796 classname = NEW_IDENTIFIER(CHILD(n, 1));
3797 if (!classname)
3798 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003799 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003800 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003801
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003802 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003803 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804}
3805
3806static stmt_ty
3807ast_for_stmt(struct compiling *c, const node *n)
3808{
3809 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003810 assert(NCH(n) == 1);
3811 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 }
3813 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003814 assert(num_stmts(n) == 1);
3815 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 }
3817 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003818 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003819 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3820 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003821 */
3822 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 case expr_stmt:
3824 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 case del_stmt:
3826 return ast_for_del_stmt(c, n);
3827 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003828 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 case flow_stmt:
3830 return ast_for_flow_stmt(c, n);
3831 case import_stmt:
3832 return ast_for_import_stmt(c, n);
3833 case global_stmt:
3834 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003835 case nonlocal_stmt:
3836 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 case assert_stmt:
3838 return ast_for_assert_stmt(c, n);
3839 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003840 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3842 TYPE(n), NCH(n));
3843 return NULL;
3844 }
3845 }
3846 else {
3847 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003848 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003849 */
3850 node *ch = CHILD(n, 0);
3851 REQ(n, compound_stmt);
3852 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 case if_stmt:
3854 return ast_for_if_stmt(c, ch);
3855 case while_stmt:
3856 return ast_for_while_stmt(c, ch);
3857 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003858 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 case try_stmt:
3860 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003861 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003862 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003864 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003866 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 case decorated:
3868 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003869 case async_stmt:
3870 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003872 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3874 TYPE(n), NCH(n));
3875 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003876 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 }
3878}
3879
3880static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003881parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003883 const char *end;
3884 long x;
3885 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003886 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003887 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003889 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003890 errno = 0;
3891 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003892 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003893 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003894 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003895 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003896 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003897 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003898 }
3899 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003900 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003901 if (*end == '\0') {
3902 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003903 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003904 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003905 }
3906 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003907 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003908 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003909 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3910 if (compl.imag == -1.0 && PyErr_Occurred())
3911 return NULL;
3912 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003913 }
3914 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003915 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003916 dx = PyOS_string_to_double(s, NULL, NULL);
3917 if (dx == -1.0 && PyErr_Occurred())
3918 return NULL;
3919 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921}
3922
3923static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003924decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003926 const char *s, *t;
3927 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003928 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3929 while (s < end && (*s & 0x80)) s++;
3930 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003931 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932}
3933
3934static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003935decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003937 PyObject *v, *u;
3938 char *buf;
3939 char *p;
3940 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003941
Guido van Rossumd8faa362007-04-27 19:54:29 +00003942 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003943 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003944 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003945 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003946 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003947 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003948 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3949 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3950 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003951 if (u == NULL)
3952 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003953 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003954 end = s + len;
3955 while (s < end) {
3956 if (*s == '\\') {
3957 *p++ = *s++;
3958 if (*s & 0x80) {
3959 strcpy(p, "u005c");
3960 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003961 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003962 }
3963 if (*s & 0x80) { /* XXX inefficient */
3964 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003965 int kind;
3966 void *data;
3967 Py_ssize_t len, i;
3968 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003969 if (w == NULL) {
3970 Py_DECREF(u);
3971 return NULL;
3972 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003973 kind = PyUnicode_KIND(w);
3974 data = PyUnicode_DATA(w);
3975 len = PyUnicode_GET_LENGTH(w);
3976 for (i = 0; i < len; i++) {
3977 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3978 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003979 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003980 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003981 /* Should be impossible to overflow */
3982 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003983 Py_DECREF(w);
3984 } else {
3985 *p++ = *s++;
3986 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003987 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003988 len = p - buf;
3989 s = buf;
3990 }
3991 if (rawmode)
3992 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3993 else
3994 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3995 Py_XDECREF(u);
3996 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997}
3998
3999/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00004000 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 * parsestr parses it, and returns the decoded Python string object.
4002 */
4003static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00004004parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004006 size_t len;
4007 const char *s = STR(n);
4008 int quote = Py_CHARMASK(*s);
4009 int rawmode = 0;
4010 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004011 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004012 while (!*bytesmode || !rawmode) {
4013 if (quote == 'b' || quote == 'B') {
4014 quote = *++s;
4015 *bytesmode = 1;
4016 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004017 else if (quote == 'u' || quote == 'U') {
4018 quote = *++s;
4019 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004020 else if (quote == 'r' || quote == 'R') {
4021 quote = *++s;
4022 rawmode = 1;
4023 }
4024 else {
4025 break;
4026 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004027 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004028 }
4029 if (quote != '\'' && quote != '\"') {
4030 PyErr_BadInternalCall();
4031 return NULL;
4032 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004033 s++;
4034 len = strlen(s);
4035 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004037 "string to parse is too long");
4038 return NULL;
4039 }
4040 if (s[--len] != quote) {
4041 PyErr_BadInternalCall();
4042 return NULL;
4043 }
4044 if (len >= 4 && s[0] == quote && s[1] == quote) {
4045 s += 2;
4046 len -= 2;
4047 if (s[--len] != quote || s[--len] != quote) {
4048 PyErr_BadInternalCall();
4049 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004050 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004051 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004052 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004053 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004055 if (*bytesmode) {
4056 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004057 const char *ch;
4058 for (ch = s; *ch; ch++) {
4059 if (Py_CHARMASK(*ch) >= 0x80) {
4060 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004061 "literal characters.");
4062 return NULL;
4063 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004064 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004065 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004066 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004067 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004068 if (rawmode || strchr(s, '\\') == NULL) {
4069 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004070 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004071 if (u == NULL || !*bytesmode)
4072 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004073 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004074 Py_DECREF(u);
4075 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004076 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00004077 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004078 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004079 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004081 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004082 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004084 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00004085 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086}
4087
Guido van Rossum29fd7122007-11-12 01:13:56 +00004088/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004089 * compile-time literal catenation, calling parsestr() on each piece, and
4090 * pasting the intermediate results together.
4091 */
4092static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00004093parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004095 PyObject *v;
4096 int i;
4097 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004098 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004099 if (v != NULL) {
4100 /* String literal concatenation */
4101 for (i = 1; i < NCH(n); i++) {
4102 PyObject *s;
4103 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004104 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004105 if (s == NULL)
4106 goto onError;
4107 if (*bytesmode != subbm) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004108 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02004109 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004110 goto onError;
4111 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004112 if (PyBytes_Check(v) && PyBytes_Check(s)) {
4113 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004114 if (v == NULL)
4115 goto onError;
4116 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004117 else {
4118 PyObject *temp = PyUnicode_Concat(v, s);
4119 Py_DECREF(s);
4120 Py_DECREF(v);
4121 v = temp;
4122 if (v == NULL)
4123 goto onError;
4124 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004125 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004126 }
4127 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128
Guido van Rossumd8faa362007-04-27 19:54:29 +00004129 onError:
4130 Py_XDECREF(v);
4131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132}