asdl_c.py 43.6 KB
Newer Older
1
#! /usr/bin/env python
Jeremy Hylton's avatar
Jeremy Hylton committed
2 3
"""Generate C code from an ASDL description."""

4
import os, sys
Jeremy Hylton's avatar
Jeremy Hylton committed
5 6 7

import asdl

8
TABSIZE = 4
Jeremy Hylton's avatar
Jeremy Hylton committed
9 10 11 12 13
MAX_COL = 80

def get_c_type(name):
    """Return a string for the C name of the type.

14
    This function special cases the default types provided by asdl.
Jeremy Hylton's avatar
Jeremy Hylton committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    """
    if name in asdl.builtin_types:
        return name
    else:
        return "%s_ty" % name

def reflow_lines(s, depth):
    """Reflow the line s indented depth tabs.

    Return a sequence of lines where no line extends beyond MAX_COL
    when properly indented.  The first line is properly indented based
    exclusively on depth * TABSIZE.  All following lines -- these are
    the reflowed lines generated by this function -- start at the same
    column as the first character beyond the opening { in the first
    line.
    """
    size = MAX_COL - depth * TABSIZE
    if len(s) < size:
        return [s]

    lines = []
    cur = s
    padding = ""
    while len(cur) > size:
        i = cur.rfind(' ', 0, size)
        # XXX this should be fixed for real
        if i == -1 and 'GeneratorExp' in cur:
            i = size + 3
43
        assert i != -1, "Impossible line %d to reflow: %r" % (size, s)
Jeremy Hylton's avatar
Jeremy Hylton committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        lines.append(padding + cur[:i])
        if len(lines) == 1:
            # find new size based on brace
            j = cur.find('{', 0, i)
            if j >= 0:
                j += 2 # account for the brace and the space after it
                size -= j
                padding = " " * j
            else:
                j = cur.find('(', 0, i)
                if j >= 0:
                    j += 1 # account for the paren (no space after it)
                    size -= j
                    padding = " " * j
        cur = cur[i+1:]
    else:
        lines.append(padding + cur)
    return lines

def is_simple(sum):
    """Return True if a sum is a simple.

    A sum is simple if its types have no fields, e.g.
    unaryop = Invert | Not | UAdd | USub
    """
    for t in sum.types:
        if t.fields:
            return False
    return True

74

Jeremy Hylton's avatar
Jeremy Hylton committed
75 76 77 78 79
class EmitVisitor(asdl.VisitorBase):
    """Visit that emits lines"""

    def __init__(self, file):
        self.file = file
80
        self.identifiers = set()
Jeremy Hylton's avatar
Jeremy Hylton committed
81 82
        super(EmitVisitor, self).__init__()

83 84 85 86
    def emit_identifier(self, name):
        name = str(name)
        if name in self.identifiers:
            return
87
        self.emit("_Py_IDENTIFIER(%s);" % name, 0)
88 89
        self.identifiers.add(name)

Benjamin Peterson's avatar
Benjamin Peterson committed
90
    def emit(self, s, depth, reflow=True):
Jeremy Hylton's avatar
Jeremy Hylton committed
91 92 93 94 95 96
        # XXX reflow long lines?
        if reflow:
            lines = reflow_lines(s, depth)
        else:
            lines = [s]
        for line in lines:
97 98 99
            if line:
                line = (" " * TABSIZE * depth) + line
            self.file.write(line + "\n")
Jeremy Hylton's avatar
Jeremy Hylton committed
100

101

Jeremy Hylton's avatar
Jeremy Hylton committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
class TypeDefVisitor(EmitVisitor):
    def visitModule(self, mod):
        for dfn in mod.dfns:
            self.visit(dfn)

    def visitType(self, type, depth=0):
        self.visit(type.value, type.name, depth)

    def visitSum(self, sum, name, depth):
        if is_simple(sum):
            self.simple_sum(sum, name, depth)
        else:
            self.sum_with_constructors(sum, name, depth)

    def simple_sum(self, sum, name, depth):
        enum = []
        for i in range(len(sum.types)):
            type = sum.types[i]
            enum.append("%s=%d" % (type.name, i + 1))
        enums = ", ".join(enum)
        ctype = get_c_type(name)
        s = "typedef enum _%s { %s } %s;" % (name, enums, ctype)
        self.emit(s, depth)
        self.emit("", depth)

    def sum_with_constructors(self, sum, name, depth):
        ctype = get_c_type(name)
        s = "typedef struct _%(name)s *%(ctype)s;" % locals()
        self.emit(s, depth)
        self.emit("", depth)

    def visitProduct(self, product, name, depth):
        ctype = get_c_type(name)
        s = "typedef struct _%(name)s *%(ctype)s;" % locals()
        self.emit(s, depth)
        self.emit("", depth)

139

Jeremy Hylton's avatar
Jeremy Hylton committed
140
class StructVisitor(EmitVisitor):
141
    """Visitor to generate typedefs for AST."""
Jeremy Hylton's avatar
Jeremy Hylton committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    def visitModule(self, mod):
        for dfn in mod.dfns:
            self.visit(dfn)

    def visitType(self, type, depth=0):
        self.visit(type.value, type.name, depth)

    def visitSum(self, sum, name, depth):
        if not is_simple(sum):
            self.sum_with_constructors(sum, name, depth)

    def sum_with_constructors(self, sum, name, depth):
        def emit(s, depth=depth):
            self.emit(s % sys._getframe(1).f_locals, depth)
        enum = []
        for i in range(len(sum.types)):
            type = sum.types[i]
            enum.append("%s_kind=%d" % (type.name, i + 1))

162 163
        emit("enum _%(name)s_kind {" + ", ".join(enum) + "};")

Jeremy Hylton's avatar
Jeremy Hylton committed
164
        emit("struct _%(name)s {")
165
        emit("enum _%(name)s_kind kind;", depth + 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        emit("union {", depth + 1)
        for t in sum.types:
            self.visit(t, depth + 2)
        emit("} v;", depth + 1)
        for field in sum.attributes:
            # rudimentary attribute handling
            type = str(field.type)
            assert type in asdl.builtin_types, type
            emit("%s %s;" % (type, field.name), depth + 1);
        emit("};")
        emit("")

    def visitConstructor(self, cons, depth):
        if cons.fields:
            self.emit("struct {", depth)
            for f in cons.fields:
                self.visit(f, depth + 1)
            self.emit("} %s;" % cons.name, depth)
            self.emit("", depth)

    def visitField(self, field, depth):
        # XXX need to lookup field.type, because it might be something
        # like a builtin...
        ctype = get_c_type(field.type)
        name = field.name
        if field.seq:
192
            if field.type == 'cmpop':
193 194 195
                self.emit("asdl_int_seq *%(name)s;" % locals(), depth)
            else:
                self.emit("asdl_seq *%(name)s;" % locals(), depth)
Jeremy Hylton's avatar
Jeremy Hylton committed
196 197 198 199 200 201 202
        else:
            self.emit("%(ctype)s %(name)s;" % locals(), depth)

    def visitProduct(self, product, name, depth):
        self.emit("struct _%(name)s {" % locals(), depth)
        for f in product.fields:
            self.visit(f, depth + 1)
203 204 205 206 207
        for field in product.attributes:
            # rudimentary attribute handling
            type = str(field.type)
            assert type in asdl.builtin_types, type
            self.emit("%s %s;" % (type, field.name), depth + 1);
Jeremy Hylton's avatar
Jeremy Hylton committed
208 209 210
        self.emit("};", depth)
        self.emit("", depth)

211

Jeremy Hylton's avatar
Jeremy Hylton committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
class PrototypeVisitor(EmitVisitor):
    """Generate function prototypes for the .h file"""

    def visitModule(self, mod):
        for dfn in mod.dfns:
            self.visit(dfn)

    def visitType(self, type):
        self.visit(type.value, type.name)

    def visitSum(self, sum, name):
        if is_simple(sum):
            pass # XXX
        else:
            for t in sum.types:
                self.visit(t, name, sum.attributes)

    def get_args(self, fields):
        """Return list of C argument into, one for each field.

        Argument info is 3-tuple of a C type, variable name, and flag
        that is true if type can be NULL.
        """
        args = []
        unnamed = {}
        for f in fields:
            if f.name is None:
                name = f.type
                c = unnamed[name] = unnamed.get(name, 0) + 1
                if c > 1:
                    name = "name%d" % (c - 1)
            else:
                name = f.name
            # XXX should extend get_c_type() to handle this
            if f.seq:
247
                if f.type == 'cmpop':
248 249 250
                    ctype = "asdl_int_seq *"
                else:
                    ctype = "asdl_seq *"
Jeremy Hylton's avatar
Jeremy Hylton committed
251 252 253 254 255 256 257 258 259 260 261
            else:
                ctype = get_c_type(f.type)
            args.append((ctype, name, f.opt or f.seq))
        return args

    def visitConstructor(self, cons, type, attrs):
        args = self.get_args(cons.fields)
        attrs = self.get_args(attrs)
        ctype = get_c_type(type)
        self.emit_function(cons.name, ctype, args, attrs)

Benjamin Peterson's avatar
Benjamin Peterson committed
262
    def emit_function(self, name, ctype, args, attrs, union=True):
Jeremy Hylton's avatar
Jeremy Hylton committed
263 264 265 266
        args = args + attrs
        if args:
            argstr = ", ".join(["%s %s" % (atype, aname)
                                for atype, aname, opt in args])
267
            argstr += ", PyArena *arena"
Jeremy Hylton's avatar
Jeremy Hylton committed
268
        else:
269
            argstr = "PyArena *arena"
270 271 272 273
        margs = "a0"
        for i in range(1, len(args)+1):
            margs += ", a%d" % i
        self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
Benjamin Peterson's avatar
Benjamin Peterson committed
274 275
                reflow=False)
        self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False)
Jeremy Hylton's avatar
Jeremy Hylton committed
276 277 278

    def visitProduct(self, prod, name):
        self.emit_function(name, get_c_type(name),
279 280 281
                           self.get_args(prod.fields),
                           self.get_args(prod.attributes),
                           union=False)
Jeremy Hylton's avatar
Jeremy Hylton committed
282

283

Jeremy Hylton's avatar
Jeremy Hylton committed
284 285 286
class FunctionVisitor(PrototypeVisitor):
    """Visitor to generate constructor functions for AST."""

Benjamin Peterson's avatar
Benjamin Peterson committed
287 288
    def emit_function(self, name, ctype, args, attrs, union=True):
        def emit(s, depth=0, reflow=True):
Jeremy Hylton's avatar
Jeremy Hylton committed
289 290 291
            self.emit(s, depth, reflow)
        argstr = ", ".join(["%s %s" % (atype, aname)
                            for atype, aname, opt in args + attrs])
292 293 294 295
        if argstr:
            argstr += ", PyArena *arena"
        else:
            argstr = "PyArena *arena"
Jeremy Hylton's avatar
Jeremy Hylton committed
296 297 298 299 300
        self.emit("%s" % ctype, 0)
        emit("%s(%s)" % (name, argstr))
        emit("{")
        emit("%s p;" % ctype, 1)
        for argtype, argname, opt in args:
301
            if not opt and argtype != "int":
Jeremy Hylton's avatar
Jeremy Hylton committed
302 303 304 305
                emit("if (!%s) {" % argname, 1)
                emit("PyErr_SetString(PyExc_ValueError,", 2)
                msg = "field %s is required for %s" % (argname, name)
                emit('                "%s");' % msg,
Benjamin Peterson's avatar
Benjamin Peterson committed
306
                     2, reflow=False)
Jeremy Hylton's avatar
Jeremy Hylton committed
307 308 309
                emit('return NULL;', 2)
                emit('}', 1)

310
        emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
311
        emit("if (!p)", 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
312 313 314 315 316 317 318 319 320 321
        emit("return NULL;", 2)
        if union:
            self.emit_body_union(name, args, attrs)
        else:
            self.emit_body_struct(name, args, attrs)
        emit("return p;", 1)
        emit("}")
        emit("")

    def emit_body_union(self, name, args, attrs):
Benjamin Peterson's avatar
Benjamin Peterson committed
322
        def emit(s, depth=0, reflow=True):
Jeremy Hylton's avatar
Jeremy Hylton committed
323 324 325 326 327 328 329 330
            self.emit(s, depth, reflow)
        emit("p->kind = %s_kind;" % name, 1)
        for argtype, argname, opt in args:
            emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
        for argtype, argname, opt in attrs:
            emit("p->%s = %s;" % (argname, argname), 1)

    def emit_body_struct(self, name, args, attrs):
Benjamin Peterson's avatar
Benjamin Peterson committed
331
        def emit(s, depth=0, reflow=True):
Jeremy Hylton's avatar
Jeremy Hylton committed
332 333 334
            self.emit(s, depth, reflow)
        for argtype, argname, opt in args:
            emit("p->%s = %s;" % (argname, argname), 1)
335 336
        for argtype, argname, opt in attrs:
            emit("p->%s = %s;" % (argname, argname), 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
337

338

Jeremy Hylton's avatar
Jeremy Hylton committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
class PickleVisitor(EmitVisitor):

    def visitModule(self, mod):
        for dfn in mod.dfns:
            self.visit(dfn)

    def visitType(self, type):
        self.visit(type.value, type.name)

    def visitSum(self, sum, name):
        pass

    def visitProduct(self, sum, name):
        pass

    def visitConstructor(self, cons, name):
        pass

    def visitField(self, sum):
        pass

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

class Obj2ModPrototypeVisitor(PickleVisitor):
    def visitProduct(self, prod, name):
        code = "static int obj2ast_%s(PyObject* obj, %s* out, PyArena* arena);"
        self.emit(code % (name, get_c_type(name)), 0)

    visitSum = visitProduct


class Obj2ModVisitor(PickleVisitor):
    def funcHeader(self, name):
        ctype = get_c_type(name)
        self.emit("int", 0)
        self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
        self.emit("{", 0)
Benjamin Peterson's avatar
Benjamin Peterson committed
375
        self.emit("int isinstance;", 1)
376 377
        self.emit("", 0)

378
    def sumTrailer(self, name, add_label=False):
379 380
        self.emit("", 0)
        # there's really nothing more we can do if this fails ...
381 382
        error = "expected some sort of %s, but got %%R" % name
        format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);"
383
        self.emit(format % error, 1, reflow=False)
384 385
        if add_label:
            self.emit("failed:", 1)
Benjamin Peterson's avatar
Benjamin Peterson committed
386
            self.emit("Py_XDECREF(tmp);", 1)
387 388 389 390 391 392 393
        self.emit("return 1;", 1)
        self.emit("}", 0)
        self.emit("", 0)

    def simpleSum(self, sum, name):
        self.funcHeader(name)
        for t in sum.types:
394 395 396 397 398 399 400
            line = ("isinstance = PyObject_IsInstance(obj, "
                    "(PyObject *)%s_type);")
            self.emit(line % (t.name,), 1)
            self.emit("if (isinstance == -1) {", 1)
            self.emit("return 1;", 2)
            self.emit("}", 1)
            self.emit("if (isinstance) {", 1)
401 402 403 404 405 406 407 408 409 410
            self.emit("*out = %s;" % t.name, 2)
            self.emit("return 0;", 2)
            self.emit("}", 1)
        self.sumTrailer(name)

    def buildArgs(self, fields):
        return ", ".join(fields + ["arena"])

    def complexSum(self, sum, name):
        self.funcHeader(name)
411
        self.emit("PyObject *tmp = NULL;", 1)
412 413 414 415 416 417 418 419 420 421 422
        for a in sum.attributes:
            self.visitAttributeDeclaration(a, name, sum=sum)
        self.emit("", 0)
        # XXX: should we only do this for 'expr'?
        self.emit("if (obj == Py_None) {", 1)
        self.emit("*out = NULL;", 2)
        self.emit("return 0;", 2)
        self.emit("}", 1)
        for a in sum.attributes:
            self.visitField(a, name, sum=sum, depth=1)
        for t in sum.types:
423 424 425 426 427 428
            line = "isinstance = PyObject_IsInstance(obj, (PyObject*)%s_type);"
            self.emit(line % (t.name,), 1)
            self.emit("if (isinstance == -1) {", 1)
            self.emit("return 1;", 2)
            self.emit("}", 1)
            self.emit("if (isinstance) {", 1)
429 430 431 432 433
            for f in t.fields:
                self.visitFieldDeclaration(f, t.name, sum=sum, depth=2)
            self.emit("", 0)
            for f in t.fields:
                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 436 437 438
            self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
            self.emit("if (*out == NULL) goto failed;", 2)
            self.emit("return 0;", 2)
            self.emit("}", 1)
439
        self.sumTrailer(name, True)
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

    def visitAttributeDeclaration(self, a, name, sum=sum):
        ctype = get_c_type(a.type)
        self.emit("%s %s;" % (ctype, a.name), 1)

    def visitSum(self, sum, name):
        if is_simple(sum):
            self.simpleSum(sum, name)
        else:
            self.complexSum(sum, name)

    def visitProduct(self, prod, name):
        ctype = get_c_type(name)
        self.emit("int", 0)
        self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
        self.emit("{", 0)
        self.emit("PyObject* tmp = NULL;", 1)
        for f in prod.fields:
            self.visitFieldDeclaration(f, name, prod=prod, depth=1)
459 460
        for a in prod.attributes:
            self.visitFieldDeclaration(a, name, prod=prod, depth=1)
461 462 463
        self.emit("", 0)
        for f in prod.fields:
            self.visitField(f, name, prod=prod, depth=1)
464 465
        for a in prod.attributes:
            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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
        self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1)
        self.emit("return 0;", 1)
        self.emit("failed:", 0)
        self.emit("Py_XDECREF(tmp);", 1)
        self.emit("return 1;", 1)
        self.emit("}", 0)
        self.emit("", 0)

    def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0):
        ctype = get_c_type(field.type)
        if field.seq:
            if self.isSimpleType(field):
                self.emit("asdl_int_seq* %s;" % field.name, depth)
            else:
                self.emit("asdl_seq* %s;" % field.name, depth)
        else:
            ctype = get_c_type(field.type)
            self.emit("%s %s;" % (ctype, field.name), depth)

    def isSimpleSum(self, field):
        # XXX can the members of this list be determined automatically?
489 490
        return field.type in ('expr_context', 'boolop', 'operator',
                              'unaryop', 'cmpop')
491 492 493 494 495 496 497 498 499

    def isNumeric(self, field):
        return get_c_type(field.type) in ("int", "bool")

    def isSimpleType(self, field):
        return self.isSimpleSum(field) or self.isNumeric(field)

    def visitField(self, field, name, sum=None, prod=None, depth=0):
        ctype = get_c_type(field.type)
500 501 502
        self.emit("if (_PyObject_LookupAttrId(obj, &PyId_%s, &tmp) < 0) {" % field.name, depth)
        self.emit("return 1;", depth+1)
        self.emit("}", depth)
503
        if not field.opt:
504 505 506 507 508
            self.emit("if (tmp == NULL) {", depth)
            message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
            format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
            self.emit(format % message, depth+1, reflow=False)
            self.emit("return 1;", depth+1)
509
        else:
510 511 512 513 514 515 516 517 518 519 520
            self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
            self.emit("Py_CLEAR(tmp);", depth+1)
            if self.isNumeric(field):
                self.emit("%s = 0;" % field.name, depth+1)
            elif not self.isSimpleType(field):
                self.emit("%s = NULL;" % field.name, depth+1)
            else:
                raise TypeError("could not determine the default value for %s" % field.name)
        self.emit("}", depth)
        self.emit("else {", depth)

521 522 523 524 525 526 527 528 529 530 531 532 533
        self.emit("int res;", depth+1)
        if field.seq:
            self.emit("Py_ssize_t len;", depth+1)
            self.emit("Py_ssize_t i;", depth+1)
            self.emit("if (!PyList_Check(tmp)) {", depth+1)
            self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
                      "be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
                      (name, field.name),
                      depth+2, reflow=False)
            self.emit("goto failed;", depth+2)
            self.emit("}", depth+1)
            self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
            if self.isSimpleType(field):
534
                self.emit("%s = _Py_asdl_int_seq_new(len, arena);" % field.name, depth+1)
535
            else:
536
                self.emit("%s = _Py_asdl_seq_new(len, arena);" % field.name, depth+1)
537 538
            self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
            self.emit("for (i = 0; i < len; i++) {", depth+1)
539 540
            self.emit("%s val;" % ctype, depth+2)
            self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &val, arena);" %
541 542
                      field.type, depth+2, reflow=False)
            self.emit("if (res != 0) goto failed;", depth+2)
543 544 545 546 547 548 549
            self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
            self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
                      "changed size during iteration\");" %
                      (name, field.name),
                      depth+3, reflow=False)
            self.emit("goto failed;", depth+3)
            self.emit("}", depth+2)
550
            self.emit("asdl_seq_SET(%s, i, val);" % field.name, depth+2)
551 552 553 554 555 556
            self.emit("}", depth+1)
        else:
            self.emit("res = obj2ast_%s(tmp, &%s, arena);" %
                      (field.type, field.name), depth+1)
            self.emit("if (res != 0) goto failed;", depth+1)

557
        self.emit("Py_CLEAR(tmp);", depth+1)
558 559 560
        self.emit("}", depth)


Jeremy Hylton's avatar
Jeremy Hylton committed
561 562 563 564
class MarshalPrototypeVisitor(PickleVisitor):

    def prototype(self, sum, name):
        ctype = get_c_type(name)
565
        self.emit("static int marshal_write_%s(PyObject **, int *, %s);"
Jeremy Hylton's avatar
Jeremy Hylton committed
566 567 568 569
                  % (name, ctype), 0)

    visitProduct = visitSum = prototype

570

571
class PyTypesDeclareVisitor(PickleVisitor):
Jeremy Hylton's avatar
Jeremy Hylton committed
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 577 578 579 580 581 582
        if prod.attributes:
            for a in prod.attributes:
                self.emit_identifier(a.name)
            self.emit("static char *%s_attributes[] = {" % name, 0)
            for a in prod.attributes:
                self.emit('"%s",' % a.name, 1)
            self.emit("};", 0)
583
        if prod.fields:
584 585
            for f in prod.fields:
                self.emit_identifier(f.name)
586
            self.emit("static char *%s_fields[]={" % name,0)
587 588 589
            for f in prod.fields:
                self.emit('"%s",' % f.name, 1)
            self.emit("};", 0)
Tim Peters's avatar
Tim Peters committed
590

591
    def visitSum(self, sum, name):
592
        self.emit("static PyTypeObject *%s_type;" % name, 0)
Martin v. Löwis's avatar
Martin v. Löwis committed
593
        if sum.attributes:
594 595
            for a in sum.attributes:
                self.emit_identifier(a.name)
596
            self.emit("static char *%s_attributes[] = {" % name, 0)
Martin v. Löwis's avatar
Martin v. Löwis committed
597 598 599
            for a in sum.attributes:
                self.emit('"%s",' % a.name, 1)
            self.emit("};", 0)
600 601 602 603 604 605 606 607 608 609 610
        ptype = "void*"
        if is_simple(sum):
            ptype = get_c_type(name)
            tnames = []
            for t in sum.types:
                tnames.append(str(t.name)+"_singleton")
            tnames = ", *".join(tnames)
            self.emit("static PyObject *%s;" % tnames, 0)
        self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0)
        for t in sum.types:
            self.visitConstructor(t, name)
Tim Peters's avatar
Tim Peters committed
611

612
    def visitConstructor(self, cons, name):
613
        self.emit("static PyTypeObject *%s_type;" % cons.name, 0)
614
        if cons.fields:
615 616
            for t in cons.fields:
                self.emit_identifier(t.name)
617
            self.emit("static char *%s_fields[]={" % cons.name, 0)
618 619 620
            for t in cons.fields:
                self.emit('"%s",' % t.name, 1)
            self.emit("};",0)
621 622 623 624 625

class PyTypesVisitor(PickleVisitor):

    def visitModule(self, mod):
        self.emit("""
626 627 628
_Py_IDENTIFIER(_fields);
_Py_IDENTIFIER(_attributes);

629
typedef struct {
630
    PyObject_HEAD
631 632 633
    PyObject *dict;
} AST_object;

Benjamin Peterson's avatar
Benjamin Peterson committed
634 635 636
static void
ast_dealloc(AST_object *self)
{
637 638
    /* bpo-31095: UnTrack is needed before calling any callbacks */
    PyObject_GC_UnTrack(self);
Benjamin Peterson's avatar
Benjamin Peterson committed
639
    Py_CLEAR(self->dict);
640
    Py_TYPE(self)->tp_free(self);
Benjamin Peterson's avatar
Benjamin Peterson committed
641 642
}

643 644 645 646 647 648 649
static int
ast_traverse(AST_object *self, visitproc visit, void *arg)
{
    Py_VISIT(self->dict);
    return 0;
}

650
static int
651 652 653
ast_clear(AST_object *self)
{
    Py_CLEAR(self->dict);
654
    return 0;
655 656
}

657 658 659 660 661 662
static int
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
{
    Py_ssize_t i, numfields = 0;
    int res = -1;
    PyObject *key, *value, *fields;
663 664 665
    if (_PyObject_LookupAttrId((PyObject*)Py_TYPE(self), &PyId__fields, &fields) < 0) {
        goto cleanup;
    }
666 667 668 669 670
    if (fields) {
        numfields = PySequence_Size(fields);
        if (numfields == -1)
            goto cleanup;
    }
671

672
    res = 0; /* if no error occurs, this stays 0 to the end */
673 674 675 676 677 678 679 680 681 682 683 684
    if (numfields < PyTuple_GET_SIZE(args)) {
        PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
                     "%zd positional argument%s",
                     Py_TYPE(self)->tp_name,
                     numfields, numfields == 1 ? "" : "s");
        res = -1;
        goto cleanup;
    }
    for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
        /* cannot be reached when fields is NULL */
        PyObject *name = PySequence_GetItem(fields, i);
        if (!name) {
685 686 687
            res = -1;
            goto cleanup;
        }
688 689 690 691
        res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
        Py_DECREF(name);
        if (res < 0)
            goto cleanup;
692 693 694 695 696 697 698 699 700 701 702 703 704 705
    }
    if (kw) {
        i = 0;  /* needed by PyDict_Next */
        while (PyDict_Next(kw, &i, &key, &value)) {
            res = PyObject_SetAttr(self, key, value);
            if (res < 0)
                goto cleanup;
        }
    }
  cleanup:
    Py_XDECREF(fields);
    return res;
}

Neal Norwitz's avatar
Neal Norwitz committed
706 707 708 709
/* Pickling support */
static PyObject *
ast_type_reduce(PyObject *self, PyObject *unused)
{
710
    _Py_IDENTIFIER(__dict__);
711 712 713
    PyObject *dict;
    if (_PyObject_LookupAttrId(self, &PyId___dict__, &dict) < 0) {
        return NULL;
Neal Norwitz's avatar
Neal Norwitz committed
714 715
    }
    if (dict) {
716
        return Py_BuildValue("O()N", Py_TYPE(self), dict);
Neal Norwitz's avatar
Neal Norwitz committed
717 718 719 720 721 722 723 724 725
    }
    return Py_BuildValue("O()", Py_TYPE(self));
}

static PyMethodDef ast_type_methods[] = {
    {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
    {NULL}
};

726 727 728 729 730
static PyGetSetDef ast_type_getsets[] = {
    {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
    {NULL}
};

731 732
static PyTypeObject AST_type = {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitz's avatar
Neal Norwitz committed
733
    "_ast.AST",
734
    sizeof(AST_object),
735
    0,
Benjamin Peterson's avatar
Benjamin Peterson committed
736
    (destructor)ast_dealloc, /* tp_dealloc */
737 738 739
    0,                       /* tp_print */
    0,                       /* tp_getattr */
    0,                       /* tp_setattr */
740
    0,                       /* tp_reserved */
741 742 743 744 745 746 747 748 749 750
    0,                       /* tp_repr */
    0,                       /* tp_as_number */
    0,                       /* tp_as_sequence */
    0,                       /* tp_as_mapping */
    0,                       /* tp_hash */
    0,                       /* tp_call */
    0,                       /* tp_str */
    PyObject_GenericGetAttr, /* tp_getattro */
    PyObject_GenericSetAttr, /* tp_setattro */
    0,                       /* tp_as_buffer */
751
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
752
    0,                       /* tp_doc */
753 754
    (traverseproc)ast_traverse, /* tp_traverse */
    (inquiry)ast_clear,      /* tp_clear */
755 756 757 758
    0,                       /* tp_richcompare */
    0,                       /* tp_weaklistoffset */
    0,                       /* tp_iter */
    0,                       /* tp_iternext */
Neal Norwitz's avatar
Neal Norwitz committed
759
    ast_type_methods,        /* tp_methods */
760
    0,                       /* tp_members */
761
    ast_type_getsets,        /* tp_getset */
762 763 764 765
    0,                       /* tp_base */
    0,                       /* tp_dict */
    0,                       /* tp_descr_get */
    0,                       /* tp_descr_set */
766
    offsetof(AST_object, dict),/* tp_dictoffset */
767 768 769
    (initproc)ast_type_init, /* tp_init */
    PyType_GenericAlloc,     /* tp_alloc */
    PyType_GenericNew,       /* tp_new */
770
    PyObject_GC_Del,         /* tp_free */
771 772 773
};


774 775
static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
{
776 777
    _Py_IDENTIFIER(__module__);
    _Py_IDENTIFIER(_ast);
778 779
    PyObject *fnames, *result;
    int i;
Neal Norwitz's avatar
Neal Norwitz committed
780 781 782
    fnames = PyTuple_New(num_fields);
    if (!fnames) return NULL;
    for (i = 0; i < num_fields; i++) {
783
        PyObject *field = PyUnicode_FromString(fields[i]);
784 785 786 787 788 789
        if (!field) {
            Py_DECREF(fnames);
            return NULL;
        }
        PyTuple_SET_ITEM(fnames, i, field);
    }
790 791 792 793 794
    result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
                    type, base,
                    _PyUnicode_FromId(&PyId__fields), fnames,
                    _PyUnicode_FromId(&PyId___module__),
                    _PyUnicode_FromId(&PyId__ast));
795 796 797 798
    Py_DECREF(fnames);
    return (PyTypeObject*)result;
}

Martin v. Löwis's avatar
Martin v. Löwis committed
799 800
static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
{
801
    int i, result;
802
    PyObject *s, *l = PyTuple_New(num_fields);
803 804 805
    if (!l)
        return 0;
    for (i = 0; i < num_fields; i++) {
806
        s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis's avatar
Martin v. Löwis committed
807 808 809 810
        if (!s) {
            Py_DECREF(l);
            return 0;
        }
811
        PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis's avatar
Martin v. Löwis committed
812
    }
813
    result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
814 815
    Py_DECREF(l);
    return result;
Martin v. Löwis's avatar
Martin v. Löwis committed
816 817
}

818 819
/* Conversion AST -> Python */

820 821
static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
{
822
    Py_ssize_t i, n = asdl_seq_LEN(seq);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    PyObject *result = PyList_New(n);
    PyObject *value;
    if (!result)
        return NULL;
    for (i = 0; i < n; i++) {
        value = func(asdl_seq_GET(seq, i));
        if (!value) {
            Py_DECREF(result);
            return NULL;
        }
        PyList_SET_ITEM(result, i, value);
    }
    return result;
}

static PyObject* ast2obj_object(void *o)
{
    if (!o)
        o = Py_None;
    Py_INCREF((PyObject*)o);
    return (PyObject*)o;
}
845
#define ast2obj_singleton ast2obj_object
Victor Stinner's avatar
Victor Stinner committed
846
#define ast2obj_constant ast2obj_object
847 848
#define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object
849
#define ast2obj_bytes ast2obj_object
Martin v. Löwis's avatar
Martin v. Löwis committed
850

851
static PyObject* ast2obj_int(long b)
Martin v. Löwis's avatar
Martin v. Löwis committed
852
{
853
    return PyLong_FromLong(b);
Martin v. Löwis's avatar
Martin v. Löwis committed
854
}
855 856 857 858 859 860 861

/* Conversion Python -> AST */

static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
{
    if (obj == Py_None)
        obj = NULL;
862 863 864 865 866 867 868
    if (obj) {
        if (PyArena_AddPyObject(arena, obj) < 0) {
            *out = NULL;
            return -1;
        }
        Py_INCREF(obj);
    }
869 870 871 872
    *out = obj;
    return 0;
}

Victor Stinner's avatar
Victor Stinner committed
873 874
static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
{
875 876 877
    if (PyArena_AddPyObject(arena, obj) < 0) {
        *out = NULL;
        return -1;
Victor Stinner's avatar
Victor Stinner committed
878
    }
879
    Py_INCREF(obj);
Victor Stinner's avatar
Victor Stinner committed
880 881 882 883
    *out = obj;
    return 0;
}

884
static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
885
{
886 887
    if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
        PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
888 889 890 891 892
        return 1;
    }
    return obj2ast_object(obj, out, arena);
}

893 894 895 896 897 898 899 900 901
static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
{
    if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
        PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
        return 1;
    }
    return obj2ast_object(obj, out, arena);
}

902 903 904 905
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
{
    int i;
    if (!PyLong_Check(obj)) {
906
        PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
907 908 909
        return 1;
    }

910
    i = _PyLong_AsInt(obj);
911 912 913 914 915 916
    if (i == -1 && PyErr_Occurred())
        return 1;
    *out = i;
    return 0;
}

Benjamin Peterson's avatar
Benjamin Peterson committed
917
static int add_ast_fields(void)
Benjamin Peterson's avatar
Benjamin Peterson committed
918 919 920 921 922 923 924
{
    PyObject *empty_tuple, *d;
    if (PyType_Ready(&AST_type) < 0)
        return -1;
    d = AST_type.tp_dict;
    empty_tuple = PyTuple_New(0);
    if (!empty_tuple ||
925 926
        _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
        _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
Benjamin Peterson's avatar
Benjamin Peterson committed
927 928 929 930 931 932 933
        Py_XDECREF(empty_tuple);
        return -1;
    }
    Py_DECREF(empty_tuple);
    return 0;
}

934 935 936 937
""", 0, reflow=False)

        self.emit("static int init_types(void)",0)
        self.emit("{", 0)
938
        self.emit("static int initialized;", 1)
939
        self.emit("if (initialized) return 1;", 1)
Benjamin Peterson's avatar
Benjamin Peterson committed
940
        self.emit("if (add_ast_fields() < 0) return 0;", 1)
941 942
        for dfn in mod.dfns:
            self.visit(dfn)
Martin v. Löwis's avatar
Martin v. Löwis committed
943 944
        self.emit("initialized = 1;", 1)
        self.emit("return 1;", 1);
945 946 947
        self.emit("}", 0)

    def visitProduct(self, prod, name):
948
        if prod.fields:
949
            fields = name+"_fields"
950 951
        else:
            fields = "NULL"
952
        self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' %
953
                        (name, name, fields, len(prod.fields)), 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
954
        self.emit("if (!%s_type) return 0;" % name, 1)
955 956 957 958 959
        if prod.attributes:
            self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
                            (name, name, len(prod.attributes)), 1)
        else:
            self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
Tim Peters's avatar
Tim Peters committed
960

961
    def visitSum(self, sum, name):
962 963
        self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' %
                  (name, name), 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
964 965
        self.emit("if (!%s_type) return 0;" % name, 1)
        if sum.attributes:
Tim Peters's avatar
Tim Peters committed
966
            self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
Martin v. Löwis's avatar
Martin v. Löwis committed
967 968 969
                            (name, name, len(sum.attributes)), 1)
        else:
            self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
970 971 972
        simple = is_simple(sum)
        for t in sum.types:
            self.visitConstructor(t, name, simple)
Tim Peters's avatar
Tim Peters committed
973

974
    def visitConstructor(self, cons, name, simple):
975
        if cons.fields:
976
            fields = cons.name+"_fields"
977 978
        else:
            fields = "NULL"
Tim Peters's avatar
Tim Peters committed
979
        self.emit('%s_type = make_type("%s", %s_type, %s, %d);' %
980
                            (cons.name, cons.name, name, fields, len(cons.fields)), 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
981
        self.emit("if (!%s_type) return 0;" % cons.name, 1)
982 983 984
        if simple:
            self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" %
                             (cons.name, cons.name), 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
985
            self.emit("if (!%s_singleton) return 0;" % cons.name, 1)
Tim Peters's avatar
Tim Peters committed
986

987

Martin v. Löwis's avatar
Martin v. Löwis committed
988 989 990
class ASTModuleVisitor(PickleVisitor):

    def visitModule(self, mod):
991 992 993
        self.emit("static struct PyModuleDef _astmodule = {", 0)
        self.emit('  PyModuleDef_HEAD_INIT, "_ast"', 0)
        self.emit("};", 0)
Martin v. Löwis's avatar
Martin v. Löwis committed
994
        self.emit("PyMODINIT_FUNC", 0)
995
        self.emit("PyInit__ast(void)", 0)
Martin v. Löwis's avatar
Martin v. Löwis committed
996 997
        self.emit("{", 0)
        self.emit("PyObject *m, *d;", 1)
998 999 1000
        self.emit("if (!init_types()) return NULL;", 1)
        self.emit('m = PyModule_Create(&_astmodule);', 1)
        self.emit("if (!m) return NULL;", 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
1001
        self.emit("d = PyModule_GetDict(m);", 1)
1002
        self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1)
1003
        self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)', 1)
1004
        self.emit("return NULL;", 2)
1005 1006
        self.emit('if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0)', 1)
        self.emit("return NULL;", 2)
Martin v. Löwis's avatar
Martin v. Löwis committed
1007 1008
        for dfn in mod.dfns:
            self.visit(dfn)
1009
        self.emit("return m;", 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
1010 1011 1012 1013
        self.emit("}", 0)

    def visitProduct(self, prod, name):
        self.addObj(name)
Tim Peters's avatar
Tim Peters committed
1014

Martin v. Löwis's avatar
Martin v. Löwis committed
1015 1016 1017 1018
    def visitSum(self, sum, name):
        self.addObj(name)
        for t in sum.types:
            self.visitConstructor(t, name)
Tim Peters's avatar
Tim Peters committed
1019

Martin v. Löwis's avatar
Martin v. Löwis committed
1020 1021
    def visitConstructor(self, cons, name):
        self.addObj(cons.name)
Tim Peters's avatar
Tim Peters committed
1022

Martin v. Löwis's avatar
Martin v. Löwis committed
1023
    def addObj(self, name):
1024
        self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return NULL;' % (name, name), 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
1025

1026

Jeremy Hylton's avatar
Jeremy Hylton committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
_SPECIALIZED_SEQUENCES = ('stmt', 'expr')

def find_sequence(fields, doing_specialization):
    """Return True if any field uses a sequence."""
    for f in fields:
        if f.seq:
            if not doing_specialization:
                return True
            if str(f.type) not in _SPECIALIZED_SEQUENCES:
                return True
    return False

def has_sequence(types, doing_specialization):
    for t in types:
        if find_sequence(t.fields, doing_specialization):
            return True
    return False


class StaticVisitor(PickleVisitor):
1047
    CODE = '''Very simple, always emit this static code.  Override CODE'''
1048 1049 1050 1051

    def visit(self, object):
        self.emit(self.CODE, 0, reflow=False)

1052

1053
class ObjVisitor(PickleVisitor):
Jeremy Hylton's avatar
Jeremy Hylton committed
1054

Martin v. Löwis's avatar
Martin v. Löwis committed
1055
    def func_begin(self, name):
Jeremy Hylton's avatar
Jeremy Hylton committed
1056
        ctype = get_c_type(name)
1057 1058
        self.emit("PyObject*", 0)
        self.emit("ast2obj_%s(void* _o)" % (name), 0)
Jeremy Hylton's avatar
Jeremy Hylton committed
1059
        self.emit("{", 0)
1060 1061 1062
        self.emit("%s o = (%s)_o;" % (ctype, ctype), 1)
        self.emit("PyObject *result = NULL, *value = NULL;", 1)
        self.emit('if (!o) {', 1)
1063
        self.emit("Py_RETURN_NONE;", 2)
1064
        self.emit("}", 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
1065 1066
        self.emit('', 0)

Martin v. Löwis's avatar
Martin v. Löwis committed
1067
    def func_end(self):
1068 1069 1070 1071 1072
        self.emit("return result;", 1)
        self.emit("failed:", 0)
        self.emit("Py_XDECREF(value);", 1)
        self.emit("Py_XDECREF(result);", 1)
        self.emit("return NULL;", 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
1073 1074 1075 1076
        self.emit("}", 0)
        self.emit("", 0)

    def visitSum(self, sum, name):
1077 1078 1079
        if is_simple(sum):
            self.simpleSum(sum, name)
            return
Martin v. Löwis's avatar
Martin v. Löwis committed
1080
        self.func_begin(name)
1081 1082 1083 1084 1085
        self.emit("switch (o->kind) {", 1)
        for i in range(len(sum.types)):
            t = sum.types[i]
            self.visitConstructor(t, i + 1, name)
        self.emit("}", 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
1086 1087 1088
        for a in sum.attributes:
            self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
            self.emit("if (!value) goto failed;", 1)
1089
            self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
1090 1091
            self.emit('goto failed;', 2)
            self.emit('Py_DECREF(value);', 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
1092
        self.func_end()
Tim Peters's avatar
Tim Peters committed
1093

1094 1095 1096 1097 1098 1099 1100 1101
    def simpleSum(self, sum, name):
        self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0)
        self.emit("{", 0)
        self.emit("switch(o) {", 1)
        for t in sum.types:
            self.emit("case %s:" % t.name, 2)
            self.emit("Py_INCREF(%s_singleton);" % t.name, 3)
            self.emit("return %s_singleton;" % t.name, 3)
1102
        self.emit("default:", 2)
1103 1104 1105 1106
        self.emit('/* should never happen, but just in case ... */', 3)
        code = "PyErr_Format(PyExc_SystemError, \"unknown %s found\");" % name
        self.emit(code, 3, reflow=False)
        self.emit("return NULL;", 3)
1107 1108
        self.emit("}", 1)
        self.emit("}", 0)
Jeremy Hylton's avatar
Jeremy Hylton committed
1109 1110

    def visitProduct(self, prod, name):
Martin v. Löwis's avatar
Martin v. Löwis committed
1111
        self.func_begin(name)
1112 1113
        self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1);
        self.emit("if (!result) return NULL;", 1)
Jeremy Hylton's avatar
Jeremy Hylton committed
1114 1115
        for field in prod.fields:
            self.visitField(field, name, 1, True)
1116 1117 1118 1119 1120 1121
        for a in prod.attributes:
            self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
            self.emit("if (!value) goto failed;", 1)
            self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
            self.emit('goto failed;', 2)
            self.emit('Py_DECREF(value);', 1)
Martin v. Löwis's avatar
Martin v. Löwis committed
1122
        self.func_end()
Tim Peters's avatar
Tim Peters committed
1123

Jeremy Hylton's avatar
Jeremy Hylton committed
1124 1125
    def visitConstructor(self, cons, enum, name):
        self.emit("case %s_kind:" % cons.name, 1)
1126 1127
        self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2);
        self.emit("if (!result) goto failed;", 2)
Jeremy Hylton's avatar
Jeremy Hylton committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
        for f in cons.fields:
            self.visitField(f, cons.name, 2, False)
        self.emit("break;", 2)

    def visitField(self, field, name, depth, product):
        def emit(s, d):
            self.emit(s, depth + d)
        if product:
            value = "o->%s" % field.name
        else:
            value = "o->v.%s.%s" % (name, field.name)
1139 1140
        self.set(field, value, depth)
        emit("if (!value) goto failed;", 0)
1141
        emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) == -1)' % field.name, 0)
1142 1143
        emit("goto failed;", 1)
        emit("Py_DECREF(value);", 0)
Jeremy Hylton's avatar
Jeremy Hylton committed
1144 1145

    def emitSeq(self, field, value, depth, emit):
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
        emit("seq = %s;" % value, 0)
        emit("n = asdl_seq_LEN(seq);", 0)
        emit("value = PyList_New(n);", 0)
        emit("if (!value) goto failed;", 0)
        emit("for (i = 0; i < n; i++) {", 0)
        self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1)
        emit("if (!value1) goto failed;", 1)
        emit("PyList_SET_ITEM(value, i, value1);", 1)
        emit("value1 = NULL;", 1)
        emit("}", 0)

    def set(self, field, value, depth):
        if field.seq:
1159
            # XXX should really check for is_simple, but that requires a symbol table
1160
            if field.type == "cmpop":
1161 1162 1163
                # While the sequence elements are stored as void*,
                # ast2obj_cmpop expects an enum
                self.emit("{", depth)
1164
                self.emit("Py_ssize_t i, n = asdl_seq_LEN(%s);" % value, depth+1)
1165 1166 1167 1168
                self.emit("value = PyList_New(n);", depth+1)
                self.emit("if (!value) goto failed;", depth+1)
                self.emit("for(i = 0; i < n; i++)", depth+1)
                # This cannot fail, so no need for error handling
1169 1170
                self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value,
                          depth+2, reflow=False)
1171
                self.emit("}", depth)
1172
            else:
1173
                self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth)
Jeremy Hylton's avatar
Jeremy Hylton committed
1174 1175
        else:
            ctype = get_c_type(field.type)
1176
            self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False)
Tim Peters's avatar
Tim Peters committed
1177

Jeremy Hylton's avatar
Jeremy Hylton committed
1178

1179 1180 1181 1182 1183
class PartingShots(StaticVisitor):

    CODE = """
PyObject* PyAST_mod2obj(mod_ty t)
{
1184 1185
    if (!init_types())
        return NULL;
1186 1187
    return ast2obj_mod(t);
}
1188

1189 1190
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
1191 1192 1193 1194 1195
{
    return PyAST_obj2mod_ex(ast, arena, mode, PY_MINOR_VERSION);
}

mod_ty PyAST_obj2mod_ex(PyObject* ast, PyArena* arena, int mode, int feature_version)
1196 1197
{
    mod_ty res;
1198
    PyObject *req_type[3];
1199
    char *req_name[] = {"Module", "Expression", "Interactive"};
1200
    int isinstance;
1201 1202 1203 1204 1205

    req_type[0] = (PyObject*)Module_type;
    req_type[1] = (PyObject*)Expression_type;
    req_type[2] = (PyObject*)Interactive_type;

1206
    assert(0 <= mode && mode <= 2);
1207

1208 1209
    if (!init_types())
        return NULL;
1210

1211 1212 1213 1214
    isinstance = PyObject_IsInstance(ast, req_type[mode]);
    if (isinstance == -1)
        return NULL;
    if (!isinstance) {
1215 1216
        PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
                     req_name[mode], Py_TYPE(ast)->tp_name);
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        return NULL;
    }
    if (obj2ast_mod(ast, &res, arena) != 0)
        return NULL;
    else
        return res;
}

int PyAST_Check(PyObject* obj)
{
1227 1228
    if (!init_types())
        return -1;
1229
    return PyObject_IsInstance(obj, (PyObject*)&AST_type);
1230
}
1231
"""
Jeremy Hylton's avatar
Jeremy Hylton committed
1232 1233 1234 1235 1236 1237 1238 1239

class ChainOfVisitors:
    def __init__(self, *visitors):
        self.visitors = visitors

    def visit(self, object):
        for v in self.visitors:
            v.visit(object)
1240
            v.emit("", 0)
Jeremy Hylton's avatar
Jeremy Hylton committed
1241

1242
common_msg = "/* File automatically generated by %s. */\n\n"
1243

1244
def main(srcfile, dump_module=False):
1245
    argv0 = sys.argv[0]
1246 1247
    components = argv0.split(os.sep)
    argv0 = os.sep.join(components[-2:])
1248
    auto_gen_msg = common_msg % argv0
Jeremy Hylton's avatar
Jeremy Hylton committed
1249
    mod = asdl.parse(srcfile)
1250 1251 1252
    if dump_module:
        print('Parsed Module:')
        print(mod)
Jeremy Hylton's avatar
Jeremy Hylton committed
1253 1254
    if not asdl.check(mod):
        sys.exit(1)
1255 1256 1257
    if H_FILE:
        with open(H_FILE, "w") as f:
            f.write(auto_gen_msg)
1258 1259 1260 1261 1262 1263 1264 1265
            f.write('#ifndef Py_PYTHON_AST_H\n')
            f.write('#define Py_PYTHON_AST_H\n')
            f.write('#ifdef __cplusplus\n')
            f.write('extern "C" {\n')
            f.write('#endif\n')
            f.write('\n')
            f.write('#include "asdl.h"\n')
            f.write('\n')
1266
            f.write('#undef Yield   /* undefine macro conflicting with <winbase.h> */\n')
1267
            f.write('\n')
1268
            c = ChainOfVisitors(TypeDefVisitor(f),
1269 1270
                                StructVisitor(f))

1271
            c.visit(mod)
1272 1273 1274
            f.write("// Note: these macros affect function definitions, not only call sites.\n")
            PrototypeVisitor(f).visit(mod)
            f.write("\n")
1275 1276
            f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
            f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
1277
            f.write("mod_ty PyAST_obj2mod_ex(PyObject* ast, PyArena* arena, int mode, int feature_version);\n")
1278
            f.write("int PyAST_Check(PyObject* obj);\n")
1279 1280 1281 1282 1283
            f.write('\n')
            f.write('#ifdef __cplusplus\n')
            f.write('}\n')
            f.write('#endif\n')
            f.write('#endif /* !Py_PYTHON_AST_H */\n')
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

    if C_FILE:
        with open(C_FILE, "w") as f:
            f.write(auto_gen_msg)
            f.write('#include <stddef.h>\n')
            f.write('\n')
            f.write('#include "Python.h"\n')
            f.write('#include "%s-ast.h"\n' % mod.name)
            f.write('\n')
            f.write("static PyTypeObject AST_type;\n")
            v = ChainOfVisitors(
                PyTypesDeclareVisitor(f),
                PyTypesVisitor(f),
                Obj2ModPrototypeVisitor(f),
                FunctionVisitor(f),
                ObjVisitor(f),
                Obj2ModVisitor(f),
                ASTModuleVisitor(f),
                PartingShots(f),
                )
            v.visit(mod)
Jeremy Hylton's avatar
Jeremy Hylton committed
1305 1306 1307 1308

if __name__ == "__main__":
    import getopt

1309 1310
    H_FILE = ''
    C_FILE = ''
1311 1312
    dump_module = False
    opts, args = getopt.getopt(sys.argv[1:], "dh:c:")
Jeremy Hylton's avatar
Jeremy Hylton committed
1313 1314
    for o, v in opts:
        if o == '-h':
1315
            H_FILE = v
1316
        elif o == '-c':
1317
            C_FILE = v
1318
        elif o == '-d':
1319
            dump_module = True
1320
    if H_FILE and C_FILE:
1321 1322 1323 1324
        print('Must specify exactly one output file')
        sys.exit(1)
    elif len(args) != 1:
        print('Must specify single input file')
1325
        sys.exit(1)
1326
    main(args[0], dump_module)