2 """Generate C code from an ASDL description."""
5 # handle fields that have a type but no name
7 import os
, sys
, traceback
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, bool.
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: %s" % (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
=1):
90 # XXX reflow long lines?
92 lines
= reflow_lines(s
, depth
)
96 line
= (" " * TABSIZE
* depth
) + line
+ "\n"
99 class TypeDefVisitor(EmitVisitor
):
100 def visitModule(self
, mod
):
104 def visitType(self
, type, depth
=0):
105 self
.visit(type.value
, type.name
, depth
)
107 def visitSum(self
, sum, name
, depth
):
109 self
.simple_sum(sum, name
, depth
)
111 self
.sum_with_constructors(sum, name
, depth
)
113 def simple_sum(self
, sum, name
, depth
):
115 for i
in range(len(sum.types
)):
117 enum
.append("%s=%d" % (type.name
, i
+ 1))
118 enums
= ", ".join(enum
)
119 ctype
= get_c_type(name
)
120 s
= "typedef enum _%s { %s } %s;" % (name
, enums
, ctype
)
124 def sum_with_constructors(self
, sum, name
, depth
):
125 ctype
= get_c_type(name
)
126 s
= "typedef struct _%(name)s *%(ctype)s;" % locals()
130 def visitProduct(self
, product
, name
, depth
):
131 ctype
= get_c_type(name
)
132 s
= "typedef struct _%(name)s *%(ctype)s;" % locals()
136 class StructVisitor(EmitVisitor
):
137 """Visitor to generate typdefs for AST."""
139 def visitModule(self
, mod
):
143 def visitType(self
, type, depth
=0):
144 self
.visit(type.value
, type.name
, depth
)
146 def visitSum(self
, sum, name
, depth
):
147 if not is_simple(sum):
148 self
.sum_with_constructors(sum, name
, depth
)
150 def sum_with_constructors(self
, sum, name
, depth
):
151 def emit(s
, depth
=depth
):
152 self
.emit(s
% sys
._getframe
(1).f_locals
, depth
)
154 for i
in range(len(sum.types
)):
156 enum
.append("%s_kind=%d" % (type.name
, i
+ 1))
158 emit("enum _%(name)s_kind {" + ", ".join(enum
) + "};")
160 emit("struct _%(name)s {")
161 emit("enum _%(name)s_kind kind;", depth
+ 1)
162 emit("union {", depth
+ 1)
164 self
.visit(t
, depth
+ 2)
165 emit("} v;", depth
+ 1)
166 for field
in sum.attributes
:
167 # rudimentary attribute handling
168 type = str(field
.type)
169 assert type in asdl
.builtin_types
, type
170 emit("%s %s;" % (type, field
.name
), depth
+ 1);
174 def visitConstructor(self
, cons
, depth
):
176 self
.emit("struct {", depth
)
177 for f
in cons
.fields
:
178 self
.visit(f
, depth
+ 1)
179 self
.emit("} %s;" % cons
.name
, depth
)
182 # XXX not sure what I want here, nothing is probably fine
185 def visitField(self
, field
, depth
):
186 # XXX need to lookup field.type, because it might be something
188 ctype
= get_c_type(field
.type)
191 if field
.type.value
in ('cmpop',):
192 self
.emit("asdl_int_seq *%(name)s;" % locals(), depth
)
194 self
.emit("asdl_seq *%(name)s;" % locals(), depth
)
196 self
.emit("%(ctype)s %(name)s;" % locals(), depth
)
198 def visitProduct(self
, product
, name
, depth
):
199 self
.emit("struct _%(name)s {" % locals(), depth
)
200 for f
in product
.fields
:
201 self
.visit(f
, depth
+ 1)
202 self
.emit("};", depth
)
205 class PrototypeVisitor(EmitVisitor
):
206 """Generate function prototypes for the .h file"""
208 def visitModule(self
, mod
):
212 def visitType(self
, type):
213 self
.visit(type.value
, type.name
)
215 def visitSum(self
, sum, name
):
220 self
.visit(t
, name
, sum.attributes
)
222 def get_args(self
, fields
):
223 """Return list of C argument into, one for each field.
225 Argument info is 3-tuple of a C type, variable name, and flag
226 that is true if type can be NULL.
233 c
= unnamed
[name
] = unnamed
.get(name
, 0) + 1
235 name
= "name%d" % (c
- 1)
238 # XXX should extend get_c_type() to handle this
240 if f
.type.value
in ('cmpop',):
241 ctype
= "asdl_int_seq *"
245 ctype
= get_c_type(f
.type)
246 args
.append((ctype
, name
, f
.opt
or f
.seq
))
249 def visitConstructor(self
, cons
, type, attrs
):
250 args
= self
.get_args(cons
.fields
)
251 attrs
= self
.get_args(attrs
)
252 ctype
= get_c_type(type)
253 self
.emit_function(cons
.name
, ctype
, args
, attrs
)
255 def emit_function(self
, name
, ctype
, args
, attrs
, union
=1):
258 argstr
= ", ".join(["%s %s" % (atype
, aname
)
259 for atype
, aname
, opt
in args
])
260 argstr
+= ", PyArena *arena"
262 argstr
= "PyArena *arena"
263 self
.emit("%s %s(%s);" % (ctype
, name
, argstr
), 0)
265 def visitProduct(self
, prod
, name
):
266 self
.emit_function(name
, get_c_type(name
),
267 self
.get_args(prod
.fields
), [], union
=0)
269 class FunctionVisitor(PrototypeVisitor
):
270 """Visitor to generate constructor functions for AST."""
272 def emit_function(self
, name
, ctype
, args
, attrs
, union
=1):
273 def emit(s
, depth
=0, reflow
=1):
274 self
.emit(s
, depth
, reflow
)
275 argstr
= ", ".join(["%s %s" % (atype
, aname
)
276 for atype
, aname
, opt
in args
+ attrs
])
278 argstr
+= ", PyArena *arena"
280 argstr
= "PyArena *arena"
281 self
.emit("%s" % ctype
, 0)
282 emit("%s(%s)" % (name
, argstr
))
284 emit("%s p;" % ctype
, 1)
285 for argtype
, argname
, opt
in args
:
286 # XXX hack alert: false is allowed for a bool
287 if not opt
and not (argtype
== "bool" or argtype
== "int"):
288 emit("if (!%s) {" % argname
, 1)
289 emit("PyErr_SetString(PyExc_ValueError,", 2)
290 msg
= "field %s is required for %s" % (argname
, name
)
291 emit(' "%s");' % msg
,
293 emit('return NULL;', 2)
296 emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype
, 1);
298 emit("PyErr_NoMemory();", 2)
299 emit("return NULL;", 2)
302 self
.emit_body_union(name
, args
, attrs
)
304 self
.emit_body_struct(name
, args
, attrs
)
309 def emit_body_union(self
, name
, args
, attrs
):
310 def emit(s
, depth
=0, reflow
=1):
311 self
.emit(s
, depth
, reflow
)
312 emit("p->kind = %s_kind;" % name
, 1)
313 for argtype
, argname
, opt
in args
:
314 emit("p->v.%s.%s = %s;" % (name
, argname
, argname
), 1)
315 for argtype
, argname
, opt
in attrs
:
316 emit("p->%s = %s;" % (argname
, argname
), 1)
318 def emit_body_struct(self
, name
, args
, attrs
):
319 def emit(s
, depth
=0, reflow
=1):
320 self
.emit(s
, depth
, reflow
)
321 for argtype
, argname
, opt
in args
:
322 emit("p->%s = %s;" % (argname
, argname
), 1)
325 class PickleVisitor(EmitVisitor
):
327 def visitModule(self
, mod
):
331 def visitType(self
, type):
332 self
.visit(type.value
, type.name
)
334 def visitSum(self
, sum, name
):
337 def visitProduct(self
, sum, name
):
340 def visitConstructor(self
, cons
, name
):
343 def visitField(self
, sum):
346 class MarshalPrototypeVisitor(PickleVisitor
):
348 def prototype(self
, sum, name
):
349 ctype
= get_c_type(name
)
350 self
.emit("static int marshal_write_%s(PyObject **, int *, %s);"
353 visitProduct
= visitSum
= prototype
355 class PyTypesDeclareVisitor(PickleVisitor
):
357 def visitProduct(self
, prod
, name
):
358 self
.emit("static PyTypeObject *%s_type;" % name
, 0)
359 self
.emit("static PyObject* ast2obj_%s(void*);" % name
, 0)
361 self
.emit("static char *%s_fields[]={" % name
,0)
362 for f
in prod
.fields
:
363 self
.emit('"%s",' % f
.name
, 1)
366 def visitSum(self
, sum, name
):
367 self
.emit("static PyTypeObject *%s_type;" % name
, 0)
369 self
.emit("static char *%s_attributes[] = {" % name
, 0)
370 for a
in sum.attributes
:
371 self
.emit('"%s",' % a
.name
, 1)
375 ptype
= get_c_type(name
)
378 tnames
.append(str(t
.name
)+"_singleton")
379 tnames
= ", *".join(tnames
)
380 self
.emit("static PyObject *%s;" % tnames
, 0)
381 self
.emit("static PyObject* ast2obj_%s(%s);" % (name
, ptype
), 0)
383 self
.visitConstructor(t
, name
)
385 def visitConstructor(self
, cons
, name
):
386 self
.emit("static PyTypeObject *%s_type;" % cons
.name
, 0)
388 self
.emit("static char *%s_fields[]={" % cons
.name
, 0)
389 for t
in cons
.fields
:
390 self
.emit('"%s",' % t
.name
, 1)
393 class PyTypesVisitor(PickleVisitor
):
395 def visitModule(self
, mod
):
397 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
399 PyObject *fnames, *result;
402 fnames = PyTuple_New(num_fields);
403 if (!fnames) return NULL;
408 for(i=0; i < num_fields; i++) {
409 PyObject *field = PyString_FromString(fields[i]);
414 PyTuple_SET_ITEM(fnames, i, field);
416 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
417 type, base, "_fields", fnames, "__module__", "_ast");
419 return (PyTypeObject*)result;
422 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
425 PyObject *s, *l = PyList_New(num_fields);
427 for(i = 0; i < num_fields; i++) {
428 s = PyString_FromString(attrs[i]);
433 PyList_SET_ITEM(l, i, s);
435 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
440 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
442 int i, n = asdl_seq_LEN(seq);
443 PyObject *result = PyList_New(n);
447 for (i = 0; i < n; i++) {
448 value = func(asdl_seq_GET(seq, i));
453 PyList_SET_ITEM(result, i, value);
458 static PyObject* ast2obj_object(void *o)
462 Py_INCREF((PyObject*)o);
465 #define ast2obj_identifier ast2obj_object
466 #define ast2obj_string ast2obj_object
467 static PyObject* ast2obj_bool(bool b)
469 return PyBool_FromLong(b);
472 static PyObject* ast2obj_int(bool b)
474 return PyInt_FromLong(b);
476 """, 0, reflow
=False)
478 self
.emit("static int init_types(void)",0)
480 self
.emit("static int initialized;", 1)
481 self
.emit("if (initialized) return 1;", 1)
482 self
.emit('AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0);', 1)
485 self
.emit("initialized = 1;", 1)
486 self
.emit("return 1;", 1);
489 def visitProduct(self
, prod
, name
):
491 fields
= name
.value
+"_fields"
494 self
.emit('%s_type = make_type("%s", AST_type, %s, %d);' %
495 (name
, name
, fields
, len(prod
.fields
)), 1)
496 self
.emit("if (!%s_type) return 0;" % name
, 1)
498 def visitSum(self
, sum, name
):
499 self
.emit('%s_type = make_type("%s", AST_type, NULL, 0);' % (name
, name
), 1)
500 self
.emit("if (!%s_type) return 0;" % name
, 1)
502 self
.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
503 (name
, name
, len(sum.attributes
)), 1)
505 self
.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name
, 1)
506 simple
= is_simple(sum)
508 self
.visitConstructor(t
, name
, simple
)
510 def visitConstructor(self
, cons
, name
, simple
):
512 fields
= cons
.name
.value
+"_fields"
515 self
.emit('%s_type = make_type("%s", %s_type, %s, %d);' %
516 (cons
.name
, cons
.name
, name
, fields
, len(cons
.fields
)), 1)
517 self
.emit("if (!%s_type) return 0;" % cons
.name
, 1)
519 self
.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" %
520 (cons
.name
, cons
.name
), 1)
521 self
.emit("if (!%s_singleton) return 0;" % cons
.name
, 1)
523 class ASTModuleVisitor(PickleVisitor
):
525 def visitModule(self
, mod
):
526 self
.emit("PyMODINIT_FUNC", 0)
527 self
.emit("init_ast(void)", 0)
529 self
.emit("PyObject *m, *d;", 1)
530 self
.emit("if (!init_types()) return;", 1)
531 self
.emit('m = Py_InitModule3("_ast", NULL, NULL);', 1)
532 self
.emit("if (!m) return;", 1)
533 self
.emit("d = PyModule_GetDict(m);", 1)
534 self
.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;', 1)
535 self
.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1)
536 self
.emit("return;", 2)
537 # Value of version: "$Revision$"
538 self
.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)' % mod
.version
.value
[12:-3], 1)
539 self
.emit("return;", 2)
544 def visitProduct(self
, prod
, name
):
547 def visitSum(self
, sum, name
):
550 self
.visitConstructor(t
, name
)
552 def visitConstructor(self
, cons
, name
):
553 self
.addObj(cons
.name
)
555 def addObj(self
, name
):
556 self
.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return;' % (name
, name
), 1)
558 _SPECIALIZED_SEQUENCES
= ('stmt', 'expr')
560 def find_sequence(fields
, doing_specialization
):
561 """Return True if any field uses a sequence."""
564 if not doing_specialization
:
566 if str(f
.type) not in _SPECIALIZED_SEQUENCES
:
570 def has_sequence(types
, doing_specialization
):
572 if find_sequence(t
.fields
, doing_specialization
):
577 class StaticVisitor(PickleVisitor
):
578 CODE
= '''Very simple, always emit this static code. Overide CODE'''
580 def visit(self
, object):
581 self
.emit(self
.CODE
, 0, reflow
=False)
583 class ObjVisitor(PickleVisitor
):
585 def func_begin(self
, name
):
586 ctype
= get_c_type(name
)
587 self
.emit("PyObject*", 0)
588 self
.emit("ast2obj_%s(void* _o)" % (name
), 0)
590 self
.emit("%s o = (%s)_o;" % (ctype
, ctype
), 1)
591 self
.emit("PyObject *result = NULL, *value = NULL;", 1)
592 self
.emit('if (!o) {', 1)
593 self
.emit("Py_INCREF(Py_None);", 2)
594 self
.emit('return Py_None;', 2)
599 self
.emit("return result;", 1)
600 self
.emit("failed:", 0)
601 self
.emit("Py_XDECREF(value);", 1)
602 self
.emit("Py_XDECREF(result);", 1)
603 self
.emit("return NULL;", 1)
607 def visitSum(self
, sum, name
):
609 self
.simpleSum(sum, name
)
611 self
.func_begin(name
)
612 self
.emit("switch (o->kind) {", 1)
613 for i
in range(len(sum.types
)):
615 self
.visitConstructor(t
, i
+ 1, name
)
617 for a
in sum.attributes
:
618 self
.emit("value = ast2obj_%s(o->%s);" % (a
.type, a
.name
), 1)
619 self
.emit("if (!value) goto failed;", 1)
620 self
.emit('if (PyObject_SetAttrString(result, "%s", value) < 0)' % a
.name
, 1)
621 self
.emit('goto failed;', 2)
622 self
.emit('Py_DECREF(value);', 1)
625 def simpleSum(self
, sum, name
):
626 self
.emit("PyObject* ast2obj_%s(%s_ty o)" % (name
, name
), 0)
628 self
.emit("switch(o) {", 1)
630 self
.emit("case %s:" % t
.name
, 2)
631 self
.emit("Py_INCREF(%s_singleton);" % t
.name
, 3)
632 self
.emit("return %s_singleton;" % t
.name
, 3)
634 self
.emit("return NULL; /* cannot happen */", 1)
637 def visitProduct(self
, prod
, name
):
638 self
.func_begin(name
)
639 self
.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name
, 1);
640 self
.emit("if (!result) return NULL;", 1)
641 for field
in prod
.fields
:
642 self
.visitField(field
, name
, 1, True)
645 def visitConstructor(self
, cons
, enum
, name
):
646 self
.emit("case %s_kind:" % cons
.name
, 1)
647 self
.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons
.name
, 2);
648 self
.emit("if (!result) goto failed;", 2)
649 for f
in cons
.fields
:
650 self
.visitField(f
, cons
.name
, 2, False)
651 self
.emit("break;", 2)
653 def visitField(self
, field
, name
, depth
, product
):
655 self
.emit(s
, depth
+ d
)
657 value
= "o->%s" % field
.name
659 value
= "o->v.%s.%s" % (name
, field
.name
)
660 self
.set(field
, value
, depth
)
661 emit("if (!value) goto failed;", 0)
662 emit('if (PyObject_SetAttrString(result, "%s", value) == -1)' % field
.name
, 0)
663 emit("goto failed;", 1)
664 emit("Py_DECREF(value);", 0)
666 def emitSeq(self
, field
, value
, depth
, emit
):
667 emit("seq = %s;" % value
, 0)
668 emit("n = asdl_seq_LEN(seq);", 0)
669 emit("value = PyList_New(n);", 0)
670 emit("if (!value) goto failed;", 0)
671 emit("for (i = 0; i < n; i++) {", 0)
672 self
.set("value", field
, "asdl_seq_GET(seq, i)", depth
+ 1)
673 emit("if (!value1) goto failed;", 1)
674 emit("PyList_SET_ITEM(value, i, value1);", 1)
675 emit("value1 = NULL;", 1)
678 def set(self
, field
, value
, depth
):
680 # XXX should really check for is_simple, but that requires a symbol table
681 if field
.type.value
== "cmpop":
682 # While the sequence elements are stored as void*,
683 # ast2obj_cmpop expects an enum
684 self
.emit("{", depth
)
685 self
.emit("int i, n = asdl_seq_LEN(%s);" % value
, depth
+1)
686 self
.emit("value = PyList_New(n);", depth
+1)
687 self
.emit("if (!value) goto failed;", depth
+1)
688 self
.emit("for(i = 0; i < n; i++)", depth
+1)
689 # This cannot fail, so no need for error handling
690 self
.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value
,
691 depth
+2, reflow
=False)
692 self
.emit("}", depth
)
694 self
.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value
, field
.type), depth
)
696 ctype
= get_c_type(field
.type)
697 self
.emit("value = ast2obj_%s(%s);" % (field
.type, value
), depth
, reflow
=False)
700 class PartingShots(StaticVisitor
):
703 PyObject* PyAST_mod2obj(mod_ty t)
706 return ast2obj_mod(t);
710 class ChainOfVisitors
:
711 def __init__(self
, *visitors
):
712 self
.visitors
= visitors
714 def visit(self
, object):
715 for v
in self
.visitors
:
721 components
= argv0
.split(os
.sep
)
722 argv0
= os
.sep
.join(components
[-2:])
723 auto_gen_msg
= '/* File automatically generated by %s */\n' % argv0
724 mod
= asdl
.parse(srcfile
)
725 if not asdl
.check(mod
):
728 p
= "%s/%s-ast.h" % (INC_DIR
, mod
.name
)
730 print >> f
, auto_gen_msg
731 print >> f
, '#include "asdl.h"\n'
732 c
= ChainOfVisitors(TypeDefVisitor(f
),
737 print >>f
, "PyObject* PyAST_mod2obj(mod_ty t);"
741 p
= os
.path
.join(SRC_DIR
, str(mod
.name
) + "-ast.c")
743 print >> f
, auto_gen_msg
744 print >> f
, '#include "Python.h"'
745 print >> f
, '#include "%s-ast.h"' % mod
.name
747 print >>f
, "static PyTypeObject* AST_type;"
749 PyTypesDeclareVisitor(f
),
759 if __name__
== "__main__":
765 opts
, args
= getopt
.getopt(sys
.argv
[1:], "h:c:")
767 print "Must specify exactly one output file"
775 print "Must specify single input file"