LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/python3/Python - ast.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 746 2067 36.1 %
Date: 2012-12-17 Functions: 42 78 53.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10