MCPcopy Index your code
hub / github.com/python/cpython / StructVisitor

Class StructVisitor

Parser/asdl_c.py:267–337  ·  view source on GitHub ↗

Visitor to generate typedefs for AST.

Source from the content-addressed store, hash-verified

265 self.emit_sequence_constructor(name, depth)
266
267class StructVisitor(EmitVisitor):
268 """Visitor to generate typedefs for AST."""
269
270 def visitModule(self, mod):
271 for dfn in mod.dfns:
272 self.visit(dfn)
273
274 def visitType(self, type, depth=0):
275 self.visit(type.value, type.name, depth)
276
277 def visitSum(self, sum, name, depth):
278 if not is_simple(sum):
279 self.sum_with_constructors(sum, name, depth)
280
281 def sum_with_constructors(self, sum, name, depth):
282 def emit(s, depth=depth):
283 self.emit(s % sys._getframe(1).f_locals, depth)
284 enum = []
285 for i in range(len(sum.types)):
286 type = sum.types[i]
287 enum.append("%s_kind=%d" % (type.name, i + 1))
288
289 emit("enum _%(name)s_kind {" + ", ".join(enum) + "};")
290
291 emit("struct _%(name)s {")
292 emit("enum _%(name)s_kind kind;", depth + 1)
293 emit("union {", depth + 1)
294 for t in sum.types:
295 self.visit(t, depth + 2)
296 emit("} v;", depth + 1)
297 for field in sum.attributes:
298 # rudimentary attribute handling
299 type = str(field.type)
300 assert type in asdl.builtin_types, type
301 emit("%s %s;" % (type, field.name), depth + 1);
302 emit("};")
303 emit("")
304
305 def visitConstructor(self, cons, depth):
306 if cons.fields:
307 self.emit("struct {", depth)
308 for f in cons.fields:
309 self.visit(f, depth + 1)
310 self.emit("} %s;" % cons.name, depth)
311 self.emit("", depth)
312
313 def visitField(self, field, depth):
314 # XXX need to lookup field.type, because it might be something
315 # like a builtin...
316 ctype = get_c_type(field.type)
317 name = field.name
318 if field.seq:
319 if field.type in self.metadata.simple_sums:
320 self.emit("asdl_int_seq *%(name)s;" % locals(), depth)
321 else:
322 _type = field.type
323 self.emit("asdl_%(_type)s_seq *%(name)s;" % locals(), depth)
324 else:

Callers 1

write_headerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…