當前位置: 首頁>>代碼示例>>Python>>正文


Python c_ast.TypeDecl方法代碼示例

本文整理匯總了Python中pycparser.c_ast.TypeDecl方法的典型用法代碼示例。如果您正苦於以下問題:Python c_ast.TypeDecl方法的具體用法?Python c_ast.TypeDecl怎麽用?Python c_ast.TypeDecl使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pycparser.c_ast的用法示例。


在下文中一共展示了c_ast.TypeDecl方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: type_from_ctype

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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) 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:19,代碼來源:types.py

示例2: visit_Block

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def visit_Block(self, node, kind):
        name = node.name
        if not name:
            name = self.path_name(kind[0])
        if not node.decls:
            if self.child_of(c_ast.TypeDecl, -2):
                # not a definition, must be a reference
                self.append(name)
            return
        fields = self.collect(node)
        # add the struct/union definition to the top level
        self.decl_stack[0].append(Block(name, fields, kind))
        if self.child_of(c_ast.TypeDecl, -2):
            # inline struct/union, add a reference to whatever name it was
            # defined on the top level
            self.append(name) 
開發者ID:tarruda,項目名稱:python-autopxd,代碼行數:18,代碼來源:__init__.py

示例3: find_substruct_array

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:27,代碼來源:types.py

示例4: _explain_type

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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)) 
開發者ID:johncsnyder,項目名稱:SwiftKitten,代碼行數:32,代碼來源:cdecl.py

示例5: ast_to_typeid_typedecl

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def ast_to_typeid_typedecl(self, ast):
        """Return the CTypeBase of a TypeDecl ast"""
        return self.ast_to_typeid(ast.type) 
開發者ID:cea-sec,項目名稱:miasm,代碼行數:5,代碼來源:ctypesmngr.py

示例6: _find_typedecl

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def _find_typedecl(self, node):
        while not isinstance(node, c_ast.TypeDecl):
            node = node.type
        return node 
開發者ID:pyhandle,項目名稱:hpy,代碼行數:6,代碼來源:autogen.py

示例7: visit_Decl

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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) 
開發者ID:pyhandle,項目名稱:hpy,代碼行數:7,代碼來源:autogen.py

示例8: visit_Enum

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def visit_Enum(self, node):
        items = []
        if node.values:
            for item in node.values.enumerators:
                items.append(item.name)
        name = node.name
        type_decl = self.child_of(c_ast.TypeDecl, -2)
        if not name and type_decl:
            name = self.path_name('e')
        # add the enum definition to the top level
        if len(items):
            self.decl_stack[0].append(Enum(name, items))
        if type_decl:
            self.append(name) 
開發者ID:tarruda,項目名稱:python-autopxd,代碼行數:16,代碼來源:__init__.py

示例9: basic_type

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def basic_type(names: List[str]) -> TypeDecl:
    idtype = IdentifierType(names=names)
    return TypeDecl(declname=None, quals=[], type=idtype) 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:5,代碼來源:c_types.py

示例10: resolve_typedefs

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def resolve_typedefs(type: CType, typemap: TypeMap) -> CType:
    while (
        isinstance(type, TypeDecl)
        and isinstance(type.type, IdentifierType)
        and len(type.type.names) == 1
        and type.type.names[0] in typemap.typedefs
    ):
        type = typemap.typedefs[type.type.names[0]]
    return type 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:11,代碼來源:c_types.py

示例11: pointer_decay

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:14,代碼來源:c_types.py

示例12: is_void

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [as 別名]
def is_void(type: CType) -> bool:
    return (
        isinstance(type, ca.TypeDecl)
        and isinstance(type.type, ca.IdentifierType)
        and type.type.names == ["void"]
    ) 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:8,代碼來源:c_types.py

示例13: is_struct_type

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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)) 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:7,代碼來源:c_types.py

示例14: set_decl_name

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:8,代碼來源:c_types.py

示例15: type_to_string

# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import TypeDecl [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) 
開發者ID:matt-kempster,項目名稱:mips_to_c,代碼行數:10,代碼來源:c_types.py


注:本文中的pycparser.c_ast.TypeDecl方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。