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