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