Files
TransPyC/lib/core/Exportable.py

178 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""
导出表模块 - 用于收集和存储文件的公开符号信息
"""
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any
@dataclass
class EnumMember:
"""枚举成员信息"""
name: str
value: int
lineno: int
@dataclass
class EnumInfo:
"""枚举类型信息"""
name: str
members: List[EnumMember] = field(default_factory=list)
lineno: int = 0
is_public: bool = True
@dataclass
class FunctionParam:
"""函数参数信息"""
name: str
type_name: str
is_pointer: bool = False
@dataclass
class FunctionInfo:
"""函数信息"""
name: str
return_type: str
params: List[FunctionParam] = field(default_factory=list)
lineno: int = 0
is_public: bool = True
@dataclass
class StructMember:
"""结构体成员信息"""
name: str
type_name: str
is_pointer: bool = False
array_size: Optional[int] = None
@dataclass
class StructInfo:
"""结构体类型信息"""
name: str
members: List[StructMember] = field(default_factory=list)
lineno: int = 0
is_public: bool = True
@dataclass
class TypedefInfo:
"""类型别名信息"""
name: str
original_type: str
lineno: int = 0
is_public: bool = True
@dataclass
class Exportable:
"""导出表 - 存储文件的所有公开符号"""
filename: str
enums: List[EnumInfo] = field(default_factory=list)
functions: List[FunctionInfo] = field(default_factory=list)
structs: List[StructInfo] = field(default_factory=list)
typedefs: List[TypedefInfo] = field(default_factory=list)
def add_enum(self, name: str, lineno: int, is_public: bool = True) -> EnumInfo:
"""添加枚举类型"""
enum_info = EnumInfo(name=name, lineno=lineno, is_public=is_public)
self.enums.append(enum_info)
return enum_info
def add_function(self, name: str, return_type: str, lineno: int, is_public: bool = True) -> FunctionInfo:
"""添加函数"""
func_info = FunctionInfo(name=name, return_type=return_type, lineno=lineno, is_public=is_public)
self.functions.append(func_info)
return func_info
def add_struct(self, name: str, lineno: int, is_public: bool = True) -> StructInfo:
"""添加结构体"""
struct_info = StructInfo(name=name, lineno=lineno, is_public=is_public)
self.structs.append(struct_info)
return struct_info
def add_typedef(self, name: str, original_type: str, lineno: int, is_public: bool = True) -> TypedefInfo:
"""添加类型别名"""
typedef_info = TypedefInfo(name=name, original_type=original_type, lineno=lineno, is_public=is_public)
self.typedefs.append(typedef_info)
return typedef_info
def to_dict(self) -> Dict[str, Any]:
"""转换为字典格式"""
return {
'filename': self.filename,
'enums': [
{
'name': enum.name,
'lineno': enum.lineno,
'is_public': enum.is_public,
'members': [
{'name': m.name, 'value': m.value, 'lineno': m.lineno}
for m in enum.members
]
}
for enum in self.enums
],
'functions': [
{
'name': func.name,
'return_type': func.return_type,
'lineno': func.lineno,
'is_public': func.is_public,
'params': [
{'name': p.name, 'type_name': p.type_name, 'is_pointer': p.is_pointer}
for p in func.params
]
}
for func in self.functions
],
'structs': [
{
'name': struct.name,
'lineno': struct.lineno,
'is_public': struct.is_public,
'members': [
{
'name': m.name,
'type_name': m.type_name,
'is_pointer': m.is_pointer,
'array_size': m.array_size
}
for m in struct.members
]
}
for struct in self.structs
],
'typedefs': [
{
'name': typedef.name,
'original_type': typedef.original_type,
'lineno': typedef.lineno,
'is_public': typedef.is_public
}
for typedef in self.typedefs
]
}
def merge(self, other: 'Exportable') -> None:
"""合并另一个导出表"""
self.enums.extend(other.enums)
self.functions.extend(other.functions)
self.structs.extend(other.structs)
self.typedefs.extend(other.typedefs)
def get_public_symbols(self) -> 'Exportable':
"""获取仅包含公开符号的导出表"""
result = Exportable(filename=self.filename)
result.enums = [e for e in self.enums if e.is_public]
result.functions = [f for f in self.functions if f.is_public]
result.structs = [s for s in self.structs if s.is_public]
result.typedefs = [t for t in self.typedefs if t.is_public]
return result