blob: c1c3907b91c101fd3f38a7b41d45aae72c9cec85 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
135validate_expr(expr_ty exp, expr_context_ty ctx)
136{
137 int check_ctx = 1;
138 expr_context_ty actual_ctx;
139
140 /* First check expression context. */
141 switch (exp->kind) {
142 case Attribute_kind:
143 actual_ctx = exp->v.Attribute.ctx;
144 break;
145 case Subscript_kind:
146 actual_ctx = exp->v.Subscript.ctx;
147 break;
148 case Starred_kind:
149 actual_ctx = exp->v.Starred.ctx;
150 break;
151 case Name_kind:
152 actual_ctx = exp->v.Name.ctx;
153 break;
154 case List_kind:
155 actual_ctx = exp->v.List.ctx;
156 break;
157 case Tuple_kind:
158 actual_ctx = exp->v.Tuple.ctx;
159 break;
160 default:
161 if (ctx != Load) {
162 PyErr_Format(PyExc_ValueError, "expression which can't be "
163 "assigned to in %s context", expr_context_name(ctx));
164 return 0;
165 }
166 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100167 /* set actual_ctx to prevent gcc warning */
168 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500169 }
170 if (check_ctx && actual_ctx != ctx) {
171 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
172 expr_context_name(ctx), expr_context_name(actual_ctx));
173 return 0;
174 }
175
176 /* Now validate expression. */
177 switch (exp->kind) {
178 case BoolOp_kind:
179 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
180 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
181 return 0;
182 }
183 return validate_exprs(exp->v.BoolOp.values, Load, 0);
184 case BinOp_kind:
185 return validate_expr(exp->v.BinOp.left, Load) &&
186 validate_expr(exp->v.BinOp.right, Load);
187 case UnaryOp_kind:
188 return validate_expr(exp->v.UnaryOp.operand, Load);
189 case Lambda_kind:
190 return validate_arguments(exp->v.Lambda.args) &&
191 validate_expr(exp->v.Lambda.body, Load);
192 case IfExp_kind:
193 return validate_expr(exp->v.IfExp.test, Load) &&
194 validate_expr(exp->v.IfExp.body, Load) &&
195 validate_expr(exp->v.IfExp.orelse, Load);
196 case Dict_kind:
197 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
198 PyErr_SetString(PyExc_ValueError,
199 "Dict doesn't have the same number of keys as values");
200 return 0;
201 }
202 return validate_exprs(exp->v.Dict.keys, Load, 0) &&
203 validate_exprs(exp->v.Dict.values, Load, 0);
204 case Set_kind:
205 return validate_exprs(exp->v.Set.elts, Load, 0);
206#define COMP(NAME) \
207 case NAME ## _kind: \
208 return validate_comprehension(exp->v.NAME.generators) && \
209 validate_expr(exp->v.NAME.elt, Load);
210 COMP(ListComp)
211 COMP(SetComp)
212 COMP(GeneratorExp)
213#undef COMP
214 case DictComp_kind:
215 return validate_comprehension(exp->v.DictComp.generators) &&
216 validate_expr(exp->v.DictComp.key, Load) &&
217 validate_expr(exp->v.DictComp.value, Load);
218 case Yield_kind:
219 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500220 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000221 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400222 case Await_kind:
223 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500224 case Compare_kind:
225 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
226 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
227 return 0;
228 }
229 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
230 asdl_seq_LEN(exp->v.Compare.ops)) {
231 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
232 "of comparators and operands");
233 return 0;
234 }
235 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
236 validate_expr(exp->v.Compare.left, Load);
237 case Call_kind:
238 return validate_expr(exp->v.Call.func, Load) &&
239 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400240 validate_keywords(exp->v.Call.keywords);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500241 case Num_kind: {
242 PyObject *n = exp->v.Num.n;
243 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
244 !PyComplex_CheckExact(n)) {
245 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
246 return 0;
247 }
248 return 1;
249 }
250 case Str_kind: {
251 PyObject *s = exp->v.Str.s;
252 if (!PyUnicode_CheckExact(s)) {
253 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
254 return 0;
255 }
256 return 1;
257 }
258 case Bytes_kind: {
259 PyObject *b = exp->v.Bytes.s;
260 if (!PyBytes_CheckExact(b)) {
261 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
262 return 0;
263 }
264 return 1;
265 }
266 case Attribute_kind:
267 return validate_expr(exp->v.Attribute.value, Load);
268 case Subscript_kind:
269 return validate_slice(exp->v.Subscript.slice) &&
270 validate_expr(exp->v.Subscript.value, Load);
271 case Starred_kind:
272 return validate_expr(exp->v.Starred.value, ctx);
273 case List_kind:
274 return validate_exprs(exp->v.List.elts, ctx, 0);
275 case Tuple_kind:
276 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
277 /* These last cases don't have any checking. */
278 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500279 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500280 case Ellipsis_kind:
281 return 1;
282 default:
283 PyErr_SetString(PyExc_SystemError, "unexpected expression");
284 return 0;
285 }
286}
287
288static int
289validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
290{
291 if (asdl_seq_LEN(seq))
292 return 1;
293 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
294 return 0;
295}
296
297static int
298validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
299{
300 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
301 validate_exprs(targets, ctx, 0);
302}
303
304static int
305validate_body(asdl_seq *body, const char *owner)
306{
307 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
308}
309
310static int
311validate_stmt(stmt_ty stmt)
312{
313 int i;
314 switch (stmt->kind) {
315 case FunctionDef_kind:
316 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
317 validate_arguments(stmt->v.FunctionDef.args) &&
318 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
319 (!stmt->v.FunctionDef.returns ||
320 validate_expr(stmt->v.FunctionDef.returns, Load));
321 case ClassDef_kind:
322 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
323 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
324 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400325 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500326 case Return_kind:
327 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
328 case Delete_kind:
329 return validate_assignlist(stmt->v.Delete.targets, Del);
330 case Assign_kind:
331 return validate_assignlist(stmt->v.Assign.targets, Store) &&
332 validate_expr(stmt->v.Assign.value, Load);
333 case AugAssign_kind:
334 return validate_expr(stmt->v.AugAssign.target, Store) &&
335 validate_expr(stmt->v.AugAssign.value, Load);
336 case For_kind:
337 return validate_expr(stmt->v.For.target, Store) &&
338 validate_expr(stmt->v.For.iter, Load) &&
339 validate_body(stmt->v.For.body, "For") &&
340 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400341 case AsyncFor_kind:
342 return validate_expr(stmt->v.AsyncFor.target, Store) &&
343 validate_expr(stmt->v.AsyncFor.iter, Load) &&
344 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
345 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346 case While_kind:
347 return validate_expr(stmt->v.While.test, Load) &&
348 validate_body(stmt->v.While.body, "While") &&
349 validate_stmts(stmt->v.While.orelse);
350 case If_kind:
351 return validate_expr(stmt->v.If.test, Load) &&
352 validate_body(stmt->v.If.body, "If") &&
353 validate_stmts(stmt->v.If.orelse);
354 case With_kind:
355 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
356 return 0;
357 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
358 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
359 if (!validate_expr(item->context_expr, Load) ||
360 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
361 return 0;
362 }
363 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400364 case AsyncWith_kind:
365 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
366 return 0;
367 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
368 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
369 if (!validate_expr(item->context_expr, Load) ||
370 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
371 return 0;
372 }
373 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500374 case Raise_kind:
375 if (stmt->v.Raise.exc) {
376 return validate_expr(stmt->v.Raise.exc, Load) &&
377 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
378 }
379 if (stmt->v.Raise.cause) {
380 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
381 return 0;
382 }
383 return 1;
384 case Try_kind:
385 if (!validate_body(stmt->v.Try.body, "Try"))
386 return 0;
387 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
388 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
389 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
390 return 0;
391 }
392 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
393 asdl_seq_LEN(stmt->v.Try.orelse)) {
394 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
395 return 0;
396 }
397 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
398 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
399 if ((handler->v.ExceptHandler.type &&
400 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
401 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
402 return 0;
403 }
404 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
405 validate_stmts(stmt->v.Try.finalbody)) &&
406 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
407 validate_stmts(stmt->v.Try.orelse));
408 case Assert_kind:
409 return validate_expr(stmt->v.Assert.test, Load) &&
410 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
411 case Import_kind:
412 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
413 case ImportFrom_kind:
414 if (stmt->v.ImportFrom.level < -1) {
415 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
416 return 0;
417 }
418 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
419 case Global_kind:
420 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
421 case Nonlocal_kind:
422 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
423 case Expr_kind:
424 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400425 case AsyncFunctionDef_kind:
426 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
427 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
428 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
429 (!stmt->v.AsyncFunctionDef.returns ||
430 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500431 case Pass_kind:
432 case Break_kind:
433 case Continue_kind:
434 return 1;
435 default:
436 PyErr_SetString(PyExc_SystemError, "unexpected statement");
437 return 0;
438 }
439}
440
441static int
442validate_stmts(asdl_seq *seq)
443{
444 int i;
445 for (i = 0; i < asdl_seq_LEN(seq); i++) {
446 stmt_ty stmt = asdl_seq_GET(seq, i);
447 if (stmt) {
448 if (!validate_stmt(stmt))
449 return 0;
450 }
451 else {
452 PyErr_SetString(PyExc_ValueError,
453 "None disallowed in statement list");
454 return 0;
455 }
456 }
457 return 1;
458}
459
460static int
461validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
462{
463 int i;
464 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
465 expr_ty expr = asdl_seq_GET(exprs, i);
466 if (expr) {
467 if (!validate_expr(expr, ctx))
468 return 0;
469 }
470 else if (!null_ok) {
471 PyErr_SetString(PyExc_ValueError,
472 "None disallowed in expression list");
473 return 0;
474 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100475
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500476 }
477 return 1;
478}
479
480int
481PyAST_Validate(mod_ty mod)
482{
483 int res = 0;
484
485 switch (mod->kind) {
486 case Module_kind:
487 res = validate_stmts(mod->v.Module.body);
488 break;
489 case Interactive_kind:
490 res = validate_stmts(mod->v.Interactive.body);
491 break;
492 case Expression_kind:
493 res = validate_expr(mod->v.Expression.body, Load);
494 break;
495 case Suite_kind:
496 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
497 break;
498 default:
499 PyErr_SetString(PyExc_SystemError, "impossible module node");
500 res = 0;
501 break;
502 }
503 return res;
504}
505
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500506/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500507#include "grammar.h"
508#include "parsetok.h"
509#include "graminit.h"
510
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511/* Data structure used internally */
512struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000513 char *c_encoding; /* source encoding */
514 PyArena *c_arena; /* arena for allocating memeory */
Victor Stinner14e461d2013-08-26 22:28:21 +0200515 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500516 PyObject *c_normalize; /* Normalization function from unicodedata. */
517 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518};
519
520static asdl_seq *seq_for_testlist(struct compiling *, const node *);
521static expr_ty ast_for_expr(struct compiling *, const node *);
522static stmt_ty ast_for_stmt(struct compiling *, const node *);
523static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
525 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000526static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000527static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Yury Selivanov75445082015-05-11 22:57:16 -0400529static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
530static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532/* Note different signature for ast_for_call */
533static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
534
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000535static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000536static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +0000537static PyObject *parsestrplus(struct compiling *, const node *n,
538 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Nick Coghlan650f0d02007-04-15 12:05:43 +0000540#define COMP_GENEXP 0
541#define COMP_LISTCOMP 1
542#define COMP_SETCOMP 2
543
Benjamin Peterson55e00432012-01-16 17:22:31 -0500544static int
545init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000546{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500547 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
548 if (!m)
549 return 0;
550 c->c_normalize = PyObject_GetAttrString(m, "normalize");
551 Py_DECREF(m);
552 if (!c->c_normalize)
553 return 0;
554 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500555 if (!c->c_normalize_args) {
556 Py_CLEAR(c->c_normalize);
557 return 0;
558 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200559 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500560 return 1;
561}
562
563static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400564new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500565{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400566 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500567 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000568 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500569 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500570 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000571 /* Check whether there are non-ASCII characters in the
572 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500573 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200574 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500575 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500576 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500578 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500579 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
580 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500581 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 if (!id2)
583 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200584 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000585 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000586 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200587 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
588 Py_DECREF(id);
589 return NULL;
590 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000591 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592}
593
Benjamin Peterson55e00432012-01-16 17:22:31 -0500594#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400597ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400599 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Victor Stinner14e461d2013-08-26 22:28:21 +0200601 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000603 Py_INCREF(Py_None);
604 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200606 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400607 if (!tmp)
608 return 0;
609 errstr = PyUnicode_FromString(errmsg);
610 if (!errstr) {
611 Py_DECREF(tmp);
612 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000613 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 Py_DECREF(errstr);
616 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400617 if (value) {
618 PyErr_SetObject(PyExc_SyntaxError, value);
619 Py_DECREF(value);
620 }
621 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622}
623
624/* num_stmts() returns number of contained statements.
625
626 Use this routine to determine how big a sequence is needed for
627 the statements in a parse tree. Its raison d'etre is this bit of
628 grammar:
629
630 stmt: simple_stmt | compound_stmt
631 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
632
633 A simple_stmt can contain multiple small_stmt elements joined
634 by semicolons. If the arg is a simple_stmt, the number of
635 small_stmt elements is returned.
636*/
637
638static int
639num_stmts(const node *n)
640{
641 int i, l;
642 node *ch;
643
644 switch (TYPE(n)) {
645 case single_input:
646 if (TYPE(CHILD(n, 0)) == NEWLINE)
647 return 0;
648 else
649 return num_stmts(CHILD(n, 0));
650 case file_input:
651 l = 0;
652 for (i = 0; i < NCH(n); i++) {
653 ch = CHILD(n, i);
654 if (TYPE(ch) == stmt)
655 l += num_stmts(ch);
656 }
657 return l;
658 case stmt:
659 return num_stmts(CHILD(n, 0));
660 case compound_stmt:
661 return 1;
662 case simple_stmt:
663 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
664 case suite:
665 if (NCH(n) == 1)
666 return num_stmts(CHILD(n, 0));
667 else {
668 l = 0;
669 for (i = 2; i < (NCH(n) - 1); i++)
670 l += num_stmts(CHILD(n, i));
671 return l;
672 }
673 default: {
674 char buf[128];
675
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000676 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 TYPE(n), NCH(n));
678 Py_FatalError(buf);
679 }
680 }
681 assert(0);
682 return 0;
683}
684
685/* Transform the CST rooted at node * to the appropriate AST
686*/
687
688mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200689PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
690 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000692 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 asdl_seq *stmts = NULL;
694 stmt_ty s;
695 node *ch;
696 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500697 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400699 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200700 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400701 c.c_filename = filename;
702 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000704 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000706#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400707 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500708 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000709#endif
710 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 } else if (TYPE(n) == encoding_decl) {
713 c.c_encoding = STR(n);
714 n = CHILD(n, 0);
715 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000717 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 }
719
Jeremy Hyltona8293132006-02-28 17:58:27 +0000720 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 switch (TYPE(n)) {
722 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200723 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500725 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 for (i = 0; i < NCH(n) - 1; i++) {
727 ch = CHILD(n, i);
728 if (TYPE(ch) == NEWLINE)
729 continue;
730 REQ(ch, stmt);
731 num = num_stmts(ch);
732 if (num == 1) {
733 s = ast_for_stmt(&c, ch);
734 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500735 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000736 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 }
738 else {
739 ch = CHILD(ch, 0);
740 REQ(ch, simple_stmt);
741 for (j = 0; j < num; j++) {
742 s = ast_for_stmt(&c, CHILD(ch, j * 2));
743 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500744 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000745 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 }
747 }
748 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500749 res = Module(stmts, arena);
750 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 case eval_input: {
752 expr_ty testlist_ast;
753
Nick Coghlan650f0d02007-04-15 12:05:43 +0000754 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000755 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 goto out;
758 res = Expression(testlist_ast, arena);
759 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 }
761 case single_input:
762 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200763 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500765 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
767 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000768 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 goto out;
770 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 }
772 else {
773 n = CHILD(n, 0);
774 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200775 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500777 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000779 s = ast_for_stmt(&c, n);
780 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 asdl_seq_SET(stmts, 0, s);
783 }
784 else {
785 /* Only a simple_stmt can contain multiple statements. */
786 REQ(n, simple_stmt);
787 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 if (TYPE(CHILD(n, i)) == NEWLINE)
789 break;
790 s = ast_for_stmt(&c, CHILD(n, i));
791 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500792 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 asdl_seq_SET(stmts, i / 2, s);
794 }
795 }
796
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500799 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000801 PyErr_Format(PyExc_SystemError,
802 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500803 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 out:
806 if (c.c_normalize) {
807 Py_DECREF(c.c_normalize);
808 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
809 Py_DECREF(c.c_normalize_args);
810 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
Victor Stinner14e461d2013-08-26 22:28:21 +0200814mod_ty
815PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
816 PyArena *arena)
817{
818 mod_ty mod;
819 PyObject *filename;
820 filename = PyUnicode_DecodeFSDefault(filename_str);
821 if (filename == NULL)
822 return NULL;
823 mod = PyAST_FromNodeObject(n, flags, filename, arena);
824 Py_DECREF(filename);
825 return mod;
826
827}
828
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
830*/
831
832static operator_ty
833get_operator(const node *n)
834{
835 switch (TYPE(n)) {
836 case VBAR:
837 return BitOr;
838 case CIRCUMFLEX:
839 return BitXor;
840 case AMPER:
841 return BitAnd;
842 case LEFTSHIFT:
843 return LShift;
844 case RIGHTSHIFT:
845 return RShift;
846 case PLUS:
847 return Add;
848 case MINUS:
849 return Sub;
850 case STAR:
851 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400852 case AT:
853 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 case SLASH:
855 return Div;
856 case DOUBLESLASH:
857 return FloorDiv;
858 case PERCENT:
859 return Mod;
860 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
863}
864
Guido van Rossume7ba4952007-06-06 23:52:48 +0000865static const char* FORBIDDEN[] = {
866 "None",
867 "True",
868 "False",
869 NULL,
870};
871
872static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400873forbidden_name(struct compiling *c, identifier name, const node *n,
874 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000875{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000876 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000877 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400878 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000879 return 1;
880 }
881 if (full_checks) {
882 const char **p;
883 for (p = FORBIDDEN; *p; p++) {
884 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400885 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000886 return 1;
887 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000888 }
889 }
890 return 0;
891}
892
Jeremy Hyltona8293132006-02-28 17:58:27 +0000893/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
895 Only sets context for expr kinds that "can appear in assignment context"
896 (according to ../Parser/Python.asdl). For other expr kinds, it sets
897 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898*/
899
900static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000901set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902{
903 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000904 /* If a particular expression type can't be used for assign / delete,
905 set expr_name to its name and an error message will be generated.
906 */
907 const char* expr_name = NULL;
908
909 /* The ast defines augmented store and load contexts, but the
910 implementation here doesn't actually use them. The code may be
911 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000912 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000913 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000914 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000915 */
916 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
918 switch (e->kind) {
919 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000920 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400921 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000922 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000923 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000925 e->v.Subscript.ctx = ctx;
926 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000927 case Starred_kind:
928 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000929 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000930 return 0;
931 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000933 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500934 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000936 }
937 e->v.Name.ctx = ctx;
938 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000940 e->v.List.ctx = ctx;
941 s = e->v.List.elts;
942 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000944 if (asdl_seq_LEN(e->v.Tuple.elts)) {
945 e->v.Tuple.ctx = ctx;
946 s = e->v.Tuple.elts;
947 }
948 else {
949 expr_name = "()";
950 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000951 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000952 case Lambda_kind:
953 expr_name = "lambda";
954 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000956 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000957 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000958 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000960 case UnaryOp_kind:
961 expr_name = "operator";
962 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 expr_name = "generator expression";
965 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500967 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000968 expr_name = "yield expression";
969 break;
Yury Selivanov75445082015-05-11 22:57:16 -0400970 case Await_kind:
971 expr_name = "await expression";
972 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 case ListComp_kind:
974 expr_name = "list comprehension";
975 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000976 case SetComp_kind:
977 expr_name = "set comprehension";
978 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000979 case DictComp_kind:
980 expr_name = "dict comprehension";
981 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000982 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000983 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Num_kind:
985 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500986 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000987 expr_name = "literal";
988 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500989 case NameConstant_kind:
990 expr_name = "keyword";
991 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000992 case Ellipsis_kind:
993 expr_name = "Ellipsis";
994 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 case Compare_kind:
996 expr_name = "comparison";
997 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998 case IfExp_kind:
999 expr_name = "conditional expression";
1000 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001001 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyErr_Format(PyExc_SystemError,
1003 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001004 e->kind, e->lineno);
1005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 /* Check for error string set by switch */
1008 if (expr_name) {
1009 char buf[300];
1010 PyOS_snprintf(buf, sizeof(buf),
1011 "can't %s %s",
1012 ctx == Store ? "assign to" : "delete",
1013 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001014 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001015 }
1016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 */
1020 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001021 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001024 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001025 return 0;
1026 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 }
1028 return 1;
1029}
1030
1031static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001032ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033{
1034 REQ(n, augassign);
1035 n = CHILD(n, 0);
1036 switch (STR(n)[0]) {
1037 case '+':
1038 return Add;
1039 case '-':
1040 return Sub;
1041 case '/':
1042 if (STR(n)[1] == '/')
1043 return FloorDiv;
1044 else
1045 return Div;
1046 case '%':
1047 return Mod;
1048 case '<':
1049 return LShift;
1050 case '>':
1051 return RShift;
1052 case '&':
1053 return BitAnd;
1054 case '^':
1055 return BitXor;
1056 case '|':
1057 return BitOr;
1058 case '*':
1059 if (STR(n)[1] == '*')
1060 return Pow;
1061 else
1062 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001063 case '@':
1064 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001066 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 }
1069}
1070
1071static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001072ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001074 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 |'is' 'not'
1076 */
1077 REQ(n, comp_op);
1078 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001079 n = CHILD(n, 0);
1080 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 case LESS:
1082 return Lt;
1083 case GREATER:
1084 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001085 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return Eq;
1087 case LESSEQUAL:
1088 return LtE;
1089 case GREATEREQUAL:
1090 return GtE;
1091 case NOTEQUAL:
1092 return NotEq;
1093 case NAME:
1094 if (strcmp(STR(n), "in") == 0)
1095 return In;
1096 if (strcmp(STR(n), "is") == 0)
1097 return Is;
1098 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001099 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 }
1104 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105 /* handle "not in" and "is not" */
1106 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 case NAME:
1108 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1109 return NotIn;
1110 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1111 return IsNot;
1112 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001113 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 }
Neal Norwitz79792652005-11-14 04:25:03 +00001118 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121}
1122
1123static asdl_seq *
1124seq_for_testlist(struct compiling *c, const node *n)
1125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001127 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1128 */
Armin Rigo31441302005-10-21 12:57:31 +00001129 asdl_seq *seq;
1130 expr_ty expression;
1131 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001132 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001134 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 if (!seq)
1136 return NULL;
1137
1138 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001140 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141
Benjamin Peterson4905e802009-09-27 02:43:28 +00001142 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145
1146 assert(i / 2 < seq->size);
1147 asdl_seq_SET(seq, i / 2, expression);
1148 }
1149 return seq;
1150}
1151
Neal Norwitzc1505362006-12-28 06:47:50 +00001152static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001153ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001154{
1155 identifier name;
1156 expr_ty annotation = NULL;
1157 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001158 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001159
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001160 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001161 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001162 name = NEW_IDENTIFIER(ch);
1163 if (!name)
1164 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001165 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001166 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001167
1168 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1169 annotation = ast_for_expr(c, CHILD(n, 2));
1170 if (!annotation)
1171 return NULL;
1172 }
1173
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001174 ret = arg(name, annotation, c->c_arena);
1175 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001176 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001177 ret->lineno = LINENO(n);
1178 ret->col_offset = n->n_col_offset;
1179 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180}
1181
Guido van Rossum4f72a782006-10-27 23:31:49 +00001182/* returns -1 if failed to handle keyword only arguments
1183 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001184 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001185 ^^^
1186 start pointing here
1187 */
1188static int
1189handle_keywordonly_args(struct compiling *c, const node *n, int start,
1190 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1191{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001192 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001193 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001194 expr_ty expression, annotation;
1195 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001196 int i = start;
1197 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001198
1199 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001200 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001201 return -1;
1202 }
1203 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001204 while (i < NCH(n)) {
1205 ch = CHILD(n, i);
1206 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001207 case vfpdef:
1208 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001209 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001210 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001211 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001212 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001213 asdl_seq_SET(kwdefaults, j, expression);
1214 i += 2; /* '=' and test */
1215 }
1216 else { /* setting NULL if no default value exists */
1217 asdl_seq_SET(kwdefaults, j, NULL);
1218 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 if (NCH(ch) == 3) {
1220 /* ch is NAME ':' test */
1221 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001222 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001223 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001224 }
1225 else {
1226 annotation = NULL;
1227 }
1228 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001229 argname = NEW_IDENTIFIER(ch);
1230 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001231 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001232 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001233 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001234 arg = arg(argname, annotation, c->c_arena);
1235 if (!arg)
1236 goto error;
Benjamin Peterson0714b8b2014-02-13 19:22:14 -05001237 arg->lineno = LINENO(ch);
1238 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001239 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 i += 2; /* the name and the comma */
1241 break;
1242 case DOUBLESTAR:
1243 return i;
1244 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001245 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 goto error;
1247 }
1248 }
1249 return i;
1250 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001252}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253
Jeremy Hyltona8293132006-02-28 17:58:27 +00001254/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255
1256static arguments_ty
1257ast_for_arguments(struct compiling *c, const node *n)
1258{
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 /* This function handles both typedargslist (function definition)
1260 and varargslist (lambda definition).
1261
1262 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001263 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1264 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1265 | '**' tfpdef [',']]]
1266 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1267 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001268 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001269 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1270 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1271 | '**' vfpdef [',']]]
1272 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1273 | '**' vfpdef [',']
1274 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001275 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001276
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001278 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1279 int nposdefaults = 0, found_default = 0;
1280 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001281 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001282 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 node *ch;
1284
1285 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001287 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001290 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291
Jeremy Hyltone921e022008-07-17 16:37:17 +00001292 /* First count the number of positional args & defaults. The
1293 variable i is the loop index for this for loop and the next.
1294 The next loop picks up where the first leaves off.
1295 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001297 ch = CHILD(n, i);
1298 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001299 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001300 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001301 if (i < NCH(n) && /* skip argument following star */
1302 (TYPE(CHILD(n, i)) == tfpdef ||
1303 TYPE(CHILD(n, i)) == vfpdef)) {
1304 i++;
1305 }
1306 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001307 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001308 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001310 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001313 defaults for keyword only args */
1314 for ( ; i < NCH(n); ++i) {
1315 ch = CHILD(n, i);
1316 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001317 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001319 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001321 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001323 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001325 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001327 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001329 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 since we set NULL as default for keyword only argument w/o default
1332 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001333 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001334 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001336 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337
1338 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001339 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001340 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001341 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001343 /* tfpdef: NAME [':' test]
1344 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 */
1346 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001347 j = 0; /* index for defaults */
1348 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001350 ch = CHILD(n, i);
1351 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001352 case tfpdef:
1353 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1355 anything other than EQUAL or a comma? */
1356 /* XXX Should NCH(n) check be made a separate check? */
1357 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001358 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1359 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001360 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 assert(posdefaults != NULL);
1362 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001367 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001369 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001371 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001372 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001374 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 i += 2; /* the name and the comma */
1376 break;
1377 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001378 if (i+1 >= NCH(n) ||
1379 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001380 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001381 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001382 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001384 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 if (TYPE(ch) == COMMA) {
1386 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 i += 2; /* now follows keyword only arguments */
1388 res = handle_keywordonly_args(c, n, i,
1389 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001390 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 i = res; /* res has new position to process */
1392 }
1393 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001394 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001395 if (!vararg)
1396 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001397
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001399 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1400 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401 int res = 0;
1402 res = handle_keywordonly_args(c, n, i,
1403 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001404 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 i = res; /* res has new position to process */
1406 }
1407 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 break;
1409 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001410 ch = CHILD(n, i+1); /* tfpdef */
1411 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001412 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001413 if (!kwarg)
1414 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 i += 3;
1416 break;
1417 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001418 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 "unexpected node in varargslist: %d @ %d",
1420 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001421 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001424 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425}
1426
1427static expr_ty
1428ast_for_dotted_name(struct compiling *c, const node *n)
1429{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001430 expr_ty e;
1431 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001432 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 int i;
1434
1435 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001436
1437 lineno = LINENO(n);
1438 col_offset = n->n_col_offset;
1439
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 id = NEW_IDENTIFIER(CHILD(n, 0));
1441 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
1447 for (i = 2; i < NCH(n); i+=2) {
1448 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001449 if (!id)
1450 return NULL;
1451 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1452 if (!e)
1453 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 }
1455
1456 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457}
1458
1459static expr_ty
1460ast_for_decorator(struct compiling *c, const node *n)
1461{
1462 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1463 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001464 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001467 REQ(CHILD(n, 0), AT);
1468 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1471 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001472 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001475 d = name_expr;
1476 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 }
1478 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001479 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001481 if (!d)
1482 return NULL;
1483 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 }
1485 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001486 d = ast_for_call(c, CHILD(n, 3), name_expr);
1487 if (!d)
1488 return NULL;
1489 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 }
1491
1492 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
1495static asdl_seq*
1496ast_for_decorators(struct compiling *c, const node *n)
1497{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001498 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001499 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001503 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 if (!decorator_seq)
1505 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001508 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001509 if (!d)
1510 return NULL;
1511 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 }
1513 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
1516static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001517ast_for_funcdef_impl(struct compiling *c, const node *n,
1518 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001520 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001521 identifier name;
1522 arguments_ty args;
1523 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001524 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001525 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526
1527 REQ(n, funcdef);
1528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 name = NEW_IDENTIFIER(CHILD(n, name_i));
1530 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001531 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001532 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001533 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1535 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001537 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1538 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1539 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001540 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001541 name_i += 2;
1542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 body = ast_for_suite(c, CHILD(n, name_i + 3));
1544 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546
Yury Selivanov75445082015-05-11 22:57:16 -04001547 if (is_async)
1548 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1549 LINENO(n),
1550 n->n_col_offset, c->c_arena);
1551 else
1552 return FunctionDef(name, args, body, decorator_seq, returns,
1553 LINENO(n),
1554 n->n_col_offset, c->c_arena);
1555}
1556
1557static stmt_ty
1558ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1559{
1560 /* async_funcdef: ASYNC funcdef */
1561 REQ(n, async_funcdef);
1562 REQ(CHILD(n, 0), ASYNC);
1563 REQ(CHILD(n, 1), funcdef);
1564
1565 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1566 1 /* is_async */);
1567}
1568
1569static stmt_ty
1570ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1571{
1572 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1573 return ast_for_funcdef_impl(c, n, decorator_seq,
1574 0 /* is_async */);
1575}
1576
1577
1578static stmt_ty
1579ast_for_async_stmt(struct compiling *c, const node *n)
1580{
1581 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1582 REQ(n, async_stmt);
1583 REQ(CHILD(n, 0), ASYNC);
1584
1585 switch (TYPE(CHILD(n, 1))) {
1586 case funcdef:
1587 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1588 1 /* is_async */);
1589 case with_stmt:
1590 return ast_for_with_stmt(c, CHILD(n, 1),
1591 1 /* is_async */);
1592
1593 case for_stmt:
1594 return ast_for_for_stmt(c, CHILD(n, 1),
1595 1 /* is_async */);
1596
1597 default:
1598 PyErr_Format(PyExc_SystemError,
1599 "invalid async stament: %s",
1600 STR(CHILD(n, 1)));
1601 return NULL;
1602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603}
1604
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001605static stmt_ty
1606ast_for_decorated(struct compiling *c, const node *n)
1607{
Yury Selivanov75445082015-05-11 22:57:16 -04001608 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001609 stmt_ty thing = NULL;
1610 asdl_seq *decorator_seq = NULL;
1611
1612 REQ(n, decorated);
1613
1614 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1615 if (!decorator_seq)
1616 return NULL;
1617
1618 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001619 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001621
1622 if (TYPE(CHILD(n, 1)) == funcdef) {
1623 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1624 } else if (TYPE(CHILD(n, 1)) == classdef) {
1625 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001626 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1627 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001628 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001629 /* we count the decorators in when talking about the class' or
1630 * function's line number */
1631 if (thing) {
1632 thing->lineno = LINENO(n);
1633 thing->col_offset = n->n_col_offset;
1634 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001635 return thing;
1636}
1637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638static expr_ty
1639ast_for_lambdef(struct compiling *c, const node *n)
1640{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001641 /* lambdef: 'lambda' [varargslist] ':' test
1642 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 arguments_ty args;
1644 expr_ty expression;
1645
1646 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001647 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 if (!args)
1649 return NULL;
1650 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001651 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 }
1654 else {
1655 args = ast_for_arguments(c, CHILD(n, 1));
1656 if (!args)
1657 return NULL;
1658 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001659 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 }
1662
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001663 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664}
1665
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001666static expr_ty
1667ast_for_ifexpr(struct compiling *c, const node *n)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001670 expr_ty expression, body, orelse;
1671
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001672 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001673 body = ast_for_expr(c, CHILD(n, 0));
1674 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001675 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001676 expression = ast_for_expr(c, CHILD(n, 2));
1677 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001678 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001679 orelse = ast_for_expr(c, CHILD(n, 4));
1680 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001681 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001682 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1683 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001684}
1685
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001687 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688
Nick Coghlan650f0d02007-04-15 12:05:43 +00001689 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690*/
1691
1692static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001693count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001695 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696
Guido van Rossumd8faa362007-04-27 19:54:29 +00001697 count_comp_for:
1698 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001699 REQ(n, comp_for);
1700 if (NCH(n) == 5)
1701 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001702 else
1703 return n_fors;
1704 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001705 REQ(n, comp_iter);
1706 n = CHILD(n, 0);
1707 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001708 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001709 else if (TYPE(n) == comp_if) {
1710 if (NCH(n) == 3) {
1711 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001712 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001713 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001714 else
1715 return n_fors;
1716 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001717
Guido van Rossumd8faa362007-04-27 19:54:29 +00001718 /* Should never be reached */
1719 PyErr_SetString(PyExc_SystemError,
1720 "logic error in count_comp_fors");
1721 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722}
1723
Nick Coghlan650f0d02007-04-15 12:05:43 +00001724/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725
Nick Coghlan650f0d02007-04-15 12:05:43 +00001726 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727*/
1728
1729static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001730count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001732 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733
Guido van Rossumd8faa362007-04-27 19:54:29 +00001734 while (1) {
1735 REQ(n, comp_iter);
1736 if (TYPE(CHILD(n, 0)) == comp_for)
1737 return n_ifs;
1738 n = CHILD(n, 0);
1739 REQ(n, comp_if);
1740 n_ifs++;
1741 if (NCH(n) == 2)
1742 return n_ifs;
1743 n = CHILD(n, 2);
1744 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745}
1746
Guido van Rossum992d4a32007-07-11 13:09:30 +00001747static asdl_seq *
1748ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001751 asdl_seq *comps;
1752
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001753 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 if (n_fors == -1)
1755 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001756
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001757 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001758 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001762 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001764 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001765 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766
Guido van Rossum992d4a32007-07-11 13:09:30 +00001767 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768
Guido van Rossum992d4a32007-07-11 13:09:30 +00001769 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001770 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001771 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001773 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001774 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001776
Thomas Wouters89f507f2006-12-13 04:49:30 +00001777 /* Check the # of children rather than the length of t, since
1778 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001779 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001780 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001781 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001783 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1784 c->c_arena),
1785 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001786 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001788
Guido van Rossum992d4a32007-07-11 13:09:30 +00001789 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 int j, n_ifs;
1791 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792
Guido van Rossum992d4a32007-07-11 13:09:30 +00001793 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001794 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001795 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001797
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001798 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001799 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001801
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001803 REQ(n, comp_iter);
1804 n = CHILD(n, 0);
1805 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806
Guido van Rossum992d4a32007-07-11 13:09:30 +00001807 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001808 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001809 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001810 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001811 if (NCH(n) == 3)
1812 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001814 /* on exit, must guarantee that n is a comp_for */
1815 if (TYPE(n) == comp_iter)
1816 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001817 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001819 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001821 return comps;
1822}
1823
1824static expr_ty
1825ast_for_itercomp(struct compiling *c, const node *n, int type)
1826{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001827 /* testlist_comp: (test|star_expr)
1828 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001829 expr_ty elt;
1830 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001831 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832
Guido van Rossum992d4a32007-07-11 13:09:30 +00001833 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001835 ch = CHILD(n, 0);
1836 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001837 if (!elt)
1838 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001839 if (elt->kind == Starred_kind) {
1840 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1841 return NULL;
1842 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843
Guido van Rossum992d4a32007-07-11 13:09:30 +00001844 comps = ast_for_comprehension(c, CHILD(n, 1));
1845 if (!comps)
1846 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001847
1848 if (type == COMP_GENEXP)
1849 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1850 else if (type == COMP_LISTCOMP)
1851 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1852 else if (type == COMP_SETCOMP)
1853 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1854 else
1855 /* Should never happen */
1856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857}
1858
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001859/* Fills in the key, value pair corresponding to the dict element. In case
1860 * of an unpacking, key is NULL. *i is advanced by the number of ast
1861 * elements. Iff successful, nonzero is returned.
1862 */
1863static int
1864ast_for_dictelement(struct compiling *c, const node *n, int *i,
1865 expr_ty *key, expr_ty *value)
1866{
1867 expr_ty expression;
1868 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1869 assert(NCH(n) - *i >= 2);
1870
1871 expression = ast_for_expr(c, CHILD(n, *i + 1));
1872 if (!expression)
1873 return 0;
1874 *key = NULL;
1875 *value = expression;
1876
1877 *i += 2;
1878 }
1879 else {
1880 assert(NCH(n) - *i >= 3);
1881
1882 expression = ast_for_expr(c, CHILD(n, *i));
1883 if (!expression)
1884 return 0;
1885 *key = expression;
1886
1887 REQ(CHILD(n, *i + 1), COLON);
1888
1889 expression = ast_for_expr(c, CHILD(n, *i + 2));
1890 if (!expression)
1891 return 0;
1892 *value = expression;
1893
1894 *i += 3;
1895 }
1896 return 1;
1897}
1898
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001900ast_for_dictcomp(struct compiling *c, const node *n)
1901{
1902 expr_ty key, value;
1903 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001904 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001906 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001908 assert(key);
1909 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001911 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001912 if (!comps)
1913 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914
Guido van Rossum992d4a32007-07-11 13:09:30 +00001915 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1916}
1917
1918static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001919ast_for_dictdisplay(struct compiling *c, const node *n)
1920{
1921 int i;
1922 int j;
1923 int size;
1924 asdl_seq *keys, *values;
1925
1926 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1927 keys = _Py_asdl_seq_new(size, c->c_arena);
1928 if (!keys)
1929 return NULL;
1930
1931 values = _Py_asdl_seq_new(size, c->c_arena);
1932 if (!values)
1933 return NULL;
1934
1935 j = 0;
1936 for (i = 0; i < NCH(n); i++) {
1937 expr_ty key, value;
1938
1939 if (!ast_for_dictelement(c, n, &i, &key, &value))
1940 return NULL;
1941 asdl_seq_SET(keys, j, key);
1942 asdl_seq_SET(values, j, value);
1943
1944 j++;
1945 }
1946 keys->size = j;
1947 values->size = j;
1948 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1949}
1950
1951static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001952ast_for_genexp(struct compiling *c, const node *n)
1953{
1954 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001955 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001956}
1957
1958static expr_ty
1959ast_for_listcomp(struct compiling *c, const node *n)
1960{
1961 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001962 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001963}
1964
1965static expr_ty
1966ast_for_setcomp(struct compiling *c, const node *n)
1967{
1968 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001969 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001970}
1971
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001972static expr_ty
1973ast_for_setdisplay(struct compiling *c, const node *n)
1974{
1975 int i;
1976 int size;
1977 asdl_seq *elts;
1978
1979 assert(TYPE(n) == (dictorsetmaker));
1980 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
1981 elts = _Py_asdl_seq_new(size, c->c_arena);
1982 if (!elts)
1983 return NULL;
1984 for (i = 0; i < NCH(n); i += 2) {
1985 expr_ty expression;
1986 expression = ast_for_expr(c, CHILD(n, i));
1987 if (!expression)
1988 return NULL;
1989 asdl_seq_SET(elts, i / 2, expression);
1990 }
1991 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1992}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001993
1994static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995ast_for_atom(struct compiling *c, const node *n)
1996{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001997 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1998 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001999 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 */
2001 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00002002 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002005 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002006 PyObject *name;
2007 const char *s = STR(ch);
2008 size_t len = strlen(s);
2009 if (len >= 4 && len <= 5) {
2010 if (!strcmp(s, "None"))
2011 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2012 if (!strcmp(s, "True"))
2013 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2014 if (!strcmp(s, "False"))
2015 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2016 }
2017 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002018 if (!name)
2019 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002020 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002021 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2022 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00002024 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002025 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002026 const char *errtype = NULL;
2027 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2028 errtype = "unicode error";
2029 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2030 errtype = "value error";
2031 if (errtype) {
2032 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002033 PyObject *type, *value, *tback, *errstr;
2034 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002035 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002036 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002037 char *s = _PyUnicode_AsString(errstr);
2038 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002039 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002040 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002041 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002042 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002043 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002044 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002045 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002046 Py_XDECREF(tback);
2047 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002048 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002049 }
Victor Stinner43d81952013-07-17 00:57:58 +02002050 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2051 Py_DECREF(str);
2052 return NULL;
2053 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00002054 if (bytesmode)
2055 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
2056 else
2057 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 }
2059 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002060 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002061 if (!pynum)
2062 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063
Victor Stinner43d81952013-07-17 00:57:58 +02002064 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2065 Py_DECREF(pynum);
2066 return NULL;
2067 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002068 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 }
Georg Brandldde00282007-03-18 19:01:53 +00002070 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002071 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002073 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 if (TYPE(ch) == RPAR)
2076 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 if (TYPE(ch) == yield_expr)
2079 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002082 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002083 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002084
Nick Coghlan650f0d02007-04-15 12:05:43 +00002085 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002087 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088
Thomas Wouters89f507f2006-12-13 04:49:30 +00002089 if (TYPE(ch) == RSQB)
2090 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091
Nick Coghlan650f0d02007-04-15 12:05:43 +00002092 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002093 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2094 asdl_seq *elts = seq_for_testlist(c, ch);
2095 if (!elts)
2096 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097
Thomas Wouters89f507f2006-12-13 04:49:30 +00002098 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2099 }
2100 else
2101 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002103 /* dictorsetmaker: ( ((test ':' test | '**' test)
2104 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2105 * ((test | '*' test)
2106 * (comp_for | (',' (test | '*' test))* [','])) ) */
Neal Norwitzc1505362006-12-28 06:47:50 +00002107 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002108 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002109 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002110 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002111 }
2112 else {
2113 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2114 if (NCH(ch) == 1 ||
2115 (NCH(ch) > 1 &&
2116 TYPE(CHILD(ch, 1)) == COMMA)) {
2117 /* It's a set display. */
2118 return ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002119 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002120 else if (NCH(ch) > 1 &&
2121 TYPE(CHILD(ch, 1)) == comp_for) {
2122 /* It's a set comprehension. */
2123 return ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002124 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002125 else if (NCH(ch) > 3 - is_dict &&
2126 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2127 /* It's a dictionary comprehension. */
2128 if (is_dict) {
2129 ast_error(c, n, "dict unpacking cannot be used in "
2130 "dict comprehension");
2131 return NULL;
2132 }
2133 return ast_for_dictcomp(c, ch);
2134 }
2135 else {
2136 /* It's a dictionary display. */
2137 return ast_for_dictdisplay(c, ch);
2138 }
Guido van Rossum86e58e22006-08-28 15:27:34 +00002139 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002142 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2143 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 }
2145}
2146
2147static slice_ty
2148ast_for_slice(struct compiling *c, const node *n)
2149{
2150 node *ch;
2151 expr_ty lower = NULL, upper = NULL, step = NULL;
2152
2153 REQ(n, subscript);
2154
2155 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002156 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 sliceop: ':' [test]
2158 */
2159 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 if (NCH(n) == 1 && TYPE(ch) == test) {
2161 /* 'step' variable hold no significance in terms of being used over
2162 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 if (!step)
2165 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 }
2169
2170 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 if (!lower)
2173 return NULL;
2174 }
2175
2176 /* If there's an upper bound it's in the second or third position. */
2177 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 if (NCH(n) > 1) {
2179 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 if (TYPE(n2) == test) {
2182 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183 if (!upper)
2184 return NULL;
2185 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002188 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189
Thomas Wouters89f507f2006-12-13 04:49:30 +00002190 if (TYPE(n2) == test) {
2191 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 if (!upper)
2193 return NULL;
2194 }
2195 }
2196
2197 ch = CHILD(n, NCH(n) - 1);
2198 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002199 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002200 ch = CHILD(ch, 1);
2201 if (TYPE(ch) == test) {
2202 step = ast_for_expr(c, ch);
2203 if (!step)
2204 return NULL;
2205 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 }
2207 }
2208
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002209 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210}
2211
2212static expr_ty
2213ast_for_binop(struct compiling *c, const node *n)
2214{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002215 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002217 BinOp(BinOp(A, op, B), op, C).
2218 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219
Guido van Rossumd8faa362007-04-27 19:54:29 +00002220 int i, nops;
2221 expr_ty expr1, expr2, result;
2222 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223
Guido van Rossumd8faa362007-04-27 19:54:29 +00002224 expr1 = ast_for_expr(c, CHILD(n, 0));
2225 if (!expr1)
2226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227
Guido van Rossumd8faa362007-04-27 19:54:29 +00002228 expr2 = ast_for_expr(c, CHILD(n, 2));
2229 if (!expr2)
2230 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231
Guido van Rossumd8faa362007-04-27 19:54:29 +00002232 newoperator = get_operator(CHILD(n, 1));
2233 if (!newoperator)
2234 return NULL;
2235
2236 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2237 c->c_arena);
2238 if (!result)
2239 return NULL;
2240
2241 nops = (NCH(n) - 1) / 2;
2242 for (i = 1; i < nops; i++) {
2243 expr_ty tmp_result, tmp;
2244 const node* next_oper = CHILD(n, i * 2 + 1);
2245
2246 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002247 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 return NULL;
2249
Guido van Rossumd8faa362007-04-27 19:54:29 +00002250 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2251 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 return NULL;
2253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002255 LINENO(next_oper), next_oper->n_col_offset,
2256 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002258 return NULL;
2259 result = tmp_result;
2260 }
2261 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262}
2263
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002264static expr_ty
2265ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002268 subscriptlist: subscript (',' subscript)* [',']
2269 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2270 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002271 REQ(n, trailer);
2272 if (TYPE(CHILD(n, 0)) == LPAR) {
2273 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002274 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002275 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002276 else
2277 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002278 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002279 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002280 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2281 if (!attr_id)
2282 return NULL;
2283 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002284 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002285 }
2286 else {
2287 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002288 REQ(CHILD(n, 2), RSQB);
2289 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002290 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002291 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2292 if (!slc)
2293 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2295 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002296 }
2297 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002299 by treating the sequence as a tuple literal if there are
2300 no slice features.
2301 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002302 int j;
2303 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002304 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002305 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002306 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002307 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002308 if (!slices)
2309 return NULL;
2310 for (j = 0; j < NCH(n); j += 2) {
2311 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002312 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002313 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002314 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002315 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002316 asdl_seq_SET(slices, j / 2, slc);
2317 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002318 if (!simple) {
2319 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002320 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002321 }
2322 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002323 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002324 if (!elts)
2325 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002326 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2327 slc = (slice_ty)asdl_seq_GET(slices, j);
2328 assert(slc->kind == Index_kind && slc->v.Index.value);
2329 asdl_seq_SET(elts, j, slc->v.Index.value);
2330 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002331 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002332 if (!e)
2333 return NULL;
2334 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002335 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002336 }
2337 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002338}
2339
2340static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002341ast_for_factor(struct compiling *c, const node *n)
2342{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002343 expr_ty expression;
2344
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002345 expression = ast_for_expr(c, CHILD(n, 1));
2346 if (!expression)
2347 return NULL;
2348
2349 switch (TYPE(CHILD(n, 0))) {
2350 case PLUS:
2351 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2352 c->c_arena);
2353 case MINUS:
2354 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2355 c->c_arena);
2356 case TILDE:
2357 return UnaryOp(Invert, expression, LINENO(n),
2358 n->n_col_offset, c->c_arena);
2359 }
2360 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2361 TYPE(CHILD(n, 0)));
2362 return NULL;
2363}
2364
2365static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002366ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002367{
Yury Selivanov75445082015-05-11 22:57:16 -04002368 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002369 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002370
2371 REQ(n, atom_expr);
2372 nch = NCH(n);
2373
2374 if (TYPE(CHILD(n, 0)) == AWAIT) {
2375 start = 1;
2376 assert(nch > 1);
2377 }
2378
2379 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002380 if (!e)
2381 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002382 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002383 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002384 if (start && nch == 2) {
2385 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2386 }
2387
2388 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002389 node *ch = CHILD(n, i);
2390 if (TYPE(ch) != trailer)
2391 break;
2392 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002393 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002394 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002395 tmp->lineno = e->lineno;
2396 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002397 e = tmp;
2398 }
Yury Selivanov75445082015-05-11 22:57:16 -04002399
2400 if (start) {
2401 /* there was an AWAIT */
2402 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2403 }
2404 else {
2405 return e;
2406 }
2407}
2408
2409static expr_ty
2410ast_for_power(struct compiling *c, const node *n)
2411{
2412 /* power: atom trailer* ('**' factor)*
2413 */
2414 expr_ty e;
2415 REQ(n, power);
2416 e = ast_for_atom_expr(c, CHILD(n, 0));
2417 if (!e)
2418 return NULL;
2419 if (NCH(n) == 1)
2420 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002421 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2422 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002423 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002424 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002425 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002426 }
2427 return e;
2428}
2429
Guido van Rossum0368b722007-05-11 16:50:42 +00002430static expr_ty
2431ast_for_starred(struct compiling *c, const node *n)
2432{
2433 expr_ty tmp;
2434 REQ(n, star_expr);
2435
2436 tmp = ast_for_expr(c, CHILD(n, 1));
2437 if (!tmp)
2438 return NULL;
2439
2440 /* The Load context is changed later. */
2441 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2442}
2443
2444
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445/* Do not name a variable 'expr'! Will cause a compile error.
2446*/
2447
2448static expr_ty
2449ast_for_expr(struct compiling *c, const node *n)
2450{
2451 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002452 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002453 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 and_test: not_test ('and' not_test)*
2456 not_test: 'not' not_test | comparison
2457 comparison: expr (comp_op expr)*
2458 expr: xor_expr ('|' xor_expr)*
2459 xor_expr: and_expr ('^' and_expr)*
2460 and_expr: shift_expr ('&' shift_expr)*
2461 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2462 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002463 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002465 power: atom_expr ['**' factor]
2466 atom_expr: [AWAIT] atom trailer*
2467 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 */
2469
2470 asdl_seq *seq;
2471 int i;
2472
2473 loop:
2474 switch (TYPE(n)) {
2475 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002476 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002477 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002478 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002480 else if (NCH(n) > 1)
2481 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002482 /* Fallthrough */
2483 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 case and_test:
2485 if (NCH(n) == 1) {
2486 n = CHILD(n, 0);
2487 goto loop;
2488 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002489 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 if (!seq)
2491 return NULL;
2492 for (i = 0; i < NCH(n); i += 2) {
2493 expr_ty e = ast_for_expr(c, CHILD(n, i));
2494 if (!e)
2495 return NULL;
2496 asdl_seq_SET(seq, i / 2, e);
2497 }
2498 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002499 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2500 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002501 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002502 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 case not_test:
2504 if (NCH(n) == 1) {
2505 n = CHILD(n, 0);
2506 goto loop;
2507 }
2508 else {
2509 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2510 if (!expression)
2511 return NULL;
2512
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002513 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2514 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 }
2516 case comparison:
2517 if (NCH(n) == 1) {
2518 n = CHILD(n, 0);
2519 goto loop;
2520 }
2521 else {
2522 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002523 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002524 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002525 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 if (!ops)
2527 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002528 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 return NULL;
2531 }
2532 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002535 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002536 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002538 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539
2540 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002541 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002545 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 asdl_seq_SET(cmps, i / 2, expression);
2547 }
2548 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002549 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002553 return Compare(expression, ops, cmps, LINENO(n),
2554 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 }
2556 break;
2557
Guido van Rossum0368b722007-05-11 16:50:42 +00002558 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 /* The next five cases all handle BinOps. The main body of code
2561 is the same in each case, but the switch turned inside out to
2562 reuse the code for each type of operator.
2563 */
2564 case expr:
2565 case xor_expr:
2566 case and_expr:
2567 case shift_expr:
2568 case arith_expr:
2569 case term:
2570 if (NCH(n) == 1) {
2571 n = CHILD(n, 0);
2572 goto loop;
2573 }
2574 return ast_for_binop(c, n);
2575 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002576 node *an = NULL;
2577 node *en = NULL;
2578 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002579 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002580 if (NCH(n) > 1)
2581 an = CHILD(n, 1); /* yield_arg */
2582 if (an) {
2583 en = CHILD(an, NCH(an) - 1);
2584 if (NCH(an) == 2) {
2585 is_from = 1;
2586 exp = ast_for_expr(c, en);
2587 }
2588 else
2589 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002590 if (!exp)
2591 return NULL;
2592 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002593 if (is_from)
2594 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2595 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002596 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002597 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 if (NCH(n) == 1) {
2599 n = CHILD(n, 0);
2600 goto loop;
2601 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002602 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002603 case power:
2604 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002606 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 return NULL;
2608 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002609 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 return NULL;
2611}
2612
2613static expr_ty
2614ast_for_call(struct compiling *c, const node *n, expr_ty func)
2615{
2616 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002617 arglist: argument (',' argument)* [',']
2618 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 */
2620
2621 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002622 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002623 asdl_seq *args;
2624 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
2626 REQ(n, arglist);
2627
2628 nargs = 0;
2629 nkeywords = 0;
2630 ngens = 0;
2631 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 node *ch = CHILD(n, i);
2633 if (TYPE(ch) == argument) {
2634 if (NCH(ch) == 1)
2635 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002636 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002638 else if (TYPE(CHILD(ch, 0)) == STAR)
2639 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002641 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642 nkeywords++;
2643 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002646 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002647 "if not sole argument");
2648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650
2651 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002652 ast_error(c, n, "more than 255 arguments");
2653 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 }
2655
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002656 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002658 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002659 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002661 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002662
2663 nargs = 0; /* positional arguments + iterable argument unpackings */
2664 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2665 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002667 node *ch = CHILD(n, i);
2668 if (TYPE(ch) == argument) {
2669 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002670 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002671 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002672 /* a positional argument */
2673 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002674 if (ndoublestars) {
2675 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002676 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002677 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002678 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002679 else {
2680 ast_error(c, chch,
2681 "positional argument follows "
2682 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002683 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002684 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002685 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002686 e = ast_for_expr(c, chch);
2687 if (!e)
2688 return NULL;
2689 asdl_seq_SET(args, nargs++, e);
2690 }
2691 else if (TYPE(chch) == STAR) {
2692 /* an iterable argument unpacking */
2693 expr_ty starred;
2694 if (ndoublestars) {
2695 ast_error(c, chch,
2696 "iterable argument unpacking follows "
2697 "keyword argument unpacking");
2698 return NULL;
2699 }
2700 e = ast_for_expr(c, CHILD(ch, 1));
2701 if (!e)
2702 return NULL;
2703 starred = Starred(e, Load, LINENO(chch),
2704 chch->n_col_offset,
2705 c->c_arena);
2706 if (!starred)
2707 return NULL;
2708 asdl_seq_SET(args, nargs++, starred);
2709
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 }
2711 else if (TYPE(chch) == DOUBLESTAR) {
2712 /* a keyword argument unpacking */
2713 keyword_ty kw;
2714 i++;
2715 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002717 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 kw = keyword(NULL, e, c->c_arena);
2719 asdl_seq_SET(keywords, nkeywords++, kw);
2720 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002722 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002723 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002724 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002726 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002729 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002732 identifier key, tmp;
2733 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002735 /* chch is test, but must be an identifier? */
2736 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 /* f(lambda x: x[0] = 3) ends up getting parsed with
2740 * LHS test = lambda x: x[0], and RHS test = 3.
2741 * SF bug 132313 points out that complaining about a keyword
2742 * then is very confusing.
2743 */
2744 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002745 ast_error(c, chch,
2746 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002747 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002748 }
2749 else if (e->kind != Name_kind) {
2750 ast_error(c, chch,
2751 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002752 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002753 }
2754 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002755 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002757 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002758 for (k = 0; k < nkeywords; k++) {
2759 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 if (tmp && !PyUnicode_Compare(tmp, key)) {
2761 ast_error(c, chch,
2762 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002763 return NULL;
2764 }
2765 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002766 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002768 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002769 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002771 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002772 asdl_seq_SET(keywords, nkeywords++, kw);
2773 }
2774 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775 }
2776
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778}
2779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002781ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002783 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002784 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002786 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002787 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002788 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002789 }
2790 else {
2791 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002792 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002793 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002795 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 else {
2797 asdl_seq *tmp = seq_for_testlist(c, n);
2798 if (!tmp)
2799 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002800 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002802}
2803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804static stmt_ty
2805ast_for_expr_stmt(struct compiling *c, const node *n)
2806{
2807 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002810 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002811 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002812 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 test: ... here starts the operator precendence dance
2814 */
2815
2816 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 if (!e)
2819 return NULL;
2820
Thomas Wouters89f507f2006-12-13 04:49:30 +00002821 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 }
2823 else if (TYPE(CHILD(n, 1)) == augassign) {
2824 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002825 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002826 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Thomas Wouters89f507f2006-12-13 04:49:30 +00002828 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 if (!expr1)
2830 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002831 if(!set_context(c, expr1, Store, ch))
2832 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002833 /* set_context checks that most expressions are not the left side.
2834 Augmented assignments can only have a name, a subscript, or an
2835 attribute on the left, though, so we have to explicitly check for
2836 those. */
2837 switch (expr1->kind) {
2838 case Name_kind:
2839 case Attribute_kind:
2840 case Subscript_kind:
2841 break;
2842 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002843 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002844 return NULL;
2845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846
Thomas Wouters89f507f2006-12-13 04:49:30 +00002847 ch = CHILD(n, 2);
2848 if (TYPE(ch) == testlist)
2849 expr2 = ast_for_testlist(c, ch);
2850 else
2851 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002852 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 return NULL;
2854
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002855 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002856 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 return NULL;
2858
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 }
2861 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 int i;
2863 asdl_seq *targets;
2864 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 expr_ty expression;
2866
Thomas Wouters89f507f2006-12-13 04:49:30 +00002867 /* a normal assignment */
2868 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002869 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002870 if (!targets)
2871 return NULL;
2872 for (i = 0; i < NCH(n) - 2; i += 2) {
2873 expr_ty e;
2874 node *ch = CHILD(n, i);
2875 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002876 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 return NULL;
2878 }
2879 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002883 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002884 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886
Thomas Wouters89f507f2006-12-13 04:49:30 +00002887 asdl_seq_SET(targets, i / 2, e);
2888 }
2889 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002890 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 expression = ast_for_testlist(c, value);
2892 else
2893 expression = ast_for_expr(c, value);
2894 if (!expression)
2895 return NULL;
2896 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898}
2899
Benjamin Peterson78565b22009-06-28 19:19:51 +00002900
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002902ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903{
2904 asdl_seq *seq;
2905 int i;
2906 expr_ty e;
2907
2908 REQ(n, exprlist);
2909
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002910 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 e = ast_for_expr(c, CHILD(n, i));
2915 if (!e)
2916 return NULL;
2917 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002918 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 }
2921 return seq;
2922}
2923
2924static stmt_ty
2925ast_for_del_stmt(struct compiling *c, const node *n)
2926{
2927 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 /* del_stmt: 'del' exprlist */
2930 REQ(n, del_stmt);
2931
2932 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2933 if (!expr_list)
2934 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002935 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936}
2937
2938static stmt_ty
2939ast_for_flow_stmt(struct compiling *c, const node *n)
2940{
2941 /*
2942 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2943 | yield_stmt
2944 break_stmt: 'break'
2945 continue_stmt: 'continue'
2946 return_stmt: 'return' [testlist]
2947 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002948 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 raise_stmt: 'raise' [test [',' test [',' test]]]
2950 */
2951 node *ch;
2952
2953 REQ(n, flow_stmt);
2954 ch = CHILD(n, 0);
2955 switch (TYPE(ch)) {
2956 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002957 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002959 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002961 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2962 if (!exp)
2963 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002964 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 }
2966 case return_stmt:
2967 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002968 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002970 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 if (!expression)
2972 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002973 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 }
2975 case raise_stmt:
2976 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002977 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2978 else if (NCH(ch) >= 2) {
2979 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2981 if (!expression)
2982 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002983 if (NCH(ch) == 4) {
2984 cause = ast_for_expr(c, CHILD(ch, 3));
2985 if (!cause)
2986 return NULL;
2987 }
2988 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 }
2990 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002991 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 "unexpected flow_stmt: %d", TYPE(ch));
2993 return NULL;
2994 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002995
2996 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998}
2999
3000static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003001alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002{
3003 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003004 import_as_name: NAME ['as' NAME]
3005 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 dotted_name: NAME ('.' NAME)*
3007 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003008 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003009
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 loop:
3011 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003012 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003013 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003014 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003015 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003016 if (!name)
3017 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003018 if (NCH(n) == 3) {
3019 node *str_node = CHILD(n, 2);
3020 str = NEW_IDENTIFIER(str_node);
3021 if (!str)
3022 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003023 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003024 return NULL;
3025 }
3026 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003027 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003028 return NULL;
3029 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003030 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003031 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 case dotted_as_name:
3033 if (NCH(n) == 1) {
3034 n = CHILD(n, 0);
3035 goto loop;
3036 }
3037 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003038 node *asname_node = CHILD(n, 2);
3039 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003040 if (!a)
3041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003043 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003044 if (!a->asname)
3045 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003046 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 return a;
3049 }
3050 break;
3051 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003052 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003053 node *name_node = CHILD(n, 0);
3054 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003055 if (!name)
3056 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003057 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003058 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003059 return alias(name, NULL, c->c_arena);
3060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061 else {
3062 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003063 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003064 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067
3068 len = 0;
3069 for (i = 0; i < NCH(n); i += 2)
3070 /* length of string plus one for the dot */
3071 len += strlen(STR(CHILD(n, i))) + 1;
3072 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003073 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 if (!str)
3075 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003076 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 if (!s)
3078 return NULL;
3079 for (i = 0; i < NCH(n); i += 2) {
3080 char *sch = STR(CHILD(n, i));
3081 strcpy(s, STR(CHILD(n, i)));
3082 s += strlen(sch);
3083 *s++ = '.';
3084 }
3085 --s;
3086 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3088 PyBytes_GET_SIZE(str),
3089 NULL);
3090 Py_DECREF(str);
3091 if (!uni)
3092 return NULL;
3093 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003094 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003095 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3096 Py_DECREF(str);
3097 return NULL;
3098 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003099 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 }
3101 break;
3102 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003103 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003104 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3105 Py_DECREF(str);
3106 return NULL;
3107 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003108 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003110 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 "unexpected import name: %d", TYPE(n));
3112 return NULL;
3113 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003114
3115 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 return NULL;
3117}
3118
3119static stmt_ty
3120ast_for_import_stmt(struct compiling *c, const node *n)
3121{
3122 /*
3123 import_stmt: import_name | import_from
3124 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003125 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3126 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003128 int lineno;
3129 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 int i;
3131 asdl_seq *aliases;
3132
3133 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003134 lineno = LINENO(n);
3135 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003137 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003139 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003140 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003141 if (!aliases)
3142 return NULL;
3143 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003144 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003145 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003147 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003149 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003151 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003153 int idx, ndots = 0;
3154 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003155 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003157 /* Count the number of dots (for relative imports) and check for the
3158 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003159 for (idx = 1; idx < NCH(n); idx++) {
3160 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003161 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3162 if (!mod)
3163 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003164 idx++;
3165 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003166 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003168 ndots += 3;
3169 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003170 } else if (TYPE(CHILD(n, idx)) != DOT) {
3171 break;
3172 }
3173 ndots++;
3174 }
3175 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003176 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003177 case STAR:
3178 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003179 n = CHILD(n, idx);
3180 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003181 break;
3182 case LPAR:
3183 /* from ... import (x, y, z) */
3184 n = CHILD(n, idx + 1);
3185 n_children = NCH(n);
3186 break;
3187 case import_as_names:
3188 /* from ... import x, y, z */
3189 n = CHILD(n, idx);
3190 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003191 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003192 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 " surrounding parentheses");
3194 return NULL;
3195 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 break;
3197 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003198 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199 return NULL;
3200 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003202 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003203 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205
3206 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003207 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003208 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003209 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003211 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003213 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003214 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003216 if (!import_alias)
3217 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003218 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003221 if (mod != NULL)
3222 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003223 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003224 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 }
Neal Norwitz79792652005-11-14 04:25:03 +00003226 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 "unknown import statement: starts with command '%s'",
3228 STR(CHILD(n, 0)));
3229 return NULL;
3230}
3231
3232static stmt_ty
3233ast_for_global_stmt(struct compiling *c, const node *n)
3234{
3235 /* global_stmt: 'global' NAME (',' NAME)* */
3236 identifier name;
3237 asdl_seq *s;
3238 int i;
3239
3240 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003241 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003243 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003245 name = NEW_IDENTIFIER(CHILD(n, i));
3246 if (!name)
3247 return NULL;
3248 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003250 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251}
3252
3253static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003254ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3255{
3256 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3257 identifier name;
3258 asdl_seq *s;
3259 int i;
3260
3261 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003262 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003263 if (!s)
3264 return NULL;
3265 for (i = 1; i < NCH(n); i += 2) {
3266 name = NEW_IDENTIFIER(CHILD(n, i));
3267 if (!name)
3268 return NULL;
3269 asdl_seq_SET(s, i / 2, name);
3270 }
3271 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3272}
3273
3274static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275ast_for_assert_stmt(struct compiling *c, const node *n)
3276{
3277 /* assert_stmt: 'assert' test [',' test] */
3278 REQ(n, assert_stmt);
3279 if (NCH(n) == 2) {
3280 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3281 if (!expression)
3282 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003283 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 }
3285 else if (NCH(n) == 4) {
3286 expr_ty expr1, expr2;
3287
3288 expr1 = ast_for_expr(c, CHILD(n, 1));
3289 if (!expr1)
3290 return NULL;
3291 expr2 = ast_for_expr(c, CHILD(n, 3));
3292 if (!expr2)
3293 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294
Thomas Wouters89f507f2006-12-13 04:49:30 +00003295 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 }
Neal Norwitz79792652005-11-14 04:25:03 +00003297 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 "improper number of parts to 'assert' statement: %d",
3299 NCH(n));
3300 return NULL;
3301}
3302
3303static asdl_seq *
3304ast_for_suite(struct compiling *c, const node *n)
3305{
3306 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003307 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 stmt_ty s;
3309 int i, total, num, end, pos = 0;
3310 node *ch;
3311
3312 REQ(n, suite);
3313
3314 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003315 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003319 n = CHILD(n, 0);
3320 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 */
3323 end = NCH(n) - 1;
3324 if (TYPE(CHILD(n, end - 1)) == SEMI)
3325 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 for (i = 0; i < end; i += 2) {
3328 ch = CHILD(n, i);
3329 s = ast_for_stmt(c, ch);
3330 if (!s)
3331 return NULL;
3332 asdl_seq_SET(seq, pos++, s);
3333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334 }
3335 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 for (i = 2; i < (NCH(n) - 1); i++) {
3337 ch = CHILD(n, i);
3338 REQ(ch, stmt);
3339 num = num_stmts(ch);
3340 if (num == 1) {
3341 /* small_stmt or compound_stmt with only one child */
3342 s = ast_for_stmt(c, ch);
3343 if (!s)
3344 return NULL;
3345 asdl_seq_SET(seq, pos++, s);
3346 }
3347 else {
3348 int j;
3349 ch = CHILD(ch, 0);
3350 REQ(ch, simple_stmt);
3351 for (j = 0; j < NCH(ch); j += 2) {
3352 /* statement terminates with a semi-colon ';' */
3353 if (NCH(CHILD(ch, j)) == 0) {
3354 assert((j + 1) == NCH(ch));
3355 break;
3356 }
3357 s = ast_for_stmt(c, CHILD(ch, j));
3358 if (!s)
3359 return NULL;
3360 asdl_seq_SET(seq, pos++, s);
3361 }
3362 }
3363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 }
3365 assert(pos == seq->size);
3366 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367}
3368
3369static stmt_ty
3370ast_for_if_stmt(struct compiling *c, const node *n)
3371{
3372 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3373 ['else' ':' suite]
3374 */
3375 char *s;
3376
3377 REQ(n, if_stmt);
3378
3379 if (NCH(n) == 4) {
3380 expr_ty expression;
3381 asdl_seq *suite_seq;
3382
3383 expression = ast_for_expr(c, CHILD(n, 1));
3384 if (!expression)
3385 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003387 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389
Guido van Rossumd8faa362007-04-27 19:54:29 +00003390 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3391 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 s = STR(CHILD(n, 4));
3395 /* s[2], the third character in the string, will be
3396 's' for el_s_e, or
3397 'i' for el_i_f
3398 */
3399 if (s[2] == 's') {
3400 expr_ty expression;
3401 asdl_seq *seq1, *seq2;
3402
3403 expression = ast_for_expr(c, CHILD(n, 1));
3404 if (!expression)
3405 return NULL;
3406 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003407 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 return NULL;
3409 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003410 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 return NULL;
3412
Guido van Rossumd8faa362007-04-27 19:54:29 +00003413 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3414 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415 }
3416 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003417 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003418 expr_ty expression;
3419 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003420 asdl_seq *orelse = NULL;
3421 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422 /* must reference the child n_elif+1 since 'else' token is third,
3423 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003424 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3425 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3426 has_else = 1;
3427 n_elif -= 3;
3428 }
3429 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430
Thomas Wouters89f507f2006-12-13 04:49:30 +00003431 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003432 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003434 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003435 if (!orelse)
3436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003438 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003440 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3441 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003443 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3444 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 asdl_seq_SET(orelse, 0,
3448 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003449 LINENO(CHILD(n, NCH(n) - 6)),
3450 CHILD(n, NCH(n) - 6)->n_col_offset,
3451 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 /* the just-created orelse handled the last elif */
3453 n_elif--;
3454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455
Thomas Wouters89f507f2006-12-13 04:49:30 +00003456 for (i = 0; i < n_elif; i++) {
3457 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003458 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003459 if (!newobj)
3460 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003462 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003465 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467
Thomas Wouters89f507f2006-12-13 04:49:30 +00003468 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003470 LINENO(CHILD(n, off)),
3471 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003472 orelse = newobj;
3473 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003474 expression = ast_for_expr(c, CHILD(n, 1));
3475 if (!expression)
3476 return NULL;
3477 suite_seq = ast_for_suite(c, CHILD(n, 3));
3478 if (!suite_seq)
3479 return NULL;
3480 return If(expression, suite_seq, orelse,
3481 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003483
3484 PyErr_Format(PyExc_SystemError,
3485 "unexpected token in 'if' statement: %s", s);
3486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487}
3488
3489static stmt_ty
3490ast_for_while_stmt(struct compiling *c, const node *n)
3491{
3492 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3493 REQ(n, while_stmt);
3494
3495 if (NCH(n) == 4) {
3496 expr_ty expression;
3497 asdl_seq *suite_seq;
3498
3499 expression = ast_for_expr(c, CHILD(n, 1));
3500 if (!expression)
3501 return NULL;
3502 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003503 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003505 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 }
3507 else if (NCH(n) == 7) {
3508 expr_ty expression;
3509 asdl_seq *seq1, *seq2;
3510
3511 expression = ast_for_expr(c, CHILD(n, 1));
3512 if (!expression)
3513 return NULL;
3514 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003515 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 return NULL;
3517 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003518 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 return NULL;
3520
Thomas Wouters89f507f2006-12-13 04:49:30 +00003521 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003523
3524 PyErr_Format(PyExc_SystemError,
3525 "wrong number of tokens for 'while' statement: %d",
3526 NCH(n));
3527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528}
3529
3530static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003531ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003533 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003535 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003536 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3538 REQ(n, for_stmt);
3539
3540 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003541 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 if (!seq)
3543 return NULL;
3544 }
3545
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003546 node_target = CHILD(n, 1);
3547 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003548 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003550 /* Check the # of children rather than the length of _target, since
3551 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003552 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003553 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003554 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003556 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003558 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003559 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 return NULL;
3561 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003562 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 return NULL;
3564
Yury Selivanov75445082015-05-11 22:57:16 -04003565 if (is_async)
3566 return AsyncFor(target, expression, suite_seq, seq,
3567 LINENO(n), n->n_col_offset,
3568 c->c_arena);
3569 else
3570 return For(target, expression, suite_seq, seq,
3571 LINENO(n), n->n_col_offset,
3572 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573}
3574
3575static excepthandler_ty
3576ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3577{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003578 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 REQ(exc, except_clause);
3580 REQ(body, suite);
3581
3582 if (NCH(exc) == 1) {
3583 asdl_seq *suite_seq = ast_for_suite(c, body);
3584 if (!suite_seq)
3585 return NULL;
3586
Neal Norwitzad74aa82008-03-31 05:14:30 +00003587 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003588 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 }
3590 else if (NCH(exc) == 2) {
3591 expr_ty expression;
3592 asdl_seq *suite_seq;
3593
3594 expression = ast_for_expr(c, CHILD(exc, 1));
3595 if (!expression)
3596 return NULL;
3597 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003598 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 return NULL;
3600
Neal Norwitzad74aa82008-03-31 05:14:30 +00003601 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003602 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 }
3604 else if (NCH(exc) == 4) {
3605 asdl_seq *suite_seq;
3606 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003607 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003608 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003610 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003611 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003613 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 return NULL;
3615 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003616 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 return NULL;
3618
Neal Norwitzad74aa82008-03-31 05:14:30 +00003619 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003620 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003622
3623 PyErr_Format(PyExc_SystemError,
3624 "wrong number of children for 'except' clause: %d",
3625 NCH(exc));
3626 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627}
3628
3629static stmt_ty
3630ast_for_try_stmt(struct compiling *c, const node *n)
3631{
Neal Norwitzf599f422005-12-17 21:33:47 +00003632 const int nch = NCH(n);
3633 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003634 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003635
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 REQ(n, try_stmt);
3637
Neal Norwitzf599f422005-12-17 21:33:47 +00003638 body = ast_for_suite(c, CHILD(n, 2));
3639 if (body == NULL)
3640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641
Neal Norwitzf599f422005-12-17 21:33:47 +00003642 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3643 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3644 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3645 /* we can assume it's an "else",
3646 because nch >= 9 for try-else-finally and
3647 it would otherwise have a type of except_clause */
3648 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3649 if (orelse == NULL)
3650 return NULL;
3651 n_except--;
3652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653
Neal Norwitzf599f422005-12-17 21:33:47 +00003654 finally = ast_for_suite(c, CHILD(n, nch - 1));
3655 if (finally == NULL)
3656 return NULL;
3657 n_except--;
3658 }
3659 else {
3660 /* we can assume it's an "else",
3661 otherwise it would have a type of except_clause */
3662 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3663 if (orelse == NULL)
3664 return NULL;
3665 n_except--;
3666 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003668 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003669 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 return NULL;
3671 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672
Neal Norwitzf599f422005-12-17 21:33:47 +00003673 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003674 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003675 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003676 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003677 if (handlers == NULL)
3678 return NULL;
3679
3680 for (i = 0; i < n_except; i++) {
3681 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3682 CHILD(n, 5 + i * 3));
3683 if (!e)
3684 return NULL;
3685 asdl_seq_SET(handlers, i, e);
3686 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003687 }
3688
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003689 assert(finally != NULL || asdl_seq_LEN(handlers));
3690 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691}
3692
Georg Brandl0c315622009-05-25 21:10:36 +00003693/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003694static withitem_ty
3695ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003696{
3697 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003698
Georg Brandl0c315622009-05-25 21:10:36 +00003699 REQ(n, with_item);
3700 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003701 if (!context_expr)
3702 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003703 if (NCH(n) == 3) {
3704 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003705
3706 if (!optional_vars) {
3707 return NULL;
3708 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003709 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003710 return NULL;
3711 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003712 }
3713
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003714 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003715}
3716
Georg Brandl0c315622009-05-25 21:10:36 +00003717/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3718static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003719ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003720{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003721 int i, n_items;
3722 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003723
3724 REQ(n, with_stmt);
3725
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003726 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003727 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003728 if (!items)
3729 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003730 for (i = 1; i < NCH(n) - 2; i += 2) {
3731 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3732 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003733 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003734 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003735 }
3736
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003737 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3738 if (!body)
3739 return NULL;
3740
Yury Selivanov75445082015-05-11 22:57:16 -04003741 if (is_async)
3742 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3743 else
3744 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003745}
3746
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003748ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003750 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003751 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003752 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003753 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003754
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 REQ(n, classdef);
3756
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003757 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 s = ast_for_suite(c, CHILD(n, 3));
3759 if (!s)
3760 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003761 classname = NEW_IDENTIFIER(CHILD(n, 1));
3762 if (!classname)
3763 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003764 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003765 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003766 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3767 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003769
3770 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003771 s = ast_for_suite(c, CHILD(n,5));
3772 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003773 return NULL;
3774 classname = NEW_IDENTIFIER(CHILD(n, 1));
3775 if (!classname)
3776 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003777 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003778 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003779 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3780 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 }
3782
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003783 /* class NAME '(' arglist ')' ':' suite */
3784 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003785 {
3786 PyObject *dummy_name;
3787 expr_ty dummy;
3788 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3789 if (!dummy_name)
3790 return NULL;
3791 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3792 call = ast_for_call(c, CHILD(n, 3), dummy);
3793 if (!call)
3794 return NULL;
3795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003797 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003799 classname = NEW_IDENTIFIER(CHILD(n, 1));
3800 if (!classname)
3801 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003802 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003803 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003804
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003805 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003806 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807}
3808
3809static stmt_ty
3810ast_for_stmt(struct compiling *c, const node *n)
3811{
3812 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003813 assert(NCH(n) == 1);
3814 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 }
3816 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003817 assert(num_stmts(n) == 1);
3818 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 }
3820 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003821 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003822 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3823 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003824 */
3825 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 case expr_stmt:
3827 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 case del_stmt:
3829 return ast_for_del_stmt(c, n);
3830 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003831 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 case flow_stmt:
3833 return ast_for_flow_stmt(c, n);
3834 case import_stmt:
3835 return ast_for_import_stmt(c, n);
3836 case global_stmt:
3837 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003838 case nonlocal_stmt:
3839 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 case assert_stmt:
3841 return ast_for_assert_stmt(c, n);
3842 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003843 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3845 TYPE(n), NCH(n));
3846 return NULL;
3847 }
3848 }
3849 else {
3850 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003851 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003852 */
3853 node *ch = CHILD(n, 0);
3854 REQ(n, compound_stmt);
3855 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 case if_stmt:
3857 return ast_for_if_stmt(c, ch);
3858 case while_stmt:
3859 return ast_for_while_stmt(c, ch);
3860 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003861 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 case try_stmt:
3863 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003864 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003865 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003867 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003869 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 case decorated:
3871 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003872 case async_stmt:
3873 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003875 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3877 TYPE(n), NCH(n));
3878 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880 }
3881}
3882
3883static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003884parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003886 const char *end;
3887 long x;
3888 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003889 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003890 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003892 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003893 errno = 0;
3894 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003895 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003896 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003897 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003898 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003899 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003900 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003901 }
3902 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003903 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003904 if (*end == '\0') {
3905 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003906 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003907 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003908 }
3909 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003910 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003911 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003912 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3913 if (compl.imag == -1.0 && PyErr_Occurred())
3914 return NULL;
3915 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003916 }
3917 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003918 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003919 dx = PyOS_string_to_double(s, NULL, NULL);
3920 if (dx == -1.0 && PyErr_Occurred())
3921 return NULL;
3922 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003923 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924}
3925
3926static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003927decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003929 const char *s, *t;
3930 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003931 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3932 while (s < end && (*s & 0x80)) s++;
3933 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003934 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935}
3936
3937static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003938decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003940 PyObject *v, *u;
3941 char *buf;
3942 char *p;
3943 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003944
Guido van Rossumd8faa362007-04-27 19:54:29 +00003945 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003946 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003947 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003948 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003949 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003950 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003951 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3952 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3953 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003954 if (u == NULL)
3955 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003956 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003957 end = s + len;
3958 while (s < end) {
3959 if (*s == '\\') {
3960 *p++ = *s++;
3961 if (*s & 0x80) {
3962 strcpy(p, "u005c");
3963 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003964 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003965 }
3966 if (*s & 0x80) { /* XXX inefficient */
3967 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003968 int kind;
3969 void *data;
3970 Py_ssize_t len, i;
3971 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003972 if (w == NULL) {
3973 Py_DECREF(u);
3974 return NULL;
3975 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003976 kind = PyUnicode_KIND(w);
3977 data = PyUnicode_DATA(w);
3978 len = PyUnicode_GET_LENGTH(w);
3979 for (i = 0; i < len; i++) {
3980 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3981 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003982 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003983 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003984 /* Should be impossible to overflow */
3985 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003986 Py_DECREF(w);
3987 } else {
3988 *p++ = *s++;
3989 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003990 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003991 len = p - buf;
3992 s = buf;
3993 }
3994 if (rawmode)
3995 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3996 else
3997 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3998 Py_XDECREF(u);
3999 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000}
4001
4002/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00004003 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 * parsestr parses it, and returns the decoded Python string object.
4005 */
4006static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00004007parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004009 size_t len;
4010 const char *s = STR(n);
4011 int quote = Py_CHARMASK(*s);
4012 int rawmode = 0;
4013 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004014 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004015 while (!*bytesmode || !rawmode) {
4016 if (quote == 'b' || quote == 'B') {
4017 quote = *++s;
4018 *bytesmode = 1;
4019 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004020 else if (quote == 'u' || quote == 'U') {
4021 quote = *++s;
4022 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004023 else if (quote == 'r' || quote == 'R') {
4024 quote = *++s;
4025 rawmode = 1;
4026 }
4027 else {
4028 break;
4029 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004030 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004031 }
4032 if (quote != '\'' && quote != '\"') {
4033 PyErr_BadInternalCall();
4034 return NULL;
4035 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004036 s++;
4037 len = strlen(s);
4038 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004040 "string to parse is too long");
4041 return NULL;
4042 }
4043 if (s[--len] != quote) {
4044 PyErr_BadInternalCall();
4045 return NULL;
4046 }
4047 if (len >= 4 && s[0] == quote && s[1] == quote) {
4048 s += 2;
4049 len -= 2;
4050 if (s[--len] != quote || s[--len] != quote) {
4051 PyErr_BadInternalCall();
4052 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004053 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004055 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004056 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004057 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 if (*bytesmode) {
4059 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004060 const char *ch;
4061 for (ch = s; *ch; ch++) {
4062 if (Py_CHARMASK(*ch) >= 0x80) {
4063 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 "literal characters.");
4065 return NULL;
4066 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004067 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004068 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004069 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004070 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004071 if (rawmode || strchr(s, '\\') == NULL) {
4072 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004073 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004074 if (u == NULL || !*bytesmode)
4075 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004076 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004077 Py_DECREF(u);
4078 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004079 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00004080 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004081 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004082 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004084 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004085 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004087 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00004088 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004089}
4090
Guido van Rossum29fd7122007-11-12 01:13:56 +00004091/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004092 * compile-time literal catenation, calling parsestr() on each piece, and
4093 * pasting the intermediate results together.
4094 */
4095static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00004096parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004098 PyObject *v;
4099 int i;
4100 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004101 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004102 if (v != NULL) {
4103 /* String literal concatenation */
4104 for (i = 1; i < NCH(n); i++) {
4105 PyObject *s;
4106 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004107 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004108 if (s == NULL)
4109 goto onError;
4110 if (*bytesmode != subbm) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004111 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02004112 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004113 goto onError;
4114 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004115 if (PyBytes_Check(v) && PyBytes_Check(s)) {
4116 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004117 if (v == NULL)
4118 goto onError;
4119 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004120 else {
4121 PyObject *temp = PyUnicode_Concat(v, s);
4122 Py_DECREF(s);
4123 Py_DECREF(v);
4124 v = temp;
4125 if (v == NULL)
4126 goto onError;
4127 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004128 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004129 }
4130 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131
Guido van Rossumd8faa362007-04-27 19:54:29 +00004132 onError:
4133 Py_XDECREF(v);
4134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135}