blob: b48471e502a0687bb3eb410ad0cc9401a7e86bd4 [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);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500222 case Compare_kind:
223 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
224 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
225 return 0;
226 }
227 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
228 asdl_seq_LEN(exp->v.Compare.ops)) {
229 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
230 "of comparators and operands");
231 return 0;
232 }
233 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
234 validate_expr(exp->v.Compare.left, Load);
235 case Call_kind:
236 return validate_expr(exp->v.Call.func, Load) &&
237 validate_exprs(exp->v.Call.args, Load, 0) &&
238 validate_keywords(exp->v.Call.keywords) &&
239 (!exp->v.Call.starargs || validate_expr(exp->v.Call.starargs, Load)) &&
240 (!exp->v.Call.kwargs || validate_expr(exp->v.Call.kwargs, Load));
241 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) &&
325 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0) &&
326 (!stmt->v.ClassDef.starargs || validate_expr(stmt->v.ClassDef.starargs, Load)) &&
327 (!stmt->v.ClassDef.kwargs || validate_expr(stmt->v.ClassDef.kwargs, Load));
328 case Return_kind:
329 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
330 case Delete_kind:
331 return validate_assignlist(stmt->v.Delete.targets, Del);
332 case Assign_kind:
333 return validate_assignlist(stmt->v.Assign.targets, Store) &&
334 validate_expr(stmt->v.Assign.value, Load);
335 case AugAssign_kind:
336 return validate_expr(stmt->v.AugAssign.target, Store) &&
337 validate_expr(stmt->v.AugAssign.value, Load);
338 case For_kind:
339 return validate_expr(stmt->v.For.target, Store) &&
340 validate_expr(stmt->v.For.iter, Load) &&
341 validate_body(stmt->v.For.body, "For") &&
342 validate_stmts(stmt->v.For.orelse);
343 case While_kind:
344 return validate_expr(stmt->v.While.test, Load) &&
345 validate_body(stmt->v.While.body, "While") &&
346 validate_stmts(stmt->v.While.orelse);
347 case If_kind:
348 return validate_expr(stmt->v.If.test, Load) &&
349 validate_body(stmt->v.If.body, "If") &&
350 validate_stmts(stmt->v.If.orelse);
351 case With_kind:
352 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
353 return 0;
354 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
355 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
356 if (!validate_expr(item->context_expr, Load) ||
357 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
358 return 0;
359 }
360 return validate_body(stmt->v.With.body, "With");
361 case Raise_kind:
362 if (stmt->v.Raise.exc) {
363 return validate_expr(stmt->v.Raise.exc, Load) &&
364 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
365 }
366 if (stmt->v.Raise.cause) {
367 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
368 return 0;
369 }
370 return 1;
371 case Try_kind:
372 if (!validate_body(stmt->v.Try.body, "Try"))
373 return 0;
374 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
375 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
376 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
377 return 0;
378 }
379 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
380 asdl_seq_LEN(stmt->v.Try.orelse)) {
381 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
382 return 0;
383 }
384 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
385 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
386 if ((handler->v.ExceptHandler.type &&
387 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
388 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
389 return 0;
390 }
391 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
392 validate_stmts(stmt->v.Try.finalbody)) &&
393 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
394 validate_stmts(stmt->v.Try.orelse));
395 case Assert_kind:
396 return validate_expr(stmt->v.Assert.test, Load) &&
397 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
398 case Import_kind:
399 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
400 case ImportFrom_kind:
401 if (stmt->v.ImportFrom.level < -1) {
402 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
403 return 0;
404 }
405 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
406 case Global_kind:
407 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
408 case Nonlocal_kind:
409 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
410 case Expr_kind:
411 return validate_expr(stmt->v.Expr.value, Load);
412 case Pass_kind:
413 case Break_kind:
414 case Continue_kind:
415 return 1;
416 default:
417 PyErr_SetString(PyExc_SystemError, "unexpected statement");
418 return 0;
419 }
420}
421
422static int
423validate_stmts(asdl_seq *seq)
424{
425 int i;
426 for (i = 0; i < asdl_seq_LEN(seq); i++) {
427 stmt_ty stmt = asdl_seq_GET(seq, i);
428 if (stmt) {
429 if (!validate_stmt(stmt))
430 return 0;
431 }
432 else {
433 PyErr_SetString(PyExc_ValueError,
434 "None disallowed in statement list");
435 return 0;
436 }
437 }
438 return 1;
439}
440
441static int
442validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
443{
444 int i;
445 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
446 expr_ty expr = asdl_seq_GET(exprs, i);
447 if (expr) {
448 if (!validate_expr(expr, ctx))
449 return 0;
450 }
451 else if (!null_ok) {
452 PyErr_SetString(PyExc_ValueError,
453 "None disallowed in expression list");
454 return 0;
455 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100456
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500457 }
458 return 1;
459}
460
461int
462PyAST_Validate(mod_ty mod)
463{
464 int res = 0;
465
466 switch (mod->kind) {
467 case Module_kind:
468 res = validate_stmts(mod->v.Module.body);
469 break;
470 case Interactive_kind:
471 res = validate_stmts(mod->v.Interactive.body);
472 break;
473 case Expression_kind:
474 res = validate_expr(mod->v.Expression.body, Load);
475 break;
476 case Suite_kind:
477 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
478 break;
479 default:
480 PyErr_SetString(PyExc_SystemError, "impossible module node");
481 res = 0;
482 break;
483 }
484 return res;
485}
486
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500487/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500488#include "grammar.h"
489#include "parsetok.h"
490#include "graminit.h"
491
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492/* Data structure used internally */
493struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000494 char *c_encoding; /* source encoding */
495 PyArena *c_arena; /* arena for allocating memeory */
Victor Stinner14e461d2013-08-26 22:28:21 +0200496 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500497 PyObject *c_normalize; /* Normalization function from unicodedata. */
498 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499};
500
501static asdl_seq *seq_for_testlist(struct compiling *, const node *);
502static expr_ty ast_for_expr(struct compiling *, const node *);
503static stmt_ty ast_for_stmt(struct compiling *, const node *);
504static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000505static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
506 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000507static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000508static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509
510/* Note different signature for ast_for_call */
511static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
512
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000513static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000514static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +0000515static PyObject *parsestrplus(struct compiling *, const node *n,
516 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517
Nick Coghlan650f0d02007-04-15 12:05:43 +0000518#define COMP_GENEXP 0
519#define COMP_LISTCOMP 1
520#define COMP_SETCOMP 2
521
Benjamin Peterson55e00432012-01-16 17:22:31 -0500522static int
523init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000524{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500525 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
526 if (!m)
527 return 0;
528 c->c_normalize = PyObject_GetAttrString(m, "normalize");
529 Py_DECREF(m);
530 if (!c->c_normalize)
531 return 0;
532 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500533 if (!c->c_normalize_args) {
534 Py_CLEAR(c->c_normalize);
535 return 0;
536 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200537 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500538 return 1;
539}
540
541static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400542new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500543{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400544 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500545 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000546 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500547 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500548 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000549 /* Check whether there are non-ASCII characters in the
550 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500551 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200552 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500553 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500554 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200555 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500556 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500557 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
558 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500559 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200560 if (!id2)
561 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200562 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000563 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000564 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200565 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
566 Py_DECREF(id);
567 return NULL;
568 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000569 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570}
571
Benjamin Peterson55e00432012-01-16 17:22:31 -0500572#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400575ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400577 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578
Victor Stinner14e461d2013-08-26 22:28:21 +0200579 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000581 Py_INCREF(Py_None);
582 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200584 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400585 if (!tmp)
586 return 0;
587 errstr = PyUnicode_FromString(errmsg);
588 if (!errstr) {
589 Py_DECREF(tmp);
590 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000591 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000592 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 Py_DECREF(errstr);
594 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400595 if (value) {
596 PyErr_SetObject(PyExc_SyntaxError, value);
597 Py_DECREF(value);
598 }
599 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600}
601
602/* num_stmts() returns number of contained statements.
603
604 Use this routine to determine how big a sequence is needed for
605 the statements in a parse tree. Its raison d'etre is this bit of
606 grammar:
607
608 stmt: simple_stmt | compound_stmt
609 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
610
611 A simple_stmt can contain multiple small_stmt elements joined
612 by semicolons. If the arg is a simple_stmt, the number of
613 small_stmt elements is returned.
614*/
615
616static int
617num_stmts(const node *n)
618{
619 int i, l;
620 node *ch;
621
622 switch (TYPE(n)) {
623 case single_input:
624 if (TYPE(CHILD(n, 0)) == NEWLINE)
625 return 0;
626 else
627 return num_stmts(CHILD(n, 0));
628 case file_input:
629 l = 0;
630 for (i = 0; i < NCH(n); i++) {
631 ch = CHILD(n, i);
632 if (TYPE(ch) == stmt)
633 l += num_stmts(ch);
634 }
635 return l;
636 case stmt:
637 return num_stmts(CHILD(n, 0));
638 case compound_stmt:
639 return 1;
640 case simple_stmt:
641 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
642 case suite:
643 if (NCH(n) == 1)
644 return num_stmts(CHILD(n, 0));
645 else {
646 l = 0;
647 for (i = 2; i < (NCH(n) - 1); i++)
648 l += num_stmts(CHILD(n, i));
649 return l;
650 }
651 default: {
652 char buf[128];
653
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000654 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 TYPE(n), NCH(n));
656 Py_FatalError(buf);
657 }
658 }
659 assert(0);
660 return 0;
661}
662
663/* Transform the CST rooted at node * to the appropriate AST
664*/
665
666mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200667PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
668 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000670 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 asdl_seq *stmts = NULL;
672 stmt_ty s;
673 node *ch;
674 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500675 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400677 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200678 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 c.c_filename = filename;
680 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000682 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000684#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400685 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500686 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000687#endif
688 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 } else if (TYPE(n) == encoding_decl) {
691 c.c_encoding = STR(n);
692 n = CHILD(n, 0);
693 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000695 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 }
697
Jeremy Hyltona8293132006-02-28 17:58:27 +0000698 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 switch (TYPE(n)) {
700 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200701 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500703 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 for (i = 0; i < NCH(n) - 1; i++) {
705 ch = CHILD(n, i);
706 if (TYPE(ch) == NEWLINE)
707 continue;
708 REQ(ch, stmt);
709 num = num_stmts(ch);
710 if (num == 1) {
711 s = ast_for_stmt(&c, ch);
712 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500713 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000714 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 }
716 else {
717 ch = CHILD(ch, 0);
718 REQ(ch, simple_stmt);
719 for (j = 0; j < num; j++) {
720 s = ast_for_stmt(&c, CHILD(ch, j * 2));
721 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500722 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000723 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 }
725 }
726 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500727 res = Module(stmts, arena);
728 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 case eval_input: {
730 expr_ty testlist_ast;
731
Nick Coghlan650f0d02007-04-15 12:05:43 +0000732 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000733 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500735 goto out;
736 res = Expression(testlist_ast, arena);
737 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 }
739 case single_input:
740 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200741 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500743 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
745 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000746 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500747 goto out;
748 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 }
750 else {
751 n = CHILD(n, 0);
752 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200753 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500755 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000757 s = ast_for_stmt(&c, n);
758 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500759 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 asdl_seq_SET(stmts, 0, s);
761 }
762 else {
763 /* Only a simple_stmt can contain multiple statements. */
764 REQ(n, simple_stmt);
765 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 if (TYPE(CHILD(n, i)) == NEWLINE)
767 break;
768 s = ast_for_stmt(&c, CHILD(n, i));
769 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500770 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 asdl_seq_SET(stmts, i / 2, s);
772 }
773 }
774
Benjamin Peterson55e00432012-01-16 17:22:31 -0500775 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500777 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000779 PyErr_Format(PyExc_SystemError,
780 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 out:
784 if (c.c_normalize) {
785 Py_DECREF(c.c_normalize);
786 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
787 Py_DECREF(c.c_normalize_args);
788 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500789 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790}
791
Victor Stinner14e461d2013-08-26 22:28:21 +0200792mod_ty
793PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
794 PyArena *arena)
795{
796 mod_ty mod;
797 PyObject *filename;
798 filename = PyUnicode_DecodeFSDefault(filename_str);
799 if (filename == NULL)
800 return NULL;
801 mod = PyAST_FromNodeObject(n, flags, filename, arena);
802 Py_DECREF(filename);
803 return mod;
804
805}
806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
808*/
809
810static operator_ty
811get_operator(const node *n)
812{
813 switch (TYPE(n)) {
814 case VBAR:
815 return BitOr;
816 case CIRCUMFLEX:
817 return BitXor;
818 case AMPER:
819 return BitAnd;
820 case LEFTSHIFT:
821 return LShift;
822 case RIGHTSHIFT:
823 return RShift;
824 case PLUS:
825 return Add;
826 case MINUS:
827 return Sub;
828 case STAR:
829 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400830 case AT:
831 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 case SLASH:
833 return Div;
834 case DOUBLESLASH:
835 return FloorDiv;
836 case PERCENT:
837 return Mod;
838 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 }
841}
842
Guido van Rossume7ba4952007-06-06 23:52:48 +0000843static const char* FORBIDDEN[] = {
844 "None",
845 "True",
846 "False",
847 NULL,
848};
849
850static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400851forbidden_name(struct compiling *c, identifier name, const node *n, int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000852{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000853 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000854 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400855 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000856 return 1;
857 }
858 if (full_checks) {
859 const char **p;
860 for (p = FORBIDDEN; *p; p++) {
861 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400862 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000863 return 1;
864 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000865 }
866 }
867 return 0;
868}
869
Jeremy Hyltona8293132006-02-28 17:58:27 +0000870/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871
872 Only sets context for expr kinds that "can appear in assignment context"
873 (according to ../Parser/Python.asdl). For other expr kinds, it sets
874 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875*/
876
877static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000878set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879{
880 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000881 /* If a particular expression type can't be used for assign / delete,
882 set expr_name to its name and an error message will be generated.
883 */
884 const char* expr_name = NULL;
885
886 /* The ast defines augmented store and load contexts, but the
887 implementation here doesn't actually use them. The code may be
888 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000889 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000890 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000891 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000892 */
893 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
895 switch (e->kind) {
896 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000897 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400898 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000899 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000900 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000902 e->v.Subscript.ctx = ctx;
903 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000904 case Starred_kind:
905 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000906 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000907 return 0;
908 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000910 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500911 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000912 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000913 }
914 e->v.Name.ctx = ctx;
915 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000917 e->v.List.ctx = ctx;
918 s = e->v.List.elts;
919 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000921 if (asdl_seq_LEN(e->v.Tuple.elts)) {
922 e->v.Tuple.ctx = ctx;
923 s = e->v.Tuple.elts;
924 }
925 else {
926 expr_name = "()";
927 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000928 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000929 case Lambda_kind:
930 expr_name = "lambda";
931 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000933 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000934 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000935 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000937 case UnaryOp_kind:
938 expr_name = "operator";
939 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000941 expr_name = "generator expression";
942 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000943 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500944 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000945 expr_name = "yield expression";
946 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000947 case ListComp_kind:
948 expr_name = "list comprehension";
949 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000950 case SetComp_kind:
951 expr_name = "set comprehension";
952 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000953 case DictComp_kind:
954 expr_name = "dict comprehension";
955 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000956 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000957 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 case Num_kind:
959 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500960 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000961 expr_name = "literal";
962 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500963 case NameConstant_kind:
964 expr_name = "keyword";
965 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000966 case Ellipsis_kind:
967 expr_name = "Ellipsis";
968 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000969 case Compare_kind:
970 expr_name = "comparison";
971 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972 case IfExp_kind:
973 expr_name = "conditional expression";
974 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 PyErr_Format(PyExc_SystemError,
977 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000978 e->kind, e->lineno);
979 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000981 /* Check for error string set by switch */
982 if (expr_name) {
983 char buf[300];
984 PyOS_snprintf(buf, sizeof(buf),
985 "can't %s %s",
986 ctx == Store ? "assign to" : "delete",
987 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400988 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000989 }
990
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 */
994 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000995 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996
Thomas Wouters89f507f2006-12-13 04:49:30 +0000997 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000998 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000999 return 0;
1000 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 }
1002 return 1;
1003}
1004
1005static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001006ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007{
1008 REQ(n, augassign);
1009 n = CHILD(n, 0);
1010 switch (STR(n)[0]) {
1011 case '+':
1012 return Add;
1013 case '-':
1014 return Sub;
1015 case '/':
1016 if (STR(n)[1] == '/')
1017 return FloorDiv;
1018 else
1019 return Div;
1020 case '%':
1021 return Mod;
1022 case '<':
1023 return LShift;
1024 case '>':
1025 return RShift;
1026 case '&':
1027 return BitAnd;
1028 case '^':
1029 return BitXor;
1030 case '|':
1031 return BitOr;
1032 case '*':
1033 if (STR(n)[1] == '*')
1034 return Pow;
1035 else
1036 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001037 case '@':
1038 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001040 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 }
1043}
1044
1045static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001046ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001048 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 |'is' 'not'
1050 */
1051 REQ(n, comp_op);
1052 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001053 n = CHILD(n, 0);
1054 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 case LESS:
1056 return Lt;
1057 case GREATER:
1058 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001059 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 return Eq;
1061 case LESSEQUAL:
1062 return LtE;
1063 case GREATEREQUAL:
1064 return GtE;
1065 case NOTEQUAL:
1066 return NotEq;
1067 case NAME:
1068 if (strcmp(STR(n), "in") == 0)
1069 return In;
1070 if (strcmp(STR(n), "is") == 0)
1071 return Is;
1072 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001073 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001076 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 }
1078 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001079 /* handle "not in" and "is not" */
1080 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 case NAME:
1082 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1083 return NotIn;
1084 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1085 return IsNot;
1086 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001087 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 }
Neal Norwitz79792652005-11-14 04:25:03 +00001092 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001094 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095}
1096
1097static asdl_seq *
1098seq_for_testlist(struct compiling *c, const node *n)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001101 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1102 */
Armin Rigo31441302005-10-21 12:57:31 +00001103 asdl_seq *seq;
1104 expr_ty expression;
1105 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001106 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001108 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 if (!seq)
1110 return NULL;
1111
1112 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001114 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115
Benjamin Peterson4905e802009-09-27 02:43:28 +00001116 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001117 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119
1120 assert(i / 2 < seq->size);
1121 asdl_seq_SET(seq, i / 2, expression);
1122 }
1123 return seq;
1124}
1125
Neal Norwitzc1505362006-12-28 06:47:50 +00001126static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001127ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001128{
1129 identifier name;
1130 expr_ty annotation = NULL;
1131 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001132 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001133
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001134 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001135 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001136 name = NEW_IDENTIFIER(ch);
1137 if (!name)
1138 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001139 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001140 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001141
1142 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1143 annotation = ast_for_expr(c, CHILD(n, 2));
1144 if (!annotation)
1145 return NULL;
1146 }
1147
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001148 ret = arg(name, annotation, c->c_arena);
1149 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001150 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001151 ret->lineno = LINENO(n);
1152 ret->col_offset = n->n_col_offset;
1153 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154}
1155
Guido van Rossum4f72a782006-10-27 23:31:49 +00001156/* returns -1 if failed to handle keyword only arguments
1157 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001158 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001159 ^^^
1160 start pointing here
1161 */
1162static int
1163handle_keywordonly_args(struct compiling *c, const node *n, int start,
1164 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1165{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001166 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001167 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001168 expr_ty expression, annotation;
1169 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001170 int i = start;
1171 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001172
1173 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001174 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001175 return -1;
1176 }
1177 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001178 while (i < NCH(n)) {
1179 ch = CHILD(n, i);
1180 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001181 case vfpdef:
1182 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001183 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001184 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001185 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001186 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001187 asdl_seq_SET(kwdefaults, j, expression);
1188 i += 2; /* '=' and test */
1189 }
1190 else { /* setting NULL if no default value exists */
1191 asdl_seq_SET(kwdefaults, j, NULL);
1192 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001193 if (NCH(ch) == 3) {
1194 /* ch is NAME ':' test */
1195 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001196 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001197 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001198 }
1199 else {
1200 annotation = NULL;
1201 }
1202 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001203 argname = NEW_IDENTIFIER(ch);
1204 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001205 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001206 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001207 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001208 arg = arg(argname, annotation, c->c_arena);
1209 if (!arg)
1210 goto error;
Benjamin Peterson0714b8b2014-02-13 19:22:14 -05001211 arg->lineno = LINENO(ch);
1212 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001213 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001214 i += 2; /* the name and the comma */
1215 break;
1216 case DOUBLESTAR:
1217 return i;
1218 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001219 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001220 goto error;
1221 }
1222 }
1223 return i;
1224 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001226}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227
Jeremy Hyltona8293132006-02-28 17:58:27 +00001228/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229
1230static arguments_ty
1231ast_for_arguments(struct compiling *c, const node *n)
1232{
Neal Norwitzc1505362006-12-28 06:47:50 +00001233 /* This function handles both typedargslist (function definition)
1234 and varargslist (lambda definition).
1235
1236 parameters: '(' [typedargslist] ')'
1237 typedargslist: ((tfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001239 | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001240 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001241 tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001242 varargslist: ((vfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001244 | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001245 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001246 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001248 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1249 int nposdefaults = 0, found_default = 0;
1250 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001251 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001252 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 node *ch;
1254
1255 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001256 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001257 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001258 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001260 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261
Jeremy Hyltone921e022008-07-17 16:37:17 +00001262 /* First count the number of positional args & defaults. The
1263 variable i is the loop index for this for loop and the next.
1264 The next loop picks up where the first leaves off.
1265 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001267 ch = CHILD(n, i);
1268 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001269 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001270 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001271 if (i < NCH(n) && /* skip argument following star */
1272 (TYPE(CHILD(n, i)) == tfpdef ||
1273 TYPE(CHILD(n, i)) == vfpdef)) {
1274 i++;
1275 }
1276 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001278 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001280 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 defaults for keyword only args */
1284 for ( ; i < NCH(n); ++i) {
1285 ch = CHILD(n, i);
1286 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001287 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001289 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001290 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001291 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001293 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001295 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001297 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001298 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001299 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001301 since we set NULL as default for keyword only argument w/o default
1302 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001303 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001304 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001305 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001306 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001307
1308 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001309 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001310 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001313 /* tfpdef: NAME [':' test]
1314 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 */
1316 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001317 j = 0; /* index for defaults */
1318 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320 ch = CHILD(n, i);
1321 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001322 case tfpdef:
1323 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1325 anything other than EQUAL or a comma? */
1326 /* XXX Should NCH(n) check be made a separate check? */
1327 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001328 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1329 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001330 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 assert(posdefaults != NULL);
1332 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001336 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001337 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001339 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001341 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001342 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001343 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 i += 2; /* the name and the comma */
1346 break;
1347 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 if (i+1 >= NCH(n)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001349 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001350 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001351 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001352 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001353 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001354 if (TYPE(ch) == COMMA) {
1355 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001356 i += 2; /* now follows keyword only arguments */
1357 res = handle_keywordonly_args(c, n, i,
1358 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001359 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 i = res; /* res has new position to process */
1361 }
1362 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001363 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001364 if (!vararg)
1365 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001366
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1369 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 int res = 0;
1371 res = handle_keywordonly_args(c, n, i,
1372 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 i = res; /* res has new position to process */
1375 }
1376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 break;
1378 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379 ch = CHILD(n, i+1); /* tfpdef */
1380 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001381 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001382 if (!kwarg)
1383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 i += 3;
1385 break;
1386 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001387 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 "unexpected node in varargslist: %d @ %d",
1389 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001390 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001393 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394}
1395
1396static expr_ty
1397ast_for_dotted_name(struct compiling *c, const node *n)
1398{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001399 expr_ty e;
1400 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001401 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 int i;
1403
1404 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001405
1406 lineno = LINENO(n);
1407 col_offset = n->n_col_offset;
1408
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 id = NEW_IDENTIFIER(CHILD(n, 0));
1410 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001411 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001412 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001414 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
1416 for (i = 2; i < NCH(n); i+=2) {
1417 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001418 if (!id)
1419 return NULL;
1420 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1421 if (!e)
1422 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 }
1424
1425 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426}
1427
1428static expr_ty
1429ast_for_decorator(struct compiling *c, const node *n)
1430{
1431 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1432 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001433 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001436 REQ(CHILD(n, 0), AT);
1437 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1440 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001441 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001444 d = name_expr;
1445 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 }
1447 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001448 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001449 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001450 if (!d)
1451 return NULL;
1452 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 }
1454 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001455 d = ast_for_call(c, CHILD(n, 3), name_expr);
1456 if (!d)
1457 return NULL;
1458 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 }
1460
1461 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462}
1463
1464static asdl_seq*
1465ast_for_decorators(struct compiling *c, const node *n)
1466{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001467 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001468 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001472 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 if (!decorator_seq)
1474 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001477 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001478 if (!d)
1479 return NULL;
1480 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 }
1482 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483}
1484
1485static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001486ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001488 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001489 identifier name;
1490 arguments_ty args;
1491 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001492 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001493 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
1495 REQ(n, funcdef);
1496
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 name = NEW_IDENTIFIER(CHILD(n, name_i));
1498 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001499 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001500 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1503 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001504 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001505 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1506 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1507 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001508 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001509 name_i += 2;
1510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 body = ast_for_suite(c, CHILD(n, name_i + 3));
1512 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001513 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514
Neal Norwitzc1505362006-12-28 06:47:50 +00001515 return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517}
1518
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001519static stmt_ty
1520ast_for_decorated(struct compiling *c, const node *n)
1521{
1522 /* decorated: decorators (classdef | funcdef) */
1523 stmt_ty thing = NULL;
1524 asdl_seq *decorator_seq = NULL;
1525
1526 REQ(n, decorated);
1527
1528 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1529 if (!decorator_seq)
1530 return NULL;
1531
1532 assert(TYPE(CHILD(n, 1)) == funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001534
1535 if (TYPE(CHILD(n, 1)) == funcdef) {
1536 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1537 } else if (TYPE(CHILD(n, 1)) == classdef) {
1538 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
1539 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001540 /* we count the decorators in when talking about the class' or
1541 * function's line number */
1542 if (thing) {
1543 thing->lineno = LINENO(n);
1544 thing->col_offset = n->n_col_offset;
1545 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001546 return thing;
1547}
1548
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549static expr_ty
1550ast_for_lambdef(struct compiling *c, const node *n)
1551{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001552 /* lambdef: 'lambda' [varargslist] ':' test
1553 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 arguments_ty args;
1555 expr_ty expression;
1556
1557 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001558 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 if (!args)
1560 return NULL;
1561 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 }
1565 else {
1566 args = ast_for_arguments(c, CHILD(n, 1));
1567 if (!args)
1568 return NULL;
1569 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001570 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 }
1573
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001574 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575}
1576
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001577static expr_ty
1578ast_for_ifexpr(struct compiling *c, const node *n)
1579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001581 expr_ty expression, body, orelse;
1582
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001583 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001584 body = ast_for_expr(c, CHILD(n, 0));
1585 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001586 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001587 expression = ast_for_expr(c, CHILD(n, 2));
1588 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001589 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001590 orelse = ast_for_expr(c, CHILD(n, 4));
1591 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001592 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001593 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1594 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001595}
1596
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001598 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599
Nick Coghlan650f0d02007-04-15 12:05:43 +00001600 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601*/
1602
1603static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001604count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001606 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607
Guido van Rossumd8faa362007-04-27 19:54:29 +00001608 count_comp_for:
1609 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001610 REQ(n, comp_for);
1611 if (NCH(n) == 5)
1612 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001613 else
1614 return n_fors;
1615 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001616 REQ(n, comp_iter);
1617 n = CHILD(n, 0);
1618 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001619 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001620 else if (TYPE(n) == comp_if) {
1621 if (NCH(n) == 3) {
1622 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001623 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001624 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001625 else
1626 return n_fors;
1627 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001628
Guido van Rossumd8faa362007-04-27 19:54:29 +00001629 /* Should never be reached */
1630 PyErr_SetString(PyExc_SystemError,
1631 "logic error in count_comp_fors");
1632 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633}
1634
Nick Coghlan650f0d02007-04-15 12:05:43 +00001635/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
Nick Coghlan650f0d02007-04-15 12:05:43 +00001637 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638*/
1639
1640static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001641count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001643 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644
Guido van Rossumd8faa362007-04-27 19:54:29 +00001645 while (1) {
1646 REQ(n, comp_iter);
1647 if (TYPE(CHILD(n, 0)) == comp_for)
1648 return n_ifs;
1649 n = CHILD(n, 0);
1650 REQ(n, comp_if);
1651 n_ifs++;
1652 if (NCH(n) == 2)
1653 return n_ifs;
1654 n = CHILD(n, 2);
1655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656}
1657
Guido van Rossum992d4a32007-07-11 13:09:30 +00001658static asdl_seq *
1659ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001662 asdl_seq *comps;
1663
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001664 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 if (n_fors == -1)
1666 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001667
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001668 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001669 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001671
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001673 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001675 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001676 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677
Guido van Rossum992d4a32007-07-11 13:09:30 +00001678 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679
Guido van Rossum992d4a32007-07-11 13:09:30 +00001680 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001681 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001682 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001684 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001685 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001687
Thomas Wouters89f507f2006-12-13 04:49:30 +00001688 /* Check the # of children rather than the length of t, since
1689 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001690 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001691 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001692 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001694 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1695 c->c_arena),
1696 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001697 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001699
Guido van Rossum992d4a32007-07-11 13:09:30 +00001700 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 int j, n_ifs;
1702 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703
Guido van Rossum992d4a32007-07-11 13:09:30 +00001704 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001705 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001708
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001709 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001710 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001714 REQ(n, comp_iter);
1715 n = CHILD(n, 0);
1716 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717
Guido van Rossum992d4a32007-07-11 13:09:30 +00001718 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001719 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001720 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001721 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001722 if (NCH(n) == 3)
1723 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001725 /* on exit, must guarantee that n is a comp_for */
1726 if (TYPE(n) == comp_iter)
1727 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001728 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001730 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001732 return comps;
1733}
1734
1735static expr_ty
1736ast_for_itercomp(struct compiling *c, const node *n, int type)
1737{
1738 /* testlist_comp: test ( comp_for | (',' test)* [','] )
1739 argument: [test '='] test [comp_for] # Really [keyword '='] test */
1740 expr_ty elt;
1741 asdl_seq *comps;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742
Guido van Rossum992d4a32007-07-11 13:09:30 +00001743 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744
Guido van Rossum992d4a32007-07-11 13:09:30 +00001745 elt = ast_for_expr(c, CHILD(n, 0));
1746 if (!elt)
1747 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748
Guido van Rossum992d4a32007-07-11 13:09:30 +00001749 comps = ast_for_comprehension(c, CHILD(n, 1));
1750 if (!comps)
1751 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001752
1753 if (type == COMP_GENEXP)
1754 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1755 else if (type == COMP_LISTCOMP)
1756 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1757 else if (type == COMP_SETCOMP)
1758 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1759 else
1760 /* Should never happen */
1761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762}
1763
1764static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001765ast_for_dictcomp(struct compiling *c, const node *n)
1766{
1767 expr_ty key, value;
1768 asdl_seq *comps;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769
Guido van Rossum992d4a32007-07-11 13:09:30 +00001770 assert(NCH(n) > 3);
1771 REQ(CHILD(n, 1), COLON);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772
Guido van Rossum992d4a32007-07-11 13:09:30 +00001773 key = ast_for_expr(c, CHILD(n, 0));
1774 if (!key)
1775 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001776 value = ast_for_expr(c, CHILD(n, 2));
1777 if (!value)
1778 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779
Guido van Rossum992d4a32007-07-11 13:09:30 +00001780 comps = ast_for_comprehension(c, CHILD(n, 3));
1781 if (!comps)
1782 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783
Guido van Rossum992d4a32007-07-11 13:09:30 +00001784 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1785}
1786
1787static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001788ast_for_genexp(struct compiling *c, const node *n)
1789{
1790 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001791 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001792}
1793
1794static expr_ty
1795ast_for_listcomp(struct compiling *c, const node *n)
1796{
1797 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001798 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001799}
1800
1801static expr_ty
1802ast_for_setcomp(struct compiling *c, const node *n)
1803{
1804 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001805 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001806}
1807
1808
1809static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810ast_for_atom(struct compiling *c, const node *n)
1811{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001812 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1813 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001814 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 */
1816 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001817 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00001820 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001821 PyObject *name;
1822 const char *s = STR(ch);
1823 size_t len = strlen(s);
1824 if (len >= 4 && len <= 5) {
1825 if (!strcmp(s, "None"))
1826 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
1827 if (!strcmp(s, "True"))
1828 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
1829 if (!strcmp(s, "False"))
1830 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
1831 }
1832 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00001833 if (!name)
1834 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001835 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00001836 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00001839 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001840 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001841 const char *errtype = NULL;
1842 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
1843 errtype = "unicode error";
1844 else if (PyErr_ExceptionMatches(PyExc_ValueError))
1845 errtype = "value error";
1846 if (errtype) {
1847 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001848 PyObject *type, *value, *tback, *errstr;
1849 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00001850 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001851 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001852 char *s = _PyUnicode_AsString(errstr);
1853 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00001854 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001855 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001856 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001857 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02001858 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001859 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02001860 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001861 Py_XDECREF(tback);
1862 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001863 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001864 }
Victor Stinner43d81952013-07-17 00:57:58 +02001865 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
1866 Py_DECREF(str);
1867 return NULL;
1868 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00001869 if (bytesmode)
1870 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
1871 else
1872 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 }
1874 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001875 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001876 if (!pynum)
1877 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001878
Victor Stinner43d81952013-07-17 00:57:58 +02001879 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
1880 Py_DECREF(pynum);
1881 return NULL;
1882 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001883 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 }
Georg Brandldde00282007-03-18 19:01:53 +00001885 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00001886 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001888 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889
Thomas Wouters89f507f2006-12-13 04:49:30 +00001890 if (TYPE(ch) == RPAR)
1891 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892
Thomas Wouters89f507f2006-12-13 04:49:30 +00001893 if (TYPE(ch) == yield_expr)
1894 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00001897 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001898 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00001899
Nick Coghlan650f0d02007-04-15 12:05:43 +00001900 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001902 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903
Thomas Wouters89f507f2006-12-13 04:49:30 +00001904 if (TYPE(ch) == RSQB)
1905 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906
Nick Coghlan650f0d02007-04-15 12:05:43 +00001907 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001908 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1909 asdl_seq *elts = seq_for_testlist(c, ch);
1910 if (!elts)
1911 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001912
Thomas Wouters89f507f2006-12-13 04:49:30 +00001913 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1914 }
1915 else
1916 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 case LBRACE: {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001918 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
1919 * test (gen_for | (',' test)* [',']) */
Neal Norwitzc1505362006-12-28 06:47:50 +00001920 int i, size;
1921 asdl_seq *keys, *values;
1922
1923 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001924 if (TYPE(ch) == RBRACE) {
1925 /* it's an empty dict */
1926 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1927 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1928 /* it's a simple set */
Guido van Rossum4d2adcc2007-04-17 21:58:50 +00001929 asdl_seq *elts;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001930 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001931 elts = _Py_asdl_seq_new(size, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001932 if (!elts)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001933 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001934 for (i = 0; i < NCH(ch); i += 2) {
1935 expr_ty expression;
1936 expression = ast_for_expr(c, CHILD(ch, i));
1937 if (!expression)
1938 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001939 asdl_seq_SET(elts, i / 2, expression);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001940 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001941 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1942 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1943 /* it's a set comprehension */
1944 return ast_for_setcomp(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001945 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
1946 return ast_for_dictcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001947 } else {
1948 /* it's a dict */
1949 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001950 keys = _Py_asdl_seq_new(size, c->c_arena);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001951 if (!keys)
1952 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001954 values = _Py_asdl_seq_new(size, c->c_arena);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001955 if (!values)
1956 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957
Guido van Rossum86e58e22006-08-28 15:27:34 +00001958 for (i = 0; i < NCH(ch); i += 4) {
1959 expr_ty expression;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960
Guido van Rossum86e58e22006-08-28 15:27:34 +00001961 expression = ast_for_expr(c, CHILD(ch, i));
1962 if (!expression)
1963 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001964
Guido van Rossum86e58e22006-08-28 15:27:34 +00001965 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001966
Guido van Rossum86e58e22006-08-28 15:27:34 +00001967 expression = ast_for_expr(c, CHILD(ch, i + 2));
1968 if (!expression)
1969 return NULL;
1970
1971 asdl_seq_SET(values, i / 4, expression);
1972 }
1973 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001977 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 }
1980}
1981
1982static slice_ty
1983ast_for_slice(struct compiling *c, const node *n)
1984{
1985 node *ch;
1986 expr_ty lower = NULL, upper = NULL, step = NULL;
1987
1988 REQ(n, subscript);
1989
1990 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001991 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 sliceop: ':' [test]
1993 */
1994 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 if (NCH(n) == 1 && TYPE(ch) == test) {
1996 /* 'step' variable hold no significance in terms of being used over
1997 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 if (!step)
2000 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001
Thomas Wouters89f507f2006-12-13 04:49:30 +00002002 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 }
2004
2005 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002006 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 if (!lower)
2008 return NULL;
2009 }
2010
2011 /* If there's an upper bound it's in the second or third position. */
2012 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002013 if (NCH(n) > 1) {
2014 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015
Thomas Wouters89f507f2006-12-13 04:49:30 +00002016 if (TYPE(n2) == test) {
2017 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 if (!upper)
2019 return NULL;
2020 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002021 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002023 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024
Thomas Wouters89f507f2006-12-13 04:49:30 +00002025 if (TYPE(n2) == test) {
2026 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027 if (!upper)
2028 return NULL;
2029 }
2030 }
2031
2032 ch = CHILD(n, NCH(n) - 1);
2033 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002034 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002035 ch = CHILD(ch, 1);
2036 if (TYPE(ch) == test) {
2037 step = ast_for_expr(c, ch);
2038 if (!step)
2039 return NULL;
2040 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 }
2042 }
2043
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002044 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045}
2046
2047static expr_ty
2048ast_for_binop(struct compiling *c, const node *n)
2049{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002050 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002052 BinOp(BinOp(A, op, B), op, C).
2053 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054
Guido van Rossumd8faa362007-04-27 19:54:29 +00002055 int i, nops;
2056 expr_ty expr1, expr2, result;
2057 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058
Guido van Rossumd8faa362007-04-27 19:54:29 +00002059 expr1 = ast_for_expr(c, CHILD(n, 0));
2060 if (!expr1)
2061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062
Guido van Rossumd8faa362007-04-27 19:54:29 +00002063 expr2 = ast_for_expr(c, CHILD(n, 2));
2064 if (!expr2)
2065 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066
Guido van Rossumd8faa362007-04-27 19:54:29 +00002067 newoperator = get_operator(CHILD(n, 1));
2068 if (!newoperator)
2069 return NULL;
2070
2071 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2072 c->c_arena);
2073 if (!result)
2074 return NULL;
2075
2076 nops = (NCH(n) - 1) / 2;
2077 for (i = 1; i < nops; i++) {
2078 expr_ty tmp_result, tmp;
2079 const node* next_oper = CHILD(n, i * 2 + 1);
2080
2081 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002082 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 return NULL;
2084
Guido van Rossumd8faa362007-04-27 19:54:29 +00002085 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2086 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 return NULL;
2088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002090 LINENO(next_oper), next_oper->n_col_offset,
2091 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002093 return NULL;
2094 result = tmp_result;
2095 }
2096 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097}
2098
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002099static expr_ty
2100ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002103 subscriptlist: subscript (',' subscript)* [',']
2104 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2105 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002106 REQ(n, trailer);
2107 if (TYPE(CHILD(n, 0)) == LPAR) {
2108 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002109 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
2110 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002111 else
2112 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002113 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002114 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002115 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2116 if (!attr_id)
2117 return NULL;
2118 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002119 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002120 }
2121 else {
2122 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002123 REQ(CHILD(n, 2), RSQB);
2124 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002125 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002126 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2127 if (!slc)
2128 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002129 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2130 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002131 }
2132 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002134 by treating the sequence as a tuple literal if there are
2135 no slice features.
2136 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002137 int j;
2138 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002139 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002140 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002141 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002142 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002143 if (!slices)
2144 return NULL;
2145 for (j = 0; j < NCH(n); j += 2) {
2146 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002147 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002148 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002149 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002150 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002151 asdl_seq_SET(slices, j / 2, slc);
2152 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002153 if (!simple) {
2154 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002155 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002156 }
2157 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002158 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002159 if (!elts)
2160 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002161 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2162 slc = (slice_ty)asdl_seq_GET(slices, j);
2163 assert(slc->kind == Index_kind && slc->v.Index.value);
2164 asdl_seq_SET(elts, j, slc->v.Index.value);
2165 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002166 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002167 if (!e)
2168 return NULL;
2169 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002170 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002171 }
2172 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002173}
2174
2175static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002176ast_for_factor(struct compiling *c, const node *n)
2177{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002178 expr_ty expression;
2179
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002180 expression = ast_for_expr(c, CHILD(n, 1));
2181 if (!expression)
2182 return NULL;
2183
2184 switch (TYPE(CHILD(n, 0))) {
2185 case PLUS:
2186 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2187 c->c_arena);
2188 case MINUS:
2189 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2190 c->c_arena);
2191 case TILDE:
2192 return UnaryOp(Invert, expression, LINENO(n),
2193 n->n_col_offset, c->c_arena);
2194 }
2195 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2196 TYPE(CHILD(n, 0)));
2197 return NULL;
2198}
2199
2200static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002201ast_for_power(struct compiling *c, const node *n)
2202{
2203 /* power: atom trailer* ('**' factor)*
2204 */
2205 int i;
2206 expr_ty e, tmp;
2207 REQ(n, power);
2208 e = ast_for_atom(c, CHILD(n, 0));
2209 if (!e)
2210 return NULL;
2211 if (NCH(n) == 1)
2212 return e;
2213 for (i = 1; i < NCH(n); i++) {
2214 node *ch = CHILD(n, i);
2215 if (TYPE(ch) != trailer)
2216 break;
2217 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002218 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002219 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002220 tmp->lineno = e->lineno;
2221 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002222 e = tmp;
2223 }
2224 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2225 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002226 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002227 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002228 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002229 }
2230 return e;
2231}
2232
Guido van Rossum0368b722007-05-11 16:50:42 +00002233static expr_ty
2234ast_for_starred(struct compiling *c, const node *n)
2235{
2236 expr_ty tmp;
2237 REQ(n, star_expr);
2238
2239 tmp = ast_for_expr(c, CHILD(n, 1));
2240 if (!tmp)
2241 return NULL;
2242
2243 /* The Load context is changed later. */
2244 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2245}
2246
2247
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248/* Do not name a variable 'expr'! Will cause a compile error.
2249*/
2250
2251static expr_ty
2252ast_for_expr(struct compiling *c, const node *n)
2253{
2254 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002255 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002256 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 and_test: not_test ('and' not_test)*
2259 not_test: 'not' not_test | comparison
2260 comparison: expr (comp_op expr)*
2261 expr: xor_expr ('|' xor_expr)*
2262 xor_expr: and_expr ('^' and_expr)*
2263 and_expr: shift_expr ('&' shift_expr)*
2264 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2265 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002266 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 factor: ('+'|'-'|'~') factor | power
2268 power: atom trailer* ('**' factor)*
2269 */
2270
2271 asdl_seq *seq;
2272 int i;
2273
2274 loop:
2275 switch (TYPE(n)) {
2276 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002277 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002278 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002279 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002281 else if (NCH(n) > 1)
2282 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002283 /* Fallthrough */
2284 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 case and_test:
2286 if (NCH(n) == 1) {
2287 n = CHILD(n, 0);
2288 goto loop;
2289 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002290 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 if (!seq)
2292 return NULL;
2293 for (i = 0; i < NCH(n); i += 2) {
2294 expr_ty e = ast_for_expr(c, CHILD(n, i));
2295 if (!e)
2296 return NULL;
2297 asdl_seq_SET(seq, i / 2, e);
2298 }
2299 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002300 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2301 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002302 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002303 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 case not_test:
2305 if (NCH(n) == 1) {
2306 n = CHILD(n, 0);
2307 goto loop;
2308 }
2309 else {
2310 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2311 if (!expression)
2312 return NULL;
2313
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2315 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 }
2317 case comparison:
2318 if (NCH(n) == 1) {
2319 n = CHILD(n, 0);
2320 goto loop;
2321 }
2322 else {
2323 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002324 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002325 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002326 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 if (!ops)
2328 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002329 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 return NULL;
2332 }
2333 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002334 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002336 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002337 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002339 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340
2341 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002342 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002346 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 asdl_seq_SET(cmps, i / 2, expression);
2348 }
2349 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002350 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354 return Compare(expression, ops, cmps, LINENO(n),
2355 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 }
2357 break;
2358
Guido van Rossum0368b722007-05-11 16:50:42 +00002359 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 /* The next five cases all handle BinOps. The main body of code
2362 is the same in each case, but the switch turned inside out to
2363 reuse the code for each type of operator.
2364 */
2365 case expr:
2366 case xor_expr:
2367 case and_expr:
2368 case shift_expr:
2369 case arith_expr:
2370 case term:
2371 if (NCH(n) == 1) {
2372 n = CHILD(n, 0);
2373 goto loop;
2374 }
2375 return ast_for_binop(c, n);
2376 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002377 node *an = NULL;
2378 node *en = NULL;
2379 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002380 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002381 if (NCH(n) > 1)
2382 an = CHILD(n, 1); /* yield_arg */
2383 if (an) {
2384 en = CHILD(an, NCH(an) - 1);
2385 if (NCH(an) == 2) {
2386 is_from = 1;
2387 exp = ast_for_expr(c, en);
2388 }
2389 else
2390 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002391 if (!exp)
2392 return NULL;
2393 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002394 if (is_from)
2395 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2396 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002397 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002398 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 if (NCH(n) == 1) {
2400 n = CHILD(n, 0);
2401 goto loop;
2402 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002403 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002404 case power:
2405 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002407 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 return NULL;
2409 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002410 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 return NULL;
2412}
2413
2414static expr_ty
2415ast_for_call(struct compiling *c, const node *n, expr_ty func)
2416{
2417 /*
2418 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
2419 | '**' test)
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002420 argument: [test '='] (test) [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 */
2422
2423 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002424 asdl_seq *args;
2425 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 expr_ty vararg = NULL, kwarg = NULL;
2427
2428 REQ(n, arglist);
2429
2430 nargs = 0;
2431 nkeywords = 0;
2432 ngens = 0;
2433 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002434 node *ch = CHILD(n, i);
2435 if (TYPE(ch) == argument) {
2436 if (NCH(ch) == 1)
2437 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002438 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002439 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00002441 nkeywords++;
2442 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443 }
2444 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002445 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002446 "if not sole argument");
2447 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 }
2449
2450 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002451 ast_error(c, n, "more than 255 arguments");
2452 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 }
2454
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002455 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002457 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002458 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002460 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 nargs = 0;
2462 nkeywords = 0;
2463 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002464 node *ch = CHILD(n, i);
2465 if (TYPE(ch) == argument) {
2466 expr_ty e;
2467 if (NCH(ch) == 1) {
2468 if (nkeywords) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002469 ast_error(c, CHILD(ch, 0),
Thomas Wouters89f507f2006-12-13 04:49:30 +00002470 "non-keyword arg after keyword arg");
2471 return NULL;
2472 }
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002473 if (vararg) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002474 ast_error(c, CHILD(ch, 0),
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002475 "only named arguments may follow *expression");
2476 return NULL;
2477 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002478 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002480 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002481 asdl_seq_SET(args, nargs++, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002483 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002484 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002486 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002487 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002489 else {
2490 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002491 identifier key, tmp;
2492 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 /* CHILD(ch, 0) is test, but must be an identifier? */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002495 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002497 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 /* f(lambda x: x[0] = 3) ends up getting parsed with
2499 * LHS test = lambda x: x[0], and RHS test = 3.
2500 * SF bug 132313 points out that complaining about a keyword
2501 * then is very confusing.
2502 */
2503 if (e->kind == Lambda_kind) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002504 ast_error(c, CHILD(ch, 0), "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 } else if (e->kind != Name_kind) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002507 ast_error(c, CHILD(ch, 0), "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002508 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002509 } else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002510 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002512 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002513 for (k = 0; k < nkeywords; k++) {
2514 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
2515 if (!PyUnicode_Compare(tmp, key)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002516 ast_error(c, CHILD(ch, 0), "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002517 return NULL;
2518 }
2519 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002520 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002522 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002523 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002525 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002526 asdl_seq_SET(keywords, nkeywords++, kw);
2527 }
2528 }
2529 else if (TYPE(ch) == STAR) {
2530 vararg = ast_for_expr(c, CHILD(n, i+1));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002531 if (!vararg)
2532 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002533 i++;
2534 }
2535 else if (TYPE(ch) == DOUBLESTAR) {
2536 kwarg = ast_for_expr(c, CHILD(n, i+1));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002537 if (!kwarg)
2538 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002539 i++;
2540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 }
2542
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002543 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544}
2545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002547ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002549 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002550 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002552 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002553 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002554 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002555 }
2556 else {
2557 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002558 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002559 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002561 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 else {
2563 asdl_seq *tmp = seq_for_testlist(c, n);
2564 if (!tmp)
2565 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002566 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002568}
2569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570static stmt_ty
2571ast_for_expr_stmt(struct compiling *c, const node *n)
2572{
2573 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002576 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002577 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002578 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 test: ... here starts the operator precendence dance
2580 */
2581
2582 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002583 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 if (!e)
2585 return NULL;
2586
Thomas Wouters89f507f2006-12-13 04:49:30 +00002587 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 }
2589 else if (TYPE(CHILD(n, 1)) == augassign) {
2590 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002591 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002592 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593
Thomas Wouters89f507f2006-12-13 04:49:30 +00002594 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 if (!expr1)
2596 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002597 if(!set_context(c, expr1, Store, ch))
2598 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002599 /* set_context checks that most expressions are not the left side.
2600 Augmented assignments can only have a name, a subscript, or an
2601 attribute on the left, though, so we have to explicitly check for
2602 those. */
2603 switch (expr1->kind) {
2604 case Name_kind:
2605 case Attribute_kind:
2606 case Subscript_kind:
2607 break;
2608 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002609 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002610 return NULL;
2611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
Thomas Wouters89f507f2006-12-13 04:49:30 +00002613 ch = CHILD(n, 2);
2614 if (TYPE(ch) == testlist)
2615 expr2 = ast_for_testlist(c, ch);
2616 else
2617 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002618 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 return NULL;
2620
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002621 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002622 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 return NULL;
2624
Thomas Wouters89f507f2006-12-13 04:49:30 +00002625 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 }
2627 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002628 int i;
2629 asdl_seq *targets;
2630 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 expr_ty expression;
2632
Thomas Wouters89f507f2006-12-13 04:49:30 +00002633 /* a normal assignment */
2634 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002635 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002636 if (!targets)
2637 return NULL;
2638 for (i = 0; i < NCH(n) - 2; i += 2) {
2639 expr_ty e;
2640 node *ch = CHILD(n, i);
2641 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002642 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002643 return NULL;
2644 }
2645 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002649 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002650 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652
Thomas Wouters89f507f2006-12-13 04:49:30 +00002653 asdl_seq_SET(targets, i / 2, e);
2654 }
2655 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002656 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002657 expression = ast_for_testlist(c, value);
2658 else
2659 expression = ast_for_expr(c, value);
2660 if (!expression)
2661 return NULL;
2662 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664}
2665
Benjamin Peterson78565b22009-06-28 19:19:51 +00002666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002668ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669{
2670 asdl_seq *seq;
2671 int i;
2672 expr_ty e;
2673
2674 REQ(n, exprlist);
2675
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002676 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002678 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002680 e = ast_for_expr(c, CHILD(n, i));
2681 if (!e)
2682 return NULL;
2683 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002684 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 }
2687 return seq;
2688}
2689
2690static stmt_ty
2691ast_for_del_stmt(struct compiling *c, const node *n)
2692{
2693 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 /* del_stmt: 'del' exprlist */
2696 REQ(n, del_stmt);
2697
2698 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2699 if (!expr_list)
2700 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002701 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702}
2703
2704static stmt_ty
2705ast_for_flow_stmt(struct compiling *c, const node *n)
2706{
2707 /*
2708 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2709 | yield_stmt
2710 break_stmt: 'break'
2711 continue_stmt: 'continue'
2712 return_stmt: 'return' [testlist]
2713 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002714 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 raise_stmt: 'raise' [test [',' test [',' test]]]
2716 */
2717 node *ch;
2718
2719 REQ(n, flow_stmt);
2720 ch = CHILD(n, 0);
2721 switch (TYPE(ch)) {
2722 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002723 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002725 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2728 if (!exp)
2729 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002730 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 }
2732 case return_stmt:
2733 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002734 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002736 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 if (!expression)
2738 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002739 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 }
2741 case raise_stmt:
2742 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002743 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2744 else if (NCH(ch) >= 2) {
2745 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2747 if (!expression)
2748 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002749 if (NCH(ch) == 4) {
2750 cause = ast_for_expr(c, CHILD(ch, 3));
2751 if (!cause)
2752 return NULL;
2753 }
2754 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 }
2756 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002757 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 "unexpected flow_stmt: %d", TYPE(ch));
2759 return NULL;
2760 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002761
2762 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764}
2765
2766static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00002767alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768{
2769 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002770 import_as_name: NAME ['as' NAME]
2771 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 dotted_name: NAME ('.' NAME)*
2773 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00002774 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002775
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 loop:
2777 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05002778 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002779 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002780 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002781 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002782 if (!name)
2783 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002784 if (NCH(n) == 3) {
2785 node *str_node = CHILD(n, 2);
2786 str = NEW_IDENTIFIER(str_node);
2787 if (!str)
2788 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002789 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002790 return NULL;
2791 }
2792 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002793 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002794 return NULL;
2795 }
Benjamin Peterson30760062008-11-25 04:02:28 +00002796 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 case dotted_as_name:
2799 if (NCH(n) == 1) {
2800 n = CHILD(n, 0);
2801 goto loop;
2802 }
2803 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002804 node *asname_node = CHILD(n, 2);
2805 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002806 if (!a)
2807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002809 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002810 if (!a->asname)
2811 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002812 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 return a;
2815 }
2816 break;
2817 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00002818 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002819 node *name_node = CHILD(n, 0);
2820 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002821 if (!name)
2822 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002823 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002824 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00002825 return alias(name, NULL, c->c_arena);
2826 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 else {
2828 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002829 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002830 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833
2834 len = 0;
2835 for (i = 0; i < NCH(n); i += 2)
2836 /* length of string plus one for the dot */
2837 len += strlen(STR(CHILD(n, i))) + 1;
2838 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00002839 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 if (!str)
2841 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00002842 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 if (!s)
2844 return NULL;
2845 for (i = 0; i < NCH(n); i += 2) {
2846 char *sch = STR(CHILD(n, i));
2847 strcpy(s, STR(CHILD(n, i)));
2848 s += strlen(sch);
2849 *s++ = '.';
2850 }
2851 --s;
2852 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
2854 PyBytes_GET_SIZE(str),
2855 NULL);
2856 Py_DECREF(str);
2857 if (!uni)
2858 return NULL;
2859 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002860 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02002861 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2862 Py_DECREF(str);
2863 return NULL;
2864 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002865 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 }
2867 break;
2868 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00002869 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02002870 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2871 Py_DECREF(str);
2872 return NULL;
2873 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002874 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002876 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 "unexpected import name: %d", TYPE(n));
2878 return NULL;
2879 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002880
2881 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 return NULL;
2883}
2884
2885static stmt_ty
2886ast_for_import_stmt(struct compiling *c, const node *n)
2887{
2888 /*
2889 import_stmt: import_name | import_from
2890 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00002891 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
2892 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002894 int lineno;
2895 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 int i;
2897 asdl_seq *aliases;
2898
2899 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002900 lineno = LINENO(n);
2901 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002903 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002906 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 if (!aliases)
2908 return NULL;
2909 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002910 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002911 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002917 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 int idx, ndots = 0;
2920 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002921 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002923 /* Count the number of dots (for relative imports) and check for the
2924 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002925 for (idx = 1; idx < NCH(n); idx++) {
2926 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002927 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2928 if (!mod)
2929 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002930 idx++;
2931 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00002932 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00002934 ndots += 3;
2935 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002936 } else if (TYPE(CHILD(n, idx)) != DOT) {
2937 break;
2938 }
2939 ndots++;
2940 }
2941 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002942 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002943 case STAR:
2944 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002945 n = CHILD(n, idx);
2946 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002947 break;
2948 case LPAR:
2949 /* from ... import (x, y, z) */
2950 n = CHILD(n, idx + 1);
2951 n_children = NCH(n);
2952 break;
2953 case import_as_names:
2954 /* from ... import x, y, z */
2955 n = CHILD(n, idx);
2956 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002957 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002958 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 " surrounding parentheses");
2960 return NULL;
2961 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002962 break;
2963 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002964 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002965 return NULL;
2966 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002968 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002969 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971
2972 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002973 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002974 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002975 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002977 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002979 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002980 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002981 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002982 if (!import_alias)
2983 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002984 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002985 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002987 if (mod != NULL)
2988 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002989 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002990 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 }
Neal Norwitz79792652005-11-14 04:25:03 +00002992 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 "unknown import statement: starts with command '%s'",
2994 STR(CHILD(n, 0)));
2995 return NULL;
2996}
2997
2998static stmt_ty
2999ast_for_global_stmt(struct compiling *c, const node *n)
3000{
3001 /* global_stmt: 'global' NAME (',' NAME)* */
3002 identifier name;
3003 asdl_seq *s;
3004 int i;
3005
3006 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003007 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003011 name = NEW_IDENTIFIER(CHILD(n, i));
3012 if (!name)
3013 return NULL;
3014 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003016 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017}
3018
3019static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003020ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3021{
3022 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3023 identifier name;
3024 asdl_seq *s;
3025 int i;
3026
3027 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003028 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003029 if (!s)
3030 return NULL;
3031 for (i = 1; i < NCH(n); i += 2) {
3032 name = NEW_IDENTIFIER(CHILD(n, i));
3033 if (!name)
3034 return NULL;
3035 asdl_seq_SET(s, i / 2, name);
3036 }
3037 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3038}
3039
3040static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041ast_for_assert_stmt(struct compiling *c, const node *n)
3042{
3043 /* assert_stmt: 'assert' test [',' test] */
3044 REQ(n, assert_stmt);
3045 if (NCH(n) == 2) {
3046 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3047 if (!expression)
3048 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003049 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 }
3051 else if (NCH(n) == 4) {
3052 expr_ty expr1, expr2;
3053
3054 expr1 = ast_for_expr(c, CHILD(n, 1));
3055 if (!expr1)
3056 return NULL;
3057 expr2 = ast_for_expr(c, CHILD(n, 3));
3058 if (!expr2)
3059 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060
Thomas Wouters89f507f2006-12-13 04:49:30 +00003061 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 }
Neal Norwitz79792652005-11-14 04:25:03 +00003063 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 "improper number of parts to 'assert' statement: %d",
3065 NCH(n));
3066 return NULL;
3067}
3068
3069static asdl_seq *
3070ast_for_suite(struct compiling *c, const node *n)
3071{
3072 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003073 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 stmt_ty s;
3075 int i, total, num, end, pos = 0;
3076 node *ch;
3077
3078 REQ(n, suite);
3079
3080 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003081 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003085 n = CHILD(n, 0);
3086 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003088 */
3089 end = NCH(n) - 1;
3090 if (TYPE(CHILD(n, end - 1)) == SEMI)
3091 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003093 for (i = 0; i < end; i += 2) {
3094 ch = CHILD(n, i);
3095 s = ast_for_stmt(c, ch);
3096 if (!s)
3097 return NULL;
3098 asdl_seq_SET(seq, pos++, s);
3099 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 }
3101 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003102 for (i = 2; i < (NCH(n) - 1); i++) {
3103 ch = CHILD(n, i);
3104 REQ(ch, stmt);
3105 num = num_stmts(ch);
3106 if (num == 1) {
3107 /* small_stmt or compound_stmt with only one child */
3108 s = ast_for_stmt(c, ch);
3109 if (!s)
3110 return NULL;
3111 asdl_seq_SET(seq, pos++, s);
3112 }
3113 else {
3114 int j;
3115 ch = CHILD(ch, 0);
3116 REQ(ch, simple_stmt);
3117 for (j = 0; j < NCH(ch); j += 2) {
3118 /* statement terminates with a semi-colon ';' */
3119 if (NCH(CHILD(ch, j)) == 0) {
3120 assert((j + 1) == NCH(ch));
3121 break;
3122 }
3123 s = ast_for_stmt(c, CHILD(ch, j));
3124 if (!s)
3125 return NULL;
3126 asdl_seq_SET(seq, pos++, s);
3127 }
3128 }
3129 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 }
3131 assert(pos == seq->size);
3132 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133}
3134
3135static stmt_ty
3136ast_for_if_stmt(struct compiling *c, const node *n)
3137{
3138 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3139 ['else' ':' suite]
3140 */
3141 char *s;
3142
3143 REQ(n, if_stmt);
3144
3145 if (NCH(n) == 4) {
3146 expr_ty expression;
3147 asdl_seq *suite_seq;
3148
3149 expression = ast_for_expr(c, CHILD(n, 1));
3150 if (!expression)
3151 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003153 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155
Guido van Rossumd8faa362007-04-27 19:54:29 +00003156 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3157 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 s = STR(CHILD(n, 4));
3161 /* s[2], the third character in the string, will be
3162 's' for el_s_e, or
3163 'i' for el_i_f
3164 */
3165 if (s[2] == 's') {
3166 expr_ty expression;
3167 asdl_seq *seq1, *seq2;
3168
3169 expression = ast_for_expr(c, CHILD(n, 1));
3170 if (!expression)
3171 return NULL;
3172 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003173 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 return NULL;
3175 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003176 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 return NULL;
3178
Guido van Rossumd8faa362007-04-27 19:54:29 +00003179 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3180 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 }
3182 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003183 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003184 expr_ty expression;
3185 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003186 asdl_seq *orelse = NULL;
3187 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188 /* must reference the child n_elif+1 since 'else' token is third,
3189 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003190 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3191 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3192 has_else = 1;
3193 n_elif -= 3;
3194 }
3195 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196
Thomas Wouters89f507f2006-12-13 04:49:30 +00003197 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003198 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003200 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003201 if (!orelse)
3202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003204 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003206 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3207 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003209 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3210 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 asdl_seq_SET(orelse, 0,
3214 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003215 LINENO(CHILD(n, NCH(n) - 6)),
3216 CHILD(n, NCH(n) - 6)->n_col_offset,
3217 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003218 /* the just-created orelse handled the last elif */
3219 n_elif--;
3220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221
Thomas Wouters89f507f2006-12-13 04:49:30 +00003222 for (i = 0; i < n_elif; i++) {
3223 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003224 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003225 if (!newobj)
3226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003228 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003231 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233
Thomas Wouters89f507f2006-12-13 04:49:30 +00003234 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003236 LINENO(CHILD(n, off)),
3237 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003238 orelse = newobj;
3239 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003240 expression = ast_for_expr(c, CHILD(n, 1));
3241 if (!expression)
3242 return NULL;
3243 suite_seq = ast_for_suite(c, CHILD(n, 3));
3244 if (!suite_seq)
3245 return NULL;
3246 return If(expression, suite_seq, orelse,
3247 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003249
3250 PyErr_Format(PyExc_SystemError,
3251 "unexpected token in 'if' statement: %s", s);
3252 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253}
3254
3255static stmt_ty
3256ast_for_while_stmt(struct compiling *c, const node *n)
3257{
3258 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3259 REQ(n, while_stmt);
3260
3261 if (NCH(n) == 4) {
3262 expr_ty expression;
3263 asdl_seq *suite_seq;
3264
3265 expression = ast_for_expr(c, CHILD(n, 1));
3266 if (!expression)
3267 return NULL;
3268 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003269 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003271 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 }
3273 else if (NCH(n) == 7) {
3274 expr_ty expression;
3275 asdl_seq *seq1, *seq2;
3276
3277 expression = ast_for_expr(c, CHILD(n, 1));
3278 if (!expression)
3279 return NULL;
3280 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003281 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 return NULL;
3283 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003284 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 return NULL;
3286
Thomas Wouters89f507f2006-12-13 04:49:30 +00003287 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003289
3290 PyErr_Format(PyExc_SystemError,
3291 "wrong number of tokens for 'while' statement: %d",
3292 NCH(n));
3293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294}
3295
3296static stmt_ty
3297ast_for_for_stmt(struct compiling *c, const node *n)
3298{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003299 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003301 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003302 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3304 REQ(n, for_stmt);
3305
3306 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 if (!seq)
3309 return NULL;
3310 }
3311
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003312 node_target = CHILD(n, 1);
3313 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003314 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003316 /* Check the # of children rather than the length of _target, since
3317 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003318 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003319 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003320 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003322 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003324 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003325 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326 return NULL;
3327 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003328 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329 return NULL;
3330
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003331 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
3332 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333}
3334
3335static excepthandler_ty
3336ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3337{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003338 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339 REQ(exc, except_clause);
3340 REQ(body, suite);
3341
3342 if (NCH(exc) == 1) {
3343 asdl_seq *suite_seq = ast_for_suite(c, body);
3344 if (!suite_seq)
3345 return NULL;
3346
Neal Norwitzad74aa82008-03-31 05:14:30 +00003347 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003348 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 }
3350 else if (NCH(exc) == 2) {
3351 expr_ty expression;
3352 asdl_seq *suite_seq;
3353
3354 expression = ast_for_expr(c, CHILD(exc, 1));
3355 if (!expression)
3356 return NULL;
3357 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003358 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359 return NULL;
3360
Neal Norwitzad74aa82008-03-31 05:14:30 +00003361 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003362 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 }
3364 else if (NCH(exc) == 4) {
3365 asdl_seq *suite_seq;
3366 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003367 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003368 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003370 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003371 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003373 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 return NULL;
3375 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003376 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 return NULL;
3378
Neal Norwitzad74aa82008-03-31 05:14:30 +00003379 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003380 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003382
3383 PyErr_Format(PyExc_SystemError,
3384 "wrong number of children for 'except' clause: %d",
3385 NCH(exc));
3386 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387}
3388
3389static stmt_ty
3390ast_for_try_stmt(struct compiling *c, const node *n)
3391{
Neal Norwitzf599f422005-12-17 21:33:47 +00003392 const int nch = NCH(n);
3393 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003394 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003395
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396 REQ(n, try_stmt);
3397
Neal Norwitzf599f422005-12-17 21:33:47 +00003398 body = ast_for_suite(c, CHILD(n, 2));
3399 if (body == NULL)
3400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401
Neal Norwitzf599f422005-12-17 21:33:47 +00003402 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3403 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3404 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3405 /* we can assume it's an "else",
3406 because nch >= 9 for try-else-finally and
3407 it would otherwise have a type of except_clause */
3408 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3409 if (orelse == NULL)
3410 return NULL;
3411 n_except--;
3412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413
Neal Norwitzf599f422005-12-17 21:33:47 +00003414 finally = ast_for_suite(c, CHILD(n, nch - 1));
3415 if (finally == NULL)
3416 return NULL;
3417 n_except--;
3418 }
3419 else {
3420 /* we can assume it's an "else",
3421 otherwise it would have a type of except_clause */
3422 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3423 if (orelse == NULL)
3424 return NULL;
3425 n_except--;
3426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003428 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003429 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 return NULL;
3431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432
Neal Norwitzf599f422005-12-17 21:33:47 +00003433 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003434 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003435 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003436 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003437 if (handlers == NULL)
3438 return NULL;
3439
3440 for (i = 0; i < n_except; i++) {
3441 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3442 CHILD(n, 5 + i * 3));
3443 if (!e)
3444 return NULL;
3445 asdl_seq_SET(handlers, i, e);
3446 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003447 }
3448
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003449 assert(finally != NULL || asdl_seq_LEN(handlers));
3450 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451}
3452
Georg Brandl0c315622009-05-25 21:10:36 +00003453/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003454static withitem_ty
3455ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003456{
3457 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003458
Georg Brandl0c315622009-05-25 21:10:36 +00003459 REQ(n, with_item);
3460 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003461 if (!context_expr)
3462 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003463 if (NCH(n) == 3) {
3464 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003465
3466 if (!optional_vars) {
3467 return NULL;
3468 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003469 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003470 return NULL;
3471 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003472 }
3473
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003474 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003475}
3476
Georg Brandl0c315622009-05-25 21:10:36 +00003477/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3478static stmt_ty
3479ast_for_with_stmt(struct compiling *c, const node *n)
3480{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003481 int i, n_items;
3482 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003483
3484 REQ(n, with_stmt);
3485
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003486 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003487 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003488 if (!items)
3489 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003490 for (i = 1; i < NCH(n) - 2; i += 2) {
3491 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3492 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003493 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003494 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003495 }
3496
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003497 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3498 if (!body)
3499 return NULL;
3500
3501 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003502}
3503
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003505ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003507 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003508 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003509 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003510 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003511
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 REQ(n, classdef);
3513
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003514 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 s = ast_for_suite(c, CHILD(n, 3));
3516 if (!s)
3517 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003518 classname = NEW_IDENTIFIER(CHILD(n, 1));
3519 if (!classname)
3520 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003521 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003522 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003523 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq,
3524 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003526
3527 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003528 s = ast_for_suite(c, CHILD(n,5));
3529 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003530 return NULL;
3531 classname = NEW_IDENTIFIER(CHILD(n, 1));
3532 if (!classname)
3533 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003534 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003535 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003536 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq,
3537 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 }
3539
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003540 /* class NAME '(' arglist ')' ':' suite */
3541 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003542 {
3543 PyObject *dummy_name;
3544 expr_ty dummy;
3545 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3546 if (!dummy_name)
3547 return NULL;
3548 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3549 call = ast_for_call(c, CHILD(n, 3), dummy);
3550 if (!call)
3551 return NULL;
3552 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003554 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003556 classname = NEW_IDENTIFIER(CHILD(n, 1));
3557 if (!classname)
3558 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003559 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003560 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003561
Benjamin Peterson30760062008-11-25 04:02:28 +00003562 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords,
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003563 call->v.Call.starargs, call->v.Call.kwargs, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003564 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565}
3566
3567static stmt_ty
3568ast_for_stmt(struct compiling *c, const node *n)
3569{
3570 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003571 assert(NCH(n) == 1);
3572 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 }
3574 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003575 assert(num_stmts(n) == 1);
3576 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 }
3578 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003579 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003580 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3581 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003582 */
3583 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 case expr_stmt:
3585 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 case del_stmt:
3587 return ast_for_del_stmt(c, n);
3588 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003589 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 case flow_stmt:
3591 return ast_for_flow_stmt(c, n);
3592 case import_stmt:
3593 return ast_for_import_stmt(c, n);
3594 case global_stmt:
3595 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003596 case nonlocal_stmt:
3597 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 case assert_stmt:
3599 return ast_for_assert_stmt(c, n);
3600 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003601 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3603 TYPE(n), NCH(n));
3604 return NULL;
3605 }
3606 }
3607 else {
3608 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003609 | funcdef | classdef | decorated
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 */
3611 node *ch = CHILD(n, 0);
3612 REQ(n, compound_stmt);
3613 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 case if_stmt:
3615 return ast_for_if_stmt(c, ch);
3616 case while_stmt:
3617 return ast_for_while_stmt(c, ch);
3618 case for_stmt:
3619 return ast_for_for_stmt(c, ch);
3620 case try_stmt:
3621 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003622 case with_stmt:
3623 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003625 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003627 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 case decorated:
3629 return ast_for_decorated(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003631 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3633 TYPE(n), NCH(n));
3634 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 }
3637}
3638
3639static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003640parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003642 const char *end;
3643 long x;
3644 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003645 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003646 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003648 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003649 errno = 0;
3650 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003651 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003652 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003653 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003654 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003655 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003656 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003657 }
3658 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003659 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003660 if (*end == '\0') {
3661 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003662 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003663 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003664 }
3665 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003666 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003667 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003668 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3669 if (compl.imag == -1.0 && PyErr_Occurred())
3670 return NULL;
3671 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003672 }
3673 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003674 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003675 dx = PyOS_string_to_double(s, NULL, NULL);
3676 if (dx == -1.0 && PyErr_Occurred())
3677 return NULL;
3678 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003679 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003680}
3681
3682static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003683decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003685 const char *s, *t;
3686 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003687 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3688 while (s < end && (*s & 0x80)) s++;
3689 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003690 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691}
3692
3693static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003694decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003696 PyObject *v, *u;
3697 char *buf;
3698 char *p;
3699 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003700
Guido van Rossumd8faa362007-04-27 19:54:29 +00003701 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003702 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003703 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003704 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003705 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003706 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003707 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3708 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3709 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003710 if (u == NULL)
3711 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003712 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003713 end = s + len;
3714 while (s < end) {
3715 if (*s == '\\') {
3716 *p++ = *s++;
3717 if (*s & 0x80) {
3718 strcpy(p, "u005c");
3719 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003720 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003721 }
3722 if (*s & 0x80) { /* XXX inefficient */
3723 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003724 int kind;
3725 void *data;
3726 Py_ssize_t len, i;
3727 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003728 if (w == NULL) {
3729 Py_DECREF(u);
3730 return NULL;
3731 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003732 kind = PyUnicode_KIND(w);
3733 data = PyUnicode_DATA(w);
3734 len = PyUnicode_GET_LENGTH(w);
3735 for (i = 0; i < len; i++) {
3736 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3737 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003738 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003739 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003740 /* Should be impossible to overflow */
3741 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003742 Py_DECREF(w);
3743 } else {
3744 *p++ = *s++;
3745 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003746 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003747 len = p - buf;
3748 s = buf;
3749 }
3750 if (rawmode)
3751 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3752 else
3753 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3754 Py_XDECREF(u);
3755 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756}
3757
3758/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003759 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 * parsestr parses it, and returns the decoded Python string object.
3761 */
3762static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00003763parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003765 size_t len;
3766 const char *s = STR(n);
3767 int quote = Py_CHARMASK(*s);
3768 int rawmode = 0;
3769 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01003770 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01003771 while (!*bytesmode || !rawmode) {
3772 if (quote == 'b' || quote == 'B') {
3773 quote = *++s;
3774 *bytesmode = 1;
3775 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00003776 else if (quote == 'u' || quote == 'U') {
3777 quote = *++s;
3778 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01003779 else if (quote == 'r' || quote == 'R') {
3780 quote = *++s;
3781 rawmode = 1;
3782 }
3783 else {
3784 break;
3785 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003786 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003787 }
3788 if (quote != '\'' && quote != '\"') {
3789 PyErr_BadInternalCall();
3790 return NULL;
3791 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003792 s++;
3793 len = strlen(s);
3794 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003796 "string to parse is too long");
3797 return NULL;
3798 }
3799 if (s[--len] != quote) {
3800 PyErr_BadInternalCall();
3801 return NULL;
3802 }
3803 if (len >= 4 && s[0] == quote && s[1] == quote) {
3804 s += 2;
3805 len -= 2;
3806 if (s[--len] != quote || s[--len] != quote) {
3807 PyErr_BadInternalCall();
3808 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003809 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003810 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00003811 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003812 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003813 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003814 if (*bytesmode) {
3815 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003816 const char *ch;
3817 for (ch = s; *ch; ch++) {
3818 if (Py_CHARMASK(*ch) >= 0x80) {
3819 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00003820 "literal characters.");
3821 return NULL;
3822 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00003823 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003824 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00003825 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00003826 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003827 if (rawmode || strchr(s, '\\') == NULL) {
3828 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003829 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00003830 if (u == NULL || !*bytesmode)
3831 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00003832 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003833 Py_DECREF(u);
3834 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003835 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003836 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00003837 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003838 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003840 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003841 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003842 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003843 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00003844 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845}
3846
Guido van Rossum29fd7122007-11-12 01:13:56 +00003847/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 * compile-time literal catenation, calling parsestr() on each piece, and
3849 * pasting the intermediate results together.
3850 */
3851static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00003852parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003854 PyObject *v;
3855 int i;
3856 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00003857 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003858 if (v != NULL) {
3859 /* String literal concatenation */
3860 for (i = 1; i < NCH(n); i++) {
3861 PyObject *s;
3862 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00003863 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003864 if (s == NULL)
3865 goto onError;
3866 if (*bytesmode != subbm) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003867 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02003868 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003869 goto onError;
3870 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003871 if (PyBytes_Check(v) && PyBytes_Check(s)) {
3872 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003873 if (v == NULL)
3874 goto onError;
3875 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003876 else {
3877 PyObject *temp = PyUnicode_Concat(v, s);
3878 Py_DECREF(s);
3879 Py_DECREF(v);
3880 v = temp;
3881 if (v == NULL)
3882 goto onError;
3883 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003884 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003885 }
3886 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887
Guido van Rossumd8faa362007-04-27 19:54:29 +00003888 onError:
3889 Py_XDECREF(v);
3890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891}