How to use the pycparser.c_ast.Struct function in pycparser

To help you get started, we’ve selected a few pycparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github maltek / swift-frida / examples / c_header_gen / c_header_gen.py View on Github external
'int64': 'int64_t',
                }
                print(f'typedef {ctype_names[info["getCType"]]} {mangle_name(name)};')
            elif name == 'Builtin.NativeObject':
                print(f'typedef void *{mangle_name(name)};')
            else:
                print(f'typedef char {mangle_name(name)}[{info["size"]}];')
        elif info['kind'] == 'Function':
            print(f"typedef void *func_{str(hash(name))[1:]};")  # TODO: proper names
        else:
            print(f'typedef char {mangle_name(name)}[{info["size"]}];')

        if ctype:
            type_decl = TypeDecl(mangle_name(name), None, ctype)
            ctypes[name] = type_decl
            type_decl_forward = Struct(mangle_name(name) + "_s", [])
            if isinstance(type_decl, PtrDecl):
                ptr_types.add(name)
                type_decl_forward = PtrDecl(None, type_decl_forward)
                print(generator.visit(Typedef(mangle_name(name), None, ['typedef'], type_decl_forward)) + ";")

    for name in ptr_types:
        req_graph.pop(name, None)

    for name in top_sort(req_graph):
        if name in ctypes:
            print(f"\n// {name}")
            print(generator.visit(Typedef(mangle_name(name), None, ['typedef'], ctypes[name])) + ";")
github littlevgl / lv_binding_micropython / gen / gen_mpy.py View on Github external
def get_name(type):
    if isinstance(type, c_ast.Decl):
        return type.name
    if isinstance(type, c_ast.Struct) and type.name and type.name in explicit_structs:
        return explicit_structs[type.name]
    if isinstance(type, c_ast.TypeDecl):
        return type.declname
    if isinstance(type, c_ast.IdentifierType):
        return type.names[0]
    if isinstance(type, c_ast.FuncDecl):
        return type.type.declname
    if isinstance(type, (c_ast.PtrDecl, c_ast.ArrayDecl)): 
        return get_type(type, remove_quals=True)
    else:
        return gen.visit(type)
github pjsip / pjproject / pjsip-apps / src / jni / jni / swig_gen.py View on Github external
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
github eliben / pycparser / pycparser / c_parser.py View on Github external
def _select_struct_union_class(self, token):
        """ Given a token (either STRUCT or UNION), selects the
            appropriate AST class.
        """
        if token == 'struct':
            return c_ast.Struct
        else:
            return c_ast.Union
github rreilink / pylvgl / sourceparser.py View on Github external
if isinstance(item, c_ast.Decl) and isinstance(item.type, c_ast.FuncDecl):
                # C function
                if item.name not in functions:
                    # If it is already in there, it might be a FuncDef and we want to keep that one
                    functions[item.name] = c_ast.FuncDef(item, None, None)

            elif isinstance(item, c_ast.FuncDef):
                functions[item.decl.name] = item
                
                        
            elif isinstance(item, c_ast.Typedef) and not item.name in self.TYPEDEFS:
                # Do not register typedefs which are defined in self.TYPEDEFS, these are
                # to be treated as basic types by the bindings generators
                typedefs[item.name] = item
                
                if isinstance(item.type, c_ast.TypeDecl) and (isinstance(item.type.type, c_ast.Struct) or isinstance(item.type.type, c_ast.Union)):
                    # typedef struct { ... } lv_struct_name_t;
                    try:
                        structs[stripstart(item.type.declname,'lv_')] = item.type.type
                    except ValueError: # If name does not start with lv_
                        pass
                    
                elif (isinstance(item.type, c_ast.TypeDecl) and isinstance(item.type.type, c_ast.IdentifierType) and 
                        isinstance(previous_item, c_ast.Decl) and isinstance(previous_item.type, c_ast.Enum)):
                    
                    # typedef lv_enum_t ...; directly after an enum definition
                    # newer lvgl uses this to define enum variables as uint8_t
                    enumname, enum = self.enum_to_dict(previous_item.type)
                    
                    enums[enumname] = enum
github jamie-pate / jstruct / parse / annotations.py View on Github external
"""
    Find the IdentifierType or Struct inside a Decl. Count PtrDecls as dereference.
    arraydecl is an ArrayDecl is present
    Returns a tuple of (idtype, structtype, enumtype, arraydecl, dereference)
    """
    idtype = decl.type
    structtype = None
    enumtype = None
    arraydecl = None
    dereference = 0
    while idtype and not isinstance(idtype, c_ast.IdentifierType):
        if isinstance(idtype, c_ast.PtrDecl):
            dereference = dereference + 1
        if isinstance(idtype, c_ast.ArrayDecl):
            arraydecl = idtype
        if isinstance(idtype, c_ast.Struct):
            structtype = idtype
            idtype = None
            break
        if isinstance(idtype, c_ast.Enum):
            enumtype = idtype
            idtype = None
            break
        if not hasattr(idtype, 'type'):
            raise ExpansionError(None, decl, 'Could not find IdentifierType or Struct')
        idtype = idtype.type
    return idtype, structtype, enumtype, arraydecl, dereference
github pjsip / pjproject / pjsip-apps / src / jni / swig_gen.py View on Github external
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
github pjsip / pjproject / pjsip-apps / src / jni / swig_gen.py View on Github external
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
github oridb / mc / support / syscall-gen / c2myr.py View on Github external
def collectdef(n, t):
    if t in [c_ast.Decl, c_ast.TypeDecl] and type(n.type) == c_ast.Struct:
        if fullydefined(n.type):
            ctypes[n.type.name] = n.type
    elif t == c_ast.Typedef:
        # typedef struct foo foo is valid.
        if n.name not in ctypes:
            ctypes[n.name] = n
        # as is 'typedef struct foo { ... } bar'
        if type(n.type) is c_ast.TypeDecl:
            if type(n.type.type) is c_ast.Struct:
                collectdef(n.type, type(n.type))
github JarryShaw / f2format / vendor / pypy / lib_pypy / cffi / cparser.py View on Github external
if not names:
                        names = ['int']    # implicitly
                    if names == ['int']:   # but kill it if 'short' or 'long'
                        if 'short' in prefixes or 'long' in prefixes:
                            names = []
                    names = newnames + names
                ident = ' '.join(names)
                if ident == 'void':
                    return model.void_type, quals
                if ident == '__dotdotdot__':
                    raise FFIError(':%d: bad usage of "..."' %
                            typenode.coord.line)
                tp0, quals0 = resolve_common_type(self, ident)
                return tp0, (quals | quals0)
            #
            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