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