Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Generate C code from an ASDL description.""" |
| 3 | |
| 4 | # TO DO |
| 5 | # handle fields that have a type but no name |
| 6 | |
| 7 | import os, sys, traceback |
| 8 | |
| 9 | import asdl |
| 10 | |
| 11 | TABSIZE = 8 |
| 12 | MAX_COL = 80 |
| 13 | |
| 14 | def get_c_type(name): |
| 15 | """Return a string for the C name of the type. |
| 16 | |
| 17 | This function special cases the default types provided by asdl: |
| 18 | identifier, string, int, bool. |
| 19 | """ |
| 20 | # XXX ack! need to figure out where Id is useful and where string |
| 21 | if isinstance(name, asdl.Id): |
| 22 | name = name.value |
| 23 | if name in asdl.builtin_types: |
| 24 | return name |
| 25 | else: |
| 26 | return "%s_ty" % name |
| 27 | |
| 28 | def reflow_lines(s, depth): |
| 29 | """Reflow the line s indented depth tabs. |
| 30 | |
| 31 | Return a sequence of lines where no line extends beyond MAX_COL |
| 32 | when properly indented. The first line is properly indented based |
| 33 | exclusively on depth * TABSIZE. All following lines -- these are |
| 34 | the reflowed lines generated by this function -- start at the same |
| 35 | column as the first character beyond the opening { in the first |
| 36 | line. |
| 37 | """ |
| 38 | size = MAX_COL - depth * TABSIZE |
| 39 | if len(s) < size: |
| 40 | return [s] |
| 41 | |
| 42 | lines = [] |
| 43 | cur = s |
| 44 | padding = "" |
| 45 | while len(cur) > size: |
| 46 | i = cur.rfind(' ', 0, size) |
| 47 | # XXX this should be fixed for real |
| 48 | if i == -1 and 'GeneratorExp' in cur: |
| 49 | i = size + 3 |
| 50 | assert i != -1, "Impossible line %d to reflow: %s" % (size, `s`) |
| 51 | lines.append(padding + cur[:i]) |
| 52 | if len(lines) == 1: |
| 53 | # find new size based on brace |
| 54 | j = cur.find('{', 0, i) |
| 55 | if j >= 0: |
| 56 | j += 2 # account for the brace and the space after it |
| 57 | size -= j |
| 58 | padding = " " * j |
| 59 | else: |
| 60 | j = cur.find('(', 0, i) |
| 61 | if j >= 0: |
| 62 | j += 1 # account for the paren (no space after it) |
| 63 | size -= j |
| 64 | padding = " " * j |
| 65 | cur = cur[i+1:] |
| 66 | else: |
| 67 | lines.append(padding + cur) |
| 68 | return lines |
| 69 | |
| 70 | def is_simple(sum): |
| 71 | """Return True if a sum is a simple. |
| 72 | |
| 73 | A sum is simple if its types have no fields, e.g. |
| 74 | unaryop = Invert | Not | UAdd | USub |
| 75 | """ |
| 76 | |
| 77 | for t in sum.types: |
| 78 | if t.fields: |
| 79 | return False |
| 80 | return True |
| 81 | |
| 82 | class EmitVisitor(asdl.VisitorBase): |
| 83 | """Visit that emits lines""" |
| 84 | |
| 85 | def __init__(self, file): |
| 86 | self.file = file |
| 87 | super(EmitVisitor, self).__init__() |
| 88 | |
| 89 | def emit(self, s, depth, reflow=1): |
| 90 | # XXX reflow long lines? |
| 91 | if reflow: |
| 92 | lines = reflow_lines(s, depth) |
| 93 | else: |
| 94 | lines = [s] |
| 95 | for line in lines: |
| 96 | line = (" " * TABSIZE * depth) + line + "\n" |
| 97 | self.file.write(line) |
| 98 | |
| 99 | class TypeDefVisitor(EmitVisitor): |
| 100 | def visitModule(self, mod): |
| 101 | for dfn in mod.dfns: |
| 102 | self.visit(dfn) |
| 103 | |
| 104 | def visitType(self, type, depth=0): |
| 105 | self.visit(type.value, type.name, depth) |
| 106 | |
| 107 | def visitSum(self, sum, name, depth): |
| 108 | if is_simple(sum): |
| 109 | self.simple_sum(sum, name, depth) |
| 110 | else: |
| 111 | self.sum_with_constructors(sum, name, depth) |
| 112 | |
| 113 | def simple_sum(self, sum, name, depth): |
| 114 | enum = [] |
| 115 | for i in range(len(sum.types)): |
| 116 | type = sum.types[i] |
| 117 | enum.append("%s=%d" % (type.name, i + 1)) |
| 118 | enums = ", ".join(enum) |
| 119 | ctype = get_c_type(name) |
| 120 | s = "typedef enum _%s { %s } %s;" % (name, enums, ctype) |
| 121 | self.emit(s, depth) |
| 122 | self.emit("", depth) |
| 123 | |
| 124 | def sum_with_constructors(self, sum, name, depth): |
| 125 | ctype = get_c_type(name) |
| 126 | s = "typedef struct _%(name)s *%(ctype)s;" % locals() |
| 127 | self.emit(s, depth) |
| 128 | self.emit("", depth) |
| 129 | |
| 130 | def visitProduct(self, product, name, depth): |
| 131 | ctype = get_c_type(name) |
| 132 | s = "typedef struct _%(name)s *%(ctype)s;" % locals() |
| 133 | self.emit(s, depth) |
| 134 | self.emit("", depth) |
| 135 | |
| 136 | class StructVisitor(EmitVisitor): |
| 137 | """Visitor to generate typdefs for AST.""" |
| 138 | |
| 139 | def visitModule(self, mod): |
| 140 | for dfn in mod.dfns: |
| 141 | self.visit(dfn) |
| 142 | |
| 143 | def visitType(self, type, depth=0): |
| 144 | self.visit(type.value, type.name, depth) |
| 145 | |
| 146 | def visitSum(self, sum, name, depth): |
| 147 | if not is_simple(sum): |
| 148 | self.sum_with_constructors(sum, name, depth) |
| 149 | |
| 150 | def sum_with_constructors(self, sum, name, depth): |
| 151 | def emit(s, depth=depth): |
| 152 | self.emit(s % sys._getframe(1).f_locals, depth) |
| 153 | enum = [] |
| 154 | for i in range(len(sum.types)): |
| 155 | type = sum.types[i] |
| 156 | enum.append("%s_kind=%d" % (type.name, i + 1)) |
| 157 | |
| 158 | emit("struct _%(name)s {") |
| 159 | emit("enum { " + ", ".join(enum) + " } kind;", depth + 1) |
| 160 | emit("union {", depth + 1) |
| 161 | for t in sum.types: |
| 162 | self.visit(t, depth + 2) |
| 163 | emit("} v;", depth + 1) |
| 164 | for field in sum.attributes: |
| 165 | # rudimentary attribute handling |
| 166 | type = str(field.type) |
| 167 | assert type in asdl.builtin_types, type |
| 168 | emit("%s %s;" % (type, field.name), depth + 1); |
| 169 | emit("};") |
| 170 | emit("") |
| 171 | |
| 172 | def visitConstructor(self, cons, depth): |
| 173 | if cons.fields: |
| 174 | self.emit("struct {", depth) |
| 175 | for f in cons.fields: |
| 176 | self.visit(f, depth + 1) |
| 177 | self.emit("} %s;" % cons.name, depth) |
| 178 | self.emit("", depth) |
| 179 | else: |
| 180 | # XXX not sure what I want here, nothing is probably fine |
| 181 | pass |
| 182 | |
| 183 | def visitField(self, field, depth): |
| 184 | # XXX need to lookup field.type, because it might be something |
| 185 | # like a builtin... |
| 186 | ctype = get_c_type(field.type) |
| 187 | name = field.name |
| 188 | if field.seq: |
| 189 | self.emit("asdl_seq *%(name)s;" % locals(), depth) |
| 190 | else: |
| 191 | self.emit("%(ctype)s %(name)s;" % locals(), depth) |
| 192 | |
| 193 | def visitProduct(self, product, name, depth): |
| 194 | self.emit("struct _%(name)s {" % locals(), depth) |
| 195 | for f in product.fields: |
| 196 | self.visit(f, depth + 1) |
| 197 | self.emit("};", depth) |
| 198 | self.emit("", depth) |
| 199 | |
| 200 | class PrototypeVisitor(EmitVisitor): |
| 201 | """Generate function prototypes for the .h file""" |
| 202 | |
| 203 | def visitModule(self, mod): |
| 204 | for dfn in mod.dfns: |
| 205 | self.visit(dfn) |
| 206 | |
| 207 | def visitType(self, type): |
| 208 | self.visit(type.value, type.name) |
| 209 | |
| 210 | def visitSum(self, sum, name): |
| 211 | if is_simple(sum): |
| 212 | pass # XXX |
| 213 | else: |
| 214 | for t in sum.types: |
| 215 | self.visit(t, name, sum.attributes) |
| 216 | |
| 217 | def get_args(self, fields): |
| 218 | """Return list of C argument into, one for each field. |
| 219 | |
| 220 | Argument info is 3-tuple of a C type, variable name, and flag |
| 221 | that is true if type can be NULL. |
| 222 | """ |
| 223 | args = [] |
| 224 | unnamed = {} |
| 225 | for f in fields: |
| 226 | if f.name is None: |
| 227 | name = f.type |
| 228 | c = unnamed[name] = unnamed.get(name, 0) + 1 |
| 229 | if c > 1: |
| 230 | name = "name%d" % (c - 1) |
| 231 | else: |
| 232 | name = f.name |
| 233 | # XXX should extend get_c_type() to handle this |
| 234 | if f.seq: |
| 235 | ctype = "asdl_seq *" |
| 236 | else: |
| 237 | ctype = get_c_type(f.type) |
| 238 | args.append((ctype, name, f.opt or f.seq)) |
| 239 | return args |
| 240 | |
| 241 | def visitConstructor(self, cons, type, attrs): |
| 242 | args = self.get_args(cons.fields) |
| 243 | attrs = self.get_args(attrs) |
| 244 | ctype = get_c_type(type) |
| 245 | self.emit_function(cons.name, ctype, args, attrs) |
| 246 | |
| 247 | def emit_function(self, name, ctype, args, attrs, union=1): |
| 248 | args = args + attrs |
| 249 | if args: |
| 250 | argstr = ", ".join(["%s %s" % (atype, aname) |
| 251 | for atype, aname, opt in args]) |
| 252 | else: |
| 253 | argstr = "void" |
| 254 | self.emit("%s %s(%s);" % (ctype, name, argstr), 0) |
| 255 | |
| 256 | def visitProduct(self, prod, name): |
| 257 | self.emit_function(name, get_c_type(name), |
| 258 | self.get_args(prod.fields), [], union=0) |
| 259 | |
| 260 | class FunctionVisitor(PrototypeVisitor): |
| 261 | """Visitor to generate constructor functions for AST.""" |
| 262 | |
| 263 | def emit_function(self, name, ctype, args, attrs, union=1): |
| 264 | def emit(s, depth=0, reflow=1): |
| 265 | self.emit(s, depth, reflow) |
| 266 | argstr = ", ".join(["%s %s" % (atype, aname) |
| 267 | for atype, aname, opt in args + attrs]) |
| 268 | self.emit("%s" % ctype, 0) |
| 269 | emit("%s(%s)" % (name, argstr)) |
| 270 | emit("{") |
| 271 | emit("%s p;" % ctype, 1) |
| 272 | for argtype, argname, opt in args: |
| 273 | # XXX hack alert: false is allowed for a bool |
| 274 | if not opt and not argtype == "bool": |
| 275 | emit("if (!%s) {" % argname, 1) |
| 276 | emit("PyErr_SetString(PyExc_ValueError,", 2) |
| 277 | msg = "field %s is required for %s" % (argname, name) |
| 278 | emit(' "%s");' % msg, |
| 279 | 2, reflow=0) |
| 280 | emit('return NULL;', 2) |
| 281 | emit('}', 1) |
| 282 | |
| 283 | emit("p = (%s)malloc(sizeof(*p));" % ctype, 1) |
| 284 | emit("if (!p) {", 1) |
Neal Norwitz | a34584b | 2005-10-23 18:59:17 +0000 | [diff] [blame] | 285 | emit("PyErr_NoMemory();", 2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 286 | emit("return NULL;", 2) |
| 287 | emit("}", 1) |
| 288 | if union: |
| 289 | self.emit_body_union(name, args, attrs) |
| 290 | else: |
| 291 | self.emit_body_struct(name, args, attrs) |
| 292 | emit("return p;", 1) |
| 293 | emit("}") |
| 294 | emit("") |
| 295 | |
| 296 | def emit_body_union(self, name, args, attrs): |
| 297 | def emit(s, depth=0, reflow=1): |
| 298 | self.emit(s, depth, reflow) |
| 299 | emit("p->kind = %s_kind;" % name, 1) |
| 300 | for argtype, argname, opt in args: |
| 301 | emit("p->v.%s.%s = %s;" % (name, argname, argname), 1) |
| 302 | for argtype, argname, opt in attrs: |
| 303 | emit("p->%s = %s;" % (argname, argname), 1) |
| 304 | |
| 305 | def emit_body_struct(self, name, args, attrs): |
| 306 | def emit(s, depth=0, reflow=1): |
| 307 | self.emit(s, depth, reflow) |
| 308 | for argtype, argname, opt in args: |
| 309 | emit("p->%s = %s;" % (argname, argname), 1) |
| 310 | assert not attrs |
| 311 | |
| 312 | class PickleVisitor(EmitVisitor): |
| 313 | |
| 314 | def visitModule(self, mod): |
| 315 | for dfn in mod.dfns: |
| 316 | self.visit(dfn) |
| 317 | |
| 318 | def visitType(self, type): |
| 319 | self.visit(type.value, type.name) |
| 320 | |
| 321 | def visitSum(self, sum, name): |
| 322 | pass |
| 323 | |
| 324 | def visitProduct(self, sum, name): |
| 325 | pass |
| 326 | |
| 327 | def visitConstructor(self, cons, name): |
| 328 | pass |
| 329 | |
| 330 | def visitField(self, sum): |
| 331 | pass |
| 332 | |
| 333 | class MarshalPrototypeVisitor(PickleVisitor): |
| 334 | |
| 335 | def prototype(self, sum, name): |
| 336 | ctype = get_c_type(name) |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 337 | self.emit("static int marshal_write_%s(PyObject **, int *, %s);" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 338 | % (name, ctype), 0) |
| 339 | |
| 340 | visitProduct = visitSum = prototype |
| 341 | |
| 342 | class FreePrototypeVisitor(PickleVisitor): |
| 343 | |
| 344 | def prototype(self, sum, name): |
| 345 | ctype = get_c_type(name) |
| 346 | self.emit("void free_%s(%s);" % (name, ctype), 0) |
| 347 | |
| 348 | visitProduct = visitSum = prototype |
| 349 | |
| 350 | _SPECIALIZED_SEQUENCES = ('stmt', 'expr') |
| 351 | |
| 352 | def find_sequence(fields, doing_specialization): |
| 353 | """Return True if any field uses a sequence.""" |
| 354 | for f in fields: |
| 355 | if f.seq: |
| 356 | if not doing_specialization: |
| 357 | return True |
| 358 | if str(f.type) not in _SPECIALIZED_SEQUENCES: |
| 359 | return True |
| 360 | return False |
| 361 | |
| 362 | def has_sequence(types, doing_specialization): |
| 363 | for t in types: |
| 364 | if find_sequence(t.fields, doing_specialization): |
| 365 | return True |
| 366 | return False |
| 367 | |
| 368 | |
| 369 | class StaticVisitor(PickleVisitor): |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 370 | CODE = '''Very simple, always emit this static code. Overide CODE''' |
| 371 | |
| 372 | def visit(self, object): |
| 373 | self.emit(self.CODE, 0, reflow=False) |
| 374 | |
| 375 | class FreeUtilVisitor(StaticVisitor): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 376 | |
| 377 | CODE = '''static void |
| 378 | free_seq_exprs(asdl_seq *seq) |
| 379 | { |
| 380 | int i, n; |
| 381 | n = asdl_seq_LEN(seq); |
| 382 | for (i = 0; i < n; i++) |
| 383 | free_expr((expr_ty)asdl_seq_GET(seq, i)); |
| 384 | asdl_seq_free(seq); |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | free_seq_stmts(asdl_seq *seq) |
| 389 | { |
| 390 | int i, n; |
| 391 | n = asdl_seq_LEN(seq); |
| 392 | for (i = 0; i < n; i++) |
| 393 | free_stmt((stmt_ty)asdl_seq_GET(seq, i)); |
| 394 | asdl_seq_free(seq); |
| 395 | } |
| 396 | ''' |
| 397 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 398 | class FreeVisitor(PickleVisitor): |
| 399 | |
| 400 | def func_begin(self, name, has_seq): |
| 401 | ctype = get_c_type(name) |
| 402 | self.emit("void", 0) |
| 403 | self.emit("free_%s(%s o)" % (name, ctype), 0) |
| 404 | self.emit("{", 0) |
| 405 | if has_seq: |
| 406 | self.emit("int i, n;", 1) |
| 407 | self.emit("asdl_seq *seq;", 1) |
| 408 | self.emit('', 0) |
| 409 | self.emit('if (!o)', 1) |
| 410 | self.emit('return;', 2) |
| 411 | self.emit('', 0) |
| 412 | |
| 413 | def func_end(self): |
| 414 | self.emit("}", 0) |
| 415 | self.emit("", 0) |
| 416 | |
| 417 | def visitSum(self, sum, name): |
| 418 | has_seq = has_sequence(sum.types, True) |
| 419 | self.func_begin(name, has_seq) |
| 420 | if not is_simple(sum): |
| 421 | self.emit("switch (o->kind) {", 1) |
| 422 | for i in range(len(sum.types)): |
| 423 | t = sum.types[i] |
| 424 | self.visitConstructor(t, i + 1, name) |
| 425 | self.emit("}", 1) |
| 426 | self.emit("", 0) |
| 427 | self.emit("free(o);", 1) |
| 428 | self.func_end() |
| 429 | |
| 430 | def visitProduct(self, prod, name): |
| 431 | self.func_begin(name, find_sequence(prod.fields, True)) |
| 432 | for field in prod.fields: |
| 433 | self.visitField(field, name, 1, True) |
| 434 | self.emit("", 0) |
| 435 | self.emit("free(o);", 1) |
| 436 | self.func_end() |
| 437 | |
| 438 | def visitConstructor(self, cons, enum, name): |
| 439 | self.emit("case %s_kind:" % cons.name, 1) |
| 440 | for f in cons.fields: |
| 441 | self.visitField(f, cons.name, 2, False) |
| 442 | self.emit("break;", 2) |
| 443 | |
| 444 | def visitField(self, field, name, depth, product): |
| 445 | def emit(s, d): |
| 446 | self.emit(s, depth + d) |
| 447 | if product: |
| 448 | value = "o->%s" % field.name |
| 449 | else: |
| 450 | value = "o->v.%s.%s" % (name, field.name) |
| 451 | if field.seq: |
| 452 | self.emitSeq(field, value, depth, emit) |
| 453 | |
| 454 | # XXX need to know the simple types in advance, so that we |
| 455 | # don't call free_TYPE() for them. |
| 456 | |
| 457 | elif field.opt: |
| 458 | emit("if (%s) {" % value, 0) |
| 459 | self.free(field, value, depth + 1) |
| 460 | emit("}", 0) |
| 461 | else: |
| 462 | self.free(field, value, depth) |
| 463 | |
| 464 | def emitSeq(self, field, value, depth, emit): |
| 465 | # specialize for freeing sequences of statements and expressions |
| 466 | if str(field.type) in _SPECIALIZED_SEQUENCES: |
| 467 | c_code = "free_seq_%ss(%s);" % (field.type, value) |
| 468 | emit(c_code, 0) |
| 469 | else: |
| 470 | emit("seq = %s;" % value, 0) |
| 471 | emit("n = asdl_seq_LEN(seq);", 0) |
| 472 | emit("for (i = 0; i < n; i++)", 0) |
| 473 | self.free(field, "asdl_seq_GET(seq, i)", depth + 1) |
| 474 | emit("asdl_seq_free(seq);", 0) |
| 475 | |
| 476 | def free(self, field, value, depth): |
| 477 | if str(field.type) in ("identifier", "string", "object"): |
| 478 | ctype = get_c_type(field.type) |
| 479 | self.emit("Py_DECREF((%s)%s);" % (ctype, value), depth) |
| 480 | elif str(field.type) == "bool": |
| 481 | return |
| 482 | else: |
| 483 | ctype = get_c_type(field.type) |
| 484 | self.emit("free_%s((%s)%s);" % (field.type, ctype, value), depth) |
| 485 | |
| 486 | |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 487 | class MarshalUtilVisitor(StaticVisitor): |
| 488 | |
| 489 | CODE = ''' |
| 490 | #define CHECKSIZE(BUF, OFF, MIN) { \\ |
| 491 | int need = *(OFF) + MIN; \\ |
| 492 | if (need >= PyString_GET_SIZE(*(BUF))) { \\ |
| 493 | int newsize = PyString_GET_SIZE(*(BUF)) * 2; \\ |
| 494 | if (newsize < need) \\ |
| 495 | newsize = need; \\ |
| 496 | if (_PyString_Resize((BUF), newsize) < 0) \\ |
| 497 | return 0; \\ |
| 498 | } \\ |
| 499 | } |
| 500 | |
| 501 | static int |
| 502 | marshal_write_int(PyObject **buf, int *offset, int x) |
| 503 | { |
| 504 | char *s; |
| 505 | |
| 506 | CHECKSIZE(buf, offset, 4) |
| 507 | s = PyString_AS_STRING(*buf) + (*offset); |
| 508 | s[0] = (x & 0xff); |
| 509 | s[1] = (x >> 8) & 0xff; |
| 510 | s[2] = (x >> 16) & 0xff; |
| 511 | s[3] = (x >> 24) & 0xff; |
| 512 | *offset += 4; |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | static int |
| 517 | marshal_write_bool(PyObject **buf, int *offset, bool b) |
| 518 | { |
| 519 | if (b) |
| 520 | marshal_write_int(buf, offset, 1); |
| 521 | else |
| 522 | marshal_write_int(buf, offset, 0); |
| 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | static int |
| 527 | marshal_write_identifier(PyObject **buf, int *offset, identifier id) |
| 528 | { |
| 529 | int l = PyString_GET_SIZE(id); |
| 530 | marshal_write_int(buf, offset, l); |
| 531 | CHECKSIZE(buf, offset, l); |
| 532 | memcpy(PyString_AS_STRING(*buf) + *offset, |
| 533 | PyString_AS_STRING(id), l); |
| 534 | *offset += l; |
| 535 | return 1; |
| 536 | } |
| 537 | |
| 538 | static int |
| 539 | marshal_write_string(PyObject **buf, int *offset, string s) |
| 540 | { |
| 541 | int len = PyString_GET_SIZE(s); |
| 542 | marshal_write_int(buf, offset, len); |
| 543 | CHECKSIZE(buf, offset, len); |
| 544 | memcpy(PyString_AS_STRING(*buf) + *offset, |
| 545 | PyString_AS_STRING(s), len); |
| 546 | *offset += len; |
| 547 | return 1; |
| 548 | } |
| 549 | |
| 550 | static int |
| 551 | marshal_write_object(PyObject **buf, int *offset, object s) |
| 552 | { |
| 553 | /* XXX */ |
| 554 | return 0; |
| 555 | } |
| 556 | ''' |
| 557 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 558 | class MarshalFunctionVisitor(PickleVisitor): |
| 559 | |
| 560 | def func_begin(self, name, has_seq): |
| 561 | ctype = get_c_type(name) |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 562 | self.emit("static int", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | self.emit("marshal_write_%s(PyObject **buf, int *off, %s o)" % |
| 564 | (name, ctype), 0) |
| 565 | self.emit("{", 0) |
Neal Norwitz | a34584b | 2005-10-23 18:59:17 +0000 | [diff] [blame] | 566 | if has_seq: |
| 567 | self.emit("int i;", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 568 | |
| 569 | def func_end(self): |
| 570 | self.emit("return 1;", 1) |
| 571 | self.emit("}", 0) |
| 572 | self.emit("", 0) |
| 573 | |
| 574 | def visitSum(self, sum, name): |
Neal Norwitz | a34584b | 2005-10-23 18:59:17 +0000 | [diff] [blame] | 575 | self.func_begin(name, has_sequence(sum.types, False)) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 576 | simple = is_simple(sum) |
| 577 | if simple: |
| 578 | self.emit("switch (o) {", 1) |
| 579 | else: |
| 580 | self.emit("switch (o->kind) {", 1) |
| 581 | for i in range(len(sum.types)): |
| 582 | t = sum.types[i] |
| 583 | self.visitConstructor(t, i + 1, name, simple) |
| 584 | self.emit("}", 1) |
| 585 | self.func_end() |
| 586 | |
| 587 | def visitProduct(self, prod, name): |
Neal Norwitz | a34584b | 2005-10-23 18:59:17 +0000 | [diff] [blame] | 588 | self.func_begin(name, find_sequence(prod.fields, False)) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 589 | for field in prod.fields: |
| 590 | self.visitField(field, name, 1, 1) |
| 591 | self.func_end() |
| 592 | |
| 593 | def visitConstructor(self, cons, enum, name, simple): |
| 594 | if simple: |
| 595 | self.emit("case %s:" % cons.name, 1) |
| 596 | self.emit("marshal_write_int(buf, off, %d);" % enum, 2); |
| 597 | self.emit("break;", 2) |
| 598 | else: |
| 599 | self.emit("case %s_kind:" % cons.name, 1) |
| 600 | self.emit("marshal_write_int(buf, off, %d);" % enum, 2) |
| 601 | for f in cons.fields: |
| 602 | self.visitField(f, cons.name, 2, 0) |
| 603 | self.emit("break;", 2) |
| 604 | |
| 605 | def visitField(self, field, name, depth, product): |
| 606 | def emit(s, d): |
| 607 | self.emit(s, depth + d) |
| 608 | if product: |
| 609 | value = "o->%s" % field.name |
| 610 | else: |
| 611 | value = "o->v.%s.%s" % (name, field.name) |
| 612 | if field.seq: |
| 613 | emit("marshal_write_int(buf, off, asdl_seq_LEN(%s));" % value, 0) |
| 614 | emit("for (i = 0; i < asdl_seq_LEN(%s); i++) {" % value, 0) |
| 615 | emit("void *elt = asdl_seq_GET(%s, i);" % value, 1); |
| 616 | ctype = get_c_type(field.type); |
| 617 | emit("marshal_write_%s(buf, off, (%s)elt);" % (field.type, |
| 618 | ctype), 1) |
| 619 | emit("}", 0) |
| 620 | elif field.opt: |
| 621 | emit("if (%s) {" % value, 0) |
| 622 | emit("marshal_write_int(buf, off, 1);", 1) |
| 623 | emit("marshal_write_%s(buf, off, %s);" % (field.type, value), 1) |
| 624 | emit("}", 0) |
| 625 | emit("else {", 0) |
| 626 | emit("marshal_write_int(buf, off, 0);", 1) |
| 627 | emit("}", 0) |
| 628 | else: |
| 629 | emit("marshal_write_%s(buf, off, %s);" % (field.type, value), 0) |
| 630 | |
| 631 | class ChainOfVisitors: |
| 632 | def __init__(self, *visitors): |
| 633 | self.visitors = visitors |
| 634 | |
| 635 | def visit(self, object): |
| 636 | for v in self.visitors: |
| 637 | v.visit(object) |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 638 | v.emit("", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 639 | |
| 640 | def main(srcfile): |
Neal Norwitz | 897ff81 | 2005-12-11 21:18:22 +0000 | [diff] [blame] | 641 | argv0 = sys.argv[0] |
Armin Rigo | 6b1793f | 2005-12-14 18:05:14 +0000 | [diff] [blame] | 642 | components = argv0.split(os.sep) |
| 643 | argv0 = os.sep.join(components[-2:]) |
Neal Norwitz | 897ff81 | 2005-12-11 21:18:22 +0000 | [diff] [blame] | 644 | auto_gen_msg = '/* File automatically generated by %s */\n' % argv0 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | mod = asdl.parse(srcfile) |
| 646 | if not asdl.check(mod): |
| 647 | sys.exit(1) |
| 648 | if INC_DIR: |
| 649 | p = "%s/%s-ast.h" % (INC_DIR, mod.name) |
| 650 | else: |
| 651 | p = "%s-ast.h" % mod.name |
| 652 | f = open(p, "wb") |
| 653 | print >> f, auto_gen_msg |
| 654 | print >> f, '#include "asdl.h"\n' |
| 655 | c = ChainOfVisitors(TypeDefVisitor(f), |
| 656 | StructVisitor(f), |
| 657 | PrototypeVisitor(f), |
| 658 | FreePrototypeVisitor(f), |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 | ) |
| 660 | c.visit(mod) |
| 661 | f.close() |
| 662 | |
| 663 | if SRC_DIR: |
| 664 | p = "%s/%s-ast.c" % (SRC_DIR, mod.name) |
| 665 | else: |
| 666 | p = "%s-ast.c" % mod.name |
| 667 | f = open(p, "wb") |
| 668 | print >> f, auto_gen_msg |
| 669 | print >> f, '#include "Python.h"' |
| 670 | print >> f, '#include "%s-ast.h"' % mod.name |
| 671 | print >> f |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 672 | v = ChainOfVisitors(MarshalPrototypeVisitor(f), |
| 673 | FunctionVisitor(f), |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 674 | FreeUtilVisitor(f), |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 675 | FreeVisitor(f), |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 676 | MarshalUtilVisitor(f), |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 677 | MarshalFunctionVisitor(f), |
| 678 | ) |
| 679 | v.visit(mod) |
| 680 | f.close() |
| 681 | |
| 682 | if __name__ == "__main__": |
| 683 | import sys |
| 684 | import getopt |
| 685 | |
| 686 | INC_DIR = '' |
| 687 | SRC_DIR = '' |
| 688 | opts, args = getopt.getopt(sys.argv[1:], "h:c:") |
| 689 | for o, v in opts: |
| 690 | if o == '-h': |
| 691 | INC_DIR = v |
| 692 | if o == '-c': |
| 693 | SRC_DIR = v |
| 694 | if len(args) != 1: |
| 695 | print "Must specify single input file" |
| 696 | main(args[0]) |