blob: b572088a46c9e84a9707edf0e9e57e4bfc82af91 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include <assert.h>
13
Benjamin Peterson832bfe22011-08-09 16:15:04 -050014static int validate_stmts(asdl_seq *);
15static int validate_exprs(asdl_seq *, expr_context_ty, int);
16static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17static int validate_stmt(stmt_ty);
18static int validate_expr(expr_ty, expr_context_ty);
19
20static int
21validate_comprehension(asdl_seq *gens)
22{
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36}
37
38static int
39validate_slice(slice_ty slice)
40{
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61}
62
63static int
64validate_keywords(asdl_seq *keywords)
65{
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
69 return 0;
70 return 1;
71}
72
73static int
74validate_args(asdl_seq *args)
75{
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83}
84
85static const char *
86expr_context_name(expr_context_ty ctx)
87{
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
135validate_expr(expr_ty exp, expr_context_ty ctx)
136{
137 int check_ctx = 1;
138 expr_context_ty actual_ctx;
139
140 /* First check expression context. */
141 switch (exp->kind) {
142 case Attribute_kind:
143 actual_ctx = exp->v.Attribute.ctx;
144 break;
145 case Subscript_kind:
146 actual_ctx = exp->v.Subscript.ctx;
147 break;
148 case Starred_kind:
149 actual_ctx = exp->v.Starred.ctx;
150 break;
151 case Name_kind:
152 actual_ctx = exp->v.Name.ctx;
153 break;
154 case List_kind:
155 actual_ctx = exp->v.List.ctx;
156 break;
157 case Tuple_kind:
158 actual_ctx = exp->v.Tuple.ctx;
159 break;
160 default:
161 if (ctx != Load) {
162 PyErr_Format(PyExc_ValueError, "expression which can't be "
163 "assigned to in %s context", expr_context_name(ctx));
164 return 0;
165 }
166 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100167 /* set actual_ctx to prevent gcc warning */
168 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500169 }
170 if (check_ctx && actual_ctx != ctx) {
171 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
172 expr_context_name(ctx), expr_context_name(actual_ctx));
173 return 0;
174 }
175
176 /* Now validate expression. */
177 switch (exp->kind) {
178 case BoolOp_kind:
179 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
180 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
181 return 0;
182 }
183 return validate_exprs(exp->v.BoolOp.values, Load, 0);
184 case BinOp_kind:
185 return validate_expr(exp->v.BinOp.left, Load) &&
186 validate_expr(exp->v.BinOp.right, Load);
187 case UnaryOp_kind:
188 return validate_expr(exp->v.UnaryOp.operand, Load);
189 case Lambda_kind:
190 return validate_arguments(exp->v.Lambda.args) &&
191 validate_expr(exp->v.Lambda.body, Load);
192 case IfExp_kind:
193 return validate_expr(exp->v.IfExp.test, Load) &&
194 validate_expr(exp->v.IfExp.body, Load) &&
195 validate_expr(exp->v.IfExp.orelse, Load);
196 case Dict_kind:
197 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
198 PyErr_SetString(PyExc_ValueError,
199 "Dict doesn't have the same number of keys as values");
200 return 0;
201 }
202 return validate_exprs(exp->v.Dict.keys, Load, 0) &&
203 validate_exprs(exp->v.Dict.values, Load, 0);
204 case Set_kind:
205 return validate_exprs(exp->v.Set.elts, Load, 0);
206#define COMP(NAME) \
207 case NAME ## _kind: \
208 return validate_comprehension(exp->v.NAME.generators) && \
209 validate_expr(exp->v.NAME.elt, Load);
210 COMP(ListComp)
211 COMP(SetComp)
212 COMP(GeneratorExp)
213#undef COMP
214 case DictComp_kind:
215 return validate_comprehension(exp->v.DictComp.generators) &&
216 validate_expr(exp->v.DictComp.key, Load) &&
217 validate_expr(exp->v.DictComp.value, Load);
218 case Yield_kind:
219 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500220 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000221 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400222 case Await_kind:
223 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500224 case Compare_kind:
225 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
226 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
227 return 0;
228 }
229 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
230 asdl_seq_LEN(exp->v.Compare.ops)) {
231 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
232 "of comparators and operands");
233 return 0;
234 }
235 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
236 validate_expr(exp->v.Compare.left, Load);
237 case Call_kind:
238 return validate_expr(exp->v.Call.func, Load) &&
239 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400240 validate_keywords(exp->v.Call.keywords);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500241 case Num_kind: {
242 PyObject *n = exp->v.Num.n;
243 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
244 !PyComplex_CheckExact(n)) {
245 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
246 return 0;
247 }
248 return 1;
249 }
250 case Str_kind: {
251 PyObject *s = exp->v.Str.s;
252 if (!PyUnicode_CheckExact(s)) {
253 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
254 return 0;
255 }
256 return 1;
257 }
258 case Bytes_kind: {
259 PyObject *b = exp->v.Bytes.s;
260 if (!PyBytes_CheckExact(b)) {
261 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
262 return 0;
263 }
264 return 1;
265 }
266 case Attribute_kind:
267 return validate_expr(exp->v.Attribute.value, Load);
268 case Subscript_kind:
269 return validate_slice(exp->v.Subscript.slice) &&
270 validate_expr(exp->v.Subscript.value, Load);
271 case Starred_kind:
272 return validate_expr(exp->v.Starred.value, ctx);
273 case List_kind:
274 return validate_exprs(exp->v.List.elts, ctx, 0);
275 case Tuple_kind:
276 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
277 /* These last cases don't have any checking. */
278 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500279 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500280 case Ellipsis_kind:
281 return 1;
282 default:
283 PyErr_SetString(PyExc_SystemError, "unexpected expression");
284 return 0;
285 }
286}
287
288static int
289validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
290{
291 if (asdl_seq_LEN(seq))
292 return 1;
293 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
294 return 0;
295}
296
297static int
298validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
299{
300 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
301 validate_exprs(targets, ctx, 0);
302}
303
304static int
305validate_body(asdl_seq *body, const char *owner)
306{
307 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
308}
309
310static int
311validate_stmt(stmt_ty stmt)
312{
313 int i;
314 switch (stmt->kind) {
315 case FunctionDef_kind:
316 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
317 validate_arguments(stmt->v.FunctionDef.args) &&
318 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
319 (!stmt->v.FunctionDef.returns ||
320 validate_expr(stmt->v.FunctionDef.returns, Load));
321 case ClassDef_kind:
322 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
323 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
324 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400325 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500326 case Return_kind:
327 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
328 case Delete_kind:
329 return validate_assignlist(stmt->v.Delete.targets, Del);
330 case Assign_kind:
331 return validate_assignlist(stmt->v.Assign.targets, Store) &&
332 validate_expr(stmt->v.Assign.value, Load);
333 case AugAssign_kind:
334 return validate_expr(stmt->v.AugAssign.target, Store) &&
335 validate_expr(stmt->v.AugAssign.value, Load);
336 case For_kind:
337 return validate_expr(stmt->v.For.target, Store) &&
338 validate_expr(stmt->v.For.iter, Load) &&
339 validate_body(stmt->v.For.body, "For") &&
340 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400341 case AsyncFor_kind:
342 return validate_expr(stmt->v.AsyncFor.target, Store) &&
343 validate_expr(stmt->v.AsyncFor.iter, Load) &&
344 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
345 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346 case While_kind:
347 return validate_expr(stmt->v.While.test, Load) &&
348 validate_body(stmt->v.While.body, "While") &&
349 validate_stmts(stmt->v.While.orelse);
350 case If_kind:
351 return validate_expr(stmt->v.If.test, Load) &&
352 validate_body(stmt->v.If.body, "If") &&
353 validate_stmts(stmt->v.If.orelse);
354 case With_kind:
355 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
356 return 0;
357 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
358 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
359 if (!validate_expr(item->context_expr, Load) ||
360 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
361 return 0;
362 }
363 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400364 case AsyncWith_kind:
365 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
366 return 0;
367 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
368 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
369 if (!validate_expr(item->context_expr, Load) ||
370 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
371 return 0;
372 }
373 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500374 case Raise_kind:
375 if (stmt->v.Raise.exc) {
376 return validate_expr(stmt->v.Raise.exc, Load) &&
377 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
378 }
379 if (stmt->v.Raise.cause) {
380 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
381 return 0;
382 }
383 return 1;
384 case Try_kind:
385 if (!validate_body(stmt->v.Try.body, "Try"))
386 return 0;
387 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
388 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
389 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
390 return 0;
391 }
392 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
393 asdl_seq_LEN(stmt->v.Try.orelse)) {
394 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
395 return 0;
396 }
397 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
398 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
399 if ((handler->v.ExceptHandler.type &&
400 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
401 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
402 return 0;
403 }
404 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
405 validate_stmts(stmt->v.Try.finalbody)) &&
406 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
407 validate_stmts(stmt->v.Try.orelse));
408 case Assert_kind:
409 return validate_expr(stmt->v.Assert.test, Load) &&
410 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
411 case Import_kind:
412 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
413 case ImportFrom_kind:
414 if (stmt->v.ImportFrom.level < -1) {
415 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
416 return 0;
417 }
418 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
419 case Global_kind:
420 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
421 case Nonlocal_kind:
422 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
423 case Expr_kind:
424 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400425 case AsyncFunctionDef_kind:
426 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
427 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
428 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
429 (!stmt->v.AsyncFunctionDef.returns ||
430 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500431 case Pass_kind:
432 case Break_kind:
433 case Continue_kind:
434 return 1;
435 default:
436 PyErr_SetString(PyExc_SystemError, "unexpected statement");
437 return 0;
438 }
439}
440
441static int
442validate_stmts(asdl_seq *seq)
443{
444 int i;
445 for (i = 0; i < asdl_seq_LEN(seq); i++) {
446 stmt_ty stmt = asdl_seq_GET(seq, i);
447 if (stmt) {
448 if (!validate_stmt(stmt))
449 return 0;
450 }
451 else {
452 PyErr_SetString(PyExc_ValueError,
453 "None disallowed in statement list");
454 return 0;
455 }
456 }
457 return 1;
458}
459
460static int
461validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
462{
463 int i;
464 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
465 expr_ty expr = asdl_seq_GET(exprs, i);
466 if (expr) {
467 if (!validate_expr(expr, ctx))
468 return 0;
469 }
470 else if (!null_ok) {
471 PyErr_SetString(PyExc_ValueError,
472 "None disallowed in expression list");
473 return 0;
474 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100475
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500476 }
477 return 1;
478}
479
480int
481PyAST_Validate(mod_ty mod)
482{
483 int res = 0;
484
485 switch (mod->kind) {
486 case Module_kind:
487 res = validate_stmts(mod->v.Module.body);
488 break;
489 case Interactive_kind:
490 res = validate_stmts(mod->v.Interactive.body);
491 break;
492 case Expression_kind:
493 res = validate_expr(mod->v.Expression.body, Load);
494 break;
495 case Suite_kind:
496 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
497 break;
498 default:
499 PyErr_SetString(PyExc_SystemError, "impossible module node");
500 res = 0;
501 break;
502 }
503 return res;
504}
505
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500506/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500507#include "grammar.h"
508#include "parsetok.h"
509#include "graminit.h"
510
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511/* Data structure used internally */
512struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000513 char *c_encoding; /* source encoding */
514 PyArena *c_arena; /* arena for allocating memeory */
Victor Stinner14e461d2013-08-26 22:28:21 +0200515 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500516 PyObject *c_normalize; /* Normalization function from unicodedata. */
517 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518};
519
520static asdl_seq *seq_for_testlist(struct compiling *, const node *);
521static expr_ty ast_for_expr(struct compiling *, const node *);
522static stmt_ty ast_for_stmt(struct compiling *, const node *);
523static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000524static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
525 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000526static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000527static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Yury Selivanov75445082015-05-11 22:57:16 -0400529static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
530static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532/* Note different signature for ast_for_call */
533static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
534
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000535static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000536static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +0000537static PyObject *parsestrplus(struct compiling *, const node *n,
538 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Nick Coghlan650f0d02007-04-15 12:05:43 +0000540#define COMP_GENEXP 0
541#define COMP_LISTCOMP 1
542#define COMP_SETCOMP 2
543
Benjamin Peterson55e00432012-01-16 17:22:31 -0500544static int
545init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000546{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500547 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
548 if (!m)
549 return 0;
550 c->c_normalize = PyObject_GetAttrString(m, "normalize");
551 Py_DECREF(m);
552 if (!c->c_normalize)
553 return 0;
554 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500555 if (!c->c_normalize_args) {
556 Py_CLEAR(c->c_normalize);
557 return 0;
558 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200559 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500560 return 1;
561}
562
563static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400564new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500565{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400566 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500567 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000568 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500569 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500570 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000571 /* Check whether there are non-ASCII characters in the
572 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500573 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200574 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500575 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500576 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500578 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500579 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
580 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500581 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 if (!id2)
583 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200584 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000585 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000586 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200587 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
588 Py_DECREF(id);
589 return NULL;
590 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000591 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592}
593
Benjamin Peterson55e00432012-01-16 17:22:31 -0500594#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400597ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400599 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Victor Stinner14e461d2013-08-26 22:28:21 +0200601 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000603 Py_INCREF(Py_None);
604 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200606 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400607 if (!tmp)
608 return 0;
609 errstr = PyUnicode_FromString(errmsg);
610 if (!errstr) {
611 Py_DECREF(tmp);
612 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000613 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 Py_DECREF(errstr);
616 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400617 if (value) {
618 PyErr_SetObject(PyExc_SyntaxError, value);
619 Py_DECREF(value);
620 }
621 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622}
623
624/* num_stmts() returns number of contained statements.
625
626 Use this routine to determine how big a sequence is needed for
627 the statements in a parse tree. Its raison d'etre is this bit of
628 grammar:
629
630 stmt: simple_stmt | compound_stmt
631 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
632
633 A simple_stmt can contain multiple small_stmt elements joined
634 by semicolons. If the arg is a simple_stmt, the number of
635 small_stmt elements is returned.
636*/
637
638static int
639num_stmts(const node *n)
640{
641 int i, l;
642 node *ch;
643
644 switch (TYPE(n)) {
645 case single_input:
646 if (TYPE(CHILD(n, 0)) == NEWLINE)
647 return 0;
648 else
649 return num_stmts(CHILD(n, 0));
650 case file_input:
651 l = 0;
652 for (i = 0; i < NCH(n); i++) {
653 ch = CHILD(n, i);
654 if (TYPE(ch) == stmt)
655 l += num_stmts(ch);
656 }
657 return l;
658 case stmt:
659 return num_stmts(CHILD(n, 0));
660 case compound_stmt:
661 return 1;
662 case simple_stmt:
663 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
664 case suite:
665 if (NCH(n) == 1)
666 return num_stmts(CHILD(n, 0));
667 else {
668 l = 0;
669 for (i = 2; i < (NCH(n) - 1); i++)
670 l += num_stmts(CHILD(n, i));
671 return l;
672 }
673 default: {
674 char buf[128];
675
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000676 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 TYPE(n), NCH(n));
678 Py_FatalError(buf);
679 }
680 }
681 assert(0);
682 return 0;
683}
684
685/* Transform the CST rooted at node * to the appropriate AST
686*/
687
688mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200689PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
690 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000692 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 asdl_seq *stmts = NULL;
694 stmt_ty s;
695 node *ch;
696 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500697 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400699 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200700 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400701 c.c_filename = filename;
702 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000704 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000706#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400707 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500708 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000709#endif
710 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 } else if (TYPE(n) == encoding_decl) {
713 c.c_encoding = STR(n);
714 n = CHILD(n, 0);
715 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000717 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 }
719
Jeremy Hyltona8293132006-02-28 17:58:27 +0000720 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 switch (TYPE(n)) {
722 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200723 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500725 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 for (i = 0; i < NCH(n) - 1; i++) {
727 ch = CHILD(n, i);
728 if (TYPE(ch) == NEWLINE)
729 continue;
730 REQ(ch, stmt);
731 num = num_stmts(ch);
732 if (num == 1) {
733 s = ast_for_stmt(&c, ch);
734 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500735 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000736 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 }
738 else {
739 ch = CHILD(ch, 0);
740 REQ(ch, simple_stmt);
741 for (j = 0; j < num; j++) {
742 s = ast_for_stmt(&c, CHILD(ch, j * 2));
743 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500744 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000745 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 }
747 }
748 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500749 res = Module(stmts, arena);
750 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 case eval_input: {
752 expr_ty testlist_ast;
753
Nick Coghlan650f0d02007-04-15 12:05:43 +0000754 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000755 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 goto out;
758 res = Expression(testlist_ast, arena);
759 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 }
761 case single_input:
762 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200763 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500765 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
767 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000768 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 goto out;
770 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 }
772 else {
773 n = CHILD(n, 0);
774 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200775 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500777 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000779 s = ast_for_stmt(&c, n);
780 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 asdl_seq_SET(stmts, 0, s);
783 }
784 else {
785 /* Only a simple_stmt can contain multiple statements. */
786 REQ(n, simple_stmt);
787 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 if (TYPE(CHILD(n, i)) == NEWLINE)
789 break;
790 s = ast_for_stmt(&c, CHILD(n, i));
791 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500792 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 asdl_seq_SET(stmts, i / 2, s);
794 }
795 }
796
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500799 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000801 PyErr_Format(PyExc_SystemError,
802 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500803 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 out:
806 if (c.c_normalize) {
807 Py_DECREF(c.c_normalize);
808 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
809 Py_DECREF(c.c_normalize_args);
810 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
Victor Stinner14e461d2013-08-26 22:28:21 +0200814mod_ty
815PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
816 PyArena *arena)
817{
818 mod_ty mod;
819 PyObject *filename;
820 filename = PyUnicode_DecodeFSDefault(filename_str);
821 if (filename == NULL)
822 return NULL;
823 mod = PyAST_FromNodeObject(n, flags, filename, arena);
824 Py_DECREF(filename);
825 return mod;
826
827}
828
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
830*/
831
832static operator_ty
833get_operator(const node *n)
834{
835 switch (TYPE(n)) {
836 case VBAR:
837 return BitOr;
838 case CIRCUMFLEX:
839 return BitXor;
840 case AMPER:
841 return BitAnd;
842 case LEFTSHIFT:
843 return LShift;
844 case RIGHTSHIFT:
845 return RShift;
846 case PLUS:
847 return Add;
848 case MINUS:
849 return Sub;
850 case STAR:
851 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400852 case AT:
853 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 case SLASH:
855 return Div;
856 case DOUBLESLASH:
857 return FloorDiv;
858 case PERCENT:
859 return Mod;
860 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000861 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
863}
864
Guido van Rossume7ba4952007-06-06 23:52:48 +0000865static const char* FORBIDDEN[] = {
866 "None",
867 "True",
868 "False",
869 NULL,
870};
871
872static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400873forbidden_name(struct compiling *c, identifier name, const node *n,
874 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000875{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000876 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000877 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400878 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000879 return 1;
880 }
881 if (full_checks) {
882 const char **p;
883 for (p = FORBIDDEN; *p; p++) {
884 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400885 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000886 return 1;
887 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000888 }
889 }
890 return 0;
891}
892
Jeremy Hyltona8293132006-02-28 17:58:27 +0000893/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
895 Only sets context for expr kinds that "can appear in assignment context"
896 (according to ../Parser/Python.asdl). For other expr kinds, it sets
897 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898*/
899
900static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000901set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902{
903 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000904 /* If a particular expression type can't be used for assign / delete,
905 set expr_name to its name and an error message will be generated.
906 */
907 const char* expr_name = NULL;
908
909 /* The ast defines augmented store and load contexts, but the
910 implementation here doesn't actually use them. The code may be
911 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000912 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000913 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000914 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000915 */
916 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
918 switch (e->kind) {
919 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000920 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400921 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000922 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000923 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000925 e->v.Subscript.ctx = ctx;
926 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000927 case Starred_kind:
928 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000929 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000930 return 0;
931 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000933 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500934 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000935 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000936 }
937 e->v.Name.ctx = ctx;
938 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000940 e->v.List.ctx = ctx;
941 s = e->v.List.elts;
942 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000944 if (asdl_seq_LEN(e->v.Tuple.elts)) {
945 e->v.Tuple.ctx = ctx;
946 s = e->v.Tuple.elts;
947 }
948 else {
949 expr_name = "()";
950 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000951 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000952 case Lambda_kind:
953 expr_name = "lambda";
954 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000956 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000957 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000958 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000960 case UnaryOp_kind:
961 expr_name = "operator";
962 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 expr_name = "generator expression";
965 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500967 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000968 expr_name = "yield expression";
969 break;
Yury Selivanov75445082015-05-11 22:57:16 -0400970 case Await_kind:
971 expr_name = "await expression";
972 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 case ListComp_kind:
974 expr_name = "list comprehension";
975 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000976 case SetComp_kind:
977 expr_name = "set comprehension";
978 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000979 case DictComp_kind:
980 expr_name = "dict comprehension";
981 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000982 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000983 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Num_kind:
985 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500986 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000987 expr_name = "literal";
988 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500989 case NameConstant_kind:
990 expr_name = "keyword";
991 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000992 case Ellipsis_kind:
993 expr_name = "Ellipsis";
994 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000995 case Compare_kind:
996 expr_name = "comparison";
997 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998 case IfExp_kind:
999 expr_name = "conditional expression";
1000 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001001 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyErr_Format(PyExc_SystemError,
1003 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001004 e->kind, e->lineno);
1005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 /* Check for error string set by switch */
1008 if (expr_name) {
1009 char buf[300];
1010 PyOS_snprintf(buf, sizeof(buf),
1011 "can't %s %s",
1012 ctx == Store ? "assign to" : "delete",
1013 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001014 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001015 }
1016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 */
1020 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001021 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001024 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001025 return 0;
1026 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 }
1028 return 1;
1029}
1030
1031static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001032ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033{
1034 REQ(n, augassign);
1035 n = CHILD(n, 0);
1036 switch (STR(n)[0]) {
1037 case '+':
1038 return Add;
1039 case '-':
1040 return Sub;
1041 case '/':
1042 if (STR(n)[1] == '/')
1043 return FloorDiv;
1044 else
1045 return Div;
1046 case '%':
1047 return Mod;
1048 case '<':
1049 return LShift;
1050 case '>':
1051 return RShift;
1052 case '&':
1053 return BitAnd;
1054 case '^':
1055 return BitXor;
1056 case '|':
1057 return BitOr;
1058 case '*':
1059 if (STR(n)[1] == '*')
1060 return Pow;
1061 else
1062 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001063 case '@':
1064 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001066 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 }
1069}
1070
1071static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001072ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001074 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 |'is' 'not'
1076 */
1077 REQ(n, comp_op);
1078 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001079 n = CHILD(n, 0);
1080 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 case LESS:
1082 return Lt;
1083 case GREATER:
1084 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001085 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return Eq;
1087 case LESSEQUAL:
1088 return LtE;
1089 case GREATEREQUAL:
1090 return GtE;
1091 case NOTEQUAL:
1092 return NotEq;
1093 case NAME:
1094 if (strcmp(STR(n), "in") == 0)
1095 return In;
1096 if (strcmp(STR(n), "is") == 0)
1097 return Is;
1098 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001099 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 }
1104 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105 /* handle "not in" and "is not" */
1106 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 case NAME:
1108 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1109 return NotIn;
1110 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1111 return IsNot;
1112 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001113 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 }
Neal Norwitz79792652005-11-14 04:25:03 +00001118 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121}
1122
1123static asdl_seq *
1124seq_for_testlist(struct compiling *c, const node *n)
1125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001127 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1128 */
Armin Rigo31441302005-10-21 12:57:31 +00001129 asdl_seq *seq;
1130 expr_ty expression;
1131 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001132 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001134 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 if (!seq)
1136 return NULL;
1137
1138 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001140 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141
Benjamin Peterson4905e802009-09-27 02:43:28 +00001142 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145
1146 assert(i / 2 < seq->size);
1147 asdl_seq_SET(seq, i / 2, expression);
1148 }
1149 return seq;
1150}
1151
Neal Norwitzc1505362006-12-28 06:47:50 +00001152static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001153ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001154{
1155 identifier name;
1156 expr_ty annotation = NULL;
1157 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001158 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001159
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001160 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001161 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001162 name = NEW_IDENTIFIER(ch);
1163 if (!name)
1164 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001165 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001166 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001167
1168 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1169 annotation = ast_for_expr(c, CHILD(n, 2));
1170 if (!annotation)
1171 return NULL;
1172 }
1173
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001174 ret = arg(name, annotation, c->c_arena);
1175 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001176 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001177 ret->lineno = LINENO(n);
1178 ret->col_offset = n->n_col_offset;
1179 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180}
1181
Guido van Rossum4f72a782006-10-27 23:31:49 +00001182/* returns -1 if failed to handle keyword only arguments
1183 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001184 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001185 ^^^
1186 start pointing here
1187 */
1188static int
1189handle_keywordonly_args(struct compiling *c, const node *n, int start,
1190 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1191{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001192 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001193 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001194 expr_ty expression, annotation;
1195 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001196 int i = start;
1197 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001198
1199 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001200 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001201 return -1;
1202 }
1203 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001204 while (i < NCH(n)) {
1205 ch = CHILD(n, i);
1206 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001207 case vfpdef:
1208 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001209 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001210 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001211 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001212 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001213 asdl_seq_SET(kwdefaults, j, expression);
1214 i += 2; /* '=' and test */
1215 }
1216 else { /* setting NULL if no default value exists */
1217 asdl_seq_SET(kwdefaults, j, NULL);
1218 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 if (NCH(ch) == 3) {
1220 /* ch is NAME ':' test */
1221 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001222 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001223 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001224 }
1225 else {
1226 annotation = NULL;
1227 }
1228 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001229 argname = NEW_IDENTIFIER(ch);
1230 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001231 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001232 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001233 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001234 arg = arg(argname, annotation, c->c_arena);
1235 if (!arg)
1236 goto error;
Benjamin Peterson0714b8b2014-02-13 19:22:14 -05001237 arg->lineno = LINENO(ch);
1238 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001239 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 i += 2; /* the name and the comma */
1241 break;
1242 case DOUBLESTAR:
1243 return i;
1244 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001245 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 goto error;
1247 }
1248 }
1249 return i;
1250 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001252}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253
Jeremy Hyltona8293132006-02-28 17:58:27 +00001254/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255
1256static arguments_ty
1257ast_for_arguments(struct compiling *c, const node *n)
1258{
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 /* This function handles both typedargslist (function definition)
1260 and varargslist (lambda definition).
1261
1262 parameters: '(' [typedargslist] ')'
1263 typedargslist: ((tfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001265 | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001266 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001267 tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001268 varargslist: ((vfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001270 | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001271 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001274 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1275 int nposdefaults = 0, found_default = 0;
1276 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001277 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 node *ch;
1280
1281 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001283 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001286 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287
Jeremy Hyltone921e022008-07-17 16:37:17 +00001288 /* First count the number of positional args & defaults. The
1289 variable i is the loop index for this for loop and the next.
1290 The next loop picks up where the first leaves off.
1291 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 ch = CHILD(n, i);
1294 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001295 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001296 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001297 if (i < NCH(n) && /* skip argument following star */
1298 (TYPE(CHILD(n, i)) == tfpdef ||
1299 TYPE(CHILD(n, i)) == vfpdef)) {
1300 i++;
1301 }
1302 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001303 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001304 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001305 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001309 defaults for keyword only args */
1310 for ( ; i < NCH(n); ++i) {
1311 ch = CHILD(n, i);
1312 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001313 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001315 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001316 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001317 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001319 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001321 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001323 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001325 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 since we set NULL as default for keyword only argument w/o default
1328 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001329 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001330 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001332 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333
1334 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001335 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001339 /* tfpdef: NAME [':' test]
1340 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 */
1342 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001343 j = 0; /* index for defaults */
1344 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 ch = CHILD(n, i);
1347 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001348 case tfpdef:
1349 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1351 anything other than EQUAL or a comma? */
1352 /* XXX Should NCH(n) check be made a separate check? */
1353 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001354 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1355 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001356 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 assert(posdefaults != NULL);
1358 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001363 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001365 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001367 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001369 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 i += 2; /* the name and the comma */
1372 break;
1373 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 if (i+1 >= NCH(n)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001375 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001376 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 if (TYPE(ch) == COMMA) {
1381 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 i += 2; /* now follows keyword only arguments */
1383 res = handle_keywordonly_args(c, n, i,
1384 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 i = res; /* res has new position to process */
1387 }
1388 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001389 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001390 if (!vararg)
1391 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001392
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1395 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 int res = 0;
1397 res = handle_keywordonly_args(c, n, i,
1398 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001399 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 i = res; /* res has new position to process */
1401 }
1402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 break;
1404 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001405 ch = CHILD(n, i+1); /* tfpdef */
1406 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001407 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001408 if (!kwarg)
1409 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 i += 3;
1411 break;
1412 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001413 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 "unexpected node in varargslist: %d @ %d",
1415 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001416 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001419 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420}
1421
1422static expr_ty
1423ast_for_dotted_name(struct compiling *c, const node *n)
1424{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001425 expr_ty e;
1426 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 int i;
1429
1430 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001431
1432 lineno = LINENO(n);
1433 col_offset = n->n_col_offset;
1434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 id = NEW_IDENTIFIER(CHILD(n, 0));
1436 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001437 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001438 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441
1442 for (i = 2; i < NCH(n); i+=2) {
1443 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001444 if (!id)
1445 return NULL;
1446 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1447 if (!e)
1448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 }
1450
1451 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452}
1453
1454static expr_ty
1455ast_for_decorator(struct compiling *c, const node *n)
1456{
1457 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1458 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001459 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001462 REQ(CHILD(n, 0), AT);
1463 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1466 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001467 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001470 d = name_expr;
1471 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 }
1473 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001474 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001476 if (!d)
1477 return NULL;
1478 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 }
1480 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001481 d = ast_for_call(c, CHILD(n, 3), name_expr);
1482 if (!d)
1483 return NULL;
1484 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 }
1486
1487 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490static asdl_seq*
1491ast_for_decorators(struct compiling *c, const node *n)
1492{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001493 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001494 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001498 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 if (!decorator_seq)
1500 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001503 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001504 if (!d)
1505 return NULL;
1506 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 }
1508 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
1511static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001512ast_for_funcdef_impl(struct compiling *c, const node *n,
1513 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001515 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001516 identifier name;
1517 arguments_ty args;
1518 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001519 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001520 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521
1522 REQ(n, funcdef);
1523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 name = NEW_IDENTIFIER(CHILD(n, name_i));
1525 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001526 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001527 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001528 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1530 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001531 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001532 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1533 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1534 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001535 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001536 name_i += 2;
1537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 body = ast_for_suite(c, CHILD(n, name_i + 3));
1539 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541
Yury Selivanov75445082015-05-11 22:57:16 -04001542 if (is_async)
1543 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1544 LINENO(n),
1545 n->n_col_offset, c->c_arena);
1546 else
1547 return FunctionDef(name, args, body, decorator_seq, returns,
1548 LINENO(n),
1549 n->n_col_offset, c->c_arena);
1550}
1551
1552static stmt_ty
1553ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1554{
1555 /* async_funcdef: ASYNC funcdef */
1556 REQ(n, async_funcdef);
1557 REQ(CHILD(n, 0), ASYNC);
1558 REQ(CHILD(n, 1), funcdef);
1559
1560 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1561 1 /* is_async */);
1562}
1563
1564static stmt_ty
1565ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1566{
1567 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1568 return ast_for_funcdef_impl(c, n, decorator_seq,
1569 0 /* is_async */);
1570}
1571
1572
1573static stmt_ty
1574ast_for_async_stmt(struct compiling *c, const node *n)
1575{
1576 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1577 REQ(n, async_stmt);
1578 REQ(CHILD(n, 0), ASYNC);
1579
1580 switch (TYPE(CHILD(n, 1))) {
1581 case funcdef:
1582 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1583 1 /* is_async */);
1584 case with_stmt:
1585 return ast_for_with_stmt(c, CHILD(n, 1),
1586 1 /* is_async */);
1587
1588 case for_stmt:
1589 return ast_for_for_stmt(c, CHILD(n, 1),
1590 1 /* is_async */);
1591
1592 default:
1593 PyErr_Format(PyExc_SystemError,
1594 "invalid async stament: %s",
1595 STR(CHILD(n, 1)));
1596 return NULL;
1597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001600static stmt_ty
1601ast_for_decorated(struct compiling *c, const node *n)
1602{
Yury Selivanov75445082015-05-11 22:57:16 -04001603 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001604 stmt_ty thing = NULL;
1605 asdl_seq *decorator_seq = NULL;
1606
1607 REQ(n, decorated);
1608
1609 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1610 if (!decorator_seq)
1611 return NULL;
1612
1613 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001614 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001616
1617 if (TYPE(CHILD(n, 1)) == funcdef) {
1618 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1619 } else if (TYPE(CHILD(n, 1)) == classdef) {
1620 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001621 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1622 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001623 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001624 /* we count the decorators in when talking about the class' or
1625 * function's line number */
1626 if (thing) {
1627 thing->lineno = LINENO(n);
1628 thing->col_offset = n->n_col_offset;
1629 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001630 return thing;
1631}
1632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633static expr_ty
1634ast_for_lambdef(struct compiling *c, const node *n)
1635{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001636 /* lambdef: 'lambda' [varargslist] ':' test
1637 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638 arguments_ty args;
1639 expr_ty expression;
1640
1641 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001642 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (!args)
1644 return NULL;
1645 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001646 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 }
1649 else {
1650 args = ast_for_arguments(c, CHILD(n, 1));
1651 if (!args)
1652 return NULL;
1653 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001654 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 }
1657
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001658 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659}
1660
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001661static expr_ty
1662ast_for_ifexpr(struct compiling *c, const node *n)
1663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001665 expr_ty expression, body, orelse;
1666
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001667 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001668 body = ast_for_expr(c, CHILD(n, 0));
1669 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001670 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001671 expression = ast_for_expr(c, CHILD(n, 2));
1672 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001673 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001674 orelse = ast_for_expr(c, CHILD(n, 4));
1675 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001676 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001677 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1678 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001679}
1680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001682 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683
Nick Coghlan650f0d02007-04-15 12:05:43 +00001684 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685*/
1686
1687static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001688count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001690 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
Guido van Rossumd8faa362007-04-27 19:54:29 +00001692 count_comp_for:
1693 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001694 REQ(n, comp_for);
1695 if (NCH(n) == 5)
1696 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001697 else
1698 return n_fors;
1699 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001700 REQ(n, comp_iter);
1701 n = CHILD(n, 0);
1702 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001703 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001704 else if (TYPE(n) == comp_if) {
1705 if (NCH(n) == 3) {
1706 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001707 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001708 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001709 else
1710 return n_fors;
1711 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001712
Guido van Rossumd8faa362007-04-27 19:54:29 +00001713 /* Should never be reached */
1714 PyErr_SetString(PyExc_SystemError,
1715 "logic error in count_comp_fors");
1716 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717}
1718
Nick Coghlan650f0d02007-04-15 12:05:43 +00001719/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720
Nick Coghlan650f0d02007-04-15 12:05:43 +00001721 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722*/
1723
1724static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001725count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001727 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728
Guido van Rossumd8faa362007-04-27 19:54:29 +00001729 while (1) {
1730 REQ(n, comp_iter);
1731 if (TYPE(CHILD(n, 0)) == comp_for)
1732 return n_ifs;
1733 n = CHILD(n, 0);
1734 REQ(n, comp_if);
1735 n_ifs++;
1736 if (NCH(n) == 2)
1737 return n_ifs;
1738 n = CHILD(n, 2);
1739 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740}
1741
Guido van Rossum992d4a32007-07-11 13:09:30 +00001742static asdl_seq *
1743ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001746 asdl_seq *comps;
1747
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001748 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 if (n_fors == -1)
1750 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001751
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001752 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001753 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001755
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001757 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001759 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761
Guido van Rossum992d4a32007-07-11 13:09:30 +00001762 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001765 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001766 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001768 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001769 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001771
Thomas Wouters89f507f2006-12-13 04:49:30 +00001772 /* Check the # of children rather than the length of t, since
1773 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001774 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001775 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001776 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001778 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1779 c->c_arena),
1780 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001781 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001783
Guido van Rossum992d4a32007-07-11 13:09:30 +00001784 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 int j, n_ifs;
1786 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787
Guido van Rossum992d4a32007-07-11 13:09:30 +00001788 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001789 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001790 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001793 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001796
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001798 REQ(n, comp_iter);
1799 n = CHILD(n, 0);
1800 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801
Guido van Rossum992d4a32007-07-11 13:09:30 +00001802 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001803 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001804 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001805 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001806 if (NCH(n) == 3)
1807 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001809 /* on exit, must guarantee that n is a comp_for */
1810 if (TYPE(n) == comp_iter)
1811 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001812 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001814 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001816 return comps;
1817}
1818
1819static expr_ty
1820ast_for_itercomp(struct compiling *c, const node *n, int type)
1821{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001822 /* testlist_comp: (test|star_expr)
1823 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001824 expr_ty elt;
1825 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001826 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827
Guido van Rossum992d4a32007-07-11 13:09:30 +00001828 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001830 ch = CHILD(n, 0);
1831 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001832 if (!elt)
1833 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001834 if (elt->kind == Starred_kind) {
1835 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1836 return NULL;
1837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838
Guido van Rossum992d4a32007-07-11 13:09:30 +00001839 comps = ast_for_comprehension(c, CHILD(n, 1));
1840 if (!comps)
1841 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001842
1843 if (type == COMP_GENEXP)
1844 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1845 else if (type == COMP_LISTCOMP)
1846 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1847 else if (type == COMP_SETCOMP)
1848 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1849 else
1850 /* Should never happen */
1851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852}
1853
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001854/* Fills in the key, value pair corresponding to the dict element. In case
1855 * of an unpacking, key is NULL. *i is advanced by the number of ast
1856 * elements. Iff successful, nonzero is returned.
1857 */
1858static int
1859ast_for_dictelement(struct compiling *c, const node *n, int *i,
1860 expr_ty *key, expr_ty *value)
1861{
1862 expr_ty expression;
1863 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1864 assert(NCH(n) - *i >= 2);
1865
1866 expression = ast_for_expr(c, CHILD(n, *i + 1));
1867 if (!expression)
1868 return 0;
1869 *key = NULL;
1870 *value = expression;
1871
1872 *i += 2;
1873 }
1874 else {
1875 assert(NCH(n) - *i >= 3);
1876
1877 expression = ast_for_expr(c, CHILD(n, *i));
1878 if (!expression)
1879 return 0;
1880 *key = expression;
1881
1882 REQ(CHILD(n, *i + 1), COLON);
1883
1884 expression = ast_for_expr(c, CHILD(n, *i + 2));
1885 if (!expression)
1886 return 0;
1887 *value = expression;
1888
1889 *i += 3;
1890 }
1891 return 1;
1892}
1893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001895ast_for_dictcomp(struct compiling *c, const node *n)
1896{
1897 expr_ty key, value;
1898 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001899 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001901 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001902 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001903 assert(key);
1904 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001906 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 if (!comps)
1908 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909
Guido van Rossum992d4a32007-07-11 13:09:30 +00001910 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1911}
1912
1913static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001914ast_for_dictdisplay(struct compiling *c, const node *n)
1915{
1916 int i;
1917 int j;
1918 int size;
1919 asdl_seq *keys, *values;
1920
1921 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1922 keys = _Py_asdl_seq_new(size, c->c_arena);
1923 if (!keys)
1924 return NULL;
1925
1926 values = _Py_asdl_seq_new(size, c->c_arena);
1927 if (!values)
1928 return NULL;
1929
1930 j = 0;
1931 for (i = 0; i < NCH(n); i++) {
1932 expr_ty key, value;
1933
1934 if (!ast_for_dictelement(c, n, &i, &key, &value))
1935 return NULL;
1936 asdl_seq_SET(keys, j, key);
1937 asdl_seq_SET(values, j, value);
1938
1939 j++;
1940 }
1941 keys->size = j;
1942 values->size = j;
1943 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1944}
1945
1946static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001947ast_for_genexp(struct compiling *c, const node *n)
1948{
1949 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001950 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001951}
1952
1953static expr_ty
1954ast_for_listcomp(struct compiling *c, const node *n)
1955{
1956 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001957 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001958}
1959
1960static expr_ty
1961ast_for_setcomp(struct compiling *c, const node *n)
1962{
1963 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001964 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001965}
1966
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001967static expr_ty
1968ast_for_setdisplay(struct compiling *c, const node *n)
1969{
1970 int i;
1971 int size;
1972 asdl_seq *elts;
1973
1974 assert(TYPE(n) == (dictorsetmaker));
1975 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
1976 elts = _Py_asdl_seq_new(size, c->c_arena);
1977 if (!elts)
1978 return NULL;
1979 for (i = 0; i < NCH(n); i += 2) {
1980 expr_ty expression;
1981 expression = ast_for_expr(c, CHILD(n, i));
1982 if (!expression)
1983 return NULL;
1984 asdl_seq_SET(elts, i / 2, expression);
1985 }
1986 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1987}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001988
1989static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990ast_for_atom(struct compiling *c, const node *n)
1991{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001992 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1993 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001994 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 */
1996 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001997 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002000 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002001 PyObject *name;
2002 const char *s = STR(ch);
2003 size_t len = strlen(s);
2004 if (len >= 4 && len <= 5) {
2005 if (!strcmp(s, "None"))
2006 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2007 if (!strcmp(s, "True"))
2008 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2009 if (!strcmp(s, "False"))
2010 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2011 }
2012 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002013 if (!name)
2014 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002015 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002016 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2017 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00002019 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002020 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002021 const char *errtype = NULL;
2022 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2023 errtype = "unicode error";
2024 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2025 errtype = "value error";
2026 if (errtype) {
2027 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002028 PyObject *type, *value, *tback, *errstr;
2029 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002030 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002031 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002032 char *s = _PyUnicode_AsString(errstr);
2033 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002034 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002035 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002036 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002037 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002038 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002039 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002040 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002041 Py_XDECREF(tback);
2042 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002043 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002044 }
Victor Stinner43d81952013-07-17 00:57:58 +02002045 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2046 Py_DECREF(str);
2047 return NULL;
2048 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00002049 if (bytesmode)
2050 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
2051 else
2052 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 }
2054 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002055 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002056 if (!pynum)
2057 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002058
Victor Stinner43d81952013-07-17 00:57:58 +02002059 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2060 Py_DECREF(pynum);
2061 return NULL;
2062 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002063 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 }
Georg Brandldde00282007-03-18 19:01:53 +00002065 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002066 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002068 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069
Thomas Wouters89f507f2006-12-13 04:49:30 +00002070 if (TYPE(ch) == RPAR)
2071 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072
Thomas Wouters89f507f2006-12-13 04:49:30 +00002073 if (TYPE(ch) == yield_expr)
2074 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002079
Nick Coghlan650f0d02007-04-15 12:05:43 +00002080 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002082 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083
Thomas Wouters89f507f2006-12-13 04:49:30 +00002084 if (TYPE(ch) == RSQB)
2085 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002088 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2089 asdl_seq *elts = seq_for_testlist(c, ch);
2090 if (!elts)
2091 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002092
Thomas Wouters89f507f2006-12-13 04:49:30 +00002093 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2094 }
2095 else
2096 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002098 /* dictorsetmaker: ( ((test ':' test | '**' test)
2099 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2100 * ((test | '*' test)
2101 * (comp_for | (',' (test | '*' test))* [','])) ) */
Neal Norwitzc1505362006-12-28 06:47:50 +00002102 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002103 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002104 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002105 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002106 }
2107 else {
2108 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2109 if (NCH(ch) == 1 ||
2110 (NCH(ch) > 1 &&
2111 TYPE(CHILD(ch, 1)) == COMMA)) {
2112 /* It's a set display. */
2113 return ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002114 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002115 else if (NCH(ch) > 1 &&
2116 TYPE(CHILD(ch, 1)) == comp_for) {
2117 /* It's a set comprehension. */
2118 return ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002119 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002120 else if (NCH(ch) > 3 - is_dict &&
2121 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2122 /* It's a dictionary comprehension. */
2123 if (is_dict) {
2124 ast_error(c, n, "dict unpacking cannot be used in "
2125 "dict comprehension");
2126 return NULL;
2127 }
2128 return ast_for_dictcomp(c, ch);
2129 }
2130 else {
2131 /* It's a dictionary display. */
2132 return ast_for_dictdisplay(c, ch);
2133 }
Guido van Rossum86e58e22006-08-28 15:27:34 +00002134 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 }
2140}
2141
2142static slice_ty
2143ast_for_slice(struct compiling *c, const node *n)
2144{
2145 node *ch;
2146 expr_ty lower = NULL, upper = NULL, step = NULL;
2147
2148 REQ(n, subscript);
2149
2150 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002151 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 sliceop: ':' [test]
2153 */
2154 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 if (NCH(n) == 1 && TYPE(ch) == test) {
2156 /* 'step' variable hold no significance in terms of being used over
2157 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 if (!step)
2160 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 }
2164
2165 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 if (!lower)
2168 return NULL;
2169 }
2170
2171 /* If there's an upper bound it's in the second or third position. */
2172 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002173 if (NCH(n) > 1) {
2174 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 if (TYPE(n2) == test) {
2177 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 if (!upper)
2179 return NULL;
2180 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002183 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184
Thomas Wouters89f507f2006-12-13 04:49:30 +00002185 if (TYPE(n2) == test) {
2186 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 if (!upper)
2188 return NULL;
2189 }
2190 }
2191
2192 ch = CHILD(n, NCH(n) - 1);
2193 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002194 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002195 ch = CHILD(ch, 1);
2196 if (TYPE(ch) == test) {
2197 step = ast_for_expr(c, ch);
2198 if (!step)
2199 return NULL;
2200 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 }
2202 }
2203
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002204 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205}
2206
2207static expr_ty
2208ast_for_binop(struct compiling *c, const node *n)
2209{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002210 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002212 BinOp(BinOp(A, op, B), op, C).
2213 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214
Guido van Rossumd8faa362007-04-27 19:54:29 +00002215 int i, nops;
2216 expr_ty expr1, expr2, result;
2217 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218
Guido van Rossumd8faa362007-04-27 19:54:29 +00002219 expr1 = ast_for_expr(c, CHILD(n, 0));
2220 if (!expr1)
2221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Guido van Rossumd8faa362007-04-27 19:54:29 +00002223 expr2 = ast_for_expr(c, CHILD(n, 2));
2224 if (!expr2)
2225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226
Guido van Rossumd8faa362007-04-27 19:54:29 +00002227 newoperator = get_operator(CHILD(n, 1));
2228 if (!newoperator)
2229 return NULL;
2230
2231 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2232 c->c_arena);
2233 if (!result)
2234 return NULL;
2235
2236 nops = (NCH(n) - 1) / 2;
2237 for (i = 1; i < nops; i++) {
2238 expr_ty tmp_result, tmp;
2239 const node* next_oper = CHILD(n, i * 2 + 1);
2240
2241 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 return NULL;
2244
Guido van Rossumd8faa362007-04-27 19:54:29 +00002245 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2246 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 return NULL;
2248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002250 LINENO(next_oper), next_oper->n_col_offset,
2251 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002253 return NULL;
2254 result = tmp_result;
2255 }
2256 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257}
2258
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002259static expr_ty
2260ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002263 subscriptlist: subscript (',' subscript)* [',']
2264 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2265 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002266 REQ(n, trailer);
2267 if (TYPE(CHILD(n, 0)) == LPAR) {
2268 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002269 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002271 else
2272 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002273 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002274 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002275 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2276 if (!attr_id)
2277 return NULL;
2278 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002279 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002280 }
2281 else {
2282 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002283 REQ(CHILD(n, 2), RSQB);
2284 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002285 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002286 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2287 if (!slc)
2288 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2290 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002291 }
2292 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002294 by treating the sequence as a tuple literal if there are
2295 no slice features.
2296 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002297 int j;
2298 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002299 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002300 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002301 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002302 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002303 if (!slices)
2304 return NULL;
2305 for (j = 0; j < NCH(n); j += 2) {
2306 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002307 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002308 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002309 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002310 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002311 asdl_seq_SET(slices, j / 2, slc);
2312 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002313 if (!simple) {
2314 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002315 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002316 }
2317 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002318 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002319 if (!elts)
2320 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002321 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2322 slc = (slice_ty)asdl_seq_GET(slices, j);
2323 assert(slc->kind == Index_kind && slc->v.Index.value);
2324 asdl_seq_SET(elts, j, slc->v.Index.value);
2325 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002326 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002327 if (!e)
2328 return NULL;
2329 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002330 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002331 }
2332 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002333}
2334
2335static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002336ast_for_factor(struct compiling *c, const node *n)
2337{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002338 expr_ty expression;
2339
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002340 expression = ast_for_expr(c, CHILD(n, 1));
2341 if (!expression)
2342 return NULL;
2343
2344 switch (TYPE(CHILD(n, 0))) {
2345 case PLUS:
2346 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2347 c->c_arena);
2348 case MINUS:
2349 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2350 c->c_arena);
2351 case TILDE:
2352 return UnaryOp(Invert, expression, LINENO(n),
2353 n->n_col_offset, c->c_arena);
2354 }
2355 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2356 TYPE(CHILD(n, 0)));
2357 return NULL;
2358}
2359
2360static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002361ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002362{
Yury Selivanov75445082015-05-11 22:57:16 -04002363 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002365
2366 REQ(n, atom_expr);
2367 nch = NCH(n);
2368
2369 if (TYPE(CHILD(n, 0)) == AWAIT) {
2370 start = 1;
2371 assert(nch > 1);
2372 }
2373
2374 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 if (!e)
2376 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002377 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002378 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002379 if (start && nch == 2) {
2380 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2381 }
2382
2383 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002384 node *ch = CHILD(n, i);
2385 if (TYPE(ch) != trailer)
2386 break;
2387 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002388 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002389 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002390 tmp->lineno = e->lineno;
2391 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 e = tmp;
2393 }
Yury Selivanov75445082015-05-11 22:57:16 -04002394
2395 if (start) {
2396 /* there was an AWAIT */
2397 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2398 }
2399 else {
2400 return e;
2401 }
2402}
2403
2404static expr_ty
2405ast_for_power(struct compiling *c, const node *n)
2406{
2407 /* power: atom trailer* ('**' factor)*
2408 */
2409 expr_ty e;
2410 REQ(n, power);
2411 e = ast_for_atom_expr(c, CHILD(n, 0));
2412 if (!e)
2413 return NULL;
2414 if (NCH(n) == 1)
2415 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002416 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2417 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002418 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002419 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002420 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002421 }
2422 return e;
2423}
2424
Guido van Rossum0368b722007-05-11 16:50:42 +00002425static expr_ty
2426ast_for_starred(struct compiling *c, const node *n)
2427{
2428 expr_ty tmp;
2429 REQ(n, star_expr);
2430
2431 tmp = ast_for_expr(c, CHILD(n, 1));
2432 if (!tmp)
2433 return NULL;
2434
2435 /* The Load context is changed later. */
2436 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2437}
2438
2439
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440/* Do not name a variable 'expr'! Will cause a compile error.
2441*/
2442
2443static expr_ty
2444ast_for_expr(struct compiling *c, const node *n)
2445{
2446 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002447 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002448 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 and_test: not_test ('and' not_test)*
2451 not_test: 'not' not_test | comparison
2452 comparison: expr (comp_op expr)*
2453 expr: xor_expr ('|' xor_expr)*
2454 xor_expr: and_expr ('^' and_expr)*
2455 and_expr: shift_expr ('&' shift_expr)*
2456 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2457 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002458 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002460 power: atom_expr ['**' factor]
2461 atom_expr: [AWAIT] atom trailer*
2462 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 */
2464
2465 asdl_seq *seq;
2466 int i;
2467
2468 loop:
2469 switch (TYPE(n)) {
2470 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002471 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002472 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002473 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002475 else if (NCH(n) > 1)
2476 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 /* Fallthrough */
2478 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 case and_test:
2480 if (NCH(n) == 1) {
2481 n = CHILD(n, 0);
2482 goto loop;
2483 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002484 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 if (!seq)
2486 return NULL;
2487 for (i = 0; i < NCH(n); i += 2) {
2488 expr_ty e = ast_for_expr(c, CHILD(n, i));
2489 if (!e)
2490 return NULL;
2491 asdl_seq_SET(seq, i / 2, e);
2492 }
2493 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002494 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2495 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002496 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002497 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 case not_test:
2499 if (NCH(n) == 1) {
2500 n = CHILD(n, 0);
2501 goto loop;
2502 }
2503 else {
2504 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2505 if (!expression)
2506 return NULL;
2507
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002508 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2509 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
2511 case comparison:
2512 if (NCH(n) == 1) {
2513 n = CHILD(n, 0);
2514 goto loop;
2515 }
2516 else {
2517 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002518 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002519 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002520 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 if (!ops)
2522 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002523 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 return NULL;
2526 }
2527 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002528 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002530 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002531 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534
2535 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002536 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002540 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 asdl_seq_SET(cmps, i / 2, expression);
2542 }
2543 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002544 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002546 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002548 return Compare(expression, ops, cmps, LINENO(n),
2549 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 }
2551 break;
2552
Guido van Rossum0368b722007-05-11 16:50:42 +00002553 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 /* The next five cases all handle BinOps. The main body of code
2556 is the same in each case, but the switch turned inside out to
2557 reuse the code for each type of operator.
2558 */
2559 case expr:
2560 case xor_expr:
2561 case and_expr:
2562 case shift_expr:
2563 case arith_expr:
2564 case term:
2565 if (NCH(n) == 1) {
2566 n = CHILD(n, 0);
2567 goto loop;
2568 }
2569 return ast_for_binop(c, n);
2570 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002571 node *an = NULL;
2572 node *en = NULL;
2573 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002574 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002575 if (NCH(n) > 1)
2576 an = CHILD(n, 1); /* yield_arg */
2577 if (an) {
2578 en = CHILD(an, NCH(an) - 1);
2579 if (NCH(an) == 2) {
2580 is_from = 1;
2581 exp = ast_for_expr(c, en);
2582 }
2583 else
2584 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002585 if (!exp)
2586 return NULL;
2587 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002588 if (is_from)
2589 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2590 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002591 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002592 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 if (NCH(n) == 1) {
2594 n = CHILD(n, 0);
2595 goto loop;
2596 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002597 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002598 case power:
2599 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002601 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
2603 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002604 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 return NULL;
2606}
2607
2608static expr_ty
2609ast_for_call(struct compiling *c, const node *n, expr_ty func)
2610{
2611 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002612 arglist: argument (',' argument)* [',']
2613 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 */
2615
2616 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002617 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618 asdl_seq *args;
2619 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620
2621 REQ(n, arglist);
2622
2623 nargs = 0;
2624 nkeywords = 0;
2625 ngens = 0;
2626 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002627 node *ch = CHILD(n, i);
2628 if (TYPE(ch) == argument) {
2629 if (NCH(ch) == 1)
2630 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002631 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002633 else if (TYPE(CHILD(ch, 0)) == STAR)
2634 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002636 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 nkeywords++;
2638 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 }
2640 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002641 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642 "if not sole argument");
2643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645
2646 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002647 ast_error(c, n, "more than 255 arguments");
2648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002651 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002653 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002654 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002656 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002657
2658 nargs = 0; /* positional arguments + iterable argument unpackings */
2659 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2660 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002662 node *ch = CHILD(n, i);
2663 if (TYPE(ch) == argument) {
2664 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002665 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002666 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002667 /* a positional argument */
2668 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002669 if (ndoublestars) {
2670 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002671 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002672 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002673 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002674 else {
2675 ast_error(c, chch,
2676 "positional argument follows "
2677 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002678 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002679 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002680 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002681 e = ast_for_expr(c, chch);
2682 if (!e)
2683 return NULL;
2684 asdl_seq_SET(args, nargs++, e);
2685 }
2686 else if (TYPE(chch) == STAR) {
2687 /* an iterable argument unpacking */
2688 expr_ty starred;
2689 if (ndoublestars) {
2690 ast_error(c, chch,
2691 "iterable argument unpacking follows "
2692 "keyword argument unpacking");
2693 return NULL;
2694 }
2695 e = ast_for_expr(c, CHILD(ch, 1));
2696 if (!e)
2697 return NULL;
2698 starred = Starred(e, Load, LINENO(chch),
2699 chch->n_col_offset,
2700 c->c_arena);
2701 if (!starred)
2702 return NULL;
2703 asdl_seq_SET(args, nargs++, starred);
2704
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002705 }
2706 else if (TYPE(chch) == DOUBLESTAR) {
2707 /* a keyword argument unpacking */
2708 keyword_ty kw;
2709 i++;
2710 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002712 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 kw = keyword(NULL, e, c->c_arena);
2714 asdl_seq_SET(keywords, nkeywords++, kw);
2715 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002717 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002719 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002721 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002722 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002724 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002726 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002727 identifier key, tmp;
2728 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 /* chch is test, but must be an identifier? */
2731 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 /* f(lambda x: x[0] = 3) ends up getting parsed with
2735 * LHS test = lambda x: x[0], and RHS test = 3.
2736 * SF bug 132313 points out that complaining about a keyword
2737 * then is very confusing.
2738 */
2739 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002740 ast_error(c, chch,
2741 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002742 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 }
2744 else if (e->kind != Name_kind) {
2745 ast_error(c, chch,
2746 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002747 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002748 }
2749 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002750 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002752 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002753 for (k = 0; k < nkeywords; k++) {
2754 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002755 if (tmp && !PyUnicode_Compare(tmp, key)) {
2756 ast_error(c, chch,
2757 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002758 return NULL;
2759 }
2760 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002761 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002763 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002764 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002766 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002767 asdl_seq_SET(keywords, nkeywords++, kw);
2768 }
2769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 }
2771
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773}
2774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002776ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002778 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002779 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002781 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002782 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002783 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002784 }
2785 else {
2786 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002787 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002788 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002790 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 else {
2792 asdl_seq *tmp = seq_for_testlist(c, n);
2793 if (!tmp)
2794 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002795 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002797}
2798
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799static stmt_ty
2800ast_for_expr_stmt(struct compiling *c, const node *n)
2801{
2802 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002805 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002806 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002807 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 test: ... here starts the operator precendence dance
2809 */
2810
2811 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002812 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 if (!e)
2814 return NULL;
2815
Thomas Wouters89f507f2006-12-13 04:49:30 +00002816 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 }
2818 else if (TYPE(CHILD(n, 1)) == augassign) {
2819 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002820 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002821 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822
Thomas Wouters89f507f2006-12-13 04:49:30 +00002823 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 if (!expr1)
2825 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002826 if(!set_context(c, expr1, Store, ch))
2827 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002828 /* set_context checks that most expressions are not the left side.
2829 Augmented assignments can only have a name, a subscript, or an
2830 attribute on the left, though, so we have to explicitly check for
2831 those. */
2832 switch (expr1->kind) {
2833 case Name_kind:
2834 case Attribute_kind:
2835 case Subscript_kind:
2836 break;
2837 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002838 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002839 return NULL;
2840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841
Thomas Wouters89f507f2006-12-13 04:49:30 +00002842 ch = CHILD(n, 2);
2843 if (TYPE(ch) == testlist)
2844 expr2 = ast_for_testlist(c, ch);
2845 else
2846 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002847 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 return NULL;
2849
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002850 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002851 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 return NULL;
2853
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 }
2856 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 int i;
2858 asdl_seq *targets;
2859 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 expr_ty expression;
2861
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 /* a normal assignment */
2863 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002864 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002865 if (!targets)
2866 return NULL;
2867 for (i = 0; i < NCH(n) - 2; i += 2) {
2868 expr_ty e;
2869 node *ch = CHILD(n, i);
2870 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002871 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002872 return NULL;
2873 }
2874 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002878 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002879 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002880 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 asdl_seq_SET(targets, i / 2, e);
2883 }
2884 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002885 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 expression = ast_for_testlist(c, value);
2887 else
2888 expression = ast_for_expr(c, value);
2889 if (!expression)
2890 return NULL;
2891 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893}
2894
Benjamin Peterson78565b22009-06-28 19:19:51 +00002895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002897ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898{
2899 asdl_seq *seq;
2900 int i;
2901 expr_ty e;
2902
2903 REQ(n, exprlist);
2904
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002905 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 e = ast_for_expr(c, CHILD(n, i));
2910 if (!e)
2911 return NULL;
2912 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002913 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 }
2916 return seq;
2917}
2918
2919static stmt_ty
2920ast_for_del_stmt(struct compiling *c, const node *n)
2921{
2922 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 /* del_stmt: 'del' exprlist */
2925 REQ(n, del_stmt);
2926
2927 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2928 if (!expr_list)
2929 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002930 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931}
2932
2933static stmt_ty
2934ast_for_flow_stmt(struct compiling *c, const node *n)
2935{
2936 /*
2937 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2938 | yield_stmt
2939 break_stmt: 'break'
2940 continue_stmt: 'continue'
2941 return_stmt: 'return' [testlist]
2942 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002943 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 raise_stmt: 'raise' [test [',' test [',' test]]]
2945 */
2946 node *ch;
2947
2948 REQ(n, flow_stmt);
2949 ch = CHILD(n, 0);
2950 switch (TYPE(ch)) {
2951 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002952 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002954 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002956 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2957 if (!exp)
2958 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002959 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 }
2961 case return_stmt:
2962 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002963 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002965 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 if (!expression)
2967 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002968 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 }
2970 case raise_stmt:
2971 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002972 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2973 else if (NCH(ch) >= 2) {
2974 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2976 if (!expression)
2977 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002978 if (NCH(ch) == 4) {
2979 cause = ast_for_expr(c, CHILD(ch, 3));
2980 if (!cause)
2981 return NULL;
2982 }
2983 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 }
2985 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002986 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 "unexpected flow_stmt: %d", TYPE(ch));
2988 return NULL;
2989 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002990
2991 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2992 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993}
2994
2995static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00002996alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997{
2998 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002999 import_as_name: NAME ['as' NAME]
3000 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 dotted_name: NAME ('.' NAME)*
3002 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003003 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003004
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 loop:
3006 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003007 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003008 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003009 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003010 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003011 if (!name)
3012 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003013 if (NCH(n) == 3) {
3014 node *str_node = CHILD(n, 2);
3015 str = NEW_IDENTIFIER(str_node);
3016 if (!str)
3017 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003018 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003019 return NULL;
3020 }
3021 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003022 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003023 return NULL;
3024 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003025 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003026 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 case dotted_as_name:
3028 if (NCH(n) == 1) {
3029 n = CHILD(n, 0);
3030 goto loop;
3031 }
3032 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003033 node *asname_node = CHILD(n, 2);
3034 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003035 if (!a)
3036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003038 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003039 if (!a->asname)
3040 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003041 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 return a;
3044 }
3045 break;
3046 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003047 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003048 node *name_node = CHILD(n, 0);
3049 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003050 if (!name)
3051 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003052 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003053 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003054 return alias(name, NULL, c->c_arena);
3055 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 else {
3057 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003058 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003059 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062
3063 len = 0;
3064 for (i = 0; i < NCH(n); i += 2)
3065 /* length of string plus one for the dot */
3066 len += strlen(STR(CHILD(n, i))) + 1;
3067 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003068 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069 if (!str)
3070 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003071 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 if (!s)
3073 return NULL;
3074 for (i = 0; i < NCH(n); i += 2) {
3075 char *sch = STR(CHILD(n, i));
3076 strcpy(s, STR(CHILD(n, i)));
3077 s += strlen(sch);
3078 *s++ = '.';
3079 }
3080 --s;
3081 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3083 PyBytes_GET_SIZE(str),
3084 NULL);
3085 Py_DECREF(str);
3086 if (!uni)
3087 return NULL;
3088 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003089 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003090 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3091 Py_DECREF(str);
3092 return NULL;
3093 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003094 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095 }
3096 break;
3097 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003098 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003099 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3100 Py_DECREF(str);
3101 return NULL;
3102 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003103 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003105 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 "unexpected import name: %d", TYPE(n));
3107 return NULL;
3108 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003109
3110 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 return NULL;
3112}
3113
3114static stmt_ty
3115ast_for_import_stmt(struct compiling *c, const node *n)
3116{
3117 /*
3118 import_stmt: import_name | import_from
3119 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003120 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3121 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003123 int lineno;
3124 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 int i;
3126 asdl_seq *aliases;
3127
3128 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003129 lineno = LINENO(n);
3130 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003132 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003134 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003135 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003136 if (!aliases)
3137 return NULL;
3138 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003139 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003140 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003142 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003144 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003146 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003148 int idx, ndots = 0;
3149 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003150 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003152 /* Count the number of dots (for relative imports) and check for the
3153 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003154 for (idx = 1; idx < NCH(n); idx++) {
3155 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003156 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3157 if (!mod)
3158 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003159 idx++;
3160 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003161 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003163 ndots += 3;
3164 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003165 } else if (TYPE(CHILD(n, idx)) != DOT) {
3166 break;
3167 }
3168 ndots++;
3169 }
3170 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003171 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003172 case STAR:
3173 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003174 n = CHILD(n, idx);
3175 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003176 break;
3177 case LPAR:
3178 /* from ... import (x, y, z) */
3179 n = CHILD(n, idx + 1);
3180 n_children = NCH(n);
3181 break;
3182 case import_as_names:
3183 /* from ... import x, y, z */
3184 n = CHILD(n, idx);
3185 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003186 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003187 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188 " surrounding parentheses");
3189 return NULL;
3190 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003191 break;
3192 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003193 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003194 return NULL;
3195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003197 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003198 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200
3201 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003202 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003203 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003204 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003206 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003208 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003209 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003210 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003211 if (!import_alias)
3212 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003213 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003216 if (mod != NULL)
3217 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003218 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003219 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 }
Neal Norwitz79792652005-11-14 04:25:03 +00003221 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 "unknown import statement: starts with command '%s'",
3223 STR(CHILD(n, 0)));
3224 return NULL;
3225}
3226
3227static stmt_ty
3228ast_for_global_stmt(struct compiling *c, const node *n)
3229{
3230 /* global_stmt: 'global' NAME (',' NAME)* */
3231 identifier name;
3232 asdl_seq *s;
3233 int i;
3234
3235 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003236 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003238 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003240 name = NEW_IDENTIFIER(CHILD(n, i));
3241 if (!name)
3242 return NULL;
3243 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003245 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246}
3247
3248static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003249ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3250{
3251 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3252 identifier name;
3253 asdl_seq *s;
3254 int i;
3255
3256 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003257 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003258 if (!s)
3259 return NULL;
3260 for (i = 1; i < NCH(n); i += 2) {
3261 name = NEW_IDENTIFIER(CHILD(n, i));
3262 if (!name)
3263 return NULL;
3264 asdl_seq_SET(s, i / 2, name);
3265 }
3266 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3267}
3268
3269static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270ast_for_assert_stmt(struct compiling *c, const node *n)
3271{
3272 /* assert_stmt: 'assert' test [',' test] */
3273 REQ(n, assert_stmt);
3274 if (NCH(n) == 2) {
3275 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3276 if (!expression)
3277 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003278 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 }
3280 else if (NCH(n) == 4) {
3281 expr_ty expr1, expr2;
3282
3283 expr1 = ast_for_expr(c, CHILD(n, 1));
3284 if (!expr1)
3285 return NULL;
3286 expr2 = ast_for_expr(c, CHILD(n, 3));
3287 if (!expr2)
3288 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289
Thomas Wouters89f507f2006-12-13 04:49:30 +00003290 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 }
Neal Norwitz79792652005-11-14 04:25:03 +00003292 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 "improper number of parts to 'assert' statement: %d",
3294 NCH(n));
3295 return NULL;
3296}
3297
3298static asdl_seq *
3299ast_for_suite(struct compiling *c, const node *n)
3300{
3301 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003302 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 stmt_ty s;
3304 int i, total, num, end, pos = 0;
3305 node *ch;
3306
3307 REQ(n, suite);
3308
3309 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003310 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 n = CHILD(n, 0);
3315 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 */
3318 end = NCH(n) - 1;
3319 if (TYPE(CHILD(n, end - 1)) == SEMI)
3320 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 for (i = 0; i < end; i += 2) {
3323 ch = CHILD(n, i);
3324 s = ast_for_stmt(c, ch);
3325 if (!s)
3326 return NULL;
3327 asdl_seq_SET(seq, pos++, s);
3328 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329 }
3330 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 for (i = 2; i < (NCH(n) - 1); i++) {
3332 ch = CHILD(n, i);
3333 REQ(ch, stmt);
3334 num = num_stmts(ch);
3335 if (num == 1) {
3336 /* small_stmt or compound_stmt with only one child */
3337 s = ast_for_stmt(c, ch);
3338 if (!s)
3339 return NULL;
3340 asdl_seq_SET(seq, pos++, s);
3341 }
3342 else {
3343 int j;
3344 ch = CHILD(ch, 0);
3345 REQ(ch, simple_stmt);
3346 for (j = 0; j < NCH(ch); j += 2) {
3347 /* statement terminates with a semi-colon ';' */
3348 if (NCH(CHILD(ch, j)) == 0) {
3349 assert((j + 1) == NCH(ch));
3350 break;
3351 }
3352 s = ast_for_stmt(c, CHILD(ch, j));
3353 if (!s)
3354 return NULL;
3355 asdl_seq_SET(seq, pos++, s);
3356 }
3357 }
3358 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359 }
3360 assert(pos == seq->size);
3361 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362}
3363
3364static stmt_ty
3365ast_for_if_stmt(struct compiling *c, const node *n)
3366{
3367 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3368 ['else' ':' suite]
3369 */
3370 char *s;
3371
3372 REQ(n, if_stmt);
3373
3374 if (NCH(n) == 4) {
3375 expr_ty expression;
3376 asdl_seq *suite_seq;
3377
3378 expression = ast_for_expr(c, CHILD(n, 1));
3379 if (!expression)
3380 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003382 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384
Guido van Rossumd8faa362007-04-27 19:54:29 +00003385 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3386 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389 s = STR(CHILD(n, 4));
3390 /* s[2], the third character in the string, will be
3391 's' for el_s_e, or
3392 'i' for el_i_f
3393 */
3394 if (s[2] == 's') {
3395 expr_ty expression;
3396 asdl_seq *seq1, *seq2;
3397
3398 expression = ast_for_expr(c, CHILD(n, 1));
3399 if (!expression)
3400 return NULL;
3401 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003402 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 return NULL;
3404 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003405 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 return NULL;
3407
Guido van Rossumd8faa362007-04-27 19:54:29 +00003408 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3409 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 }
3411 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003412 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003413 expr_ty expression;
3414 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003415 asdl_seq *orelse = NULL;
3416 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 /* must reference the child n_elif+1 since 'else' token is third,
3418 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003419 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3420 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3421 has_else = 1;
3422 n_elif -= 3;
3423 }
3424 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425
Thomas Wouters89f507f2006-12-13 04:49:30 +00003426 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003427 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003429 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003430 if (!orelse)
3431 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003433 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003435 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3436 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003438 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3439 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 asdl_seq_SET(orelse, 0,
3443 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003444 LINENO(CHILD(n, NCH(n) - 6)),
3445 CHILD(n, NCH(n) - 6)->n_col_offset,
3446 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003447 /* the just-created orelse handled the last elif */
3448 n_elif--;
3449 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450
Thomas Wouters89f507f2006-12-13 04:49:30 +00003451 for (i = 0; i < n_elif; i++) {
3452 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003453 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003454 if (!newobj)
3455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003457 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003460 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462
Thomas Wouters89f507f2006-12-13 04:49:30 +00003463 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003465 LINENO(CHILD(n, off)),
3466 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003467 orelse = newobj;
3468 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003469 expression = ast_for_expr(c, CHILD(n, 1));
3470 if (!expression)
3471 return NULL;
3472 suite_seq = ast_for_suite(c, CHILD(n, 3));
3473 if (!suite_seq)
3474 return NULL;
3475 return If(expression, suite_seq, orelse,
3476 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003478
3479 PyErr_Format(PyExc_SystemError,
3480 "unexpected token in 'if' statement: %s", s);
3481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482}
3483
3484static stmt_ty
3485ast_for_while_stmt(struct compiling *c, const node *n)
3486{
3487 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3488 REQ(n, while_stmt);
3489
3490 if (NCH(n) == 4) {
3491 expr_ty expression;
3492 asdl_seq *suite_seq;
3493
3494 expression = ast_for_expr(c, CHILD(n, 1));
3495 if (!expression)
3496 return NULL;
3497 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003498 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003500 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 }
3502 else if (NCH(n) == 7) {
3503 expr_ty expression;
3504 asdl_seq *seq1, *seq2;
3505
3506 expression = ast_for_expr(c, CHILD(n, 1));
3507 if (!expression)
3508 return NULL;
3509 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003510 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 return NULL;
3512 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003513 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514 return NULL;
3515
Thomas Wouters89f507f2006-12-13 04:49:30 +00003516 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003518
3519 PyErr_Format(PyExc_SystemError,
3520 "wrong number of tokens for 'while' statement: %d",
3521 NCH(n));
3522 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523}
3524
3525static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003526ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003528 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003530 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003531 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3533 REQ(n, for_stmt);
3534
3535 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003536 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 if (!seq)
3538 return NULL;
3539 }
3540
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003541 node_target = CHILD(n, 1);
3542 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003543 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003544 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003545 /* Check the # of children rather than the length of _target, since
3546 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003547 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003548 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003549 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003551 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003553 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003554 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 return NULL;
3556 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003557 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 return NULL;
3559
Yury Selivanov75445082015-05-11 22:57:16 -04003560 if (is_async)
3561 return AsyncFor(target, expression, suite_seq, seq,
3562 LINENO(n), n->n_col_offset,
3563 c->c_arena);
3564 else
3565 return For(target, expression, suite_seq, seq,
3566 LINENO(n), n->n_col_offset,
3567 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568}
3569
3570static excepthandler_ty
3571ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3572{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003573 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 REQ(exc, except_clause);
3575 REQ(body, suite);
3576
3577 if (NCH(exc) == 1) {
3578 asdl_seq *suite_seq = ast_for_suite(c, body);
3579 if (!suite_seq)
3580 return NULL;
3581
Neal Norwitzad74aa82008-03-31 05:14:30 +00003582 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003583 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 }
3585 else if (NCH(exc) == 2) {
3586 expr_ty expression;
3587 asdl_seq *suite_seq;
3588
3589 expression = ast_for_expr(c, CHILD(exc, 1));
3590 if (!expression)
3591 return NULL;
3592 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003593 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 return NULL;
3595
Neal Norwitzad74aa82008-03-31 05:14:30 +00003596 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003597 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 }
3599 else if (NCH(exc) == 4) {
3600 asdl_seq *suite_seq;
3601 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003602 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003605 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003608 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 return NULL;
3610 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003611 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 return NULL;
3613
Neal Norwitzad74aa82008-03-31 05:14:30 +00003614 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003615 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003617
3618 PyErr_Format(PyExc_SystemError,
3619 "wrong number of children for 'except' clause: %d",
3620 NCH(exc));
3621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622}
3623
3624static stmt_ty
3625ast_for_try_stmt(struct compiling *c, const node *n)
3626{
Neal Norwitzf599f422005-12-17 21:33:47 +00003627 const int nch = NCH(n);
3628 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003629 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003630
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 REQ(n, try_stmt);
3632
Neal Norwitzf599f422005-12-17 21:33:47 +00003633 body = ast_for_suite(c, CHILD(n, 2));
3634 if (body == NULL)
3635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636
Neal Norwitzf599f422005-12-17 21:33:47 +00003637 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3638 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3639 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3640 /* we can assume it's an "else",
3641 because nch >= 9 for try-else-finally and
3642 it would otherwise have a type of except_clause */
3643 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3644 if (orelse == NULL)
3645 return NULL;
3646 n_except--;
3647 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648
Neal Norwitzf599f422005-12-17 21:33:47 +00003649 finally = ast_for_suite(c, CHILD(n, nch - 1));
3650 if (finally == NULL)
3651 return NULL;
3652 n_except--;
3653 }
3654 else {
3655 /* we can assume it's an "else",
3656 otherwise it would have a type of except_clause */
3657 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3658 if (orelse == NULL)
3659 return NULL;
3660 n_except--;
3661 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003663 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003664 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 return NULL;
3666 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667
Neal Norwitzf599f422005-12-17 21:33:47 +00003668 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003669 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003670 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003671 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003672 if (handlers == NULL)
3673 return NULL;
3674
3675 for (i = 0; i < n_except; i++) {
3676 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3677 CHILD(n, 5 + i * 3));
3678 if (!e)
3679 return NULL;
3680 asdl_seq_SET(handlers, i, e);
3681 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003682 }
3683
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003684 assert(finally != NULL || asdl_seq_LEN(handlers));
3685 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686}
3687
Georg Brandl0c315622009-05-25 21:10:36 +00003688/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003689static withitem_ty
3690ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003691{
3692 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003693
Georg Brandl0c315622009-05-25 21:10:36 +00003694 REQ(n, with_item);
3695 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003696 if (!context_expr)
3697 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003698 if (NCH(n) == 3) {
3699 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003700
3701 if (!optional_vars) {
3702 return NULL;
3703 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003704 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003705 return NULL;
3706 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003707 }
3708
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003709 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003710}
3711
Georg Brandl0c315622009-05-25 21:10:36 +00003712/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3713static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003714ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003715{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003716 int i, n_items;
3717 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003718
3719 REQ(n, with_stmt);
3720
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003721 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003722 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003723 if (!items)
3724 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003725 for (i = 1; i < NCH(n) - 2; i += 2) {
3726 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3727 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003728 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003729 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003730 }
3731
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003732 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3733 if (!body)
3734 return NULL;
3735
Yury Selivanov75445082015-05-11 22:57:16 -04003736 if (is_async)
3737 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3738 else
3739 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003740}
3741
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003743ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003745 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003746 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003747 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003748 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003749
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 REQ(n, classdef);
3751
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003752 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 s = ast_for_suite(c, CHILD(n, 3));
3754 if (!s)
3755 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003756 classname = NEW_IDENTIFIER(CHILD(n, 1));
3757 if (!classname)
3758 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003759 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003760 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003761 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3762 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003764
3765 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003766 s = ast_for_suite(c, CHILD(n,5));
3767 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003768 return NULL;
3769 classname = NEW_IDENTIFIER(CHILD(n, 1));
3770 if (!classname)
3771 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003772 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003773 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003774 return ClassDef(classname, NULL, NULL, s, decorator_seq, LINENO(n),
3775 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 }
3777
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003778 /* class NAME '(' arglist ')' ':' suite */
3779 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003780 {
3781 PyObject *dummy_name;
3782 expr_ty dummy;
3783 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3784 if (!dummy_name)
3785 return NULL;
3786 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3787 call = ast_for_call(c, CHILD(n, 3), dummy);
3788 if (!call)
3789 return NULL;
3790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003792 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003794 classname = NEW_IDENTIFIER(CHILD(n, 1));
3795 if (!classname)
3796 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003797 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003798 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003799
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003800 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003801 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802}
3803
3804static stmt_ty
3805ast_for_stmt(struct compiling *c, const node *n)
3806{
3807 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003808 assert(NCH(n) == 1);
3809 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 }
3811 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003812 assert(num_stmts(n) == 1);
3813 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814 }
3815 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003816 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003817 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3818 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003819 */
3820 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 case expr_stmt:
3822 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 case del_stmt:
3824 return ast_for_del_stmt(c, n);
3825 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003826 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 case flow_stmt:
3828 return ast_for_flow_stmt(c, n);
3829 case import_stmt:
3830 return ast_for_import_stmt(c, n);
3831 case global_stmt:
3832 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003833 case nonlocal_stmt:
3834 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 case assert_stmt:
3836 return ast_for_assert_stmt(c, n);
3837 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003838 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3840 TYPE(n), NCH(n));
3841 return NULL;
3842 }
3843 }
3844 else {
3845 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003846 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003847 */
3848 node *ch = CHILD(n, 0);
3849 REQ(n, compound_stmt);
3850 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 case if_stmt:
3852 return ast_for_if_stmt(c, ch);
3853 case while_stmt:
3854 return ast_for_while_stmt(c, ch);
3855 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003856 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 case try_stmt:
3858 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003859 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04003860 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003862 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003864 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 case decorated:
3866 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04003867 case async_stmt:
3868 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003869 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003870 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3872 TYPE(n), NCH(n));
3873 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 }
3876}
3877
3878static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003879parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003881 const char *end;
3882 long x;
3883 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003884 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003885 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003887 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003888 errno = 0;
3889 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003890 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003891 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003892 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003893 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003894 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003895 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003896 }
3897 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003898 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003899 if (*end == '\0') {
3900 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003901 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003902 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003903 }
3904 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003905 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003906 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003907 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3908 if (compl.imag == -1.0 && PyErr_Occurred())
3909 return NULL;
3910 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003911 }
3912 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003913 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003914 dx = PyOS_string_to_double(s, NULL, NULL);
3915 if (dx == -1.0 && PyErr_Occurred())
3916 return NULL;
3917 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003918 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919}
3920
3921static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003922decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003924 const char *s, *t;
3925 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003926 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3927 while (s < end && (*s & 0x80)) s++;
3928 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003929 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930}
3931
3932static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003933decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003935 PyObject *v, *u;
3936 char *buf;
3937 char *p;
3938 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003939
Guido van Rossumd8faa362007-04-27 19:54:29 +00003940 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003941 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003942 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003943 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003944 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003945 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003946 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3947 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3948 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003949 if (u == NULL)
3950 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003951 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 end = s + len;
3953 while (s < end) {
3954 if (*s == '\\') {
3955 *p++ = *s++;
3956 if (*s & 0x80) {
3957 strcpy(p, "u005c");
3958 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003959 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003960 }
3961 if (*s & 0x80) { /* XXX inefficient */
3962 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003963 int kind;
3964 void *data;
3965 Py_ssize_t len, i;
3966 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003967 if (w == NULL) {
3968 Py_DECREF(u);
3969 return NULL;
3970 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003971 kind = PyUnicode_KIND(w);
3972 data = PyUnicode_DATA(w);
3973 len = PyUnicode_GET_LENGTH(w);
3974 for (i = 0; i < len; i++) {
3975 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3976 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003977 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003978 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003979 /* Should be impossible to overflow */
3980 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003981 Py_DECREF(w);
3982 } else {
3983 *p++ = *s++;
3984 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003985 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003986 len = p - buf;
3987 s = buf;
3988 }
3989 if (rawmode)
3990 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3991 else
3992 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3993 Py_XDECREF(u);
3994 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995}
3996
3997/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003998 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 * parsestr parses it, and returns the decoded Python string object.
4000 */
4001static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00004002parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004004 size_t len;
4005 const char *s = STR(n);
4006 int quote = Py_CHARMASK(*s);
4007 int rawmode = 0;
4008 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01004009 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004010 while (!*bytesmode || !rawmode) {
4011 if (quote == 'b' || quote == 'B') {
4012 quote = *++s;
4013 *bytesmode = 1;
4014 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00004015 else if (quote == 'u' || quote == 'U') {
4016 quote = *++s;
4017 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01004018 else if (quote == 'r' || quote == 'R') {
4019 quote = *++s;
4020 rawmode = 1;
4021 }
4022 else {
4023 break;
4024 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004025 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004026 }
4027 if (quote != '\'' && quote != '\"') {
4028 PyErr_BadInternalCall();
4029 return NULL;
4030 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004031 s++;
4032 len = strlen(s);
4033 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004035 "string to parse is too long");
4036 return NULL;
4037 }
4038 if (s[--len] != quote) {
4039 PyErr_BadInternalCall();
4040 return NULL;
4041 }
4042 if (len >= 4 && s[0] == quote && s[1] == quote) {
4043 s += 2;
4044 len -= 2;
4045 if (s[--len] != quote || s[--len] != quote) {
4046 PyErr_BadInternalCall();
4047 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00004048 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004049 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00004050 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004051 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004052 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 if (*bytesmode) {
4054 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004055 const char *ch;
4056 for (ch = s; *ch; ch++) {
4057 if (Py_CHARMASK(*ch) >= 0x80) {
4058 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004059 "literal characters.");
4060 return NULL;
4061 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00004062 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004063 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00004064 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00004065 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004066 if (rawmode || strchr(s, '\\') == NULL) {
4067 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00004068 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00004069 if (u == NULL || !*bytesmode)
4070 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004071 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004072 Py_DECREF(u);
4073 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004074 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00004075 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004076 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004077 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00004079 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004080 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004082 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00004083 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084}
4085
Guido van Rossum29fd7122007-11-12 01:13:56 +00004086/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087 * compile-time literal catenation, calling parsestr() on each piece, and
4088 * pasting the intermediate results together.
4089 */
4090static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00004091parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004092{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004093 PyObject *v;
4094 int i;
4095 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00004096 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004097 if (v != NULL) {
4098 /* String literal concatenation */
4099 for (i = 1; i < NCH(n); i++) {
4100 PyObject *s;
4101 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00004102 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004103 if (s == NULL)
4104 goto onError;
4105 if (*bytesmode != subbm) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004106 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02004107 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004108 goto onError;
4109 }
Christian Heimes72b710a2008-05-26 13:28:38 +00004110 if (PyBytes_Check(v) && PyBytes_Check(s)) {
4111 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004112 if (v == NULL)
4113 goto onError;
4114 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004115 else {
4116 PyObject *temp = PyUnicode_Concat(v, s);
4117 Py_DECREF(s);
4118 Py_DECREF(v);
4119 v = temp;
4120 if (v == NULL)
4121 goto onError;
4122 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004123 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004124 }
4125 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126
Guido van Rossumd8faa362007-04-27 19:54:29 +00004127 onError:
4128 Py_XDECREF(v);
4129 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130}