How to use the pycparser.c_ast.TypeDecl 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 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 rreilink / pylvgl / sourceparser.py View on Github external
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
                                            
                    
            elif isinstance(item, c_ast.Decl) and isinstance(item.type, c_ast.TypeDecl):
                declarations[stripstart(item.type.declname, 'lv_')] = item.type

            previous_item = item
    
        
        objects, global_functions = self.determine_objects(functions, typedefs)
        
        
        # Find defines in color.h and symbol_def.h
        defines = collections.OrderedDict() # There is no OrderedSet in Python, so let's use OrderedDict with None values
        for filename in 'src/lv_misc/lv_color.h', 'src/lv_misc/lv_symbol_def.h':
            with open(os.path.join(path, filename), 'rt', encoding='utf-8') as file:
                code = file.read()
                for define in re.findall('^\s*#define\s+(\w+)', code,flags = re.MULTILINE):
                    if not define.startswith('_'):
                        defines[define] = None
github eventh / kpro9 / source / cstruct.py View on Github external
def handle_Member(self, node):
        child = node.children()[0]
        if isinstance(child, c_ast.TypeDecl):
            type_ = ' '.join(child.type.names)
        elif isinstance(child, c_ast.ArrayDecl):
            type_ = "array"
        else:
            type_ = "unknown"

        return Member(node.name, type_)
github LedgerHQ / nanos-secure-sdk / pycparser / c_parser.py View on Github external
def p_direct_abstract_declarator_5(self, p):
        """ direct_abstract_declarator  : LBRACKET TIMES RBRACKET
        """
        p[0] = c_ast.ArrayDecl(
            type=c_ast.TypeDecl(None, None, None),
            dim=c_ast.ID(p[3], self._coord(p.lineno(3))),
            coord=self._coord(p.lineno(1)))
github eerimoq / nala / nala / generator.py View on Github external
self.mock_once_func = self.void_function_decl(
            f'{self.func_name}_mock_once',
            mock_params)
        self.set_errno_func = self.void_function_decl(
            f'{self.func_name}_mock_set_errno',
            [decl(
                "errno_value",
                node.TypeDecl("errno_value", [], node.IdentifierType(["int"])),
            )])
        self.callback_decl = function_ptr_decl(
            "callback",
            void_type("callback"),
            create_implementation_params(self.func_params))
        self.variadic_func_real_wrapper_decl = node.FuncDecl(
            node.ParamList(create_implementation_params(self.func_params)),
            node.TypeDecl(
                f'{self.func_name}_mock_va_arg_real',
                [],
                return_type))
        self.default_variadic_func_real_wrapper_decl = node.FuncDecl(
            node.ParamList(create_implementation_params(self.func_params)),
            node.TypeDecl(
                f'nala_v{self.func_name}',
                [],
                return_type))
        self.real_decl = self.rename_function(self.real_func)
        self.wrapped_decl = self.rename_function(self.wrapped_func)
        self.instance_members = []
        self.set_params = []
        self.char_pointer_params = []
        self.pointer_params = []
        self.non_pointer_params  = []
github eventh / kpro9 / CSjark / csjark / cparser.py View on Github external
def handle_array_decl(self, node, depth=None):
        """Find the depth, size and type of the array.

        'node' is a pycparser.c_ast.ArrayDecl instance
        'depth' is a list of elements already traversed
        It returns a list with count of elements in in each level,
        and a Field instance.
        """
        if depth is None:
            depth = []
        child = node.children()[0]

        size = self._get_array_size(node.children()[1])

        # String array
        if (isinstance(child, c_ast.TypeDecl) and
                hasattr(child.children()[0], 'names') and
                child.children()[0].names[0] == 'char'): #hack
            size *= self.size_of('char')
            return depth, self._create_field(child.declname, 'string', size, 0)

        # Multidimensional, handle recursively
        if isinstance(child, c_ast.ArrayDecl):
            if size > 1:
                depth.append(size)
            return self.handle_array_decl(child, depth)

        # Single dimensional normal array
        depth.append(size)
        sub_child = child.children()[0]

        if isinstance(sub_child, c_ast.IdentifierType):
github ContinuumIO / pykit / pykit / deps / pycparser / examples / cdecl.py View on Github external
def _explain_type(decl):
    """ Recursively explains a type decl node
    """
    typ = type(decl)
    
    if typ == c_ast.TypeDecl:
        quals = ' '.join(decl.quals) + ' ' if decl.quals else ''
        return quals + _explain_type(decl.type)
    elif typ == c_ast.Typename or typ == c_ast.Decl:
        return _explain_type(decl.type)
    elif typ == c_ast.IdentifierType:
        return ' '.join(decl.names)
    elif typ == c_ast.PtrDecl:
        quals = ' '.join(decl.quals) + ' ' if decl.quals else ''
        return quals + 'pointer to ' + _explain_type(decl.type)
    elif typ == c_ast.ArrayDecl:
        arr = 'array'
        if decl.dim: arr += '[%s]' % decl.dim.value
        
        return arr + " of " + _explain_type(decl.type)
        
    elif typ == c_ast.FuncDecl:
github mak / mlib / src / struct / cparse.py View on Github external
def get_fields(ty):
    tty = type(ty)
    #print tty
    if issubclass(tty,ast.Typedef):
        return get_fields(ty.type)
    elif issubclass(tty,ast.TypeDecl):
        return get_fields(ty.type)
    elif issubclass(tty,ast.Struct):
        fd = []
        for ch in ty.children():
            fd.append(get_fields(ch[1]))
        return fd
    elif issubclass(tty,ast.Decl):
        fdl= get_fields(ty.type)
        #print 'decl',ty.name,fdl
        return ty.name,fdl
    elif issubclass(tty,ast.IdentifierType):
        return name_to_ctype(' '.join(ty.names))
    elif issubclass(tty,ast.ArrayDecl):
        x=int(ty.dim.value)
        y=get_fields(ty.type)
        return x*y