Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _generate_type(self, n, modifiers=[]):
""" Recursive generation from a type node. n is the type node.
modifiers collects the PtrDecl, ArrayDecl and FuncDecl modifiers
encountered on the way down to a TypeDecl, to allow proper
generation from it.
"""
typ = type(n)
#~ print(n, modifiers)
if typ in (c_ast.TypeDecl, TypeDeclExt):
s = ''
if n.quals:
s += ' '.join(n.quals) + ' '
s += self.visit(n.type)
nstr = n.declname if n.declname else ''
# Resolve modifiers.
# Wrap in parens to distinguish pointer to array and pointer to
# function syntax.
#
for i, modifier in enumerate(modifiers):
if isinstance(modifier, c_ast.ArrayDecl):
if (i != 0 and isinstance(modifiers[i - 1], c_ast.PtrDecl)):
nstr = '(' + nstr + ')'
nstr += '[' + self.visit(modifier.dim) + ']'
elif isinstance(modifier, c_ast.FuncDecl):
def to_decl_ext(d):
if isinstance(d, c_ast.TypeDecl):
return TypeDeclExt.from_pycparser(d)
elif isinstance(d, c_ast.ArrayDecl):
return ArrayDeclExt.from_pycparser(d)
else:
raise TypeError("unexpected decl type: %s" % type(d).__name__)
def from_pycparser(td):
assert isinstance(td, c_ast.TypeDecl)
return TypeDeclExt(td.declname, td.quals, td.type, td.coord)