/Python/Python-ast.c

http://unladen-swallow.googlecode.com/ · C · 6134 lines · 5910 code · 206 blank · 18 comment · 1945 complexity · abfbffdcfe07f2a65d5898b87275d44b MD5 · raw file

Large files are truncated click here to view the full file

  1. /* File automatically generated by Parser/asdl_c.py. */
  2. /*
  3. __version__ 62047.
  4. This module must be committed separately after each AST grammar change;
  5. The __version__ number is set to the revision number of the commit
  6. containing the grammar change.
  7. */
  8. #include "Python.h"
  9. #include "Python-ast.h"
  10. static PyTypeObject AST_type;
  11. static PyTypeObject *mod_type;
  12. static PyObject* ast2obj_mod(void*);
  13. static PyTypeObject *Module_type;
  14. static char *Module_fields[]={
  15. "body",
  16. };
  17. static PyTypeObject *Interactive_type;
  18. static char *Interactive_fields[]={
  19. "body",
  20. };
  21. static PyTypeObject *Expression_type;
  22. static char *Expression_fields[]={
  23. "body",
  24. };
  25. static PyTypeObject *Suite_type;
  26. static char *Suite_fields[]={
  27. "body",
  28. };
  29. static PyTypeObject *stmt_type;
  30. static char *stmt_attributes[] = {
  31. "lineno",
  32. "col_offset",
  33. };
  34. static PyObject* ast2obj_stmt(void*);
  35. static PyTypeObject *FunctionDef_type;
  36. static char *FunctionDef_fields[]={
  37. "name",
  38. "args",
  39. "body",
  40. "decorator_list",
  41. };
  42. static PyTypeObject *ClassDef_type;
  43. static char *ClassDef_fields[]={
  44. "name",
  45. "bases",
  46. "body",
  47. "decorator_list",
  48. };
  49. static PyTypeObject *Return_type;
  50. static char *Return_fields[]={
  51. "value",
  52. };
  53. static PyTypeObject *Delete_type;
  54. static char *Delete_fields[]={
  55. "targets",
  56. };
  57. static PyTypeObject *Assign_type;
  58. static char *Assign_fields[]={
  59. "targets",
  60. "value",
  61. };
  62. static PyTypeObject *AugAssign_type;
  63. static char *AugAssign_fields[]={
  64. "target",
  65. "op",
  66. "value",
  67. };
  68. static PyTypeObject *Print_type;
  69. static char *Print_fields[]={
  70. "dest",
  71. "values",
  72. "nl",
  73. };
  74. static PyTypeObject *For_type;
  75. static char *For_fields[]={
  76. "target",
  77. "iter",
  78. "body",
  79. "orelse",
  80. };
  81. static PyTypeObject *While_type;
  82. static char *While_fields[]={
  83. "test",
  84. "body",
  85. "orelse",
  86. };
  87. static PyTypeObject *If_type;
  88. static char *If_fields[]={
  89. "test",
  90. "body",
  91. "orelse",
  92. };
  93. static PyTypeObject *With_type;
  94. static char *With_fields[]={
  95. "context_expr",
  96. "optional_vars",
  97. "body",
  98. };
  99. static PyTypeObject *Raise_type;
  100. static char *Raise_fields[]={
  101. "type",
  102. "inst",
  103. "tback",
  104. };
  105. static PyTypeObject *TryExcept_type;
  106. static char *TryExcept_fields[]={
  107. "body",
  108. "handlers",
  109. "orelse",
  110. };
  111. static PyTypeObject *TryFinally_type;
  112. static char *TryFinally_fields[]={
  113. "body",
  114. "finalbody",
  115. };
  116. static PyTypeObject *Assert_type;
  117. static char *Assert_fields[]={
  118. "test",
  119. "msg",
  120. };
  121. static PyTypeObject *Import_type;
  122. static char *Import_fields[]={
  123. "names",
  124. };
  125. static PyTypeObject *ImportFrom_type;
  126. static char *ImportFrom_fields[]={
  127. "module",
  128. "names",
  129. "level",
  130. };
  131. static PyTypeObject *Exec_type;
  132. static char *Exec_fields[]={
  133. "body",
  134. "globals",
  135. "locals",
  136. };
  137. static PyTypeObject *Global_type;
  138. static char *Global_fields[]={
  139. "names",
  140. };
  141. static PyTypeObject *Expr_type;
  142. static char *Expr_fields[]={
  143. "value",
  144. };
  145. static PyTypeObject *Pass_type;
  146. static PyTypeObject *Break_type;
  147. static PyTypeObject *Continue_type;
  148. static PyTypeObject *expr_type;
  149. static char *expr_attributes[] = {
  150. "lineno",
  151. "col_offset",
  152. };
  153. static PyObject* ast2obj_expr(void*);
  154. static PyTypeObject *BoolOp_type;
  155. static char *BoolOp_fields[]={
  156. "op",
  157. "values",
  158. };
  159. static PyTypeObject *BinOp_type;
  160. static char *BinOp_fields[]={
  161. "left",
  162. "op",
  163. "right",
  164. };
  165. static PyTypeObject *UnaryOp_type;
  166. static char *UnaryOp_fields[]={
  167. "op",
  168. "operand",
  169. };
  170. static PyTypeObject *Lambda_type;
  171. static char *Lambda_fields[]={
  172. "args",
  173. "body",
  174. };
  175. static PyTypeObject *IfExp_type;
  176. static char *IfExp_fields[]={
  177. "test",
  178. "body",
  179. "orelse",
  180. };
  181. static PyTypeObject *Dict_type;
  182. static char *Dict_fields[]={
  183. "keys",
  184. "values",
  185. };
  186. static PyTypeObject *ListComp_type;
  187. static char *ListComp_fields[]={
  188. "elt",
  189. "generators",
  190. };
  191. static PyTypeObject *GeneratorExp_type;
  192. static char *GeneratorExp_fields[]={
  193. "elt",
  194. "generators",
  195. };
  196. static PyTypeObject *Yield_type;
  197. static char *Yield_fields[]={
  198. "value",
  199. };
  200. static PyTypeObject *Compare_type;
  201. static char *Compare_fields[]={
  202. "left",
  203. "ops",
  204. "comparators",
  205. };
  206. static PyTypeObject *Call_type;
  207. static char *Call_fields[]={
  208. "func",
  209. "args",
  210. "keywords",
  211. "starargs",
  212. "kwargs",
  213. };
  214. static PyTypeObject *Repr_type;
  215. static char *Repr_fields[]={
  216. "value",
  217. };
  218. static PyTypeObject *Num_type;
  219. static char *Num_fields[]={
  220. "n",
  221. };
  222. static PyTypeObject *Str_type;
  223. static char *Str_fields[]={
  224. "s",
  225. };
  226. static PyTypeObject *Attribute_type;
  227. static char *Attribute_fields[]={
  228. "value",
  229. "attr",
  230. "ctx",
  231. };
  232. static PyTypeObject *Subscript_type;
  233. static char *Subscript_fields[]={
  234. "value",
  235. "slice",
  236. "ctx",
  237. };
  238. static PyTypeObject *Name_type;
  239. static char *Name_fields[]={
  240. "id",
  241. "ctx",
  242. };
  243. static PyTypeObject *List_type;
  244. static char *List_fields[]={
  245. "elts",
  246. "ctx",
  247. };
  248. static PyTypeObject *Tuple_type;
  249. static char *Tuple_fields[]={
  250. "elts",
  251. "ctx",
  252. };
  253. static PyTypeObject *expr_context_type;
  254. static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
  255. *AugLoad_singleton, *AugStore_singleton, *Param_singleton;
  256. static PyObject* ast2obj_expr_context(expr_context_ty);
  257. static PyTypeObject *Load_type;
  258. static PyTypeObject *Store_type;
  259. static PyTypeObject *Del_type;
  260. static PyTypeObject *AugLoad_type;
  261. static PyTypeObject *AugStore_type;
  262. static PyTypeObject *Param_type;
  263. static PyTypeObject *slice_type;
  264. static PyObject* ast2obj_slice(void*);
  265. static PyTypeObject *Ellipsis_type;
  266. static PyTypeObject *Slice_type;
  267. static char *Slice_fields[]={
  268. "lower",
  269. "upper",
  270. "step",
  271. };
  272. static PyTypeObject *ExtSlice_type;
  273. static char *ExtSlice_fields[]={
  274. "dims",
  275. };
  276. static PyTypeObject *Index_type;
  277. static char *Index_fields[]={
  278. "value",
  279. };
  280. static PyTypeObject *boolop_type;
  281. static PyObject *And_singleton, *Or_singleton;
  282. static PyObject* ast2obj_boolop(boolop_ty);
  283. static PyTypeObject *And_type;
  284. static PyTypeObject *Or_type;
  285. static PyTypeObject *operator_type;
  286. static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
  287. *Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
  288. *RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
  289. *FloorDiv_singleton;
  290. static PyObject* ast2obj_operator(operator_ty);
  291. static PyTypeObject *Add_type;
  292. static PyTypeObject *Sub_type;
  293. static PyTypeObject *Mult_type;
  294. static PyTypeObject *Div_type;
  295. static PyTypeObject *Mod_type;
  296. static PyTypeObject *Pow_type;
  297. static PyTypeObject *LShift_type;
  298. static PyTypeObject *RShift_type;
  299. static PyTypeObject *BitOr_type;
  300. static PyTypeObject *BitXor_type;
  301. static PyTypeObject *BitAnd_type;
  302. static PyTypeObject *FloorDiv_type;
  303. static PyTypeObject *unaryop_type;
  304. static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
  305. *USub_singleton;
  306. static PyObject* ast2obj_unaryop(unaryop_ty);
  307. static PyTypeObject *Invert_type;
  308. static PyTypeObject *Not_type;
  309. static PyTypeObject *UAdd_type;
  310. static PyTypeObject *USub_type;
  311. static PyTypeObject *cmpop_type;
  312. static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
  313. *Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
  314. *NotIn_singleton;
  315. static PyObject* ast2obj_cmpop(cmpop_ty);
  316. static PyTypeObject *Eq_type;
  317. static PyTypeObject *NotEq_type;
  318. static PyTypeObject *Lt_type;
  319. static PyTypeObject *LtE_type;
  320. static PyTypeObject *Gt_type;
  321. static PyTypeObject *GtE_type;
  322. static PyTypeObject *Is_type;
  323. static PyTypeObject *IsNot_type;
  324. static PyTypeObject *In_type;
  325. static PyTypeObject *NotIn_type;
  326. static PyTypeObject *comprehension_type;
  327. static PyObject* ast2obj_comprehension(void*);
  328. static char *comprehension_fields[]={
  329. "target",
  330. "iter",
  331. "ifs",
  332. };
  333. static PyTypeObject *excepthandler_type;
  334. static char *excepthandler_attributes[] = {
  335. "lineno",
  336. "col_offset",
  337. };
  338. static PyObject* ast2obj_excepthandler(void*);
  339. static PyTypeObject *ExceptHandler_type;
  340. static char *ExceptHandler_fields[]={
  341. "type",
  342. "name",
  343. "body",
  344. };
  345. static PyTypeObject *arguments_type;
  346. static PyObject* ast2obj_arguments(void*);
  347. static char *arguments_fields[]={
  348. "args",
  349. "vararg",
  350. "kwarg",
  351. "defaults",
  352. };
  353. static PyTypeObject *keyword_type;
  354. static PyObject* ast2obj_keyword(void*);
  355. static char *keyword_fields[]={
  356. "arg",
  357. "value",
  358. };
  359. static PyTypeObject *alias_type;
  360. static PyObject* ast2obj_alias(void*);
  361. static char *alias_fields[]={
  362. "name",
  363. "asname",
  364. };
  365. static int
  366. ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
  367. {
  368. Py_ssize_t i, numfields = 0;
  369. int res = -1;
  370. PyObject *key, *value, *fields;
  371. fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
  372. if (!fields)
  373. PyErr_Clear();
  374. if (fields) {
  375. numfields = PySequence_Size(fields);
  376. if (numfields == -1)
  377. goto cleanup;
  378. }
  379. res = 0; /* if no error occurs, this stays 0 to the end */
  380. if (PyTuple_GET_SIZE(args) > 0) {
  381. if (numfields != PyTuple_GET_SIZE(args)) {
  382. PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
  383. "%zd positional argument%s",
  384. Py_TYPE(self)->tp_name,
  385. numfields == 0 ? "" : "either 0 or ",
  386. numfields, numfields == 1 ? "" : "s");
  387. res = -1;
  388. goto cleanup;
  389. }
  390. for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
  391. /* cannot be reached when fields is NULL */
  392. PyObject *name = PySequence_GetItem(fields, i);
  393. if (!name) {
  394. res = -1;
  395. goto cleanup;
  396. }
  397. res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
  398. Py_DECREF(name);
  399. if (res < 0)
  400. goto cleanup;
  401. }
  402. }
  403. if (kw) {
  404. i = 0; /* needed by PyDict_Next */
  405. while (PyDict_Next(kw, &i, &key, &value)) {
  406. res = PyObject_SetAttr(self, key, value);
  407. if (res < 0)
  408. goto cleanup;
  409. }
  410. }
  411. cleanup:
  412. Py_XDECREF(fields);
  413. return res;
  414. }
  415. /* Pickling support */
  416. static PyObject *
  417. ast_type_reduce(PyObject *self, PyObject *unused)
  418. {
  419. PyObject *res;
  420. PyObject *dict = PyObject_GetAttrString(self, "__dict__");
  421. if (dict == NULL) {
  422. if (PyErr_ExceptionMatches(PyExc_AttributeError))
  423. PyErr_Clear();
  424. else
  425. return NULL;
  426. }
  427. if (dict) {
  428. res = Py_BuildValue("O()O", Py_TYPE(self), dict);
  429. Py_DECREF(dict);
  430. return res;
  431. }
  432. return Py_BuildValue("O()", Py_TYPE(self));
  433. }
  434. static PyMethodDef ast_type_methods[] = {
  435. {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
  436. {NULL}
  437. };
  438. static PyTypeObject AST_type = {
  439. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  440. "_ast.AST",
  441. sizeof(PyObject),
  442. 0,
  443. 0, /* tp_dealloc */
  444. 0, /* tp_print */
  445. 0, /* tp_getattr */
  446. 0, /* tp_setattr */
  447. 0, /* tp_compare */
  448. 0, /* tp_repr */
  449. 0, /* tp_as_number */
  450. 0, /* tp_as_sequence */
  451. 0, /* tp_as_mapping */
  452. 0, /* tp_hash */
  453. 0, /* tp_call */
  454. 0, /* tp_str */
  455. PyObject_GenericGetAttr, /* tp_getattro */
  456. PyObject_GenericSetAttr, /* tp_setattro */
  457. 0, /* tp_as_buffer */
  458. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  459. 0, /* tp_doc */
  460. 0, /* tp_traverse */
  461. 0, /* tp_clear */
  462. 0, /* tp_richcompare */
  463. 0, /* tp_weaklistoffset */
  464. 0, /* tp_iter */
  465. 0, /* tp_iternext */
  466. ast_type_methods, /* tp_methods */
  467. 0, /* tp_members */
  468. 0, /* tp_getset */
  469. 0, /* tp_base */
  470. 0, /* tp_dict */
  471. 0, /* tp_descr_get */
  472. 0, /* tp_descr_set */
  473. 0, /* tp_dictoffset */
  474. (initproc)ast_type_init, /* tp_init */
  475. PyType_GenericAlloc, /* tp_alloc */
  476. PyType_GenericNew, /* tp_new */
  477. PyObject_Del, /* tp_free */
  478. };
  479. static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
  480. {
  481. PyObject *fnames, *result;
  482. int i;
  483. fnames = PyTuple_New(num_fields);
  484. if (!fnames) return NULL;
  485. for (i = 0; i < num_fields; i++) {
  486. PyObject *field = PyString_FromString(fields[i]);
  487. if (!field) {
  488. Py_DECREF(fnames);
  489. return NULL;
  490. }
  491. PyTuple_SET_ITEM(fnames, i, field);
  492. }
  493. result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
  494. type, base, "_fields", fnames, "__module__", "_ast");
  495. Py_DECREF(fnames);
  496. return (PyTypeObject*)result;
  497. }
  498. static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
  499. {
  500. int i, result;
  501. PyObject *s, *l = PyTuple_New(num_fields);
  502. if (!l) return 0;
  503. for(i = 0; i < num_fields; i++) {
  504. s = PyString_FromString(attrs[i]);
  505. if (!s) {
  506. Py_DECREF(l);
  507. return 0;
  508. }
  509. PyTuple_SET_ITEM(l, i, s);
  510. }
  511. result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
  512. Py_DECREF(l);
  513. return result;
  514. }
  515. /* Conversion AST -> Python */
  516. static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
  517. {
  518. int i, n = asdl_seq_LEN(seq);
  519. PyObject *result = PyList_New(n);
  520. PyObject *value;
  521. if (!result)
  522. return NULL;
  523. for (i = 0; i < n; i++) {
  524. value = func(asdl_seq_GET(seq, i));
  525. if (!value) {
  526. Py_DECREF(result);
  527. return NULL;
  528. }
  529. PyList_SET_ITEM(result, i, value);
  530. }
  531. return result;
  532. }
  533. static PyObject* ast2obj_object(void *o)
  534. {
  535. if (!o)
  536. o = Py_None;
  537. Py_INCREF((PyObject*)o);
  538. return (PyObject*)o;
  539. }
  540. #define ast2obj_identifier ast2obj_object
  541. #define ast2obj_string ast2obj_object
  542. static PyObject* ast2obj_bool(bool b)
  543. {
  544. return PyBool_FromLong(b);
  545. }
  546. static PyObject* ast2obj_int(long b)
  547. {
  548. return PyInt_FromLong(b);
  549. }
  550. /* Conversion Python -> AST */
  551. static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
  552. {
  553. if (obj == Py_None)
  554. obj = NULL;
  555. if (obj)
  556. PyArena_AddPyObject(arena, obj);
  557. Py_XINCREF(obj);
  558. *out = obj;
  559. return 0;
  560. }
  561. #define obj2ast_identifier obj2ast_object
  562. #define obj2ast_string obj2ast_object
  563. static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
  564. {
  565. int i;
  566. if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
  567. PyObject *s = PyObject_Repr(obj);
  568. if (s == NULL) return 1;
  569. PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
  570. PyString_AS_STRING(s));
  571. Py_DECREF(s);
  572. return 1;
  573. }
  574. i = (int)PyLong_AsLong(obj);
  575. if (i == -1 && PyErr_Occurred())
  576. return 1;
  577. *out = i;
  578. return 0;
  579. }
  580. static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
  581. {
  582. if (!PyBool_Check(obj)) {
  583. PyObject *s = PyObject_Repr(obj);
  584. if (s == NULL) return 1;
  585. PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
  586. PyString_AS_STRING(s));
  587. Py_DECREF(s);
  588. return 1;
  589. }
  590. *out = (obj == Py_True);
  591. return 0;
  592. }
  593. static int add_ast_fields(void)
  594. {
  595. PyObject *empty_tuple, *d;
  596. if (PyType_Ready(&AST_type) < 0)
  597. return -1;
  598. d = AST_type.tp_dict;
  599. empty_tuple = PyTuple_New(0);
  600. if (!empty_tuple ||
  601. PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
  602. PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
  603. Py_XDECREF(empty_tuple);
  604. return -1;
  605. }
  606. Py_DECREF(empty_tuple);
  607. return 0;
  608. }
  609. static int init_types(void)
  610. {
  611. static int initialized;
  612. if (initialized) return 1;
  613. if (add_ast_fields() < 0) return 0;
  614. mod_type = make_type("mod", &AST_type, NULL, 0);
  615. if (!mod_type) return 0;
  616. if (!add_attributes(mod_type, NULL, 0)) return 0;
  617. Module_type = make_type("Module", mod_type, Module_fields, 1);
  618. if (!Module_type) return 0;
  619. Interactive_type = make_type("Interactive", mod_type,
  620. Interactive_fields, 1);
  621. if (!Interactive_type) return 0;
  622. Expression_type = make_type("Expression", mod_type, Expression_fields,
  623. 1);
  624. if (!Expression_type) return 0;
  625. Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
  626. if (!Suite_type) return 0;
  627. stmt_type = make_type("stmt", &AST_type, NULL, 0);
  628. if (!stmt_type) return 0;
  629. if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
  630. FunctionDef_type = make_type("FunctionDef", stmt_type,
  631. FunctionDef_fields, 4);
  632. if (!FunctionDef_type) return 0;
  633. ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
  634. if (!ClassDef_type) return 0;
  635. Return_type = make_type("Return", stmt_type, Return_fields, 1);
  636. if (!Return_type) return 0;
  637. Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
  638. if (!Delete_type) return 0;
  639. Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
  640. if (!Assign_type) return 0;
  641. AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
  642. if (!AugAssign_type) return 0;
  643. Print_type = make_type("Print", stmt_type, Print_fields, 3);
  644. if (!Print_type) return 0;
  645. For_type = make_type("For", stmt_type, For_fields, 4);
  646. if (!For_type) return 0;
  647. While_type = make_type("While", stmt_type, While_fields, 3);
  648. if (!While_type) return 0;
  649. If_type = make_type("If", stmt_type, If_fields, 3);
  650. if (!If_type) return 0;
  651. With_type = make_type("With", stmt_type, With_fields, 3);
  652. if (!With_type) return 0;
  653. Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
  654. if (!Raise_type) return 0;
  655. TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
  656. if (!TryExcept_type) return 0;
  657. TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
  658. 2);
  659. if (!TryFinally_type) return 0;
  660. Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
  661. if (!Assert_type) return 0;
  662. Import_type = make_type("Import", stmt_type, Import_fields, 1);
  663. if (!Import_type) return 0;
  664. ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
  665. 3);
  666. if (!ImportFrom_type) return 0;
  667. Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
  668. if (!Exec_type) return 0;
  669. Global_type = make_type("Global", stmt_type, Global_fields, 1);
  670. if (!Global_type) return 0;
  671. Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
  672. if (!Expr_type) return 0;
  673. Pass_type = make_type("Pass", stmt_type, NULL, 0);
  674. if (!Pass_type) return 0;
  675. Break_type = make_type("Break", stmt_type, NULL, 0);
  676. if (!Break_type) return 0;
  677. Continue_type = make_type("Continue", stmt_type, NULL, 0);
  678. if (!Continue_type) return 0;
  679. expr_type = make_type("expr", &AST_type, NULL, 0);
  680. if (!expr_type) return 0;
  681. if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
  682. BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
  683. if (!BoolOp_type) return 0;
  684. BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
  685. if (!BinOp_type) return 0;
  686. UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
  687. if (!UnaryOp_type) return 0;
  688. Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
  689. if (!Lambda_type) return 0;
  690. IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
  691. if (!IfExp_type) return 0;
  692. Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
  693. if (!Dict_type) return 0;
  694. ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
  695. if (!ListComp_type) return 0;
  696. GeneratorExp_type = make_type("GeneratorExp", expr_type,
  697. GeneratorExp_fields, 2);
  698. if (!GeneratorExp_type) return 0;
  699. Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
  700. if (!Yield_type) return 0;
  701. Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
  702. if (!Compare_type) return 0;
  703. Call_type = make_type("Call", expr_type, Call_fields, 5);
  704. if (!Call_type) return 0;
  705. Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
  706. if (!Repr_type) return 0;
  707. Num_type = make_type("Num", expr_type, Num_fields, 1);
  708. if (!Num_type) return 0;
  709. Str_type = make_type("Str", expr_type, Str_fields, 1);
  710. if (!Str_type) return 0;
  711. Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
  712. if (!Attribute_type) return 0;
  713. Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
  714. if (!Subscript_type) return 0;
  715. Name_type = make_type("Name", expr_type, Name_fields, 2);
  716. if (!Name_type) return 0;
  717. List_type = make_type("List", expr_type, List_fields, 2);
  718. if (!List_type) return 0;
  719. Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
  720. if (!Tuple_type) return 0;
  721. expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
  722. if (!expr_context_type) return 0;
  723. if (!add_attributes(expr_context_type, NULL, 0)) return 0;
  724. Load_type = make_type("Load", expr_context_type, NULL, 0);
  725. if (!Load_type) return 0;
  726. Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
  727. if (!Load_singleton) return 0;
  728. Store_type = make_type("Store", expr_context_type, NULL, 0);
  729. if (!Store_type) return 0;
  730. Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
  731. if (!Store_singleton) return 0;
  732. Del_type = make_type("Del", expr_context_type, NULL, 0);
  733. if (!Del_type) return 0;
  734. Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
  735. if (!Del_singleton) return 0;
  736. AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
  737. if (!AugLoad_type) return 0;
  738. AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
  739. if (!AugLoad_singleton) return 0;
  740. AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
  741. if (!AugStore_type) return 0;
  742. AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
  743. if (!AugStore_singleton) return 0;
  744. Param_type = make_type("Param", expr_context_type, NULL, 0);
  745. if (!Param_type) return 0;
  746. Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
  747. if (!Param_singleton) return 0;
  748. slice_type = make_type("slice", &AST_type, NULL, 0);
  749. if (!slice_type) return 0;
  750. if (!add_attributes(slice_type, NULL, 0)) return 0;
  751. Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
  752. if (!Ellipsis_type) return 0;
  753. Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
  754. if (!Slice_type) return 0;
  755. ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
  756. if (!ExtSlice_type) return 0;
  757. Index_type = make_type("Index", slice_type, Index_fields, 1);
  758. if (!Index_type) return 0;
  759. boolop_type = make_type("boolop", &AST_type, NULL, 0);
  760. if (!boolop_type) return 0;
  761. if (!add_attributes(boolop_type, NULL, 0)) return 0;
  762. And_type = make_type("And", boolop_type, NULL, 0);
  763. if (!And_type) return 0;
  764. And_singleton = PyType_GenericNew(And_type, NULL, NULL);
  765. if (!And_singleton) return 0;
  766. Or_type = make_type("Or", boolop_type, NULL, 0);
  767. if (!Or_type) return 0;
  768. Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
  769. if (!Or_singleton) return 0;
  770. operator_type = make_type("operator", &AST_type, NULL, 0);
  771. if (!operator_type) return 0;
  772. if (!add_attributes(operator_type, NULL, 0)) return 0;
  773. Add_type = make_type("Add", operator_type, NULL, 0);
  774. if (!Add_type) return 0;
  775. Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
  776. if (!Add_singleton) return 0;
  777. Sub_type = make_type("Sub", operator_type, NULL, 0);
  778. if (!Sub_type) return 0;
  779. Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
  780. if (!Sub_singleton) return 0;
  781. Mult_type = make_type("Mult", operator_type, NULL, 0);
  782. if (!Mult_type) return 0;
  783. Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
  784. if (!Mult_singleton) return 0;
  785. Div_type = make_type("Div", operator_type, NULL, 0);
  786. if (!Div_type) return 0;
  787. Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
  788. if (!Div_singleton) return 0;
  789. Mod_type = make_type("Mod", operator_type, NULL, 0);
  790. if (!Mod_type) return 0;
  791. Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
  792. if (!Mod_singleton) return 0;
  793. Pow_type = make_type("Pow", operator_type, NULL, 0);
  794. if (!Pow_type) return 0;
  795. Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
  796. if (!Pow_singleton) return 0;
  797. LShift_type = make_type("LShift", operator_type, NULL, 0);
  798. if (!LShift_type) return 0;
  799. LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
  800. if (!LShift_singleton) return 0;
  801. RShift_type = make_type("RShift", operator_type, NULL, 0);
  802. if (!RShift_type) return 0;
  803. RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
  804. if (!RShift_singleton) return 0;
  805. BitOr_type = make_type("BitOr", operator_type, NULL, 0);
  806. if (!BitOr_type) return 0;
  807. BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
  808. if (!BitOr_singleton) return 0;
  809. BitXor_type = make_type("BitXor", operator_type, NULL, 0);
  810. if (!BitXor_type) return 0;
  811. BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
  812. if (!BitXor_singleton) return 0;
  813. BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
  814. if (!BitAnd_type) return 0;
  815. BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
  816. if (!BitAnd_singleton) return 0;
  817. FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
  818. if (!FloorDiv_type) return 0;
  819. FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
  820. if (!FloorDiv_singleton) return 0;
  821. unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
  822. if (!unaryop_type) return 0;
  823. if (!add_attributes(unaryop_type, NULL, 0)) return 0;
  824. Invert_type = make_type("Invert", unaryop_type, NULL, 0);
  825. if (!Invert_type) return 0;
  826. Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
  827. if (!Invert_singleton) return 0;
  828. Not_type = make_type("Not", unaryop_type, NULL, 0);
  829. if (!Not_type) return 0;
  830. Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
  831. if (!Not_singleton) return 0;
  832. UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
  833. if (!UAdd_type) return 0;
  834. UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
  835. if (!UAdd_singleton) return 0;
  836. USub_type = make_type("USub", unaryop_type, NULL, 0);
  837. if (!USub_type) return 0;
  838. USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
  839. if (!USub_singleton) return 0;
  840. cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
  841. if (!cmpop_type) return 0;
  842. if (!add_attributes(cmpop_type, NULL, 0)) return 0;
  843. Eq_type = make_type("Eq", cmpop_type, NULL, 0);
  844. if (!Eq_type) return 0;
  845. Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
  846. if (!Eq_singleton) return 0;
  847. NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
  848. if (!NotEq_type) return 0;
  849. NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
  850. if (!NotEq_singleton) return 0;
  851. Lt_type = make_type("Lt", cmpop_type, NULL, 0);
  852. if (!Lt_type) return 0;
  853. Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
  854. if (!Lt_singleton) return 0;
  855. LtE_type = make_type("LtE", cmpop_type, NULL, 0);
  856. if (!LtE_type) return 0;
  857. LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
  858. if (!LtE_singleton) return 0;
  859. Gt_type = make_type("Gt", cmpop_type, NULL, 0);
  860. if (!Gt_type) return 0;
  861. Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
  862. if (!Gt_singleton) return 0;
  863. GtE_type = make_type("GtE", cmpop_type, NULL, 0);
  864. if (!GtE_type) return 0;
  865. GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
  866. if (!GtE_singleton) return 0;
  867. Is_type = make_type("Is", cmpop_type, NULL, 0);
  868. if (!Is_type) return 0;
  869. Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
  870. if (!Is_singleton) return 0;
  871. IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
  872. if (!IsNot_type) return 0;
  873. IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
  874. if (!IsNot_singleton) return 0;
  875. In_type = make_type("In", cmpop_type, NULL, 0);
  876. if (!In_type) return 0;
  877. In_singleton = PyType_GenericNew(In_type, NULL, NULL);
  878. if (!In_singleton) return 0;
  879. NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
  880. if (!NotIn_type) return 0;
  881. NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
  882. if (!NotIn_singleton) return 0;
  883. comprehension_type = make_type("comprehension", &AST_type,
  884. comprehension_fields, 3);
  885. if (!comprehension_type) return 0;
  886. excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
  887. if (!excepthandler_type) return 0;
  888. if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
  889. return 0;
  890. ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
  891. ExceptHandler_fields, 3);
  892. if (!ExceptHandler_type) return 0;
  893. arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
  894. if (!arguments_type) return 0;
  895. keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
  896. if (!keyword_type) return 0;
  897. alias_type = make_type("alias", &AST_type, alias_fields, 2);
  898. if (!alias_type) return 0;
  899. initialized = 1;
  900. return 1;
  901. }
  902. static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
  903. static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
  904. static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
  905. static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
  906. arena);
  907. static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
  908. static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
  909. static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
  910. static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
  911. static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
  912. static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
  913. arena);
  914. static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
  915. arena);
  916. static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
  917. static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
  918. static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
  919. mod_ty
  920. Module(asdl_seq * body, PyArena *arena)
  921. {
  922. mod_ty p;
  923. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  924. if (!p)
  925. return NULL;
  926. p->kind = Module_kind;
  927. p->v.Module.body = body;
  928. return p;
  929. }
  930. mod_ty
  931. Interactive(asdl_seq * body, PyArena *arena)
  932. {
  933. mod_ty p;
  934. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  935. if (!p)
  936. return NULL;
  937. p->kind = Interactive_kind;
  938. p->v.Interactive.body = body;
  939. return p;
  940. }
  941. mod_ty
  942. Expression(expr_ty body, PyArena *arena)
  943. {
  944. mod_ty p;
  945. if (!body) {
  946. PyErr_SetString(PyExc_ValueError,
  947. "field body is required for Expression");
  948. return NULL;
  949. }
  950. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  951. if (!p)
  952. return NULL;
  953. p->kind = Expression_kind;
  954. p->v.Expression.body = body;
  955. return p;
  956. }
  957. mod_ty
  958. Suite(asdl_seq * body, PyArena *arena)
  959. {
  960. mod_ty p;
  961. p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
  962. if (!p)
  963. return NULL;
  964. p->kind = Suite_kind;
  965. p->v.Suite.body = body;
  966. return p;
  967. }
  968. stmt_ty
  969. FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
  970. decorator_list, int lineno, int col_offset, PyArena *arena)
  971. {
  972. stmt_ty p;
  973. if (!name) {
  974. PyErr_SetString(PyExc_ValueError,
  975. "field name is required for FunctionDef");
  976. return NULL;
  977. }
  978. if (!args) {
  979. PyErr_SetString(PyExc_ValueError,
  980. "field args is required for FunctionDef");
  981. return NULL;
  982. }
  983. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  984. if (!p)
  985. return NULL;
  986. p->kind = FunctionDef_kind;
  987. p->v.FunctionDef.name = name;
  988. p->v.FunctionDef.args = args;
  989. p->v.FunctionDef.body = body;
  990. p->v.FunctionDef.decorator_list = decorator_list;
  991. p->lineno = lineno;
  992. p->col_offset = col_offset;
  993. return p;
  994. }
  995. stmt_ty
  996. ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
  997. decorator_list, int lineno, int col_offset, PyArena *arena)
  998. {
  999. stmt_ty p;
  1000. if (!name) {
  1001. PyErr_SetString(PyExc_ValueError,
  1002. "field name is required for ClassDef");
  1003. return NULL;
  1004. }
  1005. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1006. if (!p)
  1007. return NULL;
  1008. p->kind = ClassDef_kind;
  1009. p->v.ClassDef.name = name;
  1010. p->v.ClassDef.bases = bases;
  1011. p->v.ClassDef.body = body;
  1012. p->v.ClassDef.decorator_list = decorator_list;
  1013. p->lineno = lineno;
  1014. p->col_offset = col_offset;
  1015. return p;
  1016. }
  1017. stmt_ty
  1018. Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
  1019. {
  1020. stmt_ty p;
  1021. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1022. if (!p)
  1023. return NULL;
  1024. p->kind = Return_kind;
  1025. p->v.Return.value = value;
  1026. p->lineno = lineno;
  1027. p->col_offset = col_offset;
  1028. return p;
  1029. }
  1030. stmt_ty
  1031. Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
  1032. {
  1033. stmt_ty p;
  1034. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1035. if (!p)
  1036. return NULL;
  1037. p->kind = Delete_kind;
  1038. p->v.Delete.targets = targets;
  1039. p->lineno = lineno;
  1040. p->col_offset = col_offset;
  1041. return p;
  1042. }
  1043. stmt_ty
  1044. Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
  1045. *arena)
  1046. {
  1047. stmt_ty p;
  1048. if (!value) {
  1049. PyErr_SetString(PyExc_ValueError,
  1050. "field value is required for Assign");
  1051. return NULL;
  1052. }
  1053. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1054. if (!p)
  1055. return NULL;
  1056. p->kind = Assign_kind;
  1057. p->v.Assign.targets = targets;
  1058. p->v.Assign.value = value;
  1059. p->lineno = lineno;
  1060. p->col_offset = col_offset;
  1061. return p;
  1062. }
  1063. stmt_ty
  1064. AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
  1065. col_offset, PyArena *arena)
  1066. {
  1067. stmt_ty p;
  1068. if (!target) {
  1069. PyErr_SetString(PyExc_ValueError,
  1070. "field target is required for AugAssign");
  1071. return NULL;
  1072. }
  1073. if (!op) {
  1074. PyErr_SetString(PyExc_ValueError,
  1075. "field op is required for AugAssign");
  1076. return NULL;
  1077. }
  1078. if (!value) {
  1079. PyErr_SetString(PyExc_ValueError,
  1080. "field value is required for AugAssign");
  1081. return NULL;
  1082. }
  1083. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1084. if (!p)
  1085. return NULL;
  1086. p->kind = AugAssign_kind;
  1087. p->v.AugAssign.target = target;
  1088. p->v.AugAssign.op = op;
  1089. p->v.AugAssign.value = value;
  1090. p->lineno = lineno;
  1091. p->col_offset = col_offset;
  1092. return p;
  1093. }
  1094. stmt_ty
  1095. Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
  1096. PyArena *arena)
  1097. {
  1098. stmt_ty p;
  1099. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1100. if (!p)
  1101. return NULL;
  1102. p->kind = Print_kind;
  1103. p->v.Print.dest = dest;
  1104. p->v.Print.values = values;
  1105. p->v.Print.nl = nl;
  1106. p->lineno = lineno;
  1107. p->col_offset = col_offset;
  1108. return p;
  1109. }
  1110. stmt_ty
  1111. For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
  1112. lineno, int col_offset, PyArena *arena)
  1113. {
  1114. stmt_ty p;
  1115. if (!target) {
  1116. PyErr_SetString(PyExc_ValueError,
  1117. "field target is required for For");
  1118. return NULL;
  1119. }
  1120. if (!iter) {
  1121. PyErr_SetString(PyExc_ValueError,
  1122. "field iter is required for For");
  1123. return NULL;
  1124. }
  1125. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1126. if (!p)
  1127. return NULL;
  1128. p->kind = For_kind;
  1129. p->v.For.target = target;
  1130. p->v.For.iter = iter;
  1131. p->v.For.body = body;
  1132. p->v.For.orelse = orelse;
  1133. p->lineno = lineno;
  1134. p->col_offset = col_offset;
  1135. return p;
  1136. }
  1137. stmt_ty
  1138. While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
  1139. col_offset, PyArena *arena)
  1140. {
  1141. stmt_ty p;
  1142. if (!test) {
  1143. PyErr_SetString(PyExc_ValueError,
  1144. "field test is required for While");
  1145. return NULL;
  1146. }
  1147. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1148. if (!p)
  1149. return NULL;
  1150. p->kind = While_kind;
  1151. p->v.While.test = test;
  1152. p->v.While.body = body;
  1153. p->v.While.orelse = orelse;
  1154. p->lineno = lineno;
  1155. p->col_offset = col_offset;
  1156. return p;
  1157. }
  1158. stmt_ty
  1159. If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
  1160. col_offset, PyArena *arena)
  1161. {
  1162. stmt_ty p;
  1163. if (!test) {
  1164. PyErr_SetString(PyExc_ValueError,
  1165. "field test is required for If");
  1166. return NULL;
  1167. }
  1168. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1169. if (!p)
  1170. return NULL;
  1171. p->kind = If_kind;
  1172. p->v.If.test = test;
  1173. p->v.If.body = body;
  1174. p->v.If.orelse = orelse;
  1175. p->lineno = lineno;
  1176. p->col_offset = col_offset;
  1177. return p;
  1178. }
  1179. stmt_ty
  1180. With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
  1181. int col_offset, PyArena *arena)
  1182. {
  1183. stmt_ty p;
  1184. if (!context_expr) {
  1185. PyErr_SetString(PyExc_ValueError,
  1186. "field context_expr is required for With");
  1187. return NULL;
  1188. }
  1189. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1190. if (!p)
  1191. return NULL;
  1192. p->kind = With_kind;
  1193. p->v.With.context_expr = context_expr;
  1194. p->v.With.optional_vars = optional_vars;
  1195. p->v.With.body = body;
  1196. p->lineno = lineno;
  1197. p->col_offset = col_offset;
  1198. return p;
  1199. }
  1200. stmt_ty
  1201. Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
  1202. PyArena *arena)
  1203. {
  1204. stmt_ty p;
  1205. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1206. if (!p)
  1207. return NULL;
  1208. p->kind = Raise_kind;
  1209. p->v.Raise.type = type;
  1210. p->v.Raise.inst = inst;
  1211. p->v.Raise.tback = tback;
  1212. p->lineno = lineno;
  1213. p->col_offset = col_offset;
  1214. return p;
  1215. }
  1216. stmt_ty
  1217. TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
  1218. int col_offset, PyArena *arena)
  1219. {
  1220. stmt_ty p;
  1221. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1222. if (!p)
  1223. return NULL;
  1224. 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;
  1229. p->col_offset = col_offset;
  1230. return p;
  1231. }
  1232. stmt_ty
  1233. TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
  1234. PyArena *arena)
  1235. {
  1236. stmt_ty p;
  1237. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1238. if (!p)
  1239. return NULL;
  1240. p->kind = TryFinally_kind;
  1241. p->v.TryFinally.body = body;
  1242. p->v.TryFinally.finalbody = finalbody;
  1243. p->lineno = lineno;
  1244. p->col_offset = col_offset;
  1245. return p;
  1246. }
  1247. stmt_ty
  1248. Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
  1249. {
  1250. stmt_ty p;
  1251. if (!test) {
  1252. PyErr_SetString(PyExc_ValueError,
  1253. "field test is required for Assert");
  1254. return NULL;
  1255. }
  1256. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1257. if (!p)
  1258. return NULL;
  1259. p->kind = Assert_kind;
  1260. p->v.Assert.test = test;
  1261. p->v.Assert.msg = msg;
  1262. p->lineno = lineno;
  1263. p->col_offset = col_offset;
  1264. return p;
  1265. }
  1266. stmt_ty
  1267. Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
  1268. {
  1269. stmt_ty p;
  1270. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1271. if (!p)
  1272. return NULL;
  1273. p->kind = Import_kind;
  1274. p->v.Import.names = names;
  1275. p->lineno = lineno;
  1276. p->col_offset = col_offset;
  1277. return p;
  1278. }
  1279. stmt_ty
  1280. ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
  1281. col_offset, PyArena *arena)
  1282. {
  1283. stmt_ty p;
  1284. if (!module) {
  1285. PyErr_SetString(PyExc_ValueError,
  1286. "field module is required for ImportFrom");
  1287. return NULL;
  1288. }
  1289. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1290. if (!p)
  1291. return NULL;
  1292. p->kind = ImportFrom_kind;
  1293. p->v.ImportFrom.module = module;
  1294. p->v.ImportFrom.names = names;
  1295. p->v.ImportFrom.level = level;
  1296. p->lineno = lineno;
  1297. p->col_offset = col_offset;
  1298. return p;
  1299. }
  1300. stmt_ty
  1301. Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
  1302. PyArena *arena)
  1303. {
  1304. stmt_ty p;
  1305. if (!body) {
  1306. PyErr_SetString(PyExc_ValueError,
  1307. "field body is required for Exec");
  1308. return NULL;
  1309. }
  1310. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1311. if (!p)
  1312. return NULL;
  1313. p->kind = Exec_kind;
  1314. p->v.Exec.body = body;
  1315. p->v.Exec.globals = globals;
  1316. p->v.Exec.locals = locals;
  1317. p->lineno = lineno;
  1318. p->col_offset = col_offset;
  1319. return p;
  1320. }
  1321. stmt_ty
  1322. Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
  1323. {
  1324. stmt_ty p;
  1325. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1326. if (!p)
  1327. return NULL;
  1328. p->kind = Global_kind;
  1329. p->v.Global.names = names;
  1330. p->lineno = lineno;
  1331. p->col_offset = col_offset;
  1332. return p;
  1333. }
  1334. stmt_ty
  1335. Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
  1336. {
  1337. stmt_ty p;
  1338. if (!value) {
  1339. PyErr_SetString(PyExc_ValueError,
  1340. "field value is required for Expr");
  1341. return NULL;
  1342. }
  1343. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1344. if (!p)
  1345. return NULL;
  1346. p->kind = Expr_kind;
  1347. p->v.Expr.value = value;
  1348. p->lineno = lineno;
  1349. p->col_offset = col_offset;
  1350. return p;
  1351. }
  1352. stmt_ty
  1353. Pass(int lineno, int col_offset, PyArena *arena)
  1354. {
  1355. stmt_ty p;
  1356. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1357. if (!p)
  1358. return NULL;
  1359. p->kind = Pass_kind;
  1360. p->lineno = lineno;
  1361. p->col_offset = col_offset;
  1362. return p;
  1363. }
  1364. stmt_ty
  1365. Break(int lineno, int col_offset, PyArena *arena)
  1366. {
  1367. stmt_ty p;
  1368. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1369. if (!p)
  1370. return NULL;
  1371. p->kind = Break_kind;
  1372. p->lineno = lineno;
  1373. p->col_offset = col_offset;
  1374. return p;
  1375. }
  1376. stmt_ty
  1377. Continue(int lineno, int col_offset, PyArena *arena)
  1378. {
  1379. stmt_ty p;
  1380. p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
  1381. if (!p)
  1382. return NULL;
  1383. p->kind = Continue_kind;
  1384. p->lineno = lineno;
  1385. p->col_offset = col_offset;
  1386. return p;
  1387. }
  1388. expr_ty
  1389. BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
  1390. *arena)
  1391. {
  1392. expr_ty p;
  1393. if (!op) {
  1394. PyErr_SetString(PyExc_ValueError,
  1395. "field op is required for BoolOp");
  1396. return NULL;
  1397. }
  1398. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1399. if (!p)
  1400. return NULL;
  1401. p->kind = BoolOp_kind;
  1402. p->v.BoolOp.op = op;
  1403. p->v.BoolOp.values = values;
  1404. p->lineno = lineno;
  1405. p->col_offset = col_offset;
  1406. return p;
  1407. }
  1408. expr_ty
  1409. BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
  1410. PyArena *arena)
  1411. {
  1412. expr_ty p;
  1413. if (!left) {
  1414. PyErr_SetString(PyExc_ValueError,
  1415. "field left is required for BinOp");
  1416. return NULL;
  1417. }
  1418. if (!op) {
  1419. PyErr_SetString(PyExc_ValueError,
  1420. "field op is required for BinOp");
  1421. return NULL;
  1422. }
  1423. if (!right) {
  1424. PyErr_SetString(PyExc_ValueError,
  1425. "field right is required for BinOp");
  1426. return NULL;
  1427. }
  1428. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1429. if (!p)
  1430. return NULL;
  1431. p->kind = BinOp_kind;
  1432. p->v.BinOp.left = left;
  1433. p->v.BinOp.op = op;
  1434. p->v.BinOp.right = right;
  1435. p->lineno = lineno;
  1436. p->col_offset = col_offset;
  1437. return p;
  1438. }
  1439. expr_ty
  1440. UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
  1441. *arena)
  1442. {
  1443. expr_ty p;
  1444. if (!op) {
  1445. PyErr_SetString(PyExc_ValueError,
  1446. "field op is required for UnaryOp");
  1447. return NULL;
  1448. }
  1449. if (!operand) {
  1450. PyErr_SetString(PyExc_ValueError,
  1451. "field operand is required for UnaryOp");
  1452. return NULL;
  1453. }
  1454. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1455. if (!p)
  1456. return NULL;
  1457. p->kind = UnaryOp_kind;
  1458. p->v.UnaryOp.op = op;
  1459. p->v.UnaryOp.operand = operand;
  1460. p->lineno = lineno;
  1461. p->col_offset = col_offset;
  1462. return p;
  1463. }
  1464. expr_ty
  1465. Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
  1466. *arena)
  1467. {
  1468. expr_ty p;
  1469. if (!args) {
  1470. PyErr_SetString(PyExc_ValueError,
  1471. "field args is required for Lambda");
  1472. return NULL;
  1473. }
  1474. if (!body) {
  1475. PyErr_SetString(PyExc_ValueError,
  1476. "field body is required for Lambda");
  1477. return NULL;
  1478. }
  1479. p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
  1480. if (!p)
  1481. return NULL;
  1482. p->kind = Lambda_kind;
  1483. p->v.Lambda.args = args;
  1484. p->v.Lambda.body = body;
  1485. p->lineno = lineno;
  1486. p->col_offset = col_offset;
  1487. return p;
  1488. }
  1489. expr_ty
  1490. IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
  1491. PyArena *arena)
  1492. {
  1493. expr_ty p;
  1494. if (!test) {
  1495. PyErr_SetString(PyExc_ValueError,
  1496. "field test is required for IfExp");
  1497. return NULL;
  1498. }
  1499. if (!body) {
  1500. PyErr_SetString(PyExc_Value