Python pycparser.c_ast.PtrDecl() Examples
The following are 19
code examples of pycparser.c_ast.PtrDecl().
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 __str__(self) -> str: type = self.get_representative() size = type.size or 32 sign = "s" if type.sign & Type.SIGNED else "u" if type.kind == Type.K_ANY: if type.size is not None: return f"?{size}" return "?" if type.kind == Type.K_PTR: if type.ptr_to is not None: if isinstance(type.ptr_to, Type): return (str(type.ptr_to) + " *").replace("* *", "**") return type_to_string(ca.PtrDecl([], type.ptr_to)) return "void *" if type.kind == Type.K_FLOAT: return f"f{size}" return f"{sign}{size}"
Example #3
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 6 votes |
def parse_struct_member( type: CType, field_name: str, typemap: TypeMap ) -> Tuple[int, int, Optional[Struct]]: type = resolve_typedefs(type, typemap) if isinstance(type, PtrDecl): return 4, 4, None if isinstance(type, ArrayDecl): if type.dim is None: raise DecompFailure(f"Array field {field_name} must have a size") dim = parse_constant_int(type.dim) size, align, _ = parse_struct_member(type.type, field_name, typemap) return size * dim, align, None assert not isinstance(type, FuncDecl), "Struct can not contain a function" inner_type = type.type if isinstance(inner_type, (ca.Struct, ca.Union)): substr = parse_struct(inner_type, typemap) return substr.size, substr.align, substr # Otherwise it has to be of type Enum or IdentifierType size = primitive_size(inner_type) return size, size, None
Example #4
Source File: __init__.py From python-autopxd with MIT License | 6 votes |
def visit_FuncDecl(self, node): decls = self.collect(node) return_type = decls[-1].type_name fname = decls[-1].name args = decls[:-1] if (len(args) == 1 and isinstance(args[0], IdentifierType) and args[0].type_name == 'void'): args = [] if (self.child_of(c_ast.PtrDecl, -2) and not self.child_of(c_ast.Typedef, -3)): # declaring a variable or parameter name = self.path_name('ft'.format(fname)) self.decl_stack[0].append(Type(Ptr(Function(return_type, name, args)))) self.append(name) else: self.append(Function(return_type, fname, args))
Example #5
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 #6
Source File: map_hal2intercepts.py From halucinator with GNU General Public License v3.0 | 5 votes |
def get_function_name(node): ''' Gets the function name from ast node ''' if type(node.type) == c_ast.PtrDecl: return node.type.type.declname else: return node.type.declname
Example #7
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 #8
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def function_arg_size_align(type: CType, typemap: TypeMap) -> Tuple[int, int]: type = resolve_typedefs(type, typemap) if isinstance(type, PtrDecl) or isinstance(type, ArrayDecl): return 4, 4 assert not isinstance(type, FuncDecl), "Function argument can not be a function" inner_type = type.type if isinstance(inner_type, (ca.Struct, ca.Union)): struct = get_struct(inner_type, typemap) assert ( struct is not None ), "Function argument can not be of an incomplete struct" return struct.size, struct.align size = primitive_size(inner_type) return size, size
Example #9
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def deref_type(type: CType, typemap: TypeMap) -> CType: type = resolve_typedefs(type, typemap) assert isinstance(type, (ArrayDecl, PtrDecl)), "dereferencing non-pointer" return type.type
Example #10
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 #11
Source File: c_types.py From mips_to_c with GNU General Public License v3.0 | 5 votes |
def pointer(type: CType) -> CType: return PtrDecl(quals=[], type=type)
Example #12
Source File: autogen.py From hpy with MIT License | 5 votes |
def ctx_decl(self): # e.g. "HPy (*ctx_Module_Create)(HPyContext ctx, HPyModuleDef *def)" # # turn the function declaration into a function POINTER declaration newnode = deepcopy(self.node) newnode.type = c_ast.PtrDecl(type=newnode.type, quals=[]) # fix the name of the function pointer typedecl = self._find_typedecl(newnode) typedecl.declname = self.ctx_name() return toC(newnode)
Example #13
Source File: ctypesmngr.py From miasm with GNU General Public License v2.0 | 5 votes |
def ast_parse_ptrdecl(self, ast): """Parse ast PtrDecl""" return CTypePtr(self.ast_parse_declaration(ast.type))
Example #14
Source File: ctypesmngr.py From miasm with GNU General Public License v2.0 | 5 votes |
def ast_to_typeid_ptrdecl(self, ast): """Return the CTypeBase of a PtrDecl ast""" return CTypePtr(self.ast_to_typeid(ast.type))
Example #15
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 #16
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 #17
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 #18
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 #19
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, })