本文整理汇总了Python中pycparser.c_ast.Struct方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.Struct方法的具体用法?Python c_ast.Struct怎么用?Python c_ast.Struct使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.Struct方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: type_from_ctype
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [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_structs
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def parse_structs(fileast, structs, typedefs):
output = []
for decl in fileast.ext:
if ((not isinstance(decl, c_ast.Decl) or
not isinstance(decl.type, c_ast.Struct)) and
not isinstance(decl, c_ast.Typedef)):
continue
struct, struct_name = get_struct_and_name(decl)
if struct_name not in structs:
continue
definitions = parse_struct(decl, typedefs)
for name, base, fields in definitions:
output.append('\n\nclass {}({}):'.format(name, base))
output.append(' _fields_ = (')
for field_name, field_type in fields:
output.append(' ({!r}, {}),'.format(
field_name, field_type))
output.append(' )')
return output
示例3: parse_struct_member
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [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: find_substruct_array
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [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
示例5: parse_struct
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def parse_struct(decl, typedefs, is_union=False):
definitions = []
struct, struct_name = get_struct_and_name(decl)
fields = []
for field_decl in struct.decls:
if isinstance(field_decl.type.type, c_ast.Union):
definitions.extend(
parse_struct(field_decl.type, typedefs, is_union=True))
ctype = definitions[-1][0]
elif isinstance(field_decl.type.type, c_ast.Struct):
definitions.extend(
parse_struct(field_decl.type, typedefs))
ctype = definitions[-1][0]
else:
ctype = get_final_ctypes_type(field_decl.type, typedefs)
fields.append((field_decl.name, ctype))
base = 'ctypes.Union' if is_union else 'ctypes.Structure'
definitions.append((struct_name, base, fields))
return definitions
示例6: __str__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def __str__(self):
out = []
out.append("<Struct:%s>" % self.name)
for name, field in self.fields:
out.append("\t%-10s %s" % (name, field))
return '\n'.join(out)
示例7: ast_to_typeid_struct
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def ast_to_typeid_struct(self, ast):
"""Return the CTypeBase of an Struct ast"""
name = self.gen_uniq_name() if ast.name is None else ast.name
args = []
if ast.decls:
for arg in ast.decls:
if arg.name is None:
arg_name = self.gen_anon_name()
else:
arg_name = arg.name
args.append((arg_name, self.ast_to_typeid(arg)))
decl = CTypeStruct(name, args)
return decl
示例8: ast_parse_struct
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def ast_parse_struct(self, ast):
"""Parse ast Struct"""
obj = self.ast_to_typeid(ast)
if ast.decls and ast.name is not None:
# Add struct to types if named
decl = CTypeStruct(ast.name)
if not self.is_known_type(decl):
self.add_type(decl, obj)
return obj
示例9: function_arg_size_align
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [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
示例10: is_struct_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
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))
示例11: get_struct
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def get_struct(
struct: Union[ca.Struct, ca.Union], typemap: TypeMap
) -> Optional[Struct]:
if struct.name:
return typemap.named_structs.get(struct.name)
else:
return typemap.anon_structs.get(id(struct))
示例12: parse_struct
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
def parse_struct(struct: Union[ca.Struct, ca.Union], typemap: TypeMap) -> Struct:
existing = get_struct(struct, typemap)
if existing:
return existing
if struct.decls is None:
raise DecompFailure(f"Tried to use struct {struct.name} before it is defined.")
ret = do_parse_struct(struct, typemap)
if struct.name:
typemap.named_structs[struct.name] = ret
else:
typemap.anon_structs[id(struct)] = ret
return ret
示例13: type_to_string
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [as 别名]
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)
示例14: get_ctypes_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [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
示例15: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Struct [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,
})