本文整理汇总了Python中pycparser.c_ast.Typename方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.Typename方法的具体用法?Python c_ast.Typename怎么用?Python c_ast.Typename使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.Typename方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _explain_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [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))
示例2: fold_const_expr
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [as 别名]
def fold_const_expr(expr, typedefs):
if isinstance(expr, c_ast.BinaryOp):
left = fold_const_expr(expr.left, typedefs)
right = fold_const_expr(expr.right, typedefs)
oper = _binopmap.get(expr.op)
if oper is None:
die('cannot fold binop with {!r}'.format(expr.op))
result = oper(left, right)
elif isinstance(expr, c_ast.UnaryOp):
operand = fold_const_expr(expr.expr, typedefs)
oper = _unopmap.get(expr.op)
if oper is None:
die('cannot fold unop with {!r}'.format(expr.op))
result = oper(operand)
elif isinstance(expr, c_ast.Constant):
lit_type = _literalmap.get(expr.type)
if lit_type is None:
die('unexpected constant type: {!r}'.format(expr.type))
result = lit_type(expr.value)
elif isinstance(expr, c_ast.Typename):
# sizeof operand
result = get_final_ctypes_type(expr.type, typedefs)
_, _, typ = result.rpartition('.')
result = getattr(ctypes, typ)
else:
die('cannot fold {!r} expr'.format(expr))
return result
示例3: parse_function
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [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)
示例4: transform_array_decl_to_malloc
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [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_
示例5: render_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [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
示例6: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [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,
}
示例7: ast_eval_int
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Typename [as 别名]
def ast_eval_int(self, ast):
"""Eval a C ast object integer
@ast: parsed pycparser.c_ast object
"""
if isinstance(ast, c_ast.BinaryOp):
left = self.ast_eval_int(ast.left)
right = self.ast_eval_int(ast.right)
is_pure_int = (isinstance(left, int) and
isinstance(right, int))
if is_pure_int:
if ast.op == '*':
result = left * right
elif ast.op == '/':
assert left % right == 0
result = left // right
elif ast.op == '+':
result = left + right
elif ast.op == '-':
result = left - right
elif ast.op == '<<':
result = left << right
elif ast.op == '>>':
result = left >> right
else:
raise NotImplementedError("Not implemented!")
else:
result = CTypeOp(ast.op, left, right)
elif isinstance(ast, c_ast.UnaryOp):
if ast.op == 'sizeof' and isinstance(ast.expr, c_ast.Typename):
subobj = self.ast_to_typeid(ast.expr)
result = CTypeSizeof(subobj)
else:
raise NotImplementedError("Not implemented!")
elif isinstance(ast, c_ast.Constant):
result = int(ast.value, 0)
elif isinstance(ast, c_ast.Cast):
# TODO: Can trunc integers?
result = self.ast_eval_int(ast.expr)
else:
raise NotImplementedError("Not implemented!")
return result