本文整理汇总了Python中pycparser.c_ast.ArrayDecl方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.ArrayDecl方法的具体用法?Python c_ast.ArrayDecl怎么用?Python c_ast.ArrayDecl使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.ArrayDecl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: type_from_ctype
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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: parse_struct_member
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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
示例3: find_substruct_array
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
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
示例4: transform_multidim_to_1d_decl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
def transform_multidim_to_1d_decl(decl):
"""
Transform ast of multidimensional declaration to a single dimension declaration.
In-place operation!
Returns name and dimensions of array (to be used with transform_multidim_to_1d_ref())
"""
dims = []
type_ = decl.type
while type(type_) is c_ast.ArrayDecl:
dims.append(type_.dim)
type_ = type_.type
if dims:
# Multidimensional array
decl.type.dim = reduce(lambda l, r: c_ast.BinaryOp('*', l, r), dims)
decl.type.type = type_
return decl.name, dims
示例5: _explain_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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_arraydecl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
def ast_to_typeid_arraydecl(self, ast):
"""Return the CTypeBase of an ArrayDecl ast"""
target = self.ast_to_typeid(ast.type)
if ast.dim is None:
value = None
else:
value = self.ast_eval_int(ast.dim)
return CTypeArray(target, value)
示例7: ast_parse_arraydecl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
def ast_parse_arraydecl(self, ast):
"""Parse ast ArrayDecl"""
return self.ast_to_typeid(ast)
示例8: pointer_decay
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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
示例9: deref_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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
示例10: function_arg_size_align
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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
示例11: transform_array_decl_to_malloc
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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_
示例12: get_array_declarations
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
def get_array_declarations(self):
"""Return array 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.ArrayDecl]
示例13: _build_dummy_calls
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
def _build_dummy_calls(self):
"""
Generate false if branch with dummy calls
Requires kerncraft.h to be included, which defines dummy(...) and var_false.
:return: dummy statement
"""
# Make sure nothing gets removed by inserting dummy calls
dummy_calls = []
for d in self.kernel_ast.block_items:
# Only consider toplevel declarations from kernel ast
if type(d) is not c_ast.Decl: continue
if type(d.type) is c_ast.ArrayDecl:
dummy_calls.append(c_ast.FuncCall(
c_ast.ID('dummy'),
c_ast.ExprList([c_ast.ID(d.name)])))
else:
dummy_calls.append(c_ast.FuncCall(
c_ast.ID('dummy'),
c_ast.ExprList([c_ast.UnaryOp('&', c_ast.ID(d.name))])))
dummy_stmt = c_ast.If(
cond=c_ast.ID('var_false'),
iftrue=c_ast.Compound(dummy_calls),
iffalse=None)
return dummy_stmt
示例14: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [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,
})
示例15: get_ctypes_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import ArrayDecl [as 别名]
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