blob: d4734187de785dcf90435b32a91b91e9c0f12420 [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Georg Brandlc52ed592008-03-30 07:01:47 +00005 __version__ 62047.
Brett Cannon0db62aa2007-02-12 03:51:02 +00006
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Georg Brandlc52ed592008-03-30 07:01:47 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000045 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000046};
Neal Norwitz53d960c2006-02-28 22:47:29 +000047static PyTypeObject *ClassDef_type;
48static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000049 "name",
50 "bases",
51 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000052 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000053};
Neal Norwitz53d960c2006-02-28 22:47:29 +000054static PyTypeObject *Return_type;
55static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000056 "value",
57};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Delete_type;
59static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "targets",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Assign_type;
63static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65 "value",
66};
Neal Norwitz53d960c2006-02-28 22:47:29 +000067static PyTypeObject *AugAssign_type;
68static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000069 "target",
70 "op",
71 "value",
72};
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static PyTypeObject *Print_type;
74static char *Print_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075 "dest",
76 "values",
77 "nl",
78};
Neal Norwitz53d960c2006-02-28 22:47:29 +000079static PyTypeObject *For_type;
80static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000081 "target",
82 "iter",
83 "body",
84 "orelse",
85};
Neal Norwitz53d960c2006-02-28 22:47:29 +000086static PyTypeObject *While_type;
87static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088 "test",
89 "body",
90 "orelse",
91};
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static PyTypeObject *If_type;
93static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000094 "test",
95 "body",
96 "orelse",
97};
Neal Norwitz53d960c2006-02-28 22:47:29 +000098static PyTypeObject *With_type;
99static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +0000100 "context_expr",
101 "optional_vars",
102 "body",
103};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000104static PyTypeObject *Raise_type;
105static char *Raise_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106 "type",
107 "inst",
108 "tback",
109};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000110static PyTypeObject *TryExcept_type;
111static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000112 "body",
113 "handlers",
114 "orelse",
115};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static PyTypeObject *TryFinally_type;
117static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000118 "body",
119 "finalbody",
120};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static PyTypeObject *Assert_type;
122static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000123 "test",
124 "msg",
125};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000126static PyTypeObject *Import_type;
127static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000128 "names",
129};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000130static PyTypeObject *ImportFrom_type;
131static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132 "module",
133 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000134 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000136static PyTypeObject *Exec_type;
137static char *Exec_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000138 "body",
139 "globals",
140 "locals",
141};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000142static PyTypeObject *Global_type;
143static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144 "names",
145};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000146static PyTypeObject *Expr_type;
147static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000148 "value",
149};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000150static PyTypeObject *Pass_type;
151static PyTypeObject *Break_type;
152static PyTypeObject *Continue_type;
153static PyTypeObject *expr_type;
154static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000155 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000156 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000157};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000158static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BoolOp_type;
160static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "op",
162 "values",
163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *BinOp_type;
165static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000166 "left",
167 "op",
168 "right",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *UnaryOp_type;
171static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "op",
173 "operand",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *Lambda_type;
176static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000177 "args",
178 "body",
179};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000180static PyTypeObject *IfExp_type;
181static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000182 "test",
183 "body",
184 "orelse",
185};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000186static PyTypeObject *Dict_type;
187static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000188 "keys",
189 "values",
190};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000191static PyTypeObject *ListComp_type;
192static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000193 "elt",
194 "generators",
195};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000196static PyTypeObject *GeneratorExp_type;
197static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000198 "elt",
199 "generators",
200};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static PyTypeObject *Yield_type;
202static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000203 "value",
204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Compare_type;
206static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207 "left",
208 "ops",
209 "comparators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *Call_type;
212static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "func",
214 "args",
215 "keywords",
216 "starargs",
217 "kwargs",
218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *Repr_type;
220static char *Repr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221 "value",
222};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000223static PyTypeObject *Num_type;
224static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000225 "n",
226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *Str_type;
228static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000229 "s",
230};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000231static PyTypeObject *Attribute_type;
232static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000233 "value",
234 "attr",
235 "ctx",
236};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000237static PyTypeObject *Subscript_type;
238static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000239 "value",
240 "slice",
241 "ctx",
242};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000243static PyTypeObject *Name_type;
244static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000245 "id",
246 "ctx",
247};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000248static PyTypeObject *List_type;
249static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250 "elts",
251 "ctx",
252};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000253static PyTypeObject *Tuple_type;
254static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000255 "elts",
256 "ctx",
257};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000258static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
260*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
261static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000262static PyTypeObject *Load_type;
263static PyTypeObject *Store_type;
264static PyTypeObject *Del_type;
265static PyTypeObject *AugLoad_type;
266static PyTypeObject *AugStore_type;
267static PyTypeObject *Param_type;
268static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000269static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000270static PyTypeObject *Ellipsis_type;
271static PyTypeObject *Slice_type;
272static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000273 "lower",
274 "upper",
275 "step",
276};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000277static PyTypeObject *ExtSlice_type;
278static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000279 "dims",
280};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000281static PyTypeObject *Index_type;
282static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000283 "value",
284};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286static PyObject *And_singleton, *Or_singleton;
287static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000288static PyTypeObject *And_type;
289static PyTypeObject *Or_type;
290static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000291static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
292*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
293*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
294*FloorDiv_singleton;
295static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Add_type;
297static PyTypeObject *Sub_type;
298static PyTypeObject *Mult_type;
299static PyTypeObject *Div_type;
300static PyTypeObject *Mod_type;
301static PyTypeObject *Pow_type;
302static PyTypeObject *LShift_type;
303static PyTypeObject *RShift_type;
304static PyTypeObject *BitOr_type;
305static PyTypeObject *BitXor_type;
306static PyTypeObject *BitAnd_type;
307static PyTypeObject *FloorDiv_type;
308static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000309static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
310*USub_singleton;
311static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000312static PyTypeObject *Invert_type;
313static PyTypeObject *Not_type;
314static PyTypeObject *UAdd_type;
315static PyTypeObject *USub_type;
316static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000317static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
318*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
319*NotIn_singleton;
320static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000321static PyTypeObject *Eq_type;
322static PyTypeObject *NotEq_type;
323static PyTypeObject *Lt_type;
324static PyTypeObject *LtE_type;
325static PyTypeObject *Gt_type;
326static PyTypeObject *GtE_type;
327static PyTypeObject *Is_type;
328static PyTypeObject *IsNot_type;
329static PyTypeObject *In_type;
330static PyTypeObject *NotIn_type;
331static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334 "target",
335 "iter",
336 "ifs",
337};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000338static PyTypeObject *excepthandler_type;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000339static char *excepthandler_attributes[] = {
340 "lineno",
341 "col_offset",
342};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000343static PyObject* ast2obj_excepthandler(void*);
Georg Brandla48f3ab2008-03-30 06:40:17 +0000344static PyTypeObject *ExceptHandler_type;
345static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346 "type",
347 "name",
348 "body",
349};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000350static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000351static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000353 "args",
354 "vararg",
355 "kwarg",
356 "defaults",
357};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000358static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000359static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000360static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361 "arg",
362 "value",
363};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000364static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367 "name",
368 "asname",
369};
370
371
Georg Brandlc52ed592008-03-30 07:01:47 +0000372static int
373ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
374{
375 Py_ssize_t i, numfields = 0;
376 int res = -1;
377 PyObject *key, *value, *fields;
378 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
379 if (!fields)
380 PyErr_Clear();
381 if (fields) {
382 numfields = PySequence_Size(fields);
383 if (numfields == -1)
384 goto cleanup;
385 }
386 res = 0; /* if no error occurs, this stays 0 to the end */
387 if (PyTuple_GET_SIZE(args) > 0) {
388 if (numfields != PyTuple_GET_SIZE(args)) {
389 PyErr_Format(PyExc_TypeError, "%.400s constructor takes either 0 or "
390 "%d positional argument%s", Py_TYPE(self)->tp_name,
391 numfields, numfields == 1 ? "" : "s");
392 res = -1;
393 goto cleanup;
394 }
395 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
396 /* cannot be reached when fields is NULL */
397 PyObject *name = PySequence_GetItem(fields, i);
398 if (!name) {
399 res = -1;
400 goto cleanup;
401 }
402 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
403 Py_DECREF(name);
404 if (res < 0)
405 goto cleanup;
406 }
407 }
408 if (kw) {
409 i = 0; /* needed by PyDict_Next */
410 while (PyDict_Next(kw, &i, &key, &value)) {
411 res = PyObject_SetAttr(self, key, value);
412 if (res < 0)
413 goto cleanup;
414 }
415 }
416 cleanup:
417 Py_XDECREF(fields);
418 return res;
419}
420
421static PyTypeObject AST_type = {
422 PyVarObject_HEAD_INIT(&PyType_Type, 0)
423 "AST",
424 sizeof(PyObject),
425 0,
426 0, /* tp_dealloc */
427 0, /* tp_print */
428 0, /* tp_getattr */
429 0, /* tp_setattr */
430 0, /* tp_compare */
431 0, /* tp_repr */
432 0, /* tp_as_number */
433 0, /* tp_as_sequence */
434 0, /* tp_as_mapping */
435 0, /* tp_hash */
436 0, /* tp_call */
437 0, /* tp_str */
438 PyObject_GenericGetAttr, /* tp_getattro */
439 PyObject_GenericSetAttr, /* tp_setattro */
440 0, /* tp_as_buffer */
441 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
442 0, /* tp_doc */
443 0, /* tp_traverse */
444 0, /* tp_clear */
445 0, /* tp_richcompare */
446 0, /* tp_weaklistoffset */
447 0, /* tp_iter */
448 0, /* tp_iternext */
449 0, /* tp_methods */
450 0, /* tp_members */
451 0, /* tp_getset */
452 0, /* tp_base */
453 0, /* tp_dict */
454 0, /* tp_descr_get */
455 0, /* tp_descr_set */
456 0, /* tp_dictoffset */
457 (initproc)ast_type_init, /* tp_init */
458 PyType_GenericAlloc, /* tp_alloc */
459 PyType_GenericNew, /* tp_new */
460 PyObject_Del, /* tp_free */
461};
462
463
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000464static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
465{
466 PyObject *fnames, *result;
467 int i;
Georg Brandl2c55c592008-03-30 19:00:49 +0000468 fnames = PyTuple_New(num_fields);
469 if (!fnames) return NULL;
470 for (i = 0; i < num_fields; i++) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000471 PyObject *field = PyString_FromString(fields[i]);
472 if (!field) {
473 Py_DECREF(fnames);
474 return NULL;
475 }
476 PyTuple_SET_ITEM(fnames, i, field);
477 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000478 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000479 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000480 Py_DECREF(fnames);
481 return (PyTypeObject*)result;
482}
483
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000484static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
485{
Neal Norwitz19379f12006-04-03 04:50:58 +0000486 int i, result;
Georg Brandlc52ed592008-03-30 07:01:47 +0000487 PyObject *s, *l = PyTuple_New(num_fields);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000488 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000489 for(i = 0; i < num_fields; i++) {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000490 s = PyString_FromString(attrs[i]);
491 if (!s) {
492 Py_DECREF(l);
493 return 0;
494 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000495 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000496 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000497 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
498 Py_DECREF(l);
499 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000500}
501
Georg Brandlfc8eef32008-03-28 12:11:56 +0000502/* Conversion AST -> Python */
503
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000504static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
505{
506 int i, n = asdl_seq_LEN(seq);
507 PyObject *result = PyList_New(n);
508 PyObject *value;
509 if (!result)
510 return NULL;
511 for (i = 0; i < n; i++) {
512 value = func(asdl_seq_GET(seq, i));
513 if (!value) {
514 Py_DECREF(result);
515 return NULL;
516 }
517 PyList_SET_ITEM(result, i, value);
518 }
519 return result;
520}
521
522static PyObject* ast2obj_object(void *o)
523{
524 if (!o)
525 o = Py_None;
526 Py_INCREF((PyObject*)o);
527 return (PyObject*)o;
528}
529#define ast2obj_identifier ast2obj_object
530#define ast2obj_string ast2obj_object
531static PyObject* ast2obj_bool(bool b)
532{
533 return PyBool_FromLong(b);
534}
535
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000536static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000537{
538 return PyInt_FromLong(b);
539}
540
Georg Brandlfc8eef32008-03-28 12:11:56 +0000541/* Conversion Python -> AST */
542
543static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
544{
545 if (obj == Py_None)
546 obj = NULL;
547 if (obj)
548 PyArena_AddPyObject(arena, obj);
549 Py_XINCREF(obj);
550 *out = obj;
551 return 0;
552}
553
554#define obj2ast_identifier obj2ast_object
555#define obj2ast_string obj2ast_object
556
557static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
558{
559 int i;
560 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
561 PyObject *s = PyObject_Repr(obj);
562 if (s == NULL) return 1;
563 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
564 PyString_AS_STRING(s));
565 Py_DECREF(s);
566 return 1;
567 }
568
569 i = (int)PyLong_AsLong(obj);
570 if (i == -1 && PyErr_Occurred())
571 return 1;
572 *out = i;
573 return 0;
574}
575
576static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
577{
578 if (!PyBool_Check(obj)) {
579 PyObject *s = PyObject_Repr(obj);
580 if (s == NULL) return 1;
581 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
582 PyString_AS_STRING(s));
583 Py_DECREF(s);
584 return 1;
585 }
586
587 *out = (obj == Py_True);
588 return 0;
589}
590
591
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000592static int init_types(void)
593{
Neal Norwitz19379f12006-04-03 04:50:58 +0000594 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000595 if (initialized) return 1;
Georg Brandlc52ed592008-03-30 07:01:47 +0000596 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000597 if (!mod_type) return 0;
598 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000599 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000600 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000601 Interactive_type = make_type("Interactive", mod_type,
602 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000603 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000604 Expression_type = make_type("Expression", mod_type, Expression_fields,
605 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000606 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000607 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000608 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000609 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000610 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000611 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000612 FunctionDef_type = make_type("FunctionDef", stmt_type,
613 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000614 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000615 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000616 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000617 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000618 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000619 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000620 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000621 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000622 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000623 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000624 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000625 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000626 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000627 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000628 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000629 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000630 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000631 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000632 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000633 With_type = make_type("With", stmt_type, With_fields, 3);
634 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000635 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000636 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000637 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000638 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000639 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
640 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000641 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000642 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000643 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000645 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000646 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000647 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000648 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000649 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000650 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000654 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000655 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000656 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000657 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000658 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000659 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000660 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000661 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000662 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000663 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000672 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Dict_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 GeneratorExp_type = make_type("GeneratorExp", expr_type,
679 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000680 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000683 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000684 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000687 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000689 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000693 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000699 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000700 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000703 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!expr_context_type) return 0;
705 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000706 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000708 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000710 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000714 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000716 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000718 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000722 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000724 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000726 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000730 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!slice_type) return 0;
732 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000733 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000737 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000741 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!boolop_type) return 0;
743 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000744 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000748 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000752 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!operator_type) return 0;
754 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000755 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000759 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000761 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000763 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000767 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000769 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000771 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000775 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000779 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000781 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000783 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000785 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000787 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000789 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000791 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000793 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000795 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000797 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000799 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000801 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000803 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!unaryop_type) return 0;
805 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000806 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000814 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000816 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000818 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000820 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000822 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!cmpop_type) return 0;
824 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000825 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000827 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000829 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000830 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000831 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000832 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000833 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000835 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000837 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000839 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000841 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000843 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000845 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000847 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000849 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000851 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000853 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000855 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000857 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000861 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000863 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000865 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000866 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000868 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000870 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
871 return 0;
872 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
873 ExceptHandler_fields, 3);
874 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000875 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000877 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000879 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!alias_type) return 0;
881 initialized = 1;
882 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000883}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000884
Georg Brandlfc8eef32008-03-28 12:11:56 +0000885static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
886static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
887static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
888static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
889 arena);
890static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
891static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
892static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
893static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
894static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
895static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
896 arena);
897static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
898 arena);
899static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
900static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
901static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
902
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000904Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905{
906 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000907 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000908 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 p->kind = Module_kind;
911 p->v.Module.body = body;
912 return p;
913}
914
915mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000916Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917{
918 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000919 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000920 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 p->kind = Interactive_kind;
923 p->v.Interactive.body = body;
924 return p;
925}
926
927mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000928Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929{
930 mod_ty p;
931 if (!body) {
932 PyErr_SetString(PyExc_ValueError,
933 "field body is required for Expression");
934 return NULL;
935 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000936 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000937 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 p->kind = Expression_kind;
940 p->v.Expression.body = body;
941 return p;
942}
943
944mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000945Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946{
947 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000948 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000949 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 p->kind = Suite_kind;
952 p->v.Suite.body = body;
953 return p;
954}
955
956stmt_ty
957FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +0000958 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959{
960 stmt_ty p;
961 if (!name) {
962 PyErr_SetString(PyExc_ValueError,
963 "field name is required for FunctionDef");
964 return NULL;
965 }
966 if (!args) {
967 PyErr_SetString(PyExc_ValueError,
968 "field args is required for FunctionDef");
969 return NULL;
970 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000972 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 p->kind = FunctionDef_kind;
975 p->v.FunctionDef.name = name;
976 p->v.FunctionDef.args = args;
977 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +0000978 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000980 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 return p;
982}
983
984stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000985ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
986 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987{
988 stmt_ty p;
989 if (!name) {
990 PyErr_SetString(PyExc_ValueError,
991 "field name is required for ClassDef");
992 return NULL;
993 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000995 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 p->kind = ClassDef_kind;
998 p->v.ClassDef.name = name;
999 p->v.ClassDef.bases = bases;
1000 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001001 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001003 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 return p;
1005}
1006
1007stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001008Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009{
1010 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001011 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001012 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 p->kind = Return_kind;
1015 p->v.Return.value = value;
1016 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001017 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 return p;
1019}
1020
1021stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001022Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023{
1024 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001025 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001026 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 p->kind = Delete_kind;
1029 p->v.Delete.targets = targets;
1030 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001031 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 return p;
1033}
1034
1035stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001036Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1037 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038{
1039 stmt_ty p;
1040 if (!value) {
1041 PyErr_SetString(PyExc_ValueError,
1042 "field value is required for Assign");
1043 return NULL;
1044 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001045 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001046 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 p->kind = Assign_kind;
1049 p->v.Assign.targets = targets;
1050 p->v.Assign.value = value;
1051 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001052 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 return p;
1054}
1055
1056stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001057AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1058 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059{
1060 stmt_ty p;
1061 if (!target) {
1062 PyErr_SetString(PyExc_ValueError,
1063 "field target is required for AugAssign");
1064 return NULL;
1065 }
1066 if (!op) {
1067 PyErr_SetString(PyExc_ValueError,
1068 "field op is required for AugAssign");
1069 return NULL;
1070 }
1071 if (!value) {
1072 PyErr_SetString(PyExc_ValueError,
1073 "field value is required for AugAssign");
1074 return NULL;
1075 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001076 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001077 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 p->kind = AugAssign_kind;
1080 p->v.AugAssign.target = target;
1081 p->v.AugAssign.op = op;
1082 p->v.AugAssign.value = value;
1083 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001084 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 return p;
1086}
1087
1088stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001089Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1090 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091{
1092 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001093 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001094 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 p->kind = Print_kind;
1097 p->v.Print.dest = dest;
1098 p->v.Print.values = values;
1099 p->v.Print.nl = nl;
1100 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001101 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 return p;
1103}
1104
1105stmt_ty
1106For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001107 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108{
1109 stmt_ty p;
1110 if (!target) {
1111 PyErr_SetString(PyExc_ValueError,
1112 "field target is required for For");
1113 return NULL;
1114 }
1115 if (!iter) {
1116 PyErr_SetString(PyExc_ValueError,
1117 "field iter is required for For");
1118 return NULL;
1119 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001120 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001121 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 p->kind = For_kind;
1124 p->v.For.target = target;
1125 p->v.For.iter = iter;
1126 p->v.For.body = body;
1127 p->v.For.orelse = orelse;
1128 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001129 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 return p;
1131}
1132
1133stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001134While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1135 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136{
1137 stmt_ty p;
1138 if (!test) {
1139 PyErr_SetString(PyExc_ValueError,
1140 "field test is required for While");
1141 return NULL;
1142 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001144 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 p->kind = While_kind;
1147 p->v.While.test = test;
1148 p->v.While.body = body;
1149 p->v.While.orelse = orelse;
1150 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001151 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 return p;
1153}
1154
1155stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001156If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1157 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158{
1159 stmt_ty p;
1160 if (!test) {
1161 PyErr_SetString(PyExc_ValueError,
1162 "field test is required for If");
1163 return NULL;
1164 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001165 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001166 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 p->kind = If_kind;
1169 p->v.If.test = test;
1170 p->v.If.body = body;
1171 p->v.If.orelse = orelse;
1172 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001173 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 return p;
1175}
1176
1177stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001178With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001179 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001180{
1181 stmt_ty p;
1182 if (!context_expr) {
1183 PyErr_SetString(PyExc_ValueError,
1184 "field context_expr is required for With");
1185 return NULL;
1186 }
1187 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001188 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001189 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001190 p->kind = With_kind;
1191 p->v.With.context_expr = context_expr;
1192 p->v.With.optional_vars = optional_vars;
1193 p->v.With.body = body;
1194 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001195 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001196 return p;
1197}
1198
1199stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1201 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202{
1203 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001205 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 p->kind = Raise_kind;
1208 p->v.Raise.type = type;
1209 p->v.Raise.inst = inst;
1210 p->v.Raise.tback = tback;
1211 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001212 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 return p;
1214}
1215
1216stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001217TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219{
1220 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001222 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 p->kind = TryExcept_kind;
1225 p->v.TryExcept.body = body;
1226 p->v.TryExcept.handlers = handlers;
1227 p->v.TryExcept.orelse = orelse;
1228 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001229 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 return p;
1231}
1232
1233stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001234TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1235 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236{
1237 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001239 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 p->kind = TryFinally_kind;
1242 p->v.TryFinally.body = body;
1243 p->v.TryFinally.finalbody = finalbody;
1244 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001245 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 return p;
1247}
1248
1249stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001250Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251{
1252 stmt_ty p;
1253 if (!test) {
1254 PyErr_SetString(PyExc_ValueError,
1255 "field test is required for Assert");
1256 return NULL;
1257 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001258 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001259 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 p->kind = Assert_kind;
1262 p->v.Assert.test = test;
1263 p->v.Assert.msg = msg;
1264 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001265 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 return p;
1267}
1268
1269stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001270Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271{
1272 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001274 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 p->kind = Import_kind;
1277 p->v.Import.names = names;
1278 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return p;
1281}
1282
1283stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1285 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286{
1287 stmt_ty p;
1288 if (!module) {
1289 PyErr_SetString(PyExc_ValueError,
1290 "field module is required for ImportFrom");
1291 return NULL;
1292 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001293 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001294 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 p->kind = ImportFrom_kind;
1297 p->v.ImportFrom.module = module;
1298 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001299 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001301 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 return p;
1303}
1304
1305stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001306Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1307 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308{
1309 stmt_ty p;
1310 if (!body) {
1311 PyErr_SetString(PyExc_ValueError,
1312 "field body is required for Exec");
1313 return NULL;
1314 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001315 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001316 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 p->kind = Exec_kind;
1319 p->v.Exec.body = body;
1320 p->v.Exec.globals = globals;
1321 p->v.Exec.locals = locals;
1322 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001323 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 return p;
1325}
1326
1327stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001328Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329{
1330 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001331 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001332 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 p->kind = Global_kind;
1335 p->v.Global.names = names;
1336 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001337 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 return p;
1339}
1340
1341stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001342Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343{
1344 stmt_ty p;
1345 if (!value) {
1346 PyErr_SetString(PyExc_ValueError,
1347 "field value is required for Expr");
1348 return NULL;
1349 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001350 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001351 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 p->kind = Expr_kind;
1354 p->v.Expr.value = value;
1355 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001356 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 return p;
1358}
1359
1360stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001361Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362{
1363 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001364 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001365 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 p->kind = Pass_kind;
1368 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001369 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 return p;
1371}
1372
1373stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001374Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375{
1376 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001378 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 p->kind = Break_kind;
1381 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001382 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 return p;
1384}
1385
1386stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001387Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388{
1389 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001390 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001391 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 p->kind = Continue_kind;
1394 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001395 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return p;
1397}
1398
1399expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001400BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1401 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402{
1403 expr_ty p;
1404 if (!op) {
1405 PyErr_SetString(PyExc_ValueError,
1406 "field op is required for BoolOp");
1407 return NULL;
1408 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001409 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001410 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 p->kind = BoolOp_kind;
1413 p->v.BoolOp.op = op;
1414 p->v.BoolOp.values = values;
1415 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001416 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return p;
1418}
1419
1420expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1422 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423{
1424 expr_ty p;
1425 if (!left) {
1426 PyErr_SetString(PyExc_ValueError,
1427 "field left is required for BinOp");
1428 return NULL;
1429 }
1430 if (!op) {
1431 PyErr_SetString(PyExc_ValueError,
1432 "field op is required for BinOp");
1433 return NULL;
1434 }
1435 if (!right) {
1436 PyErr_SetString(PyExc_ValueError,
1437 "field right is required for BinOp");
1438 return NULL;
1439 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001440 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001441 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 p->kind = BinOp_kind;
1444 p->v.BinOp.left = left;
1445 p->v.BinOp.op = op;
1446 p->v.BinOp.right = right;
1447 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001448 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 return p;
1450}
1451
1452expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001453UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1454 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455{
1456 expr_ty p;
1457 if (!op) {
1458 PyErr_SetString(PyExc_ValueError,
1459 "field op is required for UnaryOp");
1460 return NULL;
1461 }
1462 if (!operand) {
1463 PyErr_SetString(PyExc_ValueError,
1464 "field operand is required for UnaryOp");
1465 return NULL;
1466 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001467 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001468 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 p->kind = UnaryOp_kind;
1471 p->v.UnaryOp.op = op;
1472 p->v.UnaryOp.operand = operand;
1473 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001474 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 return p;
1476}
1477
1478expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1480 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481{
1482 expr_ty p;
1483 if (!args) {
1484 PyErr_SetString(PyExc_ValueError,
1485 "field args is required for Lambda");
1486 return NULL;
1487 }
1488 if (!body) {
1489 PyErr_SetString(PyExc_ValueError,
1490 "field body is required for Lambda");
1491 return NULL;
1492 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001493 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001494 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 p->kind = Lambda_kind;
1497 p->v.Lambda.args = args;
1498 p->v.Lambda.body = body;
1499 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 return p;
1502}
1503
1504expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001505IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1506 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001507{
1508 expr_ty p;
1509 if (!test) {
1510 PyErr_SetString(PyExc_ValueError,
1511 "field test is required for IfExp");
1512 return NULL;
1513 }
1514 if (!body) {
1515 PyErr_SetString(PyExc_ValueError,
1516 "field body is required for IfExp");
1517 return NULL;
1518 }
1519 if (!orelse) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "field orelse is required for IfExp");
1522 return NULL;
1523 }
1524 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001525 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001526 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001527 p->kind = IfExp_kind;
1528 p->v.IfExp.test = test;
1529 p->v.IfExp.body = body;
1530 p->v.IfExp.orelse = orelse;
1531 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001532 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001533 return p;
1534}
1535
1536expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001537Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1538 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539{
1540 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001541 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001542 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 p->kind = Dict_kind;
1545 p->v.Dict.keys = keys;
1546 p->v.Dict.values = values;
1547 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001548 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 return p;
1550}
1551
1552expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1554 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555{
1556 expr_ty p;
1557 if (!elt) {
1558 PyErr_SetString(PyExc_ValueError,
1559 "field elt is required for ListComp");
1560 return NULL;
1561 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001563 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 p->kind = ListComp_kind;
1566 p->v.ListComp.elt = elt;
1567 p->v.ListComp.generators = generators;
1568 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001569 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 return p;
1571}
1572
1573expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001574GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1575 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576{
1577 expr_ty p;
1578 if (!elt) {
1579 PyErr_SetString(PyExc_ValueError,
1580 "field elt is required for GeneratorExp");
1581 return NULL;
1582 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001583 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001584 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 p->kind = GeneratorExp_kind;
1587 p->v.GeneratorExp.elt = elt;
1588 p->v.GeneratorExp.generators = generators;
1589 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001590 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 return p;
1592}
1593
1594expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001595Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596{
1597 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001598 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001599 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601 p->kind = Yield_kind;
1602 p->v.Yield.value = value;
1603 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001604 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 return p;
1606}
1607
1608expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001609Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1610 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611{
1612 expr_ty p;
1613 if (!left) {
1614 PyErr_SetString(PyExc_ValueError,
1615 "field left is required for Compare");
1616 return NULL;
1617 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001618 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001619 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621 p->kind = Compare_kind;
1622 p->v.Compare.left = left;
1623 p->v.Compare.ops = ops;
1624 p->v.Compare.comparators = comparators;
1625 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001626 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 return p;
1628}
1629
1630expr_ty
1631Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001632 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633{
1634 expr_ty p;
1635 if (!func) {
1636 PyErr_SetString(PyExc_ValueError,
1637 "field func is required for Call");
1638 return NULL;
1639 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001640 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001641 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 p->kind = Call_kind;
1644 p->v.Call.func = func;
1645 p->v.Call.args = args;
1646 p->v.Call.keywords = keywords;
1647 p->v.Call.starargs = starargs;
1648 p->v.Call.kwargs = kwargs;
1649 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001650 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 return p;
1652}
1653
1654expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656{
1657 expr_ty p;
1658 if (!value) {
1659 PyErr_SetString(PyExc_ValueError,
1660 "field value is required for Repr");
1661 return NULL;
1662 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001663 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001664 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 p->kind = Repr_kind;
1667 p->v.Repr.value = value;
1668 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001669 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 return p;
1671}
1672
1673expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001674Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675{
1676 expr_ty p;
1677 if (!n) {
1678 PyErr_SetString(PyExc_ValueError,
1679 "field n is required for Num");
1680 return NULL;
1681 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001682 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001683 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 p->kind = Num_kind;
1686 p->v.Num.n = n;
1687 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001688 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 return p;
1690}
1691
1692expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001693Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694{
1695 expr_ty p;
1696 if (!s) {
1697 PyErr_SetString(PyExc_ValueError,
1698 "field s is required for Str");
1699 return NULL;
1700 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001701 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001702 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 p->kind = Str_kind;
1705 p->v.Str.s = s;
1706 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001707 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return p;
1709}
1710
1711expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001712Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1713 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714{
1715 expr_ty p;
1716 if (!value) {
1717 PyErr_SetString(PyExc_ValueError,
1718 "field value is required for Attribute");
1719 return NULL;
1720 }
1721 if (!attr) {
1722 PyErr_SetString(PyExc_ValueError,
1723 "field attr is required for Attribute");
1724 return NULL;
1725 }
1726 if (!ctx) {
1727 PyErr_SetString(PyExc_ValueError,
1728 "field ctx is required for Attribute");
1729 return NULL;
1730 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001731 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001732 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 p->kind = Attribute_kind;
1735 p->v.Attribute.value = value;
1736 p->v.Attribute.attr = attr;
1737 p->v.Attribute.ctx = ctx;
1738 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001739 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 return p;
1741}
1742
1743expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001744Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1745 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746{
1747 expr_ty p;
1748 if (!value) {
1749 PyErr_SetString(PyExc_ValueError,
1750 "field value is required for Subscript");
1751 return NULL;
1752 }
1753 if (!slice) {
1754 PyErr_SetString(PyExc_ValueError,
1755 "field slice is required for Subscript");
1756 return NULL;
1757 }
1758 if (!ctx) {
1759 PyErr_SetString(PyExc_ValueError,
1760 "field ctx is required for Subscript");
1761 return NULL;
1762 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001763 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001764 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 p->kind = Subscript_kind;
1767 p->v.Subscript.value = value;
1768 p->v.Subscript.slice = slice;
1769 p->v.Subscript.ctx = ctx;
1770 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001771 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 return p;
1773}
1774
1775expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001776Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1777 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778{
1779 expr_ty p;
1780 if (!id) {
1781 PyErr_SetString(PyExc_ValueError,
1782 "field id is required for Name");
1783 return NULL;
1784 }
1785 if (!ctx) {
1786 PyErr_SetString(PyExc_ValueError,
1787 "field ctx is required for Name");
1788 return NULL;
1789 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001790 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001791 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 p->kind = Name_kind;
1794 p->v.Name.id = id;
1795 p->v.Name.ctx = ctx;
1796 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001797 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 return p;
1799}
1800
1801expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001802List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1803 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804{
1805 expr_ty p;
1806 if (!ctx) {
1807 PyErr_SetString(PyExc_ValueError,
1808 "field ctx is required for List");
1809 return NULL;
1810 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001811 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001812 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 p->kind = List_kind;
1815 p->v.List.elts = elts;
1816 p->v.List.ctx = ctx;
1817 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001818 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 return p;
1820}
1821
1822expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001823Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1824 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825{
1826 expr_ty p;
1827 if (!ctx) {
1828 PyErr_SetString(PyExc_ValueError,
1829 "field ctx is required for Tuple");
1830 return NULL;
1831 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001833 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 p->kind = Tuple_kind;
1836 p->v.Tuple.elts = elts;
1837 p->v.Tuple.ctx = ctx;
1838 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001839 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 return p;
1841}
1842
1843slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845{
1846 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001847 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001848 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 p->kind = Ellipsis_kind;
1851 return p;
1852}
1853
1854slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001855Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856{
1857 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001859 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 p->kind = Slice_kind;
1862 p->v.Slice.lower = lower;
1863 p->v.Slice.upper = upper;
1864 p->v.Slice.step = step;
1865 return p;
1866}
1867
1868slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870{
1871 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001873 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 p->kind = ExtSlice_kind;
1876 p->v.ExtSlice.dims = dims;
1877 return p;
1878}
1879
1880slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882{
1883 slice_ty p;
1884 if (!value) {
1885 PyErr_SetString(PyExc_ValueError,
1886 "field value is required for Index");
1887 return NULL;
1888 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001890 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 p->kind = Index_kind;
1893 p->v.Index.value = value;
1894 return p;
1895}
1896
1897comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899{
1900 comprehension_ty p;
1901 if (!target) {
1902 PyErr_SetString(PyExc_ValueError,
1903 "field target is required for comprehension");
1904 return NULL;
1905 }
1906 if (!iter) {
1907 PyErr_SetString(PyExc_ValueError,
1908 "field iter is required for comprehension");
1909 return NULL;
1910 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001911 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001912 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 p->target = target;
1915 p->iter = iter;
1916 p->ifs = ifs;
1917 return p;
1918}
1919
1920excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00001921ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001922 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923{
1924 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001925 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001926 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001928 p->kind = ExceptHandler_kind;
1929 p->v.ExceptHandler.type = type;
1930 p->v.ExceptHandler.name = name;
1931 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001932 p->lineno = lineno;
1933 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 return p;
1935}
1936
1937arguments_ty
1938arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001939 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940{
1941 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001942 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001943 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 p->args = args;
1946 p->vararg = vararg;
1947 p->kwarg = kwarg;
1948 p->defaults = defaults;
1949 return p;
1950}
1951
1952keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001953keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954{
1955 keyword_ty p;
1956 if (!arg) {
1957 PyErr_SetString(PyExc_ValueError,
1958 "field arg is required for keyword");
1959 return NULL;
1960 }
1961 if (!value) {
1962 PyErr_SetString(PyExc_ValueError,
1963 "field value is required for keyword");
1964 return NULL;
1965 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001966 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001967 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 p->arg = arg;
1970 p->value = value;
1971 return p;
1972}
1973
1974alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001975alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976{
1977 alias_ty p;
1978 if (!name) {
1979 PyErr_SetString(PyExc_ValueError,
1980 "field name is required for alias");
1981 return NULL;
1982 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001983 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001984 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 p->name = name;
1987 p->asname = asname;
1988 return p;
1989}
1990
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001991
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001992PyObject*
1993ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001994{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001995 mod_ty o = (mod_ty)_o;
1996 PyObject *result = NULL, *value = NULL;
1997 if (!o) {
1998 Py_INCREF(Py_None);
1999 return Py_None;
2000 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002001
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 switch (o->kind) {
2003 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002004 result = PyType_GenericNew(Module_type, NULL, NULL);
2005 if (!result) goto failed;
2006 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2007 if (!value) goto failed;
2008 if (PyObject_SetAttrString(result, "body", value) == -1)
2009 goto failed;
2010 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 break;
2012 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002013 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2014 if (!result) goto failed;
2015 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2016 if (!value) goto failed;
2017 if (PyObject_SetAttrString(result, "body", value) == -1)
2018 goto failed;
2019 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 break;
2021 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002022 result = PyType_GenericNew(Expression_type, NULL, NULL);
2023 if (!result) goto failed;
2024 value = ast2obj_expr(o->v.Expression.body);
2025 if (!value) goto failed;
2026 if (PyObject_SetAttrString(result, "body", value) == -1)
2027 goto failed;
2028 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 break;
2030 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002031 result = PyType_GenericNew(Suite_type, NULL, NULL);
2032 if (!result) goto failed;
2033 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2034 if (!value) goto failed;
2035 if (PyObject_SetAttrString(result, "body", value) == -1)
2036 goto failed;
2037 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 break;
2039 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002040 return result;
2041failed:
2042 Py_XDECREF(value);
2043 Py_XDECREF(result);
2044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045}
2046
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002047PyObject*
2048ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002050 stmt_ty o = (stmt_ty)_o;
2051 PyObject *result = NULL, *value = NULL;
2052 if (!o) {
2053 Py_INCREF(Py_None);
2054 return Py_None;
2055 }
2056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 switch (o->kind) {
2058 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002059 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2060 if (!result) goto failed;
2061 value = ast2obj_identifier(o->v.FunctionDef.name);
2062 if (!value) goto failed;
2063 if (PyObject_SetAttrString(result, "name", value) == -1)
2064 goto failed;
2065 Py_DECREF(value);
2066 value = ast2obj_arguments(o->v.FunctionDef.args);
2067 if (!value) goto failed;
2068 if (PyObject_SetAttrString(result, "args", value) == -1)
2069 goto failed;
2070 Py_DECREF(value);
2071 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2072 if (!value) goto failed;
2073 if (PyObject_SetAttrString(result, "body", value) == -1)
2074 goto failed;
2075 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002076 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2077 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002078 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002079 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2080 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002081 goto failed;
2082 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 break;
2084 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002085 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2086 if (!result) goto failed;
2087 value = ast2obj_identifier(o->v.ClassDef.name);
2088 if (!value) goto failed;
2089 if (PyObject_SetAttrString(result, "name", value) == -1)
2090 goto failed;
2091 Py_DECREF(value);
2092 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2093 if (!value) goto failed;
2094 if (PyObject_SetAttrString(result, "bases", value) == -1)
2095 goto failed;
2096 Py_DECREF(value);
2097 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2098 if (!value) goto failed;
2099 if (PyObject_SetAttrString(result, "body", value) == -1)
2100 goto failed;
2101 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002102 value = ast2obj_list(o->v.ClassDef.decorator_list,
2103 ast2obj_expr);
2104 if (!value) goto failed;
2105 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2106 -1)
2107 goto failed;
2108 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 break;
2110 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002111 result = PyType_GenericNew(Return_type, NULL, NULL);
2112 if (!result) goto failed;
2113 value = ast2obj_expr(o->v.Return.value);
2114 if (!value) goto failed;
2115 if (PyObject_SetAttrString(result, "value", value) == -1)
2116 goto failed;
2117 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 break;
2119 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002120 result = PyType_GenericNew(Delete_type, NULL, NULL);
2121 if (!result) goto failed;
2122 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2123 if (!value) goto failed;
2124 if (PyObject_SetAttrString(result, "targets", value) == -1)
2125 goto failed;
2126 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 break;
2128 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002129 result = PyType_GenericNew(Assign_type, NULL, NULL);
2130 if (!result) goto failed;
2131 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2132 if (!value) goto failed;
2133 if (PyObject_SetAttrString(result, "targets", value) == -1)
2134 goto failed;
2135 Py_DECREF(value);
2136 value = ast2obj_expr(o->v.Assign.value);
2137 if (!value) goto failed;
2138 if (PyObject_SetAttrString(result, "value", value) == -1)
2139 goto failed;
2140 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 break;
2142 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002143 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2144 if (!result) goto failed;
2145 value = ast2obj_expr(o->v.AugAssign.target);
2146 if (!value) goto failed;
2147 if (PyObject_SetAttrString(result, "target", value) == -1)
2148 goto failed;
2149 Py_DECREF(value);
2150 value = ast2obj_operator(o->v.AugAssign.op);
2151 if (!value) goto failed;
2152 if (PyObject_SetAttrString(result, "op", value) == -1)
2153 goto failed;
2154 Py_DECREF(value);
2155 value = ast2obj_expr(o->v.AugAssign.value);
2156 if (!value) goto failed;
2157 if (PyObject_SetAttrString(result, "value", value) == -1)
2158 goto failed;
2159 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 break;
2161 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002162 result = PyType_GenericNew(Print_type, NULL, NULL);
2163 if (!result) goto failed;
2164 value = ast2obj_expr(o->v.Print.dest);
2165 if (!value) goto failed;
2166 if (PyObject_SetAttrString(result, "dest", value) == -1)
2167 goto failed;
2168 Py_DECREF(value);
2169 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2170 if (!value) goto failed;
2171 if (PyObject_SetAttrString(result, "values", value) == -1)
2172 goto failed;
2173 Py_DECREF(value);
2174 value = ast2obj_bool(o->v.Print.nl);
2175 if (!value) goto failed;
2176 if (PyObject_SetAttrString(result, "nl", value) == -1)
2177 goto failed;
2178 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 break;
2180 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002181 result = PyType_GenericNew(For_type, NULL, NULL);
2182 if (!result) goto failed;
2183 value = ast2obj_expr(o->v.For.target);
2184 if (!value) goto failed;
2185 if (PyObject_SetAttrString(result, "target", value) == -1)
2186 goto failed;
2187 Py_DECREF(value);
2188 value = ast2obj_expr(o->v.For.iter);
2189 if (!value) goto failed;
2190 if (PyObject_SetAttrString(result, "iter", value) == -1)
2191 goto failed;
2192 Py_DECREF(value);
2193 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2194 if (!value) goto failed;
2195 if (PyObject_SetAttrString(result, "body", value) == -1)
2196 goto failed;
2197 Py_DECREF(value);
2198 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2199 if (!value) goto failed;
2200 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2201 goto failed;
2202 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 break;
2204 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002205 result = PyType_GenericNew(While_type, NULL, NULL);
2206 if (!result) goto failed;
2207 value = ast2obj_expr(o->v.While.test);
2208 if (!value) goto failed;
2209 if (PyObject_SetAttrString(result, "test", value) == -1)
2210 goto failed;
2211 Py_DECREF(value);
2212 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2213 if (!value) goto failed;
2214 if (PyObject_SetAttrString(result, "body", value) == -1)
2215 goto failed;
2216 Py_DECREF(value);
2217 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2218 if (!value) goto failed;
2219 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2220 goto failed;
2221 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222 break;
2223 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002224 result = PyType_GenericNew(If_type, NULL, NULL);
2225 if (!result) goto failed;
2226 value = ast2obj_expr(o->v.If.test);
2227 if (!value) goto failed;
2228 if (PyObject_SetAttrString(result, "test", value) == -1)
2229 goto failed;
2230 Py_DECREF(value);
2231 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2232 if (!value) goto failed;
2233 if (PyObject_SetAttrString(result, "body", value) == -1)
2234 goto failed;
2235 Py_DECREF(value);
2236 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2237 if (!value) goto failed;
2238 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2239 goto failed;
2240 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002242 case With_kind:
2243 result = PyType_GenericNew(With_type, NULL, NULL);
2244 if (!result) goto failed;
2245 value = ast2obj_expr(o->v.With.context_expr);
2246 if (!value) goto failed;
2247 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2248 goto failed;
2249 Py_DECREF(value);
2250 value = ast2obj_expr(o->v.With.optional_vars);
2251 if (!value) goto failed;
2252 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2253 -1)
2254 goto failed;
2255 Py_DECREF(value);
2256 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2257 if (!value) goto failed;
2258 if (PyObject_SetAttrString(result, "body", value) == -1)
2259 goto failed;
2260 Py_DECREF(value);
2261 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002263 result = PyType_GenericNew(Raise_type, NULL, NULL);
2264 if (!result) goto failed;
2265 value = ast2obj_expr(o->v.Raise.type);
2266 if (!value) goto failed;
2267 if (PyObject_SetAttrString(result, "type", value) == -1)
2268 goto failed;
2269 Py_DECREF(value);
2270 value = ast2obj_expr(o->v.Raise.inst);
2271 if (!value) goto failed;
2272 if (PyObject_SetAttrString(result, "inst", value) == -1)
2273 goto failed;
2274 Py_DECREF(value);
2275 value = ast2obj_expr(o->v.Raise.tback);
2276 if (!value) goto failed;
2277 if (PyObject_SetAttrString(result, "tback", value) == -1)
2278 goto failed;
2279 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 break;
2281 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002282 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2283 if (!result) goto failed;
2284 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2285 if (!value) goto failed;
2286 if (PyObject_SetAttrString(result, "body", value) == -1)
2287 goto failed;
2288 Py_DECREF(value);
2289 value = ast2obj_list(o->v.TryExcept.handlers,
2290 ast2obj_excepthandler);
2291 if (!value) goto failed;
2292 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2293 goto failed;
2294 Py_DECREF(value);
2295 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2296 if (!value) goto failed;
2297 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2298 goto failed;
2299 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 break;
2301 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002302 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2303 if (!result) goto failed;
2304 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2305 if (!value) goto failed;
2306 if (PyObject_SetAttrString(result, "body", value) == -1)
2307 goto failed;
2308 Py_DECREF(value);
2309 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2310 if (!value) goto failed;
2311 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2312 goto failed;
2313 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 break;
2315 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002316 result = PyType_GenericNew(Assert_type, NULL, NULL);
2317 if (!result) goto failed;
2318 value = ast2obj_expr(o->v.Assert.test);
2319 if (!value) goto failed;
2320 if (PyObject_SetAttrString(result, "test", value) == -1)
2321 goto failed;
2322 Py_DECREF(value);
2323 value = ast2obj_expr(o->v.Assert.msg);
2324 if (!value) goto failed;
2325 if (PyObject_SetAttrString(result, "msg", value) == -1)
2326 goto failed;
2327 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 break;
2329 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002330 result = PyType_GenericNew(Import_type, NULL, NULL);
2331 if (!result) goto failed;
2332 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2333 if (!value) goto failed;
2334 if (PyObject_SetAttrString(result, "names", value) == -1)
2335 goto failed;
2336 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 break;
2338 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002339 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2340 if (!result) goto failed;
2341 value = ast2obj_identifier(o->v.ImportFrom.module);
2342 if (!value) goto failed;
2343 if (PyObject_SetAttrString(result, "module", value) == -1)
2344 goto failed;
2345 Py_DECREF(value);
2346 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2347 if (!value) goto failed;
2348 if (PyObject_SetAttrString(result, "names", value) == -1)
2349 goto failed;
2350 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002351 value = ast2obj_int(o->v.ImportFrom.level);
2352 if (!value) goto failed;
2353 if (PyObject_SetAttrString(result, "level", value) == -1)
2354 goto failed;
2355 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 break;
2357 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002358 result = PyType_GenericNew(Exec_type, NULL, NULL);
2359 if (!result) goto failed;
2360 value = ast2obj_expr(o->v.Exec.body);
2361 if (!value) goto failed;
2362 if (PyObject_SetAttrString(result, "body", value) == -1)
2363 goto failed;
2364 Py_DECREF(value);
2365 value = ast2obj_expr(o->v.Exec.globals);
2366 if (!value) goto failed;
2367 if (PyObject_SetAttrString(result, "globals", value) == -1)
2368 goto failed;
2369 Py_DECREF(value);
2370 value = ast2obj_expr(o->v.Exec.locals);
2371 if (!value) goto failed;
2372 if (PyObject_SetAttrString(result, "locals", value) == -1)
2373 goto failed;
2374 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 break;
2376 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002377 result = PyType_GenericNew(Global_type, NULL, NULL);
2378 if (!result) goto failed;
2379 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2380 if (!value) goto failed;
2381 if (PyObject_SetAttrString(result, "names", value) == -1)
2382 goto failed;
2383 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 break;
2385 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002386 result = PyType_GenericNew(Expr_type, NULL, NULL);
2387 if (!result) goto failed;
2388 value = ast2obj_expr(o->v.Expr.value);
2389 if (!value) goto failed;
2390 if (PyObject_SetAttrString(result, "value", value) == -1)
2391 goto failed;
2392 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 break;
2394 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002395 result = PyType_GenericNew(Pass_type, NULL, NULL);
2396 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 break;
2398 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002399 result = PyType_GenericNew(Break_type, NULL, NULL);
2400 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 break;
2402 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002403 result = PyType_GenericNew(Continue_type, NULL, NULL);
2404 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 break;
2406 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002407 value = ast2obj_int(o->lineno);
2408 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002409 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2410 goto failed;
2411 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002412 value = ast2obj_int(o->col_offset);
2413 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002414 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2415 goto failed;
2416 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002417 return result;
2418failed:
2419 Py_XDECREF(value);
2420 Py_XDECREF(result);
2421 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422}
2423
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002424PyObject*
2425ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002427 expr_ty o = (expr_ty)_o;
2428 PyObject *result = NULL, *value = NULL;
2429 if (!o) {
2430 Py_INCREF(Py_None);
2431 return Py_None;
2432 }
2433
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 switch (o->kind) {
2435 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002436 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2437 if (!result) goto failed;
2438 value = ast2obj_boolop(o->v.BoolOp.op);
2439 if (!value) goto failed;
2440 if (PyObject_SetAttrString(result, "op", value) == -1)
2441 goto failed;
2442 Py_DECREF(value);
2443 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2444 if (!value) goto failed;
2445 if (PyObject_SetAttrString(result, "values", value) == -1)
2446 goto failed;
2447 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 break;
2449 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002450 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2451 if (!result) goto failed;
2452 value = ast2obj_expr(o->v.BinOp.left);
2453 if (!value) goto failed;
2454 if (PyObject_SetAttrString(result, "left", value) == -1)
2455 goto failed;
2456 Py_DECREF(value);
2457 value = ast2obj_operator(o->v.BinOp.op);
2458 if (!value) goto failed;
2459 if (PyObject_SetAttrString(result, "op", value) == -1)
2460 goto failed;
2461 Py_DECREF(value);
2462 value = ast2obj_expr(o->v.BinOp.right);
2463 if (!value) goto failed;
2464 if (PyObject_SetAttrString(result, "right", value) == -1)
2465 goto failed;
2466 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 break;
2468 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002469 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2470 if (!result) goto failed;
2471 value = ast2obj_unaryop(o->v.UnaryOp.op);
2472 if (!value) goto failed;
2473 if (PyObject_SetAttrString(result, "op", value) == -1)
2474 goto failed;
2475 Py_DECREF(value);
2476 value = ast2obj_expr(o->v.UnaryOp.operand);
2477 if (!value) goto failed;
2478 if (PyObject_SetAttrString(result, "operand", value) == -1)
2479 goto failed;
2480 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 break;
2482 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002483 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2484 if (!result) goto failed;
2485 value = ast2obj_arguments(o->v.Lambda.args);
2486 if (!value) goto failed;
2487 if (PyObject_SetAttrString(result, "args", value) == -1)
2488 goto failed;
2489 Py_DECREF(value);
2490 value = ast2obj_expr(o->v.Lambda.body);
2491 if (!value) goto failed;
2492 if (PyObject_SetAttrString(result, "body", value) == -1)
2493 goto failed;
2494 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002496 case IfExp_kind:
2497 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2498 if (!result) goto failed;
2499 value = ast2obj_expr(o->v.IfExp.test);
2500 if (!value) goto failed;
2501 if (PyObject_SetAttrString(result, "test", value) == -1)
2502 goto failed;
2503 Py_DECREF(value);
2504 value = ast2obj_expr(o->v.IfExp.body);
2505 if (!value) goto failed;
2506 if (PyObject_SetAttrString(result, "body", value) == -1)
2507 goto failed;
2508 Py_DECREF(value);
2509 value = ast2obj_expr(o->v.IfExp.orelse);
2510 if (!value) goto failed;
2511 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2512 goto failed;
2513 Py_DECREF(value);
2514 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002516 result = PyType_GenericNew(Dict_type, NULL, NULL);
2517 if (!result) goto failed;
2518 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2519 if (!value) goto failed;
2520 if (PyObject_SetAttrString(result, "keys", value) == -1)
2521 goto failed;
2522 Py_DECREF(value);
2523 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2524 if (!value) goto failed;
2525 if (PyObject_SetAttrString(result, "values", value) == -1)
2526 goto failed;
2527 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 break;
2529 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002530 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2531 if (!result) goto failed;
2532 value = ast2obj_expr(o->v.ListComp.elt);
2533 if (!value) goto failed;
2534 if (PyObject_SetAttrString(result, "elt", value) == -1)
2535 goto failed;
2536 Py_DECREF(value);
2537 value = ast2obj_list(o->v.ListComp.generators,
2538 ast2obj_comprehension);
2539 if (!value) goto failed;
2540 if (PyObject_SetAttrString(result, "generators", value) == -1)
2541 goto failed;
2542 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 break;
2544 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002545 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2546 if (!result) goto failed;
2547 value = ast2obj_expr(o->v.GeneratorExp.elt);
2548 if (!value) goto failed;
2549 if (PyObject_SetAttrString(result, "elt", value) == -1)
2550 goto failed;
2551 Py_DECREF(value);
2552 value = ast2obj_list(o->v.GeneratorExp.generators,
2553 ast2obj_comprehension);
2554 if (!value) goto failed;
2555 if (PyObject_SetAttrString(result, "generators", value) == -1)
2556 goto failed;
2557 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 break;
2559 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002560 result = PyType_GenericNew(Yield_type, NULL, NULL);
2561 if (!result) goto failed;
2562 value = ast2obj_expr(o->v.Yield.value);
2563 if (!value) goto failed;
2564 if (PyObject_SetAttrString(result, "value", value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 break;
2568 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002569 result = PyType_GenericNew(Compare_type, NULL, NULL);
2570 if (!result) goto failed;
2571 value = ast2obj_expr(o->v.Compare.left);
2572 if (!value) goto failed;
2573 if (PyObject_SetAttrString(result, "left", value) == -1)
2574 goto failed;
2575 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002576 {
2577 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2578 value = PyList_New(n);
2579 if (!value) goto failed;
2580 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002581 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002582 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002583 if (!value) goto failed;
2584 if (PyObject_SetAttrString(result, "ops", value) == -1)
2585 goto failed;
2586 Py_DECREF(value);
2587 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2588 if (!value) goto failed;
2589 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 break;
2593 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002594 result = PyType_GenericNew(Call_type, NULL, NULL);
2595 if (!result) goto failed;
2596 value = ast2obj_expr(o->v.Call.func);
2597 if (!value) goto failed;
2598 if (PyObject_SetAttrString(result, "func", value) == -1)
2599 goto failed;
2600 Py_DECREF(value);
2601 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2602 if (!value) goto failed;
2603 if (PyObject_SetAttrString(result, "args", value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
2606 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2607 if (!value) goto failed;
2608 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2609 goto failed;
2610 Py_DECREF(value);
2611 value = ast2obj_expr(o->v.Call.starargs);
2612 if (!value) goto failed;
2613 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2614 goto failed;
2615 Py_DECREF(value);
2616 value = ast2obj_expr(o->v.Call.kwargs);
2617 if (!value) goto failed;
2618 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2619 goto failed;
2620 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 break;
2622 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002623 result = PyType_GenericNew(Repr_type, NULL, NULL);
2624 if (!result) goto failed;
2625 value = ast2obj_expr(o->v.Repr.value);
2626 if (!value) goto failed;
2627 if (PyObject_SetAttrString(result, "value", value) == -1)
2628 goto failed;
2629 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 break;
2631 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002632 result = PyType_GenericNew(Num_type, NULL, NULL);
2633 if (!result) goto failed;
2634 value = ast2obj_object(o->v.Num.n);
2635 if (!value) goto failed;
2636 if (PyObject_SetAttrString(result, "n", value) == -1)
2637 goto failed;
2638 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 break;
2640 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002641 result = PyType_GenericNew(Str_type, NULL, NULL);
2642 if (!result) goto failed;
2643 value = ast2obj_string(o->v.Str.s);
2644 if (!value) goto failed;
2645 if (PyObject_SetAttrString(result, "s", value) == -1)
2646 goto failed;
2647 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 break;
2649 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002650 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2651 if (!result) goto failed;
2652 value = ast2obj_expr(o->v.Attribute.value);
2653 if (!value) goto failed;
2654 if (PyObject_SetAttrString(result, "value", value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
2657 value = ast2obj_identifier(o->v.Attribute.attr);
2658 if (!value) goto failed;
2659 if (PyObject_SetAttrString(result, "attr", value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 value = ast2obj_expr_context(o->v.Attribute.ctx);
2663 if (!value) goto failed;
2664 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 break;
2668 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002669 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2670 if (!result) goto failed;
2671 value = ast2obj_expr(o->v.Subscript.value);
2672 if (!value) goto failed;
2673 if (PyObject_SetAttrString(result, "value", value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
2676 value = ast2obj_slice(o->v.Subscript.slice);
2677 if (!value) goto failed;
2678 if (PyObject_SetAttrString(result, "slice", value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 value = ast2obj_expr_context(o->v.Subscript.ctx);
2682 if (!value) goto failed;
2683 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2684 goto failed;
2685 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 break;
2687 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002688 result = PyType_GenericNew(Name_type, NULL, NULL);
2689 if (!result) goto failed;
2690 value = ast2obj_identifier(o->v.Name.id);
2691 if (!value) goto failed;
2692 if (PyObject_SetAttrString(result, "id", value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
2695 value = ast2obj_expr_context(o->v.Name.ctx);
2696 if (!value) goto failed;
2697 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 break;
2701 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002702 result = PyType_GenericNew(List_type, NULL, NULL);
2703 if (!result) goto failed;
2704 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2705 if (!value) goto failed;
2706 if (PyObject_SetAttrString(result, "elts", value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_expr_context(o->v.List.ctx);
2710 if (!value) goto failed;
2711 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 break;
2715 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002716 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2717 if (!result) goto failed;
2718 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2719 if (!value) goto failed;
2720 if (PyObject_SetAttrString(result, "elts", value) == -1)
2721 goto failed;
2722 Py_DECREF(value);
2723 value = ast2obj_expr_context(o->v.Tuple.ctx);
2724 if (!value) goto failed;
2725 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 break;
2729 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002730 value = ast2obj_int(o->lineno);
2731 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002732 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2733 goto failed;
2734 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002735 value = ast2obj_int(o->col_offset);
2736 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002737 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2738 goto failed;
2739 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002740 return result;
2741failed:
2742 Py_XDECREF(value);
2743 Py_XDECREF(result);
2744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745}
2746
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002747PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002749 switch(o) {
2750 case Load:
2751 Py_INCREF(Load_singleton);
2752 return Load_singleton;
2753 case Store:
2754 Py_INCREF(Store_singleton);
2755 return Store_singleton;
2756 case Del:
2757 Py_INCREF(Del_singleton);
2758 return Del_singleton;
2759 case AugLoad:
2760 Py_INCREF(AugLoad_singleton);
2761 return AugLoad_singleton;
2762 case AugStore:
2763 Py_INCREF(AugStore_singleton);
2764 return AugStore_singleton;
2765 case Param:
2766 Py_INCREF(Param_singleton);
2767 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002768 default:
2769 /* should never happen, but just in case ... */
2770 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002774PyObject*
2775ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002777 slice_ty o = (slice_ty)_o;
2778 PyObject *result = NULL, *value = NULL;
2779 if (!o) {
2780 Py_INCREF(Py_None);
2781 return Py_None;
2782 }
2783
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 switch (o->kind) {
2785 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002786 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2787 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 break;
2789 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002790 result = PyType_GenericNew(Slice_type, NULL, NULL);
2791 if (!result) goto failed;
2792 value = ast2obj_expr(o->v.Slice.lower);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "lower", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
2797 value = ast2obj_expr(o->v.Slice.upper);
2798 if (!value) goto failed;
2799 if (PyObject_SetAttrString(result, "upper", value) == -1)
2800 goto failed;
2801 Py_DECREF(value);
2802 value = ast2obj_expr(o->v.Slice.step);
2803 if (!value) goto failed;
2804 if (PyObject_SetAttrString(result, "step", value) == -1)
2805 goto failed;
2806 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 break;
2808 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002809 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2810 if (!result) goto failed;
2811 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2812 if (!value) goto failed;
2813 if (PyObject_SetAttrString(result, "dims", value) == -1)
2814 goto failed;
2815 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 break;
2817 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002818 result = PyType_GenericNew(Index_type, NULL, NULL);
2819 if (!result) goto failed;
2820 value = ast2obj_expr(o->v.Index.value);
2821 if (!value) goto failed;
2822 if (PyObject_SetAttrString(result, "value", value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 break;
2826 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002827 return result;
2828failed:
2829 Py_XDECREF(value);
2830 Py_XDECREF(result);
2831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832}
2833
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002834PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002836 switch(o) {
2837 case And:
2838 Py_INCREF(And_singleton);
2839 return And_singleton;
2840 case Or:
2841 Py_INCREF(Or_singleton);
2842 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002843 default:
2844 /* should never happen, but just in case ... */
2845 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002848}
2849PyObject* ast2obj_operator(operator_ty o)
2850{
2851 switch(o) {
2852 case Add:
2853 Py_INCREF(Add_singleton);
2854 return Add_singleton;
2855 case Sub:
2856 Py_INCREF(Sub_singleton);
2857 return Sub_singleton;
2858 case Mult:
2859 Py_INCREF(Mult_singleton);
2860 return Mult_singleton;
2861 case Div:
2862 Py_INCREF(Div_singleton);
2863 return Div_singleton;
2864 case Mod:
2865 Py_INCREF(Mod_singleton);
2866 return Mod_singleton;
2867 case Pow:
2868 Py_INCREF(Pow_singleton);
2869 return Pow_singleton;
2870 case LShift:
2871 Py_INCREF(LShift_singleton);
2872 return LShift_singleton;
2873 case RShift:
2874 Py_INCREF(RShift_singleton);
2875 return RShift_singleton;
2876 case BitOr:
2877 Py_INCREF(BitOr_singleton);
2878 return BitOr_singleton;
2879 case BitXor:
2880 Py_INCREF(BitXor_singleton);
2881 return BitXor_singleton;
2882 case BitAnd:
2883 Py_INCREF(BitAnd_singleton);
2884 return BitAnd_singleton;
2885 case FloorDiv:
2886 Py_INCREF(FloorDiv_singleton);
2887 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002888 default:
2889 /* should never happen, but just in case ... */
2890 PyErr_Format(PyExc_SystemError, "unknown operator found");
2891 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002892 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002893}
2894PyObject* ast2obj_unaryop(unaryop_ty o)
2895{
2896 switch(o) {
2897 case Invert:
2898 Py_INCREF(Invert_singleton);
2899 return Invert_singleton;
2900 case Not:
2901 Py_INCREF(Not_singleton);
2902 return Not_singleton;
2903 case UAdd:
2904 Py_INCREF(UAdd_singleton);
2905 return UAdd_singleton;
2906 case USub:
2907 Py_INCREF(USub_singleton);
2908 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002909 default:
2910 /* should never happen, but just in case ... */
2911 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2912 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002913 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002914}
2915PyObject* ast2obj_cmpop(cmpop_ty o)
2916{
2917 switch(o) {
2918 case Eq:
2919 Py_INCREF(Eq_singleton);
2920 return Eq_singleton;
2921 case NotEq:
2922 Py_INCREF(NotEq_singleton);
2923 return NotEq_singleton;
2924 case Lt:
2925 Py_INCREF(Lt_singleton);
2926 return Lt_singleton;
2927 case LtE:
2928 Py_INCREF(LtE_singleton);
2929 return LtE_singleton;
2930 case Gt:
2931 Py_INCREF(Gt_singleton);
2932 return Gt_singleton;
2933 case GtE:
2934 Py_INCREF(GtE_singleton);
2935 return GtE_singleton;
2936 case Is:
2937 Py_INCREF(Is_singleton);
2938 return Is_singleton;
2939 case IsNot:
2940 Py_INCREF(IsNot_singleton);
2941 return IsNot_singleton;
2942 case In:
2943 Py_INCREF(In_singleton);
2944 return In_singleton;
2945 case NotIn:
2946 Py_INCREF(NotIn_singleton);
2947 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002948 default:
2949 /* should never happen, but just in case ... */
2950 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2951 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002952 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002953}
2954PyObject*
2955ast2obj_comprehension(void* _o)
2956{
2957 comprehension_ty o = (comprehension_ty)_o;
2958 PyObject *result = NULL, *value = NULL;
2959 if (!o) {
2960 Py_INCREF(Py_None);
2961 return Py_None;
2962 }
2963
2964 result = PyType_GenericNew(comprehension_type, NULL, NULL);
2965 if (!result) return NULL;
2966 value = ast2obj_expr(o->target);
2967 if (!value) goto failed;
2968 if (PyObject_SetAttrString(result, "target", value) == -1)
2969 goto failed;
2970 Py_DECREF(value);
2971 value = ast2obj_expr(o->iter);
2972 if (!value) goto failed;
2973 if (PyObject_SetAttrString(result, "iter", value) == -1)
2974 goto failed;
2975 Py_DECREF(value);
2976 value = ast2obj_list(o->ifs, ast2obj_expr);
2977 if (!value) goto failed;
2978 if (PyObject_SetAttrString(result, "ifs", value) == -1)
2979 goto failed;
2980 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002981 return result;
2982failed:
2983 Py_XDECREF(value);
2984 Py_XDECREF(result);
2985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986}
2987
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002988PyObject*
2989ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002991 excepthandler_ty o = (excepthandler_ty)_o;
2992 PyObject *result = NULL, *value = NULL;
2993 if (!o) {
2994 Py_INCREF(Py_None);
2995 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002997
Georg Brandla48f3ab2008-03-30 06:40:17 +00002998 switch (o->kind) {
2999 case ExceptHandler_kind:
3000 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3001 if (!result) goto failed;
3002 value = ast2obj_expr(o->v.ExceptHandler.type);
3003 if (!value) goto failed;
3004 if (PyObject_SetAttrString(result, "type", value) == -1)
3005 goto failed;
3006 Py_DECREF(value);
3007 value = ast2obj_expr(o->v.ExceptHandler.name);
3008 if (!value) goto failed;
3009 if (PyObject_SetAttrString(result, "name", value) == -1)
3010 goto failed;
3011 Py_DECREF(value);
3012 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3013 if (!value) goto failed;
3014 if (PyObject_SetAttrString(result, "body", value) == -1)
3015 goto failed;
3016 Py_DECREF(value);
3017 break;
3018 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003019 value = ast2obj_int(o->lineno);
3020 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003021 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003022 goto failed;
3023 Py_DECREF(value);
3024 value = ast2obj_int(o->col_offset);
3025 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003026 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003027 goto failed;
3028 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003029 return result;
3030failed:
3031 Py_XDECREF(value);
3032 Py_XDECREF(result);
3033 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034}
3035
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003036PyObject*
3037ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003039 arguments_ty o = (arguments_ty)_o;
3040 PyObject *result = NULL, *value = NULL;
3041 if (!o) {
3042 Py_INCREF(Py_None);
3043 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003045
3046 result = PyType_GenericNew(arguments_type, NULL, NULL);
3047 if (!result) return NULL;
3048 value = ast2obj_list(o->args, ast2obj_expr);
3049 if (!value) goto failed;
3050 if (PyObject_SetAttrString(result, "args", value) == -1)
3051 goto failed;
3052 Py_DECREF(value);
3053 value = ast2obj_identifier(o->vararg);
3054 if (!value) goto failed;
3055 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3056 goto failed;
3057 Py_DECREF(value);
3058 value = ast2obj_identifier(o->kwarg);
3059 if (!value) goto failed;
3060 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3061 goto failed;
3062 Py_DECREF(value);
3063 value = ast2obj_list(o->defaults, ast2obj_expr);
3064 if (!value) goto failed;
3065 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3066 goto failed;
3067 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003068 return result;
3069failed:
3070 Py_XDECREF(value);
3071 Py_XDECREF(result);
3072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073}
3074
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003075PyObject*
3076ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003078 keyword_ty o = (keyword_ty)_o;
3079 PyObject *result = NULL, *value = NULL;
3080 if (!o) {
3081 Py_INCREF(Py_None);
3082 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003084
3085 result = PyType_GenericNew(keyword_type, NULL, NULL);
3086 if (!result) return NULL;
3087 value = ast2obj_identifier(o->arg);
3088 if (!value) goto failed;
3089 if (PyObject_SetAttrString(result, "arg", value) == -1)
3090 goto failed;
3091 Py_DECREF(value);
3092 value = ast2obj_expr(o->value);
3093 if (!value) goto failed;
3094 if (PyObject_SetAttrString(result, "value", value) == -1)
3095 goto failed;
3096 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003097 return result;
3098failed:
3099 Py_XDECREF(value);
3100 Py_XDECREF(result);
3101 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102}
3103
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003104PyObject*
3105ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003107 alias_ty o = (alias_ty)_o;
3108 PyObject *result = NULL, *value = NULL;
3109 if (!o) {
3110 Py_INCREF(Py_None);
3111 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003113
3114 result = PyType_GenericNew(alias_type, NULL, NULL);
3115 if (!result) return NULL;
3116 value = ast2obj_identifier(o->name);
3117 if (!value) goto failed;
3118 if (PyObject_SetAttrString(result, "name", value) == -1)
3119 goto failed;
3120 Py_DECREF(value);
3121 value = ast2obj_identifier(o->asname);
3122 if (!value) goto failed;
3123 if (PyObject_SetAttrString(result, "asname", value) == -1)
3124 goto failed;
3125 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003126 return result;
3127failed:
3128 Py_XDECREF(value);
3129 Py_XDECREF(result);
3130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131}
3132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133
Georg Brandlfc8eef32008-03-28 12:11:56 +00003134int
3135obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3136{
3137 PyObject* tmp = NULL;
3138
3139
3140 if (obj == Py_None) {
3141 *out = NULL;
3142 return 0;
3143 }
3144 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3145 asdl_seq* body;
3146
3147 if (PyObject_HasAttrString(obj, "body")) {
3148 int res;
3149 Py_ssize_t len;
3150 Py_ssize_t i;
3151 tmp = PyObject_GetAttrString(obj, "body");
3152 if (tmp == NULL) goto failed;
3153 if (!PyList_Check(tmp)) {
3154 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3155 goto failed;
3156 }
3157 len = PyList_GET_SIZE(tmp);
3158 body = asdl_seq_new(len, arena);
3159 if (body == NULL) goto failed;
3160 for (i = 0; i < len; i++) {
3161 stmt_ty value;
3162 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3163 if (res != 0) goto failed;
3164 asdl_seq_SET(body, i, value);
3165 }
3166 Py_XDECREF(tmp);
3167 tmp = NULL;
3168 } else {
3169 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3170 return 1;
3171 }
3172 *out = Module(body, arena);
3173 if (*out == NULL) goto failed;
3174 return 0;
3175 }
3176 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3177 asdl_seq* body;
3178
3179 if (PyObject_HasAttrString(obj, "body")) {
3180 int res;
3181 Py_ssize_t len;
3182 Py_ssize_t i;
3183 tmp = PyObject_GetAttrString(obj, "body");
3184 if (tmp == NULL) goto failed;
3185 if (!PyList_Check(tmp)) {
3186 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3187 goto failed;
3188 }
3189 len = PyList_GET_SIZE(tmp);
3190 body = asdl_seq_new(len, arena);
3191 if (body == NULL) goto failed;
3192 for (i = 0; i < len; i++) {
3193 stmt_ty value;
3194 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3195 if (res != 0) goto failed;
3196 asdl_seq_SET(body, i, value);
3197 }
3198 Py_XDECREF(tmp);
3199 tmp = NULL;
3200 } else {
3201 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3202 return 1;
3203 }
3204 *out = Interactive(body, arena);
3205 if (*out == NULL) goto failed;
3206 return 0;
3207 }
3208 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3209 expr_ty body;
3210
3211 if (PyObject_HasAttrString(obj, "body")) {
3212 int res;
3213 tmp = PyObject_GetAttrString(obj, "body");
3214 if (tmp == NULL) goto failed;
3215 res = obj2ast_expr(tmp, &body, arena);
3216 if (res != 0) goto failed;
3217 Py_XDECREF(tmp);
3218 tmp = NULL;
3219 } else {
3220 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3221 return 1;
3222 }
3223 *out = Expression(body, arena);
3224 if (*out == NULL) goto failed;
3225 return 0;
3226 }
3227 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3228 asdl_seq* body;
3229
3230 if (PyObject_HasAttrString(obj, "body")) {
3231 int res;
3232 Py_ssize_t len;
3233 Py_ssize_t i;
3234 tmp = PyObject_GetAttrString(obj, "body");
3235 if (tmp == NULL) goto failed;
3236 if (!PyList_Check(tmp)) {
3237 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3238 goto failed;
3239 }
3240 len = PyList_GET_SIZE(tmp);
3241 body = asdl_seq_new(len, arena);
3242 if (body == NULL) goto failed;
3243 for (i = 0; i < len; i++) {
3244 stmt_ty value;
3245 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3246 if (res != 0) goto failed;
3247 asdl_seq_SET(body, i, value);
3248 }
3249 Py_XDECREF(tmp);
3250 tmp = NULL;
3251 } else {
3252 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3253 return 1;
3254 }
3255 *out = Suite(body, arena);
3256 if (*out == NULL) goto failed;
3257 return 0;
3258 }
3259
3260 tmp = PyObject_Repr(obj);
3261 if (tmp == NULL) goto failed;
3262 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3263failed:
3264 Py_XDECREF(tmp);
3265 return 1;
3266}
3267
3268int
3269obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3270{
3271 PyObject* tmp = NULL;
3272
3273 int lineno;
3274 int col_offset;
3275
3276 if (obj == Py_None) {
3277 *out = NULL;
3278 return 0;
3279 }
3280 if (PyObject_HasAttrString(obj, "lineno")) {
3281 int res;
3282 tmp = PyObject_GetAttrString(obj, "lineno");
3283 if (tmp == NULL) goto failed;
3284 res = obj2ast_int(tmp, &lineno, arena);
3285 if (res != 0) goto failed;
3286 Py_XDECREF(tmp);
3287 tmp = NULL;
3288 } else {
3289 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3290 return 1;
3291 }
3292 if (PyObject_HasAttrString(obj, "col_offset")) {
3293 int res;
3294 tmp = PyObject_GetAttrString(obj, "col_offset");
3295 if (tmp == NULL) goto failed;
3296 res = obj2ast_int(tmp, &col_offset, arena);
3297 if (res != 0) goto failed;
3298 Py_XDECREF(tmp);
3299 tmp = NULL;
3300 } else {
3301 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3302 return 1;
3303 }
3304 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3305 identifier name;
3306 arguments_ty args;
3307 asdl_seq* body;
3308 asdl_seq* decorator_list;
3309
3310 if (PyObject_HasAttrString(obj, "name")) {
3311 int res;
3312 tmp = PyObject_GetAttrString(obj, "name");
3313 if (tmp == NULL) goto failed;
3314 res = obj2ast_identifier(tmp, &name, arena);
3315 if (res != 0) goto failed;
3316 Py_XDECREF(tmp);
3317 tmp = NULL;
3318 } else {
3319 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3320 return 1;
3321 }
3322 if (PyObject_HasAttrString(obj, "args")) {
3323 int res;
3324 tmp = PyObject_GetAttrString(obj, "args");
3325 if (tmp == NULL) goto failed;
3326 res = obj2ast_arguments(tmp, &args, arena);
3327 if (res != 0) goto failed;
3328 Py_XDECREF(tmp);
3329 tmp = NULL;
3330 } else {
3331 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3332 return 1;
3333 }
3334 if (PyObject_HasAttrString(obj, "body")) {
3335 int res;
3336 Py_ssize_t len;
3337 Py_ssize_t i;
3338 tmp = PyObject_GetAttrString(obj, "body");
3339 if (tmp == NULL) goto failed;
3340 if (!PyList_Check(tmp)) {
3341 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3342 goto failed;
3343 }
3344 len = PyList_GET_SIZE(tmp);
3345 body = asdl_seq_new(len, arena);
3346 if (body == NULL) goto failed;
3347 for (i = 0; i < len; i++) {
3348 stmt_ty value;
3349 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3350 if (res != 0) goto failed;
3351 asdl_seq_SET(body, i, value);
3352 }
3353 Py_XDECREF(tmp);
3354 tmp = NULL;
3355 } else {
3356 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3357 return 1;
3358 }
3359 if (PyObject_HasAttrString(obj, "decorator_list")) {
3360 int res;
3361 Py_ssize_t len;
3362 Py_ssize_t i;
3363 tmp = PyObject_GetAttrString(obj, "decorator_list");
3364 if (tmp == NULL) goto failed;
3365 if (!PyList_Check(tmp)) {
3366 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3367 goto failed;
3368 }
3369 len = PyList_GET_SIZE(tmp);
3370 decorator_list = asdl_seq_new(len, arena);
3371 if (decorator_list == NULL) goto failed;
3372 for (i = 0; i < len; i++) {
3373 expr_ty value;
3374 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3375 if (res != 0) goto failed;
3376 asdl_seq_SET(decorator_list, i, value);
3377 }
3378 Py_XDECREF(tmp);
3379 tmp = NULL;
3380 } else {
3381 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3382 return 1;
3383 }
3384 *out = FunctionDef(name, args, body, decorator_list, lineno,
3385 col_offset, arena);
3386 if (*out == NULL) goto failed;
3387 return 0;
3388 }
3389 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3390 identifier name;
3391 asdl_seq* bases;
3392 asdl_seq* body;
3393 asdl_seq* decorator_list;
3394
3395 if (PyObject_HasAttrString(obj, "name")) {
3396 int res;
3397 tmp = PyObject_GetAttrString(obj, "name");
3398 if (tmp == NULL) goto failed;
3399 res = obj2ast_identifier(tmp, &name, arena);
3400 if (res != 0) goto failed;
3401 Py_XDECREF(tmp);
3402 tmp = NULL;
3403 } else {
3404 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3405 return 1;
3406 }
3407 if (PyObject_HasAttrString(obj, "bases")) {
3408 int res;
3409 Py_ssize_t len;
3410 Py_ssize_t i;
3411 tmp = PyObject_GetAttrString(obj, "bases");
3412 if (tmp == NULL) goto failed;
3413 if (!PyList_Check(tmp)) {
3414 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3415 goto failed;
3416 }
3417 len = PyList_GET_SIZE(tmp);
3418 bases = asdl_seq_new(len, arena);
3419 if (bases == NULL) goto failed;
3420 for (i = 0; i < len; i++) {
3421 expr_ty value;
3422 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3423 if (res != 0) goto failed;
3424 asdl_seq_SET(bases, i, value);
3425 }
3426 Py_XDECREF(tmp);
3427 tmp = NULL;
3428 } else {
3429 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3430 return 1;
3431 }
3432 if (PyObject_HasAttrString(obj, "body")) {
3433 int res;
3434 Py_ssize_t len;
3435 Py_ssize_t i;
3436 tmp = PyObject_GetAttrString(obj, "body");
3437 if (tmp == NULL) goto failed;
3438 if (!PyList_Check(tmp)) {
3439 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3440 goto failed;
3441 }
3442 len = PyList_GET_SIZE(tmp);
3443 body = asdl_seq_new(len, arena);
3444 if (body == NULL) goto failed;
3445 for (i = 0; i < len; i++) {
3446 stmt_ty value;
3447 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3448 if (res != 0) goto failed;
3449 asdl_seq_SET(body, i, value);
3450 }
3451 Py_XDECREF(tmp);
3452 tmp = NULL;
3453 } else {
3454 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3455 return 1;
3456 }
3457 if (PyObject_HasAttrString(obj, "decorator_list")) {
3458 int res;
3459 Py_ssize_t len;
3460 Py_ssize_t i;
3461 tmp = PyObject_GetAttrString(obj, "decorator_list");
3462 if (tmp == NULL) goto failed;
3463 if (!PyList_Check(tmp)) {
3464 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3465 goto failed;
3466 }
3467 len = PyList_GET_SIZE(tmp);
3468 decorator_list = asdl_seq_new(len, arena);
3469 if (decorator_list == NULL) goto failed;
3470 for (i = 0; i < len; i++) {
3471 expr_ty value;
3472 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3473 if (res != 0) goto failed;
3474 asdl_seq_SET(decorator_list, i, value);
3475 }
3476 Py_XDECREF(tmp);
3477 tmp = NULL;
3478 } else {
3479 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3480 return 1;
3481 }
3482 *out = ClassDef(name, bases, body, decorator_list, lineno,
3483 col_offset, arena);
3484 if (*out == NULL) goto failed;
3485 return 0;
3486 }
3487 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3488 expr_ty value;
3489
3490 if (PyObject_HasAttrString(obj, "value")) {
3491 int res;
3492 tmp = PyObject_GetAttrString(obj, "value");
3493 if (tmp == NULL) goto failed;
3494 res = obj2ast_expr(tmp, &value, arena);
3495 if (res != 0) goto failed;
3496 Py_XDECREF(tmp);
3497 tmp = NULL;
3498 } else {
3499 value = NULL;
3500 }
3501 *out = Return(value, lineno, col_offset, arena);
3502 if (*out == NULL) goto failed;
3503 return 0;
3504 }
3505 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3506 asdl_seq* targets;
3507
3508 if (PyObject_HasAttrString(obj, "targets")) {
3509 int res;
3510 Py_ssize_t len;
3511 Py_ssize_t i;
3512 tmp = PyObject_GetAttrString(obj, "targets");
3513 if (tmp == NULL) goto failed;
3514 if (!PyList_Check(tmp)) {
3515 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3516 goto failed;
3517 }
3518 len = PyList_GET_SIZE(tmp);
3519 targets = asdl_seq_new(len, arena);
3520 if (targets == NULL) goto failed;
3521 for (i = 0; i < len; i++) {
3522 expr_ty value;
3523 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3524 if (res != 0) goto failed;
3525 asdl_seq_SET(targets, i, value);
3526 }
3527 Py_XDECREF(tmp);
3528 tmp = NULL;
3529 } else {
3530 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3531 return 1;
3532 }
3533 *out = Delete(targets, lineno, col_offset, arena);
3534 if (*out == NULL) goto failed;
3535 return 0;
3536 }
3537 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3538 asdl_seq* targets;
3539 expr_ty value;
3540
3541 if (PyObject_HasAttrString(obj, "targets")) {
3542 int res;
3543 Py_ssize_t len;
3544 Py_ssize_t i;
3545 tmp = PyObject_GetAttrString(obj, "targets");
3546 if (tmp == NULL) goto failed;
3547 if (!PyList_Check(tmp)) {
3548 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3549 goto failed;
3550 }
3551 len = PyList_GET_SIZE(tmp);
3552 targets = asdl_seq_new(len, arena);
3553 if (targets == NULL) goto failed;
3554 for (i = 0; i < len; i++) {
3555 expr_ty value;
3556 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3557 if (res != 0) goto failed;
3558 asdl_seq_SET(targets, i, value);
3559 }
3560 Py_XDECREF(tmp);
3561 tmp = NULL;
3562 } else {
3563 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3564 return 1;
3565 }
3566 if (PyObject_HasAttrString(obj, "value")) {
3567 int res;
3568 tmp = PyObject_GetAttrString(obj, "value");
3569 if (tmp == NULL) goto failed;
3570 res = obj2ast_expr(tmp, &value, arena);
3571 if (res != 0) goto failed;
3572 Py_XDECREF(tmp);
3573 tmp = NULL;
3574 } else {
3575 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3576 return 1;
3577 }
3578 *out = Assign(targets, value, lineno, col_offset, arena);
3579 if (*out == NULL) goto failed;
3580 return 0;
3581 }
3582 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3583 expr_ty target;
3584 operator_ty op;
3585 expr_ty value;
3586
3587 if (PyObject_HasAttrString(obj, "target")) {
3588 int res;
3589 tmp = PyObject_GetAttrString(obj, "target");
3590 if (tmp == NULL) goto failed;
3591 res = obj2ast_expr(tmp, &target, arena);
3592 if (res != 0) goto failed;
3593 Py_XDECREF(tmp);
3594 tmp = NULL;
3595 } else {
3596 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3597 return 1;
3598 }
3599 if (PyObject_HasAttrString(obj, "op")) {
3600 int res;
3601 tmp = PyObject_GetAttrString(obj, "op");
3602 if (tmp == NULL) goto failed;
3603 res = obj2ast_operator(tmp, &op, arena);
3604 if (res != 0) goto failed;
3605 Py_XDECREF(tmp);
3606 tmp = NULL;
3607 } else {
3608 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3609 return 1;
3610 }
3611 if (PyObject_HasAttrString(obj, "value")) {
3612 int res;
3613 tmp = PyObject_GetAttrString(obj, "value");
3614 if (tmp == NULL) goto failed;
3615 res = obj2ast_expr(tmp, &value, arena);
3616 if (res != 0) goto failed;
3617 Py_XDECREF(tmp);
3618 tmp = NULL;
3619 } else {
3620 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3621 return 1;
3622 }
3623 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3624 if (*out == NULL) goto failed;
3625 return 0;
3626 }
3627 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3628 expr_ty dest;
3629 asdl_seq* values;
3630 bool nl;
3631
3632 if (PyObject_HasAttrString(obj, "dest")) {
3633 int res;
3634 tmp = PyObject_GetAttrString(obj, "dest");
3635 if (tmp == NULL) goto failed;
3636 res = obj2ast_expr(tmp, &dest, arena);
3637 if (res != 0) goto failed;
3638 Py_XDECREF(tmp);
3639 tmp = NULL;
3640 } else {
3641 dest = NULL;
3642 }
3643 if (PyObject_HasAttrString(obj, "values")) {
3644 int res;
3645 Py_ssize_t len;
3646 Py_ssize_t i;
3647 tmp = PyObject_GetAttrString(obj, "values");
3648 if (tmp == NULL) goto failed;
3649 if (!PyList_Check(tmp)) {
3650 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3651 goto failed;
3652 }
3653 len = PyList_GET_SIZE(tmp);
3654 values = asdl_seq_new(len, arena);
3655 if (values == NULL) goto failed;
3656 for (i = 0; i < len; i++) {
3657 expr_ty value;
3658 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3659 if (res != 0) goto failed;
3660 asdl_seq_SET(values, i, value);
3661 }
3662 Py_XDECREF(tmp);
3663 tmp = NULL;
3664 } else {
3665 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3666 return 1;
3667 }
3668 if (PyObject_HasAttrString(obj, "nl")) {
3669 int res;
3670 tmp = PyObject_GetAttrString(obj, "nl");
3671 if (tmp == NULL) goto failed;
3672 res = obj2ast_bool(tmp, &nl, arena);
3673 if (res != 0) goto failed;
3674 Py_XDECREF(tmp);
3675 tmp = NULL;
3676 } else {
3677 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3678 return 1;
3679 }
3680 *out = Print(dest, values, nl, lineno, col_offset, arena);
3681 if (*out == NULL) goto failed;
3682 return 0;
3683 }
3684 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3685 expr_ty target;
3686 expr_ty iter;
3687 asdl_seq* body;
3688 asdl_seq* orelse;
3689
3690 if (PyObject_HasAttrString(obj, "target")) {
3691 int res;
3692 tmp = PyObject_GetAttrString(obj, "target");
3693 if (tmp == NULL) goto failed;
3694 res = obj2ast_expr(tmp, &target, arena);
3695 if (res != 0) goto failed;
3696 Py_XDECREF(tmp);
3697 tmp = NULL;
3698 } else {
3699 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3700 return 1;
3701 }
3702 if (PyObject_HasAttrString(obj, "iter")) {
3703 int res;
3704 tmp = PyObject_GetAttrString(obj, "iter");
3705 if (tmp == NULL) goto failed;
3706 res = obj2ast_expr(tmp, &iter, arena);
3707 if (res != 0) goto failed;
3708 Py_XDECREF(tmp);
3709 tmp = NULL;
3710 } else {
3711 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3712 return 1;
3713 }
3714 if (PyObject_HasAttrString(obj, "body")) {
3715 int res;
3716 Py_ssize_t len;
3717 Py_ssize_t i;
3718 tmp = PyObject_GetAttrString(obj, "body");
3719 if (tmp == NULL) goto failed;
3720 if (!PyList_Check(tmp)) {
3721 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3722 goto failed;
3723 }
3724 len = PyList_GET_SIZE(tmp);
3725 body = asdl_seq_new(len, arena);
3726 if (body == NULL) goto failed;
3727 for (i = 0; i < len; i++) {
3728 stmt_ty value;
3729 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3730 if (res != 0) goto failed;
3731 asdl_seq_SET(body, i, value);
3732 }
3733 Py_XDECREF(tmp);
3734 tmp = NULL;
3735 } else {
3736 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3737 return 1;
3738 }
3739 if (PyObject_HasAttrString(obj, "orelse")) {
3740 int res;
3741 Py_ssize_t len;
3742 Py_ssize_t i;
3743 tmp = PyObject_GetAttrString(obj, "orelse");
3744 if (tmp == NULL) goto failed;
3745 if (!PyList_Check(tmp)) {
3746 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3747 goto failed;
3748 }
3749 len = PyList_GET_SIZE(tmp);
3750 orelse = asdl_seq_new(len, arena);
3751 if (orelse == NULL) goto failed;
3752 for (i = 0; i < len; i++) {
3753 stmt_ty value;
3754 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3755 if (res != 0) goto failed;
3756 asdl_seq_SET(orelse, i, value);
3757 }
3758 Py_XDECREF(tmp);
3759 tmp = NULL;
3760 } else {
3761 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3762 return 1;
3763 }
3764 *out = For(target, iter, body, orelse, lineno, col_offset,
3765 arena);
3766 if (*out == NULL) goto failed;
3767 return 0;
3768 }
3769 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3770 expr_ty test;
3771 asdl_seq* body;
3772 asdl_seq* orelse;
3773
3774 if (PyObject_HasAttrString(obj, "test")) {
3775 int res;
3776 tmp = PyObject_GetAttrString(obj, "test");
3777 if (tmp == NULL) goto failed;
3778 res = obj2ast_expr(tmp, &test, arena);
3779 if (res != 0) goto failed;
3780 Py_XDECREF(tmp);
3781 tmp = NULL;
3782 } else {
3783 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3784 return 1;
3785 }
3786 if (PyObject_HasAttrString(obj, "body")) {
3787 int res;
3788 Py_ssize_t len;
3789 Py_ssize_t i;
3790 tmp = PyObject_GetAttrString(obj, "body");
3791 if (tmp == NULL) goto failed;
3792 if (!PyList_Check(tmp)) {
3793 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3794 goto failed;
3795 }
3796 len = PyList_GET_SIZE(tmp);
3797 body = asdl_seq_new(len, arena);
3798 if (body == NULL) goto failed;
3799 for (i = 0; i < len; i++) {
3800 stmt_ty value;
3801 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3802 if (res != 0) goto failed;
3803 asdl_seq_SET(body, i, value);
3804 }
3805 Py_XDECREF(tmp);
3806 tmp = NULL;
3807 } else {
3808 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3809 return 1;
3810 }
3811 if (PyObject_HasAttrString(obj, "orelse")) {
3812 int res;
3813 Py_ssize_t len;
3814 Py_ssize_t i;
3815 tmp = PyObject_GetAttrString(obj, "orelse");
3816 if (tmp == NULL) goto failed;
3817 if (!PyList_Check(tmp)) {
3818 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3819 goto failed;
3820 }
3821 len = PyList_GET_SIZE(tmp);
3822 orelse = asdl_seq_new(len, arena);
3823 if (orelse == NULL) goto failed;
3824 for (i = 0; i < len; i++) {
3825 stmt_ty value;
3826 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3827 if (res != 0) goto failed;
3828 asdl_seq_SET(orelse, i, value);
3829 }
3830 Py_XDECREF(tmp);
3831 tmp = NULL;
3832 } else {
3833 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3834 return 1;
3835 }
3836 *out = While(test, body, orelse, lineno, col_offset, arena);
3837 if (*out == NULL) goto failed;
3838 return 0;
3839 }
3840 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3841 expr_ty test;
3842 asdl_seq* body;
3843 asdl_seq* orelse;
3844
3845 if (PyObject_HasAttrString(obj, "test")) {
3846 int res;
3847 tmp = PyObject_GetAttrString(obj, "test");
3848 if (tmp == NULL) goto failed;
3849 res = obj2ast_expr(tmp, &test, arena);
3850 if (res != 0) goto failed;
3851 Py_XDECREF(tmp);
3852 tmp = NULL;
3853 } else {
3854 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3855 return 1;
3856 }
3857 if (PyObject_HasAttrString(obj, "body")) {
3858 int res;
3859 Py_ssize_t len;
3860 Py_ssize_t i;
3861 tmp = PyObject_GetAttrString(obj, "body");
3862 if (tmp == NULL) goto failed;
3863 if (!PyList_Check(tmp)) {
3864 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3865 goto failed;
3866 }
3867 len = PyList_GET_SIZE(tmp);
3868 body = asdl_seq_new(len, arena);
3869 if (body == NULL) goto failed;
3870 for (i = 0; i < len; i++) {
3871 stmt_ty value;
3872 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3873 if (res != 0) goto failed;
3874 asdl_seq_SET(body, i, value);
3875 }
3876 Py_XDECREF(tmp);
3877 tmp = NULL;
3878 } else {
3879 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3880 return 1;
3881 }
3882 if (PyObject_HasAttrString(obj, "orelse")) {
3883 int res;
3884 Py_ssize_t len;
3885 Py_ssize_t i;
3886 tmp = PyObject_GetAttrString(obj, "orelse");
3887 if (tmp == NULL) goto failed;
3888 if (!PyList_Check(tmp)) {
3889 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3890 goto failed;
3891 }
3892 len = PyList_GET_SIZE(tmp);
3893 orelse = asdl_seq_new(len, arena);
3894 if (orelse == NULL) goto failed;
3895 for (i = 0; i < len; i++) {
3896 stmt_ty value;
3897 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3898 if (res != 0) goto failed;
3899 asdl_seq_SET(orelse, i, value);
3900 }
3901 Py_XDECREF(tmp);
3902 tmp = NULL;
3903 } else {
3904 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3905 return 1;
3906 }
3907 *out = If(test, body, orelse, lineno, col_offset, arena);
3908 if (*out == NULL) goto failed;
3909 return 0;
3910 }
3911 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3912 expr_ty context_expr;
3913 expr_ty optional_vars;
3914 asdl_seq* body;
3915
3916 if (PyObject_HasAttrString(obj, "context_expr")) {
3917 int res;
3918 tmp = PyObject_GetAttrString(obj, "context_expr");
3919 if (tmp == NULL) goto failed;
3920 res = obj2ast_expr(tmp, &context_expr, arena);
3921 if (res != 0) goto failed;
3922 Py_XDECREF(tmp);
3923 tmp = NULL;
3924 } else {
3925 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3926 return 1;
3927 }
3928 if (PyObject_HasAttrString(obj, "optional_vars")) {
3929 int res;
3930 tmp = PyObject_GetAttrString(obj, "optional_vars");
3931 if (tmp == NULL) goto failed;
3932 res = obj2ast_expr(tmp, &optional_vars, arena);
3933 if (res != 0) goto failed;
3934 Py_XDECREF(tmp);
3935 tmp = NULL;
3936 } else {
3937 optional_vars = NULL;
3938 }
3939 if (PyObject_HasAttrString(obj, "body")) {
3940 int res;
3941 Py_ssize_t len;
3942 Py_ssize_t i;
3943 tmp = PyObject_GetAttrString(obj, "body");
3944 if (tmp == NULL) goto failed;
3945 if (!PyList_Check(tmp)) {
3946 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3947 goto failed;
3948 }
3949 len = PyList_GET_SIZE(tmp);
3950 body = asdl_seq_new(len, arena);
3951 if (body == NULL) goto failed;
3952 for (i = 0; i < len; i++) {
3953 stmt_ty value;
3954 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3955 if (res != 0) goto failed;
3956 asdl_seq_SET(body, i, value);
3957 }
3958 Py_XDECREF(tmp);
3959 tmp = NULL;
3960 } else {
3961 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
3962 return 1;
3963 }
3964 *out = With(context_expr, optional_vars, body, lineno,
3965 col_offset, arena);
3966 if (*out == NULL) goto failed;
3967 return 0;
3968 }
3969 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
3970 expr_ty type;
3971 expr_ty inst;
3972 expr_ty tback;
3973
3974 if (PyObject_HasAttrString(obj, "type")) {
3975 int res;
3976 tmp = PyObject_GetAttrString(obj, "type");
3977 if (tmp == NULL) goto failed;
3978 res = obj2ast_expr(tmp, &type, arena);
3979 if (res != 0) goto failed;
3980 Py_XDECREF(tmp);
3981 tmp = NULL;
3982 } else {
3983 type = NULL;
3984 }
3985 if (PyObject_HasAttrString(obj, "inst")) {
3986 int res;
3987 tmp = PyObject_GetAttrString(obj, "inst");
3988 if (tmp == NULL) goto failed;
3989 res = obj2ast_expr(tmp, &inst, arena);
3990 if (res != 0) goto failed;
3991 Py_XDECREF(tmp);
3992 tmp = NULL;
3993 } else {
3994 inst = NULL;
3995 }
3996 if (PyObject_HasAttrString(obj, "tback")) {
3997 int res;
3998 tmp = PyObject_GetAttrString(obj, "tback");
3999 if (tmp == NULL) goto failed;
4000 res = obj2ast_expr(tmp, &tback, arena);
4001 if (res != 0) goto failed;
4002 Py_XDECREF(tmp);
4003 tmp = NULL;
4004 } else {
4005 tback = NULL;
4006 }
4007 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4008 if (*out == NULL) goto failed;
4009 return 0;
4010 }
4011 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4012 asdl_seq* body;
4013 asdl_seq* handlers;
4014 asdl_seq* orelse;
4015
4016 if (PyObject_HasAttrString(obj, "body")) {
4017 int res;
4018 Py_ssize_t len;
4019 Py_ssize_t i;
4020 tmp = PyObject_GetAttrString(obj, "body");
4021 if (tmp == NULL) goto failed;
4022 if (!PyList_Check(tmp)) {
4023 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4024 goto failed;
4025 }
4026 len = PyList_GET_SIZE(tmp);
4027 body = asdl_seq_new(len, arena);
4028 if (body == NULL) goto failed;
4029 for (i = 0; i < len; i++) {
4030 stmt_ty value;
4031 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4032 if (res != 0) goto failed;
4033 asdl_seq_SET(body, i, value);
4034 }
4035 Py_XDECREF(tmp);
4036 tmp = NULL;
4037 } else {
4038 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4039 return 1;
4040 }
4041 if (PyObject_HasAttrString(obj, "handlers")) {
4042 int res;
4043 Py_ssize_t len;
4044 Py_ssize_t i;
4045 tmp = PyObject_GetAttrString(obj, "handlers");
4046 if (tmp == NULL) goto failed;
4047 if (!PyList_Check(tmp)) {
4048 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4049 goto failed;
4050 }
4051 len = PyList_GET_SIZE(tmp);
4052 handlers = asdl_seq_new(len, arena);
4053 if (handlers == NULL) goto failed;
4054 for (i = 0; i < len; i++) {
4055 excepthandler_ty value;
4056 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4057 if (res != 0) goto failed;
4058 asdl_seq_SET(handlers, i, value);
4059 }
4060 Py_XDECREF(tmp);
4061 tmp = NULL;
4062 } else {
4063 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4064 return 1;
4065 }
4066 if (PyObject_HasAttrString(obj, "orelse")) {
4067 int res;
4068 Py_ssize_t len;
4069 Py_ssize_t i;
4070 tmp = PyObject_GetAttrString(obj, "orelse");
4071 if (tmp == NULL) goto failed;
4072 if (!PyList_Check(tmp)) {
4073 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4074 goto failed;
4075 }
4076 len = PyList_GET_SIZE(tmp);
4077 orelse = asdl_seq_new(len, arena);
4078 if (orelse == NULL) goto failed;
4079 for (i = 0; i < len; i++) {
4080 stmt_ty value;
4081 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4082 if (res != 0) goto failed;
4083 asdl_seq_SET(orelse, i, value);
4084 }
4085 Py_XDECREF(tmp);
4086 tmp = NULL;
4087 } else {
4088 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4089 return 1;
4090 }
4091 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4092 arena);
4093 if (*out == NULL) goto failed;
4094 return 0;
4095 }
4096 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4097 asdl_seq* body;
4098 asdl_seq* finalbody;
4099
4100 if (PyObject_HasAttrString(obj, "body")) {
4101 int res;
4102 Py_ssize_t len;
4103 Py_ssize_t i;
4104 tmp = PyObject_GetAttrString(obj, "body");
4105 if (tmp == NULL) goto failed;
4106 if (!PyList_Check(tmp)) {
4107 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4108 goto failed;
4109 }
4110 len = PyList_GET_SIZE(tmp);
4111 body = asdl_seq_new(len, arena);
4112 if (body == NULL) goto failed;
4113 for (i = 0; i < len; i++) {
4114 stmt_ty value;
4115 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4116 if (res != 0) goto failed;
4117 asdl_seq_SET(body, i, value);
4118 }
4119 Py_XDECREF(tmp);
4120 tmp = NULL;
4121 } else {
4122 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4123 return 1;
4124 }
4125 if (PyObject_HasAttrString(obj, "finalbody")) {
4126 int res;
4127 Py_ssize_t len;
4128 Py_ssize_t i;
4129 tmp = PyObject_GetAttrString(obj, "finalbody");
4130 if (tmp == NULL) goto failed;
4131 if (!PyList_Check(tmp)) {
4132 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4133 goto failed;
4134 }
4135 len = PyList_GET_SIZE(tmp);
4136 finalbody = asdl_seq_new(len, arena);
4137 if (finalbody == NULL) goto failed;
4138 for (i = 0; i < len; i++) {
4139 stmt_ty value;
4140 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4141 if (res != 0) goto failed;
4142 asdl_seq_SET(finalbody, i, value);
4143 }
4144 Py_XDECREF(tmp);
4145 tmp = NULL;
4146 } else {
4147 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4148 return 1;
4149 }
4150 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4151 if (*out == NULL) goto failed;
4152 return 0;
4153 }
4154 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4155 expr_ty test;
4156 expr_ty msg;
4157
4158 if (PyObject_HasAttrString(obj, "test")) {
4159 int res;
4160 tmp = PyObject_GetAttrString(obj, "test");
4161 if (tmp == NULL) goto failed;
4162 res = obj2ast_expr(tmp, &test, arena);
4163 if (res != 0) goto failed;
4164 Py_XDECREF(tmp);
4165 tmp = NULL;
4166 } else {
4167 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4168 return 1;
4169 }
4170 if (PyObject_HasAttrString(obj, "msg")) {
4171 int res;
4172 tmp = PyObject_GetAttrString(obj, "msg");
4173 if (tmp == NULL) goto failed;
4174 res = obj2ast_expr(tmp, &msg, arena);
4175 if (res != 0) goto failed;
4176 Py_XDECREF(tmp);
4177 tmp = NULL;
4178 } else {
4179 msg = NULL;
4180 }
4181 *out = Assert(test, msg, lineno, col_offset, arena);
4182 if (*out == NULL) goto failed;
4183 return 0;
4184 }
4185 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4186 asdl_seq* names;
4187
4188 if (PyObject_HasAttrString(obj, "names")) {
4189 int res;
4190 Py_ssize_t len;
4191 Py_ssize_t i;
4192 tmp = PyObject_GetAttrString(obj, "names");
4193 if (tmp == NULL) goto failed;
4194 if (!PyList_Check(tmp)) {
4195 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4196 goto failed;
4197 }
4198 len = PyList_GET_SIZE(tmp);
4199 names = asdl_seq_new(len, arena);
4200 if (names == NULL) goto failed;
4201 for (i = 0; i < len; i++) {
4202 alias_ty value;
4203 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4204 if (res != 0) goto failed;
4205 asdl_seq_SET(names, i, value);
4206 }
4207 Py_XDECREF(tmp);
4208 tmp = NULL;
4209 } else {
4210 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4211 return 1;
4212 }
4213 *out = Import(names, lineno, col_offset, arena);
4214 if (*out == NULL) goto failed;
4215 return 0;
4216 }
4217 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4218 identifier module;
4219 asdl_seq* names;
4220 int level;
4221
4222 if (PyObject_HasAttrString(obj, "module")) {
4223 int res;
4224 tmp = PyObject_GetAttrString(obj, "module");
4225 if (tmp == NULL) goto failed;
4226 res = obj2ast_identifier(tmp, &module, arena);
4227 if (res != 0) goto failed;
4228 Py_XDECREF(tmp);
4229 tmp = NULL;
4230 } else {
4231 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4232 return 1;
4233 }
4234 if (PyObject_HasAttrString(obj, "names")) {
4235 int res;
4236 Py_ssize_t len;
4237 Py_ssize_t i;
4238 tmp = PyObject_GetAttrString(obj, "names");
4239 if (tmp == NULL) goto failed;
4240 if (!PyList_Check(tmp)) {
4241 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4242 goto failed;
4243 }
4244 len = PyList_GET_SIZE(tmp);
4245 names = asdl_seq_new(len, arena);
4246 if (names == NULL) goto failed;
4247 for (i = 0; i < len; i++) {
4248 alias_ty value;
4249 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4250 if (res != 0) goto failed;
4251 asdl_seq_SET(names, i, value);
4252 }
4253 Py_XDECREF(tmp);
4254 tmp = NULL;
4255 } else {
4256 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4257 return 1;
4258 }
4259 if (PyObject_HasAttrString(obj, "level")) {
4260 int res;
4261 tmp = PyObject_GetAttrString(obj, "level");
4262 if (tmp == NULL) goto failed;
4263 res = obj2ast_int(tmp, &level, arena);
4264 if (res != 0) goto failed;
4265 Py_XDECREF(tmp);
4266 tmp = NULL;
4267 } else {
4268 level = 0;
4269 }
4270 *out = ImportFrom(module, names, level, lineno, col_offset,
4271 arena);
4272 if (*out == NULL) goto failed;
4273 return 0;
4274 }
4275 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4276 expr_ty body;
4277 expr_ty globals;
4278 expr_ty locals;
4279
4280 if (PyObject_HasAttrString(obj, "body")) {
4281 int res;
4282 tmp = PyObject_GetAttrString(obj, "body");
4283 if (tmp == NULL) goto failed;
4284 res = obj2ast_expr(tmp, &body, arena);
4285 if (res != 0) goto failed;
4286 Py_XDECREF(tmp);
4287 tmp = NULL;
4288 } else {
4289 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4290 return 1;
4291 }
4292 if (PyObject_HasAttrString(obj, "globals")) {
4293 int res;
4294 tmp = PyObject_GetAttrString(obj, "globals");
4295 if (tmp == NULL) goto failed;
4296 res = obj2ast_expr(tmp, &globals, arena);
4297 if (res != 0) goto failed;
4298 Py_XDECREF(tmp);
4299 tmp = NULL;
4300 } else {
4301 globals = NULL;
4302 }
4303 if (PyObject_HasAttrString(obj, "locals")) {
4304 int res;
4305 tmp = PyObject_GetAttrString(obj, "locals");
4306 if (tmp == NULL) goto failed;
4307 res = obj2ast_expr(tmp, &locals, arena);
4308 if (res != 0) goto failed;
4309 Py_XDECREF(tmp);
4310 tmp = NULL;
4311 } else {
4312 locals = NULL;
4313 }
4314 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4315 if (*out == NULL) goto failed;
4316 return 0;
4317 }
4318 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4319 asdl_seq* names;
4320
4321 if (PyObject_HasAttrString(obj, "names")) {
4322 int res;
4323 Py_ssize_t len;
4324 Py_ssize_t i;
4325 tmp = PyObject_GetAttrString(obj, "names");
4326 if (tmp == NULL) goto failed;
4327 if (!PyList_Check(tmp)) {
4328 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4329 goto failed;
4330 }
4331 len = PyList_GET_SIZE(tmp);
4332 names = asdl_seq_new(len, arena);
4333 if (names == NULL) goto failed;
4334 for (i = 0; i < len; i++) {
4335 identifier value;
4336 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4337 if (res != 0) goto failed;
4338 asdl_seq_SET(names, i, value);
4339 }
4340 Py_XDECREF(tmp);
4341 tmp = NULL;
4342 } else {
4343 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4344 return 1;
4345 }
4346 *out = Global(names, lineno, col_offset, arena);
4347 if (*out == NULL) goto failed;
4348 return 0;
4349 }
4350 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4351 expr_ty value;
4352
4353 if (PyObject_HasAttrString(obj, "value")) {
4354 int res;
4355 tmp = PyObject_GetAttrString(obj, "value");
4356 if (tmp == NULL) goto failed;
4357 res = obj2ast_expr(tmp, &value, arena);
4358 if (res != 0) goto failed;
4359 Py_XDECREF(tmp);
4360 tmp = NULL;
4361 } else {
4362 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4363 return 1;
4364 }
4365 *out = Expr(value, lineno, col_offset, arena);
4366 if (*out == NULL) goto failed;
4367 return 0;
4368 }
4369 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4370
4371 *out = Pass(lineno, col_offset, arena);
4372 if (*out == NULL) goto failed;
4373 return 0;
4374 }
4375 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4376
4377 *out = Break(lineno, col_offset, arena);
4378 if (*out == NULL) goto failed;
4379 return 0;
4380 }
4381 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4382
4383 *out = Continue(lineno, col_offset, arena);
4384 if (*out == NULL) goto failed;
4385 return 0;
4386 }
4387
4388 tmp = PyObject_Repr(obj);
4389 if (tmp == NULL) goto failed;
4390 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4391failed:
4392 Py_XDECREF(tmp);
4393 return 1;
4394}
4395
4396int
4397obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4398{
4399 PyObject* tmp = NULL;
4400
4401 int lineno;
4402 int col_offset;
4403
4404 if (obj == Py_None) {
4405 *out = NULL;
4406 return 0;
4407 }
4408 if (PyObject_HasAttrString(obj, "lineno")) {
4409 int res;
4410 tmp = PyObject_GetAttrString(obj, "lineno");
4411 if (tmp == NULL) goto failed;
4412 res = obj2ast_int(tmp, &lineno, arena);
4413 if (res != 0) goto failed;
4414 Py_XDECREF(tmp);
4415 tmp = NULL;
4416 } else {
4417 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4418 return 1;
4419 }
4420 if (PyObject_HasAttrString(obj, "col_offset")) {
4421 int res;
4422 tmp = PyObject_GetAttrString(obj, "col_offset");
4423 if (tmp == NULL) goto failed;
4424 res = obj2ast_int(tmp, &col_offset, arena);
4425 if (res != 0) goto failed;
4426 Py_XDECREF(tmp);
4427 tmp = NULL;
4428 } else {
4429 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4430 return 1;
4431 }
4432 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4433 boolop_ty op;
4434 asdl_seq* values;
4435
4436 if (PyObject_HasAttrString(obj, "op")) {
4437 int res;
4438 tmp = PyObject_GetAttrString(obj, "op");
4439 if (tmp == NULL) goto failed;
4440 res = obj2ast_boolop(tmp, &op, arena);
4441 if (res != 0) goto failed;
4442 Py_XDECREF(tmp);
4443 tmp = NULL;
4444 } else {
4445 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4446 return 1;
4447 }
4448 if (PyObject_HasAttrString(obj, "values")) {
4449 int res;
4450 Py_ssize_t len;
4451 Py_ssize_t i;
4452 tmp = PyObject_GetAttrString(obj, "values");
4453 if (tmp == NULL) goto failed;
4454 if (!PyList_Check(tmp)) {
4455 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4456 goto failed;
4457 }
4458 len = PyList_GET_SIZE(tmp);
4459 values = asdl_seq_new(len, arena);
4460 if (values == NULL) goto failed;
4461 for (i = 0; i < len; i++) {
4462 expr_ty value;
4463 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4464 if (res != 0) goto failed;
4465 asdl_seq_SET(values, i, value);
4466 }
4467 Py_XDECREF(tmp);
4468 tmp = NULL;
4469 } else {
4470 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4471 return 1;
4472 }
4473 *out = BoolOp(op, values, lineno, col_offset, arena);
4474 if (*out == NULL) goto failed;
4475 return 0;
4476 }
4477 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4478 expr_ty left;
4479 operator_ty op;
4480 expr_ty right;
4481
4482 if (PyObject_HasAttrString(obj, "left")) {
4483 int res;
4484 tmp = PyObject_GetAttrString(obj, "left");
4485 if (tmp == NULL) goto failed;
4486 res = obj2ast_expr(tmp, &left, arena);
4487 if (res != 0) goto failed;
4488 Py_XDECREF(tmp);
4489 tmp = NULL;
4490 } else {
4491 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4492 return 1;
4493 }
4494 if (PyObject_HasAttrString(obj, "op")) {
4495 int res;
4496 tmp = PyObject_GetAttrString(obj, "op");
4497 if (tmp == NULL) goto failed;
4498 res = obj2ast_operator(tmp, &op, arena);
4499 if (res != 0) goto failed;
4500 Py_XDECREF(tmp);
4501 tmp = NULL;
4502 } else {
4503 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4504 return 1;
4505 }
4506 if (PyObject_HasAttrString(obj, "right")) {
4507 int res;
4508 tmp = PyObject_GetAttrString(obj, "right");
4509 if (tmp == NULL) goto failed;
4510 res = obj2ast_expr(tmp, &right, arena);
4511 if (res != 0) goto failed;
4512 Py_XDECREF(tmp);
4513 tmp = NULL;
4514 } else {
4515 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4516 return 1;
4517 }
4518 *out = BinOp(left, op, right, lineno, col_offset, arena);
4519 if (*out == NULL) goto failed;
4520 return 0;
4521 }
4522 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4523 unaryop_ty op;
4524 expr_ty operand;
4525
4526 if (PyObject_HasAttrString(obj, "op")) {
4527 int res;
4528 tmp = PyObject_GetAttrString(obj, "op");
4529 if (tmp == NULL) goto failed;
4530 res = obj2ast_unaryop(tmp, &op, arena);
4531 if (res != 0) goto failed;
4532 Py_XDECREF(tmp);
4533 tmp = NULL;
4534 } else {
4535 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4536 return 1;
4537 }
4538 if (PyObject_HasAttrString(obj, "operand")) {
4539 int res;
4540 tmp = PyObject_GetAttrString(obj, "operand");
4541 if (tmp == NULL) goto failed;
4542 res = obj2ast_expr(tmp, &operand, arena);
4543 if (res != 0) goto failed;
4544 Py_XDECREF(tmp);
4545 tmp = NULL;
4546 } else {
4547 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4548 return 1;
4549 }
4550 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4551 if (*out == NULL) goto failed;
4552 return 0;
4553 }
4554 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4555 arguments_ty args;
4556 expr_ty body;
4557
4558 if (PyObject_HasAttrString(obj, "args")) {
4559 int res;
4560 tmp = PyObject_GetAttrString(obj, "args");
4561 if (tmp == NULL) goto failed;
4562 res = obj2ast_arguments(tmp, &args, arena);
4563 if (res != 0) goto failed;
4564 Py_XDECREF(tmp);
4565 tmp = NULL;
4566 } else {
4567 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4568 return 1;
4569 }
4570 if (PyObject_HasAttrString(obj, "body")) {
4571 int res;
4572 tmp = PyObject_GetAttrString(obj, "body");
4573 if (tmp == NULL) goto failed;
4574 res = obj2ast_expr(tmp, &body, arena);
4575 if (res != 0) goto failed;
4576 Py_XDECREF(tmp);
4577 tmp = NULL;
4578 } else {
4579 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4580 return 1;
4581 }
4582 *out = Lambda(args, body, lineno, col_offset, arena);
4583 if (*out == NULL) goto failed;
4584 return 0;
4585 }
4586 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4587 expr_ty test;
4588 expr_ty body;
4589 expr_ty orelse;
4590
4591 if (PyObject_HasAttrString(obj, "test")) {
4592 int res;
4593 tmp = PyObject_GetAttrString(obj, "test");
4594 if (tmp == NULL) goto failed;
4595 res = obj2ast_expr(tmp, &test, arena);
4596 if (res != 0) goto failed;
4597 Py_XDECREF(tmp);
4598 tmp = NULL;
4599 } else {
4600 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4601 return 1;
4602 }
4603 if (PyObject_HasAttrString(obj, "body")) {
4604 int res;
4605 tmp = PyObject_GetAttrString(obj, "body");
4606 if (tmp == NULL) goto failed;
4607 res = obj2ast_expr(tmp, &body, arena);
4608 if (res != 0) goto failed;
4609 Py_XDECREF(tmp);
4610 tmp = NULL;
4611 } else {
4612 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4613 return 1;
4614 }
4615 if (PyObject_HasAttrString(obj, "orelse")) {
4616 int res;
4617 tmp = PyObject_GetAttrString(obj, "orelse");
4618 if (tmp == NULL) goto failed;
4619 res = obj2ast_expr(tmp, &orelse, arena);
4620 if (res != 0) goto failed;
4621 Py_XDECREF(tmp);
4622 tmp = NULL;
4623 } else {
4624 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4625 return 1;
4626 }
4627 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4628 if (*out == NULL) goto failed;
4629 return 0;
4630 }
4631 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4632 asdl_seq* keys;
4633 asdl_seq* values;
4634
4635 if (PyObject_HasAttrString(obj, "keys")) {
4636 int res;
4637 Py_ssize_t len;
4638 Py_ssize_t i;
4639 tmp = PyObject_GetAttrString(obj, "keys");
4640 if (tmp == NULL) goto failed;
4641 if (!PyList_Check(tmp)) {
4642 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4643 goto failed;
4644 }
4645 len = PyList_GET_SIZE(tmp);
4646 keys = asdl_seq_new(len, arena);
4647 if (keys == NULL) goto failed;
4648 for (i = 0; i < len; i++) {
4649 expr_ty value;
4650 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4651 if (res != 0) goto failed;
4652 asdl_seq_SET(keys, i, value);
4653 }
4654 Py_XDECREF(tmp);
4655 tmp = NULL;
4656 } else {
4657 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4658 return 1;
4659 }
4660 if (PyObject_HasAttrString(obj, "values")) {
4661 int res;
4662 Py_ssize_t len;
4663 Py_ssize_t i;
4664 tmp = PyObject_GetAttrString(obj, "values");
4665 if (tmp == NULL) goto failed;
4666 if (!PyList_Check(tmp)) {
4667 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4668 goto failed;
4669 }
4670 len = PyList_GET_SIZE(tmp);
4671 values = asdl_seq_new(len, arena);
4672 if (values == NULL) goto failed;
4673 for (i = 0; i < len; i++) {
4674 expr_ty value;
4675 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4676 if (res != 0) goto failed;
4677 asdl_seq_SET(values, i, value);
4678 }
4679 Py_XDECREF(tmp);
4680 tmp = NULL;
4681 } else {
4682 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4683 return 1;
4684 }
4685 *out = Dict(keys, values, lineno, col_offset, arena);
4686 if (*out == NULL) goto failed;
4687 return 0;
4688 }
4689 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4690 expr_ty elt;
4691 asdl_seq* generators;
4692
4693 if (PyObject_HasAttrString(obj, "elt")) {
4694 int res;
4695 tmp = PyObject_GetAttrString(obj, "elt");
4696 if (tmp == NULL) goto failed;
4697 res = obj2ast_expr(tmp, &elt, arena);
4698 if (res != 0) goto failed;
4699 Py_XDECREF(tmp);
4700 tmp = NULL;
4701 } else {
4702 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4703 return 1;
4704 }
4705 if (PyObject_HasAttrString(obj, "generators")) {
4706 int res;
4707 Py_ssize_t len;
4708 Py_ssize_t i;
4709 tmp = PyObject_GetAttrString(obj, "generators");
4710 if (tmp == NULL) goto failed;
4711 if (!PyList_Check(tmp)) {
4712 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4713 goto failed;
4714 }
4715 len = PyList_GET_SIZE(tmp);
4716 generators = asdl_seq_new(len, arena);
4717 if (generators == NULL) goto failed;
4718 for (i = 0; i < len; i++) {
4719 comprehension_ty value;
4720 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4721 if (res != 0) goto failed;
4722 asdl_seq_SET(generators, i, value);
4723 }
4724 Py_XDECREF(tmp);
4725 tmp = NULL;
4726 } else {
4727 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4728 return 1;
4729 }
4730 *out = ListComp(elt, generators, lineno, col_offset, arena);
4731 if (*out == NULL) goto failed;
4732 return 0;
4733 }
4734 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4735 expr_ty elt;
4736 asdl_seq* generators;
4737
4738 if (PyObject_HasAttrString(obj, "elt")) {
4739 int res;
4740 tmp = PyObject_GetAttrString(obj, "elt");
4741 if (tmp == NULL) goto failed;
4742 res = obj2ast_expr(tmp, &elt, arena);
4743 if (res != 0) goto failed;
4744 Py_XDECREF(tmp);
4745 tmp = NULL;
4746 } else {
4747 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4748 return 1;
4749 }
4750 if (PyObject_HasAttrString(obj, "generators")) {
4751 int res;
4752 Py_ssize_t len;
4753 Py_ssize_t i;
4754 tmp = PyObject_GetAttrString(obj, "generators");
4755 if (tmp == NULL) goto failed;
4756 if (!PyList_Check(tmp)) {
4757 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4758 goto failed;
4759 }
4760 len = PyList_GET_SIZE(tmp);
4761 generators = asdl_seq_new(len, arena);
4762 if (generators == NULL) goto failed;
4763 for (i = 0; i < len; i++) {
4764 comprehension_ty value;
4765 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4766 if (res != 0) goto failed;
4767 asdl_seq_SET(generators, i, value);
4768 }
4769 Py_XDECREF(tmp);
4770 tmp = NULL;
4771 } else {
4772 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4773 return 1;
4774 }
4775 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4776 if (*out == NULL) goto failed;
4777 return 0;
4778 }
4779 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4780 expr_ty value;
4781
4782 if (PyObject_HasAttrString(obj, "value")) {
4783 int res;
4784 tmp = PyObject_GetAttrString(obj, "value");
4785 if (tmp == NULL) goto failed;
4786 res = obj2ast_expr(tmp, &value, arena);
4787 if (res != 0) goto failed;
4788 Py_XDECREF(tmp);
4789 tmp = NULL;
4790 } else {
4791 value = NULL;
4792 }
4793 *out = Yield(value, lineno, col_offset, arena);
4794 if (*out == NULL) goto failed;
4795 return 0;
4796 }
4797 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4798 expr_ty left;
4799 asdl_int_seq* ops;
4800 asdl_seq* comparators;
4801
4802 if (PyObject_HasAttrString(obj, "left")) {
4803 int res;
4804 tmp = PyObject_GetAttrString(obj, "left");
4805 if (tmp == NULL) goto failed;
4806 res = obj2ast_expr(tmp, &left, arena);
4807 if (res != 0) goto failed;
4808 Py_XDECREF(tmp);
4809 tmp = NULL;
4810 } else {
4811 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4812 return 1;
4813 }
4814 if (PyObject_HasAttrString(obj, "ops")) {
4815 int res;
4816 Py_ssize_t len;
4817 Py_ssize_t i;
4818 tmp = PyObject_GetAttrString(obj, "ops");
4819 if (tmp == NULL) goto failed;
4820 if (!PyList_Check(tmp)) {
4821 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4822 goto failed;
4823 }
4824 len = PyList_GET_SIZE(tmp);
4825 ops = asdl_int_seq_new(len, arena);
4826 if (ops == NULL) goto failed;
4827 for (i = 0; i < len; i++) {
4828 cmpop_ty value;
4829 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4830 if (res != 0) goto failed;
4831 asdl_seq_SET(ops, i, value);
4832 }
4833 Py_XDECREF(tmp);
4834 tmp = NULL;
4835 } else {
4836 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4837 return 1;
4838 }
4839 if (PyObject_HasAttrString(obj, "comparators")) {
4840 int res;
4841 Py_ssize_t len;
4842 Py_ssize_t i;
4843 tmp = PyObject_GetAttrString(obj, "comparators");
4844 if (tmp == NULL) goto failed;
4845 if (!PyList_Check(tmp)) {
4846 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4847 goto failed;
4848 }
4849 len = PyList_GET_SIZE(tmp);
4850 comparators = asdl_seq_new(len, arena);
4851 if (comparators == NULL) goto failed;
4852 for (i = 0; i < len; i++) {
4853 expr_ty value;
4854 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4855 if (res != 0) goto failed;
4856 asdl_seq_SET(comparators, i, value);
4857 }
4858 Py_XDECREF(tmp);
4859 tmp = NULL;
4860 } else {
4861 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4862 return 1;
4863 }
4864 *out = Compare(left, ops, comparators, lineno, col_offset,
4865 arena);
4866 if (*out == NULL) goto failed;
4867 return 0;
4868 }
4869 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4870 expr_ty func;
4871 asdl_seq* args;
4872 asdl_seq* keywords;
4873 expr_ty starargs;
4874 expr_ty kwargs;
4875
4876 if (PyObject_HasAttrString(obj, "func")) {
4877 int res;
4878 tmp = PyObject_GetAttrString(obj, "func");
4879 if (tmp == NULL) goto failed;
4880 res = obj2ast_expr(tmp, &func, arena);
4881 if (res != 0) goto failed;
4882 Py_XDECREF(tmp);
4883 tmp = NULL;
4884 } else {
4885 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4886 return 1;
4887 }
4888 if (PyObject_HasAttrString(obj, "args")) {
4889 int res;
4890 Py_ssize_t len;
4891 Py_ssize_t i;
4892 tmp = PyObject_GetAttrString(obj, "args");
4893 if (tmp == NULL) goto failed;
4894 if (!PyList_Check(tmp)) {
4895 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4896 goto failed;
4897 }
4898 len = PyList_GET_SIZE(tmp);
4899 args = asdl_seq_new(len, arena);
4900 if (args == NULL) goto failed;
4901 for (i = 0; i < len; i++) {
4902 expr_ty value;
4903 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4904 if (res != 0) goto failed;
4905 asdl_seq_SET(args, i, value);
4906 }
4907 Py_XDECREF(tmp);
4908 tmp = NULL;
4909 } else {
4910 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4911 return 1;
4912 }
4913 if (PyObject_HasAttrString(obj, "keywords")) {
4914 int res;
4915 Py_ssize_t len;
4916 Py_ssize_t i;
4917 tmp = PyObject_GetAttrString(obj, "keywords");
4918 if (tmp == NULL) goto failed;
4919 if (!PyList_Check(tmp)) {
4920 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4921 goto failed;
4922 }
4923 len = PyList_GET_SIZE(tmp);
4924 keywords = asdl_seq_new(len, arena);
4925 if (keywords == NULL) goto failed;
4926 for (i = 0; i < len; i++) {
4927 keyword_ty value;
4928 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4929 if (res != 0) goto failed;
4930 asdl_seq_SET(keywords, i, value);
4931 }
4932 Py_XDECREF(tmp);
4933 tmp = NULL;
4934 } else {
4935 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4936 return 1;
4937 }
4938 if (PyObject_HasAttrString(obj, "starargs")) {
4939 int res;
4940 tmp = PyObject_GetAttrString(obj, "starargs");
4941 if (tmp == NULL) goto failed;
4942 res = obj2ast_expr(tmp, &starargs, arena);
4943 if (res != 0) goto failed;
4944 Py_XDECREF(tmp);
4945 tmp = NULL;
4946 } else {
4947 starargs = NULL;
4948 }
4949 if (PyObject_HasAttrString(obj, "kwargs")) {
4950 int res;
4951 tmp = PyObject_GetAttrString(obj, "kwargs");
4952 if (tmp == NULL) goto failed;
4953 res = obj2ast_expr(tmp, &kwargs, arena);
4954 if (res != 0) goto failed;
4955 Py_XDECREF(tmp);
4956 tmp = NULL;
4957 } else {
4958 kwargs = NULL;
4959 }
4960 *out = Call(func, args, keywords, starargs, kwargs, lineno,
4961 col_offset, arena);
4962 if (*out == NULL) goto failed;
4963 return 0;
4964 }
4965 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
4966 expr_ty value;
4967
4968 if (PyObject_HasAttrString(obj, "value")) {
4969 int res;
4970 tmp = PyObject_GetAttrString(obj, "value");
4971 if (tmp == NULL) goto failed;
4972 res = obj2ast_expr(tmp, &value, arena);
4973 if (res != 0) goto failed;
4974 Py_XDECREF(tmp);
4975 tmp = NULL;
4976 } else {
4977 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
4978 return 1;
4979 }
4980 *out = Repr(value, lineno, col_offset, arena);
4981 if (*out == NULL) goto failed;
4982 return 0;
4983 }
4984 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
4985 object n;
4986
4987 if (PyObject_HasAttrString(obj, "n")) {
4988 int res;
4989 tmp = PyObject_GetAttrString(obj, "n");
4990 if (tmp == NULL) goto failed;
4991 res = obj2ast_object(tmp, &n, arena);
4992 if (res != 0) goto failed;
4993 Py_XDECREF(tmp);
4994 tmp = NULL;
4995 } else {
4996 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
4997 return 1;
4998 }
4999 *out = Num(n, lineno, col_offset, arena);
5000 if (*out == NULL) goto failed;
5001 return 0;
5002 }
5003 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5004 string s;
5005
5006 if (PyObject_HasAttrString(obj, "s")) {
5007 int res;
5008 tmp = PyObject_GetAttrString(obj, "s");
5009 if (tmp == NULL) goto failed;
5010 res = obj2ast_string(tmp, &s, arena);
5011 if (res != 0) goto failed;
5012 Py_XDECREF(tmp);
5013 tmp = NULL;
5014 } else {
5015 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5016 return 1;
5017 }
5018 *out = Str(s, lineno, col_offset, arena);
5019 if (*out == NULL) goto failed;
5020 return 0;
5021 }
5022 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5023 expr_ty value;
5024 identifier attr;
5025 expr_context_ty ctx;
5026
5027 if (PyObject_HasAttrString(obj, "value")) {
5028 int res;
5029 tmp = PyObject_GetAttrString(obj, "value");
5030 if (tmp == NULL) goto failed;
5031 res = obj2ast_expr(tmp, &value, arena);
5032 if (res != 0) goto failed;
5033 Py_XDECREF(tmp);
5034 tmp = NULL;
5035 } else {
5036 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5037 return 1;
5038 }
5039 if (PyObject_HasAttrString(obj, "attr")) {
5040 int res;
5041 tmp = PyObject_GetAttrString(obj, "attr");
5042 if (tmp == NULL) goto failed;
5043 res = obj2ast_identifier(tmp, &attr, arena);
5044 if (res != 0) goto failed;
5045 Py_XDECREF(tmp);
5046 tmp = NULL;
5047 } else {
5048 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5049 return 1;
5050 }
5051 if (PyObject_HasAttrString(obj, "ctx")) {
5052 int res;
5053 tmp = PyObject_GetAttrString(obj, "ctx");
5054 if (tmp == NULL) goto failed;
5055 res = obj2ast_expr_context(tmp, &ctx, arena);
5056 if (res != 0) goto failed;
5057 Py_XDECREF(tmp);
5058 tmp = NULL;
5059 } else {
5060 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5061 return 1;
5062 }
5063 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5064 if (*out == NULL) goto failed;
5065 return 0;
5066 }
5067 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5068 expr_ty value;
5069 slice_ty slice;
5070 expr_context_ty ctx;
5071
5072 if (PyObject_HasAttrString(obj, "value")) {
5073 int res;
5074 tmp = PyObject_GetAttrString(obj, "value");
5075 if (tmp == NULL) goto failed;
5076 res = obj2ast_expr(tmp, &value, arena);
5077 if (res != 0) goto failed;
5078 Py_XDECREF(tmp);
5079 tmp = NULL;
5080 } else {
5081 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5082 return 1;
5083 }
5084 if (PyObject_HasAttrString(obj, "slice")) {
5085 int res;
5086 tmp = PyObject_GetAttrString(obj, "slice");
5087 if (tmp == NULL) goto failed;
5088 res = obj2ast_slice(tmp, &slice, arena);
5089 if (res != 0) goto failed;
5090 Py_XDECREF(tmp);
5091 tmp = NULL;
5092 } else {
5093 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5094 return 1;
5095 }
5096 if (PyObject_HasAttrString(obj, "ctx")) {
5097 int res;
5098 tmp = PyObject_GetAttrString(obj, "ctx");
5099 if (tmp == NULL) goto failed;
5100 res = obj2ast_expr_context(tmp, &ctx, arena);
5101 if (res != 0) goto failed;
5102 Py_XDECREF(tmp);
5103 tmp = NULL;
5104 } else {
5105 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5106 return 1;
5107 }
5108 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5109 if (*out == NULL) goto failed;
5110 return 0;
5111 }
5112 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5113 identifier id;
5114 expr_context_ty ctx;
5115
5116 if (PyObject_HasAttrString(obj, "id")) {
5117 int res;
5118 tmp = PyObject_GetAttrString(obj, "id");
5119 if (tmp == NULL) goto failed;
5120 res = obj2ast_identifier(tmp, &id, arena);
5121 if (res != 0) goto failed;
5122 Py_XDECREF(tmp);
5123 tmp = NULL;
5124 } else {
5125 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5126 return 1;
5127 }
5128 if (PyObject_HasAttrString(obj, "ctx")) {
5129 int res;
5130 tmp = PyObject_GetAttrString(obj, "ctx");
5131 if (tmp == NULL) goto failed;
5132 res = obj2ast_expr_context(tmp, &ctx, arena);
5133 if (res != 0) goto failed;
5134 Py_XDECREF(tmp);
5135 tmp = NULL;
5136 } else {
5137 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5138 return 1;
5139 }
5140 *out = Name(id, ctx, lineno, col_offset, arena);
5141 if (*out == NULL) goto failed;
5142 return 0;
5143 }
5144 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5145 asdl_seq* elts;
5146 expr_context_ty ctx;
5147
5148 if (PyObject_HasAttrString(obj, "elts")) {
5149 int res;
5150 Py_ssize_t len;
5151 Py_ssize_t i;
5152 tmp = PyObject_GetAttrString(obj, "elts");
5153 if (tmp == NULL) goto failed;
5154 if (!PyList_Check(tmp)) {
5155 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5156 goto failed;
5157 }
5158 len = PyList_GET_SIZE(tmp);
5159 elts = asdl_seq_new(len, arena);
5160 if (elts == NULL) goto failed;
5161 for (i = 0; i < len; i++) {
5162 expr_ty value;
5163 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5164 if (res != 0) goto failed;
5165 asdl_seq_SET(elts, i, value);
5166 }
5167 Py_XDECREF(tmp);
5168 tmp = NULL;
5169 } else {
5170 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5171 return 1;
5172 }
5173 if (PyObject_HasAttrString(obj, "ctx")) {
5174 int res;
5175 tmp = PyObject_GetAttrString(obj, "ctx");
5176 if (tmp == NULL) goto failed;
5177 res = obj2ast_expr_context(tmp, &ctx, arena);
5178 if (res != 0) goto failed;
5179 Py_XDECREF(tmp);
5180 tmp = NULL;
5181 } else {
5182 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5183 return 1;
5184 }
5185 *out = List(elts, ctx, lineno, col_offset, arena);
5186 if (*out == NULL) goto failed;
5187 return 0;
5188 }
5189 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5190 asdl_seq* elts;
5191 expr_context_ty ctx;
5192
5193 if (PyObject_HasAttrString(obj, "elts")) {
5194 int res;
5195 Py_ssize_t len;
5196 Py_ssize_t i;
5197 tmp = PyObject_GetAttrString(obj, "elts");
5198 if (tmp == NULL) goto failed;
5199 if (!PyList_Check(tmp)) {
5200 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5201 goto failed;
5202 }
5203 len = PyList_GET_SIZE(tmp);
5204 elts = asdl_seq_new(len, arena);
5205 if (elts == NULL) goto failed;
5206 for (i = 0; i < len; i++) {
5207 expr_ty value;
5208 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5209 if (res != 0) goto failed;
5210 asdl_seq_SET(elts, i, value);
5211 }
5212 Py_XDECREF(tmp);
5213 tmp = NULL;
5214 } else {
5215 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5216 return 1;
5217 }
5218 if (PyObject_HasAttrString(obj, "ctx")) {
5219 int res;
5220 tmp = PyObject_GetAttrString(obj, "ctx");
5221 if (tmp == NULL) goto failed;
5222 res = obj2ast_expr_context(tmp, &ctx, arena);
5223 if (res != 0) goto failed;
5224 Py_XDECREF(tmp);
5225 tmp = NULL;
5226 } else {
5227 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5228 return 1;
5229 }
5230 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5231 if (*out == NULL) goto failed;
5232 return 0;
5233 }
5234
5235 tmp = PyObject_Repr(obj);
5236 if (tmp == NULL) goto failed;
5237 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5238failed:
5239 Py_XDECREF(tmp);
5240 return 1;
5241}
5242
5243int
5244obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5245{
5246 PyObject* tmp = NULL;
5247
5248 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5249 *out = Load;
5250 return 0;
5251 }
5252 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5253 *out = Store;
5254 return 0;
5255 }
5256 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5257 *out = Del;
5258 return 0;
5259 }
5260 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5261 *out = AugLoad;
5262 return 0;
5263 }
5264 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5265 *out = AugStore;
5266 return 0;
5267 }
5268 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5269 *out = Param;
5270 return 0;
5271 }
5272
5273 tmp = PyObject_Repr(obj);
5274 if (tmp == NULL) goto failed;
5275 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5276failed:
5277 Py_XDECREF(tmp);
5278 return 1;
5279}
5280
5281int
5282obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5283{
5284 PyObject* tmp = NULL;
5285
5286
5287 if (obj == Py_None) {
5288 *out = NULL;
5289 return 0;
5290 }
5291 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5292
5293 *out = Ellipsis(arena);
5294 if (*out == NULL) goto failed;
5295 return 0;
5296 }
5297 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5298 expr_ty lower;
5299 expr_ty upper;
5300 expr_ty step;
5301
5302 if (PyObject_HasAttrString(obj, "lower")) {
5303 int res;
5304 tmp = PyObject_GetAttrString(obj, "lower");
5305 if (tmp == NULL) goto failed;
5306 res = obj2ast_expr(tmp, &lower, arena);
5307 if (res != 0) goto failed;
5308 Py_XDECREF(tmp);
5309 tmp = NULL;
5310 } else {
5311 lower = NULL;
5312 }
5313 if (PyObject_HasAttrString(obj, "upper")) {
5314 int res;
5315 tmp = PyObject_GetAttrString(obj, "upper");
5316 if (tmp == NULL) goto failed;
5317 res = obj2ast_expr(tmp, &upper, arena);
5318 if (res != 0) goto failed;
5319 Py_XDECREF(tmp);
5320 tmp = NULL;
5321 } else {
5322 upper = NULL;
5323 }
5324 if (PyObject_HasAttrString(obj, "step")) {
5325 int res;
5326 tmp = PyObject_GetAttrString(obj, "step");
5327 if (tmp == NULL) goto failed;
5328 res = obj2ast_expr(tmp, &step, arena);
5329 if (res != 0) goto failed;
5330 Py_XDECREF(tmp);
5331 tmp = NULL;
5332 } else {
5333 step = NULL;
5334 }
5335 *out = Slice(lower, upper, step, arena);
5336 if (*out == NULL) goto failed;
5337 return 0;
5338 }
5339 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5340 asdl_seq* dims;
5341
5342 if (PyObject_HasAttrString(obj, "dims")) {
5343 int res;
5344 Py_ssize_t len;
5345 Py_ssize_t i;
5346 tmp = PyObject_GetAttrString(obj, "dims");
5347 if (tmp == NULL) goto failed;
5348 if (!PyList_Check(tmp)) {
5349 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5350 goto failed;
5351 }
5352 len = PyList_GET_SIZE(tmp);
5353 dims = asdl_seq_new(len, arena);
5354 if (dims == NULL) goto failed;
5355 for (i = 0; i < len; i++) {
5356 slice_ty value;
5357 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5358 if (res != 0) goto failed;
5359 asdl_seq_SET(dims, i, value);
5360 }
5361 Py_XDECREF(tmp);
5362 tmp = NULL;
5363 } else {
5364 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5365 return 1;
5366 }
5367 *out = ExtSlice(dims, arena);
5368 if (*out == NULL) goto failed;
5369 return 0;
5370 }
5371 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5372 expr_ty value;
5373
5374 if (PyObject_HasAttrString(obj, "value")) {
5375 int res;
5376 tmp = PyObject_GetAttrString(obj, "value");
5377 if (tmp == NULL) goto failed;
5378 res = obj2ast_expr(tmp, &value, arena);
5379 if (res != 0) goto failed;
5380 Py_XDECREF(tmp);
5381 tmp = NULL;
5382 } else {
5383 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5384 return 1;
5385 }
5386 *out = Index(value, arena);
5387 if (*out == NULL) goto failed;
5388 return 0;
5389 }
5390
5391 tmp = PyObject_Repr(obj);
5392 if (tmp == NULL) goto failed;
5393 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5394failed:
5395 Py_XDECREF(tmp);
5396 return 1;
5397}
5398
5399int
5400obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5401{
5402 PyObject* tmp = NULL;
5403
5404 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5405 *out = And;
5406 return 0;
5407 }
5408 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5409 *out = Or;
5410 return 0;
5411 }
5412
5413 tmp = PyObject_Repr(obj);
5414 if (tmp == NULL) goto failed;
5415 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5416failed:
5417 Py_XDECREF(tmp);
5418 return 1;
5419}
5420
5421int
5422obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5423{
5424 PyObject* tmp = NULL;
5425
5426 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5427 *out = Add;
5428 return 0;
5429 }
5430 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5431 *out = Sub;
5432 return 0;
5433 }
5434 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5435 *out = Mult;
5436 return 0;
5437 }
5438 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5439 *out = Div;
5440 return 0;
5441 }
5442 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5443 *out = Mod;
5444 return 0;
5445 }
5446 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5447 *out = Pow;
5448 return 0;
5449 }
5450 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5451 *out = LShift;
5452 return 0;
5453 }
5454 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5455 *out = RShift;
5456 return 0;
5457 }
5458 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5459 *out = BitOr;
5460 return 0;
5461 }
5462 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5463 *out = BitXor;
5464 return 0;
5465 }
5466 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5467 *out = BitAnd;
5468 return 0;
5469 }
5470 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5471 *out = FloorDiv;
5472 return 0;
5473 }
5474
5475 tmp = PyObject_Repr(obj);
5476 if (tmp == NULL) goto failed;
5477 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5478failed:
5479 Py_XDECREF(tmp);
5480 return 1;
5481}
5482
5483int
5484obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5485{
5486 PyObject* tmp = NULL;
5487
5488 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5489 *out = Invert;
5490 return 0;
5491 }
5492 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5493 *out = Not;
5494 return 0;
5495 }
5496 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5497 *out = UAdd;
5498 return 0;
5499 }
5500 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5501 *out = USub;
5502 return 0;
5503 }
5504
5505 tmp = PyObject_Repr(obj);
5506 if (tmp == NULL) goto failed;
5507 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5508failed:
5509 Py_XDECREF(tmp);
5510 return 1;
5511}
5512
5513int
5514obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5515{
5516 PyObject* tmp = NULL;
5517
5518 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5519 *out = Eq;
5520 return 0;
5521 }
5522 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5523 *out = NotEq;
5524 return 0;
5525 }
5526 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5527 *out = Lt;
5528 return 0;
5529 }
5530 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5531 *out = LtE;
5532 return 0;
5533 }
5534 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5535 *out = Gt;
5536 return 0;
5537 }
5538 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5539 *out = GtE;
5540 return 0;
5541 }
5542 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5543 *out = Is;
5544 return 0;
5545 }
5546 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5547 *out = IsNot;
5548 return 0;
5549 }
5550 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5551 *out = In;
5552 return 0;
5553 }
5554 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5555 *out = NotIn;
5556 return 0;
5557 }
5558
5559 tmp = PyObject_Repr(obj);
5560 if (tmp == NULL) goto failed;
5561 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5562failed:
5563 Py_XDECREF(tmp);
5564 return 1;
5565}
5566
5567int
5568obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5569{
5570 PyObject* tmp = NULL;
5571 expr_ty target;
5572 expr_ty iter;
5573 asdl_seq* ifs;
5574
5575 if (PyObject_HasAttrString(obj, "target")) {
5576 int res;
5577 tmp = PyObject_GetAttrString(obj, "target");
5578 if (tmp == NULL) goto failed;
5579 res = obj2ast_expr(tmp, &target, arena);
5580 if (res != 0) goto failed;
5581 Py_XDECREF(tmp);
5582 tmp = NULL;
5583 } else {
5584 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5585 return 1;
5586 }
5587 if (PyObject_HasAttrString(obj, "iter")) {
5588 int res;
5589 tmp = PyObject_GetAttrString(obj, "iter");
5590 if (tmp == NULL) goto failed;
5591 res = obj2ast_expr(tmp, &iter, arena);
5592 if (res != 0) goto failed;
5593 Py_XDECREF(tmp);
5594 tmp = NULL;
5595 } else {
5596 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5597 return 1;
5598 }
5599 if (PyObject_HasAttrString(obj, "ifs")) {
5600 int res;
5601 Py_ssize_t len;
5602 Py_ssize_t i;
5603 tmp = PyObject_GetAttrString(obj, "ifs");
5604 if (tmp == NULL) goto failed;
5605 if (!PyList_Check(tmp)) {
5606 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5607 goto failed;
5608 }
5609 len = PyList_GET_SIZE(tmp);
5610 ifs = asdl_seq_new(len, arena);
5611 if (ifs == NULL) goto failed;
5612 for (i = 0; i < len; i++) {
5613 expr_ty value;
5614 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5615 if (res != 0) goto failed;
5616 asdl_seq_SET(ifs, i, value);
5617 }
5618 Py_XDECREF(tmp);
5619 tmp = NULL;
5620 } else {
5621 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5622 return 1;
5623 }
5624 *out = comprehension(target, iter, ifs, arena);
5625 return 0;
5626failed:
5627 Py_XDECREF(tmp);
5628 return 1;
5629}
5630
5631int
5632obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5633{
5634 PyObject* tmp = NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00005635
Georg Brandlfc8eef32008-03-28 12:11:56 +00005636 int lineno;
5637 int col_offset;
5638
Georg Brandla48f3ab2008-03-30 06:40:17 +00005639 if (obj == Py_None) {
5640 *out = NULL;
5641 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005642 }
5643 if (PyObject_HasAttrString(obj, "lineno")) {
5644 int res;
5645 tmp = PyObject_GetAttrString(obj, "lineno");
5646 if (tmp == NULL) goto failed;
5647 res = obj2ast_int(tmp, &lineno, arena);
5648 if (res != 0) goto failed;
5649 Py_XDECREF(tmp);
5650 tmp = NULL;
5651 } else {
5652 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5653 return 1;
5654 }
5655 if (PyObject_HasAttrString(obj, "col_offset")) {
5656 int res;
5657 tmp = PyObject_GetAttrString(obj, "col_offset");
5658 if (tmp == NULL) goto failed;
5659 res = obj2ast_int(tmp, &col_offset, arena);
5660 if (res != 0) goto failed;
5661 Py_XDECREF(tmp);
5662 tmp = NULL;
5663 } else {
5664 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5665 return 1;
5666 }
Georg Brandla48f3ab2008-03-30 06:40:17 +00005667 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
5668 expr_ty type;
5669 expr_ty name;
5670 asdl_seq* body;
5671
5672 if (PyObject_HasAttrString(obj, "type")) {
5673 int res;
5674 tmp = PyObject_GetAttrString(obj, "type");
5675 if (tmp == NULL) goto failed;
5676 res = obj2ast_expr(tmp, &type, arena);
5677 if (res != 0) goto failed;
5678 Py_XDECREF(tmp);
5679 tmp = NULL;
5680 } else {
5681 type = NULL;
5682 }
5683 if (PyObject_HasAttrString(obj, "name")) {
5684 int res;
5685 tmp = PyObject_GetAttrString(obj, "name");
5686 if (tmp == NULL) goto failed;
5687 res = obj2ast_expr(tmp, &name, arena);
5688 if (res != 0) goto failed;
5689 Py_XDECREF(tmp);
5690 tmp = NULL;
5691 } else {
5692 name = NULL;
5693 }
5694 if (PyObject_HasAttrString(obj, "body")) {
5695 int res;
5696 Py_ssize_t len;
5697 Py_ssize_t i;
5698 tmp = PyObject_GetAttrString(obj, "body");
5699 if (tmp == NULL) goto failed;
5700 if (!PyList_Check(tmp)) {
5701 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5702 goto failed;
5703 }
5704 len = PyList_GET_SIZE(tmp);
5705 body = asdl_seq_new(len, arena);
5706 if (body == NULL) goto failed;
5707 for (i = 0; i < len; i++) {
5708 stmt_ty value;
5709 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5710 if (res != 0) goto failed;
5711 asdl_seq_SET(body, i, value);
5712 }
5713 Py_XDECREF(tmp);
5714 tmp = NULL;
5715 } else {
5716 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
5717 return 1;
5718 }
5719 *out = ExceptHandler(type, name, body, lineno, col_offset,
5720 arena);
5721 if (*out == NULL) goto failed;
5722 return 0;
5723 }
5724
5725 tmp = PyObject_Repr(obj);
5726 if (tmp == NULL) goto failed;
5727 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005728failed:
5729 Py_XDECREF(tmp);
5730 return 1;
5731}
5732
5733int
5734obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5735{
5736 PyObject* tmp = NULL;
5737 asdl_seq* args;
5738 identifier vararg;
5739 identifier kwarg;
5740 asdl_seq* defaults;
5741
5742 if (PyObject_HasAttrString(obj, "args")) {
5743 int res;
5744 Py_ssize_t len;
5745 Py_ssize_t i;
5746 tmp = PyObject_GetAttrString(obj, "args");
5747 if (tmp == NULL) goto failed;
5748 if (!PyList_Check(tmp)) {
5749 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5750 goto failed;
5751 }
5752 len = PyList_GET_SIZE(tmp);
5753 args = asdl_seq_new(len, arena);
5754 if (args == NULL) goto failed;
5755 for (i = 0; i < len; i++) {
5756 expr_ty value;
5757 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5758 if (res != 0) goto failed;
5759 asdl_seq_SET(args, i, value);
5760 }
5761 Py_XDECREF(tmp);
5762 tmp = NULL;
5763 } else {
5764 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5765 return 1;
5766 }
5767 if (PyObject_HasAttrString(obj, "vararg")) {
5768 int res;
5769 tmp = PyObject_GetAttrString(obj, "vararg");
5770 if (tmp == NULL) goto failed;
5771 res = obj2ast_identifier(tmp, &vararg, arena);
5772 if (res != 0) goto failed;
5773 Py_XDECREF(tmp);
5774 tmp = NULL;
5775 } else {
5776 vararg = NULL;
5777 }
5778 if (PyObject_HasAttrString(obj, "kwarg")) {
5779 int res;
5780 tmp = PyObject_GetAttrString(obj, "kwarg");
5781 if (tmp == NULL) goto failed;
5782 res = obj2ast_identifier(tmp, &kwarg, arena);
5783 if (res != 0) goto failed;
5784 Py_XDECREF(tmp);
5785 tmp = NULL;
5786 } else {
5787 kwarg = NULL;
5788 }
5789 if (PyObject_HasAttrString(obj, "defaults")) {
5790 int res;
5791 Py_ssize_t len;
5792 Py_ssize_t i;
5793 tmp = PyObject_GetAttrString(obj, "defaults");
5794 if (tmp == NULL) goto failed;
5795 if (!PyList_Check(tmp)) {
5796 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5797 goto failed;
5798 }
5799 len = PyList_GET_SIZE(tmp);
5800 defaults = asdl_seq_new(len, arena);
5801 if (defaults == NULL) goto failed;
5802 for (i = 0; i < len; i++) {
5803 expr_ty value;
5804 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5805 if (res != 0) goto failed;
5806 asdl_seq_SET(defaults, i, value);
5807 }
5808 Py_XDECREF(tmp);
5809 tmp = NULL;
5810 } else {
5811 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5812 return 1;
5813 }
5814 *out = arguments(args, vararg, kwarg, defaults, arena);
5815 return 0;
5816failed:
5817 Py_XDECREF(tmp);
5818 return 1;
5819}
5820
5821int
5822obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5823{
5824 PyObject* tmp = NULL;
5825 identifier arg;
5826 expr_ty value;
5827
5828 if (PyObject_HasAttrString(obj, "arg")) {
5829 int res;
5830 tmp = PyObject_GetAttrString(obj, "arg");
5831 if (tmp == NULL) goto failed;
5832 res = obj2ast_identifier(tmp, &arg, arena);
5833 if (res != 0) goto failed;
5834 Py_XDECREF(tmp);
5835 tmp = NULL;
5836 } else {
5837 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5838 return 1;
5839 }
5840 if (PyObject_HasAttrString(obj, "value")) {
5841 int res;
5842 tmp = PyObject_GetAttrString(obj, "value");
5843 if (tmp == NULL) goto failed;
5844 res = obj2ast_expr(tmp, &value, arena);
5845 if (res != 0) goto failed;
5846 Py_XDECREF(tmp);
5847 tmp = NULL;
5848 } else {
5849 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5850 return 1;
5851 }
5852 *out = keyword(arg, value, arena);
5853 return 0;
5854failed:
5855 Py_XDECREF(tmp);
5856 return 1;
5857}
5858
5859int
5860obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5861{
5862 PyObject* tmp = NULL;
5863 identifier name;
5864 identifier asname;
5865
5866 if (PyObject_HasAttrString(obj, "name")) {
5867 int res;
5868 tmp = PyObject_GetAttrString(obj, "name");
5869 if (tmp == NULL) goto failed;
5870 res = obj2ast_identifier(tmp, &name, arena);
5871 if (res != 0) goto failed;
5872 Py_XDECREF(tmp);
5873 tmp = NULL;
5874 } else {
5875 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5876 return 1;
5877 }
5878 if (PyObject_HasAttrString(obj, "asname")) {
5879 int res;
5880 tmp = PyObject_GetAttrString(obj, "asname");
5881 if (tmp == NULL) goto failed;
5882 res = obj2ast_identifier(tmp, &asname, arena);
5883 if (res != 0) goto failed;
5884 Py_XDECREF(tmp);
5885 tmp = NULL;
5886 } else {
5887 asname = NULL;
5888 }
5889 *out = alias(name, asname, arena);
5890 return 0;
5891failed:
5892 Py_XDECREF(tmp);
5893 return 1;
5894}
5895
5896
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005897PyMODINIT_FUNC
5898init_ast(void)
5899{
5900 PyObject *m, *d;
5901 if (!init_types()) return;
5902 m = Py_InitModule3("_ast", NULL, NULL);
5903 if (!m) return;
5904 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00005905 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005906 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5907 return;
Georg Brandlc52ed592008-03-30 07:01:47 +00005908 if (PyModule_AddStringConstant(m, "__version__", "62047") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00005909 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00005910 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5911 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5912 return;
5913 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5914 < 0) return;
5915 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
5916 0) return;
5917 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
5918 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
5919 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
5920 < 0) return;
5921 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
5922 return;
5923 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
5924 return;
5925 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
5926 return;
5927 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
5928 return;
5929 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
5930 0) return;
5931 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
5932 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
5933 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
5934 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
5935 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
5936 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
5937 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
5938 0) return;
5939 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
5940 0) return;
5941 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
5942 return;
5943 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
5944 return;
5945 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
5946 0) return;
5947 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
5948 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
5949 return;
5950 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
5951 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
5952 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
5953 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
5954 return;
5955 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
5956 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
5957 return;
5958 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
5959 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
5960 return;
5961 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
5962 return;
5963 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
5964 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
5965 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
5966 return;
5967 if (PyDict_SetItemString(d, "GeneratorExp",
5968 (PyObject*)GeneratorExp_type) < 0) return;
5969 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
5970 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
5971 return;
5972 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
5973 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
5974 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
5975 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
5976 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
5977 0) return;
5978 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
5979 0) return;
5980 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
5981 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
5982 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
5983 if (PyDict_SetItemString(d, "expr_context",
5984 (PyObject*)expr_context_type) < 0) return;
5985 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
5986 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
5987 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
5988 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
5989 return;
5990 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
5991 return;
5992 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
5993 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
5994 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
5995 return;
5996 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
5997 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
5998 return;
5999 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6000 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6001 return;
6002 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6003 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6004 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6005 return;
6006 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6007 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6008 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6009 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6010 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6011 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6012 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6013 return;
6014 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6015 return;
6016 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6017 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6018 return;
6019 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6020 return;
6021 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6022 return;
6023 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6024 return;
6025 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6026 return;
6027 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6028 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6029 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6030 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6031 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6032 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6033 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6034 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6035 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6036 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6037 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6038 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6039 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6040 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6041 if (PyDict_SetItemString(d, "comprehension",
6042 (PyObject*)comprehension_type) < 0) return;
6043 if (PyDict_SetItemString(d, "excepthandler",
6044 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006045 if (PyDict_SetItemString(d, "ExceptHandler",
6046 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006047 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6048 0) return;
6049 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6050 return;
6051 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006052}
6053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006054
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006055PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006056{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006057 init_types();
6058 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006059}
6060
Georg Brandlf2bfd542008-03-29 13:24:23 +00006061/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6062mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006063{
6064 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006065 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6066 (PyObject*)Interactive_type};
6067 char *req_name[] = {"Module", "Expression", "Interactive"};
6068 assert(0 <= mode && mode <= 2);
6069
Georg Brandlfc8eef32008-03-28 12:11:56 +00006070 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006071
6072 if (!PyObject_IsInstance(ast, req_type[mode])) {
6073 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6074 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006075 return NULL;
6076 }
6077 if (obj2ast_mod(ast, &res, arena) != 0)
6078 return NULL;
6079 else
6080 return res;
6081}
6082
6083int PyAST_Check(PyObject* obj)
6084{
6085 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006086 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006087}
6088
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006089