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

+10
View File
@@ -13,6 +13,7 @@ from lib.core.VLogger import get_logger as _vlog
from lib.core.DecoratorPass import run as _run_decorator_pass
from lib.core.SymbolUtils import (
IsTModule, AnnotationContainsName, RequireDefineConstStorable,
DefineIntWidthOf, DefineIntSignednessOf,
)
# 治本修复:导入 ConstEvaluator 用于预扫描 CDefine 常量的复杂表达式求值
from lib.core.ConstEvaluator import ConstEvaluator
@@ -108,6 +109,15 @@ class LlvmGeneratorMixin:
if not hasattr(Gen, '_define_constants'):
Gen._define_constants: dict[str, int | str | float | bool] = {}
Gen._define_constants[NameNode.id] = val
# 带类型注册:记录声明整数类型(位宽 + 符号性),供使用点按该类型物化
# (如 `define X: DWORD = 5` → i32/u32,而不是按值大小猜 i32/i64)。
if isinstance(val, int) and not isinstance(val, bool):
_w: int | None = DefineIntWidthOf(TypeInfo.BaseType)
if _w is not None:
_s: bool | None = DefineIntSignednessOf(TypeInfo.BaseType)
if not hasattr(Gen, '_define_decl_types'):
Gen._define_decl_types: dict[str, tuple[int, bool]] = {}
Gen._define_decl_types[NameNode.id] = (_w, bool(_s) if _s is not None else False)
existing = self.SymbolTable.lookup(NameNode.id)
if not (existing and existing.IsTypedef and existing.OriginalType):
sym_info: _CTypeInfo = _CTypeInfo()