From 95133d848381371b08fba9d0f5dc0cbed1da44d7 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 11 Sep 2026 22:02:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(tpv):=20=E5=8F=98=E9=87=8F/=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=B3=A8=E8=A7=A3=E7=B1=BB=E5=90=8D=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=B5=B0=20Spec.SourceName=20=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - VarGenMixin 新增 var_annot_class_name(SourceName 优先,回退 AnnotClassName) - 变量侧类名恢复与诊断读取切读:ExprGenAttr(5) / ExprGenOps(6) / ExprGenName(1) / CallGenDispatch(3) / CallGenCall / CallGenProp / CallGenVt / ExprGenCls / AssignGenMixin / MatchGenMixin - ExprGenAttr 字段侧直读改走 get_field_annot_class_name --- App/lib/core/Gen/AssignGenMixin.vp | 2 +- App/lib/core/Gen/Call/CallGenCall.vp | 2 +- App/lib/core/Gen/Call/CallGenDispatch.vp | 13 +++++----- App/lib/core/Gen/Call/CallGenProp.vp | 4 +-- App/lib/core/Gen/Call/CallGenVt.vp | 2 +- App/lib/core/Gen/Expr/ExprGenAttr.vp | 15 ++++++----- App/lib/core/Gen/Expr/ExprGenCls.vp | 2 +- App/lib/core/Gen/Expr/ExprGenName.vp | 5 ++-- App/lib/core/Gen/Expr/ExprGenOps.vp | 33 ++++++++++++++---------- App/lib/core/Gen/MatchGenMixin.vp | 7 ++--- App/lib/core/Gen/VarGenMixin.vp | 16 ++++++++++++ 11 files changed, 63 insertions(+), 38 deletions(-) diff --git a/App/lib/core/Gen/AssignGenMixin.vp b/App/lib/core/Gen/AssignGenMixin.vp index 7f86b2f..fc0f6dc 100644 --- a/App/lib/core/Gen/AssignGenMixin.vp +++ b/App/lib/core/Gen/AssignGenMixin.vp @@ -2383,7 +2383,7 @@ class AssignGenMixin: trans.SymTab, nm.id) if ve is None: return None - return ve.AnnotClassName + return self.Gen.VarGen.var_annot_class_name(ve) # BinOp/UnaryOp: 运算符重载返回同类对象 if k == ast.ASTKind.BinOp: bop: ast.BinOp | t.CPtr = (ast.BinOp | t.CPtr)(node) diff --git a/App/lib/core/Gen/Call/CallGenCall.vp b/App/lib/core/Gen/Call/CallGenCall.vp index 7dc6456..53ace18 100644 --- a/App/lib/core/Gen/Call/CallGenCall.vp +++ b/App/lib/core/Gen/Call/CallGenCall.vp @@ -1911,7 +1911,7 @@ class CallGenCall(CallGenBase.CallGenBase): if self_entry is None: self.Gen.TypeGenErr.fatal_error(cl, "super(): 当前作用域无 self 变量(静态方法内不可用)", "TypeError") return None - cur_class: str = self_entry.AnnotClassName + cur_class: str = self.Gen.VarGen.var_annot_class_name(self_entry) if cur_class is None: self.Gen.TypeGenErr.fatal_error(cl, "super() 只能在类方法内使用(self 无注解类名)") return None diff --git a/App/lib/core/Gen/Call/CallGenDispatch.vp b/App/lib/core/Gen/Call/CallGenDispatch.vp index 798aa39..da3cd55 100644 --- a/App/lib/core/Gen/Call/CallGenDispatch.vp +++ b/App/lib/core/Gen/Call/CallGenDispatch.vp @@ -1053,8 +1053,8 @@ class CallGenDispatch(CallGenBase.CallGenBase): if rk_nm.id is not None: rk_ve: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, rk_nm.id) - if rk_ve is not None and rk_ve.AnnotClassName is not None: - rk_annot_raw: str = rk_ve.AnnotClassName + rk_annot_raw: str = self.Gen.VarGen.var_annot_class_name(rk_ve) + if rk_annot_raw is not None: # 截断联合/泛型后缀(" | t.CPtr" / "[...]") rk_ae: sizet = 0 while rk_annot_raw[rk_ae] != '\0' and rk_annot_raw[rk_ae] != ' ' \ @@ -1256,8 +1256,8 @@ class CallGenDispatch(CallGenBase.CallGenBase): var_entry: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, method_obj_nm.id) is_list_annot_builtin: int = 0 - if var_entry is not None and var_entry.AnnotClassName is not None: - la_buf: str = var_entry.AnnotClassName + la_buf: str = self.Gen.VarGen.var_annot_class_name(var_entry) + if la_buf is not None: la_tail: str = la_buf if string.strncmp(la_buf, "rowptr[", 7) == 0: la_tail = la_buf + 7 @@ -1326,9 +1326,8 @@ class CallGenDispatch(CallGenBase.CallGenBase): # _is_list_builtin_method 前缀匹配不中 → 误走 FakeDuck/sha1 # 方法构造 → _translate_method_call MRO fatal。list 调用由 # 尾部 list 内置分支统一翻译。 - if var_entry is not None and var_entry.AnnotClassName is not None \ - and is_list_annot_builtin == 0: - annot_val: str = var_entry.AnnotClassName + annot_val: str = self.Gen.VarGen.var_annot_class_name(var_entry) + if annot_val is not None and is_list_annot_builtin == 0: # 解析模块限定名 "memhub.MemBuddy" → mod_nm="memhub", cls_nm="MemBuddy" dot_ptr: str = string.strrchr(annot_val, '.') cls_nm2: str = annot_val diff --git a/App/lib/core/Gen/Call/CallGenProp.vp b/App/lib/core/Gen/Call/CallGenProp.vp index 2956a50..7e1485f 100644 --- a/App/lib/core/Gen/Call/CallGenProp.vp +++ b/App/lib/core/Gen/Call/CallGenProp.vp @@ -183,8 +183,8 @@ class CallGenProp(CallGenBase.CallGenBase): if obj_nm.id is not None: var_entry: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, obj_nm.id) - if var_entry is not None and var_entry.AnnotClassName is not None: - annot_val: str = var_entry.AnnotClassName + annot_val: str = self.Gen.VarGen.var_annot_class_name(var_entry) + if annot_val is not None: dot_ptr: str = string.strrchr(annot_val, '.') if dot_ptr is not None: return dot_ptr + 1 diff --git a/App/lib/core/Gen/Call/CallGenVt.vp b/App/lib/core/Gen/Call/CallGenVt.vp index b8d4f1f..9eff0bd 100644 --- a/App/lib/core/Gen/Call/CallGenVt.vp +++ b/App/lib/core/Gen/Call/CallGenVt.vp @@ -194,7 +194,7 @@ class CallGenVt(CallGenBase.CallGenBase): ve_v: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, nm_v.id) if ve_v is not None: - class_nm_v = ve_v.AnnotClassName + class_nm_v = self.Gen.VarGen.var_annot_class_name(ve_v) elif at.value.kind() == ast.ASTKind.Attribute: # 跨 Mixin:_resolve_attr_field_class 定义在 ExprGenAttr,经 self.Gen 调用 class_nm_v = self.Gen.ExprGenAttr._resolve_attr_field_class(pool, trans, at.value) diff --git a/App/lib/core/Gen/Expr/ExprGenAttr.vp b/App/lib/core/Gen/Expr/ExprGenAttr.vp index a4089af..29e906c 100644 --- a/App/lib/core/Gen/Expr/ExprGenAttr.vp +++ b/App/lib/core/Gen/Expr/ExprGenAttr.vp @@ -864,7 +864,7 @@ class ExprGenAttr(ExprGenBase.ExprGenBase): var_entry: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, nm_fb.id) if var_entry is not None: - base_class = var_entry.AnnotClassName + base_class = self.Gen.VarGen.var_annot_class_name(var_entry) elif at.value.kind() == ast.ASTKind.Attribute: base_class = self._resolve_attr_field_class(pool, trans, at.value) if base_class is not None: @@ -951,10 +951,11 @@ class ExprGenAttr(ExprGenBase.ExprGenBase): # 注意:仅当字段类型本身是指针时才 bitcast(指针→指针); # 内嵌结构体字段(field_ty=Struct)load 出的是结构体值, # bitcast 值→指针是非法 IR(llc: invalid cast opcode)。 - if loaded_val is not None and field_info.AnnotClassName is not None \ + fc_annot: str = self.Gen.StructGen.get_field_annot_class_name(field_info) + if loaded_val is not None and fc_annot is not None \ and self.is_ptr_type(field_ty) != 0: annot_struct: GenTypes.StructEntry | t.CPtr = self.Gen.StructGen.find_struct( - field_info.AnnotClassName) + fc_annot) if annot_struct is not None and annot_struct.Ty is not None: # 确保目标结构体类型在当前模块有声明(可能未定义/未 opaque): # 联合字段 AnnotClassName 可能解析到本模块未 import 的类 @@ -998,7 +999,7 @@ class ExprGenAttr(ExprGenBase.ExprGenBase): dve: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, dnm.id) if dve is not None: - diag_annot = dve.AnnotClassName + diag_annot = self.Gen.VarGen.var_annot_class_name(dve) diag_var_txt: str = "?" if diag_var_nm is not None: diag_var_txt = diag_var_nm @@ -1065,7 +1066,7 @@ class ExprGenAttr(ExprGenBase.ExprGenBase): trans.SymTab, nm.id) if var_entry is None: return None - parent_class = var_entry.AnnotClassName + parent_class = self.Gen.VarGen.var_annot_class_name(var_entry) elif at.value.kind() == ast.ASTKind.Attribute: parent_class = self._resolve_attr_field_class(pool, trans, at.value) else: @@ -1207,7 +1208,7 @@ class ExprGenAttr(ExprGenBase.ExprGenBase): var_entry: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, nm_fb.id) if var_entry is not None: - base_class = var_entry.AnnotClassName + base_class = self.Gen.VarGen.var_annot_class_name(var_entry) elif at.value.kind() == ast.ASTKind.Attribute: base_class = self._resolve_attr_field_class(pool, trans, at.value) if base_class is not None: @@ -1291,7 +1292,7 @@ class ExprGenAttr(ExprGenBase.ExprGenBase): dve_g: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, dnm_g.id) if dve_g is not None: - diag_annot_g = dve_g.AnnotClassName + diag_annot_g = self.Gen.VarGen.var_annot_class_name(dve_g) diag_var_txt_g: str = "?" if diag_var_nm_g is not None: diag_var_txt_g = diag_var_nm_g diff --git a/App/lib/core/Gen/Expr/ExprGenCls.vp b/App/lib/core/Gen/Expr/ExprGenCls.vp index 9677ec6..80cd11c 100644 --- a/App/lib/core/Gen/Expr/ExprGenCls.vp +++ b/App/lib/core/Gen/Expr/ExprGenCls.vp @@ -374,7 +374,7 @@ class ExprGenCls(ExprGenBase.ExprGenBase): trans.SymTab, nm.id) if ve is None: return None - acn_n: str = ve.AnnotClassName + acn_n: str = self.Gen.VarGen.var_annot_class_name(ve) if acn_n is None: return None # 去模块前缀:json.JsonValue → JsonValue。AnnotClassName 可能带 diff --git a/App/lib/core/Gen/Expr/ExprGenName.vp b/App/lib/core/Gen/Expr/ExprGenName.vp index 9d0d0ef..c6eae81 100644 --- a/App/lib/core/Gen/Expr/ExprGenName.vp +++ b/App/lib/core/Gen/Expr/ExprGenName.vp @@ -1197,8 +1197,9 @@ class ExprGenName(ExprGenBase.ExprGenBase): # 语义 *np=(i64)x)。GEP 元素必须用 Int64(标量)而非指针, # 否则元素类型为指针 → i64 存储触发整数隐式转指针 # fail-fast(把解引用写入误判为 int→ptr)。 - if ve_pe.AnnotClassName is not None \ - and self._annot_is_64bit_scalar(ve_pe.AnnotClassName) != 0: + pe_annot: str = self.Gen.VarGen.var_annot_class_name(ve_pe) + if pe_annot is not None \ + and self._annot_is_64bit_scalar(pe_annot) != 0: return llvmlite.build_gep( builder, llvmlite.Int64(pool), ptr_val_pe, idx_val) match pointee: diff --git a/App/lib/core/Gen/Expr/ExprGenOps.vp b/App/lib/core/Gen/Expr/ExprGenOps.vp index 4e8102f..3237c6c 100644 --- a/App/lib/core/Gen/Expr/ExprGenOps.vp +++ b/App/lib/core/Gen/Expr/ExprGenOps.vp @@ -268,8 +268,9 @@ class ExprGenOps(ExprGenBase.ExprGenBase): # [S4 切读] Spec 明确无符号(TS_UINT)优先于字符串判定 if self._entry_spec_unsigned(ve.Spec) != 0: return 1 - if ve.AnnotClassName is not None: - if self._type_name_unsigned(ve.AnnotClassName) != 0: + vcn_u: str = self.Gen.VarGen.var_annot_class_name(ve) + if vcn_u is not None: + if self._type_name_unsigned(vcn_u) != 0: return 1 return 0 if k == ast.ASTKind.BinOp: @@ -298,8 +299,9 @@ class ExprGenOps(ExprGenBase.ExprGenBase): # 时 Spec 仍是 Ptr/Array ⇒ 返回 0,自动回退字符串路径) if self._entry_spec_unsigned(ve_su.Spec) != 0: return 1 - if ve_su.AnnotClassName is not None: - if self._annot_str_unsigned(ve_su.AnnotClassName) != 0: + scn_u: str = self.Gen.VarGen.var_annot_class_name(ve_su) + if scn_u is not None: + if self._annot_str_unsigned(scn_u) != 0: return 1 return 0 if k == ast.ASTKind.Attribute: @@ -320,7 +322,7 @@ class ExprGenOps(ExprGenBase.ExprGenBase): ve_u: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, vn_u.id) if ve_u is not None: - cls_nm_u = ve_u.AnnotClassName + cls_nm_u = self.Gen.VarGen.var_annot_class_name(ve_u) if cls_nm_u is not None: # 取裸类名(点前部分):只做裸名结构体查找,避免模块限定名解析 bare_u: str = cls_nm_u @@ -339,8 +341,9 @@ class ExprGenOps(ExprGenBase.ExprGenBase): # [S4 切读] 字段 Spec 明确无符号优先 if self._entry_spec_unsigned(fe_u.Spec) != 0: return 1 - if fe_u.AnnotClassName is not None: - if self._annot_str_unsigned(fe_u.AnnotClassName) != 0: + fcn_u: str = self.Gen.StructGen.get_field_annot_class_name(fe_u) + if fcn_u is not None: + if self._annot_str_unsigned(fcn_u) != 0: return 1 break fi_u += 1 @@ -379,9 +382,11 @@ class ExprGenOps(ExprGenBase.ExprGenBase): if trans is not None: ve: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, nm.id) - if ve is not None and ve.AnnotClassName is not None: - if self._spec_sign_from_annot(ve.AnnotClassName) == TSpec.TS_SIGN_SIGNED: - return 1 + if ve is not None: + ncn_s: str = self.Gen.VarGen.var_annot_class_name(ve) + if ncn_s is not None: + if self._spec_sign_from_annot(ncn_s) == TSpec.TS_SIGN_SIGNED: + return 1 return 0 if k == ast.ASTKind.Subscript: sub_s: ast.Subscript | t.CPtr = (ast.Subscript | t.CPtr)(node) @@ -390,9 +395,11 @@ class ExprGenOps(ExprGenBase.ExprGenBase): if snm.id is not None and trans is not None: ve_s: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, snm.id) - if ve_s is not None and ve_s.AnnotClassName is not None: - if self._spec_sign_from_annot(ve_s.AnnotClassName) == TSpec.TS_SIGN_SIGNED: - return 1 + if ve_s is not None: + scn_s: str = self.Gen.VarGen.var_annot_class_name(ve_s) + if scn_s is not None: + if self._spec_sign_from_annot(scn_s) == TSpec.TS_SIGN_SIGNED: + return 1 return 0 return 0 def _last_dot_tail(self, name: str) -> str: diff --git a/App/lib/core/Gen/MatchGenMixin.vp b/App/lib/core/Gen/MatchGenMixin.vp index dbf9647..e7777f9 100644 --- a/App/lib/core/Gen/MatchGenMixin.vp +++ b/App/lib/core/Gen/MatchGenMixin.vp @@ -134,11 +134,12 @@ class MatchGenMixin: if nm.id is not None: entry: GenTypes.VarEntry | t.CPtr = self.Gen.VarGen.lookup_var_entry( trans.SymTab, nm.id) - if entry is not None and entry.AnnotClassName is not None \ + entry_annot: str = self.Gen.VarGen.var_annot_class_name(entry) + if entry_annot is not None \ and self.Gen.ClassGenEnum._is_renum_class_name( - entry.AnnotClassName) != 0: + entry_annot) != 0: is_renum = 1 - renum_name = entry.AnnotClassName + renum_name = entry_annot subject_ptr = self.Gen.VarGen.lookup_var(trans.SymTab, nm.id) # 2b. 若未确定,遍历 cases 查 MatchClass 变体 diff --git a/App/lib/core/Gen/VarGenMixin.vp b/App/lib/core/Gen/VarGenMixin.vp index 9b025bf..8468c30 100644 --- a/App/lib/core/Gen/VarGenMixin.vp +++ b/App/lib/core/Gen/VarGenMixin.vp @@ -143,6 +143,22 @@ class VarGenMixin: entry.Spec = TSpec.SpecWithSource(entry.Spec, class_name) return 0 + # ============================================================ + # var_annot_class_name - 变量条目注解类名的统一读取入口(S4 切读·类名块) + # + # Spec.SourceName 优先(注解侧由 set_var_annot_class_name 经 SpecWithSource 填), + # 未设置时回退旧字段 AnnotClassName。变量侧类名恢复读取点统一走此入口。 + # ============================================================ + def var_annot_class_name(self, entry: GenTypes.VarEntry | t.CPtr) -> str: + """变量条目注解类名;未设置返回 None""" + if entry is None: + return None + if entry.Spec is not None: + sn: str = TSpec.SpecSourceName(entry.Spec) + if sn is not None: + return sn + return entry.AnnotClassName + # ============================================================ # set_var_ptr_element — 标记变量为"指针到 str/bytes"类型 #