尽可能修补 Gen 的导入
This commit is contained in:
1 parent
748eda9c69
commit
ec2feea15b
11 files changed
+24
-33
No files matched your search
@@ -21,6 +21,8 @@ import lib.core.Gen.CallGenBase as CallGenBase
|
||||
import lib.core.Gen.CallGenFd as CallGenFd
|
||||
import lib.core.Gen.CallGenVt as CallGenVt
|
||||
|
||||
import lib.Stub.StubGen.VpiReader as VpiReader
|
||||
|
||||
# ============================================================
|
||||
# AssignGenMixin - 赋值翻译 Mixin(P6 方法化)
|
||||
#
|
||||
@@ -2045,7 +2047,6 @@ class AssignGenMixin:
|
||||
mod_sha1_rhs: str = self.Gen.CallGenBase._lookup_module_sha1(
|
||||
mod_nm_rhs.id)
|
||||
if mod_sha1_rhs is not None:
|
||||
import lib.StubGen.VpiReader as VpiReader
|
||||
temp_dir_rhs: str = None
|
||||
if self.Gen.CallGenBase.TempDir is not None:
|
||||
temp_dir_rhs = self.Gen.CallGenBase.TempDir
|
||||
@@ -2068,7 +2069,6 @@ class AssignGenMixin:
|
||||
cls_sha1 = trans.ModuleSha1
|
||||
ret_cls: str = None
|
||||
if cls_sha1 is not None:
|
||||
import lib.StubGen.VpiReader as VpiReader
|
||||
temp_dir: str = None
|
||||
if self.Gen.CallGenBase.TempDir is not None:
|
||||
temp_dir = self.Gen.CallGenBase.TempDir
|
||||
|
||||
@@ -161,7 +161,7 @@ class CallGenCtor(CallGenBase.CallGenBase):
|
||||
ctor_entry.HasInit = 1
|
||||
else:
|
||||
# 方案2: 检查全局默认参数表(翻译时填充,不依赖 stub 注入)
|
||||
init_defaults_entry: FuncDefaultsEntry | t.CPtr = self.Gen.CallGenLeafKit.find_func_defaults(init_lookup_name)
|
||||
init_defaults_entry: GenTypes.FuncDefaultsEntry | t.CPtr = self.Gen.CallGenLeafKit.find_func_defaults(init_lookup_name)
|
||||
if init_defaults_entry is not None:
|
||||
is_oop = 1
|
||||
if ctor_entry is not None:
|
||||
@@ -516,7 +516,7 @@ class CallGenCtor(CallGenBase.CallGenBase):
|
||||
kw_entry.HasInit = 1
|
||||
else:
|
||||
# 方案2: 检查全局默认参数表(翻译时填充,不依赖 stub 注入)
|
||||
kw_init_defaults: FuncDefaultsEntry | t.CPtr = self.Gen.CallGenLeafKit.find_func_defaults(kw_init_lookup)
|
||||
kw_init_defaults: GenTypes.FuncDefaultsEntry | t.CPtr = self.Gen.CallGenLeafKit.find_func_defaults(kw_init_lookup)
|
||||
if kw_init_defaults is not None:
|
||||
is_oop = 1
|
||||
if kw_entry is not None:
|
||||
|
||||
@@ -17,6 +17,7 @@ import lib.core.Handles.HandlesExpr as HandlesExpr
|
||||
import lib.Stub.StubMerger as StubMerger
|
||||
import lib.core.Handles.HandlesExprCall as HandlesExprCall
|
||||
import lib.core.CompilerFactory as CompilerFactory
|
||||
import lib.Stub.StubGen.VpiReader as VpiReader
|
||||
|
||||
import lib.core.Gen.CallGenBase as CallGenBase
|
||||
import lib.core.Handles.ExprCallTable as ExprCallTable
|
||||
@@ -686,7 +687,6 @@ class CallGenMixin(CallGenBase.CallGenBase):
|
||||
if mod_ret_sha1 is not None:
|
||||
# 函数内懒 import:VpiReader imports HandlesExprCall,
|
||||
# 顶层 import 会成环(与 _lookup_method_ret_ty_via_vpi 一致)
|
||||
import lib.StubGen.VpiReader as VpiReader
|
||||
VpiReader._ensure_vpi_ret_annot_loaded(mod_ret_sha1, self.Gen.CallGenBase.TempDir)
|
||||
ret_annot_fc: str = VpiReader._lookup_vpi_ret_annot(
|
||||
mod_ret_sha1, None, func_name)
|
||||
@@ -831,7 +831,7 @@ class CallGenMixin(CallGenBase.CallGenBase):
|
||||
df_param_count: int = 0
|
||||
# 先从当前模块函数表查找 FuncEntry
|
||||
if trans is not None and trans._funcs is not None:
|
||||
df_entry: FuncEntry | t.CPtr = ExprCallTable.find_func_entry_in_table(
|
||||
df_entry: GenTypes.FuncEntry | t.CPtr = ExprCallTable.find_func_entry_in_table(
|
||||
trans._funcs, trans._func_count, func_name)
|
||||
if df_entry is not None:
|
||||
df_defaults = df_entry.Defaults
|
||||
@@ -842,7 +842,7 @@ class CallGenMixin(CallGenBase.CallGenBase):
|
||||
# 快照才是跨文件永久存活。同模块命中时 gd_entry 也必须已赋值,
|
||||
# 否则下方 _build_const_from_snapshot 读到未初始化栈垃圾指针崩溃。
|
||||
# 与 _translate_method_call 的 mdf_gd 无条件查询(L2677)保持一致。
|
||||
gd_entry: FuncDefaultsEntry | t.CPtr = self.Gen.CallGenLeafKit.find_func_defaults(func_name)
|
||||
gd_entry: GenTypes.FuncDefaultsEntry | t.CPtr = self.Gen.CallGenLeafKit.find_func_defaults(func_name)
|
||||
if df_defaults is None and gd_entry is not None:
|
||||
df_defaults = gd_entry.Defaults
|
||||
df_default_count = gd_entry.DefaultCount
|
||||
|
||||
@@ -20,6 +20,7 @@ import lib.Stub.StubMerger as StubMerger
|
||||
import lib.core.VLogger as VLogger
|
||||
import lib.Projectrans.Config as Config
|
||||
import lib.Projectrans.Utils as Utils
|
||||
import lib.Stub.StubGen.VpiReader as VpiReader
|
||||
|
||||
import lib.core.Gen.ExprGenBase as ExprGenBase
|
||||
import lib.Stub.StubMerger.StubMergerStore as StubMergerStore
|
||||
@@ -345,7 +346,6 @@ class ExprGenCls(ExprGenBase.ExprGenBase):
|
||||
cls_sha1 = trans.ModuleSha1
|
||||
ret_cls: str = None
|
||||
if cls_sha1 is not None:
|
||||
import lib.StubGen.VpiReader as VpiReader
|
||||
temp_dir: str = None
|
||||
# 跨子对象访问经 self.Gen 取 CallGenBase.TempDir(双向引用)
|
||||
if self.Gen.CallGenBase.TempDir is not None:
|
||||
|
||||
@@ -881,11 +881,6 @@ class ExprGenOps(ExprGenBase.ExprGenBase):
|
||||
"""尝试运算符重载:lhs 是结构体指针时调用 dunder 方法,否则返回 None"""
|
||||
if lhs is None or rhs is None:
|
||||
return None
|
||||
|
||||
# 延迟导入避免循环依赖
|
||||
import lib.core.Handles.HandlesStruct as HandlesStruct
|
||||
import lib.core.Handles.HandlesExprCall as HandlesExprCall
|
||||
|
||||
# 映射运算符到 dunder 方法名
|
||||
dunder: str = None
|
||||
if is_compare != 0:
|
||||
@@ -993,11 +988,6 @@ class ExprGenOps(ExprGenBase.ExprGenBase):
|
||||
"""尝试一元运算符重载:operand 是结构体指针时调用 dunder 方法,否则返回 None"""
|
||||
if operand is None:
|
||||
return None
|
||||
|
||||
# 延迟导入避免循环依赖
|
||||
import lib.core.Handles.HandlesStruct as HandlesStruct
|
||||
import lib.core.Handles.HandlesExprCall as HandlesExprCall
|
||||
|
||||
dunder: str = self._unaryop_to_dunder(op)
|
||||
if dunder is None:
|
||||
return None
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import lib.core.Gen.GenTypes as GenTypes
|
||||
import lib.core.Gen.FuncGenMixin as FuncGenMixin
|
||||
import t, c
|
||||
from stdint import *
|
||||
import ast
|
||||
@@ -617,7 +618,7 @@ class FuncGenDef(CallGenBase.CallGenBase):
|
||||
llvmlite.build_store(func_builder, tlone, tls_flag_ref)
|
||||
|
||||
# 进入函数作用域(嵌套符号表)
|
||||
self.Gen.VarGen.enter_scope(trans.SymTab, SCOPE_FUNCTION)
|
||||
self.Gen.VarGen.enter_scope(trans.SymTab, FuncGenMixin.SCOPE_FUNCTION)
|
||||
|
||||
# PEP 695 泛型函数: def f[T](...) — 设置泛型上下文,类型参数映射到 "int" 兜底。
|
||||
# TPV 无泛型函数特化机制(类泛型走 _specialize_generic_class),泛型函数按普通
|
||||
|
||||
@@ -494,7 +494,6 @@ class ImportsGenMixin:
|
||||
# ============================================================
|
||||
def set_current_file(self, file_path: str) -> None:
|
||||
"""设置当前正在编译的文件名(与 HandlesType.set_current_file 同步调用)"""
|
||||
import lib.core.Handles.HandlesType as HandlesType
|
||||
self.Gen.TypeGenResolve.set_current_file(file_path)
|
||||
|
||||
# ============================================================
|
||||
@@ -508,7 +507,6 @@ class ImportsGenMixin:
|
||||
# ============================================================
|
||||
def _report_import_error(self, node: ast.AST | t.CPtr, msg: str) -> None:
|
||||
"""以 Python 风格报错并退出(本地实现,避免循环依赖)"""
|
||||
import lib.core.Handles.HandlesType as HandlesType
|
||||
stdio.printf("Traceback (most recent call last):\n")
|
||||
lineno: int = 0
|
||||
if node is not None:
|
||||
@@ -549,7 +547,6 @@ class ImportsGenMixin:
|
||||
# ============================================================
|
||||
def _is_local_module_file(self, mod_name: str) -> int:
|
||||
"""当前文件同目录存在 {mod_name}.vp/.py → 项目内兄弟模块"""
|
||||
import lib.core.Handles.HandlesType as HandlesType
|
||||
cur: str = self.Gen.TypeGenResolve.get_current_file()
|
||||
if cur is None or mod_name is None:
|
||||
return 0
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import lib.core.Gen.TypeGenMixin as TypeGenMixin
|
||||
import lib.core.Gen.GenTypes as GenTypes
|
||||
import lib.core.Gen.Generator as Generator
|
||||
import t, c
|
||||
@@ -28,7 +29,7 @@ class TypeGenBase:
|
||||
|
||||
# --- 仿 Python Traceback 调用栈(原 _g_trace_frames/_g_trace_count) ---
|
||||
Gen: Generator.Generator | t.CPtr
|
||||
TraceFrames: GenTypes.TraceFrame | t.CPtr # malloc 数组(TRACE_MAX 槽)
|
||||
TraceFrames: GenTypes.TraceFrame | t.CPtr # malloc 数组(TypeGenMixin.TRACE_MAX 槽)
|
||||
TraceCount: int
|
||||
|
||||
# --- 全局泛型特化上下文(原 _g_generic_tp_names/_g_generic_type_args) ---
|
||||
@@ -114,14 +115,14 @@ class TypeGenBase:
|
||||
if self.TraceFrames is not None:
|
||||
return 1
|
||||
entry_size: t.CSizeT = GenTypes.TraceFrame.__sizeof__()
|
||||
self.TraceFrames = CompilerFactory.get_perm_pool().alloc(entry_size * TRACE_MAX)
|
||||
self.TraceFrames = CompilerFactory.get_perm_pool().alloc(entry_size * TypeGenMixin.TRACE_MAX)
|
||||
if self.TraceFrames is None:
|
||||
return 0
|
||||
string.memset(self.TraceFrames, 0, entry_size * TRACE_MAX)
|
||||
string.memset(self.TraceFrames, 0, entry_size * TypeGenMixin.TRACE_MAX)
|
||||
return 1
|
||||
def _get_trace_frame(self, i: int) -> GenTypes.TraceFrame | t.CPtr:
|
||||
"""获取第 i 个 TraceFrame 槽位"""
|
||||
if self.TraceFrames is None or i < 0 or i >= TRACE_MAX:
|
||||
if self.TraceFrames is None or i < 0 or i >= TypeGenMixin.TRACE_MAX:
|
||||
return None
|
||||
entry_size: t.CSizeT = GenTypes.TraceFrame.__sizeof__()
|
||||
addr: t.CUInt64T = t.CUInt64T(self.TraceFrames) + i * entry_size
|
||||
@@ -130,7 +131,7 @@ class TypeGenBase:
|
||||
"""压入一个翻译上下文帧(进入新的翻译作用域时调用)"""
|
||||
if self._ensure_trace_init() == 0:
|
||||
return
|
||||
if self.TraceCount >= TRACE_MAX:
|
||||
if self.TraceCount >= TypeGenMixin.TRACE_MAX:
|
||||
return
|
||||
frame: GenTypes.TraceFrame | t.CPtr = self._get_trace_frame(self.TraceCount)
|
||||
if frame is None:
|
||||
|
||||
@@ -53,7 +53,7 @@ class TypeGenCType(TypeGenBase.TypeGenBase):
|
||||
# 已存在,更新类型字符串
|
||||
self.CTypedefTypeStrs.set(ei, type_str)
|
||||
return
|
||||
self.CTypedefNames.append(name)
|
||||
self.CTypedefNames.append(name) # 存疑
|
||||
self.CTypedefTypeStrs.append(type_str)
|
||||
def lookup_ctypedef(self, name: str) -> str:
|
||||
"""查找 CTypedef,返回类型表达式字符串
|
||||
|
||||
@@ -9,15 +9,16 @@ import string
|
||||
import viperlib
|
||||
import stdio
|
||||
import stdlib
|
||||
import sys
|
||||
import w32.fileio as fileio
|
||||
import lib.core.VLogger as VLogger
|
||||
import lib.Stub.StubMerger as StubMerger
|
||||
import lib.core.CompilerFactory as CompilerFactory
|
||||
import lib.core.Handles.HandlesTranslator as HT
|
||||
|
||||
|
||||
import lib.core.Gen.TypeGenBase as TypeGenBase
|
||||
|
||||
|
||||
# ============================================================
|
||||
# TypeGenErr - TypeGenMixin 拆分(阶段 D):错误上报/源行读取/类型推断(fatal_error/infer_* 等)
|
||||
#
|
||||
@@ -386,4 +387,4 @@ class TypeGenErr(TypeGenBase.TypeGenBase):
|
||||
limit = cap
|
||||
for i in range(limit):
|
||||
dst[dlen + i] = src[i]
|
||||
dst[dlen + i] = '\0'
|
||||
dst[dlen + i] = '\0' # i 退出值存疑
|
||||
@@ -1,6 +1,7 @@
|
||||
import lib.core.Gen.GenTypes as GenTypes
|
||||
import lib.core.Gen.Generator as Generator
|
||||
import lib.core.Gen.TypeGenCType as TypeGenCType
|
||||
import lib.core.Gen.TypeGenMixin as TypeGenMixin
|
||||
import t, c
|
||||
from stdint import *
|
||||
import ast
|
||||
@@ -1028,7 +1029,7 @@ class TypeGenResolve(TypeGenBase.TypeGenBase):
|
||||
# CTypedef 动态查表:查动态 CTypedef 表,递归解析类型表达式
|
||||
# 用户自定义 CTypedef(如 MyHandle: t.CTypedef = VOIDPTR)在此解析
|
||||
# 递归深度限制 MAX_CTYPEDEF_DEPTH 防止无限递归,循环检测防止 A→B→A
|
||||
if self.Gen.TypeGenCType.CTypedefResolveDepth < MAX_CTYPEDEF_DEPTH:
|
||||
if self.Gen.TypeGenCType.CTypedefResolveDepth < TypeGenMixin.MAX_CTYPEDEF_DEPTH:
|
||||
ctypedef_str: str = self.Gen.TypeGenCType.lookup_ctypedef(nm.id)
|
||||
if ctypedef_str is not None and self.Gen.TypeGenCType.CTypedefFound != 0:
|
||||
# 循环检测:将当前 CTypedef 名加入正在解析列表
|
||||
@@ -1089,7 +1090,7 @@ class TypeGenResolve(TypeGenBase.TypeGenBase):
|
||||
# 追加20只补了嵌套 Attribute(mod.sub.Typedef);单层
|
||||
# Module.Typedef(value=Name 非 t/c)同样缺失——shutil.vp
|
||||
# L190 `dirp: posix.DIR` 变量注解解析失败根因。
|
||||
if self.Gen.TypeGenCType.CTypedefResolveDepth < MAX_CTYPEDEF_DEPTH:
|
||||
if self.Gen.TypeGenCType.CTypedefResolveDepth < TypeGenMixin.MAX_CTYPEDEF_DEPTH:
|
||||
mod_td_str: str = self.Gen.TypeGenCType.lookup_ctypedef(at.attr)
|
||||
if mod_td_str is not None and self.Gen.TypeGenCType.CTypedefFound != 0:
|
||||
if self.Gen.TypeGenCType._ctypedef_push_visiting(pool, at.attr) == 0:
|
||||
@@ -1120,7 +1121,7 @@ class TypeGenResolve(TypeGenBase.TypeGenBase):
|
||||
# 与 Name 分支(L1986-2009)同一套 lookup_ctypedef + 递归 + map_t_type 兜底。
|
||||
# 此前只查 struct/enum → w32.win32base.HANDLE 解析失败(fail-fast 报
|
||||
# NameError: 'w32.win32base.HANDLE' is not defined,FileIoTest 根因)。
|
||||
if self.Gen.TypeGenCType.CTypedefResolveDepth < MAX_CTYPEDEF_DEPTH:
|
||||
if self.Gen.TypeGenCType.CTypedefResolveDepth < TypeGenMixin.MAX_CTYPEDEF_DEPTH:
|
||||
nested_td_str: str = self.Gen.TypeGenCType.lookup_ctypedef(at.attr)
|
||||
if nested_td_str is not None and self.Gen.TypeGenCType.CTypedefFound != 0:
|
||||
if self.Gen.TypeGenCType._ctypedef_push_visiting(pool, at.attr) == 0:
|
||||
|
||||
Reference in new issue
Block a user