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
@@ -6,8 +6,8 @@ import struct
|
||||
|
||||
from lib.utils.helpers import DetectFileType
|
||||
from lib.core.Handles.HandlesBase import CTypeInfo
|
||||
from lib.core.SymbolUtils import IsTModule, AnnotationContainsName, ExpandSoftKeywords
|
||||
from lib.includes import t
|
||||
from lib.core.SymbolUtils import AnnotationContainsName, ExpandSoftKeywords
|
||||
from lib.core import TypeSystem
|
||||
from lib.constants.config import mode as _ConfigMode
|
||||
from lib.core.VLogger import get_logger as _vlog
|
||||
|
||||
@@ -116,18 +116,18 @@ class PythonParserMixin:
|
||||
j += 1
|
||||
continue
|
||||
elif isinstance(NextNode, ast.AnnAssign):
|
||||
# 检测 xxx: t.Postdefinition(yyy) 或 xxx: t.CTypedef = xxx 或 xxx: t.CPtr = xxx
|
||||
# 检测 xxx: Postdefinition(yyy) 或 xxx: CTypedef = xxx 或 xxx: CPtr = xxx
|
||||
if isinstance(NextNode.target, ast.Name):
|
||||
TargetName: str = NextNode.target.id
|
||||
# 先处理新格式:t.Postdefinition(...)
|
||||
# 先处理新格式:Postdefinition(...)
|
||||
annotation: ast.expr | None = NextNode.annotation
|
||||
PostdefNode: ast.Call | None = None
|
||||
|
||||
# 递归搜索整个注解树,找到 t.Postdefinition(...) 调用
|
||||
# 递归搜索整个注解树,找到 Postdefinition(...) 调用
|
||||
def find_PostdefNode(node: ast.AST) -> ast.Call | None:
|
||||
if isinstance(node, ast.Call):
|
||||
if isinstance(node.func, ast.Attribute):
|
||||
if hasattr(node.func.value, 'id') and IsTModule(node.func.value.id, self.SymbolTable) and node.func.attr == 'Postdefinition':
|
||||
if hasattr(node.func.value, 'id') and node.func.attr == 'Postdefinition':
|
||||
return node
|
||||
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
|
||||
LeftResult: ast.Call | None = find_PostdefNode(node.left)
|
||||
@@ -139,12 +139,11 @@ class PythonParserMixin:
|
||||
return None
|
||||
|
||||
PostdefNode = find_PostdefNode(annotation)
|
||||
# 检查是否找到 t.Postdefinition(...) 调用
|
||||
# 检查是否找到 Postdefinition(...) 调用
|
||||
if PostdefNode:
|
||||
if (isinstance(PostdefNode.func, ast.Attribute) and
|
||||
hasattr(PostdefNode.func, 'value') and
|
||||
hasattr(PostdefNode.func.value, 'id') and
|
||||
IsTModule(PostdefNode.func.value.id, self.SymbolTable) and
|
||||
hasattr(PostdefNode.func.value, 'id') and
|
||||
PostdefNode.func.attr == 'Postdefinition' and
|
||||
PostdefNode.args):
|
||||
# 检查第一个参数是否是当前类名
|
||||
@@ -164,7 +163,7 @@ class PythonParserMixin:
|
||||
dimensions.append(DimExpr)
|
||||
|
||||
# 如果没有从 Postdefinition 参数中获取维度,尝试从类型注解中的 Subscript 获取
|
||||
# 例如:memory_block_t[MAX_ORDER + 1] | t.CPtr
|
||||
# 例如:memory_block_t[MAX_ORDER + 1] | CPtr
|
||||
if not dimensions:
|
||||
def extract_subscript_dims(node: ast.AST) -> list[ast.expr]:
|
||||
"""递归提取 Subscript 节点的维度"""
|
||||
@@ -180,7 +179,7 @@ class PythonParserMixin:
|
||||
# 递归提取更外层的维度
|
||||
dims.extend(extract_subscript_dims(node.value))
|
||||
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
|
||||
# 处理 Type | t.CPtr 的情况,检查左侧
|
||||
# 处理 Type | CPtr 的情况,检查左侧
|
||||
dims.extend(extract_subscript_dims(node.left))
|
||||
return dims
|
||||
|
||||
@@ -196,7 +195,7 @@ class PythonParserMixin:
|
||||
value = NextNode.value
|
||||
|
||||
# 添加到对应的 assignments
|
||||
# 对于 t.Postdefinition(...),只有包含 CTypedef 或 CPtr 时才视为 typedef 类型
|
||||
# 对于 Postdefinition(...),只有包含 CTypedef 或 CPtr 时才视为 typedef 类型
|
||||
if HasCtypedef or IsPtr:
|
||||
if ClassKey not in self.TypedefAssignments:
|
||||
self.TypedefAssignments[ClassKey] = []
|
||||
@@ -229,8 +228,8 @@ class PythonParserMixin:
|
||||
# (2026-09-30)】原调用指向的方法全仓无定义 ⇒ 一旦命中即
|
||||
# `AttributeError`(与 ClassHandle._PreRegisterClassMembers
|
||||
# 同族:死调用,非"未走到")。
|
||||
# 现状核实:`__set_default__` 在 TransPyC/lib 内只有
|
||||
# t.py 的定义与 StubGen/Converter 的"剥离该调用、保留类型",
|
||||
# `__set_default__` 在 TransPyC/lib 内只有
|
||||
# StubGen/Converter 的"剥离该调用、保留类型",
|
||||
# **没有任何消费端**读取它的 kwargs;includes 全仓亦无源使用。
|
||||
# 因此这里既不调用幽灵方法,也不静默丢弃源码语义:明确报未支持,
|
||||
# 语义(字段默认值如何落到 class_member_defaults)确定后再实现。
|
||||
@@ -240,26 +239,19 @@ class PythonParserMixin:
|
||||
f"拒绝静默丢弃该默认值语义)")
|
||||
j += 1
|
||||
continue
|
||||
# 再处理旧格式:xxx: t.CTypedef = xxx 或 xxx: t.CPtr = xxx
|
||||
# 注意:这种格式的 typedef 不应该被合并到结构体定义中,
|
||||
# 而应该生成独立的 typedef 语句,所以不添加到 TypedefAssignments
|
||||
elif NextNode.annotation and isinstance(NextNode.annotation, ast.Attribute):
|
||||
if isinstance(NextNode.annotation.value, ast.Name):
|
||||
annot_id: str = NextNode.annotation.value.id
|
||||
annot_attr: str = NextNode.annotation.attr
|
||||
if annot_id == 't' and NextNode.value:
|
||||
if isinstance(NextNode.value, ast.Name):
|
||||
ValueName: str = NextNode.value.id
|
||||
if ValueName == ClassName:
|
||||
# 检查是 CTypedef 还是 CPtr
|
||||
if annot_attr == t.CPtr.__name__:
|
||||
# t.CPtr 表示指针变量,加入 TypedefAssignments,设置 IsPtr=True
|
||||
if ClassKey not in self.TypedefAssignments:
|
||||
self.TypedefAssignments[ClassKey] = []
|
||||
self.TypedefAssignments[ClassKey].append((TargetName, True))
|
||||
j += 1
|
||||
continue
|
||||
# 注意:t.CTypedef 不在这里处理,由 HandlesAnnAssign 生成独立的 typedef 语句
|
||||
# 再处理 xxx: CPtr = xxx:typedef 不合并进结构体定义,
|
||||
# 而应生成独立的 typedef 语句,故不添加到 TypedefAssignments
|
||||
elif (NextNode.annotation and isinstance(NextNode.annotation, ast.Name)
|
||||
and NextNode.annotation.id == TypeSystem.CPtr.__name__):
|
||||
if NextNode.value and isinstance(NextNode.value, ast.Name):
|
||||
ValueName: str = NextNode.value.id
|
||||
if ValueName == ClassName:
|
||||
# CPtr 表示指针变量,加入 TypedefAssignments,设置 IsPtr=True
|
||||
if ClassKey not in self.TypedefAssignments:
|
||||
self.TypedefAssignments[ClassKey] = []
|
||||
self.TypedefAssignments[ClassKey].append((TargetName, True))
|
||||
j += 1
|
||||
continue
|
||||
# 如果不是特殊赋值,停止收集
|
||||
break
|
||||
|
||||
@@ -287,20 +279,18 @@ class PythonParserMixin:
|
||||
IsCenum = True
|
||||
break
|
||||
|
||||
# 检测是否有 @t.Object 装饰器或继承自 t.Object
|
||||
# 检测是否有 @Object 装饰器或继承自 Object
|
||||
IsCpythonObject: bool = False
|
||||
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.SymbolTable) and
|
||||
if (hasattr(decorator.value, 'id') and
|
||||
decorator.attr == 'Object'):
|
||||
IsCpythonObject = True
|
||||
break
|
||||
elif isinstance(decorator, ast.Call):
|
||||
if isinstance(decorator.func, ast.Attribute):
|
||||
if (hasattr(decorator.func.value, 'id') and
|
||||
IsTModule(decorator.func.value.id, self.SymbolTable) and
|
||||
if (hasattr(decorator.func.value, 'id') and
|
||||
decorator.func.attr == 'Object'):
|
||||
IsCpythonObject = True
|
||||
break
|
||||
@@ -315,8 +305,7 @@ class PythonParserMixin:
|
||||
if not IsCpythonObject and hasattr(node, 'bases') and node.bases:
|
||||
for base in node.bases:
|
||||
if isinstance(base, ast.Attribute):
|
||||
if (hasattr(base.value, 'id') and
|
||||
IsTModule(base.value.id, self.SymbolTable) and
|
||||
if (hasattr(base.value, 'id') and
|
||||
base.attr == 'Object'):
|
||||
IsCpythonObject = True
|
||||
break
|
||||
@@ -348,7 +337,7 @@ class PythonParserMixin:
|
||||
continue
|
||||
MemberNode: CTypeInfo = CTypeInfo()
|
||||
MemberNode.Name = MemberName
|
||||
MemberNode.BaseType = t.CEnum(ClassName)
|
||||
MemberNode.BaseType = TypeSystem.CEnum(ClassName)
|
||||
# 提取枚举值:优先用显式赋值,否则自动编号
|
||||
if ItemValue is not None and isinstance(ItemValue, ast.Constant) and isinstance(ItemValue.value, int):
|
||||
MemberNode.value = ItemValue.value
|
||||
@@ -377,7 +366,7 @@ class PythonParserMixin:
|
||||
TypeInfo: CTypeInfo | None = self.TypeMergeHandler.GetCTypeInfo(item.annotation)
|
||||
if TypeInfo is None:
|
||||
TypeInfo = CTypeInfo()
|
||||
TypeInfo.BaseType = t.CInt()
|
||||
TypeInfo.BaseType = TypeSystem.CInt()
|
||||
IsPtr: bool = TypeInfo.IsPtr
|
||||
if not IsPtr:
|
||||
if isinstance(item.annotation, ast.BinOp) and isinstance(item.annotation.op, ast.BitOr):
|
||||
@@ -386,7 +375,7 @@ class PythonParserMixin:
|
||||
dims: list[int] = []
|
||||
if hasattr(item.annotation, 'slice') and isinstance(item.annotation.slice, ast.Constant):
|
||||
if isinstance(item.annotation.value, ast.Name):
|
||||
if IsTModule(item.annotation.value.id, self.SymbolTable):
|
||||
if item.annotation.value.id == 'CArray':
|
||||
if hasattr(item.annotation, 'slice'):
|
||||
try:
|
||||
dims.append(int(item.annotation.slice.value))
|
||||
@@ -421,17 +410,17 @@ class PythonParserMixin:
|
||||
elif isinstance(node, ast.AnnAssign):
|
||||
if isinstance(node.target, ast.Name):
|
||||
VarName: str = node.target.id
|
||||
# 检查是否是 t.CTypedef 格式,如果是则跳过(由 HandleClassDef 处理)
|
||||
# 检查是否是 CTypedef 格式,如果是则跳过(由 HandleClassDef 处理)
|
||||
is_ctypedef: bool = False
|
||||
if node.annotation:
|
||||
if isinstance(node.annotation, ast.Attribute):
|
||||
if isinstance(node.annotation.value, ast.Name):
|
||||
if IsTModule(node.annotation.value.id, self.SymbolTable) and node.annotation.attr == 'CTypedef':
|
||||
if node.annotation.attr == 'CTypedef':
|
||||
is_ctypedef = True
|
||||
elif isinstance(node.annotation, ast.BinOp) and isinstance(node.annotation.op, ast.BitOr):
|
||||
def _has_ctype(ann: ast.AST) -> bool:
|
||||
if isinstance(ann, ast.Attribute):
|
||||
return hasattr(ann.value, 'id') and IsTModule(ann.value.id) and ann.attr == 'CTypedef'
|
||||
return hasattr(ann.value, 'id') and ann.attr == 'CTypedef'
|
||||
if isinstance(ann, ast.BinOp) and isinstance(ann.op, ast.BitOr):
|
||||
return _has_ctype(ann.left) or _has_ctype(ann.right)
|
||||
if isinstance(ann, ast.Subscript):
|
||||
|
||||
Reference in new issue
Block a user