diff --git a/App/lib/core/Gen/Call/CallGenDispatch.vp b/App/lib/core/Gen/Call/CallGenDispatch.vp index 20a7d9e..59a4944 100644 --- a/App/lib/core/Gen/Call/CallGenDispatch.vp +++ b/App/lib/core/Gen/Call/CallGenDispatch.vp @@ -18,10 +18,10 @@ import lib.core.Gen.Type.TypeSpec as TSpec import lib.Stub.StubMerger as StubMerger import lib.core.CompilerFactory as CompilerFactory from . import CallGenBase -import lib.core.Gen.GenApi as GenApi - - -# ============================================================ +import lib.core.Gen.GenApi as GenApi + + +# ============================================================ # CallGenDispatch - CallGenMixin 拆分(阶段 C):translate_call 检测段 # # 继承 CallGenBase(1 级,TPV self 调用仅支持 1 级父类)。 @@ -813,15 +813,19 @@ novirtual class CallGenDispatch(CallGenBase.CallGenBase): # - 普通类:调用 {Class}.__len__ 函数 # - 都没有:fatal_error # str(Ptr(Int8))保持 strlen(内建语义)。 - len_struct_ty: llvmlite.LLVMType* = \ - self.Gen.StructGen.get_struct_type_from_value(len_val) + len_struct_ty: llvmlite.LLVMType* = self.Gen.StructGen.get_struct_type_from_value(len_val) if len_struct_ty is not None: len_cls_nm: str = self.Gen.StructGen.get_class_name_by_type(pool, len_struct_ty) if len_cls_nm is None: self.Gen.TypeGenErr.fatal_error(len_arg_node, "len() 参数类型推断失败(结构体类名未知)", "TypeError") # list 内置 __len__:读 __count__(GEP 偏移 1,i64) # 判据优先取接收者实际类型(_is_list_builtin_receiver) - if self.Gen.CallGenProp._is_list_builtin_receiver(pool, len_val, len_cls_nm, "__len__", len_arg_node) != 0: + # 目标类型已由 can==1(len 的实参存在)确定为表达式节点 + # ⇒ 在此显式窄化:形参是 ast.Call*(仅用于 fail-fast + # 定位节点),而 len_arg_node 声明为基类 ast.AST*; + # 基类指针传派生类形参属向下转型,严格指针规则下禁止。 + len_arg_cl: ast.Call* = ast.Call*(len_arg_node) + if self.Gen.CallGenProp._is_list_builtin_receiver(pool, len_val, len_cls_nm, "__len__", len_arg_cl) != 0: i64_ty_lb: llvmlite.LLVMType* = llvmlite.Int64(pool) idx_one_lb: llvmlite.Value* = llvmlite.const_int64(pool, 1) cnt_ptr_lb: llvmlite.Value* = llvmlite.build_gep(builder, i64_ty_lb, len_val, idx_one_lb) diff --git a/App/lib/core/Gen/TranslatorGenMixin.vp b/App/lib/core/Gen/TranslatorGenMixin.vp index 83c341a..aa613ba 100644 --- a/App/lib/core/Gen/TranslatorGenMixin.vp +++ b/App/lib/core/Gen/TranslatorGenMixin.vp @@ -133,9 +133,16 @@ novirtual class TranslatorGenMixin: # 避免 Phase2 翻译 App 文件时访问悬空结构体(UAF)。 # 仅解析类型触发特化,不验证结果(1b 全量翻译时重新解析)。 # ============================================================ - def _prescan_funcdef_signatures(self, trans: HT.Translator*, fd: ast.FunctionDef*) -> None: + def _prescan_funcdef_signatures(self, trans: HT.Translator*, node: ast.AST*) -> None: """预解析模块级函数签名的返回/参数类型,触发泛型类特化""" - if trans is None or fd is None: + if trans is None or node is None: + return + # 入口收基类 ast.AST*(模块级调度器只持有 ast.AST* 子节点,与同一处调用的 + # translate_function_def / translate_class_def / _register_cexport_from_funcdef + # 形参口径一致),在此显式窄化为 ast.FunctionDef*:基类指针传派生类形参属 + # 向下转型,严格指针规则下禁止隐式转换。 + fd: ast.FunctionDef* = ast.FunctionDef*(node) + if fd is None: return pool: memhub.MemBuddy* = trans.Pool if pool is None: