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