Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for a in node.decls:
a_func = a.children()[0][1].children()[0][1].children()
a_func_args = a_func[0][1]
a_func_rettype = a_func[1][1]
fname = "unknown"
if isinstance(a_func_rettype, c_ast.TypeDecl):
fname = a_func_rettype.declname
a_func_rettype.declname = ""
print(c_gen.visit(c_ast.Typedef("dummy", [], [], a_func_rettype)))
a_func_rettype.declname = fname
elif isinstance(a_func_rettype, c_ast.PtrDecl):
a_func_rettype = a_func_rettype.children()[0][1]
fname = a_func_rettype.declname
a_func_rettype.declname = ""
print (c_gen.visit(c_ast.Typedef("dummy", [], [], a_func_rettype)) + " *")
a_func_rettype.declname = fname
arg_names = (list(c[1].name for c in a_func_args.children() if c[1].name is not None))
print(lowcase_name(node.name, fname) + "(" + c_gen.visit(a_func_args) + ")", end="")
if gen_prototypes_only:
print(";")
else:
print("")
print("{")
print("}")
print("")
if not obj_name in enums:
return []
return [enum_member_name for enum_member_name, value in enums[obj_name].items()]
def get_enum_member_name(enum_member):
if enum_member[0].isdigit():
enum_member = '_' + enum_member # needs to be a valid attribute name
return enum_member
def get_enum_value(obj_name, enum_member):
return enums[obj_name][enum_member]
# eprint(enums)
# parse function pointers
func_typedefs = collections.OrderedDict((t.name, t) for t in ast.ext if isinstance(t, c_ast.Typedef) and isinstance(t.type, c_ast.PtrDecl) and isinstance(t.type.type, c_ast.FuncDecl))
# Global blobs
blobs = collections.OrderedDict((decl.name, decl.type.type) for decl in ast.ext \
if isinstance(decl, c_ast.Decl) \
and 'extern' in decl.storage \
and hasattr(decl, 'type') \
and isinstance(decl.type, c_ast.TypeDecl))
#
# Type convertors
#
class MissingConversionException(ValueError):
pass
mp_to_lv = {
def _is_struct_opaque(self, node):
if isinstance(node, c_ast.Typedef) and isinstance(node.type, c_ast.TypeDecl) and \
isinstance(node.type.type, c_ast.Struct) and node.type.type.decls == None:
return True
elif isinstance(node, c_ast.Decl) and isinstance(node.type, c_ast.Struct) and \
node.type.decls == None:
return True
return False
(
ca.TypeDecl,
ca.PtrDecl,
ca.ArrayDecl,
ca.Typename,
ca.IdentifierType,
ca.Struct,
ca.Union,
ca.Enum,
ca.EmptyStatement,
ca.Pragma,
ca.Break,
ca.Continue,
ca.Goto,
ca.CompoundLiteral,
ca.Typedef,
ca.FuncDecl,
ca.FuncDef,
ca.EllipsisParam,
ca.Enumerator,
ca.EnumeratorList,
ca.FileAST,
ca.InitList,
ca.NamedInitializer,
ca.ParamList,
),
):
pass
else:
_: None = node
assert False, f"Node with unknown type: {node}"
return node
# between the repeated typedefs and the real csource
iterator = iter(ast.ext)
for decl in iterator:
if decl.name == '__dotdotdot__':
break
else:
assert 0
current_decl = None
#
try:
self._inside_extern_python = '__cffi_extern_python_stop'
for decl in iterator:
current_decl = decl
if isinstance(decl, pycparser.c_ast.Decl):
self._parse_decl(decl)
elif isinstance(decl, pycparser.c_ast.Typedef):
if not decl.name:
raise CDefError("typedef does not declare any name",
decl)
quals = 0
if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and
decl.type.type.names[-1].startswith('__dotdotdot')):
realtype = self._get_unknown_type(decl)
elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
isinstance(decl.type.type.type,
pycparser.c_ast.IdentifierType) and
decl.type.type.type.names[-1].startswith('__dotdotdot')):
realtype = self._get_unknown_ptr_type(decl)
else:
realtype, quals = self._get_type_and_quals(
decl.type, name=decl.name, partial_length_ok=True)
ast, macros, csource = self._parse(csource)
# add the macros
self._process_macros(macros)
# find the first "__dotdotdot__" and use that as a separator
# between the repeated typedefs and the real csource
iterator = iter(ast.ext)
for decl in iterator:
if decl.name == '__dotdotdot__':
break
#
try:
self._inside_extern_python = '__cffi_extern_python_stop'
for decl in iterator:
if isinstance(decl, pycparser.c_ast.Decl):
self._parse_decl(decl)
elif isinstance(decl, pycparser.c_ast.Typedef):
if not decl.name:
raise api.CDefError("typedef does not declare any name",
decl)
quals = 0
if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType)
and decl.type.type.names[-1] == '__dotdotdot__'):
realtype = self._get_unknown_type(decl)
elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
isinstance(decl.type.type.type,
pycparser.c_ast.IdentifierType) and
decl.type.type.type.names == ['__dotdotdot__']):
realtype = model.unknown_ptr_type(decl.name)
else:
realtype, quals = self._get_type_and_quals(
decl.type, name=decl.name)
def visit_EnumStructUnion(self, node):
if self.in_typedef:
return node
else:
self.added_types.add(node.name)
typedecl_node = c_ast.TypeDecl(node.name, [], node)
return c_ast.Typedef(node.name, [], ['typedef'], typedecl_node)
def cdef(self, csource):
"""Parse the given C source. This registers all declared functions,
types, and global variables. The functions and global variables can
then be accessed via 'ffi.C' or 'ffi.load()'. The types can be used
in 'ffi.new()' and other functions.
"""
csource = ("typedef int __dotdotdot__;\n" +
csource.replace('...', '__dotdotdot__'))
ast = _get_parser().parse(csource)
for decl in ast.ext:
if isinstance(decl, pycparser.c_ast.Decl):
self._parse_decl(decl)
elif isinstance(decl, pycparser.c_ast.Typedef):
if not decl.name:
raise CDefError("typedef does not declare any name", decl)
self._declare('typedef ' + decl.name, decl.type)
else:
raise CDefError("unrecognized construct", decl)
# A similar problem can occur where the declaration ends up looking
# like an abstract declarator. Give it a name if this is the case.
#
elif not isinstance(decls[0]['decl'],
(c_ast.Struct, c_ast.Union, c_ast.IdentifierType)):
decls_0_tail = decls[0]['decl']
while not isinstance(decls_0_tail, c_ast.TypeDecl):
decls_0_tail = decls_0_tail.type
if decls_0_tail.declname is None:
decls_0_tail.declname = spec['type'][-1].names[0]
del spec['type'][-1]
for decl in decls:
assert decl['decl'] is not None
if is_typedef:
declaration = c_ast.Typedef(
name=None,
quals=spec['qual'],
storage=spec['storage'],
type=decl['decl'],
coord=decl['decl'].coord)
else:
declaration = c_ast.Decl(
name=None,
quals=spec['qual'],
storage=spec['storage'],
funcspec=spec['function'],
type=decl['decl'],
init=decl.get('init'),
bitsize=decl.get('bitsize'),
coord=decl['decl'].coord)