修复了一些错误,更改了一点语法

This commit is contained in:
TermiNexus committed 2026-09-18 20:00:40 +08:00
1 parent 9e2ccbfab7
commit 9910e05760
61 files changed
+2810 -2810

No files matched your search

+68 -68
View File
@@ -47,10 +47,10 @@ class VarGenMixin:
# init_vars — 分配并清零 VarEntry 数组
# ============================================================
Gen: rawptr[Generator.Generator]
def init_vars(self, pool: rawptr[memhub.MemBuddy]) -> GenTypes.VarEntry | t.CPtr:
def init_vars(self, pool: rawptr[memhub.MemBuddy]) -> rawptr[GenTypes.VarEntry]:
"""分配并清零变量表数组"""
size: sizet = MAX_VARS * GenTypes.VarEntry.__sizeof__()
vars_ptr: GenTypes.VarEntry | t.CPtr = pool.alloc(size)
vars_ptr: rawptr[GenTypes.VarEntry] = pool.alloc(size)
if vars_ptr is not None:
string.memset(vars_ptr, 0, size)
return vars_ptr
@@ -58,16 +58,16 @@ class VarGenMixin:
# ============================================================
# find_var — 在变量表中按名称查找
# ============================================================
def find_var(self, vars_ptr: GenTypes.VarEntry | t.CPtr,
def find_var(self, vars_ptr: rawptr[GenTypes.VarEntry],
var_count: int,
name: str) -> llvmlite.Value | t.CPtr:
name: str) -> rawptr[llvmlite.Value]:
"""在变量表中按名称查找"""
if name is None or vars_ptr is None:
return None
entry_size: sizet = GenTypes.VarEntry.__sizeof__()
for i in range(var_count):
entry_addr: uint64 = uint64(vars_ptr) + i * entry_size
entry: GenTypes.VarEntry | t.CPtr = (GenTypes.VarEntry | t.CPtr)(entry_addr)
entry: rawptr[GenTypes.VarEntry] = rawptr[GenTypes.VarEntry](entry_addr)
if entry.Name is not None and entry.Used != 0:
# 快速路径:strcmp 判等蕴含首字符相等,首字符不同者跳过 strcmp
if entry.Name[0] == name[0] and string.strcmp(entry.Name, name) == 0:
@@ -77,16 +77,16 @@ class VarGenMixin:
# ============================================================
# find_var_entry — 在变量表中按名称查找,返回 VarEntry(含 AnnotTy)
# ============================================================
def find_var_entry(self, vars_ptr: GenTypes.VarEntry | t.CPtr,
def find_var_entry(self, vars_ptr: rawptr[GenTypes.VarEntry],
var_count: int,
name: str) -> GenTypes.VarEntry | t.CPtr:
name: str) -> rawptr[GenTypes.VarEntry]:
"""在变量表中按名称查找,返回 VarEntry 或 None"""
if name is None or vars_ptr is None:
return None
entry_size: sizet = GenTypes.VarEntry.__sizeof__()
for i in range(var_count):
entry_addr: uint64 = uint64(vars_ptr) + i * entry_size
entry: GenTypes.VarEntry | t.CPtr = (GenTypes.VarEntry | t.CPtr)(entry_addr)
entry: rawptr[GenTypes.VarEntry] = rawptr[GenTypes.VarEntry](entry_addr)
if entry.Name is not None and entry.Used != 0:
if string.strcmp(entry.Name, name) == 0:
return entry
@@ -95,14 +95,14 @@ class VarGenMixin:
# ============================================================
# lookup_var_entry — 从当前作用域逐级向上查找变量,返回 VarEntry
# ============================================================
def lookup_var_entry(self, symtab: GenTypes.SymbolTable | t.CPtr,
name: str) -> GenTypes.VarEntry | t.CPtr:
def lookup_var_entry(self, symtab: rawptr[GenTypes.SymbolTable],
name: str) -> rawptr[GenTypes.VarEntry]:
"""从当前作用域逐级向上查找变量,返回 VarEntry 或 None"""
if symtab is None or name is None:
return None
scope: GenTypes.Scope | t.CPtr = symtab.Current
scope: rawptr[GenTypes.Scope] = symtab.Current
while scope is not None:
result: GenTypes.VarEntry | t.CPtr = self.find_var_entry(scope.Vars, scope.VarCount, name)
result: rawptr[GenTypes.VarEntry] = self.find_var_entry(scope.Vars, scope.VarCount, name)
if result is not None:
return result
scope = scope.Parent
@@ -115,7 +115,7 @@ class VarGenMixin:
# 方法调用检测时,当 alloca 类型是 Ptr(i8)(联合类型简化),
# 通过 Spec.SourceName 查找实际结构体类型。
# ============================================================
def set_var_annot_class_name(self, symtab: GenTypes.SymbolTable | t.CPtr,
def set_var_annot_class_name(self, symtab: rawptr[GenTypes.SymbolTable],
name: str,
class_name: str) -> int:
"""设置变量的原始类型注解类名(写入 Spec.SourceName),返回 0=成功 / 1=失败
@@ -130,13 +130,13 @@ class VarGenMixin:
"""
if symtab is None or name is None:
return 1
entry: GenTypes.VarEntry | t.CPtr = self.lookup_var_entry(symtab, name)
entry: rawptr[GenTypes.VarEntry] = self.lookup_var_entry(symtab, name)
if entry is None:
return 1
if class_name is None:
# 无注解名:不写入(VarEntry 槽位不复用,原值恒为 None,与清空等价)。
return 0
arena_sn: TSpec.TypeSpecArena | t.CPtr = self.Gen.TypeGenBase.ensure_spec_arena()
arena_sn: rawptr[TSpec.TypeSpecArena] = self.Gen.TypeGenBase.ensure_spec_arena()
if arena_sn is None:
return 1
if entry.Spec is None:
@@ -156,7 +156,7 @@ class VarGenMixin:
# 注解侧由 set_var_annot_class_name 经 SpecWithSource 写入 Spec.SourceName。
# 变量侧类名恢复读取点统一走此入口。
# ============================================================
def var_annot_class_name(self, entry: GenTypes.VarEntry | t.CPtr) -> str:
def var_annot_class_name(self, entry: rawptr[GenTypes.VarEntry]) -> str:
"""变量条目注解类名(Spec.SourceName);未设置返回 None"""
if entry is None:
return None
@@ -169,12 +169,12 @@ class VarGenMixin:
# 该语义由 Spec.ElemBits 承载:上述四类在 LLVM 层都是 Ptr(Int8)(ElemBits=8,
# 无法区分),故注解侧把元素槽宽覆盖为 8 字节,消费点经 var_is_ptr_element 读取。
# 由 AnnAssign 按注解形式(_is_ptr_element_annotation)调用。
def set_var_ptr_element(self, symtab: GenTypes.SymbolTable | t.CPtr,
def set_var_ptr_element(self, symtab: rawptr[GenTypes.SymbolTable],
name: str) -> int:
"""标记变量为 ptr-element(Spec.ElemBits=64),返回 0=成功 / 1=失败"""
if symtab is None or name is None:
return 1
entry: GenTypes.VarEntry | t.CPtr = self.lookup_var_entry(symtab, name)
entry: rawptr[GenTypes.VarEntry] = self.lookup_var_entry(symtab, name)
if entry is None:
return 1
# ptr-element 变量的 alloca 是 Ptr(Ptr(Int8)),其值类型 Ptr(Int8) ⇒ Spec 必为
@@ -192,11 +192,11 @@ class VarGenMixin:
# 判据:PtrLevel>=1 且元素位宽为 8(LLVM 层退化后的 i8*)且 ElemBits==64
# (注解侧覆盖的 8 字节槽)。后两个条件缺一不可:真实 i64* 变量 ElemBits 也为 64
# 但 Elem.Bits=64,不命中;纯 str/bytes/char|t.CPtr 变量 ElemBits=8,同样不命中。
def var_is_ptr_element(self, entry: GenTypes.VarEntry | t.CPtr) -> int:
def var_is_ptr_element(self, entry: rawptr[GenTypes.VarEntry]) -> int:
"""变量是否为 ptr-element 语义(1=是 / 0=否)"""
if entry is None or entry.Spec is None:
return 0
sp: TSpec.TypeSpec | t.CPtr = entry.Spec
sp: rawptr[TSpec.TypeSpec] = entry.Spec
if sp.PtrLevel < 1 or sp.Elem is None:
return 0
if sp.Elem.Bits != 8:
@@ -208,10 +208,10 @@ class VarGenMixin:
# ============================================================
# add_var — 添加变量到变量表
# ============================================================
def add_var(self, vars_ptr: GenTypes.VarEntry | t.CPtr,
def add_var(self, vars_ptr: rawptr[GenTypes.VarEntry],
var_count: int,
name: str,
alloca: llvmlite.Value | t.CPtr) -> int:
alloca: rawptr[llvmlite.Value]) -> int:
"""添加变量到变量表"""
if name is None or alloca is None or vars_ptr is None:
return 1
@@ -219,7 +219,7 @@ class VarGenMixin:
return 1
entry_size: sizet = GenTypes.VarEntry.__sizeof__()
entry_addr: uint64 = uint64(vars_ptr) + var_count * entry_size
entry: GenTypes.VarEntry | t.CPtr = (GenTypes.VarEntry | t.CPtr)(entry_addr)
entry: rawptr[GenTypes.VarEntry] = rawptr[GenTypes.VarEntry](entry_addr)
# 变量名必须深拷贝到 stdlib(跨 pool reset 存活):entry.Name 直接存 AST
# 节点 Name.id 指针时,该段内存被后续分配覆盖会使 find_var 的 strcmp 静默
# 失配 → Handle 阶段找不到 PreScan 已建的同名 alloca → 另建类型退化的
@@ -245,14 +245,14 @@ class VarGenMixin:
# SpecFromLLVMType 不支持者)返回 None —— 后者是设计内的「不适用」,非兜底。
# 分配一律 PermPool 驻留 ⇒ 跨文件/跨 1b reset 安全。
# ============================================================
def _spec_of_var_alloca(self, alloca: llvmlite.Value | t.CPtr) -> TSpec.TypeSpec | t.CPtr:
def _spec_of_var_alloca(self, alloca: rawptr[llvmlite.Value]) -> rawptr[TSpec.TypeSpec]:
"""由 alloca 的值类型构造 TypeSpec,不适用/失败返回 None"""
if alloca is None or alloca.Ty is None:
return None
ty: rawptr[llvmlite.LLVMType] = alloca.Ty
if self.Gen.ExprGenBase.is_ptr_type(ty) != 0:
ty = ty.Pointee
arena: TSpec.TypeSpecArena | t.CPtr = self.Gen.TypeGenBase.ensure_spec_arena()
arena: rawptr[TSpec.TypeSpecArena] = self.Gen.TypeGenBase.ensure_spec_arena()
if arena is None:
return None
return TSpec.SpecFromLLVMType(arena, ty)
@@ -260,8 +260,8 @@ class VarGenMixin:
# _alloca_at_entry - 在函数入口块生成 alloca(确保支配性)
# LLVM IR 要求指令支配所有使用点,故所有 alloca 须在函数入口块生成。
# 实现:临时切换 builder.CurBlock 到入口块,生成 alloca 后恢复。
def _alloca_at_entry(self, builder: llvmlite.IRBuilder | t.CPtr,
ty: rawptr[llvmlite.LLVMType]) -> llvmlite.Value | t.CPtr:
def _alloca_at_entry(self, builder: rawptr[llvmlite.IRBuilder],
ty: rawptr[llvmlite.LLVMType]) -> rawptr[llvmlite.Value]:
"""在函数入口块生成 alloca,返回 alloca 值
使用 llvmlite.build_alloca_at_entry 在入口块终止指令之前插入 alloca,
@@ -273,16 +273,16 @@ class VarGenMixin:
# get_or_create_var — 查找或创建变量 alloca(旧版兼容)
def get_or_create_var(self, pool: rawptr[memhub.MemBuddy],
builder: llvmlite.IRBuilder | t.CPtr,
vars_ptr: GenTypes.VarEntry | t.CPtr,
builder: rawptr[llvmlite.IRBuilder],
vars_ptr: rawptr[GenTypes.VarEntry],
var_count: int,
name: str,
ty: rawptr[llvmlite.LLVMType]) -> llvmlite.Value | t.CPtr:
ty: rawptr[llvmlite.LLVMType]) -> rawptr[llvmlite.Value]:
"""查找或创建变量 alloca"""
existing: llvmlite.Value | t.CPtr = self.find_var(vars_ptr, var_count, name)
existing: rawptr[llvmlite.Value] = self.find_var(vars_ptr, var_count, name)
if existing is not None:
return existing
alloca: llvmlite.Value | t.CPtr = self._alloca_at_entry(builder, ty)
alloca: rawptr[llvmlite.Value] = self._alloca_at_entry(builder, ty)
if alloca is None:
return None
if self.add_var(vars_ptr, var_count, name, alloca) != 0:
@@ -293,10 +293,10 @@ class VarGenMixin:
# _create_scope — 创建新作用域节点(内部辅助函数)
# ============================================================
def _create_scope(self, pool: rawptr[memhub.MemBuddy],
parent: GenTypes.Scope | t.CPtr,
kind: int) -> GenTypes.Scope | t.CPtr:
parent: rawptr[GenTypes.Scope],
kind: int) -> rawptr[GenTypes.Scope]:
"""创建并初始化作用域节点"""
scope: GenTypes.Scope | t.CPtr = pool.alloc(GenTypes.Scope.__sizeof__())
scope: rawptr[GenTypes.Scope] = pool.alloc(GenTypes.Scope.__sizeof__())
if scope is None:
return None
string.memset(scope, 0, GenTypes.Scope.__sizeof__())
@@ -311,18 +311,18 @@ class VarGenMixin:
# ============================================================
# init_symbol_table — 创建符号表(含模块级根作用域)
# ============================================================
def init_symbol_table(self, pool: rawptr[memhub.MemBuddy]) -> GenTypes.SymbolTable | t.CPtr:
def init_symbol_table(self, pool: rawptr[memhub.MemBuddy]) -> rawptr[GenTypes.SymbolTable]:
"""创建并初始化符号表,包含模块级根作用域"""
if pool is None:
return None
symtab: GenTypes.SymbolTable | t.CPtr = pool.alloc(GenTypes.SymbolTable.__sizeof__())
symtab: rawptr[GenTypes.SymbolTable] = pool.alloc(GenTypes.SymbolTable.__sizeof__())
if symtab is None:
return None
string.memset(symtab, 0, GenTypes.SymbolTable.__sizeof__())
symtab.Pool = pool
# 创建根作用域(模块级)
root: GenTypes.Scope | t.CPtr = self._create_scope(pool, None, SCOPE_MODULE)
root: rawptr[GenTypes.Scope] = self._create_scope(pool, None, SCOPE_MODULE)
if root is None:
return None
@@ -333,13 +333,13 @@ class VarGenMixin:
# ============================================================
# enter_scope — 进入新作用域
# ============================================================
def enter_scope(self, symtab: GenTypes.SymbolTable | t.CPtr,
kind: int) -> GenTypes.Scope | t.CPtr:
def enter_scope(self, symtab: rawptr[GenTypes.SymbolTable],
kind: int) -> rawptr[GenTypes.Scope]:
"""进入新作用域,返回新创建的作用域"""
if symtab is None:
return None
pool: rawptr[memhub.MemBuddy] = symtab.Pool
scope: GenTypes.Scope | t.CPtr = self._create_scope(pool, symtab.Current, kind)
scope: rawptr[GenTypes.Scope] = self._create_scope(pool, symtab.Current, kind)
if scope is None:
return None
symtab.Current = scope
@@ -348,7 +348,7 @@ class VarGenMixin:
# ============================================================
# exit_scope — 退出当前作用域
# ============================================================
def exit_scope(self, symtab: GenTypes.SymbolTable | t.CPtr):
def exit_scope(self, symtab: rawptr[GenTypes.SymbolTable]):
"""退出当前作用域,恢复到父作用域"""
if symtab is not None and symtab.Current is not None:
symtab.Current = symtab.Current.Parent
@@ -356,13 +356,13 @@ class VarGenMixin:
# ============================================================
# define_var — 在当前作用域定义变量
# ============================================================
def define_var(self, symtab: GenTypes.SymbolTable | t.CPtr,
def define_var(self, symtab: rawptr[GenTypes.SymbolTable],
name: str,
alloca: llvmlite.Value | t.CPtr) -> int:
alloca: rawptr[llvmlite.Value]) -> int:
"""在当前作用域定义变量,返回 0 成功"""
if symtab is None or name is None or alloca is None:
return 1
scope: GenTypes.Scope | t.CPtr = symtab.Current
scope: rawptr[GenTypes.Scope] = symtab.Current
if scope is None:
return 1
ret: int = self.add_var(scope.Vars, scope.VarCount, name, alloca)
@@ -373,13 +373,13 @@ class VarGenMixin:
# ============================================================
# define_module_var — 在模块作用域定义变量
# ============================================================
def define_module_var(self, symtab: GenTypes.SymbolTable | t.CPtr,
def define_module_var(self, symtab: rawptr[GenTypes.SymbolTable],
name: str,
alloca: llvmlite.Value | t.CPtr) -> int:
alloca: rawptr[llvmlite.Value]) -> int:
"""在模块作用域定义变量,返回 0 成功"""
if symtab is None or name is None or alloca is None:
return 1
scope: GenTypes.Scope | t.CPtr = symtab.Root
scope: rawptr[GenTypes.Scope] = symtab.Root
if scope is None:
return 1
ret: int = self.add_var(scope.Vars, scope.VarCount, name, alloca)
@@ -390,14 +390,14 @@ class VarGenMixin:
# ============================================================
# lookup_var — 从当前作用域逐级向上查找变量
# ============================================================
def lookup_var(self, symtab: GenTypes.SymbolTable | t.CPtr,
name: str) -> llvmlite.Value | t.CPtr:
def lookup_var(self, symtab: rawptr[GenTypes.SymbolTable],
name: str) -> rawptr[llvmlite.Value]:
"""从当前作用域逐级向上查找变量,返回 alloca 或 None"""
if symtab is None or name is None:
return None
scope: GenTypes.Scope | t.CPtr = symtab.Current
scope: rawptr[GenTypes.Scope] = symtab.Current
while scope is not None:
result: llvmlite.Value | t.CPtr = self.find_var(
result: rawptr[llvmlite.Value] = self.find_var(
scope.Vars, scope.VarCount, name)
if result is not None:
return result
@@ -407,12 +407,12 @@ class VarGenMixin:
# ============================================================
# lookup_current — 仅在当前作用域查找变量
# ============================================================
def lookup_current(self, symtab: GenTypes.SymbolTable | t.CPtr,
name: str) -> llvmlite.Value | t.CPtr:
def lookup_current(self, symtab: rawptr[GenTypes.SymbolTable],
name: str) -> rawptr[llvmlite.Value]:
"""仅在当前作用域查找变量"""
if symtab is None or name is None:
return None
scope: GenTypes.Scope | t.CPtr = symtab.Current
scope: rawptr[GenTypes.Scope] = symtab.Current
if scope is None:
return None
return self.find_var(scope.Vars, scope.VarCount, name)
@@ -420,12 +420,12 @@ class VarGenMixin:
# ============================================================
# lookup_module_var — 在模块作用域查找变量
# ============================================================
def lookup_module_var(self, symtab: GenTypes.SymbolTable | t.CPtr,
name: str) -> llvmlite.Value | t.CPtr:
def lookup_module_var(self, symtab: rawptr[GenTypes.SymbolTable],
name: str) -> rawptr[llvmlite.Value]:
"""在模块作用域查找变量"""
if symtab is None or name is None:
return None
scope: GenTypes.Scope | t.CPtr = symtab.Root
scope: rawptr[GenTypes.Scope] = symtab.Root
if scope is None:
return None
return self.find_var(scope.Vars, scope.VarCount, name)
@@ -437,7 +437,7 @@ class VarGenMixin:
# - 模块级代码:AnnAssign 应修改模块级变量(Python 语义)
# - 函数内代码:无 global 声明时 AnnAssign 应创建局部变量(Python 语义)
# ============================================================
def is_module_scope(self, symtab: GenTypes.SymbolTable | t.CPtr) -> int:
def is_module_scope(self, symtab: rawptr[GenTypes.SymbolTable]) -> int:
"""判断当前作用域是否为模块级作用域(Current == Root)
Returns: 1=模块级作用域, 0=函数内作用域
@@ -458,14 +458,14 @@ class VarGenMixin:
# 遍历规则:逐级向上查找;SCOPE_MATCH 边界可穿越(可命中外层 case 声明的变量),
# 遇到第一个非 match 作用域(函数/模块)后停止——不跨函数边界(闭包 nonlocal
# 语义由 is_nonlocal_name 单独处理)。
def _lookup_through_match_scope(self, symtab: GenTypes.SymbolTable | t.CPtr,
name: str) -> llvmlite.Value | t.CPtr:
def _lookup_through_match_scope(self, symtab: rawptr[GenTypes.SymbolTable],
name: str) -> rawptr[llvmlite.Value]:
"""从当前作用域向上,仅穿越 SCOPE_MATCH 作用域查找变量 alloca"""
if symtab is None or name is None:
return None
scope: GenTypes.Scope | t.CPtr = symtab.Current
scope: rawptr[GenTypes.Scope] = symtab.Current
while scope is not None:
result: llvmlite.Value | t.CPtr = self.find_var(
result: rawptr[llvmlite.Value] = self.find_var(
scope.Vars, scope.VarCount, name)
if result is not None:
return result
@@ -478,13 +478,13 @@ class VarGenMixin:
# ============================================================
# get_or_create_sym — 查找或创建变量(在当前作用域)
# ============================================================
def get_or_create_sym(self, symtab: GenTypes.SymbolTable | t.CPtr,
def get_or_create_sym(self, symtab: rawptr[GenTypes.SymbolTable],
pool: rawptr[memhub.MemBuddy],
builder: llvmlite.IRBuilder | t.CPtr,
builder: rawptr[llvmlite.IRBuilder],
name: str,
ty: rawptr[llvmlite.LLVMType]) -> llvmlite.Value | t.CPtr:
ty: rawptr[llvmlite.LLVMType]) -> rawptr[llvmlite.Value]:
"""查找或创建变量 alloca(在当前作用域)"""
existing: llvmlite.Value | t.CPtr = self.lookup_current(symtab, name)
existing: rawptr[llvmlite.Value] = self.lookup_current(symtab, name)
if existing is not None:
return existing
# SCOPE_MATCH 隔离作用域对"赋值"必须透明(详见 _lookup_through_match_scope):
@@ -493,7 +493,7 @@ class VarGenMixin:
existing = self._lookup_through_match_scope(symtab, name)
if existing is not None:
return existing
alloca: llvmlite.Value | t.CPtr = self._alloca_at_entry(builder, ty)
alloca: rawptr[llvmlite.Value] = self._alloca_at_entry(builder, ty)
if alloca is None:
return None
if self.define_var(symtab, name, alloca) != 0: