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