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 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 7 | import os, sys |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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: |
Neal Norwitz | 3591bbe | 2007-02-26 19:04:49 +0000 | [diff] [blame] | 18 | identifier, string, int. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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 |
Brett Cannon | f036551 | 2006-08-25 04:06:31 +0000 | [diff] [blame] | 50 | assert i != -1, "Impossible line %d to reflow: %r" % (size, s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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 | """ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 76 | for t in sum.types: |
| 77 | if t.fields: |
| 78 | return False |
| 79 | return True |
| 80 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 81 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 89 | def emit(self, s, depth, reflow=True): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 99 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 100 | class TypeDefVisitor(EmitVisitor): |
| 101 | def visitModule(self, mod): |
| 102 | for dfn in mod.dfns: |
| 103 | self.visit(dfn) |
| 104 | |
| 105 | def visitType(self, type, depth=0): |
| 106 | self.visit(type.value, type.name, depth) |
| 107 | |
| 108 | def visitSum(self, sum, name, depth): |
| 109 | if is_simple(sum): |
| 110 | self.simple_sum(sum, name, depth) |
| 111 | else: |
| 112 | self.sum_with_constructors(sum, name, depth) |
| 113 | |
| 114 | def simple_sum(self, sum, name, depth): |
| 115 | enum = [] |
| 116 | for i in range(len(sum.types)): |
| 117 | type = sum.types[i] |
| 118 | enum.append("%s=%d" % (type.name, i + 1)) |
| 119 | enums = ", ".join(enum) |
| 120 | ctype = get_c_type(name) |
| 121 | s = "typedef enum _%s { %s } %s;" % (name, enums, ctype) |
| 122 | self.emit(s, depth) |
| 123 | self.emit("", depth) |
| 124 | |
| 125 | def sum_with_constructors(self, sum, name, depth): |
| 126 | ctype = get_c_type(name) |
| 127 | s = "typedef struct _%(name)s *%(ctype)s;" % locals() |
| 128 | self.emit(s, depth) |
| 129 | self.emit("", depth) |
| 130 | |
| 131 | def visitProduct(self, product, name, depth): |
| 132 | ctype = get_c_type(name) |
| 133 | s = "typedef struct _%(name)s *%(ctype)s;" % locals() |
| 134 | self.emit(s, depth) |
| 135 | self.emit("", depth) |
| 136 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 137 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 138 | class StructVisitor(EmitVisitor): |
| 139 | """Visitor to generate typdefs for AST.""" |
| 140 | |
| 141 | def visitModule(self, mod): |
| 142 | for dfn in mod.dfns: |
| 143 | self.visit(dfn) |
| 144 | |
| 145 | def visitType(self, type, depth=0): |
| 146 | self.visit(type.value, type.name, depth) |
| 147 | |
| 148 | def visitSum(self, sum, name, depth): |
| 149 | if not is_simple(sum): |
| 150 | self.sum_with_constructors(sum, name, depth) |
| 151 | |
| 152 | def sum_with_constructors(self, sum, name, depth): |
| 153 | def emit(s, depth=depth): |
| 154 | self.emit(s % sys._getframe(1).f_locals, depth) |
| 155 | enum = [] |
| 156 | for i in range(len(sum.types)): |
| 157 | type = sum.types[i] |
| 158 | enum.append("%s_kind=%d" % (type.name, i + 1)) |
| 159 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 160 | emit("enum _%(name)s_kind {" + ", ".join(enum) + "};") |
| 161 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 162 | emit("struct _%(name)s {") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 163 | emit("enum _%(name)s_kind kind;", depth + 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | emit("union {", depth + 1) |
| 165 | for t in sum.types: |
| 166 | self.visit(t, depth + 2) |
| 167 | emit("} v;", depth + 1) |
| 168 | for field in sum.attributes: |
| 169 | # rudimentary attribute handling |
| 170 | type = str(field.type) |
| 171 | assert type in asdl.builtin_types, type |
| 172 | emit("%s %s;" % (type, field.name), depth + 1); |
| 173 | emit("};") |
| 174 | emit("") |
| 175 | |
| 176 | def visitConstructor(self, cons, depth): |
| 177 | if cons.fields: |
| 178 | self.emit("struct {", depth) |
| 179 | for f in cons.fields: |
| 180 | self.visit(f, depth + 1) |
| 181 | self.emit("} %s;" % cons.name, depth) |
| 182 | self.emit("", depth) |
| 183 | else: |
| 184 | # XXX not sure what I want here, nothing is probably fine |
| 185 | pass |
| 186 | |
| 187 | def visitField(self, field, depth): |
| 188 | # XXX need to lookup field.type, because it might be something |
| 189 | # like a builtin... |
| 190 | ctype = get_c_type(field.type) |
| 191 | name = field.name |
| 192 | if field.seq: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 193 | if field.type.value in ('cmpop',): |
| 194 | self.emit("asdl_int_seq *%(name)s;" % locals(), depth) |
| 195 | else: |
| 196 | self.emit("asdl_seq *%(name)s;" % locals(), depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 197 | else: |
| 198 | self.emit("%(ctype)s %(name)s;" % locals(), depth) |
| 199 | |
| 200 | def visitProduct(self, product, name, depth): |
| 201 | self.emit("struct _%(name)s {" % locals(), depth) |
| 202 | for f in product.fields: |
| 203 | self.visit(f, depth + 1) |
| 204 | self.emit("};", depth) |
| 205 | self.emit("", depth) |
| 206 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 207 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 208 | class PrototypeVisitor(EmitVisitor): |
| 209 | """Generate function prototypes for the .h file""" |
| 210 | |
| 211 | def visitModule(self, mod): |
| 212 | for dfn in mod.dfns: |
| 213 | self.visit(dfn) |
| 214 | |
| 215 | def visitType(self, type): |
| 216 | self.visit(type.value, type.name) |
| 217 | |
| 218 | def visitSum(self, sum, name): |
| 219 | if is_simple(sum): |
| 220 | pass # XXX |
| 221 | else: |
| 222 | for t in sum.types: |
| 223 | self.visit(t, name, sum.attributes) |
| 224 | |
| 225 | def get_args(self, fields): |
| 226 | """Return list of C argument into, one for each field. |
| 227 | |
| 228 | Argument info is 3-tuple of a C type, variable name, and flag |
| 229 | that is true if type can be NULL. |
| 230 | """ |
| 231 | args = [] |
| 232 | unnamed = {} |
| 233 | for f in fields: |
| 234 | if f.name is None: |
| 235 | name = f.type |
| 236 | c = unnamed[name] = unnamed.get(name, 0) + 1 |
| 237 | if c > 1: |
| 238 | name = "name%d" % (c - 1) |
| 239 | else: |
| 240 | name = f.name |
| 241 | # XXX should extend get_c_type() to handle this |
| 242 | if f.seq: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 243 | if f.type.value in ('cmpop',): |
| 244 | ctype = "asdl_int_seq *" |
| 245 | else: |
| 246 | ctype = "asdl_seq *" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 247 | else: |
| 248 | ctype = get_c_type(f.type) |
| 249 | args.append((ctype, name, f.opt or f.seq)) |
| 250 | return args |
| 251 | |
| 252 | def visitConstructor(self, cons, type, attrs): |
| 253 | args = self.get_args(cons.fields) |
| 254 | attrs = self.get_args(attrs) |
| 255 | ctype = get_c_type(type) |
| 256 | self.emit_function(cons.name, ctype, args, attrs) |
| 257 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 258 | def emit_function(self, name, ctype, args, attrs, union=True): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 259 | args = args + attrs |
| 260 | if args: |
| 261 | argstr = ", ".join(["%s %s" % (atype, aname) |
| 262 | for atype, aname, opt in args]) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 263 | argstr += ", PyArena *arena" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 264 | else: |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 265 | argstr = "PyArena *arena" |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 266 | margs = "a0" |
| 267 | for i in range(1, len(args)+1): |
| 268 | margs += ", a%d" % i |
| 269 | self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0, |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 270 | reflow=False) |
| 271 | self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 272 | |
| 273 | def visitProduct(self, prod, name): |
| 274 | self.emit_function(name, get_c_type(name), |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 275 | self.get_args(prod.fields), [], union=False) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 276 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 277 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 278 | class FunctionVisitor(PrototypeVisitor): |
| 279 | """Visitor to generate constructor functions for AST.""" |
| 280 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 281 | def emit_function(self, name, ctype, args, attrs, union=True): |
| 282 | def emit(s, depth=0, reflow=True): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 283 | self.emit(s, depth, reflow) |
| 284 | argstr = ", ".join(["%s %s" % (atype, aname) |
| 285 | for atype, aname, opt in args + attrs]) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 286 | if argstr: |
| 287 | argstr += ", PyArena *arena" |
| 288 | else: |
| 289 | argstr = "PyArena *arena" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 290 | self.emit("%s" % ctype, 0) |
| 291 | emit("%s(%s)" % (name, argstr)) |
| 292 | emit("{") |
| 293 | emit("%s p;" % ctype, 1) |
| 294 | for argtype, argname, opt in args: |
Neal Norwitz | 3591bbe | 2007-02-26 19:04:49 +0000 | [diff] [blame] | 295 | if not opt and argtype != "int": |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 296 | emit("if (!%s) {" % argname, 1) |
| 297 | emit("PyErr_SetString(PyExc_ValueError,", 2) |
| 298 | msg = "field %s is required for %s" % (argname, name) |
| 299 | emit(' "%s");' % msg, |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 300 | 2, reflow=False) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 301 | emit('return NULL;', 2) |
| 302 | emit('}', 1) |
| 303 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 304 | emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1); |
Thomas Wouters | a44f3a3 | 2007-02-26 18:20:15 +0000 | [diff] [blame] | 305 | emit("if (!p)", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 306 | emit("return NULL;", 2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 307 | if union: |
| 308 | self.emit_body_union(name, args, attrs) |
| 309 | else: |
| 310 | self.emit_body_struct(name, args, attrs) |
| 311 | emit("return p;", 1) |
| 312 | emit("}") |
| 313 | emit("") |
| 314 | |
| 315 | def emit_body_union(self, name, args, attrs): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 316 | def emit(s, depth=0, reflow=True): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 317 | self.emit(s, depth, reflow) |
| 318 | emit("p->kind = %s_kind;" % name, 1) |
| 319 | for argtype, argname, opt in args: |
| 320 | emit("p->v.%s.%s = %s;" % (name, argname, argname), 1) |
| 321 | for argtype, argname, opt in attrs: |
| 322 | emit("p->%s = %s;" % (argname, argname), 1) |
| 323 | |
| 324 | def emit_body_struct(self, name, args, attrs): |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 325 | def emit(s, depth=0, reflow=True): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 326 | self.emit(s, depth, reflow) |
| 327 | for argtype, argname, opt in args: |
| 328 | emit("p->%s = %s;" % (argname, argname), 1) |
| 329 | assert not attrs |
| 330 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 331 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 332 | class PickleVisitor(EmitVisitor): |
| 333 | |
| 334 | def visitModule(self, mod): |
| 335 | for dfn in mod.dfns: |
| 336 | self.visit(dfn) |
| 337 | |
| 338 | def visitType(self, type): |
| 339 | self.visit(type.value, type.name) |
| 340 | |
| 341 | def visitSum(self, sum, name): |
| 342 | pass |
| 343 | |
| 344 | def visitProduct(self, sum, name): |
| 345 | pass |
| 346 | |
| 347 | def visitConstructor(self, cons, name): |
| 348 | pass |
| 349 | |
| 350 | def visitField(self, sum): |
| 351 | pass |
| 352 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 353 | |
| 354 | class Obj2ModPrototypeVisitor(PickleVisitor): |
| 355 | def visitProduct(self, prod, name): |
| 356 | code = "static int obj2ast_%s(PyObject* obj, %s* out, PyArena* arena);" |
| 357 | self.emit(code % (name, get_c_type(name)), 0) |
| 358 | |
| 359 | visitSum = visitProduct |
| 360 | |
| 361 | |
| 362 | class Obj2ModVisitor(PickleVisitor): |
| 363 | def funcHeader(self, name): |
| 364 | ctype = get_c_type(name) |
| 365 | self.emit("int", 0) |
| 366 | self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0) |
| 367 | self.emit("{", 0) |
Benjamin Peterson | 0e9e98e | 2010-11-20 02:01:45 +0000 | [diff] [blame] | 368 | self.emit("int isinstance;", 1) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 369 | self.emit("", 0) |
| 370 | |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 371 | def sumTrailer(self, name, add_label=False): |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 372 | self.emit("", 0) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 373 | # there's really nothing more we can do if this fails ... |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 374 | error = "expected some sort of %s, but got %%R" % name |
| 375 | format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);" |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 376 | self.emit(format % error, 1, reflow=False) |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 377 | if add_label: |
| 378 | self.emit("failed:", 1) |
Benjamin Peterson | 0a4dae5 | 2010-11-21 15:12:34 +0000 | [diff] [blame] | 379 | self.emit("Py_XDECREF(tmp);", 1) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 380 | self.emit("return 1;", 1) |
| 381 | self.emit("}", 0) |
| 382 | self.emit("", 0) |
| 383 | |
| 384 | def simpleSum(self, sum, name): |
| 385 | self.funcHeader(name) |
| 386 | for t in sum.types: |
Benjamin Peterson | 97dd987 | 2009-12-13 01:23:39 +0000 | [diff] [blame] | 387 | line = ("isinstance = PyObject_IsInstance(obj, " |
| 388 | "(PyObject *)%s_type);") |
| 389 | self.emit(line % (t.name,), 1) |
| 390 | self.emit("if (isinstance == -1) {", 1) |
| 391 | self.emit("return 1;", 2) |
| 392 | self.emit("}", 1) |
| 393 | self.emit("if (isinstance) {", 1) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 394 | self.emit("*out = %s;" % t.name, 2) |
| 395 | self.emit("return 0;", 2) |
| 396 | self.emit("}", 1) |
| 397 | self.sumTrailer(name) |
| 398 | |
| 399 | def buildArgs(self, fields): |
| 400 | return ", ".join(fields + ["arena"]) |
| 401 | |
| 402 | def complexSum(self, sum, name): |
| 403 | self.funcHeader(name) |
Benjamin Peterson | d8f6597 | 2010-11-20 04:31:07 +0000 | [diff] [blame] | 404 | self.emit("PyObject *tmp = NULL;", 1) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 405 | for a in sum.attributes: |
| 406 | self.visitAttributeDeclaration(a, name, sum=sum) |
| 407 | self.emit("", 0) |
| 408 | # XXX: should we only do this for 'expr'? |
| 409 | self.emit("if (obj == Py_None) {", 1) |
| 410 | self.emit("*out = NULL;", 2) |
| 411 | self.emit("return 0;", 2) |
| 412 | self.emit("}", 1) |
| 413 | for a in sum.attributes: |
| 414 | self.visitField(a, name, sum=sum, depth=1) |
| 415 | for t in sum.types: |
Benjamin Peterson | 97dd987 | 2009-12-13 01:23:39 +0000 | [diff] [blame] | 416 | line = "isinstance = PyObject_IsInstance(obj, (PyObject*)%s_type);" |
| 417 | self.emit(line % (t.name,), 1) |
| 418 | self.emit("if (isinstance == -1) {", 1) |
| 419 | self.emit("return 1;", 2) |
| 420 | self.emit("}", 1) |
| 421 | self.emit("if (isinstance) {", 1) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 422 | for f in t.fields: |
| 423 | self.visitFieldDeclaration(f, t.name, sum=sum, depth=2) |
| 424 | self.emit("", 0) |
| 425 | for f in t.fields: |
| 426 | self.visitField(f, t.name, sum=sum, depth=2) |
| 427 | args = [f.name.value for f in t.fields] + [a.name.value for a in sum.attributes] |
| 428 | self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2) |
| 429 | self.emit("if (*out == NULL) goto failed;", 2) |
| 430 | self.emit("return 0;", 2) |
| 431 | self.emit("}", 1) |
Benjamin Peterson | 5b06681 | 2010-11-20 01:38:49 +0000 | [diff] [blame] | 432 | self.sumTrailer(name, True) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 433 | |
| 434 | def visitAttributeDeclaration(self, a, name, sum=sum): |
| 435 | ctype = get_c_type(a.type) |
| 436 | self.emit("%s %s;" % (ctype, a.name), 1) |
| 437 | |
| 438 | def visitSum(self, sum, name): |
| 439 | if is_simple(sum): |
| 440 | self.simpleSum(sum, name) |
| 441 | else: |
| 442 | self.complexSum(sum, name) |
| 443 | |
| 444 | def visitProduct(self, prod, name): |
| 445 | ctype = get_c_type(name) |
| 446 | self.emit("int", 0) |
| 447 | self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0) |
| 448 | self.emit("{", 0) |
| 449 | self.emit("PyObject* tmp = NULL;", 1) |
| 450 | for f in prod.fields: |
| 451 | self.visitFieldDeclaration(f, name, prod=prod, depth=1) |
| 452 | self.emit("", 0) |
| 453 | for f in prod.fields: |
| 454 | self.visitField(f, name, prod=prod, depth=1) |
| 455 | args = [f.name.value for f in prod.fields] |
| 456 | self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1) |
| 457 | self.emit("return 0;", 1) |
| 458 | self.emit("failed:", 0) |
| 459 | self.emit("Py_XDECREF(tmp);", 1) |
| 460 | self.emit("return 1;", 1) |
| 461 | self.emit("}", 0) |
| 462 | self.emit("", 0) |
| 463 | |
| 464 | def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0): |
| 465 | ctype = get_c_type(field.type) |
| 466 | if field.seq: |
| 467 | if self.isSimpleType(field): |
| 468 | self.emit("asdl_int_seq* %s;" % field.name, depth) |
| 469 | else: |
| 470 | self.emit("asdl_seq* %s;" % field.name, depth) |
| 471 | else: |
| 472 | ctype = get_c_type(field.type) |
| 473 | self.emit("%s %s;" % (ctype, field.name), depth) |
| 474 | |
| 475 | def isSimpleSum(self, field): |
| 476 | # XXX can the members of this list be determined automatically? |
| 477 | return field.type.value in ('expr_context', 'boolop', 'operator', |
| 478 | 'unaryop', 'cmpop') |
| 479 | |
| 480 | def isNumeric(self, field): |
| 481 | return get_c_type(field.type) in ("int", "bool") |
| 482 | |
| 483 | def isSimpleType(self, field): |
| 484 | return self.isSimpleSum(field) or self.isNumeric(field) |
| 485 | |
| 486 | def visitField(self, field, name, sum=None, prod=None, depth=0): |
| 487 | ctype = get_c_type(field.type) |
| 488 | self.emit("if (PyObject_HasAttrString(obj, \"%s\")) {" % field.name, depth) |
| 489 | self.emit("int res;", depth+1) |
| 490 | if field.seq: |
| 491 | self.emit("Py_ssize_t len;", depth+1) |
| 492 | self.emit("Py_ssize_t i;", depth+1) |
| 493 | self.emit("tmp = PyObject_GetAttrString(obj, \"%s\");" % field.name, depth+1) |
| 494 | self.emit("if (tmp == NULL) goto failed;", depth+1) |
| 495 | if field.seq: |
| 496 | self.emit("if (!PyList_Check(tmp)) {", depth+1) |
| 497 | self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must " |
| 498 | "be a list, not a %%.200s\", tmp->ob_type->tp_name);" % |
| 499 | (name, field.name), |
| 500 | depth+2, reflow=False) |
| 501 | self.emit("goto failed;", depth+2) |
| 502 | self.emit("}", depth+1) |
| 503 | self.emit("len = PyList_GET_SIZE(tmp);", depth+1) |
| 504 | if self.isSimpleType(field): |
| 505 | self.emit("%s = asdl_int_seq_new(len, arena);" % field.name, depth+1) |
| 506 | else: |
| 507 | self.emit("%s = asdl_seq_new(len, arena);" % field.name, depth+1) |
| 508 | self.emit("if (%s == NULL) goto failed;" % field.name, depth+1) |
| 509 | self.emit("for (i = 0; i < len; i++) {", depth+1) |
| 510 | self.emit("%s value;" % ctype, depth+2) |
| 511 | self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &value, arena);" % |
| 512 | field.type, depth+2, reflow=False) |
| 513 | self.emit("if (res != 0) goto failed;", depth+2) |
| 514 | self.emit("asdl_seq_SET(%s, i, value);" % field.name, depth+2) |
| 515 | self.emit("}", depth+1) |
| 516 | else: |
| 517 | self.emit("res = obj2ast_%s(tmp, &%s, arena);" % |
| 518 | (field.type, field.name), depth+1) |
| 519 | self.emit("if (res != 0) goto failed;", depth+1) |
| 520 | |
| 521 | self.emit("Py_XDECREF(tmp);", depth+1) |
| 522 | self.emit("tmp = NULL;", depth+1) |
| 523 | self.emit("} else {", depth) |
| 524 | if not field.opt: |
| 525 | message = "required field \\\"%s\\\" missing from %s" % (field.name, name) |
| 526 | format = "PyErr_SetString(PyExc_TypeError, \"%s\");" |
| 527 | self.emit(format % message, depth+1, reflow=False) |
| 528 | self.emit("return 1;", depth+1) |
| 529 | else: |
| 530 | if self.isNumeric(field): |
| 531 | self.emit("%s = 0;" % field.name, depth+1) |
| 532 | elif not self.isSimpleType(field): |
| 533 | self.emit("%s = NULL;" % field.name, depth+1) |
| 534 | else: |
| 535 | raise TypeError("could not determine the default value for %s" % field.name) |
| 536 | self.emit("}", depth) |
| 537 | |
| 538 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 539 | class MarshalPrototypeVisitor(PickleVisitor): |
| 540 | |
| 541 | def prototype(self, sum, name): |
| 542 | ctype = get_c_type(name) |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 543 | self.emit("static int marshal_write_%s(PyObject **, int *, %s);" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 544 | % (name, ctype), 0) |
| 545 | |
| 546 | visitProduct = visitSum = prototype |
| 547 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 548 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 549 | class PyTypesDeclareVisitor(PickleVisitor): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 550 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 551 | def visitProduct(self, prod, name): |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 552 | self.emit("static PyTypeObject *%s_type;" % name, 0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 553 | self.emit("static PyObject* ast2obj_%s(void*);" % name, 0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 554 | if prod.fields: |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 555 | self.emit("static char *%s_fields[]={" % name,0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 556 | for f in prod.fields: |
| 557 | self.emit('"%s",' % f.name, 1) |
| 558 | self.emit("};", 0) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 559 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 560 | def visitSum(self, sum, name): |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 561 | self.emit("static PyTypeObject *%s_type;" % name, 0) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 562 | if sum.attributes: |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 563 | self.emit("static char *%s_attributes[] = {" % name, 0) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 564 | for a in sum.attributes: |
| 565 | self.emit('"%s",' % a.name, 1) |
| 566 | self.emit("};", 0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 567 | ptype = "void*" |
| 568 | if is_simple(sum): |
| 569 | ptype = get_c_type(name) |
| 570 | tnames = [] |
| 571 | for t in sum.types: |
| 572 | tnames.append(str(t.name)+"_singleton") |
| 573 | tnames = ", *".join(tnames) |
| 574 | self.emit("static PyObject *%s;" % tnames, 0) |
| 575 | self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0) |
| 576 | for t in sum.types: |
| 577 | self.visitConstructor(t, name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 578 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 579 | def visitConstructor(self, cons, name): |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 580 | self.emit("static PyTypeObject *%s_type;" % cons.name, 0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 581 | if cons.fields: |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 582 | self.emit("static char *%s_fields[]={" % cons.name, 0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 583 | for t in cons.fields: |
| 584 | self.emit('"%s",' % t.name, 1) |
| 585 | self.emit("};",0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 586 | |
| 587 | class PyTypesVisitor(PickleVisitor): |
| 588 | |
| 589 | def visitModule(self, mod): |
| 590 | self.emit(""" |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 591 | static int |
| 592 | ast_type_init(PyObject *self, PyObject *args, PyObject *kw) |
| 593 | { |
| 594 | Py_ssize_t i, numfields = 0; |
| 595 | int res = -1; |
| 596 | PyObject *key, *value, *fields; |
| 597 | fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields"); |
| 598 | if (!fields) |
| 599 | PyErr_Clear(); |
| 600 | if (fields) { |
| 601 | numfields = PySequence_Size(fields); |
| 602 | if (numfields == -1) |
| 603 | goto cleanup; |
| 604 | } |
| 605 | res = 0; /* if no error occurs, this stays 0 to the end */ |
| 606 | if (PyTuple_GET_SIZE(args) > 0) { |
| 607 | if (numfields != PyTuple_GET_SIZE(args)) { |
| 608 | PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s" |
Benjamin Peterson | 75080da | 2008-09-10 22:37:34 +0000 | [diff] [blame] | 609 | "%zd positional argument%s", |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 610 | Py_TYPE(self)->tp_name, |
| 611 | numfields == 0 ? "" : "either 0 or ", |
| 612 | numfields, numfields == 1 ? "" : "s"); |
| 613 | res = -1; |
| 614 | goto cleanup; |
| 615 | } |
| 616 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
| 617 | /* cannot be reached when fields is NULL */ |
| 618 | PyObject *name = PySequence_GetItem(fields, i); |
| 619 | if (!name) { |
| 620 | res = -1; |
| 621 | goto cleanup; |
| 622 | } |
| 623 | res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i)); |
| 624 | Py_DECREF(name); |
| 625 | if (res < 0) |
| 626 | goto cleanup; |
| 627 | } |
| 628 | } |
| 629 | if (kw) { |
| 630 | i = 0; /* needed by PyDict_Next */ |
| 631 | while (PyDict_Next(kw, &i, &key, &value)) { |
| 632 | res = PyObject_SetAttr(self, key, value); |
| 633 | if (res < 0) |
| 634 | goto cleanup; |
| 635 | } |
| 636 | } |
| 637 | cleanup: |
| 638 | Py_XDECREF(fields); |
| 639 | return res; |
| 640 | } |
| 641 | |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 642 | /* Pickling support */ |
| 643 | static PyObject * |
| 644 | ast_type_reduce(PyObject *self, PyObject *unused) |
| 645 | { |
| 646 | PyObject *res; |
| 647 | PyObject *dict = PyObject_GetAttrString(self, "__dict__"); |
| 648 | if (dict == NULL) { |
| 649 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 650 | PyErr_Clear(); |
| 651 | else |
| 652 | return NULL; |
| 653 | } |
| 654 | if (dict) { |
| 655 | res = Py_BuildValue("O()O", Py_TYPE(self), dict); |
| 656 | Py_DECREF(dict); |
| 657 | return res; |
| 658 | } |
| 659 | return Py_BuildValue("O()", Py_TYPE(self)); |
| 660 | } |
| 661 | |
| 662 | static PyMethodDef ast_type_methods[] = { |
| 663 | {"__reduce__", ast_type_reduce, METH_NOARGS, NULL}, |
| 664 | {NULL} |
| 665 | }; |
| 666 | |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 667 | static PyTypeObject AST_type = { |
| 668 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 669 | "_ast.AST", |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 670 | sizeof(PyObject), |
| 671 | 0, |
| 672 | 0, /* tp_dealloc */ |
| 673 | 0, /* tp_print */ |
| 674 | 0, /* tp_getattr */ |
| 675 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 676 | 0, /* tp_reserved */ |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 677 | 0, /* tp_repr */ |
| 678 | 0, /* tp_as_number */ |
| 679 | 0, /* tp_as_sequence */ |
| 680 | 0, /* tp_as_mapping */ |
| 681 | 0, /* tp_hash */ |
| 682 | 0, /* tp_call */ |
| 683 | 0, /* tp_str */ |
| 684 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 685 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 686 | 0, /* tp_as_buffer */ |
| 687 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 688 | 0, /* tp_doc */ |
| 689 | 0, /* tp_traverse */ |
| 690 | 0, /* tp_clear */ |
| 691 | 0, /* tp_richcompare */ |
| 692 | 0, /* tp_weaklistoffset */ |
| 693 | 0, /* tp_iter */ |
| 694 | 0, /* tp_iternext */ |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 695 | ast_type_methods, /* tp_methods */ |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 696 | 0, /* tp_members */ |
| 697 | 0, /* tp_getset */ |
| 698 | 0, /* tp_base */ |
| 699 | 0, /* tp_dict */ |
| 700 | 0, /* tp_descr_get */ |
| 701 | 0, /* tp_descr_set */ |
| 702 | 0, /* tp_dictoffset */ |
| 703 | (initproc)ast_type_init, /* tp_init */ |
| 704 | PyType_GenericAlloc, /* tp_alloc */ |
| 705 | PyType_GenericNew, /* tp_new */ |
| 706 | PyObject_Del, /* tp_free */ |
| 707 | }; |
| 708 | |
| 709 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 710 | static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) |
| 711 | { |
| 712 | PyObject *fnames, *result; |
| 713 | int i; |
Neal Norwitz | ee9b10a | 2008-03-31 05:29:39 +0000 | [diff] [blame] | 714 | fnames = PyTuple_New(num_fields); |
| 715 | if (!fnames) return NULL; |
| 716 | for (i = 0; i < num_fields; i++) { |
Neal Norwitz | e4dc324 | 2007-08-25 01:33:49 +0000 | [diff] [blame] | 717 | PyObject *field = PyUnicode_FromString(fields[i]); |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 718 | if (!field) { |
| 719 | Py_DECREF(fnames); |
| 720 | return NULL; |
| 721 | } |
| 722 | PyTuple_SET_ITEM(fnames, i, field); |
| 723 | } |
Benjamin Peterson | 5b92772 | 2010-06-07 22:24:18 +0000 | [diff] [blame] | 724 | result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}", |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 725 | type, base, "_fields", fnames, "__module__", "_ast"); |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 726 | Py_DECREF(fnames); |
| 727 | return (PyTypeObject*)result; |
| 728 | } |
| 729 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 730 | static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) |
| 731 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 732 | int i, result; |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 733 | PyObject *s, *l = PyTuple_New(num_fields); |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 734 | if (!l) |
| 735 | return 0; |
| 736 | for (i = 0; i < num_fields; i++) { |
Neal Norwitz | e4dc324 | 2007-08-25 01:33:49 +0000 | [diff] [blame] | 737 | s = PyUnicode_FromString(attrs[i]); |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 738 | if (!s) { |
| 739 | Py_DECREF(l); |
| 740 | return 0; |
| 741 | } |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 742 | PyTuple_SET_ITEM(l, i, s); |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 743 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 744 | result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; |
| 745 | Py_DECREF(l); |
| 746 | return result; |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 749 | /* Conversion AST -> Python */ |
| 750 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 751 | static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) |
| 752 | { |
| 753 | int i, n = asdl_seq_LEN(seq); |
| 754 | PyObject *result = PyList_New(n); |
| 755 | PyObject *value; |
| 756 | if (!result) |
| 757 | return NULL; |
| 758 | for (i = 0; i < n; i++) { |
| 759 | value = func(asdl_seq_GET(seq, i)); |
| 760 | if (!value) { |
| 761 | Py_DECREF(result); |
| 762 | return NULL; |
| 763 | } |
| 764 | PyList_SET_ITEM(result, i, value); |
| 765 | } |
| 766 | return result; |
| 767 | } |
| 768 | |
| 769 | static PyObject* ast2obj_object(void *o) |
| 770 | { |
| 771 | if (!o) |
| 772 | o = Py_None; |
| 773 | Py_INCREF((PyObject*)o); |
| 774 | return (PyObject*)o; |
| 775 | } |
| 776 | #define ast2obj_identifier ast2obj_object |
| 777 | #define ast2obj_string ast2obj_object |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 778 | |
Thomas Wouters | a44f3a3 | 2007-02-26 18:20:15 +0000 | [diff] [blame] | 779 | static PyObject* ast2obj_int(long b) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 780 | { |
Christian Heimes | 2b1c592 | 2007-12-02 22:43:00 +0000 | [diff] [blame] | 781 | return PyLong_FromLong(b); |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 782 | } |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 783 | |
| 784 | /* Conversion Python -> AST */ |
| 785 | |
| 786 | static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) |
| 787 | { |
| 788 | if (obj == Py_None) |
| 789 | obj = NULL; |
| 790 | if (obj) |
| 791 | PyArena_AddPyObject(arena, obj); |
| 792 | Py_XINCREF(obj); |
| 793 | *out = obj; |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | #define obj2ast_identifier obj2ast_object |
| 798 | #define obj2ast_string obj2ast_object |
| 799 | |
| 800 | static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) |
| 801 | { |
| 802 | int i; |
| 803 | if (!PyLong_Check(obj)) { |
| 804 | PyObject *s = PyObject_Repr(obj); |
| 805 | if (s == NULL) return 1; |
| 806 | PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s", |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 807 | PyBytes_AS_STRING(s)); |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 808 | Py_DECREF(s); |
| 809 | return 1; |
| 810 | } |
| 811 | |
| 812 | i = (int)PyLong_AsLong(obj); |
| 813 | if (i == -1 && PyErr_Occurred()) |
| 814 | return 1; |
| 815 | *out = i; |
| 816 | return 0; |
| 817 | } |
| 818 | |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 819 | static int add_ast_fields(void) |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 820 | { |
| 821 | PyObject *empty_tuple, *d; |
| 822 | if (PyType_Ready(&AST_type) < 0) |
| 823 | return -1; |
| 824 | d = AST_type.tp_dict; |
| 825 | empty_tuple = PyTuple_New(0); |
| 826 | if (!empty_tuple || |
| 827 | PyDict_SetItemString(d, "_fields", empty_tuple) < 0 || |
| 828 | PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) { |
| 829 | Py_XDECREF(empty_tuple); |
| 830 | return -1; |
| 831 | } |
| 832 | Py_DECREF(empty_tuple); |
| 833 | return 0; |
| 834 | } |
| 835 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 836 | """, 0, reflow=False) |
| 837 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 838 | self.emit("static int init_types(void)",0) |
| 839 | self.emit("{", 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 840 | self.emit("static int initialized;", 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 841 | self.emit("if (initialized) return 1;", 1) |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 842 | self.emit("if (add_ast_fields() < 0) return 0;", 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 843 | for dfn in mod.dfns: |
| 844 | self.visit(dfn) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 845 | self.emit("initialized = 1;", 1) |
| 846 | self.emit("return 1;", 1); |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 847 | self.emit("}", 0) |
| 848 | |
| 849 | def visitProduct(self, prod, name): |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 850 | if prod.fields: |
| 851 | fields = name.value+"_fields" |
| 852 | else: |
| 853 | fields = "NULL" |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 854 | self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' % |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 855 | (name, name, fields, len(prod.fields)), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 856 | self.emit("if (!%s_type) return 0;" % name, 1) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 857 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 858 | def visitSum(self, sum, name): |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 859 | self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' % |
| 860 | (name, name), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 861 | self.emit("if (!%s_type) return 0;" % name, 1) |
| 862 | if sum.attributes: |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 863 | self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" % |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 864 | (name, name, len(sum.attributes)), 1) |
| 865 | else: |
| 866 | self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 867 | simple = is_simple(sum) |
| 868 | for t in sum.types: |
| 869 | self.visitConstructor(t, name, simple) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 870 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 871 | def visitConstructor(self, cons, name, simple): |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 872 | if cons.fields: |
| 873 | fields = cons.name.value+"_fields" |
| 874 | else: |
| 875 | fields = "NULL" |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 876 | self.emit('%s_type = make_type("%s", %s_type, %s, %d);' % |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 877 | (cons.name, cons.name, name, fields, len(cons.fields)), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 878 | self.emit("if (!%s_type) return 0;" % cons.name, 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 879 | if simple: |
| 880 | self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" % |
| 881 | (cons.name, cons.name), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 882 | self.emit("if (!%s_singleton) return 0;" % cons.name, 1) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 883 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 884 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 885 | def parse_version(mod): |
| 886 | return mod.version.value[12:-3] |
| 887 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 888 | class ASTModuleVisitor(PickleVisitor): |
| 889 | |
| 890 | def visitModule(self, mod): |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 891 | self.emit("static struct PyModuleDef _astmodule = {", 0) |
| 892 | self.emit(' PyModuleDef_HEAD_INIT, "_ast"', 0) |
| 893 | self.emit("};", 0) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 894 | self.emit("PyMODINIT_FUNC", 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 895 | self.emit("PyInit__ast(void)", 0) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 896 | self.emit("{", 0) |
| 897 | self.emit("PyObject *m, *d;", 1) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 898 | self.emit("if (!init_types()) return NULL;", 1) |
| 899 | self.emit('m = PyModule_Create(&_astmodule);', 1) |
| 900 | self.emit("if (!m) return NULL;", 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 901 | self.emit("d = PyModule_GetDict(m);", 1) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 902 | self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 903 | self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 904 | self.emit("return NULL;", 2) |
Martin v. Löwis | 40d8459 | 2006-02-28 00:30:54 +0000 | [diff] [blame] | 905 | # Value of version: "$Revision$" |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 906 | self.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)' |
| 907 | % parse_version(mod), 1) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 908 | self.emit("return NULL;", 2) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 909 | for dfn in mod.dfns: |
| 910 | self.visit(dfn) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 911 | self.emit("return m;", 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 912 | self.emit("}", 0) |
| 913 | |
| 914 | def visitProduct(self, prod, name): |
| 915 | self.addObj(name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 916 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 917 | def visitSum(self, sum, name): |
| 918 | self.addObj(name) |
| 919 | for t in sum.types: |
| 920 | self.visitConstructor(t, name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 921 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 922 | def visitConstructor(self, cons, name): |
| 923 | self.addObj(cons.name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 924 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 925 | def addObj(self, name): |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 926 | self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return NULL;' % (name, name), 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 928 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 | _SPECIALIZED_SEQUENCES = ('stmt', 'expr') |
| 930 | |
| 931 | def find_sequence(fields, doing_specialization): |
| 932 | """Return True if any field uses a sequence.""" |
| 933 | for f in fields: |
| 934 | if f.seq: |
| 935 | if not doing_specialization: |
| 936 | return True |
| 937 | if str(f.type) not in _SPECIALIZED_SEQUENCES: |
| 938 | return True |
| 939 | return False |
| 940 | |
| 941 | def has_sequence(types, doing_specialization): |
| 942 | for t in types: |
| 943 | if find_sequence(t.fields, doing_specialization): |
| 944 | return True |
| 945 | return False |
| 946 | |
| 947 | |
| 948 | class StaticVisitor(PickleVisitor): |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 949 | CODE = '''Very simple, always emit this static code. Overide CODE''' |
| 950 | |
| 951 | def visit(self, object): |
| 952 | self.emit(self.CODE, 0, reflow=False) |
| 953 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 954 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 955 | class ObjVisitor(PickleVisitor): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 956 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 957 | def func_begin(self, name): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 958 | ctype = get_c_type(name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 959 | self.emit("PyObject*", 0) |
| 960 | self.emit("ast2obj_%s(void* _o)" % (name), 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 961 | self.emit("{", 0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 962 | self.emit("%s o = (%s)_o;" % (ctype, ctype), 1) |
| 963 | self.emit("PyObject *result = NULL, *value = NULL;", 1) |
| 964 | self.emit('if (!o) {', 1) |
| 965 | self.emit("Py_INCREF(Py_None);", 2) |
| 966 | self.emit('return Py_None;', 2) |
| 967 | self.emit("}", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 968 | self.emit('', 0) |
| 969 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 970 | def func_end(self): |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 971 | self.emit("return result;", 1) |
| 972 | self.emit("failed:", 0) |
| 973 | self.emit("Py_XDECREF(value);", 1) |
| 974 | self.emit("Py_XDECREF(result);", 1) |
| 975 | self.emit("return NULL;", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 976 | self.emit("}", 0) |
| 977 | self.emit("", 0) |
| 978 | |
| 979 | def visitSum(self, sum, name): |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 980 | if is_simple(sum): |
| 981 | self.simpleSum(sum, name) |
| 982 | return |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 983 | self.func_begin(name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 984 | self.emit("switch (o->kind) {", 1) |
| 985 | for i in range(len(sum.types)): |
| 986 | t = sum.types[i] |
| 987 | self.visitConstructor(t, i + 1, name) |
| 988 | self.emit("}", 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 989 | for a in sum.attributes: |
| 990 | self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1) |
| 991 | self.emit("if (!value) goto failed;", 1) |
Martin v. Löwis | 03e5bc0 | 2006-03-02 00:31:27 +0000 | [diff] [blame] | 992 | self.emit('if (PyObject_SetAttrString(result, "%s", value) < 0)' % a.name, 1) |
| 993 | self.emit('goto failed;', 2) |
| 994 | self.emit('Py_DECREF(value);', 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 995 | self.func_end() |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 996 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 997 | def simpleSum(self, sum, name): |
| 998 | self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0) |
| 999 | self.emit("{", 0) |
| 1000 | self.emit("switch(o) {", 1) |
| 1001 | for t in sum.types: |
| 1002 | self.emit("case %s:" % t.name, 2) |
| 1003 | self.emit("Py_INCREF(%s_singleton);" % t.name, 3) |
| 1004 | self.emit("return %s_singleton;" % t.name, 3) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1005 | self.emit("default:" % name, 2) |
| 1006 | self.emit('/* should never happen, but just in case ... */', 3) |
| 1007 | code = "PyErr_Format(PyExc_SystemError, \"unknown %s found\");" % name |
| 1008 | self.emit(code, 3, reflow=False) |
| 1009 | self.emit("return NULL;", 3) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1010 | self.emit("}", 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1011 | self.emit("}", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1012 | |
| 1013 | def visitProduct(self, prod, name): |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 1014 | self.func_begin(name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1015 | self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1); |
| 1016 | self.emit("if (!result) return NULL;", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1017 | for field in prod.fields: |
| 1018 | self.visitField(field, name, 1, True) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 1019 | self.func_end() |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 1020 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1021 | def visitConstructor(self, cons, enum, name): |
| 1022 | self.emit("case %s_kind:" % cons.name, 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1023 | self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2); |
| 1024 | self.emit("if (!result) goto failed;", 2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1025 | for f in cons.fields: |
| 1026 | self.visitField(f, cons.name, 2, False) |
| 1027 | self.emit("break;", 2) |
| 1028 | |
| 1029 | def visitField(self, field, name, depth, product): |
| 1030 | def emit(s, d): |
| 1031 | self.emit(s, depth + d) |
| 1032 | if product: |
| 1033 | value = "o->%s" % field.name |
| 1034 | else: |
| 1035 | value = "o->v.%s.%s" % (name, field.name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1036 | self.set(field, value, depth) |
| 1037 | emit("if (!value) goto failed;", 0) |
| 1038 | emit('if (PyObject_SetAttrString(result, "%s", value) == -1)' % field.name, 0) |
| 1039 | emit("goto failed;", 1) |
| 1040 | emit("Py_DECREF(value);", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1041 | |
| 1042 | def emitSeq(self, field, value, depth, emit): |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1043 | emit("seq = %s;" % value, 0) |
| 1044 | emit("n = asdl_seq_LEN(seq);", 0) |
| 1045 | emit("value = PyList_New(n);", 0) |
| 1046 | emit("if (!value) goto failed;", 0) |
| 1047 | emit("for (i = 0; i < n; i++) {", 0) |
| 1048 | self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1) |
| 1049 | emit("if (!value1) goto failed;", 1) |
| 1050 | emit("PyList_SET_ITEM(value, i, value1);", 1) |
| 1051 | emit("value1 = NULL;", 1) |
| 1052 | emit("}", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1053 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1054 | def set(self, field, value, depth): |
| 1055 | if field.seq: |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 1056 | # XXX should really check for is_simple, but that requires a symbol table |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1057 | if field.type.value == "cmpop": |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 1058 | # While the sequence elements are stored as void*, |
| 1059 | # ast2obj_cmpop expects an enum |
| 1060 | self.emit("{", depth) |
| 1061 | self.emit("int i, n = asdl_seq_LEN(%s);" % value, depth+1) |
| 1062 | self.emit("value = PyList_New(n);", depth+1) |
| 1063 | self.emit("if (!value) goto failed;", depth+1) |
| 1064 | self.emit("for(i = 0; i < n; i++)", depth+1) |
| 1065 | # This cannot fail, so no need for error handling |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1066 | self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value, |
| 1067 | depth+2, reflow=False) |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 1068 | self.emit("}", depth) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1069 | else: |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 1070 | self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1071 | else: |
| 1072 | ctype = get_c_type(field.type) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1073 | self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False) |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 1074 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1075 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1076 | class PartingShots(StaticVisitor): |
| 1077 | |
| 1078 | CODE = """ |
| 1079 | PyObject* PyAST_mod2obj(mod_ty t) |
| 1080 | { |
| 1081 | init_types(); |
| 1082 | return ast2obj_mod(t); |
| 1083 | } |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1084 | |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 1085 | /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */ |
| 1086 | mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1087 | { |
| 1088 | mod_ty res; |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 1089 | PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type, |
| 1090 | (PyObject*)Interactive_type}; |
| 1091 | char *req_name[] = {"Module", "Expression", "Interactive"}; |
Benjamin Peterson | 97dd987 | 2009-12-13 01:23:39 +0000 | [diff] [blame] | 1092 | int isinstance; |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 1093 | assert(0 <= mode && mode <= 2); |
| 1094 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1095 | init_types(); |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 1096 | |
Benjamin Peterson | 97dd987 | 2009-12-13 01:23:39 +0000 | [diff] [blame] | 1097 | isinstance = PyObject_IsInstance(ast, req_type[mode]); |
| 1098 | if (isinstance == -1) |
| 1099 | return NULL; |
| 1100 | if (!isinstance) { |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 1101 | PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s", |
| 1102 | req_name[mode], Py_TYPE(ast)->tp_name); |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1103 | return NULL; |
| 1104 | } |
| 1105 | if (obj2ast_mod(ast, &res, arena) != 0) |
| 1106 | return NULL; |
| 1107 | else |
| 1108 | return res; |
| 1109 | } |
| 1110 | |
| 1111 | int PyAST_Check(PyObject* obj) |
| 1112 | { |
| 1113 | init_types(); |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 1114 | return PyObject_IsInstance(obj, (PyObject*)&AST_type); |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1115 | } |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 1116 | """ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1117 | |
| 1118 | class ChainOfVisitors: |
| 1119 | def __init__(self, *visitors): |
| 1120 | self.visitors = visitors |
| 1121 | |
| 1122 | def visit(self, object): |
| 1123 | for v in self.visitors: |
| 1124 | v.visit(object) |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 1125 | v.emit("", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1126 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1127 | common_msg = "/* File automatically generated by %s. */\n\n" |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1128 | |
| 1129 | c_file_msg = """ |
| 1130 | /* |
| 1131 | __version__ %s. |
| 1132 | |
| 1133 | This module must be committed separately after each AST grammar change; |
| 1134 | The __version__ number is set to the revision number of the commit |
| 1135 | containing the grammar change. |
| 1136 | */ |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1137 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1138 | """ |
| 1139 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1140 | def main(srcfile): |
Neal Norwitz | 897ff81 | 2005-12-11 21:18:22 +0000 | [diff] [blame] | 1141 | argv0 = sys.argv[0] |
Armin Rigo | 6b1793f | 2005-12-14 18:05:14 +0000 | [diff] [blame] | 1142 | components = argv0.split(os.sep) |
| 1143 | argv0 = os.sep.join(components[-2:]) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1144 | auto_gen_msg = common_msg % argv0 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1145 | mod = asdl.parse(srcfile) |
| 1146 | if not asdl.check(mod): |
| 1147 | sys.exit(1) |
| 1148 | if INC_DIR: |
| 1149 | p = "%s/%s-ast.h" % (INC_DIR, mod.name) |
Guido van Rossum | 2af1d89 | 2007-11-23 21:26:49 +0000 | [diff] [blame] | 1150 | f = open(p, "w") |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1151 | f.write(auto_gen_msg) |
| 1152 | f.write('#include "asdl.h"\n\n') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1153 | c = ChainOfVisitors(TypeDefVisitor(f), |
| 1154 | StructVisitor(f), |
| 1155 | PrototypeVisitor(f), |
| 1156 | ) |
| 1157 | c.visit(mod) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1158 | f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 1159 | f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") |
| 1160 | f.write("int PyAST_Check(PyObject* obj);\n") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1161 | f.close() |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | |
| 1163 | if SRC_DIR: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1164 | p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") |
Guido van Rossum | 2af1d89 | 2007-11-23 21:26:49 +0000 | [diff] [blame] | 1165 | f = open(p, "w") |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1166 | f.write(auto_gen_msg) |
| 1167 | f.write(c_file_msg % parse_version(mod)) |
| 1168 | f.write('#include "Python.h"\n') |
| 1169 | f.write('#include "%s-ast.h"\n' % mod.name) |
| 1170 | f.write('\n') |
Neal Norwitz | 207c9f3 | 2008-03-31 04:42:11 +0000 | [diff] [blame] | 1171 | f.write("static PyTypeObject AST_type;\n") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1172 | v = ChainOfVisitors( |
| 1173 | PyTypesDeclareVisitor(f), |
| 1174 | PyTypesVisitor(f), |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1175 | Obj2ModPrototypeVisitor(f), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1176 | FunctionVisitor(f), |
| 1177 | ObjVisitor(f), |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 1178 | Obj2ModVisitor(f), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1179 | ASTModuleVisitor(f), |
| 1180 | PartingShots(f), |
| 1181 | ) |
| 1182 | v.visit(mod) |
| 1183 | f.close() |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | |
| 1185 | if __name__ == "__main__": |
| 1186 | import sys |
| 1187 | import getopt |
| 1188 | |
| 1189 | INC_DIR = '' |
| 1190 | SRC_DIR = '' |
| 1191 | opts, args = getopt.getopt(sys.argv[1:], "h:c:") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1192 | if len(opts) != 1: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1193 | sys.stdout.write("Must specify exactly one output file\n") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1194 | sys.exit(1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1195 | for o, v in opts: |
| 1196 | if o == '-h': |
| 1197 | INC_DIR = v |
| 1198 | if o == '-c': |
| 1199 | SRC_DIR = v |
| 1200 | if len(args) != 1: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1201 | sys.stdout.write("Must specify single input file\n") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1202 | sys.exit(1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1203 | main(args[0]) |