将部分 | t.CPtr 修正为 rawptr[T]

This commit is contained in:
TermiNexus committed 2026-09-18 17:56:24 +08:00
1 parent eb01a45024
commit 9e2ccbfab7
58 files changed
+3543 -3564

No files matched your search

+63 -63
View File
@@ -37,8 +37,8 @@ import lib.core.Gen.Stmt.StmtGenDeclMixin as StmtGenDeclMixin
class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
"""语句/流程翻译 Mixin(拆分后顶层,继承 Decl)"""
def translate_stmt(self, trans: HT.Translator | t.CPtr,
node: ast.AST | t.CPtr) -> int:
def translate_stmt(self, trans: rawptr[HT.Translator],
node: rawptr[ast.AST]) -> int:
"""翻译单条语句,返回新增的变量数(深度守卫包装器,原逻辑见 _translate_stmt_impl)"""
if trans is not None:
if trans.TranslateDepth >= HT.TRANSLATE_MAX_DEPTH:
@@ -50,8 +50,8 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
trans.TranslateDepth -= 1
return mret
def _translate_stmt_impl(self, trans: HT.Translator | t.CPtr,
node: ast.AST | t.CPtr) -> int:
def _translate_stmt_impl(self, trans: rawptr[HT.Translator],
node: rawptr[ast.AST]) -> int:
"""翻译单条语句,返回新增的变量数"""
if node is None:
return 0
@@ -107,8 +107,8 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
self.Gen.TypeGenErr.fatal_error(node, f"未知语句类型 kind={k}", "SyntaxError")
return 0
def translate_delete(self, trans: HT.Translator | t.CPtr,
node: ast.AST | t.CPtr) -> int:
def translate_delete(self, trans: rawptr[HT.Translator],
node: rawptr[ast.AST]) -> int:
"""翻译 del 语句,返回 0"""
if trans is None or node is None:
return 0
@@ -116,32 +116,32 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
if dnode is None or dnode.targets is None:
return 0
pool: rawptr[memhub.MemBuddy] = trans.Pool
mod: llvmlite.LLVMModule | t.CPtr = trans.Module
mod: rawptr[llvmlite.LLVMModule] = trans.Module
builder: llvmlite.IRBuilder | t.CPtr = trans._cur_builder
if pool is None or mod is None or builder is None:
return 0
tn: sizet = len(dnode.targets)
for ti in range(tn):
tgt: ast.AST | t.CPtr = dnode.targets.get(ti)
tgt: rawptr[ast.AST] = dnode.targets.get(ti)
if tgt is None:
continue
# del obj.attr → property deleter
if tgt.kind() == ast.ASTKind.Attribute:
del_at: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(tgt)
del_at: rawptr[ast.Attribute] = rawptr[ast.Attribute](tgt)
if del_at is not None:
self.Gen.CallGenProp._try_property_deleter(
pool, builder, mod, del_at, trans)
# del obj[key] → __delitem__(obj, key)(对齐 TPC HandlesDelete.py)。
# 不能静默忽略 Subscript 目标——否则 del 不生效(len 不变)。
elif tgt.kind() == ast.ASTKind.Subscript:
dl_sub: ast.Subscript | t.CPtr = (ast.Subscript | t.CPtr)(tgt)
dl_sub: rawptr[ast.Subscript] = rawptr[ast.Subscript](tgt)
if dl_sub is not None and dl_sub.value is not None and dl_sub.slice is not None:
dl_obj: llvmlite.Value | t.CPtr = self.Gen.ExprGen.translate_value(
builder, pool, mod, dl_sub.value, None, 0, trans)
dl_idx: llvmlite.Value | t.CPtr = self.Gen.ExprGen.translate_value(
builder, pool, mod, dl_sub.slice, None, 0, trans)
if dl_obj is not None and dl_idx is not None:
dl_st: llvmlite.LLVMType | t.CPtr = self.Gen.StructGen.get_struct_type_from_value(
dl_st: rawptr[llvmlite.LLVMType] = self.Gen.StructGen.get_struct_type_from_value(
dl_obj)
if dl_st is not None:
dl_cls: str = self.Gen.StructGen.get_class_name_by_type(pool, dl_st)
@@ -149,17 +149,17 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
dl_lookup: str = pool.alloc(128)
if dl_lookup is not None:
viperlib.snprintf(dl_lookup, 128, "%s.__delitem__", dl_cls)
dl_fn: llvmlite.Function | t.CPtr = self.Gen.CallGenBase.find_func_in_module(
dl_fn: rawptr[llvmlite.Function] = self.Gen.CallGenBase.find_func_in_module(
mod, dl_lookup)
if dl_fn is not None:
dl_ret: llvmlite.LLVMType | t.CPtr = llvmlite.function_get_ret_ty(dl_fn)
dl_ret: rawptr[llvmlite.LLVMType] = llvmlite.function_get_ret_ty(dl_fn)
dl_fn_name: str = llvmlite.function_get_name(dl_fn)
if dl_ret is not None and dl_fn_name is not None:
# self 参数(容器)按 __delitem__ 首参类型对齐
dl_self: llvmlite.Value | t.CPtr = dl_obj
dl_first: llvmlite.Param | t.CPtr = llvmlite.function_get_param_head(dl_fn)
if dl_first is not None:
dl_self_ty: llvmlite.LLVMType | t.CPtr = llvmlite.param_get_ty(dl_first)
dl_self_ty: rawptr[llvmlite.LLVMType] = llvmlite.param_get_ty(dl_first)
if dl_self_ty is not None:
dl_self = llvmlite.build_bitcast(builder, dl_obj, dl_self_ty)
if dl_self is not None:
@@ -185,26 +185,26 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
# v1 未实现 finalbody(TestFBProject 未使用;finally+return 交互复杂,
# 后续按 TPC HandlesTry.py L188-214 的 pending_ret 方案扩展)。
# ============================================================
def translate_try(self, trans: HT.Translator | t.CPtr,
node: ast.AST | t.CPtr) -> int:
def translate_try(self, trans: rawptr[HT.Translator],
node: rawptr[ast.AST]) -> int:
"""翻译 try/except 语句,返回 0"""
if trans is None or node is None:
return 0
tr: ast.Try | t.CPtr = (ast.Try | t.CPtr)(node)
tr: rawptr[ast.Try] = rawptr[ast.Try](node)
if tr is None:
return 0
pool: rawptr[memhub.MemBuddy] = trans.Pool
builder: llvmlite.IRBuilder | t.CPtr = trans._cur_builder
func: llvmlite.Function | t.CPtr = trans._cur_func
func: rawptr[llvmlite.Function] = trans._cur_func
if pool is None or builder is None or func is None:
return 0
if llvmlite.builder_cur_block_is_terminated(builder) != 0:
return 0
# ---- 1. 异常编码/消息 alloca(当前 try 上下文)----
i32_ty_t: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
i8_ty_t: llvmlite.LLVMType | t.CPtr = llvmlite.Int8(pool)
i8_ptr_ty_t: llvmlite.LLVMType | t.CPtr = llvmlite.Ptr(pool, i8_ty_t)
i32_ty_t: rawptr[llvmlite.LLVMType] = llvmlite.Int32(pool)
i8_ty_t: rawptr[llvmlite.LLVMType] = llvmlite.Int8(pool)
i8_ptr_ty_t: rawptr[llvmlite.LLVMType] = llvmlite.Ptr(pool, i8_ty_t)
if i32_ty_t is None or i8_ptr_ty_t is None:
return 0
exception_code: llvmlite.Value | t.CPtr = llvmlite.build_alloca(builder, i32_ty_t)
@@ -272,7 +272,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
if tr.children is not None:
tb_n: sizet = len(tr.children)
for tbi in range(tb_n):
stmt_t: ast.AST | t.CPtr = tr.children.get(tbi)
stmt_t: rawptr[ast.AST] = tr.children.get(tbi)
if stmt_t is not None:
self.translate_stmt(trans, stmt_t)
# 正常走完 try 体:清异常码,进 else(若有)或 After
@@ -294,7 +294,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
builder, i32_ty_t, exception_code)
next_check_t: llvmlite.BasicBlock | t.CPtr = None
for i_t in range(n_handlers_t):
hdr_t: ast.ExceptHandler | t.CPtr = (ast.ExceptHandler | t.CPtr)(
hdr_t: rawptr[ast.ExceptHandler] = rawptr[ast.ExceptHandler](
tr.handlers.get(i_t))
match_bb_t: llvmlite.BasicBlock | t.CPtr = match_bbs.get(i_t)
if i_t > 0 and next_check_t is not None:
@@ -307,11 +307,11 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
else:
# 展开匹配编码集(handler.type 自身 + 全部子孙异常类)
hname_t: str = None
htype_t: ast.AST | t.CPtr = hdr_t.type
htype_t: rawptr[ast.AST] = hdr_t.type
if htype_t.kind() == ast.ASTKind.Name:
hname_t = ((ast.Name | t.CPtr)(htype_t)).id
hname_t = (rawptr[ast.Name](htype_t)).id
elif htype_t.kind() == ast.ASTKind.Attribute:
hname_t = ((ast.Attribute | t.CPtr)(htype_t)).attr
hname_t = (rawptr[ast.Attribute](htype_t)).attr
# 匹配编码缓冲:必须是 int*(i32 步长)——collect_descendant_codes 按
# out_codes[i](4 字节)写入;用 sizet*(8 字节)读会读出 64 位拼接垃圾
# (多码时 code[i+1]<<32|code[i];单码时高位恰为 0 掩盖)。
@@ -391,14 +391,14 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
# ---- 7. Except 体 ----
for i_t in range(n_handlers_t):
hdr2_t: ast.ExceptHandler | t.CPtr = (ast.ExceptHandler | t.CPtr)(
hdr2_t: rawptr[ast.ExceptHandler] = rawptr[ast.ExceptHandler](
tr.handlers.get(i_t))
except_bb2_t: llvmlite.BasicBlock | t.CPtr = except_bbs.get(i_t)
llvmlite.position_at_end(builder, except_bb2_t)
if hdr2_t is not None and hdr2_t.children is not None:
hn2_t: sizet = len(hdr2_t.children)
for hi2 in range(hn2_t):
stmt2_t: ast.AST | t.CPtr = hdr2_t.children.get(hi2)
stmt2_t: rawptr[ast.AST] = hdr2_t.children.get(hi2)
if stmt2_t is not None:
self.translate_stmt(trans, stmt2_t)
if llvmlite.builder_cur_block_is_terminated(builder) == 0:
@@ -410,7 +410,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
if tr.orelse is not None:
en_t: sizet = len(tr.orelse)
for ei in range(en_t):
stmt3_t: ast.AST | t.CPtr = tr.orelse.get(ei)
stmt3_t: rawptr[ast.AST] = tr.orelse.get(ei)
if stmt3_t is not None:
self.translate_stmt(trans, stmt3_t)
if llvmlite.builder_cur_block_is_terminated(builder) == 0:
@@ -423,8 +423,8 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
trans._eh_msg_ptr = old_msg_t
return 0
def translate_raise(self, trans: HT.Translator | t.CPtr,
node: ast.AST | t.CPtr) -> int:
def translate_raise(self, trans: rawptr[HT.Translator],
node: rawptr[ast.AST]) -> int:
"""翻译 raise 语句,返回 0
对齐 TPC HandlesRaise.py 的 try-except 语义(v1 迁移):
@@ -440,7 +440,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
return 0
pool: rawptr[memhub.MemBuddy] = trans.Pool
builder: llvmlite.IRBuilder | t.CPtr = trans._cur_builder
mod: llvmlite.LLVMModule | t.CPtr = trans.Module
mod: rawptr[llvmlite.LLVMModule] = trans.Module
if pool is None or builder is None or mod is None:
return 0
rn: ast.Raise | t.CPtr = (ast.Raise | t.CPtr)(node)
@@ -454,18 +454,18 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
ek: int = rn.exc.kind()
is_stop_iter: int = 0
if ek == ast.ASTKind.Name:
enm: ast.Name | t.CPtr = (ast.Name | t.CPtr)(rn.exc)
enm: rawptr[ast.Name] = rawptr[ast.Name](rn.exc)
if enm.id is not None and string.strcmp(enm.id, "StopIteration") == 0:
is_stop_iter = 1
elif ek == ast.ASTKind.Call:
ecl: ast.Call | t.CPtr = (ast.Call | t.CPtr)(rn.exc)
ecl: rawptr[ast.Call] = rawptr[ast.Call](rn.exc)
if ecl.func is not None and ecl.func.kind() == ast.ASTKind.Name:
ecn: ast.Name | t.CPtr = (ast.Name | t.CPtr)(ecl.func)
ecn: rawptr[ast.Name] = rawptr[ast.Name](ecl.func)
if ecn.id is not None and string.strcmp(ecn.id, "StopIteration") == 0:
is_stop_iter = 1
if is_stop_iter != 0:
if trans._stop_iter_flag_ptr is not None:
i1_ty_r: llvmlite.LLVMType | t.CPtr = llvmlite.Int1(pool)
i1_ty_r: rawptr[llvmlite.LLVMType] = llvmlite.Int1(pool)
if i1_ty_r is not None:
one_i1_r: llvmlite.Value | t.CPtr = llvmlite.ConstInt(
pool, i1_ty_r, 1, "1")
@@ -479,19 +479,19 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
exc_code: int = 1
exc_msg: llvmlite.Value | t.CPtr = None
if ek == ast.ASTKind.Constant:
cst_r: ast.Constant | t.CPtr = (ast.Constant | t.CPtr)(rn.exc)
cst_r: rawptr[ast.Constant] = rawptr[ast.Constant](rn.exc)
if cst_r is not None and cst_r.const_kind == ast.CONST_INT:
exc_code = cst_r.int_val
elif ek == ast.ASTKind.Name:
enm2: ast.Name | t.CPtr = (ast.Name | t.CPtr)(rn.exc)
enm2: rawptr[ast.Name] = rawptr[ast.Name](rn.exc)
if enm2.id is not None:
exc_code = self.Gen.ClassGenBase.lookup_exception_code(enm2.id)
if exc_code == 0:
exc_code = 1
elif ek == ast.ASTKind.Call:
ecl2: ast.Call | t.CPtr = (ast.Call | t.CPtr)(rn.exc)
ecl2: rawptr[ast.Call] = rawptr[ast.Call](rn.exc)
if ecl2.func is not None and ecl2.func.kind() == ast.ASTKind.Name:
ecn2: ast.Name | t.CPtr = (ast.Name | t.CPtr)(ecl2.func)
ecn2: rawptr[ast.Name] = rawptr[ast.Name](ecl2.func)
if ecn2.id is not None:
exc_code = self.Gen.ClassGenBase.lookup_exception_code(ecn2.id)
if exc_code == 0:
@@ -537,7 +537,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
# (void→ret_void,ptr→null,float→0.0,其余→零值)。当前块已
# 终结(前一条是 raise/return)时跳过,避免在终止块后插指令。
# ============================================================
def _eh_ret_default(self, trans: HT.Translator | t.CPtr,
def _eh_ret_default(self, trans: rawptr[HT.Translator],
pool: rawptr[memhub.MemBuddy],
builder: llvmlite.IRBuilder | t.CPtr) -> int:
"""异常路径返回当前函数默认零值,返回 0"""
@@ -545,10 +545,10 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
return 0
if llvmlite.builder_cur_block_is_terminated(builder) != 0:
return 0
cur_func: llvmlite.Function | t.CPtr = trans._cur_func
cur_func: rawptr[llvmlite.Function] = trans._cur_func
if cur_func is None:
return 0
ret_ty: llvmlite.LLVMType | t.CPtr = llvmlite.function_get_ret_ty(cur_func)
ret_ty: rawptr[llvmlite.LLVMType] = llvmlite.function_get_ret_ty(cur_func)
if ret_ty is None:
return 0
match ret_ty:
@@ -584,20 +584,20 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
# 退出后恢复,避免内层覆盖导致外层 __exit__ 解析到错误对象。
# 对齐 TPC HandlesWith 语义(不含 __provides__ provider 机制)。
# ============================================================
def translate_with(self, trans: HT.Translator | t.CPtr,
node: ast.AST | t.CPtr) -> int:
def translate_with(self, trans: rawptr[HT.Translator],
node: rawptr[ast.AST]) -> int:
"""翻译 with 语句,返回 0"""
if trans is None or node is None:
return 0
wt: ast.With | t.CPtr = (ast.With | t.CPtr)(node)
wt: rawptr[ast.With] = rawptr[ast.With](node)
if wt is None:
return 0
pool: rawptr[memhub.MemBuddy] = trans.Pool
builder: llvmlite.IRBuilder | t.CPtr = trans._cur_builder
mod: llvmlite.LLVMModule | t.CPtr = trans.Module
mod: rawptr[llvmlite.LLVMModule] = trans.Module
if pool is None or builder is None or mod is None:
return 0
items: list[ast.AST | t.CPtr] | t.CPtr = wt.items
items: list[rawptr[ast.AST]] | t.CPtr = wt.items
if items is None:
return 0
n: sizet = len(items)
@@ -614,7 +614,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
# 1. 全部 item:构造 context 对象 + 调用 __enter__ + asname 绑定
for i in range(n):
item_node: ast.AST | t.CPtr = items.get(i)
item_node: rawptr[ast.AST] = items.get(i)
if item_node is None:
continue
if item_node.kind() != ast.ASTKind.WithItem:
@@ -632,14 +632,14 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
# 与 AnnAssign 的 extract_class_name_from_annotation 一致,跨模块
# 方法调用靠 AnnotClassName 回退按模块名查 SHA1)
cls_name: str = None
ce: ast.AST | t.CPtr = item.context_expr
ce: rawptr[ast.AST] = item.context_expr
if ce.kind() == ast.ASTKind.Call:
cfn: ast.AST | t.CPtr = ((ast.Call | t.CPtr)(ce)).func
cfn: rawptr[ast.AST] = (rawptr[ast.Call](ce)).func
if cfn is not None:
if cfn.kind() == ast.ASTKind.Attribute:
ca: ast.Attribute | t.CPtr = (ast.Attribute | t.CPtr)(cfn)
ca: rawptr[ast.Attribute] = rawptr[ast.Attribute](cfn)
if ca.value is not None and ca.value.kind() == ast.ASTKind.Name:
cmn: ast.Name | t.CPtr = (ast.Name | t.CPtr)(ca.value)
cmn: rawptr[ast.Name] = rawptr[ast.Name](ca.value)
if cmn.id is not None:
ml2: sizet = string.strlen(cmn.id) + string.strlen(ca.attr) + 2
mbuf: str = pool.alloc(ml2)
@@ -649,7 +649,7 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
if cls_name is None:
cls_name = ca.attr
elif cfn.kind() == ast.ASTKind.Name:
cls_name = ((ast.Name | t.CPtr)(cfn)).id
cls_name = (rawptr[ast.Name](cfn)).id
# context 对象存入 __with_ctx(方法调用解析 self 用)
ctx_alloca: llvmlite.Value | t.CPtr = llvmlite.build_alloca(
builder, ctx_val.Ty)
@@ -666,9 +666,9 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
trans, pool, builder, mod, "__enter__")
# asname 绑定(__enter__ 返回值,缺失时退回 context 对象)
if item.optional_vars is not None:
ov: ast.AST | t.CPtr = item.optional_vars
ov: rawptr[ast.AST] = item.optional_vars
if ov.kind() == ast.ASTKind.Name:
asname: str = ((ast.Name | t.CPtr)(ov)).id
asname: str = (rawptr[ast.Name](ov)).id
if asname is not None:
bind_val: llvmlite.Value | t.CPtr = enter_val
if bind_val is None:
@@ -695,11 +695,11 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
trans, pool, builder, mod, fd_cls_bare, ctx_val)
# 2. 翻译 body
body: list[ast.AST | t.CPtr] | t.CPtr = wt.children
body: list[rawptr[ast.AST]] | t.CPtr = wt.children
if body is not None:
bn: sizet = len(body)
for bi in range(bn):
st: ast.AST | t.CPtr = body.get(bi)
st: rawptr[ast.AST] = body.get(bi)
if st is not None:
self.translate_stmt(trans, st)
@@ -731,22 +731,22 @@ class StmtGenMixin(StmtGenDeclMixin.StmtGenDeclMixin):
self.Gen.VarGen.define_var(trans.SymTab, "__with_ctx", old_ctx_alloca)
return 0
def _with_ctx_call(self, trans: HT.Translator | t.CPtr,
def _with_ctx_call(self, trans: rawptr[HT.Translator],
pool: rawptr[memhub.MemBuddy],
builder: llvmlite.IRBuilder | t.CPtr,
mod: llvmlite.LLVMModule | t.CPtr,
mod: rawptr[llvmlite.LLVMModule],
method_name: str) -> llvmlite.Value | t.CPtr:
"""构造并翻译 __with_ctx.<method_name>() 调用(__enter__/__exit__)"""
if pool is None or method_name is None:
return None
name_node: ast.AST | t.CPtr = ast.Name(pool, "__with_ctx", ast.ASTCtx.Load)
name_node: rawptr[ast.AST] = ast.Name(pool, "__with_ctx", ast.ASTCtx.Load)
if name_node is None:
return None
attr_node: ast.AST | t.CPtr = ast.Attribute(
attr_node: rawptr[ast.AST] = ast.Attribute(
pool, name_node, method_name, ast.ASTCtx.Load)
if attr_node is None:
return None
call_node: ast.AST | t.CPtr = ast.Call(pool, attr_node, None, None)
call_node: rawptr[ast.AST] = ast.Call(pool, attr_node, None, None)
if call_node is None:
return None
return self.Gen.ExprGen.translate_value(