2 """Generate C code from an ASDL description."""
5 # handle fields that have a type but no name
15 """Return a string for the C name of the type.
17 This function special cases the default types provided by asdl:
18 identifier, string, int.
20 # XXX ack! need to figure out where Id is useful and where string
21 if isinstance(name
, asdl
.Id
):
23 if name
in asdl
.builtin_types
:
28 def reflow_lines(s
, depth
):
29 """Reflow the line s indented depth tabs.
31 Return a sequence of lines where no line extends beyond MAX_COL
32 when properly indented. The first line is properly indented based
33 exclusively on depth * TABSIZE. All following lines -- these are
34 the reflowed lines generated by this function -- start at the same
35 column as the first character beyond the opening { in the first
38 size
= MAX_COL
- depth
* TABSIZE
45 while len(cur
) > size
:
46 i
= cur
.rfind(' ', 0, size
)
47 # XXX this should be fixed for real
48 if i
== -1 and 'GeneratorExp' in cur
:
50 assert i
!= -1, "Impossible line %d to reflow: %r" % (size
, s
)
51 lines
.append(padding
+ cur
[:i
])
53 # find new size based on brace
54 j
= cur
.find('{', 0, i
)
56 j
+= 2 # account for the brace and the space after it
60 j
= cur
.find('(', 0, i
)
62 j
+= 1 # account for the paren (no space after it)
67 lines
.append(padding
+ cur
)
71 """Return True if a sum is a simple.
73 A sum is simple if its types have no fields, e.g.
74 unaryop = Invert | Not | UAdd | USub
82 class EmitVisitor(asdl
.VisitorBase
):
83 """Visit that emits lines"""
85 def __init__(self
, file):
87 super(EmitVisitor
, self
).__init
__()
89 def emit(self
, s
, depth
, reflow
=True):
90 # XXX reflow long lines?
92 lines
= reflow_lines(s
, depth
)
96 line
= (" " * TABSIZE
* depth
) + line
+ "\n"
100 class TypeDefVisitor(EmitVisitor
):
101 def visitModule(self
, mod
):
105 def visitType(self
, type, depth
=0):
106 self
.visit(type.value
, type.name
, depth
)
108 def visitSum(self
, sum, name
, depth
):
110 self
.simple_sum(sum, name
, depth
)
112 self
.sum_with_constructors(sum, name
, depth
)
114 def simple_sum(self
, sum, name
, depth
):
116 for i
in range(len(sum.types
)):
118 enum
.append("%s=%d" % (type.name
, i
+ 1))
119 enums
= ", ".join(enum
)
120 ctype
= get_c_type(name
)
121 s
= "typedef enum _%s { %s } %s;" % (name
, enums
, ctype
)
125 def sum_with_constructors(self
, sum, name
, depth
):
126 ctype
= get_c_type(name
)
127 s
= "typedef struct _%(name)s *%(ctype)s;" % locals()
131 def visitProduct(self
, product
, name
, depth
):
132 ctype
= get_c_type(name
)
133 s
= "typedef struct _%(name)s *%(ctype)s;" % locals()
138 class StructVisitor(EmitVisitor
):
139 """Visitor to generate typdefs for AST."""
141 def visitModule(self
, mod
):
145 def visitType(self
, type, depth
=0):
146 self
.visit(type.value
, type.name
, depth
)
148 def visitSum(self
, sum, name
, depth
):
149 if not is_simple(sum):
150 self
.sum_with_constructors(sum, name
, depth
)
152 def sum_with_constructors(self
, sum, name
, depth
):
153 def emit(s
, depth
=depth
):
154 self
.emit(s
% sys
._getframe
(1).f_locals
, depth
)
156 for i
in range(len(sum.types
)):
158 enum
.append("%s_kind=%d" % (type.name
, i
+ 1))
160 emit("enum _%(name)s_kind {" + ", ".join(enum
) + "};")
162 emit("struct _%(name)s {")
163 emit("enum _%(name)s_kind kind;", depth
+ 1)
164 emit("union {", depth
+ 1)
166 self
.visit(t
, depth
+ 2)
167 emit("} v;", depth
+ 1)
168 for field
in sum.attributes
:
169 # rudimentary attribute handling
170 type = str(field
.type)
171 assert type in asdl
.builtin_types
, type
172 emit("%s %s;" % (type, field
.name
), depth
+ 1);
176 def visitConstructor(self
, cons
, depth
):
178 self
.emit("struct {", depth
)
179 for f
in cons
.fields
:
180 self
.visit(f
, depth
+ 1)
181 self
.emit("} %s;" % cons
.name
, depth
)
184 # XXX not sure what I want here, nothing is probably fine
187 def visitField(self
, field
, depth
):
188 # XXX need to lookup field.type, because it might be something
190 ctype
= get_c_type(field
.type)
193 if field
.type.value
in ('cmpop',):
194 self
.emit("asdl_int_seq *%(name)s;" % locals(), depth
)
196 self
.emit("asdl_seq *%(name)s;" % locals(), depth
)
198 self
.emit("%(ctype)s %(name)s;" % locals(), depth
)
200 def visitProduct(self
, product
, name
, depth
):
201 self
.emit("struct _%(name)s {" % locals(), depth
)
202 for f
in product
.fields
:
203 self
.visit(f
, depth
+ 1)
204 self
.emit("};", depth
)
208 class PrototypeVisitor(EmitVisitor
):
209 """Generate function prototypes for the .h file"""
211 def visitModule(self
, mod
):
215 def visitType(self
, type):
216 self
.visit(type.value
, type.name
)
218 def visitSum(self
, sum, name
):
223 self
.visit(t
, name
, sum.attributes
)
225 def get_args(self
, fields
):
226 """Return list of C argument into, one for each field.
228 Argument info is 3-tuple of a C type, variable name, and flag
229 that is true if type can be NULL.
236 c
= unnamed
[name
] = unnamed
.get(name
, 0) + 1
238 name
= "name%d" % (c
- 1)
241 # XXX should extend get_c_type() to handle this
243 if f
.type.value
in ('cmpop',):
244 ctype
= "asdl_int_seq *"
248 ctype
= get_c_type(f
.type)
249 args
.append((ctype
, name
, f
.opt
or f
.seq
))
252 def visitConstructor(self
, cons
, type, attrs
):
253 args
= self
.get_args(cons
.fields
)
254 attrs
= self
.get_args(attrs
)
255 ctype
= get_c_type(type)
256 self
.emit_function(cons
.name
, ctype
, args
, attrs
)
258 def emit_function(self
, name
, ctype
, args
, attrs
, union
=True):
261 argstr
= ", ".join(["%s %s" % (atype
, aname
)
262 for atype
, aname
, opt
in args
])
263 argstr
+= ", PyArena *arena"
265 argstr
= "PyArena *arena"
267 for i
in range(1, len(args
)+1):
269 self
.emit("#define %s(%s) _Py_%s(%s)" % (name
, margs
, name
, margs
), 0,
271 self
.emit("%s _Py_%s(%s);" % (ctype
, name
, argstr
), False)
273 def visitProduct(self
, prod
, name
):
274 self
.emit_function(name
, get_c_type(name
),
275 self
.get_args(prod
.fields
), [], union
=False)
278 class FunctionVisitor(PrototypeVisitor
):
279 """Visitor to generate constructor functions for AST."""
281 def emit_function(self
, name
, ctype
, args
, attrs
, union
=True):
282 def emit(s
, depth
=0, reflow
=True):
283 self
.emit(s
, depth
, reflow
)
284 argstr
= ", ".join(["%s %s" % (atype
, aname
)
285 for atype
, aname
, opt
in args
+ attrs
])
287 argstr
+= ", PyArena *arena"
289 argstr
= "PyArena *arena"
290 self
.emit("%s" % ctype
, 0)
291 emit("%s(%s)" % (name
, argstr
))
293 emit("%s p;" % ctype
, 1)
294 for argtype
, argname
, opt
in args
:
295 if not opt
and argtype
!= "int":
296 emit("if (!%s) {" % argname
, 1)
297 emit("PyErr_SetString(PyExc_ValueError,", 2)
298 msg
= "field %s is required for %s" % (argname
, name
)
299 emit(' "%s");' % msg
,
301 emit('return NULL;', 2)
304 emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype
, 1);
306 emit("return NULL;", 2)
308 self
.emit_body_union(name
, args
, attrs
)
310 self
.emit_body_struct(name
, args
, attrs
)
315 def emit_body_union(self
, name
, args
, attrs
):
316 def emit(s
, depth
=0, reflow
=True):
317 self
.emit(s
, depth
, reflow
)
318 emit("p->kind = %s_kind;" % name
, 1)
319 for argtype
, argname
, opt
in args
:
320 emit("p->v.%s.%s = %s;" % (name
, argname
, argname
), 1)
321 for argtype
, argname
, opt
in attrs
:
322 emit("p->%s = %s;" % (argname
, argname
), 1)
324 def emit_body_struct(self
, name
, args
, attrs
):
325 def emit(s
, depth
=0, reflow
=True):
326 self
.emit(s
, depth
, reflow
)
327 for argtype
, argname
, opt
in args
:
328 emit("p->%s = %s;" % (argname
, argname
), 1)
332 class PickleVisitor(EmitVisitor
):
334 def visitModule(self
, mod
):
338 def visitType(self
, type):
339 self
.visit(type.value
, type.name
)
341 def visitSum(self
, sum, name
):
344 def visitProduct(self
, sum, name
):
347 def visitConstructor(self
, cons
, name
):
350 def visitField(self
, sum):
354 class Obj2ModPrototypeVisitor(PickleVisitor
):
355 def visitProduct(self
, prod
, name
):
356 code
= "static int obj2ast_%s(PyObject* obj, %s* out, PyArena* arena);"
357 self
.emit(code
% (name
, get_c_type(name
)), 0)
359 visitSum
= visitProduct
362 class Obj2ModVisitor(PickleVisitor
):
363 def funcHeader(self
, name
):
364 ctype
= get_c_type(name
)
366 self
.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name
, ctype
), 0)
368 self
.emit("PyObject* tmp = NULL;", 1)
369 self
.emit("int isinstance;", 1)
370 # Prevent compiler warnings about unused variable.
371 self
.emit("tmp = tmp;", 1)
374 def sumTrailer(self
, name
, add_label
=False):
376 # there's really nothing more we can do if this fails ...
377 error
= "expected some sort of %s, but got %%R" % name
378 format
= "PyErr_Format(PyExc_TypeError, \"%s\", obj);"
379 self
.emit(format
% error
, 1, reflow
=False)
381 self
.emit("failed:", 1)
382 self
.emit("return 1;", 1)
386 def simpleSum(self
, sum, name
):
387 self
.funcHeader(name
)
389 line
= ("isinstance = PyObject_IsInstance(obj, "
390 "(PyObject *)%s_type);")
391 self
.emit(line
% (t
.name
,), 1)
392 self
.emit("if (isinstance == -1) {", 1)
393 self
.emit("return 1;", 2)
395 self
.emit("if (isinstance) {", 1)
396 self
.emit("*out = %s;" % t
.name
, 2)
397 self
.emit("return 0;", 2)
399 self
.sumTrailer(name
)
401 def buildArgs(self
, fields
):
402 return ", ".join(fields
+ ["arena"])
404 def complexSum(self
, sum, name
):
405 self
.funcHeader(name
)
406 for a
in sum.attributes
:
407 self
.visitAttributeDeclaration(a
, name
, sum=sum)
409 # XXX: should we only do this for 'expr'?
410 self
.emit("if (obj == Py_None) {", 1)
411 self
.emit("*out = NULL;", 2)
412 self
.emit("return 0;", 2)
414 for a
in sum.attributes
:
415 self
.visitField(a
, name
, sum=sum, depth
=1)
417 line
= "isinstance = PyObject_IsInstance(obj, (PyObject*)%s_type);"
418 self
.emit(line
% (t
.name
,), 1)
419 self
.emit("if (isinstance == -1) {", 1)
420 self
.emit("return 1;", 2)
422 self
.emit("if (isinstance) {", 1)
424 self
.visitFieldDeclaration(f
, t
.name
, sum=sum, depth
=2)
427 self
.visitField(f
, t
.name
, sum=sum, depth
=2)
428 args
= [f
.name
.value
for f
in t
.fields
] + [a
.name
.value
for a
in sum.attributes
]
429 self
.emit("*out = %s(%s);" % (t
.name
, self
.buildArgs(args
)), 2)
430 self
.emit("if (*out == NULL) goto failed;", 2)
431 self
.emit("return 0;", 2)
433 self
.sumTrailer(name
, True)
435 def visitAttributeDeclaration(self
, a
, name
, sum=sum):
436 ctype
= get_c_type(a
.type)
437 self
.emit("%s %s;" % (ctype
, a
.name
), 1)
439 def visitSum(self
, sum, name
):
441 self
.simpleSum(sum, name
)
443 self
.complexSum(sum, name
)
445 def visitProduct(self
, prod
, name
):
446 ctype
= get_c_type(name
)
448 self
.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name
, ctype
), 0)
450 self
.emit("PyObject* tmp = NULL;", 1)
451 for f
in prod
.fields
:
452 self
.visitFieldDeclaration(f
, name
, prod
=prod
, depth
=1)
454 for f
in prod
.fields
:
455 self
.visitField(f
, name
, prod
=prod
, depth
=1)
456 args
= [f
.name
.value
for f
in prod
.fields
]
457 self
.emit("*out = %s(%s);" % (name
, self
.buildArgs(args
)), 1)
458 self
.emit("return 0;", 1)
459 self
.emit("failed:", 0)
460 self
.emit("Py_XDECREF(tmp);", 1)
461 self
.emit("return 1;", 1)
465 def visitFieldDeclaration(self
, field
, name
, sum=None, prod
=None, depth
=0):
466 ctype
= get_c_type(field
.type)
468 if self
.isSimpleType(field
):
469 self
.emit("asdl_int_seq* %s;" % field
.name
, depth
)
471 self
.emit("asdl_seq* %s;" % field
.name
, depth
)
473 ctype
= get_c_type(field
.type)
474 self
.emit("%s %s;" % (ctype
, field
.name
), depth
)
476 def isSimpleSum(self
, field
):
477 # XXX can the members of this list be determined automatically?
478 return field
.type.value
in ('expr_context', 'boolop', 'operator',
481 def isNumeric(self
, field
):
482 return get_c_type(field
.type) in ("int", "bool")
484 def isSimpleType(self
, field
):
485 return self
.isSimpleSum(field
) or self
.isNumeric(field
)
487 def visitField(self
, field
, name
, sum=None, prod
=None, depth
=0):
488 ctype
= get_c_type(field
.type)
489 self
.emit("if (PyObject_HasAttrString(obj, \"%s\")) {" % field
.name
, depth
)
490 self
.emit("int res;", depth
+1)
492 self
.emit("Py_ssize_t len;", depth
+1)
493 self
.emit("Py_ssize_t i;", depth
+1)
494 self
.emit("tmp = PyObject_GetAttrString(obj, \"%s\");" % field
.name
, depth
+1)
495 self
.emit("if (tmp == NULL) goto failed;", depth
+1)
497 self
.emit("if (!PyList_Check(tmp)) {", depth
+1)
498 self
.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
499 "be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
501 depth
+2, reflow
=False)
502 self
.emit("goto failed;", depth
+2)
503 self
.emit("}", depth
+1)
504 self
.emit("len = PyList_GET_SIZE(tmp);", depth
+1)
505 if self
.isSimpleType(field
):
506 self
.emit("%s = asdl_int_seq_new(len, arena);" % field
.name
, depth
+1)
508 self
.emit("%s = asdl_seq_new(len, arena);" % field
.name
, depth
+1)
509 self
.emit("if (%s == NULL) goto failed;" % field
.name
, depth
+1)
510 self
.emit("for (i = 0; i < len; i++) {", depth
+1)
511 self
.emit("%s value;" % ctype
, depth
+2)
512 self
.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &value, arena);" %
513 field
.type, depth
+2, reflow
=False)
514 self
.emit("if (res != 0) goto failed;", depth
+2)
515 self
.emit("asdl_seq_SET(%s, i, value);" % field
.name
, depth
+2)
516 self
.emit("}", depth
+1)
518 self
.emit("res = obj2ast_%s(tmp, &%s, arena);" %
519 (field
.type, field
.name
), depth
+1)
520 self
.emit("if (res != 0) goto failed;", depth
+1)
522 self
.emit("Py_XDECREF(tmp);", depth
+1)
523 self
.emit("tmp = NULL;", depth
+1)
524 self
.emit("} else {", depth
)
526 message
= "required field \\\"%s\\\" missing from %s" % (field
.name
, name
)
527 format
= "PyErr_SetString(PyExc_TypeError, \"%s\");"
528 self
.emit(format
% message
, depth
+1, reflow
=False)
529 self
.emit("return 1;", depth
+1)
531 if self
.isNumeric(field
):
532 self
.emit("%s = 0;" % field
.name
, depth
+1)
533 elif not self
.isSimpleType(field
):
534 self
.emit("%s = NULL;" % field
.name
, depth
+1)
536 raise TypeError("could not determine the default value for %s" % field
.name
)
537 self
.emit("}", depth
)
540 class MarshalPrototypeVisitor(PickleVisitor
):
542 def prototype(self
, sum, name
):
543 ctype
= get_c_type(name
)
544 self
.emit("static int marshal_write_%s(PyObject **, int *, %s);"
547 visitProduct
= visitSum
= prototype
550 class PyTypesDeclareVisitor(PickleVisitor
):
552 def visitProduct(self
, prod
, name
):
553 self
.emit("static PyTypeObject *%s_type;" % name
, 0)
554 self
.emit("static PyObject* ast2obj_%s(void*);" % name
, 0)
556 self
.emit("static char *%s_fields[]={" % name
,0)
557 for f
in prod
.fields
:
558 self
.emit('"%s",' % f
.name
, 1)
561 def visitSum(self
, sum, name
):
562 self
.emit("static PyTypeObject *%s_type;" % name
, 0)
564 self
.emit("static char *%s_attributes[] = {" % name
, 0)
565 for a
in sum.attributes
:
566 self
.emit('"%s",' % a
.name
, 1)
570 ptype
= get_c_type(name
)
573 tnames
.append(str(t
.name
)+"_singleton")
574 tnames
= ", *".join(tnames
)
575 self
.emit("static PyObject *%s;" % tnames
, 0)
576 self
.emit("static PyObject* ast2obj_%s(%s);" % (name
, ptype
), 0)
578 self
.visitConstructor(t
, name
)
580 def visitConstructor(self
, cons
, name
):
581 self
.emit("static PyTypeObject *%s_type;" % cons
.name
, 0)
583 self
.emit("static char *%s_fields[]={" % cons
.name
, 0)
584 for t
in cons
.fields
:
585 self
.emit('"%s",' % t
.name
, 1)
588 class PyTypesVisitor(PickleVisitor
):
590 def visitModule(self
, mod
):
593 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
595 Py_ssize_t i, numfields = 0;
597 PyObject *key, *value, *fields;
598 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
602 numfields = PySequence_Size(fields);
606 res = 0; /* if no error occurs, this stays 0 to the end */
607 if (PyTuple_GET_SIZE(args) > 0) {
608 if (numfields != PyTuple_GET_SIZE(args)) {
609 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
610 "%zd positional argument%s",
611 Py_TYPE(self)->tp_name,
612 numfields == 0 ? "" : "either 0 or ",
613 numfields, numfields == 1 ? "" : "s");
617 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
618 /* cannot be reached when fields is NULL */
619 PyObject *name = PySequence_GetItem(fields, i);
624 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
631 i = 0; /* needed by PyDict_Next */
632 while (PyDict_Next(kw, &i, &key, &value)) {
633 res = PyObject_SetAttr(self, key, value);
643 /* Pickling support */
645 ast_type_reduce(PyObject *self, PyObject *unused)
648 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
650 if (PyErr_ExceptionMatches(PyExc_AttributeError))
656 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
660 return Py_BuildValue("O()", Py_TYPE(self));
663 static PyMethodDef ast_type_methods[] = {
664 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
668 static PyTypeObject AST_type = {
669 PyVarObject_HEAD_INIT(&PyType_Type, 0)
679 0, /* tp_as_number */
680 0, /* tp_as_sequence */
681 0, /* tp_as_mapping */
685 PyObject_GenericGetAttr, /* tp_getattro */
686 PyObject_GenericSetAttr, /* tp_setattro */
687 0, /* tp_as_buffer */
688 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
692 0, /* tp_richcompare */
693 0, /* tp_weaklistoffset */
696 ast_type_methods, /* tp_methods */
701 0, /* tp_descr_get */
702 0, /* tp_descr_set */
703 0, /* tp_dictoffset */
704 (initproc)ast_type_init, /* tp_init */
705 PyType_GenericAlloc, /* tp_alloc */
706 PyType_GenericNew, /* tp_new */
707 PyObject_Del, /* tp_free */
711 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
713 PyObject *fnames, *result;
715 fnames = PyTuple_New(num_fields);
716 if (!fnames) return NULL;
717 for (i = 0; i < num_fields; i++) {
718 PyObject *field = PyUnicode_FromString(fields[i]);
723 PyTuple_SET_ITEM(fnames, i, field);
725 result = PyObject_CallFunction((PyObject*)&PyType_Type, "U(O){sOss}",
726 type, base, "_fields", fnames, "__module__", "_ast");
728 return (PyTypeObject*)result;
731 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
734 PyObject *s, *l = PyTuple_New(num_fields);
736 for(i = 0; i < num_fields; i++) {
737 s = PyUnicode_FromString(attrs[i]);
742 PyTuple_SET_ITEM(l, i, s);
744 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
749 /* Conversion AST -> Python */
751 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
753 int i, n = asdl_seq_LEN(seq);
754 PyObject *result = PyList_New(n);
758 for (i = 0; i < n; i++) {
759 value = func(asdl_seq_GET(seq, i));
764 PyList_SET_ITEM(result, i, value);
769 static PyObject* ast2obj_object(void *o)
773 Py_INCREF((PyObject*)o);
776 #define ast2obj_identifier ast2obj_object
777 #define ast2obj_string ast2obj_object
779 static PyObject* ast2obj_int(long b)
781 return PyLong_FromLong(b);
784 /* Conversion Python -> AST */
786 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
791 PyArena_AddPyObject(arena, obj);
797 #define obj2ast_identifier obj2ast_object
798 #define obj2ast_string obj2ast_object
800 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
803 if (!PyLong_Check(obj)) {
804 PyObject *s = PyObject_Repr(obj);
805 if (s == NULL) return 1;
806 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
807 PyBytes_AS_STRING(s));
812 i = (int)PyLong_AsLong(obj);
813 if (i == -1 && PyErr_Occurred())
819 static int add_ast_fields(void)
821 PyObject *empty_tuple, *d;
822 if (PyType_Ready(&AST_type) < 0)
824 d = AST_type.tp_dict;
825 empty_tuple = PyTuple_New(0);
827 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
828 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
829 Py_XDECREF(empty_tuple);
832 Py_DECREF(empty_tuple);
836 """, 0, reflow
=False)
838 self
.emit("static int init_types(void)",0)
840 self
.emit("static int initialized;", 1)
841 self
.emit("if (initialized) return 1;", 1)
842 self
.emit("if (add_ast_fields() < 0) return 0;", 1)
845 self
.emit("initialized = 1;", 1)
846 self
.emit("return 1;", 1);
849 def visitProduct(self
, prod
, name
):
851 fields
= name
.value
+"_fields"
854 self
.emit('%s_type = make_type("%s", &AST_type, %s, %d);' %
855 (name
, name
, fields
, len(prod
.fields
)), 1)
856 self
.emit("if (!%s_type) return 0;" % name
, 1)
858 def visitSum(self
, sum, name
):
859 self
.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' %
861 self
.emit("if (!%s_type) return 0;" % name
, 1)
863 self
.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
864 (name
, name
, len(sum.attributes
)), 1)
866 self
.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name
, 1)
867 simple
= is_simple(sum)
869 self
.visitConstructor(t
, name
, simple
)
871 def visitConstructor(self
, cons
, name
, simple
):
873 fields
= cons
.name
.value
+"_fields"
876 self
.emit('%s_type = make_type("%s", %s_type, %s, %d);' %
877 (cons
.name
, cons
.name
, name
, fields
, len(cons
.fields
)), 1)
878 self
.emit("if (!%s_type) return 0;" % cons
.name
, 1)
880 self
.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" %
881 (cons
.name
, cons
.name
), 1)
882 self
.emit("if (!%s_singleton) return 0;" % cons
.name
, 1)
885 def parse_version(mod
):
886 return mod
.version
.value
[12:-3]
888 class ASTModuleVisitor(PickleVisitor
):
890 def visitModule(self
, mod
):
891 self
.emit("static struct PyModuleDef _astmodule = {", 0)
892 self
.emit(' PyModuleDef_HEAD_INIT, "_ast"', 0)
894 self
.emit("PyMODINIT_FUNC", 0)
895 self
.emit("PyInit__ast(void)", 0)
897 self
.emit("PyObject *m, *d;", 1)
898 self
.emit("if (!init_types()) return NULL;", 1)
899 self
.emit('m = PyModule_Create(&_astmodule);', 1)
900 self
.emit("if (!m) return NULL;", 1)
901 self
.emit("d = PyModule_GetDict(m);", 1)
902 self
.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1)
903 self
.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1)
904 self
.emit("return NULL;", 2)
905 # Value of version: "$Revision$"
906 self
.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)'
907 % parse_version(mod
), 1)
908 self
.emit("return NULL;", 2)
911 self
.emit("return m;", 1)
914 def visitProduct(self
, prod
, name
):
917 def visitSum(self
, sum, name
):
920 self
.visitConstructor(t
, name
)
922 def visitConstructor(self
, cons
, name
):
923 self
.addObj(cons
.name
)
925 def addObj(self
, name
):
926 self
.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return NULL;' % (name
, name
), 1)
929 _SPECIALIZED_SEQUENCES
= ('stmt', 'expr')
931 def find_sequence(fields
, doing_specialization
):
932 """Return True if any field uses a sequence."""
935 if not doing_specialization
:
937 if str(f
.type) not in _SPECIALIZED_SEQUENCES
:
941 def has_sequence(types
, doing_specialization
):
943 if find_sequence(t
.fields
, doing_specialization
):
948 class StaticVisitor(PickleVisitor
):
949 CODE
= '''Very simple, always emit this static code. Overide CODE'''
951 def visit(self
, object):
952 self
.emit(self
.CODE
, 0, reflow
=False)
955 class ObjVisitor(PickleVisitor
):
957 def func_begin(self
, name
):
958 ctype
= get_c_type(name
)
959 self
.emit("PyObject*", 0)
960 self
.emit("ast2obj_%s(void* _o)" % (name
), 0)
962 self
.emit("%s o = (%s)_o;" % (ctype
, ctype
), 1)
963 self
.emit("PyObject *result = NULL, *value = NULL;", 1)
964 self
.emit('if (!o) {', 1)
965 self
.emit("Py_INCREF(Py_None);", 2)
966 self
.emit('return Py_None;', 2)
971 self
.emit("return result;", 1)
972 self
.emit("failed:", 0)
973 self
.emit("Py_XDECREF(value);", 1)
974 self
.emit("Py_XDECREF(result);", 1)
975 self
.emit("return NULL;", 1)
979 def visitSum(self
, sum, name
):
981 self
.simpleSum(sum, name
)
983 self
.func_begin(name
)
984 self
.emit("switch (o->kind) {", 1)
985 for i
in range(len(sum.types
)):
987 self
.visitConstructor(t
, i
+ 1, name
)
989 for a
in sum.attributes
:
990 self
.emit("value = ast2obj_%s(o->%s);" % (a
.type, a
.name
), 1)
991 self
.emit("if (!value) goto failed;", 1)
992 self
.emit('if (PyObject_SetAttrString(result, "%s", value) < 0)' % a
.name
, 1)
993 self
.emit('goto failed;', 2)
994 self
.emit('Py_DECREF(value);', 1)
997 def simpleSum(self
, sum, name
):
998 self
.emit("PyObject* ast2obj_%s(%s_ty o)" % (name
, name
), 0)
1000 self
.emit("switch(o) {", 1)
1002 self
.emit("case %s:" % t
.name
, 2)
1003 self
.emit("Py_INCREF(%s_singleton);" % t
.name
, 3)
1004 self
.emit("return %s_singleton;" % t
.name
, 3)
1005 self
.emit("default:" % name
, 2)
1006 self
.emit('/* should never happen, but just in case ... */', 3)
1007 code
= "PyErr_Format(PyExc_SystemError, \"unknown %s found\");" % name
1008 self
.emit(code
, 3, reflow
=False)
1009 self
.emit("return NULL;", 3)
1013 def visitProduct(self
, prod
, name
):
1014 self
.func_begin(name
)
1015 self
.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name
, 1);
1016 self
.emit("if (!result) return NULL;", 1)
1017 for field
in prod
.fields
:
1018 self
.visitField(field
, name
, 1, True)
1021 def visitConstructor(self
, cons
, enum
, name
):
1022 self
.emit("case %s_kind:" % cons
.name
, 1)
1023 self
.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons
.name
, 2);
1024 self
.emit("if (!result) goto failed;", 2)
1025 for f
in cons
.fields
:
1026 self
.visitField(f
, cons
.name
, 2, False)
1027 self
.emit("break;", 2)
1029 def visitField(self
, field
, name
, depth
, product
):
1031 self
.emit(s
, depth
+ d
)
1033 value
= "o->%s" % field
.name
1035 value
= "o->v.%s.%s" % (name
, field
.name
)
1036 self
.set(field
, value
, depth
)
1037 emit("if (!value) goto failed;", 0)
1038 emit('if (PyObject_SetAttrString(result, "%s", value) == -1)' % field
.name
, 0)
1039 emit("goto failed;", 1)
1040 emit("Py_DECREF(value);", 0)
1042 def emitSeq(self
, field
, value
, depth
, emit
):
1043 emit("seq = %s;" % value
, 0)
1044 emit("n = asdl_seq_LEN(seq);", 0)
1045 emit("value = PyList_New(n);", 0)
1046 emit("if (!value) goto failed;", 0)
1047 emit("for (i = 0; i < n; i++) {", 0)
1048 self
.set("value", field
, "asdl_seq_GET(seq, i)", depth
+ 1)
1049 emit("if (!value1) goto failed;", 1)
1050 emit("PyList_SET_ITEM(value, i, value1);", 1)
1051 emit("value1 = NULL;", 1)
1054 def set(self
, field
, value
, depth
):
1056 # XXX should really check for is_simple, but that requires a symbol table
1057 if field
.type.value
== "cmpop":
1058 # While the sequence elements are stored as void*,
1059 # ast2obj_cmpop expects an enum
1060 self
.emit("{", depth
)
1061 self
.emit("int i, n = asdl_seq_LEN(%s);" % value
, depth
+1)
1062 self
.emit("value = PyList_New(n);", depth
+1)
1063 self
.emit("if (!value) goto failed;", depth
+1)
1064 self
.emit("for(i = 0; i < n; i++)", depth
+1)
1065 # This cannot fail, so no need for error handling
1066 self
.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value
,
1067 depth
+2, reflow
=False)
1068 self
.emit("}", depth
)
1070 self
.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value
, field
.type), depth
)
1072 ctype
= get_c_type(field
.type)
1073 self
.emit("value = ast2obj_%s(%s);" % (field
.type, value
), depth
, reflow
=False)
1076 class PartingShots(StaticVisitor
):
1079 PyObject* PyAST_mod2obj(mod_ty t)
1082 return ast2obj_mod(t);
1085 /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
1086 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
1089 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
1090 (PyObject*)Interactive_type};
1091 char *req_name[] = {"Module", "Expression", "Interactive"};
1093 assert(0 <= mode && mode <= 2);
1097 isinstance = PyObject_IsInstance(ast, req_type[mode]);
1098 if (isinstance == -1)
1101 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
1102 req_name[mode], Py_TYPE(ast)->tp_name);
1105 if (obj2ast_mod(ast, &res, arena) != 0)
1111 int PyAST_Check(PyObject* obj)
1114 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
1118 class ChainOfVisitors
:
1119 def __init__(self
, *visitors
):
1120 self
.visitors
= visitors
1122 def visit(self
, object):
1123 for v
in self
.visitors
:
1127 common_msg
= "/* File automatically generated by %s. */\n\n"
1133 This module must be committed separately after each AST grammar change;
1134 The __version__ number is set to the revision number of the commit
1135 containing the grammar change.
1142 components
= argv0
.split(os
.sep
)
1143 argv0
= os
.sep
.join(components
[-2:])
1144 auto_gen_msg
= common_msg
% argv0
1145 mod
= asdl
.parse(srcfile
)
1146 if not asdl
.check(mod
):
1149 p
= "%s/%s-ast.h" % (INC_DIR
, mod
.name
)
1151 f
.write(auto_gen_msg
)
1152 f
.write('#include "asdl.h"\n\n')
1153 c
= ChainOfVisitors(TypeDefVisitor(f
),
1155 PrototypeVisitor(f
),
1158 f
.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
1159 f
.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
1160 f
.write("int PyAST_Check(PyObject* obj);\n")
1164 p
= os
.path
.join(SRC_DIR
, str(mod
.name
) + "-ast.c")
1166 f
.write(auto_gen_msg
)
1167 f
.write(c_file_msg
% parse_version(mod
))
1168 f
.write('#include "Python.h"\n')
1169 f
.write('#include "%s-ast.h"\n' % mod
.name
)
1171 f
.write("static PyTypeObject AST_type;\n")
1172 v
= ChainOfVisitors(
1173 PyTypesDeclareVisitor(f
),
1175 Obj2ModPrototypeVisitor(f
),
1179 ASTModuleVisitor(f
),
1185 if __name__
== "__main__":
1191 opts
, args
= getopt
.getopt(sys
.argv
[1:], "h:c:")
1193 sys
.stdout
.write("Must specify exactly one output file\n")
1201 sys
.stdout
.write("Must specify single input file\n")