refactor(tpc): 退役 t/c 模块并将定宽类型升级为内建
删除 lib/includes/t.py、c.py,类型模型迁入 lib/core/TypeSystem.py;移除 IsTModule/_T_MODULE_NAMES 等名称判据,语法糖与内部标记统一为裸名;builtins 的 sizet/intN/uintN/fast/least/uchar/short/long 等改为编译器内建短名。
This commit is contained in:
1 parent
8d27747c7c
commit
f19d25782f
66 files changed
+2445
-3638
No files matched your search
+33
-36
@@ -10,9 +10,9 @@ from lib.core.SymbolData import (
|
||||
FuncSymbolData, DefineSymbolData, AnonymousTypeData
|
||||
)
|
||||
from lib.core.Handles.HandlesBase import CTypeInfo
|
||||
from lib.core.SymbolUtils import AnnotationContainsTType, CheckAnnotationHasCInline, IsTModule, AnnotationContainsName
|
||||
from lib.core.SymbolUtils import AnnotationContainsMarker, CheckAnnotationHasCInline, AnnotationContainsName
|
||||
from lib.core.SymbolUtils import EnumClassIntWidth, _ENUM_DEFAULT_INT_WIDTH
|
||||
from lib.includes import t
|
||||
from lib.core import TypeSystem
|
||||
|
||||
|
||||
class ASTSymbolExtractor:
|
||||
@@ -62,7 +62,7 @@ class ASTSymbolExtractor:
|
||||
member_type: CTypeInfo | None = CTypeInfo.FromNode(item.annotation, self._symtab)
|
||||
if member_type is None:
|
||||
member_type = CTypeInfo()
|
||||
member_type.BaseType = t.CInt()
|
||||
member_type.BaseType = TypeSystem.CInt()
|
||||
is_ptr: bool = member_type.IsPtr
|
||||
if isinstance(item.annotation, ast.BinOp):
|
||||
if AnnotationContainsName(item.annotation, 'CPtr'):
|
||||
@@ -71,24 +71,24 @@ class ASTSymbolExtractor:
|
||||
|
||||
elif isinstance(item, ast.ClassDef):
|
||||
is_anonymous: bool = any(
|
||||
any(AnnotationContainsTType(base, 'Anonymous') for base in item.bases)
|
||||
any(AnnotationContainsMarker(base, 'Anonymous') for base in item.bases)
|
||||
) if item.bases else False
|
||||
|
||||
if is_anonymous:
|
||||
is_union: bool = any(
|
||||
any(AnnotationContainsTType(base, 'CUnion') for base in item.bases)
|
||||
any(AnnotationContainsMarker(base, 'CUnion') for base in item.bases)
|
||||
)
|
||||
# 也检查装饰器形式 @t.CUnion
|
||||
# 也检查装饰器形式 @CUnion
|
||||
if not is_union and hasattr(item, 'decorator_list') and item.decorator_list:
|
||||
for deco in item.decorator_list:
|
||||
if isinstance(deco, ast.Attribute) and hasattr(deco.value, 'id') and IsTModule(deco.value.id, self._symtab) and deco.attr == 'CUnion':
|
||||
if isinstance(deco, ast.Attribute) and hasattr(deco.value, 'id') and deco.attr == 'CUnion':
|
||||
is_union = True
|
||||
break
|
||||
elif isinstance(deco, ast.Name):
|
||||
cls2: type | None = getattr(t, deco.id, None)
|
||||
if cls2 is None and hasattr(self._symtab, '_t_type_symbols'):
|
||||
cls2 = self._symtab._t_type_symbols.get(deco.id)
|
||||
if cls2 is not None and isinstance(cls2, type) and issubclass(cls2, t.CUnion):
|
||||
cls2: type | None = getattr(TypeSystem, deco.id, None)
|
||||
if cls2 is None and hasattr(self._symtab, '_builtin_type_symbols'):
|
||||
cls2 = self._symtab._builtin_type_symbols.get(deco.id)
|
||||
if cls2 is not None and isinstance(cls2, type) and issubclass(cls2, TypeSystem.CUnion):
|
||||
is_union = True
|
||||
break
|
||||
|
||||
@@ -129,8 +129,7 @@ class ASTSymbolExtractor:
|
||||
if hasattr(node, 'decorator_list') and node.decorator_list:
|
||||
for decorator in node.decorator_list:
|
||||
if isinstance(decorator, ast.Attribute):
|
||||
if (hasattr(decorator.value, 'id') and
|
||||
IsTModule(decorator.value.id, self._symtab)):
|
||||
if (hasattr(decorator.value, 'id') ):
|
||||
if decorator.attr == 'Object':
|
||||
is_cpython_object = True
|
||||
elif decorator.attr == 'CUnion':
|
||||
@@ -143,44 +142,42 @@ class ASTSymbolExtractor:
|
||||
type_kind = 'struct'
|
||||
elif isinstance(decorator, ast.Name):
|
||||
# from t import CStruct / CUnion / CEnum / REnum
|
||||
cls: type | None = getattr(t, decorator.id, None)
|
||||
if cls is None and hasattr(self._symtab, '_t_type_symbols'):
|
||||
cls = self._symtab._t_type_symbols.get(decorator.id)
|
||||
if cls is not None and isinstance(cls, type) and issubclass(cls, t.CType):
|
||||
if issubclass(cls, t.CUnion):
|
||||
cls: type | None = getattr(TypeSystem, decorator.id, None)
|
||||
if cls is None and hasattr(self._symtab, '_builtin_type_symbols'):
|
||||
cls = self._symtab._builtin_type_symbols.get(decorator.id)
|
||||
if cls is not None and isinstance(cls, type) and issubclass(cls, TypeSystem.CType):
|
||||
if issubclass(cls, TypeSystem.CUnion):
|
||||
type_kind = 'union'
|
||||
elif issubclass(cls, t.REnum):
|
||||
elif issubclass(cls, TypeSystem.REnum):
|
||||
type_kind = 'renum'
|
||||
elif issubclass(cls, t.CEnum):
|
||||
elif issubclass(cls, TypeSystem.CEnum):
|
||||
type_kind = 'enum'
|
||||
elif issubclass(cls, t.CStruct):
|
||||
elif issubclass(cls, TypeSystem.CStruct):
|
||||
type_kind = 'struct'
|
||||
elif decorator.id == 'Object':
|
||||
is_cpython_object = True
|
||||
elif isinstance(decorator, ast.Call):
|
||||
if isinstance(decorator.func, ast.Attribute):
|
||||
if hasattr(decorator.func.value, 'id') and decorator.func.value.id == 'c' and decorator.func.attr == 'Attribute':
|
||||
for arg in decorator.args:
|
||||
if isinstance(arg, ast.Attribute):
|
||||
if isinstance(arg.value, ast.Attribute):
|
||||
if hasattr(arg.value.value, 'id') and IsTModule(arg.value.value.id, self._symtab) and arg.value.attr == 'attr' and arg.attr == 'packed':
|
||||
is_packed = True
|
||||
if isinstance(decorator.func, ast.Name) and decorator.func.id == 'Attribute':
|
||||
for arg in decorator.args:
|
||||
if (isinstance(arg, ast.Attribute) and arg.attr == 'packed'
|
||||
and isinstance(arg.value, ast.Name) and arg.value.id == 'attr'):
|
||||
is_packed = True
|
||||
|
||||
# 检查基类
|
||||
for base in node.bases:
|
||||
if AnnotationContainsTType(base, 'CUnion'):
|
||||
if AnnotationContainsMarker(base, 'CUnion'):
|
||||
type_kind = 'union'
|
||||
break
|
||||
elif AnnotationContainsTType(base, 'CEnum'):
|
||||
elif AnnotationContainsMarker(base, 'CEnum'):
|
||||
type_kind = 'enum'
|
||||
break
|
||||
elif AnnotationContainsTType(base, 'REnum'):
|
||||
elif AnnotationContainsMarker(base, 'REnum'):
|
||||
type_kind = 'renum'
|
||||
break
|
||||
elif AnnotationContainsTType(base, 'CStruct'):
|
||||
elif AnnotationContainsMarker(base, 'CStruct'):
|
||||
type_kind = 'struct'
|
||||
break
|
||||
elif AnnotationContainsTType(base, 'Object'):
|
||||
elif AnnotationContainsMarker(base, 'Object'):
|
||||
is_cpython_object = True
|
||||
type_kind = 'struct'
|
||||
break
|
||||
@@ -275,9 +272,9 @@ class ASTSymbolExtractor:
|
||||
def _extract_ann_assign(self, node: ast.AnnAssign, result: ModuleSymbols) -> None:
|
||||
"""从 AnnAssign 节点提取 typedef 或 define 数据"""
|
||||
var_name: str = node.target.id
|
||||
has_cdefine: bool = AnnotationContainsTType(node.annotation, 'CDefine') if node.annotation else False
|
||||
has_postdef: bool = AnnotationContainsTType(node.annotation, 'Postdefinition') if node.annotation else False
|
||||
has_ctypedef: bool = AnnotationContainsTType(node.annotation, 'CTypedef') if node.annotation else False
|
||||
has_cdefine: bool = AnnotationContainsMarker(node.annotation, 'CDefine') if node.annotation else False
|
||||
has_postdef: bool = AnnotationContainsMarker(node.annotation, 'Postdefinition') if node.annotation else False
|
||||
has_ctypedef: bool = AnnotationContainsMarker(node.annotation, 'CTypedef') if node.annotation else False
|
||||
|
||||
if has_cdefine:
|
||||
result.defines.append(DefineSymbolData(
|
||||
|
||||
Reference in new issue
Block a user