feat(TPC): 拓展 isinstance 跨模块支持
vtable linkage 改默认外部强定义(含 initializer 自动归一为空),跨模块可引用 sha1.Class_Vtable;外部引用声明为 i8* 避免 array 类型不匹配;_ResolveVtableAddr 增加 CVTable 状态检查,NoVTable 类回退编译时静态判断避免坏引用。新增 geom_lib.vp 跨模块测试库,main.vp 增加跨模块继承/运行时多态/NoVTable 场景。
This commit is contained in:
1 parent
8783340385
commit
1b89225130
4 files changed
+90
-6
No files matched your search
@@ -289,9 +289,17 @@ class ExprBuiltinHandle(BaseHandle):
|
||||
return addrs
|
||||
|
||||
def _ResolveVtableAddr(self, Gen, ClassName: str):
|
||||
"""解析类 ClassName 的 vtable 全局地址(本地 Vtables / 跨模块外部引用),
|
||||
无 vtable(NoVTable / 未注册)返回 None。
|
||||
"""解析类 ClassName 的 vtable 全局地址(本地 Vtables / 跨模块外部引用)。
|
||||
|
||||
仅对 CVTable 类返回地址;NoVTable 类(class_vtable 与
|
||||
_cross_module_vtable_classes 均不含)没有 vtable 全局变量,
|
||||
返回 None 让调用方回退编译时静态判断(避免生成对不存在的
|
||||
sha1.Class_Vtable 符号的引用导致链接失败)。
|
||||
"""
|
||||
has_vtable: bool = (ClassName in Gen.class_vtable
|
||||
or ClassName in Gen._cross_module_vtable_classes)
|
||||
if not has_vtable:
|
||||
return None
|
||||
vt = Gen.Vtables.get(ClassName) if hasattr(Gen, 'Vtables') else None
|
||||
if vt is not None:
|
||||
return vt
|
||||
@@ -303,7 +311,9 @@ class ExprBuiltinHandle(BaseHandle):
|
||||
if vname in Gen.module.globals:
|
||||
return Gen.module.globals[vname]
|
||||
try:
|
||||
ext = ir.GlobalVariable(Gen.module, ir.ArrayType(ir.PointerType(ir.IntType(8)), 1), name=vname)
|
||||
# 外部引用声明为 i8*(而非 [N x i8*]),避免与定义模块
|
||||
# 不同长度的 array 类型在链接时 type mismatch
|
||||
ext = ir.GlobalVariable(Gen.module, ir.PointerType(ir.IntType(8)), name=vname)
|
||||
ext.linkage = 'external'
|
||||
return ext
|
||||
except Exception:
|
||||
@@ -321,7 +331,10 @@ class ExprBuiltinHandle(BaseHandle):
|
||||
VtPtr: ir.Value = Gen._load(ObjAsVt, name="isinstance_vt")
|
||||
Res: ir.Value | None = None
|
||||
for addr in VtableAddrs:
|
||||
AddrI8: ir.Value = Gen.builder.bitcast(addr, ir.PointerType(ir.IntType(8)), name="vt_addr")
|
||||
if addr.type == ir.PointerType(ir.IntType(8)):
|
||||
AddrI8: ir.Value = addr
|
||||
else:
|
||||
AddrI8 = Gen.builder.bitcast(addr, ir.PointerType(ir.IntType(8)), name="vt_addr")
|
||||
Cmp: ir.Value = Gen.builder.icmp_unsigned('==', VtPtr, AddrI8, name="isinstance_cmp")
|
||||
if Res is None:
|
||||
Res = Cmp
|
||||
|
||||
@@ -560,7 +560,10 @@ class StructGenMixin:
|
||||
VtableType: ir.ArrayType = ir.ArrayType(ir.PointerType(ir.IntType(8)), len(methods))
|
||||
Vtable: ir.GlobalVariable = ir.GlobalVariable(self.module, VtableType, name=self._mangle_name(f"{ClassName}_Vtable"))
|
||||
Vtable.initializer = ir.Constant(VtableType, [ir.Constant(ir.PointerType(ir.IntType(8)), None)] * len(methods))
|
||||
Vtable.linkage = 'internal'
|
||||
# 保持默认 linkage(空字符串,带 initializer 自动打印为外部强定义
|
||||
# global,llvmlite descr 规则:initializer 非空时 linkage 归一为空)。
|
||||
# 2026-08-10:跨模块 isinstance 需引用 sha1.Class_Vtable 符号,
|
||||
# vtable 名含模块 sha1 前缀全局唯一,外部可见不会冲突。
|
||||
self.Vtables[ClassName] = Vtable
|
||||
|
||||
def _create_vtable_for_class(self, ClassName: str) -> None:
|
||||
@@ -589,7 +592,7 @@ class StructGenMixin:
|
||||
VtableType: ir.ArrayType = ir.ArrayType(ir.PointerType(ir.IntType(8)), len(methods))
|
||||
Vtable: ir.GlobalVariable = ir.GlobalVariable(self.module, VtableType, name=_vtable_name)
|
||||
Vtable.initializer = ir.Constant(VtableType, [ir.Constant(ir.PointerType(ir.IntType(8)), None)] * len(methods))
|
||||
Vtable.linkage = 'internal'
|
||||
# 默认 linkage(外部强定义),跨模块 isinstance 引用(与 _create_Vtable_globals 一致)
|
||||
self.Vtables[ClassName] = Vtable
|
||||
|
||||
def _fix_global_var_init(self) -> None:
|
||||
|
||||
Reference in new issue
Block a user