refactor(tpv): get_llvm_type_bits 双副本收敛到 TSpec.SpecTypeBits

TypeSpec 新增 SpecTypeBits 作为「LLVMType → 整数位宽」块的唯一实现(对齐 SpecTypePtrLevel 的收拢先例);ExprGenBase/TypeGenErr 两份同码副本改为委托,TypeGenErr 补 TypeSpec 导入(无环)。

保留方法名与 self. 调用点:@t.NoVTable 静态绑定下 Mixin 内部大量 self.get_llvm_type_bits 依赖其存在,只换实现体不动签名。副本分叉会导致同一类型两处算出不同位宽的隐蔽不一致。
This commit is contained in:
TermiNexus committed 2026-09-11 23:14:51 +08:00
1 parent 41c5908c5e
commit 19fca19ca7
3 files changed
+38 -17

No files matched your search

+7 -8
View File
@@ -100,16 +100,15 @@ class ExprGenBase:
return buf
# ============================================================
# 获取 LLVM 整数类型的位宽
#
# [S4 切读·位宽块] 实现已收拢到 `TSpec.SpecTypeBits`(该块在 TypeSpec 模块中的
# **唯一实现**),此处只做委托,不再保留同码副本(TypeGenErr.get_llvm_type_bits
# 同样委托到它)。两份副本一旦分叉,会出现「同一类型在两处算出不同位宽」的
# 隐蔽不一致。私有调用点(self.get_llvm_type_bits)保持原样不受影响。
# ============================================================
def get_llvm_type_bits(self, ty: llvmlite.LLVMType | t.CPtr) -> int:
"""获取 LLVM 整数类型的位宽,非整数类型返回 0"""
if ty is None:
return 0
match ty:
case llvmlite.LLVMType.Int(bits):
return bits
case _:
return 0
"""获取 LLVM 整数类型的位宽,非整数类型返回 0(委托 SpecTypeBits)"""
return TSpec.SpecTypeBits(ty)
# ============================================================
# 获取 LLVM 浮点类型的位宽
# ============================================================
+11 -9
View File
@@ -16,7 +16,8 @@ import lib.Stub.StubMerger as StubMerger
import lib.core.CompilerFactory as CompilerFactory
import lib.core.Gen.Translator as HT
import lib.core.Gen.Type.TypeGenBase as TypeGenBase
import lib.core.Gen.Type.TypeGenBase as TypeGenBase
import lib.core.Gen.Type.TypeSpec as TSpec
# ============================================================
@@ -250,15 +251,16 @@ class TypeGenErr(TypeGenBase.TypeGenBase):
if stmt is not None and stmt.kind() == ast.ASTKind.Pass:
return 1
return 0
# ============================================================
# 获取 LLVM 整数类型的位宽
#
# [S4 切读·位宽块] 实现已收拢到 `TSpec.SpecTypeBits`(该块在 TypeSpec 模块中的
# **唯一实现**),此处只做委托,不再保留同码副本(ExprGenBase.get_llvm_type_bits
# 同样委托到它)。私有调用点(self.get_llvm_type_bits)保持原样不受影响。
# ============================================================
def get_llvm_type_bits(self, ty: llvmlite.LLVMType | t.CPtr) -> int:
"""获取 LLVM 整数类型的位宽,非整数类型返回 0"""
if ty is None:
return 0
match ty:
case llvmlite.LLVMType.Int(bits):
return bits
case _:
return 0
"""获取 LLVM 整数类型的位宽,非整数类型返回 0(委托 SpecTypeBits)"""
return TSpec.SpecTypeBits(ty)
def infer_expr_type(self, pool: memhub.MemBuddy | t.CPtr,
node: ast.AST | t.CPtr,
param_types: str) -> llvmlite.LLVMType | t.CPtr:
+20
View File
@@ -1033,6 +1033,26 @@ def SpecTypePtrLevel(ty: llvmlite.LLVMType | t.CPtr) -> int:
return level
# ============================================================
# SpecTypeBits - LLVM 类型 → 整数位宽(**位宽块的唯一实现**)
#
# S4 起由 ExprGenBase.get_llvm_type_bits 与 TypeGenErr.get_llvm_type_bits 委托调用
# (原为两份同码副本:ExprGenBase.vp:104 + TypeGenErr.vp:253)。收拢理由与
# SpecTypePtrLevel 相同:同为「LLVMType → 标量」的纯函数,属本模块职责域;
# 副本一旦分叉会出现「同一类型在两处算出不同位宽」的隐蔽不一致。
# 非整数类型(指针/浮点/结构体等)返回 0 —— 0 是"非整数"的表态,不是兜底位宽。
# ============================================================
def SpecTypeBits(ty: llvmlite.LLVMType | t.CPtr) -> int:
"""获取 LLVM 整数类型的位宽,非整数类型返回 0"""
if ty is None:
return 0
match ty:
case llvmlite.LLVMType.Int(bits):
return bits
case _:
return 0
# ============================================================
# SpecToLLVMText - TypeSpec → LLVM IR 类型文本(**逐字节对齐 llvmlite.TypePrint**)
#