本文整理汇总了Python中pycparser.c_ast.Decl方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.Decl方法的具体用法?Python c_ast.Decl怎么用?Python c_ast.Decl使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.Decl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: explain_c_declaration
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def explain_c_declaration(c_decl):
""" Parses the declaration in c_decl and returns a text
explanation as a string.
The last external node of the string is used, to allow
earlier typedefs for used types.
"""
parser = c_parser.CParser()
try:
node = parser.parse(c_decl, filename='<stdin>')
except c_parser.ParseError:
e = sys.exc_info()[1]
return "Parse error:" + str(e)
if (not isinstance(node, c_ast.FileAST) or
not isinstance(node.ext[-1], c_ast.Decl)
):
return "Not a valid declaration"
return _explain_decl_node(node.ext[-1])
示例2: read_ptrace_h
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def read_ptrace_h(path, include_path):
text = get_header_text(path, include_path)
parser = ext_c_parser.GnuCParser()
fileast = parser.parse(text, path)
if not isinstance(fileast, c_ast.FileAST):
die('could not parse user.h')
output = ['\n']
for decl in fileast.ext:
if (not isinstance(decl, c_ast.Decl) or
not isinstance(decl.type, c_ast.Enum)):
continue
for item in decl.type.values.enumerators:
if item.name.startswith('PTRACE_'):
output.append('{} = {}'.format(
item.name, render_const(item.value)))
typedefs = parse_typedefs(fileast)
structs = {'__ptrace_peeksiginfo_args'}
output.extend(parse_structs(fileast, structs, typedefs))
return output
示例3: parse_structs
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [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
示例4: _explain_decl_node
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def _explain_decl_node(decl_node):
""" Receives a c_ast.Decl note and returns its explanation in
English.
"""
storage = ' '.join(decl_node.storage) + ' ' if decl_node.storage else ''
return (decl_node.name +
" is a " +
storage +
_explain_type(decl_node.type))
示例5: _explain_type
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [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_decl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def ast_to_typeid_decl(self, ast):
"""Return the CTypeBase of a Decl ast"""
return self.ast_to_typeid(ast.type)
示例7: ast_parse_decl
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def ast_parse_decl(self, ast):
"""Parse ast Decl"""
return self.ast_parse_declaration(ast.type)
示例8: parse_function
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [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: set_decl_name
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def set_decl_name(decl: ca.Decl) -> None:
name = decl.name
type = decl.type
while not isinstance(type, TypeDecl):
type = type.type
type.declname = name
示例10: type_to_string
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [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)
示例11: _build_const_declartions
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def _build_const_declartions(self, with_init=True):
"""
Generate constants declarations
:return: list of declarations
"""
decls = []
# Use type as provided by user in loop indices
index_type = self.get_index_type()
i = 2 # subscript for cli input, 1 is reserved for repeat
for k in self.constants:
# const long long N = strtoul(argv[2])
# with increasing N and 1
# TODO change subscript of argv depending on constant count
type_decl = c_ast.TypeDecl(k.name, ['const'], c_ast.IdentifierType(index_type))
init = None
if with_init:
init = c_ast.FuncCall(
c_ast.ID('atoi'),
c_ast.ExprList([c_ast.ArrayRef(c_ast.ID('argv'),
c_ast.Constant('int', str(i)))]))
i += 1
decls.append(c_ast.Decl(
k.name, ['const'], [], [],
type_decl, init, None))
return decls
示例12: get_array_declarations
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [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 Decl [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: get_scalar_declarations
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [as 别名]
def get_scalar_declarations(self):
"""Get all scalar 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.TypeDecl]
示例15: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import Decl [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,
})