From 20e31b8dbd3b7d5f45a80cd3733dadda2f362f40 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 24 Oct 2026 17:18:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(overload):=20=E6=8A=BD=E5=87=BA?= =?UTF-8?q?=E4=B8=8D=E4=BE=9D=E8=B5=96=20translator=20=E7=9A=84=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E5=90=8E=E7=BC=80=E8=AE=A1=E7=AE=97=EF=BC=88vpi=20?= =?UTF-8?q?=E4=B8=A4=E7=AB=AF=E5=A4=8D=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _overload_sig_text 拆成 _overload_sig_text_ctx(显式传 imported_modules/from_imports/trans)与薄包装;新增 _overload_sig_suffix_ctx 只返回 _v{16位签名hash}。GenApi.overload_sig_suffix 提供无 translator 的入口,供 vpi 写入端(写 def 名)与读取端(核对注入名)共用同一 hash,避免三处各算一套导致跨模块符号对不上。行为不变(尚无调用方)。注:本提交含此前未提交的既有改动。 --- App/lib/core/Gen/Func/FuncGenDef.vp | 29 +++++++++++++-- App/lib/core/Gen/GenApi.vp | 57 ++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/App/lib/core/Gen/Func/FuncGenDef.vp b/App/lib/core/Gen/Func/FuncGenDef.vp index 4ba5908..8c796d8 100644 --- a/App/lib/core/Gen/Func/FuncGenDef.vp +++ b/App/lib/core/Gen/Func/FuncGenDef.vp @@ -105,6 +105,14 @@ novirtual class FuncGenDef(CallGenBase.CallGenBase): def _overload_sig_text(self, pool: memhub.MemBuddy*, fd: ast.FunctionDef*, trans: HT.Translator*, skip_first: int = 0) -> str: """返回规范形参签名文本(stdlib 缓冲,调用方 free);失败时已报错并返回 None""" if pool is None or fd is None or trans is None: return None + return self._overload_sig_text_ctx(pool, fd, trans._imported_modules, trans._from_imports, trans, skip_first) + + # _overload_sig_text_ctx - 同上,但显式传入 import 上下文,供没有 translator 的场景复用: + # vpi 生成(写 vpi 时 translator 还没创建)与 vpi 读取(注入 declare 时持有可能为 None 的 trans)。 + # 三者(定义端 / vpi 写入端 / vpi 读取端)必须用同一个函数,否则跨模块符号名对不上。 + def _overload_sig_text_ctx(self, pool: memhub.MemBuddy*, fd: ast.FunctionDef*, imported_modules: str, from_imports: str, trans: HT.Translator*, skip_first: int) -> str: + """按注解生成规范形参签名文本;失败时已报错并返回 None""" + if pool is None or fd is None: return None arena: TSpec.TypeSpecArena* = self.Gen.TypeGenBase.ensure_spec_arena() if arena is None: return None buf: str = stdlib.malloc(OVERLOAD_SIG_TEXT_MAX) @@ -133,8 +141,8 @@ novirtual class FuncGenDef(CallGenBase.CallGenBase): ty: mlirlite.LLVMType* = None if arg.annotation is not None: # CVoid 是"空形参"占位,不参与签名 - if self.Gen.TypeGenResolve._is_builtin_marker(arg.annotation, "CVoid", trans._imported_modules) != 0: continue - ty = self.Gen.TypeGenResolve.resolve_annotation_type(pool, arg.annotation, trans._imported_modules, trans._from_imports, trans, 0) + if self.Gen.TypeGenResolve._is_builtin_marker(arg.annotation, "CVoid", imported_modules) != 0: continue + ty = self.Gen.TypeGenResolve.resolve_annotation_type(pool, arg.annotation, imported_modules, from_imports, trans, 0) if ty is None: stdlib.free(buf) stdlib.free(one) @@ -154,7 +162,7 @@ novirtual class FuncGenDef(CallGenBase.CallGenBase): self.Gen.TypeGenErr.fatal_error(arg, f"overloaded function '{fd.name}' parameter '{arg.arg}' has an unsupported type", "TypeError") return None if arg.annotation is not None: - an_text: str = self.Gen.TypeGenResolve.extract_class_name_from_annotation(arg.annotation, trans._imported_modules, pool, trans._from_imports) + an_text: str = self.Gen.TypeGenResolve.extract_class_name_from_annotation(arg.annotation, imported_modules, pool, from_imports) if an_text is not None: sign: int = self.Gen.TypeGenCType.spec_sign_of_annot(an_text) if sign != TSpec.TS_SIGN_UNKNOWN: sp = TSpec.SpecWithSign(arena, sp, sign) @@ -192,6 +200,21 @@ novirtual class FuncGenDef(CallGenBase.CallGenBase): stdlib.free(one) return buf + # _overload_sig_suffix_ctx - 只返回符号后缀 "_v{参数签名hash}"。 + # 供没有 translator 的两端复用:vpi 生成(把后缀写进 def 名)与 vpi 读取(核对注入的 declare 名)。 + def _overload_sig_suffix_ctx(self, pool: memhub.MemBuddy*, fd: ast.FunctionDef*, imported_modules: str, from_imports: str, skip_first: int) -> str: + """返回 "_v{16位签名hash}";失败已报错并返回 None""" + if pool is None or fd is None: return None + sig: str = self._overload_sig_text_ctx(pool, fd, imported_modules, from_imports, None, skip_first) + if sig is None: return None + h: str = TSpec.SpecSigHash16(sig) + stdlib.free(sig) + if h is None: return None + out: str = CompilerFactory.factory.PermPool.alloc(20) + if out is None: return None + viperlib.snprintf(out, 20, "_v%s", h) + return out + # _overload_symbol - 拼出 "{模块sha1}.{name}_v{参数签名hash}"(stdlib 缓冲) def _overload_symbol(self, trans: HT.Translator*, base_name: str, sig_text: str) -> str: """构造重载符号名;模块 sha1 或哈希失败时返回 None""" diff --git a/App/lib/core/Gen/GenApi.vp b/App/lib/core/Gen/GenApi.vp index d5b3c83..9cd7854 100644 --- a/App/lib/core/Gen/GenApi.vp +++ b/App/lib/core/Gen/GenApi.vp @@ -89,13 +89,13 @@ def fatal_error(node: ast.AST*, msg: str, err_type: str = "SyntaxError") -> None # ---------------- map_builtin_type (delegates to TypeGenResolve.map_builtin_type) ---------------- def map_builtin_type(pool: memhub.MemBuddy*, type_name: str) -> mlirlite.LLVMType*: """Delegate to gen.map_builtin_type""" - if _g_gen is not None: _g_gen.TypeGenResolve.map_builtin_type(pool, type_name) + if _g_gen is not None: return _g_gen.TypeGenResolve.map_builtin_type(pool, type_name) return None # ---------------- resolve_typedef_or_map (delegates to TypeGenResolve.resolve_typedef_or_map) ---------------- def resolve_typedef_or_map(pool: memhub.MemBuddy*, name: str, imported_modules: str, from_imports: str, trans: HT.Translator* = None) -> mlirlite.LLVMType*: - """Delegate to gen.resolve_typedef_or_map: CTypedef table first, then map_builtin_type.""" + """Delegate to gen.resolve_typedef_or_map: TypedefT table first, then map_builtin_type.""" if _g_gen is not None: return _g_gen.TypeGenResolve.resolve_typedef_or_map(pool, name, imported_modules, from_imports, trans) return None @@ -137,14 +137,14 @@ def has_decorator_marker(node: ast.AST*, marker_name: str, depth: int = 0) -> in # ---------------- has_pointee_volatile_marker (delegates to TypeGenErr.has_pointee_volatile_marker) ---------------- def has_pointee_volatile_marker(node: ast.AST*) -> int: - """Delegate to gen.has_pointee_volatile_marker (rawptr[t.CVolatile | T] field/var pointee)""" + """Delegate to gen.has_pointee_volatile_marker (rawptr[t.VolatileT | T] field/var pointee)""" if _g_gen is not None: return _g_gen.TypeGenErr.has_pointee_volatile_marker(node) return 0 # ---------------- is_const_object_marker (delegates to TypeGenErr.is_const_object_marker) ---------------- def is_const_object_marker(node: ast.AST*, var_ty: mlirlite.LLVMType*) -> int: - """Delegate to gen.is_const_object_marker (t.CConst non-pointer object field/var)""" + """Delegate to gen.is_const_object_marker (t.ConstT non-pointer object field/var)""" if _g_gen is not None: return _g_gen.TypeGenErr.is_const_object_marker(node, var_ty) return 0 @@ -277,12 +277,40 @@ def get_enum_base_type(enum_name: str) -> mlirlite.LLVMType*: return None +# ---------------- register_enum / add_enum_member (delegates to StmtGenDeclMixin) ---------------- +# 供 .vpi stub 注入登记依赖模块的 `enum X:`:否则跨模块枚举名的类型只能等定义端文件被翻译后 +# 才有(与文件顺序强相关),之前会退化成"把枚举名当类名做前向声明"。 +def register_enum(pool: memhub.MemBuddy*, name: str, base_ty: mlirlite.LLVMType*, sha1: str = None) -> GenTypes.EnumEntry*: + """Delegate to gen.register_enum (键 = (定义模块 SHA1, 名字))。""" + if _g_gen is not None: return _g_gen.StmtGenDecl.register_enum(pool, name, base_ty, sha1) + return None + + +def add_enum_member(pool: memhub.MemBuddy*, entry: GenTypes.EnumEntry*, member_name: str, member_val: int64, member_ty: mlirlite.LLVMType*) -> int: + """Delegate to gen.add_enum_member (dedups by member name; returns the member index or -1).""" + if _g_gen is not None: return _g_gen.StmtGenDecl.add_enum_member(pool, entry, member_name, member_val, member_ty) + return -1 + + +def get_enum_base_type_compat(enum_name: str) -> mlirlite.LLVMType*: + """Delegate to gen.get_enum_base_type_compat(无模块上下文时用:同名基类型一致才返回)。""" + if _g_gen is not None: return _g_gen.StmtGenDecl.get_enum_base_type_compat(enum_name) + return None + + # ---------------- add_visible_struct (delegates to StructGenMixin.add_visible_struct) ---------------- def add_visible_struct(pool: memhub.MemBuddy*, name: str): """Delegate to gen.add_visible_struct""" if _g_gen is not None: _g_gen.StructGen.add_visible_struct(pool, name) +# ---------------- find_struct (delegates to StructGenMixin.find_struct) ---------------- +def find_struct(name: str) -> GenTypes.StructEntry*: + """Delegate to gen.find_struct (supports module-qualified names).""" + if _g_gen is not None: return _g_gen.StructGen.find_struct(name) + return None + + # ---------------- find_struct_by_module (delegates to StructGenMixin.find_struct_by_module) ---------------- def find_struct_by_module(name: str, sha1: str) -> GenTypes.StructEntry*: """Delegate to gen.find_struct_by_module""" @@ -1014,12 +1042,29 @@ def find_func_in_module(mod: mlirlite.LLVMModule*, name: str) -> mlirlite.Functi # ---------------- add_func_to_table (delegates to CallGenLeafKit.add_func_to_table) ---------------- -def add_func_to_table(funcs: GenTypes.FuncEntry*, func_count: int, name: str, func: mlirlite.Function*, max_count: int, defaults: t.CPtr = None, default_count: int = 0, param_count: int = 0, pool: memhub.MemBuddy* = None, def_sha1: str = None) -> int: +def add_func_to_table(funcs: GenTypes.FuncEntry*, func_count: int, name: str, func: mlirlite.Function*, max_count: int, defaults: rawptr = None, default_count: int = 0, param_count: int = 0, pool: memhub.MemBuddy* = None, def_sha1: str = None) -> int: """Delegate to gen.add_func_to_table; def_sha1 is the defining module's SHA1.""" if _g_gen is not None: return _g_gen.CallGenLeafKit.add_func_to_table(funcs, func_count, name, func, max_count, defaults, default_count, param_count, pool, def_sha1) return 0 +# ---------------- overload_sig_suffix (delegates to FuncGenDef._overload_sig_suffix_ctx) ---------------- +# 重载符号后缀 "_v{参数签名hash}":vpi 写入端(把后缀写进 def 名)与读取端(核对注入名)共用, +# 不依赖 translator(vpi 生成时 translator 尚未创建),import 上下文由调用方给出。 +def overload_sig_suffix(pool: memhub.MemBuddy*, fd: ast.AST*, imported_modules: str, from_imports: str, skip_first: int = 0) -> str: + """返回 "_v{16位签名hash}";失败返回 None""" + if _g_gen is None or fd is None: return None + if fd.kind() != ast.ASTKind.FunctionDef: return None + return _g_gen.FuncGenDef._overload_sig_suffix_ctx(pool, ast.FunctionDef*(fd), imported_modules, from_imports, skip_first) + + +# ---------------- register_global_defaults_literals (delegates to CallGenLeafKit) ---------------- +def register_global_defaults_literals(name: str, kinds: int*, ints: int64*, strs: sizet*, count: int, param_count: int, def_sha1: str) -> int: + """Delegate to gen.register_global_defaults_literals; def_sha1 is the defining module's SHA1.""" + if _g_gen is not None: return _g_gen.CallGenLeafKit.register_global_defaults_literals(name, kinds, ints, strs, count, param_count, def_sha1) + return 1 + + # ---------------- init_func_table (delegates to CallGenLeafKit.init_func_table) ---------------- def init_func_table(pool: memhub.MemBuddy*, max_count: int) -> GenTypes.FuncEntry*: """Delegate to gen.init_func_table""" @@ -1028,7 +1073,7 @@ def init_func_table(pool: memhub.MemBuddy*, max_count: int) -> GenTypes.FuncEntr # ---------------- translate_call (delegates to CallGenMixin.translate_call) ---------------- -def translate_call(pool: memhub.MemBuddy*, builder: mlirlite.IRBuilder*, mod: mlirlite.LLVMModule*, node: ast.AST*, funcs_ptr: t.CPtr, func_count: int, trans: HT.Translator* = None) -> mlirlite.Value*: +def translate_call(pool: memhub.MemBuddy*, builder: mlirlite.IRBuilder*, mod: mlirlite.LLVMModule*, node: ast.AST*, funcs_ptr: rawptr, func_count: int, trans: HT.Translator* = None) -> mlirlite.Value*: """Delegate to gen.translate_call""" if _g_gen is not None: return _g_gen.CallGen.translate_call(pool, builder, mod, node, funcs_ptr, func_count, trans) return None