From 2dd96793e19eb5debf1c80b669d517f9a9c20bf6 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Thu, 1 Oct 2026 22:25:04 +0800 Subject: [PATCH] =?UTF-8?q?docs(wiki):=20=E5=AF=B9=E8=B1=A1=E5=86=99?= =?UTF-8?q?=E6=B3=95=E6=94=B9=E7=94=A8=20virtual/novirtual=EF=BC=8C@t.Obje?= =?UTF-8?q?ct=20=E9=BB=98=E8=AE=A4=E4=B8=8D=E5=86=99=E5=8F=AA=E4=BD=9C?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02-type-system.md | 2 +- 05-classes.md | 2 +- 06-oop.md | 94 ++++++++++++++------------------------ 08-c-operations.md | 2 +- 10-imports.md | 6 +-- 13-bootstrapping.md | 6 +-- 14-oo-refactor.md | 20 ++++---- 15-multiple-inheritance.md | 5 +- README.MD | 6 +-- 9 files changed, 57 insertions(+), 86 deletions(-) diff --git a/02-type-system.md b/02-type-system.md index 9c209e5..09d3ebf 100644 --- a/02-type-system.md +++ b/02-type-system.md @@ -373,7 +373,7 @@ if p == 0: ... # ❌ 不用:虽同样编译为 icmp eq , null #### 读"对象首字"(vptr)的唯一可用写法 -`@t.CVTable` 类对象的 **vptr 在偏移 0**(IR:`getelementptr %T, %T* %p, i32 0, i32 0` + `load i8*`), +`virtual` 类对象的 **vptr 在偏移 0**(IR:`getelementptr %T, %T* %p, i32 0, i32 0` + `load i8*`), `kind()` 等是**虚调用**(vptr 为 0 ⇒ `load [0 + slot]` ⇒ ASan `READ at 0x0`)。 因此"对象有效性校验"必须能读到首字: diff --git a/05-classes.md b/05-classes.md index 485f067..0a883af 100644 --- a/05-classes.md +++ b/05-classes.md @@ -259,7 +259,7 @@ novirtual class GNode(GSListNode[GNode]): # 递归泛型继承 # 此时 GNode.Next: GNode* —— 强类型,零转型 ``` -这是 C(`void*` + cast)、C++(模板继承但无 `@t.NoVTable` 精细控制)、Rust(无继承,需 trait bound)均无法如此简洁表达的能力。 +这是 C(`void*` + cast)、C++(模板继承但无 `novirtual` 精细控制)、Rust(无继承,需 trait bound)均无法如此简洁表达的能力。 ### 实例化 diff --git a/06-oop.md b/06-oop.md index d1d36be..87c4742 100644 --- a/06-oop.md +++ b/06-oop.md @@ -1,16 +1,18 @@ # 06 - 面向对象与运算符重载 -Viper 中的 `class` 有两种语义:**数据布局**(结构体/联合体/枚举,见 [05-classes.md](05-classes.md))和**面向对象**。OOP 能力由**类内是否存在方法**自动判定(`StructEntry.IsOOP`);多态通过 `@t.CVTable`(或软关键字 `virtual class`)启用,非多态继承(C++ 风格,无 vtable 开销)通过 `@t.NoVTable`(或 `novirtual class`)启用。`t.Object` 亦可作为**标记基类**(`class X(t.Object)`)。 +Viper 中的 `class` 有两种语义:**数据布局**(结构体/联合体/枚举,见 [05-classes.md](05-classes.md))和**面向对象**。OOP 能力由**类内是否存在方法**自动判定(`StructEntry.IsOOP`);多态用 `virtual`,非多态继承(C++ 风格,无 vtable 开销)用 `novirtual`。 -## 类修饰符(`virtual` / `novirtual` 软关键字) +> `@t.Object` 装饰器默认**不需要写、也尽量避免使用**——OOP 由「类内是否有方法」自动判定,写与不写行为一致。这是本文唯一一次提及,新代码请直接省略。 -类级 / 方法级的 `@t.CVTable` / `@t.NoVTable` 也可写成前置软关键字——解析器把它们合成为同一套装饰器表示: +## 类修饰符(`virtual` / `novirtual`) + +`virtual` / `novirtual` 是前置软关键字,**类级与方法级都可用**: ```python -virtual class Shape: # 等价 @t.CVTable class Shape +virtual class Shape: # 多态(含 vtable) ... -novirtual class Node: # 等价 @t.NoVTable class Node +novirtual class Node: # 非多态继承(零 vtable 开销) novirtual def helper(self) -> int: ... ``` @@ -18,16 +20,15 @@ novirtual class Node: # 等价 @t.NoVTable class Node - `virtual class` / `novirtual class`:**类级**修饰符; - `virtual def` / `novirtual def`:**方法级**修饰符,只能直接写在类体内; - `novirtual class` 内写 `virtual def` 合法(仅该方法进虚表),反之亦然; -- 非法形态(如修饰符位置错误、`virtual virtual`)会编译期 fail-fast。 +- 非法形态(修饰符位置错误、`virtual virtual` 等)编译期 fail-fast。 ## 面向对象(OOP) -类内定义成员方法即拥有面向对象支持(成员方法、运算符重载、属性装饰器等)。编译器根据「结构体内是否有函数定义」自动判定 OOP 行为;`@t.Object` 装饰器在 TransPyV 中并无专门分支,可视为标记,实际工程中保持写法一致即可。 +类内定义成员方法即拥有面向对象支持(成员方法、运算符重载、属性装饰器等);编译器根据「结构体内是否有函数定义」自动判定 OOP 行为。 ### 基本定义 ```python -@t.Object class ViperKernel: BootInfo: bootinfo* VGA: vga._VGAScreenDriver* @@ -44,7 +45,6 @@ class ViperKernel: ### 成员方法 ```python -@t.Object class Sheet: id: int x: int @@ -82,7 +82,6 @@ sheet.Resize(800, 600) > `@xxx.setter` 和自定义装饰器正在试验阶段,尽可能不要使用 ```python -@t.Object class MyObj: _value: int @@ -105,7 +104,7 @@ def _yield(): ### 对象指针与解引用 -`@t.Object` 的实例通常通过指针操作: +对象类实例通常通过指针操作: ```python obj_ptr: MyClass* = &MyClass() @@ -125,10 +124,9 @@ def method(self) -> void: ### 内嵌对象 -`@t.Object` 可以包含其他对象实例(非指针): +对象类可以包含其他对象实例(非指针): ```python -@t.Object class Sheet: _surf_obj: gfx.Surface _renderer_obj: gfx.Renderer @@ -144,12 +142,12 @@ class Sheet: ### `__before_init__` 方法 -仅当类内含方法或继承虚方法时才生成 `__before_init__` 方法(纯数据 `class` 不生成)。它在 `__init__` 之前**自动填充成员默认值**,并对 `@t.CVTable` 类初始化 vtable: +仅当类内含方法或继承虚方法时才生成 `__before_init__` 方法(纯数据 `class` 不生成)。它在 `__init__` 之前**自动填充成员默认值**,并对 `virtual` 类初始化 vtable: ```llvm define void @".ClassName.__before_init__"(%".ClassName"* %self) { entry: - ; 对 @t.CVTable 类:存储 vtable 指针到 slot 0 + ; 对 virtual 类:存储 vtable 指针到 slot 0 ; 遍历所有成员:若有默认值则 store 默认值,否则 store 零值 ret void } @@ -158,7 +156,7 @@ entry: **职责**: - 填充类定义中声明的成员默认值(如 `x: int = 0` 中的 `0`) - 对无默认值的成员填充类型零值 -- 对 `@t.CVTable` 类额外初始化 vtable 指针 +- 对 `virtual` 类额外初始化 vtable 指针 **调用时机**(由编译器在构造时自动插入): 1. 不存在 `__new__` 时:在 `alloca` 分配之后调用一次,随后进入 `__init__` @@ -167,7 +165,7 @@ entry: **关于 `__new__`**: - `__new__` 返回 `self` 时使用默认的 `alloca` 存储 - `__new__` 的签名可与 `__init__` 不同 -- `__new__` 与 `@t.CVTable` 兼容 +- `__new__` 与 `virtual` 兼容 - `__new__` 本身不入虚表 **就地构造**:用户也可手动调用 `__before_init__` 实现就地构造(placement new 语义),典型用法见 `mpool.alloc_buf`: @@ -180,11 +178,11 @@ buf.__init__(args) # 用户初始化逻辑 跨模块导入类时,导入侧也会声明 `__before_init__`,保证跨模块构造行为一致。 -## `@t.NoVTable` 非多态继承 +## `novirtual` 非多态继承 -`@t.NoVTable` 启用 C++ 风格的非多态继承(对应 `struct A : B {}` 无 virtual)。与 `@t.CVTable` 的区别: +`novirtual` 启用 C++ 风格的非多态继承(对应 `struct A : B {}` 无 virtual)。与 `virtual` 的区别: -| 特性 | `@t.NoVTable` | `@t.CVTable` | +| 特性 | `novirtual` | `virtual` | |------|---------------|--------------| | vtable 指针 | ❌ 无 | ✅ 自动插入 slot 0 | | 虚派发 | ❌ 无 | ✅ 支持 | @@ -195,14 +193,12 @@ buf.__init__(args) # 用户初始化逻辑 ### 基本定义 ```python -@t.NoVTable -class LinkedNode: +novirtual class LinkedNode: next: LinkedNode* prev: LinkedNode* def append(self, node: LinkedNode*): ... -@t.NoVTable -class GNode(LinkedNode): # 非多态继承 +novirtual class GNode(LinkedNode): # 非多态继承 value: int ``` @@ -216,7 +212,7 @@ class GNode(LinkedNode): # 非多态继承 ### 跨模块继承 -跨模块父类(如 `linkedlist.LinkedNode`)的 `@t.NoVTable` 标记会在导入时被检测并记录,子类继承时同样跳过 vtable 插入。 +跨模块父类(如 `linkedlist.LinkedNode`)的 `novirtual` 标记会在导入时被检测并记录,子类继承时同样跳过 vtable 插入。 ### 向下转型 @@ -231,27 +227,25 @@ node: Node* = (Node*)(child) 隐式转型仅向上(子→父)。 -## `@t.CVTable` 虚函数表与多态 +## `virtual` 虚函数表与多态 > **规划**:将引入 `T1 & T2`(TypeIntersection)表达 object(尤其是带 vtable 的 object)的交集类型,作为多态对象组合的静态类型手段(详见 02-type-system.md 类型组合规划演进,尚未实现)。 > 尽可能避免使用 `self[0]` 来获取结构体成员,其不稳定,也不要手动操作虚表。 -使用 `@t.CVTable`(或 `virtual class`)启用虚函数表支持。启用后,结构体第一个成员自动插入 `i8*` 类型的 vtable 指针。类内需有方法(OOP 行为由「结构体内是否有函数」自动判定)。 +用 `virtual`(类级)启用虚函数表支持。启用后,结构体第一个成员自动插入 `i8*` 类型的 vtable 指针。类内需有方法(OOP 行为由「结构体内是否有函数」自动判定)。 ### 基本定义 ```python -@t.Object -@t.CVTable -class Base: +virtual class Base: def __init__(self): pass def method(self) -> int: return 0 ``` -在 LLVM IR 中,`@t.CVTable` 类的结构体类型自动插入 vtable 指针: +在 LLVM IR 中,`virtual` 类的结构体类型自动插入 vtable 指针: ```llvm %".Base" = type { i8*, i32 } @@ -270,17 +264,13 @@ class Base: 虚表可以帮你使用继承语法实现多态: ```python -@t.Object -@t.CVTable -class Base: +virtual class Base: def __init__(self): pass def method(self) -> int: return 0 -@t.Object -@t.CVTable -class M1(Base): +virtual class M1(Base): def method(self) -> int: return 1 ``` @@ -294,9 +284,7 @@ class M1(Base): 如果需要临时构造一个额外的 `Base` 结构体,还可以使用: ```python -@t.Object -@t.CVTable -class Base: +virtual class Base: def __init__(self): pass def method(self) -> int: @@ -326,10 +314,10 @@ export def main() -> int: ### 方法级虚表开关 -类级装饰器 `@t.CVTable`/`@t.NoVTable` 决定类级默认策略,方法级装饰器可覆盖类级默认: +`virtual` / `novirtual` 决定类级默认策略,方法级修饰符可覆盖类级默认: -- `@t.NoVTable` 类中单独标注 `@t.CVTable` 的方法进入虚表 -- `@t.CVTable` 类中单独标注 `@t.NoVTable` 的方法被排除在虚表之外 +- `novirtual` 类中单独标注 `virtual` 的方法进入虚表 +- `virtual` 类中单独标注 `novirtual` 的方法被排除在虚表之外 以上行为由 `func_vtable_test` 实证。 @@ -338,15 +326,11 @@ export def main() -> int: `super().method(...)` 形态被编译为**静态父类方法调用**(仅该形态被支持),不经过 vtable 动态派发: ```python -@t.Object -@t.CVTable -class Base: +virtual class Base: def method(self) -> void: pass -@t.Object -@t.CVTable -class Derived(Base): +virtual class Derived(Base): def method(self) -> void: super().method() # 静态调用父类实现 ``` @@ -362,7 +346,6 @@ class Derived(Base): - 父类的方法被继承(重命名为 `子类名.方法名`) ```python -@t.Object class Animal: name: [32]char age: int @@ -373,7 +356,6 @@ class Animal: def Speak(self) -> void: serial.puts("...\n") -@t.Object class Dog(Animal): breed: int @@ -397,7 +379,7 @@ class SpecificError(MyError): ## 运算符重载 -`@t.Object` 类支持运算符重载。编译器在遇到运算符时,首先检查左操作数是否为结构体类型,如果是则尝试调用对应的 dunder 方法。 +对象类支持运算符重载。编译器在遇到运算符时,首先检查左操作数是否为结构体类型,如果是则尝试调用对应的 dunder 方法。 ### 算术运算符重载 @@ -416,7 +398,6 @@ class SpecificError(MyError): 示例: ```python -@t.Object class Vec2: x: float y: float @@ -484,7 +465,6 @@ class Vec2: > **期望实现**:当前未调度 `__call__`,`obj(args)` 不会自动分发到该方法。 ```python -@t.Object class Counter: value: int @@ -528,7 +508,6 @@ def __delitem__(self, key: int): > **期望实现**:当前条件真值对 struct 值恒真/指针判非空,不调度 `__bool__`。 ```python -@t.Object class Buffer: size: int data: [1024]char @@ -544,7 +523,6 @@ if buf: ## 长度 `__len__` ```python -@t.Object class Buffer: size: int @@ -560,7 +538,6 @@ n: int = len(buf) > **期望实现**:`print` 是类型感知格式化(`str`/`%s`、`int`、`float`、`struct` 输出占位符),当前**不调度** `__str__`。 ```python -@t.Object class Point: x: int y: int @@ -577,7 +554,6 @@ print(p) # 类型感知格式化:不调用 p.__str__() > **期望实现**:当前 `abs()` 不调度 `__abs__`(`abs` 为 C 库裸名调用/TPC 内建,见 11-builtins.md)。 ```python -@t.Object class SignedValue: val: int @@ -598,7 +574,6 @@ Viper 的迭代器协议与 Python 相同。`__next__` 带编译器注入的隐 ### `__iter__` 和 `__next__` ```python -@t.Object class IntRange: start: int end: int @@ -646,7 +621,6 @@ while (1) { `with` 语句要求对象实现 `__enter__` 和 `__exit__` 方法: ```python -@t.Object class File: fd: int path: const char* diff --git a/08-c-operations.md b/08-c-operations.md index 6919e26..c9cb48a 100644 --- a/08-c-operations.md +++ b/08-c-operations.md @@ -349,4 +349,4 @@ irout(expr) # 输出操作数:为 expr 变量分配唯一 SSA 寄存器 %l ## 运算符重载 -`@t.Object` 类支持运算符重载,详见 [06-oop.md 中 运算符重载](06-oop.md#运算符重载)。 +对象类支持运算符重载,详见 [06-oop.md 中 运算符重载](06-oop.md#运算符重载)。 diff --git a/10-imports.md b/10-imports.md index 3ec1302..875d5fc 100644 --- a/10-imports.md +++ b/10-imports.md @@ -152,9 +152,9 @@ func: Function* = mod_core.CreateFunction(mod, "main", ret_ty) 类型系统用 `t.REnum`(`LLVMType`,少变体适合 match),指令 opcode 用 `t.CEnum`(`IROp`),对象分配用普通类 + `mpool`。详见 [includes/llvmlite/README.md](../includes/llvmlite/README.md) 的选型论证。 -### `linkedlist` 模块(NoVTable + PEP 695 泛型示范) +### `linkedlist` 模块(novirtual + PEP 695 泛型示范) -[includes/linkedlist](../includes/linkedlist.vp) 提供链表实现,是 `@t.NoVTable` 非多态继承和 PEP 695 递归泛型继承的典范用例: +[includes/linkedlist](../includes/linkedlist.vp) 提供链表实现,是 `novirtual` 非多态继承和 PEP 695 递归泛型继承的典范用例: ```python from linkedlist import LinkedNode, GSList @@ -168,7 +168,7 @@ node.value = 42 lst.append(node) # 类型安全:append 只接受 Node ``` -包含 `LinkedNode`/`SListNode`(双向/单向节点)和 `GSList[T]`(泛型链表,支持递归泛型继承 `class GNode(GSListNode[GNode])`)。详见 [05-classes.md 的泛型类章节](05-classes.md#泛型类pep-695-语法) 和 [06-oop.md 的 NoVTable 章节](06-oop.md#tnovtable-非多态继承)。 +包含 `LinkedNode`/`SListNode`(双向/单向节点)和 `GSList[T]`(泛型链表,支持递归泛型继承 `class GNode(GSListNode[GNode])`)。详见 [05-classes.md 的泛型类章节](05-classes.md#泛型类pep-695-语法) 和 [06-oop.md 的 novirtual 章节](06-oop.md#novirtual-非多态继承)。 ### `w32` 模块(Win32 FFI 绑定) diff --git a/13-bootstrapping.md b/13-bootstrapping.md index a4f82d6..eff02f0 100644 --- a/13-bootstrapping.md +++ b/13-bootstrapping.md @@ -62,7 +62,7 @@ | `includes/zlib/` | ~1,900 | 完整 deflate/inflate/huffman/checksum | | `includes/ast/` | 4,176 | **自研 Python 解析器(自举组件)**(历史数据,2026-06-30) | | `includes/llvmlite/` | ~1,500 | **自研 LLVM IR 生成库(自举组件)**(历史数据,2026-06-30) | -| `includes/linkedlist.vp` | ~400 | 泛型链表(@t.NoVTable + PEP 695) | +| `includes/linkedlist.vp` | ~400 | 泛型链表(novirtual + PEP 695) | | `includes/vipermath.vp` | 450 | 数学库 | | ...(30+ 文件) | ... | mpool/mbuddy/string/stdio/json/w32 等 | | **includes 总计** | **~12,000** | | @@ -130,7 +130,7 @@ | 库 | 自举中的作用 | |----|-------------| -| `linkedlist.vp` | AST 节点树、符号表链、IR 指令链的容器(`@t.NoVTable` + PEP 695 递归泛型继承) | +| `linkedlist.vp` | AST 节点树、符号表链、IR 指令链的容器(`novirtual` + PEP 695 递归泛型继承) | | `mpool.vp` / `mbuddy.vp` | 编译器内存管理(替代 Python GC) | | `string.vp` / `viperlib.vp` | 字符串处理 + snprintf(替代 Python 字符串方法) | | `w32/` | FFI 声明,自举后编译器调用 llc/clang/linker 的基础 | @@ -225,7 +225,7 @@ 3. **语言成熟度证明**:能写自己的编译器是系统级语言的"成人礼"。C、C++、Rust、Go、Zig 全都走过这条路。 -4. **dogfooding 闭环**:自举后每次改编译器都用编译器自己编译自己,持续暴露 bug 和性能瓶颈,形成正反馈。`includes/ast` 和 `includes/llvmlite` 的开发过程已经产出了大量深层 bug 修复(REnum 变体名冲突、跨模块继承字段展平、NoVTable OOP 方法继承等)。 +4. **dogfooding 闭环**:自举后每次改编译器都用编译器自己编译自己,持续暴露 bug 和性能瓶颈,形成正反馈。`includes/ast` 和 `includes/llvmlite` 的开发过程已经产出了大量深层 bug 修复(REnum 变体名冲突、跨模块继承字段展平、novirtual OOP 方法继承等)。 --- diff --git a/14-oo-refactor.md b/14-oo-refactor.md index e1fc2d5..2b9298b 100644 --- a/14-oo-refactor.md +++ b/14-oo-refactor.md @@ -49,7 +49,7 @@ TPV 编译器当前是"**数据对象 + 行为函数式**"架构: ### 2.2 演进阻力点 -1. `@t.NoVTable` + 硬编码 SIZE(`BUILDER_SIZE=32`/`pool.alloc(96)`)强耦合——**方法化不改布局则无影响**;若转 `@t.CVTable` 需全量重写 +1. `novirtual` + 硬编码 SIZE(`BUILDER_SIZE=32`/`pool.alloc(96)`)强耦合——**方法化不改布局则无影响**;若转 `virtual` 需全量重写 2. 16 个跨模块 stub 访问器(`value_get_ty`/`paramnode_get_ty`…)是规避 TransPyC stub 限制的产物——方法化后可逐步消除(**依赖编译器缺陷修复,非本方案范围**) 3. `LLVMType` 的 `pool.alloc(__sizeof__)` + `c.DerefAs` 构造模式——保留为构造方法 @@ -65,7 +65,7 @@ includes/llvmlite/ # ① 方法化平移(P0) TransPyV/App/lib/core/ ├── Gen/ # ② 生成器对象(P1-P6) - │ ├── Generator.vp # 入口:实际落地为组合:@t.NoVTable class Generator 持 46 个 GenMixin 子对象,非 Mixin 多继承 + │ ├── Generator.vp # 入口:实际落地为组合:novirtual class Generator 持 46 个 GenMixin 子对象,非 Mixin 多继承 │ ├── BaseGenMixin.vp # 状态/Pool/Module/Builder/SymTab + 错误/trace │ ├── TypeGenMixin.vp # 类型映射/注解解析(← HandlesType) │ ├── StructGenMixin.vp # struct 注册/字段/继承/vtable(← HandlesStruct+ClassDef 布局) @@ -84,7 +84,7 @@ TransPyV/App/lib/core/ ### 4.1 原则 -**纯方法化平移,不改布局、不改语义**:顶层函数 → 类方法,`self` 替代首参;类保持 `@t.NoVTable` + 硬编码 SIZE(不引入 vtable 继承)。 +**纯方法化平移,不改布局、不改语义**:顶层函数 → 类方法,`self` 替代首参;类保持 `novirtual` + 硬编码 SIZE(不引入 vtable 继承)。 ### 4.2 方法映射示例 @@ -94,8 +94,7 @@ def build_load(builder, ty, ptr): ... def build_gep_struct(builder, struct_ty, field_ty, ptr, field_idx): ... # 目标(同文件,类方法) -@t.NoVTable -class IRBuilder: +novirtual class IRBuilder: Pool, Func, CurBlock, Counter def load(self, ty, ptr): ... # 原函数体,builder → self def gep_struct(self, struct_ty, field_ty, ptr, field_idx): ... @@ -141,16 +140,15 @@ def print_ir(self, mode): ... ### 5.1 Generator 骨架(P1) ```python -# 采用 @t.CVTable(多态 Mixin 继承,与现有 ReturnHandle(HandlesBase.Mixin) 模式一致, -# 符合"多态类不用 NoVTable"约定;数据类 llvmlite 仍为 NoVTable) -@t.CVTable -class Generator(BaseGenMixin, TypeGenMixin, StructGenMixin, +# 采用 virtual(多态 Mixin 继承,与现有 ReturnHandle(HandlesBase.Mixin) 模式一致, +# 符合"多态类不用 novirtual"约定;数据类 llvmlite 仍为 novirtual) +virtual class Generator(BaseGenMixin, TypeGenMixin, StructGenMixin, ExprGenMixin, CallGenMixin, FuncGenMixin, ClassGenMixin, StmtGenMixin, ImportsGenMixin): pass ``` -*注:本骨架为原始方案;实际落地为组合架构(@t.NoVTable class Generator 持 46 个 GenMixin 子对象),见 §3 图注。* +*注:本骨架为原始方案;实际落地为组合架构(novirtual class Generator 持 46 个 GenMixin 子对象),见 §3 图注。* BaseGenMixin 状态字段(迁移自 HandlesTranslator): `Pool` / `Module` / `Builder` / `SymTab` / `FuncTable` / `FuncCount` / `StructTable` / `VTableCache` / `ImportedModules` / `FromImports` / `ModuleSha1` / `CurFunc` / `CurBuilder` / `CurClassName` / 错误 trace 栈 @@ -217,7 +215,7 @@ self.visit_call(node) # 内部用 self.Pool/self.Builder/self.Module/se | 风险 | 对策 | |---|---| -| `@t.NoVTable` + 硬编码 SIZE 布局 | 方法化不改布局;Generator 用 `@t.CVTable`(数据类保持 NoVTable) | +| `novirtual` + 硬编码 SIZE 布局 | 方法化不改布局;Generator 用 `virtual`(数据类保持 novirtual) | | 跨模块 stub 访问器 | 方法化后保留访问器(消除依赖编译器缺陷修复,延后) | | Viper 循环 import | 保持现有"延迟 import"模式 | | 迁移引入行为差异 | 每阶段 `.text.ll` 基线 diff 校验 | diff --git a/15-multiple-inheritance.md b/15-multiple-inheritance.md index e6ed0d6..8f45317 100644 --- a/15-multiple-inheritance.md +++ b/15-multiple-inheritance.md @@ -36,8 +36,7 @@ ## 3. 数据结构改造(HandlesStruct.vp) ```python -@t.NoVTable -class StructEntry: +novirtual class StructEntry: # 现有字段不变 ParentNames: list[str]* # 新增:父类名数组(声明顺序,跳过 marker) ParentCount: int # 新增 @@ -149,7 +148,7 @@ TPC(Python,`lib/core/`)与 TPV 结构一致,按顺序同步: ## 13. 兄弟 Mixin 方法解析(已落地,判据化) -Viper 的 "Mixin" 用**普通类 + 多继承**表达:`novirtual class Mixin` 持有回指针,组合类经 `class X(A.Mixin)` 继承。`@t.NoVTable` 多继承下,`BaseB.caller` 内调用 `self.helper()`(`helper` 定义在兄弟 `BaseA`)由**两条正向判据**解析到定义类并做 this-adjust: +Viper 的 "Mixin" 用**普通类 + 多继承**表达:`novirtual class Mixin` 持有回指针,组合类经 `class X(A.Mixin)` 继承。`novirtual` 多继承下,`BaseB.caller` 内调用 `self.helper()`(`helper` 定义在兄弟 `BaseA`)由**两条正向判据**解析到定义类并做 this-adjust: 1. 接收类必须是某个已注册类的**直接父类**(真的被组合使用); 2. 候选类与接收类必须同处某个已注册组合类的 **MRO**(真·兄弟)。 diff --git a/README.MD b/README.MD index da62608..e78856d 100644 --- a/README.MD +++ b/README.MD @@ -25,7 +25,7 @@ | 类型系统 | 动态类型 | 静态类型(通过 `t` 模块注解,编译时确定) | | 内存管理 | `GC` 自动回收 | 手动管理,无 `GC`,无运行时 | | 运行时 | 解释执行 + 字节码 | 编译为原生机器码(通过 `LLVM`) | -| 对象模型 | 万物皆对象 | 类内定义方法即启用 OOP(自动判定 `IsOOP`);`@t.CVTable` / `virtual class` 为多态(vtable),`@t.NoVTable` / `novirtual class` 为非多态继承(C++ 风格,零运行时开销);普通 `class`(无方法)仅为结构体布局;`t.Object` 亦可作标记基类 | +| 对象模型 | 万物皆对象 | 类内定义方法即启用 OOP(自动判定 `IsOOP`);`virtual class` 为多态(vtable),`novirtual class` 为非多态继承(C++ 风格,零运行时开销);普通 `class`(无方法)仅为结构体布局。`@t.Object` 默认不写、避免使用 | | 标准库 | `Python` 标准库 | `Viper` 标准库(`includes/`)+ `ViperOS SDK` | | 空值 | `None` | `None`(编译为 `NULL`/零值) | | 模块系统 | 运行时 `import` | 编译时解析,插入 `.stub.ll`,`SHA1` 命名空间隔离 | @@ -38,7 +38,7 @@ Viper 编译后的代码在底层等价于 `C` 编译后的机器码(都经过 - **类型书写**:短名别名(`int`/`uchar`/`uint64`/`bool`…)+ `T*` 指针 + 前缀描述符(`const`/`volatile`/`__attribute__(...)`),接近 C 且比链式 `t.CConst | t.CInt | t.CPtr` 更简洁 - **声明式内联汇编**:`__asm__(f"mov {asmout(x, ASM_DESCR.OUTPUT_REG)}, rdi")` 比裸 `__asm__` 更安全、更具可读性(旧写法 `c.Asm`/`c.AsmOut` 等价)。 - **结构化预处理**:`if c.CIfdef(...)` / `if c.CIfndef(...)` / `if c.CIf(...)` 缩进块替代 `#ifdef`/`#ifndef`/`#if`,块结束即 `#endif`(无独立的 `c.CEndif()`)。 -- **面向对象**:类内方法即 OOP;`@t.CVTable`(或 `virtual class`)提供 `vtable` 支持的多态;`@t.NoVTable`(或 `novirtual class`)提供 C++ 风格的非多态继承(零 vtable 开销);PEP 695 泛型 + 递归泛型继承实现强类型自引用结构 +- **面向对象**:类内方法即 OOP;`virtual class` 提供 `vtable` 支持的多态;`novirtual class` 提供 C++ 风格的非多态继承(零 vtable 开销);PEP 695 泛型 + 递归泛型继承实现强类型自引用结构 - **存根驱动的模块系统**:`.vpi` 存根文件实现跨模块类型解析,无需头文件 - **更多内容**:额外更多的不依赖操作系统的函数和语法 @@ -355,7 +355,7 @@ def fast_add(a: t.CInt, b: t.CInt) -> t.CInt | t.CInline: | `export` | `export def f(...)` / `export NAME: T [= v]` | 加 `t.CExport`;裸名导出 | | `static` | `static def f(...)` / `static x: T = v` | 类型联合最左插 `t.CStatic`;禁止跨模块 | | `inline` | `inline def f(...)` | 合成 `@t.attr.always_inline` | -| `virtual` / `novirtual` | `virtual class C` / `novirtual def m` | 合成 `@t.CVTable` / `@t.NoVTable`(类级 + 方法级) | +| `virtual` / `novirtual` | `virtual class C` / `novirtual def m` | 多态(vtable)/ 非多态继承;类级 + 方法级都可用 | | `const` / `volatile` | `const T` / `volatile T`(注解前缀) | 合成 `t.CConst` / `t.CVolatile`;volatile 落 LLVM `volatile` | | `__attribute__(...)` | 注解前缀或 `@__attribute__(...)` | 脱糖为 `t.attr.x`(比 GCC 少一层括号) | | `define` | `define X = v` / `define X: T = v` | 编译期常量(自动加 `t.CDefine`) |