#!builtin #!role(builtins) // splc3 的预置头:把编译器底层能力以 `#builtin` 声明暴露给用户/comptime 代码。 // `#builtin()` 的 id 必须命中 stage3/intrinsic.spl 的 intrinsic_of_id。 // 查询 / 断言类。静态类型实参由 sema 折成常量;动态类型值 // (comptime VM 里的 type 句柄)走宿主桥,因此同时声明为 extern。 #builtin(sizeof) #extern(builtin) comptime fn sizeof(t: type): usize; #builtin(bitsizeof) #extern(builtin) comptime fn bitsizeof(t: type): usize; #builtin(alignof) #extern(builtin) comptime fn alignof(t: type): usize; #builtin(field_count) #extern(builtin) comptime fn field_count(t: type): usize; #builtin(type_of) comptime fn type_of(v: any): type; #builtin(self) comptime fn self(): type; // spl_ir 接口:单写/单读,经编译器生成的 VM ABI 调用(无显式桥)。 // `#extern(builtin)` 表示后端外部实现(等价于当前后端的 `#extern(vm)`)。 #builtin(echo) #extern(builtin) comptime fn echo(x: u8): u8; // 运行期 panic:消息是动态切片,由宿主打印并终止。 #extern(vm) fn panic(msg: []u8): void; type Ref = @import("./spl_ir.spl").Ref; type Node = @import("./spl_ir.spl").Node; #builtin(inspect) #extern(builtin) comptime fn inspect(n: Ref): Node; #builtin(construct) #extern(builtin) comptime fn construct(n: Node): Ref; // receiver hook:@Self 只挂在方法的第一个参数上。 // 参数类型是 *T 时取地址,是 T 时按值传递;已显式传 &x 的调用不再二次取地址。 #hook(param) macro fn Self(callee: Ref, value: Ref): Ref { if value == 0 { ret 0; } var vn = @inspect(value); match vn.kind { .Addr(_) => { ret value; }, _ => {}, } var cn = @inspect(callee); match cn.kind { .FnDecl(f) => { var pn = @inspect(f.params[0]); match pn.kind { .ParamDecl(p) => { var tn = @inspect(p.ty); match tn.kind { .PtrType(_) => { ret @construct(.{ .kind = .Addr(value) }); }, _ => {}, } }, _ => {}, } }, _ => {}, } ret value; }