feat(cdef): define 常量带类型注册——按声明类型物化(DWORD(-11) → i32 位型)

方案 C:CDefine 不再只存无类型 int,而是把**声明整数类型**(位宽 + 符号性)一并登记,使用点按该类型物化,而不是按数值大小猜 i32/i64。链路:① SymbolUtils 新增 DefineIntSignednessOf / DefineDeclIntTypeFromAnnotation(由 T | t.CDefine 注解经符号表 typedef 链解析宽度与符号性)/ DefineDeclTypeValue(按声明类型把值归一成 (位宽, 位型值));② 同模块注册(LlvmGenerator._register_cdefine_constant)与跨模块提取(Phase2Translator._ExtractCDefineConstantsFromPyi,含 _shared_all_decl_types 的 pickle 传递)分别写 Gen._define_decl_types / translator._define_decl_types;③ 物化处(HandlesExpr 裸名两处、HandlesExprAttr._make_define_constant,及 from-import 注册)优先按声明类型出常量。这样 win32file 的 STD_*_HANDLE(DWORD(-10/-11/-12))解析为 i32 位型 0xFFFFFFF5 等,与 GetStdHandle(ULONG) 使用点一致。
This commit is contained in:
TermiNexus committed 2026-10-10 21:20:30 +08:00
1 parent e1a0fbf9c0
commit d5b4e9a8b9
6 files changed
+198 -20

No files matched your search

+32 -13
View File
@@ -18,7 +18,7 @@ from lib.constants.config import mode as _config_mode
from lib.core.Handles.HandlesBase import CTypeInfo
from lib.core.SymbolData import ModuleSymbols
from lib.includes import t
from lib.core.SymbolUtils import AnnotationContainsName, ExpandSoftKeywords
from lib.core.SymbolUtils import AnnotationContainsName, ExpandSoftKeywords, DefineDeclIntTypeFromAnnotation
from lib.core.SymbolUtils import ClassHasFuncLevelCVTable, IsVtableSlotMethod
from lib.Projectrans.Utils import compute_sha1, _check_annotation_for_export, find_reachable_files_from_entries, topological_sort_files, parse_python_file
from lib.Projectrans.Phase1Generator import _TryGlobalCache, _SaveToGlobalCache, _GLOBAL_CACHE_DIR
@@ -469,9 +469,9 @@ _JOBS_ENV: str = 'TPC_JOBS'
_MemProbeDone: set[int] = set()
# 预分配校验的分块大小(MB):分块提交并**全部持有**到校验结束(峰值 = 预算),
# 结束后立即整体释放 —— 紧接其后就创建 worker 进程,OS 直接复用这批刚释放的页。
_MEM_PROBE_CHUNK_MB: int = 256
# 自动并行度上限(用户 2026-10-01 定:16 worker 太多)。只约束**自动**取值,
# 显式 TPC_JOBS=N 仍按硬件/文件数上限生效。
_MEM_PROBE_CHUNK_MB: int = 256
# 自动并行度上限(用户 2026-10-01 定:16 worker 太多)。只约束**自动**取值,
# 显式 TPC_JOBS=N 仍按硬件/文件数上限生效。
_MAX_WORKERS: int = 8
@@ -583,7 +583,9 @@ def _PickWorkerCount(n_files: int, per_worker_mb: int = 350) -> int:
return base
def _ExtractCDefineConstantsFromPyi(pyi_path: str, all_dc: dict[str, object]) -> None:
def _ExtractCDefineConstantsFromPyi(pyi_path: str, all_dc: dict[str, object],
symtab: object | None = None,
all_dt: dict[str, tuple[int, bool]] | None = None) -> None:
"""从 .pyi 文件中提取 CDefine 常量到 all_dc。文件不存在时静默跳过。
治本修复:原实现只支持 ast.Constant 和 float() 调用,
@@ -632,6 +634,12 @@ def _ExtractCDefineConstantsFromPyi(pyi_path: str, all_dc: dict[str, object]) ->
val = ConstEvaluator.eval_full(node.value.args[0], ctx)
if val is not None and isinstance(val, (int, float, str)):
all_dc[node.target.id] = val
# 带类型注册:记录该 define 的**声明整数类型**(如 `DWORD` → (32, False)),
# 供使用点按声明类型物化(u32 用 i32 + 位型,而不是按值大小猜 i32/i64)。
if all_dt is not None and isinstance(val, int) and not isinstance(val, bool):
decl_type: tuple[int, bool] | None = DefineDeclIntTypeFromAnnotation(symtab, node.annotation)
if decl_type is not None:
all_dt[node.target.id] = decl_type
except Exception as e:
_vlog().warning(f"Phase2Translator: 忽略异常 {e}", exc_info=e)
@@ -706,6 +714,7 @@ def _parallel_translate_worker(src_path: str, out_path: str, src_root: str, temp
trans._shared_symbol_table = pickle.loads(_sym_bytes)
trans._shared_source_module_sig_files = shared_data['source_module_sig_files']
trans._shared_all_dc = shared_data['all_dc']
trans._shared_all_decl_types = shared_data.get('all_decl_types', {})
trans._shared_export_extern_funcs = shared_data['export_extern_funcs']
trans.inline_func_symbols = shared_data['inline_func_symbols']
trans.function_default_args = shared_data['function_default_args']
@@ -776,6 +785,7 @@ def _parallel_translate_batch(batch: list, src_root: str, temp_dir: str, output_
trans._shared_symbol_table = pickle.loads(symbol_table_bytes)
trans._shared_source_module_sig_files = shared_data['source_module_sig_files']
trans._shared_all_dc = shared_data['all_dc']
trans._shared_all_decl_types = shared_data.get('all_decl_types', {})
trans._shared_export_extern_funcs = shared_data['export_extern_funcs']
trans.inline_func_symbols = shared_data['inline_func_symbols']
trans.function_default_args = shared_data['function_default_args']
@@ -1332,6 +1342,7 @@ class Phase2Translator:
self._shared_symbol_table: object | None = None
self._shared_source_module_sig_files: dict[str, str] = {}
self._shared_all_dc: dict[str, dict] = {}
self._shared_all_decl_types: dict[str, tuple[int, bool]] = {}
self._shared_export_extern_funcs: dict[str, str] = {}
self.include_dirs: list[str] = []
self.struct_sha1_map: dict[str, str] = {}
@@ -1650,6 +1661,7 @@ class Phase2Translator:
'symbol_table_bytes': symbol_table_bytes,
'source_module_sig_files': self._shared_source_module_sig_files,
'all_dc': self._shared_all_dc,
'all_decl_types': self._shared_all_decl_types,
'export_extern_funcs': self._shared_export_extern_funcs,
'inline_func_symbols': self.inline_func_symbols,
'function_default_args': self.function_default_args,
@@ -2340,6 +2352,7 @@ class Phase2Translator:
self._shared_symbol_table.translator = trans.translator
trans.translator._source_module_sig_files = self._shared_source_module_sig_files
trans.translator._all_define_constants = self._shared_all_dc
trans.translator._define_decl_types = self._shared_all_decl_types
trans.translator._export_extern_funcs = self._shared_export_extern_funcs
if self._shared_generic_class_templates:
if not hasattr(trans.translator.ClassHandler, '_generic_class_templates'):
@@ -2408,6 +2421,7 @@ class Phase2Translator:
trans.translator._source_module_sig_files[short_name] = sig_path
all_dc = {}
all_dt: dict[str, tuple[int, bool]] = {}
for sha1_key, stub_path in self.stub_files.items():
if sha1_key == sha1:
continue
@@ -2429,9 +2443,11 @@ class Phase2Translator:
for sha1_key, pyi_path in self.sig_files.items():
if sha1_key == sha1:
continue
_ExtractCDefineConstantsFromPyi(pyi_path, all_dc)
_ExtractCDefineConstantsFromPyi(pyi_path, all_dc, trans.translator.SymbolTable, all_dt)
if all_dc:
trans.translator._all_define_constants = all_dc
if all_dt:
trans.translator._define_decl_types = all_dt
for sym_name, sym_info in self.inline_func_symbols.items():
if sym_name not in trans.translator.SymbolTable:
@@ -3324,19 +3340,19 @@ class Phase2Translator:
_vlog().warning(f"发现 include 传递依赖失败: {_e}", "Exception")
def _try_add_include_dep(self, module_name: str, includes_dir: str, discovered: set, py_index: set[str] | None = None):
"""尝试将模块添加为 include 依赖(支持子包,如 w32.win32base)
"""尝试将模块添加为 include 依赖(支持子包,如 win32.win32base)
py_index 非空时用 set 查询替代 os.path.isfile(O(1) vs 24ms/次)。
"""
# 候选路径:子包路径(w32/win32base.vp/.py)、顶层模块(w32.vp/.py)、子包 __init__.vp/__init__.py
# 候选路径:子包路径(win32/win32base.vp/.py)、顶层模块(win32.vp/.py)、子包 __init__.vp/__init__.py
candidates: list[str] = []
# 子包路径:w32.win32base → w32/win32base.vp / .py
# 子包路径:win32.win32base → win32/win32base.vp / .py
candidates.append(os.path.join(includes_dir, module_name.replace('.', os.sep) + '.vp'))
candidates.append(os.path.join(includes_dir, module_name.replace('.', os.sep) + '.py'))
# 子包 __init__.vp/__init__.py:w32.win32base → w32/win32base/__init__.vp / .py
# 子包 __init__.vp/__init__.py:win32.win32base → win32/win32base/__init__.vp / .py
candidates.append(os.path.join(includes_dir, module_name.replace('.', os.sep), '__init__.vp'))
candidates.append(os.path.join(includes_dir, module_name.replace('.', os.sep), '__init__.py'))
# 顶层模块:取第一个组件 → w32.vp / .py
# 顶层模块:取第一个组件 → win32.vp / .py
top_module = module_name.split('.')[0]
if top_module != module_name:
candidates.append(os.path.join(includes_dir, top_module + '.vp'))
@@ -3568,7 +3584,7 @@ class Phase2Translator:
hotspot_on: bool = bool(self.hotspot_sites_file)
# --traceback:include 模块也必须带 DWARF 行表(否则运行时 Ctrl+C 的栈里
# 落在 includes/*(memhub/string/viperlib/w32…)的帧取不到源行,只能落到
# 落在 includes/*(memhub/string/viperlib/win32…)的帧取不到源行,只能落到
# 邻行或 "+0x…(unknown)")。为此翻译时给 include 透传 dwarf_enabled=True
# (见下方 worker/串行四处的 dwarf_enabled=self.traceback_enabled),并把
# 旧目标文件(output 目录 {sha1}.obj 及隔离缓存 includes.binary_traceback
@@ -3694,6 +3710,7 @@ class Phase2Translator:
'symbol_table_bytes': pickle.dumps(self._shared_symbol_table),
'source_module_sig_files': self._shared_source_module_sig_files,
'all_dc': self._shared_all_dc,
'all_decl_types': self._shared_all_decl_types,
'export_extern_funcs': self._shared_export_extern_funcs,
'inline_func_symbols': self.inline_func_symbols,
'function_default_args': self.function_default_args,
@@ -4434,6 +4451,7 @@ class Phase2Translator:
self._register_package_reexports(base_trans, sig_path, package_name)
all_dc = {}
all_dt: dict[str, tuple[int, bool]] = {}
for sha1_key, stub_path in self.stub_files.items():
if os.path.exists(stub_path):
try:
@@ -4452,7 +4470,7 @@ class Phase2Translator:
_vlog().warning(f"Phase2Translator: 忽略异常 {e}", exc_info=e)
for sha1_key, pyi_path in self.sig_files.items():
_ExtractCDefineConstantsFromPyi(pyi_path, all_dc)
_ExtractCDefineConstantsFromPyi(pyi_path, all_dc, base_trans.translator.SymbolTable, all_dt)
export_extern_funcs = {}
for sha1_key, sig_path in self.sig_files.items():
@@ -4483,6 +4501,7 @@ class Phase2Translator:
self._shared_symbol_table = base_trans.translator.SymbolTable
self._shared_source_module_sig_files = dict(base_trans.translator._source_module_sig_files)
self._shared_all_dc = all_dc
self._shared_all_decl_types = all_dt
self._shared_export_extern_funcs = export_extern_funcs
generic_class_templates = {}