blob: d50cb80f24583a490c83b7d578f8e838a42bd235 [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;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700118 if (args->kwarg && args->kwarg->annotation
119 && !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;
167 }
168 if (check_ctx && actual_ctx != ctx) {
169 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
170 expr_context_name(ctx), expr_context_name(actual_ctx));
171 return 0;
172 }
173
174 /* Now validate expression. */
175 switch (exp->kind) {
176 case BoolOp_kind:
177 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
178 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
179 return 0;
180 }
181 return validate_exprs(exp->v.BoolOp.values, Load, 0);
182 case BinOp_kind:
183 return validate_expr(exp->v.BinOp.left, Load) &&
184 validate_expr(exp->v.BinOp.right, Load);
185 case UnaryOp_kind:
186 return validate_expr(exp->v.UnaryOp.operand, Load);
187 case Lambda_kind:
188 return validate_arguments(exp->v.Lambda.args) &&
189 validate_expr(exp->v.Lambda.body, Load);
190 case IfExp_kind:
191 return validate_expr(exp->v.IfExp.test, Load) &&
192 validate_expr(exp->v.IfExp.body, Load) &&
193 validate_expr(exp->v.IfExp.orelse, Load);
194 case Dict_kind:
195 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
196 PyErr_SetString(PyExc_ValueError,
197 "Dict doesn't have the same number of keys as values");
198 return 0;
199 }
200 return validate_exprs(exp->v.Dict.keys, Load, 0) &&
201 validate_exprs(exp->v.Dict.values, Load, 0);
202 case Set_kind:
203 return validate_exprs(exp->v.Set.elts, Load, 0);
204#define COMP(NAME) \
205 case NAME ## _kind: \
206 return validate_comprehension(exp->v.NAME.generators) && \
207 validate_expr(exp->v.NAME.elt, Load);
208 COMP(ListComp)
209 COMP(SetComp)
210 COMP(GeneratorExp)
211#undef COMP
212 case DictComp_kind:
213 return validate_comprehension(exp->v.DictComp.generators) &&
214 validate_expr(exp->v.DictComp.key, Load) &&
215 validate_expr(exp->v.DictComp.value, Load);
216 case Yield_kind:
217 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500218 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000219 return validate_expr(exp->v.YieldFrom.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500220 case Compare_kind:
221 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
222 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
223 return 0;
224 }
225 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
226 asdl_seq_LEN(exp->v.Compare.ops)) {
227 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
228 "of comparators and operands");
229 return 0;
230 }
231 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
232 validate_expr(exp->v.Compare.left, Load);
233 case Call_kind:
234 return validate_expr(exp->v.Call.func, Load) &&
235 validate_exprs(exp->v.Call.args, Load, 0) &&
236 validate_keywords(exp->v.Call.keywords) &&
237 (!exp->v.Call.starargs || validate_expr(exp->v.Call.starargs, Load)) &&
238 (!exp->v.Call.kwargs || validate_expr(exp->v.Call.kwargs, Load));
239 case Num_kind: {
240 PyObject *n = exp->v.Num.n;
241 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
242 !PyComplex_CheckExact(n)) {
243 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
244 return 0;
245 }
246 return 1;
247 }
248 case Str_kind: {
249 PyObject *s = exp->v.Str.s;
250 if (!PyUnicode_CheckExact(s)) {
251 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
252 return 0;
253 }
254 return 1;
255 }
256 case Bytes_kind: {
257 PyObject *b = exp->v.Bytes.s;
258 if (!PyBytes_CheckExact(b)) {
259 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
260 return 0;
261 }
262 return 1;
263 }
264 case Attribute_kind:
265 return validate_expr(exp->v.Attribute.value, Load);
266 case Subscript_kind:
267 return validate_slice(exp->v.Subscript.slice) &&
268 validate_expr(exp->v.Subscript.value, Load);
269 case Starred_kind:
270 return validate_expr(exp->v.Starred.value, ctx);
271 case List_kind:
272 return validate_exprs(exp->v.List.elts, ctx, 0);
273 case Tuple_kind:
274 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
275 /* These last cases don't have any checking. */
276 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500277 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500278 case Ellipsis_kind:
279 return 1;
280 default:
281 PyErr_SetString(PyExc_SystemError, "unexpected expression");
282 return 0;
283 }
284}
285
286static int
287validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
288{
289 if (asdl_seq_LEN(seq))
290 return 1;
291 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
292 return 0;
293}
294
295static int
296validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
297{
298 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
299 validate_exprs(targets, ctx, 0);
300}
301
302static int
303validate_body(asdl_seq *body, const char *owner)
304{
305 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
306}
307
308static int
309validate_stmt(stmt_ty stmt)
310{
311 int i;
312 switch (stmt->kind) {
313 case FunctionDef_kind:
314 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
315 validate_arguments(stmt->v.FunctionDef.args) &&
316 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
317 (!stmt->v.FunctionDef.returns ||
318 validate_expr(stmt->v.FunctionDef.returns, Load));
319 case ClassDef_kind:
320 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
321 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
322 validate_keywords(stmt->v.ClassDef.keywords) &&
323 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0) &&
324 (!stmt->v.ClassDef.starargs || validate_expr(stmt->v.ClassDef.starargs, Load)) &&
325 (!stmt->v.ClassDef.kwargs || validate_expr(stmt->v.ClassDef.kwargs, Load));
326 case Return_kind:
327 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
328 case Delete_kind:
329 return validate_assignlist(stmt->v.Delete.targets, Del);
330 case Assign_kind:
331 return validate_assignlist(stmt->v.Assign.targets, Store) &&
332 validate_expr(stmt->v.Assign.value, Load);
333 case AugAssign_kind:
334 return validate_expr(stmt->v.AugAssign.target, Store) &&
335 validate_expr(stmt->v.AugAssign.value, Load);
336 case For_kind:
337 return validate_expr(stmt->v.For.target, Store) &&
338 validate_expr(stmt->v.For.iter, Load) &&
339 validate_body(stmt->v.For.body, "For") &&
340 validate_stmts(stmt->v.For.orelse);
341 case While_kind:
342 return validate_expr(stmt->v.While.test, Load) &&
343 validate_body(stmt->v.While.body, "While") &&
344 validate_stmts(stmt->v.While.orelse);
345 case If_kind:
346 return validate_expr(stmt->v.If.test, Load) &&
347 validate_body(stmt->v.If.body, "If") &&
348 validate_stmts(stmt->v.If.orelse);
349 case With_kind:
350 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
351 return 0;
352 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
353 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
354 if (!validate_expr(item->context_expr, Load) ||
355 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
356 return 0;
357 }
358 return validate_body(stmt->v.With.body, "With");
359 case Raise_kind:
360 if (stmt->v.Raise.exc) {
361 return validate_expr(stmt->v.Raise.exc, Load) &&
362 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
363 }
364 if (stmt->v.Raise.cause) {
365 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
366 return 0;
367 }
368 return 1;
369 case Try_kind:
370 if (!validate_body(stmt->v.Try.body, "Try"))
371 return 0;
372 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
373 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
374 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
375 return 0;
376 }
377 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
378 asdl_seq_LEN(stmt->v.Try.orelse)) {
379 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
380 return 0;
381 }
382 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
383 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
384 if ((handler->v.ExceptHandler.type &&
385 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
386 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
387 return 0;
388 }
389 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
390 validate_stmts(stmt->v.Try.finalbody)) &&
391 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
392 validate_stmts(stmt->v.Try.orelse));
393 case Assert_kind:
394 return validate_expr(stmt->v.Assert.test, Load) &&
395 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
396 case Import_kind:
397 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
398 case ImportFrom_kind:
399 if (stmt->v.ImportFrom.level < -1) {
400 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
401 return 0;
402 }
403 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
404 case Global_kind:
405 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
406 case Nonlocal_kind:
407 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
408 case Expr_kind:
409 return validate_expr(stmt->v.Expr.value, Load);
410 case Pass_kind:
411 case Break_kind:
412 case Continue_kind:
413 return 1;
414 default:
415 PyErr_SetString(PyExc_SystemError, "unexpected statement");
416 return 0;
417 }
418}
419
420static int
421validate_stmts(asdl_seq *seq)
422{
423 int i;
424 for (i = 0; i < asdl_seq_LEN(seq); i++) {
425 stmt_ty stmt = asdl_seq_GET(seq, i);
426 if (stmt) {
427 if (!validate_stmt(stmt))
428 return 0;
429 }
430 else {
431 PyErr_SetString(PyExc_ValueError,
432 "None disallowed in statement list");
433 return 0;
434 }
435 }
436 return 1;
437}
438
439static int
440validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
441{
442 int i;
443 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
444 expr_ty expr = asdl_seq_GET(exprs, i);
445 if (expr) {
446 if (!validate_expr(expr, ctx))
447 return 0;
448 }
449 else if (!null_ok) {
450 PyErr_SetString(PyExc_ValueError,
451 "None disallowed in expression list");
452 return 0;
453 }
454
455 }
456 return 1;
457}
458
459int
460PyAST_Validate(mod_ty mod)
461{
462 int res = 0;
463
464 switch (mod->kind) {
465 case Module_kind:
466 res = validate_stmts(mod->v.Module.body);
467 break;
468 case Interactive_kind:
469 res = validate_stmts(mod->v.Interactive.body);
470 break;
471 case Expression_kind:
472 res = validate_expr(mod->v.Expression.body, Load);
473 break;
474 case Suite_kind:
475 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
476 break;
477 default:
478 PyErr_SetString(PyExc_SystemError, "impossible module node");
479 res = 0;
480 break;
481 }
482 return res;
483}
484
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500485/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500486#include "grammar.h"
487#include "parsetok.h"
488#include "graminit.h"
489
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490/* Data structure used internally */
491struct compiling {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000492 char *c_encoding; /* source encoding */
493 PyArena *c_arena; /* arena for allocating memeory */
Victor Stinner14e461d2013-08-26 22:28:21 +0200494 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500495 PyObject *c_normalize; /* Normalization function from unicodedata. */
496 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497};
498
499static asdl_seq *seq_for_testlist(struct compiling *, const node *);
500static expr_ty ast_for_expr(struct compiling *, const node *);
501static stmt_ty ast_for_stmt(struct compiling *, const node *);
502static asdl_seq *ast_for_suite(struct compiling *, const node *);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000503static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
504 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000505static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000506static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507
508/* Note different signature for ast_for_call */
509static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
510
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000511static PyObject *parsenumber(struct compiling *, const char *);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000512static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode);
Thomas Wouters00e41de2007-02-23 19:56:57 +0000513static PyObject *parsestrplus(struct compiling *, const node *n,
514 int *bytesmode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515
Nick Coghlan650f0d02007-04-15 12:05:43 +0000516#define COMP_GENEXP 0
517#define COMP_LISTCOMP 1
518#define COMP_SETCOMP 2
519
Benjamin Peterson55e00432012-01-16 17:22:31 -0500520static int
521init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000522{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500523 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
524 if (!m)
525 return 0;
526 c->c_normalize = PyObject_GetAttrString(m, "normalize");
527 Py_DECREF(m);
528 if (!c->c_normalize)
529 return 0;
530 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500531 if (!c->c_normalize_args) {
532 Py_CLEAR(c->c_normalize);
533 return 0;
534 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200535 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500536 return 1;
537}
538
539static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400540new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500541{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400542 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500543 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000544 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500545 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500546 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000547 /* Check whether there are non-ASCII characters in the
548 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500549 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200550 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500551 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500552 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200553 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500554 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500555 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
556 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500557 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200558 if (!id2)
559 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200560 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000561 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000562 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200563 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
564 Py_DECREF(id);
565 return NULL;
566 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000567 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568}
569
Benjamin Peterson55e00432012-01-16 17:22:31 -0500570#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400573ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400575 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Victor Stinner14e461d2013-08-26 22:28:21 +0200577 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000579 Py_INCREF(Py_None);
580 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200582 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400583 if (!tmp)
584 return 0;
585 errstr = PyUnicode_FromString(errmsg);
586 if (!errstr) {
587 Py_DECREF(tmp);
588 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000589 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000590 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 Py_DECREF(errstr);
592 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400593 if (value) {
594 PyErr_SetObject(PyExc_SyntaxError, value);
595 Py_DECREF(value);
596 }
597 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598}
599
600/* num_stmts() returns number of contained statements.
601
602 Use this routine to determine how big a sequence is needed for
603 the statements in a parse tree. Its raison d'etre is this bit of
604 grammar:
605
606 stmt: simple_stmt | compound_stmt
607 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
608
609 A simple_stmt can contain multiple small_stmt elements joined
610 by semicolons. If the arg is a simple_stmt, the number of
611 small_stmt elements is returned.
612*/
613
614static int
615num_stmts(const node *n)
616{
617 int i, l;
618 node *ch;
619
620 switch (TYPE(n)) {
621 case single_input:
622 if (TYPE(CHILD(n, 0)) == NEWLINE)
623 return 0;
624 else
625 return num_stmts(CHILD(n, 0));
626 case file_input:
627 l = 0;
628 for (i = 0; i < NCH(n); i++) {
629 ch = CHILD(n, i);
630 if (TYPE(ch) == stmt)
631 l += num_stmts(ch);
632 }
633 return l;
634 case stmt:
635 return num_stmts(CHILD(n, 0));
636 case compound_stmt:
637 return 1;
638 case simple_stmt:
639 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
640 case suite:
641 if (NCH(n) == 1)
642 return num_stmts(CHILD(n, 0));
643 else {
644 l = 0;
645 for (i = 2; i < (NCH(n) - 1); i++)
646 l += num_stmts(CHILD(n, i));
647 return l;
648 }
649 default: {
650 char buf[128];
651
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000652 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 TYPE(n), NCH(n));
654 Py_FatalError(buf);
655 }
656 }
657 assert(0);
658 return 0;
659}
660
661/* Transform the CST rooted at node * to the appropriate AST
662*/
663
664mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200665PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
666 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000668 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 asdl_seq *stmts = NULL;
670 stmt_ty s;
671 node *ch;
672 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500673 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200676 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400677 c.c_filename = filename;
678 c.c_normalize = c.c_normalize_args = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000680 c.c_encoding = "utf-8";
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681 if (TYPE(n) == encoding_decl) {
Guido van Rossum53970392007-06-12 00:28:30 +0000682#if 0
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400683 ast_error(c, n, "encoding declaration in Unicode string");
Benjamin Peterson55e00432012-01-16 17:22:31 -0500684 goto out;
Guido van Rossum53970392007-06-12 00:28:30 +0000685#endif
686 n = CHILD(n, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000687 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 } else if (TYPE(n) == encoding_decl) {
689 c.c_encoding = STR(n);
690 n = CHILD(n, 0);
691 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 /* PEP 3120 */
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000693 c.c_encoding = "utf-8";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 }
695
Jeremy Hyltona8293132006-02-28 17:58:27 +0000696 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 switch (TYPE(n)) {
698 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200699 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500701 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 for (i = 0; i < NCH(n) - 1; i++) {
703 ch = CHILD(n, i);
704 if (TYPE(ch) == NEWLINE)
705 continue;
706 REQ(ch, stmt);
707 num = num_stmts(ch);
708 if (num == 1) {
709 s = ast_for_stmt(&c, ch);
710 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500711 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000712 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 }
714 else {
715 ch = CHILD(ch, 0);
716 REQ(ch, simple_stmt);
717 for (j = 0; j < num; j++) {
718 s = ast_for_stmt(&c, CHILD(ch, j * 2));
719 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500720 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000721 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 }
723 }
724 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500725 res = Module(stmts, arena);
726 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 case eval_input: {
728 expr_ty testlist_ast;
729
Nick Coghlan650f0d02007-04-15 12:05:43 +0000730 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000731 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500733 goto out;
734 res = Expression(testlist_ast, arena);
735 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 }
737 case single_input:
738 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200739 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500741 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
743 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000744 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500745 goto out;
746 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 }
748 else {
749 n = CHILD(n, 0);
750 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200751 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500753 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000755 s = ast_for_stmt(&c, n);
756 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 asdl_seq_SET(stmts, 0, s);
759 }
760 else {
761 /* Only a simple_stmt can contain multiple statements. */
762 REQ(n, simple_stmt);
763 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (TYPE(CHILD(n, i)) == NEWLINE)
765 break;
766 s = ast_for_stmt(&c, CHILD(n, i));
767 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500768 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 asdl_seq_SET(stmts, i / 2, s);
770 }
771 }
772
Benjamin Peterson55e00432012-01-16 17:22:31 -0500773 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500775 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000777 PyErr_Format(PyExc_SystemError,
778 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500779 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 out:
782 if (c.c_normalize) {
783 Py_DECREF(c.c_normalize);
784 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
785 Py_DECREF(c.c_normalize_args);
786 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500787 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788}
789
Victor Stinner14e461d2013-08-26 22:28:21 +0200790mod_ty
791PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
792 PyArena *arena)
793{
794 mod_ty mod;
795 PyObject *filename;
796 filename = PyUnicode_DecodeFSDefault(filename_str);
797 if (filename == NULL)
798 return NULL;
799 mod = PyAST_FromNodeObject(n, flags, filename, arena);
800 Py_DECREF(filename);
801 return mod;
802
803}
804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
806*/
807
808static operator_ty
809get_operator(const node *n)
810{
811 switch (TYPE(n)) {
812 case VBAR:
813 return BitOr;
814 case CIRCUMFLEX:
815 return BitXor;
816 case AMPER:
817 return BitAnd;
818 case LEFTSHIFT:
819 return LShift;
820 case RIGHTSHIFT:
821 return RShift;
822 case PLUS:
823 return Add;
824 case MINUS:
825 return Sub;
826 case STAR:
827 return Mult;
828 case SLASH:
829 return Div;
830 case DOUBLESLASH:
831 return FloorDiv;
832 case PERCENT:
833 return Mod;
834 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 }
837}
838
Guido van Rossume7ba4952007-06-06 23:52:48 +0000839static const char* FORBIDDEN[] = {
840 "None",
841 "True",
842 "False",
843 NULL,
844};
845
846static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400847forbidden_name(struct compiling *c, identifier name, const node *n, int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000848{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000849 assert(PyUnicode_Check(name));
Benjamin Peterson70f52762009-06-28 23:32:44 +0000850 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400851 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000852 return 1;
853 }
854 if (full_checks) {
855 const char **p;
856 for (p = FORBIDDEN; *p; p++) {
857 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400858 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000859 return 1;
860 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000861 }
862 }
863 return 0;
864}
865
Jeremy Hyltona8293132006-02-28 17:58:27 +0000866/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867
868 Only sets context for expr kinds that "can appear in assignment context"
869 (according to ../Parser/Python.asdl). For other expr kinds, it sets
870 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871*/
872
873static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000874set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875{
876 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000877 /* If a particular expression type can't be used for assign / delete,
878 set expr_name to its name and an error message will be generated.
879 */
880 const char* expr_name = NULL;
881
882 /* The ast defines augmented store and load contexts, but the
883 implementation here doesn't actually use them. The code may be
884 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000885 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000886 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000887 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000888 */
889 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890
891 switch (e->kind) {
892 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000893 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400894 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000895 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000896 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000898 e->v.Subscript.ctx = ctx;
899 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000900 case Starred_kind:
901 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000902 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000903 return 0;
904 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000906 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500907 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000908 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000909 }
910 e->v.Name.ctx = ctx;
911 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000913 e->v.List.ctx = ctx;
914 s = e->v.List.elts;
915 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916 case Tuple_kind:
Benjamin Peterson78565b22009-06-28 19:19:51 +0000917 if (asdl_seq_LEN(e->v.Tuple.elts)) {
918 e->v.Tuple.ctx = ctx;
919 s = e->v.Tuple.elts;
920 }
921 else {
922 expr_name = "()";
923 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000924 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000925 case Lambda_kind:
926 expr_name = "lambda";
927 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000929 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000930 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000931 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000933 case UnaryOp_kind:
934 expr_name = "operator";
935 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000937 expr_name = "generator expression";
938 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000939 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -0500940 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000941 expr_name = "yield expression";
942 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000943 case ListComp_kind:
944 expr_name = "list comprehension";
945 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000946 case SetComp_kind:
947 expr_name = "set comprehension";
948 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000949 case DictComp_kind:
950 expr_name = "dict comprehension";
951 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000952 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +0000953 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 case Num_kind:
955 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500956 case Bytes_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000957 expr_name = "literal";
958 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -0500959 case NameConstant_kind:
960 expr_name = "keyword";
961 break;
Neal Norwitzc1505362006-12-28 06:47:50 +0000962 case Ellipsis_kind:
963 expr_name = "Ellipsis";
964 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000965 case Compare_kind:
966 expr_name = "comparison";
967 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968 case IfExp_kind:
969 expr_name = "conditional expression";
970 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000971 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyErr_Format(PyExc_SystemError,
973 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000974 e->kind, e->lineno);
975 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000977 /* Check for error string set by switch */
978 if (expr_name) {
979 char buf[300];
980 PyOS_snprintf(buf, sizeof(buf),
981 "can't %s %s",
982 ctx == Store ? "assign to" : "delete",
983 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400984 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000985 }
986
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 */
990 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000991 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
Thomas Wouters89f507f2006-12-13 04:49:30 +0000993 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000994 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000995 return 0;
996 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 }
998 return 1;
999}
1000
1001static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001002ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003{
1004 REQ(n, augassign);
1005 n = CHILD(n, 0);
1006 switch (STR(n)[0]) {
1007 case '+':
1008 return Add;
1009 case '-':
1010 return Sub;
1011 case '/':
1012 if (STR(n)[1] == '/')
1013 return FloorDiv;
1014 else
1015 return Div;
1016 case '%':
1017 return Mod;
1018 case '<':
1019 return LShift;
1020 case '>':
1021 return RShift;
1022 case '&':
1023 return BitAnd;
1024 case '^':
1025 return BitXor;
1026 case '|':
1027 return BitOr;
1028 case '*':
1029 if (STR(n)[1] == '*')
1030 return Pow;
1031 else
1032 return Mult;
1033 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001034 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001035 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 }
1037}
1038
1039static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001040ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001042 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 |'is' 'not'
1044 */
1045 REQ(n, comp_op);
1046 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001047 n = CHILD(n, 0);
1048 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 case LESS:
1050 return Lt;
1051 case GREATER:
1052 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001053 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054 return Eq;
1055 case LESSEQUAL:
1056 return LtE;
1057 case GREATEREQUAL:
1058 return GtE;
1059 case NOTEQUAL:
1060 return NotEq;
1061 case NAME:
1062 if (strcmp(STR(n), "in") == 0)
1063 return In;
1064 if (strcmp(STR(n), "is") == 0)
1065 return Is;
1066 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001067 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001070 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 }
1072 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001073 /* handle "not in" and "is not" */
1074 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 case NAME:
1076 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1077 return NotIn;
1078 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1079 return IsNot;
1080 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001081 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001084 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 }
Neal Norwitz79792652005-11-14 04:25:03 +00001086 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001088 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
1090
1091static asdl_seq *
1092seq_for_testlist(struct compiling *c, const node *n)
1093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001095 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1096 */
Armin Rigo31441302005-10-21 12:57:31 +00001097 asdl_seq *seq;
1098 expr_ty expression;
1099 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001100 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001102 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 if (!seq)
1104 return NULL;
1105
1106 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001108 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109
Benjamin Peterson4905e802009-09-27 02:43:28 +00001110 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001111 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113
1114 assert(i / 2 < seq->size);
1115 asdl_seq_SET(seq, i / 2, expression);
1116 }
1117 return seq;
1118}
1119
Neal Norwitzc1505362006-12-28 06:47:50 +00001120static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001121ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001122{
1123 identifier name;
1124 expr_ty annotation = NULL;
1125 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001126 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001127
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001128 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001129 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001130 name = NEW_IDENTIFIER(ch);
1131 if (!name)
1132 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001133 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001134 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001135
1136 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1137 annotation = ast_for_expr(c, CHILD(n, 2));
1138 if (!annotation)
1139 return NULL;
1140 }
1141
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001142 ret = arg(name, annotation, c->c_arena);
1143 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001144 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001145 ret->lineno = LINENO(n);
1146 ret->col_offset = n->n_col_offset;
1147 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148}
1149
Guido van Rossum4f72a782006-10-27 23:31:49 +00001150/* returns -1 if failed to handle keyword only arguments
1151 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001152 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001153 ^^^
1154 start pointing here
1155 */
1156static int
1157handle_keywordonly_args(struct compiling *c, const node *n, int start,
1158 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1159{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001160 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001161 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001162 expr_ty expression, annotation;
1163 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001164 int i = start;
1165 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001166
1167 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001168 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001169 return -1;
1170 }
1171 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001172 while (i < NCH(n)) {
1173 ch = CHILD(n, i);
1174 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001175 case vfpdef:
1176 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001177 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001178 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001179 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001180 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001181 asdl_seq_SET(kwdefaults, j, expression);
1182 i += 2; /* '=' and test */
1183 }
1184 else { /* setting NULL if no default value exists */
1185 asdl_seq_SET(kwdefaults, j, NULL);
1186 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001187 if (NCH(ch) == 3) {
1188 /* ch is NAME ':' test */
1189 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001190 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001191 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001192 }
1193 else {
1194 annotation = NULL;
1195 }
1196 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001197 argname = NEW_IDENTIFIER(ch);
1198 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001199 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001200 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001201 goto error;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001202 arg = arg(argname, annotation, c->c_arena);
1203 if (!arg)
1204 goto error;
Benjamin Petersone84fde92014-02-13 19:22:14 -05001205 arg->lineno = LINENO(ch);
1206 arg->col_offset = ch->n_col_offset;
Neal Norwitzc1505362006-12-28 06:47:50 +00001207 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001208 i += 2; /* the name and the comma */
1209 break;
1210 case DOUBLESTAR:
1211 return i;
1212 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001213 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001214 goto error;
1215 }
1216 }
1217 return i;
1218 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001220}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221
Jeremy Hyltona8293132006-02-28 17:58:27 +00001222/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223
1224static arguments_ty
1225ast_for_arguments(struct compiling *c, const node *n)
1226{
Neal Norwitzc1505362006-12-28 06:47:50 +00001227 /* This function handles both typedargslist (function definition)
1228 and varargslist (lambda definition).
1229
1230 parameters: '(' [typedargslist] ')'
1231 typedargslist: ((tfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001233 | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001234 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001235 tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001236 varargslist: ((vfpdef ['=' test] ',')*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001238 | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001239 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001240 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001242 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1243 int nposdefaults = 0, found_default = 0;
1244 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001245 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001246 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 node *ch;
1248
1249 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001250 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001251 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001252 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001254 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255
Jeremy Hyltone921e022008-07-17 16:37:17 +00001256 /* First count the number of positional args & defaults. The
1257 variable i is the loop index for this for loop and the next.
1258 The next loop picks up where the first leaves off.
1259 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001261 ch = CHILD(n, i);
1262 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001263 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001264 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001265 if (i < NCH(n) && /* skip argument following star */
1266 (TYPE(CHILD(n, i)) == tfpdef ||
1267 TYPE(CHILD(n, i)) == vfpdef)) {
1268 i++;
1269 }
1270 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001272 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001273 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001274 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277 defaults for keyword only args */
1278 for ( ; i < NCH(n); ++i) {
1279 ch = CHILD(n, i);
1280 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001281 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001283 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001285 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001287 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001288 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001289 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001291 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001293 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295 since we set NULL as default for keyword only argument w/o default
1296 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001297 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001298 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001299 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001300 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001301
1302 if (nposargs + nkwonlyargs > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001303 ast_error(c, n, "more than 255 arguments");
Neal Norwitzc1505362006-12-28 06:47:50 +00001304 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001305 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001307 /* tfpdef: NAME [':' test]
1308 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 */
1310 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001311 j = 0; /* index for defaults */
1312 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 ch = CHILD(n, i);
1315 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 case tfpdef:
1317 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1319 anything other than EQUAL or a comma? */
1320 /* XXX Should NCH(n) check be made a separate check? */
1321 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001322 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1323 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001324 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 assert(posdefaults != NULL);
1326 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001330 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001331 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001333 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001335 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001337 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 i += 2; /* the name and the comma */
1340 break;
1341 case STAR:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 if (i+1 >= NCH(n)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001343 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001344 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001345 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001347 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001348 if (TYPE(ch) == COMMA) {
1349 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001350 i += 2; /* now follows keyword only arguments */
1351 res = handle_keywordonly_args(c, n, i,
1352 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001353 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 i = res; /* res has new position to process */
1355 }
1356 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001357 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001358 if (!vararg)
1359 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001360
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1363 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 int res = 0;
1365 res = handle_keywordonly_args(c, n, i,
1366 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001367 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001368 i = res; /* res has new position to process */
1369 }
1370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 break;
1372 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001373 ch = CHILD(n, i+1); /* tfpdef */
1374 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001375 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001376 if (!kwarg)
1377 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 i += 3;
1379 break;
1380 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001381 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 "unexpected node in varargslist: %d @ %d",
1383 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001384 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001387 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388}
1389
1390static expr_ty
1391ast_for_dotted_name(struct compiling *c, const node *n)
1392{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001393 expr_ty e;
1394 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001395 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 int i;
1397
1398 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001399
1400 lineno = LINENO(n);
1401 col_offset = n->n_col_offset;
1402
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 id = NEW_IDENTIFIER(CHILD(n, 0));
1404 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001405 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001406 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001408 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409
1410 for (i = 2; i < NCH(n); i+=2) {
1411 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001412 if (!id)
1413 return NULL;
1414 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1415 if (!e)
1416 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 }
1418
1419 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420}
1421
1422static expr_ty
1423ast_for_decorator(struct compiling *c, const node *n)
1424{
1425 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1426 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001427 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001430 REQ(CHILD(n, 0), AT);
1431 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1434 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001435 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001438 d = name_expr;
1439 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 }
1441 else if (NCH(n) == 5) { /* Call with no arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001442 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001443 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001444 if (!d)
1445 return NULL;
1446 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 }
1448 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001449 d = ast_for_call(c, CHILD(n, 3), name_expr);
1450 if (!d)
1451 return NULL;
1452 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 }
1454
1455 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456}
1457
1458static asdl_seq*
1459ast_for_decorators(struct compiling *c, const node *n)
1460{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001461 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001462 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001466 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 if (!decorator_seq)
1468 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001471 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001472 if (!d)
1473 return NULL;
1474 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 }
1476 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477}
1478
1479static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001480ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001482 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001483 identifier name;
1484 arguments_ty args;
1485 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001486 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001487 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488
1489 REQ(n, funcdef);
1490
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 name = NEW_IDENTIFIER(CHILD(n, name_i));
1492 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001493 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001494 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1497 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001498 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001499 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1500 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1501 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001502 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001503 name_i += 2;
1504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 body = ast_for_suite(c, CHILD(n, name_i + 3));
1506 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
Neal Norwitzc1505362006-12-28 06:47:50 +00001509 return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001510 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001513static stmt_ty
1514ast_for_decorated(struct compiling *c, const node *n)
1515{
1516 /* decorated: decorators (classdef | funcdef) */
1517 stmt_ty thing = NULL;
1518 asdl_seq *decorator_seq = NULL;
1519
1520 REQ(n, decorated);
1521
1522 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1523 if (!decorator_seq)
1524 return NULL;
1525
1526 assert(TYPE(CHILD(n, 1)) == funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001528
1529 if (TYPE(CHILD(n, 1)) == funcdef) {
1530 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1531 } else if (TYPE(CHILD(n, 1)) == classdef) {
1532 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
1533 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001534 /* we count the decorators in when talking about the class' or
1535 * function's line number */
1536 if (thing) {
1537 thing->lineno = LINENO(n);
1538 thing->col_offset = n->n_col_offset;
1539 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001540 return thing;
1541}
1542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543static expr_ty
1544ast_for_lambdef(struct compiling *c, const node *n)
1545{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001546 /* lambdef: 'lambda' [varargslist] ':' test
1547 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 arguments_ty args;
1549 expr_ty expression;
1550
1551 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001552 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 if (!args)
1554 return NULL;
1555 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001556 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 }
1559 else {
1560 args = ast_for_arguments(c, CHILD(n, 1));
1561 if (!args)
1562 return NULL;
1563 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001564 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 }
1567
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001568 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569}
1570
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001571static expr_ty
1572ast_for_ifexpr(struct compiling *c, const node *n)
1573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001575 expr_ty expression, body, orelse;
1576
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001577 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001578 body = ast_for_expr(c, CHILD(n, 0));
1579 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001580 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001581 expression = ast_for_expr(c, CHILD(n, 2));
1582 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001583 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001584 orelse = ast_for_expr(c, CHILD(n, 4));
1585 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001586 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001587 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1588 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001589}
1590
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001592 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593
Nick Coghlan650f0d02007-04-15 12:05:43 +00001594 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595*/
1596
1597static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001598count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001600 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601
Guido van Rossumd8faa362007-04-27 19:54:29 +00001602 count_comp_for:
1603 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001604 REQ(n, comp_for);
1605 if (NCH(n) == 5)
1606 n = CHILD(n, 4);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001607 else
1608 return n_fors;
1609 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001610 REQ(n, comp_iter);
1611 n = CHILD(n, 0);
1612 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001613 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001614 else if (TYPE(n) == comp_if) {
1615 if (NCH(n) == 3) {
1616 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001617 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001618 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001619 else
1620 return n_fors;
1621 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001622
Guido van Rossumd8faa362007-04-27 19:54:29 +00001623 /* Should never be reached */
1624 PyErr_SetString(PyExc_SystemError,
1625 "logic error in count_comp_fors");
1626 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627}
1628
Nick Coghlan650f0d02007-04-15 12:05:43 +00001629/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630
Nick Coghlan650f0d02007-04-15 12:05:43 +00001631 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632*/
1633
1634static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001635count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001637 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638
Guido van Rossumd8faa362007-04-27 19:54:29 +00001639 while (1) {
1640 REQ(n, comp_iter);
1641 if (TYPE(CHILD(n, 0)) == comp_for)
1642 return n_ifs;
1643 n = CHILD(n, 0);
1644 REQ(n, comp_if);
1645 n_ifs++;
1646 if (NCH(n) == 2)
1647 return n_ifs;
1648 n = CHILD(n, 2);
1649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
Guido van Rossum992d4a32007-07-11 13:09:30 +00001652static asdl_seq *
1653ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001656 asdl_seq *comps;
1657
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001658 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 if (n_fors == -1)
1660 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001661
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001662 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001663 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001665
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001667 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001669 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001670 node *for_ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671
Guido van Rossum992d4a32007-07-11 13:09:30 +00001672 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673
Guido van Rossum992d4a32007-07-11 13:09:30 +00001674 for_ch = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001675 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001676 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001678 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001679 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001681
Thomas Wouters89f507f2006-12-13 04:49:30 +00001682 /* Check the # of children rather than the length of t, since
1683 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001684 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001685 if (NCH(for_ch) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001686 comp = comprehension(first, expression, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001688 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1689 c->c_arena),
1690 expression, NULL, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001691 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001693
Guido van Rossum992d4a32007-07-11 13:09:30 +00001694 if (NCH(n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 int j, n_ifs;
1696 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697
Guido van Rossum992d4a32007-07-11 13:09:30 +00001698 n = CHILD(n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001699 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001700 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001702
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001703 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001704 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001708 REQ(n, comp_iter);
1709 n = CHILD(n, 0);
1710 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711
Guido van Rossum992d4a32007-07-11 13:09:30 +00001712 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001713 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001714 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001715 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001716 if (NCH(n) == 3)
1717 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001719 /* on exit, must guarantee that n is a comp_for */
1720 if (TYPE(n) == comp_iter)
1721 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001722 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001724 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001726 return comps;
1727}
1728
1729static expr_ty
1730ast_for_itercomp(struct compiling *c, const node *n, int type)
1731{
1732 /* testlist_comp: test ( comp_for | (',' test)* [','] )
1733 argument: [test '='] test [comp_for] # Really [keyword '='] test */
1734 expr_ty elt;
1735 asdl_seq *comps;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736
Guido van Rossum992d4a32007-07-11 13:09:30 +00001737 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738
Guido van Rossum992d4a32007-07-11 13:09:30 +00001739 elt = ast_for_expr(c, CHILD(n, 0));
1740 if (!elt)
1741 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742
Guido van Rossum992d4a32007-07-11 13:09:30 +00001743 comps = ast_for_comprehension(c, CHILD(n, 1));
1744 if (!comps)
1745 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001746
1747 if (type == COMP_GENEXP)
1748 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1749 else if (type == COMP_LISTCOMP)
1750 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1751 else if (type == COMP_SETCOMP)
1752 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1753 else
1754 /* Should never happen */
1755 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756}
1757
1758static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001759ast_for_dictcomp(struct compiling *c, const node *n)
1760{
1761 expr_ty key, value;
1762 asdl_seq *comps;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 assert(NCH(n) > 3);
1765 REQ(CHILD(n, 1), COLON);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766
Guido van Rossum992d4a32007-07-11 13:09:30 +00001767 key = ast_for_expr(c, CHILD(n, 0));
1768 if (!key)
1769 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001770 value = ast_for_expr(c, CHILD(n, 2));
1771 if (!value)
1772 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773
Guido van Rossum992d4a32007-07-11 13:09:30 +00001774 comps = ast_for_comprehension(c, CHILD(n, 3));
1775 if (!comps)
1776 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777
Guido van Rossum992d4a32007-07-11 13:09:30 +00001778 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1779}
1780
1781static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001782ast_for_genexp(struct compiling *c, const node *n)
1783{
1784 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001785 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001786}
1787
1788static expr_ty
1789ast_for_listcomp(struct compiling *c, const node *n)
1790{
1791 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001792 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001793}
1794
1795static expr_ty
1796ast_for_setcomp(struct compiling *c, const node *n)
1797{
1798 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001799 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001800}
1801
1802
1803static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804ast_for_atom(struct compiling *c, const node *n)
1805{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001806 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
1807 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00001808 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 */
1810 node *ch = CHILD(n, 0);
Thomas Wouters00e41de2007-02-23 19:56:57 +00001811 int bytesmode = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00001814 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001815 PyObject *name;
1816 const char *s = STR(ch);
1817 size_t len = strlen(s);
1818 if (len >= 4 && len <= 5) {
1819 if (!strcmp(s, "None"))
1820 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
1821 if (!strcmp(s, "True"))
1822 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
1823 if (!strcmp(s, "False"))
1824 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
1825 }
1826 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00001827 if (!name)
1828 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001829 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00001830 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
1831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 case STRING: {
Thomas Wouters00e41de2007-02-23 19:56:57 +00001833 PyObject *str = parsestrplus(c, n, &bytesmode);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001834 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001835 const char *errtype = NULL;
1836 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
1837 errtype = "unicode error";
1838 else if (PyErr_ExceptionMatches(PyExc_ValueError))
1839 errtype = "value error";
1840 if (errtype) {
1841 char buf[128];
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001842 PyObject *type, *value, *tback, *errstr;
1843 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00001844 errstr = PyObject_Str(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001845 if (errstr) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001846 char *s = _PyUnicode_AsString(errstr);
1847 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00001848 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001849 } else {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001850 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001851 }
Serhiy Storchaka801d9552013-02-10 17:42:01 +02001852 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001853 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02001854 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001855 Py_XDECREF(tback);
1856 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001857 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001858 }
Victor Stinner43d81952013-07-17 00:57:58 +02001859 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
1860 Py_DECREF(str);
1861 return NULL;
1862 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00001863 if (bytesmode)
1864 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
1865 else
1866 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 }
1868 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001869 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001870 if (!pynum)
1871 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872
Victor Stinner43d81952013-07-17 00:57:58 +02001873 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
1874 Py_DECREF(pynum);
1875 return NULL;
1876 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001877 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 }
Georg Brandldde00282007-03-18 19:01:53 +00001879 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00001880 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001882 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883
Thomas Wouters89f507f2006-12-13 04:49:30 +00001884 if (TYPE(ch) == RPAR)
1885 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886
Thomas Wouters89f507f2006-12-13 04:49:30 +00001887 if (TYPE(ch) == yield_expr)
1888 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00001891 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001892 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00001893
Nick Coghlan650f0d02007-04-15 12:05:43 +00001894 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001896 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897
Thomas Wouters89f507f2006-12-13 04:49:30 +00001898 if (TYPE(ch) == RSQB)
1899 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900
Nick Coghlan650f0d02007-04-15 12:05:43 +00001901 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001902 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1903 asdl_seq *elts = seq_for_testlist(c, ch);
1904 if (!elts)
1905 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001906
Thomas Wouters89f507f2006-12-13 04:49:30 +00001907 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1908 }
1909 else
1910 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 case LBRACE: {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001912 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
1913 * test (gen_for | (',' test)* [',']) */
Neal Norwitzc1505362006-12-28 06:47:50 +00001914 int i, size;
1915 asdl_seq *keys, *values;
1916
1917 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001918 if (TYPE(ch) == RBRACE) {
1919 /* it's an empty dict */
1920 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
1921 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1922 /* it's a simple set */
Guido van Rossum4d2adcc2007-04-17 21:58:50 +00001923 asdl_seq *elts;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001924 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001925 elts = _Py_asdl_seq_new(size, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001926 if (!elts)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001927 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001928 for (i = 0; i < NCH(ch); i += 2) {
1929 expr_ty expression;
1930 expression = ast_for_expr(c, CHILD(ch, i));
1931 if (!expression)
1932 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001933 asdl_seq_SET(elts, i / 2, expression);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001934 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001935 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
1936 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
1937 /* it's a set comprehension */
1938 return ast_for_setcomp(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001939 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
1940 return ast_for_dictcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001941 } else {
1942 /* it's a dict */
1943 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001944 keys = _Py_asdl_seq_new(size, c->c_arena);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001945 if (!keys)
1946 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001948 values = _Py_asdl_seq_new(size, c->c_arena);
Guido van Rossum86e58e22006-08-28 15:27:34 +00001949 if (!values)
1950 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951
Guido van Rossum86e58e22006-08-28 15:27:34 +00001952 for (i = 0; i < NCH(ch); i += 4) {
1953 expr_ty expression;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954
Guido van Rossum86e58e22006-08-28 15:27:34 +00001955 expression = ast_for_expr(c, CHILD(ch, i));
1956 if (!expression)
1957 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001958
Guido van Rossum86e58e22006-08-28 15:27:34 +00001959 asdl_seq_SET(keys, i / 4, expression);
Neal Norwitze76adcd2005-11-15 05:04:31 +00001960
Guido van Rossum86e58e22006-08-28 15:27:34 +00001961 expression = ast_for_expr(c, CHILD(ch, i + 2));
1962 if (!expression)
1963 return NULL;
1964
1965 asdl_seq_SET(values, i / 4, expression);
1966 }
1967 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1968 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001971 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 }
1974}
1975
1976static slice_ty
1977ast_for_slice(struct compiling *c, const node *n)
1978{
1979 node *ch;
1980 expr_ty lower = NULL, upper = NULL, step = NULL;
1981
1982 REQ(n, subscript);
1983
1984 /*
Georg Brandl52318d62006-09-06 07:06:08 +00001985 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 sliceop: ':' [test]
1987 */
1988 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 if (NCH(n) == 1 && TYPE(ch) == test) {
1990 /* 'step' variable hold no significance in terms of being used over
1991 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 if (!step)
1994 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995
Thomas Wouters89f507f2006-12-13 04:49:30 +00001996 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 }
1998
1999 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002000 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 if (!lower)
2002 return NULL;
2003 }
2004
2005 /* If there's an upper bound it's in the second or third position. */
2006 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002007 if (NCH(n) > 1) {
2008 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009
Thomas Wouters89f507f2006-12-13 04:49:30 +00002010 if (TYPE(n2) == test) {
2011 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 if (!upper)
2013 return NULL;
2014 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002015 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002017 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018
Thomas Wouters89f507f2006-12-13 04:49:30 +00002019 if (TYPE(n2) == test) {
2020 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 if (!upper)
2022 return NULL;
2023 }
2024 }
2025
2026 ch = CHILD(n, NCH(n) - 1);
2027 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002028 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002029 ch = CHILD(ch, 1);
2030 if (TYPE(ch) == test) {
2031 step = ast_for_expr(c, ch);
2032 if (!step)
2033 return NULL;
2034 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 }
2036 }
2037
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039}
2040
2041static expr_ty
2042ast_for_binop(struct compiling *c, const node *n)
2043{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002044 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002046 BinOp(BinOp(A, op, B), op, C).
2047 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048
Guido van Rossumd8faa362007-04-27 19:54:29 +00002049 int i, nops;
2050 expr_ty expr1, expr2, result;
2051 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052
Guido van Rossumd8faa362007-04-27 19:54:29 +00002053 expr1 = ast_for_expr(c, CHILD(n, 0));
2054 if (!expr1)
2055 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056
Guido van Rossumd8faa362007-04-27 19:54:29 +00002057 expr2 = ast_for_expr(c, CHILD(n, 2));
2058 if (!expr2)
2059 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060
Guido van Rossumd8faa362007-04-27 19:54:29 +00002061 newoperator = get_operator(CHILD(n, 1));
2062 if (!newoperator)
2063 return NULL;
2064
2065 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2066 c->c_arena);
2067 if (!result)
2068 return NULL;
2069
2070 nops = (NCH(n) - 1) / 2;
2071 for (i = 1; i < nops; i++) {
2072 expr_ty tmp_result, tmp;
2073 const node* next_oper = CHILD(n, i * 2 + 1);
2074
2075 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002076 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 return NULL;
2078
Guido van Rossumd8faa362007-04-27 19:54:29 +00002079 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2080 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 return NULL;
2082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002084 LINENO(next_oper), next_oper->n_col_offset,
2085 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002087 return NULL;
2088 result = tmp_result;
2089 }
2090 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091}
2092
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002093static expr_ty
2094ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002097 subscriptlist: subscript (',' subscript)* [',']
2098 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2099 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002100 REQ(n, trailer);
2101 if (TYPE(CHILD(n, 0)) == LPAR) {
2102 if (NCH(n) == 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002103 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
2104 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002105 else
2106 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002107 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002108 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002109 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2110 if (!attr_id)
2111 return NULL;
2112 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002113 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002114 }
2115 else {
2116 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002117 REQ(CHILD(n, 2), RSQB);
2118 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002119 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002120 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2121 if (!slc)
2122 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002123 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2124 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002125 }
2126 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002128 by treating the sequence as a tuple literal if there are
2129 no slice features.
2130 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002131 int j;
2132 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002133 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002134 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002135 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002136 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002137 if (!slices)
2138 return NULL;
2139 for (j = 0; j < NCH(n); j += 2) {
2140 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002141 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002142 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002143 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002144 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002145 asdl_seq_SET(slices, j / 2, slc);
2146 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002147 if (!simple) {
2148 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002149 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002150 }
2151 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002152 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002153 if (!elts)
2154 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002155 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2156 slc = (slice_ty)asdl_seq_GET(slices, j);
2157 assert(slc->kind == Index_kind && slc->v.Index.value);
2158 asdl_seq_SET(elts, j, slc->v.Index.value);
2159 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002160 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002161 if (!e)
2162 return NULL;
2163 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002164 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002165 }
2166 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002167}
2168
2169static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002170ast_for_factor(struct compiling *c, const node *n)
2171{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002172 expr_ty expression;
2173
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002174 expression = ast_for_expr(c, CHILD(n, 1));
2175 if (!expression)
2176 return NULL;
2177
2178 switch (TYPE(CHILD(n, 0))) {
2179 case PLUS:
2180 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2181 c->c_arena);
2182 case MINUS:
2183 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2184 c->c_arena);
2185 case TILDE:
2186 return UnaryOp(Invert, expression, LINENO(n),
2187 n->n_col_offset, c->c_arena);
2188 }
2189 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2190 TYPE(CHILD(n, 0)));
2191 return NULL;
2192}
2193
2194static expr_ty
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002195ast_for_power(struct compiling *c, const node *n)
2196{
2197 /* power: atom trailer* ('**' factor)*
2198 */
2199 int i;
2200 expr_ty e, tmp;
2201 REQ(n, power);
2202 e = ast_for_atom(c, CHILD(n, 0));
2203 if (!e)
2204 return NULL;
2205 if (NCH(n) == 1)
2206 return e;
2207 for (i = 1; i < NCH(n); i++) {
2208 node *ch = CHILD(n, i);
2209 if (TYPE(ch) != trailer)
2210 break;
2211 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002212 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002213 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002214 tmp->lineno = e->lineno;
2215 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002216 e = tmp;
2217 }
2218 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2219 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002220 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002221 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002222 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002223 }
2224 return e;
2225}
2226
Guido van Rossum0368b722007-05-11 16:50:42 +00002227static expr_ty
2228ast_for_starred(struct compiling *c, const node *n)
2229{
2230 expr_ty tmp;
2231 REQ(n, star_expr);
2232
2233 tmp = ast_for_expr(c, CHILD(n, 1));
2234 if (!tmp)
2235 return NULL;
2236
2237 /* The Load context is changed later. */
2238 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2239}
2240
2241
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242/* Do not name a variable 'expr'! Will cause a compile error.
2243*/
2244
2245static expr_ty
2246ast_for_expr(struct compiling *c, const node *n)
2247{
2248 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002249 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002250 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 and_test: not_test ('and' not_test)*
2253 not_test: 'not' not_test | comparison
2254 comparison: expr (comp_op expr)*
2255 expr: xor_expr ('|' xor_expr)*
2256 xor_expr: and_expr ('^' and_expr)*
2257 and_expr: shift_expr ('&' shift_expr)*
2258 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2259 arith_expr: term (('+'|'-') term)*
2260 term: factor (('*'|'/'|'%'|'//') factor)*
2261 factor: ('+'|'-'|'~') factor | power
2262 power: atom trailer* ('**' factor)*
2263 */
2264
2265 asdl_seq *seq;
2266 int i;
2267
2268 loop:
2269 switch (TYPE(n)) {
2270 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002271 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002272 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002273 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002275 else if (NCH(n) > 1)
2276 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002277 /* Fallthrough */
2278 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 case and_test:
2280 if (NCH(n) == 1) {
2281 n = CHILD(n, 0);
2282 goto loop;
2283 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002284 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 if (!seq)
2286 return NULL;
2287 for (i = 0; i < NCH(n); i += 2) {
2288 expr_ty e = ast_for_expr(c, CHILD(n, i));
2289 if (!e)
2290 return NULL;
2291 asdl_seq_SET(seq, i / 2, e);
2292 }
2293 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2295 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002296 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002297 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 case not_test:
2299 if (NCH(n) == 1) {
2300 n = CHILD(n, 0);
2301 goto loop;
2302 }
2303 else {
2304 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2305 if (!expression)
2306 return NULL;
2307
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002308 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2309 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 }
2311 case comparison:
2312 if (NCH(n) == 1) {
2313 n = CHILD(n, 0);
2314 goto loop;
2315 }
2316 else {
2317 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002318 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002319 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002320 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 if (!ops)
2322 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002323 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 return NULL;
2326 }
2327 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002328 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002330 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002331 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334
2335 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002336 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002340 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 asdl_seq_SET(cmps, i / 2, expression);
2342 }
2343 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002344 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002348 return Compare(expression, ops, cmps, LINENO(n),
2349 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 }
2351 break;
2352
Guido van Rossum0368b722007-05-11 16:50:42 +00002353 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 /* The next five cases all handle BinOps. The main body of code
2356 is the same in each case, but the switch turned inside out to
2357 reuse the code for each type of operator.
2358 */
2359 case expr:
2360 case xor_expr:
2361 case and_expr:
2362 case shift_expr:
2363 case arith_expr:
2364 case term:
2365 if (NCH(n) == 1) {
2366 n = CHILD(n, 0);
2367 goto loop;
2368 }
2369 return ast_for_binop(c, n);
2370 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002371 node *an = NULL;
2372 node *en = NULL;
2373 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002374 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002375 if (NCH(n) > 1)
2376 an = CHILD(n, 1); /* yield_arg */
2377 if (an) {
2378 en = CHILD(an, NCH(an) - 1);
2379 if (NCH(an) == 2) {
2380 is_from = 1;
2381 exp = ast_for_expr(c, en);
2382 }
2383 else
2384 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002385 if (!exp)
2386 return NULL;
2387 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002388 if (is_from)
2389 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2390 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002391 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002392 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 if (NCH(n) == 1) {
2394 n = CHILD(n, 0);
2395 goto loop;
2396 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002397 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002398 case power:
2399 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002401 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 return NULL;
2403 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002404 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 return NULL;
2406}
2407
2408static expr_ty
2409ast_for_call(struct compiling *c, const node *n, expr_ty func)
2410{
2411 /*
2412 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
2413 | '**' test)
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002414 argument: [test '='] (test) [comp_for] # Really [keyword '='] test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 */
2416
2417 int i, nargs, nkeywords, ngens;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002418 asdl_seq *args;
2419 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 expr_ty vararg = NULL, kwarg = NULL;
2421
2422 REQ(n, arglist);
2423
2424 nargs = 0;
2425 nkeywords = 0;
2426 ngens = 0;
2427 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002428 node *ch = CHILD(n, i);
2429 if (TYPE(ch) == argument) {
2430 if (NCH(ch) == 1)
2431 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002432 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002433 ngens++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 else
Thomas Wouters89f507f2006-12-13 04:49:30 +00002435 nkeywords++;
2436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 }
2438 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002439 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002440 "if not sole argument");
2441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 }
2443
2444 if (nargs + nkeywords + ngens > 255) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002445 ast_error(c, n, "more than 255 arguments");
2446 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 }
2448
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002449 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002451 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002452 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002454 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 nargs = 0;
2456 nkeywords = 0;
2457 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002458 node *ch = CHILD(n, i);
2459 if (TYPE(ch) == argument) {
2460 expr_ty e;
2461 if (NCH(ch) == 1) {
2462 if (nkeywords) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002463 ast_error(c, CHILD(ch, 0),
Thomas Wouters89f507f2006-12-13 04:49:30 +00002464 "non-keyword arg after keyword arg");
2465 return NULL;
2466 }
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002467 if (vararg) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002468 ast_error(c, CHILD(ch, 0),
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002469 "only named arguments may follow *expression");
2470 return NULL;
2471 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002472 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002474 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002475 asdl_seq_SET(args, nargs++, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002477 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002478 e = ast_for_genexp(c, ch);
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);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002483 else {
2484 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002485 identifier key, tmp;
2486 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 /* CHILD(ch, 0) is test, but must be an identifier? */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002489 e = ast_for_expr(c, CHILD(ch, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002491 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 /* f(lambda x: x[0] = 3) ends up getting parsed with
2493 * LHS test = lambda x: x[0], and RHS test = 3.
2494 * SF bug 132313 points out that complaining about a keyword
2495 * then is very confusing.
2496 */
2497 if (e->kind == Lambda_kind) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002498 ast_error(c, CHILD(ch, 0), "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002499 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 } else if (e->kind != Name_kind) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002501 ast_error(c, CHILD(ch, 0), "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002502 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002503 } else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002504 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002506 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002507 for (k = 0; k < nkeywords; k++) {
2508 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
2509 if (!PyUnicode_Compare(tmp, key)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002510 ast_error(c, CHILD(ch, 0), "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002511 return NULL;
2512 }
2513 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002514 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002516 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002517 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002519 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002520 asdl_seq_SET(keywords, nkeywords++, kw);
2521 }
2522 }
2523 else if (TYPE(ch) == STAR) {
2524 vararg = ast_for_expr(c, CHILD(n, i+1));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002525 if (!vararg)
2526 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002527 i++;
2528 }
2529 else if (TYPE(ch) == DOUBLESTAR) {
2530 kwarg = ast_for_expr(c, CHILD(n, i+1));
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002531 if (!kwarg)
2532 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002533 i++;
2534 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 }
2536
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002537 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538}
2539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002541ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002543 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002544 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002546 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002547 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002548 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002549 }
2550 else {
2551 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002552 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002555 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 else {
2557 asdl_seq *tmp = seq_for_testlist(c, n);
2558 if (!tmp)
2559 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002560 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002562}
2563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564static stmt_ty
2565ast_for_expr_stmt(struct compiling *c, const node *n)
2566{
2567 REQ(n, expr_stmt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 | ('=' (yield_expr|testlist))*)
Benjamin Peterson4905e802009-09-27 02:43:28 +00002570 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002572 | '<<=' | '>>=' | '**=' | '//='
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 test: ... here starts the operator precendence dance
2574 */
2575
2576 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 if (!e)
2579 return NULL;
2580
Thomas Wouters89f507f2006-12-13 04:49:30 +00002581 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 }
2583 else if (TYPE(CHILD(n, 1)) == augassign) {
2584 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002585 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002586 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587
Thomas Wouters89f507f2006-12-13 04:49:30 +00002588 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 if (!expr1)
2590 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002591 if(!set_context(c, expr1, Store, ch))
2592 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002593 /* set_context checks that most expressions are not the left side.
2594 Augmented assignments can only have a name, a subscript, or an
2595 attribute on the left, though, so we have to explicitly check for
2596 those. */
2597 switch (expr1->kind) {
2598 case Name_kind:
2599 case Attribute_kind:
2600 case Subscript_kind:
2601 break;
2602 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002603 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002604 return NULL;
2605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
Thomas Wouters89f507f2006-12-13 04:49:30 +00002607 ch = CHILD(n, 2);
2608 if (TYPE(ch) == testlist)
2609 expr2 = ast_for_testlist(c, ch);
2610 else
2611 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002612 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 return NULL;
2614
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002615 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002616 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 return NULL;
2618
Thomas Wouters89f507f2006-12-13 04:49:30 +00002619 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 }
2621 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002622 int i;
2623 asdl_seq *targets;
2624 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 expr_ty expression;
2626
Thomas Wouters89f507f2006-12-13 04:49:30 +00002627 /* a normal assignment */
2628 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002629 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002630 if (!targets)
2631 return NULL;
2632 for (i = 0; i < NCH(n) - 2; i += 2) {
2633 expr_ty e;
2634 node *ch = CHILD(n, i);
2635 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002636 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 return NULL;
2638 }
2639 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002641 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00002643 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002644 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646
Thomas Wouters89f507f2006-12-13 04:49:30 +00002647 asdl_seq_SET(targets, i / 2, e);
2648 }
2649 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00002650 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002651 expression = ast_for_testlist(c, value);
2652 else
2653 expression = ast_for_expr(c, value);
2654 if (!expression)
2655 return NULL;
2656 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658}
2659
Benjamin Peterson78565b22009-06-28 19:19:51 +00002660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002662ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663{
2664 asdl_seq *seq;
2665 int i;
2666 expr_ty e;
2667
2668 REQ(n, exprlist);
2669
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002670 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002672 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 e = ast_for_expr(c, CHILD(n, i));
2675 if (!e)
2676 return NULL;
2677 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002678 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002679 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 }
2681 return seq;
2682}
2683
2684static stmt_ty
2685ast_for_del_stmt(struct compiling *c, const node *n)
2686{
2687 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 /* del_stmt: 'del' exprlist */
2690 REQ(n, del_stmt);
2691
2692 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2693 if (!expr_list)
2694 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002695 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696}
2697
2698static stmt_ty
2699ast_for_flow_stmt(struct compiling *c, const node *n)
2700{
2701 /*
2702 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2703 | yield_stmt
2704 break_stmt: 'break'
2705 continue_stmt: 'continue'
2706 return_stmt: 'return' [testlist]
2707 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002708 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 raise_stmt: 'raise' [test [',' test [',' test]]]
2710 */
2711 node *ch;
2712
2713 REQ(n, flow_stmt);
2714 ch = CHILD(n, 0);
2715 switch (TYPE(ch)) {
2716 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002717 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002719 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002721 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2722 if (!exp)
2723 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002724 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 }
2726 case return_stmt:
2727 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002728 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002730 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 if (!expression)
2732 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002733 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 }
2735 case raise_stmt:
2736 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00002737 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
2738 else if (NCH(ch) >= 2) {
2739 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2741 if (!expression)
2742 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002743 if (NCH(ch) == 4) {
2744 cause = ast_for_expr(c, CHILD(ch, 3));
2745 if (!cause)
2746 return NULL;
2747 }
2748 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 }
2750 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002751 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 "unexpected flow_stmt: %d", TYPE(ch));
2753 return NULL;
2754 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002755
2756 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758}
2759
2760static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00002761alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762{
2763 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00002764 import_as_name: NAME ['as' NAME]
2765 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 dotted_name: NAME ('.' NAME)*
2767 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00002768 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002769
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 loop:
2771 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05002772 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002773 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002774 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002775 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002776 if (!name)
2777 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002778 if (NCH(n) == 3) {
2779 node *str_node = CHILD(n, 2);
2780 str = NEW_IDENTIFIER(str_node);
2781 if (!str)
2782 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002783 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002784 return NULL;
2785 }
2786 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002787 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002788 return NULL;
2789 }
Benjamin Peterson30760062008-11-25 04:02:28 +00002790 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002791 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 case dotted_as_name:
2793 if (NCH(n) == 1) {
2794 n = CHILD(n, 0);
2795 goto loop;
2796 }
2797 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002798 node *asname_node = CHILD(n, 2);
2799 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002800 if (!a)
2801 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002803 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002804 if (!a->asname)
2805 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002806 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 return a;
2809 }
2810 break;
2811 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00002812 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002813 node *name_node = CHILD(n, 0);
2814 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00002815 if (!name)
2816 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002817 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00002818 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00002819 return alias(name, NULL, c->c_arena);
2820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 else {
2822 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00002823 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00002824 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
2828 len = 0;
2829 for (i = 0; i < NCH(n); i += 2)
2830 /* length of string plus one for the dot */
2831 len += strlen(STR(CHILD(n, i))) + 1;
2832 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00002833 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 if (!str)
2835 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00002836 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 if (!s)
2838 return NULL;
2839 for (i = 0; i < NCH(n); i += 2) {
2840 char *sch = STR(CHILD(n, i));
2841 strcpy(s, STR(CHILD(n, i)));
2842 s += strlen(sch);
2843 *s++ = '.';
2844 }
2845 --s;
2846 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
2848 PyBytes_GET_SIZE(str),
2849 NULL);
2850 Py_DECREF(str);
2851 if (!uni)
2852 return NULL;
2853 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002854 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02002855 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2856 Py_DECREF(str);
2857 return NULL;
2858 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002859 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 }
2861 break;
2862 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00002863 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02002864 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
2865 Py_DECREF(str);
2866 return NULL;
2867 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002868 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002870 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 "unexpected import name: %d", TYPE(n));
2872 return NULL;
2873 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002874
2875 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 return NULL;
2877}
2878
2879static stmt_ty
2880ast_for_import_stmt(struct compiling *c, const node *n)
2881{
2882 /*
2883 import_stmt: import_name | import_from
2884 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00002885 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
2886 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002888 int lineno;
2889 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 int i;
2891 asdl_seq *aliases;
2892
2893 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002894 lineno = LINENO(n);
2895 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00002897 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002899 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002900 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002901 if (!aliases)
2902 return NULL;
2903 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002904 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002905 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 }
Thomas Wouters8622e932006-02-27 17:14:45 +00002911 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002913 int idx, ndots = 0;
2914 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002915 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002917 /* Count the number of dots (for relative imports) and check for the
2918 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 for (idx = 1; idx < NCH(n); idx++) {
2920 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002921 mod = alias_for_import_name(c, CHILD(n, idx), 0);
2922 if (!mod)
2923 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002924 idx++;
2925 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00002926 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00002928 ndots += 3;
2929 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002930 } else if (TYPE(CHILD(n, idx)) != DOT) {
2931 break;
2932 }
2933 ndots++;
2934 }
2935 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002936 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00002937 case STAR:
2938 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002939 n = CHILD(n, idx);
2940 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002941 break;
2942 case LPAR:
2943 /* from ... import (x, y, z) */
2944 n = CHILD(n, idx + 1);
2945 n_children = NCH(n);
2946 break;
2947 case import_as_names:
2948 /* from ... import x, y, z */
2949 n = CHILD(n, idx);
2950 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00002951 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002952 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 " surrounding parentheses");
2954 return NULL;
2955 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002956 break;
2957 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002958 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00002959 return NULL;
2960 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002962 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002963 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965
2966 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00002967 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002968 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002969 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002971 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00002973 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002974 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002975 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002976 if (!import_alias)
2977 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002978 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00002979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002981 if (mod != NULL)
2982 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002983 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002984 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 }
Neal Norwitz79792652005-11-14 04:25:03 +00002986 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 "unknown import statement: starts with command '%s'",
2988 STR(CHILD(n, 0)));
2989 return NULL;
2990}
2991
2992static stmt_ty
2993ast_for_global_stmt(struct compiling *c, const node *n)
2994{
2995 /* global_stmt: 'global' NAME (',' NAME)* */
2996 identifier name;
2997 asdl_seq *s;
2998 int i;
2999
3000 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003001 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003005 name = NEW_IDENTIFIER(CHILD(n, i));
3006 if (!name)
3007 return NULL;
3008 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003010 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011}
3012
3013static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003014ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3015{
3016 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3017 identifier name;
3018 asdl_seq *s;
3019 int i;
3020
3021 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003022 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003023 if (!s)
3024 return NULL;
3025 for (i = 1; i < NCH(n); i += 2) {
3026 name = NEW_IDENTIFIER(CHILD(n, i));
3027 if (!name)
3028 return NULL;
3029 asdl_seq_SET(s, i / 2, name);
3030 }
3031 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3032}
3033
3034static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035ast_for_assert_stmt(struct compiling *c, const node *n)
3036{
3037 /* assert_stmt: 'assert' test [',' test] */
3038 REQ(n, assert_stmt);
3039 if (NCH(n) == 2) {
3040 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3041 if (!expression)
3042 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003043 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 }
3045 else if (NCH(n) == 4) {
3046 expr_ty expr1, expr2;
3047
3048 expr1 = ast_for_expr(c, CHILD(n, 1));
3049 if (!expr1)
3050 return NULL;
3051 expr2 = ast_for_expr(c, CHILD(n, 3));
3052 if (!expr2)
3053 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054
Thomas Wouters89f507f2006-12-13 04:49:30 +00003055 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 }
Neal Norwitz79792652005-11-14 04:25:03 +00003057 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 "improper number of parts to 'assert' statement: %d",
3059 NCH(n));
3060 return NULL;
3061}
3062
3063static asdl_seq *
3064ast_for_suite(struct compiling *c, const node *n)
3065{
3066 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003067 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 stmt_ty s;
3069 int i, total, num, end, pos = 0;
3070 node *ch;
3071
3072 REQ(n, suite);
3073
3074 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003075 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003079 n = CHILD(n, 0);
3080 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003082 */
3083 end = NCH(n) - 1;
3084 if (TYPE(CHILD(n, end - 1)) == SEMI)
3085 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003087 for (i = 0; i < end; i += 2) {
3088 ch = CHILD(n, i);
3089 s = ast_for_stmt(c, ch);
3090 if (!s)
3091 return NULL;
3092 asdl_seq_SET(seq, pos++, s);
3093 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094 }
3095 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003096 for (i = 2; i < (NCH(n) - 1); i++) {
3097 ch = CHILD(n, i);
3098 REQ(ch, stmt);
3099 num = num_stmts(ch);
3100 if (num == 1) {
3101 /* small_stmt or compound_stmt with only one child */
3102 s = ast_for_stmt(c, ch);
3103 if (!s)
3104 return NULL;
3105 asdl_seq_SET(seq, pos++, s);
3106 }
3107 else {
3108 int j;
3109 ch = CHILD(ch, 0);
3110 REQ(ch, simple_stmt);
3111 for (j = 0; j < NCH(ch); j += 2) {
3112 /* statement terminates with a semi-colon ';' */
3113 if (NCH(CHILD(ch, j)) == 0) {
3114 assert((j + 1) == NCH(ch));
3115 break;
3116 }
3117 s = ast_for_stmt(c, CHILD(ch, j));
3118 if (!s)
3119 return NULL;
3120 asdl_seq_SET(seq, pos++, s);
3121 }
3122 }
3123 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 }
3125 assert(pos == seq->size);
3126 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127}
3128
3129static stmt_ty
3130ast_for_if_stmt(struct compiling *c, const node *n)
3131{
3132 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3133 ['else' ':' suite]
3134 */
3135 char *s;
3136
3137 REQ(n, if_stmt);
3138
3139 if (NCH(n) == 4) {
3140 expr_ty expression;
3141 asdl_seq *suite_seq;
3142
3143 expression = ast_for_expr(c, CHILD(n, 1));
3144 if (!expression)
3145 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003147 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149
Guido van Rossumd8faa362007-04-27 19:54:29 +00003150 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3151 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 s = STR(CHILD(n, 4));
3155 /* s[2], the third character in the string, will be
3156 's' for el_s_e, or
3157 'i' for el_i_f
3158 */
3159 if (s[2] == 's') {
3160 expr_ty expression;
3161 asdl_seq *seq1, *seq2;
3162
3163 expression = ast_for_expr(c, CHILD(n, 1));
3164 if (!expression)
3165 return NULL;
3166 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003167 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 return NULL;
3169 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003170 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 return NULL;
3172
Guido van Rossumd8faa362007-04-27 19:54:29 +00003173 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3174 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 }
3176 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003177 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003178 expr_ty expression;
3179 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003180 asdl_seq *orelse = NULL;
3181 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 /* must reference the child n_elif+1 since 'else' token is third,
3183 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003184 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3185 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3186 has_else = 1;
3187 n_elif -= 3;
3188 }
3189 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190
Thomas Wouters89f507f2006-12-13 04:49:30 +00003191 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003192 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003194 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003195 if (!orelse)
3196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003198 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003200 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3201 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003203 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3204 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 asdl_seq_SET(orelse, 0,
3208 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003209 LINENO(CHILD(n, NCH(n) - 6)),
3210 CHILD(n, NCH(n) - 6)->n_col_offset,
3211 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003212 /* the just-created orelse handled the last elif */
3213 n_elif--;
3214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215
Thomas Wouters89f507f2006-12-13 04:49:30 +00003216 for (i = 0; i < n_elif; i++) {
3217 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003218 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003219 if (!newobj)
3220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003222 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003225 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227
Thomas Wouters89f507f2006-12-13 04:49:30 +00003228 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003230 LINENO(CHILD(n, off)),
3231 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003232 orelse = newobj;
3233 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003234 expression = ast_for_expr(c, CHILD(n, 1));
3235 if (!expression)
3236 return NULL;
3237 suite_seq = ast_for_suite(c, CHILD(n, 3));
3238 if (!suite_seq)
3239 return NULL;
3240 return If(expression, suite_seq, orelse,
3241 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003243
3244 PyErr_Format(PyExc_SystemError,
3245 "unexpected token in 'if' statement: %s", s);
3246 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247}
3248
3249static stmt_ty
3250ast_for_while_stmt(struct compiling *c, const node *n)
3251{
3252 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3253 REQ(n, while_stmt);
3254
3255 if (NCH(n) == 4) {
3256 expr_ty expression;
3257 asdl_seq *suite_seq;
3258
3259 expression = ast_for_expr(c, CHILD(n, 1));
3260 if (!expression)
3261 return NULL;
3262 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003263 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003265 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 }
3267 else if (NCH(n) == 7) {
3268 expr_ty expression;
3269 asdl_seq *seq1, *seq2;
3270
3271 expression = ast_for_expr(c, CHILD(n, 1));
3272 if (!expression)
3273 return NULL;
3274 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003275 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276 return NULL;
3277 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003278 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 return NULL;
3280
Thomas Wouters89f507f2006-12-13 04:49:30 +00003281 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003283
3284 PyErr_Format(PyExc_SystemError,
3285 "wrong number of tokens for 'while' statement: %d",
3286 NCH(n));
3287 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288}
3289
3290static stmt_ty
3291ast_for_for_stmt(struct compiling *c, const node *n)
3292{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003293 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003295 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003296 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3298 REQ(n, for_stmt);
3299
3300 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003301 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 if (!seq)
3303 return NULL;
3304 }
3305
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003306 node_target = CHILD(n, 1);
3307 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003308 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003310 /* Check the # of children rather than the length of _target, since
3311 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003312 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003313 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003314 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003316 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003318 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003319 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320 return NULL;
3321 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003322 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323 return NULL;
3324
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003325 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
3326 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327}
3328
3329static excepthandler_ty
3330ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3331{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003332 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333 REQ(exc, except_clause);
3334 REQ(body, suite);
3335
3336 if (NCH(exc) == 1) {
3337 asdl_seq *suite_seq = ast_for_suite(c, body);
3338 if (!suite_seq)
3339 return NULL;
3340
Neal Norwitzad74aa82008-03-31 05:14:30 +00003341 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003342 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343 }
3344 else if (NCH(exc) == 2) {
3345 expr_ty expression;
3346 asdl_seq *suite_seq;
3347
3348 expression = ast_for_expr(c, CHILD(exc, 1));
3349 if (!expression)
3350 return NULL;
3351 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003352 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 return NULL;
3354
Neal Norwitzad74aa82008-03-31 05:14:30 +00003355 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003356 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 }
3358 else if (NCH(exc) == 4) {
3359 asdl_seq *suite_seq;
3360 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003361 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003364 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003365 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003367 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 return NULL;
3369 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003370 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 return NULL;
3372
Neal Norwitzad74aa82008-03-31 05:14:30 +00003373 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003374 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003376
3377 PyErr_Format(PyExc_SystemError,
3378 "wrong number of children for 'except' clause: %d",
3379 NCH(exc));
3380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381}
3382
3383static stmt_ty
3384ast_for_try_stmt(struct compiling *c, const node *n)
3385{
Neal Norwitzf599f422005-12-17 21:33:47 +00003386 const int nch = NCH(n);
3387 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003388 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003389
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 REQ(n, try_stmt);
3391
Neal Norwitzf599f422005-12-17 21:33:47 +00003392 body = ast_for_suite(c, CHILD(n, 2));
3393 if (body == NULL)
3394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395
Neal Norwitzf599f422005-12-17 21:33:47 +00003396 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3397 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3398 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3399 /* we can assume it's an "else",
3400 because nch >= 9 for try-else-finally and
3401 it would otherwise have a type of except_clause */
3402 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3403 if (orelse == NULL)
3404 return NULL;
3405 n_except--;
3406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407
Neal Norwitzf599f422005-12-17 21:33:47 +00003408 finally = ast_for_suite(c, CHILD(n, nch - 1));
3409 if (finally == NULL)
3410 return NULL;
3411 n_except--;
3412 }
3413 else {
3414 /* we can assume it's an "else",
3415 otherwise it would have a type of except_clause */
3416 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3417 if (orelse == NULL)
3418 return NULL;
3419 n_except--;
3420 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003422 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003423 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424 return NULL;
3425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426
Neal Norwitzf599f422005-12-17 21:33:47 +00003427 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003428 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003429 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003430 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003431 if (handlers == NULL)
3432 return NULL;
3433
3434 for (i = 0; i < n_except; i++) {
3435 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3436 CHILD(n, 5 + i * 3));
3437 if (!e)
3438 return NULL;
3439 asdl_seq_SET(handlers, i, e);
3440 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003441 }
3442
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003443 assert(finally != NULL || asdl_seq_LEN(handlers));
3444 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445}
3446
Georg Brandl0c315622009-05-25 21:10:36 +00003447/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003448static withitem_ty
3449ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003450{
3451 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003452
Georg Brandl0c315622009-05-25 21:10:36 +00003453 REQ(n, with_item);
3454 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003455 if (!context_expr)
3456 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003457 if (NCH(n) == 3) {
3458 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003459
3460 if (!optional_vars) {
3461 return NULL;
3462 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003463 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003464 return NULL;
3465 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003466 }
3467
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003468 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003469}
3470
Georg Brandl0c315622009-05-25 21:10:36 +00003471/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3472static stmt_ty
3473ast_for_with_stmt(struct compiling *c, const node *n)
3474{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003475 int i, n_items;
3476 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003477
3478 REQ(n, with_stmt);
3479
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003480 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003481 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003482 if (!items)
3483 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003484 for (i = 1; i < NCH(n) - 2; i += 2) {
3485 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3486 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003487 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003488 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003489 }
3490
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003491 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3492 if (!body)
3493 return NULL;
3494
3495 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003496}
3497
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003499ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003501 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003502 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003503 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003504 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 REQ(n, classdef);
3507
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003508 if (NCH(n) == 4) { /* class NAME ':' suite */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 s = ast_for_suite(c, CHILD(n, 3));
3510 if (!s)
3511 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003512 classname = NEW_IDENTIFIER(CHILD(n, 1));
3513 if (!classname)
3514 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003515 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003516 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003517 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq,
3518 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003520
3521 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003522 s = ast_for_suite(c, CHILD(n,5));
3523 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003524 return NULL;
3525 classname = NEW_IDENTIFIER(CHILD(n, 1));
3526 if (!classname)
3527 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003528 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003529 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003530 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq,
3531 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 }
3533
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003534 /* class NAME '(' arglist ')' ':' suite */
3535 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003536 {
3537 PyObject *dummy_name;
3538 expr_ty dummy;
3539 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3540 if (!dummy_name)
3541 return NULL;
3542 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3543 call = ast_for_call(c, CHILD(n, 3), dummy);
3544 if (!call)
3545 return NULL;
3546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003548 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003550 classname = NEW_IDENTIFIER(CHILD(n, 1));
3551 if (!classname)
3552 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003553 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003554 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003555
Benjamin Peterson30760062008-11-25 04:02:28 +00003556 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords,
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003557 call->v.Call.starargs, call->v.Call.kwargs, s,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003558 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559}
3560
3561static stmt_ty
3562ast_for_stmt(struct compiling *c, const node *n)
3563{
3564 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003565 assert(NCH(n) == 1);
3566 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 }
3568 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003569 assert(num_stmts(n) == 1);
3570 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 }
3572 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003573 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003574 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3575 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003576 */
3577 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 case expr_stmt:
3579 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 case del_stmt:
3581 return ast_for_del_stmt(c, n);
3582 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003583 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 case flow_stmt:
3585 return ast_for_flow_stmt(c, n);
3586 case import_stmt:
3587 return ast_for_import_stmt(c, n);
3588 case global_stmt:
3589 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003590 case nonlocal_stmt:
3591 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 case assert_stmt:
3593 return ast_for_assert_stmt(c, n);
3594 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003595 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3597 TYPE(n), NCH(n));
3598 return NULL;
3599 }
3600 }
3601 else {
3602 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003603 | funcdef | classdef | decorated
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 */
3605 node *ch = CHILD(n, 0);
3606 REQ(n, compound_stmt);
3607 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608 case if_stmt:
3609 return ast_for_if_stmt(c, ch);
3610 case while_stmt:
3611 return ast_for_while_stmt(c, ch);
3612 case for_stmt:
3613 return ast_for_for_stmt(c, ch);
3614 case try_stmt:
3615 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003616 case with_stmt:
3617 return ast_for_with_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003619 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003621 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 case decorated:
3623 return ast_for_decorated(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003625 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3627 TYPE(n), NCH(n));
3628 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 }
3631}
3632
3633static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003634parsenumber(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003636 const char *end;
3637 long x;
3638 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003639 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003640 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641
Mark Dickinsond3c827b2008-12-05 18:10:46 +00003642 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003643 errno = 0;
3644 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003645 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00003646 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003647 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003648 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03003649 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003650 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003651 }
3652 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03003653 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003654 if (*end == '\0') {
3655 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03003656 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00003657 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003658 }
3659 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003660 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003661 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00003662 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
3663 if (compl.imag == -1.0 && PyErr_Occurred())
3664 return NULL;
3665 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003666 }
3667 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00003668 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00003669 dx = PyOS_string_to_double(s, NULL, NULL);
3670 if (dx == -1.0 && PyErr_Occurred())
3671 return NULL;
3672 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674}
3675
3676static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003677decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678{
Serhiy Storchakac6792272013-10-19 21:03:34 +03003679 const char *s, *t;
3680 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003681 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3682 while (s < end && (*s & 0x80)) s++;
3683 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003684 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685}
3686
3687static PyObject *
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003688decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003690 PyObject *v, *u;
3691 char *buf;
3692 char *p;
3693 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003694
Guido van Rossumd8faa362007-04-27 19:54:29 +00003695 if (encoding == NULL) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003696 u = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003697 } else {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003698 /* check for integer overflow */
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003699 if (len > PY_SIZE_MAX / 6)
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003700 return NULL;
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003701 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
3702 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
3703 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003704 if (u == NULL)
3705 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003706 p = buf = PyBytes_AsString(u);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003707 end = s + len;
3708 while (s < end) {
3709 if (*s == '\\') {
3710 *p++ = *s++;
3711 if (*s & 0x80) {
3712 strcpy(p, "u005c");
3713 p += 5;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003714 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003715 }
3716 if (*s & 0x80) { /* XXX inefficient */
3717 PyObject *w;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003718 int kind;
3719 void *data;
3720 Py_ssize_t len, i;
3721 w = decode_utf8(c, &s, end);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003722 if (w == NULL) {
3723 Py_DECREF(u);
3724 return NULL;
3725 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003726 kind = PyUnicode_KIND(w);
3727 data = PyUnicode_DATA(w);
3728 len = PyUnicode_GET_LENGTH(w);
3729 for (i = 0; i < len; i++) {
3730 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3731 sprintf(p, "\\U%08x", chr);
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003732 p += 10;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003733 }
Benjamin Petersonb2e796a2009-10-28 21:59:39 +00003734 /* Should be impossible to overflow */
3735 assert(p - buf <= Py_SIZE(u));
Guido van Rossumd8faa362007-04-27 19:54:29 +00003736 Py_DECREF(w);
3737 } else {
3738 *p++ = *s++;
3739 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003740 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003741 len = p - buf;
3742 s = buf;
3743 }
3744 if (rawmode)
3745 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3746 else
3747 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3748 Py_XDECREF(u);
3749 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750}
3751
3752/* s is a Python string literal, including the bracketing quote characters,
Guido van Rossum98297ee2007-11-06 21:34:58 +00003753 * and r &/or b prefixes (if any), and embedded escape sequences (if any).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 * parsestr parses it, and returns the decoded Python string object.
3755 */
3756static PyObject *
Christian Heimes4d6ec852008-03-26 22:34:47 +00003757parsestr(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003759 size_t len;
3760 const char *s = STR(n);
3761 int quote = Py_CHARMASK(*s);
3762 int rawmode = 0;
3763 int need_encoding;
Antoine Pitrou4de74572013-02-09 23:11:27 +01003764 if (Py_ISALPHA(quote)) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01003765 while (!*bytesmode || !rawmode) {
3766 if (quote == 'b' || quote == 'B') {
3767 quote = *++s;
3768 *bytesmode = 1;
3769 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00003770 else if (quote == 'u' || quote == 'U') {
3771 quote = *++s;
3772 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01003773 else if (quote == 'r' || quote == 'R') {
3774 quote = *++s;
3775 rawmode = 1;
3776 }
3777 else {
3778 break;
3779 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003780 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003781 }
3782 if (quote != '\'' && quote != '\"') {
3783 PyErr_BadInternalCall();
3784 return NULL;
3785 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003786 s++;
3787 len = strlen(s);
3788 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003790 "string to parse is too long");
3791 return NULL;
3792 }
3793 if (s[--len] != quote) {
3794 PyErr_BadInternalCall();
3795 return NULL;
3796 }
3797 if (len >= 4 && s[0] == quote && s[1] == quote) {
3798 s += 2;
3799 len -= 2;
3800 if (s[--len] != quote || s[--len] != quote) {
3801 PyErr_BadInternalCall();
3802 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003803 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003804 }
Benjamin Peterson8dbca062008-04-05 14:49:54 +00003805 if (!*bytesmode && !rawmode) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003806 return decode_unicode(c, s, len, rawmode, c->c_encoding);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003807 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003808 if (*bytesmode) {
3809 /* Disallow non-ascii characters (but not escapes) */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003810 const char *ch;
3811 for (ch = s; *ch; ch++) {
3812 if (Py_CHARMASK(*ch) >= 0x80) {
3813 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00003814 "literal characters.");
3815 return NULL;
3816 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00003817 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003818 }
Christian Heimes4d6ec852008-03-26 22:34:47 +00003819 need_encoding = (!*bytesmode && c->c_encoding != NULL &&
Brett Cannonda780432008-10-17 03:38:50 +00003820 strcmp(c->c_encoding, "utf-8") != 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003821 if (rawmode || strchr(s, '\\') == NULL) {
3822 if (need_encoding) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003823 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
Guido van Rossum29fd7122007-11-12 01:13:56 +00003824 if (u == NULL || !*bytesmode)
3825 return u;
Christian Heimes4d6ec852008-03-26 22:34:47 +00003826 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003827 Py_DECREF(u);
3828 return v;
Guido van Rossum29fd7122007-11-12 01:13:56 +00003829 } else if (*bytesmode) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003830 return PyBytes_FromStringAndSize(s, len);
Christian Heimes4d6ec852008-03-26 22:34:47 +00003831 } else if (strcmp(c->c_encoding, "utf-8") == 0) {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003832 return PyUnicode_FromStringAndSize(s, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 } else {
Guido van Rossum29fd7122007-11-12 01:13:56 +00003834 return PyUnicode_DecodeLatin1(s, len, NULL);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003835 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003836 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003837 return PyBytes_DecodeEscape(s, len, NULL, 1,
Christian Heimes4d6ec852008-03-26 22:34:47 +00003838 need_encoding ? c->c_encoding : NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839}
3840
Guido van Rossum29fd7122007-11-12 01:13:56 +00003841/* Build a Python string object out of a STRING+ atom. This takes care of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842 * compile-time literal catenation, calling parsestr() on each piece, and
3843 * pasting the intermediate results together.
3844 */
3845static PyObject *
Thomas Wouters00e41de2007-02-23 19:56:57 +00003846parsestrplus(struct compiling *c, const node *n, int *bytesmode)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003848 PyObject *v;
3849 int i;
3850 REQ(CHILD(n, 0), STRING);
Christian Heimes4d6ec852008-03-26 22:34:47 +00003851 v = parsestr(c, CHILD(n, 0), bytesmode);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003852 if (v != NULL) {
3853 /* String literal concatenation */
3854 for (i = 1; i < NCH(n); i++) {
3855 PyObject *s;
3856 int subbm = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00003857 s = parsestr(c, CHILD(n, i), &subbm);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003858 if (s == NULL)
3859 goto onError;
3860 if (*bytesmode != subbm) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003861 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Christian Heimes3d463392012-09-10 16:52:42 +02003862 Py_DECREF(s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003863 goto onError;
3864 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003865 if (PyBytes_Check(v) && PyBytes_Check(s)) {
3866 PyBytes_ConcatAndDel(&v, s);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003867 if (v == NULL)
3868 goto onError;
3869 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003870 else {
3871 PyObject *temp = PyUnicode_Concat(v, s);
3872 Py_DECREF(s);
3873 Py_DECREF(v);
3874 v = temp;
3875 if (v == NULL)
3876 goto onError;
3877 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003878 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003879 }
3880 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881
Guido van Rossumd8faa362007-04-27 19:54:29 +00003882 onError:
3883 Py_XDECREF(v);
3884 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885}