实现了 TPV 的 pyi 生成逻辑(部分),删除了直接拷贝自 CPython(TPC) 版本的死代码

This commit is contained in:
2026-07-22 19:47:23 +08:00
parent a99d2a5ad9
commit 751dc72d61
19 changed files with 641 additions and 1436 deletions

View File

@@ -206,19 +206,11 @@ class Translator:
pool: memhub.MemBuddy | t.CPtr = _mbuddy
stdio.printf("[TR] step1 _init_state 前\n")
stdio.fflush(0)
# 初始化状态
self._init_state(pool)
stdio.printf("[TR] step1 _init_state 后\n")
stdio.fflush(0)
# 创建 LLVM 模块
stdio.printf("[TR] step2 new_module 前\n")
stdio.fflush(0)
mod: llvmlite.LLVMModule | t.CPtr = llvmlite.new_module(pool, "main")
stdio.printf("[TR] step2 new_module 后=%d\n", mod)
stdio.fflush(0)
if mod is None:
stdio.printf("[TR] NewModule returned NULL\n")
return 1
@@ -228,11 +220,7 @@ class Translator:
triple: str = Config.TargetTriple
if triple is None:
triple = "x86_64-pc-windows-msvc"
stdio.printf("[TR] step3 module_set_target 前\n")
stdio.fflush(0)
llvmlite.module_set_target(mod, triple)
stdio.printf("[TR] step3 module_set_target 后\n")
stdio.fflush(0)
# 类型
i32_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
@@ -240,12 +228,8 @@ class Translator:
i8_ptr_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Ptr(pool, i8_ty)
# 声明 printf
stdio.printf("[TR] step4 create_declare printf 前\n")
stdio.fflush(0)
printf_func: llvmlite.Function | t.CPtr = llvmlite.create_declare(
pool, mod, "printf", i32_ty)
stdio.printf("[TR] step4 create_declare printf 后=%d\n", printf_func)
stdio.fflush(0)
if printf_func is None:
stdio.printf("[TR] CreateDeclare printf returned NULL\n")
return 1
@@ -253,19 +237,13 @@ class Translator:
printf_func.IsVarArg = 1
# 声明 malloc闭包分配用
stdio.printf("[TR] step5 create_declare malloc 前\n")
stdio.fflush(0)
malloc_func: llvmlite.Function | t.CPtr = llvmlite.create_declare(
pool, mod, "malloc", i8_ptr_ty)
stdio.printf("[TR] step5 create_declare malloc 后\n")
stdio.fflush(0)
if malloc_func is not None:
i64_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int64(pool)
llvmlite.add_param(pool, malloc_func, i64_ty, "size")
# 检查用户是否定义了 main 函数
stdio.printf("[TR] step6 检查 user main 前\n")
stdio.fflush(0)
has_user_main: int = 0
ch: list[ast.AST | t.CPtr] | t.CPtr = tree.children
if ch is not None:
@@ -278,11 +256,7 @@ class Translator:
if string.strcmp(fd.name, "main") == 0:
has_user_main = 1
break
stdio.printf("[TR] step6 检查 user main 后=%d\n", has_user_main)
stdio.fflush(0)
stdio.printf("[TR] step7 _translate_module_level 前\n")
stdio.fflush(0)
if self._declare_only != 0:
# Phase 1a-pre(2=import扫描) / Phase 1a(1=struct注册): 只处理模块级,不创建 main 函数和 builder
self._translate_module_level(pool, mod, tree)
@@ -294,8 +268,6 @@ class Translator:
else:
# 用户已定义 main → 委托 HandlesMain 翻译模块级
self._translate_module_level(pool, mod, tree)
stdio.printf("[TR] step7 _translate_module_level 后\n")
stdio.fflush(0)
return 0
@@ -318,8 +290,6 @@ class Translator:
"""委托 HandlesMain.translate_children() 翻译模块级语句trans 单参)"""
# 全量翻译模式下,先处理导入语句,再创建前向声明,解决前向引用问题
if self._declare_only == 0:
stdio.printf("[TR._translate_module_level] _declare_only==0, 预处理 imports\n")
stdio.fflush(0)
# 预处理导入语句,确保 _imported_modules 和 _from_imports 已填充
# (前向声明需要解析类型注解,如 t.CArray[str] 依赖 t 模块已导入)
ch: list[ast.AST | t.CPtr] | t.CPtr = tree.children
@@ -335,19 +305,9 @@ class Translator:
elif kd == ast.ASTKind.ImportFrom:
self.ImportsH.HandleImportFromModule(child)
self.ImportsH.HandleImportFromNames(child)
stdio.printf("[TR._translate_module_level] 预处理 imports 完成\n")
stdio.fflush(0)
# 创建前向声明
stdio.printf("[TR._translate_module_level] forward_declare_functions 前\n")
stdio.fflush(0)
HandlesFunctions.forward_declare_functions(self, tree)
stdio.printf("[TR._translate_module_level] forward_declare_functions 后\n")
stdio.fflush(0)
stdio.printf("[TR._translate_module_level] translate_children 前\n")
stdio.fflush(0)
added: int = HandlesMain.translate_children(self, tree)
stdio.printf("[TR._translate_module_level] translate_children 后=%d\n", added)
stdio.fflush(0)
# ============================================================
# IR 输出