LCOV - code coverage report
Current view: top level - Python - ast.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 1037 1843 56.3 %
Date: 2017-04-19 Functions: 57 67 85.1 %

          Line data    Source code
       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().
       4             :  *
       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"
      15             : 
      16             : #include <assert.h>
      17             : 
      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 */
      24             : };
      25             : 
      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_comp(struct compiling *, const node *);
      35             : 
      36             : /* Note different signature for ast_for_call */
      37             : static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
      38             : 
      39             : static PyObject *parsenumber(struct compiling *, const char *);
      40             : static PyObject *parsestr(struct compiling *, const node *n, const char *);
      41             : static PyObject *parsestrplus(struct compiling *, const node *n);
      42             : 
      43             : #ifndef LINENO
      44             : #define LINENO(n)       ((n)->n_lineno)
      45             : #endif
      46             : 
      47             : #define COMP_GENEXP 0
      48             : #define COMP_SETCOMP  1
      49             : 
      50             : static identifier
      51       23533 : new_identifier(const char* n, PyArena *arena) {
      52       23533 :     PyObject* id = PyString_InternFromString(n);
      53       23533 :     if (id != NULL)
      54       23533 :         PyArena_AddPyObject(arena, id);
      55       23533 :     return id;
      56             : }
      57             : 
      58             : #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
      59             : 
      60             : /* This routine provides an invalid object for the syntax error.
      61             :    The outermost routine must unpack this error and create the
      62             :    proper object.  We do this so that we don't have to pass
      63             :    the filename to everything function.
      64             : 
      65             :    XXX Maybe we should just pass the filename...
      66             : */
      67             : 
      68             : static int
      69           0 : ast_error(const node *n, const char *errstr)
      70             : {
      71           0 :     PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
      72           0 :     if (!u)
      73           0 :         return 0;
      74           0 :     PyErr_SetObject(PyExc_SyntaxError, u);
      75           0 :     Py_DECREF(u);
      76           0 :     return 0;
      77             : }
      78             : 
      79             : static void
      80           0 : ast_error_finish(const char *filename)
      81             : {
      82             :     PyObject *type, *value, *tback, *errstr, *loc, *tmp;
      83             :     long lineno;
      84             : 
      85             :     assert(PyErr_Occurred());
      86           0 :     if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
      87           0 :         return;
      88             : 
      89           0 :     PyErr_Fetch(&type, &value, &tback);
      90           0 :     errstr = PyTuple_GetItem(value, 0);
      91           0 :     if (!errstr)
      92           0 :         return;
      93           0 :     Py_INCREF(errstr);
      94           0 :     lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
      95           0 :     if (lineno == -1) {
      96           0 :         Py_DECREF(errstr);
      97           0 :         return;
      98             :     }
      99           0 :     Py_DECREF(value);
     100             : 
     101           0 :     loc = PyErr_ProgramText(filename, lineno);
     102           0 :     if (!loc) {
     103           0 :         Py_INCREF(Py_None);
     104           0 :         loc = Py_None;
     105             :     }
     106           0 :     tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
     107           0 :     Py_DECREF(loc);
     108           0 :     if (!tmp) {
     109           0 :         Py_DECREF(errstr);
     110           0 :         return;
     111             :     }
     112           0 :     value = PyTuple_Pack(2, errstr, tmp);
     113           0 :     Py_DECREF(errstr);
     114           0 :     Py_DECREF(tmp);
     115           0 :     if (!value)
     116           0 :         return;
     117           0 :     PyErr_Restore(type, value, tback);
     118             : }
     119             : 
     120             : static int
     121           0 : ast_warn(struct compiling *c, const node *n, char *msg)
     122             : {
     123           0 :     if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
     124             :                            NULL, NULL) < 0) {
     125             :         /* if -Werr, change it to a SyntaxError */
     126           0 :         if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
     127           0 :             ast_error(n, msg);
     128           0 :         return 0;
     129             :     }
     130           0 :     return 1;
     131             : }
     132             : 
     133             : static int
     134        5870 : forbidden_check(struct compiling *c, const node *n, const char *x)
     135             : {
     136        5870 :     if (!strcmp(x, "None"))
     137           0 :         return ast_error(n, "cannot assign to None");
     138        5870 :     if (!strcmp(x, "__debug__"))
     139           0 :         return ast_error(n, "cannot assign to __debug__");
     140        5870 :     if (Py_Py3kWarningFlag) {
     141           0 :         if (!(strcmp(x, "True") && strcmp(x, "False")) &&
     142           0 :             !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
     143           0 :             return 0;
     144           0 :         if (!strcmp(x, "nonlocal") &&
     145           0 :             !ast_warn(c, n, "nonlocal is a keyword in 3.x"))
     146           0 :             return 0;
     147             :     }
     148        5870 :     return 1;
     149             : }
     150             : 
     151             : /* num_stmts() returns number of contained statements.
     152             : 
     153             :    Use this routine to determine how big a sequence is needed for
     154             :    the statements in a parse tree.  Its raison d'etre is this bit of
     155             :    grammar:
     156             : 
     157             :    stmt: simple_stmt | compound_stmt
     158             :    simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
     159             : 
     160             :    A simple_stmt can contain multiple small_stmt elements joined
     161             :    by semicolons.  If the arg is a simple_stmt, the number of
     162             :    small_stmt elements is returned.
     163             : */
     164             : 
     165             : static int
     166       34244 : num_stmts(const node *n)
     167             : {
     168             :     int i, l;
     169             :     node *ch;
     170             : 
     171       34244 :     switch (TYPE(n)) {
     172             :         case single_input:
     173           0 :             if (TYPE(CHILD(n, 0)) == NEWLINE)
     174           0 :                 return 0;
     175             :             else
     176           0 :                 return num_stmts(CHILD(n, 0));
     177             :         case file_input:
     178          63 :             l = 0;
     179         869 :             for (i = 0; i < NCH(n); i++) {
     180         806 :                 ch = CHILD(n, i);
     181         806 :                 if (TYPE(ch) == stmt)
     182         683 :                     l += num_stmts(ch);
     183             :             }
     184          63 :             return l;
     185             :         case stmt:
     186       15596 :             return num_stmts(CHILD(n, 0));
     187             :         case compound_stmt:
     188        4412 :             return 1;
     189             :         case simple_stmt:
     190       11294 :             return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
     191             :         case suite:
     192        2879 :             if (NCH(n) == 1)
     193         110 :                 return num_stmts(CHILD(n, 0));
     194             :             else {
     195        2769 :                 l = 0;
     196        9884 :                 for (i = 2; i < (NCH(n) - 1); i++)
     197        7115 :                     l += num_stmts(CHILD(n, i));
     198        2769 :                 return l;
     199             :             }
     200             :         default: {
     201             :             char buf[128];
     202             : 
     203           0 :             sprintf(buf, "Non-statement found: %d %d",
     204           0 :                     TYPE(n), NCH(n));
     205           0 :             Py_FatalError(buf);
     206             :         }
     207             :     }
     208             :     assert(0);
     209           0 :     return 0;
     210             : }
     211             : 
     212             : /* Transform the CST rooted at node * to the appropriate AST
     213             : */
     214             : 
     215             : mod_ty
     216          63 : PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
     217             :                PyArena *arena)
     218             : {
     219             :     int i, j, k, num;
     220          63 :     asdl_seq *stmts = NULL;
     221             :     stmt_ty s;
     222             :     node *ch;
     223             :     struct compiling c;
     224             : 
     225          63 :     if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
     226           0 :         c.c_encoding = "utf-8";
     227           0 :         if (TYPE(n) == encoding_decl) {
     228           0 :             ast_error(n, "encoding declaration in Unicode string");
     229           0 :             goto error;
     230             :         }
     231          63 :     } else if (TYPE(n) == encoding_decl) {
     232           0 :         c.c_encoding = STR(n);
     233           0 :         n = CHILD(n, 0);
     234             :     } else {
     235          63 :         c.c_encoding = NULL;
     236             :     }
     237          63 :     c.c_future_unicode = flags && flags->cf_flags & CO_FUTURE_UNICODE_LITERALS;
     238          63 :     c.c_arena = arena;
     239          63 :     c.c_filename = filename;
     240             : 
     241          63 :     k = 0;
     242          63 :     switch (TYPE(n)) {
     243             :         case file_input:
     244          63 :             stmts = asdl_seq_new(num_stmts(n), arena);
     245          63 :             if (!stmts)
     246           0 :                 return NULL;
     247         806 :             for (i = 0; i < NCH(n) - 1; i++) {
     248         743 :                 ch = CHILD(n, i);
     249         743 :                 if (TYPE(ch) == NEWLINE)
     250          60 :                     continue;
     251             :                 REQ(ch, stmt);
     252         683 :                 num = num_stmts(ch);
     253         683 :                 if (num == 1) {
     254         683 :                     s = ast_for_stmt(&c, ch);
     255         683 :                     if (!s)
     256           0 :                         goto error;
     257         683 :                     asdl_seq_SET(stmts, k++, s);
     258             :                 }
     259             :                 else {
     260           0 :                     ch = CHILD(ch, 0);
     261             :                     REQ(ch, simple_stmt);
     262           0 :                     for (j = 0; j < num; j++) {
     263           0 :                         s = ast_for_stmt(&c, CHILD(ch, j * 2));
     264           0 :                         if (!s)
     265           0 :                             goto error;
     266           0 :                         asdl_seq_SET(stmts, k++, s);
     267             :                     }
     268             :                 }
     269             :             }
     270          63 :             return Module(stmts, arena);
     271             :         case eval_input: {
     272             :             expr_ty testlist_ast;
     273             : 
     274             :             /* XXX Why not comp_for here? */
     275           0 :             testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
     276           0 :             if (!testlist_ast)
     277           0 :                 goto error;
     278           0 :             return Expression(testlist_ast, arena);
     279             :         }
     280             :         case single_input:
     281           0 :             if (TYPE(CHILD(n, 0)) == NEWLINE) {
     282           0 :                 stmts = asdl_seq_new(1, arena);
     283           0 :                 if (!stmts)
     284           0 :                     goto error;
     285           0 :                 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
     286             :                                             arena));
     287           0 :                 if (!asdl_seq_GET(stmts, 0))
     288           0 :                     goto error;
     289           0 :                 return Interactive(stmts, arena);
     290             :             }
     291             :             else {
     292           0 :                 n = CHILD(n, 0);
     293           0 :                 num = num_stmts(n);
     294           0 :                 stmts = asdl_seq_new(num, arena);
     295           0 :                 if (!stmts)
     296           0 :                     goto error;
     297           0 :                 if (num == 1) {
     298           0 :                     s = ast_for_stmt(&c, n);
     299           0 :                     if (!s)
     300           0 :                         goto error;
     301           0 :                     asdl_seq_SET(stmts, 0, s);
     302             :                 }
     303             :                 else {
     304             :                     /* Only a simple_stmt can contain multiple statements. */
     305             :                     REQ(n, simple_stmt);
     306           0 :                     for (i = 0; i < NCH(n); i += 2) {
     307           0 :                         if (TYPE(CHILD(n, i)) == NEWLINE)
     308           0 :                             break;
     309           0 :                         s = ast_for_stmt(&c, CHILD(n, i));
     310           0 :                         if (!s)
     311           0 :                             goto error;
     312           0 :                         asdl_seq_SET(stmts, i / 2, s);
     313             :                     }
     314             :                 }
     315             : 
     316           0 :                 return Interactive(stmts, arena);
     317             :             }
     318             :         default:
     319           0 :             PyErr_Format(PyExc_SystemError,
     320           0 :                          "invalid node %d for PyAST_FromNode", TYPE(n));
     321           0 :             goto error;
     322             :     }
     323             :  error:
     324           0 :     ast_error_finish(filename);
     325           0 :     return NULL;
     326             : }
     327             : 
     328             : /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
     329             : */
     330             : 
     331             : static operator_ty
     332         371 : get_operator(const node *n)
     333             : {
     334         371 :     switch (TYPE(n)) {
     335             :         case VBAR:
     336           4 :             return BitOr;
     337             :         case CIRCUMFLEX:
     338           0 :             return BitXor;
     339             :         case AMPER:
     340           3 :             return BitAnd;
     341             :         case LEFTSHIFT:
     342           3 :             return LShift;
     343             :         case RIGHTSHIFT:
     344           0 :             return RShift;
     345             :         case PLUS:
     346         131 :             return Add;
     347             :         case MINUS:
     348          23 :             return Sub;
     349             :         case STAR:
     350          12 :             return Mult;
     351             :         case SLASH:
     352           1 :             return Div;
     353             :         case DOUBLESLASH:
     354           1 :             return FloorDiv;
     355             :         case PERCENT:
     356         193 :             return Mod;
     357             :         default:
     358           0 :             return (operator_ty)0;
     359             :     }
     360             : }
     361             : 
     362             : /* Set the context ctx for expr_ty e, recursively traversing e.
     363             : 
     364             :    Only sets context for expr kinds that "can appear in assignment context"
     365             :    (according to ../Parser/Python.asdl).  For other expr kinds, it sets
     366             :    an appropriate syntax error and returns false.
     367             : */
     368             : 
     369             : static int
     370        2747 : set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
     371             : {
     372        2747 :     asdl_seq *s = NULL;
     373             :     /* If a particular expression type can't be used for assign / delete,
     374             :        set expr_name to its name and an error message will be generated.
     375             :     */
     376        2747 :     const char* expr_name = NULL;
     377             : 
     378             :     /* The ast defines augmented store and load contexts, but the
     379             :        implementation here doesn't actually use them.  The code may be
     380             :        a little more complex than necessary as a result.  It also means
     381             :        that expressions in an augmented assignment have a Store context.
     382             :        Consider restructuring so that augmented assignment uses
     383             :        set_context(), too.
     384             :     */
     385             :     assert(ctx != AugStore && ctx != AugLoad);
     386             : 
     387        2747 :     switch (e->kind) {
     388             :         case Attribute_kind:
     389         810 :             if (ctx == Store && !forbidden_check(c, n,
     390         405 :                                 PyBytes_AS_STRING(e->v.Attribute.attr)))
     391           0 :                     return 0;
     392         405 :             e->v.Attribute.ctx = ctx;
     393         405 :             break;
     394             :         case Subscript_kind:
     395          50 :             e->v.Subscript.ctx = ctx;
     396          50 :             break;
     397             :         case Name_kind:
     398        4395 :             if (ctx == Store && !forbidden_check(c, n,
     399        2197 :                                 PyBytes_AS_STRING(e->v.Name.id)))
     400           0 :                     return 0;
     401        2198 :             e->v.Name.ctx = ctx;
     402        2198 :             break;
     403             :         case List_kind:
     404           0 :             e->v.List.ctx = ctx;
     405           0 :             s = e->v.List.elts;
     406           0 :             break;
     407             :         case Tuple_kind:
     408          94 :             if (asdl_seq_LEN(e->v.Tuple.elts))  {
     409          94 :                 e->v.Tuple.ctx = ctx;
     410          94 :                 s = e->v.Tuple.elts;
     411             :             }
     412             :             else {
     413           0 :                 expr_name = "()";
     414             :             }
     415          94 :             break;
     416             :         case Lambda_kind:
     417           0 :             expr_name = "lambda";
     418           0 :             break;
     419             :         case Call_kind:
     420           0 :             expr_name = "function call";
     421           0 :             break;
     422             :         case BoolOp_kind:
     423             :         case BinOp_kind:
     424             :         case UnaryOp_kind:
     425           0 :             expr_name = "operator";
     426           0 :             break;
     427             :         case GeneratorExp_kind:
     428           0 :             expr_name = "generator expression";
     429           0 :             break;
     430             :         case Yield_kind:
     431           0 :             expr_name = "yield expression";
     432           0 :             break;
     433             :         case ListComp_kind:
     434           0 :             expr_name = "list comprehension";
     435           0 :             break;
     436             :         case SetComp_kind:
     437           0 :             expr_name = "set comprehension";
     438           0 :             break;
     439             :         case DictComp_kind:
     440           0 :             expr_name = "dict comprehension";
     441           0 :             break;
     442             :         case Dict_kind:
     443             :         case Set_kind:
     444             :         case Num_kind:
     445             :         case Str_kind:
     446           0 :             expr_name = "literal";
     447           0 :             break;
     448             :         case Compare_kind:
     449           0 :             expr_name = "comparison";
     450           0 :             break;
     451             :         case Repr_kind:
     452           0 :             expr_name = "repr";
     453           0 :             break;
     454             :         case IfExp_kind:
     455           0 :             expr_name = "conditional expression";
     456           0 :             break;
     457             :         default:
     458           0 :             PyErr_Format(PyExc_SystemError,
     459             :                          "unexpected expression in assignment %d (line %d)",
     460           0 :                          e->kind, e->lineno);
     461           0 :             return 0;
     462             :     }
     463             :     /* Check for error string set by switch */
     464        2747 :     if (expr_name) {
     465             :         char buf[300];
     466           0 :         PyOS_snprintf(buf, sizeof(buf),
     467             :                       "can't %s %s",
     468             :                       ctx == Store ? "assign to" : "delete",
     469             :                       expr_name);
     470           0 :         return ast_error(n, buf);
     471             :     }
     472             : 
     473             :     /* If the LHS is a list or tuple, we need to set the assignment
     474             :        context for all the contained elements.
     475             :     */
     476        2747 :     if (s) {
     477             :         int i;
     478             : 
     479         312 :         for (i = 0; i < asdl_seq_LEN(s); i++) {
     480         218 :             if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
     481           0 :                 return 0;
     482             :         }
     483             :     }
     484        2747 :     return 1;
     485             : }
     486             : 
     487             : static operator_ty
     488          48 : ast_for_augassign(struct compiling *c, const node *n)
     489             : {
     490             :     REQ(n, augassign);
     491          48 :     n = CHILD(n, 0);
     492          48 :     switch (STR(n)[0]) {
     493             :         case '+':
     494          43 :             return Add;
     495             :         case '-':
     496           2 :             return Sub;
     497             :         case '/':
     498           0 :             if (STR(n)[1] == '/')
     499           0 :                 return FloorDiv;
     500             :             else
     501           0 :                 return Div;
     502             :         case '%':
     503           0 :             return Mod;
     504             :         case '<':
     505           0 :             return LShift;
     506             :         case '>':
     507           2 :             return RShift;
     508             :         case '&':
     509           0 :             return BitAnd;
     510             :         case '^':
     511           0 :             return BitXor;
     512             :         case '|':
     513           0 :             return BitOr;
     514             :         case '*':
     515           1 :             if (STR(n)[1] == '*')
     516           0 :                 return Pow;
     517             :             else
     518           1 :                 return Mult;
     519             :         default:
     520           0 :             PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
     521           0 :             return (operator_ty)0;
     522             :     }
     523             : }
     524             : 
     525             : static cmpop_ty
     526         911 : ast_for_comp_op(struct compiling *c, const node *n)
     527             : {
     528             :     /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
     529             :                |'is' 'not'
     530             :     */
     531             :     REQ(n, comp_op);
     532         911 :     if (NCH(n) == 1) {
     533         882 :         n = CHILD(n, 0);
     534         882 :         switch (TYPE(n)) {
     535             :             case LESS:
     536           5 :                 return Lt;
     537             :             case GREATER:
     538           6 :                 return Gt;
     539             :             case EQEQUAL:                       /* == */
     540         615 :                 return Eq;
     541             :             case LESSEQUAL:
     542           4 :                 return LtE;
     543             :             case GREATEREQUAL:
     544          10 :                 return GtE;
     545             :             case NOTEQUAL:
     546         103 :                 return NotEq;
     547             :             case NAME:
     548         139 :                 if (strcmp(STR(n), "in") == 0)
     549         102 :                     return In;
     550          37 :                 if (strcmp(STR(n), "is") == 0)
     551          37 :                     return Is;
     552             :             default:
     553           0 :                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
     554             :                              STR(n));
     555           0 :                 return (cmpop_ty)0;
     556             :         }
     557             :     }
     558          29 :     else if (NCH(n) == 2) {
     559             :         /* handle "not in" and "is not" */
     560          29 :         switch (TYPE(CHILD(n, 0))) {
     561             :             case NAME:
     562          29 :                 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
     563          11 :                     return NotIn;
     564          18 :                 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
     565          18 :                     return IsNot;
     566             :             default:
     567           0 :                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
     568           0 :                              STR(CHILD(n, 0)), STR(CHILD(n, 1)));
     569           0 :                 return (cmpop_ty)0;
     570             :         }
     571             :     }
     572           0 :     PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
     573             :                  NCH(n));
     574           0 :     return (cmpop_ty)0;
     575             : }
     576             : 
     577             : static asdl_seq *
     578         580 : seq_for_testlist(struct compiling *c, const node *n)
     579             : {
     580             :     /* testlist: test (',' test)* [','] */
     581             :     asdl_seq *seq;
     582             :     expr_ty expression;
     583             :     int i;
     584             :     assert(TYPE(n) == testlist ||
     585             :            TYPE(n) == listmaker ||
     586             :            TYPE(n) == testlist_comp ||
     587             :            TYPE(n) == testlist_safe ||
     588             :            TYPE(n) == testlist1);
     589             : 
     590         580 :     seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
     591         580 :     if (!seq)
     592           0 :         return NULL;
     593             : 
     594        2293 :     for (i = 0; i < NCH(n); i += 2) {
     595             :         assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
     596             : 
     597        1713 :         expression = ast_for_expr(c, CHILD(n, i));
     598        1713 :         if (!expression)
     599           0 :             return NULL;
     600             : 
     601             :         assert(i / 2 < seq->size);
     602        1713 :         asdl_seq_SET(seq, i / 2, expression);
     603             :     }
     604         580 :     return seq;
     605             : }
     606             : 
     607             : static expr_ty
     608           0 : compiler_complex_args(struct compiling *c, const node *n)
     609             : {
     610           0 :     int i, len = (NCH(n) + 1) / 2;
     611             :     expr_ty result;
     612           0 :     asdl_seq *args = asdl_seq_new(len, c->c_arena);
     613           0 :     if (!args)
     614           0 :         return NULL;
     615             : 
     616             :     /* fpdef: NAME | '(' fplist ')'
     617             :        fplist: fpdef (',' fpdef)* [',']
     618             :     */
     619             :     REQ(n, fplist);
     620           0 :     for (i = 0; i < len; i++) {
     621             :         PyObject *arg_id;
     622           0 :         const node *fpdef_node = CHILD(n, 2*i);
     623             :         const node *child;
     624             :         expr_ty arg;
     625             : set_name:
     626             :         /* fpdef_node is either a NAME or an fplist */
     627           0 :         child = CHILD(fpdef_node, 0);
     628           0 :         if (TYPE(child) == NAME) {
     629           0 :             if (!forbidden_check(c, n, STR(child)))
     630           0 :                 return NULL;
     631           0 :             arg_id = NEW_IDENTIFIER(child);
     632           0 :             if (!arg_id)
     633           0 :                 return NULL;
     634           0 :             arg = Name(arg_id, Store, LINENO(child), child->n_col_offset,
     635             :                        c->c_arena);
     636             :         }
     637             :         else {
     638             :             assert(TYPE(fpdef_node) == fpdef);
     639             :             /* fpdef_node[0] is not a name, so it must be '(', get CHILD[1] */
     640           0 :             child = CHILD(fpdef_node, 1);
     641             :             assert(TYPE(child) == fplist);
     642             :             /* NCH == 1 means we have (x), we need to elide the extra parens */
     643           0 :             if (NCH(child) == 1) {
     644           0 :                 fpdef_node = CHILD(child, 0);
     645             :                 assert(TYPE(fpdef_node) == fpdef);
     646           0 :                 goto set_name;
     647             :             }
     648           0 :             arg = compiler_complex_args(c, child);
     649             :         }
     650           0 :         asdl_seq_SET(args, i, arg);
     651             :     }
     652             : 
     653           0 :     result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
     654           0 :     if (!set_context(c, result, Store, n))
     655           0 :         return NULL;
     656           0 :     return result;
     657             : }
     658             : 
     659             : 
     660             : /* Create AST for argument list. */
     661             : 
     662             : static arguments_ty
     663         825 : ast_for_arguments(struct compiling *c, const node *n)
     664             : {
     665             :     /* parameters: '(' [varargslist] ')'
     666             :        varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME]
     667             :             | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
     668             :     */
     669         825 :     int i, j, k, n_args = 0, n_defaults = 0, found_default = 0;
     670             :     asdl_seq *args, *defaults;
     671         825 :     identifier vararg = NULL, kwarg = NULL;
     672             :     node *ch;
     673             : 
     674         825 :     if (TYPE(n) == parameters) {
     675         821 :         if (NCH(n) == 2) /* () as argument list */
     676           9 :             return arguments(NULL, NULL, NULL, NULL, c->c_arena);
     677         812 :         n = CHILD(n, 1);
     678             :     }
     679             :     REQ(n, varargslist);
     680             : 
     681             :     /* first count the number of normal args & defaults */
     682        3633 :     for (i = 0; i < NCH(n); i++) {
     683        2817 :         ch = CHILD(n, i);
     684        2817 :         if (TYPE(ch) == fpdef)
     685        1617 :             n_args++;
     686        2817 :         if (TYPE(ch) == EQUAL)
     687         123 :             n_defaults++;
     688             :     }
     689         816 :     args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL);
     690         816 :     if (!args && n_args)
     691           0 :         return NULL;
     692         816 :     defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
     693         816 :     if (!defaults && n_defaults)
     694           0 :         return NULL;
     695             : 
     696             :     /* fpdef: NAME | '(' fplist ')'
     697             :        fplist: fpdef (',' fpdef)* [',']
     698             :     */
     699         816 :     i = 0;
     700         816 :     j = 0;  /* index for defaults */
     701         816 :     k = 0;  /* index for args */
     702        3300 :     while (i < NCH(n)) {
     703        1668 :         ch = CHILD(n, i);
     704        1668 :         switch (TYPE(ch)) {
     705             :             case fpdef: {
     706        1617 :                 int complex_args = 0, parenthesized = 0;
     707             :             handle_fpdef:
     708             :                 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
     709             :                    anything other than EQUAL or a comma? */
     710             :                 /* XXX Should NCH(n) check be made a separate check? */
     711        1740 :                 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
     712         123 :                     expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
     713         123 :                     if (!expression)
     714           0 :                         return NULL;
     715             :                     assert(defaults != NULL);
     716         123 :                     asdl_seq_SET(defaults, j++, expression);
     717         123 :                     i += 2;
     718         123 :                     found_default = 1;
     719             :                 }
     720        1494 :                 else if (found_default) {
     721             :                     /* def f((x)=4): pass should raise an error.
     722             :                        def f((x, (y))): pass will just incur the tuple unpacking warning. */
     723           0 :                     if (parenthesized && !complex_args) {
     724           0 :                         ast_error(n, "parenthesized arg with default");
     725           0 :                         return NULL;
     726             :                     }
     727           0 :                     ast_error(n,
     728             :                              "non-default argument follows default argument");
     729           0 :                     return NULL;
     730             :                 }
     731        1617 :                 if (NCH(ch) == 3) {
     732           0 :                     ch = CHILD(ch, 1);
     733             :                     /* def foo((x)): is not complex, special case. */
     734           0 :                     if (NCH(ch) != 1) {
     735             :                         /* We have complex arguments, setup for unpacking. */
     736           0 :                         if (Py_Py3kWarningFlag && !ast_warn(c, ch,
     737             :                             "tuple parameter unpacking has been removed in 3.x"))
     738           0 :                             return NULL;
     739           0 :                         complex_args = 1;
     740           0 :                         asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
     741           0 :                         if (!asdl_seq_GET(args, k-1))
     742           0 :                                 return NULL;
     743             :                     } else {
     744             :                         /* def foo((x)): setup for checking NAME below. */
     745             :                         /* Loop because there can be many parens and tuple
     746             :                            unpacking mixed in. */
     747           0 :                         parenthesized = 1;
     748           0 :                         ch = CHILD(ch, 0);
     749             :                         assert(TYPE(ch) == fpdef);
     750           0 :                         goto handle_fpdef;
     751             :                     }
     752             :                 }
     753        1617 :                 if (TYPE(CHILD(ch, 0)) == NAME) {
     754             :                     PyObject *id;
     755             :                     expr_ty name;
     756        1617 :                     if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
     757           0 :                         return NULL;
     758        1617 :                     id = NEW_IDENTIFIER(CHILD(ch, 0));
     759        1617 :                     if (!id)
     760           0 :                         return NULL;
     761        1617 :                     name = Name(id, Param, LINENO(ch), ch->n_col_offset,
     762             :                                 c->c_arena);
     763        1617 :                     if (!name)
     764           0 :                         return NULL;
     765        1617 :                     asdl_seq_SET(args, k++, name);
     766             : 
     767             :                 }
     768        1617 :                 i += 2; /* the name and the comma */
     769        1617 :                 if (parenthesized && Py_Py3kWarningFlag &&
     770           0 :                     !ast_warn(c, ch, "parenthesized argument names "
     771             :                               "are invalid in 3.x"))
     772           0 :                     return NULL;
     773             : 
     774        1617 :                 break;
     775             :             }
     776             :             case STAR:
     777          17 :                 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
     778           0 :                     return NULL;
     779          17 :                 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
     780          17 :                 if (!vararg)
     781           0 :                     return NULL;
     782          17 :                 i += 3;
     783          17 :                 break;
     784             :             case DOUBLESTAR:
     785          34 :                 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
     786           0 :                     return NULL;
     787          34 :                 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
     788          34 :                 if (!kwarg)
     789           0 :                     return NULL;
     790          34 :                 i += 3;
     791          34 :                 break;
     792             :             default:
     793           0 :                 PyErr_Format(PyExc_SystemError,
     794             :                              "unexpected node in varargslist: %d @ %d",
     795           0 :                              TYPE(ch), i);
     796           0 :                 return NULL;
     797             :         }
     798             :     }
     799             : 
     800         816 :     return arguments(args, vararg, kwarg, defaults, c->c_arena);
     801             : }
     802             : 
     803             : static expr_ty
     804          27 : ast_for_dotted_name(struct compiling *c, const node *n)
     805             : {
     806             :     expr_ty e;
     807             :     identifier id;
     808             :     int lineno, col_offset;
     809             :     int i;
     810             : 
     811             :     REQ(n, dotted_name);
     812             : 
     813          27 :     lineno = LINENO(n);
     814          27 :     col_offset = n->n_col_offset;
     815             : 
     816          27 :     id = NEW_IDENTIFIER(CHILD(n, 0));
     817          27 :     if (!id)
     818           0 :         return NULL;
     819          27 :     e = Name(id, Load, lineno, col_offset, c->c_arena);
     820          27 :     if (!e)
     821           0 :         return NULL;
     822             : 
     823          27 :     for (i = 2; i < NCH(n); i+=2) {
     824           0 :         id = NEW_IDENTIFIER(CHILD(n, i));
     825           0 :         if (!id)
     826           0 :             return NULL;
     827           0 :         e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
     828           0 :         if (!e)
     829           0 :             return NULL;
     830             :     }
     831             : 
     832          27 :     return e;
     833             : }
     834             : 
     835             : static expr_ty
     836          27 : ast_for_decorator(struct compiling *c, const node *n)
     837             : {
     838             :     /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
     839          27 :     expr_ty d = NULL;
     840             :     expr_ty name_expr;
     841             : 
     842             :     REQ(n, decorator);
     843             :     REQ(CHILD(n, 0), AT);
     844             :     REQ(RCHILD(n, -1), NEWLINE);
     845             : 
     846          27 :     name_expr = ast_for_dotted_name(c, CHILD(n, 1));
     847          27 :     if (!name_expr)
     848           0 :         return NULL;
     849             : 
     850          27 :     if (NCH(n) == 3) { /* No arguments */
     851          27 :         d = name_expr;
     852          27 :         name_expr = NULL;
     853             :     }
     854           0 :     else if (NCH(n) == 5) { /* Call with no arguments */
     855           0 :         d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
     856             :                  n->n_col_offset, c->c_arena);
     857           0 :         if (!d)
     858           0 :             return NULL;
     859           0 :         name_expr = NULL;
     860             :     }
     861             :     else {
     862           0 :         d = ast_for_call(c, CHILD(n, 3), name_expr);
     863           0 :         if (!d)
     864           0 :             return NULL;
     865           0 :         name_expr = NULL;
     866             :     }
     867             : 
     868          27 :     return d;
     869             : }
     870             : 
     871             : static asdl_seq*
     872          27 : ast_for_decorators(struct compiling *c, const node *n)
     873             : {
     874             :     asdl_seq* decorator_seq;
     875             :     expr_ty d;
     876             :     int i;
     877             : 
     878             :     REQ(n, decorators);
     879          27 :     decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
     880          27 :     if (!decorator_seq)
     881           0 :         return NULL;
     882             : 
     883          54 :     for (i = 0; i < NCH(n); i++) {
     884          27 :         d = ast_for_decorator(c, CHILD(n, i));
     885          27 :         if (!d)
     886           0 :             return NULL;
     887          27 :         asdl_seq_SET(decorator_seq, i, d);
     888             :     }
     889          27 :     return decorator_seq;
     890             : }
     891             : 
     892             : static stmt_ty
     893         821 : ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
     894             : {
     895             :     /* funcdef: 'def' NAME parameters ':' suite */
     896             :     identifier name;
     897             :     arguments_ty args;
     898             :     asdl_seq *body;
     899         821 :     int name_i = 1;
     900             : 
     901             :     REQ(n, funcdef);
     902             : 
     903         821 :     name = NEW_IDENTIFIER(CHILD(n, name_i));
     904         821 :     if (!name)
     905           0 :         return NULL;
     906         821 :     else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
     907           0 :         return NULL;
     908         821 :     args = ast_for_arguments(c, CHILD(n, name_i + 1));
     909         821 :     if (!args)
     910           0 :         return NULL;
     911         821 :     body = ast_for_suite(c, CHILD(n, name_i + 3));
     912         821 :     if (!body)
     913           0 :         return NULL;
     914             : 
     915         821 :     return FunctionDef(name, args, body, decorator_seq, LINENO(n),
     916             :                        n->n_col_offset, c->c_arena);
     917             : }
     918             : 
     919             : static stmt_ty
     920          27 : ast_for_decorated(struct compiling *c, const node *n)
     921             : {
     922             :     /* decorated: decorators (classdef | funcdef) */
     923          27 :     stmt_ty thing = NULL;
     924          27 :     asdl_seq *decorator_seq = NULL;
     925             : 
     926             :     REQ(n, decorated);
     927             : 
     928          27 :     decorator_seq = ast_for_decorators(c, CHILD(n, 0));
     929          27 :     if (!decorator_seq)
     930           0 :       return NULL;
     931             : 
     932             :     assert(TYPE(CHILD(n, 1)) == funcdef ||
     933             :            TYPE(CHILD(n, 1)) == classdef);
     934             : 
     935          27 :     if (TYPE(CHILD(n, 1)) == funcdef) {
     936          27 :       thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
     937           0 :     } else if (TYPE(CHILD(n, 1)) == classdef) {
     938           0 :       thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
     939             :     }
     940             :     /* we count the decorators in when talking about the class' or
     941             :        function's line number */
     942          27 :     if (thing) {
     943          27 :         thing->lineno = LINENO(n);
     944          27 :         thing->col_offset = n->n_col_offset;
     945             :     }
     946          27 :     return thing;
     947             : }
     948             : 
     949             : static expr_ty
     950           4 : ast_for_lambdef(struct compiling *c, const node *n)
     951             : {
     952             :     /* lambdef: 'lambda' [varargslist] ':' test */
     953             :     arguments_ty args;
     954             :     expr_ty expression;
     955             : 
     956           4 :     if (NCH(n) == 3) {
     957           0 :         args = arguments(NULL, NULL, NULL, NULL, c->c_arena);
     958           0 :         if (!args)
     959           0 :             return NULL;
     960           0 :         expression = ast_for_expr(c, CHILD(n, 2));
     961           0 :         if (!expression)
     962           0 :             return NULL;
     963             :     }
     964             :     else {
     965           4 :         args = ast_for_arguments(c, CHILD(n, 1));
     966           4 :         if (!args)
     967           0 :             return NULL;
     968           4 :         expression = ast_for_expr(c, CHILD(n, 3));
     969           4 :         if (!expression)
     970           0 :             return NULL;
     971             :     }
     972             : 
     973           4 :     return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
     974             : }
     975             : 
     976             : static expr_ty
     977          14 : ast_for_ifexpr(struct compiling *c, const node *n)
     978             : {
     979             :     /* test: or_test 'if' or_test 'else' test */
     980             :     expr_ty expression, body, orelse;
     981             : 
     982             :     assert(NCH(n) == 5);
     983          14 :     body = ast_for_expr(c, CHILD(n, 0));
     984          14 :     if (!body)
     985           0 :         return NULL;
     986          14 :     expression = ast_for_expr(c, CHILD(n, 2));
     987          14 :     if (!expression)
     988           0 :         return NULL;
     989          14 :     orelse = ast_for_expr(c, CHILD(n, 4));
     990          14 :     if (!orelse)
     991           0 :         return NULL;
     992          14 :     return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
     993             :                  c->c_arena);
     994             : }
     995             : 
     996             : /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
     997             :    so there is only a single version.  Possibly for loops can also re-use
     998             :    the code.
     999             : */
    1000             : 
    1001             : /* Count the number of 'for' loop in a list comprehension.
    1002             : 
    1003             :    Helper for ast_for_listcomp().
    1004             : */
    1005             : 
    1006             : static int
    1007          11 : count_list_fors(struct compiling *c, const node *n)
    1008             : {
    1009          11 :     int n_fors = 0;
    1010          11 :     node *ch = CHILD(n, 1);
    1011             : 
    1012             :  count_list_for:
    1013          11 :     n_fors++;
    1014             :     REQ(ch, list_for);
    1015          11 :     if (NCH(ch) == 5)
    1016           2 :         ch = CHILD(ch, 4);
    1017             :     else
    1018           9 :         return n_fors;
    1019             :  count_list_iter:
    1020             :     REQ(ch, list_iter);
    1021           2 :     ch = CHILD(ch, 0);
    1022           2 :     if (TYPE(ch) == list_for)
    1023           0 :         goto count_list_for;
    1024           2 :     else if (TYPE(ch) == list_if) {
    1025           2 :         if (NCH(ch) == 3) {
    1026           0 :             ch = CHILD(ch, 2);
    1027           0 :             goto count_list_iter;
    1028             :         }
    1029             :         else
    1030           2 :             return n_fors;
    1031             :     }
    1032             : 
    1033             :     /* Should never be reached */
    1034           0 :     PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
    1035           0 :     return -1;
    1036             : }
    1037             : 
    1038             : /* Count the number of 'if' statements in a list comprehension.
    1039             : 
    1040             :    Helper for ast_for_listcomp().
    1041             : */
    1042             : 
    1043             : static int
    1044           2 : count_list_ifs(struct compiling *c, const node *n)
    1045             : {
    1046           2 :     int n_ifs = 0;
    1047             : 
    1048             :  count_list_iter:
    1049             :     REQ(n, list_iter);
    1050           2 :     if (TYPE(CHILD(n, 0)) == list_for)
    1051           0 :         return n_ifs;
    1052           2 :     n = CHILD(n, 0);
    1053             :     REQ(n, list_if);
    1054           2 :     n_ifs++;
    1055           2 :     if (NCH(n) == 2)
    1056           2 :         return n_ifs;
    1057           0 :     n = CHILD(n, 2);
    1058           0 :     goto count_list_iter;
    1059             : }
    1060             : 
    1061             : static expr_ty
    1062          11 : ast_for_listcomp(struct compiling *c, const node *n)
    1063             : {
    1064             :     /* listmaker: test ( list_for | (',' test)* [','] )
    1065             :        list_for: 'for' exprlist 'in' testlist_safe [list_iter]
    1066             :        list_iter: list_for | list_if
    1067             :        list_if: 'if' test [list_iter]
    1068             :        testlist_safe: test [(',' test)+ [',']]
    1069             :     */
    1070             :     expr_ty elt, first;
    1071             :     asdl_seq *listcomps;
    1072             :     int i, n_fors;
    1073             :     node *ch;
    1074             : 
    1075             :     REQ(n, listmaker);
    1076             :     assert(NCH(n) > 1);
    1077             : 
    1078          11 :     elt = ast_for_expr(c, CHILD(n, 0));
    1079          11 :     if (!elt)
    1080           0 :         return NULL;
    1081             : 
    1082          11 :     n_fors = count_list_fors(c, n);
    1083          11 :     if (n_fors == -1)
    1084           0 :         return NULL;
    1085             : 
    1086          11 :     listcomps = asdl_seq_new(n_fors, c->c_arena);
    1087          11 :     if (!listcomps)
    1088           0 :         return NULL;
    1089             : 
    1090          11 :     ch = CHILD(n, 1);
    1091          22 :     for (i = 0; i < n_fors; i++) {
    1092             :         comprehension_ty lc;
    1093             :         asdl_seq *t;
    1094             :         expr_ty expression;
    1095             :         node *for_ch;
    1096             : 
    1097             :         REQ(ch, list_for);
    1098             : 
    1099          11 :         for_ch = CHILD(ch, 1);
    1100          11 :         t = ast_for_exprlist(c, for_ch, Store);
    1101          11 :         if (!t)
    1102           0 :             return NULL;
    1103          11 :         expression = ast_for_testlist(c, CHILD(ch, 3));
    1104          11 :         if (!expression)
    1105           0 :             return NULL;
    1106             : 
    1107             :         /* Check the # of children rather than the length of t, since
    1108             :            [x for x, in ... ] has 1 element in t, but still requires a Tuple.
    1109             :         */
    1110          11 :         first = (expr_ty)asdl_seq_GET(t, 0);
    1111          11 :         if (NCH(for_ch) == 1)
    1112           9 :             lc = comprehension(first, expression, NULL, c->c_arena);
    1113             :         else
    1114           2 :             lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
    1115             :                                      c->c_arena),
    1116             :                                expression, NULL, c->c_arena);
    1117          11 :         if (!lc)
    1118           0 :             return NULL;
    1119             : 
    1120          11 :         if (NCH(ch) == 5) {
    1121             :             int j, n_ifs;
    1122             :             asdl_seq *ifs;
    1123             :             expr_ty list_for_expr;
    1124             : 
    1125           2 :             ch = CHILD(ch, 4);
    1126           2 :             n_ifs = count_list_ifs(c, ch);
    1127           2 :             if (n_ifs == -1)
    1128           0 :                 return NULL;
    1129             : 
    1130           2 :             ifs = asdl_seq_new(n_ifs, c->c_arena);
    1131           2 :             if (!ifs)
    1132           0 :                 return NULL;
    1133             : 
    1134           4 :             for (j = 0; j < n_ifs; j++) {
    1135             :                 REQ(ch, list_iter);
    1136           2 :                 ch = CHILD(ch, 0);
    1137             :                 REQ(ch, list_if);
    1138             : 
    1139           2 :                 list_for_expr = ast_for_expr(c, CHILD(ch, 1));
    1140           2 :                 if (!list_for_expr)
    1141           0 :                     return NULL;
    1142             : 
    1143           2 :                 asdl_seq_SET(ifs, j, list_for_expr);
    1144           2 :                 if (NCH(ch) == 3)
    1145           0 :                     ch = CHILD(ch, 2);
    1146             :             }
    1147             :             /* on exit, must guarantee that ch is a list_for */
    1148           2 :             if (TYPE(ch) == list_iter)
    1149           0 :                 ch = CHILD(ch, 0);
    1150           2 :             lc->ifs = ifs;
    1151             :         }
    1152          11 :         asdl_seq_SET(listcomps, i, lc);
    1153             :     }
    1154             : 
    1155          11 :     return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
    1156             : }
    1157             : 
    1158             : /*
    1159             :    Count the number of 'for' loops in a comprehension.
    1160             : 
    1161             :    Helper for ast_for_comprehension().
    1162             : */
    1163             : 
    1164             : static int
    1165          11 : count_comp_fors(struct compiling *c, const node *n)
    1166             : {
    1167          11 :     int n_fors = 0;
    1168             : 
    1169             :   count_comp_for:
    1170          11 :     n_fors++;
    1171             :     REQ(n, comp_for);
    1172          11 :     if (NCH(n) == 5)
    1173           0 :         n = CHILD(n, 4);
    1174             :     else
    1175          11 :         return n_fors;
    1176             :   count_comp_iter:
    1177             :     REQ(n, comp_iter);
    1178           0 :     n = CHILD(n, 0);
    1179           0 :     if (TYPE(n) == comp_for)
    1180           0 :         goto count_comp_for;
    1181           0 :     else if (TYPE(n) == comp_if) {
    1182           0 :         if (NCH(n) == 3) {
    1183           0 :             n = CHILD(n, 2);
    1184           0 :             goto count_comp_iter;
    1185             :         }
    1186             :         else
    1187           0 :             return n_fors;
    1188             :     }
    1189             : 
    1190             :     /* Should never be reached */
    1191           0 :     PyErr_SetString(PyExc_SystemError,
    1192             :                     "logic error in count_comp_fors");
    1193           0 :     return -1;
    1194             : }
    1195             : 
    1196             : /* Count the number of 'if' statements in a comprehension.
    1197             : 
    1198             :    Helper for ast_for_comprehension().
    1199             : */
    1200             : 
    1201             : static int
    1202           0 : count_comp_ifs(struct compiling *c, const node *n)
    1203             : {
    1204           0 :     int n_ifs = 0;
    1205             : 
    1206             :     while (1) {
    1207             :         REQ(n, comp_iter);
    1208           0 :         if (TYPE(CHILD(n, 0)) == comp_for)
    1209           0 :             return n_ifs;
    1210           0 :         n = CHILD(n, 0);
    1211             :         REQ(n, comp_if);
    1212           0 :         n_ifs++;
    1213           0 :         if (NCH(n) == 2)
    1214           0 :             return n_ifs;
    1215           0 :         n = CHILD(n, 2);
    1216           0 :     }
    1217             : }
    1218             : 
    1219             : static asdl_seq *
    1220          11 : ast_for_comprehension(struct compiling *c, const node *n)
    1221             : {
    1222             :     int i, n_fors;
    1223             :     asdl_seq *comps;
    1224             : 
    1225          11 :     n_fors = count_comp_fors(c, n);
    1226          11 :     if (n_fors == -1)
    1227           0 :         return NULL;
    1228             : 
    1229          11 :     comps = asdl_seq_new(n_fors, c->c_arena);
    1230          11 :     if (!comps)
    1231           0 :         return NULL;
    1232             : 
    1233          22 :     for (i = 0; i < n_fors; i++) {
    1234             :         comprehension_ty comp;
    1235             :         asdl_seq *t;
    1236             :         expr_ty expression, first;
    1237             :         node *for_ch;
    1238             : 
    1239             :         REQ(n, comp_for);
    1240             : 
    1241          11 :         for_ch = CHILD(n, 1);
    1242          11 :         t = ast_for_exprlist(c, for_ch, Store);
    1243          11 :         if (!t)
    1244           0 :             return NULL;
    1245          11 :         expression = ast_for_expr(c, CHILD(n, 3));
    1246          11 :         if (!expression)
    1247           0 :             return NULL;
    1248             : 
    1249             :         /* Check the # of children rather than the length of t, since
    1250             :            (x for x, in ...) has 1 element in t, but still requires a Tuple. */
    1251          11 :         first = (expr_ty)asdl_seq_GET(t, 0);
    1252          11 :         if (NCH(for_ch) == 1)
    1253          11 :             comp = comprehension(first, expression, NULL, c->c_arena);
    1254             :         else
    1255           0 :             comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
    1256             :                                      c->c_arena),
    1257             :                                expression, NULL, c->c_arena);
    1258          11 :         if (!comp)
    1259           0 :             return NULL;
    1260             : 
    1261          11 :         if (NCH(n) == 5) {
    1262             :             int j, n_ifs;
    1263             :             asdl_seq *ifs;
    1264             : 
    1265           0 :             n = CHILD(n, 4);
    1266           0 :             n_ifs = count_comp_ifs(c, n);
    1267           0 :             if (n_ifs == -1)
    1268           0 :                 return NULL;
    1269             : 
    1270           0 :             ifs = asdl_seq_new(n_ifs, c->c_arena);
    1271           0 :             if (!ifs)
    1272           0 :                 return NULL;
    1273             : 
    1274           0 :             for (j = 0; j < n_ifs; j++) {
    1275             :                 REQ(n, comp_iter);
    1276           0 :                 n = CHILD(n, 0);
    1277             :                 REQ(n, comp_if);
    1278             : 
    1279           0 :                 expression = ast_for_expr(c, CHILD(n, 1));
    1280           0 :                 if (!expression)
    1281           0 :                     return NULL;
    1282           0 :                 asdl_seq_SET(ifs, j, expression);
    1283           0 :                 if (NCH(n) == 3)
    1284           0 :                     n = CHILD(n, 2);
    1285             :             }
    1286             :             /* on exit, must guarantee that n is a comp_for */
    1287           0 :             if (TYPE(n) == comp_iter)
    1288           0 :                 n = CHILD(n, 0);
    1289           0 :             comp->ifs = ifs;
    1290             :         }
    1291          11 :         asdl_seq_SET(comps, i, comp);
    1292             :     }
    1293          11 :     return comps;
    1294             : }
    1295             : 
    1296             : static expr_ty
    1297           9 : ast_for_itercomp(struct compiling *c, const node *n, int type)
    1298             : {
    1299             :     expr_ty elt;
    1300             :     asdl_seq *comps;
    1301             : 
    1302             :     assert(NCH(n) > 1);
    1303             : 
    1304           9 :     elt = ast_for_expr(c, CHILD(n, 0));
    1305           9 :     if (!elt)
    1306           0 :         return NULL;
    1307             : 
    1308           9 :     comps = ast_for_comprehension(c, CHILD(n, 1));
    1309           9 :     if (!comps)
    1310           0 :         return NULL;
    1311             : 
    1312           9 :     if (type == COMP_GENEXP)
    1313           9 :         return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
    1314           0 :     else if (type == COMP_SETCOMP)
    1315           0 :         return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
    1316             :     else
    1317             :         /* Should never happen */
    1318           0 :         return NULL;
    1319             : }
    1320             : 
    1321             : static expr_ty
    1322           2 : ast_for_dictcomp(struct compiling *c, const node *n)
    1323             : {
    1324             :     expr_ty key, value;
    1325             :     asdl_seq *comps;
    1326             : 
    1327             :     assert(NCH(n) > 3);
    1328             :     REQ(CHILD(n, 1), COLON);
    1329             : 
    1330           2 :     key = ast_for_expr(c, CHILD(n, 0));
    1331           2 :     if (!key)
    1332           0 :         return NULL;
    1333             : 
    1334           2 :     value = ast_for_expr(c, CHILD(n, 2));
    1335           2 :     if (!value)
    1336           0 :         return NULL;
    1337             : 
    1338           2 :     comps = ast_for_comprehension(c, CHILD(n, 3));
    1339           2 :     if (!comps)
    1340           0 :         return NULL;
    1341             : 
    1342           2 :     return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
    1343             : }
    1344             : 
    1345             : static expr_ty
    1346           9 : ast_for_genexp(struct compiling *c, const node *n)
    1347             : {
    1348             :     assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
    1349           9 :     return ast_for_itercomp(c, n, COMP_GENEXP);
    1350             : }
    1351             : 
    1352             : static expr_ty
    1353           0 : ast_for_setcomp(struct compiling *c, const node *n)
    1354             : {
    1355             :     assert(TYPE(n) == (dictorsetmaker));
    1356           0 :     return ast_for_itercomp(c, n, COMP_SETCOMP);
    1357             : }
    1358             : 
    1359             : static expr_ty
    1360       17911 : ast_for_atom(struct compiling *c, const node *n)
    1361             : {
    1362             :     /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']'
    1363             :        | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
    1364             :     */
    1365       17911 :     node *ch = CHILD(n, 0);
    1366             : 
    1367       17911 :     switch (TYPE(ch)) {
    1368             :     case NAME: {
    1369             :         /* All names start in Load context, but may later be
    1370             :            changed. */
    1371       14238 :         PyObject *name = NEW_IDENTIFIER(ch);
    1372       14238 :         if (!name)
    1373           0 :             return NULL;
    1374       14238 :         return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
    1375             :     }
    1376             :     case STRING: {
    1377        2353 :         PyObject *str = parsestrplus(c, n);
    1378        2353 :         if (!str) {
    1379             : #ifdef Py_USING_UNICODE
    1380           0 :             if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
    1381             :                 PyObject *type, *value, *tback, *errstr;
    1382           0 :                 PyErr_Fetch(&type, &value, &tback);
    1383           0 :                 errstr = PyObject_Str(value);
    1384           0 :                 if (errstr) {
    1385           0 :                     char *s = "";
    1386             :                     char buf[128];
    1387           0 :                     s = PyString_AsString(errstr);
    1388           0 :                     PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
    1389           0 :                     ast_error(n, buf);
    1390           0 :                     Py_DECREF(errstr);
    1391             :                 } else {
    1392           0 :                     ast_error(n, "(unicode error) unknown error");
    1393             :                 }
    1394           0 :                 Py_DECREF(type);
    1395           0 :                 Py_DECREF(value);
    1396           0 :                 Py_XDECREF(tback);
    1397             :             }
    1398             : #endif
    1399           0 :             return NULL;
    1400             :         }
    1401        2353 :         PyArena_AddPyObject(c->c_arena, str);
    1402        2353 :         return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
    1403             :     }
    1404             :     case NUMBER: {
    1405         672 :         PyObject *pynum = parsenumber(c, STR(ch));
    1406         672 :         if (!pynum)
    1407           0 :             return NULL;
    1408             : 
    1409         672 :         PyArena_AddPyObject(c->c_arena, pynum);
    1410         672 :         return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
    1411             :     }
    1412             :     case LPAR: /* some parenthesized expressions */
    1413         369 :         ch = CHILD(n, 1);
    1414             : 
    1415         369 :         if (TYPE(ch) == RPAR)
    1416          28 :             return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
    1417             : 
    1418         341 :         if (TYPE(ch) == yield_expr)
    1419           0 :             return ast_for_expr(c, ch);
    1420             : 
    1421         341 :         return ast_for_testlist_comp(c, ch);
    1422             :     case LSQB: /* list (or list comprehension) */
    1423         232 :         ch = CHILD(n, 1);
    1424             : 
    1425         232 :         if (TYPE(ch) == RSQB)
    1426         107 :             return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
    1427             : 
    1428             :         REQ(ch, listmaker);
    1429         125 :         if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
    1430         114 :             asdl_seq *elts = seq_for_testlist(c, ch);
    1431         114 :             if (!elts)
    1432           0 :                 return NULL;
    1433             : 
    1434         114 :             return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
    1435             :         }
    1436             :         else
    1437          11 :             return ast_for_listcomp(c, ch);
    1438             :     case LBRACE: {
    1439             :         /* dictorsetmaker:
    1440             :          *    (test ':' test (comp_for | (',' test ':' test)* [','])) |
    1441             :          *    (test (comp_for | (',' test)* [',']))
    1442             :          */
    1443             :         int i, size;
    1444             :         asdl_seq *keys, *values;
    1445             : 
    1446          47 :         ch = CHILD(n, 1);
    1447          47 :         if (TYPE(ch) == RBRACE) {
    1448             :             /* it's an empty dict */
    1449          33 :             return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
    1450          14 :         } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
    1451             :             /* it's a simple set */
    1452             :             asdl_seq *elts;
    1453           1 :             size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
    1454           1 :             elts = asdl_seq_new(size, c->c_arena);
    1455           1 :             if (!elts)
    1456           0 :                 return NULL;
    1457           4 :             for (i = 0; i < NCH(ch); i += 2) {
    1458             :                 expr_ty expression;
    1459           3 :                 expression = ast_for_expr(c, CHILD(ch, i));
    1460           3 :                 if (!expression)
    1461           0 :                     return NULL;
    1462           3 :                 asdl_seq_SET(elts, i / 2, expression);
    1463             :             }
    1464           1 :             return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
    1465          13 :         } else if (TYPE(CHILD(ch, 1)) == comp_for) {
    1466             :             /* it's a set comprehension */
    1467           0 :             return ast_for_setcomp(c, ch);
    1468          13 :         } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
    1469           2 :             return ast_for_dictcomp(c, ch);
    1470             :         } else {
    1471             :             /* it's a dict */
    1472          11 :             size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
    1473          11 :             keys = asdl_seq_new(size, c->c_arena);
    1474          11 :             if (!keys)
    1475           0 :                 return NULL;
    1476             : 
    1477          11 :             values = asdl_seq_new(size, c->c_arena);
    1478          11 :             if (!values)
    1479           0 :                 return NULL;
    1480             : 
    1481          57 :             for (i = 0; i < NCH(ch); i += 4) {
    1482             :                 expr_ty expression;
    1483             : 
    1484          46 :                 expression = ast_for_expr(c, CHILD(ch, i));
    1485          46 :                 if (!expression)
    1486           0 :                     return NULL;
    1487             : 
    1488          46 :                 asdl_seq_SET(keys, i / 4, expression);
    1489             : 
    1490          46 :                 expression = ast_for_expr(c, CHILD(ch, i + 2));
    1491          46 :                 if (!expression)
    1492           0 :                     return NULL;
    1493             : 
    1494          46 :                 asdl_seq_SET(values, i / 4, expression);
    1495             :             }
    1496          11 :             return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
    1497             :         }
    1498             :     }
    1499             :     case BACKQUOTE: { /* repr */
    1500             :         expr_ty expression;
    1501           0 :         if (Py_Py3kWarningFlag &&
    1502           0 :             !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
    1503           0 :             return NULL;
    1504           0 :         expression = ast_for_testlist(c, CHILD(n, 1));
    1505           0 :         if (!expression)
    1506           0 :             return NULL;
    1507             : 
    1508           0 :         return Repr(expression, LINENO(n), n->n_col_offset, c->c_arena);
    1509             :     }
    1510             :     default:
    1511           0 :         PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
    1512           0 :         return NULL;
    1513             :     }
    1514             : }
    1515             : 
    1516             : static slice_ty
    1517         284 : ast_for_slice(struct compiling *c, const node *n)
    1518             : {
    1519             :     node *ch;
    1520         284 :     expr_ty lower = NULL, upper = NULL, step = NULL;
    1521             : 
    1522             :     REQ(n, subscript);
    1523             : 
    1524             :     /*
    1525             :        subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
    1526             :        sliceop: ':' [test]
    1527             :     */
    1528         284 :     ch = CHILD(n, 0);
    1529         284 :     if (TYPE(ch) == DOT)
    1530           0 :         return Ellipsis(c->c_arena);
    1531             : 
    1532         284 :     if (NCH(n) == 1 && TYPE(ch) == test) {
    1533             :         /* 'step' variable hold no significance in terms of being used over
    1534             :            other vars */
    1535         242 :         step = ast_for_expr(c, ch);
    1536         242 :         if (!step)
    1537           0 :             return NULL;
    1538             : 
    1539         242 :         return Index(step, c->c_arena);
    1540             :     }
    1541             : 
    1542          42 :     if (TYPE(ch) == test) {
    1543          32 :         lower = ast_for_expr(c, ch);
    1544          32 :         if (!lower)
    1545           0 :             return NULL;
    1546             :     }
    1547             : 
    1548             :     /* If there's an upper bound it's in the second or third position. */
    1549          42 :     if (TYPE(ch) == COLON) {
    1550          10 :         if (NCH(n) > 1) {
    1551          10 :             node *n2 = CHILD(n, 1);
    1552             : 
    1553          10 :             if (TYPE(n2) == test) {
    1554          10 :                 upper = ast_for_expr(c, n2);
    1555          10 :                 if (!upper)
    1556           0 :                     return NULL;
    1557             :             }
    1558             :         }
    1559          32 :     } else if (NCH(n) > 2) {
    1560           6 :         node *n2 = CHILD(n, 2);
    1561             : 
    1562           6 :         if (TYPE(n2) == test) {
    1563           6 :             upper = ast_for_expr(c, n2);
    1564           6 :             if (!upper)
    1565           0 :                 return NULL;
    1566             :         }
    1567             :     }
    1568             : 
    1569          42 :     ch = CHILD(n, NCH(n) - 1);
    1570          42 :     if (TYPE(ch) == sliceop) {
    1571           0 :         if (NCH(ch) == 1) {
    1572             :             /*
    1573             :               This is an extended slice (ie "x[::]") with no expression in the
    1574             :               step field. We set this literally to "None" in order to
    1575             :               disambiguate it from x[:]. (The interpreter might have to call
    1576             :               __getslice__ for x[:], but it must call __getitem__ for x[::].)
    1577             :             */
    1578           0 :             identifier none = new_identifier("None", c->c_arena);
    1579           0 :             if (!none)
    1580           0 :                 return NULL;
    1581           0 :             ch = CHILD(ch, 0);
    1582           0 :             step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena);
    1583           0 :             if (!step)
    1584           0 :                 return NULL;
    1585             :         } else {
    1586           0 :             ch = CHILD(ch, 1);
    1587           0 :             if (TYPE(ch) == test) {
    1588           0 :                 step = ast_for_expr(c, ch);
    1589           0 :                 if (!step)
    1590           0 :                     return NULL;
    1591             :             }
    1592             :         }
    1593             :     }
    1594             : 
    1595          42 :     return Slice(lower, upper, step, c->c_arena);
    1596             : }
    1597             : 
    1598             : static expr_ty
    1599         342 : ast_for_binop(struct compiling *c, const node *n)
    1600             : {
    1601             :         /* Must account for a sequence of expressions.
    1602             :            How should A op B op C by represented?
    1603             :            BinOp(BinOp(A, op, B), op, C).
    1604             :         */
    1605             : 
    1606             :         int i, nops;
    1607             :         expr_ty expr1, expr2, result;
    1608             :         operator_ty newoperator;
    1609             : 
    1610         342 :         expr1 = ast_for_expr(c, CHILD(n, 0));
    1611         342 :         if (!expr1)
    1612           0 :             return NULL;
    1613             : 
    1614         342 :         expr2 = ast_for_expr(c, CHILD(n, 2));
    1615         342 :         if (!expr2)
    1616           0 :             return NULL;
    1617             : 
    1618         342 :         newoperator = get_operator(CHILD(n, 1));
    1619         342 :         if (!newoperator)
    1620           0 :             return NULL;
    1621             : 
    1622         342 :         result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
    1623             :                        c->c_arena);
    1624         342 :         if (!result)
    1625           0 :             return NULL;
    1626             : 
    1627         342 :         nops = (NCH(n) - 1) / 2;
    1628         371 :         for (i = 1; i < nops; i++) {
    1629             :                 expr_ty tmp_result, tmp;
    1630          29 :                 const node* next_oper = CHILD(n, i * 2 + 1);
    1631             : 
    1632          29 :                 newoperator = get_operator(next_oper);
    1633          29 :                 if (!newoperator)
    1634           0 :                     return NULL;
    1635             : 
    1636          29 :                 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
    1637          29 :                 if (!tmp)
    1638           0 :                     return NULL;
    1639             : 
    1640          29 :                 tmp_result = BinOp(result, newoperator, tmp,
    1641             :                                    LINENO(next_oper), next_oper->n_col_offset,
    1642             :                                    c->c_arena);
    1643          29 :                 if (!tmp_result)
    1644           0 :                         return NULL;
    1645          29 :                 result = tmp_result;
    1646             :         }
    1647         342 :         return result;
    1648             : }
    1649             : 
    1650             : static expr_ty
    1651       10133 : ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
    1652             : {
    1653             :     /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
    1654             :        subscriptlist: subscript (',' subscript)* [',']
    1655             :        subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
    1656             :      */
    1657             :     REQ(n, trailer);
    1658       10133 :     if (TYPE(CHILD(n, 0)) == LPAR) {
    1659        3664 :         if (NCH(n) == 2)
    1660         586 :             return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
    1661             :                         n->n_col_offset, c->c_arena);
    1662             :         else
    1663        3078 :             return ast_for_call(c, CHILD(n, 1), left_expr);
    1664             :     }
    1665        6469 :     else if (TYPE(CHILD(n, 0)) == DOT ) {
    1666        6185 :         PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
    1667        6185 :         if (!attr_id)
    1668           0 :             return NULL;
    1669        6185 :         return Attribute(left_expr, attr_id, Load,
    1670             :                          LINENO(n), n->n_col_offset, c->c_arena);
    1671             :     }
    1672             :     else {
    1673             :         REQ(CHILD(n, 0), LSQB);
    1674             :         REQ(CHILD(n, 2), RSQB);
    1675         284 :         n = CHILD(n, 1);
    1676         284 :         if (NCH(n) == 1) {
    1677         284 :             slice_ty slc = ast_for_slice(c, CHILD(n, 0));
    1678         284 :             if (!slc)
    1679           0 :                 return NULL;
    1680         284 :             return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
    1681             :                              c->c_arena);
    1682             :         }
    1683             :         else {
    1684             :             /* The grammar is ambiguous here. The ambiguity is resolved
    1685             :                by treating the sequence as a tuple literal if there are
    1686             :                no slice features.
    1687             :             */
    1688             :             int j;
    1689             :             slice_ty slc;
    1690             :             expr_ty e;
    1691           0 :             bool simple = true;
    1692             :             asdl_seq *slices, *elts;
    1693           0 :             slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
    1694           0 :             if (!slices)
    1695           0 :                 return NULL;
    1696           0 :             for (j = 0; j < NCH(n); j += 2) {
    1697           0 :                 slc = ast_for_slice(c, CHILD(n, j));
    1698           0 :                 if (!slc)
    1699           0 :                     return NULL;
    1700           0 :                 if (slc->kind != Index_kind)
    1701           0 :                     simple = false;
    1702           0 :                 asdl_seq_SET(slices, j / 2, slc);
    1703             :             }
    1704           0 :             if (!simple) {
    1705           0 :                 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
    1706             :                                  Load, LINENO(n), n->n_col_offset, c->c_arena);
    1707             :             }
    1708             :             /* extract Index values and put them in a Tuple */
    1709           0 :             elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
    1710           0 :             if (!elts)
    1711           0 :                 return NULL;
    1712           0 :             for (j = 0; j < asdl_seq_LEN(slices); ++j) {
    1713           0 :                 slc = (slice_ty)asdl_seq_GET(slices, j);
    1714             :                 assert(slc->kind == Index_kind  && slc->v.Index.value);
    1715           0 :                 asdl_seq_SET(elts, j, slc->v.Index.value);
    1716             :             }
    1717           0 :             e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
    1718           0 :             if (!e)
    1719           0 :                 return NULL;
    1720           0 :             return Subscript(left_expr, Index(e, c->c_arena),
    1721             :                              Load, LINENO(n), n->n_col_offset, c->c_arena);
    1722             :         }
    1723             :     }
    1724             : }
    1725             : 
    1726             : static expr_ty
    1727          91 : ast_for_factor(struct compiling *c, const node *n)
    1728             : {
    1729             :     node *pfactor, *ppower, *patom, *pnum;
    1730             :     expr_ty expression;
    1731             : 
    1732             :     /* If the unary - operator is applied to a constant, don't generate
    1733             :        a UNARY_NEGATIVE opcode.  Just store the approriate value as a
    1734             :        constant.  The peephole optimizer already does something like
    1735             :        this but it doesn't handle the case where the constant is
    1736             :        (sys.maxint - 1).  In that case, we want a PyIntObject, not a
    1737             :        PyLongObject.
    1738             :     */
    1739         182 :     if (TYPE(CHILD(n, 0)) == MINUS &&
    1740         182 :         NCH(n) == 2 &&
    1741         182 :         TYPE((pfactor = CHILD(n, 1))) == factor &&
    1742         182 :         NCH(pfactor) == 1 &&
    1743         182 :         TYPE((ppower = CHILD(pfactor, 0))) == power &&
    1744         178 :         NCH(ppower) == 1 &&
    1745         174 :         TYPE((patom = CHILD(ppower, 0))) == atom &&
    1746          87 :         TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
    1747             :         PyObject *pynum;
    1748          87 :         char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
    1749          87 :         if (s == NULL)
    1750           0 :             return NULL;
    1751          87 :         s[0] = '-';
    1752          87 :         strcpy(s + 1, STR(pnum));
    1753          87 :         pynum = parsenumber(c, s);
    1754          87 :         PyObject_FREE(s);
    1755          87 :         if (!pynum)
    1756           0 :             return NULL;
    1757             : 
    1758          87 :         PyArena_AddPyObject(c->c_arena, pynum);
    1759          87 :         return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
    1760             :     }
    1761             : 
    1762           4 :     expression = ast_for_expr(c, CHILD(n, 1));
    1763           4 :     if (!expression)
    1764           0 :         return NULL;
    1765             : 
    1766           4 :     switch (TYPE(CHILD(n, 0))) {
    1767             :         case PLUS:
    1768           0 :             return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
    1769             :                            c->c_arena);
    1770             :         case MINUS:
    1771           4 :             return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
    1772             :                            c->c_arena);
    1773             :         case TILDE:
    1774           0 :             return UnaryOp(Invert, expression, LINENO(n),
    1775             :                            n->n_col_offset, c->c_arena);
    1776             :     }
    1777           0 :     PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
    1778           0 :                  TYPE(CHILD(n, 0)));
    1779           0 :     return NULL;
    1780             : }
    1781             : 
    1782             : static expr_ty
    1783       17911 : ast_for_power(struct compiling *c, const node *n)
    1784             : {
    1785             :     /* power: atom trailer* ('**' factor)*
    1786             :      */
    1787             :     int i;
    1788             :     expr_ty e, tmp;
    1789             :     REQ(n, power);
    1790       17911 :     e = ast_for_atom(c, CHILD(n, 0));
    1791       17911 :     if (!e)
    1792           0 :         return NULL;
    1793       17911 :     if (NCH(n) == 1)
    1794       10840 :         return e;
    1795       17204 :     for (i = 1; i < NCH(n); i++) {
    1796       10134 :         node *ch = CHILD(n, i);
    1797       10134 :         if (TYPE(ch) != trailer)
    1798           1 :             break;
    1799       10133 :         tmp = ast_for_trailer(c, ch, e);
    1800       10133 :         if (!tmp)
    1801           0 :             return NULL;
    1802       10133 :         tmp->lineno = e->lineno;
    1803       10133 :         tmp->col_offset = e->col_offset;
    1804       10133 :         e = tmp;
    1805             :     }
    1806        7071 :     if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
    1807           1 :         expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
    1808           1 :         if (!f)
    1809           0 :             return NULL;
    1810           1 :         tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
    1811           1 :         if (!tmp)
    1812           0 :             return NULL;
    1813           1 :         e = tmp;
    1814             :     }
    1815        7071 :     return e;
    1816             : }
    1817             : 
    1818             : /* Do not name a variable 'expr'!  Will cause a compile error.
    1819             : */
    1820             : 
    1821             : static expr_ty
    1822      224292 : ast_for_expr(struct compiling *c, const node *n)
    1823             : {
    1824             :     /* handle the full range of simple expressions
    1825             :        test: or_test ['if' or_test 'else' test] | lambdef
    1826             :        or_test: and_test ('or' and_test)*
    1827             :        and_test: not_test ('and' not_test)*
    1828             :        not_test: 'not' not_test | comparison
    1829             :        comparison: expr (comp_op expr)*
    1830             :        expr: xor_expr ('|' xor_expr)*
    1831             :        xor_expr: and_expr ('^' and_expr)*
    1832             :        and_expr: shift_expr ('&' shift_expr)*
    1833             :        shift_expr: arith_expr (('<<'|'>>') arith_expr)*
    1834             :        arith_expr: term (('+'|'-') term)*
    1835             :        term: factor (('*'|'/'|'%'|'//') factor)*
    1836             :        factor: ('+'|'-'|'~') factor | power
    1837             :        power: atom trailer* ('**' factor)*
    1838             : 
    1839             :        As well as modified versions that exist for backward compatibility,
    1840             :        to explicitly allow:
    1841             :        [ x for x in lambda: 0, lambda: 1 ]
    1842             :        (which would be ambiguous without these extra rules)
    1843             : 
    1844             :        old_test: or_test | old_lambdef
    1845             :        old_lambdef: 'lambda' [vararglist] ':' old_test
    1846             : 
    1847             :     */
    1848             : 
    1849             :     asdl_seq *seq;
    1850             :     int i;
    1851             : 
    1852             :  loop:
    1853      224292 :     switch (TYPE(n)) {
    1854             :         case test:
    1855             :         case old_test:
    1856       32770 :             if (TYPE(CHILD(n, 0)) == lambdef ||
    1857       16383 :                 TYPE(CHILD(n, 0)) == old_lambdef)
    1858           4 :                 return ast_for_lambdef(c, CHILD(n, 0));
    1859       16383 :             else if (NCH(n) > 1)
    1860          14 :                 return ast_for_ifexpr(c, n);
    1861             :             /* Fallthrough */
    1862             :         case or_test:
    1863             :         case and_test:
    1864       49214 :             if (NCH(n) == 1) {
    1865       49158 :                 n = CHILD(n, 0);
    1866       49158 :                 goto loop;
    1867             :             }
    1868          56 :             seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
    1869          56 :             if (!seq)
    1870           0 :                 return NULL;
    1871         174 :             for (i = 0; i < NCH(n); i += 2) {
    1872         118 :                 expr_ty e = ast_for_expr(c, CHILD(n, i));
    1873         118 :                 if (!e)
    1874           0 :                     return NULL;
    1875         118 :                 asdl_seq_SET(seq, i / 2, e);
    1876             :             }
    1877          56 :             if (!strcmp(STR(CHILD(n, 1)), "and"))
    1878          30 :                 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
    1879             :                               c->c_arena);
    1880             :             assert(!strcmp(STR(CHILD(n, 1)), "or"));
    1881          26 :             return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
    1882             :         case not_test:
    1883       16720 :             if (NCH(n) == 1) {
    1884       16470 :                 n = CHILD(n, 0);
    1885       16470 :                 goto loop;
    1886             :             }
    1887             :             else {
    1888         250 :                 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
    1889         250 :                 if (!expression)
    1890           0 :                     return NULL;
    1891             : 
    1892         250 :                 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
    1893             :                                c->c_arena);
    1894             :             }
    1895             :         case comparison:
    1896       16470 :             if (NCH(n) == 1) {
    1897       15559 :                 n = CHILD(n, 0);
    1898       15559 :                 goto loop;
    1899             :             }
    1900             :             else {
    1901             :                 expr_ty expression;
    1902             :                 asdl_int_seq *ops;
    1903             :                 asdl_seq *cmps;
    1904         911 :                 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
    1905         911 :                 if (!ops)
    1906           0 :                     return NULL;
    1907         911 :                 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
    1908         911 :                 if (!cmps) {
    1909           0 :                     return NULL;
    1910             :                 }
    1911        1822 :                 for (i = 1; i < NCH(n); i += 2) {
    1912             :                     cmpop_ty newoperator;
    1913             : 
    1914         911 :                     newoperator = ast_for_comp_op(c, CHILD(n, i));
    1915         911 :                     if (!newoperator) {
    1916           0 :                         return NULL;
    1917             :                     }
    1918             : 
    1919         911 :                     expression = ast_for_expr(c, CHILD(n, i + 1));
    1920         911 :                     if (!expression) {
    1921           0 :                         return NULL;
    1922             :                     }
    1923             : 
    1924         911 :                     asdl_seq_SET(ops, i / 2, newoperator);
    1925         911 :                     asdl_seq_SET(cmps, i / 2, expression);
    1926             :                 }
    1927         911 :                 expression = ast_for_expr(c, CHILD(n, 0));
    1928         911 :                 if (!expression) {
    1929           0 :                     return NULL;
    1930             :                 }
    1931             : 
    1932         911 :                 return Compare(expression, ops, cmps, LINENO(n),
    1933             :                                n->n_col_offset, c->c_arena);
    1934             :             }
    1935             :             break;
    1936             : 
    1937             :         /* The next five cases all handle BinOps.  The main body of code
    1938             :            is the same in each case, but the switch turned inside out to
    1939             :            reuse the code for each type of operator.
    1940             :          */
    1941             :         case expr:
    1942             :         case xor_expr:
    1943             :         case and_expr:
    1944             :         case shift_expr:
    1945             :         case arith_expr:
    1946             :         case term:
    1947      105945 :             if (NCH(n) == 1) {
    1948      105603 :                 n = CHILD(n, 0);
    1949      105603 :                 goto loop;
    1950             :             }
    1951         342 :             return ast_for_binop(c, n);
    1952             :         case yield_expr: {
    1953          12 :             expr_ty exp = NULL;
    1954          12 :             if (NCH(n) == 2) {
    1955          12 :                 exp = ast_for_testlist(c, CHILD(n, 1));
    1956          12 :                 if (!exp)
    1957           0 :                     return NULL;
    1958             :             }
    1959          12 :             return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
    1960             :         }
    1961             :         case factor:
    1962       18002 :             if (NCH(n) == 1) {
    1963       17911 :                 n = CHILD(n, 0);
    1964       17911 :                 goto loop;
    1965             :             }
    1966          91 :             return ast_for_factor(c, n);
    1967             :         case power:
    1968       17911 :             return ast_for_power(c, n);
    1969             :         default:
    1970           0 :             PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
    1971           0 :             return NULL;
    1972             :     }
    1973             :     /* should never get here unless if error is set */
    1974             :     return NULL;
    1975             : }
    1976             : 
    1977             : static expr_ty
    1978        3078 : ast_for_call(struct compiling *c, const node *n, expr_ty func)
    1979             : {
    1980             :     /*
    1981             :       arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
    1982             :                | '**' test)
    1983             :       argument: [test '='] test [comp_for]        # Really [keyword '='] test
    1984             :     */
    1985             : 
    1986             :     int i, nargs, nkeywords, ngens;
    1987             :     asdl_seq *args;
    1988             :     asdl_seq *keywords;
    1989        3078 :     expr_ty vararg = NULL, kwarg = NULL;
    1990             : 
    1991             :     REQ(n, arglist);
    1992             : 
    1993        3078 :     nargs = 0;
    1994        3078 :     nkeywords = 0;
    1995        3078 :     ngens = 0;
    1996        9186 :     for (i = 0; i < NCH(n); i++) {
    1997        6108 :         node *ch = CHILD(n, i);
    1998        6108 :         if (TYPE(ch) == argument) {
    1999        4575 :             if (NCH(ch) == 1)
    2000        4210 :                 nargs++;
    2001         365 :             else if (TYPE(CHILD(ch, 1)) == comp_for)
    2002           9 :                 ngens++;
    2003             :             else
    2004         356 :                 nkeywords++;
    2005             :         }
    2006             :     }
    2007        3078 :     if (ngens > 1 || (ngens && (nargs || nkeywords))) {
    2008           0 :         ast_error(n, "Generator expression must be parenthesized "
    2009             :                   "if not sole argument");
    2010           0 :         return NULL;
    2011             :     }
    2012             : 
    2013        3078 :     if (nargs + nkeywords + ngens > 255) {
    2014           0 :       ast_error(n, "more than 255 arguments");
    2015           0 :       return NULL;
    2016             :     }
    2017             : 
    2018        3078 :     args = asdl_seq_new(nargs + ngens, c->c_arena);
    2019        3078 :     if (!args)
    2020           0 :         return NULL;
    2021        3078 :     keywords = asdl_seq_new(nkeywords, c->c_arena);
    2022        3078 :     if (!keywords)
    2023           0 :         return NULL;
    2024        3078 :     nargs = 0;
    2025        3078 :     nkeywords = 0;
    2026        9174 :     for (i = 0; i < NCH(n); i++) {
    2027        6096 :         node *ch = CHILD(n, i);
    2028        6096 :         if (TYPE(ch) == argument) {
    2029             :             expr_ty e;
    2030        4575 :             if (NCH(ch) == 1) {
    2031        4210 :                 if (nkeywords) {
    2032           0 :                     ast_error(CHILD(ch, 0),
    2033             :                               "non-keyword arg after keyword arg");
    2034           0 :                     return NULL;
    2035             :                 }
    2036        4210 :                 if (vararg) {
    2037           0 :                     ast_error(CHILD(ch, 0),
    2038             :                               "only named arguments may follow *expression");
    2039           0 :                     return NULL;
    2040             :                 }
    2041        4210 :                 e = ast_for_expr(c, CHILD(ch, 0));
    2042        4210 :                 if (!e)
    2043           0 :                     return NULL;
    2044        4210 :                 asdl_seq_SET(args, nargs++, e);
    2045             :             }
    2046         365 :             else if (TYPE(CHILD(ch, 1)) == comp_for) {
    2047           9 :                 e = ast_for_genexp(c, ch);
    2048           9 :                 if (!e)
    2049           0 :                     return NULL;
    2050           9 :                 asdl_seq_SET(args, nargs++, e);
    2051             :             }
    2052             :             else {
    2053             :                 keyword_ty kw;
    2054             :                 identifier key;
    2055             :                 int k;
    2056             :                 char *tmp;
    2057             : 
    2058             :                 /* CHILD(ch, 0) is test, but must be an identifier? */
    2059         356 :                 e = ast_for_expr(c, CHILD(ch, 0));
    2060         356 :                 if (!e)
    2061           0 :                     return NULL;
    2062             :                 /* f(lambda x: x[0] = 3) ends up getting parsed with
    2063             :                  * LHS test = lambda x: x[0], and RHS test = 3.
    2064             :                  * SF bug 132313 points out that complaining about a keyword
    2065             :                  * then is very confusing.
    2066             :                  */
    2067         356 :                 if (e->kind == Lambda_kind) {
    2068           0 :                     ast_error(CHILD(ch, 0),
    2069             :                               "lambda cannot contain assignment");
    2070           0 :                     return NULL;
    2071         356 :                 } else if (e->kind != Name_kind) {
    2072           0 :                     ast_error(CHILD(ch, 0), "keyword can't be an expression");
    2073           0 :                     return NULL;
    2074             :                 }
    2075         356 :                 key = e->v.Name.id;
    2076         356 :                 if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
    2077           0 :                     return NULL;
    2078         521 :                 for (k = 0; k < nkeywords; k++) {
    2079         165 :                     tmp = PyString_AS_STRING(
    2080             :                         ((keyword_ty)asdl_seq_GET(keywords, k))->arg);
    2081         165 :                     if (!strcmp(tmp, PyString_AS_STRING(key))) {
    2082           0 :                         ast_error(CHILD(ch, 0), "keyword argument repeated");
    2083           0 :                         return NULL;
    2084             :                     }
    2085             :                 }
    2086         356 :                 e = ast_for_expr(c, CHILD(ch, 2));
    2087         356 :                 if (!e)
    2088           0 :                     return NULL;
    2089         356 :                 kw = keyword(key, e, c->c_arena);
    2090         356 :                 if (!kw)
    2091           0 :                     return NULL;
    2092         356 :                 asdl_seq_SET(keywords, nkeywords++, kw);
    2093             :             }
    2094             :         }
    2095        1521 :         else if (TYPE(ch) == STAR) {
    2096           7 :             vararg = ast_for_expr(c, CHILD(n, i+1));
    2097           7 :             if (!vararg)
    2098           0 :                 return NULL;
    2099           7 :             i++;
    2100             :         }
    2101        1514 :         else if (TYPE(ch) == DOUBLESTAR) {
    2102           5 :             kwarg = ast_for_expr(c, CHILD(n, i+1));
    2103           5 :             if (!kwarg)
    2104           0 :                 return NULL;
    2105           5 :             i++;
    2106             :         }
    2107             :     }
    2108             : 
    2109        3078 :     return Call(func, args, keywords, vararg, kwarg, func->lineno,
    2110             :                 func->col_offset, c->c_arena);
    2111             : }
    2112             : 
    2113             : static expr_ty
    2114        7761 : ast_for_testlist(struct compiling *c, const node* n)
    2115             : {
    2116             :     /* testlist_comp: test (',' test)* [','] */
    2117             :     /* testlist: test (',' test)* [','] */
    2118             :     /* testlist_safe: test (',' test)+ [','] */
    2119             :     /* testlist1: test (',' test)* */
    2120             :     assert(NCH(n) > 0);
    2121        7761 :     if (TYPE(n) == testlist_comp) {
    2122         341 :         if (NCH(n) > 1)
    2123             :             assert(TYPE(CHILD(n, 1)) != comp_for);
    2124             :     }
    2125             :     else {
    2126             :         assert(TYPE(n) == testlist ||
    2127             :                TYPE(n) == testlist_safe ||
    2128             :                TYPE(n) == testlist1);
    2129             :     }
    2130        7761 :     if (NCH(n) == 1)
    2131        7295 :         return ast_for_expr(c, CHILD(n, 0));
    2132             :     else {
    2133         466 :         asdl_seq *tmp = seq_for_testlist(c, n);
    2134         466 :         if (!tmp)
    2135           0 :             return NULL;
    2136         466 :         return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
    2137             :     }
    2138             : }
    2139             : 
    2140             : static expr_ty
    2141         341 : ast_for_testlist_comp(struct compiling *c, const node* n)
    2142             : {
    2143             :     /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
    2144             :     /* argument: test [ comp_for ] */
    2145             :     assert(TYPE(n) == testlist_comp || TYPE(n) == argument);
    2146         341 :     if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == comp_for)
    2147           0 :         return ast_for_genexp(c, n);
    2148         341 :     return ast_for_testlist(c, n);
    2149             : }
    2150             : 
    2151             : /* like ast_for_testlist() but returns a sequence */
    2152             : static asdl_seq*
    2153         119 : ast_for_class_bases(struct compiling *c, const node* n)
    2154             : {
    2155             :     /* testlist: test (',' test)* [','] */
    2156             :     assert(NCH(n) > 0);
    2157             :     REQ(n, testlist);
    2158         119 :     if (NCH(n) == 1) {
    2159             :         expr_ty base;
    2160         119 :         asdl_seq *bases = asdl_seq_new(1, c->c_arena);
    2161         119 :         if (!bases)
    2162           0 :             return NULL;
    2163         119 :         base = ast_for_expr(c, CHILD(n, 0));
    2164         119 :         if (!base)
    2165           0 :             return NULL;
    2166         119 :         asdl_seq_SET(bases, 0, base);
    2167         119 :         return bases;
    2168             :     }
    2169             : 
    2170           0 :     return seq_for_testlist(c, n);
    2171             : }
    2172             : 
    2173             : static stmt_ty
    2174        3986 : ast_for_expr_stmt(struct compiling *c, const node *n)
    2175             : {
    2176             :     REQ(n, expr_stmt);
    2177             :     /* expr_stmt: testlist (augassign (yield_expr|testlist)
    2178             :                 | ('=' (yield_expr|testlist))*)
    2179             :        testlist: test (',' test)* [',']
    2180             :        augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
    2181             :                 | '<<=' | '>>=' | '**=' | '//='
    2182             :        test: ... here starts the operator precedence dance
    2183             :      */
    2184             : 
    2185        3986 :     if (NCH(n) == 1) {
    2186        1731 :         expr_ty e = ast_for_testlist(c, CHILD(n, 0));
    2187        1731 :         if (!e)
    2188           0 :             return NULL;
    2189             : 
    2190        1731 :         return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
    2191             :     }
    2192        2255 :     else if (TYPE(CHILD(n, 1)) == augassign) {
    2193             :         expr_ty expr1, expr2;
    2194             :         operator_ty newoperator;
    2195          48 :         node *ch = CHILD(n, 0);
    2196             : 
    2197          48 :         expr1 = ast_for_testlist(c, ch);
    2198          48 :         if (!expr1)
    2199           0 :             return NULL;
    2200          48 :         if(!set_context(c, expr1, Store, ch))
    2201           0 :             return NULL;
    2202             :         /* set_context checks that most expressions are not the left side.
    2203             :           Augmented assignments can only have a name, a subscript, or an
    2204             :           attribute on the left, though, so we have to explicitly check for
    2205             :           those. */
    2206          48 :         switch (expr1->kind) {
    2207             :             case Name_kind:
    2208             :             case Attribute_kind:
    2209             :             case Subscript_kind:
    2210          48 :                 break;
    2211             :             default:
    2212           0 :                 ast_error(ch, "illegal expression for augmented assignment");
    2213           0 :                 return NULL;
    2214             :         }
    2215             : 
    2216          48 :         ch = CHILD(n, 2);
    2217          48 :         if (TYPE(ch) == testlist)
    2218          48 :             expr2 = ast_for_testlist(c, ch);
    2219             :         else
    2220           0 :             expr2 = ast_for_expr(c, ch);
    2221          48 :         if (!expr2)
    2222           0 :             return NULL;
    2223             : 
    2224          48 :         newoperator = ast_for_augassign(c, CHILD(n, 1));
    2225          48 :         if (!newoperator)
    2226           0 :             return NULL;
    2227             : 
    2228          48 :         return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
    2229             :                          c->c_arena);
    2230             :     }
    2231             :     else {
    2232             :         int i;
    2233             :         asdl_seq *targets;
    2234             :         node *value;
    2235             :         expr_ty expression;
    2236             : 
    2237             :         /* a normal assignment */
    2238             :         REQ(CHILD(n, 1), EQUAL);
    2239        2207 :         targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
    2240        2207 :         if (!targets)
    2241           0 :             return NULL;
    2242        4414 :         for (i = 0; i < NCH(n) - 2; i += 2) {
    2243             :             expr_ty e;
    2244        2207 :             node *ch = CHILD(n, i);
    2245        2207 :             if (TYPE(ch) == yield_expr) {
    2246           0 :                 ast_error(ch, "assignment to yield expression not possible");
    2247           0 :                 return NULL;
    2248             :             }
    2249        2207 :             e = ast_for_testlist(c, ch);
    2250        2207 :             if (!e)
    2251           0 :                 return NULL;
    2252             : 
    2253             :             /* set context to assign */
    2254        2207 :             if (!set_context(c, e, Store, CHILD(n, i)))
    2255           0 :                 return NULL;
    2256             : 
    2257        2207 :             asdl_seq_SET(targets, i / 2, e);
    2258             :         }
    2259        2207 :         value = CHILD(n, NCH(n) - 1);
    2260        2207 :         if (TYPE(value) == testlist)
    2261        2207 :             expression = ast_for_testlist(c, value);
    2262             :         else
    2263           0 :             expression = ast_for_expr(c, value);
    2264        2207 :         if (!expression)
    2265           0 :             return NULL;
    2266        2207 :         return Assign(targets, expression, LINENO(n), n->n_col_offset,
    2267             :                       c->c_arena);
    2268             :     }
    2269             : }
    2270             : 
    2271             : static stmt_ty
    2272           8 : ast_for_print_stmt(struct compiling *c, const node *n)
    2273             : {
    2274             :     /* print_stmt: 'print' ( [ test (',' test)* [','] ]
    2275             :                              | '>>' test [ (',' test)+ [','] ] )
    2276             :      */
    2277           8 :     expr_ty dest = NULL, expression;
    2278           8 :     asdl_seq *seq = NULL;
    2279             :     bool nl;
    2280           8 :     int i, j, values_count, start = 1;
    2281             : 
    2282             :     REQ(n, print_stmt);
    2283           8 :     if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
    2284           0 :         dest = ast_for_expr(c, CHILD(n, 2));
    2285           0 :         if (!dest)
    2286           0 :             return NULL;
    2287           0 :         start = 4;
    2288             :     }
    2289           8 :     values_count = (NCH(n) + 1 - start) / 2;
    2290           8 :     if (values_count) {
    2291           8 :         seq = asdl_seq_new(values_count, c->c_arena);
    2292           8 :         if (!seq)
    2293           0 :             return NULL;
    2294          16 :         for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
    2295           8 :             expression = ast_for_expr(c, CHILD(n, i));
    2296           8 :             if (!expression)
    2297           0 :                 return NULL;
    2298           8 :             asdl_seq_SET(seq, j, expression);
    2299             :         }
    2300             :     }
    2301           8 :     nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
    2302           8 :     return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
    2303             : }
    2304             : 
    2305             : static asdl_seq *
    2306         196 : ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
    2307             : {
    2308             :     asdl_seq *seq;
    2309             :     int i;
    2310             :     expr_ty e;
    2311             : 
    2312             :     REQ(n, exprlist);
    2313             : 
    2314         196 :     seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
    2315         196 :     if (!seq)
    2316           0 :         return NULL;
    2317         433 :     for (i = 0; i < NCH(n); i += 2) {
    2318         237 :         e = ast_for_expr(c, CHILD(n, i));
    2319         237 :         if (!e)
    2320           0 :             return NULL;
    2321         237 :         asdl_seq_SET(seq, i / 2, e);
    2322         237 :         if (context && !set_context(c, e, context, CHILD(n, i)))
    2323           0 :             return NULL;
    2324             :     }
    2325         196 :     return seq;
    2326             : }
    2327             : 
    2328             : static stmt_ty
    2329           2 : ast_for_del_stmt(struct compiling *c, const node *n)
    2330             : {
    2331             :     asdl_seq *expr_list;
    2332             : 
    2333             :     /* del_stmt: 'del' exprlist */
    2334             :     REQ(n, del_stmt);
    2335             : 
    2336           2 :     expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
    2337           2 :     if (!expr_list)
    2338           0 :         return NULL;
    2339           2 :     return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
    2340             : }
    2341             : 
    2342             : static stmt_ty
    2343        1300 : ast_for_flow_stmt(struct compiling *c, const node *n)
    2344             : {
    2345             :     /*
    2346             :       flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
    2347             :                  | yield_stmt
    2348             :       break_stmt: 'break'
    2349             :       continue_stmt: 'continue'
    2350             :       return_stmt: 'return' [testlist]
    2351             :       yield_stmt: yield_expr
    2352             :       yield_expr: 'yield' testlist
    2353             :       raise_stmt: 'raise' [test [',' test [',' test]]]
    2354             :     */
    2355             :     node *ch;
    2356             : 
    2357             :     REQ(n, flow_stmt);
    2358        1300 :     ch = CHILD(n, 0);
    2359        1300 :     switch (TYPE(ch)) {
    2360             :         case break_stmt:
    2361          35 :             return Break(LINENO(n), n->n_col_offset, c->c_arena);
    2362             :         case continue_stmt:
    2363           9 :             return Continue(LINENO(n), n->n_col_offset, c->c_arena);
    2364             :         case yield_stmt: { /* will reduce to yield_expr */
    2365          12 :             expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
    2366          12 :             if (!exp)
    2367           0 :                 return NULL;
    2368          12 :             return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
    2369             :         }
    2370             :         case return_stmt:
    2371         999 :             if (NCH(ch) == 1)
    2372          15 :                 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
    2373             :             else {
    2374         984 :                 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
    2375         984 :                 if (!expression)
    2376           0 :                     return NULL;
    2377         984 :                 return Return(expression, LINENO(n), n->n_col_offset,
    2378             :                               c->c_arena);
    2379             :             }
    2380             :         case raise_stmt:
    2381         245 :             if (NCH(ch) == 1)
    2382          12 :                 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset,
    2383             :                              c->c_arena);
    2384         233 :             else if (NCH(ch) == 2) {
    2385         233 :                 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
    2386         233 :                 if (!expression)
    2387           0 :                     return NULL;
    2388         233 :                 return Raise(expression, NULL, NULL, LINENO(n),
    2389             :                              n->n_col_offset, c->c_arena);
    2390             :             }
    2391           0 :             else if (NCH(ch) == 4) {
    2392             :                 expr_ty expr1, expr2;
    2393             : 
    2394           0 :                 expr1 = ast_for_expr(c, CHILD(ch, 1));
    2395           0 :                 if (!expr1)
    2396           0 :                     return NULL;
    2397           0 :                 expr2 = ast_for_expr(c, CHILD(ch, 3));
    2398           0 :                 if (!expr2)
    2399           0 :                     return NULL;
    2400             : 
    2401           0 :                 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset,
    2402             :                              c->c_arena);
    2403             :             }
    2404           0 :             else if (NCH(ch) == 6) {
    2405             :                 expr_ty expr1, expr2, expr3;
    2406             : 
    2407           0 :                 expr1 = ast_for_expr(c, CHILD(ch, 1));
    2408           0 :                 if (!expr1)
    2409           0 :                     return NULL;
    2410           0 :                 expr2 = ast_for_expr(c, CHILD(ch, 3));
    2411           0 :                 if (!expr2)
    2412           0 :                     return NULL;
    2413           0 :                 expr3 = ast_for_expr(c, CHILD(ch, 5));
    2414           0 :                 if (!expr3)
    2415           0 :                     return NULL;
    2416             : 
    2417           0 :                 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset,
    2418             :                              c->c_arena);
    2419             :             }
    2420             :         default:
    2421           0 :             PyErr_Format(PyExc_SystemError,
    2422           0 :                          "unexpected flow_stmt: %d", TYPE(ch));
    2423           0 :             return NULL;
    2424             :     }
    2425             : 
    2426             :     PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
    2427             :     return NULL;
    2428             : }
    2429             : 
    2430             : static alias_ty
    2431         524 : alias_for_import_name(struct compiling *c, const node *n, int store)
    2432             : {
    2433             :     /*
    2434             :       import_as_name: NAME ['as' NAME]
    2435             :       dotted_as_name: dotted_name ['as' NAME]
    2436             :       dotted_name: NAME ('.' NAME)*
    2437             :     */
    2438             :     PyObject *str, *name;
    2439             : 
    2440             :  loop:
    2441         524 :     switch (TYPE(n)) {
    2442             :          case import_as_name: {
    2443         210 :             node *name_node = CHILD(n, 0);
    2444         210 :             str = NULL;
    2445         210 :             if (NCH(n) == 3) {
    2446          30 :                 node *str_node = CHILD(n, 2);
    2447          30 :                 if (store && !forbidden_check(c, str_node, STR(str_node)))
    2448           0 :                     return NULL;
    2449          30 :                 str = NEW_IDENTIFIER(str_node);
    2450          30 :                 if (!str)
    2451           0 :                     return NULL;
    2452             :             }
    2453             :             else {
    2454         180 :                 if (!forbidden_check(c, name_node, STR(name_node)))
    2455           0 :                     return NULL;
    2456             :             }
    2457         210 :             name = NEW_IDENTIFIER(name_node);
    2458         210 :             if (!name)
    2459           0 :                 return NULL;
    2460         210 :             return alias(name, str, c->c_arena);
    2461             :         }
    2462             :         case dotted_as_name:
    2463          69 :             if (NCH(n) == 1) {
    2464          69 :                 n = CHILD(n, 0);
    2465          69 :                 goto loop;
    2466             :             }
    2467             :             else {
    2468           0 :                 node *asname_node = CHILD(n, 2);
    2469           0 :                 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
    2470           0 :                 if (!a)
    2471           0 :                     return NULL;
    2472             :                 assert(!a->asname);
    2473           0 :                 if (!forbidden_check(c, asname_node, STR(asname_node)))
    2474           0 :                     return NULL;
    2475           0 :                 a->asname = NEW_IDENTIFIER(asname_node);
    2476           0 :                 if (!a->asname)
    2477           0 :                     return NULL;
    2478           0 :                 return a;
    2479             :             }
    2480             :             break;
    2481             :         case dotted_name:
    2482         245 :             if (NCH(n) == 1) {
    2483         210 :                 node *name_node = CHILD(n, 0);
    2484         210 :                 if (store && !forbidden_check(c, name_node, STR(name_node)))
    2485           0 :                     return NULL;
    2486         210 :                 name = NEW_IDENTIFIER(name_node);
    2487         210 :                 if (!name)
    2488           0 :                     return NULL;
    2489         210 :                 return alias(name, NULL, c->c_arena);
    2490             :             }
    2491             :             else {
    2492             :                 /* Create a string of the form "a.b.c" */
    2493             :                 int i;
    2494             :                 size_t len;
    2495             :                 char *s;
    2496             : 
    2497          35 :                 len = 0;
    2498         105 :                 for (i = 0; i < NCH(n); i += 2)
    2499             :                     /* length of string plus one for the dot */
    2500          70 :                     len += strlen(STR(CHILD(n, i))) + 1;
    2501          35 :                 len--; /* the last name doesn't have a dot */
    2502          35 :                 str = PyString_FromStringAndSize(NULL, len);
    2503          35 :                 if (!str)
    2504           0 :                     return NULL;
    2505          35 :                 s = PyString_AS_STRING(str);
    2506          35 :                 if (!s)
    2507           0 :                     return NULL;
    2508         105 :                 for (i = 0; i < NCH(n); i += 2) {
    2509          70 :                     char *sch = STR(CHILD(n, i));
    2510          70 :                     strcpy(s, STR(CHILD(n, i)));
    2511          70 :                     s += strlen(sch);
    2512          70 :                     *s++ = '.';
    2513             :                 }
    2514          35 :                 --s;
    2515          35 :                 *s = '\0';
    2516          35 :                 PyString_InternInPlace(&str);
    2517          35 :                 PyArena_AddPyObject(c->c_arena, str);
    2518          35 :                 return alias(str, NULL, c->c_arena);
    2519             :             }
    2520             :             break;
    2521             :         case STAR:
    2522           0 :             str = PyString_InternFromString("*");
    2523           0 :             PyArena_AddPyObject(c->c_arena, str);
    2524           0 :             return alias(str, NULL, c->c_arena);
    2525             :         default:
    2526           0 :             PyErr_Format(PyExc_SystemError,
    2527           0 :                          "unexpected import name: %d", TYPE(n));
    2528           0 :             return NULL;
    2529             :     }
    2530             : 
    2531             :     PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
    2532             :     return NULL;
    2533             : }
    2534             : 
    2535             : static stmt_ty
    2536         245 : ast_for_import_stmt(struct compiling *c, const node *n)
    2537             : {
    2538             :     /*
    2539             :       import_stmt: import_name | import_from
    2540             :       import_name: 'import' dotted_as_names
    2541             :       import_from: 'from' ('.'* dotted_name | '.') 'import'
    2542             :                           ('*' | '(' import_as_names ')' | import_as_names)
    2543             :     */
    2544             :     int lineno;
    2545             :     int col_offset;
    2546             :     int i;
    2547             :     asdl_seq *aliases;
    2548             : 
    2549             :     REQ(n, import_stmt);
    2550         245 :     lineno = LINENO(n);
    2551         245 :     col_offset = n->n_col_offset;
    2552         245 :     n = CHILD(n, 0);
    2553         245 :     if (TYPE(n) == import_name) {
    2554          69 :         n = CHILD(n, 1);
    2555             :         REQ(n, dotted_as_names);
    2556          69 :         aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
    2557          69 :         if (!aliases)
    2558           0 :             return NULL;
    2559         138 :         for (i = 0; i < NCH(n); i += 2) {
    2560          69 :             alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
    2561          69 :             if (!import_alias)
    2562           0 :                 return NULL;
    2563          69 :             asdl_seq_SET(aliases, i / 2, import_alias);
    2564             :         }
    2565          69 :         return Import(aliases, lineno, col_offset, c->c_arena);
    2566             :     }
    2567         176 :     else if (TYPE(n) == import_from) {
    2568             :         int n_children;
    2569         176 :         int idx, ndots = 0;
    2570         176 :         alias_ty mod = NULL;
    2571         176 :         identifier modname = NULL;
    2572             : 
    2573             :        /* Count the number of dots (for relative imports) and check for the
    2574             :           optional module name */
    2575         176 :         for (idx = 1; idx < NCH(n); idx++) {
    2576         176 :             if (TYPE(CHILD(n, idx)) == dotted_name) {
    2577         176 :                 mod = alias_for_import_name(c, CHILD(n, idx), 0);
    2578         176 :                 if (!mod)
    2579           0 :                     return NULL;
    2580         176 :                 idx++;
    2581         176 :                 break;
    2582           0 :             } else if (TYPE(CHILD(n, idx)) != DOT) {
    2583           0 :                 break;
    2584             :             }
    2585           0 :             ndots++;
    2586             :         }
    2587         176 :         idx++; /* skip over the 'import' keyword */
    2588         176 :         switch (TYPE(CHILD(n, idx))) {
    2589             :         case STAR:
    2590             :             /* from ... import * */
    2591           0 :             n = CHILD(n, idx);
    2592           0 :             n_children = 1;
    2593           0 :             break;
    2594             :         case LPAR:
    2595             :             /* from ... import (x, y, z) */
    2596           1 :             n = CHILD(n, idx + 1);
    2597           1 :             n_children = NCH(n);
    2598           1 :             break;
    2599             :         case import_as_names:
    2600             :             /* from ... import x, y, z */
    2601         175 :             n = CHILD(n, idx);
    2602         175 :             n_children = NCH(n);
    2603         175 :             if (n_children % 2 == 0) {
    2604           0 :                 ast_error(n, "trailing comma not allowed without"
    2605             :                              " surrounding parentheses");
    2606           0 :                 return NULL;
    2607             :             }
    2608         175 :             break;
    2609             :         default:
    2610           0 :             ast_error(n, "Unexpected node-type in from-import");
    2611           0 :             return NULL;
    2612             :         }
    2613             : 
    2614         176 :         aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
    2615         176 :         if (!aliases)
    2616           0 :             return NULL;
    2617             : 
    2618             :         /* handle "from ... import *" special b/c there's no children */
    2619         176 :         if (TYPE(n) == STAR) {
    2620           0 :             alias_ty import_alias = alias_for_import_name(c, n, 1);
    2621           0 :             if (!import_alias)
    2622           0 :                 return NULL;
    2623           0 :                 asdl_seq_SET(aliases, 0, import_alias);
    2624             :         }
    2625             :         else {
    2626         386 :             for (i = 0; i < NCH(n); i += 2) {
    2627         210 :                 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
    2628         210 :                 if (!import_alias)
    2629           0 :                     return NULL;
    2630         210 :                     asdl_seq_SET(aliases, i / 2, import_alias);
    2631             :             }
    2632             :         }
    2633         176 :         if (mod != NULL)
    2634         176 :             modname = mod->name;
    2635         176 :         return ImportFrom(modname, aliases, ndots, lineno, col_offset,
    2636             :                           c->c_arena);
    2637             :     }
    2638           0 :     PyErr_Format(PyExc_SystemError,
    2639             :                  "unknown import statement: starts with command '%s'",
    2640           0 :                  STR(CHILD(n, 0)));
    2641           0 :     return NULL;
    2642             : }
    2643             : 
    2644             : static stmt_ty
    2645           0 : ast_for_global_stmt(struct compiling *c, const node *n)
    2646             : {
    2647             :     /* global_stmt: 'global' NAME (',' NAME)* */
    2648             :     identifier name;
    2649             :     asdl_seq *s;
    2650             :     int i;
    2651             : 
    2652             :     REQ(n, global_stmt);
    2653           0 :     s = asdl_seq_new(NCH(n) / 2, c->c_arena);
    2654           0 :     if (!s)
    2655           0 :         return NULL;
    2656           0 :     for (i = 1; i < NCH(n); i += 2) {
    2657           0 :         name = NEW_IDENTIFIER(CHILD(n, i));
    2658           0 :         if (!name)
    2659           0 :             return NULL;
    2660           0 :         asdl_seq_SET(s, i / 2, name);
    2661             :     }
    2662           0 :     return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
    2663             : }
    2664             : 
    2665             : static stmt_ty
    2666           0 : ast_for_exec_stmt(struct compiling *c, const node *n)
    2667             : {
    2668           0 :     expr_ty expr1, globals = NULL, locals = NULL;
    2669           0 :     int n_children = NCH(n);
    2670           0 :     if (n_children != 2 && n_children != 4 && n_children != 6) {
    2671           0 :         PyErr_Format(PyExc_SystemError,
    2672             :                      "poorly formed 'exec' statement: %d parts to statement",
    2673             :                      n_children);
    2674           0 :         return NULL;
    2675             :     }
    2676             : 
    2677             :     /* exec_stmt: 'exec' expr ['in' test [',' test]] */
    2678             :     REQ(n, exec_stmt);
    2679           0 :     expr1 = ast_for_expr(c, CHILD(n, 1));
    2680           0 :     if (!expr1)
    2681           0 :         return NULL;
    2682             : 
    2683           0 :     if (expr1->kind == Tuple_kind && n_children < 4 &&
    2684           0 :         (asdl_seq_LEN(expr1->v.Tuple.elts) == 2 ||
    2685           0 :          asdl_seq_LEN(expr1->v.Tuple.elts) == 3)) {
    2686             :         /* Backwards compatibility: passing exec args as a tuple */
    2687           0 :         globals = asdl_seq_GET(expr1->v.Tuple.elts, 1);
    2688           0 :         if (asdl_seq_LEN(expr1->v.Tuple.elts) == 3) {
    2689           0 :             locals = asdl_seq_GET(expr1->v.Tuple.elts, 2);
    2690             :         }
    2691           0 :         expr1 = asdl_seq_GET(expr1->v.Tuple.elts, 0);
    2692             :     }
    2693             : 
    2694           0 :     if (n_children >= 4) {
    2695           0 :         globals = ast_for_expr(c, CHILD(n, 3));
    2696           0 :         if (!globals)
    2697           0 :             return NULL;
    2698             :     }
    2699           0 :     if (n_children == 6) {
    2700           0 :         locals = ast_for_expr(c, CHILD(n, 5));
    2701           0 :         if (!locals)
    2702           0 :             return NULL;
    2703             :     }
    2704             : 
    2705           0 :     return Exec(expr1, globals, locals, LINENO(n), n->n_col_offset,
    2706             :                 c->c_arena);
    2707             : }
    2708             : 
    2709             : static stmt_ty
    2710          73 : ast_for_assert_stmt(struct compiling *c, const node *n)
    2711             : {
    2712             :     /* assert_stmt: 'assert' test [',' test] */
    2713             :     REQ(n, assert_stmt);
    2714          73 :     if (NCH(n) == 2) {
    2715          42 :         expr_ty expression = ast_for_expr(c, CHILD(n, 1));
    2716          42 :         if (!expression)
    2717           0 :             return NULL;
    2718          42 :         return Assert(expression, NULL, LINENO(n), n->n_col_offset,
    2719             :                       c->c_arena);
    2720             :     }
    2721          31 :     else if (NCH(n) == 4) {
    2722             :         expr_ty expr1, expr2;
    2723             : 
    2724          31 :         expr1 = ast_for_expr(c, CHILD(n, 1));
    2725          31 :         if (!expr1)
    2726           0 :             return NULL;
    2727          31 :         expr2 = ast_for_expr(c, CHILD(n, 3));
    2728          31 :         if (!expr2)
    2729           0 :             return NULL;
    2730             : 
    2731          31 :         return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
    2732             :     }
    2733           0 :     PyErr_Format(PyExc_SystemError,
    2734             :                  "improper number of parts to 'assert' statement: %d",
    2735             :                  NCH(n));
    2736           0 :     return NULL;
    2737             : }
    2738             : 
    2739             : static asdl_seq *
    2740        2879 : ast_for_suite(struct compiling *c, const node *n)
    2741             : {
    2742             :     /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
    2743             :     asdl_seq *seq;
    2744             :     stmt_ty s;
    2745        2879 :     int i, total, num, end, pos = 0;
    2746             :     node *ch;
    2747             : 
    2748             :     REQ(n, suite);
    2749             : 
    2750        2879 :     total = num_stmts(n);
    2751        2879 :     seq = asdl_seq_new(total, c->c_arena);
    2752        2879 :     if (!seq)
    2753           0 :         return NULL;
    2754        2879 :     if (TYPE(CHILD(n, 0)) == simple_stmt) {
    2755         110 :         n = CHILD(n, 0);
    2756             :         /* simple_stmt always ends with a NEWLINE,
    2757             :            and may have a trailing SEMI
    2758             :         */
    2759         110 :         end = NCH(n) - 1;
    2760         110 :         if (TYPE(CHILD(n, end - 1)) == SEMI)
    2761           0 :             end--;
    2762             :         /* loop by 2 to skip semi-colons */
    2763         220 :         for (i = 0; i < end; i += 2) {
    2764         110 :             ch = CHILD(n, i);
    2765         110 :             s = ast_for_stmt(c, ch);
    2766         110 :             if (!s)
    2767           0 :                 return NULL;
    2768         110 :             asdl_seq_SET(seq, pos++, s);
    2769             :         }
    2770             :     }
    2771             :     else {
    2772        9884 :         for (i = 2; i < (NCH(n) - 1); i++) {
    2773        7115 :             ch = CHILD(n, i);
    2774             :             REQ(ch, stmt);
    2775        7115 :             num = num_stmts(ch);
    2776        7115 :             if (num == 1) {
    2777             :                 /* small_stmt or compound_stmt with only one child */
    2778        7115 :                 s = ast_for_stmt(c, ch);
    2779        7115 :                 if (!s)
    2780           0 :                     return NULL;
    2781        7115 :                 asdl_seq_SET(seq, pos++, s);
    2782             :             }
    2783             :             else {
    2784             :                 int j;
    2785           0 :                 ch = CHILD(ch, 0);
    2786             :                 REQ(ch, simple_stmt);
    2787           0 :                 for (j = 0; j < NCH(ch); j += 2) {
    2788             :                     /* statement terminates with a semi-colon ';' */
    2789           0 :                     if (NCH(CHILD(ch, j)) == 0) {
    2790             :                         assert((j + 1) == NCH(ch));
    2791           0 :                         break;
    2792             :                     }
    2793           0 :                     s = ast_for_stmt(c, CHILD(ch, j));
    2794           0 :                     if (!s)
    2795           0 :                         return NULL;
    2796           0 :                     asdl_seq_SET(seq, pos++, s);
    2797             :                 }
    2798             :             }
    2799             :         }
    2800             :     }
    2801             :     assert(pos == seq->size);
    2802        2879 :     return seq;
    2803             : }
    2804             : 
    2805             : static stmt_ty
    2806         970 : ast_for_if_stmt(struct compiling *c, const node *n)
    2807             : {
    2808             :     /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
    2809             :        ['else' ':' suite]
    2810             :     */
    2811             :     char *s;
    2812             : 
    2813             :     REQ(n, if_stmt);
    2814             : 
    2815         970 :     if (NCH(n) == 4) {
    2816             :         expr_ty expression;
    2817             :         asdl_seq *suite_seq;
    2818             : 
    2819         658 :         expression = ast_for_expr(c, CHILD(n, 1));
    2820         658 :         if (!expression)
    2821           0 :             return NULL;
    2822         658 :         suite_seq = ast_for_suite(c, CHILD(n, 3));
    2823         658 :         if (!suite_seq)
    2824           0 :             return NULL;
    2825             : 
    2826         658 :         return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
    2827             :                   c->c_arena);
    2828             :     }
    2829             : 
    2830         312 :     s = STR(CHILD(n, 4));
    2831             :     /* s[2], the third character in the string, will be
    2832             :        's' for el_s_e, or
    2833             :        'i' for el_i_f
    2834             :     */
    2835         312 :     if (s[2] == 's') {
    2836             :         expr_ty expression;
    2837             :         asdl_seq *seq1, *seq2;
    2838             : 
    2839         176 :         expression = ast_for_expr(c, CHILD(n, 1));
    2840         176 :         if (!expression)
    2841           0 :             return NULL;
    2842         176 :         seq1 = ast_for_suite(c, CHILD(n, 3));
    2843         176 :         if (!seq1)
    2844           0 :             return NULL;
    2845         176 :         seq2 = ast_for_suite(c, CHILD(n, 6));
    2846         176 :         if (!seq2)
    2847           0 :             return NULL;
    2848             : 
    2849         176 :         return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
    2850             :                   c->c_arena);
    2851             :     }
    2852         136 :     else if (s[2] == 'i') {
    2853         136 :         int i, n_elif, has_else = 0;
    2854             :         expr_ty expression;
    2855             :         asdl_seq *suite_seq;
    2856         136 :         asdl_seq *orelse = NULL;
    2857         136 :         n_elif = NCH(n) - 4;
    2858             :         /* must reference the child n_elif+1 since 'else' token is third,
    2859             :            not fourth, child from the end. */
    2860         136 :         if (TYPE(CHILD(n, (n_elif + 1))) == NAME
    2861          99 :             && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
    2862          99 :             has_else = 1;
    2863          99 :             n_elif -= 3;
    2864             :         }
    2865         136 :         n_elif /= 4;
    2866             : 
    2867         136 :         if (has_else) {
    2868             :             asdl_seq *suite_seq2;
    2869             : 
    2870          99 :             orelse = asdl_seq_new(1, c->c_arena);
    2871          99 :             if (!orelse)
    2872           0 :                 return NULL;
    2873          99 :             expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
    2874          99 :             if (!expression)
    2875           0 :                 return NULL;
    2876          99 :             suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
    2877          99 :             if (!suite_seq)
    2878           0 :                 return NULL;
    2879          99 :             suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
    2880          99 :             if (!suite_seq2)
    2881           0 :                 return NULL;
    2882             : 
    2883          99 :             asdl_seq_SET(orelse, 0,
    2884             :                          If(expression, suite_seq, suite_seq2,
    2885             :                             LINENO(CHILD(n, NCH(n) - 6)),
    2886             :                             CHILD(n, NCH(n) - 6)->n_col_offset,
    2887             :                             c->c_arena));
    2888             :             /* the just-created orelse handled the last elif */
    2889          99 :             n_elif--;
    2890             :         }
    2891             : 
    2892         360 :         for (i = 0; i < n_elif; i++) {
    2893         224 :             int off = 5 + (n_elif - i - 1) * 4;
    2894         224 :             asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
    2895         224 :             if (!newobj)
    2896           0 :                 return NULL;
    2897         224 :             expression = ast_for_expr(c, CHILD(n, off));
    2898         224 :             if (!expression)
    2899           0 :                 return NULL;
    2900         224 :             suite_seq = ast_for_suite(c, CHILD(n, off + 2));
    2901         224 :             if (!suite_seq)
    2902           0 :                 return NULL;
    2903             : 
    2904         224 :             asdl_seq_SET(newobj, 0,
    2905             :                          If(expression, suite_seq, orelse,
    2906             :                             LINENO(CHILD(n, off)),
    2907             :                             CHILD(n, off)->n_col_offset, c->c_arena));
    2908         224 :             orelse = newobj;
    2909             :         }
    2910         136 :         expression = ast_for_expr(c, CHILD(n, 1));
    2911         136 :         if (!expression)
    2912           0 :             return NULL;
    2913         136 :         suite_seq = ast_for_suite(c, CHILD(n, 3));
    2914         136 :         if (!suite_seq)
    2915           0 :             return NULL;
    2916         136 :         return If(expression, suite_seq, orelse,
    2917             :                   LINENO(n), n->n_col_offset, c->c_arena);
    2918             :     }
    2919             : 
    2920           0 :     PyErr_Format(PyExc_SystemError,
    2921             :                  "unexpected token in 'if' statement: %s", s);
    2922           0 :     return NULL;
    2923             : }
    2924             : 
    2925             : static stmt_ty
    2926          31 : ast_for_while_stmt(struct compiling *c, const node *n)
    2927             : {
    2928             :     /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
    2929             :     REQ(n, while_stmt);
    2930             : 
    2931          31 :     if (NCH(n) == 4) {
    2932             :         expr_ty expression;
    2933             :         asdl_seq *suite_seq;
    2934             : 
    2935          31 :         expression = ast_for_expr(c, CHILD(n, 1));
    2936          31 :         if (!expression)
    2937           0 :             return NULL;
    2938          31 :         suite_seq = ast_for_suite(c, CHILD(n, 3));
    2939          31 :         if (!suite_seq)
    2940           0 :             return NULL;
    2941          31 :         return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
    2942             :                      c->c_arena);
    2943             :     }
    2944           0 :     else if (NCH(n) == 7) {
    2945             :         expr_ty expression;
    2946             :         asdl_seq *seq1, *seq2;
    2947             : 
    2948           0 :         expression = ast_for_expr(c, CHILD(n, 1));
    2949           0 :         if (!expression)
    2950           0 :             return NULL;
    2951           0 :         seq1 = ast_for_suite(c, CHILD(n, 3));
    2952           0 :         if (!seq1)
    2953           0 :             return NULL;
    2954           0 :         seq2 = ast_for_suite(c, CHILD(n, 6));
    2955           0 :         if (!seq2)
    2956           0 :             return NULL;
    2957             : 
    2958           0 :         return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
    2959             :                      c->c_arena);
    2960             :     }
    2961             : 
    2962           0 :     PyErr_Format(PyExc_SystemError,
    2963             :                  "wrong number of tokens for 'while' statement: %d",
    2964             :                  NCH(n));
    2965           0 :     return NULL;
    2966             : }
    2967             : 
    2968             : static stmt_ty
    2969         172 : ast_for_for_stmt(struct compiling *c, const node *n)
    2970             : {
    2971         172 :     asdl_seq *_target, *seq = NULL, *suite_seq;
    2972             :     expr_ty expression;
    2973             :     expr_ty target, first;
    2974             :     const node *node_target;
    2975             :     /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
    2976             :     REQ(n, for_stmt);
    2977             : 
    2978         172 :     if (NCH(n) == 9) {
    2979           0 :         seq = ast_for_suite(c, CHILD(n, 8));
    2980           0 :         if (!seq)
    2981           0 :             return NULL;
    2982             :     }
    2983             : 
    2984         172 :     node_target = CHILD(n, 1);
    2985         172 :     _target = ast_for_exprlist(c, node_target, Store);
    2986         172 :     if (!_target)
    2987           0 :         return NULL;
    2988             :     /* Check the # of children rather than the length of _target, since
    2989             :        for x, in ... has 1 element in _target, but still requires a Tuple. */
    2990         172 :     first = (expr_ty)asdl_seq_GET(_target, 0);
    2991         172 :     if (NCH(node_target) == 1)
    2992         138 :         target = first;
    2993             :     else
    2994          34 :         target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
    2995             : 
    2996         172 :     expression = ast_for_testlist(c, CHILD(n, 3));
    2997         172 :     if (!expression)
    2998           0 :         return NULL;
    2999         172 :     suite_seq = ast_for_suite(c, CHILD(n, 5));
    3000         172 :     if (!suite_seq)
    3001           0 :         return NULL;
    3002             : 
    3003         172 :     return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
    3004             :                c->c_arena);
    3005             : }
    3006             : 
    3007             : static excepthandler_ty
    3008          68 : ast_for_except_clause(struct compiling *c, const node *exc, node *body)
    3009             : {
    3010             :     /* except_clause: 'except' [test [(',' | 'as') test]] */
    3011             :     REQ(exc, except_clause);
    3012             :     REQ(body, suite);
    3013             : 
    3014          68 :     if (NCH(exc) == 1) {
    3015           0 :         asdl_seq *suite_seq = ast_for_suite(c, body);
    3016           0 :         if (!suite_seq)
    3017           0 :             return NULL;
    3018             : 
    3019           0 :         return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
    3020             :                              exc->n_col_offset, c->c_arena);
    3021             :     }
    3022          68 :     else if (NCH(exc) == 2) {
    3023             :         expr_ty expression;
    3024             :         asdl_seq *suite_seq;
    3025             : 
    3026          39 :         expression = ast_for_expr(c, CHILD(exc, 1));
    3027          39 :         if (!expression)
    3028           0 :             return NULL;
    3029          39 :         suite_seq = ast_for_suite(c, body);
    3030          39 :         if (!suite_seq)
    3031           0 :             return NULL;
    3032             : 
    3033          39 :         return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
    3034             :                              exc->n_col_offset, c->c_arena);
    3035             :     }
    3036          29 :     else if (NCH(exc) == 4) {
    3037             :         asdl_seq *suite_seq;
    3038             :         expr_ty expression;
    3039          29 :         expr_ty e = ast_for_expr(c, CHILD(exc, 3));
    3040          29 :         if (!e)
    3041           0 :             return NULL;
    3042          29 :         if (!set_context(c, e, Store, CHILD(exc, 3)))
    3043           0 :             return NULL;
    3044          29 :         expression = ast_for_expr(c, CHILD(exc, 1));
    3045          29 :         if (!expression)
    3046           0 :             return NULL;
    3047          29 :         suite_seq = ast_for_suite(c, body);
    3048          29 :         if (!suite_seq)
    3049           0 :             return NULL;
    3050             : 
    3051          29 :         return ExceptHandler(expression, e, suite_seq, LINENO(exc),
    3052             :                              exc->n_col_offset, c->c_arena);
    3053             :     }
    3054             : 
    3055           0 :     PyErr_Format(PyExc_SystemError,
    3056             :                  "wrong number of children for 'except' clause: %d",
    3057             :                  NCH(exc));
    3058           0 :     return NULL;
    3059             : }
    3060             : 
    3061             : static stmt_ty
    3062          60 : ast_for_try_stmt(struct compiling *c, const node *n)
    3063             : {
    3064          60 :     const int nch = NCH(n);
    3065          60 :     int n_except = (nch - 3)/3;
    3066          60 :     asdl_seq *body, *orelse = NULL, *finally = NULL;
    3067             : 
    3068             :     REQ(n, try_stmt);
    3069             : 
    3070          60 :     body = ast_for_suite(c, CHILD(n, 2));
    3071          60 :     if (body == NULL)
    3072           0 :         return NULL;
    3073             : 
    3074          60 :     if (TYPE(CHILD(n, nch - 3)) == NAME) {
    3075           7 :         if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
    3076           0 :             if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
    3077             :                 /* we can assume it's an "else",
    3078             :                    because nch >= 9 for try-else-finally and
    3079             :                    it would otherwise have a type of except_clause */
    3080           0 :                 orelse = ast_for_suite(c, CHILD(n, nch - 4));
    3081           0 :                 if (orelse == NULL)
    3082           0 :                     return NULL;
    3083           0 :                 n_except--;
    3084             :             }
    3085             : 
    3086           0 :             finally = ast_for_suite(c, CHILD(n, nch - 1));
    3087           0 :             if (finally == NULL)
    3088           0 :                 return NULL;
    3089           0 :             n_except--;
    3090             :         }
    3091             :         else {
    3092             :             /* we can assume it's an "else",
    3093             :                otherwise it would have a type of except_clause */
    3094           7 :             orelse = ast_for_suite(c, CHILD(n, nch - 1));
    3095           7 :             if (orelse == NULL)
    3096           0 :                 return NULL;
    3097           7 :             n_except--;
    3098             :         }
    3099             :     }
    3100          53 :     else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
    3101           0 :         ast_error(n, "malformed 'try' statement");
    3102           0 :         return NULL;
    3103             :     }
    3104             : 
    3105          60 :     if (n_except > 0) {
    3106             :         int i;
    3107             :         stmt_ty except_st;
    3108             :         /* process except statements to create a try ... except */
    3109          60 :         asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
    3110          60 :         if (handlers == NULL)
    3111           0 :             return NULL;
    3112             : 
    3113         128 :         for (i = 0; i < n_except; i++) {
    3114         136 :             excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
    3115         136 :                                                        CHILD(n, 5 + i * 3));
    3116          68 :             if (!e)
    3117           0 :                 return NULL;
    3118          68 :             asdl_seq_SET(handlers, i, e);
    3119             :         }
    3120             : 
    3121          60 :         except_st = TryExcept(body, handlers, orelse, LINENO(n),
    3122             :                               n->n_col_offset, c->c_arena);
    3123          60 :         if (!finally)
    3124          60 :             return except_st;
    3125             : 
    3126             :         /* if a 'finally' is present too, we nest the TryExcept within a
    3127             :            TryFinally to emulate try ... except ... finally */
    3128           0 :         body = asdl_seq_new(1, c->c_arena);
    3129           0 :         if (body == NULL)
    3130           0 :             return NULL;
    3131           0 :         asdl_seq_SET(body, 0, except_st);
    3132             :     }
    3133             : 
    3134             :     /* must be a try ... finally (except clauses are in body, if any exist) */
    3135             :     assert(finally != NULL);
    3136           0 :     return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
    3137             : }
    3138             : 
    3139             : /* with_item: test ['as' expr] */
    3140             : static stmt_ty
    3141           8 : ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
    3142             : {
    3143           8 :     expr_ty context_expr, optional_vars = NULL;
    3144             : 
    3145             :     REQ(n, with_item);
    3146           8 :     context_expr = ast_for_expr(c, CHILD(n, 0));
    3147           8 :     if (!context_expr)
    3148           0 :         return NULL;
    3149           8 :     if (NCH(n) == 3) {
    3150           8 :         optional_vars = ast_for_expr(c, CHILD(n, 2));
    3151             : 
    3152           8 :         if (!optional_vars) {
    3153           0 :             return NULL;
    3154             :         }
    3155           8 :         if (!set_context(c, optional_vars, Store, n)) {
    3156           0 :             return NULL;
    3157             :         }
    3158             :     }
    3159             : 
    3160           8 :     return With(context_expr, optional_vars, content, LINENO(n),
    3161             :                 n->n_col_offset, c->c_arena);
    3162             : }
    3163             : 
    3164             : /* with_stmt: 'with' with_item (',' with_item)* ':' suite */
    3165             : static stmt_ty
    3166           8 : ast_for_with_stmt(struct compiling *c, const node *n)
    3167             : {
    3168             :     int i;
    3169             :     stmt_ty ret;
    3170             :     asdl_seq *inner;
    3171             : 
    3172             :     REQ(n, with_stmt);
    3173             : 
    3174             :     /* process the with items inside-out */
    3175           8 :     i = NCH(n) - 1;
    3176             :     /* the suite of the innermost with item is the suite of the with stmt */
    3177           8 :     inner = ast_for_suite(c, CHILD(n, i));
    3178           8 :     if (!inner)
    3179           0 :         return NULL;
    3180             : 
    3181             :     for (;;) {
    3182           8 :         i -= 2;
    3183           8 :         ret = ast_for_with_item(c, CHILD(n, i), inner);
    3184           8 :         if (!ret)
    3185           0 :             return NULL;
    3186             :         /* was this the last item? */
    3187           8 :         if (i == 1)
    3188           8 :             break;
    3189             :         /* if not, wrap the result so far in a new sequence */
    3190           0 :         inner = asdl_seq_new(1, c->c_arena);
    3191           0 :         if (!inner)
    3192           0 :             return NULL;
    3193           0 :         asdl_seq_SET(inner, 0, ret);
    3194           0 :     }
    3195             : 
    3196           8 :     return ret;
    3197             : }
    3198             : 
    3199             : static stmt_ty
    3200         144 : ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
    3201             : {
    3202             :     /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
    3203             :     PyObject *classname;
    3204             :     asdl_seq *bases, *s;
    3205             : 
    3206             :     REQ(n, classdef);
    3207             : 
    3208         144 :     if (!forbidden_check(c, n, STR(CHILD(n, 1))))
    3209           0 :             return NULL;
    3210             : 
    3211         144 :     if (NCH(n) == 4) {
    3212          25 :         s = ast_for_suite(c, CHILD(n, 3));
    3213          25 :         if (!s)
    3214           0 :             return NULL;
    3215          25 :         classname = NEW_IDENTIFIER(CHILD(n, 1));
    3216          25 :         if (!classname)
    3217           0 :             return NULL;
    3218          25 :         return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
    3219             :                         n->n_col_offset, c->c_arena);
    3220             :     }
    3221             :     /* check for empty base list */
    3222         119 :     if (TYPE(CHILD(n,3)) == RPAR) {
    3223           0 :         s = ast_for_suite(c, CHILD(n,5));
    3224           0 :         if (!s)
    3225           0 :             return NULL;
    3226           0 :         classname = NEW_IDENTIFIER(CHILD(n, 1));
    3227           0 :         if (!classname)
    3228           0 :             return NULL;
    3229           0 :         return ClassDef(classname, NULL, s, decorator_seq, LINENO(n),
    3230             :                         n->n_col_offset, c->c_arena);
    3231             :     }
    3232             : 
    3233             :     /* else handle the base class list */
    3234         119 :     bases = ast_for_class_bases(c, CHILD(n, 3));
    3235         119 :     if (!bases)
    3236           0 :         return NULL;
    3237             : 
    3238         119 :     s = ast_for_suite(c, CHILD(n, 6));
    3239         119 :     if (!s)
    3240           0 :         return NULL;
    3241         119 :     classname = NEW_IDENTIFIER(CHILD(n, 1));
    3242         119 :     if (!classname)
    3243           0 :         return NULL;
    3244         119 :     return ClassDef(classname, bases, s, decorator_seq,
    3245             :                     LINENO(n), n->n_col_offset, c->c_arena);
    3246             : }
    3247             : 
    3248             : static stmt_ty
    3249        7908 : ast_for_stmt(struct compiling *c, const node *n)
    3250             : {
    3251        7908 :     if (TYPE(n) == stmt) {
    3252             :         assert(NCH(n) == 1);
    3253        7798 :         n = CHILD(n, 0);
    3254             :     }
    3255        7908 :     if (TYPE(n) == simple_stmt) {
    3256             :         assert(num_stmts(n) == 1);
    3257        5592 :         n = CHILD(n, 0);
    3258             :     }
    3259        7908 :     if (TYPE(n) == small_stmt) {
    3260        5702 :         n = CHILD(n, 0);
    3261             :         /* small_stmt: expr_stmt | print_stmt  | del_stmt | pass_stmt
    3262             :                      | flow_stmt | import_stmt | global_stmt | exec_stmt
    3263             :                      | assert_stmt
    3264             :         */
    3265        5702 :         switch (TYPE(n)) {
    3266             :             case expr_stmt:
    3267        3986 :                 return ast_for_expr_stmt(c, n);
    3268             :             case print_stmt:
    3269           8 :                 return ast_for_print_stmt(c, n);
    3270             :             case del_stmt:
    3271           2 :                 return ast_for_del_stmt(c, n);
    3272             :             case pass_stmt:
    3273          88 :                 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
    3274             :             case flow_stmt:
    3275        1300 :                 return ast_for_flow_stmt(c, n);
    3276             :             case import_stmt:
    3277         245 :                 return ast_for_import_stmt(c, n);
    3278             :             case global_stmt:
    3279           0 :                 return ast_for_global_stmt(c, n);
    3280             :             case exec_stmt:
    3281           0 :                 return ast_for_exec_stmt(c, n);
    3282             :             case assert_stmt:
    3283          73 :                 return ast_for_assert_stmt(c, n);
    3284             :             default:
    3285           0 :                 PyErr_Format(PyExc_SystemError,
    3286             :                              "unhandled small_stmt: TYPE=%d NCH=%d\n",
    3287           0 :                              TYPE(n), NCH(n));
    3288           0 :                 return NULL;
    3289             :         }
    3290             :     }
    3291             :     else {
    3292             :         /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
    3293             :                         | funcdef | classdef | decorated
    3294             :         */
    3295        2206 :         node *ch = CHILD(n, 0);
    3296             :         REQ(n, compound_stmt);
    3297        2206 :         switch (TYPE(ch)) {
    3298             :             case if_stmt:
    3299         970 :                 return ast_for_if_stmt(c, ch);
    3300             :             case while_stmt:
    3301          31 :                 return ast_for_while_stmt(c, ch);
    3302             :             case for_stmt:
    3303         172 :                 return ast_for_for_stmt(c, ch);
    3304             :             case try_stmt:
    3305          60 :                 return ast_for_try_stmt(c, ch);
    3306             :             case with_stmt:
    3307           8 :                 return ast_for_with_stmt(c, ch);
    3308             :             case funcdef:
    3309         794 :                 return ast_for_funcdef(c, ch, NULL);
    3310             :             case classdef:
    3311         144 :                 return ast_for_classdef(c, ch, NULL);
    3312             :             case decorated:
    3313          27 :                 return ast_for_decorated(c, ch);
    3314             :             default:
    3315           0 :                 PyErr_Format(PyExc_SystemError,
    3316             :                              "unhandled small_stmt: TYPE=%d NCH=%d\n",
    3317           0 :                              TYPE(n), NCH(n));
    3318           0 :                 return NULL;
    3319             :         }
    3320             :     }
    3321             : }
    3322             : 
    3323             : static PyObject *
    3324         759 : parsenumber(struct compiling *c, const char *s)
    3325             : {
    3326             :         const char *end;
    3327             :         long x;
    3328             :         double dx;
    3329             : #ifndef WITHOUT_COMPLEX
    3330             :         Py_complex complex;
    3331             :         int imflag;
    3332             : #endif
    3333             : 
    3334             :         assert(s != NULL);
    3335         759 :         errno = 0;
    3336         759 :         end = s + strlen(s) - 1;
    3337             : #ifndef WITHOUT_COMPLEX
    3338         759 :         imflag = *end == 'j' || *end == 'J';
    3339             : #endif
    3340         759 :         if (*end == 'l' || *end == 'L')
    3341           0 :                 return PyLong_FromString((char *)s, (char **)0, 0);
    3342         759 :         x = PyOS_strtol((char *)s, (char **)&end, 0);
    3343         759 :         if (*end == '\0') {
    3344         758 :                 if (errno != 0)
    3345           0 :                         return PyLong_FromString((char *)s, (char **)0, 0);
    3346         758 :                 return PyInt_FromLong(x);
    3347             :         }
    3348             :         /* XXX Huge floats may silently fail */
    3349             : #ifndef WITHOUT_COMPLEX
    3350           1 :         if (imflag) {
    3351           0 :                 complex.real = 0.;
    3352           0 :                 complex.imag = PyOS_string_to_double(s, (char **)&end, NULL);
    3353           0 :                 if (complex.imag == -1.0 && PyErr_Occurred())
    3354           0 :                         return NULL;
    3355           0 :                 return PyComplex_FromCComplex(complex);
    3356             :         }
    3357             :         else
    3358             : #endif
    3359             :         {
    3360           1 :                 dx = PyOS_string_to_double(s, NULL, NULL);
    3361           1 :                 if (dx == -1.0 && PyErr_Occurred())
    3362           0 :                         return NULL;
    3363           1 :                 return PyFloat_FromDouble(dx);
    3364             :         }
    3365             : }
    3366             : 
    3367             : static PyObject *
    3368           0 : decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding)
    3369             : {
    3370             : #ifndef Py_USING_UNICODE
    3371             :         Py_FatalError("decode_utf8 should not be called in this build.");
    3372             :         return NULL;
    3373             : #else
    3374             :         PyObject *u, *v;
    3375             :         char *s, *t;
    3376           0 :         t = s = (char *)*sPtr;
    3377             :         /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
    3378           0 :         while (s < end && (*s & 0x80)) s++;
    3379           0 :         *sPtr = s;
    3380           0 :         u = PyUnicode_DecodeUTF8(t, s - t, NULL);
    3381           0 :         if (u == NULL)
    3382           0 :                 return NULL;
    3383           0 :         v = PyUnicode_AsEncodedString(u, encoding, NULL);
    3384           0 :         Py_DECREF(u);
    3385           0 :         return v;
    3386             : #endif
    3387             : }
    3388             : 
    3389             : #ifdef Py_USING_UNICODE
    3390             : static PyObject *
    3391           0 : decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding)
    3392             : {
    3393             :         PyObject *v;
    3394           0 :         PyObject *u = NULL;
    3395             :         char *buf;
    3396             :         char *p;
    3397             :         const char *end;
    3398           0 :         if (encoding != NULL && strcmp(encoding, "iso-8859-1")) {
    3399             :                 /* check for integer overflow */
    3400           0 :                 if (len > PY_SIZE_MAX / 6)
    3401           0 :                         return NULL;
    3402             :                 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
    3403             :                    "\รค" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
    3404           0 :                 u = PyString_FromStringAndSize((char *)NULL, len * 6);
    3405           0 :                 if (u == NULL)
    3406           0 :                         return NULL;
    3407           0 :                 p = buf = PyString_AsString(u);
    3408           0 :                 end = s + len;
    3409           0 :                 while (s < end) {
    3410           0 :                         if (*s == '\\') {
    3411           0 :                                 *p++ = *s++;
    3412           0 :                                 if (*s & 0x80) {
    3413           0 :                                         strcpy(p, "u005c");
    3414           0 :                                         p += 5;
    3415             :                                 }
    3416             :                         }
    3417           0 :                         if (*s & 0x80) { /* XXX inefficient */
    3418             :                                 PyObject *w;
    3419             :                                 char *r;
    3420             :                                 Py_ssize_t rn, i;
    3421           0 :                                 w = decode_utf8(c, &s, end, "utf-32-be");
    3422           0 :                                 if (w == NULL) {
    3423           0 :                                         Py_DECREF(u);
    3424           0 :                                         return NULL;
    3425             :                                 }
    3426           0 :                                 r = PyString_AsString(w);
    3427           0 :                                 rn = PyString_Size(w);
    3428             :                                 assert(rn % 4 == 0);
    3429           0 :                                 for (i = 0; i < rn; i += 4) {
    3430           0 :                                         sprintf(p, "\\U%02x%02x%02x%02x",
    3431           0 :                                                 r[i + 0] & 0xFF,
    3432           0 :                                                 r[i + 1] & 0xFF,
    3433           0 :                                                 r[i + 2] & 0xFF,
    3434           0 :                                                 r[i + 3] & 0xFF);
    3435           0 :                                         p += 10;
    3436             :                                 }
    3437           0 :                                 Py_DECREF(w);
    3438             :                         } else {
    3439           0 :                                 *p++ = *s++;
    3440             :                         }
    3441             :                 }
    3442           0 :                 len = p - buf;
    3443           0 :                 s = buf;
    3444             :         }
    3445           0 :         if (rawmode)
    3446           0 :                 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
    3447             :         else
    3448           0 :                 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
    3449           0 :         Py_XDECREF(u);
    3450           0 :         return v;
    3451             : }
    3452             : #endif
    3453             : 
    3454             : /* s is a Python string literal, including the bracketing quote characters,
    3455             :  * and r &/or u prefixes (if any), and embedded escape sequences (if any).
    3456             :  * parsestr parses it, and returns the decoded Python string object.
    3457             :  */
    3458             : static PyObject *
    3459        2359 : parsestr(struct compiling *c, const node *n, const char *s)
    3460             : {
    3461             :         size_t len, i;
    3462        2359 :         int quote = Py_CHARMASK(*s);
    3463        2359 :         int rawmode = 0;
    3464             :         int need_encoding;
    3465        2359 :         int unicode = c->c_future_unicode;
    3466        2359 :         int bytes = 0;
    3467             : 
    3468        2359 :         if (isalpha(quote) || quote == '_') {
    3469          47 :                 if (quote == 'u' || quote == 'U') {
    3470           0 :                         quote = *++s;
    3471           0 :                         unicode = 1;
    3472             :                 }
    3473          47 :                 if (quote == 'b' || quote == 'B') {
    3474           3 :                         quote = *++s;
    3475           3 :                         unicode = 0;
    3476           3 :                         bytes = 1;
    3477             :                 }
    3478          47 :                 if (quote == 'r' || quote == 'R') {
    3479          44 :                         quote = *++s;
    3480          44 :                         rawmode = 1;
    3481             :                 }
    3482             :         }
    3483        2359 :         if (quote != '\'' && quote != '\"') {
    3484           0 :                 PyErr_BadInternalCall();
    3485           0 :                 return NULL;
    3486             :         }
    3487        2359 :         s++;
    3488        2359 :         len = strlen(s);
    3489        2359 :         if (len > INT_MAX) {
    3490           0 :                 PyErr_SetString(PyExc_OverflowError,
    3491             :                                 "string to parse is too long");
    3492           0 :                 return NULL;
    3493             :         }
    3494        2359 :         if (s[--len] != quote) {
    3495           0 :                 PyErr_BadInternalCall();
    3496           0 :                 return NULL;
    3497             :         }
    3498        2359 :         if (len >= 4 && s[0] == quote && s[1] == quote) {
    3499         357 :                 s += 2;
    3500         357 :                 len -= 2;
    3501         357 :                 if (s[--len] != quote || s[--len] != quote) {
    3502           0 :                         PyErr_BadInternalCall();
    3503           0 :                         return NULL;
    3504             :                 }
    3505             :         }
    3506        2359 :         if (Py_Py3kWarningFlag && bytes) {
    3507           0 :             for (i = 0; i < len; i++) {
    3508           0 :                 if ((unsigned char)s[i] > 127) {
    3509           0 :                     if (!ast_warn(c, n,
    3510             :                         "non-ascii bytes literals not supported in 3.x"))
    3511           0 :                         return NULL;
    3512           0 :                     break;
    3513             :                 }
    3514             :             }
    3515             :         }
    3516             : #ifdef Py_USING_UNICODE
    3517        2359 :         if (unicode || Py_UnicodeFlag) {
    3518           0 :                 return decode_unicode(c, s, len, rawmode, c->c_encoding);
    3519             :         }
    3520             : #endif
    3521        4718 :         need_encoding = (c->c_encoding != NULL &&
    3522        2359 :                          strcmp(c->c_encoding, "utf-8") != 0 &&
    3523           0 :                          strcmp(c->c_encoding, "iso-8859-1") != 0);
    3524        2359 :         if (rawmode || strchr(s, '\\') == NULL) {
    3525        2290 :                 if (need_encoding) {
    3526             : #ifndef Py_USING_UNICODE
    3527             :                         /* This should not happen - we never see any other
    3528             :                            encoding. */
    3529             :                         Py_FatalError(
    3530             :                             "cannot deal with encodings in this build.");
    3531             : #else
    3532           0 :                         PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
    3533           0 :                         if (u == NULL)
    3534           0 :                                 return NULL;
    3535           0 :                         v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL);
    3536           0 :                         Py_DECREF(u);
    3537           0 :                         return v;
    3538             : #endif
    3539             :                 } else {
    3540        2290 :                         return PyString_FromStringAndSize(s, len);
    3541             :                 }
    3542             :         }
    3543             : 
    3544          69 :         return PyString_DecodeEscape(s, len, NULL, unicode,
    3545             :                                      need_encoding ? c->c_encoding : NULL);
    3546             : }
    3547             : 
    3548             : /* Build a Python string object out of a STRING atom.  This takes care of
    3549             :  * compile-time literal catenation, calling parsestr() on each piece, and
    3550             :  * pasting the intermediate results together.
    3551             :  */
    3552             : static PyObject *
    3553        2353 : parsestrplus(struct compiling *c, const node *n)
    3554             : {
    3555             :         PyObject *v;
    3556             :         int i;
    3557             :         REQ(CHILD(n, 0), STRING);
    3558        2353 :         if ((v = parsestr(c, n, STR(CHILD(n, 0)))) != NULL) {
    3559             :                 /* String literal concatenation */
    3560        2359 :                 for (i = 1; i < NCH(n); i++) {
    3561             :                         PyObject *s;
    3562           6 :                         s = parsestr(c, n, STR(CHILD(n, i)));
    3563           6 :                         if (s == NULL)
    3564           0 :                                 goto onError;
    3565           6 :                         if (PyString_Check(v) && PyString_Check(s)) {
    3566           6 :                                 PyString_ConcatAndDel(&v, s);
    3567          12 :                                 if (v == NULL)
    3568           0 :                                     goto onError;
    3569             :                         }
    3570             : #ifdef Py_USING_UNICODE
    3571             :                         else {
    3572           0 :                                 PyObject *temp = PyUnicode_Concat(v, s);
    3573           0 :                                 Py_DECREF(s);
    3574           0 :                                 Py_DECREF(v);
    3575           0 :                                 v = temp;
    3576           0 :                                 if (v == NULL)
    3577           0 :                                     goto onError;
    3578             :                         }
    3579             : #endif
    3580             :                 }
    3581             :         }
    3582        2353 :         return v;
    3583             : 
    3584             :  onError:
    3585           0 :         Py_XDECREF(v);
    3586           0 :         return NULL;
    3587             : }

Generated by: LCOV version 1.10