Python pycparser.c_ast.TypeDecl() Examples
The following are 26
code examples of pycparser.c_ast.TypeDecl().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pycparser.c_ast
, or try the search function
.

Example #1
Source File: types.py From mips_to_c with GNU General Public License v3.0 | 7 votes |
def type_from_ctype(ctype: CType, typemap: TypeMap) -> Type: ctype = resolve_typedefs(ctype, typemap) if isinstance(ctype, (ca.PtrDecl, ca.ArrayDecl)): return Type.ptr(ctype.type) if isinstance(ctype, ca.FuncDecl): return Type.ptr(ctype) if isinstance(ctype, ca.TypeDecl): if isinstance(ctype.type, (ca.Struct, ca.Union)): return Type.any() names = ["int"] if isinstance(ctype.type, ca.Enum) else ctype.type.names if "double" in names: return Type.f64() if "float" in names: return Type.f32() size = 8 * primitive_size(ctype.type) sign = Type.UNSIGNED if "unsigned" in names else Type.SIGNED return Type(kind=Type.K_INT, size=size, sign=sign)
Example #2
Source File: types.py From mips_to_c with GNU General Public License v3.0 | 6 votes |
def find_substruct_array( type: Type, offset: int, scale: int, typemap: TypeMap ) -> Optional[Tuple[str, int, CType]]: type = type.get_representative() if not type.ptr_to or isinstance(type.ptr_to, Type): return None ctype = resolve_typedefs(type.ptr_to, typemap) if not isinstance(ctype, ca.TypeDecl): return None if not isinstance(ctype.type, (ca.Struct, ca.Union)): return None struct = get_struct(ctype.type, typemap) if not struct: return None try: sub_offset = max(off for off in struct.fields.keys() if off <= offset) except ValueError: return None for field in struct.fields[sub_offset]: field_type = resolve_typedefs(field.type, typemap) if isinstance(field_type, ca.ArrayDecl): size = var_size_align(field_type.type, typemap)[0] if size == scale: return field.name, sub_offset, field_type.type return None
Example #3
Source File: __init__.py From python-autopxd with MIT License | 6 votes |
def visit_Block(self, node, kind): name = node.name if not name: name = self.path_name(kind[0]) if not node.decls: if self.child_of(c_ast.TypeDecl, -2): # not a definition, must be a reference self.append(name) return fields = self.collect(node) # add the struct/union definition to the top level self.decl_stack[0].append(Block(name, fields, kind)) if self.child_of(c_ast.TypeDecl, -2): # inline struct/union, add a reference to whatever name it was # defined on the top level self.append(name)
Example #4
Source File: cdecl.py From SwiftKitten with MIT License | 5 votes |
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: if decl.args: params = [_explain_type(param) for param in decl.args.params] args = ', '.join(params) else: args = '' return ('function(%s) returning ' % (args) + _explain_type(decl.type))
Example #5
Source File: ast_generalizer.py From transpyle with Apache License 2.0 | 5 votes |
def visit_Decl(self, node) -> t.Union[typed_ast3.AnnAssign, # pylint: disable=invalid-name t.Tuple[str, typed_ast3.arguments, typed_ast3.AST]]: """Transform Decl.""" name = node.name assert isinstance(name, str), type(name) quals = node.quals if quals: _LOG.warning('ignoring unsupported C grammar: %s', quals) storage = [self.visit(subnode) for subnode in node.storage] if storage: raise NotImplementedError(_node_debug(node.storage), str(storage)) funcspec = [self.visit(subnode) for subnode in node.funcspec] if funcspec: raise NotImplementedError(_node_debug(node.funcspec), str(funcspec)) type_data = self.visit(node.type) assert isinstance(type_data, tuple) assert len(type_data) == DECL_DATA_LENGTHS[type(node.type)], (type(node.type), type_data) init = self.visit(node.init) if init is not None: assert isinstance(node.type, INITIALIZABLE_DECLARATIONS) # assert isinstance(node.type, c_ast.TypeDecl), type(node.type) # raise NotImplementedError(_node_debug(node.init), str(init)) bitsize = self.visit(node.bitsize) if bitsize is not None: raise NotImplementedError(_node_debug(node.bitsize), str(bitsize)) _ = self.visit(node.coord) if init is not None or isinstance(node.type, INITIALIZABLE_DECLARATIONS): name_, type_ = type_data assert name_ == name return typed_ast3.AnnAssign(target=typed_ast3.Name(id=name_, ctx=typed_ast3.Store()), annotation=type_, value=init, simple=1) if isinstance(node.type, (c_ast.FuncDecl,)): return type_data return self.generic_visit(node)
Example #6
Source File: kernel.py From kerncraft with GNU Affero General Public License v3.0 | 5 votes |
def get_scalar_declarations(self): """Get all scalar declarations.""" return [d for d in self.kernel_ast.block_items if type(d) is c_ast.Decl and type(d.type) is c_ast.TypeDecl]
Example #7
Source File: kernel.py From kerncraft with GNU Affero General Public License v3.0 | 5 votes |
def _build_kernel_function_declaration(self, name='kernel'): """Build and return kernel function declaration""" array_declarations, array_dimensions = self._build_array_declarations(with_init=False) const_declarations = self._build_const_declartions(with_init=False) return c_ast.FuncDecl(args=c_ast.ParamList(params=array_declarations + const_declarations), type=c_ast.TypeDecl(declname=name, quals=[], type=c_ast.IdentifierType(names=['void'])))
Example #8
Source File: kernel.py From kerncraft with GNU Affero General Public License v3.0 | 5 votes |
def _build_const_declartions(self, with_init=True): """ Generate constants declarations :return: list of declarations """ decls = [] # Use type as provided by user in loop indices index_type = self.get_index_type() i = 2 # subscript for cli input, 1 is reserved for repeat for k in self.constants: # const long long N = strtoul(argv[2]) # with increasing N and 1 # TODO change subscript of argv depending on constant count type_decl = c_ast.TypeDecl(k.name, ['const'], c_ast.IdentifierType(index_type)) init = None if with_init: init = c_ast.FuncCall( c_ast.ID('atoi'), c_ast.ExprList([c_ast.ArrayRef(c_ast.ID('argv'), c_ast.Constant('int', str(i)))])) i += 1 decls.append(c_ast.Decl( k.name, ['const'], [], [], type_decl, init, None)) return decls
Example #9
Source File: kernel.py From kerncraft with GNU Affero General Public License v3.0 | 5 votes |
def transform_array_decl_to_malloc(decl, with_init=True): """ Transform ast of "type var_name[N]" to "type* var_name = aligned_malloc(sizeof(type)*N, 32)" In-place operation. :param with_init: if False, ommit malloc """ if type(decl.type) is not c_ast.ArrayDecl: # Not an array declaration, can be ignored return type_ = c_ast.PtrDecl([], decl.type.type) if with_init: decl.init = c_ast.FuncCall( c_ast.ID('aligned_malloc'), c_ast.ExprList([ c_ast.BinaryOp( '*', c_ast.UnaryOp( 'sizeof', c_ast.Typename(None, [], c_ast.TypeDecl( None, [], decl.type.type.type))), decl.type.dim), c_ast.Constant('int', '32')])) decl.type = type_
Example #10
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def type_to_string(type: CType) -> str: if isinstance(type, TypeDecl) and isinstance(type.type, (ca.Struct, ca.Union)): su = "struct" if isinstance(type.type, ca.Struct) else "union" return type.type.name or f"anon {su}" else: decl = ca.Decl("", [], [], [], type, None, None) set_decl_name(decl) return to_c(decl)
Example #11
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def set_decl_name(decl: ca.Decl) -> None: name = decl.name type = decl.type while not isinstance(type, TypeDecl): type = type.type type.declname = name
Example #12
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def is_struct_type(type: CType, typemap: TypeMap) -> bool: type = resolve_typedefs(type, typemap) if not isinstance(type, TypeDecl): return False return isinstance(type.type, (ca.Struct, ca.Union))
Example #13
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def is_void(type: CType) -> bool: return ( isinstance(type, ca.TypeDecl) and isinstance(type.type, ca.IdentifierType) and type.type.names == ["void"] )
Example #14
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def pointer_decay(type: CType, typemap: TypeMap) -> SimpleType: real_type = resolve_typedefs(type, typemap) if isinstance(real_type, ArrayDecl): return PtrDecl(quals=[], type=real_type.type) if isinstance(real_type, FuncDecl): return PtrDecl(quals=[], type=type) if isinstance(real_type, TypeDecl) and isinstance(real_type.type, ca.Enum): return basic_type(["int"]) assert not isinstance( type, (ArrayDecl, FuncDecl) ), "resolve_typedefs can't hide arrays/functions" return type
Example #15
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def resolve_typedefs(type: CType, typemap: TypeMap) -> CType: while ( isinstance(type, TypeDecl) and isinstance(type.type, IdentifierType) and len(type.type.names) == 1 and type.type.names[0] in typemap.typedefs ): type = typemap.typedefs[type.type.names[0]] return type
Example #16
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def basic_type(names: List[str]) -> TypeDecl: idtype = IdentifierType(names=names) return TypeDecl(declname=None, quals=[], type=idtype)
Example #17
Source File: __init__.py From python-autopxd with MIT License | 5 votes |
def visit_Enum(self, node): items = [] if node.values: for item in node.values.enumerators: items.append(item.name) name = node.name type_decl = self.child_of(c_ast.TypeDecl, -2) if not name and type_decl: name = self.path_name('e') # add the enum definition to the top level if len(items): self.decl_stack[0].append(Enum(name, items)) if type_decl: self.append(name)
Example #18
Source File: autogen.py From hpy with MIT License | 5 votes |
def visit_Decl(self, node): if isinstance(node.type, c_ast.FuncDecl): self._visit_function(node) elif isinstance(node.type, c_ast.TypeDecl): self._visit_global_var(node)
Example #19
Source File: autogen.py From hpy with MIT License | 5 votes |
def _find_typedecl(self, node): while not isinstance(node, c_ast.TypeDecl): node = node.type return node
Example #20
Source File: ctypesmngr.py From miasm with GNU General Public License v2.0 | 5 votes |
def ast_to_typeid_typedecl(self, ast): """Return the CTypeBase of a TypeDecl ast""" return self.ast_to_typeid(ast.type)
Example #21
Source File: types.py From mips_to_c with GNU General Public License v3.0 | 4 votes |
def get_field( type: Type, offset: int, typemap: TypeMap, *, target_size: Optional[int] ) -> Tuple[Optional[str], Type, Type, bool]: """Returns field name, target type, target pointer type, and whether the field is an array.""" if target_size is None and offset == 0: # We might as well take a pointer to the whole struct target = get_pointer_target(type, typemap) target_type = target[1] if target else Type.any() return None, target_type, type, False type = type.get_representative() if not type.ptr_to or isinstance(type.ptr_to, Type): return None, Type.any(), Type.ptr(), False ctype = resolve_typedefs(type.ptr_to, typemap) if isinstance(ctype, ca.TypeDecl) and isinstance(ctype.type, (ca.Struct, ca.Union)): struct = get_struct(ctype.type, typemap) if struct: fields = struct.fields.get(offset) if fields: # Ideally, we should use target_size and the target pointer type to # determine which struct field to use if there are multiple at the # same offset (e.g. if a struct starts here, or we have a union). # For now though, we just use target_size as a boolean signal -- if # it's known we take an arbitrary subfield that's as concrete as # possible, if unknown we prefer a whole substruct. (The latter case # happens when taking pointers to fields -- pointers to substructs are # more common and can later be converted to concrete field pointers.) if target_size is None: # Structs will be placed first in the field list. field = fields[0] else: # Pick the first subfield in case of unions. correct_size_fields = [f for f in fields if f.size == target_size] if len(correct_size_fields) == 1: field = correct_size_fields[0] else: ind = 0 while ind + 1 < len(fields) and fields[ind + 1].name.startswith( fields[ind].name + "." ): ind += 1 field = fields[ind] return ( field.name, type_from_ctype(field.type, typemap), *ptr_type_from_ctype(field.type, typemap), ) return None, Type.any(), Type.ptr(), False
Example #22
Source File: ctypesmngr.py From miasm with GNU General Public License v2.0 | 4 votes |
def __init__(self, knowntypes=None, knowntypedefs=None): if knowntypes is None: knowntypes = {} if knowntypedefs is None: knowntypedefs = {} 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, c_ast.FuncDecl: self.ast_parse_funcdecl, c_ast.FuncDef: self.ast_parse_funcdef, c_ast.Pragma: self.ast_parse_pragma, }
Example #23
Source File: extract_ptrace_constants.py From ptracer with Apache License 2.0 | 4 votes |
def render_type(typedecl): res = [] if isinstance(typedecl, (c_ast.TypeDecl, c_ast.Typename)): res.extend(typedecl.quals) res.extend(render_type(typedecl.type)) elif isinstance(typedecl, c_ast.PtrDecl): res.extend(typedecl.quals) res.extend(render_type(typedecl.type)) res.append('*') elif isinstance(typedecl, c_ast.IdentifierType): res.extend(typedecl.names) elif isinstance(typedecl, c_ast.Struct): res.extend(['struct', typedecl.name]) elif isinstance(typedecl, c_ast.Union): res.extend(['union', typedecl.name]) elif isinstance(typedecl, (c_ast.FuncDecl, ext_c_parser.FuncDeclExt)): ret = render_type(typedecl.type) args = [] for param in typedecl.args.params: args.append(' '.join(render_type(param))) ret.append('({})'.format(', '.join(args))) res.extend(ret) elif isinstance(typedecl, c_ast.ArrayDecl): res.extend(render_type(typedecl.type)) if typedecl.dim is None: res.append('[]') elif isinstance(typedecl.dim, c_ast.Constant): res.append('[{}]'.format(typedecl.dim.value)) else: die('non-constant dimension in array declaration') else: die('unexpected {!r}'.format(typedecl)) return res
Example #24
Source File: extract_ptrace_constants.py From ptracer with Apache License 2.0 | 4 votes |
def get_ctypes_type(typedecl, typedefs): ptr_indirection = 0 if isinstance(typedecl, c_ast.TypeDecl): if isinstance(typedecl.type, c_ast.IdentifierType): tnames = typedecl.type.names while True: if ((len(tnames) == 1 and tnames[0] in typedefs) or (tnames[-1] in typedefs and tnames[-2] not in {'struct', 'union'})): tnames = list(tnames[:-1]) + list(typedefs[tnames[-1]]) else: break ptr_indirection = 1 if tnames[-1] == '*' else 0 if ptr_indirection: tnames = tnames[:-1] if len(tnames) > 1 and tnames[-2] == 'struct': ctype = 'ctypes.c_void_p' ptr_indirection = 0 elif len(tnames) > 1 and tnames[-2] == 'union': ctype = 'ctypes.c_void_p' ptr_indirection = 0 else: ctype = ctype_map.get(tuple(tnames)) if ctype is None: die('unrecognized C type: {}'.format(' '.join(tnames))) elif isinstance(typedecl.type, c_ast.Struct): ctype = 'ctypes.c_void_p' elif isinstance(typedecl.type, c_ast.Union): ctype = 'ctypes.c_void_p' else: die('unexpected syntax in type declaration: {!r}'.format( typedecl.type)) elif isinstance(typedecl, c_ast.PtrDecl): ctype, ptr_indirection = get_ctypes_type( typedecl.type, typedefs) if ctype != 'ctypes.c_void_p': ptr_indirection += 1 elif isinstance(typedecl, c_ast.ArrayDecl): array_type, ptr_indirection = get_ctypes_type(typedecl.type, typedefs) dim = fold_const_expr(typedecl.dim, typedefs) ctype = '{} * {}'.format(array_type, dim) else: die('unexpected syntax in type declaration: {!r}'.format( typedecl)) return ctype, ptr_indirection
Example #25
Source File: cparse.py From nightmare with GNU General Public License v2.0 | 4 votes |
def __init__(self, psize=4, bigend=False): self.psize = psize self.pclass = vs_prim.v_ptr32 self.cls_parsers = { c_ast.Decl: self.c_getVsDecl, c_ast.Struct: self.c_getVsStruct, c_ast.FileAST: self.c_getFileAst, c_ast.PtrDecl: self.c_getPointer, #c_ast.FuncDecl: self.c_getFuncDecl, c_ast.Constant: self.c_getConstant, c_ast.TypeDecl: self.c_getVsType, c_ast.ArrayDecl: self.c_getVsArray, c_ast.IdentifierType: self.c_getIdentType, } self.vs_ctypes = { ('char',): vs_prim.v_int8, ('unsigned','char'): vs_prim.v_uint8, ('short',): vs_prim.v_int16, ('short','int'): vs_prim.v_int16, ('unsigned', 'short',): vs_prim.v_uint16, ('unsigned', 'short','int'):vs_prim.v_uint16, ('int',): vs_prim.v_int32, ('unsigned','int',): vs_prim.v_uint32, ('long',): vs_prim.v_int32, ('long','int'): vs_prim.v_int32, ('unsigned','long',): vs_prim.v_uint32, ('unsigned','long','int'): vs_prim.v_uint32, } if psize == 8: self.pclass = vs_prim.v_ptr64 self.vs_ctypes.update({ ('long',): vs_prim.v_int64, ('long','int'): vs_prim.v_int64, ('unsigned','long',): vs_prim.v_uint64, ('unsigned','long','int'): vs_prim.v_uint64, })
Example #26
Source File: cparse.py From CanCat with BSD 2-Clause "Simplified" License | 4 votes |
def __init__(self, psize=4, bigend=False): self.psize = psize self.pclass = vs_prim.v_ptr32 self.cls_parsers = { c_ast.Decl: self.c_getVsDecl, c_ast.Struct: self.c_getVsStruct, c_ast.FileAST: self.c_getFileAst, c_ast.PtrDecl: self.c_getPointer, #c_ast.FuncDecl: self.c_getFuncDecl, c_ast.Constant: self.c_getConstant, c_ast.TypeDecl: self.c_getVsType, c_ast.ArrayDecl: self.c_getVsArray, c_ast.IdentifierType: self.c_getIdentType, } self.vs_ctypes = { ('char',): vs_prim.v_int8, ('unsigned','char'): vs_prim.v_uint8, ('short',): vs_prim.v_int16, ('short','int'): vs_prim.v_int16, ('unsigned', 'short',): vs_prim.v_uint16, ('unsigned', 'short','int'):vs_prim.v_uint16, ('int',): vs_prim.v_int32, ('unsigned','int',): vs_prim.v_uint32, ('long',): vs_prim.v_int32, ('long','int'): vs_prim.v_int32, ('unsigned','long',): vs_prim.v_uint32, ('unsigned','long','int'): vs_prim.v_uint32, } if psize == 8: self.pclass = vs_prim.v_ptr64 self.vs_ctypes.update({ ('long',): vs_prim.v_int64, ('long','int'): vs_prim.v_int64, ('unsigned','long',): vs_prim.v_uint64, ('unsigned','long','int'): vs_prim.v_uint64, })