初步清理 t. 类型异味
This commit is contained in:
1 parent
4ce55e3ea3
commit
cfb92e5da5
84 files changed
+7769
-7907
No files matched your search
@@ -36,17 +36,17 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# ============================================================
|
||||
# _vtable_strcat — 将 src 追加到 dst 末尾
|
||||
# ============================================================
|
||||
def _vtable_strcat(self, dst: str, dst_size: t.CSizeT,
|
||||
def _vtable_strcat(self, dst: str, dst_size: sizet,
|
||||
src: str):
|
||||
"""将 src 追加到 dst 末尾"""
|
||||
if dst is None or src is None:
|
||||
return
|
||||
dlen: t.CSizeT = string.strlen(dst)
|
||||
slen: t.CSizeT = string.strlen(src)
|
||||
remain: t.CSizeT = dst_size - dlen
|
||||
dlen: sizet = string.strlen(dst)
|
||||
slen: sizet = string.strlen(src)
|
||||
remain: sizet = dst_size - dlen
|
||||
if remain <= 0:
|
||||
return
|
||||
i: t.CSizeT = 0
|
||||
i: sizet = 0
|
||||
while i < slen and i + 1 < remain:
|
||||
dst[dlen + i] = src[i]
|
||||
i += 1
|
||||
@@ -71,7 +71,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# 例如: void ({i32, i32}*, i32)
|
||||
# ============================================================
|
||||
def _format_func_type_str(self, pool: memhub.MemBuddy | t.CPtr,
|
||||
buf: str, buf_size: t.CSizeT,
|
||||
buf: str, buf_size: sizet,
|
||||
func: llvmlite.Function | t.CPtr) -> int:
|
||||
"""从 Function 对象生成函数类型字符串"""
|
||||
if buf is None or func is None or buf_size == 0:
|
||||
@@ -132,15 +132,15 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
children: list[ast.AST | t.CPtr] | t.CPtr = cd.children
|
||||
if children is None:
|
||||
return 0
|
||||
cn: t.CSizeT = len(children)
|
||||
cn: sizet = len(children)
|
||||
|
||||
# 1. 收集虚方法信息
|
||||
# stdlib 分配:method_names_buf 存到 StructEntry.VTableMethods(跨文件永久存活),
|
||||
# 必须迁出 mb 池 reset 区间,否则 1b 每文件 reset_to(mark1) 会回收导致悬空。
|
||||
full_names_buf: t.CSizeT | t.CPtr = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
||||
method_names_buf: t.CSizeT | t.CPtr = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
||||
full_names_buf: sizet | t.CPtr = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
||||
method_names_buf: sizet | t.CPtr = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
||||
# 记录每个方法的父类 SHA1(非 0=继承方法用父模块 SHA1 mangling,0=当前类方法)
|
||||
method_parent_sha1s: t.CSizeT | t.CPtr = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
||||
method_parent_sha1s: sizet | t.CPtr = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
||||
if full_names_buf is None or method_names_buf is None or method_parent_sha1s is None:
|
||||
return 0
|
||||
string.memset(full_names_buf, 0, 8 * VTBL_METHOD_MAX)
|
||||
@@ -218,7 +218,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
else:
|
||||
def_class = pname
|
||||
# 多继承:沿 pname 的 MRO 祖先链探测实际定义类(跳过自身,先声明父类优先)
|
||||
mro_ptr: t.CSizeT | t.CPtr = self.Gen.StructGen.get_mro_chain(pool, pname)
|
||||
mro_ptr: sizet | t.CPtr = self.Gen.StructGen.get_mro_chain(pool, pname)
|
||||
mro_i: int = 1
|
||||
while mro_ptr is not None and mro_ptr[mro_i] != 0:
|
||||
anc: str = (str | t.CPtr)(t.CVoid(mro_ptr[mro_i], t.CPtr))
|
||||
@@ -233,13 +233,13 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
pfname_buf: str = CompilerFactory.factory.PermPool.alloc(128)
|
||||
if pfname_buf is not None:
|
||||
viperlib.snprintf(pfname_buf, 128, "%s.%s", def_class, pmethod_name)
|
||||
full_names_buf[method_count] = t.CSizeT(pfname_buf)
|
||||
full_names_buf[method_count] = sizet(pfname_buf)
|
||||
# 复制方法名(短名)
|
||||
pmname_len: t.CSizeT = string.strlen(pmethod_name)
|
||||
pmname_len: sizet = string.strlen(pmethod_name)
|
||||
pmname_buf: str = CompilerFactory.factory.PermPool.alloc(pmname_len + 1)
|
||||
if pmname_buf is not None:
|
||||
string.strcpy(pmname_buf, pmethod_name)
|
||||
method_names_buf[method_count] = t.CSizeT(pmname_buf)
|
||||
method_names_buf[method_count] = sizet(pmname_buf)
|
||||
# 记录 mangling 用的 SHA1(继承未覆盖的方法用实际定义类的模块 SHA1)
|
||||
if child_overrides == 1 and child_nvt == 0:
|
||||
# 子类覆盖: 用当前模块 SHA1(保持 0)
|
||||
@@ -249,7 +249,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
def_entry2: GenTypes.StructEntry | t.CPtr = self.Gen.StructGen.find_struct(
|
||||
def_class)
|
||||
if def_entry2 is not None and def_entry2.ModuleSha1 is not None:
|
||||
method_parent_sha1s[method_count] = t.CSizeT(def_entry2.ModuleSha1)
|
||||
method_parent_sha1s[method_count] = sizet(def_entry2.ModuleSha1)
|
||||
else:
|
||||
method_parent_sha1s[method_count] = 0
|
||||
method_count += 1
|
||||
@@ -281,7 +281,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# 检查是否已从父类继承(避免重复)
|
||||
already_in_vt: int = 0
|
||||
for mi in range(method_count):
|
||||
exist_addr: t.CSizeT = method_names_buf[mi]
|
||||
exist_addr: sizet = method_names_buf[mi]
|
||||
if exist_addr != 0:
|
||||
exist_name: str = (str | t.CPtr)(t.CVoid(exist_addr, t.CPtr))
|
||||
if exist_name is not None and string.strcmp(exist_name, mname) == 0:
|
||||
@@ -295,14 +295,14 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
fname_buf: str = CompilerFactory.factory.PermPool.alloc(128)
|
||||
if fname_buf is not None:
|
||||
viperlib.snprintf(fname_buf, 128, "%s.%s", class_name, mname)
|
||||
full_names_buf[method_count] = t.CSizeT(fname_buf)
|
||||
full_names_buf[method_count] = sizet(fname_buf)
|
||||
|
||||
# 复制方法名
|
||||
mname_len: t.CSizeT = string.strlen(mname)
|
||||
mname_len: sizet = string.strlen(mname)
|
||||
mname_buf: str = CompilerFactory.factory.PermPool.alloc(mname_len + 1)
|
||||
if mname_buf is not None:
|
||||
string.strcpy(mname_buf, mname)
|
||||
method_names_buf[method_count] = t.CSizeT(mname_buf)
|
||||
method_names_buf[method_count] = sizet(mname_buf)
|
||||
|
||||
method_count += 1
|
||||
|
||||
@@ -345,9 +345,9 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# 同时设置 entry.VTableMethodCount/VTableMethods(虚分派查询用)
|
||||
if self._collect_vtable_methods(trans, cd, class_name) == 0:
|
||||
return 0
|
||||
full_names_buf: t.CSizeT | t.CPtr = self.VtFullNames
|
||||
method_names_buf: t.CSizeT | t.CPtr = self.VtMethodNames
|
||||
method_parent_sha1s: t.CSizeT | t.CPtr = self.VtParentSha1s
|
||||
full_names_buf: sizet | t.CPtr = self.VtFullNames
|
||||
method_names_buf: sizet | t.CPtr = self.VtMethodNames
|
||||
method_parent_sha1s: sizet | t.CPtr = self.VtParentSha1s
|
||||
method_count: int = self.VtMethodCount
|
||||
# 重新获取 entry(后续存储 VTableMethods 用)
|
||||
vt_self_entry: GenTypes.StructEntry | t.CPtr = self.Gen.StructGen.find_struct_by_module(
|
||||
@@ -384,7 +384,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
if i > 0:
|
||||
self._vtable_strcat(init_buf, 16384, ", ")
|
||||
|
||||
full_name_addr: t.CSizeT = full_names_buf[i]
|
||||
full_name_addr: sizet = full_names_buf[i]
|
||||
if full_name_addr == 0:
|
||||
self._vtable_strcat(init_buf, 16384, "i8* null")
|
||||
continue
|
||||
@@ -392,7 +392,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
full_name: str = (str | t.CPtr)(t.CVoid(full_name_addr, t.CPtr))
|
||||
|
||||
# 检查是否是继承方法(method_parent_sha1s 非 0 表示用父模块 SHA1 mangling)
|
||||
parent_sha1_addr: t.CSizeT = method_parent_sha1s[i]
|
||||
parent_sha1_addr: sizet = method_parent_sha1s[i]
|
||||
if parent_sha1_addr != 0:
|
||||
# 继承方法: 用父类模块 SHA1 做 mangling
|
||||
parent_sha1: str = (str | t.CPtr)(t.CVoid(parent_sha1_addr, t.CPtr))
|
||||
@@ -524,7 +524,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
if children is None:
|
||||
return 0
|
||||
|
||||
cn: t.CSizeT = len(children)
|
||||
cn: sizet = len(children)
|
||||
has_method: int = 0
|
||||
has_init: int = 0
|
||||
has_new: int = 0
|
||||
@@ -715,11 +715,11 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
viperlib.snprintf(method_name_buf, 128, "%s", fd.name)
|
||||
if func_meta & GenTypes.META_PROP_SETTER != 0:
|
||||
# 128 字节缓冲:追加 "$set" 前检查剩余容量,防 strcat 无界写穿
|
||||
mn_len2: t.CSizeT = string.strlen(method_name_buf)
|
||||
mn_len2: sizet = string.strlen(method_name_buf)
|
||||
if mn_len2 + 5 < 128:
|
||||
string.strcat(method_name_buf, "$set")
|
||||
elif func_meta & GenTypes.META_PROP_DELETER != 0:
|
||||
mn_len2: t.CSizeT = string.strlen(method_name_buf)
|
||||
mn_len2: sizet = string.strlen(method_name_buf)
|
||||
if mn_len2 + 5 < 128:
|
||||
string.strcat(method_name_buf, "$del")
|
||||
method_name = method_name_buf
|
||||
@@ -774,7 +774,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# @staticmethod 无 self,param_count 为完整参数数)
|
||||
# 注意: 必须先把属性赋给显式类型为 list[...] | t.CPtr 的局部变量再调用 __len__(),
|
||||
# 否则编译器无法识别属性返回的 list 类型,GEP base 会变成 i32 0 导致 llc 报错
|
||||
md_defaults: t.CVoid | t.CPtr = None
|
||||
md_defaults: t.CPtr = None
|
||||
md_default_count: int = 0
|
||||
md_param_count: int = 0
|
||||
md_args_node: ast.Arguments | t.CPtr = fd.args
|
||||
@@ -819,7 +819,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
ags: ast.Arguments | t.CPtr = (ast.Arguments | t.CPtr)(args_node)
|
||||
if ags.args is not None:
|
||||
alist: list[ast.AST | t.CPtr] | t.CPtr = ags.args
|
||||
an: t.CSizeT = len(alist)
|
||||
an: sizet = len(alist)
|
||||
start_idx: int = 0
|
||||
if is_static_m == 0:
|
||||
start_idx = 1
|
||||
@@ -963,7 +963,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
ags2: ast.Arguments | t.CPtr = (ast.Arguments | t.CPtr)(args_node)
|
||||
if ags2.args is not None:
|
||||
alist2: list[ast.AST | t.CPtr] | t.CPtr = ags2.args
|
||||
an2: t.CSizeT = len(alist2)
|
||||
an2: sizet = len(alist2)
|
||||
start_idx2: int = 0
|
||||
if is_static_m == 0:
|
||||
start_idx2 = 1
|
||||
@@ -1044,7 +1044,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# (main.vp __init__ BootInfo memmap 全 0 根因)。
|
||||
body: list[ast.AST | t.CPtr] | t.CPtr = fd.children
|
||||
if body is not None:
|
||||
bn: t.CSizeT = len(body)
|
||||
bn: sizet = len(body)
|
||||
for bi in range(bn):
|
||||
stmt: ast.AST | t.CPtr = body.get(bi)
|
||||
if stmt is not None:
|
||||
@@ -1056,7 +1056,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
|
||||
# 翻译方法体
|
||||
if body is not None:
|
||||
bn2: t.CSizeT = len(body)
|
||||
bn2: sizet = len(body)
|
||||
for bi2 in range(bn2):
|
||||
stmt2: ast.AST | t.CPtr = body.get(bi2)
|
||||
if stmt2 is not None:
|
||||
@@ -1077,7 +1077,7 @@ class ClassGenVTable(ClassGenBase.ClassGenBase):
|
||||
# FuncGenMixin 模块级函数路径,方法路径此处追加89 一并同步)
|
||||
last_is_return: int = 0
|
||||
# 追加89:bn3 提前初始化,避免 body is None 时下方引用未定义变量
|
||||
bn3: t.CSizeT = 0
|
||||
bn3: sizet = 0
|
||||
if body is not None:
|
||||
bn3 = len(body)
|
||||
if bn3 > 0:
|
||||
|
||||
Reference in new issue
Block a user