本文整理汇总了Python中pycparser.c_ast.NodeVisitor方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.NodeVisitor方法的具体用法?Python c_ast.NodeVisitor怎么用?Python c_ast.NodeVisitor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.NodeVisitor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_typemap
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import NodeVisitor [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