• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* File automatically generated by Parser/asdl_c.py. */
2 
3 #include <stddef.h>
4 
5 #include "Python.h"
6 #include "Python-ast.h"
7 
8 static PyTypeObject AST_type;
9 static PyTypeObject *mod_type;
10 static PyObject* ast2obj_mod(void*);
11 static PyTypeObject *Module_type;
12 _Py_IDENTIFIER(body);
13 static char *Module_fields[]={
14     "body",
15 };
16 static PyTypeObject *Interactive_type;
17 static char *Interactive_fields[]={
18     "body",
19 };
20 static PyTypeObject *Expression_type;
21 static char *Expression_fields[]={
22     "body",
23 };
24 static PyTypeObject *Suite_type;
25 static char *Suite_fields[]={
26     "body",
27 };
28 static PyTypeObject *stmt_type;
29 _Py_IDENTIFIER(lineno);
30 _Py_IDENTIFIER(col_offset);
31 static char *stmt_attributes[] = {
32     "lineno",
33     "col_offset",
34 };
35 static PyObject* ast2obj_stmt(void*);
36 static PyTypeObject *FunctionDef_type;
37 _Py_IDENTIFIER(name);
38 _Py_IDENTIFIER(args);
39 _Py_IDENTIFIER(decorator_list);
40 _Py_IDENTIFIER(returns);
41 static char *FunctionDef_fields[]={
42     "name",
43     "args",
44     "body",
45     "decorator_list",
46     "returns",
47 };
48 static PyTypeObject *AsyncFunctionDef_type;
49 static char *AsyncFunctionDef_fields[]={
50     "name",
51     "args",
52     "body",
53     "decorator_list",
54     "returns",
55 };
56 static PyTypeObject *ClassDef_type;
57 _Py_IDENTIFIER(bases);
58 _Py_IDENTIFIER(keywords);
59 static char *ClassDef_fields[]={
60     "name",
61     "bases",
62     "keywords",
63     "body",
64     "decorator_list",
65 };
66 static PyTypeObject *Return_type;
67 _Py_IDENTIFIER(value);
68 static char *Return_fields[]={
69     "value",
70 };
71 static PyTypeObject *Delete_type;
72 _Py_IDENTIFIER(targets);
73 static char *Delete_fields[]={
74     "targets",
75 };
76 static PyTypeObject *Assign_type;
77 static char *Assign_fields[]={
78     "targets",
79     "value",
80 };
81 static PyTypeObject *AugAssign_type;
82 _Py_IDENTIFIER(target);
83 _Py_IDENTIFIER(op);
84 static char *AugAssign_fields[]={
85     "target",
86     "op",
87     "value",
88 };
89 static PyTypeObject *AnnAssign_type;
90 _Py_IDENTIFIER(annotation);
91 _Py_IDENTIFIER(simple);
92 static char *AnnAssign_fields[]={
93     "target",
94     "annotation",
95     "value",
96     "simple",
97 };
98 static PyTypeObject *For_type;
99 _Py_IDENTIFIER(iter);
100 _Py_IDENTIFIER(orelse);
101 static char *For_fields[]={
102     "target",
103     "iter",
104     "body",
105     "orelse",
106 };
107 static PyTypeObject *AsyncFor_type;
108 static char *AsyncFor_fields[]={
109     "target",
110     "iter",
111     "body",
112     "orelse",
113 };
114 static PyTypeObject *While_type;
115 _Py_IDENTIFIER(test);
116 static char *While_fields[]={
117     "test",
118     "body",
119     "orelse",
120 };
121 static PyTypeObject *If_type;
122 static char *If_fields[]={
123     "test",
124     "body",
125     "orelse",
126 };
127 static PyTypeObject *With_type;
128 _Py_IDENTIFIER(items);
129 static char *With_fields[]={
130     "items",
131     "body",
132 };
133 static PyTypeObject *AsyncWith_type;
134 static char *AsyncWith_fields[]={
135     "items",
136     "body",
137 };
138 static PyTypeObject *Raise_type;
139 _Py_IDENTIFIER(exc);
140 _Py_IDENTIFIER(cause);
141 static char *Raise_fields[]={
142     "exc",
143     "cause",
144 };
145 static PyTypeObject *Try_type;
146 _Py_IDENTIFIER(handlers);
147 _Py_IDENTIFIER(finalbody);
148 static char *Try_fields[]={
149     "body",
150     "handlers",
151     "orelse",
152     "finalbody",
153 };
154 static PyTypeObject *Assert_type;
155 _Py_IDENTIFIER(msg);
156 static char *Assert_fields[]={
157     "test",
158     "msg",
159 };
160 static PyTypeObject *Import_type;
161 _Py_IDENTIFIER(names);
162 static char *Import_fields[]={
163     "names",
164 };
165 static PyTypeObject *ImportFrom_type;
166 _Py_IDENTIFIER(module);
167 _Py_IDENTIFIER(level);
168 static char *ImportFrom_fields[]={
169     "module",
170     "names",
171     "level",
172 };
173 static PyTypeObject *Global_type;
174 static char *Global_fields[]={
175     "names",
176 };
177 static PyTypeObject *Nonlocal_type;
178 static char *Nonlocal_fields[]={
179     "names",
180 };
181 static PyTypeObject *Expr_type;
182 static char *Expr_fields[]={
183     "value",
184 };
185 static PyTypeObject *Pass_type;
186 static PyTypeObject *Break_type;
187 static PyTypeObject *Continue_type;
188 static PyTypeObject *expr_type;
189 static char *expr_attributes[] = {
190     "lineno",
191     "col_offset",
192 };
193 static PyObject* ast2obj_expr(void*);
194 static PyTypeObject *BoolOp_type;
195 _Py_IDENTIFIER(values);
196 static char *BoolOp_fields[]={
197     "op",
198     "values",
199 };
200 static PyTypeObject *BinOp_type;
201 _Py_IDENTIFIER(left);
202 _Py_IDENTIFIER(right);
203 static char *BinOp_fields[]={
204     "left",
205     "op",
206     "right",
207 };
208 static PyTypeObject *UnaryOp_type;
209 _Py_IDENTIFIER(operand);
210 static char *UnaryOp_fields[]={
211     "op",
212     "operand",
213 };
214 static PyTypeObject *Lambda_type;
215 static char *Lambda_fields[]={
216     "args",
217     "body",
218 };
219 static PyTypeObject *IfExp_type;
220 static char *IfExp_fields[]={
221     "test",
222     "body",
223     "orelse",
224 };
225 static PyTypeObject *Dict_type;
226 _Py_IDENTIFIER(keys);
227 static char *Dict_fields[]={
228     "keys",
229     "values",
230 };
231 static PyTypeObject *Set_type;
232 _Py_IDENTIFIER(elts);
233 static char *Set_fields[]={
234     "elts",
235 };
236 static PyTypeObject *ListComp_type;
237 _Py_IDENTIFIER(elt);
238 _Py_IDENTIFIER(generators);
239 static char *ListComp_fields[]={
240     "elt",
241     "generators",
242 };
243 static PyTypeObject *SetComp_type;
244 static char *SetComp_fields[]={
245     "elt",
246     "generators",
247 };
248 static PyTypeObject *DictComp_type;
249 _Py_IDENTIFIER(key);
250 static char *DictComp_fields[]={
251     "key",
252     "value",
253     "generators",
254 };
255 static PyTypeObject *GeneratorExp_type;
256 static char *GeneratorExp_fields[]={
257     "elt",
258     "generators",
259 };
260 static PyTypeObject *Await_type;
261 static char *Await_fields[]={
262     "value",
263 };
264 static PyTypeObject *Yield_type;
265 static char *Yield_fields[]={
266     "value",
267 };
268 static PyTypeObject *YieldFrom_type;
269 static char *YieldFrom_fields[]={
270     "value",
271 };
272 static PyTypeObject *Compare_type;
273 _Py_IDENTIFIER(ops);
274 _Py_IDENTIFIER(comparators);
275 static char *Compare_fields[]={
276     "left",
277     "ops",
278     "comparators",
279 };
280 static PyTypeObject *Call_type;
281 _Py_IDENTIFIER(func);
282 static char *Call_fields[]={
283     "func",
284     "args",
285     "keywords",
286 };
287 static PyTypeObject *Num_type;
288 _Py_IDENTIFIER(n);
289 static char *Num_fields[]={
290     "n",
291 };
292 static PyTypeObject *Str_type;
293 _Py_IDENTIFIER(s);
294 static char *Str_fields[]={
295     "s",
296 };
297 static PyTypeObject *FormattedValue_type;
298 _Py_IDENTIFIER(conversion);
299 _Py_IDENTIFIER(format_spec);
300 static char *FormattedValue_fields[]={
301     "value",
302     "conversion",
303     "format_spec",
304 };
305 static PyTypeObject *JoinedStr_type;
306 static char *JoinedStr_fields[]={
307     "values",
308 };
309 static PyTypeObject *Bytes_type;
310 static char *Bytes_fields[]={
311     "s",
312 };
313 static PyTypeObject *NameConstant_type;
314 static char *NameConstant_fields[]={
315     "value",
316 };
317 static PyTypeObject *Ellipsis_type;
318 static PyTypeObject *Constant_type;
319 static char *Constant_fields[]={
320     "value",
321 };
322 static PyTypeObject *Attribute_type;
323 _Py_IDENTIFIER(attr);
324 _Py_IDENTIFIER(ctx);
325 static char *Attribute_fields[]={
326     "value",
327     "attr",
328     "ctx",
329 };
330 static PyTypeObject *Subscript_type;
331 _Py_IDENTIFIER(slice);
332 static char *Subscript_fields[]={
333     "value",
334     "slice",
335     "ctx",
336 };
337 static PyTypeObject *Starred_type;
338 static char *Starred_fields[]={
339     "value",
340     "ctx",
341 };
342 static PyTypeObject *Name_type;
343 _Py_IDENTIFIER(id);
344 static char *Name_fields[]={
345     "id",
346     "ctx",
347 };
348 static PyTypeObject *List_type;
349 static char *List_fields[]={
350     "elts",
351     "ctx",
352 };
353 static PyTypeObject *Tuple_type;
354 static char *Tuple_fields[]={
355     "elts",
356     "ctx",
357 };
358 static PyTypeObject *expr_context_type;
359 static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
360 *AugLoad_singleton, *AugStore_singleton, *Param_singleton;
361 static PyObject* ast2obj_expr_context(expr_context_ty);
362 static PyTypeObject *Load_type;
363 static PyTypeObject *Store_type;
364 static PyTypeObject *Del_type;
365 static PyTypeObject *AugLoad_type;
366 static PyTypeObject *AugStore_type;
367 static PyTypeObject *Param_type;
368 static PyTypeObject *slice_type;
369 static PyObject* ast2obj_slice(void*);
370 static PyTypeObject *Slice_type;
371 _Py_IDENTIFIER(lower);
372 _Py_IDENTIFIER(upper);
373 _Py_IDENTIFIER(step);
374 static char *Slice_fields[]={
375     "lower",
376     "upper",
377     "step",
378 };
379 static PyTypeObject *ExtSlice_type;
380 _Py_IDENTIFIER(dims);
381 static char *ExtSlice_fields[]={
382     "dims",
383 };
384 static PyTypeObject *Index_type;
385 static char *Index_fields[]={
386     "value",
387 };
388 static PyTypeObject *boolop_type;
389 static PyObject *And_singleton, *Or_singleton;
390 static PyObject* ast2obj_boolop(boolop_ty);
391 static PyTypeObject *And_type;
392 static PyTypeObject *Or_type;
393 static PyTypeObject *operator_type;
394 static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
395 *MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
396 *LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
397 *BitAnd_singleton, *FloorDiv_singleton;
398 static PyObject* ast2obj_operator(operator_ty);
399 static PyTypeObject *Add_type;
400 static PyTypeObject *Sub_type;
401 static PyTypeObject *Mult_type;
402 static PyTypeObject *MatMult_type;
403 static PyTypeObject *Div_type;
404 static PyTypeObject *Mod_type;
405 static PyTypeObject *Pow_type;
406 static PyTypeObject *LShift_type;
407 static PyTypeObject *RShift_type;
408 static PyTypeObject *BitOr_type;
409 static PyTypeObject *BitXor_type;
410 static PyTypeObject *BitAnd_type;
411 static PyTypeObject *FloorDiv_type;
412 static PyTypeObject *unaryop_type;
413 static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
414 *USub_singleton;
415 static PyObject* ast2obj_unaryop(unaryop_ty);
416 static PyTypeObject *Invert_type;
417 static PyTypeObject *Not_type;
418 static PyTypeObject *UAdd_type;
419 static PyTypeObject *USub_type;
420 static PyTypeObject *cmpop_type;
421 static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
422 *Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
423 *NotIn_singleton;
424 static PyObject* ast2obj_cmpop(cmpop_ty);
425 static PyTypeObject *Eq_type;
426 static PyTypeObject *NotEq_type;
427 static PyTypeObject *Lt_type;
428 static PyTypeObject *LtE_type;
429 static PyTypeObject *Gt_type;
430 static PyTypeObject *GtE_type;
431 static PyTypeObject *Is_type;
432 static PyTypeObject *IsNot_type;
433 static PyTypeObject *In_type;
434 static PyTypeObject *NotIn_type;
435 static PyTypeObject *comprehension_type;
436 static PyObject* ast2obj_comprehension(void*);
437 _Py_IDENTIFIER(ifs);
438 _Py_IDENTIFIER(is_async);
439 static char *comprehension_fields[]={
440     "target",
441     "iter",
442     "ifs",
443     "is_async",
444 };
445 static PyTypeObject *excepthandler_type;
446 static char *excepthandler_attributes[] = {
447     "lineno",
448     "col_offset",
449 };
450 static PyObject* ast2obj_excepthandler(void*);
451 static PyTypeObject *ExceptHandler_type;
452 _Py_IDENTIFIER(type);
453 static char *ExceptHandler_fields[]={
454     "type",
455     "name",
456     "body",
457 };
458 static PyTypeObject *arguments_type;
459 static PyObject* ast2obj_arguments(void*);
460 _Py_IDENTIFIER(vararg);
461 _Py_IDENTIFIER(kwonlyargs);
462 _Py_IDENTIFIER(kw_defaults);
463 _Py_IDENTIFIER(kwarg);
464 _Py_IDENTIFIER(defaults);
465 static char *arguments_fields[]={
466     "args",
467     "vararg",
468     "kwonlyargs",
469     "kw_defaults",
470     "kwarg",
471     "defaults",
472 };
473 static PyTypeObject *arg_type;
474 static PyObject* ast2obj_arg(void*);
475 static char *arg_attributes[] = {
476     "lineno",
477     "col_offset",
478 };
479 _Py_IDENTIFIER(arg);
480 static char *arg_fields[]={
481     "arg",
482     "annotation",
483 };
484 static PyTypeObject *keyword_type;
485 static PyObject* ast2obj_keyword(void*);
486 static char *keyword_fields[]={
487     "arg",
488     "value",
489 };
490 static PyTypeObject *alias_type;
491 static PyObject* ast2obj_alias(void*);
492 _Py_IDENTIFIER(asname);
493 static char *alias_fields[]={
494     "name",
495     "asname",
496 };
497 static PyTypeObject *withitem_type;
498 static PyObject* ast2obj_withitem(void*);
499 _Py_IDENTIFIER(context_expr);
500 _Py_IDENTIFIER(optional_vars);
501 static char *withitem_fields[]={
502     "context_expr",
503     "optional_vars",
504 };
505 
506 
507 typedef struct {
508     PyObject_HEAD
509     PyObject *dict;
510 } AST_object;
511 
512 static void
ast_dealloc(AST_object * self)513 ast_dealloc(AST_object *self)
514 {
515     Py_CLEAR(self->dict);
516     Py_TYPE(self)->tp_free(self);
517 }
518 
519 static int
ast_traverse(AST_object * self,visitproc visit,void * arg)520 ast_traverse(AST_object *self, visitproc visit, void *arg)
521 {
522     Py_VISIT(self->dict);
523     return 0;
524 }
525 
526 static void
ast_clear(AST_object * self)527 ast_clear(AST_object *self)
528 {
529     Py_CLEAR(self->dict);
530 }
531 
532 static int
ast_type_init(PyObject * self,PyObject * args,PyObject * kw)533 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
534 {
535     _Py_IDENTIFIER(_fields);
536     Py_ssize_t i, numfields = 0;
537     int res = -1;
538     PyObject *key, *value, *fields;
539     fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
540     if (!fields)
541         PyErr_Clear();
542     if (fields) {
543         numfields = PySequence_Size(fields);
544         if (numfields == -1)
545             goto cleanup;
546     }
547     res = 0; /* if no error occurs, this stays 0 to the end */
548     if (PyTuple_GET_SIZE(args) > 0) {
549         if (numfields != PyTuple_GET_SIZE(args)) {
550             PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
551                          "%zd positional argument%s",
552                          Py_TYPE(self)->tp_name,
553                          numfields == 0 ? "" : "either 0 or ",
554                          numfields, numfields == 1 ? "" : "s");
555             res = -1;
556             goto cleanup;
557         }
558         for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
559             /* cannot be reached when fields is NULL */
560             PyObject *name = PySequence_GetItem(fields, i);
561             if (!name) {
562                 res = -1;
563                 goto cleanup;
564             }
565             res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
566             Py_DECREF(name);
567             if (res < 0)
568                 goto cleanup;
569         }
570     }
571     if (kw) {
572         i = 0;  /* needed by PyDict_Next */
573         while (PyDict_Next(kw, &i, &key, &value)) {
574             res = PyObject_SetAttr(self, key, value);
575             if (res < 0)
576                 goto cleanup;
577         }
578     }
579   cleanup:
580     Py_XDECREF(fields);
581     return res;
582 }
583 
584 /* Pickling support */
585 static PyObject *
ast_type_reduce(PyObject * self,PyObject * unused)586 ast_type_reduce(PyObject *self, PyObject *unused)
587 {
588     PyObject *res;
589     _Py_IDENTIFIER(__dict__);
590     PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
591     if (dict == NULL) {
592         if (PyErr_ExceptionMatches(PyExc_AttributeError))
593             PyErr_Clear();
594         else
595             return NULL;
596     }
597     if (dict) {
598         res = Py_BuildValue("O()O", Py_TYPE(self), dict);
599         Py_DECREF(dict);
600         return res;
601     }
602     return Py_BuildValue("O()", Py_TYPE(self));
603 }
604 
605 static PyMethodDef ast_type_methods[] = {
606     {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
607     {NULL}
608 };
609 
610 static PyGetSetDef ast_type_getsets[] = {
611     {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
612     {NULL}
613 };
614 
615 static PyTypeObject AST_type = {
616     PyVarObject_HEAD_INIT(&PyType_Type, 0)
617     "_ast.AST",
618     sizeof(AST_object),
619     0,
620     (destructor)ast_dealloc, /* tp_dealloc */
621     0,                       /* tp_print */
622     0,                       /* tp_getattr */
623     0,                       /* tp_setattr */
624     0,                       /* tp_reserved */
625     0,                       /* tp_repr */
626     0,                       /* tp_as_number */
627     0,                       /* tp_as_sequence */
628     0,                       /* tp_as_mapping */
629     0,                       /* tp_hash */
630     0,                       /* tp_call */
631     0,                       /* tp_str */
632     PyObject_GenericGetAttr, /* tp_getattro */
633     PyObject_GenericSetAttr, /* tp_setattro */
634     0,                       /* tp_as_buffer */
635     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
636     0,                       /* tp_doc */
637     (traverseproc)ast_traverse, /* tp_traverse */
638     (inquiry)ast_clear,      /* tp_clear */
639     0,                       /* tp_richcompare */
640     0,                       /* tp_weaklistoffset */
641     0,                       /* tp_iter */
642     0,                       /* tp_iternext */
643     ast_type_methods,        /* tp_methods */
644     0,                       /* tp_members */
645     ast_type_getsets,        /* tp_getset */
646     0,                       /* tp_base */
647     0,                       /* tp_dict */
648     0,                       /* tp_descr_get */
649     0,                       /* tp_descr_set */
650     offsetof(AST_object, dict),/* tp_dictoffset */
651     (initproc)ast_type_init, /* tp_init */
652     PyType_GenericAlloc,     /* tp_alloc */
653     PyType_GenericNew,       /* tp_new */
654     PyObject_GC_Del,         /* tp_free */
655 };
656 
657 
make_type(char * type,PyTypeObject * base,char ** fields,int num_fields)658 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
659 {
660     PyObject *fnames, *result;
661     int i;
662     fnames = PyTuple_New(num_fields);
663     if (!fnames) return NULL;
664     for (i = 0; i < num_fields; i++) {
665         PyObject *field = PyUnicode_FromString(fields[i]);
666         if (!field) {
667             Py_DECREF(fnames);
668             return NULL;
669         }
670         PyTuple_SET_ITEM(fnames, i, field);
671     }
672     result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
673                     type, base, "_fields", fnames, "__module__", "_ast");
674     Py_DECREF(fnames);
675     return (PyTypeObject*)result;
676 }
677 
add_attributes(PyTypeObject * type,char ** attrs,int num_fields)678 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
679 {
680     int i, result;
681     _Py_IDENTIFIER(_attributes);
682     PyObject *s, *l = PyTuple_New(num_fields);
683     if (!l)
684         return 0;
685     for (i = 0; i < num_fields; i++) {
686         s = PyUnicode_FromString(attrs[i]);
687         if (!s) {
688             Py_DECREF(l);
689             return 0;
690         }
691         PyTuple_SET_ITEM(l, i, s);
692     }
693     result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
694     Py_DECREF(l);
695     return result;
696 }
697 
698 /* Conversion AST -> Python */
699 
ast2obj_list(asdl_seq * seq,PyObject * (* func)(void *))700 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
701 {
702     Py_ssize_t i, n = asdl_seq_LEN(seq);
703     PyObject *result = PyList_New(n);
704     PyObject *value;
705     if (!result)
706         return NULL;
707     for (i = 0; i < n; i++) {
708         value = func(asdl_seq_GET(seq, i));
709         if (!value) {
710             Py_DECREF(result);
711             return NULL;
712         }
713         PyList_SET_ITEM(result, i, value);
714     }
715     return result;
716 }
717 
ast2obj_object(void * o)718 static PyObject* ast2obj_object(void *o)
719 {
720     if (!o)
721         o = Py_None;
722     Py_INCREF((PyObject*)o);
723     return (PyObject*)o;
724 }
725 #define ast2obj_singleton ast2obj_object
726 #define ast2obj_constant ast2obj_object
727 #define ast2obj_identifier ast2obj_object
728 #define ast2obj_string ast2obj_object
729 #define ast2obj_bytes ast2obj_object
730 
ast2obj_int(long b)731 static PyObject* ast2obj_int(long b)
732 {
733     return PyLong_FromLong(b);
734 }
735 
736 /* Conversion Python -> AST */
737 
obj2ast_singleton(PyObject * obj,PyObject ** out,PyArena * arena)738 static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
739 {
740     if (obj != Py_None && obj != Py_True && obj != Py_False) {
741         PyErr_SetString(PyExc_ValueError,
742                         "AST singleton must be True, False, or None");
743         return 1;
744     }
745     *out = obj;
746     return 0;
747 }
748 
obj2ast_object(PyObject * obj,PyObject ** out,PyArena * arena)749 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
750 {
751     if (obj == Py_None)
752         obj = NULL;
753     if (obj) {
754         if (PyArena_AddPyObject(arena, obj) < 0) {
755             *out = NULL;
756             return -1;
757         }
758         Py_INCREF(obj);
759     }
760     *out = obj;
761     return 0;
762 }
763 
obj2ast_constant(PyObject * obj,PyObject ** out,PyArena * arena)764 static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
765 {
766     if (obj) {
767         if (PyArena_AddPyObject(arena, obj) < 0) {
768             *out = NULL;
769             return -1;
770         }
771         Py_INCREF(obj);
772     }
773     *out = obj;
774     return 0;
775 }
776 
obj2ast_identifier(PyObject * obj,PyObject ** out,PyArena * arena)777 static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
778 {
779     if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
780         PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
781         return 1;
782     }
783     return obj2ast_object(obj, out, arena);
784 }
785 
obj2ast_string(PyObject * obj,PyObject ** out,PyArena * arena)786 static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
787 {
788     if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
789         PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
790         return 1;
791     }
792     return obj2ast_object(obj, out, arena);
793 }
794 
obj2ast_bytes(PyObject * obj,PyObject ** out,PyArena * arena)795 static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
796 {
797     if (!PyBytes_CheckExact(obj)) {
798         PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
799         return 1;
800     }
801     return obj2ast_object(obj, out, arena);
802 }
803 
obj2ast_int(PyObject * obj,int * out,PyArena * arena)804 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
805 {
806     int i;
807     if (!PyLong_Check(obj)) {
808         PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
809         return 1;
810     }
811 
812     i = _PyLong_AsInt(obj);
813     if (i == -1 && PyErr_Occurred())
814         return 1;
815     *out = i;
816     return 0;
817 }
818 
add_ast_fields(void)819 static int add_ast_fields(void)
820 {
821     PyObject *empty_tuple, *d;
822     if (PyType_Ready(&AST_type) < 0)
823         return -1;
824     d = AST_type.tp_dict;
825     empty_tuple = PyTuple_New(0);
826     if (!empty_tuple ||
827         PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
828         PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
829         Py_XDECREF(empty_tuple);
830         return -1;
831     }
832     Py_DECREF(empty_tuple);
833     return 0;
834 }
835 
exists_not_none(PyObject * obj,_Py_Identifier * id)836 static int exists_not_none(PyObject *obj, _Py_Identifier *id)
837 {
838     int isnone;
839     PyObject *attr = _PyObject_GetAttrId(obj, id);
840     if (!attr) {
841         PyErr_Clear();
842         return 0;
843     }
844     isnone = attr == Py_None;
845     Py_DECREF(attr);
846     return !isnone;
847 }
848 
849 
init_types(void)850 static int init_types(void)
851 {
852     static int initialized;
853     if (initialized) return 1;
854     if (add_ast_fields() < 0) return 0;
855     mod_type = make_type("mod", &AST_type, NULL, 0);
856     if (!mod_type) return 0;
857     if (!add_attributes(mod_type, NULL, 0)) return 0;
858     Module_type = make_type("Module", mod_type, Module_fields, 1);
859     if (!Module_type) return 0;
860     Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
861                                  1);
862     if (!Interactive_type) return 0;
863     Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
864     if (!Expression_type) return 0;
865     Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
866     if (!Suite_type) return 0;
867     stmt_type = make_type("stmt", &AST_type, NULL, 0);
868     if (!stmt_type) return 0;
869     if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
870     FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
871                                  5);
872     if (!FunctionDef_type) return 0;
873     AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
874                                       AsyncFunctionDef_fields, 5);
875     if (!AsyncFunctionDef_type) return 0;
876     ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
877     if (!ClassDef_type) return 0;
878     Return_type = make_type("Return", stmt_type, Return_fields, 1);
879     if (!Return_type) return 0;
880     Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
881     if (!Delete_type) return 0;
882     Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
883     if (!Assign_type) return 0;
884     AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
885     if (!AugAssign_type) return 0;
886     AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
887     if (!AnnAssign_type) return 0;
888     For_type = make_type("For", stmt_type, For_fields, 4);
889     if (!For_type) return 0;
890     AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
891     if (!AsyncFor_type) return 0;
892     While_type = make_type("While", stmt_type, While_fields, 3);
893     if (!While_type) return 0;
894     If_type = make_type("If", stmt_type, If_fields, 3);
895     if (!If_type) return 0;
896     With_type = make_type("With", stmt_type, With_fields, 2);
897     if (!With_type) return 0;
898     AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
899     if (!AsyncWith_type) return 0;
900     Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
901     if (!Raise_type) return 0;
902     Try_type = make_type("Try", stmt_type, Try_fields, 4);
903     if (!Try_type) return 0;
904     Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
905     if (!Assert_type) return 0;
906     Import_type = make_type("Import", stmt_type, Import_fields, 1);
907     if (!Import_type) return 0;
908     ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
909     if (!ImportFrom_type) return 0;
910     Global_type = make_type("Global", stmt_type, Global_fields, 1);
911     if (!Global_type) return 0;
912     Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
913     if (!Nonlocal_type) return 0;
914     Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
915     if (!Expr_type) return 0;
916     Pass_type = make_type("Pass", stmt_type, NULL, 0);
917     if (!Pass_type) return 0;
918     Break_type = make_type("Break", stmt_type, NULL, 0);
919     if (!Break_type) return 0;
920     Continue_type = make_type("Continue", stmt_type, NULL, 0);
921     if (!Continue_type) return 0;
922     expr_type = make_type("expr", &AST_type, NULL, 0);
923     if (!expr_type) return 0;
924     if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
925     BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
926     if (!BoolOp_type) return 0;
927     BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
928     if (!BinOp_type) return 0;
929     UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
930     if (!UnaryOp_type) return 0;
931     Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
932     if (!Lambda_type) return 0;
933     IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
934     if (!IfExp_type) return 0;
935     Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
936     if (!Dict_type) return 0;
937     Set_type = make_type("Set", expr_type, Set_fields, 1);
938     if (!Set_type) return 0;
939     ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
940     if (!ListComp_type) return 0;
941     SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
942     if (!SetComp_type) return 0;
943     DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
944     if (!DictComp_type) return 0;
945     GeneratorExp_type = make_type("GeneratorExp", expr_type,
946                                   GeneratorExp_fields, 2);
947     if (!GeneratorExp_type) return 0;
948     Await_type = make_type("Await", expr_type, Await_fields, 1);
949     if (!Await_type) return 0;
950     Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
951     if (!Yield_type) return 0;
952     YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
953     if (!YieldFrom_type) return 0;
954     Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
955     if (!Compare_type) return 0;
956     Call_type = make_type("Call", expr_type, Call_fields, 3);
957     if (!Call_type) return 0;
958     Num_type = make_type("Num", expr_type, Num_fields, 1);
959     if (!Num_type) return 0;
960     Str_type = make_type("Str", expr_type, Str_fields, 1);
961     if (!Str_type) return 0;
962     FormattedValue_type = make_type("FormattedValue", expr_type,
963                                     FormattedValue_fields, 3);
964     if (!FormattedValue_type) return 0;
965     JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
966     if (!JoinedStr_type) return 0;
967     Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
968     if (!Bytes_type) return 0;
969     NameConstant_type = make_type("NameConstant", expr_type,
970                                   NameConstant_fields, 1);
971     if (!NameConstant_type) return 0;
972     Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
973     if (!Ellipsis_type) return 0;
974     Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
975     if (!Constant_type) return 0;
976     Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
977     if (!Attribute_type) return 0;
978     Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
979     if (!Subscript_type) return 0;
980     Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
981     if (!Starred_type) return 0;
982     Name_type = make_type("Name", expr_type, Name_fields, 2);
983     if (!Name_type) return 0;
984     List_type = make_type("List", expr_type, List_fields, 2);
985     if (!List_type) return 0;
986     Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
987     if (!Tuple_type) return 0;
988     expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
989     if (!expr_context_type) return 0;
990     if (!add_attributes(expr_context_type, NULL, 0)) return 0;
991     Load_type = make_type("Load", expr_context_type, NULL, 0);
992     if (!Load_type) return 0;
993     Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
994     if (!Load_singleton) return 0;
995     Store_type = make_type("Store", expr_context_type, NULL, 0);
996     if (!Store_type) return 0;
997     Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
998     if (!Store_singleton) return 0;
999     Del_type = make_type("Del", expr_context_type, NULL, 0);
1000     if (!Del_type) return 0;
1001     Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1002     if (!Del_singleton) return 0;
1003     AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1004     if (!AugLoad_type) return 0;
1005     AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1006     if (!AugLoad_singleton) return 0;
1007     AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1008     if (!AugStore_type) return 0;
1009     AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1010     if (!AugStore_singleton) return 0;
1011     Param_type = make_type("Param", expr_context_type, NULL, 0);
1012     if (!Param_type) return 0;
1013     Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1014     if (!Param_singleton) return 0;
1015     slice_type = make_type("slice", &AST_type, NULL, 0);
1016     if (!slice_type) return 0;
1017     if (!add_attributes(slice_type, NULL, 0)) return 0;
1018     Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1019     if (!Slice_type) return 0;
1020     ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1021     if (!ExtSlice_type) return 0;
1022     Index_type = make_type("Index", slice_type, Index_fields, 1);
1023     if (!Index_type) return 0;
1024     boolop_type = make_type("boolop", &AST_type, NULL, 0);
1025     if (!boolop_type) return 0;
1026     if (!add_attributes(boolop_type, NULL, 0)) return 0;
1027     And_type = make_type("And", boolop_type, NULL, 0);
1028     if (!And_type) return 0;
1029     And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1030     if (!And_singleton) return 0;
1031     Or_type = make_type("Or", boolop_type, NULL, 0);
1032     if (!Or_type) return 0;
1033     Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1034     if (!Or_singleton) return 0;
1035     operator_type = make_type("operator", &AST_type, NULL, 0);
1036     if (!operator_type) return 0;
1037     if (!add_attributes(operator_type, NULL, 0)) return 0;
1038     Add_type = make_type("Add", operator_type, NULL, 0);
1039     if (!Add_type) return 0;
1040     Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1041     if (!Add_singleton) return 0;
1042     Sub_type = make_type("Sub", operator_type, NULL, 0);
1043     if (!Sub_type) return 0;
1044     Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1045     if (!Sub_singleton) return 0;
1046     Mult_type = make_type("Mult", operator_type, NULL, 0);
1047     if (!Mult_type) return 0;
1048     Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1049     if (!Mult_singleton) return 0;
1050     MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1051     if (!MatMult_type) return 0;
1052     MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1053     if (!MatMult_singleton) return 0;
1054     Div_type = make_type("Div", operator_type, NULL, 0);
1055     if (!Div_type) return 0;
1056     Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1057     if (!Div_singleton) return 0;
1058     Mod_type = make_type("Mod", operator_type, NULL, 0);
1059     if (!Mod_type) return 0;
1060     Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1061     if (!Mod_singleton) return 0;
1062     Pow_type = make_type("Pow", operator_type, NULL, 0);
1063     if (!Pow_type) return 0;
1064     Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1065     if (!Pow_singleton) return 0;
1066     LShift_type = make_type("LShift", operator_type, NULL, 0);
1067     if (!LShift_type) return 0;
1068     LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1069     if (!LShift_singleton) return 0;
1070     RShift_type = make_type("RShift", operator_type, NULL, 0);
1071     if (!RShift_type) return 0;
1072     RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1073     if (!RShift_singleton) return 0;
1074     BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1075     if (!BitOr_type) return 0;
1076     BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1077     if (!BitOr_singleton) return 0;
1078     BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1079     if (!BitXor_type) return 0;
1080     BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1081     if (!BitXor_singleton) return 0;
1082     BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1083     if (!BitAnd_type) return 0;
1084     BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1085     if (!BitAnd_singleton) return 0;
1086     FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1087     if (!FloorDiv_type) return 0;
1088     FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1089     if (!FloorDiv_singleton) return 0;
1090     unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1091     if (!unaryop_type) return 0;
1092     if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1093     Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1094     if (!Invert_type) return 0;
1095     Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1096     if (!Invert_singleton) return 0;
1097     Not_type = make_type("Not", unaryop_type, NULL, 0);
1098     if (!Not_type) return 0;
1099     Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1100     if (!Not_singleton) return 0;
1101     UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1102     if (!UAdd_type) return 0;
1103     UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1104     if (!UAdd_singleton) return 0;
1105     USub_type = make_type("USub", unaryop_type, NULL, 0);
1106     if (!USub_type) return 0;
1107     USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1108     if (!USub_singleton) return 0;
1109     cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1110     if (!cmpop_type) return 0;
1111     if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1112     Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1113     if (!Eq_type) return 0;
1114     Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1115     if (!Eq_singleton) return 0;
1116     NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1117     if (!NotEq_type) return 0;
1118     NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1119     if (!NotEq_singleton) return 0;
1120     Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1121     if (!Lt_type) return 0;
1122     Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1123     if (!Lt_singleton) return 0;
1124     LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1125     if (!LtE_type) return 0;
1126     LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1127     if (!LtE_singleton) return 0;
1128     Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1129     if (!Gt_type) return 0;
1130     Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1131     if (!Gt_singleton) return 0;
1132     GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1133     if (!GtE_type) return 0;
1134     GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1135     if (!GtE_singleton) return 0;
1136     Is_type = make_type("Is", cmpop_type, NULL, 0);
1137     if (!Is_type) return 0;
1138     Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1139     if (!Is_singleton) return 0;
1140     IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1141     if (!IsNot_type) return 0;
1142     IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1143     if (!IsNot_singleton) return 0;
1144     In_type = make_type("In", cmpop_type, NULL, 0);
1145     if (!In_type) return 0;
1146     In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1147     if (!In_singleton) return 0;
1148     NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1149     if (!NotIn_type) return 0;
1150     NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1151     if (!NotIn_singleton) return 0;
1152     comprehension_type = make_type("comprehension", &AST_type,
1153                                    comprehension_fields, 4);
1154     if (!comprehension_type) return 0;
1155     if (!add_attributes(comprehension_type, NULL, 0)) return 0;
1156     excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1157     if (!excepthandler_type) return 0;
1158     if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1159         return 0;
1160     ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1161                                    ExceptHandler_fields, 3);
1162     if (!ExceptHandler_type) return 0;
1163     arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
1164     if (!arguments_type) return 0;
1165     if (!add_attributes(arguments_type, NULL, 0)) return 0;
1166     arg_type = make_type("arg", &AST_type, arg_fields, 2);
1167     if (!arg_type) return 0;
1168     if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
1169     keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1170     if (!keyword_type) return 0;
1171     if (!add_attributes(keyword_type, NULL, 0)) return 0;
1172     alias_type = make_type("alias", &AST_type, alias_fields, 2);
1173     if (!alias_type) return 0;
1174     if (!add_attributes(alias_type, NULL, 0)) return 0;
1175     withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1176     if (!withitem_type) return 0;
1177     if (!add_attributes(withitem_type, NULL, 0)) return 0;
1178     initialized = 1;
1179     return 1;
1180 }
1181 
1182 static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1183 static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1184 static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1185 static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1186                                 arena);
1187 static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1188 static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1189 static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1190 static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1191 static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1192 static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1193                                  arena);
1194 static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1195                                  arena);
1196 static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1197 static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1198 static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1199 static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
1200 static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
1201 
1202 mod_ty
Module(asdl_seq * body,PyArena * arena)1203 Module(asdl_seq * body, PyArena *arena)
1204 {
1205     mod_ty p;
1206     p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1207     if (!p)
1208         return NULL;
1209     p->kind = Module_kind;
1210     p->v.Module.body = body;
1211     return p;
1212 }
1213 
1214 mod_ty
Interactive(asdl_seq * body,PyArena * arena)1215 Interactive(asdl_seq * body, PyArena *arena)
1216 {
1217     mod_ty p;
1218     p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1219     if (!p)
1220         return NULL;
1221     p->kind = Interactive_kind;
1222     p->v.Interactive.body = body;
1223     return p;
1224 }
1225 
1226 mod_ty
Expression(expr_ty body,PyArena * arena)1227 Expression(expr_ty body, PyArena *arena)
1228 {
1229     mod_ty p;
1230     if (!body) {
1231         PyErr_SetString(PyExc_ValueError,
1232                         "field body is required for Expression");
1233         return NULL;
1234     }
1235     p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1236     if (!p)
1237         return NULL;
1238     p->kind = Expression_kind;
1239     p->v.Expression.body = body;
1240     return p;
1241 }
1242 
1243 mod_ty
Suite(asdl_seq * body,PyArena * arena)1244 Suite(asdl_seq * body, PyArena *arena)
1245 {
1246     mod_ty p;
1247     p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1248     if (!p)
1249         return NULL;
1250     p->kind = Suite_kind;
1251     p->v.Suite.body = body;
1252     return p;
1253 }
1254 
1255 stmt_ty
FunctionDef(identifier name,arguments_ty args,asdl_seq * body,asdl_seq * decorator_list,expr_ty returns,int lineno,int col_offset,PyArena * arena)1256 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
1257             decorator_list, expr_ty returns, int lineno, int col_offset,
1258             PyArena *arena)
1259 {
1260     stmt_ty p;
1261     if (!name) {
1262         PyErr_SetString(PyExc_ValueError,
1263                         "field name is required for FunctionDef");
1264         return NULL;
1265     }
1266     if (!args) {
1267         PyErr_SetString(PyExc_ValueError,
1268                         "field args is required for FunctionDef");
1269         return NULL;
1270     }
1271     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1272     if (!p)
1273         return NULL;
1274     p->kind = FunctionDef_kind;
1275     p->v.FunctionDef.name = name;
1276     p->v.FunctionDef.args = args;
1277     p->v.FunctionDef.body = body;
1278     p->v.FunctionDef.decorator_list = decorator_list;
1279     p->v.FunctionDef.returns = returns;
1280     p->lineno = lineno;
1281     p->col_offset = col_offset;
1282     return p;
1283 }
1284 
1285 stmt_ty
AsyncFunctionDef(identifier name,arguments_ty args,asdl_seq * body,asdl_seq * decorator_list,expr_ty returns,int lineno,int col_offset,PyArena * arena)1286 AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1287                  * decorator_list, expr_ty returns, int lineno, int col_offset,
1288                  PyArena *arena)
1289 {
1290     stmt_ty p;
1291     if (!name) {
1292         PyErr_SetString(PyExc_ValueError,
1293                         "field name is required for AsyncFunctionDef");
1294         return NULL;
1295     }
1296     if (!args) {
1297         PyErr_SetString(PyExc_ValueError,
1298                         "field args is required for AsyncFunctionDef");
1299         return NULL;
1300     }
1301     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1302     if (!p)
1303         return NULL;
1304     p->kind = AsyncFunctionDef_kind;
1305     p->v.AsyncFunctionDef.name = name;
1306     p->v.AsyncFunctionDef.args = args;
1307     p->v.AsyncFunctionDef.body = body;
1308     p->v.AsyncFunctionDef.decorator_list = decorator_list;
1309     p->v.AsyncFunctionDef.returns = returns;
1310     p->lineno = lineno;
1311     p->col_offset = col_offset;
1312     return p;
1313 }
1314 
1315 stmt_ty
ClassDef(identifier name,asdl_seq * bases,asdl_seq * keywords,asdl_seq * body,asdl_seq * decorator_list,int lineno,int col_offset,PyArena * arena)1316 ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1317          body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1318          *arena)
1319 {
1320     stmt_ty p;
1321     if (!name) {
1322         PyErr_SetString(PyExc_ValueError,
1323                         "field name is required for ClassDef");
1324         return NULL;
1325     }
1326     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1327     if (!p)
1328         return NULL;
1329     p->kind = ClassDef_kind;
1330     p->v.ClassDef.name = name;
1331     p->v.ClassDef.bases = bases;
1332     p->v.ClassDef.keywords = keywords;
1333     p->v.ClassDef.body = body;
1334     p->v.ClassDef.decorator_list = decorator_list;
1335     p->lineno = lineno;
1336     p->col_offset = col_offset;
1337     return p;
1338 }
1339 
1340 stmt_ty
Return(expr_ty value,int lineno,int col_offset,PyArena * arena)1341 Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
1342 {
1343     stmt_ty p;
1344     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1345     if (!p)
1346         return NULL;
1347     p->kind = Return_kind;
1348     p->v.Return.value = value;
1349     p->lineno = lineno;
1350     p->col_offset = col_offset;
1351     return p;
1352 }
1353 
1354 stmt_ty
Delete(asdl_seq * targets,int lineno,int col_offset,PyArena * arena)1355 Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
1356 {
1357     stmt_ty p;
1358     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1359     if (!p)
1360         return NULL;
1361     p->kind = Delete_kind;
1362     p->v.Delete.targets = targets;
1363     p->lineno = lineno;
1364     p->col_offset = col_offset;
1365     return p;
1366 }
1367 
1368 stmt_ty
Assign(asdl_seq * targets,expr_ty value,int lineno,int col_offset,PyArena * arena)1369 Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1370        *arena)
1371 {
1372     stmt_ty p;
1373     if (!value) {
1374         PyErr_SetString(PyExc_ValueError,
1375                         "field value is required for Assign");
1376         return NULL;
1377     }
1378     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1379     if (!p)
1380         return NULL;
1381     p->kind = Assign_kind;
1382     p->v.Assign.targets = targets;
1383     p->v.Assign.value = value;
1384     p->lineno = lineno;
1385     p->col_offset = col_offset;
1386     return p;
1387 }
1388 
1389 stmt_ty
AugAssign(expr_ty target,operator_ty op,expr_ty value,int lineno,int col_offset,PyArena * arena)1390 AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1391           col_offset, PyArena *arena)
1392 {
1393     stmt_ty p;
1394     if (!target) {
1395         PyErr_SetString(PyExc_ValueError,
1396                         "field target is required for AugAssign");
1397         return NULL;
1398     }
1399     if (!op) {
1400         PyErr_SetString(PyExc_ValueError,
1401                         "field op is required for AugAssign");
1402         return NULL;
1403     }
1404     if (!value) {
1405         PyErr_SetString(PyExc_ValueError,
1406                         "field value is required for AugAssign");
1407         return NULL;
1408     }
1409     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1410     if (!p)
1411         return NULL;
1412     p->kind = AugAssign_kind;
1413     p->v.AugAssign.target = target;
1414     p->v.AugAssign.op = op;
1415     p->v.AugAssign.value = value;
1416     p->lineno = lineno;
1417     p->col_offset = col_offset;
1418     return p;
1419 }
1420 
1421 stmt_ty
AnnAssign(expr_ty target,expr_ty annotation,expr_ty value,int simple,int lineno,int col_offset,PyArena * arena)1422 AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1423           lineno, int col_offset, PyArena *arena)
1424 {
1425     stmt_ty p;
1426     if (!target) {
1427         PyErr_SetString(PyExc_ValueError,
1428                         "field target is required for AnnAssign");
1429         return NULL;
1430     }
1431     if (!annotation) {
1432         PyErr_SetString(PyExc_ValueError,
1433                         "field annotation is required for AnnAssign");
1434         return NULL;
1435     }
1436     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1437     if (!p)
1438         return NULL;
1439     p->kind = AnnAssign_kind;
1440     p->v.AnnAssign.target = target;
1441     p->v.AnnAssign.annotation = annotation;
1442     p->v.AnnAssign.value = value;
1443     p->v.AnnAssign.simple = simple;
1444     p->lineno = lineno;
1445     p->col_offset = col_offset;
1446     return p;
1447 }
1448 
1449 stmt_ty
For(expr_ty target,expr_ty iter,asdl_seq * body,asdl_seq * orelse,int lineno,int col_offset,PyArena * arena)1450 For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1451     lineno, int col_offset, PyArena *arena)
1452 {
1453     stmt_ty p;
1454     if (!target) {
1455         PyErr_SetString(PyExc_ValueError,
1456                         "field target is required for For");
1457         return NULL;
1458     }
1459     if (!iter) {
1460         PyErr_SetString(PyExc_ValueError,
1461                         "field iter is required for For");
1462         return NULL;
1463     }
1464     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1465     if (!p)
1466         return NULL;
1467     p->kind = For_kind;
1468     p->v.For.target = target;
1469     p->v.For.iter = iter;
1470     p->v.For.body = body;
1471     p->v.For.orelse = orelse;
1472     p->lineno = lineno;
1473     p->col_offset = col_offset;
1474     return p;
1475 }
1476 
1477 stmt_ty
AsyncFor(expr_ty target,expr_ty iter,asdl_seq * body,asdl_seq * orelse,int lineno,int col_offset,PyArena * arena)1478 AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1479          lineno, int col_offset, PyArena *arena)
1480 {
1481     stmt_ty p;
1482     if (!target) {
1483         PyErr_SetString(PyExc_ValueError,
1484                         "field target is required for AsyncFor");
1485         return NULL;
1486     }
1487     if (!iter) {
1488         PyErr_SetString(PyExc_ValueError,
1489                         "field iter is required for AsyncFor");
1490         return NULL;
1491     }
1492     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1493     if (!p)
1494         return NULL;
1495     p->kind = AsyncFor_kind;
1496     p->v.AsyncFor.target = target;
1497     p->v.AsyncFor.iter = iter;
1498     p->v.AsyncFor.body = body;
1499     p->v.AsyncFor.orelse = orelse;
1500     p->lineno = lineno;
1501     p->col_offset = col_offset;
1502     return p;
1503 }
1504 
1505 stmt_ty
While(expr_ty test,asdl_seq * body,asdl_seq * orelse,int lineno,int col_offset,PyArena * arena)1506 While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1507       col_offset, PyArena *arena)
1508 {
1509     stmt_ty p;
1510     if (!test) {
1511         PyErr_SetString(PyExc_ValueError,
1512                         "field test is required for While");
1513         return NULL;
1514     }
1515     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1516     if (!p)
1517         return NULL;
1518     p->kind = While_kind;
1519     p->v.While.test = test;
1520     p->v.While.body = body;
1521     p->v.While.orelse = orelse;
1522     p->lineno = lineno;
1523     p->col_offset = col_offset;
1524     return p;
1525 }
1526 
1527 stmt_ty
If(expr_ty test,asdl_seq * body,asdl_seq * orelse,int lineno,int col_offset,PyArena * arena)1528 If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1529    col_offset, PyArena *arena)
1530 {
1531     stmt_ty p;
1532     if (!test) {
1533         PyErr_SetString(PyExc_ValueError,
1534                         "field test is required for If");
1535         return NULL;
1536     }
1537     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1538     if (!p)
1539         return NULL;
1540     p->kind = If_kind;
1541     p->v.If.test = test;
1542     p->v.If.body = body;
1543     p->v.If.orelse = orelse;
1544     p->lineno = lineno;
1545     p->col_offset = col_offset;
1546     return p;
1547 }
1548 
1549 stmt_ty
With(asdl_seq * items,asdl_seq * body,int lineno,int col_offset,PyArena * arena)1550 With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1551      *arena)
1552 {
1553     stmt_ty p;
1554     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1555     if (!p)
1556         return NULL;
1557     p->kind = With_kind;
1558     p->v.With.items = items;
1559     p->v.With.body = body;
1560     p->lineno = lineno;
1561     p->col_offset = col_offset;
1562     return p;
1563 }
1564 
1565 stmt_ty
AsyncWith(asdl_seq * items,asdl_seq * body,int lineno,int col_offset,PyArena * arena)1566 AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1567           PyArena *arena)
1568 {
1569     stmt_ty p;
1570     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1571     if (!p)
1572         return NULL;
1573     p->kind = AsyncWith_kind;
1574     p->v.AsyncWith.items = items;
1575     p->v.AsyncWith.body = body;
1576     p->lineno = lineno;
1577     p->col_offset = col_offset;
1578     return p;
1579 }
1580 
1581 stmt_ty
Raise(expr_ty exc,expr_ty cause,int lineno,int col_offset,PyArena * arena)1582 Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
1583 {
1584     stmt_ty p;
1585     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1586     if (!p)
1587         return NULL;
1588     p->kind = Raise_kind;
1589     p->v.Raise.exc = exc;
1590     p->v.Raise.cause = cause;
1591     p->lineno = lineno;
1592     p->col_offset = col_offset;
1593     return p;
1594 }
1595 
1596 stmt_ty
Try(asdl_seq * body,asdl_seq * handlers,asdl_seq * orelse,asdl_seq * finalbody,int lineno,int col_offset,PyArena * arena)1597 Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1598     finalbody, int lineno, int col_offset, PyArena *arena)
1599 {
1600     stmt_ty p;
1601     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1602     if (!p)
1603         return NULL;
1604     p->kind = Try_kind;
1605     p->v.Try.body = body;
1606     p->v.Try.handlers = handlers;
1607     p->v.Try.orelse = orelse;
1608     p->v.Try.finalbody = finalbody;
1609     p->lineno = lineno;
1610     p->col_offset = col_offset;
1611     return p;
1612 }
1613 
1614 stmt_ty
Assert(expr_ty test,expr_ty msg,int lineno,int col_offset,PyArena * arena)1615 Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
1616 {
1617     stmt_ty p;
1618     if (!test) {
1619         PyErr_SetString(PyExc_ValueError,
1620                         "field test is required for Assert");
1621         return NULL;
1622     }
1623     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1624     if (!p)
1625         return NULL;
1626     p->kind = Assert_kind;
1627     p->v.Assert.test = test;
1628     p->v.Assert.msg = msg;
1629     p->lineno = lineno;
1630     p->col_offset = col_offset;
1631     return p;
1632 }
1633 
1634 stmt_ty
Import(asdl_seq * names,int lineno,int col_offset,PyArena * arena)1635 Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1636 {
1637     stmt_ty p;
1638     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1639     if (!p)
1640         return NULL;
1641     p->kind = Import_kind;
1642     p->v.Import.names = names;
1643     p->lineno = lineno;
1644     p->col_offset = col_offset;
1645     return p;
1646 }
1647 
1648 stmt_ty
ImportFrom(identifier module,asdl_seq * names,int level,int lineno,int col_offset,PyArena * arena)1649 ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1650            col_offset, PyArena *arena)
1651 {
1652     stmt_ty p;
1653     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1654     if (!p)
1655         return NULL;
1656     p->kind = ImportFrom_kind;
1657     p->v.ImportFrom.module = module;
1658     p->v.ImportFrom.names = names;
1659     p->v.ImportFrom.level = level;
1660     p->lineno = lineno;
1661     p->col_offset = col_offset;
1662     return p;
1663 }
1664 
1665 stmt_ty
Global(asdl_seq * names,int lineno,int col_offset,PyArena * arena)1666 Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1667 {
1668     stmt_ty p;
1669     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1670     if (!p)
1671         return NULL;
1672     p->kind = Global_kind;
1673     p->v.Global.names = names;
1674     p->lineno = lineno;
1675     p->col_offset = col_offset;
1676     return p;
1677 }
1678 
1679 stmt_ty
Nonlocal(asdl_seq * names,int lineno,int col_offset,PyArena * arena)1680 Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1681 {
1682     stmt_ty p;
1683     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1684     if (!p)
1685         return NULL;
1686     p->kind = Nonlocal_kind;
1687     p->v.Nonlocal.names = names;
1688     p->lineno = lineno;
1689     p->col_offset = col_offset;
1690     return p;
1691 }
1692 
1693 stmt_ty
Expr(expr_ty value,int lineno,int col_offset,PyArena * arena)1694 Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
1695 {
1696     stmt_ty p;
1697     if (!value) {
1698         PyErr_SetString(PyExc_ValueError,
1699                         "field value is required for Expr");
1700         return NULL;
1701     }
1702     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1703     if (!p)
1704         return NULL;
1705     p->kind = Expr_kind;
1706     p->v.Expr.value = value;
1707     p->lineno = lineno;
1708     p->col_offset = col_offset;
1709     return p;
1710 }
1711 
1712 stmt_ty
Pass(int lineno,int col_offset,PyArena * arena)1713 Pass(int lineno, int col_offset, PyArena *arena)
1714 {
1715     stmt_ty p;
1716     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1717     if (!p)
1718         return NULL;
1719     p->kind = Pass_kind;
1720     p->lineno = lineno;
1721     p->col_offset = col_offset;
1722     return p;
1723 }
1724 
1725 stmt_ty
Break(int lineno,int col_offset,PyArena * arena)1726 Break(int lineno, int col_offset, PyArena *arena)
1727 {
1728     stmt_ty p;
1729     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1730     if (!p)
1731         return NULL;
1732     p->kind = Break_kind;
1733     p->lineno = lineno;
1734     p->col_offset = col_offset;
1735     return p;
1736 }
1737 
1738 stmt_ty
Continue(int lineno,int col_offset,PyArena * arena)1739 Continue(int lineno, int col_offset, PyArena *arena)
1740 {
1741     stmt_ty p;
1742     p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1743     if (!p)
1744         return NULL;
1745     p->kind = Continue_kind;
1746     p->lineno = lineno;
1747     p->col_offset = col_offset;
1748     return p;
1749 }
1750 
1751 expr_ty
BoolOp(boolop_ty op,asdl_seq * values,int lineno,int col_offset,PyArena * arena)1752 BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1753        *arena)
1754 {
1755     expr_ty p;
1756     if (!op) {
1757         PyErr_SetString(PyExc_ValueError,
1758                         "field op is required for BoolOp");
1759         return NULL;
1760     }
1761     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1762     if (!p)
1763         return NULL;
1764     p->kind = BoolOp_kind;
1765     p->v.BoolOp.op = op;
1766     p->v.BoolOp.values = values;
1767     p->lineno = lineno;
1768     p->col_offset = col_offset;
1769     return p;
1770 }
1771 
1772 expr_ty
BinOp(expr_ty left,operator_ty op,expr_ty right,int lineno,int col_offset,PyArena * arena)1773 BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1774       PyArena *arena)
1775 {
1776     expr_ty p;
1777     if (!left) {
1778         PyErr_SetString(PyExc_ValueError,
1779                         "field left is required for BinOp");
1780         return NULL;
1781     }
1782     if (!op) {
1783         PyErr_SetString(PyExc_ValueError,
1784                         "field op is required for BinOp");
1785         return NULL;
1786     }
1787     if (!right) {
1788         PyErr_SetString(PyExc_ValueError,
1789                         "field right is required for BinOp");
1790         return NULL;
1791     }
1792     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1793     if (!p)
1794         return NULL;
1795     p->kind = BinOp_kind;
1796     p->v.BinOp.left = left;
1797     p->v.BinOp.op = op;
1798     p->v.BinOp.right = right;
1799     p->lineno = lineno;
1800     p->col_offset = col_offset;
1801     return p;
1802 }
1803 
1804 expr_ty
UnaryOp(unaryop_ty op,expr_ty operand,int lineno,int col_offset,PyArena * arena)1805 UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1806         *arena)
1807 {
1808     expr_ty p;
1809     if (!op) {
1810         PyErr_SetString(PyExc_ValueError,
1811                         "field op is required for UnaryOp");
1812         return NULL;
1813     }
1814     if (!operand) {
1815         PyErr_SetString(PyExc_ValueError,
1816                         "field operand is required for UnaryOp");
1817         return NULL;
1818     }
1819     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1820     if (!p)
1821         return NULL;
1822     p->kind = UnaryOp_kind;
1823     p->v.UnaryOp.op = op;
1824     p->v.UnaryOp.operand = operand;
1825     p->lineno = lineno;
1826     p->col_offset = col_offset;
1827     return p;
1828 }
1829 
1830 expr_ty
Lambda(arguments_ty args,expr_ty body,int lineno,int col_offset,PyArena * arena)1831 Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1832        *arena)
1833 {
1834     expr_ty p;
1835     if (!args) {
1836         PyErr_SetString(PyExc_ValueError,
1837                         "field args is required for Lambda");
1838         return NULL;
1839     }
1840     if (!body) {
1841         PyErr_SetString(PyExc_ValueError,
1842                         "field body is required for Lambda");
1843         return NULL;
1844     }
1845     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1846     if (!p)
1847         return NULL;
1848     p->kind = Lambda_kind;
1849     p->v.Lambda.args = args;
1850     p->v.Lambda.body = body;
1851     p->lineno = lineno;
1852     p->col_offset = col_offset;
1853     return p;
1854 }
1855 
1856 expr_ty
IfExp(expr_ty test,expr_ty body,expr_ty orelse,int lineno,int col_offset,PyArena * arena)1857 IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1858       PyArena *arena)
1859 {
1860     expr_ty p;
1861     if (!test) {
1862         PyErr_SetString(PyExc_ValueError,
1863                         "field test is required for IfExp");
1864         return NULL;
1865     }
1866     if (!body) {
1867         PyErr_SetString(PyExc_ValueError,
1868                         "field body is required for IfExp");
1869         return NULL;
1870     }
1871     if (!orelse) {
1872         PyErr_SetString(PyExc_ValueError,
1873                         "field orelse is required for IfExp");
1874         return NULL;
1875     }
1876     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1877     if (!p)
1878         return NULL;
1879     p->kind = IfExp_kind;
1880     p->v.IfExp.test = test;
1881     p->v.IfExp.body = body;
1882     p->v.IfExp.orelse = orelse;
1883     p->lineno = lineno;
1884     p->col_offset = col_offset;
1885     return p;
1886 }
1887 
1888 expr_ty
Dict(asdl_seq * keys,asdl_seq * values,int lineno,int col_offset,PyArena * arena)1889 Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1890      *arena)
1891 {
1892     expr_ty p;
1893     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1894     if (!p)
1895         return NULL;
1896     p->kind = Dict_kind;
1897     p->v.Dict.keys = keys;
1898     p->v.Dict.values = values;
1899     p->lineno = lineno;
1900     p->col_offset = col_offset;
1901     return p;
1902 }
1903 
1904 expr_ty
Set(asdl_seq * elts,int lineno,int col_offset,PyArena * arena)1905 Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1906 {
1907     expr_ty p;
1908     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1909     if (!p)
1910         return NULL;
1911     p->kind = Set_kind;
1912     p->v.Set.elts = elts;
1913     p->lineno = lineno;
1914     p->col_offset = col_offset;
1915     return p;
1916 }
1917 
1918 expr_ty
ListComp(expr_ty elt,asdl_seq * generators,int lineno,int col_offset,PyArena * arena)1919 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1920          PyArena *arena)
1921 {
1922     expr_ty p;
1923     if (!elt) {
1924         PyErr_SetString(PyExc_ValueError,
1925                         "field elt is required for ListComp");
1926         return NULL;
1927     }
1928     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1929     if (!p)
1930         return NULL;
1931     p->kind = ListComp_kind;
1932     p->v.ListComp.elt = elt;
1933     p->v.ListComp.generators = generators;
1934     p->lineno = lineno;
1935     p->col_offset = col_offset;
1936     return p;
1937 }
1938 
1939 expr_ty
SetComp(expr_ty elt,asdl_seq * generators,int lineno,int col_offset,PyArena * arena)1940 SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1941         *arena)
1942 {
1943     expr_ty p;
1944     if (!elt) {
1945         PyErr_SetString(PyExc_ValueError,
1946                         "field elt is required for SetComp");
1947         return NULL;
1948     }
1949     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1950     if (!p)
1951         return NULL;
1952     p->kind = SetComp_kind;
1953     p->v.SetComp.elt = elt;
1954     p->v.SetComp.generators = generators;
1955     p->lineno = lineno;
1956     p->col_offset = col_offset;
1957     return p;
1958 }
1959 
1960 expr_ty
DictComp(expr_ty key,expr_ty value,asdl_seq * generators,int lineno,int col_offset,PyArena * arena)1961 DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1962          col_offset, PyArena *arena)
1963 {
1964     expr_ty p;
1965     if (!key) {
1966         PyErr_SetString(PyExc_ValueError,
1967                         "field key is required for DictComp");
1968         return NULL;
1969     }
1970     if (!value) {
1971         PyErr_SetString(PyExc_ValueError,
1972                         "field value is required for DictComp");
1973         return NULL;
1974     }
1975     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1976     if (!p)
1977         return NULL;
1978     p->kind = DictComp_kind;
1979     p->v.DictComp.key = key;
1980     p->v.DictComp.value = value;
1981     p->v.DictComp.generators = generators;
1982     p->lineno = lineno;
1983     p->col_offset = col_offset;
1984     return p;
1985 }
1986 
1987 expr_ty
GeneratorExp(expr_ty elt,asdl_seq * generators,int lineno,int col_offset,PyArena * arena)1988 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1989              PyArena *arena)
1990 {
1991     expr_ty p;
1992     if (!elt) {
1993         PyErr_SetString(PyExc_ValueError,
1994                         "field elt is required for GeneratorExp");
1995         return NULL;
1996     }
1997     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1998     if (!p)
1999         return NULL;
2000     p->kind = GeneratorExp_kind;
2001     p->v.GeneratorExp.elt = elt;
2002     p->v.GeneratorExp.generators = generators;
2003     p->lineno = lineno;
2004     p->col_offset = col_offset;
2005     return p;
2006 }
2007 
2008 expr_ty
Await(expr_ty value,int lineno,int col_offset,PyArena * arena)2009 Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2010 {
2011     expr_ty p;
2012     if (!value) {
2013         PyErr_SetString(PyExc_ValueError,
2014                         "field value is required for Await");
2015         return NULL;
2016     }
2017     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2018     if (!p)
2019         return NULL;
2020     p->kind = Await_kind;
2021     p->v.Await.value = value;
2022     p->lineno = lineno;
2023     p->col_offset = col_offset;
2024     return p;
2025 }
2026 
2027 expr_ty
Yield(expr_ty value,int lineno,int col_offset,PyArena * arena)2028 Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
2029 {
2030     expr_ty p;
2031     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2032     if (!p)
2033         return NULL;
2034     p->kind = Yield_kind;
2035     p->v.Yield.value = value;
2036     p->lineno = lineno;
2037     p->col_offset = col_offset;
2038     return p;
2039 }
2040 
2041 expr_ty
YieldFrom(expr_ty value,int lineno,int col_offset,PyArena * arena)2042 YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2043 {
2044     expr_ty p;
2045     if (!value) {
2046         PyErr_SetString(PyExc_ValueError,
2047                         "field value is required for YieldFrom");
2048         return NULL;
2049     }
2050     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2051     if (!p)
2052         return NULL;
2053     p->kind = YieldFrom_kind;
2054     p->v.YieldFrom.value = value;
2055     p->lineno = lineno;
2056     p->col_offset = col_offset;
2057     return p;
2058 }
2059 
2060 expr_ty
Compare(expr_ty left,asdl_int_seq * ops,asdl_seq * comparators,int lineno,int col_offset,PyArena * arena)2061 Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2062         int col_offset, PyArena *arena)
2063 {
2064     expr_ty p;
2065     if (!left) {
2066         PyErr_SetString(PyExc_ValueError,
2067                         "field left is required for Compare");
2068         return NULL;
2069     }
2070     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2071     if (!p)
2072         return NULL;
2073     p->kind = Compare_kind;
2074     p->v.Compare.left = left;
2075     p->v.Compare.ops = ops;
2076     p->v.Compare.comparators = comparators;
2077     p->lineno = lineno;
2078     p->col_offset = col_offset;
2079     return p;
2080 }
2081 
2082 expr_ty
Call(expr_ty func,asdl_seq * args,asdl_seq * keywords,int lineno,int col_offset,PyArena * arena)2083 Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2084      col_offset, PyArena *arena)
2085 {
2086     expr_ty p;
2087     if (!func) {
2088         PyErr_SetString(PyExc_ValueError,
2089                         "field func is required for Call");
2090         return NULL;
2091     }
2092     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2093     if (!p)
2094         return NULL;
2095     p->kind = Call_kind;
2096     p->v.Call.func = func;
2097     p->v.Call.args = args;
2098     p->v.Call.keywords = keywords;
2099     p->lineno = lineno;
2100     p->col_offset = col_offset;
2101     return p;
2102 }
2103 
2104 expr_ty
Num(object n,int lineno,int col_offset,PyArena * arena)2105 Num(object n, int lineno, int col_offset, PyArena *arena)
2106 {
2107     expr_ty p;
2108     if (!n) {
2109         PyErr_SetString(PyExc_ValueError,
2110                         "field n is required for Num");
2111         return NULL;
2112     }
2113     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2114     if (!p)
2115         return NULL;
2116     p->kind = Num_kind;
2117     p->v.Num.n = n;
2118     p->lineno = lineno;
2119     p->col_offset = col_offset;
2120     return p;
2121 }
2122 
2123 expr_ty
Str(string s,int lineno,int col_offset,PyArena * arena)2124 Str(string s, int lineno, int col_offset, PyArena *arena)
2125 {
2126     expr_ty p;
2127     if (!s) {
2128         PyErr_SetString(PyExc_ValueError,
2129                         "field s is required for Str");
2130         return NULL;
2131     }
2132     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2133     if (!p)
2134         return NULL;
2135     p->kind = Str_kind;
2136     p->v.Str.s = s;
2137     p->lineno = lineno;
2138     p->col_offset = col_offset;
2139     return p;
2140 }
2141 
2142 expr_ty
FormattedValue(expr_ty value,int conversion,expr_ty format_spec,int lineno,int col_offset,PyArena * arena)2143 FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2144                int col_offset, PyArena *arena)
2145 {
2146     expr_ty p;
2147     if (!value) {
2148         PyErr_SetString(PyExc_ValueError,
2149                         "field value is required for FormattedValue");
2150         return NULL;
2151     }
2152     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2153     if (!p)
2154         return NULL;
2155     p->kind = FormattedValue_kind;
2156     p->v.FormattedValue.value = value;
2157     p->v.FormattedValue.conversion = conversion;
2158     p->v.FormattedValue.format_spec = format_spec;
2159     p->lineno = lineno;
2160     p->col_offset = col_offset;
2161     return p;
2162 }
2163 
2164 expr_ty
JoinedStr(asdl_seq * values,int lineno,int col_offset,PyArena * arena)2165 JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2166 {
2167     expr_ty p;
2168     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2169     if (!p)
2170         return NULL;
2171     p->kind = JoinedStr_kind;
2172     p->v.JoinedStr.values = values;
2173     p->lineno = lineno;
2174     p->col_offset = col_offset;
2175     return p;
2176 }
2177 
2178 expr_ty
Bytes(bytes s,int lineno,int col_offset,PyArena * arena)2179 Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
2180 {
2181     expr_ty p;
2182     if (!s) {
2183         PyErr_SetString(PyExc_ValueError,
2184                         "field s is required for Bytes");
2185         return NULL;
2186     }
2187     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2188     if (!p)
2189         return NULL;
2190     p->kind = Bytes_kind;
2191     p->v.Bytes.s = s;
2192     p->lineno = lineno;
2193     p->col_offset = col_offset;
2194     return p;
2195 }
2196 
2197 expr_ty
NameConstant(singleton value,int lineno,int col_offset,PyArena * arena)2198 NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2199 {
2200     expr_ty p;
2201     if (!value) {
2202         PyErr_SetString(PyExc_ValueError,
2203                         "field value is required for NameConstant");
2204         return NULL;
2205     }
2206     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2207     if (!p)
2208         return NULL;
2209     p->kind = NameConstant_kind;
2210     p->v.NameConstant.value = value;
2211     p->lineno = lineno;
2212     p->col_offset = col_offset;
2213     return p;
2214 }
2215 
2216 expr_ty
Ellipsis(int lineno,int col_offset,PyArena * arena)2217 Ellipsis(int lineno, int col_offset, PyArena *arena)
2218 {
2219     expr_ty p;
2220     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2221     if (!p)
2222         return NULL;
2223     p->kind = Ellipsis_kind;
2224     p->lineno = lineno;
2225     p->col_offset = col_offset;
2226     return p;
2227 }
2228 
2229 expr_ty
Constant(constant value,int lineno,int col_offset,PyArena * arena)2230 Constant(constant value, int lineno, int col_offset, PyArena *arena)
2231 {
2232     expr_ty p;
2233     if (!value) {
2234         PyErr_SetString(PyExc_ValueError,
2235                         "field value is required for Constant");
2236         return NULL;
2237     }
2238     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2239     if (!p)
2240         return NULL;
2241     p->kind = Constant_kind;
2242     p->v.Constant.value = value;
2243     p->lineno = lineno;
2244     p->col_offset = col_offset;
2245     return p;
2246 }
2247 
2248 expr_ty
Attribute(expr_ty value,identifier attr,expr_context_ty ctx,int lineno,int col_offset,PyArena * arena)2249 Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2250           col_offset, PyArena *arena)
2251 {
2252     expr_ty p;
2253     if (!value) {
2254         PyErr_SetString(PyExc_ValueError,
2255                         "field value is required for Attribute");
2256         return NULL;
2257     }
2258     if (!attr) {
2259         PyErr_SetString(PyExc_ValueError,
2260                         "field attr is required for Attribute");
2261         return NULL;
2262     }
2263     if (!ctx) {
2264         PyErr_SetString(PyExc_ValueError,
2265                         "field ctx is required for Attribute");
2266         return NULL;
2267     }
2268     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2269     if (!p)
2270         return NULL;
2271     p->kind = Attribute_kind;
2272     p->v.Attribute.value = value;
2273     p->v.Attribute.attr = attr;
2274     p->v.Attribute.ctx = ctx;
2275     p->lineno = lineno;
2276     p->col_offset = col_offset;
2277     return p;
2278 }
2279 
2280 expr_ty
Subscript(expr_ty value,slice_ty slice,expr_context_ty ctx,int lineno,int col_offset,PyArena * arena)2281 Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2282           col_offset, PyArena *arena)
2283 {
2284     expr_ty p;
2285     if (!value) {
2286         PyErr_SetString(PyExc_ValueError,
2287                         "field value is required for Subscript");
2288         return NULL;
2289     }
2290     if (!slice) {
2291         PyErr_SetString(PyExc_ValueError,
2292                         "field slice is required for Subscript");
2293         return NULL;
2294     }
2295     if (!ctx) {
2296         PyErr_SetString(PyExc_ValueError,
2297                         "field ctx is required for Subscript");
2298         return NULL;
2299     }
2300     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2301     if (!p)
2302         return NULL;
2303     p->kind = Subscript_kind;
2304     p->v.Subscript.value = value;
2305     p->v.Subscript.slice = slice;
2306     p->v.Subscript.ctx = ctx;
2307     p->lineno = lineno;
2308     p->col_offset = col_offset;
2309     return p;
2310 }
2311 
2312 expr_ty
Starred(expr_ty value,expr_context_ty ctx,int lineno,int col_offset,PyArena * arena)2313 Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2314         *arena)
2315 {
2316     expr_ty p;
2317     if (!value) {
2318         PyErr_SetString(PyExc_ValueError,
2319                         "field value is required for Starred");
2320         return NULL;
2321     }
2322     if (!ctx) {
2323         PyErr_SetString(PyExc_ValueError,
2324                         "field ctx is required for Starred");
2325         return NULL;
2326     }
2327     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2328     if (!p)
2329         return NULL;
2330     p->kind = Starred_kind;
2331     p->v.Starred.value = value;
2332     p->v.Starred.ctx = ctx;
2333     p->lineno = lineno;
2334     p->col_offset = col_offset;
2335     return p;
2336 }
2337 
2338 expr_ty
Name(identifier id,expr_context_ty ctx,int lineno,int col_offset,PyArena * arena)2339 Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2340      *arena)
2341 {
2342     expr_ty p;
2343     if (!id) {
2344         PyErr_SetString(PyExc_ValueError,
2345                         "field id is required for Name");
2346         return NULL;
2347     }
2348     if (!ctx) {
2349         PyErr_SetString(PyExc_ValueError,
2350                         "field ctx is required for Name");
2351         return NULL;
2352     }
2353     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2354     if (!p)
2355         return NULL;
2356     p->kind = Name_kind;
2357     p->v.Name.id = id;
2358     p->v.Name.ctx = ctx;
2359     p->lineno = lineno;
2360     p->col_offset = col_offset;
2361     return p;
2362 }
2363 
2364 expr_ty
List(asdl_seq * elts,expr_context_ty ctx,int lineno,int col_offset,PyArena * arena)2365 List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2366      *arena)
2367 {
2368     expr_ty p;
2369     if (!ctx) {
2370         PyErr_SetString(PyExc_ValueError,
2371                         "field ctx is required for List");
2372         return NULL;
2373     }
2374     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2375     if (!p)
2376         return NULL;
2377     p->kind = List_kind;
2378     p->v.List.elts = elts;
2379     p->v.List.ctx = ctx;
2380     p->lineno = lineno;
2381     p->col_offset = col_offset;
2382     return p;
2383 }
2384 
2385 expr_ty
Tuple(asdl_seq * elts,expr_context_ty ctx,int lineno,int col_offset,PyArena * arena)2386 Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2387       *arena)
2388 {
2389     expr_ty p;
2390     if (!ctx) {
2391         PyErr_SetString(PyExc_ValueError,
2392                         "field ctx is required for Tuple");
2393         return NULL;
2394     }
2395     p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2396     if (!p)
2397         return NULL;
2398     p->kind = Tuple_kind;
2399     p->v.Tuple.elts = elts;
2400     p->v.Tuple.ctx = ctx;
2401     p->lineno = lineno;
2402     p->col_offset = col_offset;
2403     return p;
2404 }
2405 
2406 slice_ty
Slice(expr_ty lower,expr_ty upper,expr_ty step,PyArena * arena)2407 Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
2408 {
2409     slice_ty p;
2410     p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2411     if (!p)
2412         return NULL;
2413     p->kind = Slice_kind;
2414     p->v.Slice.lower = lower;
2415     p->v.Slice.upper = upper;
2416     p->v.Slice.step = step;
2417     return p;
2418 }
2419 
2420 slice_ty
ExtSlice(asdl_seq * dims,PyArena * arena)2421 ExtSlice(asdl_seq * dims, PyArena *arena)
2422 {
2423     slice_ty p;
2424     p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2425     if (!p)
2426         return NULL;
2427     p->kind = ExtSlice_kind;
2428     p->v.ExtSlice.dims = dims;
2429     return p;
2430 }
2431 
2432 slice_ty
Index(expr_ty value,PyArena * arena)2433 Index(expr_ty value, PyArena *arena)
2434 {
2435     slice_ty p;
2436     if (!value) {
2437         PyErr_SetString(PyExc_ValueError,
2438                         "field value is required for Index");
2439         return NULL;
2440     }
2441     p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2442     if (!p)
2443         return NULL;
2444     p->kind = Index_kind;
2445     p->v.Index.value = value;
2446     return p;
2447 }
2448 
2449 comprehension_ty
comprehension(expr_ty target,expr_ty iter,asdl_seq * ifs,int is_async,PyArena * arena)2450 comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2451               PyArena *arena)
2452 {
2453     comprehension_ty p;
2454     if (!target) {
2455         PyErr_SetString(PyExc_ValueError,
2456                         "field target is required for comprehension");
2457         return NULL;
2458     }
2459     if (!iter) {
2460         PyErr_SetString(PyExc_ValueError,
2461                         "field iter is required for comprehension");
2462         return NULL;
2463     }
2464     p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2465     if (!p)
2466         return NULL;
2467     p->target = target;
2468     p->iter = iter;
2469     p->ifs = ifs;
2470     p->is_async = is_async;
2471     return p;
2472 }
2473 
2474 excepthandler_ty
ExceptHandler(expr_ty type,identifier name,asdl_seq * body,int lineno,int col_offset,PyArena * arena)2475 ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
2476               col_offset, PyArena *arena)
2477 {
2478     excepthandler_ty p;
2479     p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2480     if (!p)
2481         return NULL;
2482     p->kind = ExceptHandler_kind;
2483     p->v.ExceptHandler.type = type;
2484     p->v.ExceptHandler.name = name;
2485     p->v.ExceptHandler.body = body;
2486     p->lineno = lineno;
2487     p->col_offset = col_offset;
2488     return p;
2489 }
2490 
2491 arguments_ty
arguments(asdl_seq * args,arg_ty vararg,asdl_seq * kwonlyargs,asdl_seq * kw_defaults,arg_ty kwarg,asdl_seq * defaults,PyArena * arena)2492 arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2493           kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
2494 {
2495     arguments_ty p;
2496     p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2497     if (!p)
2498         return NULL;
2499     p->args = args;
2500     p->vararg = vararg;
2501     p->kwonlyargs = kwonlyargs;
2502     p->kw_defaults = kw_defaults;
2503     p->kwarg = kwarg;
2504     p->defaults = defaults;
2505     return p;
2506 }
2507 
2508 arg_ty
arg(identifier arg,expr_ty annotation,int lineno,int col_offset,PyArena * arena)2509 arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2510     *arena)
2511 {
2512     arg_ty p;
2513     if (!arg) {
2514         PyErr_SetString(PyExc_ValueError,
2515                         "field arg is required for arg");
2516         return NULL;
2517     }
2518     p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2519     if (!p)
2520         return NULL;
2521     p->arg = arg;
2522     p->annotation = annotation;
2523     p->lineno = lineno;
2524     p->col_offset = col_offset;
2525     return p;
2526 }
2527 
2528 keyword_ty
keyword(identifier arg,expr_ty value,PyArena * arena)2529 keyword(identifier arg, expr_ty value, PyArena *arena)
2530 {
2531     keyword_ty p;
2532     if (!value) {
2533         PyErr_SetString(PyExc_ValueError,
2534                         "field value is required for keyword");
2535         return NULL;
2536     }
2537     p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2538     if (!p)
2539         return NULL;
2540     p->arg = arg;
2541     p->value = value;
2542     return p;
2543 }
2544 
2545 alias_ty
alias(identifier name,identifier asname,PyArena * arena)2546 alias(identifier name, identifier asname, PyArena *arena)
2547 {
2548     alias_ty p;
2549     if (!name) {
2550         PyErr_SetString(PyExc_ValueError,
2551                         "field name is required for alias");
2552         return NULL;
2553     }
2554     p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2555     if (!p)
2556         return NULL;
2557     p->name = name;
2558     p->asname = asname;
2559     return p;
2560 }
2561 
2562 withitem_ty
withitem(expr_ty context_expr,expr_ty optional_vars,PyArena * arena)2563 withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2564 {
2565     withitem_ty p;
2566     if (!context_expr) {
2567         PyErr_SetString(PyExc_ValueError,
2568                         "field context_expr is required for withitem");
2569         return NULL;
2570     }
2571     p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2572     if (!p)
2573         return NULL;
2574     p->context_expr = context_expr;
2575     p->optional_vars = optional_vars;
2576     return p;
2577 }
2578 
2579 
2580 PyObject*
ast2obj_mod(void * _o)2581 ast2obj_mod(void* _o)
2582 {
2583     mod_ty o = (mod_ty)_o;
2584     PyObject *result = NULL, *value = NULL;
2585     if (!o) {
2586         Py_INCREF(Py_None);
2587         return Py_None;
2588     }
2589 
2590     switch (o->kind) {
2591     case Module_kind:
2592         result = PyType_GenericNew(Module_type, NULL, NULL);
2593         if (!result) goto failed;
2594         value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2595         if (!value) goto failed;
2596         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2597             goto failed;
2598         Py_DECREF(value);
2599         break;
2600     case Interactive_kind:
2601         result = PyType_GenericNew(Interactive_type, NULL, NULL);
2602         if (!result) goto failed;
2603         value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2604         if (!value) goto failed;
2605         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2606             goto failed;
2607         Py_DECREF(value);
2608         break;
2609     case Expression_kind:
2610         result = PyType_GenericNew(Expression_type, NULL, NULL);
2611         if (!result) goto failed;
2612         value = ast2obj_expr(o->v.Expression.body);
2613         if (!value) goto failed;
2614         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2615             goto failed;
2616         Py_DECREF(value);
2617         break;
2618     case Suite_kind:
2619         result = PyType_GenericNew(Suite_type, NULL, NULL);
2620         if (!result) goto failed;
2621         value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2622         if (!value) goto failed;
2623         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2624             goto failed;
2625         Py_DECREF(value);
2626         break;
2627     }
2628     return result;
2629 failed:
2630     Py_XDECREF(value);
2631     Py_XDECREF(result);
2632     return NULL;
2633 }
2634 
2635 PyObject*
ast2obj_stmt(void * _o)2636 ast2obj_stmt(void* _o)
2637 {
2638     stmt_ty o = (stmt_ty)_o;
2639     PyObject *result = NULL, *value = NULL;
2640     if (!o) {
2641         Py_INCREF(Py_None);
2642         return Py_None;
2643     }
2644 
2645     switch (o->kind) {
2646     case FunctionDef_kind:
2647         result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2648         if (!result) goto failed;
2649         value = ast2obj_identifier(o->v.FunctionDef.name);
2650         if (!value) goto failed;
2651         if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2652             goto failed;
2653         Py_DECREF(value);
2654         value = ast2obj_arguments(o->v.FunctionDef.args);
2655         if (!value) goto failed;
2656         if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2657             goto failed;
2658         Py_DECREF(value);
2659         value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2660         if (!value) goto failed;
2661         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2662             goto failed;
2663         Py_DECREF(value);
2664         value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2665         if (!value) goto failed;
2666         if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2667             goto failed;
2668         Py_DECREF(value);
2669         value = ast2obj_expr(o->v.FunctionDef.returns);
2670         if (!value) goto failed;
2671         if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2672             goto failed;
2673         Py_DECREF(value);
2674         break;
2675     case AsyncFunctionDef_kind:
2676         result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2677         if (!result) goto failed;
2678         value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2679         if (!value) goto failed;
2680         if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2681             goto failed;
2682         Py_DECREF(value);
2683         value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2684         if (!value) goto failed;
2685         if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2686             goto failed;
2687         Py_DECREF(value);
2688         value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2689         if (!value) goto failed;
2690         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2691             goto failed;
2692         Py_DECREF(value);
2693         value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2694                              ast2obj_expr);
2695         if (!value) goto failed;
2696         if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2697             goto failed;
2698         Py_DECREF(value);
2699         value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2700         if (!value) goto failed;
2701         if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2702             goto failed;
2703         Py_DECREF(value);
2704         break;
2705     case ClassDef_kind:
2706         result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2707         if (!result) goto failed;
2708         value = ast2obj_identifier(o->v.ClassDef.name);
2709         if (!value) goto failed;
2710         if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2711             goto failed;
2712         Py_DECREF(value);
2713         value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2714         if (!value) goto failed;
2715         if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2716             goto failed;
2717         Py_DECREF(value);
2718         value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2719         if (!value) goto failed;
2720         if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2721             goto failed;
2722         Py_DECREF(value);
2723         value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2724         if (!value) goto failed;
2725         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2726             goto failed;
2727         Py_DECREF(value);
2728         value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2729         if (!value) goto failed;
2730         if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2731             goto failed;
2732         Py_DECREF(value);
2733         break;
2734     case Return_kind:
2735         result = PyType_GenericNew(Return_type, NULL, NULL);
2736         if (!result) goto failed;
2737         value = ast2obj_expr(o->v.Return.value);
2738         if (!value) goto failed;
2739         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2740             goto failed;
2741         Py_DECREF(value);
2742         break;
2743     case Delete_kind:
2744         result = PyType_GenericNew(Delete_type, NULL, NULL);
2745         if (!result) goto failed;
2746         value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2747         if (!value) goto failed;
2748         if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2749             goto failed;
2750         Py_DECREF(value);
2751         break;
2752     case Assign_kind:
2753         result = PyType_GenericNew(Assign_type, NULL, NULL);
2754         if (!result) goto failed;
2755         value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2756         if (!value) goto failed;
2757         if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2758             goto failed;
2759         Py_DECREF(value);
2760         value = ast2obj_expr(o->v.Assign.value);
2761         if (!value) goto failed;
2762         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2763             goto failed;
2764         Py_DECREF(value);
2765         break;
2766     case AugAssign_kind:
2767         result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2768         if (!result) goto failed;
2769         value = ast2obj_expr(o->v.AugAssign.target);
2770         if (!value) goto failed;
2771         if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2772             goto failed;
2773         Py_DECREF(value);
2774         value = ast2obj_operator(o->v.AugAssign.op);
2775         if (!value) goto failed;
2776         if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2777             goto failed;
2778         Py_DECREF(value);
2779         value = ast2obj_expr(o->v.AugAssign.value);
2780         if (!value) goto failed;
2781         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2782             goto failed;
2783         Py_DECREF(value);
2784         break;
2785     case AnnAssign_kind:
2786         result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2787         if (!result) goto failed;
2788         value = ast2obj_expr(o->v.AnnAssign.target);
2789         if (!value) goto failed;
2790         if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2791             goto failed;
2792         Py_DECREF(value);
2793         value = ast2obj_expr(o->v.AnnAssign.annotation);
2794         if (!value) goto failed;
2795         if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2796             goto failed;
2797         Py_DECREF(value);
2798         value = ast2obj_expr(o->v.AnnAssign.value);
2799         if (!value) goto failed;
2800         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2801             goto failed;
2802         Py_DECREF(value);
2803         value = ast2obj_int(o->v.AnnAssign.simple);
2804         if (!value) goto failed;
2805         if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2806             goto failed;
2807         Py_DECREF(value);
2808         break;
2809     case For_kind:
2810         result = PyType_GenericNew(For_type, NULL, NULL);
2811         if (!result) goto failed;
2812         value = ast2obj_expr(o->v.For.target);
2813         if (!value) goto failed;
2814         if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2815             goto failed;
2816         Py_DECREF(value);
2817         value = ast2obj_expr(o->v.For.iter);
2818         if (!value) goto failed;
2819         if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2820             goto failed;
2821         Py_DECREF(value);
2822         value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2823         if (!value) goto failed;
2824         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2825             goto failed;
2826         Py_DECREF(value);
2827         value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2828         if (!value) goto failed;
2829         if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2830             goto failed;
2831         Py_DECREF(value);
2832         break;
2833     case AsyncFor_kind:
2834         result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2835         if (!result) goto failed;
2836         value = ast2obj_expr(o->v.AsyncFor.target);
2837         if (!value) goto failed;
2838         if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2839             goto failed;
2840         Py_DECREF(value);
2841         value = ast2obj_expr(o->v.AsyncFor.iter);
2842         if (!value) goto failed;
2843         if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2844             goto failed;
2845         Py_DECREF(value);
2846         value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2847         if (!value) goto failed;
2848         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2849             goto failed;
2850         Py_DECREF(value);
2851         value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2852         if (!value) goto failed;
2853         if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2854             goto failed;
2855         Py_DECREF(value);
2856         break;
2857     case While_kind:
2858         result = PyType_GenericNew(While_type, NULL, NULL);
2859         if (!result) goto failed;
2860         value = ast2obj_expr(o->v.While.test);
2861         if (!value) goto failed;
2862         if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2863             goto failed;
2864         Py_DECREF(value);
2865         value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2866         if (!value) goto failed;
2867         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2868             goto failed;
2869         Py_DECREF(value);
2870         value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2871         if (!value) goto failed;
2872         if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2873             goto failed;
2874         Py_DECREF(value);
2875         break;
2876     case If_kind:
2877         result = PyType_GenericNew(If_type, NULL, NULL);
2878         if (!result) goto failed;
2879         value = ast2obj_expr(o->v.If.test);
2880         if (!value) goto failed;
2881         if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2882             goto failed;
2883         Py_DECREF(value);
2884         value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2885         if (!value) goto failed;
2886         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2887             goto failed;
2888         Py_DECREF(value);
2889         value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2890         if (!value) goto failed;
2891         if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2892             goto failed;
2893         Py_DECREF(value);
2894         break;
2895     case With_kind:
2896         result = PyType_GenericNew(With_type, NULL, NULL);
2897         if (!result) goto failed;
2898         value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2899         if (!value) goto failed;
2900         if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2901             goto failed;
2902         Py_DECREF(value);
2903         value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2904         if (!value) goto failed;
2905         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2906             goto failed;
2907         Py_DECREF(value);
2908         break;
2909     case AsyncWith_kind:
2910         result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2911         if (!result) goto failed;
2912         value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2913         if (!value) goto failed;
2914         if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2915             goto failed;
2916         Py_DECREF(value);
2917         value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2918         if (!value) goto failed;
2919         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2920             goto failed;
2921         Py_DECREF(value);
2922         break;
2923     case Raise_kind:
2924         result = PyType_GenericNew(Raise_type, NULL, NULL);
2925         if (!result) goto failed;
2926         value = ast2obj_expr(o->v.Raise.exc);
2927         if (!value) goto failed;
2928         if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2929             goto failed;
2930         Py_DECREF(value);
2931         value = ast2obj_expr(o->v.Raise.cause);
2932         if (!value) goto failed;
2933         if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2934             goto failed;
2935         Py_DECREF(value);
2936         break;
2937     case Try_kind:
2938         result = PyType_GenericNew(Try_type, NULL, NULL);
2939         if (!result) goto failed;
2940         value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2941         if (!value) goto failed;
2942         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2943             goto failed;
2944         Py_DECREF(value);
2945         value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2946         if (!value) goto failed;
2947         if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2948             goto failed;
2949         Py_DECREF(value);
2950         value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2951         if (!value) goto failed;
2952         if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2953             goto failed;
2954         Py_DECREF(value);
2955         value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2956         if (!value) goto failed;
2957         if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2958             goto failed;
2959         Py_DECREF(value);
2960         break;
2961     case Assert_kind:
2962         result = PyType_GenericNew(Assert_type, NULL, NULL);
2963         if (!result) goto failed;
2964         value = ast2obj_expr(o->v.Assert.test);
2965         if (!value) goto failed;
2966         if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2967             goto failed;
2968         Py_DECREF(value);
2969         value = ast2obj_expr(o->v.Assert.msg);
2970         if (!value) goto failed;
2971         if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2972             goto failed;
2973         Py_DECREF(value);
2974         break;
2975     case Import_kind:
2976         result = PyType_GenericNew(Import_type, NULL, NULL);
2977         if (!result) goto failed;
2978         value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2979         if (!value) goto failed;
2980         if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2981             goto failed;
2982         Py_DECREF(value);
2983         break;
2984     case ImportFrom_kind:
2985         result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2986         if (!result) goto failed;
2987         value = ast2obj_identifier(o->v.ImportFrom.module);
2988         if (!value) goto failed;
2989         if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2990             goto failed;
2991         Py_DECREF(value);
2992         value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2993         if (!value) goto failed;
2994         if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2995             goto failed;
2996         Py_DECREF(value);
2997         value = ast2obj_int(o->v.ImportFrom.level);
2998         if (!value) goto failed;
2999         if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3000             goto failed;
3001         Py_DECREF(value);
3002         break;
3003     case Global_kind:
3004         result = PyType_GenericNew(Global_type, NULL, NULL);
3005         if (!result) goto failed;
3006         value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3007         if (!value) goto failed;
3008         if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3009             goto failed;
3010         Py_DECREF(value);
3011         break;
3012     case Nonlocal_kind:
3013         result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3014         if (!result) goto failed;
3015         value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3016         if (!value) goto failed;
3017         if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3018             goto failed;
3019         Py_DECREF(value);
3020         break;
3021     case Expr_kind:
3022         result = PyType_GenericNew(Expr_type, NULL, NULL);
3023         if (!result) goto failed;
3024         value = ast2obj_expr(o->v.Expr.value);
3025         if (!value) goto failed;
3026         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3027             goto failed;
3028         Py_DECREF(value);
3029         break;
3030     case Pass_kind:
3031         result = PyType_GenericNew(Pass_type, NULL, NULL);
3032         if (!result) goto failed;
3033         break;
3034     case Break_kind:
3035         result = PyType_GenericNew(Break_type, NULL, NULL);
3036         if (!result) goto failed;
3037         break;
3038     case Continue_kind:
3039         result = PyType_GenericNew(Continue_type, NULL, NULL);
3040         if (!result) goto failed;
3041         break;
3042     }
3043     value = ast2obj_int(o->lineno);
3044     if (!value) goto failed;
3045     if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3046         goto failed;
3047     Py_DECREF(value);
3048     value = ast2obj_int(o->col_offset);
3049     if (!value) goto failed;
3050     if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3051         goto failed;
3052     Py_DECREF(value);
3053     return result;
3054 failed:
3055     Py_XDECREF(value);
3056     Py_XDECREF(result);
3057     return NULL;
3058 }
3059 
3060 PyObject*
ast2obj_expr(void * _o)3061 ast2obj_expr(void* _o)
3062 {
3063     expr_ty o = (expr_ty)_o;
3064     PyObject *result = NULL, *value = NULL;
3065     if (!o) {
3066         Py_INCREF(Py_None);
3067         return Py_None;
3068     }
3069 
3070     switch (o->kind) {
3071     case BoolOp_kind:
3072         result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3073         if (!result) goto failed;
3074         value = ast2obj_boolop(o->v.BoolOp.op);
3075         if (!value) goto failed;
3076         if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3077             goto failed;
3078         Py_DECREF(value);
3079         value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3080         if (!value) goto failed;
3081         if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3082             goto failed;
3083         Py_DECREF(value);
3084         break;
3085     case BinOp_kind:
3086         result = PyType_GenericNew(BinOp_type, NULL, NULL);
3087         if (!result) goto failed;
3088         value = ast2obj_expr(o->v.BinOp.left);
3089         if (!value) goto failed;
3090         if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3091             goto failed;
3092         Py_DECREF(value);
3093         value = ast2obj_operator(o->v.BinOp.op);
3094         if (!value) goto failed;
3095         if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3096             goto failed;
3097         Py_DECREF(value);
3098         value = ast2obj_expr(o->v.BinOp.right);
3099         if (!value) goto failed;
3100         if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3101             goto failed;
3102         Py_DECREF(value);
3103         break;
3104     case UnaryOp_kind:
3105         result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3106         if (!result) goto failed;
3107         value = ast2obj_unaryop(o->v.UnaryOp.op);
3108         if (!value) goto failed;
3109         if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3110             goto failed;
3111         Py_DECREF(value);
3112         value = ast2obj_expr(o->v.UnaryOp.operand);
3113         if (!value) goto failed;
3114         if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3115             goto failed;
3116         Py_DECREF(value);
3117         break;
3118     case Lambda_kind:
3119         result = PyType_GenericNew(Lambda_type, NULL, NULL);
3120         if (!result) goto failed;
3121         value = ast2obj_arguments(o->v.Lambda.args);
3122         if (!value) goto failed;
3123         if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3124             goto failed;
3125         Py_DECREF(value);
3126         value = ast2obj_expr(o->v.Lambda.body);
3127         if (!value) goto failed;
3128         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3129             goto failed;
3130         Py_DECREF(value);
3131         break;
3132     case IfExp_kind:
3133         result = PyType_GenericNew(IfExp_type, NULL, NULL);
3134         if (!result) goto failed;
3135         value = ast2obj_expr(o->v.IfExp.test);
3136         if (!value) goto failed;
3137         if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3138             goto failed;
3139         Py_DECREF(value);
3140         value = ast2obj_expr(o->v.IfExp.body);
3141         if (!value) goto failed;
3142         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3143             goto failed;
3144         Py_DECREF(value);
3145         value = ast2obj_expr(o->v.IfExp.orelse);
3146         if (!value) goto failed;
3147         if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3148             goto failed;
3149         Py_DECREF(value);
3150         break;
3151     case Dict_kind:
3152         result = PyType_GenericNew(Dict_type, NULL, NULL);
3153         if (!result) goto failed;
3154         value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3155         if (!value) goto failed;
3156         if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3157             goto failed;
3158         Py_DECREF(value);
3159         value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3160         if (!value) goto failed;
3161         if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3162             goto failed;
3163         Py_DECREF(value);
3164         break;
3165     case Set_kind:
3166         result = PyType_GenericNew(Set_type, NULL, NULL);
3167         if (!result) goto failed;
3168         value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3169         if (!value) goto failed;
3170         if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3171             goto failed;
3172         Py_DECREF(value);
3173         break;
3174     case ListComp_kind:
3175         result = PyType_GenericNew(ListComp_type, NULL, NULL);
3176         if (!result) goto failed;
3177         value = ast2obj_expr(o->v.ListComp.elt);
3178         if (!value) goto failed;
3179         if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3180             goto failed;
3181         Py_DECREF(value);
3182         value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3183         if (!value) goto failed;
3184         if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3185             goto failed;
3186         Py_DECREF(value);
3187         break;
3188     case SetComp_kind:
3189         result = PyType_GenericNew(SetComp_type, NULL, NULL);
3190         if (!result) goto failed;
3191         value = ast2obj_expr(o->v.SetComp.elt);
3192         if (!value) goto failed;
3193         if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3194             goto failed;
3195         Py_DECREF(value);
3196         value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3197         if (!value) goto failed;
3198         if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3199             goto failed;
3200         Py_DECREF(value);
3201         break;
3202     case DictComp_kind:
3203         result = PyType_GenericNew(DictComp_type, NULL, NULL);
3204         if (!result) goto failed;
3205         value = ast2obj_expr(o->v.DictComp.key);
3206         if (!value) goto failed;
3207         if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3208             goto failed;
3209         Py_DECREF(value);
3210         value = ast2obj_expr(o->v.DictComp.value);
3211         if (!value) goto failed;
3212         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3213             goto failed;
3214         Py_DECREF(value);
3215         value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3216         if (!value) goto failed;
3217         if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3218             goto failed;
3219         Py_DECREF(value);
3220         break;
3221     case GeneratorExp_kind:
3222         result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3223         if (!result) goto failed;
3224         value = ast2obj_expr(o->v.GeneratorExp.elt);
3225         if (!value) goto failed;
3226         if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3227             goto failed;
3228         Py_DECREF(value);
3229         value = ast2obj_list(o->v.GeneratorExp.generators,
3230                              ast2obj_comprehension);
3231         if (!value) goto failed;
3232         if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3233             goto failed;
3234         Py_DECREF(value);
3235         break;
3236     case Await_kind:
3237         result = PyType_GenericNew(Await_type, NULL, NULL);
3238         if (!result) goto failed;
3239         value = ast2obj_expr(o->v.Await.value);
3240         if (!value) goto failed;
3241         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3242             goto failed;
3243         Py_DECREF(value);
3244         break;
3245     case Yield_kind:
3246         result = PyType_GenericNew(Yield_type, NULL, NULL);
3247         if (!result) goto failed;
3248         value = ast2obj_expr(o->v.Yield.value);
3249         if (!value) goto failed;
3250         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3251             goto failed;
3252         Py_DECREF(value);
3253         break;
3254     case YieldFrom_kind:
3255         result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3256         if (!result) goto failed;
3257         value = ast2obj_expr(o->v.YieldFrom.value);
3258         if (!value) goto failed;
3259         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3260             goto failed;
3261         Py_DECREF(value);
3262         break;
3263     case Compare_kind:
3264         result = PyType_GenericNew(Compare_type, NULL, NULL);
3265         if (!result) goto failed;
3266         value = ast2obj_expr(o->v.Compare.left);
3267         if (!value) goto failed;
3268         if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3269             goto failed;
3270         Py_DECREF(value);
3271         {
3272             Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3273             value = PyList_New(n);
3274             if (!value) goto failed;
3275             for(i = 0; i < n; i++)
3276                 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
3277         }
3278         if (!value) goto failed;
3279         if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3280             goto failed;
3281         Py_DECREF(value);
3282         value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
3283         if (!value) goto failed;
3284         if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3285             goto failed;
3286         Py_DECREF(value);
3287         break;
3288     case Call_kind:
3289         result = PyType_GenericNew(Call_type, NULL, NULL);
3290         if (!result) goto failed;
3291         value = ast2obj_expr(o->v.Call.func);
3292         if (!value) goto failed;
3293         if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3294             goto failed;
3295         Py_DECREF(value);
3296         value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3297         if (!value) goto failed;
3298         if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3299             goto failed;
3300         Py_DECREF(value);
3301         value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3302         if (!value) goto failed;
3303         if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3304             goto failed;
3305         Py_DECREF(value);
3306         break;
3307     case Num_kind:
3308         result = PyType_GenericNew(Num_type, NULL, NULL);
3309         if (!result) goto failed;
3310         value = ast2obj_object(o->v.Num.n);
3311         if (!value) goto failed;
3312         if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3313             goto failed;
3314         Py_DECREF(value);
3315         break;
3316     case Str_kind:
3317         result = PyType_GenericNew(Str_type, NULL, NULL);
3318         if (!result) goto failed;
3319         value = ast2obj_string(o->v.Str.s);
3320         if (!value) goto failed;
3321         if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3322             goto failed;
3323         Py_DECREF(value);
3324         break;
3325     case FormattedValue_kind:
3326         result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3327         if (!result) goto failed;
3328         value = ast2obj_expr(o->v.FormattedValue.value);
3329         if (!value) goto failed;
3330         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3331             goto failed;
3332         Py_DECREF(value);
3333         value = ast2obj_int(o->v.FormattedValue.conversion);
3334         if (!value) goto failed;
3335         if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3336             goto failed;
3337         Py_DECREF(value);
3338         value = ast2obj_expr(o->v.FormattedValue.format_spec);
3339         if (!value) goto failed;
3340         if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3341             goto failed;
3342         Py_DECREF(value);
3343         break;
3344     case JoinedStr_kind:
3345         result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3346         if (!result) goto failed;
3347         value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3348         if (!value) goto failed;
3349         if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3350             goto failed;
3351         Py_DECREF(value);
3352         break;
3353     case Bytes_kind:
3354         result = PyType_GenericNew(Bytes_type, NULL, NULL);
3355         if (!result) goto failed;
3356         value = ast2obj_bytes(o->v.Bytes.s);
3357         if (!value) goto failed;
3358         if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3359             goto failed;
3360         Py_DECREF(value);
3361         break;
3362     case NameConstant_kind:
3363         result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3364         if (!result) goto failed;
3365         value = ast2obj_singleton(o->v.NameConstant.value);
3366         if (!value) goto failed;
3367         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3368             goto failed;
3369         Py_DECREF(value);
3370         break;
3371     case Ellipsis_kind:
3372         result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3373         if (!result) goto failed;
3374         break;
3375     case Constant_kind:
3376         result = PyType_GenericNew(Constant_type, NULL, NULL);
3377         if (!result) goto failed;
3378         value = ast2obj_constant(o->v.Constant.value);
3379         if (!value) goto failed;
3380         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3381             goto failed;
3382         Py_DECREF(value);
3383         break;
3384     case Attribute_kind:
3385         result = PyType_GenericNew(Attribute_type, NULL, NULL);
3386         if (!result) goto failed;
3387         value = ast2obj_expr(o->v.Attribute.value);
3388         if (!value) goto failed;
3389         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3390             goto failed;
3391         Py_DECREF(value);
3392         value = ast2obj_identifier(o->v.Attribute.attr);
3393         if (!value) goto failed;
3394         if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3395             goto failed;
3396         Py_DECREF(value);
3397         value = ast2obj_expr_context(o->v.Attribute.ctx);
3398         if (!value) goto failed;
3399         if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3400             goto failed;
3401         Py_DECREF(value);
3402         break;
3403     case Subscript_kind:
3404         result = PyType_GenericNew(Subscript_type, NULL, NULL);
3405         if (!result) goto failed;
3406         value = ast2obj_expr(o->v.Subscript.value);
3407         if (!value) goto failed;
3408         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3409             goto failed;
3410         Py_DECREF(value);
3411         value = ast2obj_slice(o->v.Subscript.slice);
3412         if (!value) goto failed;
3413         if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3414             goto failed;
3415         Py_DECREF(value);
3416         value = ast2obj_expr_context(o->v.Subscript.ctx);
3417         if (!value) goto failed;
3418         if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3419             goto failed;
3420         Py_DECREF(value);
3421         break;
3422     case Starred_kind:
3423         result = PyType_GenericNew(Starred_type, NULL, NULL);
3424         if (!result) goto failed;
3425         value = ast2obj_expr(o->v.Starred.value);
3426         if (!value) goto failed;
3427         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3428             goto failed;
3429         Py_DECREF(value);
3430         value = ast2obj_expr_context(o->v.Starred.ctx);
3431         if (!value) goto failed;
3432         if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3433             goto failed;
3434         Py_DECREF(value);
3435         break;
3436     case Name_kind:
3437         result = PyType_GenericNew(Name_type, NULL, NULL);
3438         if (!result) goto failed;
3439         value = ast2obj_identifier(o->v.Name.id);
3440         if (!value) goto failed;
3441         if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3442             goto failed;
3443         Py_DECREF(value);
3444         value = ast2obj_expr_context(o->v.Name.ctx);
3445         if (!value) goto failed;
3446         if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3447             goto failed;
3448         Py_DECREF(value);
3449         break;
3450     case List_kind:
3451         result = PyType_GenericNew(List_type, NULL, NULL);
3452         if (!result) goto failed;
3453         value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3454         if (!value) goto failed;
3455         if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3456             goto failed;
3457         Py_DECREF(value);
3458         value = ast2obj_expr_context(o->v.List.ctx);
3459         if (!value) goto failed;
3460         if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3461             goto failed;
3462         Py_DECREF(value);
3463         break;
3464     case Tuple_kind:
3465         result = PyType_GenericNew(Tuple_type, NULL, NULL);
3466         if (!result) goto failed;
3467         value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3468         if (!value) goto failed;
3469         if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3470             goto failed;
3471         Py_DECREF(value);
3472         value = ast2obj_expr_context(o->v.Tuple.ctx);
3473         if (!value) goto failed;
3474         if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3475             goto failed;
3476         Py_DECREF(value);
3477         break;
3478     }
3479     value = ast2obj_int(o->lineno);
3480     if (!value) goto failed;
3481     if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3482         goto failed;
3483     Py_DECREF(value);
3484     value = ast2obj_int(o->col_offset);
3485     if (!value) goto failed;
3486     if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3487         goto failed;
3488     Py_DECREF(value);
3489     return result;
3490 failed:
3491     Py_XDECREF(value);
3492     Py_XDECREF(result);
3493     return NULL;
3494 }
3495 
ast2obj_expr_context(expr_context_ty o)3496 PyObject* ast2obj_expr_context(expr_context_ty o)
3497 {
3498     switch(o) {
3499         case Load:
3500             Py_INCREF(Load_singleton);
3501             return Load_singleton;
3502         case Store:
3503             Py_INCREF(Store_singleton);
3504             return Store_singleton;
3505         case Del:
3506             Py_INCREF(Del_singleton);
3507             return Del_singleton;
3508         case AugLoad:
3509             Py_INCREF(AugLoad_singleton);
3510             return AugLoad_singleton;
3511         case AugStore:
3512             Py_INCREF(AugStore_singleton);
3513             return AugStore_singleton;
3514         case Param:
3515             Py_INCREF(Param_singleton);
3516             return Param_singleton;
3517         default:
3518             /* should never happen, but just in case ... */
3519             PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3520             return NULL;
3521     }
3522 }
3523 PyObject*
ast2obj_slice(void * _o)3524 ast2obj_slice(void* _o)
3525 {
3526     slice_ty o = (slice_ty)_o;
3527     PyObject *result = NULL, *value = NULL;
3528     if (!o) {
3529         Py_INCREF(Py_None);
3530         return Py_None;
3531     }
3532 
3533     switch (o->kind) {
3534     case Slice_kind:
3535         result = PyType_GenericNew(Slice_type, NULL, NULL);
3536         if (!result) goto failed;
3537         value = ast2obj_expr(o->v.Slice.lower);
3538         if (!value) goto failed;
3539         if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3540             goto failed;
3541         Py_DECREF(value);
3542         value = ast2obj_expr(o->v.Slice.upper);
3543         if (!value) goto failed;
3544         if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3545             goto failed;
3546         Py_DECREF(value);
3547         value = ast2obj_expr(o->v.Slice.step);
3548         if (!value) goto failed;
3549         if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3550             goto failed;
3551         Py_DECREF(value);
3552         break;
3553     case ExtSlice_kind:
3554         result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3555         if (!result) goto failed;
3556         value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3557         if (!value) goto failed;
3558         if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3559             goto failed;
3560         Py_DECREF(value);
3561         break;
3562     case Index_kind:
3563         result = PyType_GenericNew(Index_type, NULL, NULL);
3564         if (!result) goto failed;
3565         value = ast2obj_expr(o->v.Index.value);
3566         if (!value) goto failed;
3567         if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3568             goto failed;
3569         Py_DECREF(value);
3570         break;
3571     }
3572     return result;
3573 failed:
3574     Py_XDECREF(value);
3575     Py_XDECREF(result);
3576     return NULL;
3577 }
3578 
ast2obj_boolop(boolop_ty o)3579 PyObject* ast2obj_boolop(boolop_ty o)
3580 {
3581     switch(o) {
3582         case And:
3583             Py_INCREF(And_singleton);
3584             return And_singleton;
3585         case Or:
3586             Py_INCREF(Or_singleton);
3587             return Or_singleton;
3588         default:
3589             /* should never happen, but just in case ... */
3590             PyErr_Format(PyExc_SystemError, "unknown boolop found");
3591             return NULL;
3592     }
3593 }
ast2obj_operator(operator_ty o)3594 PyObject* ast2obj_operator(operator_ty o)
3595 {
3596     switch(o) {
3597         case Add:
3598             Py_INCREF(Add_singleton);
3599             return Add_singleton;
3600         case Sub:
3601             Py_INCREF(Sub_singleton);
3602             return Sub_singleton;
3603         case Mult:
3604             Py_INCREF(Mult_singleton);
3605             return Mult_singleton;
3606         case MatMult:
3607             Py_INCREF(MatMult_singleton);
3608             return MatMult_singleton;
3609         case Div:
3610             Py_INCREF(Div_singleton);
3611             return Div_singleton;
3612         case Mod:
3613             Py_INCREF(Mod_singleton);
3614             return Mod_singleton;
3615         case Pow:
3616             Py_INCREF(Pow_singleton);
3617             return Pow_singleton;
3618         case LShift:
3619             Py_INCREF(LShift_singleton);
3620             return LShift_singleton;
3621         case RShift:
3622             Py_INCREF(RShift_singleton);
3623             return RShift_singleton;
3624         case BitOr:
3625             Py_INCREF(BitOr_singleton);
3626             return BitOr_singleton;
3627         case BitXor:
3628             Py_INCREF(BitXor_singleton);
3629             return BitXor_singleton;
3630         case BitAnd:
3631             Py_INCREF(BitAnd_singleton);
3632             return BitAnd_singleton;
3633         case FloorDiv:
3634             Py_INCREF(FloorDiv_singleton);
3635             return FloorDiv_singleton;
3636         default:
3637             /* should never happen, but just in case ... */
3638             PyErr_Format(PyExc_SystemError, "unknown operator found");
3639             return NULL;
3640     }
3641 }
ast2obj_unaryop(unaryop_ty o)3642 PyObject* ast2obj_unaryop(unaryop_ty o)
3643 {
3644     switch(o) {
3645         case Invert:
3646             Py_INCREF(Invert_singleton);
3647             return Invert_singleton;
3648         case Not:
3649             Py_INCREF(Not_singleton);
3650             return Not_singleton;
3651         case UAdd:
3652             Py_INCREF(UAdd_singleton);
3653             return UAdd_singleton;
3654         case USub:
3655             Py_INCREF(USub_singleton);
3656             return USub_singleton;
3657         default:
3658             /* should never happen, but just in case ... */
3659             PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3660             return NULL;
3661     }
3662 }
ast2obj_cmpop(cmpop_ty o)3663 PyObject* ast2obj_cmpop(cmpop_ty o)
3664 {
3665     switch(o) {
3666         case Eq:
3667             Py_INCREF(Eq_singleton);
3668             return Eq_singleton;
3669         case NotEq:
3670             Py_INCREF(NotEq_singleton);
3671             return NotEq_singleton;
3672         case Lt:
3673             Py_INCREF(Lt_singleton);
3674             return Lt_singleton;
3675         case LtE:
3676             Py_INCREF(LtE_singleton);
3677             return LtE_singleton;
3678         case Gt:
3679             Py_INCREF(Gt_singleton);
3680             return Gt_singleton;
3681         case GtE:
3682             Py_INCREF(GtE_singleton);
3683             return GtE_singleton;
3684         case Is:
3685             Py_INCREF(Is_singleton);
3686             return Is_singleton;
3687         case IsNot:
3688             Py_INCREF(IsNot_singleton);
3689             return IsNot_singleton;
3690         case In:
3691             Py_INCREF(In_singleton);
3692             return In_singleton;
3693         case NotIn:
3694             Py_INCREF(NotIn_singleton);
3695             return NotIn_singleton;
3696         default:
3697             /* should never happen, but just in case ... */
3698             PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3699             return NULL;
3700     }
3701 }
3702 PyObject*
ast2obj_comprehension(void * _o)3703 ast2obj_comprehension(void* _o)
3704 {
3705     comprehension_ty o = (comprehension_ty)_o;
3706     PyObject *result = NULL, *value = NULL;
3707     if (!o) {
3708         Py_INCREF(Py_None);
3709         return Py_None;
3710     }
3711 
3712     result = PyType_GenericNew(comprehension_type, NULL, NULL);
3713     if (!result) return NULL;
3714     value = ast2obj_expr(o->target);
3715     if (!value) goto failed;
3716     if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3717         goto failed;
3718     Py_DECREF(value);
3719     value = ast2obj_expr(o->iter);
3720     if (!value) goto failed;
3721     if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3722         goto failed;
3723     Py_DECREF(value);
3724     value = ast2obj_list(o->ifs, ast2obj_expr);
3725     if (!value) goto failed;
3726     if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3727         goto failed;
3728     Py_DECREF(value);
3729     value = ast2obj_int(o->is_async);
3730     if (!value) goto failed;
3731     if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3732         goto failed;
3733     Py_DECREF(value);
3734     return result;
3735 failed:
3736     Py_XDECREF(value);
3737     Py_XDECREF(result);
3738     return NULL;
3739 }
3740 
3741 PyObject*
ast2obj_excepthandler(void * _o)3742 ast2obj_excepthandler(void* _o)
3743 {
3744     excepthandler_ty o = (excepthandler_ty)_o;
3745     PyObject *result = NULL, *value = NULL;
3746     if (!o) {
3747         Py_INCREF(Py_None);
3748         return Py_None;
3749     }
3750 
3751     switch (o->kind) {
3752     case ExceptHandler_kind:
3753         result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3754         if (!result) goto failed;
3755         value = ast2obj_expr(o->v.ExceptHandler.type);
3756         if (!value) goto failed;
3757         if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3758             goto failed;
3759         Py_DECREF(value);
3760         value = ast2obj_identifier(o->v.ExceptHandler.name);
3761         if (!value) goto failed;
3762         if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3763             goto failed;
3764         Py_DECREF(value);
3765         value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3766         if (!value) goto failed;
3767         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3768             goto failed;
3769         Py_DECREF(value);
3770         break;
3771     }
3772     value = ast2obj_int(o->lineno);
3773     if (!value) goto failed;
3774     if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3775         goto failed;
3776     Py_DECREF(value);
3777     value = ast2obj_int(o->col_offset);
3778     if (!value) goto failed;
3779     if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3780         goto failed;
3781     Py_DECREF(value);
3782     return result;
3783 failed:
3784     Py_XDECREF(value);
3785     Py_XDECREF(result);
3786     return NULL;
3787 }
3788 
3789 PyObject*
ast2obj_arguments(void * _o)3790 ast2obj_arguments(void* _o)
3791 {
3792     arguments_ty o = (arguments_ty)_o;
3793     PyObject *result = NULL, *value = NULL;
3794     if (!o) {
3795         Py_INCREF(Py_None);
3796         return Py_None;
3797     }
3798 
3799     result = PyType_GenericNew(arguments_type, NULL, NULL);
3800     if (!result) return NULL;
3801     value = ast2obj_list(o->args, ast2obj_arg);
3802     if (!value) goto failed;
3803     if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3804         goto failed;
3805     Py_DECREF(value);
3806     value = ast2obj_arg(o->vararg);
3807     if (!value) goto failed;
3808     if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3809         goto failed;
3810     Py_DECREF(value);
3811     value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3812     if (!value) goto failed;
3813     if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3814         goto failed;
3815     Py_DECREF(value);
3816     value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3817     if (!value) goto failed;
3818     if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
3819         goto failed;
3820     Py_DECREF(value);
3821     value = ast2obj_arg(o->kwarg);
3822     if (!value) goto failed;
3823     if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
3824         goto failed;
3825     Py_DECREF(value);
3826     value = ast2obj_list(o->defaults, ast2obj_expr);
3827     if (!value) goto failed;
3828     if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3829         goto failed;
3830     Py_DECREF(value);
3831     return result;
3832 failed:
3833     Py_XDECREF(value);
3834     Py_XDECREF(result);
3835     return NULL;
3836 }
3837 
3838 PyObject*
ast2obj_arg(void * _o)3839 ast2obj_arg(void* _o)
3840 {
3841     arg_ty o = (arg_ty)_o;
3842     PyObject *result = NULL, *value = NULL;
3843     if (!o) {
3844         Py_INCREF(Py_None);
3845         return Py_None;
3846     }
3847 
3848     result = PyType_GenericNew(arg_type, NULL, NULL);
3849     if (!result) return NULL;
3850     value = ast2obj_identifier(o->arg);
3851     if (!value) goto failed;
3852     if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3853         goto failed;
3854     Py_DECREF(value);
3855     value = ast2obj_expr(o->annotation);
3856     if (!value) goto failed;
3857     if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3858         goto failed;
3859     Py_DECREF(value);
3860     value = ast2obj_int(o->lineno);
3861     if (!value) goto failed;
3862     if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3863         goto failed;
3864     Py_DECREF(value);
3865     value = ast2obj_int(o->col_offset);
3866     if (!value) goto failed;
3867     if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3868         goto failed;
3869     Py_DECREF(value);
3870     return result;
3871 failed:
3872     Py_XDECREF(value);
3873     Py_XDECREF(result);
3874     return NULL;
3875 }
3876 
3877 PyObject*
ast2obj_keyword(void * _o)3878 ast2obj_keyword(void* _o)
3879 {
3880     keyword_ty o = (keyword_ty)_o;
3881     PyObject *result = NULL, *value = NULL;
3882     if (!o) {
3883         Py_INCREF(Py_None);
3884         return Py_None;
3885     }
3886 
3887     result = PyType_GenericNew(keyword_type, NULL, NULL);
3888     if (!result) return NULL;
3889     value = ast2obj_identifier(o->arg);
3890     if (!value) goto failed;
3891     if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3892         goto failed;
3893     Py_DECREF(value);
3894     value = ast2obj_expr(o->value);
3895     if (!value) goto failed;
3896     if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3897         goto failed;
3898     Py_DECREF(value);
3899     return result;
3900 failed:
3901     Py_XDECREF(value);
3902     Py_XDECREF(result);
3903     return NULL;
3904 }
3905 
3906 PyObject*
ast2obj_alias(void * _o)3907 ast2obj_alias(void* _o)
3908 {
3909     alias_ty o = (alias_ty)_o;
3910     PyObject *result = NULL, *value = NULL;
3911     if (!o) {
3912         Py_INCREF(Py_None);
3913         return Py_None;
3914     }
3915 
3916     result = PyType_GenericNew(alias_type, NULL, NULL);
3917     if (!result) return NULL;
3918     value = ast2obj_identifier(o->name);
3919     if (!value) goto failed;
3920     if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3921         goto failed;
3922     Py_DECREF(value);
3923     value = ast2obj_identifier(o->asname);
3924     if (!value) goto failed;
3925     if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3926         goto failed;
3927     Py_DECREF(value);
3928     return result;
3929 failed:
3930     Py_XDECREF(value);
3931     Py_XDECREF(result);
3932     return NULL;
3933 }
3934 
3935 PyObject*
ast2obj_withitem(void * _o)3936 ast2obj_withitem(void* _o)
3937 {
3938     withitem_ty o = (withitem_ty)_o;
3939     PyObject *result = NULL, *value = NULL;
3940     if (!o) {
3941         Py_INCREF(Py_None);
3942         return Py_None;
3943     }
3944 
3945     result = PyType_GenericNew(withitem_type, NULL, NULL);
3946     if (!result) return NULL;
3947     value = ast2obj_expr(o->context_expr);
3948     if (!value) goto failed;
3949     if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3950         goto failed;
3951     Py_DECREF(value);
3952     value = ast2obj_expr(o->optional_vars);
3953     if (!value) goto failed;
3954     if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3955         goto failed;
3956     Py_DECREF(value);
3957     return result;
3958 failed:
3959     Py_XDECREF(value);
3960     Py_XDECREF(result);
3961     return NULL;
3962 }
3963 
3964 
3965 int
obj2ast_mod(PyObject * obj,mod_ty * out,PyArena * arena)3966 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3967 {
3968     int isinstance;
3969 
3970     PyObject *tmp = NULL;
3971 
3972     if (obj == Py_None) {
3973         *out = NULL;
3974         return 0;
3975     }
3976     isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3977     if (isinstance == -1) {
3978         return 1;
3979     }
3980     if (isinstance) {
3981         asdl_seq* body;
3982 
3983         if (_PyObject_HasAttrId(obj, &PyId_body)) {
3984             int res;
3985             Py_ssize_t len;
3986             Py_ssize_t i;
3987             tmp = _PyObject_GetAttrId(obj, &PyId_body);
3988             if (tmp == NULL) goto failed;
3989             if (!PyList_Check(tmp)) {
3990                 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3991                 goto failed;
3992             }
3993             len = PyList_GET_SIZE(tmp);
3994             body = _Py_asdl_seq_new(len, arena);
3995             if (body == NULL) goto failed;
3996             for (i = 0; i < len; i++) {
3997                 stmt_ty value;
3998                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3999                 if (res != 0) goto failed;
4000                 if (len != PyList_GET_SIZE(tmp)) {
4001                     PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4002                     goto failed;
4003                 }
4004                 asdl_seq_SET(body, i, value);
4005             }
4006             Py_CLEAR(tmp);
4007         } else {
4008             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4009             return 1;
4010         }
4011         *out = Module(body, arena);
4012         if (*out == NULL) goto failed;
4013         return 0;
4014     }
4015     isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4016     if (isinstance == -1) {
4017         return 1;
4018     }
4019     if (isinstance) {
4020         asdl_seq* body;
4021 
4022         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4023             int res;
4024             Py_ssize_t len;
4025             Py_ssize_t i;
4026             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4027             if (tmp == NULL) goto failed;
4028             if (!PyList_Check(tmp)) {
4029                 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4030                 goto failed;
4031             }
4032             len = PyList_GET_SIZE(tmp);
4033             body = _Py_asdl_seq_new(len, arena);
4034             if (body == NULL) goto failed;
4035             for (i = 0; i < len; i++) {
4036                 stmt_ty value;
4037                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4038                 if (res != 0) goto failed;
4039                 if (len != PyList_GET_SIZE(tmp)) {
4040                     PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4041                     goto failed;
4042                 }
4043                 asdl_seq_SET(body, i, value);
4044             }
4045             Py_CLEAR(tmp);
4046         } else {
4047             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4048             return 1;
4049         }
4050         *out = Interactive(body, arena);
4051         if (*out == NULL) goto failed;
4052         return 0;
4053     }
4054     isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4055     if (isinstance == -1) {
4056         return 1;
4057     }
4058     if (isinstance) {
4059         expr_ty body;
4060 
4061         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4062             int res;
4063             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4064             if (tmp == NULL) goto failed;
4065             res = obj2ast_expr(tmp, &body, arena);
4066             if (res != 0) goto failed;
4067             Py_CLEAR(tmp);
4068         } else {
4069             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4070             return 1;
4071         }
4072         *out = Expression(body, arena);
4073         if (*out == NULL) goto failed;
4074         return 0;
4075     }
4076     isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4077     if (isinstance == -1) {
4078         return 1;
4079     }
4080     if (isinstance) {
4081         asdl_seq* body;
4082 
4083         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4084             int res;
4085             Py_ssize_t len;
4086             Py_ssize_t i;
4087             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4088             if (tmp == NULL) goto failed;
4089             if (!PyList_Check(tmp)) {
4090                 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4091                 goto failed;
4092             }
4093             len = PyList_GET_SIZE(tmp);
4094             body = _Py_asdl_seq_new(len, arena);
4095             if (body == NULL) goto failed;
4096             for (i = 0; i < len; i++) {
4097                 stmt_ty value;
4098                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4099                 if (res != 0) goto failed;
4100                 if (len != PyList_GET_SIZE(tmp)) {
4101                     PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4102                     goto failed;
4103                 }
4104                 asdl_seq_SET(body, i, value);
4105             }
4106             Py_CLEAR(tmp);
4107         } else {
4108             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4109             return 1;
4110         }
4111         *out = Suite(body, arena);
4112         if (*out == NULL) goto failed;
4113         return 0;
4114     }
4115 
4116     PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4117     failed:
4118     Py_XDECREF(tmp);
4119     return 1;
4120 }
4121 
4122 int
obj2ast_stmt(PyObject * obj,stmt_ty * out,PyArena * arena)4123 obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4124 {
4125     int isinstance;
4126 
4127     PyObject *tmp = NULL;
4128     int lineno;
4129     int col_offset;
4130 
4131     if (obj == Py_None) {
4132         *out = NULL;
4133         return 0;
4134     }
4135     if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4136         int res;
4137         tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4138         if (tmp == NULL) goto failed;
4139         res = obj2ast_int(tmp, &lineno, arena);
4140         if (res != 0) goto failed;
4141         Py_CLEAR(tmp);
4142     } else {
4143         PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
4144         return 1;
4145     }
4146     if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4147         int res;
4148         tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4149         if (tmp == NULL) goto failed;
4150         res = obj2ast_int(tmp, &col_offset, arena);
4151         if (res != 0) goto failed;
4152         Py_CLEAR(tmp);
4153     } else {
4154         PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4155         return 1;
4156     }
4157     isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4158     if (isinstance == -1) {
4159         return 1;
4160     }
4161     if (isinstance) {
4162         identifier name;
4163         arguments_ty args;
4164         asdl_seq* body;
4165         asdl_seq* decorator_list;
4166         expr_ty returns;
4167 
4168         if (_PyObject_HasAttrId(obj, &PyId_name)) {
4169             int res;
4170             tmp = _PyObject_GetAttrId(obj, &PyId_name);
4171             if (tmp == NULL) goto failed;
4172             res = obj2ast_identifier(tmp, &name, arena);
4173             if (res != 0) goto failed;
4174             Py_CLEAR(tmp);
4175         } else {
4176             PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4177             return 1;
4178         }
4179         if (_PyObject_HasAttrId(obj, &PyId_args)) {
4180             int res;
4181             tmp = _PyObject_GetAttrId(obj, &PyId_args);
4182             if (tmp == NULL) goto failed;
4183             res = obj2ast_arguments(tmp, &args, arena);
4184             if (res != 0) goto failed;
4185             Py_CLEAR(tmp);
4186         } else {
4187             PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4188             return 1;
4189         }
4190         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4191             int res;
4192             Py_ssize_t len;
4193             Py_ssize_t i;
4194             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4195             if (tmp == NULL) goto failed;
4196             if (!PyList_Check(tmp)) {
4197                 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4198                 goto failed;
4199             }
4200             len = PyList_GET_SIZE(tmp);
4201             body = _Py_asdl_seq_new(len, arena);
4202             if (body == NULL) goto failed;
4203             for (i = 0; i < len; i++) {
4204                 stmt_ty value;
4205                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4206                 if (res != 0) goto failed;
4207                 if (len != PyList_GET_SIZE(tmp)) {
4208                     PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4209                     goto failed;
4210                 }
4211                 asdl_seq_SET(body, i, value);
4212             }
4213             Py_CLEAR(tmp);
4214         } else {
4215             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4216             return 1;
4217         }
4218         if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4219             int res;
4220             Py_ssize_t len;
4221             Py_ssize_t i;
4222             tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4223             if (tmp == NULL) goto failed;
4224             if (!PyList_Check(tmp)) {
4225                 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4226                 goto failed;
4227             }
4228             len = PyList_GET_SIZE(tmp);
4229             decorator_list = _Py_asdl_seq_new(len, arena);
4230             if (decorator_list == NULL) goto failed;
4231             for (i = 0; i < len; i++) {
4232                 expr_ty value;
4233                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4234                 if (res != 0) goto failed;
4235                 if (len != PyList_GET_SIZE(tmp)) {
4236                     PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4237                     goto failed;
4238                 }
4239                 asdl_seq_SET(decorator_list, i, value);
4240             }
4241             Py_CLEAR(tmp);
4242         } else {
4243             PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4244             return 1;
4245         }
4246         if (exists_not_none(obj, &PyId_returns)) {
4247             int res;
4248             tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4249             if (tmp == NULL) goto failed;
4250             res = obj2ast_expr(tmp, &returns, arena);
4251             if (res != 0) goto failed;
4252             Py_CLEAR(tmp);
4253         } else {
4254             returns = NULL;
4255         }
4256         *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4257                            col_offset, arena);
4258         if (*out == NULL) goto failed;
4259         return 0;
4260     }
4261     isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4262     if (isinstance == -1) {
4263         return 1;
4264     }
4265     if (isinstance) {
4266         identifier name;
4267         arguments_ty args;
4268         asdl_seq* body;
4269         asdl_seq* decorator_list;
4270         expr_ty returns;
4271 
4272         if (_PyObject_HasAttrId(obj, &PyId_name)) {
4273             int res;
4274             tmp = _PyObject_GetAttrId(obj, &PyId_name);
4275             if (tmp == NULL) goto failed;
4276             res = obj2ast_identifier(tmp, &name, arena);
4277             if (res != 0) goto failed;
4278             Py_CLEAR(tmp);
4279         } else {
4280             PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4281             return 1;
4282         }
4283         if (_PyObject_HasAttrId(obj, &PyId_args)) {
4284             int res;
4285             tmp = _PyObject_GetAttrId(obj, &PyId_args);
4286             if (tmp == NULL) goto failed;
4287             res = obj2ast_arguments(tmp, &args, arena);
4288             if (res != 0) goto failed;
4289             Py_CLEAR(tmp);
4290         } else {
4291             PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4292             return 1;
4293         }
4294         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4295             int res;
4296             Py_ssize_t len;
4297             Py_ssize_t i;
4298             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4299             if (tmp == NULL) goto failed;
4300             if (!PyList_Check(tmp)) {
4301                 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4302                 goto failed;
4303             }
4304             len = PyList_GET_SIZE(tmp);
4305             body = _Py_asdl_seq_new(len, arena);
4306             if (body == NULL) goto failed;
4307             for (i = 0; i < len; i++) {
4308                 stmt_ty value;
4309                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4310                 if (res != 0) goto failed;
4311                 if (len != PyList_GET_SIZE(tmp)) {
4312                     PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4313                     goto failed;
4314                 }
4315                 asdl_seq_SET(body, i, value);
4316             }
4317             Py_CLEAR(tmp);
4318         } else {
4319             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4320             return 1;
4321         }
4322         if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4323             int res;
4324             Py_ssize_t len;
4325             Py_ssize_t i;
4326             tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4327             if (tmp == NULL) goto failed;
4328             if (!PyList_Check(tmp)) {
4329                 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4330                 goto failed;
4331             }
4332             len = PyList_GET_SIZE(tmp);
4333             decorator_list = _Py_asdl_seq_new(len, arena);
4334             if (decorator_list == NULL) goto failed;
4335             for (i = 0; i < len; i++) {
4336                 expr_ty value;
4337                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4338                 if (res != 0) goto failed;
4339                 if (len != PyList_GET_SIZE(tmp)) {
4340                     PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4341                     goto failed;
4342                 }
4343                 asdl_seq_SET(decorator_list, i, value);
4344             }
4345             Py_CLEAR(tmp);
4346         } else {
4347             PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4348             return 1;
4349         }
4350         if (exists_not_none(obj, &PyId_returns)) {
4351             int res;
4352             tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4353             if (tmp == NULL) goto failed;
4354             res = obj2ast_expr(tmp, &returns, arena);
4355             if (res != 0) goto failed;
4356             Py_CLEAR(tmp);
4357         } else {
4358             returns = NULL;
4359         }
4360         *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4361                                 lineno, col_offset, arena);
4362         if (*out == NULL) goto failed;
4363         return 0;
4364     }
4365     isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4366     if (isinstance == -1) {
4367         return 1;
4368     }
4369     if (isinstance) {
4370         identifier name;
4371         asdl_seq* bases;
4372         asdl_seq* keywords;
4373         asdl_seq* body;
4374         asdl_seq* decorator_list;
4375 
4376         if (_PyObject_HasAttrId(obj, &PyId_name)) {
4377             int res;
4378             tmp = _PyObject_GetAttrId(obj, &PyId_name);
4379             if (tmp == NULL) goto failed;
4380             res = obj2ast_identifier(tmp, &name, arena);
4381             if (res != 0) goto failed;
4382             Py_CLEAR(tmp);
4383         } else {
4384             PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4385             return 1;
4386         }
4387         if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4388             int res;
4389             Py_ssize_t len;
4390             Py_ssize_t i;
4391             tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4392             if (tmp == NULL) goto failed;
4393             if (!PyList_Check(tmp)) {
4394                 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4395                 goto failed;
4396             }
4397             len = PyList_GET_SIZE(tmp);
4398             bases = _Py_asdl_seq_new(len, arena);
4399             if (bases == NULL) goto failed;
4400             for (i = 0; i < len; i++) {
4401                 expr_ty value;
4402                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4403                 if (res != 0) goto failed;
4404                 if (len != PyList_GET_SIZE(tmp)) {
4405                     PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4406                     goto failed;
4407                 }
4408                 asdl_seq_SET(bases, i, value);
4409             }
4410             Py_CLEAR(tmp);
4411         } else {
4412             PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4413             return 1;
4414         }
4415         if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4416             int res;
4417             Py_ssize_t len;
4418             Py_ssize_t i;
4419             tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4420             if (tmp == NULL) goto failed;
4421             if (!PyList_Check(tmp)) {
4422                 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4423                 goto failed;
4424             }
4425             len = PyList_GET_SIZE(tmp);
4426             keywords = _Py_asdl_seq_new(len, arena);
4427             if (keywords == NULL) goto failed;
4428             for (i = 0; i < len; i++) {
4429                 keyword_ty value;
4430                 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4431                 if (res != 0) goto failed;
4432                 if (len != PyList_GET_SIZE(tmp)) {
4433                     PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4434                     goto failed;
4435                 }
4436                 asdl_seq_SET(keywords, i, value);
4437             }
4438             Py_CLEAR(tmp);
4439         } else {
4440             PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4441             return 1;
4442         }
4443         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4444             int res;
4445             Py_ssize_t len;
4446             Py_ssize_t i;
4447             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4448             if (tmp == NULL) goto failed;
4449             if (!PyList_Check(tmp)) {
4450                 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4451                 goto failed;
4452             }
4453             len = PyList_GET_SIZE(tmp);
4454             body = _Py_asdl_seq_new(len, arena);
4455             if (body == NULL) goto failed;
4456             for (i = 0; i < len; i++) {
4457                 stmt_ty value;
4458                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4459                 if (res != 0) goto failed;
4460                 if (len != PyList_GET_SIZE(tmp)) {
4461                     PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4462                     goto failed;
4463                 }
4464                 asdl_seq_SET(body, i, value);
4465             }
4466             Py_CLEAR(tmp);
4467         } else {
4468             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4469             return 1;
4470         }
4471         if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4472             int res;
4473             Py_ssize_t len;
4474             Py_ssize_t i;
4475             tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4476             if (tmp == NULL) goto failed;
4477             if (!PyList_Check(tmp)) {
4478                 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4479                 goto failed;
4480             }
4481             len = PyList_GET_SIZE(tmp);
4482             decorator_list = _Py_asdl_seq_new(len, arena);
4483             if (decorator_list == NULL) goto failed;
4484             for (i = 0; i < len; i++) {
4485                 expr_ty value;
4486                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4487                 if (res != 0) goto failed;
4488                 if (len != PyList_GET_SIZE(tmp)) {
4489                     PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4490                     goto failed;
4491                 }
4492                 asdl_seq_SET(decorator_list, i, value);
4493             }
4494             Py_CLEAR(tmp);
4495         } else {
4496             PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4497             return 1;
4498         }
4499         *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4500                         col_offset, arena);
4501         if (*out == NULL) goto failed;
4502         return 0;
4503     }
4504     isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4505     if (isinstance == -1) {
4506         return 1;
4507     }
4508     if (isinstance) {
4509         expr_ty value;
4510 
4511         if (exists_not_none(obj, &PyId_value)) {
4512             int res;
4513             tmp = _PyObject_GetAttrId(obj, &PyId_value);
4514             if (tmp == NULL) goto failed;
4515             res = obj2ast_expr(tmp, &value, arena);
4516             if (res != 0) goto failed;
4517             Py_CLEAR(tmp);
4518         } else {
4519             value = NULL;
4520         }
4521         *out = Return(value, lineno, col_offset, arena);
4522         if (*out == NULL) goto failed;
4523         return 0;
4524     }
4525     isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4526     if (isinstance == -1) {
4527         return 1;
4528     }
4529     if (isinstance) {
4530         asdl_seq* targets;
4531 
4532         if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4533             int res;
4534             Py_ssize_t len;
4535             Py_ssize_t i;
4536             tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4537             if (tmp == NULL) goto failed;
4538             if (!PyList_Check(tmp)) {
4539                 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4540                 goto failed;
4541             }
4542             len = PyList_GET_SIZE(tmp);
4543             targets = _Py_asdl_seq_new(len, arena);
4544             if (targets == NULL) goto failed;
4545             for (i = 0; i < len; i++) {
4546                 expr_ty value;
4547                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4548                 if (res != 0) goto failed;
4549                 if (len != PyList_GET_SIZE(tmp)) {
4550                     PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4551                     goto failed;
4552                 }
4553                 asdl_seq_SET(targets, i, value);
4554             }
4555             Py_CLEAR(tmp);
4556         } else {
4557             PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4558             return 1;
4559         }
4560         *out = Delete(targets, lineno, col_offset, arena);
4561         if (*out == NULL) goto failed;
4562         return 0;
4563     }
4564     isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4565     if (isinstance == -1) {
4566         return 1;
4567     }
4568     if (isinstance) {
4569         asdl_seq* targets;
4570         expr_ty value;
4571 
4572         if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4573             int res;
4574             Py_ssize_t len;
4575             Py_ssize_t i;
4576             tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4577             if (tmp == NULL) goto failed;
4578             if (!PyList_Check(tmp)) {
4579                 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4580                 goto failed;
4581             }
4582             len = PyList_GET_SIZE(tmp);
4583             targets = _Py_asdl_seq_new(len, arena);
4584             if (targets == NULL) goto failed;
4585             for (i = 0; i < len; i++) {
4586                 expr_ty value;
4587                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4588                 if (res != 0) goto failed;
4589                 if (len != PyList_GET_SIZE(tmp)) {
4590                     PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4591                     goto failed;
4592                 }
4593                 asdl_seq_SET(targets, i, value);
4594             }
4595             Py_CLEAR(tmp);
4596         } else {
4597             PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4598             return 1;
4599         }
4600         if (_PyObject_HasAttrId(obj, &PyId_value)) {
4601             int res;
4602             tmp = _PyObject_GetAttrId(obj, &PyId_value);
4603             if (tmp == NULL) goto failed;
4604             res = obj2ast_expr(tmp, &value, arena);
4605             if (res != 0) goto failed;
4606             Py_CLEAR(tmp);
4607         } else {
4608             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4609             return 1;
4610         }
4611         *out = Assign(targets, value, lineno, col_offset, arena);
4612         if (*out == NULL) goto failed;
4613         return 0;
4614     }
4615     isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4616     if (isinstance == -1) {
4617         return 1;
4618     }
4619     if (isinstance) {
4620         expr_ty target;
4621         operator_ty op;
4622         expr_ty value;
4623 
4624         if (_PyObject_HasAttrId(obj, &PyId_target)) {
4625             int res;
4626             tmp = _PyObject_GetAttrId(obj, &PyId_target);
4627             if (tmp == NULL) goto failed;
4628             res = obj2ast_expr(tmp, &target, arena);
4629             if (res != 0) goto failed;
4630             Py_CLEAR(tmp);
4631         } else {
4632             PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4633             return 1;
4634         }
4635         if (_PyObject_HasAttrId(obj, &PyId_op)) {
4636             int res;
4637             tmp = _PyObject_GetAttrId(obj, &PyId_op);
4638             if (tmp == NULL) goto failed;
4639             res = obj2ast_operator(tmp, &op, arena);
4640             if (res != 0) goto failed;
4641             Py_CLEAR(tmp);
4642         } else {
4643             PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4644             return 1;
4645         }
4646         if (_PyObject_HasAttrId(obj, &PyId_value)) {
4647             int res;
4648             tmp = _PyObject_GetAttrId(obj, &PyId_value);
4649             if (tmp == NULL) goto failed;
4650             res = obj2ast_expr(tmp, &value, arena);
4651             if (res != 0) goto failed;
4652             Py_CLEAR(tmp);
4653         } else {
4654             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4655             return 1;
4656         }
4657         *out = AugAssign(target, op, value, lineno, col_offset, arena);
4658         if (*out == NULL) goto failed;
4659         return 0;
4660     }
4661     isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4662     if (isinstance == -1) {
4663         return 1;
4664     }
4665     if (isinstance) {
4666         expr_ty target;
4667         expr_ty annotation;
4668         expr_ty value;
4669         int simple;
4670 
4671         if (_PyObject_HasAttrId(obj, &PyId_target)) {
4672             int res;
4673             tmp = _PyObject_GetAttrId(obj, &PyId_target);
4674             if (tmp == NULL) goto failed;
4675             res = obj2ast_expr(tmp, &target, arena);
4676             if (res != 0) goto failed;
4677             Py_CLEAR(tmp);
4678         } else {
4679             PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4680             return 1;
4681         }
4682         if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4683             int res;
4684             tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4685             if (tmp == NULL) goto failed;
4686             res = obj2ast_expr(tmp, &annotation, arena);
4687             if (res != 0) goto failed;
4688             Py_CLEAR(tmp);
4689         } else {
4690             PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4691             return 1;
4692         }
4693         if (exists_not_none(obj, &PyId_value)) {
4694             int res;
4695             tmp = _PyObject_GetAttrId(obj, &PyId_value);
4696             if (tmp == NULL) goto failed;
4697             res = obj2ast_expr(tmp, &value, arena);
4698             if (res != 0) goto failed;
4699             Py_CLEAR(tmp);
4700         } else {
4701             value = NULL;
4702         }
4703         if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4704             int res;
4705             tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4706             if (tmp == NULL) goto failed;
4707             res = obj2ast_int(tmp, &simple, arena);
4708             if (res != 0) goto failed;
4709             Py_CLEAR(tmp);
4710         } else {
4711             PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4712             return 1;
4713         }
4714         *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4715                          arena);
4716         if (*out == NULL) goto failed;
4717         return 0;
4718     }
4719     isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4720     if (isinstance == -1) {
4721         return 1;
4722     }
4723     if (isinstance) {
4724         expr_ty target;
4725         expr_ty iter;
4726         asdl_seq* body;
4727         asdl_seq* orelse;
4728 
4729         if (_PyObject_HasAttrId(obj, &PyId_target)) {
4730             int res;
4731             tmp = _PyObject_GetAttrId(obj, &PyId_target);
4732             if (tmp == NULL) goto failed;
4733             res = obj2ast_expr(tmp, &target, arena);
4734             if (res != 0) goto failed;
4735             Py_CLEAR(tmp);
4736         } else {
4737             PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4738             return 1;
4739         }
4740         if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4741             int res;
4742             tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4743             if (tmp == NULL) goto failed;
4744             res = obj2ast_expr(tmp, &iter, arena);
4745             if (res != 0) goto failed;
4746             Py_CLEAR(tmp);
4747         } else {
4748             PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4749             return 1;
4750         }
4751         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4752             int res;
4753             Py_ssize_t len;
4754             Py_ssize_t i;
4755             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4756             if (tmp == NULL) goto failed;
4757             if (!PyList_Check(tmp)) {
4758                 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4759                 goto failed;
4760             }
4761             len = PyList_GET_SIZE(tmp);
4762             body = _Py_asdl_seq_new(len, arena);
4763             if (body == NULL) goto failed;
4764             for (i = 0; i < len; i++) {
4765                 stmt_ty value;
4766                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4767                 if (res != 0) goto failed;
4768                 if (len != PyList_GET_SIZE(tmp)) {
4769                     PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
4770                     goto failed;
4771                 }
4772                 asdl_seq_SET(body, i, value);
4773             }
4774             Py_CLEAR(tmp);
4775         } else {
4776             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4777             return 1;
4778         }
4779         if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4780             int res;
4781             Py_ssize_t len;
4782             Py_ssize_t i;
4783             tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4784             if (tmp == NULL) goto failed;
4785             if (!PyList_Check(tmp)) {
4786                 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4787                 goto failed;
4788             }
4789             len = PyList_GET_SIZE(tmp);
4790             orelse = _Py_asdl_seq_new(len, arena);
4791             if (orelse == NULL) goto failed;
4792             for (i = 0; i < len; i++) {
4793                 stmt_ty value;
4794                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4795                 if (res != 0) goto failed;
4796                 if (len != PyList_GET_SIZE(tmp)) {
4797                     PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
4798                     goto failed;
4799                 }
4800                 asdl_seq_SET(orelse, i, value);
4801             }
4802             Py_CLEAR(tmp);
4803         } else {
4804             PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4805             return 1;
4806         }
4807         *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4808         if (*out == NULL) goto failed;
4809         return 0;
4810     }
4811     isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4812     if (isinstance == -1) {
4813         return 1;
4814     }
4815     if (isinstance) {
4816         expr_ty target;
4817         expr_ty iter;
4818         asdl_seq* body;
4819         asdl_seq* orelse;
4820 
4821         if (_PyObject_HasAttrId(obj, &PyId_target)) {
4822             int res;
4823             tmp = _PyObject_GetAttrId(obj, &PyId_target);
4824             if (tmp == NULL) goto failed;
4825             res = obj2ast_expr(tmp, &target, arena);
4826             if (res != 0) goto failed;
4827             Py_CLEAR(tmp);
4828         } else {
4829             PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4830             return 1;
4831         }
4832         if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4833             int res;
4834             tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4835             if (tmp == NULL) goto failed;
4836             res = obj2ast_expr(tmp, &iter, arena);
4837             if (res != 0) goto failed;
4838             Py_CLEAR(tmp);
4839         } else {
4840             PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4841             return 1;
4842         }
4843         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4844             int res;
4845             Py_ssize_t len;
4846             Py_ssize_t i;
4847             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4848             if (tmp == NULL) goto failed;
4849             if (!PyList_Check(tmp)) {
4850                 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4851                 goto failed;
4852             }
4853             len = PyList_GET_SIZE(tmp);
4854             body = _Py_asdl_seq_new(len, arena);
4855             if (body == NULL) goto failed;
4856             for (i = 0; i < len; i++) {
4857                 stmt_ty value;
4858                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4859                 if (res != 0) goto failed;
4860                 if (len != PyList_GET_SIZE(tmp)) {
4861                     PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
4862                     goto failed;
4863                 }
4864                 asdl_seq_SET(body, i, value);
4865             }
4866             Py_CLEAR(tmp);
4867         } else {
4868             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4869             return 1;
4870         }
4871         if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4872             int res;
4873             Py_ssize_t len;
4874             Py_ssize_t i;
4875             tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4876             if (tmp == NULL) goto failed;
4877             if (!PyList_Check(tmp)) {
4878                 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4879                 goto failed;
4880             }
4881             len = PyList_GET_SIZE(tmp);
4882             orelse = _Py_asdl_seq_new(len, arena);
4883             if (orelse == NULL) goto failed;
4884             for (i = 0; i < len; i++) {
4885                 stmt_ty value;
4886                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4887                 if (res != 0) goto failed;
4888                 if (len != PyList_GET_SIZE(tmp)) {
4889                     PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
4890                     goto failed;
4891                 }
4892                 asdl_seq_SET(orelse, i, value);
4893             }
4894             Py_CLEAR(tmp);
4895         } else {
4896             PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4897             return 1;
4898         }
4899         *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4900         if (*out == NULL) goto failed;
4901         return 0;
4902     }
4903     isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4904     if (isinstance == -1) {
4905         return 1;
4906     }
4907     if (isinstance) {
4908         expr_ty test;
4909         asdl_seq* body;
4910         asdl_seq* orelse;
4911 
4912         if (_PyObject_HasAttrId(obj, &PyId_test)) {
4913             int res;
4914             tmp = _PyObject_GetAttrId(obj, &PyId_test);
4915             if (tmp == NULL) goto failed;
4916             res = obj2ast_expr(tmp, &test, arena);
4917             if (res != 0) goto failed;
4918             Py_CLEAR(tmp);
4919         } else {
4920             PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4921             return 1;
4922         }
4923         if (_PyObject_HasAttrId(obj, &PyId_body)) {
4924             int res;
4925             Py_ssize_t len;
4926             Py_ssize_t i;
4927             tmp = _PyObject_GetAttrId(obj, &PyId_body);
4928             if (tmp == NULL) goto failed;
4929             if (!PyList_Check(tmp)) {
4930                 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4931                 goto failed;
4932             }
4933             len = PyList_GET_SIZE(tmp);
4934             body = _Py_asdl_seq_new(len, arena);
4935             if (body == NULL) goto failed;
4936             for (i = 0; i < len; i++) {
4937                 stmt_ty value;
4938                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4939                 if (res != 0) goto failed;
4940                 if (len != PyList_GET_SIZE(tmp)) {
4941                     PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
4942                     goto failed;
4943                 }
4944                 asdl_seq_SET(body, i, value);
4945             }
4946             Py_CLEAR(tmp);
4947         } else {
4948             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4949             return 1;
4950         }
4951         if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4952             int res;
4953             Py_ssize_t len;
4954             Py_ssize_t i;
4955             tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4956             if (tmp == NULL) goto failed;
4957             if (!PyList_Check(tmp)) {
4958                 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4959                 goto failed;
4960             }
4961             len = PyList_GET_SIZE(tmp);
4962             orelse = _Py_asdl_seq_new(len, arena);
4963             if (orelse == NULL) goto failed;
4964             for (i = 0; i < len; i++) {
4965                 stmt_ty value;
4966                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4967                 if (res != 0) goto failed;
4968                 if (len != PyList_GET_SIZE(tmp)) {
4969                     PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
4970                     goto failed;
4971                 }
4972                 asdl_seq_SET(orelse, i, value);
4973             }
4974             Py_CLEAR(tmp);
4975         } else {
4976             PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4977             return 1;
4978         }
4979         *out = While(test, body, orelse, lineno, col_offset, arena);
4980         if (*out == NULL) goto failed;
4981         return 0;
4982     }
4983     isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4984     if (isinstance == -1) {
4985         return 1;
4986     }
4987     if (isinstance) {
4988         expr_ty test;
4989         asdl_seq* body;
4990         asdl_seq* orelse;
4991 
4992         if (_PyObject_HasAttrId(obj, &PyId_test)) {
4993             int res;
4994             tmp = _PyObject_GetAttrId(obj, &PyId_test);
4995             if (tmp == NULL) goto failed;
4996             res = obj2ast_expr(tmp, &test, arena);
4997             if (res != 0) goto failed;
4998             Py_CLEAR(tmp);
4999         } else {
5000             PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
5001             return 1;
5002         }
5003         if (_PyObject_HasAttrId(obj, &PyId_body)) {
5004             int res;
5005             Py_ssize_t len;
5006             Py_ssize_t i;
5007             tmp = _PyObject_GetAttrId(obj, &PyId_body);
5008             if (tmp == NULL) goto failed;
5009             if (!PyList_Check(tmp)) {
5010                 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5011                 goto failed;
5012             }
5013             len = PyList_GET_SIZE(tmp);
5014             body = _Py_asdl_seq_new(len, arena);
5015             if (body == NULL) goto failed;
5016             for (i = 0; i < len; i++) {
5017                 stmt_ty value;
5018                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5019                 if (res != 0) goto failed;
5020                 if (len != PyList_GET_SIZE(tmp)) {
5021                     PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5022                     goto failed;
5023                 }
5024                 asdl_seq_SET(body, i, value);
5025             }
5026             Py_CLEAR(tmp);
5027         } else {
5028             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5029             return 1;
5030         }
5031         if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5032             int res;
5033             Py_ssize_t len;
5034             Py_ssize_t i;
5035             tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5036             if (tmp == NULL) goto failed;
5037             if (!PyList_Check(tmp)) {
5038                 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5039                 goto failed;
5040             }
5041             len = PyList_GET_SIZE(tmp);
5042             orelse = _Py_asdl_seq_new(len, arena);
5043             if (orelse == NULL) goto failed;
5044             for (i = 0; i < len; i++) {
5045                 stmt_ty value;
5046                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5047                 if (res != 0) goto failed;
5048                 if (len != PyList_GET_SIZE(tmp)) {
5049                     PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5050                     goto failed;
5051                 }
5052                 asdl_seq_SET(orelse, i, value);
5053             }
5054             Py_CLEAR(tmp);
5055         } else {
5056             PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5057             return 1;
5058         }
5059         *out = If(test, body, orelse, lineno, col_offset, arena);
5060         if (*out == NULL) goto failed;
5061         return 0;
5062     }
5063     isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5064     if (isinstance == -1) {
5065         return 1;
5066     }
5067     if (isinstance) {
5068         asdl_seq* items;
5069         asdl_seq* body;
5070 
5071         if (_PyObject_HasAttrId(obj, &PyId_items)) {
5072             int res;
5073             Py_ssize_t len;
5074             Py_ssize_t i;
5075             tmp = _PyObject_GetAttrId(obj, &PyId_items);
5076             if (tmp == NULL) goto failed;
5077             if (!PyList_Check(tmp)) {
5078                 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5079                 goto failed;
5080             }
5081             len = PyList_GET_SIZE(tmp);
5082             items = _Py_asdl_seq_new(len, arena);
5083             if (items == NULL) goto failed;
5084             for (i = 0; i < len; i++) {
5085                 withitem_ty value;
5086                 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5087                 if (res != 0) goto failed;
5088                 if (len != PyList_GET_SIZE(tmp)) {
5089                     PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5090                     goto failed;
5091                 }
5092                 asdl_seq_SET(items, i, value);
5093             }
5094             Py_CLEAR(tmp);
5095         } else {
5096             PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5097             return 1;
5098         }
5099         if (_PyObject_HasAttrId(obj, &PyId_body)) {
5100             int res;
5101             Py_ssize_t len;
5102             Py_ssize_t i;
5103             tmp = _PyObject_GetAttrId(obj, &PyId_body);
5104             if (tmp == NULL) goto failed;
5105             if (!PyList_Check(tmp)) {
5106                 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5107                 goto failed;
5108             }
5109             len = PyList_GET_SIZE(tmp);
5110             body = _Py_asdl_seq_new(len, arena);
5111             if (body == NULL) goto failed;
5112             for (i = 0; i < len; i++) {
5113                 stmt_ty value;
5114                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5115                 if (res != 0) goto failed;
5116                 if (len != PyList_GET_SIZE(tmp)) {
5117                     PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5118                     goto failed;
5119                 }
5120                 asdl_seq_SET(body, i, value);
5121             }
5122             Py_CLEAR(tmp);
5123         } else {
5124             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5125             return 1;
5126         }
5127         *out = With(items, body, lineno, col_offset, arena);
5128         if (*out == NULL) goto failed;
5129         return 0;
5130     }
5131     isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5132     if (isinstance == -1) {
5133         return 1;
5134     }
5135     if (isinstance) {
5136         asdl_seq* items;
5137         asdl_seq* body;
5138 
5139         if (_PyObject_HasAttrId(obj, &PyId_items)) {
5140             int res;
5141             Py_ssize_t len;
5142             Py_ssize_t i;
5143             tmp = _PyObject_GetAttrId(obj, &PyId_items);
5144             if (tmp == NULL) goto failed;
5145             if (!PyList_Check(tmp)) {
5146                 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5147                 goto failed;
5148             }
5149             len = PyList_GET_SIZE(tmp);
5150             items = _Py_asdl_seq_new(len, arena);
5151             if (items == NULL) goto failed;
5152             for (i = 0; i < len; i++) {
5153                 withitem_ty value;
5154                 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5155                 if (res != 0) goto failed;
5156                 if (len != PyList_GET_SIZE(tmp)) {
5157                     PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5158                     goto failed;
5159                 }
5160                 asdl_seq_SET(items, i, value);
5161             }
5162             Py_CLEAR(tmp);
5163         } else {
5164             PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5165             return 1;
5166         }
5167         if (_PyObject_HasAttrId(obj, &PyId_body)) {
5168             int res;
5169             Py_ssize_t len;
5170             Py_ssize_t i;
5171             tmp = _PyObject_GetAttrId(obj, &PyId_body);
5172             if (tmp == NULL) goto failed;
5173             if (!PyList_Check(tmp)) {
5174                 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5175                 goto failed;
5176             }
5177             len = PyList_GET_SIZE(tmp);
5178             body = _Py_asdl_seq_new(len, arena);
5179             if (body == NULL) goto failed;
5180             for (i = 0; i < len; i++) {
5181                 stmt_ty value;
5182                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5183                 if (res != 0) goto failed;
5184                 if (len != PyList_GET_SIZE(tmp)) {
5185                     PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5186                     goto failed;
5187                 }
5188                 asdl_seq_SET(body, i, value);
5189             }
5190             Py_CLEAR(tmp);
5191         } else {
5192             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5193             return 1;
5194         }
5195         *out = AsyncWith(items, body, lineno, col_offset, arena);
5196         if (*out == NULL) goto failed;
5197         return 0;
5198     }
5199     isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5200     if (isinstance == -1) {
5201         return 1;
5202     }
5203     if (isinstance) {
5204         expr_ty exc;
5205         expr_ty cause;
5206 
5207         if (exists_not_none(obj, &PyId_exc)) {
5208             int res;
5209             tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5210             if (tmp == NULL) goto failed;
5211             res = obj2ast_expr(tmp, &exc, arena);
5212             if (res != 0) goto failed;
5213             Py_CLEAR(tmp);
5214         } else {
5215             exc = NULL;
5216         }
5217         if (exists_not_none(obj, &PyId_cause)) {
5218             int res;
5219             tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5220             if (tmp == NULL) goto failed;
5221             res = obj2ast_expr(tmp, &cause, arena);
5222             if (res != 0) goto failed;
5223             Py_CLEAR(tmp);
5224         } else {
5225             cause = NULL;
5226         }
5227         *out = Raise(exc, cause, lineno, col_offset, arena);
5228         if (*out == NULL) goto failed;
5229         return 0;
5230     }
5231     isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5232     if (isinstance == -1) {
5233         return 1;
5234     }
5235     if (isinstance) {
5236         asdl_seq* body;
5237         asdl_seq* handlers;
5238         asdl_seq* orelse;
5239         asdl_seq* finalbody;
5240 
5241         if (_PyObject_HasAttrId(obj, &PyId_body)) {
5242             int res;
5243             Py_ssize_t len;
5244             Py_ssize_t i;
5245             tmp = _PyObject_GetAttrId(obj, &PyId_body);
5246             if (tmp == NULL) goto failed;
5247             if (!PyList_Check(tmp)) {
5248                 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5249                 goto failed;
5250             }
5251             len = PyList_GET_SIZE(tmp);
5252             body = _Py_asdl_seq_new(len, arena);
5253             if (body == NULL) goto failed;
5254             for (i = 0; i < len; i++) {
5255                 stmt_ty value;
5256                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5257                 if (res != 0) goto failed;
5258                 if (len != PyList_GET_SIZE(tmp)) {
5259                     PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5260                     goto failed;
5261                 }
5262                 asdl_seq_SET(body, i, value);
5263             }
5264             Py_CLEAR(tmp);
5265         } else {
5266             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5267             return 1;
5268         }
5269         if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5270             int res;
5271             Py_ssize_t len;
5272             Py_ssize_t i;
5273             tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5274             if (tmp == NULL) goto failed;
5275             if (!PyList_Check(tmp)) {
5276                 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5277                 goto failed;
5278             }
5279             len = PyList_GET_SIZE(tmp);
5280             handlers = _Py_asdl_seq_new(len, arena);
5281             if (handlers == NULL) goto failed;
5282             for (i = 0; i < len; i++) {
5283                 excepthandler_ty value;
5284                 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5285                 if (res != 0) goto failed;
5286                 if (len != PyList_GET_SIZE(tmp)) {
5287                     PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5288                     goto failed;
5289                 }
5290                 asdl_seq_SET(handlers, i, value);
5291             }
5292             Py_CLEAR(tmp);
5293         } else {
5294             PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5295             return 1;
5296         }
5297         if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5298             int res;
5299             Py_ssize_t len;
5300             Py_ssize_t i;
5301             tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5302             if (tmp == NULL) goto failed;
5303             if (!PyList_Check(tmp)) {
5304                 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5305                 goto failed;
5306             }
5307             len = PyList_GET_SIZE(tmp);
5308             orelse = _Py_asdl_seq_new(len, arena);
5309             if (orelse == NULL) goto failed;
5310             for (i = 0; i < len; i++) {
5311                 stmt_ty value;
5312                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5313                 if (res != 0) goto failed;
5314                 if (len != PyList_GET_SIZE(tmp)) {
5315                     PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5316                     goto failed;
5317                 }
5318                 asdl_seq_SET(orelse, i, value);
5319             }
5320             Py_CLEAR(tmp);
5321         } else {
5322             PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5323             return 1;
5324         }
5325         if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5326             int res;
5327             Py_ssize_t len;
5328             Py_ssize_t i;
5329             tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5330             if (tmp == NULL) goto failed;
5331             if (!PyList_Check(tmp)) {
5332                 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5333                 goto failed;
5334             }
5335             len = PyList_GET_SIZE(tmp);
5336             finalbody = _Py_asdl_seq_new(len, arena);
5337             if (finalbody == NULL) goto failed;
5338             for (i = 0; i < len; i++) {
5339                 stmt_ty value;
5340                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5341                 if (res != 0) goto failed;
5342                 if (len != PyList_GET_SIZE(tmp)) {
5343                     PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5344                     goto failed;
5345                 }
5346                 asdl_seq_SET(finalbody, i, value);
5347             }
5348             Py_CLEAR(tmp);
5349         } else {
5350             PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5351             return 1;
5352         }
5353         *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5354                    arena);
5355         if (*out == NULL) goto failed;
5356         return 0;
5357     }
5358     isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5359     if (isinstance == -1) {
5360         return 1;
5361     }
5362     if (isinstance) {
5363         expr_ty test;
5364         expr_ty msg;
5365 
5366         if (_PyObject_HasAttrId(obj, &PyId_test)) {
5367             int res;
5368             tmp = _PyObject_GetAttrId(obj, &PyId_test);
5369             if (tmp == NULL) goto failed;
5370             res = obj2ast_expr(tmp, &test, arena);
5371             if (res != 0) goto failed;
5372             Py_CLEAR(tmp);
5373         } else {
5374             PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5375             return 1;
5376         }
5377         if (exists_not_none(obj, &PyId_msg)) {
5378             int res;
5379             tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5380             if (tmp == NULL) goto failed;
5381             res = obj2ast_expr(tmp, &msg, arena);
5382             if (res != 0) goto failed;
5383             Py_CLEAR(tmp);
5384         } else {
5385             msg = NULL;
5386         }
5387         *out = Assert(test, msg, lineno, col_offset, arena);
5388         if (*out == NULL) goto failed;
5389         return 0;
5390     }
5391     isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5392     if (isinstance == -1) {
5393         return 1;
5394     }
5395     if (isinstance) {
5396         asdl_seq* names;
5397 
5398         if (_PyObject_HasAttrId(obj, &PyId_names)) {
5399             int res;
5400             Py_ssize_t len;
5401             Py_ssize_t i;
5402             tmp = _PyObject_GetAttrId(obj, &PyId_names);
5403             if (tmp == NULL) goto failed;
5404             if (!PyList_Check(tmp)) {
5405                 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5406                 goto failed;
5407             }
5408             len = PyList_GET_SIZE(tmp);
5409             names = _Py_asdl_seq_new(len, arena);
5410             if (names == NULL) goto failed;
5411             for (i = 0; i < len; i++) {
5412                 alias_ty value;
5413                 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5414                 if (res != 0) goto failed;
5415                 if (len != PyList_GET_SIZE(tmp)) {
5416                     PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5417                     goto failed;
5418                 }
5419                 asdl_seq_SET(names, i, value);
5420             }
5421             Py_CLEAR(tmp);
5422         } else {
5423             PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5424             return 1;
5425         }
5426         *out = Import(names, lineno, col_offset, arena);
5427         if (*out == NULL) goto failed;
5428         return 0;
5429     }
5430     isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5431     if (isinstance == -1) {
5432         return 1;
5433     }
5434     if (isinstance) {
5435         identifier module;
5436         asdl_seq* names;
5437         int level;
5438 
5439         if (exists_not_none(obj, &PyId_module)) {
5440             int res;
5441             tmp = _PyObject_GetAttrId(obj, &PyId_module);
5442             if (tmp == NULL) goto failed;
5443             res = obj2ast_identifier(tmp, &module, arena);
5444             if (res != 0) goto failed;
5445             Py_CLEAR(tmp);
5446         } else {
5447             module = NULL;
5448         }
5449         if (_PyObject_HasAttrId(obj, &PyId_names)) {
5450             int res;
5451             Py_ssize_t len;
5452             Py_ssize_t i;
5453             tmp = _PyObject_GetAttrId(obj, &PyId_names);
5454             if (tmp == NULL) goto failed;
5455             if (!PyList_Check(tmp)) {
5456                 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5457                 goto failed;
5458             }
5459             len = PyList_GET_SIZE(tmp);
5460             names = _Py_asdl_seq_new(len, arena);
5461             if (names == NULL) goto failed;
5462             for (i = 0; i < len; i++) {
5463                 alias_ty value;
5464                 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5465                 if (res != 0) goto failed;
5466                 if (len != PyList_GET_SIZE(tmp)) {
5467                     PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
5468                     goto failed;
5469                 }
5470                 asdl_seq_SET(names, i, value);
5471             }
5472             Py_CLEAR(tmp);
5473         } else {
5474             PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5475             return 1;
5476         }
5477         if (exists_not_none(obj, &PyId_level)) {
5478             int res;
5479             tmp = _PyObject_GetAttrId(obj, &PyId_level);
5480             if (tmp == NULL) goto failed;
5481             res = obj2ast_int(tmp, &level, arena);
5482             if (res != 0) goto failed;
5483             Py_CLEAR(tmp);
5484         } else {
5485             level = 0;
5486         }
5487         *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5488         if (*out == NULL) goto failed;
5489         return 0;
5490     }
5491     isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5492     if (isinstance == -1) {
5493         return 1;
5494     }
5495     if (isinstance) {
5496         asdl_seq* names;
5497 
5498         if (_PyObject_HasAttrId(obj, &PyId_names)) {
5499             int res;
5500             Py_ssize_t len;
5501             Py_ssize_t i;
5502             tmp = _PyObject_GetAttrId(obj, &PyId_names);
5503             if (tmp == NULL) goto failed;
5504             if (!PyList_Check(tmp)) {
5505                 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5506                 goto failed;
5507             }
5508             len = PyList_GET_SIZE(tmp);
5509             names = _Py_asdl_seq_new(len, arena);
5510             if (names == NULL) goto failed;
5511             for (i = 0; i < len; i++) {
5512                 identifier value;
5513                 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5514                 if (res != 0) goto failed;
5515                 if (len != PyList_GET_SIZE(tmp)) {
5516                     PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
5517                     goto failed;
5518                 }
5519                 asdl_seq_SET(names, i, value);
5520             }
5521             Py_CLEAR(tmp);
5522         } else {
5523             PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5524             return 1;
5525         }
5526         *out = Global(names, lineno, col_offset, arena);
5527         if (*out == NULL) goto failed;
5528         return 0;
5529     }
5530     isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5531     if (isinstance == -1) {
5532         return 1;
5533     }
5534     if (isinstance) {
5535         asdl_seq* names;
5536 
5537         if (_PyObject_HasAttrId(obj, &PyId_names)) {
5538             int res;
5539             Py_ssize_t len;
5540             Py_ssize_t i;
5541             tmp = _PyObject_GetAttrId(obj, &PyId_names);
5542             if (tmp == NULL) goto failed;
5543             if (!PyList_Check(tmp)) {
5544                 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5545                 goto failed;
5546             }
5547             len = PyList_GET_SIZE(tmp);
5548             names = _Py_asdl_seq_new(len, arena);
5549             if (names == NULL) goto failed;
5550             for (i = 0; i < len; i++) {
5551                 identifier value;
5552                 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5553                 if (res != 0) goto failed;
5554                 if (len != PyList_GET_SIZE(tmp)) {
5555                     PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
5556                     goto failed;
5557                 }
5558                 asdl_seq_SET(names, i, value);
5559             }
5560             Py_CLEAR(tmp);
5561         } else {
5562             PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5563             return 1;
5564         }
5565         *out = Nonlocal(names, lineno, col_offset, arena);
5566         if (*out == NULL) goto failed;
5567         return 0;
5568     }
5569     isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5570     if (isinstance == -1) {
5571         return 1;
5572     }
5573     if (isinstance) {
5574         expr_ty value;
5575 
5576         if (_PyObject_HasAttrId(obj, &PyId_value)) {
5577             int res;
5578             tmp = _PyObject_GetAttrId(obj, &PyId_value);
5579             if (tmp == NULL) goto failed;
5580             res = obj2ast_expr(tmp, &value, arena);
5581             if (res != 0) goto failed;
5582             Py_CLEAR(tmp);
5583         } else {
5584             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5585             return 1;
5586         }
5587         *out = Expr(value, lineno, col_offset, arena);
5588         if (*out == NULL) goto failed;
5589         return 0;
5590     }
5591     isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5592     if (isinstance == -1) {
5593         return 1;
5594     }
5595     if (isinstance) {
5596 
5597         *out = Pass(lineno, col_offset, arena);
5598         if (*out == NULL) goto failed;
5599         return 0;
5600     }
5601     isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5602     if (isinstance == -1) {
5603         return 1;
5604     }
5605     if (isinstance) {
5606 
5607         *out = Break(lineno, col_offset, arena);
5608         if (*out == NULL) goto failed;
5609         return 0;
5610     }
5611     isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5612     if (isinstance == -1) {
5613         return 1;
5614     }
5615     if (isinstance) {
5616 
5617         *out = Continue(lineno, col_offset, arena);
5618         if (*out == NULL) goto failed;
5619         return 0;
5620     }
5621 
5622     PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5623     failed:
5624     Py_XDECREF(tmp);
5625     return 1;
5626 }
5627 
5628 int
obj2ast_expr(PyObject * obj,expr_ty * out,PyArena * arena)5629 obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5630 {
5631     int isinstance;
5632 
5633     PyObject *tmp = NULL;
5634     int lineno;
5635     int col_offset;
5636 
5637     if (obj == Py_None) {
5638         *out = NULL;
5639         return 0;
5640     }
5641     if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5642         int res;
5643         tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5644         if (tmp == NULL) goto failed;
5645         res = obj2ast_int(tmp, &lineno, arena);
5646         if (res != 0) goto failed;
5647         Py_CLEAR(tmp);
5648     } else {
5649         PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
5650         return 1;
5651     }
5652     if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5653         int res;
5654         tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5655         if (tmp == NULL) goto failed;
5656         res = obj2ast_int(tmp, &col_offset, arena);
5657         if (res != 0) goto failed;
5658         Py_CLEAR(tmp);
5659     } else {
5660         PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5661         return 1;
5662     }
5663     isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5664     if (isinstance == -1) {
5665         return 1;
5666     }
5667     if (isinstance) {
5668         boolop_ty op;
5669         asdl_seq* values;
5670 
5671         if (_PyObject_HasAttrId(obj, &PyId_op)) {
5672             int res;
5673             tmp = _PyObject_GetAttrId(obj, &PyId_op);
5674             if (tmp == NULL) goto failed;
5675             res = obj2ast_boolop(tmp, &op, arena);
5676             if (res != 0) goto failed;
5677             Py_CLEAR(tmp);
5678         } else {
5679             PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5680             return 1;
5681         }
5682         if (_PyObject_HasAttrId(obj, &PyId_values)) {
5683             int res;
5684             Py_ssize_t len;
5685             Py_ssize_t i;
5686             tmp = _PyObject_GetAttrId(obj, &PyId_values);
5687             if (tmp == NULL) goto failed;
5688             if (!PyList_Check(tmp)) {
5689                 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5690                 goto failed;
5691             }
5692             len = PyList_GET_SIZE(tmp);
5693             values = _Py_asdl_seq_new(len, arena);
5694             if (values == NULL) goto failed;
5695             for (i = 0; i < len; i++) {
5696                 expr_ty value;
5697                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5698                 if (res != 0) goto failed;
5699                 if (len != PyList_GET_SIZE(tmp)) {
5700                     PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
5701                     goto failed;
5702                 }
5703                 asdl_seq_SET(values, i, value);
5704             }
5705             Py_CLEAR(tmp);
5706         } else {
5707             PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5708             return 1;
5709         }
5710         *out = BoolOp(op, values, lineno, col_offset, arena);
5711         if (*out == NULL) goto failed;
5712         return 0;
5713     }
5714     isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5715     if (isinstance == -1) {
5716         return 1;
5717     }
5718     if (isinstance) {
5719         expr_ty left;
5720         operator_ty op;
5721         expr_ty right;
5722 
5723         if (_PyObject_HasAttrId(obj, &PyId_left)) {
5724             int res;
5725             tmp = _PyObject_GetAttrId(obj, &PyId_left);
5726             if (tmp == NULL) goto failed;
5727             res = obj2ast_expr(tmp, &left, arena);
5728             if (res != 0) goto failed;
5729             Py_CLEAR(tmp);
5730         } else {
5731             PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5732             return 1;
5733         }
5734         if (_PyObject_HasAttrId(obj, &PyId_op)) {
5735             int res;
5736             tmp = _PyObject_GetAttrId(obj, &PyId_op);
5737             if (tmp == NULL) goto failed;
5738             res = obj2ast_operator(tmp, &op, arena);
5739             if (res != 0) goto failed;
5740             Py_CLEAR(tmp);
5741         } else {
5742             PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5743             return 1;
5744         }
5745         if (_PyObject_HasAttrId(obj, &PyId_right)) {
5746             int res;
5747             tmp = _PyObject_GetAttrId(obj, &PyId_right);
5748             if (tmp == NULL) goto failed;
5749             res = obj2ast_expr(tmp, &right, arena);
5750             if (res != 0) goto failed;
5751             Py_CLEAR(tmp);
5752         } else {
5753             PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5754             return 1;
5755         }
5756         *out = BinOp(left, op, right, lineno, col_offset, arena);
5757         if (*out == NULL) goto failed;
5758         return 0;
5759     }
5760     isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5761     if (isinstance == -1) {
5762         return 1;
5763     }
5764     if (isinstance) {
5765         unaryop_ty op;
5766         expr_ty operand;
5767 
5768         if (_PyObject_HasAttrId(obj, &PyId_op)) {
5769             int res;
5770             tmp = _PyObject_GetAttrId(obj, &PyId_op);
5771             if (tmp == NULL) goto failed;
5772             res = obj2ast_unaryop(tmp, &op, arena);
5773             if (res != 0) goto failed;
5774             Py_CLEAR(tmp);
5775         } else {
5776             PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5777             return 1;
5778         }
5779         if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5780             int res;
5781             tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5782             if (tmp == NULL) goto failed;
5783             res = obj2ast_expr(tmp, &operand, arena);
5784             if (res != 0) goto failed;
5785             Py_CLEAR(tmp);
5786         } else {
5787             PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5788             return 1;
5789         }
5790         *out = UnaryOp(op, operand, lineno, col_offset, arena);
5791         if (*out == NULL) goto failed;
5792         return 0;
5793     }
5794     isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5795     if (isinstance == -1) {
5796         return 1;
5797     }
5798     if (isinstance) {
5799         arguments_ty args;
5800         expr_ty body;
5801 
5802         if (_PyObject_HasAttrId(obj, &PyId_args)) {
5803             int res;
5804             tmp = _PyObject_GetAttrId(obj, &PyId_args);
5805             if (tmp == NULL) goto failed;
5806             res = obj2ast_arguments(tmp, &args, arena);
5807             if (res != 0) goto failed;
5808             Py_CLEAR(tmp);
5809         } else {
5810             PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5811             return 1;
5812         }
5813         if (_PyObject_HasAttrId(obj, &PyId_body)) {
5814             int res;
5815             tmp = _PyObject_GetAttrId(obj, &PyId_body);
5816             if (tmp == NULL) goto failed;
5817             res = obj2ast_expr(tmp, &body, arena);
5818             if (res != 0) goto failed;
5819             Py_CLEAR(tmp);
5820         } else {
5821             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5822             return 1;
5823         }
5824         *out = Lambda(args, body, lineno, col_offset, arena);
5825         if (*out == NULL) goto failed;
5826         return 0;
5827     }
5828     isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5829     if (isinstance == -1) {
5830         return 1;
5831     }
5832     if (isinstance) {
5833         expr_ty test;
5834         expr_ty body;
5835         expr_ty orelse;
5836 
5837         if (_PyObject_HasAttrId(obj, &PyId_test)) {
5838             int res;
5839             tmp = _PyObject_GetAttrId(obj, &PyId_test);
5840             if (tmp == NULL) goto failed;
5841             res = obj2ast_expr(tmp, &test, arena);
5842             if (res != 0) goto failed;
5843             Py_CLEAR(tmp);
5844         } else {
5845             PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5846             return 1;
5847         }
5848         if (_PyObject_HasAttrId(obj, &PyId_body)) {
5849             int res;
5850             tmp = _PyObject_GetAttrId(obj, &PyId_body);
5851             if (tmp == NULL) goto failed;
5852             res = obj2ast_expr(tmp, &body, arena);
5853             if (res != 0) goto failed;
5854             Py_CLEAR(tmp);
5855         } else {
5856             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5857             return 1;
5858         }
5859         if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5860             int res;
5861             tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5862             if (tmp == NULL) goto failed;
5863             res = obj2ast_expr(tmp, &orelse, arena);
5864             if (res != 0) goto failed;
5865             Py_CLEAR(tmp);
5866         } else {
5867             PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5868             return 1;
5869         }
5870         *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5871         if (*out == NULL) goto failed;
5872         return 0;
5873     }
5874     isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5875     if (isinstance == -1) {
5876         return 1;
5877     }
5878     if (isinstance) {
5879         asdl_seq* keys;
5880         asdl_seq* values;
5881 
5882         if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5883             int res;
5884             Py_ssize_t len;
5885             Py_ssize_t i;
5886             tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5887             if (tmp == NULL) goto failed;
5888             if (!PyList_Check(tmp)) {
5889                 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5890                 goto failed;
5891             }
5892             len = PyList_GET_SIZE(tmp);
5893             keys = _Py_asdl_seq_new(len, arena);
5894             if (keys == NULL) goto failed;
5895             for (i = 0; i < len; i++) {
5896                 expr_ty value;
5897                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5898                 if (res != 0) goto failed;
5899                 if (len != PyList_GET_SIZE(tmp)) {
5900                     PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
5901                     goto failed;
5902                 }
5903                 asdl_seq_SET(keys, i, value);
5904             }
5905             Py_CLEAR(tmp);
5906         } else {
5907             PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5908             return 1;
5909         }
5910         if (_PyObject_HasAttrId(obj, &PyId_values)) {
5911             int res;
5912             Py_ssize_t len;
5913             Py_ssize_t i;
5914             tmp = _PyObject_GetAttrId(obj, &PyId_values);
5915             if (tmp == NULL) goto failed;
5916             if (!PyList_Check(tmp)) {
5917                 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5918                 goto failed;
5919             }
5920             len = PyList_GET_SIZE(tmp);
5921             values = _Py_asdl_seq_new(len, arena);
5922             if (values == NULL) goto failed;
5923             for (i = 0; i < len; i++) {
5924                 expr_ty value;
5925                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5926                 if (res != 0) goto failed;
5927                 if (len != PyList_GET_SIZE(tmp)) {
5928                     PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
5929                     goto failed;
5930                 }
5931                 asdl_seq_SET(values, i, value);
5932             }
5933             Py_CLEAR(tmp);
5934         } else {
5935             PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5936             return 1;
5937         }
5938         *out = Dict(keys, values, lineno, col_offset, arena);
5939         if (*out == NULL) goto failed;
5940         return 0;
5941     }
5942     isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5943     if (isinstance == -1) {
5944         return 1;
5945     }
5946     if (isinstance) {
5947         asdl_seq* elts;
5948 
5949         if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5950             int res;
5951             Py_ssize_t len;
5952             Py_ssize_t i;
5953             tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5954             if (tmp == NULL) goto failed;
5955             if (!PyList_Check(tmp)) {
5956                 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5957                 goto failed;
5958             }
5959             len = PyList_GET_SIZE(tmp);
5960             elts = _Py_asdl_seq_new(len, arena);
5961             if (elts == NULL) goto failed;
5962             for (i = 0; i < len; i++) {
5963                 expr_ty value;
5964                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5965                 if (res != 0) goto failed;
5966                 if (len != PyList_GET_SIZE(tmp)) {
5967                     PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
5968                     goto failed;
5969                 }
5970                 asdl_seq_SET(elts, i, value);
5971             }
5972             Py_CLEAR(tmp);
5973         } else {
5974             PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5975             return 1;
5976         }
5977         *out = Set(elts, lineno, col_offset, arena);
5978         if (*out == NULL) goto failed;
5979         return 0;
5980     }
5981     isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5982     if (isinstance == -1) {
5983         return 1;
5984     }
5985     if (isinstance) {
5986         expr_ty elt;
5987         asdl_seq* generators;
5988 
5989         if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5990             int res;
5991             tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5992             if (tmp == NULL) goto failed;
5993             res = obj2ast_expr(tmp, &elt, arena);
5994             if (res != 0) goto failed;
5995             Py_CLEAR(tmp);
5996         } else {
5997             PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5998             return 1;
5999         }
6000         if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6001             int res;
6002             Py_ssize_t len;
6003             Py_ssize_t i;
6004             tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6005             if (tmp == NULL) goto failed;
6006             if (!PyList_Check(tmp)) {
6007                 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6008                 goto failed;
6009             }
6010             len = PyList_GET_SIZE(tmp);
6011             generators = _Py_asdl_seq_new(len, arena);
6012             if (generators == NULL) goto failed;
6013             for (i = 0; i < len; i++) {
6014                 comprehension_ty value;
6015                 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6016                 if (res != 0) goto failed;
6017                 if (len != PyList_GET_SIZE(tmp)) {
6018                     PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6019                     goto failed;
6020                 }
6021                 asdl_seq_SET(generators, i, value);
6022             }
6023             Py_CLEAR(tmp);
6024         } else {
6025             PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
6026             return 1;
6027         }
6028         *out = ListComp(elt, generators, lineno, col_offset, arena);
6029         if (*out == NULL) goto failed;
6030         return 0;
6031     }
6032     isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6033     if (isinstance == -1) {
6034         return 1;
6035     }
6036     if (isinstance) {
6037         expr_ty elt;
6038         asdl_seq* generators;
6039 
6040         if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6041             int res;
6042             tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6043             if (tmp == NULL) goto failed;
6044             res = obj2ast_expr(tmp, &elt, arena);
6045             if (res != 0) goto failed;
6046             Py_CLEAR(tmp);
6047         } else {
6048             PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6049             return 1;
6050         }
6051         if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6052             int res;
6053             Py_ssize_t len;
6054             Py_ssize_t i;
6055             tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6056             if (tmp == NULL) goto failed;
6057             if (!PyList_Check(tmp)) {
6058                 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6059                 goto failed;
6060             }
6061             len = PyList_GET_SIZE(tmp);
6062             generators = _Py_asdl_seq_new(len, arena);
6063             if (generators == NULL) goto failed;
6064             for (i = 0; i < len; i++) {
6065                 comprehension_ty value;
6066                 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6067                 if (res != 0) goto failed;
6068                 if (len != PyList_GET_SIZE(tmp)) {
6069                     PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6070                     goto failed;
6071                 }
6072                 asdl_seq_SET(generators, i, value);
6073             }
6074             Py_CLEAR(tmp);
6075         } else {
6076             PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6077             return 1;
6078         }
6079         *out = SetComp(elt, generators, lineno, col_offset, arena);
6080         if (*out == NULL) goto failed;
6081         return 0;
6082     }
6083     isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6084     if (isinstance == -1) {
6085         return 1;
6086     }
6087     if (isinstance) {
6088         expr_ty key;
6089         expr_ty value;
6090         asdl_seq* generators;
6091 
6092         if (_PyObject_HasAttrId(obj, &PyId_key)) {
6093             int res;
6094             tmp = _PyObject_GetAttrId(obj, &PyId_key);
6095             if (tmp == NULL) goto failed;
6096             res = obj2ast_expr(tmp, &key, arena);
6097             if (res != 0) goto failed;
6098             Py_CLEAR(tmp);
6099         } else {
6100             PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6101             return 1;
6102         }
6103         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6104             int res;
6105             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6106             if (tmp == NULL) goto failed;
6107             res = obj2ast_expr(tmp, &value, arena);
6108             if (res != 0) goto failed;
6109             Py_CLEAR(tmp);
6110         } else {
6111             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6112             return 1;
6113         }
6114         if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6115             int res;
6116             Py_ssize_t len;
6117             Py_ssize_t i;
6118             tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6119             if (tmp == NULL) goto failed;
6120             if (!PyList_Check(tmp)) {
6121                 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6122                 goto failed;
6123             }
6124             len = PyList_GET_SIZE(tmp);
6125             generators = _Py_asdl_seq_new(len, arena);
6126             if (generators == NULL) goto failed;
6127             for (i = 0; i < len; i++) {
6128                 comprehension_ty value;
6129                 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6130                 if (res != 0) goto failed;
6131                 if (len != PyList_GET_SIZE(tmp)) {
6132                     PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6133                     goto failed;
6134                 }
6135                 asdl_seq_SET(generators, i, value);
6136             }
6137             Py_CLEAR(tmp);
6138         } else {
6139             PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6140             return 1;
6141         }
6142         *out = DictComp(key, value, generators, lineno, col_offset, arena);
6143         if (*out == NULL) goto failed;
6144         return 0;
6145     }
6146     isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6147     if (isinstance == -1) {
6148         return 1;
6149     }
6150     if (isinstance) {
6151         expr_ty elt;
6152         asdl_seq* generators;
6153 
6154         if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6155             int res;
6156             tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6157             if (tmp == NULL) goto failed;
6158             res = obj2ast_expr(tmp, &elt, arena);
6159             if (res != 0) goto failed;
6160             Py_CLEAR(tmp);
6161         } else {
6162             PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6163             return 1;
6164         }
6165         if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6166             int res;
6167             Py_ssize_t len;
6168             Py_ssize_t i;
6169             tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6170             if (tmp == NULL) goto failed;
6171             if (!PyList_Check(tmp)) {
6172                 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6173                 goto failed;
6174             }
6175             len = PyList_GET_SIZE(tmp);
6176             generators = _Py_asdl_seq_new(len, arena);
6177             if (generators == NULL) goto failed;
6178             for (i = 0; i < len; i++) {
6179                 comprehension_ty value;
6180                 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6181                 if (res != 0) goto failed;
6182                 if (len != PyList_GET_SIZE(tmp)) {
6183                     PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
6184                     goto failed;
6185                 }
6186                 asdl_seq_SET(generators, i, value);
6187             }
6188             Py_CLEAR(tmp);
6189         } else {
6190             PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6191             return 1;
6192         }
6193         *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6194         if (*out == NULL) goto failed;
6195         return 0;
6196     }
6197     isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6198     if (isinstance == -1) {
6199         return 1;
6200     }
6201     if (isinstance) {
6202         expr_ty value;
6203 
6204         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6205             int res;
6206             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6207             if (tmp == NULL) goto failed;
6208             res = obj2ast_expr(tmp, &value, arena);
6209             if (res != 0) goto failed;
6210             Py_CLEAR(tmp);
6211         } else {
6212             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6213             return 1;
6214         }
6215         *out = Await(value, lineno, col_offset, arena);
6216         if (*out == NULL) goto failed;
6217         return 0;
6218     }
6219     isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6220     if (isinstance == -1) {
6221         return 1;
6222     }
6223     if (isinstance) {
6224         expr_ty value;
6225 
6226         if (exists_not_none(obj, &PyId_value)) {
6227             int res;
6228             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6229             if (tmp == NULL) goto failed;
6230             res = obj2ast_expr(tmp, &value, arena);
6231             if (res != 0) goto failed;
6232             Py_CLEAR(tmp);
6233         } else {
6234             value = NULL;
6235         }
6236         *out = Yield(value, lineno, col_offset, arena);
6237         if (*out == NULL) goto failed;
6238         return 0;
6239     }
6240     isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6241     if (isinstance == -1) {
6242         return 1;
6243     }
6244     if (isinstance) {
6245         expr_ty value;
6246 
6247         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6248             int res;
6249             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6250             if (tmp == NULL) goto failed;
6251             res = obj2ast_expr(tmp, &value, arena);
6252             if (res != 0) goto failed;
6253             Py_CLEAR(tmp);
6254         } else {
6255             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6256             return 1;
6257         }
6258         *out = YieldFrom(value, lineno, col_offset, arena);
6259         if (*out == NULL) goto failed;
6260         return 0;
6261     }
6262     isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6263     if (isinstance == -1) {
6264         return 1;
6265     }
6266     if (isinstance) {
6267         expr_ty left;
6268         asdl_int_seq* ops;
6269         asdl_seq* comparators;
6270 
6271         if (_PyObject_HasAttrId(obj, &PyId_left)) {
6272             int res;
6273             tmp = _PyObject_GetAttrId(obj, &PyId_left);
6274             if (tmp == NULL) goto failed;
6275             res = obj2ast_expr(tmp, &left, arena);
6276             if (res != 0) goto failed;
6277             Py_CLEAR(tmp);
6278         } else {
6279             PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6280             return 1;
6281         }
6282         if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6283             int res;
6284             Py_ssize_t len;
6285             Py_ssize_t i;
6286             tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6287             if (tmp == NULL) goto failed;
6288             if (!PyList_Check(tmp)) {
6289                 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6290                 goto failed;
6291             }
6292             len = PyList_GET_SIZE(tmp);
6293             ops = _Py_asdl_int_seq_new(len, arena);
6294             if (ops == NULL) goto failed;
6295             for (i = 0; i < len; i++) {
6296                 cmpop_ty value;
6297                 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6298                 if (res != 0) goto failed;
6299                 if (len != PyList_GET_SIZE(tmp)) {
6300                     PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
6301                     goto failed;
6302                 }
6303                 asdl_seq_SET(ops, i, value);
6304             }
6305             Py_CLEAR(tmp);
6306         } else {
6307             PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6308             return 1;
6309         }
6310         if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6311             int res;
6312             Py_ssize_t len;
6313             Py_ssize_t i;
6314             tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6315             if (tmp == NULL) goto failed;
6316             if (!PyList_Check(tmp)) {
6317                 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6318                 goto failed;
6319             }
6320             len = PyList_GET_SIZE(tmp);
6321             comparators = _Py_asdl_seq_new(len, arena);
6322             if (comparators == NULL) goto failed;
6323             for (i = 0; i < len; i++) {
6324                 expr_ty value;
6325                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6326                 if (res != 0) goto failed;
6327                 if (len != PyList_GET_SIZE(tmp)) {
6328                     PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
6329                     goto failed;
6330                 }
6331                 asdl_seq_SET(comparators, i, value);
6332             }
6333             Py_CLEAR(tmp);
6334         } else {
6335             PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6336             return 1;
6337         }
6338         *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6339         if (*out == NULL) goto failed;
6340         return 0;
6341     }
6342     isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6343     if (isinstance == -1) {
6344         return 1;
6345     }
6346     if (isinstance) {
6347         expr_ty func;
6348         asdl_seq* args;
6349         asdl_seq* keywords;
6350 
6351         if (_PyObject_HasAttrId(obj, &PyId_func)) {
6352             int res;
6353             tmp = _PyObject_GetAttrId(obj, &PyId_func);
6354             if (tmp == NULL) goto failed;
6355             res = obj2ast_expr(tmp, &func, arena);
6356             if (res != 0) goto failed;
6357             Py_CLEAR(tmp);
6358         } else {
6359             PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6360             return 1;
6361         }
6362         if (_PyObject_HasAttrId(obj, &PyId_args)) {
6363             int res;
6364             Py_ssize_t len;
6365             Py_ssize_t i;
6366             tmp = _PyObject_GetAttrId(obj, &PyId_args);
6367             if (tmp == NULL) goto failed;
6368             if (!PyList_Check(tmp)) {
6369                 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6370                 goto failed;
6371             }
6372             len = PyList_GET_SIZE(tmp);
6373             args = _Py_asdl_seq_new(len, arena);
6374             if (args == NULL) goto failed;
6375             for (i = 0; i < len; i++) {
6376                 expr_ty value;
6377                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6378                 if (res != 0) goto failed;
6379                 if (len != PyList_GET_SIZE(tmp)) {
6380                     PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
6381                     goto failed;
6382                 }
6383                 asdl_seq_SET(args, i, value);
6384             }
6385             Py_CLEAR(tmp);
6386         } else {
6387             PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6388             return 1;
6389         }
6390         if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6391             int res;
6392             Py_ssize_t len;
6393             Py_ssize_t i;
6394             tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6395             if (tmp == NULL) goto failed;
6396             if (!PyList_Check(tmp)) {
6397                 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6398                 goto failed;
6399             }
6400             len = PyList_GET_SIZE(tmp);
6401             keywords = _Py_asdl_seq_new(len, arena);
6402             if (keywords == NULL) goto failed;
6403             for (i = 0; i < len; i++) {
6404                 keyword_ty value;
6405                 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6406                 if (res != 0) goto failed;
6407                 if (len != PyList_GET_SIZE(tmp)) {
6408                     PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
6409                     goto failed;
6410                 }
6411                 asdl_seq_SET(keywords, i, value);
6412             }
6413             Py_CLEAR(tmp);
6414         } else {
6415             PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6416             return 1;
6417         }
6418         *out = Call(func, args, keywords, lineno, col_offset, arena);
6419         if (*out == NULL) goto failed;
6420         return 0;
6421     }
6422     isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6423     if (isinstance == -1) {
6424         return 1;
6425     }
6426     if (isinstance) {
6427         object n;
6428 
6429         if (_PyObject_HasAttrId(obj, &PyId_n)) {
6430             int res;
6431             tmp = _PyObject_GetAttrId(obj, &PyId_n);
6432             if (tmp == NULL) goto failed;
6433             res = obj2ast_object(tmp, &n, arena);
6434             if (res != 0) goto failed;
6435             Py_CLEAR(tmp);
6436         } else {
6437             PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6438             return 1;
6439         }
6440         *out = Num(n, lineno, col_offset, arena);
6441         if (*out == NULL) goto failed;
6442         return 0;
6443     }
6444     isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6445     if (isinstance == -1) {
6446         return 1;
6447     }
6448     if (isinstance) {
6449         string s;
6450 
6451         if (_PyObject_HasAttrId(obj, &PyId_s)) {
6452             int res;
6453             tmp = _PyObject_GetAttrId(obj, &PyId_s);
6454             if (tmp == NULL) goto failed;
6455             res = obj2ast_string(tmp, &s, arena);
6456             if (res != 0) goto failed;
6457             Py_CLEAR(tmp);
6458         } else {
6459             PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6460             return 1;
6461         }
6462         *out = Str(s, lineno, col_offset, arena);
6463         if (*out == NULL) goto failed;
6464         return 0;
6465     }
6466     isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6467     if (isinstance == -1) {
6468         return 1;
6469     }
6470     if (isinstance) {
6471         expr_ty value;
6472         int conversion;
6473         expr_ty format_spec;
6474 
6475         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6476             int res;
6477             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6478             if (tmp == NULL) goto failed;
6479             res = obj2ast_expr(tmp, &value, arena);
6480             if (res != 0) goto failed;
6481             Py_CLEAR(tmp);
6482         } else {
6483             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6484             return 1;
6485         }
6486         if (exists_not_none(obj, &PyId_conversion)) {
6487             int res;
6488             tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6489             if (tmp == NULL) goto failed;
6490             res = obj2ast_int(tmp, &conversion, arena);
6491             if (res != 0) goto failed;
6492             Py_CLEAR(tmp);
6493         } else {
6494             conversion = 0;
6495         }
6496         if (exists_not_none(obj, &PyId_format_spec)) {
6497             int res;
6498             tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6499             if (tmp == NULL) goto failed;
6500             res = obj2ast_expr(tmp, &format_spec, arena);
6501             if (res != 0) goto failed;
6502             Py_CLEAR(tmp);
6503         } else {
6504             format_spec = NULL;
6505         }
6506         *out = FormattedValue(value, conversion, format_spec, lineno,
6507                               col_offset, arena);
6508         if (*out == NULL) goto failed;
6509         return 0;
6510     }
6511     isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6512     if (isinstance == -1) {
6513         return 1;
6514     }
6515     if (isinstance) {
6516         asdl_seq* values;
6517 
6518         if (_PyObject_HasAttrId(obj, &PyId_values)) {
6519             int res;
6520             Py_ssize_t len;
6521             Py_ssize_t i;
6522             tmp = _PyObject_GetAttrId(obj, &PyId_values);
6523             if (tmp == NULL) goto failed;
6524             if (!PyList_Check(tmp)) {
6525                 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6526                 goto failed;
6527             }
6528             len = PyList_GET_SIZE(tmp);
6529             values = _Py_asdl_seq_new(len, arena);
6530             if (values == NULL) goto failed;
6531             for (i = 0; i < len; i++) {
6532                 expr_ty value;
6533                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6534                 if (res != 0) goto failed;
6535                 if (len != PyList_GET_SIZE(tmp)) {
6536                     PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
6537                     goto failed;
6538                 }
6539                 asdl_seq_SET(values, i, value);
6540             }
6541             Py_CLEAR(tmp);
6542         } else {
6543             PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6544             return 1;
6545         }
6546         *out = JoinedStr(values, lineno, col_offset, arena);
6547         if (*out == NULL) goto failed;
6548         return 0;
6549     }
6550     isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6551     if (isinstance == -1) {
6552         return 1;
6553     }
6554     if (isinstance) {
6555         bytes s;
6556 
6557         if (_PyObject_HasAttrId(obj, &PyId_s)) {
6558             int res;
6559             tmp = _PyObject_GetAttrId(obj, &PyId_s);
6560             if (tmp == NULL) goto failed;
6561             res = obj2ast_bytes(tmp, &s, arena);
6562             if (res != 0) goto failed;
6563             Py_CLEAR(tmp);
6564         } else {
6565             PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6566             return 1;
6567         }
6568         *out = Bytes(s, lineno, col_offset, arena);
6569         if (*out == NULL) goto failed;
6570         return 0;
6571     }
6572     isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6573     if (isinstance == -1) {
6574         return 1;
6575     }
6576     if (isinstance) {
6577         singleton value;
6578 
6579         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6580             int res;
6581             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6582             if (tmp == NULL) goto failed;
6583             res = obj2ast_singleton(tmp, &value, arena);
6584             if (res != 0) goto failed;
6585             Py_CLEAR(tmp);
6586         } else {
6587             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6588             return 1;
6589         }
6590         *out = NameConstant(value, lineno, col_offset, arena);
6591         if (*out == NULL) goto failed;
6592         return 0;
6593     }
6594     isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6595     if (isinstance == -1) {
6596         return 1;
6597     }
6598     if (isinstance) {
6599 
6600         *out = Ellipsis(lineno, col_offset, arena);
6601         if (*out == NULL) goto failed;
6602         return 0;
6603     }
6604     isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6605     if (isinstance == -1) {
6606         return 1;
6607     }
6608     if (isinstance) {
6609         constant value;
6610 
6611         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6612             int res;
6613             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6614             if (tmp == NULL) goto failed;
6615             res = obj2ast_constant(tmp, &value, arena);
6616             if (res != 0) goto failed;
6617             Py_CLEAR(tmp);
6618         } else {
6619             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6620             return 1;
6621         }
6622         *out = Constant(value, lineno, col_offset, arena);
6623         if (*out == NULL) goto failed;
6624         return 0;
6625     }
6626     isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6627     if (isinstance == -1) {
6628         return 1;
6629     }
6630     if (isinstance) {
6631         expr_ty value;
6632         identifier attr;
6633         expr_context_ty ctx;
6634 
6635         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6636             int res;
6637             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6638             if (tmp == NULL) goto failed;
6639             res = obj2ast_expr(tmp, &value, arena);
6640             if (res != 0) goto failed;
6641             Py_CLEAR(tmp);
6642         } else {
6643             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6644             return 1;
6645         }
6646         if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6647             int res;
6648             tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6649             if (tmp == NULL) goto failed;
6650             res = obj2ast_identifier(tmp, &attr, arena);
6651             if (res != 0) goto failed;
6652             Py_CLEAR(tmp);
6653         } else {
6654             PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6655             return 1;
6656         }
6657         if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6658             int res;
6659             tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6660             if (tmp == NULL) goto failed;
6661             res = obj2ast_expr_context(tmp, &ctx, arena);
6662             if (res != 0) goto failed;
6663             Py_CLEAR(tmp);
6664         } else {
6665             PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6666             return 1;
6667         }
6668         *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6669         if (*out == NULL) goto failed;
6670         return 0;
6671     }
6672     isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6673     if (isinstance == -1) {
6674         return 1;
6675     }
6676     if (isinstance) {
6677         expr_ty value;
6678         slice_ty slice;
6679         expr_context_ty ctx;
6680 
6681         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6682             int res;
6683             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6684             if (tmp == NULL) goto failed;
6685             res = obj2ast_expr(tmp, &value, arena);
6686             if (res != 0) goto failed;
6687             Py_CLEAR(tmp);
6688         } else {
6689             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6690             return 1;
6691         }
6692         if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6693             int res;
6694             tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6695             if (tmp == NULL) goto failed;
6696             res = obj2ast_slice(tmp, &slice, arena);
6697             if (res != 0) goto failed;
6698             Py_CLEAR(tmp);
6699         } else {
6700             PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6701             return 1;
6702         }
6703         if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6704             int res;
6705             tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6706             if (tmp == NULL) goto failed;
6707             res = obj2ast_expr_context(tmp, &ctx, arena);
6708             if (res != 0) goto failed;
6709             Py_CLEAR(tmp);
6710         } else {
6711             PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6712             return 1;
6713         }
6714         *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6715         if (*out == NULL) goto failed;
6716         return 0;
6717     }
6718     isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6719     if (isinstance == -1) {
6720         return 1;
6721     }
6722     if (isinstance) {
6723         expr_ty value;
6724         expr_context_ty ctx;
6725 
6726         if (_PyObject_HasAttrId(obj, &PyId_value)) {
6727             int res;
6728             tmp = _PyObject_GetAttrId(obj, &PyId_value);
6729             if (tmp == NULL) goto failed;
6730             res = obj2ast_expr(tmp, &value, arena);
6731             if (res != 0) goto failed;
6732             Py_CLEAR(tmp);
6733         } else {
6734             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6735             return 1;
6736         }
6737         if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6738             int res;
6739             tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6740             if (tmp == NULL) goto failed;
6741             res = obj2ast_expr_context(tmp, &ctx, arena);
6742             if (res != 0) goto failed;
6743             Py_CLEAR(tmp);
6744         } else {
6745             PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6746             return 1;
6747         }
6748         *out = Starred(value, ctx, lineno, col_offset, arena);
6749         if (*out == NULL) goto failed;
6750         return 0;
6751     }
6752     isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6753     if (isinstance == -1) {
6754         return 1;
6755     }
6756     if (isinstance) {
6757         identifier id;
6758         expr_context_ty ctx;
6759 
6760         if (_PyObject_HasAttrId(obj, &PyId_id)) {
6761             int res;
6762             tmp = _PyObject_GetAttrId(obj, &PyId_id);
6763             if (tmp == NULL) goto failed;
6764             res = obj2ast_identifier(tmp, &id, arena);
6765             if (res != 0) goto failed;
6766             Py_CLEAR(tmp);
6767         } else {
6768             PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6769             return 1;
6770         }
6771         if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6772             int res;
6773             tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6774             if (tmp == NULL) goto failed;
6775             res = obj2ast_expr_context(tmp, &ctx, arena);
6776             if (res != 0) goto failed;
6777             Py_CLEAR(tmp);
6778         } else {
6779             PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6780             return 1;
6781         }
6782         *out = Name(id, ctx, lineno, col_offset, arena);
6783         if (*out == NULL) goto failed;
6784         return 0;
6785     }
6786     isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6787     if (isinstance == -1) {
6788         return 1;
6789     }
6790     if (isinstance) {
6791         asdl_seq* elts;
6792         expr_context_ty ctx;
6793 
6794         if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6795             int res;
6796             Py_ssize_t len;
6797             Py_ssize_t i;
6798             tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6799             if (tmp == NULL) goto failed;
6800             if (!PyList_Check(tmp)) {
6801                 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6802                 goto failed;
6803             }
6804             len = PyList_GET_SIZE(tmp);
6805             elts = _Py_asdl_seq_new(len, arena);
6806             if (elts == NULL) goto failed;
6807             for (i = 0; i < len; i++) {
6808                 expr_ty value;
6809                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6810                 if (res != 0) goto failed;
6811                 if (len != PyList_GET_SIZE(tmp)) {
6812                     PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
6813                     goto failed;
6814                 }
6815                 asdl_seq_SET(elts, i, value);
6816             }
6817             Py_CLEAR(tmp);
6818         } else {
6819             PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6820             return 1;
6821         }
6822         if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6823             int res;
6824             tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6825             if (tmp == NULL) goto failed;
6826             res = obj2ast_expr_context(tmp, &ctx, arena);
6827             if (res != 0) goto failed;
6828             Py_CLEAR(tmp);
6829         } else {
6830             PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6831             return 1;
6832         }
6833         *out = List(elts, ctx, lineno, col_offset, arena);
6834         if (*out == NULL) goto failed;
6835         return 0;
6836     }
6837     isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6838     if (isinstance == -1) {
6839         return 1;
6840     }
6841     if (isinstance) {
6842         asdl_seq* elts;
6843         expr_context_ty ctx;
6844 
6845         if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6846             int res;
6847             Py_ssize_t len;
6848             Py_ssize_t i;
6849             tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6850             if (tmp == NULL) goto failed;
6851             if (!PyList_Check(tmp)) {
6852                 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6853                 goto failed;
6854             }
6855             len = PyList_GET_SIZE(tmp);
6856             elts = _Py_asdl_seq_new(len, arena);
6857             if (elts == NULL) goto failed;
6858             for (i = 0; i < len; i++) {
6859                 expr_ty value;
6860                 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6861                 if (res != 0) goto failed;
6862                 if (len != PyList_GET_SIZE(tmp)) {
6863                     PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
6864                     goto failed;
6865                 }
6866                 asdl_seq_SET(elts, i, value);
6867             }
6868             Py_CLEAR(tmp);
6869         } else {
6870             PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6871             return 1;
6872         }
6873         if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6874             int res;
6875             tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6876             if (tmp == NULL) goto failed;
6877             res = obj2ast_expr_context(tmp, &ctx, arena);
6878             if (res != 0) goto failed;
6879             Py_CLEAR(tmp);
6880         } else {
6881             PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6882             return 1;
6883         }
6884         *out = Tuple(elts, ctx, lineno, col_offset, arena);
6885         if (*out == NULL) goto failed;
6886         return 0;
6887     }
6888 
6889     PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6890     failed:
6891     Py_XDECREF(tmp);
6892     return 1;
6893 }
6894 
6895 int
obj2ast_expr_context(PyObject * obj,expr_context_ty * out,PyArena * arena)6896 obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6897 {
6898     int isinstance;
6899 
6900     isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6901     if (isinstance == -1) {
6902         return 1;
6903     }
6904     if (isinstance) {
6905         *out = Load;
6906         return 0;
6907     }
6908     isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6909     if (isinstance == -1) {
6910         return 1;
6911     }
6912     if (isinstance) {
6913         *out = Store;
6914         return 0;
6915     }
6916     isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6917     if (isinstance == -1) {
6918         return 1;
6919     }
6920     if (isinstance) {
6921         *out = Del;
6922         return 0;
6923     }
6924     isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6925     if (isinstance == -1) {
6926         return 1;
6927     }
6928     if (isinstance) {
6929         *out = AugLoad;
6930         return 0;
6931     }
6932     isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6933     if (isinstance == -1) {
6934         return 1;
6935     }
6936     if (isinstance) {
6937         *out = AugStore;
6938         return 0;
6939     }
6940     isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6941     if (isinstance == -1) {
6942         return 1;
6943     }
6944     if (isinstance) {
6945         *out = Param;
6946         return 0;
6947     }
6948 
6949     PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6950     return 1;
6951 }
6952 
6953 int
obj2ast_slice(PyObject * obj,slice_ty * out,PyArena * arena)6954 obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6955 {
6956     int isinstance;
6957 
6958     PyObject *tmp = NULL;
6959 
6960     if (obj == Py_None) {
6961         *out = NULL;
6962         return 0;
6963     }
6964     isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6965     if (isinstance == -1) {
6966         return 1;
6967     }
6968     if (isinstance) {
6969         expr_ty lower;
6970         expr_ty upper;
6971         expr_ty step;
6972 
6973         if (exists_not_none(obj, &PyId_lower)) {
6974             int res;
6975             tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6976             if (tmp == NULL) goto failed;
6977             res = obj2ast_expr(tmp, &lower, arena);
6978             if (res != 0) goto failed;
6979             Py_CLEAR(tmp);
6980         } else {
6981             lower = NULL;
6982         }
6983         if (exists_not_none(obj, &PyId_upper)) {
6984             int res;
6985             tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6986             if (tmp == NULL) goto failed;
6987             res = obj2ast_expr(tmp, &upper, arena);
6988             if (res != 0) goto failed;
6989             Py_CLEAR(tmp);
6990         } else {
6991             upper = NULL;
6992         }
6993         if (exists_not_none(obj, &PyId_step)) {
6994             int res;
6995             tmp = _PyObject_GetAttrId(obj, &PyId_step);
6996             if (tmp == NULL) goto failed;
6997             res = obj2ast_expr(tmp, &step, arena);
6998             if (res != 0) goto failed;
6999             Py_CLEAR(tmp);
7000         } else {
7001             step = NULL;
7002         }
7003         *out = Slice(lower, upper, step, arena);
7004         if (*out == NULL) goto failed;
7005         return 0;
7006     }
7007     isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7008     if (isinstance == -1) {
7009         return 1;
7010     }
7011     if (isinstance) {
7012         asdl_seq* dims;
7013 
7014         if (_PyObject_HasAttrId(obj, &PyId_dims)) {
7015             int res;
7016             Py_ssize_t len;
7017             Py_ssize_t i;
7018             tmp = _PyObject_GetAttrId(obj, &PyId_dims);
7019             if (tmp == NULL) goto failed;
7020             if (!PyList_Check(tmp)) {
7021                 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7022                 goto failed;
7023             }
7024             len = PyList_GET_SIZE(tmp);
7025             dims = _Py_asdl_seq_new(len, arena);
7026             if (dims == NULL) goto failed;
7027             for (i = 0; i < len; i++) {
7028                 slice_ty value;
7029                 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
7030                 if (res != 0) goto failed;
7031                 if (len != PyList_GET_SIZE(tmp)) {
7032                     PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7033                     goto failed;
7034                 }
7035                 asdl_seq_SET(dims, i, value);
7036             }
7037             Py_CLEAR(tmp);
7038         } else {
7039             PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7040             return 1;
7041         }
7042         *out = ExtSlice(dims, arena);
7043         if (*out == NULL) goto failed;
7044         return 0;
7045     }
7046     isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7047     if (isinstance == -1) {
7048         return 1;
7049     }
7050     if (isinstance) {
7051         expr_ty value;
7052 
7053         if (_PyObject_HasAttrId(obj, &PyId_value)) {
7054             int res;
7055             tmp = _PyObject_GetAttrId(obj, &PyId_value);
7056             if (tmp == NULL) goto failed;
7057             res = obj2ast_expr(tmp, &value, arena);
7058             if (res != 0) goto failed;
7059             Py_CLEAR(tmp);
7060         } else {
7061             PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7062             return 1;
7063         }
7064         *out = Index(value, arena);
7065         if (*out == NULL) goto failed;
7066         return 0;
7067     }
7068 
7069     PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7070     failed:
7071     Py_XDECREF(tmp);
7072     return 1;
7073 }
7074 
7075 int
obj2ast_boolop(PyObject * obj,boolop_ty * out,PyArena * arena)7076 obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
7077 {
7078     int isinstance;
7079 
7080     isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
7081     if (isinstance == -1) {
7082         return 1;
7083     }
7084     if (isinstance) {
7085         *out = And;
7086         return 0;
7087     }
7088     isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7089     if (isinstance == -1) {
7090         return 1;
7091     }
7092     if (isinstance) {
7093         *out = Or;
7094         return 0;
7095     }
7096 
7097     PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7098     return 1;
7099 }
7100 
7101 int
obj2ast_operator(PyObject * obj,operator_ty * out,PyArena * arena)7102 obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7103 {
7104     int isinstance;
7105 
7106     isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7107     if (isinstance == -1) {
7108         return 1;
7109     }
7110     if (isinstance) {
7111         *out = Add;
7112         return 0;
7113     }
7114     isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7115     if (isinstance == -1) {
7116         return 1;
7117     }
7118     if (isinstance) {
7119         *out = Sub;
7120         return 0;
7121     }
7122     isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7123     if (isinstance == -1) {
7124         return 1;
7125     }
7126     if (isinstance) {
7127         *out = Mult;
7128         return 0;
7129     }
7130     isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7131     if (isinstance == -1) {
7132         return 1;
7133     }
7134     if (isinstance) {
7135         *out = MatMult;
7136         return 0;
7137     }
7138     isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
7139     if (isinstance == -1) {
7140         return 1;
7141     }
7142     if (isinstance) {
7143         *out = Div;
7144         return 0;
7145     }
7146     isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
7147     if (isinstance == -1) {
7148         return 1;
7149     }
7150     if (isinstance) {
7151         *out = Mod;
7152         return 0;
7153     }
7154     isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
7155     if (isinstance == -1) {
7156         return 1;
7157     }
7158     if (isinstance) {
7159         *out = Pow;
7160         return 0;
7161     }
7162     isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
7163     if (isinstance == -1) {
7164         return 1;
7165     }
7166     if (isinstance) {
7167         *out = LShift;
7168         return 0;
7169     }
7170     isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
7171     if (isinstance == -1) {
7172         return 1;
7173     }
7174     if (isinstance) {
7175         *out = RShift;
7176         return 0;
7177     }
7178     isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
7179     if (isinstance == -1) {
7180         return 1;
7181     }
7182     if (isinstance) {
7183         *out = BitOr;
7184         return 0;
7185     }
7186     isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
7187     if (isinstance == -1) {
7188         return 1;
7189     }
7190     if (isinstance) {
7191         *out = BitXor;
7192         return 0;
7193     }
7194     isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
7195     if (isinstance == -1) {
7196         return 1;
7197     }
7198     if (isinstance) {
7199         *out = BitAnd;
7200         return 0;
7201     }
7202     isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7203     if (isinstance == -1) {
7204         return 1;
7205     }
7206     if (isinstance) {
7207         *out = FloorDiv;
7208         return 0;
7209     }
7210 
7211     PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7212     return 1;
7213 }
7214 
7215 int
obj2ast_unaryop(PyObject * obj,unaryop_ty * out,PyArena * arena)7216 obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7217 {
7218     int isinstance;
7219 
7220     isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7221     if (isinstance == -1) {
7222         return 1;
7223     }
7224     if (isinstance) {
7225         *out = Invert;
7226         return 0;
7227     }
7228     isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7229     if (isinstance == -1) {
7230         return 1;
7231     }
7232     if (isinstance) {
7233         *out = Not;
7234         return 0;
7235     }
7236     isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7237     if (isinstance == -1) {
7238         return 1;
7239     }
7240     if (isinstance) {
7241         *out = UAdd;
7242         return 0;
7243     }
7244     isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7245     if (isinstance == -1) {
7246         return 1;
7247     }
7248     if (isinstance) {
7249         *out = USub;
7250         return 0;
7251     }
7252 
7253     PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7254     return 1;
7255 }
7256 
7257 int
obj2ast_cmpop(PyObject * obj,cmpop_ty * out,PyArena * arena)7258 obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7259 {
7260     int isinstance;
7261 
7262     isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7263     if (isinstance == -1) {
7264         return 1;
7265     }
7266     if (isinstance) {
7267         *out = Eq;
7268         return 0;
7269     }
7270     isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7271     if (isinstance == -1) {
7272         return 1;
7273     }
7274     if (isinstance) {
7275         *out = NotEq;
7276         return 0;
7277     }
7278     isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7279     if (isinstance == -1) {
7280         return 1;
7281     }
7282     if (isinstance) {
7283         *out = Lt;
7284         return 0;
7285     }
7286     isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7287     if (isinstance == -1) {
7288         return 1;
7289     }
7290     if (isinstance) {
7291         *out = LtE;
7292         return 0;
7293     }
7294     isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7295     if (isinstance == -1) {
7296         return 1;
7297     }
7298     if (isinstance) {
7299         *out = Gt;
7300         return 0;
7301     }
7302     isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7303     if (isinstance == -1) {
7304         return 1;
7305     }
7306     if (isinstance) {
7307         *out = GtE;
7308         return 0;
7309     }
7310     isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7311     if (isinstance == -1) {
7312         return 1;
7313     }
7314     if (isinstance) {
7315         *out = Is;
7316         return 0;
7317     }
7318     isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7319     if (isinstance == -1) {
7320         return 1;
7321     }
7322     if (isinstance) {
7323         *out = IsNot;
7324         return 0;
7325     }
7326     isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7327     if (isinstance == -1) {
7328         return 1;
7329     }
7330     if (isinstance) {
7331         *out = In;
7332         return 0;
7333     }
7334     isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7335     if (isinstance == -1) {
7336         return 1;
7337     }
7338     if (isinstance) {
7339         *out = NotIn;
7340         return 0;
7341     }
7342 
7343     PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7344     return 1;
7345 }
7346 
7347 int
obj2ast_comprehension(PyObject * obj,comprehension_ty * out,PyArena * arena)7348 obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7349 {
7350     PyObject* tmp = NULL;
7351     expr_ty target;
7352     expr_ty iter;
7353     asdl_seq* ifs;
7354     int is_async;
7355 
7356     if (_PyObject_HasAttrId(obj, &PyId_target)) {
7357         int res;
7358         tmp = _PyObject_GetAttrId(obj, &PyId_target);
7359         if (tmp == NULL) goto failed;
7360         res = obj2ast_expr(tmp, &target, arena);
7361         if (res != 0) goto failed;
7362         Py_CLEAR(tmp);
7363     } else {
7364         PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
7365         return 1;
7366     }
7367     if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7368         int res;
7369         tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7370         if (tmp == NULL) goto failed;
7371         res = obj2ast_expr(tmp, &iter, arena);
7372         if (res != 0) goto failed;
7373         Py_CLEAR(tmp);
7374     } else {
7375         PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7376         return 1;
7377     }
7378     if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7379         int res;
7380         Py_ssize_t len;
7381         Py_ssize_t i;
7382         tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7383         if (tmp == NULL) goto failed;
7384         if (!PyList_Check(tmp)) {
7385             PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7386             goto failed;
7387         }
7388         len = PyList_GET_SIZE(tmp);
7389         ifs = _Py_asdl_seq_new(len, arena);
7390         if (ifs == NULL) goto failed;
7391         for (i = 0; i < len; i++) {
7392             expr_ty value;
7393             res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7394             if (res != 0) goto failed;
7395             if (len != PyList_GET_SIZE(tmp)) {
7396                 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
7397                 goto failed;
7398             }
7399             asdl_seq_SET(ifs, i, value);
7400         }
7401         Py_CLEAR(tmp);
7402     } else {
7403         PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7404         return 1;
7405     }
7406     if (_PyObject_HasAttrId(obj, &PyId_is_async)) {
7407         int res;
7408         tmp = _PyObject_GetAttrId(obj, &PyId_is_async);
7409         if (tmp == NULL) goto failed;
7410         res = obj2ast_int(tmp, &is_async, arena);
7411         if (res != 0) goto failed;
7412         Py_CLEAR(tmp);
7413     } else {
7414         PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
7415         return 1;
7416     }
7417     *out = comprehension(target, iter, ifs, is_async, arena);
7418     return 0;
7419 failed:
7420     Py_XDECREF(tmp);
7421     return 1;
7422 }
7423 
7424 int
obj2ast_excepthandler(PyObject * obj,excepthandler_ty * out,PyArena * arena)7425 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7426 {
7427     int isinstance;
7428 
7429     PyObject *tmp = NULL;
7430     int lineno;
7431     int col_offset;
7432 
7433     if (obj == Py_None) {
7434         *out = NULL;
7435         return 0;
7436     }
7437     if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7438         int res;
7439         tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7440         if (tmp == NULL) goto failed;
7441         res = obj2ast_int(tmp, &lineno, arena);
7442         if (res != 0) goto failed;
7443         Py_CLEAR(tmp);
7444     } else {
7445         PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
7446         return 1;
7447     }
7448     if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7449         int res;
7450         tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7451         if (tmp == NULL) goto failed;
7452         res = obj2ast_int(tmp, &col_offset, arena);
7453         if (res != 0) goto failed;
7454         Py_CLEAR(tmp);
7455     } else {
7456         PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7457         return 1;
7458     }
7459     isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7460     if (isinstance == -1) {
7461         return 1;
7462     }
7463     if (isinstance) {
7464         expr_ty type;
7465         identifier name;
7466         asdl_seq* body;
7467 
7468         if (exists_not_none(obj, &PyId_type)) {
7469             int res;
7470             tmp = _PyObject_GetAttrId(obj, &PyId_type);
7471             if (tmp == NULL) goto failed;
7472             res = obj2ast_expr(tmp, &type, arena);
7473             if (res != 0) goto failed;
7474             Py_CLEAR(tmp);
7475         } else {
7476             type = NULL;
7477         }
7478         if (exists_not_none(obj, &PyId_name)) {
7479             int res;
7480             tmp = _PyObject_GetAttrId(obj, &PyId_name);
7481             if (tmp == NULL) goto failed;
7482             res = obj2ast_identifier(tmp, &name, arena);
7483             if (res != 0) goto failed;
7484             Py_CLEAR(tmp);
7485         } else {
7486             name = NULL;
7487         }
7488         if (_PyObject_HasAttrId(obj, &PyId_body)) {
7489             int res;
7490             Py_ssize_t len;
7491             Py_ssize_t i;
7492             tmp = _PyObject_GetAttrId(obj, &PyId_body);
7493             if (tmp == NULL) goto failed;
7494             if (!PyList_Check(tmp)) {
7495                 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7496                 goto failed;
7497             }
7498             len = PyList_GET_SIZE(tmp);
7499             body = _Py_asdl_seq_new(len, arena);
7500             if (body == NULL) goto failed;
7501             for (i = 0; i < len; i++) {
7502                 stmt_ty value;
7503                 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7504                 if (res != 0) goto failed;
7505                 if (len != PyList_GET_SIZE(tmp)) {
7506                     PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
7507                     goto failed;
7508                 }
7509                 asdl_seq_SET(body, i, value);
7510             }
7511             Py_CLEAR(tmp);
7512         } else {
7513             PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7514             return 1;
7515         }
7516         *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7517         if (*out == NULL) goto failed;
7518         return 0;
7519     }
7520 
7521     PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7522     failed:
7523     Py_XDECREF(tmp);
7524     return 1;
7525 }
7526 
7527 int
obj2ast_arguments(PyObject * obj,arguments_ty * out,PyArena * arena)7528 obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7529 {
7530     PyObject* tmp = NULL;
7531     asdl_seq* args;
7532     arg_ty vararg;
7533     asdl_seq* kwonlyargs;
7534     asdl_seq* kw_defaults;
7535     arg_ty kwarg;
7536     asdl_seq* defaults;
7537 
7538     if (_PyObject_HasAttrId(obj, &PyId_args)) {
7539         int res;
7540         Py_ssize_t len;
7541         Py_ssize_t i;
7542         tmp = _PyObject_GetAttrId(obj, &PyId_args);
7543         if (tmp == NULL) goto failed;
7544         if (!PyList_Check(tmp)) {
7545             PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7546             goto failed;
7547         }
7548         len = PyList_GET_SIZE(tmp);
7549         args = _Py_asdl_seq_new(len, arena);
7550         if (args == NULL) goto failed;
7551         for (i = 0; i < len; i++) {
7552             arg_ty value;
7553             res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7554             if (res != 0) goto failed;
7555             if (len != PyList_GET_SIZE(tmp)) {
7556                 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
7557                 goto failed;
7558             }
7559             asdl_seq_SET(args, i, value);
7560         }
7561         Py_CLEAR(tmp);
7562     } else {
7563         PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
7564         return 1;
7565     }
7566     if (exists_not_none(obj, &PyId_vararg)) {
7567         int res;
7568         tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7569         if (tmp == NULL) goto failed;
7570         res = obj2ast_arg(tmp, &vararg, arena);
7571         if (res != 0) goto failed;
7572         Py_CLEAR(tmp);
7573     } else {
7574         vararg = NULL;
7575     }
7576     if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7577         int res;
7578         Py_ssize_t len;
7579         Py_ssize_t i;
7580         tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7581         if (tmp == NULL) goto failed;
7582         if (!PyList_Check(tmp)) {
7583             PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7584             goto failed;
7585         }
7586         len = PyList_GET_SIZE(tmp);
7587         kwonlyargs = _Py_asdl_seq_new(len, arena);
7588         if (kwonlyargs == NULL) goto failed;
7589         for (i = 0; i < len; i++) {
7590             arg_ty value;
7591             res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7592             if (res != 0) goto failed;
7593             if (len != PyList_GET_SIZE(tmp)) {
7594                 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
7595                 goto failed;
7596             }
7597             asdl_seq_SET(kwonlyargs, i, value);
7598         }
7599         Py_CLEAR(tmp);
7600     } else {
7601         PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7602         return 1;
7603     }
7604     if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7605         int res;
7606         Py_ssize_t len;
7607         Py_ssize_t i;
7608         tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7609         if (tmp == NULL) goto failed;
7610         if (!PyList_Check(tmp)) {
7611             PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7612             goto failed;
7613         }
7614         len = PyList_GET_SIZE(tmp);
7615         kw_defaults = _Py_asdl_seq_new(len, arena);
7616         if (kw_defaults == NULL) goto failed;
7617         for (i = 0; i < len; i++) {
7618             expr_ty value;
7619             res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7620             if (res != 0) goto failed;
7621             if (len != PyList_GET_SIZE(tmp)) {
7622                 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
7623                 goto failed;
7624             }
7625             asdl_seq_SET(kw_defaults, i, value);
7626         }
7627         Py_CLEAR(tmp);
7628     } else {
7629         PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7630         return 1;
7631     }
7632     if (exists_not_none(obj, &PyId_kwarg)) {
7633         int res;
7634         tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7635         if (tmp == NULL) goto failed;
7636         res = obj2ast_arg(tmp, &kwarg, arena);
7637         if (res != 0) goto failed;
7638         Py_CLEAR(tmp);
7639     } else {
7640         kwarg = NULL;
7641     }
7642     if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7643         int res;
7644         Py_ssize_t len;
7645         Py_ssize_t i;
7646         tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7647         if (tmp == NULL) goto failed;
7648         if (!PyList_Check(tmp)) {
7649             PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7650             goto failed;
7651         }
7652         len = PyList_GET_SIZE(tmp);
7653         defaults = _Py_asdl_seq_new(len, arena);
7654         if (defaults == NULL) goto failed;
7655         for (i = 0; i < len; i++) {
7656             expr_ty value;
7657             res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7658             if (res != 0) goto failed;
7659             if (len != PyList_GET_SIZE(tmp)) {
7660                 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
7661                 goto failed;
7662             }
7663             asdl_seq_SET(defaults, i, value);
7664         }
7665         Py_CLEAR(tmp);
7666     } else {
7667         PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7668         return 1;
7669     }
7670     *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7671                      arena);
7672     return 0;
7673 failed:
7674     Py_XDECREF(tmp);
7675     return 1;
7676 }
7677 
7678 int
obj2ast_arg(PyObject * obj,arg_ty * out,PyArena * arena)7679 obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7680 {
7681     PyObject* tmp = NULL;
7682     identifier arg;
7683     expr_ty annotation;
7684     int lineno;
7685     int col_offset;
7686 
7687     if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7688         int res;
7689         tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7690         if (tmp == NULL) goto failed;
7691         res = obj2ast_identifier(tmp, &arg, arena);
7692         if (res != 0) goto failed;
7693         Py_CLEAR(tmp);
7694     } else {
7695         PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
7696         return 1;
7697     }
7698     if (exists_not_none(obj, &PyId_annotation)) {
7699         int res;
7700         tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7701         if (tmp == NULL) goto failed;
7702         res = obj2ast_expr(tmp, &annotation, arena);
7703         if (res != 0) goto failed;
7704         Py_CLEAR(tmp);
7705     } else {
7706         annotation = NULL;
7707     }
7708     if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7709         int res;
7710         tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7711         if (tmp == NULL) goto failed;
7712         res = obj2ast_int(tmp, &lineno, arena);
7713         if (res != 0) goto failed;
7714         Py_CLEAR(tmp);
7715     } else {
7716         PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7717         return 1;
7718     }
7719     if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7720         int res;
7721         tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7722         if (tmp == NULL) goto failed;
7723         res = obj2ast_int(tmp, &col_offset, arena);
7724         if (res != 0) goto failed;
7725         Py_CLEAR(tmp);
7726     } else {
7727         PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7728         return 1;
7729     }
7730     *out = arg(arg, annotation, lineno, col_offset, arena);
7731     return 0;
7732 failed:
7733     Py_XDECREF(tmp);
7734     return 1;
7735 }
7736 
7737 int
obj2ast_keyword(PyObject * obj,keyword_ty * out,PyArena * arena)7738 obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7739 {
7740     PyObject* tmp = NULL;
7741     identifier arg;
7742     expr_ty value;
7743 
7744     if (exists_not_none(obj, &PyId_arg)) {
7745         int res;
7746         tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7747         if (tmp == NULL) goto failed;
7748         res = obj2ast_identifier(tmp, &arg, arena);
7749         if (res != 0) goto failed;
7750         Py_CLEAR(tmp);
7751     } else {
7752         arg = NULL;
7753     }
7754     if (_PyObject_HasAttrId(obj, &PyId_value)) {
7755         int res;
7756         tmp = _PyObject_GetAttrId(obj, &PyId_value);
7757         if (tmp == NULL) goto failed;
7758         res = obj2ast_expr(tmp, &value, arena);
7759         if (res != 0) goto failed;
7760         Py_CLEAR(tmp);
7761     } else {
7762         PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7763         return 1;
7764     }
7765     *out = keyword(arg, value, arena);
7766     return 0;
7767 failed:
7768     Py_XDECREF(tmp);
7769     return 1;
7770 }
7771 
7772 int
obj2ast_alias(PyObject * obj,alias_ty * out,PyArena * arena)7773 obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7774 {
7775     PyObject* tmp = NULL;
7776     identifier name;
7777     identifier asname;
7778 
7779     if (_PyObject_HasAttrId(obj, &PyId_name)) {
7780         int res;
7781         tmp = _PyObject_GetAttrId(obj, &PyId_name);
7782         if (tmp == NULL) goto failed;
7783         res = obj2ast_identifier(tmp, &name, arena);
7784         if (res != 0) goto failed;
7785         Py_CLEAR(tmp);
7786     } else {
7787         PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
7788         return 1;
7789     }
7790     if (exists_not_none(obj, &PyId_asname)) {
7791         int res;
7792         tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7793         if (tmp == NULL) goto failed;
7794         res = obj2ast_identifier(tmp, &asname, arena);
7795         if (res != 0) goto failed;
7796         Py_CLEAR(tmp);
7797     } else {
7798         asname = NULL;
7799     }
7800     *out = alias(name, asname, arena);
7801     return 0;
7802 failed:
7803     Py_XDECREF(tmp);
7804     return 1;
7805 }
7806 
7807 int
obj2ast_withitem(PyObject * obj,withitem_ty * out,PyArena * arena)7808 obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7809 {
7810     PyObject* tmp = NULL;
7811     expr_ty context_expr;
7812     expr_ty optional_vars;
7813 
7814     if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7815         int res;
7816         tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7817         if (tmp == NULL) goto failed;
7818         res = obj2ast_expr(tmp, &context_expr, arena);
7819         if (res != 0) goto failed;
7820         Py_CLEAR(tmp);
7821     } else {
7822         PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
7823         return 1;
7824     }
7825     if (exists_not_none(obj, &PyId_optional_vars)) {
7826         int res;
7827         tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7828         if (tmp == NULL) goto failed;
7829         res = obj2ast_expr(tmp, &optional_vars, arena);
7830         if (res != 0) goto failed;
7831         Py_CLEAR(tmp);
7832     } else {
7833         optional_vars = NULL;
7834     }
7835     *out = withitem(context_expr, optional_vars, arena);
7836     return 0;
7837 failed:
7838     Py_XDECREF(tmp);
7839     return 1;
7840 }
7841 
7842 
7843 static struct PyModuleDef _astmodule = {
7844   PyModuleDef_HEAD_INIT, "_ast"
7845 };
7846 PyMODINIT_FUNC
PyInit__ast(void)7847 PyInit__ast(void)
7848 {
7849     PyObject *m, *d;
7850     if (!init_types()) return NULL;
7851     m = PyModule_Create(&_astmodule);
7852     if (!m) return NULL;
7853     d = PyModule_GetDict(m);
7854     if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
7855     if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
7856         return NULL;
7857     if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7858     if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7859         NULL;
7860     if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7861         0) return NULL;
7862     if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7863         return NULL;
7864     if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7865         NULL;
7866     if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7867     if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7868         0) return NULL;
7869     if (PyDict_SetItemString(d, "AsyncFunctionDef",
7870         (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
7871     if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7872         return NULL;
7873     if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7874         NULL;
7875     if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7876         NULL;
7877     if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7878         NULL;
7879     if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7880         return NULL;
7881     if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7882         return NULL;
7883     if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
7884     if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7885         return NULL;
7886     if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7887         NULL;
7888     if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7889     if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
7890     if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7891         return NULL;
7892     if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7893         NULL;
7894     if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7895     if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7896         NULL;
7897     if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7898         NULL;
7899     if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7900         return NULL;
7901     if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7902         NULL;
7903     if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7904         return NULL;
7905     if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7906     if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7907     if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7908         NULL;
7909     if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7910         return NULL;
7911     if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7912     if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7913         NULL;
7914     if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7915         NULL;
7916     if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7917         NULL;
7918     if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7919         NULL;
7920     if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7921         NULL;
7922     if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7923     if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7924     if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7925         return NULL;
7926     if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7927         NULL;
7928     if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7929         return NULL;
7930     if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7931         0) return NULL;
7932     if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7933         NULL;
7934     if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7935         NULL;
7936     if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7937         return NULL;
7938     if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7939         NULL;
7940     if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7941     if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7942     if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
7943     if (PyDict_SetItemString(d, "FormattedValue",
7944         (PyObject*)FormattedValue_type) < 0) return NULL;
7945     if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7946         return NULL;
7947     if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7948         NULL;
7949     if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7950         0) return NULL;
7951     if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7952         return NULL;
7953     if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
7954         return NULL;
7955     if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7956         return NULL;
7957     if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7958         return NULL;
7959     if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7960         NULL;
7961     if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7962     if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7963     if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7964         NULL;
7965     if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7966         0) return NULL;
7967     if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7968     if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7969         NULL;
7970     if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7971     if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7972         NULL;
7973     if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7974         return NULL;
7975     if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7976         NULL;
7977     if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7978         NULL;
7979     if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7980         NULL;
7981     if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7982         return NULL;
7983     if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7984         NULL;
7985     if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7986         NULL;
7987     if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7988     if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7989     if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7990         return NULL;
7991     if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7992     if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7993     if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
7994     if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7995         NULL;
7996     if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7997     if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7998     if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7999     if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
8000         NULL;
8001     if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
8002         NULL;
8003     if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
8004         NULL;
8005     if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
8006         NULL;
8007     if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
8008         NULL;
8009     if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
8010         return NULL;
8011     if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
8012         NULL;
8013     if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
8014         NULL;
8015     if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
8016     if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
8017     if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
8018     if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
8019         NULL;
8020     if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
8021     if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
8022         NULL;
8023     if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
8024     if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
8025     if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
8026     if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
8027     if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
8028     if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
8029         NULL;
8030     if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
8031     if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
8032         NULL;
8033     if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
8034         < 0) return NULL;
8035     if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
8036         < 0) return NULL;
8037     if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
8038         < 0) return NULL;
8039     if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
8040         return NULL;
8041     if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
8042     if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
8043         NULL;
8044     if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
8045         NULL;
8046     if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
8047         return NULL;
8048     return m;
8049 }
8050 
8051 
PyAST_mod2obj(mod_ty t)8052 PyObject* PyAST_mod2obj(mod_ty t)
8053 {
8054     if (!init_types())
8055         return NULL;
8056     return ast2obj_mod(t);
8057 }
8058 
8059 /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
PyAST_obj2mod(PyObject * ast,PyArena * arena,int mode)8060 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
8061 {
8062     mod_ty res;
8063     PyObject *req_type[3];
8064     char *req_name[] = {"Module", "Expression", "Interactive"};
8065     int isinstance;
8066 
8067     req_type[0] = (PyObject*)Module_type;
8068     req_type[1] = (PyObject*)Expression_type;
8069     req_type[2] = (PyObject*)Interactive_type;
8070 
8071     assert(0 <= mode && mode <= 2);
8072 
8073     if (!init_types())
8074         return NULL;
8075 
8076     isinstance = PyObject_IsInstance(ast, req_type[mode]);
8077     if (isinstance == -1)
8078         return NULL;
8079     if (!isinstance) {
8080         PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
8081                      req_name[mode], Py_TYPE(ast)->tp_name);
8082         return NULL;
8083     }
8084     if (obj2ast_mod(ast, &res, arena) != 0)
8085         return NULL;
8086     else
8087         return res;
8088 }
8089 
PyAST_Check(PyObject * obj)8090 int PyAST_Check(PyObject* obj)
8091 {
8092     if (!init_types())
8093         return -1;
8094     return PyObject_IsInstance(obj, (PyObject*)&AST_type);
8095 }
8096 
8097 
8098