ยปCore Development>Code coverage>Parser/asdl_c.py

Python code coverage for Parser/asdl_c.py

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