Adds a profile-opt target for easy compilation of a python binary using
[python.git] / Python / ast.c
blobcf9a06aa9e8b10cfe508124b6463345210c1c791
1 /*
2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
5 */
6 #include "Python.h"
7 #include "Python-ast.h"
8 #include "grammar.h"
9 #include "node.h"
10 #include "pyarena.h"
11 #include "ast.h"
12 #include "token.h"
13 #include "parsetok.h"
14 #include "graminit.h"
16 #include <assert.h>
18 /* Data structure used internally */
19 struct compiling {
20 char *c_encoding; /* source encoding */
21 int c_future_unicode; /* __future__ unicode literals flag */
22 PyArena *c_arena; /* arena for allocating memeory */
23 const char *c_filename; /* filename */
26 static asdl_seq *seq_for_testlist(struct compiling *, const node *);
27 static expr_ty ast_for_expr(struct compiling *, const node *);
28 static stmt_ty ast_for_stmt(struct compiling *, const node *);
29 static asdl_seq *ast_for_suite(struct compiling *, const node *);
30 static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
31 expr_context_ty);
32 static expr_ty ast_for_testlist(struct compiling *, const node *);
33 static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
34 static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
36 /* Note different signature for ast_for_call */
37 static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
39 static PyObject *parsenumber(const char *);
40 static PyObject *parsestr(struct compiling *, const char *);
41 static PyObject *parsestrplus(struct compiling *, const node *n);
43 #ifndef LINENO
44 #define LINENO(n) ((n)->n_lineno)
45 #endif
47 static identifier
48 new_identifier(const char* n, PyArena *arena) {
49 PyObject* id = PyString_InternFromString(n);
50 PyArena_AddPyObject(arena, id);
51 return id;
54 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
56 /* This routine provides an invalid object for the syntax error.
57 The outermost routine must unpack this error and create the
58 proper object. We do this so that we don't have to pass
59 the filename to everything function.
61 XXX Maybe we should just pass the filename...
64 static int
65 ast_error(const node *n, const char *errstr)
67 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
68 if (!u)
69 return 0;
70 PyErr_SetObject(PyExc_SyntaxError, u);
71 Py_DECREF(u);
72 return 0;
75 static void
76 ast_error_finish(const char *filename)
78 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
79 long lineno;
81 assert(PyErr_Occurred());
82 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
83 return;
85 PyErr_Fetch(&type, &value, &tback);
86 errstr = PyTuple_GetItem(value, 0);
87 if (!errstr)
88 return;
89 Py_INCREF(errstr);
90 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
91 if (lineno == -1) {
92 Py_DECREF(errstr);
93 return;
95 Py_DECREF(value);
97 loc = PyErr_ProgramText(filename, lineno);
98 if (!loc) {
99 Py_INCREF(Py_None);
100 loc = Py_None;
102 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
103 Py_DECREF(loc);
104 if (!tmp) {
105 Py_DECREF(errstr);
106 return;
108 value = PyTuple_Pack(2, errstr, tmp);
109 Py_DECREF(errstr);
110 Py_DECREF(tmp);
111 if (!value)
112 return;
113 PyErr_Restore(type, value, tback);
116 /* num_stmts() returns number of contained statements.
118 Use this routine to determine how big a sequence is needed for
119 the statements in a parse tree. Its raison d'etre is this bit of
120 grammar:
122 stmt: simple_stmt | compound_stmt
123 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
125 A simple_stmt can contain multiple small_stmt elements joined
126 by semicolons. If the arg is a simple_stmt, the number of
127 small_stmt elements is returned.
130 static int
131 num_stmts(const node *n)
133 int i, l;
134 node *ch;
136 switch (TYPE(n)) {
137 case single_input:
138 if (TYPE(CHILD(n, 0)) == NEWLINE)
139 return 0;
140 else
141 return num_stmts(CHILD(n, 0));
142 case file_input:
143 l = 0;
144 for (i = 0; i < NCH(n); i++) {
145 ch = CHILD(n, i);
146 if (TYPE(ch) == stmt)
147 l += num_stmts(ch);
149 return l;
150 case stmt:
151 return num_stmts(CHILD(n, 0));
152 case compound_stmt:
153 return 1;
154 case simple_stmt:
155 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
156 case suite:
157 if (NCH(n) == 1)
158 return num_stmts(CHILD(n, 0));
159 else {
160 l = 0;
161 for (i = 2; i < (NCH(n) - 1); i++)
162 l += num_stmts(CHILD(n, i));
163 return l;
165 default: {
166 char buf[128];
168 sprintf(buf, "Non-statement found: %d %d\n",
169 TYPE(n), NCH(n));
170 Py_FatalError(buf);
173 assert(0);
174 return 0;
177 /* Transform the CST rooted at node * to the appropriate AST
180 mod_ty
181 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
182 PyArena *arena)
184 int i, j, k, num;
185 asdl_seq *stmts = NULL;
186 stmt_ty s;
187 node *ch;
188 struct compiling c;
190 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
191 c.c_encoding = "utf-8";
192 if (TYPE(n) == encoding_decl) {
193 ast_error(n, "encoding declaration in Unicode string");
194 goto error;
196 } else if (TYPE(n) == encoding_decl) {
197 c.c_encoding = STR(n);
198 n = CHILD(n, 0);
199 } else {
200 c.c_encoding = NULL;
202 c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
203 c.c_arena = arena;
204 c.c_filename = filename;
206 k = 0;
207 switch (TYPE(n)) {
208 case file_input:
209 stmts = asdl_seq_new(num_stmts(n), arena);
210 if (!stmts)
211 return NULL;
212 for (i = 0; i < NCH(n) - 1; i++) {
213 ch = CHILD(n, i);
214 if (TYPE(ch) == NEWLINE)
215 continue;
216 REQ(ch, stmt);
217 num = num_stmts(ch);
218 if (num == 1) {
219 s = ast_for_stmt(&c, ch);
220 if (!s)
221 goto error;
222 asdl_seq_SET(stmts, k++, s);
224 else {
225 ch = CHILD(ch, 0);
226 REQ(ch, simple_stmt);
227 for (j = 0; j < num; j++) {
228 s = ast_for_stmt(&c, CHILD(ch, j * 2));
229 if (!s)
230 goto error;
231 asdl_seq_SET(stmts, k++, s);
235 return Module(stmts, arena);
236 case eval_input: {
237 expr_ty testlist_ast;
239 /* XXX Why not gen_for here? */
240 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
241 if (!testlist_ast)
242 goto error;
243 return Expression(testlist_ast, arena);
245 case single_input:
246 if (TYPE(CHILD(n, 0)) == NEWLINE) {
247 stmts = asdl_seq_new(1, arena);
248 if (!stmts)
249 goto error;
250 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
251 arena));
252 if (!asdl_seq_GET(stmts, 0))
253 goto error;
254 return Interactive(stmts, arena);
256 else {
257 n = CHILD(n, 0);
258 num = num_stmts(n);
259 stmts = asdl_seq_new(num, arena);
260 if (!stmts)
261 goto error;
262 if (num == 1) {
263 s = ast_for_stmt(&c, n);
264 if (!s)
265 goto error;
266 asdl_seq_SET(stmts, 0, s);
268 else {
269 /* Only a simple_stmt can contain multiple statements. */
270 REQ(n, simple_stmt);
271 for (i = 0; i < NCH(n); i += 2) {
272 if (TYPE(CHILD(n, i)) == NEWLINE)
273 break;
274 s = ast_for_stmt(&c, CHILD(n, i));
275 if (!s)
276 goto error;
277 asdl_seq_SET(stmts, i / 2, s);
281 return Interactive(stmts, arena);
283 default:
284 PyErr_Format(PyExc_SystemError,
285 "invalid node %d for PyAST_FromNode", TYPE(n));
286 goto error;
288 error:
289 ast_error_finish(filename);
290 return NULL;
293 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
296 static operator_ty
297 get_operator(const node *n)
299 switch (TYPE(n)) {
300 case VBAR:
301 return BitOr;
302 case CIRCUMFLEX:
303 return BitXor;
304 case AMPER:
305 return BitAnd;
306 case LEFTSHIFT:
307 return LShift;
308 case RIGHTSHIFT:
309 return RShift;
310 case PLUS:
311 return Add;
312 case MINUS:
313 return Sub;
314 case STAR:
315 return Mult;
316 case SLASH:
317 return Div;
318 case DOUBLESLASH:
319 return FloorDiv;
320 case PERCENT:
321 return Mod;
322 default:
323 return (operator_ty)0;
327 /* Set the context ctx for expr_ty e, recursively traversing e.
329 Only sets context for expr kinds that "can appear in assignment context"
330 (according to ../Parser/Python.asdl). For other expr kinds, it sets
331 an appropriate syntax error and returns false.
334 static int
335 set_context(expr_ty e, expr_context_ty ctx, const node *n)
337 asdl_seq *s = NULL;
338 /* If a particular expression type can't be used for assign / delete,
339 set expr_name to its name and an error message will be generated.
341 const char* expr_name = NULL;
343 /* The ast defines augmented store and load contexts, but the
344 implementation here doesn't actually use them. The code may be
345 a little more complex than necessary as a result. It also means
346 that expressions in an augmented assignment have a Store context.
347 Consider restructuring so that augmented assignment uses
348 set_context(), too.
350 assert(ctx != AugStore && ctx != AugLoad);
352 switch (e->kind) {
353 case Attribute_kind:
354 if (ctx == Store &&
355 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
356 return ast_error(n, "assignment to None");
358 e->v.Attribute.ctx = ctx;
359 break;
360 case Subscript_kind:
361 e->v.Subscript.ctx = ctx;
362 break;
363 case Name_kind:
364 if (ctx == Store &&
365 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
366 return ast_error(n, "assignment to None");
368 e->v.Name.ctx = ctx;
369 break;
370 case List_kind:
371 e->v.List.ctx = ctx;
372 s = e->v.List.elts;
373 break;
374 case Tuple_kind:
375 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
376 return ast_error(n, "can't assign to ()");
377 e->v.Tuple.ctx = ctx;
378 s = e->v.Tuple.elts;
379 break;
380 case Lambda_kind:
381 expr_name = "lambda";
382 break;
383 case Call_kind:
384 expr_name = "function call";
385 break;
386 case BoolOp_kind:
387 case BinOp_kind:
388 case UnaryOp_kind:
389 expr_name = "operator";
390 break;
391 case GeneratorExp_kind:
392 expr_name = "generator expression";
393 break;
394 case Yield_kind:
395 expr_name = "yield expression";
396 break;
397 case ListComp_kind:
398 expr_name = "list comprehension";
399 break;
400 case Dict_kind:
401 case Num_kind:
402 case Str_kind:
403 expr_name = "literal";
404 break;
405 case Compare_kind:
406 expr_name = "comparison";
407 break;
408 case Repr_kind:
409 expr_name = "repr";
410 break;
411 case IfExp_kind:
412 expr_name = "conditional expression";
413 break;
414 default:
415 PyErr_Format(PyExc_SystemError,
416 "unexpected expression in assignment %d (line %d)",
417 e->kind, e->lineno);
418 return 0;
420 /* Check for error string set by switch */
421 if (expr_name) {
422 char buf[300];
423 PyOS_snprintf(buf, sizeof(buf),
424 "can't %s %s",
425 ctx == Store ? "assign to" : "delete",
426 expr_name);
427 return ast_error(n, buf);
430 /* If the LHS is a list or tuple, we need to set the assignment
431 context for all the contained elements.
433 if (s) {
434 int i;
436 for (i = 0; i < asdl_seq_LEN(s); i++) {
437 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
438 return 0;
441 return 1;
444 static operator_ty
445 ast_for_augassign(const node *n)
447 REQ(n, augassign);
448 n = CHILD(n, 0);
449 switch (STR(n)[0]) {
450 case '+':
451 return Add;
452 case '-':
453 return Sub;
454 case '/':
455 if (STR(n)[1] == '/')
456 return FloorDiv;
457 else
458 return Div;
459 case '%':
460 return Mod;
461 case '<':
462 return LShift;
463 case '>':
464 return RShift;
465 case '&':
466 return BitAnd;
467 case '^':
468 return BitXor;
469 case '|':
470 return BitOr;
471 case '*':
472 if (STR(n)[1] == '*')
473 return Pow;
474 else
475 return Mult;
476 default:
477 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
478 return (operator_ty)0;
482 static cmpop_ty
483 ast_for_comp_op(const node *n)
485 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
486 |'is' 'not'
488 REQ(n, comp_op);
489 if (NCH(n) == 1) {
490 n = CHILD(n, 0);
491 switch (TYPE(n)) {
492 case LESS:
493 return Lt;
494 case GREATER:
495 return Gt;
496 case EQEQUAL: /* == */
497 return Eq;
498 case LESSEQUAL:
499 return LtE;
500 case GREATEREQUAL:
501 return GtE;
502 case NOTEQUAL:
503 return NotEq;
504 case NAME:
505 if (strcmp(STR(n), "in") == 0)
506 return In;
507 if (strcmp(STR(n), "is") == 0)
508 return Is;
509 default:
510 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
511 STR(n));
512 return (cmpop_ty)0;
515 else if (NCH(n) == 2) {
516 /* handle "not in" and "is not" */
517 switch (TYPE(CHILD(n, 0))) {
518 case NAME:
519 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
520 return NotIn;
521 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
522 return IsNot;
523 default:
524 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
525 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
526 return (cmpop_ty)0;
529 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
530 NCH(n));
531 return (cmpop_ty)0;
534 static asdl_seq *
535 seq_for_testlist(struct compiling *c, const node *n)
537 /* testlist: test (',' test)* [','] */
538 asdl_seq *seq;
539 expr_ty expression;
540 int i;
541 assert(TYPE(n) == testlist ||
542 TYPE(n) == listmaker ||
543 TYPE(n) == testlist_gexp ||
544 TYPE(n) == testlist_safe ||
545 TYPE(n) == testlist1);
547 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
548 if (!seq)
549 return NULL;
551 for (i = 0; i < NCH(n); i += 2) {
552 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
554 expression = ast_for_expr(c, CHILD(n, i));
555 if (!expression)
556 return NULL;
558 assert(i / 2 < seq->size);
559 asdl_seq_SET(seq, i / 2, expression);
561 return seq;
564 static expr_ty
565 compiler_complex_args(struct compiling *c, const node *n)
567 int i, len = (NCH(n) + 1) / 2;
568 expr_ty result;
569 asdl_seq *args = asdl_seq_new(len, c->c_arena);
570 if (!args)
571 return NULL;
573 /* fpdef: NAME | '(' fplist ')'
574 fplist: fpdef (',' fpdef)* [',']
576 REQ(n, fplist);
577 for (i = 0; i < len; i++) {
578 const node *fpdef_node = CHILD(n, 2*i);
579 const node *child;
580 expr_ty arg;
581 set_name:
582 /* fpdef_node is either a NAME or an fplist */
583 child = CHILD(fpdef_node, 0);
584 if (TYPE(child) == NAME) {
585 if (!strcmp(STR(child), "None")) {
586 ast_error(child, "assignment to None");
587 return NULL;
589 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
590 child->n_col_offset, c->c_arena);
592 else {
593 assert(TYPE(fpdef_node) == fpdef);
594 /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
595 child = CHILD(fpdef_node, 1);
596 assert(TYPE(child) == fplist);
597 /* NCH == 1 means we have (x), we need to elide the extra parens */
598 if (NCH(child) == 1) {
599 fpdef_node = CHILD(child, 0);
600 assert(TYPE(fpdef_node) == fpdef);
601 goto set_name;
603 arg = compiler_complex_args(c, child);
605 asdl_seq_SET(args, i, arg);
608 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
609 if (!set_context(result, Store, n))
610 return NULL;
611 return result;
615 /* Create AST for argument list. */
617 static arguments_ty
618 ast_for_arguments(struct compiling *c, const node *n)
620 /* parameters: '(' [varargslist] ')'
621 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
622 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
624 int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
625 asdl_seq *args, *defaults;
626 identifier vararg = NULL, kwarg = NULL;
627 node *ch;
629 if (TYPE(n) == parameters) {
630 if (NCH(n) == 2) /* () as argument list */
631 return arguments(NULL, NULL, NULL, NULL, c->c_arena);
632 n = CHILD(n, 1);
634 REQ(n, varargslist);
636 /* first count the number of normal args & defaults */
637 for (i = 0; i < NCH(n); i++) {
638 ch = CHILD(n, i);
639 if (TYPE(ch) == fpdef)
640 n_args++;
641 if (TYPE(ch) == EQUAL)
642 n_defaults++;
644 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
645 if (!args && n_args)
646 return NULL; /* Don't need to goto error; no objects allocated */
647 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
648 if (!defaults && n_defaults)
649 return NULL; /* Don't need to goto error; no objects allocated */
651 /* fpdef: NAME | '(' fplist ')'
652 fplist: fpdef (',' fpdef)* [',']
654 i = 0;
655 j = 0; /* index for defaults */
656 k = 0; /* index for args */
657 while (i < NCH(n)) {
658 ch = CHILD(n, i);
659 switch (TYPE(ch)) {
660 case fpdef:
661 handle_fpdef:
662 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
663 anything other than EQUAL or a comma? */
664 /* XXX Should NCH(n) check be made a separate check? */
665 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
666 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
667 if (!expression)
668 goto error;
669 assert(defaults != NULL);
670 asdl_seq_SET(defaults, j++, expression);
671 i += 2;
672 found_default = 1;
674 else if (found_default) {
675 ast_error(n,
676 "non-default argument follows default argument");
677 goto error;
679 if (NCH(ch) == 3) {
680 ch = CHILD(ch, 1);
681 /* def foo((x)): is not complex, special case. */
682 if (NCH(ch) != 1) {
683 /* We have complex arguments, setup for unpacking. */
684 asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
685 if (!asdl_seq_GET(args, k-1))
686 goto error;
687 } else {
688 /* def foo((x)): setup for checking NAME below. */
689 /* Loop because there can be many parens and tuple
690 unpacking mixed in. */
691 ch = CHILD(ch, 0);
692 assert(TYPE(ch) == fpdef);
693 goto handle_fpdef;
696 if (TYPE(CHILD(ch, 0)) == NAME) {
697 expr_ty name;
698 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
699 ast_error(CHILD(ch, 0), "assignment to None");
700 goto error;
702 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
703 Param, LINENO(ch), ch->n_col_offset,
704 c->c_arena);
705 if (!name)
706 goto error;
707 asdl_seq_SET(args, k++, name);
710 i += 2; /* the name and the comma */
711 break;
712 case STAR:
713 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
714 ast_error(CHILD(n, i+1), "assignment to None");
715 goto error;
717 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
718 i += 3;
719 break;
720 case DOUBLESTAR:
721 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
722 ast_error(CHILD(n, i+1), "assignment to None");
723 goto error;
725 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
726 i += 3;
727 break;
728 default:
729 PyErr_Format(PyExc_SystemError,
730 "unexpected node in varargslist: %d @ %d",
731 TYPE(ch), i);
732 goto error;
736 return arguments(args, vararg, kwarg, defaults, c->c_arena);
738 error:
739 Py_XDECREF(vararg);
740 Py_XDECREF(kwarg);
741 return NULL;
744 static expr_ty
745 ast_for_dotted_name(struct compiling *c, const node *n)
747 expr_ty e;
748 identifier id;
749 int lineno, col_offset;
750 int i;
752 REQ(n, dotted_name);
754 lineno = LINENO(n);
755 col_offset = n->n_col_offset;
757 id = NEW_IDENTIFIER(CHILD(n, 0));
758 if (!id)
759 return NULL;
760 e = Name(id, Load, lineno, col_offset, c->c_arena);
761 if (!e)
762 return NULL;
764 for (i = 2; i < NCH(n); i+=2) {
765 id = NEW_IDENTIFIER(CHILD(n, i));
766 if (!id)
767 return NULL;
768 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
769 if (!e)
770 return NULL;
773 return e;
776 static expr_ty
777 ast_for_decorator(struct compiling *c, const node *n)
779 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
780 expr_ty d = NULL;
781 expr_ty name_expr;
783 REQ(n, decorator);
784 REQ(CHILD(n, 0), AT);
785 REQ(RCHILD(n, -1), NEWLINE);
787 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
788 if (!name_expr)
789 return NULL;
791 if (NCH(n) == 3) { /* No arguments */
792 d = name_expr;
793 name_expr = NULL;
795 else if (NCH(n) == 5) { /* Call with no arguments */
796 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
797 n->n_col_offset, c->c_arena);
798 if (!d)
799 return NULL;
800 name_expr = NULL;
802 else {
803 d = ast_for_call(c, CHILD(n, 3), name_expr);
804 if (!d)
805 return NULL;
806 name_expr = NULL;
809 return d;
812 static asdl_seq*
813 ast_for_decorators(struct compiling *c, const node *n)
815 asdl_seq* decorator_seq;
816 expr_ty d;
817 int i;
819 REQ(n, decorators);
820 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
821 if (!decorator_seq)
822 return NULL;
824 for (i = 0; i < NCH(n); i++) {
825 d = ast_for_decorator(c, CHILD(n, i));
826 if (!d)
827 return NULL;
828 asdl_seq_SET(decorator_seq, i, d);
830 return decorator_seq;
833 static stmt_ty
834 ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
836 /* funcdef: 'def' NAME parameters ':' suite */
837 identifier name;
838 arguments_ty args;
839 asdl_seq *body;
840 int name_i = 1;
842 REQ(n, funcdef);
844 name = NEW_IDENTIFIER(CHILD(n, name_i));
845 if (!name)
846 return NULL;
847 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
848 ast_error(CHILD(n, name_i), "assignment to None");
849 return NULL;
851 args = ast_for_arguments(c, CHILD(n, name_i + 1));
852 if (!args)
853 return NULL;
854 body = ast_for_suite(c, CHILD(n, name_i + 3));
855 if (!body)
856 return NULL;
858 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
859 n->n_col_offset, c->c_arena);
862 static stmt_ty
863 ast_for_decorated(struct compiling *c, const node *n)
865 /* decorated: decorators (classdef | funcdef) */
866 stmt_ty thing = NULL;
867 asdl_seq *decorator_seq = NULL;
869 REQ(n, decorated);
871 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
872 if (!decorator_seq)
873 return NULL;
875 assert(TYPE(CHILD(n, 1)) == funcdef ||
876 TYPE(CHILD(n, 1)) == classdef);
878 if (TYPE(CHILD(n, 1)) == funcdef) {
879 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
880 } else if (TYPE(CHILD(n, 1)) == classdef) {
881 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
883 /* we count the decorators in when talking about the class' or
884 function's line number */
885 if (thing) {
886 thing->lineno = LINENO(n);
887 thing->col_offset = n->n_col_offset;
889 return thing;
892 static expr_ty
893 ast_for_lambdef(struct compiling *c, const node *n)
895 /* lambdef: 'lambda' [varargslist] ':' test */
896 arguments_ty args;
897 expr_ty expression;
899 if (NCH(n) == 3) {
900 args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
901 if (!args)
902 return NULL;
903 expression = ast_for_expr(c, CHILD(n, 2));
904 if (!expression)
905 return NULL;
907 else {
908 args = ast_for_arguments(c, CHILD(n, 1));
909 if (!args)
910 return NULL;
911 expression = ast_for_expr(c, CHILD(n, 3));
912 if (!expression)
913 return NULL;
916 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
919 static expr_ty
920 ast_for_ifexpr(struct compiling *c, const node *n)
922 /* test: or_test 'if' or_test 'else' test */
923 expr_ty expression, body, orelse;
925 assert(NCH(n) == 5);
926 body = ast_for_expr(c, CHILD(n, 0));
927 if (!body)
928 return NULL;
929 expression = ast_for_expr(c, CHILD(n, 2));
930 if (!expression)
931 return NULL;
932 orelse = ast_for_expr(c, CHILD(n, 4));
933 if (!orelse)
934 return NULL;
935 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
936 c->c_arena);
939 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
940 so there is only a single version. Possibly for loops can also re-use
941 the code.
944 /* Count the number of 'for' loop in a list comprehension.
946 Helper for ast_for_listcomp().
949 static int
950 count_list_fors(const node *n)
952 int n_fors = 0;
953 node *ch = CHILD(n, 1);
955 count_list_for:
956 n_fors++;
957 REQ(ch, list_for);
958 if (NCH(ch) == 5)
959 ch = CHILD(ch, 4);
960 else
961 return n_fors;
962 count_list_iter:
963 REQ(ch, list_iter);
964 ch = CHILD(ch, 0);
965 if (TYPE(ch) == list_for)
966 goto count_list_for;
967 else if (TYPE(ch) == list_if) {
968 if (NCH(ch) == 3) {
969 ch = CHILD(ch, 2);
970 goto count_list_iter;
972 else
973 return n_fors;
976 /* Should never be reached */
977 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
978 return -1;
981 /* Count the number of 'if' statements in a list comprehension.
983 Helper for ast_for_listcomp().
986 static int
987 count_list_ifs(const node *n)
989 int n_ifs = 0;
991 count_list_iter:
992 REQ(n, list_iter);
993 if (TYPE(CHILD(n, 0)) == list_for)
994 return n_ifs;
995 n = CHILD(n, 0);
996 REQ(n, list_if);
997 n_ifs++;
998 if (NCH(n) == 2)
999 return n_ifs;
1000 n = CHILD(n, 2);
1001 goto count_list_iter;
1004 static expr_ty
1005 ast_for_listcomp(struct compiling *c, const node *n)
1007 /* listmaker: test ( list_for | (',' test)* [','] )
1008 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1009 list_iter: list_for | list_if
1010 list_if: 'if' test [list_iter]
1011 testlist_safe: test [(',' test)+ [',']]
1013 expr_ty elt;
1014 asdl_seq *listcomps;
1015 int i, n_fors;
1016 node *ch;
1018 REQ(n, listmaker);
1019 assert(NCH(n) > 1);
1021 elt = ast_for_expr(c, CHILD(n, 0));
1022 if (!elt)
1023 return NULL;
1025 n_fors = count_list_fors(n);
1026 if (n_fors == -1)
1027 return NULL;
1029 listcomps = asdl_seq_new(n_fors, c->c_arena);
1030 if (!listcomps)
1031 return NULL;
1033 ch = CHILD(n, 1);
1034 for (i = 0; i < n_fors; i++) {
1035 comprehension_ty lc;
1036 asdl_seq *t;
1037 expr_ty expression;
1038 node *for_ch;
1040 REQ(ch, list_for);
1042 for_ch = CHILD(ch, 1);
1043 t = ast_for_exprlist(c, for_ch, Store);
1044 if (!t)
1045 return NULL;
1046 expression = ast_for_testlist(c, CHILD(ch, 3));
1047 if (!expression)
1048 return NULL;
1050 /* Check the # of children rather than the length of t, since
1051 [x for x, in ... ] has 1 element in t, but still requires a Tuple.
1053 if (NCH(for_ch) == 1)
1054 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
1055 c->c_arena);
1056 else
1057 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1058 c->c_arena),
1059 expression, NULL, c->c_arena);
1060 if (!lc)
1061 return NULL;
1063 if (NCH(ch) == 5) {
1064 int j, n_ifs;
1065 asdl_seq *ifs;
1066 expr_ty list_for_expr;
1068 ch = CHILD(ch, 4);
1069 n_ifs = count_list_ifs(ch);
1070 if (n_ifs == -1)
1071 return NULL;
1073 ifs = asdl_seq_new(n_ifs, c->c_arena);
1074 if (!ifs)
1075 return NULL;
1077 for (j = 0; j < n_ifs; j++) {
1078 REQ(ch, list_iter);
1079 ch = CHILD(ch, 0);
1080 REQ(ch, list_if);
1082 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
1083 if (!list_for_expr)
1084 return NULL;
1086 asdl_seq_SET(ifs, j, list_for_expr);
1087 if (NCH(ch) == 3)
1088 ch = CHILD(ch, 2);
1090 /* on exit, must guarantee that ch is a list_for */
1091 if (TYPE(ch) == list_iter)
1092 ch = CHILD(ch, 0);
1093 lc->ifs = ifs;
1095 asdl_seq_SET(listcomps, i, lc);
1098 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
1101 /* Count the number of 'for' loops in a generator expression.
1103 Helper for ast_for_genexp().
1106 static int
1107 count_gen_fors(const node *n)
1109 int n_fors = 0;
1110 node *ch = CHILD(n, 1);
1112 count_gen_for:
1113 n_fors++;
1114 REQ(ch, gen_for);
1115 if (NCH(ch) == 5)
1116 ch = CHILD(ch, 4);
1117 else
1118 return n_fors;
1119 count_gen_iter:
1120 REQ(ch, gen_iter);
1121 ch = CHILD(ch, 0);
1122 if (TYPE(ch) == gen_for)
1123 goto count_gen_for;
1124 else if (TYPE(ch) == gen_if) {
1125 if (NCH(ch) == 3) {
1126 ch = CHILD(ch, 2);
1127 goto count_gen_iter;
1129 else
1130 return n_fors;
1133 /* Should never be reached */
1134 PyErr_SetString(PyExc_SystemError,
1135 "logic error in count_gen_fors");
1136 return -1;
1139 /* Count the number of 'if' statements in a generator expression.
1141 Helper for ast_for_genexp().
1144 static int
1145 count_gen_ifs(const node *n)
1147 int n_ifs = 0;
1149 while (1) {
1150 REQ(n, gen_iter);
1151 if (TYPE(CHILD(n, 0)) == gen_for)
1152 return n_ifs;
1153 n = CHILD(n, 0);
1154 REQ(n, gen_if);
1155 n_ifs++;
1156 if (NCH(n) == 2)
1157 return n_ifs;
1158 n = CHILD(n, 2);
1162 /* TODO(jhylton): Combine with list comprehension code? */
1163 static expr_ty
1164 ast_for_genexp(struct compiling *c, const node *n)
1166 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
1167 argument: [test '='] test [gen_for] # Really [keyword '='] test */
1168 expr_ty elt;
1169 asdl_seq *genexps;
1170 int i, n_fors;
1171 node *ch;
1173 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1174 assert(NCH(n) > 1);
1176 elt = ast_for_expr(c, CHILD(n, 0));
1177 if (!elt)
1178 return NULL;
1180 n_fors = count_gen_fors(n);
1181 if (n_fors == -1)
1182 return NULL;
1184 genexps = asdl_seq_new(n_fors, c->c_arena);
1185 if (!genexps)
1186 return NULL;
1188 ch = CHILD(n, 1);
1189 for (i = 0; i < n_fors; i++) {
1190 comprehension_ty ge;
1191 asdl_seq *t;
1192 expr_ty expression;
1193 node *for_ch;
1195 REQ(ch, gen_for);
1197 for_ch = CHILD(ch, 1);
1198 t = ast_for_exprlist(c, for_ch, Store);
1199 if (!t)
1200 return NULL;
1201 expression = ast_for_expr(c, CHILD(ch, 3));
1202 if (!expression)
1203 return NULL;
1205 /* Check the # of children rather than the length of t, since
1206 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
1207 if (NCH(for_ch) == 1)
1208 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
1209 NULL, c->c_arena);
1210 else
1211 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
1212 c->c_arena),
1213 expression, NULL, c->c_arena);
1215 if (!ge)
1216 return NULL;
1218 if (NCH(ch) == 5) {
1219 int j, n_ifs;
1220 asdl_seq *ifs;
1222 ch = CHILD(ch, 4);
1223 n_ifs = count_gen_ifs(ch);
1224 if (n_ifs == -1)
1225 return NULL;
1227 ifs = asdl_seq_new(n_ifs, c->c_arena);
1228 if (!ifs)
1229 return NULL;
1231 for (j = 0; j < n_ifs; j++) {
1232 REQ(ch, gen_iter);
1233 ch = CHILD(ch, 0);
1234 REQ(ch, gen_if);
1236 expression = ast_for_expr(c, CHILD(ch, 1));
1237 if (!expression)
1238 return NULL;
1239 asdl_seq_SET(ifs, j, expression);
1240 if (NCH(ch) == 3)
1241 ch = CHILD(ch, 2);
1243 /* on exit, must guarantee that ch is a gen_for */
1244 if (TYPE(ch) == gen_iter)
1245 ch = CHILD(ch, 0);
1246 ge->ifs = ifs;
1248 asdl_seq_SET(genexps, i, ge);
1251 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
1254 static expr_ty
1255 ast_for_atom(struct compiling *c, const node *n)
1257 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
1258 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
1260 node *ch = CHILD(n, 0);
1262 switch (TYPE(ch)) {
1263 case NAME:
1264 /* All names start in Load context, but may later be
1265 changed. */
1266 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset,
1267 c->c_arena);
1268 case STRING: {
1269 PyObject *str = parsestrplus(c, n);
1270 if (!str) {
1271 #ifdef Py_USING_UNICODE
1272 if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
1273 PyObject *type, *value, *tback, *errstr;
1274 PyErr_Fetch(&type, &value, &tback);
1275 errstr = ((PyUnicodeErrorObject *)value)->reason;
1276 if (errstr) {
1277 char *s = "";
1278 char buf[128];
1279 s = PyString_AsString(errstr);
1280 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
1281 ast_error(n, buf);
1282 } else {
1283 ast_error(n, "(unicode error) unknown error");
1285 Py_DECREF(type);
1286 Py_DECREF(value);
1287 Py_XDECREF(tback);
1289 #endif
1290 return NULL;
1292 PyArena_AddPyObject(c->c_arena, str);
1293 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
1295 case NUMBER: {
1296 PyObject *pynum = parsenumber(STR(ch));
1297 if (!pynum)
1298 return NULL;
1300 PyArena_AddPyObject(c->c_arena, pynum);
1301 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
1303 case LPAR: /* some parenthesized expressions */
1304 ch = CHILD(n, 1);
1306 if (TYPE(ch) == RPAR)
1307 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1309 if (TYPE(ch) == yield_expr)
1310 return ast_for_expr(c, ch);
1312 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
1313 return ast_for_genexp(c, ch);
1315 return ast_for_testlist_gexp(c, ch);
1316 case LSQB: /* list (or list comprehension) */
1317 ch = CHILD(n, 1);
1319 if (TYPE(ch) == RSQB)
1320 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
1322 REQ(ch, listmaker);
1323 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
1324 asdl_seq *elts = seq_for_testlist(c, ch);
1325 if (!elts)
1326 return NULL;
1328 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1330 else
1331 return ast_for_listcomp(c, ch);
1332 case LBRACE: {
1333 /* dictmaker: test ':' test (',' test ':' test)* [','] */
1334 int i, size;
1335 asdl_seq *keys, *values;
1337 ch = CHILD(n, 1);
1338 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
1339 keys = asdl_seq_new(size, c->c_arena);
1340 if (!keys)
1341 return NULL;
1343 values = asdl_seq_new(size, c->c_arena);
1344 if (!values)
1345 return NULL;
1347 for (i = 0; i < NCH(ch); i += 4) {
1348 expr_ty expression;
1350 expression = ast_for_expr(c, CHILD(ch, i));
1351 if (!expression)
1352 return NULL;
1354 asdl_seq_SET(keys, i / 4, expression);
1356 expression = ast_for_expr(c, CHILD(ch, i + 2));
1357 if (!expression)
1358 return NULL;
1360 asdl_seq_SET(values, i / 4, expression);
1362 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
1364 case BACKQUOTE: { /* repr */
1365 expr_ty expression;
1366 if (Py_Py3kWarningFlag) {
1367 if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
1368 "backquote not supported in 3.x; use repr()",
1369 c->c_filename, LINENO(n),
1370 NULL, NULL)) {
1371 return NULL;
1374 expression = ast_for_testlist(c, CHILD(n, 1));
1375 if (!expression)
1376 return NULL;
1378 return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
1380 default:
1381 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
1382 return NULL;
1386 static slice_ty
1387 ast_for_slice(struct compiling *c, const node *n)
1389 node *ch;
1390 expr_ty lower = NULL, upper = NULL, step = NULL;
1392 REQ(n, subscript);
1395 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1396 sliceop: ':' [test]
1398 ch = CHILD(n, 0);
1399 if (TYPE(ch) == DOT)
1400 return Ellipsis(c->c_arena);
1402 if (NCH(n) == 1 && TYPE(ch) == test) {
1403 /* 'step' variable hold no significance in terms of being used over
1404 other vars */
1405 step = ast_for_expr(c, ch);
1406 if (!step)
1407 return NULL;
1409 return Index(step, c->c_arena);
1412 if (TYPE(ch) == test) {
1413 lower = ast_for_expr(c, ch);
1414 if (!lower)
1415 return NULL;
1418 /* If there's an upper bound it's in the second or third position. */
1419 if (TYPE(ch) == COLON) {
1420 if (NCH(n) > 1) {
1421 node *n2 = CHILD(n, 1);
1423 if (TYPE(n2) == test) {
1424 upper = ast_for_expr(c, n2);
1425 if (!upper)
1426 return NULL;
1429 } else if (NCH(n) > 2) {
1430 node *n2 = CHILD(n, 2);
1432 if (TYPE(n2) == test) {
1433 upper = ast_for_expr(c, n2);
1434 if (!upper)
1435 return NULL;
1439 ch = CHILD(n, NCH(n) - 1);
1440 if (TYPE(ch) == sliceop) {
1441 if (NCH(ch) == 1) {
1442 /* No expression, so step is None */
1443 ch = CHILD(ch, 0);
1444 step = Name(new_identifier("None", c->c_arena), Load,
1445 LINENO(ch), ch->n_col_offset, c->c_arena);
1446 if (!step)
1447 return NULL;
1448 } else {
1449 ch = CHILD(ch, 1);
1450 if (TYPE(ch) == test) {
1451 step = ast_for_expr(c, ch);
1452 if (!step)
1453 return NULL;
1458 return Slice(lower, upper, step, c->c_arena);
1461 static expr_ty
1462 ast_for_binop(struct compiling *c, const node *n)
1464 /* Must account for a sequence of expressions.
1465 How should A op B op C by represented?
1466 BinOp(BinOp(A, op, B), op, C).
1469 int i, nops;
1470 expr_ty expr1, expr2, result;
1471 operator_ty newoperator;
1473 expr1 = ast_for_expr(c, CHILD(n, 0));
1474 if (!expr1)
1475 return NULL;
1477 expr2 = ast_for_expr(c, CHILD(n, 2));
1478 if (!expr2)
1479 return NULL;
1481 newoperator = get_operator(CHILD(n, 1));
1482 if (!newoperator)
1483 return NULL;
1485 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
1486 c->c_arena);
1487 if (!result)
1488 return NULL;
1490 nops = (NCH(n) - 1) / 2;
1491 for (i = 1; i < nops; i++) {
1492 expr_ty tmp_result, tmp;
1493 const node* next_oper = CHILD(n, i * 2 + 1);
1495 newoperator = get_operator(next_oper);
1496 if (!newoperator)
1497 return NULL;
1499 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1500 if (!tmp)
1501 return NULL;
1503 tmp_result = BinOp(result, newoperator, tmp,
1504 LINENO(next_oper), next_oper->n_col_offset,
1505 c->c_arena);
1506 if (!tmp_result)
1507 return NULL;
1508 result = tmp_result;
1510 return result;
1513 static expr_ty
1514 ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
1516 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1517 subscriptlist: subscript (',' subscript)* [',']
1518 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1520 REQ(n, trailer);
1521 if (TYPE(CHILD(n, 0)) == LPAR) {
1522 if (NCH(n) == 2)
1523 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
1524 n->n_col_offset, c->c_arena);
1525 else
1526 return ast_for_call(c, CHILD(n, 1), left_expr);
1528 else if (TYPE(CHILD(n, 0)) == DOT ) {
1529 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
1530 LINENO(n), n->n_col_offset, c->c_arena);
1532 else {
1533 REQ(CHILD(n, 0), LSQB);
1534 REQ(CHILD(n, 2), RSQB);
1535 n = CHILD(n, 1);
1536 if (NCH(n) == 1) {
1537 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
1538 if (!slc)
1539 return NULL;
1540 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
1541 c->c_arena);
1543 else {
1544 /* The grammar is ambiguous here. The ambiguity is resolved
1545 by treating the sequence as a tuple literal if there are
1546 no slice features.
1548 int j;
1549 slice_ty slc;
1550 expr_ty e;
1551 bool simple = true;
1552 asdl_seq *slices, *elts;
1553 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1554 if (!slices)
1555 return NULL;
1556 for (j = 0; j < NCH(n); j += 2) {
1557 slc = ast_for_slice(c, CHILD(n, j));
1558 if (!slc)
1559 return NULL;
1560 if (slc->kind != Index_kind)
1561 simple = false;
1562 asdl_seq_SET(slices, j / 2, slc);
1564 if (!simple) {
1565 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
1566 Load, LINENO(n), n->n_col_offset, c->c_arena);
1568 /* extract Index values and put them in a Tuple */
1569 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1570 if (!elts)
1571 return NULL;
1572 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
1573 slc = (slice_ty)asdl_seq_GET(slices, j);
1574 assert(slc->kind == Index_kind && slc->v.Index.value);
1575 asdl_seq_SET(elts, j, slc->v.Index.value);
1577 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
1578 if (!e)
1579 return NULL;
1580 return Subscript(left_expr, Index(e, c->c_arena),
1581 Load, LINENO(n), n->n_col_offset, c->c_arena);
1586 static expr_ty
1587 ast_for_factor(struct compiling *c, const node *n)
1589 node *pfactor, *ppower, *patom, *pnum;
1590 expr_ty expression;
1592 /* If the unary - operator is applied to a constant, don't generate
1593 a UNARY_NEGATIVE opcode. Just store the approriate value as a
1594 constant. The peephole optimizer already does something like
1595 this but it doesn't handle the case where the constant is
1596 (sys.maxint - 1). In that case, we want a PyIntObject, not a
1597 PyLongObject.
1599 if (TYPE(CHILD(n, 0)) == MINUS &&
1600 NCH(n) == 2 &&
1601 TYPE((pfactor = CHILD(n, 1))) == factor &&
1602 NCH(pfactor) == 1 &&
1603 TYPE((ppower = CHILD(pfactor, 0))) == power &&
1604 NCH(ppower) == 1 &&
1605 TYPE((patom = CHILD(ppower, 0))) == atom &&
1606 TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1607 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1608 if (s == NULL)
1609 return NULL;
1610 s[0] = '-';
1611 strcpy(s + 1, STR(pnum));
1612 PyObject_FREE(STR(pnum));
1613 STR(pnum) = s;
1614 return ast_for_atom(c, patom);
1617 expression = ast_for_expr(c, CHILD(n, 1));
1618 if (!expression)
1619 return NULL;
1621 switch (TYPE(CHILD(n, 0))) {
1622 case PLUS:
1623 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
1624 c->c_arena);
1625 case MINUS:
1626 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
1627 c->c_arena);
1628 case TILDE:
1629 return UnaryOp(Invert, expression, LINENO(n),
1630 n->n_col_offset, c->c_arena);
1632 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
1633 TYPE(CHILD(n, 0)));
1634 return NULL;
1637 static expr_ty
1638 ast_for_power(struct compiling *c, const node *n)
1640 /* power: atom trailer* ('**' factor)*
1642 int i;
1643 expr_ty e, tmp;
1644 REQ(n, power);
1645 e = ast_for_atom(c, CHILD(n, 0));
1646 if (!e)
1647 return NULL;
1648 if (NCH(n) == 1)
1649 return e;
1650 for (i = 1; i < NCH(n); i++) {
1651 node *ch = CHILD(n, i);
1652 if (TYPE(ch) != trailer)
1653 break;
1654 tmp = ast_for_trailer(c, ch, e);
1655 if (!tmp)
1656 return NULL;
1657 tmp->lineno = e->lineno;
1658 tmp->col_offset = e->col_offset;
1659 e = tmp;
1661 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
1662 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
1663 if (!f)
1664 return NULL;
1665 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
1666 if (!tmp)
1667 return NULL;
1668 e = tmp;
1670 return e;
1673 /* Do not name a variable 'expr'! Will cause a compile error.
1676 static expr_ty
1677 ast_for_expr(struct compiling *c, const node *n)
1679 /* handle the full range of simple expressions
1680 test: or_test ['if' or_test 'else' test] | lambdef
1681 or_test: and_test ('or' and_test)*
1682 and_test: not_test ('and' not_test)*
1683 not_test: 'not' not_test | comparison
1684 comparison: expr (comp_op expr)*
1685 expr: xor_expr ('|' xor_expr)*
1686 xor_expr: and_expr ('^' and_expr)*
1687 and_expr: shift_expr ('&' shift_expr)*
1688 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1689 arith_expr: term (('+'|'-') term)*
1690 term: factor (('*'|'/'|'%'|'//') factor)*
1691 factor: ('+'|'-'|'~') factor | power
1692 power: atom trailer* ('**' factor)*
1694 As well as modified versions that exist for backward compatibility,
1695 to explicitly allow:
1696 [ x for x in lambda: 0, lambda: 1 ]
1697 (which would be ambiguous without these extra rules)
1699 old_test: or_test | old_lambdef
1700 old_lambdef: 'lambda' [vararglist] ':' old_test
1704 asdl_seq *seq;
1705 int i;
1707 loop:
1708 switch (TYPE(n)) {
1709 case test:
1710 case old_test:
1711 if (TYPE(CHILD(n, 0)) == lambdef ||
1712 TYPE(CHILD(n, 0)) == old_lambdef)
1713 return ast_for_lambdef(c, CHILD(n, 0));
1714 else if (NCH(n) > 1)
1715 return ast_for_ifexpr(c, n);
1716 /* Fallthrough */
1717 case or_test:
1718 case and_test:
1719 if (NCH(n) == 1) {
1720 n = CHILD(n, 0);
1721 goto loop;
1723 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
1724 if (!seq)
1725 return NULL;
1726 for (i = 0; i < NCH(n); i += 2) {
1727 expr_ty e = ast_for_expr(c, CHILD(n, i));
1728 if (!e)
1729 return NULL;
1730 asdl_seq_SET(seq, i / 2, e);
1732 if (!strcmp(STR(CHILD(n, 1)), "and"))
1733 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
1734 c->c_arena);
1735 assert(!strcmp(STR(CHILD(n, 1)), "or"));
1736 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
1737 case not_test:
1738 if (NCH(n) == 1) {
1739 n = CHILD(n, 0);
1740 goto loop;
1742 else {
1743 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1744 if (!expression)
1745 return NULL;
1747 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
1748 c->c_arena);
1750 case comparison:
1751 if (NCH(n) == 1) {
1752 n = CHILD(n, 0);
1753 goto loop;
1755 else {
1756 expr_ty expression;
1757 asdl_int_seq *ops;
1758 asdl_seq *cmps;
1759 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
1760 if (!ops)
1761 return NULL;
1762 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
1763 if (!cmps) {
1764 return NULL;
1766 for (i = 1; i < NCH(n); i += 2) {
1767 cmpop_ty newoperator;
1769 newoperator = ast_for_comp_op(CHILD(n, i));
1770 if (!newoperator) {
1771 return NULL;
1774 expression = ast_for_expr(c, CHILD(n, i + 1));
1775 if (!expression) {
1776 return NULL;
1779 asdl_seq_SET(ops, i / 2, newoperator);
1780 asdl_seq_SET(cmps, i / 2, expression);
1782 expression = ast_for_expr(c, CHILD(n, 0));
1783 if (!expression) {
1784 return NULL;
1787 return Compare(expression, ops, cmps, LINENO(n),
1788 n->n_col_offset, c->c_arena);
1790 break;
1792 /* The next five cases all handle BinOps. The main body of code
1793 is the same in each case, but the switch turned inside out to
1794 reuse the code for each type of operator.
1796 case expr:
1797 case xor_expr:
1798 case and_expr:
1799 case shift_expr:
1800 case arith_expr:
1801 case term:
1802 if (NCH(n) == 1) {
1803 n = CHILD(n, 0);
1804 goto loop;
1806 return ast_for_binop(c, n);
1807 case yield_expr: {
1808 expr_ty exp = NULL;
1809 if (NCH(n) == 2) {
1810 exp = ast_for_testlist(c, CHILD(n, 1));
1811 if (!exp)
1812 return NULL;
1814 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
1816 case factor:
1817 if (NCH(n) == 1) {
1818 n = CHILD(n, 0);
1819 goto loop;
1821 return ast_for_factor(c, n);
1822 case power:
1823 return ast_for_power(c, n);
1824 default:
1825 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1826 return NULL;
1828 /* should never get here unless if error is set */
1829 return NULL;
1832 static expr_ty
1833 ast_for_call(struct compiling *c, const node *n, expr_ty func)
1836 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1837 | '**' test)
1838 argument: [test '='] test [gen_for] # Really [keyword '='] test
1841 int i, nargs, nkeywords, ngens;
1842 asdl_seq *args;
1843 asdl_seq *keywords;
1844 expr_ty vararg = NULL, kwarg = NULL;
1846 REQ(n, arglist);
1848 nargs = 0;
1849 nkeywords = 0;
1850 ngens = 0;
1851 for (i = 0; i < NCH(n); i++) {
1852 node *ch = CHILD(n, i);
1853 if (TYPE(ch) == argument) {
1854 if (NCH(ch) == 1)
1855 nargs++;
1856 else if (TYPE(CHILD(ch, 1)) == gen_for)
1857 ngens++;
1858 else
1859 nkeywords++;
1862 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
1863 ast_error(n, "Generator expression must be parenthesized "
1864 "if not sole argument");
1865 return NULL;
1868 if (nargs + nkeywords + ngens > 255) {
1869 ast_error(n, "more than 255 arguments");
1870 return NULL;
1873 args = asdl_seq_new(nargs + ngens, c->c_arena);
1874 if (!args)
1875 return NULL;
1876 keywords = asdl_seq_new(nkeywords, c->c_arena);
1877 if (!keywords)
1878 return NULL;
1879 nargs = 0;
1880 nkeywords = 0;
1881 for (i = 0; i < NCH(n); i++) {
1882 node *ch = CHILD(n, i);
1883 if (TYPE(ch) == argument) {
1884 expr_ty e;
1885 if (NCH(ch) == 1) {
1886 if (nkeywords) {
1887 ast_error(CHILD(ch, 0),
1888 "non-keyword arg after keyword arg");
1889 return NULL;
1891 e = ast_for_expr(c, CHILD(ch, 0));
1892 if (!e)
1893 return NULL;
1894 asdl_seq_SET(args, nargs++, e);
1896 else if (TYPE(CHILD(ch, 1)) == gen_for) {
1897 e = ast_for_genexp(c, ch);
1898 if (!e)
1899 return NULL;
1900 asdl_seq_SET(args, nargs++, e);
1902 else {
1903 keyword_ty kw;
1904 identifier key;
1906 /* CHILD(ch, 0) is test, but must be an identifier? */
1907 e = ast_for_expr(c, CHILD(ch, 0));
1908 if (!e)
1909 return NULL;
1910 /* f(lambda x: x[0] = 3) ends up getting parsed with
1911 * LHS test = lambda x: x[0], and RHS test = 3.
1912 * SF bug 132313 points out that complaining about a keyword
1913 * then is very confusing.
1915 if (e->kind == Lambda_kind) {
1916 ast_error(CHILD(ch, 0),
1917 "lambda cannot contain assignment");
1918 return NULL;
1919 } else if (e->kind != Name_kind) {
1920 ast_error(CHILD(ch, 0), "keyword can't be an expression");
1921 return NULL;
1923 key = e->v.Name.id;
1924 if (!strcmp(PyString_AS_STRING(key), "None")) {
1925 ast_error(CHILD(ch, 0), "assignment to None");
1926 return NULL;
1928 e = ast_for_expr(c, CHILD(ch, 2));
1929 if (!e)
1930 return NULL;
1931 kw = keyword(key, e, c->c_arena);
1932 if (!kw)
1933 return NULL;
1934 asdl_seq_SET(keywords, nkeywords++, kw);
1937 else if (TYPE(ch) == STAR) {
1938 vararg = ast_for_expr(c, CHILD(n, i+1));
1939 if (!vararg)
1940 return NULL;
1941 i++;
1943 else if (TYPE(ch) == DOUBLESTAR) {
1944 kwarg = ast_for_expr(c, CHILD(n, i+1));
1945 if (!kwarg)
1946 return NULL;
1947 i++;
1951 return Call(func, args, keywords, vararg, kwarg, func->lineno,
1952 func->col_offset, c->c_arena);
1955 static expr_ty
1956 ast_for_testlist(struct compiling *c, const node* n)
1958 /* testlist_gexp: test (',' test)* [','] */
1959 /* testlist: test (',' test)* [','] */
1960 /* testlist_safe: test (',' test)+ [','] */
1961 /* testlist1: test (',' test)* */
1962 assert(NCH(n) > 0);
1963 if (TYPE(n) == testlist_gexp) {
1964 if (NCH(n) > 1)
1965 assert(TYPE(CHILD(n, 1)) != gen_for);
1967 else {
1968 assert(TYPE(n) == testlist ||
1969 TYPE(n) == testlist_safe ||
1970 TYPE(n) == testlist1);
1972 if (NCH(n) == 1)
1973 return ast_for_expr(c, CHILD(n, 0));
1974 else {
1975 asdl_seq *tmp = seq_for_testlist(c, n);
1976 if (!tmp)
1977 return NULL;
1978 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
1982 static expr_ty
1983 ast_for_testlist_gexp(struct compiling *c, const node* n)
1985 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
1986 /* argument: test [ gen_for ] */
1987 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
1988 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
1989 return ast_for_genexp(c, n);
1990 return ast_for_testlist(c, n);
1993 /* like ast_for_testlist() but returns a sequence */
1994 static asdl_seq*
1995 ast_for_class_bases(struct compiling *c, const node* n)
1997 /* testlist: test (',' test)* [','] */
1998 assert(NCH(n) > 0);
1999 REQ(n, testlist);
2000 if (NCH(n) == 1) {
2001 expr_ty base;
2002 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
2003 if (!bases)
2004 return NULL;
2005 base = ast_for_expr(c, CHILD(n, 0));
2006 if (!base)
2007 return NULL;
2008 asdl_seq_SET(bases, 0, base);
2009 return bases;
2012 return seq_for_testlist(c, n);
2015 static stmt_ty
2016 ast_for_expr_stmt(struct compiling *c, const node *n)
2018 REQ(n, expr_stmt);
2019 /* expr_stmt: testlist (augassign (yield_expr|testlist)
2020 | ('=' (yield_expr|testlist))*)
2021 testlist: test (',' test)* [',']
2022 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
2023 | '<<=' | '>>=' | '**=' | '//='
2024 test: ... here starts the operator precendence dance
2027 if (NCH(n) == 1) {
2028 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
2029 if (!e)
2030 return NULL;
2032 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
2034 else if (TYPE(CHILD(n, 1)) == augassign) {
2035 expr_ty expr1, expr2;
2036 operator_ty newoperator;
2037 node *ch = CHILD(n, 0);
2039 expr1 = ast_for_testlist(c, ch);
2040 if (!expr1)
2041 return NULL;
2042 /* TODO(nas): Remove duplicated error checks (set_context does it) */
2043 switch (expr1->kind) {
2044 case GeneratorExp_kind:
2045 ast_error(ch, "augmented assignment to generator "
2046 "expression not possible");
2047 return NULL;
2048 case Yield_kind:
2049 ast_error(ch, "augmented assignment to yield "
2050 "expression not possible");
2051 return NULL;
2052 case Name_kind: {
2053 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2054 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2055 ast_error(ch, "assignment to None");
2056 return NULL;
2058 break;
2060 case Attribute_kind:
2061 case Subscript_kind:
2062 break;
2063 default:
2064 ast_error(ch, "illegal expression for augmented "
2065 "assignment");
2066 return NULL;
2068 if(!set_context(expr1, Store, ch))
2069 return NULL;
2071 ch = CHILD(n, 2);
2072 if (TYPE(ch) == testlist)
2073 expr2 = ast_for_testlist(c, ch);
2074 else
2075 expr2 = ast_for_expr(c, ch);
2076 if (!expr2)
2077 return NULL;
2079 newoperator = ast_for_augassign(CHILD(n, 1));
2080 if (!newoperator)
2081 return NULL;
2083 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2084 c->c_arena);
2086 else {
2087 int i;
2088 asdl_seq *targets;
2089 node *value;
2090 expr_ty expression;
2092 /* a normal assignment */
2093 REQ(CHILD(n, 1), EQUAL);
2094 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
2095 if (!targets)
2096 return NULL;
2097 for (i = 0; i < NCH(n) - 2; i += 2) {
2098 expr_ty e;
2099 node *ch = CHILD(n, i);
2100 if (TYPE(ch) == yield_expr) {
2101 ast_error(ch, "assignment to yield expression not possible");
2102 return NULL;
2104 e = ast_for_testlist(c, ch);
2106 /* set context to assign */
2107 if (!e)
2108 return NULL;
2110 if (!set_context(e, Store, CHILD(n, i)))
2111 return NULL;
2113 asdl_seq_SET(targets, i / 2, e);
2115 value = CHILD(n, NCH(n) - 1);
2116 if (TYPE(value) == testlist)
2117 expression = ast_for_testlist(c, value);
2118 else
2119 expression = ast_for_expr(c, value);
2120 if (!expression)
2121 return NULL;
2122 return Assign(targets, expression, LINENO(n), n->n_col_offset,
2123 c->c_arena);
2127 static stmt_ty
2128 ast_for_print_stmt(struct compiling *c, const node *n)
2130 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
2131 | '>>' test [ (',' test)+ [','] ] )
2133 expr_ty dest = NULL, expression;
2134 asdl_seq *seq;
2135 bool nl;
2136 int i, j, start = 1;
2138 REQ(n, print_stmt);
2139 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2140 dest = ast_for_expr(c, CHILD(n, 2));
2141 if (!dest)
2142 return NULL;
2143 start = 4;
2145 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
2146 if (!seq)
2147 return NULL;
2148 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
2149 expression = ast_for_expr(c, CHILD(n, i));
2150 if (!expression)
2151 return NULL;
2152 asdl_seq_SET(seq, j, expression);
2154 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
2155 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
2158 static asdl_seq *
2159 ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
2161 asdl_seq *seq;
2162 int i;
2163 expr_ty e;
2165 REQ(n, exprlist);
2167 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2168 if (!seq)
2169 return NULL;
2170 for (i = 0; i < NCH(n); i += 2) {
2171 e = ast_for_expr(c, CHILD(n, i));
2172 if (!e)
2173 return NULL;
2174 asdl_seq_SET(seq, i / 2, e);
2175 if (context && !set_context(e, context, CHILD(n, i)))
2176 return NULL;
2178 return seq;
2181 static stmt_ty
2182 ast_for_del_stmt(struct compiling *c, const node *n)
2184 asdl_seq *expr_list;
2186 /* del_stmt: 'del' exprlist */
2187 REQ(n, del_stmt);
2189 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
2190 if (!expr_list)
2191 return NULL;
2192 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
2195 static stmt_ty
2196 ast_for_flow_stmt(struct compiling *c, const node *n)
2199 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
2200 | yield_stmt
2201 break_stmt: 'break'
2202 continue_stmt: 'continue'
2203 return_stmt: 'return' [testlist]
2204 yield_stmt: yield_expr
2205 yield_expr: 'yield' testlist
2206 raise_stmt: 'raise' [test [',' test [',' test]]]
2208 node *ch;
2210 REQ(n, flow_stmt);
2211 ch = CHILD(n, 0);
2212 switch (TYPE(ch)) {
2213 case break_stmt:
2214 return Break(LINENO(n), n->n_col_offset, c->c_arena);
2215 case continue_stmt:
2216 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
2217 case yield_stmt: { /* will reduce to yield_expr */
2218 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
2219 if (!exp)
2220 return NULL;
2221 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
2223 case return_stmt:
2224 if (NCH(ch) == 1)
2225 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
2226 else {
2227 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
2228 if (!expression)
2229 return NULL;
2230 return Return(expression, LINENO(n), n->n_col_offset,
2231 c->c_arena);
2233 case raise_stmt:
2234 if (NCH(ch) == 1)
2235 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
2236 c->c_arena);
2237 else if (NCH(ch) == 2) {
2238 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
2239 if (!expression)
2240 return NULL;
2241 return Raise(expression, NULL, NULL, LINENO(n),
2242 n->n_col_offset, c->c_arena);
2244 else if (NCH(ch) == 4) {
2245 expr_ty expr1, expr2;
2247 expr1 = ast_for_expr(c, CHILD(ch, 1));
2248 if (!expr1)
2249 return NULL;
2250 expr2 = ast_for_expr(c, CHILD(ch, 3));
2251 if (!expr2)
2252 return NULL;
2254 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
2255 c->c_arena);
2257 else if (NCH(ch) == 6) {
2258 expr_ty expr1, expr2, expr3;
2260 expr1 = ast_for_expr(c, CHILD(ch, 1));
2261 if (!expr1)
2262 return NULL;
2263 expr2 = ast_for_expr(c, CHILD(ch, 3));
2264 if (!expr2)
2265 return NULL;
2266 expr3 = ast_for_expr(c, CHILD(ch, 5));
2267 if (!expr3)
2268 return NULL;
2270 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
2271 c->c_arena);
2273 default:
2274 PyErr_Format(PyExc_SystemError,
2275 "unexpected flow_stmt: %d", TYPE(ch));
2276 return NULL;
2279 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
2280 return NULL;
2283 static alias_ty
2284 alias_for_import_name(struct compiling *c, const node *n)
2287 import_as_name: NAME ['as' NAME]
2288 dotted_as_name: dotted_name ['as' NAME]
2289 dotted_name: NAME ('.' NAME)*
2291 PyObject *str;
2293 loop:
2294 switch (TYPE(n)) {
2295 case import_as_name:
2296 str = NULL;
2297 if (NCH(n) == 3) {
2298 str = NEW_IDENTIFIER(CHILD(n, 2));
2300 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
2301 case dotted_as_name:
2302 if (NCH(n) == 1) {
2303 n = CHILD(n, 0);
2304 goto loop;
2306 else {
2307 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
2308 if (!a)
2309 return NULL;
2310 assert(!a->asname);
2311 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
2312 return a;
2314 break;
2315 case dotted_name:
2316 if (NCH(n) == 1)
2317 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
2318 else {
2319 /* Create a string of the form "a.b.c" */
2320 int i;
2321 size_t len;
2322 char *s;
2324 len = 0;
2325 for (i = 0; i < NCH(n); i += 2)
2326 /* length of string plus one for the dot */
2327 len += strlen(STR(CHILD(n, i))) + 1;
2328 len--; /* the last name doesn't have a dot */
2329 str = PyString_FromStringAndSize(NULL, len);
2330 if (!str)
2331 return NULL;
2332 s = PyString_AS_STRING(str);
2333 if (!s)
2334 return NULL;
2335 for (i = 0; i < NCH(n); i += 2) {
2336 char *sch = STR(CHILD(n, i));
2337 strcpy(s, STR(CHILD(n, i)));
2338 s += strlen(sch);
2339 *s++ = '.';
2341 --s;
2342 *s = '\0';
2343 PyString_InternInPlace(&str);
2344 PyArena_AddPyObject(c->c_arena, str);
2345 return alias(str, NULL, c->c_arena);
2347 break;
2348 case STAR:
2349 str = PyString_InternFromString("*");
2350 PyArena_AddPyObject(c->c_arena, str);
2351 return alias(str, NULL, c->c_arena);
2352 default:
2353 PyErr_Format(PyExc_SystemError,
2354 "unexpected import name: %d", TYPE(n));
2355 return NULL;
2358 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
2359 return NULL;
2362 static stmt_ty
2363 ast_for_import_stmt(struct compiling *c, const node *n)
2366 import_stmt: import_name | import_from
2367 import_name: 'import' dotted_as_names
2368 import_from: 'from' ('.'* dotted_name | '.') 'import'
2369 ('*' | '(' import_as_names ')' | import_as_names)
2371 int lineno;
2372 int col_offset;
2373 int i;
2374 asdl_seq *aliases;
2376 REQ(n, import_stmt);
2377 lineno = LINENO(n);
2378 col_offset = n->n_col_offset;
2379 n = CHILD(n, 0);
2380 if (TYPE(n) == import_name) {
2381 n = CHILD(n, 1);
2382 REQ(n, dotted_as_names);
2383 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2384 if (!aliases)
2385 return NULL;
2386 for (i = 0; i < NCH(n); i += 2) {
2387 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2388 if (!import_alias)
2389 return NULL;
2390 asdl_seq_SET(aliases, i / 2, import_alias);
2392 return Import(aliases, lineno, col_offset, c->c_arena);
2394 else if (TYPE(n) == import_from) {
2395 int n_children;
2396 int idx, ndots = 0;
2397 alias_ty mod = NULL;
2398 identifier modname;
2400 /* Count the number of dots (for relative imports) and check for the
2401 optional module name */
2402 for (idx = 1; idx < NCH(n); idx++) {
2403 if (TYPE(CHILD(n, idx)) == dotted_name) {
2404 mod = alias_for_import_name(c, CHILD(n, idx));
2405 idx++;
2406 break;
2407 } else if (TYPE(CHILD(n, idx)) != DOT) {
2408 break;
2410 ndots++;
2412 idx++; /* skip over the 'import' keyword */
2413 switch (TYPE(CHILD(n, idx))) {
2414 case STAR:
2415 /* from ... import * */
2416 n = CHILD(n, idx);
2417 n_children = 1;
2418 break;
2419 case LPAR:
2420 /* from ... import (x, y, z) */
2421 n = CHILD(n, idx + 1);
2422 n_children = NCH(n);
2423 break;
2424 case import_as_names:
2425 /* from ... import x, y, z */
2426 n = CHILD(n, idx);
2427 n_children = NCH(n);
2428 if (n_children % 2 == 0) {
2429 ast_error(n, "trailing comma not allowed without"
2430 " surrounding parentheses");
2431 return NULL;
2433 break;
2434 default:
2435 ast_error(n, "Unexpected node-type in from-import");
2436 return NULL;
2439 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
2440 if (!aliases)
2441 return NULL;
2443 /* handle "from ... import *" special b/c there's no children */
2444 if (TYPE(n) == STAR) {
2445 alias_ty import_alias = alias_for_import_name(c, n);
2446 if (!import_alias)
2447 return NULL;
2448 asdl_seq_SET(aliases, 0, import_alias);
2450 else {
2451 for (i = 0; i < NCH(n); i += 2) {
2452 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
2453 if (!import_alias)
2454 return NULL;
2455 asdl_seq_SET(aliases, i / 2, import_alias);
2458 if (mod != NULL)
2459 modname = mod->name;
2460 else
2461 modname = new_identifier("", c->c_arena);
2462 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
2463 c->c_arena);
2465 PyErr_Format(PyExc_SystemError,
2466 "unknown import statement: starts with command '%s'",
2467 STR(CHILD(n, 0)));
2468 return NULL;
2471 static stmt_ty
2472 ast_for_global_stmt(struct compiling *c, const node *n)
2474 /* global_stmt: 'global' NAME (',' NAME)* */
2475 identifier name;
2476 asdl_seq *s;
2477 int i;
2479 REQ(n, global_stmt);
2480 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
2481 if (!s)
2482 return NULL;
2483 for (i = 1; i < NCH(n); i += 2) {
2484 name = NEW_IDENTIFIER(CHILD(n, i));
2485 if (!name)
2486 return NULL;
2487 asdl_seq_SET(s, i / 2, name);
2489 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
2492 static stmt_ty
2493 ast_for_exec_stmt(struct compiling *c, const node *n)
2495 expr_ty expr1, globals = NULL, locals = NULL;
2496 int n_children = NCH(n);
2497 if (n_children != 2 && n_children != 4 && n_children != 6) {
2498 PyErr_Format(PyExc_SystemError,
2499 "poorly formed 'exec' statement: %d parts to statement",
2500 n_children);
2501 return NULL;
2504 /* exec_stmt: 'exec' expr ['in' test [',' test]] */
2505 REQ(n, exec_stmt);
2506 expr1 = ast_for_expr(c, CHILD(n, 1));
2507 if (!expr1)
2508 return NULL;
2509 if (n_children >= 4) {
2510 globals = ast_for_expr(c, CHILD(n, 3));
2511 if (!globals)
2512 return NULL;
2514 if (n_children == 6) {
2515 locals = ast_for_expr(c, CHILD(n, 5));
2516 if (!locals)
2517 return NULL;
2520 return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
2521 c->c_arena);
2524 static stmt_ty
2525 ast_for_assert_stmt(struct compiling *c, const node *n)
2527 /* assert_stmt: 'assert' test [',' test] */
2528 REQ(n, assert_stmt);
2529 if (NCH(n) == 2) {
2530 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2531 if (!expression)
2532 return NULL;
2533 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
2534 c->c_arena);
2536 else if (NCH(n) == 4) {
2537 expr_ty expr1, expr2;
2539 expr1 = ast_for_expr(c, CHILD(n, 1));
2540 if (!expr1)
2541 return NULL;
2542 expr2 = ast_for_expr(c, CHILD(n, 3));
2543 if (!expr2)
2544 return NULL;
2546 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
2548 PyErr_Format(PyExc_SystemError,
2549 "improper number of parts to 'assert' statement: %d",
2550 NCH(n));
2551 return NULL;
2554 static asdl_seq *
2555 ast_for_suite(struct compiling *c, const node *n)
2557 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
2558 asdl_seq *seq;
2559 stmt_ty s;
2560 int i, total, num, end, pos = 0;
2561 node *ch;
2563 REQ(n, suite);
2565 total = num_stmts(n);
2566 seq = asdl_seq_new(total, c->c_arena);
2567 if (!seq)
2568 return NULL;
2569 if (TYPE(CHILD(n, 0)) == simple_stmt) {
2570 n = CHILD(n, 0);
2571 /* simple_stmt always ends with a NEWLINE,
2572 and may have a trailing SEMI
2574 end = NCH(n) - 1;
2575 if (TYPE(CHILD(n, end - 1)) == SEMI)
2576 end--;
2577 /* loop by 2 to skip semi-colons */
2578 for (i = 0; i < end; i += 2) {
2579 ch = CHILD(n, i);
2580 s = ast_for_stmt(c, ch);
2581 if (!s)
2582 return NULL;
2583 asdl_seq_SET(seq, pos++, s);
2586 else {
2587 for (i = 2; i < (NCH(n) - 1); i++) {
2588 ch = CHILD(n, i);
2589 REQ(ch, stmt);
2590 num = num_stmts(ch);
2591 if (num == 1) {
2592 /* small_stmt or compound_stmt with only one child */
2593 s = ast_for_stmt(c, ch);
2594 if (!s)
2595 return NULL;
2596 asdl_seq_SET(seq, pos++, s);
2598 else {
2599 int j;
2600 ch = CHILD(ch, 0);
2601 REQ(ch, simple_stmt);
2602 for (j = 0; j < NCH(ch); j += 2) {
2603 /* statement terminates with a semi-colon ';' */
2604 if (NCH(CHILD(ch, j)) == 0) {
2605 assert((j + 1) == NCH(ch));
2606 break;
2608 s = ast_for_stmt(c, CHILD(ch, j));
2609 if (!s)
2610 return NULL;
2611 asdl_seq_SET(seq, pos++, s);
2616 assert(pos == seq->size);
2617 return seq;
2620 static stmt_ty
2621 ast_for_if_stmt(struct compiling *c, const node *n)
2623 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
2624 ['else' ':' suite]
2626 char *s;
2628 REQ(n, if_stmt);
2630 if (NCH(n) == 4) {
2631 expr_ty expression;
2632 asdl_seq *suite_seq;
2634 expression = ast_for_expr(c, CHILD(n, 1));
2635 if (!expression)
2636 return NULL;
2637 suite_seq = ast_for_suite(c, CHILD(n, 3));
2638 if (!suite_seq)
2639 return NULL;
2641 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2642 c->c_arena);
2645 s = STR(CHILD(n, 4));
2646 /* s[2], the third character in the string, will be
2647 's' for el_s_e, or
2648 'i' for el_i_f
2650 if (s[2] == 's') {
2651 expr_ty expression;
2652 asdl_seq *seq1, *seq2;
2654 expression = ast_for_expr(c, CHILD(n, 1));
2655 if (!expression)
2656 return NULL;
2657 seq1 = ast_for_suite(c, CHILD(n, 3));
2658 if (!seq1)
2659 return NULL;
2660 seq2 = ast_for_suite(c, CHILD(n, 6));
2661 if (!seq2)
2662 return NULL;
2664 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2665 c->c_arena);
2667 else if (s[2] == 'i') {
2668 int i, n_elif, has_else = 0;
2669 expr_ty expression;
2670 asdl_seq *suite_seq;
2671 asdl_seq *orelse = NULL;
2672 n_elif = NCH(n) - 4;
2673 /* must reference the child n_elif+1 since 'else' token is third,
2674 not fourth, child from the end. */
2675 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
2676 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
2677 has_else = 1;
2678 n_elif -= 3;
2680 n_elif /= 4;
2682 if (has_else) {
2683 asdl_seq *suite_seq2;
2685 orelse = asdl_seq_new(1, c->c_arena);
2686 if (!orelse)
2687 return NULL;
2688 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
2689 if (!expression)
2690 return NULL;
2691 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
2692 if (!suite_seq)
2693 return NULL;
2694 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
2695 if (!suite_seq2)
2696 return NULL;
2698 asdl_seq_SET(orelse, 0,
2699 If(expression, suite_seq, suite_seq2,
2700 LINENO(CHILD(n, NCH(n) - 6)),
2701 CHILD(n, NCH(n) - 6)->n_col_offset,
2702 c->c_arena));
2703 /* the just-created orelse handled the last elif */
2704 n_elif--;
2707 for (i = 0; i < n_elif; i++) {
2708 int off = 5 + (n_elif - i - 1) * 4;
2709 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
2710 if (!newobj)
2711 return NULL;
2712 expression = ast_for_expr(c, CHILD(n, off));
2713 if (!expression)
2714 return NULL;
2715 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
2716 if (!suite_seq)
2717 return NULL;
2719 asdl_seq_SET(newobj, 0,
2720 If(expression, suite_seq, orelse,
2721 LINENO(CHILD(n, off)),
2722 CHILD(n, off)->n_col_offset, c->c_arena));
2723 orelse = newobj;
2725 expression = ast_for_expr(c, CHILD(n, 1));
2726 if (!expression)
2727 return NULL;
2728 suite_seq = ast_for_suite(c, CHILD(n, 3));
2729 if (!suite_seq)
2730 return NULL;
2731 return If(expression, suite_seq, orelse,
2732 LINENO(n), n->n_col_offset, c->c_arena);
2735 PyErr_Format(PyExc_SystemError,
2736 "unexpected token in 'if' statement: %s", s);
2737 return NULL;
2740 static stmt_ty
2741 ast_for_while_stmt(struct compiling *c, const node *n)
2743 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
2744 REQ(n, while_stmt);
2746 if (NCH(n) == 4) {
2747 expr_ty expression;
2748 asdl_seq *suite_seq;
2750 expression = ast_for_expr(c, CHILD(n, 1));
2751 if (!expression)
2752 return NULL;
2753 suite_seq = ast_for_suite(c, CHILD(n, 3));
2754 if (!suite_seq)
2755 return NULL;
2756 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
2757 c->c_arena);
2759 else if (NCH(n) == 7) {
2760 expr_ty expression;
2761 asdl_seq *seq1, *seq2;
2763 expression = ast_for_expr(c, CHILD(n, 1));
2764 if (!expression)
2765 return NULL;
2766 seq1 = ast_for_suite(c, CHILD(n, 3));
2767 if (!seq1)
2768 return NULL;
2769 seq2 = ast_for_suite(c, CHILD(n, 6));
2770 if (!seq2)
2771 return NULL;
2773 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
2774 c->c_arena);
2777 PyErr_Format(PyExc_SystemError,
2778 "wrong number of tokens for 'while' statement: %d",
2779 NCH(n));
2780 return NULL;
2783 static stmt_ty
2784 ast_for_for_stmt(struct compiling *c, const node *n)
2786 asdl_seq *_target, *seq = NULL, *suite_seq;
2787 expr_ty expression;
2788 expr_ty target;
2789 const node *node_target;
2790 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
2791 REQ(n, for_stmt);
2793 if (NCH(n) == 9) {
2794 seq = ast_for_suite(c, CHILD(n, 8));
2795 if (!seq)
2796 return NULL;
2799 node_target = CHILD(n, 1);
2800 _target = ast_for_exprlist(c, node_target, Store);
2801 if (!_target)
2802 return NULL;
2803 /* Check the # of children rather than the length of _target, since
2804 for x, in ... has 1 element in _target, but still requires a Tuple. */
2805 if (NCH(node_target) == 1)
2806 target = (expr_ty)asdl_seq_GET(_target, 0);
2807 else
2808 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
2810 expression = ast_for_testlist(c, CHILD(n, 3));
2811 if (!expression)
2812 return NULL;
2813 suite_seq = ast_for_suite(c, CHILD(n, 5));
2814 if (!suite_seq)
2815 return NULL;
2817 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
2818 c->c_arena);
2821 static excepthandler_ty
2822 ast_for_except_clause(struct compiling *c, const node *exc, node *body)
2824 /* except_clause: 'except' [test [(',' | 'as') test]] */
2825 REQ(exc, except_clause);
2826 REQ(body, suite);
2828 if (NCH(exc) == 1) {
2829 asdl_seq *suite_seq = ast_for_suite(c, body);
2830 if (!suite_seq)
2831 return NULL;
2833 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
2834 exc->n_col_offset, c->c_arena);
2836 else if (NCH(exc) == 2) {
2837 expr_ty expression;
2838 asdl_seq *suite_seq;
2840 expression = ast_for_expr(c, CHILD(exc, 1));
2841 if (!expression)
2842 return NULL;
2843 suite_seq = ast_for_suite(c, body);
2844 if (!suite_seq)
2845 return NULL;
2847 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
2848 exc->n_col_offset, c->c_arena);
2850 else if (NCH(exc) == 4) {
2851 asdl_seq *suite_seq;
2852 expr_ty expression;
2853 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
2854 if (!e)
2855 return NULL;
2856 if (!set_context(e, Store, CHILD(exc, 3)))
2857 return NULL;
2858 expression = ast_for_expr(c, CHILD(exc, 1));
2859 if (!expression)
2860 return NULL;
2861 suite_seq = ast_for_suite(c, body);
2862 if (!suite_seq)
2863 return NULL;
2865 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
2866 exc->n_col_offset, c->c_arena);
2869 PyErr_Format(PyExc_SystemError,
2870 "wrong number of children for 'except' clause: %d",
2871 NCH(exc));
2872 return NULL;
2875 static stmt_ty
2876 ast_for_try_stmt(struct compiling *c, const node *n)
2878 const int nch = NCH(n);
2879 int n_except = (nch - 3)/3;
2880 asdl_seq *body, *orelse = NULL, *finally = NULL;
2882 REQ(n, try_stmt);
2884 body = ast_for_suite(c, CHILD(n, 2));
2885 if (body == NULL)
2886 return NULL;
2888 if (TYPE(CHILD(n, nch - 3)) == NAME) {
2889 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
2890 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
2891 /* we can assume it's an "else",
2892 because nch >= 9 for try-else-finally and
2893 it would otherwise have a type of except_clause */
2894 orelse = ast_for_suite(c, CHILD(n, nch - 4));
2895 if (orelse == NULL)
2896 return NULL;
2897 n_except--;
2900 finally = ast_for_suite(c, CHILD(n, nch - 1));
2901 if (finally == NULL)
2902 return NULL;
2903 n_except--;
2905 else {
2906 /* we can assume it's an "else",
2907 otherwise it would have a type of except_clause */
2908 orelse = ast_for_suite(c, CHILD(n, nch - 1));
2909 if (orelse == NULL)
2910 return NULL;
2911 n_except--;
2914 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
2915 ast_error(n, "malformed 'try' statement");
2916 return NULL;
2919 if (n_except > 0) {
2920 int i;
2921 stmt_ty except_st;
2922 /* process except statements to create a try ... except */
2923 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
2924 if (handlers == NULL)
2925 return NULL;
2927 for (i = 0; i < n_except; i++) {
2928 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
2929 CHILD(n, 5 + i * 3));
2930 if (!e)
2931 return NULL;
2932 asdl_seq_SET(handlers, i, e);
2935 except_st = TryExcept(body, handlers, orelse, LINENO(n),
2936 n->n_col_offset, c->c_arena);
2937 if (!finally)
2938 return except_st;
2940 /* if a 'finally' is present too, we nest the TryExcept within a
2941 TryFinally to emulate try ... except ... finally */
2942 body = asdl_seq_new(1, c->c_arena);
2943 if (body == NULL)
2944 return NULL;
2945 asdl_seq_SET(body, 0, except_st);
2948 /* must be a try ... finally (except clauses are in body, if any exist) */
2949 assert(finally != NULL);
2950 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
2953 static expr_ty
2954 ast_for_with_var(struct compiling *c, const node *n)
2956 REQ(n, with_var);
2957 return ast_for_expr(c, CHILD(n, 1));
2960 /* with_stmt: 'with' test [ with_var ] ':' suite */
2961 static stmt_ty
2962 ast_for_with_stmt(struct compiling *c, const node *n)
2964 expr_ty context_expr, optional_vars = NULL;
2965 int suite_index = 3; /* skip 'with', test, and ':' */
2966 asdl_seq *suite_seq;
2968 assert(TYPE(n) == with_stmt);
2969 context_expr = ast_for_expr(c, CHILD(n, 1));
2970 if (!context_expr)
2971 return NULL;
2972 if (TYPE(CHILD(n, 2)) == with_var) {
2973 optional_vars = ast_for_with_var(c, CHILD(n, 2));
2975 if (!optional_vars) {
2976 return NULL;
2978 if (!set_context(optional_vars, Store, n)) {
2979 return NULL;
2981 suite_index = 4;
2984 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
2985 if (!suite_seq) {
2986 return NULL;
2988 return With(context_expr, optional_vars, suite_seq, LINENO(n),
2989 n->n_col_offset, c->c_arena);
2992 static stmt_ty
2993 ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
2995 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2996 asdl_seq *bases, *s;
2998 REQ(n, classdef);
3000 if (!strcmp(STR(CHILD(n, 1)), "None")) {
3001 ast_error(n, "assignment to None");
3002 return NULL;
3005 if (NCH(n) == 4) {
3006 s = ast_for_suite(c, CHILD(n, 3));
3007 if (!s)
3008 return NULL;
3009 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3010 LINENO(n), n->n_col_offset, c->c_arena);
3012 /* check for empty base list */
3013 if (TYPE(CHILD(n,3)) == RPAR) {
3014 s = ast_for_suite(c, CHILD(n,5));
3015 if (!s)
3016 return NULL;
3017 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, decorator_seq,
3018 LINENO(n), n->n_col_offset, c->c_arena);
3021 /* else handle the base class list */
3022 bases = ast_for_class_bases(c, CHILD(n, 3));
3023 if (!bases)
3024 return NULL;
3026 s = ast_for_suite(c, CHILD(n, 6));
3027 if (!s)
3028 return NULL;
3029 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, decorator_seq,
3030 LINENO(n), n->n_col_offset, c->c_arena);
3033 static stmt_ty
3034 ast_for_stmt(struct compiling *c, const node *n)
3036 if (TYPE(n) == stmt) {
3037 assert(NCH(n) == 1);
3038 n = CHILD(n, 0);
3040 if (TYPE(n) == simple_stmt) {
3041 assert(num_stmts(n) == 1);
3042 n = CHILD(n, 0);
3044 if (TYPE(n) == small_stmt) {
3045 REQ(n, small_stmt);
3046 n = CHILD(n, 0);
3047 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
3048 | flow_stmt | import_stmt | global_stmt | exec_stmt
3049 | assert_stmt
3051 switch (TYPE(n)) {
3052 case expr_stmt:
3053 return ast_for_expr_stmt(c, n);
3054 case print_stmt:
3055 return ast_for_print_stmt(c, n);
3056 case del_stmt:
3057 return ast_for_del_stmt(c, n);
3058 case pass_stmt:
3059 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
3060 case flow_stmt:
3061 return ast_for_flow_stmt(c, n);
3062 case import_stmt:
3063 return ast_for_import_stmt(c, n);
3064 case global_stmt:
3065 return ast_for_global_stmt(c, n);
3066 case exec_stmt:
3067 return ast_for_exec_stmt(c, n);
3068 case assert_stmt:
3069 return ast_for_assert_stmt(c, n);
3070 default:
3071 PyErr_Format(PyExc_SystemError,
3072 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3073 TYPE(n), NCH(n));
3074 return NULL;
3077 else {
3078 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
3079 | funcdef | classdef | decorated
3081 node *ch = CHILD(n, 0);
3082 REQ(n, compound_stmt);
3083 switch (TYPE(ch)) {
3084 case if_stmt:
3085 return ast_for_if_stmt(c, ch);
3086 case while_stmt:
3087 return ast_for_while_stmt(c, ch);
3088 case for_stmt:
3089 return ast_for_for_stmt(c, ch);
3090 case try_stmt:
3091 return ast_for_try_stmt(c, ch);
3092 case with_stmt:
3093 return ast_for_with_stmt(c, ch);
3094 case funcdef:
3095 return ast_for_funcdef(c, ch, NULL);
3096 case classdef:
3097 return ast_for_classdef(c, ch, NULL);
3098 case decorated:
3099 return ast_for_decorated(c, ch);
3100 default:
3101 PyErr_Format(PyExc_SystemError,
3102 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3103 TYPE(n), NCH(n));
3104 return NULL;
3109 static PyObject *
3110 parsenumber(const char *s)
3112 const char *end;
3113 long x;
3114 double dx;
3115 #ifndef WITHOUT_COMPLEX
3116 Py_complex c;
3117 int imflag;
3118 #endif
3120 errno = 0;
3121 end = s + strlen(s) - 1;
3122 #ifndef WITHOUT_COMPLEX
3123 imflag = *end == 'j' || *end == 'J';
3124 #endif
3125 if (*end == 'l' || *end == 'L')
3126 return PyLong_FromString((char *)s, (char **)0, 0);
3127 if (s[0] == '0') {
3128 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
3129 if (x < 0 && errno == 0) {
3130 return PyLong_FromString((char *)s,
3131 (char **)0,
3135 else
3136 x = PyOS_strtol((char *)s, (char **)&end, 0);
3137 if (*end == '\0') {
3138 if (errno != 0)
3139 return PyLong_FromString((char *)s, (char **)0, 0);
3140 return PyInt_FromLong(x);
3142 /* XXX Huge floats may silently fail */
3143 #ifndef WITHOUT_COMPLEX
3144 if (imflag) {
3145 c.real = 0.;
3146 PyFPE_START_PROTECT("atof", return 0)
3147 c.imag = PyOS_ascii_atof(s);
3148 PyFPE_END_PROTECT(c)
3149 return PyComplex_FromCComplex(c);
3151 else
3152 #endif
3154 PyFPE_START_PROTECT("atof", return 0)
3155 dx = PyOS_ascii_atof(s);
3156 PyFPE_END_PROTECT(dx)
3157 return PyFloat_FromDouble(dx);
3161 static PyObject *
3162 decode_utf8(const char **sPtr, const char *end, char* encoding)
3164 #ifndef Py_USING_UNICODE
3165 Py_FatalError("decode_utf8 should not be called in this build.");
3166 return NULL;
3167 #else
3168 PyObject *u, *v;
3169 char *s, *t;
3170 t = s = (char *)*sPtr;
3171 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3172 while (s < end && (*s & 0x80)) s++;
3173 *sPtr = s;
3174 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
3175 if (u == NULL)
3176 return NULL;
3177 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3178 Py_DECREF(u);
3179 return v;
3180 #endif
3183 #ifdef Py_USING_UNICODE
3184 static PyObject *
3185 decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
3187 PyObject *v, *u;
3188 char *buf;
3189 char *p;
3190 const char *end;
3191 if (encoding == NULL) {
3192 buf = (char *)s;
3193 u = NULL;
3194 } else if (strcmp(encoding, "iso-8859-1") == 0) {
3195 buf = (char *)s;
3196 u = NULL;
3197 } else {
3198 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
3199 u = PyString_FromStringAndSize((char *)NULL, len * 4);
3200 if (u == NULL)
3201 return NULL;
3202 p = buf = PyString_AsString(u);
3203 end = s + len;
3204 while (s < end) {
3205 if (*s == '\\') {
3206 *p++ = *s++;
3207 if (*s & 0x80) {
3208 strcpy(p, "u005c");
3209 p += 5;
3212 if (*s & 0x80) { /* XXX inefficient */
3213 PyObject *w;
3214 char *r;
3215 Py_ssize_t rn, i;
3216 w = decode_utf8(&s, end, "utf-16-be");
3217 if (w == NULL) {
3218 Py_DECREF(u);
3219 return NULL;
3221 r = PyString_AsString(w);
3222 rn = PyString_Size(w);
3223 assert(rn % 2 == 0);
3224 for (i = 0; i < rn; i += 2) {
3225 sprintf(p, "\\u%02x%02x",
3226 r[i + 0] & 0xFF,
3227 r[i + 1] & 0xFF);
3228 p += 6;
3230 Py_DECREF(w);
3231 } else {
3232 *p++ = *s++;
3235 len = p - buf;
3236 s = buf;
3238 if (rawmode)
3239 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
3240 else
3241 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
3242 Py_XDECREF(u);
3243 return v;
3245 #endif
3247 /* s is a Python string literal, including the bracketing quote characters,
3248 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
3249 * parsestr parses it, and returns the decoded Python string object.
3251 static PyObject *
3252 parsestr(struct compiling *c, const char *s)
3254 size_t len;
3255 int quote = Py_CHARMASK(*s);
3256 int rawmode = 0;
3257 int need_encoding;
3258 int unicode = c->c_future_unicode;
3260 if (isalpha(quote) || quote == '_') {
3261 if (quote == 'u' || quote == 'U') {
3262 quote = *++s;
3263 unicode = 1;
3265 if (quote == 'b' || quote == 'B') {
3266 quote = *++s;
3267 unicode = 0;
3269 if (quote == 'r' || quote == 'R') {
3270 quote = *++s;
3271 rawmode = 1;
3274 if (quote != '\'' && quote != '\"') {
3275 PyErr_BadInternalCall();
3276 return NULL;
3278 s++;
3279 len = strlen(s);
3280 if (len > INT_MAX) {
3281 PyErr_SetString(PyExc_OverflowError,
3282 "string to parse is too long");
3283 return NULL;
3285 if (s[--len] != quote) {
3286 PyErr_BadInternalCall();
3287 return NULL;
3289 if (len >= 4 && s[0] == quote && s[1] == quote) {
3290 s += 2;
3291 len -= 2;
3292 if (s[--len] != quote || s[--len] != quote) {
3293 PyErr_BadInternalCall();
3294 return NULL;
3297 #ifdef Py_USING_UNICODE
3298 if (unicode || Py_UnicodeFlag) {
3299 return decode_unicode(s, len, rawmode, c->c_encoding);
3301 #endif
3302 need_encoding = (c->c_encoding != NULL &&
3303 strcmp(c->c_encoding, "utf-8") != 0 &&
3304 strcmp(c->c_encoding, "iso-8859-1") != 0);
3305 if (rawmode || strchr(s, '\\') == NULL) {
3306 if (need_encoding) {
3307 #ifndef Py_USING_UNICODE
3308 /* This should not happen - we never see any other
3309 encoding. */
3310 Py_FatalError(
3311 "cannot deal with encodings in this build.");
3312 #else
3313 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
3314 if (u == NULL)
3315 return NULL;
3316 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
3317 Py_DECREF(u);
3318 return v;
3319 #endif
3320 } else {
3321 return PyString_FromStringAndSize(s, len);
3325 return PyString_DecodeEscape(s, len, NULL, unicode,
3326 need_encoding ? c->c_encoding : NULL);
3329 /* Build a Python string object out of a STRING atom. This takes care of
3330 * compile-time literal catenation, calling parsestr() on each piece, and
3331 * pasting the intermediate results together.
3333 static PyObject *
3334 parsestrplus(struct compiling *c, const node *n)
3336 PyObject *v;
3337 int i;
3338 REQ(CHILD(n, 0), STRING);
3339 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
3340 /* String literal concatenation */
3341 for (i = 1; i < NCH(n); i++) {
3342 PyObject *s;
3343 s = parsestr(c, STR(CHILD(n, i)));
3344 if (s == NULL)
3345 goto onError;
3346 if (PyString_Check(v) && PyString_Check(s)) {
3347 PyString_ConcatAndDel(&v, s);
3348 if (v == NULL)
3349 goto onError;
3351 #ifdef Py_USING_UNICODE
3352 else {
3353 PyObject *temp = PyUnicode_Concat(v, s);
3354 Py_DECREF(s);
3355 Py_DECREF(v);
3356 v = temp;
3357 if (v == NULL)
3358 goto onError;
3360 #endif
3363 return v;
3365 onError:
3366 Py_XDECREF(v);
3367 return NULL;