Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if typ == c_ast.TypeDecl:
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):
if (i != 0 and isinstance(modifiers[i - 1], c_ast.PtrDecl)):
nstr = '(' + nstr + ')'
nstr += '(' + self.visit(modifier.args) + ')'
elif isinstance(modifier, c_ast.PtrDecl):
nstr = '*' + nstr
if nstr: s += ' ' + nstr
return s
elif typ == c_ast.Decl:
return self._generate_decl(n.type)
elif typ == c_ast.Typename:
return self._generate_type(n.type)
elif typ == c_ast.IdentifierType:
return ' '.join(n.names) + ' '
elif typ in (c_ast.ArrayDecl, c_ast.PtrDecl, c_ast.FuncDecl):
return self._generate_type(n.type, modifiers + [n])
else:
# copies.
all_decls: List[Tuple[ca.Decl, int, "ca.ExternalDeclaration"]] = []
main_decl: Optional[ca.Decl] = None
for i in range(len(ast.ext)):
item = ast.ext[i]
if (
isinstance(item, ca.Decl)
and isinstance(item.type, ca.FuncDecl)
and item.name == name
):
new_decl = copy.copy(item)
ast.ext[i] = new_decl
all_decls.append((new_decl, i, new_decl))
if isinstance(item, ca.FuncDef) and item.decl.name == name:
assert isinstance(
item.decl.type, ca.FuncDecl
), "function definitions have function types"
new_fndef = copy.copy(item)
new_decl = copy.copy(item.decl)
new_fndef.decl = new_decl
ast.ext[i] = new_fndef
all_decls.append((new_decl, i, new_fndef))
main_decl = new_decl
# Change the type within the function definition if there is one (since we
# need to keep names there), or else within an arbitrary of the (typically
# just one) declarations. We later mirror the change to all declarations.
ensure(all_decls)
if not main_decl:
main_decl = random.choice(all_decls)[0]
typemap = build_typemap(ast)
if isinstance(type, pycparser.c_ast.Struct):
# 'struct foobar'
tp = self._get_struct_union_enum_type('struct', type, name)
return tp, quals
#
if isinstance(type, pycparser.c_ast.Union):
# 'union foobar'
tp = self._get_struct_union_enum_type('union', type, name)
return tp, quals
#
if isinstance(type, pycparser.c_ast.Enum):
# 'enum foobar'
tp = self._get_struct_union_enum_type('enum', type, name)
return tp, quals
#
if isinstance(typenode, pycparser.c_ast.FuncDecl):
# a function type
return self._parse_function_type(typenode, name), 0
#
# nested anonymous structs or unions end up here
if isinstance(typenode, pycparser.c_ast.Struct):
return self._get_struct_union_enum_type('struct', typenode, name,
nested=True), 0
if isinstance(typenode, pycparser.c_ast.Union):
return self._get_struct_union_enum_type('union', typenode, name,
nested=True), 0
#
raise api.FFIError(":%d: bad or unsupported type declaration" %
typenode.coord.line)
def p_direct_abstract_declarator_6(self, p):
""" direct_abstract_declarator : direct_abstract_declarator LPAREN parameter_type_list_opt RPAREN
"""
func = c_ast.FuncDecl(
args=p[3],
type=None,
coord=p[1].coord)
p[0] = self._type_modify_decl(decl=p[1], modifier=func)
def visit_Decl(self, node):
if isinstance(node.type, FuncDecl):
param_list = node.type.args # type: ParamList
args = [a[1] for a in param_list.children()]
# We need at least three params: the function to call, the number of
# the following paramters, and the ellipsis.
if len(args) < 3:
return
if not isinstance(args[-1], c_ast.EllipsisParam):
return
return_type = node.type.type # type: Decl
function = args[-3] # type: Decl
argcount = args[-2] # type: Decl
# Function must be a pointer to a function
def void_function_decl(self, name, parameters):
return node.FuncDecl(node.ParamList(parameters),
void_type(name))
"""
if hasattr(p[3], "exprs"):
attr_decl = p[3]
decl = p[2]
else:
attr_decl = p[2]
decl = p[3]
if not attr_decl.exprs:
attr_decl = None
if attr_decl:
if isinstance(decl, c_ast.ArrayDecl):
decl.type = to_decl_ext(decl.type)
decl.type.attributes = attr_decl
elif isinstance(decl, c_ast.FuncDecl):
decl.type = to_decl_ext(decl.type)
decl.type.attributes = attr_decl
elif not isinstance(p[2], c_ast.TypeDecl):
raise NotImplementedError(
"cannot attach attributes to nodes of type '%s'"
% type(p[1]))
else:
decl = to_decl_ext(decl)
decl.attributes = attr_decl
p[0] = self._type_modify_decl(decl, p[1])
self._types = dict(knowntypes)
self._typedefs = dict(knowntypedefs)
self.cpt = 0
self.loc_to_decl_info = {}
self.parser = c_parser.CParser()
self._cpt_decl = 0
self.ast_to_typeid_rules = {
c_ast.Struct: self.ast_to_typeid_struct,
c_ast.Union: self.ast_to_typeid_union,
c_ast.IdentifierType: self.ast_to_typeid_identifiertype,
c_ast.TypeDecl: self.ast_to_typeid_typedecl,
c_ast.Decl: self.ast_to_typeid_decl,
c_ast.Typename: self.ast_to_typeid_typename,
c_ast.FuncDecl: self.ast_to_typeid_funcdecl,
c_ast.Enum: self.ast_to_typeid_enum,
c_ast.PtrDecl: self.ast_to_typeid_ptrdecl,
c_ast.EllipsisParam: self.ast_to_typeid_ellipsisparam,
c_ast.ArrayDecl: self.ast_to_typeid_arraydecl,
}
self.ast_parse_rules = {
c_ast.Struct: self.ast_parse_struct,
c_ast.Union: self.ast_parse_union,
c_ast.Typedef: self.ast_parse_typedef,
c_ast.TypeDecl: self.ast_parse_typedecl,
c_ast.IdentifierType: self.ast_parse_identifiertype,
c_ast.Decl: self.ast_parse_decl,
c_ast.PtrDecl: self.ast_parse_ptrdecl,
c_ast.Enum: self.ast_parse_enum,
c_ast.ArrayDecl: self.ast_parse_arraydecl,
def _parse_decl(self, decl):
node = decl.type
if isinstance(node, pycparser.c_ast.FuncDecl):
tp, quals = self._get_type_and_quals(node, name=decl.name)
assert isinstance(tp, model.RawFunctionType)
self._declare_function(tp, quals, decl)
else:
if isinstance(node, pycparser.c_ast.Struct):
self._get_struct_union_enum_type('struct', node)
elif isinstance(node, pycparser.c_ast.Union):
self._get_struct_union_enum_type('union', node)
elif isinstance(node, pycparser.c_ast.Enum):
self._get_struct_union_enum_type('enum', node)
elif not decl.name:
raise CDefError("construct does not declare any variable",
decl)
#
if decl.name:
tp, quals = self._get_type_and_quals(node,