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) _Py_%s(%s)" % (name, margs, name, margs), 0, 274 reflow=False) 275 self.emit("%s _Py_%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 (_PyObject_LookupAttrId(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 = _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 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 658ast_type_init(PyObject *self, PyObject *args, PyObject *kw) 659{ 660 Py_ssize_t i, numfields = 0; 661 int res = -1; 662 PyObject *key, *value, *fields; 663 if (_PyObject_LookupAttrId((PyObject*)Py_TYPE(self), &PyId__fields, &fields) < 0) { 664 goto cleanup; 665 } 666 if (fields) { 667 numfields = PySequence_Size(fields); 668 if (numfields == -1) 669 goto cleanup; 670 } 671 672 res = 0; /* if no error occurs, this stays 0 to the end */ 673 if (numfields < PyTuple_GET_SIZE(args)) { 674 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most " 675 "%zd positional argument%s", 676 Py_TYPE(self)->tp_name, 677 numfields, numfields == 1 ? "" : "s"); 678 res = -1; 679 goto cleanup; 680 } 681 for (i = 0; i < PyTuple_GET_SIZE(args); i++) { 682 /* cannot be reached when fields is NULL */ 683 PyObject *name = PySequence_GetItem(fields, i); 684 if (!name) { 685 res = -1; 686 goto cleanup; 687 } 688 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i)); 689 Py_DECREF(name); 690 if (res < 0) 691 goto cleanup; 692 } 693 if (kw) { 694 i = 0; /* needed by PyDict_Next */ 695 while (PyDict_Next(kw, &i, &key, &value)) { 696 res = PyObject_SetAttr(self, key, value); 697 if (res < 0) 698 goto cleanup; 699 } 700 } 701 cleanup: 702 Py_XDECREF(fields); 703 return res; 704} 705 706/* Pickling support */ 707static PyObject * 708ast_type_reduce(PyObject *self, PyObject *unused) 709{ 710 _Py_IDENTIFIER(__dict__); 711 PyObject *dict; 712 if (_PyObject_LookupAttrId(self, &PyId___dict__, &dict) < 0) { 713 return NULL; 714 } 715 if (dict) { 716 return Py_BuildValue("O()N", Py_TYPE(self), dict); 717 } 718 return Py_BuildValue("O()", Py_TYPE(self)); 719} 720 721static PyMethodDef ast_type_methods[] = { 722 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL}, 723 {NULL} 724}; 725 726static PyGetSetDef ast_type_getsets[] = { 727 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, 728 {NULL} 729}; 730 731static PyTypeObject AST_type = { 732 PyVarObject_HEAD_INIT(&PyType_Type, 0) 733 "_ast.AST", 734 sizeof(AST_object), 735 0, 736 (destructor)ast_dealloc, /* tp_dealloc */ 737 0, /* tp_print */ 738 0, /* tp_getattr */ 739 0, /* tp_setattr */ 740 0, /* tp_reserved */ 741 0, /* tp_repr */ 742 0, /* tp_as_number */ 743 0, /* tp_as_sequence */ 744 0, /* tp_as_mapping */ 745 0, /* tp_hash */ 746 0, /* tp_call */ 747 0, /* tp_str */ 748 PyObject_GenericGetAttr, /* tp_getattro */ 749 PyObject_GenericSetAttr, /* tp_setattro */ 750 0, /* tp_as_buffer */ 751 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 752 0, /* tp_doc */ 753 (traverseproc)ast_traverse, /* tp_traverse */ 754 (inquiry)ast_clear, /* tp_clear */ 755 0, /* tp_richcompare */ 756 0, /* tp_weaklistoffset */ 757 0, /* tp_iter */ 758 0, /* tp_iternext */ 759 ast_type_methods, /* tp_methods */ 760 0, /* tp_members */ 761 ast_type_getsets, /* tp_getset */ 762 0, /* tp_base */ 763 0, /* tp_dict */ 764 0, /* tp_descr_get */ 765 0, /* tp_descr_set */ 766 offsetof(AST_object, dict),/* tp_dictoffset */ 767 (initproc)ast_type_init, /* tp_init */ 768 PyType_GenericAlloc, /* tp_alloc */ 769 PyType_GenericNew, /* tp_new */ 770 PyObject_GC_Del, /* tp_free */ 771}; 772 773 774static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) 775{ 776 _Py_IDENTIFIER(__module__); 777 _Py_IDENTIFIER(_ast); 778 PyObject *fnames, *result; 779 int i; 780 fnames = PyTuple_New(num_fields); 781 if (!fnames) return NULL; 782 for (i = 0; i < num_fields; i++) { 783 PyObject *field = PyUnicode_FromString(fields[i]); 784 if (!field) { 785 Py_DECREF(fnames); 786 return NULL; 787 } 788 PyTuple_SET_ITEM(fnames, i, field); 789 } 790 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}", 791 type, base, 792 _PyUnicode_FromId(&PyId__fields), fnames, 793 _PyUnicode_FromId(&PyId___module__), 794 _PyUnicode_FromId(&PyId__ast)); 795 Py_DECREF(fnames); 796 return (PyTypeObject*)result; 797} 798 799static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) 800{ 801 int i, result; 802 PyObject *s, *l = PyTuple_New(num_fields); 803 if (!l) 804 return 0; 805 for (i = 0; i < num_fields; i++) { 806 s = PyUnicode_FromString(attrs[i]); 807 if (!s) { 808 Py_DECREF(l); 809 return 0; 810 } 811 PyTuple_SET_ITEM(l, i, s); 812 } 813 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0; 814 Py_DECREF(l); 815 return result; 816} 817 818/* Conversion AST -> Python */ 819 820static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) 821{ 822 Py_ssize_t i, n = asdl_seq_LEN(seq); 823 PyObject *result = PyList_New(n); 824 PyObject *value; 825 if (!result) 826 return NULL; 827 for (i = 0; i < n; i++) { 828 value = func(asdl_seq_GET(seq, i)); 829 if (!value) { 830 Py_DECREF(result); 831 return NULL; 832 } 833 PyList_SET_ITEM(result, i, value); 834 } 835 return result; 836} 837 838static PyObject* ast2obj_object(void *o) 839{ 840 if (!o) 841 o = Py_None; 842 Py_INCREF((PyObject*)o); 843 return (PyObject*)o; 844} 845#define ast2obj_singleton ast2obj_object 846#define ast2obj_constant ast2obj_object 847#define ast2obj_identifier ast2obj_object 848#define ast2obj_string ast2obj_object 849#define ast2obj_bytes ast2obj_object 850 851static PyObject* ast2obj_int(long b) 852{ 853 return PyLong_FromLong(b); 854} 855 856/* Conversion Python -> AST */ 857 858static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena) 859{ 860 if (obj != Py_None && obj != Py_True && obj != Py_False) { 861 PyErr_SetString(PyExc_ValueError, 862 "AST singleton must be True, False, or None"); 863 return 1; 864 } 865 *out = obj; 866 return 0; 867} 868 869static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) 870{ 871 if (obj == Py_None) 872 obj = NULL; 873 if (obj) { 874 if (PyArena_AddPyObject(arena, obj) < 0) { 875 *out = NULL; 876 return -1; 877 } 878 Py_INCREF(obj); 879 } 880 *out = obj; 881 return 0; 882} 883 884static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena) 885{ 886 if (obj) { 887 if (PyArena_AddPyObject(arena, obj) < 0) { 888 *out = NULL; 889 return -1; 890 } 891 Py_INCREF(obj); 892 } 893 *out = obj; 894 return 0; 895} 896 897static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena) 898{ 899 if (!PyUnicode_CheckExact(obj) && obj != Py_None) { 900 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str"); 901 return 1; 902 } 903 return obj2ast_object(obj, out, arena); 904} 905 906static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) 907{ 908 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) { 909 PyErr_SetString(PyExc_TypeError, "AST string must be of type str"); 910 return 1; 911 } 912 return obj2ast_object(obj, out, arena); 913} 914 915static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena) 916{ 917 if (!PyBytes_CheckExact(obj)) { 918 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes"); 919 return 1; 920 } 921 return obj2ast_object(obj, out, arena); 922} 923 924static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) 925{ 926 int i; 927 if (!PyLong_Check(obj)) { 928 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj); 929 return 1; 930 } 931 932 i = _PyLong_AsInt(obj); 933 if (i == -1 && PyErr_Occurred()) 934 return 1; 935 *out = i; 936 return 0; 937} 938 939static int add_ast_fields(void) 940{ 941 PyObject *empty_tuple, *d; 942 if (PyType_Ready(&AST_type) < 0) 943 return -1; 944 d = AST_type.tp_dict; 945 empty_tuple = PyTuple_New(0); 946 if (!empty_tuple || 947 _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 || 948 _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) { 949 Py_XDECREF(empty_tuple); 950 return -1; 951 } 952 Py_DECREF(empty_tuple); 953 return 0; 954} 955 956""", 0, reflow=False) 957 958 self.emit("static int init_types(void)",0) 959 self.emit("{", 0) 960 self.emit("static int initialized;", 1) 961 self.emit("if (initialized) return 1;", 1) 962 self.emit("if (add_ast_fields() < 0) return 0;", 1) 963 for dfn in mod.dfns: 964 self.visit(dfn) 965 self.emit("initialized = 1;", 1) 966 self.emit("return 1;", 1); 967 self.emit("}", 0) 968 969 def visitProduct(self, prod, name): 970 if prod.fields: 971 fields = name+"_fields" 972 else: 973 fields = "NULL" 974 self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' % 975 (name, name, fields, len(prod.fields)), 1) 976 self.emit("if (!%s_type) return 0;" % name, 1) 977 if prod.attributes: 978 self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" % 979 (name, name, len(prod.attributes)), 1) 980 else: 981 self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1) 982 983 def visitSum(self, sum, name): 984 self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' % 985 (name, name), 1) 986 self.emit("if (!%s_type) return 0;" % name, 1) 987 if sum.attributes: 988 self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" % 989 (name, name, len(sum.attributes)), 1) 990 else: 991 self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1) 992 simple = is_simple(sum) 993 for t in sum.types: 994 self.visitConstructor(t, name, simple) 995 996 def visitConstructor(self, cons, name, simple): 997 if cons.fields: 998 fields = cons.name+"_fields" 999 else: 1000 fields = "NULL" 1001 self.emit('%s_type = make_type("%s", %s_type, %s, %d);' % 1002 (cons.name, cons.name, name, fields, len(cons.fields)), 1) 1003 self.emit("if (!%s_type) return 0;" % cons.name, 1) 1004 if simple: 1005 self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" % 1006 (cons.name, cons.name), 1) 1007 self.emit("if (!%s_singleton) return 0;" % cons.name, 1) 1008 1009 1010class ASTModuleVisitor(PickleVisitor): 1011 1012 def visitModule(self, mod): 1013 self.emit("static struct PyModuleDef _astmodule = {", 0) 1014 self.emit(' PyModuleDef_HEAD_INIT, "_ast"', 0) 1015 self.emit("};", 0) 1016 self.emit("PyMODINIT_FUNC", 0) 1017 self.emit("PyInit__ast(void)", 0) 1018 self.emit("{", 0) 1019 self.emit("PyObject *m, *d;", 1) 1020 self.emit("if (!init_types()) return NULL;", 1) 1021 self.emit('m = PyModule_Create(&_astmodule);', 1) 1022 self.emit("if (!m) return NULL;", 1) 1023 self.emit("d = PyModule_GetDict(m);", 1) 1024 self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1) 1025 self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)', 1) 1026 self.emit("return NULL;", 2) 1027 for dfn in mod.dfns: 1028 self.visit(dfn) 1029 self.emit("return m;", 1) 1030 self.emit("}", 0) 1031 1032 def visitProduct(self, prod, name): 1033 self.addObj(name) 1034 1035 def visitSum(self, sum, name): 1036 self.addObj(name) 1037 for t in sum.types: 1038 self.visitConstructor(t, name) 1039 1040 def visitConstructor(self, cons, name): 1041 self.addObj(cons.name) 1042 1043 def addObj(self, name): 1044 self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return NULL;' % (name, name), 1) 1045 1046 1047_SPECIALIZED_SEQUENCES = ('stmt', 'expr') 1048 1049def find_sequence(fields, doing_specialization): 1050 """Return True if any field uses a sequence.""" 1051 for f in fields: 1052 if f.seq: 1053 if not doing_specialization: 1054 return True 1055 if str(f.type) not in _SPECIALIZED_SEQUENCES: 1056 return True 1057 return False 1058 1059def has_sequence(types, doing_specialization): 1060 for t in types: 1061 if find_sequence(t.fields, doing_specialization): 1062 return True 1063 return False 1064 1065 1066class StaticVisitor(PickleVisitor): 1067 CODE = '''Very simple, always emit this static code. Override CODE''' 1068 1069 def visit(self, object): 1070 self.emit(self.CODE, 0, reflow=False) 1071 1072 1073class ObjVisitor(PickleVisitor): 1074 1075 def func_begin(self, name): 1076 ctype = get_c_type(name) 1077 self.emit("PyObject*", 0) 1078 self.emit("ast2obj_%s(void* _o)" % (name), 0) 1079 self.emit("{", 0) 1080 self.emit("%s o = (%s)_o;" % (ctype, ctype), 1) 1081 self.emit("PyObject *result = NULL, *value = NULL;", 1) 1082 self.emit('if (!o) {', 1) 1083 self.emit("Py_RETURN_NONE;", 2) 1084 self.emit("}", 1) 1085 self.emit('', 0) 1086 1087 def func_end(self): 1088 self.emit("return result;", 1) 1089 self.emit("failed:", 0) 1090 self.emit("Py_XDECREF(value);", 1) 1091 self.emit("Py_XDECREF(result);", 1) 1092 self.emit("return NULL;", 1) 1093 self.emit("}", 0) 1094 self.emit("", 0) 1095 1096 def visitSum(self, sum, name): 1097 if is_simple(sum): 1098 self.simpleSum(sum, name) 1099 return 1100 self.func_begin(name) 1101 self.emit("switch (o->kind) {", 1) 1102 for i in range(len(sum.types)): 1103 t = sum.types[i] 1104 self.visitConstructor(t, i + 1, name) 1105 self.emit("}", 1) 1106 for a in sum.attributes: 1107 self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1) 1108 self.emit("if (!value) goto failed;", 1) 1109 self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1) 1110 self.emit('goto failed;', 2) 1111 self.emit('Py_DECREF(value);', 1) 1112 self.func_end() 1113 1114 def simpleSum(self, sum, name): 1115 self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0) 1116 self.emit("{", 0) 1117 self.emit("switch(o) {", 1) 1118 for t in sum.types: 1119 self.emit("case %s:" % t.name, 2) 1120 self.emit("Py_INCREF(%s_singleton);" % t.name, 3) 1121 self.emit("return %s_singleton;" % t.name, 3) 1122 self.emit("default:", 2) 1123 self.emit('/* should never happen, but just in case ... */', 3) 1124 code = "PyErr_Format(PyExc_SystemError, \"unknown %s found\");" % name 1125 self.emit(code, 3, reflow=False) 1126 self.emit("return NULL;", 3) 1127 self.emit("}", 1) 1128 self.emit("}", 0) 1129 1130 def visitProduct(self, prod, name): 1131 self.func_begin(name) 1132 self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1); 1133 self.emit("if (!result) return NULL;", 1) 1134 for field in prod.fields: 1135 self.visitField(field, name, 1, True) 1136 for a in prod.attributes: 1137 self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1) 1138 self.emit("if (!value) goto failed;", 1) 1139 self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1) 1140 self.emit('goto failed;', 2) 1141 self.emit('Py_DECREF(value);', 1) 1142 self.func_end() 1143 1144 def visitConstructor(self, cons, enum, name): 1145 self.emit("case %s_kind:" % cons.name, 1) 1146 self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2); 1147 self.emit("if (!result) goto failed;", 2) 1148 for f in cons.fields: 1149 self.visitField(f, cons.name, 2, False) 1150 self.emit("break;", 2) 1151 1152 def visitField(self, field, name, depth, product): 1153 def emit(s, d): 1154 self.emit(s, depth + d) 1155 if product: 1156 value = "o->%s" % field.name 1157 else: 1158 value = "o->v.%s.%s" % (name, field.name) 1159 self.set(field, value, depth) 1160 emit("if (!value) goto failed;", 0) 1161 emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) == -1)' % field.name, 0) 1162 emit("goto failed;", 1) 1163 emit("Py_DECREF(value);", 0) 1164 1165 def emitSeq(self, field, value, depth, emit): 1166 emit("seq = %s;" % value, 0) 1167 emit("n = asdl_seq_LEN(seq);", 0) 1168 emit("value = PyList_New(n);", 0) 1169 emit("if (!value) goto failed;", 0) 1170 emit("for (i = 0; i < n; i++) {", 0) 1171 self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1) 1172 emit("if (!value1) goto failed;", 1) 1173 emit("PyList_SET_ITEM(value, i, value1);", 1) 1174 emit("value1 = NULL;", 1) 1175 emit("}", 0) 1176 1177 def set(self, field, value, depth): 1178 if field.seq: 1179 # XXX should really check for is_simple, but that requires a symbol table 1180 if field.type == "cmpop": 1181 # While the sequence elements are stored as void*, 1182 # ast2obj_cmpop expects an enum 1183 self.emit("{", depth) 1184 self.emit("Py_ssize_t i, n = asdl_seq_LEN(%s);" % value, depth+1) 1185 self.emit("value = PyList_New(n);", depth+1) 1186 self.emit("if (!value) goto failed;", depth+1) 1187 self.emit("for(i = 0; i < n; i++)", depth+1) 1188 # This cannot fail, so no need for error handling 1189 self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value, 1190 depth+2, reflow=False) 1191 self.emit("}", depth) 1192 else: 1193 self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth) 1194 else: 1195 ctype = get_c_type(field.type) 1196 self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False) 1197 1198 1199class PartingShots(StaticVisitor): 1200 1201 CODE = """ 1202PyObject* PyAST_mod2obj(mod_ty t) 1203{ 1204 if (!init_types()) 1205 return NULL; 1206 return ast2obj_mod(t); 1207} 1208 1209/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */ 1210mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) 1211{ 1212 PyObject *req_type[3]; 1213 char *req_name[] = {"Module", "Expression", "Interactive"}; 1214 int isinstance; 1215 1216 req_type[0] = (PyObject*)Module_type; 1217 req_type[1] = (PyObject*)Expression_type; 1218 req_type[2] = (PyObject*)Interactive_type; 1219 1220 assert(0 <= mode && mode <= 2); 1221 1222 if (!init_types()) 1223 return NULL; 1224 1225 isinstance = PyObject_IsInstance(ast, req_type[mode]); 1226 if (isinstance == -1) 1227 return NULL; 1228 if (!isinstance) { 1229 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s", 1230 req_name[mode], Py_TYPE(ast)->tp_name); 1231 return NULL; 1232 } 1233 1234 mod_ty res = NULL; 1235 if (obj2ast_mod(ast, &res, arena) != 0) 1236 return NULL; 1237 else 1238 return res; 1239} 1240 1241int PyAST_Check(PyObject* obj) 1242{ 1243 if (!init_types()) 1244 return -1; 1245 return PyObject_IsInstance(obj, (PyObject*)&AST_type); 1246} 1247""" 1248 1249class ChainOfVisitors: 1250 def __init__(self, *visitors): 1251 self.visitors = visitors 1252 1253 def visit(self, object): 1254 for v in self.visitors: 1255 v.visit(object) 1256 v.emit("", 0) 1257 1258common_msg = "/* File automatically generated by %s. */\n\n" 1259 1260def main(srcfile, dump_module=False): 1261 argv0 = sys.argv[0] 1262 components = argv0.split(os.sep) 1263 argv0 = os.sep.join(components[-2:]) 1264 auto_gen_msg = common_msg % argv0 1265 mod = asdl.parse(srcfile) 1266 if dump_module: 1267 print('Parsed Module:') 1268 print(mod) 1269 if not asdl.check(mod): 1270 sys.exit(1) 1271 if H_FILE: 1272 with open(H_FILE, "w") as f: 1273 f.write(auto_gen_msg) 1274 f.write('#include "asdl.h"\n\n') 1275 c = ChainOfVisitors(TypeDefVisitor(f), 1276 StructVisitor(f), 1277 PrototypeVisitor(f), 1278 ) 1279 c.visit(mod) 1280 f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") 1281 f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") 1282 f.write("int PyAST_Check(PyObject* obj);\n") 1283 1284 if C_FILE: 1285 with open(C_FILE, "w") as f: 1286 f.write(auto_gen_msg) 1287 f.write('#include <stddef.h>\n') 1288 f.write('\n') 1289 f.write('#include "Python.h"\n') 1290 f.write('#include "%s-ast.h"\n' % mod.name) 1291 f.write('\n') 1292 f.write("static PyTypeObject AST_type;\n") 1293 v = ChainOfVisitors( 1294 PyTypesDeclareVisitor(f), 1295 PyTypesVisitor(f), 1296 Obj2ModPrototypeVisitor(f), 1297 FunctionVisitor(f), 1298 ObjVisitor(f), 1299 Obj2ModVisitor(f), 1300 ASTModuleVisitor(f), 1301 PartingShots(f), 1302 ) 1303 v.visit(mod) 1304 1305if __name__ == "__main__": 1306 import getopt 1307 1308 H_FILE = '' 1309 C_FILE = '' 1310 dump_module = False 1311 opts, args = getopt.getopt(sys.argv[1:], "dh:c:") 1312 for o, v in opts: 1313 if o == '-h': 1314 H_FILE = v 1315 if o == '-c': 1316 C_FILE = v 1317 if o == '-d': 1318 dump_module = True 1319 if H_FILE and C_FILE: 1320 print('Must specify exactly one output file') 1321 sys.exit(1) 1322 elif len(args) != 1: 1323 print('Must specify single input file') 1324 sys.exit(1) 1325 main(args[0], dump_module) 1326