本文整理汇总了Python中pycparser.c_ast.FuncDecl方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.FuncDecl方法的具体用法?Python c_ast.FuncDecl怎么用?Python c_ast.FuncDecl使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.FuncDecl方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ast_to_typeid_funcdecl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
def ast_to_typeid_funcdecl(self, ast):
"""Return the CTypeBase of an FuncDecl ast"""
type_ret = self.ast_to_typeid(ast.type)
name, decl_info = self.get_funcname(ast.type)
if ast.args:
args = []
for arg in ast.args.params:
typeid = self.ast_to_typeid(arg)
if isinstance(typeid, CTypeEllipsis):
arg_name = None
else:
arg_name = arg.name
args.append((arg_name, typeid))
else:
args = []
obj = CTypeFunc(name, decl_info, type_ret, args)
decl = CTypeFunc(name)
if not self.is_known_type(decl):
self.add_type(decl, obj)
return obj
示例2: parse_struct_member
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [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: _explain_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [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))
示例4: ast_parse_funcdecl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
def ast_parse_funcdecl(self, ast):
"""Parse ast FuncDecl"""
return self.ast_to_typeid(ast)
示例5: visit_Decl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
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)
示例6: pointer_decay
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [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
示例7: function_arg_size_align
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [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
示例8: parse_function
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
def parse_function(fn: FuncDecl) -> Function:
params: List[Param] = []
is_variadic = False
has_void = False
if fn.args:
for arg in fn.args.params:
if isinstance(arg, ca.EllipsisParam):
is_variadic = True
elif isinstance(arg, ca.Decl):
params.append(Param(type=arg.type, name=arg.name))
elif isinstance(arg, ca.ID):
raise DecompFailure(
"K&R-style function header is not supported: " + to_c(fn)
)
else:
assert isinstance(arg, ca.Typename)
if is_void(arg.type):
has_void = True
else:
params.append(Param(type=arg.type, name=None))
maybe_params: Optional[List[Param]] = params
if not params and not has_void and not is_variadic:
# Function declaration without a parameter list
maybe_params = None
ret_type = None if is_void(fn.type) else fn.type
return Function(ret_type=ret_type, params=maybe_params, is_variadic=is_variadic)
示例9: _build_kernel_function_declaration
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
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'])))
示例10: visit_Decl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
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)
示例11: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [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,
})
示例12: render_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
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
示例13: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
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,
}
示例14: build_typemap
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FuncDecl [as 别名]
def build_typemap(source: str) -> TypeMap:
source = add_builtin_typedefs(source)
source = strip_comments(source)
ast: ca.FileAST = parse_c(source)
ret = TypeMap()
for item in ast.ext:
if isinstance(item, ca.Typedef):
ret.typedefs[item.name] = item.type
if isinstance(item, ca.FuncDef):
assert item.decl.name is not None, "cannot define anonymous function"
assert isinstance(item.decl.type, FuncDecl)
ret.functions[item.decl.name] = parse_function(item.decl.type)
if isinstance(item, ca.Decl) and isinstance(item.type, FuncDecl):
assert item.name is not None, "cannot define anonymous function"
ret.functions[item.name] = parse_function(item.type)
defined_function_decls: Set[ca.Decl] = set()
class Visitor(ca.NodeVisitor):
def visit_Struct(self, struct: ca.Struct) -> None:
if struct.decls is not None:
parse_struct(struct, ret)
def visit_Union(self, union: ca.Union) -> None:
if union.decls is not None:
parse_struct(union, ret)
def visit_Decl(self, decl: ca.Decl) -> None:
if decl.name is not None:
ret.var_types[decl.name] = decl.type
if not isinstance(decl.type, FuncDecl):
self.visit(decl.type)
def visit_Enum(self, enum: ca.Enum) -> None:
if enum.name is not None:
ret.typedefs[enum.name] = basic_type(["int"])
def visit_FuncDef(self, fn: ca.FuncDef) -> None:
if fn.decl.name is not None:
ret.var_types[fn.decl.name] = fn.decl.type
Visitor().visit(ast)
return ret