本文整理匯總了Python中pycparser.c_ast.PtrDecl方法的典型用法代碼示例。如果您正苦於以下問題:Python c_ast.PtrDecl方法的具體用法?Python c_ast.PtrDecl怎麽用?Python c_ast.PtrDecl使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.PtrDecl方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: type_from_ctype
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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)
示例2: visit_FuncDecl
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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))
示例3: parse_struct_member
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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
示例4: __str__
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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}"
示例5: _explain_type
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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))
示例6: ast_to_typeid_ptrdecl
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
def ast_to_typeid_ptrdecl(self, ast):
"""Return the CTypeBase of a PtrDecl ast"""
return CTypePtr(self.ast_to_typeid(ast.type))
示例7: ast_parse_ptrdecl
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
def ast_parse_ptrdecl(self, ast):
"""Parse ast PtrDecl"""
return CTypePtr(self.ast_parse_declaration(ast.type))
示例8: ctx_decl
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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)
示例9: pointer
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
def pointer(type: CType) -> CType:
return PtrDecl(quals=[], type=type)
示例10: pointer_decay
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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
示例11: deref_type
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
def deref_type(type: CType, typemap: TypeMap) -> CType:
type = resolve_typedefs(type, typemap)
assert isinstance(type, (ArrayDecl, PtrDecl)), "dereferencing non-pointer"
return type.type
示例12: function_arg_size_align
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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
示例13: transform_array_decl_to_malloc
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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_
示例14: get_function_name
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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
示例15: __init__
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import PtrDecl [as 別名]
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,
})