1#! /usr/bin/env python
2"""Generate C code from an ASDL description."""
3
4import os, sys
5
6import asdl
7
8TABSIZE = 4
9MAX_COL = 80
10
11def get_c_type(name):
12    """Return a string for the C name of the type.
13
14    This function special cases the default types provided by asdl.
15    """
16    if name in asdl.builtin_types:
17        return name
18    else:
19        return "%s_ty" % name
20
21def reflow_lines(s, depth):
22    """Reflow the line s indented depth tabs.
23
24    Return a sequence of lines where no line extends beyond MAX_COL
25    when properly indented.  The first line is properly indented based
26    exclusively on depth * TABSIZE.  All following lines -- these are
27    the reflowed lines generated by this function -- start at the same
28    column as the first character beyond the opening { in the first
29    line.
30    """
31    size = MAX_COL - depth * TABSIZE
32    if len(s) < size:
33        return [s]
34
35    lines = []
36    cur = s
37    padding = ""
38    while len(cur) > size:
39        i = cur.rfind(' ', 0, size)
40        # XXX this should be fixed for real
41        if i == -1 and 'GeneratorExp' in cur:
42            i = size + 3
43        assert i != -1, "Impossible line %d to reflow: %r" % (size, s)
44        lines.append(padding + cur[:i])
45        if len(lines) == 1:
46            # find new size based on brace
47            j = cur.find('{', 0, i)
48            if j >= 0:
49                j += 2 # account for the brace and the space after it
50                size -= j
51                padding = " " * j
52            else:
53                j = cur.find('(', 0, i)
54                if j >= 0:
55                    j += 1 # account for the paren (no space after it)
56                    size -= j
57                    padding = " " * j
58        cur = cur[i+1:]
59    else:
60        lines.append(padding + cur)
61    return lines
62
63def is_simple(sum):
64    """Return True if a sum is a simple.
65
66    A sum is simple if its types have no fields, e.g.
67    unaryop = Invert | Not | UAdd | USub
68    """
69    for t in sum.types:
70        if t.fields:
71            return False
72    return True
73
74
75class EmitVisitor(asdl.VisitorBase):
76    """Visit that emits lines"""
77
78    def __init__(self, file):
79        self.file = file
80        self.identifiers = set()
81        super(EmitVisitor, self).__init__()
82
83    def emit_identifier(self, name):
84        name = str(name)
85        if name in self.identifiers:
86            return
87        self.emit("_Py_IDENTIFIER(%s);" % name, 0)
88        self.identifiers.add(name)
89
90    def emit(self, s, depth, reflow=True):
91        # XXX reflow long lines?
92        if reflow:
93            lines = reflow_lines(s, depth)
94        else:
95            lines = [s]
96        for line in lines:
97            if line:
98                line = (" " * TABSIZE * depth) + line
99            self.file.write(line + "\n")
100
101
102class TypeDefVisitor(EmitVisitor):
103    def visitModule(self, mod):
104        for dfn in mod.dfns:
105            self.visit(dfn)
106
107    def visitType(self, type, depth=0):
108        self.visit(type.value, type.name, depth)
109
110    def visitSum(self, sum, name, depth):
111        if is_simple(sum):
112            self.simple_sum(sum, name, depth)
113        else:
114            self.sum_with_constructors(sum, name, depth)
115
116    def simple_sum(self, sum, name, depth):
117        enum = []
118        for i in range(len(sum.types)):
119            type = sum.types[i]
120            enum.append("%s=%d" % (type.name, i + 1))
121        enums = ", ".join(enum)
122        ctype = get_c_type(name)
123        s = "typedef enum _%s { %s } %s;" % (name, enums, ctype)
124        self.emit(s, depth)
125        self.emit("", depth)
126
127    def sum_with_constructors(self, sum, name, depth):
128        ctype = get_c_type(name)
129        s = "typedef struct _%(name)s *%(ctype)s;" % locals()
130        self.emit(s, depth)
131        self.emit("", depth)
132
133    def visitProduct(self, product, name, depth):
134        ctype = get_c_type(name)
135        s = "typedef struct _%(name)s *%(ctype)s;" % locals()
136        self.emit(s, depth)
137        self.emit("", depth)
138
139
140class StructVisitor(EmitVisitor):
141    """Visitor to generate typedefs for AST."""
142
143    def visitModule(self, mod):
144        for dfn in mod.dfns:
145            self.visit(dfn)
146
147    def visitType(self, type, depth=0):
148        self.visit(type.value, type.name, depth)
149
150    def visitSum(self, sum, name, depth):
151        if not is_simple(sum):
152            self.sum_with_constructors(sum, name, depth)
153
154    def sum_with_constructors(self, sum, name, depth):
155        def emit(s, depth=depth):
156            self.emit(s % sys._getframe(1).f_locals, depth)
157        enum = []
158        for i in range(len(sum.types)):
159            type = sum.types[i]
160            enum.append("%s_kind=%d" % (type.name, i + 1))
161
162        emit("enum _%(name)s_kind {" + ", ".join(enum) + "};")
163
164        emit("struct _%(name)s {")
165        emit("enum _%(name)s_kind kind;", depth + 1)
166        emit("union {", depth + 1)
167        for t in sum.types:
168            self.visit(t, depth + 2)
169        emit("} v;", depth + 1)
170        for field in sum.attributes:
171            # rudimentary attribute handling
172            type = str(field.type)
173            assert type in asdl.builtin_types, type
174            emit("%s %s;" % (type, field.name), depth + 1);
175        emit("};")
176        emit("")
177
178    def visitConstructor(self, cons, depth):
179        if cons.fields:
180            self.emit("struct {", depth)
181            for f in cons.fields:
182                self.visit(f, depth + 1)
183            self.emit("} %s;" % cons.name, depth)
184            self.emit("", depth)
185
186    def visitField(self, field, depth):
187        # XXX need to lookup field.type, because it might be something
188        # like a builtin...
189        ctype = get_c_type(field.type)
190        name = field.name
191        if field.seq:
192            if field.type == 'cmpop':
193                self.emit("asdl_int_seq *%(name)s;" % locals(), depth)
194            else:
195                self.emit("asdl_seq *%(name)s;" % locals(), depth)
196        else:
197            self.emit("%(ctype)s %(name)s;" % locals(), depth)
198
199    def visitProduct(self, product, name, depth):
200        self.emit("struct _%(name)s {" % locals(), depth)
201        for f in product.fields:
202            self.visit(f, depth + 1)
203        for field in product.attributes:
204            # rudimentary attribute handling
205            type = str(field.type)
206            assert type in asdl.builtin_types, type
207            self.emit("%s %s;" % (type, field.name), depth + 1);
208        self.emit("};", depth)
209        self.emit("", depth)
210
211
212class PrototypeVisitor(EmitVisitor):
213    """Generate function prototypes for the .h file"""
214
215    def visitModule(self, mod):
216        for dfn in mod.dfns:
217            self.visit(dfn)
218
219    def visitType(self, type):
220        self.visit(type.value, type.name)
221
222    def visitSum(self, sum, name):
223        if is_simple(sum):
224            pass # XXX
225        else:
226            for t in sum.types:
227                self.visit(t, name, sum.attributes)
228
229    def get_args(self, fields):
230        """Return list of C argument into, one for each field.
231
232        Argument info is 3-tuple of a C type, variable name, and flag
233        that is true if type can be NULL.
234        """
235        args = []
236        unnamed = {}
237        for f in fields:
238            if f.name is None:
239                name = f.type
240                c = unnamed[name] = unnamed.get(name, 0) + 1
241                if c > 1:
242                    name = "name%d" % (c - 1)
243            else:
244                name = f.name
245            # XXX should extend get_c_type() to handle this
246            if f.seq:
247                if f.type == 'cmpop':
248                    ctype = "asdl_int_seq *"
249                else:
250                    ctype = "asdl_seq *"
251            else:
252                ctype = get_c_type(f.type)
253            args.append((ctype, name, f.opt or f.seq))
254        return args
255
256    def visitConstructor(self, cons, type, attrs):
257        args = self.get_args(cons.fields)
258        attrs = self.get_args(attrs)
259        ctype = get_c_type(type)
260        self.emit_function(cons.name, ctype, args, attrs)
261
262    def emit_function(self, name, ctype, args, attrs, union=True):
263        args = args + attrs
264        if args:
265            argstr = ", ".join(["%s %s" % (atype, aname)
266                                for atype, aname, opt in args])
267            argstr += ", PyArena *arena"
268        else:
269            argstr = "PyArena *arena"
270        margs = "a0"
271        for i in range(1, len(args)+1):
272            margs += ", a%d" % i
273        self.emit("#define %s(%s) _Ta3_%s(%s)" % (name, margs, name, margs), 0,
274                reflow=False)
275        self.emit("%s _Ta3_%s(%s);" % (ctype, name, argstr), False)
276
277    def visitProduct(self, prod, name):
278        self.emit_function(name, get_c_type(name),
279                           self.get_args(prod.fields),
280                           self.get_args(prod.attributes),
281                           union=False)
282
283
284class FunctionVisitor(PrototypeVisitor):
285    """Visitor to generate constructor functions for AST."""
286
287    def emit_function(self, name, ctype, args, attrs, union=True):
288        def emit(s, depth=0, reflow=True):
289            self.emit(s, depth, reflow)
290        argstr = ", ".join(["%s %s" % (atype, aname)
291                            for atype, aname, opt in args + attrs])
292        if argstr:
293            argstr += ", PyArena *arena"
294        else:
295            argstr = "PyArena *arena"
296        self.emit("%s" % ctype, 0)
297        emit("%s(%s)" % (name, argstr))
298        emit("{")
299        emit("%s p;" % ctype, 1)
300        for argtype, argname, opt in args:
301            if not opt and argtype != "int":
302                emit("if (!%s) {" % argname, 1)
303                emit("PyErr_SetString(PyExc_ValueError,", 2)
304                msg = "field %s is required for %s" % (argname, name)
305                emit('                "%s");' % msg,
306                     2, reflow=False)
307                emit('return NULL;', 2)
308                emit('}', 1)
309
310        emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
311        emit("if (!p)", 1)
312        emit("return NULL;", 2)
313        if union:
314            self.emit_body_union(name, args, attrs)
315        else:
316            self.emit_body_struct(name, args, attrs)
317        emit("return p;", 1)
318        emit("}")
319        emit("")
320
321    def emit_body_union(self, name, args, attrs):
322        def emit(s, depth=0, reflow=True):
323            self.emit(s, depth, reflow)
324        emit("p->kind = %s_kind;" % name, 1)
325        for argtype, argname, opt in args:
326            emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
327        for argtype, argname, opt in attrs:
328            emit("p->%s = %s;" % (argname, argname), 1)
329
330    def emit_body_struct(self, name, args, attrs):
331        def emit(s, depth=0, reflow=True):
332            self.emit(s, depth, reflow)
333        for argtype, argname, opt in args:
334            emit("p->%s = %s;" % (argname, argname), 1)
335        for argtype, argname, opt in attrs:
336            emit("p->%s = %s;" % (argname, argname), 1)
337
338
339class PickleVisitor(EmitVisitor):
340
341    def visitModule(self, mod):
342        for dfn in mod.dfns:
343            self.visit(dfn)
344
345    def visitType(self, type):
346        self.visit(type.value, type.name)
347
348    def visitSum(self, sum, name):
349        pass
350
351    def visitProduct(self, sum, name):
352        pass
353
354    def visitConstructor(self, cons, name):
355        pass
356
357    def visitField(self, sum):
358        pass
359
360
361class Obj2ModPrototypeVisitor(PickleVisitor):
362    def visitProduct(self, prod, name):
363        code = "static int obj2ast_%s(PyObject* obj, %s* out, PyArena* arena);"
364        self.emit(code % (name, get_c_type(name)), 0)
365
366    visitSum = visitProduct
367
368
369class Obj2ModVisitor(PickleVisitor):
370    def funcHeader(self, name):
371        ctype = get_c_type(name)
372        self.emit("int", 0)
373        self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
374        self.emit("{", 0)
375        self.emit("int isinstance;", 1)
376        self.emit("", 0)
377
378    def sumTrailer(self, name, add_label=False):
379        self.emit("", 0)
380        # there's really nothing more we can do if this fails ...
381        error = "expected some sort of %s, but got %%R" % name
382        format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);"
383        self.emit(format % error, 1, reflow=False)
384        if add_label:
385            self.emit("failed:", 1)
386            self.emit("Py_XDECREF(tmp);", 1)
387        self.emit("return 1;", 1)
388        self.emit("}", 0)
389        self.emit("", 0)
390
391    def simpleSum(self, sum, name):
392        self.funcHeader(name)
393        for t in sum.types:
394            line = ("isinstance = PyObject_IsInstance(obj, "
395                    "(PyObject *)%s_type);")
396            self.emit(line % (t.name,), 1)
397            self.emit("if (isinstance == -1) {", 1)
398            self.emit("return 1;", 2)
399            self.emit("}", 1)
400            self.emit("if (isinstance) {", 1)
401            self.emit("*out = %s;" % t.name, 2)
402            self.emit("return 0;", 2)
403            self.emit("}", 1)
404        self.sumTrailer(name)
405
406    def buildArgs(self, fields):
407        return ", ".join(fields + ["arena"])
408
409    def complexSum(self, sum, name):
410        self.funcHeader(name)
411        self.emit("PyObject *tmp = NULL;", 1)
412        for a in sum.attributes:
413            self.visitAttributeDeclaration(a, name, sum=sum)
414        self.emit("", 0)
415        # XXX: should we only do this for 'expr'?
416        self.emit("if (obj == Py_None) {", 1)
417        self.emit("*out = NULL;", 2)
418        self.emit("return 0;", 2)
419        self.emit("}", 1)
420        for a in sum.attributes:
421            self.visitField(a, name, sum=sum, depth=1)
422        for t in sum.types:
423            line = "isinstance = PyObject_IsInstance(obj, (PyObject*)%s_type);"
424            self.emit(line % (t.name,), 1)
425            self.emit("if (isinstance == -1) {", 1)
426            self.emit("return 1;", 2)
427            self.emit("}", 1)
428            self.emit("if (isinstance) {", 1)
429            for f in t.fields:
430                self.visitFieldDeclaration(f, t.name, sum=sum, depth=2)
431            self.emit("", 0)
432            for f in t.fields:
433                self.visitField(f, t.name, sum=sum, depth=2)
434            args = [f.name for f in t.fields] + [a.name for a in sum.attributes]
435            self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
436            self.emit("if (*out == NULL) goto failed;", 2)
437            self.emit("return 0;", 2)
438            self.emit("}", 1)
439        self.sumTrailer(name, True)
440
441    def visitAttributeDeclaration(self, a, name, sum=sum):
442        ctype = get_c_type(a.type)
443        self.emit("%s %s;" % (ctype, a.name), 1)
444
445    def visitSum(self, sum, name):
446        if is_simple(sum):
447            self.simpleSum(sum, name)
448        else:
449            self.complexSum(sum, name)
450
451    def visitProduct(self, prod, name):
452        ctype = get_c_type(name)
453        self.emit("int", 0)
454        self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
455        self.emit("{", 0)
456        self.emit("PyObject* tmp = NULL;", 1)
457        for f in prod.fields:
458            self.visitFieldDeclaration(f, name, prod=prod, depth=1)
459        for a in prod.attributes:
460            self.visitFieldDeclaration(a, name, prod=prod, depth=1)
461        self.emit("", 0)
462        for f in prod.fields:
463            self.visitField(f, name, prod=prod, depth=1)
464        for a in prod.attributes:
465            self.visitField(a, name, prod=prod, depth=1)
466        args = [f.name for f in prod.fields]
467        args.extend([a.name for a in prod.attributes])
468        self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1)
469        self.emit("return 0;", 1)
470        self.emit("failed:", 0)
471        self.emit("Py_XDECREF(tmp);", 1)
472        self.emit("return 1;", 1)
473        self.emit("}", 0)
474        self.emit("", 0)
475
476    def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0):
477        ctype = get_c_type(field.type)
478        if field.seq:
479            if self.isSimpleType(field):
480                self.emit("asdl_int_seq* %s;" % field.name, depth)
481            else:
482                self.emit("asdl_seq* %s;" % field.name, depth)
483        else:
484            ctype = get_c_type(field.type)
485            self.emit("%s %s;" % (ctype, field.name), depth)
486
487    def isSimpleSum(self, field):
488        # XXX can the members of this list be determined automatically?
489        return field.type in ('expr_context', 'boolop', 'operator',
490                              'unaryop', 'cmpop')
491
492    def isNumeric(self, field):
493        return get_c_type(field.type) in ("int", "bool")
494
495    def isSimpleType(self, field):
496        return self.isSimpleSum(field) or self.isNumeric(field)
497
498    def visitField(self, field, name, sum=None, prod=None, depth=0):
499        ctype = get_c_type(field.type)
500        self.emit("if (lookup_attr_id(obj, &PyId_%s, &tmp) < 0) {" % field.name, depth)
501        self.emit("return 1;", depth+1)
502        self.emit("}", depth)
503        if not field.opt:
504            self.emit("if (tmp == NULL) {", depth)
505            message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
506            format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
507            self.emit(format % message, depth+1, reflow=False)
508            self.emit("return 1;", depth+1)
509        else:
510            self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
511            self.emit("Py_CLEAR(tmp);", depth+1)
512            if self.isNumeric(field):
513                self.emit("%s = 0;" % field.name, depth+1)
514            elif not self.isSimpleType(field):
515                self.emit("%s = NULL;" % field.name, depth+1)
516            else:
517                raise TypeError("could not determine the default value for %s" % field.name)
518        self.emit("}", depth)
519        self.emit("else {", depth)
520
521        self.emit("int res;", depth+1)
522        if field.seq:
523            self.emit("Py_ssize_t len;", depth+1)
524            self.emit("Py_ssize_t i;", depth+1)
525            self.emit("if (!PyList_Check(tmp)) {", depth+1)
526            self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
527                      "be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
528                      (name, field.name),
529                      depth+2, reflow=False)
530            self.emit("goto failed;", depth+2)
531            self.emit("}", depth+1)
532            self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
533            if self.isSimpleType(field):
534                self.emit("%s = _Ta3_asdl_int_seq_new(len, arena);" % field.name, depth+1)
535            else:
536                self.emit("%s = _Ta3_asdl_seq_new(len, arena);" % field.name, depth+1)
537            self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
538            self.emit("for (i = 0; i < len; i++) {", depth+1)
539            self.emit("%s val;" % ctype, depth+2)
540            self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &val, arena);" %
541                      field.type, depth+2, reflow=False)
542            self.emit("if (res != 0) goto failed;", depth+2)
543            self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
544            self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
545                      "changed size during iteration\");" %
546                      (name, field.name),
547                      depth+3, reflow=False)
548            self.emit("goto failed;", depth+3)
549            self.emit("}", depth+2)
550            self.emit("asdl_seq_SET(%s, i, val);" % field.name, depth+2)
551            self.emit("}", depth+1)
552        else:
553            self.emit("res = obj2ast_%s(tmp, &%s, arena);" %
554                      (field.type, field.name), depth+1)
555            self.emit("if (res != 0) goto failed;", depth+1)
556
557        self.emit("Py_CLEAR(tmp);", depth+1)
558        self.emit("}", depth)
559
560
561class MarshalPrototypeVisitor(PickleVisitor):
562
563    def prototype(self, sum, name):
564        ctype = get_c_type(name)
565        self.emit("static int marshal_write_%s(PyObject **, int *, %s);"
566                  % (name, ctype), 0)
567
568    visitProduct = visitSum = prototype
569
570
571class PyTypesDeclareVisitor(PickleVisitor):
572
573    def visitProduct(self, prod, name):
574        self.emit("static PyTypeObject *%s_type;" % name, 0)
575        self.emit("static PyObject* ast2obj_%s(void*);" % name, 0)
576        if prod.attributes:
577            for a in prod.attributes:
578                self.emit_identifier(a.name)
579            self.emit("static char *%s_attributes[] = {" % name, 0)
580            for a in prod.attributes:
581                self.emit('"%s",' % a.name, 1)
582            self.emit("};", 0)
583        if prod.fields:
584            for f in prod.fields:
585                self.emit_identifier(f.name)
586            self.emit("static char *%s_fields[]={" % name,0)
587            for f in prod.fields:
588                self.emit('"%s",' % f.name, 1)
589            self.emit("};", 0)
590
591    def visitSum(self, sum, name):
592        self.emit("static PyTypeObject *%s_type;" % name, 0)
593        if sum.attributes:
594            for a in sum.attributes:
595                self.emit_identifier(a.name)
596            self.emit("static char *%s_attributes[] = {" % name, 0)
597            for a in sum.attributes:
598                self.emit('"%s",' % a.name, 1)
599            self.emit("};", 0)
600        ptype = "void*"
601        if is_simple(sum):
602            ptype = get_c_type(name)
603            tnames = []
604            for t in sum.types:
605                tnames.append(str(t.name)+"_singleton")
606            tnames = ", *".join(tnames)
607            self.emit("static PyObject *%s;" % tnames, 0)
608        self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0)
609        for t in sum.types:
610            self.visitConstructor(t, name)
611
612    def visitConstructor(self, cons, name):
613        self.emit("static PyTypeObject *%s_type;" % cons.name, 0)
614        if cons.fields:
615            for t in cons.fields:
616                self.emit_identifier(t.name)
617            self.emit("static char *%s_fields[]={" % cons.name, 0)
618            for t in cons.fields:
619                self.emit('"%s",' % t.name, 1)
620            self.emit("};",0)
621
622class PyTypesVisitor(PickleVisitor):
623
624    def visitModule(self, mod):
625        self.emit("""
626_Py_IDENTIFIER(_fields);
627_Py_IDENTIFIER(_attributes);
628
629typedef struct {
630    PyObject_HEAD
631    PyObject *dict;
632} AST_object;
633
634static void
635ast_dealloc(AST_object *self)
636{
637    /* bpo-31095: UnTrack is needed before calling any callbacks */
638    PyObject_GC_UnTrack(self);
639    Py_CLEAR(self->dict);
640    Py_TYPE(self)->tp_free(self);
641}
642
643static int
644ast_traverse(AST_object *self, visitproc visit, void *arg)
645{
646    Py_VISIT(self->dict);
647    return 0;
648}
649
650static int
651ast_clear(AST_object *self)
652{
653    Py_CLEAR(self->dict);
654    return 0;
655}
656
657static int lookup_attr_id(PyObject *v, _Py_Identifier *name, PyObject **result)
658{
659    PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
660    if (!oname) {
661        *result = NULL;
662        return -1;
663    }
664    *result = PyObject_GetAttr(v, oname);
665    if (*result == NULL) {
666        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
667            return -1;
668        }
669        PyErr_Clear();
670    }
671    return 0;
672}
673
674static int
675ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
676{
677    Py_ssize_t i, numfields = 0;
678    int res = -1;
679    PyObject *key, *value, *fields;
680    if (lookup_attr_id((PyObject*)Py_TYPE(self), &PyId__fields, &fields) < 0) {
681        goto cleanup;
682    }
683    if (fields) {
684        numfields = PySequence_Size(fields);
685        if (numfields == -1)
686            goto cleanup;
687    }
688
689    res = 0; /* if no error occurs, this stays 0 to the end */
690    if (numfields < PyTuple_GET_SIZE(args)) {
691        PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
692                     "%zd positional argument%s",
693                     Py_TYPE(self)->tp_name,
694                     numfields, numfields == 1 ? "" : "s");
695        res = -1;
696        goto cleanup;
697    }
698    for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
699        /* cannot be reached when fields is NULL */
700        PyObject *name = PySequence_GetItem(fields, i);
701        if (!name) {
702            res = -1;
703            goto cleanup;
704        }
705        res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
706        Py_DECREF(name);
707        if (res < 0)
708            goto cleanup;
709    }
710    if (kw) {
711        i = 0;  /* needed by PyDict_Next */
712        while (PyDict_Next(kw, &i, &key, &value)) {
713            res = PyObject_SetAttr(self, key, value);
714            if (res < 0)
715                goto cleanup;
716        }
717    }
718  cleanup:
719    Py_XDECREF(fields);
720    return res;
721}
722
723/* Pickling support */
724static PyObject *
725ast_type_reduce(PyObject *self, PyObject *unused)
726{
727    _Py_IDENTIFIER(__dict__);
728    PyObject *dict;
729    if (lookup_attr_id(self, &PyId___dict__, &dict) < 0) {
730        return NULL;
731    }
732    if (dict) {
733        return Py_BuildValue("O()N", Py_TYPE(self), dict);
734    }
735    return Py_BuildValue("O()", Py_TYPE(self));
736}
737
738static PyMethodDef ast_type_methods[] = {
739    {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
740    {NULL}
741};
742
743static PyGetSetDef ast_type_getsets[] = {
744    {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
745    {NULL}
746};
747
748static PyTypeObject AST_type = {
749    PyVarObject_HEAD_INIT(NULL, 0)
750    "typed_ast._ast3.AST",
751    sizeof(AST_object),
752    0,
753    (destructor)ast_dealloc, /* tp_dealloc */
754    0,                       /* tp_print */
755    0,                       /* tp_getattr */
756    0,                       /* tp_setattr */
757    0,                       /* tp_reserved */
758    0,                       /* tp_repr */
759    0,                       /* tp_as_number */
760    0,                       /* tp_as_sequence */
761    0,                       /* tp_as_mapping */
762    0,                       /* tp_hash */
763    0,                       /* tp_call */
764    0,                       /* tp_str */
765    PyObject_GenericGetAttr, /* tp_getattro */
766    PyObject_GenericSetAttr, /* tp_setattro */
767    0,                       /* tp_as_buffer */
768    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
769    0,                       /* tp_doc */
770    (traverseproc)ast_traverse, /* tp_traverse */
771    (inquiry)ast_clear,      /* tp_clear */
772    0,                       /* tp_richcompare */
773    0,                       /* tp_weaklistoffset */
774    0,                       /* tp_iter */
775    0,                       /* tp_iternext */
776    ast_type_methods,        /* tp_methods */
777    0,                       /* tp_members */
778    ast_type_getsets,        /* tp_getset */
779    0,                       /* tp_base */
780    0,                       /* tp_dict */
781    0,                       /* tp_descr_get */
782    0,                       /* tp_descr_set */
783    offsetof(AST_object, dict),/* tp_dictoffset */
784    (initproc)ast_type_init, /* tp_init */
785    PyType_GenericAlloc,     /* tp_alloc */
786    PyType_GenericNew,       /* tp_new */
787    PyObject_GC_Del,         /* tp_free */
788};
789
790
791static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
792{
793    _Py_IDENTIFIER(__module__);
794    _Py_static_string(PyId_typed_ast_ast3, "typed_ast._ast3");
795    PyObject *fnames, *result;
796    int i;
797    fnames = PyTuple_New(num_fields);
798    if (!fnames) return NULL;
799    for (i = 0; i < num_fields; i++) {
800        PyObject *field = PyUnicode_FromString(fields[i]);
801        if (!field) {
802            Py_DECREF(fnames);
803            return NULL;
804        }
805        PyTuple_SET_ITEM(fnames, i, field);
806    }
807    result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
808                    type, base,
809                    _PyUnicode_FromId(&PyId__fields), fnames,
810                    _PyUnicode_FromId(&PyId___module__),
811                    _PyUnicode_FromId(&PyId_typed_ast_ast3));
812    Py_DECREF(fnames);
813    return (PyTypeObject*)result;
814}
815
816static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
817{
818    int i, result;
819    PyObject *s, *l = PyTuple_New(num_fields);
820    if (!l)
821        return 0;
822    for (i = 0; i < num_fields; i++) {
823        s = PyUnicode_FromString(attrs[i]);
824        if (!s) {
825            Py_DECREF(l);
826            return 0;
827        }
828        PyTuple_SET_ITEM(l, i, s);
829    }
830    result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
831    Py_DECREF(l);
832    return result;
833}
834
835/* Conversion AST -> Python */
836
837static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
838{
839    Py_ssize_t i, n = asdl_seq_LEN(seq);
840    PyObject *result = PyList_New(n);
841    PyObject *value;
842    if (!result)
843        return NULL;
844    for (i = 0; i < n; i++) {
845        value = func(asdl_seq_GET(seq, i));
846        if (!value) {
847            Py_DECREF(result);
848            return NULL;
849        }
850        PyList_SET_ITEM(result, i, value);
851    }
852    return result;
853}
854
855static PyObject* ast2obj_object(void *o)
856{
857    if (!o)
858        o = Py_None;
859    Py_INCREF((PyObject*)o);
860    return (PyObject*)o;
861}
862#define ast2obj_singleton ast2obj_object
863#define ast2obj_constant ast2obj_object
864#define ast2obj_identifier ast2obj_object
865#define ast2obj_string ast2obj_object
866#define ast2obj_bytes ast2obj_object
867
868static PyObject* ast2obj_int(long b)
869{
870    return PyLong_FromLong(b);
871}
872
873/* Conversion Python -> AST */
874
875static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
876{
877    if (obj != Py_None && obj != Py_True && obj != Py_False) {
878        PyErr_SetString(PyExc_ValueError,
879                        "AST singleton must be True, False, or None");
880        return 1;
881    }
882    *out = obj;
883    return 0;
884}
885
886static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
887{
888    if (obj == Py_None)
889        obj = NULL;
890    if (obj) {
891        if (PyArena_AddPyObject(arena, obj) < 0) {
892            *out = NULL;
893            return -1;
894        }
895        Py_INCREF(obj);
896    }
897    *out = obj;
898    return 0;
899}
900
901static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
902{
903    if (obj) {
904        if (PyArena_AddPyObject(arena, obj) < 0) {
905            *out = NULL;
906            return -1;
907        }
908        Py_INCREF(obj);
909    }
910    *out = obj;
911    return 0;
912}
913
914static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
915{
916    if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
917        PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
918        return 1;
919    }
920    return obj2ast_object(obj, out, arena);
921}
922
923static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
924{
925    if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
926        PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
927        return 1;
928    }
929    return obj2ast_object(obj, out, arena);
930}
931
932static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
933{
934    if (!PyBytes_CheckExact(obj)) {
935        PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
936        return 1;
937    }
938    return obj2ast_object(obj, out, arena);
939}
940
941static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
942{
943    int i;
944    if (!PyLong_Check(obj)) {
945        PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
946        return 1;
947    }
948
949    i = _PyLong_AsInt(obj);
950    if (i == -1 && PyErr_Occurred())
951        return 1;
952    *out = i;
953    return 0;
954}
955
956static int add_ast_fields(void)
957{
958    PyObject *empty_tuple, *d;
959    if (PyType_Ready(&AST_type) < 0)
960        return -1;
961    d = AST_type.tp_dict;
962    empty_tuple = PyTuple_New(0);
963    if (!empty_tuple ||
964        _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
965        _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
966        Py_XDECREF(empty_tuple);
967        return -1;
968    }
969    Py_DECREF(empty_tuple);
970    return 0;
971}
972
973""", 0, reflow=False)
974
975        self.emit("static int init_types(void)",0)
976        self.emit("{", 0)
977        self.emit("static int initialized;", 1)
978        self.emit("if (initialized) return 1;", 1)
979        self.emit("if (add_ast_fields() < 0) return 0;", 1)
980        for dfn in mod.dfns:
981            self.visit(dfn)
982        self.emit("initialized = 1;", 1)
983        self.emit("return 1;", 1);
984        self.emit("}", 0)
985
986    def visitProduct(self, prod, name):
987        if prod.fields:
988            fields = name+"_fields"
989        else:
990            fields = "NULL"
991        self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' %
992                        (name, name, fields, len(prod.fields)), 1)
993        self.emit("if (!%s_type) return 0;" % name, 1)
994        if prod.attributes:
995            self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
996                            (name, name, len(prod.attributes)), 1)
997        else:
998            self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
999
1000    def visitSum(self, sum, name):
1001        self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' %
1002                  (name, name), 1)
1003        self.emit("if (!%s_type) return 0;" % name, 1)
1004        if sum.attributes:
1005            self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
1006                            (name, name, len(sum.attributes)), 1)
1007        else:
1008            self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
1009        simple = is_simple(sum)
1010        for t in sum.types:
1011            self.visitConstructor(t, name, simple)
1012
1013    def visitConstructor(self, cons, name, simple):
1014        if cons.fields:
1015            fields = cons.name+"_fields"
1016        else:
1017            fields = "NULL"
1018        self.emit('%s_type = make_type("%s", %s_type, %s, %d);' %
1019                            (cons.name, cons.name, name, fields, len(cons.fields)), 1)
1020        self.emit("if (!%s_type) return 0;" % cons.name, 1)
1021        if simple:
1022            self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" %
1023                             (cons.name, cons.name), 1)
1024            self.emit("if (!%s_singleton) return 0;" % cons.name, 1)
1025
1026
1027class ASTModuleVisitor(PickleVisitor):
1028
1029    def visitModule(self, mod):
1030        self.emit("PyObject *ast3_parse(PyObject *self, PyObject *args);", 0)
1031        self.emit("static PyMethodDef ast3_methods[] = {", 0)
1032        self.emit('    {"_parse",  ast3_parse, METH_VARARGS, "Parse string into typed AST."},', 0)
1033        self.emit("    {NULL, NULL, 0, NULL}", 0)
1034        self.emit("};", 0)
1035        self.emit("static struct PyModuleDef _astmodule = {", 0)
1036        self.emit('    PyModuleDef_HEAD_INIT, "_ast3", NULL, 0, ast3_methods', 0)
1037        self.emit("};", 0)
1038        self.emit("PyMODINIT_FUNC", 0)
1039        self.emit("PyInit__ast3(void)", 0)
1040        self.emit("{", 0)
1041        self.emit("PyObject *m, *d;", 1)
1042        self.emit("if (!init_types()) return NULL;", 1)
1043        self.emit('m = PyModule_Create(&_astmodule);', 1)
1044        self.emit("if (!m) return NULL;", 1)
1045        self.emit("d = PyModule_GetDict(m);", 1)
1046        self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1)
1047        self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)', 1)
1048        self.emit("return NULL;", 2)
1049        for dfn in mod.dfns:
1050            self.visit(dfn)
1051        self.emit("return m;", 1)
1052        self.emit("}", 0)
1053
1054    def visitProduct(self, prod, name):
1055        self.addObj(name)
1056
1057    def visitSum(self, sum, name):
1058        self.addObj(name)
1059        for t in sum.types:
1060            self.visitConstructor(t, name)
1061
1062    def visitConstructor(self, cons, name):
1063        self.addObj(cons.name)
1064
1065    def addObj(self, name):
1066        self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return NULL;' % (name, name), 1)
1067
1068
1069_SPECIALIZED_SEQUENCES = ('stmt', 'expr')
1070
1071def find_sequence(fields, doing_specialization):
1072    """Return True if any field uses a sequence."""
1073    for f in fields:
1074        if f.seq:
1075            if not doing_specialization:
1076                return True
1077            if str(f.type) not in _SPECIALIZED_SEQUENCES:
1078                return True
1079    return False
1080
1081def has_sequence(types, doing_specialization):
1082    for t in types:
1083        if find_sequence(t.fields, doing_specialization):
1084            return True
1085    return False
1086
1087
1088class StaticVisitor(PickleVisitor):
1089    CODE = '''Very simple, always emit this static code.  Override CODE'''
1090
1091    def visit(self, object):
1092        self.emit(self.CODE, 0, reflow=False)
1093
1094
1095class ObjVisitor(PickleVisitor):
1096
1097    def func_begin(self, name):
1098        ctype = get_c_type(name)
1099        self.emit("PyObject*", 0)
1100        self.emit("ast2obj_%s(void* _o)" % (name), 0)
1101        self.emit("{", 0)
1102        self.emit("%s o = (%s)_o;" % (ctype, ctype), 1)
1103        self.emit("PyObject *result = NULL, *value = NULL;", 1)
1104        self.emit('if (!o) {', 1)
1105        self.emit("Py_RETURN_NONE;", 2)
1106        self.emit("}", 1)
1107        self.emit('', 0)
1108
1109    def func_end(self):
1110        self.emit("return result;", 1)
1111        self.emit("failed:", 0)
1112        self.emit("Py_XDECREF(value);", 1)
1113        self.emit("Py_XDECREF(result);", 1)
1114        self.emit("return NULL;", 1)
1115        self.emit("}", 0)
1116        self.emit("", 0)
1117
1118    def visitSum(self, sum, name):
1119        if is_simple(sum):
1120            self.simpleSum(sum, name)
1121            return
1122        self.func_begin(name)
1123        self.emit("switch (o->kind) {", 1)
1124        for i in range(len(sum.types)):
1125            t = sum.types[i]
1126            self.visitConstructor(t, i + 1, name)
1127        self.emit("}", 1)
1128        for a in sum.attributes:
1129            self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
1130            self.emit("if (!value) goto failed;", 1)
1131            self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
1132            self.emit('goto failed;', 2)
1133            self.emit('Py_DECREF(value);', 1)
1134        self.func_end()
1135
1136    def simpleSum(self, sum, name):
1137        self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0)
1138        self.emit("{", 0)
1139        self.emit("switch(o) {", 1)
1140        for t in sum.types:
1141            self.emit("case %s:" % t.name, 2)
1142            self.emit("Py_INCREF(%s_singleton);" % t.name, 3)
1143            self.emit("return %s_singleton;" % t.name, 3)
1144        self.emit("default:", 2)
1145        self.emit('/* should never happen, but just in case ... */', 3)
1146        code = "PyErr_Format(PyExc_SystemError, \"unknown %s found\");" % name
1147        self.emit(code, 3, reflow=False)
1148        self.emit("return NULL;", 3)
1149        self.emit("}", 1)
1150        self.emit("}", 0)
1151
1152    def visitProduct(self, prod, name):
1153        self.func_begin(name)
1154        self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1);
1155        self.emit("if (!result) return NULL;", 1)
1156        for field in prod.fields:
1157            self.visitField(field, name, 1, True)
1158        for a in prod.attributes:
1159            self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
1160            self.emit("if (!value) goto failed;", 1)
1161            self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
1162            self.emit('goto failed;', 2)
1163            self.emit('Py_DECREF(value);', 1)
1164        self.func_end()
1165
1166    def visitConstructor(self, cons, enum, name):
1167        self.emit("case %s_kind:" % cons.name, 1)
1168        self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2);
1169        self.emit("if (!result) goto failed;", 2)
1170        for f in cons.fields:
1171            self.visitField(f, cons.name, 2, False)
1172        self.emit("break;", 2)
1173
1174    def visitField(self, field, name, depth, product):
1175        def emit(s, d):
1176            self.emit(s, depth + d)
1177        if product:
1178            value = "o->%s" % field.name
1179        else:
1180            value = "o->v.%s.%s" % (name, field.name)
1181        self.set(field, value, depth)
1182        emit("if (!value) goto failed;", 0)
1183        emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) == -1)' % field.name, 0)
1184        emit("goto failed;", 1)
1185        emit("Py_DECREF(value);", 0)
1186
1187    def emitSeq(self, field, value, depth, emit):
1188        emit("seq = %s;" % value, 0)
1189        emit("n = asdl_seq_LEN(seq);", 0)
1190        emit("value = PyList_New(n);", 0)
1191        emit("if (!value) goto failed;", 0)
1192        emit("for (i = 0; i < n; i++) {", 0)
1193        self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1)
1194        emit("if (!value1) goto failed;", 1)
1195        emit("PyList_SET_ITEM(value, i, value1);", 1)
1196        emit("value1 = NULL;", 1)
1197        emit("}", 0)
1198
1199    def set(self, field, value, depth):
1200        if field.seq:
1201            # XXX should really check for is_simple, but that requires a symbol table
1202            if field.type == "cmpop":
1203                # While the sequence elements are stored as void*,
1204                # ast2obj_cmpop expects an enum
1205                self.emit("{", depth)
1206                self.emit("Py_ssize_t i, n = asdl_seq_LEN(%s);" % value, depth+1)
1207                self.emit("value = PyList_New(n);", depth+1)
1208                self.emit("if (!value) goto failed;", depth+1)
1209                self.emit("for(i = 0; i < n; i++)", depth+1)
1210                # This cannot fail, so no need for error handling
1211                self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value,
1212                          depth+2, reflow=False)
1213                self.emit("}", depth)
1214            else:
1215                self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth)
1216        else:
1217            ctype = get_c_type(field.type)
1218            self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False)
1219
1220
1221class PartingShots(StaticVisitor):
1222
1223    CODE = """
1224PyObject* Ta3AST_mod2obj(mod_ty t)
1225{
1226    if (!init_types())
1227        return NULL;
1228    return ast2obj_mod(t);
1229}
1230
1231/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
1232mod_ty Ta3AST_obj2mod(PyObject* ast, PyArena* arena, int mode)
1233{
1234    mod_ty res;
1235    PyObject *req_type[3];
1236    char *req_name[] = {"Module", "Expression", "Interactive"};
1237    int isinstance;
1238
1239    req_type[0] = (PyObject*)Module_type;
1240    req_type[1] = (PyObject*)Expression_type;
1241    req_type[2] = (PyObject*)Interactive_type;
1242
1243    assert(0 <= mode && mode <= 2);
1244
1245    if (!init_types())
1246        return NULL;
1247
1248    isinstance = PyObject_IsInstance(ast, req_type[mode]);
1249    if (isinstance == -1)
1250        return NULL;
1251    if (!isinstance) {
1252        PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
1253                     req_name[mode], Py_TYPE(ast)->tp_name);
1254        return NULL;
1255    }
1256    if (obj2ast_mod(ast, &res, arena) != 0)
1257        return NULL;
1258    else
1259        return res;
1260}
1261
1262int Ta3AST_Check(PyObject* obj)
1263{
1264    if (!init_types())
1265        return -1;
1266    return PyObject_IsInstance(obj, (PyObject*)&AST_type);
1267}
1268"""
1269
1270class ChainOfVisitors:
1271    def __init__(self, *visitors):
1272        self.visitors = visitors
1273
1274    def visit(self, object):
1275        for v in self.visitors:
1276            v.visit(object)
1277            v.emit("", 0)
1278
1279common_msg = "/* File automatically generated by %s. */\n\n"
1280
1281def main(srcfile, dump_module=False):
1282    argv0 = sys.argv[0]
1283    components = argv0.split(os.sep)
1284    argv0 = os.sep.join(components[-2:])
1285    auto_gen_msg = common_msg % argv0
1286    mod = asdl.parse(srcfile)
1287    if dump_module:
1288        print('Parsed Module:')
1289        print(mod)
1290    if not asdl.check(mod):
1291        sys.exit(1)
1292    if H_FILE:
1293        with open(H_FILE, "w") as f:
1294            f.write(auto_gen_msg)
1295            f.write('#include "asdl.h"\n\n')
1296            c = ChainOfVisitors(TypeDefVisitor(f),
1297                                StructVisitor(f),
1298                                PrototypeVisitor(f),
1299                                )
1300            c.visit(mod)
1301            f.write("PyObject* Ta3AST_mod2obj(mod_ty t);\n")
1302            f.write("mod_ty Ta3AST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
1303            f.write("int Ta3AST_Check(PyObject* obj);\n")
1304
1305    if C_FILE:
1306        with open(C_FILE, "w") as f:
1307            f.write(auto_gen_msg)
1308            f.write('#include <stddef.h>\n')
1309            f.write('\n')
1310            f.write('#include "Python.h"\n')
1311            f.write('#include "%s-ast.h"\n' % mod.name)
1312            f.write('\n')
1313            f.write("static PyTypeObject AST_type;\n")
1314            v = ChainOfVisitors(
1315                PyTypesDeclareVisitor(f),
1316                PyTypesVisitor(f),
1317                Obj2ModPrototypeVisitor(f),
1318                FunctionVisitor(f),
1319                ObjVisitor(f),
1320                Obj2ModVisitor(f),
1321                ASTModuleVisitor(f),
1322                PartingShots(f),
1323                )
1324            v.visit(mod)
1325
1326if __name__ == "__main__":
1327    import getopt
1328
1329    H_FILE = ''
1330    C_FILE = ''
1331    dump_module = False
1332    opts, args = getopt.getopt(sys.argv[1:], "dh:c:")
1333    for o, v in opts:
1334        if o == '-h':
1335            H_FILE = v
1336        if o == '-c':
1337            C_FILE = v
1338        if o == '-d':
1339            dump_module = True
1340    if H_FILE and C_FILE:
1341        print('Must specify exactly one output file')
1342        sys.exit(1)
1343    elif len(args) != 1:
1344        print('Must specify single input file')
1345        sys.exit(1)
1346    main(args[0], dump_module)
1347