1949 lines
70 KiB
Plaintext
1949 lines
70 KiB
Plaintext
// Expect retcode = 0
|
||
|
||
// sema(stage3):在 HIR 上做名字/类型解析,产出 stage2 的 TypeTable。
|
||
// 架构(参考 Zig):分两阶段——(A) 模块级「签名」阶段,为每个声明解析类型并记忆化;
|
||
// (B) 逐「函数体」分块分析(局部作用域帧)。后续 comptime 调用即为被调函数体开新分析并缓存。
|
||
|
||
type .{ .Vec } = @import("../stage2/utils/vec.spl");
|
||
type .{ .ByteView } = @import("../stage2/utils/byte_view.spl");
|
||
type .{ .ByteBuffer } = @import("../stage2/utils/byte_buffer.spl");
|
||
type .{ .append_usize } = @import("../stage2/utils/fmt.spl");
|
||
type LangType = @import("./lang_type.spl");
|
||
type .{ .Node, .Ref } = @import("./builtins/spl_ir.spl");
|
||
type .{ .ErrorCode, .label } = @import("./builtins/error_code.spl");
|
||
type Bld = @import("./spl_ir_builder.spl");
|
||
type NodeClone = @import("./node_clone.spl");
|
||
type B = @import("./intrinsic.spl");
|
||
type .{ .Builtin } = @import("./intrinsic.spl");
|
||
type .{ .StructMember } = @import("./lang_type.spl");
|
||
type .{ .LangKind, .ReprKind, .ReprField } = @import("./lang_type.spl");
|
||
type LayoutM = @import("../stage2/layout.spl");
|
||
type .{ .Layout } = @import("../stage2/layout.spl");
|
||
// 元编程与 sema 交换的编译期句柄:HIR 节点用 Ref,类型值用 ct_handle 分配的 tid。
|
||
type tid = usize;
|
||
|
||
type Def = struct {
|
||
tid: usize,
|
||
init: Ref, // 初值 HirRef(0 = 无);仅当 assigned=false 且初值为常量时可作常量复现
|
||
assigned: bool, // 是否被重新赋值(赋值后不可当常量)
|
||
}
|
||
|
||
// 每个 HIR Ref 的全部语义缓存集中到一项,避免多张 per-Ref 表长度漂移。
|
||
type RefInfo = struct {
|
||
tid: usize,
|
||
def: usize,
|
||
fn_sym: usize,
|
||
builtin: usize,
|
||
variant: usize,
|
||
initref: usize,
|
||
aggval: usize,
|
||
mod: usize, // 1 起;0 = 生成节点,没有源模块归属
|
||
contained: bool,
|
||
}
|
||
|
||
// 调用边(用于 comptime 按依赖块编译)。
|
||
type CallEdge = struct {
|
||
from: usize, // 函数 sym id(1 起)或 comptime 根 id
|
||
to: usize, // 被调函数 sym id(1 起)
|
||
}
|
||
|
||
// 全局名字 intern:把任意模块的字节名映射为稳定的全局 NameId(1 起)。
|
||
type Interned = struct { off: usize, len: usize };
|
||
|
||
// 一个内置绑定:暴露名 -> 目标。
|
||
type Target = enum { Intrinsic, ComptimeFn, Native };
|
||
|
||
type Binding = struct {
|
||
name: usize, // 暴露名的全局 NameId(1 起)
|
||
target: Target,
|
||
kind: Builtin, // target == Intrinsic
|
||
mod: usize, // target == ComptimeFn
|
||
container: Ref, // 所属容器 AggType ref(0 = 模块级)
|
||
fnref: Ref,
|
||
decl: usize, // Fn 声明下标(0 起),惰性填充
|
||
}
|
||
|
||
type FnSym = struct {
|
||
name: []u8,
|
||
ref: Ref,
|
||
fn_tid: usize,
|
||
ret_tid: usize,
|
||
owner: usize, // 所属聚合 lang(0 = 模块级)
|
||
mod: usize, // 所属模块
|
||
decl: usize, // 声明在 decls 中的下标(0 起)
|
||
builtin: Builtin, // 编译器 intrinsic;None = 普通函数
|
||
op: usize, // SemaOp 值;0 = 不是语言操作
|
||
}
|
||
|
||
type ReflectSrc = struct {
|
||
copy: Ref, // @meta.reflect 生成的方法签名副本
|
||
src: Ref, // 原始 FnDecl 节点,用于反查 FnSym
|
||
}
|
||
|
||
type TypeSym = struct {
|
||
name: []u8,
|
||
lang: usize,
|
||
}
|
||
|
||
type GlobalSym = struct {
|
||
name: []u8,
|
||
lang: usize,
|
||
def: usize,
|
||
decl: Ref, // 顶层 VarDecl/ConstDecl 的 HirRef(取初值用)
|
||
}
|
||
|
||
// ---- Zig Sema 风格:Decl + Namespace + 惰性分析 ----
|
||
|
||
type DeclKind = enum { Fn, Var, Const, Type };
|
||
|
||
type DeclState = enum { Unresolved, InProgress, Resolved };
|
||
|
||
// 一个命名声明(= ZIR 容器 decls 里的元素)。
|
||
type Decl = struct {
|
||
name: []u8,
|
||
kind: DeclKind,
|
||
node: Ref, // 声明节点 HirRef(FnDecl/VarDecl/ConstDecl/TypeDecl)
|
||
container: Ref, // 所在容器(AggType 的 HirRef;0 = 文件根)
|
||
mod: usize, // 所在模块
|
||
state: DeclState,
|
||
lang: usize, // 类型:Type=该类型;Var/Const=值类型;Fn=fn 类型
|
||
ret_lang: usize, // Fn 返回类型
|
||
sym: usize, // Fn:fn_syms 下标+1
|
||
gidx: usize, // Var/Const 全局:gidx+1
|
||
init: Ref, // Var/Const:可折叠初值(含函数指针时为 HirRef,否则 0)
|
||
def: usize, // Var/Const:def id
|
||
body_done: bool, // Fn 体是否已分析
|
||
}
|
||
|
||
type NameEntry = struct { name: []u8, decl: usize }; // decl = decls 下标+1
|
||
|
||
type Namespace = struct { mod: usize, container: Ref, entries: Vec }; // NameEntry
|
||
|
||
// 一个模块(文件):共享 HIR 容器;refs 是本模块节点的遍历顺序,imports 记录模块内 Import 边。
|
||
type ImportEdge = struct { ref: Ref, mod: usize };
|
||
|
||
type Module = struct {
|
||
path: ByteView,
|
||
root: Ref,
|
||
refs: Vec, // Ref,模块节点遍历顺序
|
||
imports: Vec, // ImportEdge
|
||
root_lang: usize,
|
||
built: bool,
|
||
}
|
||
|
||
|
||
type Sema = @self;
|
||
|
||
// comptime 类型生成器实例化时的参数绑定:T: type -> lang;N: usize -> value。
|
||
type GenBind = struct { name: []u8, lang: usize, value: usize, is_type: bool };
|
||
// 记录「生成的某个方法 FnDecl 在实例化时的参数绑定」,供分析方法体时重新激活。
|
||
type GenBindRec = struct { fnref: Ref, name: []u8, lang: usize, value: usize, is_type: bool };
|
||
|
||
// 类型生成器结果缓存:同一 (fn_ref, 实参) 必须得到同一个 LangType,
|
||
// 否则 `VecI` 与 `@Vec(i32)` 会被当成两个不同的结构体。
|
||
type GenInstance = struct { fn_ref: Ref, args: Vec, lang: usize };
|
||
|
||
// shape 参数在声明期绑定:adapter 与 shape 都属于这个形参,不在调用点重新按名字找。
|
||
type ShapeParam = struct {
|
||
fn_ref: Ref,
|
||
param_index: usize,
|
||
has_adapter: bool,
|
||
adapter_decl: usize,
|
||
shape_lang: usize,
|
||
shape_ref: Ref, // ShapeApply / NamedType 使用点
|
||
};
|
||
|
||
// 一个 shape 泛型函数按全部 shape 形参的实参类型生成出的实例。
|
||
type ShapeInstance = struct {
|
||
fn_ref: Ref,
|
||
args: Vec, // usize,按声明中的 shape 形参顺序
|
||
sym: usize,
|
||
};
|
||
|
||
// shape 使用点的解析结果:`@adapter[Shape]` 里 adapter、shape 和绑定。
|
||
type ShapeUse = struct {
|
||
is_shape: bool,
|
||
has_adapter: bool,
|
||
adapter_decl: usize,
|
||
shape_lang: usize,
|
||
shape_ref: Ref,
|
||
};
|
||
|
||
// shape 声明里的 `type Item;` 占位:同一 shape 的同一名字稳定复用。
|
||
type ShapeAssocType = struct {
|
||
shape_lang: usize,
|
||
decl_ref: Ref,
|
||
lang: usize,
|
||
};
|
||
|
||
// `A + B` 的临时组合 shape:只记录 parts,方法查找再回到各自 shape。
|
||
type ShapeComp = struct {
|
||
lang: usize,
|
||
parts: Vec,
|
||
};
|
||
|
||
// `for` 的迭代协议解析结果,供 hir2ir 生成循环。
|
||
type IterInfo = struct {
|
||
ref: Ref,
|
||
next_sym: usize,
|
||
item_lang: usize,
|
||
item_index: usize,
|
||
done_index: usize,
|
||
};
|
||
|
||
type ShapeAdapterResult = struct {
|
||
lang: usize,
|
||
wrapper_sym: usize, // 旧契约:真实 comptime 结果里的函数 sym(1 起)
|
||
adapter_ref: Ref, // 新契约:动态构造的 adapter FnDecl Ref
|
||
adapter_mod: usize, // adapter FnDecl 所属模块
|
||
};
|
||
|
||
// 类型生成器形参的 shape 约束求值结果:lang 是最终绑定进生成器的类型。
|
||
type ShapeArgBind = struct {
|
||
ok: bool,
|
||
lang: usize,
|
||
};
|
||
|
||
// comptime VM 内 IR 函数引用 -> Sema FnSym 的映射,用于把函数值还原为符号。
|
||
type CtFnRef = struct { ref: Ref, sym: usize };
|
||
|
||
// 命名聚合的声明 lang 不能改由节点 tid 承担:结构解析可能覆盖节点 tid。
|
||
type ContainerLang = struct { ref: Ref, lang: usize };
|
||
|
||
// 生成类型实例化时已经解析好的 conformance(关联类型绑定不再依赖 gen_binds)。
|
||
type LangConform = struct { lang: usize, shape_lang: usize, binds: Vec };
|
||
type OpShapeEntry = struct { op: usize, lang: usize };
|
||
|
||
type ModuleRole = enum { None, Builtins, Ops, Lang };
|
||
|
||
// 0 = 未声明;i+1 = 模块下标 i。
|
||
type RoleMap = struct { builtins: usize, ops: usize, lang: usize };
|
||
|
||
// 生成器声明上的 `#repr(...)` / `#repr_field(...)`。
|
||
type GenReprEntry = struct {
|
||
decl_mod: usize,
|
||
decl_ref: Ref,
|
||
repr: ReprKind,
|
||
fields: Vec, // ReprField
|
||
};
|
||
|
||
type ShapeBind = struct { name: []u8, lang: usize };
|
||
|
||
type ShapeAdapterEval = struct { lang: usize, wrapper_sym: usize, adapter_ref: Ref };
|
||
|
||
// 实例形参的确定性类型:不依赖 HIR 类型节点的 tid 缓存。
|
||
type ShapeTypeBind = struct { ref: Ref, lang: usize };
|
||
|
||
type ShapeState = struct {
|
||
params: Vec, // ShapeParam,声明期绑定
|
||
instances: Vec, // ShapeInstance,按实参类型生成
|
||
seq: usize, // 实例名序号
|
||
type_binds: Vec, // ShapeTypeBind,实例形参 ref -> lang
|
||
assoc_types: Vec, // ShapeAssocType,shape 关联类型占位缓存
|
||
composed: Vec, // ShapeComp,A + B 的临时组合
|
||
patch_roots: Vec, // Ref,实例化时只需重解析的语句根
|
||
patching: bool,
|
||
patch_decl: usize, // 正在 patch 的 decl 下标;嵌套分析其他函数时必须走完整 sema
|
||
};
|
||
|
||
// 一次 shape 调用点处理的结果;fn_tid/ret_tid 为实例化后的真实签名。
|
||
type ShapeCallResult = struct {
|
||
ok: bool,
|
||
fn_tid: usize,
|
||
ret_tid: usize,
|
||
};
|
||
|
||
// shape 子系统由 driver 注入,避免 sema <-> shape 的 import 环。
|
||
type ShapeHooks = struct {
|
||
bind_params: fn(*any, Ref, []Ref): void,
|
||
type_bind_of: fn(*any, Ref): usize,
|
||
prepare_call: fn(*any, Ref, Ref, []usize, *ShapeCallResult): void,
|
||
check_conforms: fn(*any, Ref, usize): void,
|
||
op_method: fn(*any, usize, usize, []u8): usize,
|
||
has_conformance: fn(*any, usize, usize): bool,
|
||
is_shape_lang: fn(*any, usize): bool,
|
||
bind_type_arg: fn(*any, Ref, usize, *ShapeArgBind): void,
|
||
apply_expr: fn(*any, Ref, []u8, []Ref, []Ref): usize,
|
||
};
|
||
|
||
// decorator/param/impl 的挂载与调用由 hook 模块负责;sema 只提供
|
||
// 声明分析、名字解析和 comptime 求值入口。
|
||
type HookParamResult = struct {
|
||
args: []Ref,
|
||
changed: bool,
|
||
};
|
||
|
||
type HookHooks = struct {
|
||
apply_decorators: fn(*any, usize): void,
|
||
prepare_params: fn(*any, Ref, Ref, []Ref, *HookParamResult): void,
|
||
is_impl: fn(*any, Ref): bool,
|
||
};
|
||
|
||
type ExprHooks = struct {
|
||
sema_expr: fn(*any, Ref, usize): void,
|
||
sema_body: fn(*any, usize, usize): void,
|
||
patch_body: fn(*any, usize, usize): void,
|
||
iter_capture_members: fn(*any, Ref): Vec,
|
||
};
|
||
|
||
type DeclHooks = struct {
|
||
ensure_decl: fn(*any, usize): void,
|
||
ensure_module: fn(*any, usize): void,
|
||
analyze_fn_body: fn(*any, usize): void,
|
||
};
|
||
|
||
type LangHooks = struct {
|
||
record_conforms: fn(*any, usize): void,
|
||
copy_conforms: fn(*any, usize, usize): void,
|
||
conform_binds: fn(*any, usize, usize, *Vec): bool,
|
||
method_sym: fn(*any, usize, []u8): usize,
|
||
ensure_lang_of: fn(*any, usize): void,
|
||
iter_shape: fn(*any): usize,
|
||
build_roles: fn(*any): void,
|
||
role_mod: fn(*any, usize): isize,
|
||
};
|
||
|
||
type TypeHooks = struct {
|
||
resolve_type: fn(*any, Ref): usize,
|
||
resolve_param: fn(*any, Ref): usize,
|
||
type_gen: fn(*any, Ref, Node.Kind.Call, usize): usize,
|
||
instantiate_generator: fn(*any, Ref, Node.Kind.Call, usize, usize): usize,
|
||
type_is_generator: fn(*any, Ref): bool,
|
||
type_arg_lang: fn(*any, Ref): usize,
|
||
type_value_lang: fn(*any, Ref): usize,
|
||
type_handle_arg: fn(*any, usize): usize,
|
||
type_arg_handle: fn(*any, usize): usize,
|
||
type_query_int: fn(*any, usize, Builtin): usize,
|
||
type_builtin: fn(*any, Ref, []Ref, Builtin): usize,
|
||
emit_intrinsic: fn(*any, Ref, []Ref, Builtin): void,
|
||
builtin_tid: fn(*any, []u8): usize,
|
||
lit_u64: fn(*any, Ref): u64,
|
||
const_eval_usize: fn(*any, Ref): usize,
|
||
gen_spec_activate: fn(*any, Ref): void,
|
||
};
|
||
|
||
type BindingHooks = struct {
|
||
find_binding: fn(*any, []u8): usize,
|
||
builtin_tag_id: fn(*any, Ref): Builtin,
|
||
has_tag: fn(*any, Ref, []u8): bool,
|
||
has_builtin_tag: fn(*any, Ref): bool,
|
||
collect_builtins: fn(*any): void,
|
||
list_has_tag: fn(*any, []Ref, []u8): bool,
|
||
raw_tag: fn(*any, Ref): []u8,
|
||
tag_arg: fn(*any, Ref, []u8): []u8,
|
||
};
|
||
|
||
type DumpHooks = struct {
|
||
dump: fn(*any, *ByteBuffer): bool,
|
||
};
|
||
|
||
type CtState = struct {
|
||
types: Vec, // comptime 类型值句柄表(lang id;句柄 = 下标+1)
|
||
fn_refs: Vec, // CtFnRef,最近一次 comptime eval 的函数引用映射
|
||
eval: fn(*any, usize): void, // comptime VM 求值器(由 driver 注入)
|
||
building: bool, // 正在为 comptime chunk 编译
|
||
};
|
||
|
||
hir: Bld.HirNodeContainer,
|
||
mods: Vec, // Module
|
||
cur_mod: usize,
|
||
refs: Vec, // RefInfo,按 Ref 下标;0 号为哨兵
|
||
reflect_stack: Vec, // 正在反射的 lang;互递归类型用名字截断
|
||
reflect_srcs: Vec, // ReflectSrc:反射方法副本 -> 原始 FnDecl
|
||
types: LangType,
|
||
defs: Vec, // Def
|
||
fn_syms: Vec, // FnSym(函数记录,惰性登记)
|
||
fn_nodes: Vec, // FnDecl 的 HirRef(按出现顺序)
|
||
decls: Vec, // Decl(全部命名声明)
|
||
namespaces: Vec, // Namespace(0 号 = 文件根)
|
||
def_global: Vec, // per def id -> gidx+1(0 = 非全局)
|
||
loop_results: Vec, // 循环结果 lang 栈(供 break 值)
|
||
current_agg: usize, // 当前所在聚合 lang(`@self`)
|
||
self_lang: usize, // 非 0:当前实例化目标的 lang;`@self` 优先解析到它
|
||
ret_override: usize, // 非 0:catch handler 内的 ret 使用外层函数返回类型
|
||
lang_attach_depth: usize,
|
||
|
||
current_container: Ref, // 当前名称空间容器 ref(名字解析用)
|
||
in_callee: bool, // 正在分析方法调用的 callee(此时才允许容器方法名)
|
||
container_langs: Vec, // ContainerLang:容器 ref -> 声明期 lang
|
||
lang_conforms: Vec, // LangConform:生成类型实例化后的 conformance
|
||
roles: RoleMap,
|
||
op_shapes: Vec, // OpShapeEntry:SemaOp -> ops shape lang
|
||
op_registry_built: bool,
|
||
|
||
gen_reprs: Vec, // GenReprEntry:生成器 decl -> 内建表示
|
||
gen_repr_built: bool,
|
||
nglobals: usize, // 已登记的模块级全局计数
|
||
ct: CtState,
|
||
warn: fn(*any, []u8, []u8): void, // 警告通道(由 driver 注入):(user, 函数名, 未知 id)
|
||
warn_user: *any, // 传给 warn 的上下文(通常是 Logger 指针)
|
||
err: fn(*any, []u8, []u8): void, // 错误通道(由 driver 注入)
|
||
err_user: *any,
|
||
had_error: bool,
|
||
call_edges: Vec, // CallEdge
|
||
bindings: Vec, // Binding(暴露名 -> Intrinsic/ComptimeFn)
|
||
name_pool: Vec, // u8,intern 的字节池
|
||
name_infos: Vec, // Interned
|
||
gen_binds: Vec, // GenBind,类型生成器实例化期间的参数绑定
|
||
gen_method_binds: Vec, // GenBindRec,生成方法 FnDecl 的实例化绑定
|
||
gen_instances: Vec, // GenInstance,(fn_ref, args) -> lang 缓存
|
||
shape_state: ShapeState,
|
||
shape_hooks: ShapeHooks,
|
||
shape_for_item: fn(*any, usize, usize): usize,
|
||
hook_hooks: HookHooks,
|
||
cur_fn: usize, // 当前正在分析体所属的 fn sym id(0 = 无);comptime 根用大偏移 id
|
||
cur_fn_ref: Ref, // 当前函数/声明节点,用于没有精确 ref 时的诊断定位
|
||
ct_call_ref: Ref, // 最近一次 comptime 调用节点,宏内诊断回指调用点
|
||
ct_call_mod: usize, // 最近一次 comptime 调用所在模块
|
||
macro_call_ref: Ref, // 外层用户宏调用节点,@meta.error 优先用它
|
||
macro_call_mod: usize, // 外层宏调用所在模块,诊断路径不能用执行期 cur_mod
|
||
layout: Layout, // 类型大小/对齐(@sizeof/@alignOf)
|
||
iter_infos: Vec, // IterInfo,for -> 协议调用
|
||
expr_hooks: ExprHooks,
|
||
decl_hooks: DeclHooks,
|
||
lang_hooks: LangHooks,
|
||
type_hooks: TypeHooks,
|
||
binding_hooks: BindingHooks,
|
||
dump_hooks: DumpHooks,
|
||
|
||
fn node_loc_of(self: *Sema, ref: Ref, line: *usize, col: *usize): bool {
|
||
if ref == 0 { ret false; }
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null || n.loc.line == 0 { ret false; }
|
||
line.* = n.loc.line;
|
||
col.* = n.loc.col;
|
||
ret true;
|
||
}
|
||
|
||
fn append_error_loc(self: *Sema, b: *ByteBuffer, ref: Ref) {
|
||
var use = ref;
|
||
var line: usize = 0;
|
||
var col: usize = 0;
|
||
var has = node_loc_of(self, use, &line, &col);
|
||
// 生成/合成节点常常保留 ref 但 loc 为 0;继续向调用点/当前函数回退。
|
||
if !has {
|
||
use = self.macro_call_ref;
|
||
has = node_loc_of(self, use, &line, &col);
|
||
}
|
||
if !has {
|
||
use = self.ct_call_ref;
|
||
has = node_loc_of(self, use, &line, &col);
|
||
}
|
||
if !has {
|
||
use = self.cur_fn_ref;
|
||
has = node_loc_of(self, use, &line, &col);
|
||
}
|
||
var path_mod = self.cur_mod;
|
||
if use != 0 {
|
||
var ri = ref_info(self, use);
|
||
if ri != null && ri.mod != 0 && ri.mod - 1 < self.mods.len { path_mod = ri.mod - 1; }
|
||
}
|
||
if use == self.macro_call_ref && self.macro_call_mod < self.mods.len {
|
||
path_mod = self.macro_call_mod;
|
||
} else if use == self.ct_call_ref && self.ct_call_mod < self.mods.len {
|
||
path_mod = self.ct_call_mod;
|
||
}
|
||
var path = module_at(self, path_mod).path;
|
||
if path.len != 0 {
|
||
ByteBuffer.append(b, ByteView.as_slice(path));
|
||
if has {
|
||
ByteBuffer.append(b, ":");
|
||
ByteBuffer.append(b, itext(self, line));
|
||
ByteBuffer.append(b, ":");
|
||
ByteBuffer.append(b, itext(self, col));
|
||
}
|
||
ByteBuffer.append(b, ": ");
|
||
}
|
||
}
|
||
|
||
fn emit_error(self: *Sema, ref: Ref, code: []u8, msg: []u8, detail: []u8) {
|
||
self.had_error = true;
|
||
if self.err == null { ret; }
|
||
var b = ByteBuffer.new();
|
||
defer { ByteBuffer.drop(&b); }
|
||
append_error_loc(self, &b, ref);
|
||
if code.len != 0 {
|
||
ByteBuffer.append(&b, "error[");
|
||
ByteBuffer.append(&b, code);
|
||
ByteBuffer.append(&b, "]: ");
|
||
}
|
||
ByteBuffer.append(&b, msg);
|
||
self.err(self.err_user, ByteBuffer.as_slice(b), detail);
|
||
}
|
||
|
||
fn sema_error(self: *Sema, msg: []u8, detail: []u8) {
|
||
emit_error(self, 0, "", msg, detail);
|
||
}
|
||
|
||
fn sema_error_at(self: *Sema, ref: Ref, msg: []u8, detail: []u8) {
|
||
emit_error(self, ref, "", msg, detail);
|
||
}
|
||
|
||
// 带稳定错误码的 sema 诊断;测试按 error[E####] 匹配,不依赖消息文字。
|
||
fn sema_error_code(self: *Sema, code: ErrorCode, msg: []u8, detail: []u8) {
|
||
emit_error(self, 0, label(code), msg, detail);
|
||
}
|
||
|
||
fn sema_error_code_at(self: *Sema, ref: Ref, code: ErrorCode, msg: []u8, detail: []u8) {
|
||
emit_error(self, ref, label(code), msg, detail);
|
||
}
|
||
|
||
// 把 LangType 渲染成诊断文本;只做名字/结构,不做求值。
|
||
fn append_type_name(self: *Sema, b: *ByteBuffer, t: usize) {
|
||
if t == 0 {
|
||
ByteBuffer.append(b, "<unresolved>");
|
||
ret;
|
||
}
|
||
var name = LangType.name_of(&self.types, t);
|
||
var k = LangType.kind_of(&self.types, t);
|
||
match k {
|
||
.Int => {
|
||
ByteBuffer.append(b, if LangType.int_signed(&self.types, t) { "i" } else { "u" });
|
||
match append_usize(b, LangType.int_bits(&self.types, t), 10, false) { .Ok => {}, .Err(error) => {} }
|
||
},
|
||
.Float => {
|
||
ByteBuffer.append(b, "f");
|
||
match append_usize(b, LangType.float_bits(&self.types, t), 10, false) { .Ok => {}, .Err(error) => {} }
|
||
},
|
||
.Bool => { ByteBuffer.append(b, "bool"); },
|
||
.Unit => { ByteBuffer.append(b, "void"); },
|
||
.Null => { ByteBuffer.append(b, "null"); },
|
||
.Type => { ByteBuffer.append(b, "type"); },
|
||
.Any => { ByteBuffer.append(b, "any"); },
|
||
.Error => { ByteBuffer.append(b, "<error>"); },
|
||
.Never => { ByteBuffer.append(b, "never"); },
|
||
.Ptr => {
|
||
ByteBuffer.append(b, "*");
|
||
append_type_name(self, b, LangType.elem_of(&self.types, t));
|
||
},
|
||
.Slice => {
|
||
ByteBuffer.append(b, "[]");
|
||
append_type_name(self, b, LangType.elem_of(&self.types, t));
|
||
},
|
||
.Array => {
|
||
ByteBuffer.append(b, "[");
|
||
match append_usize(b, LangType.array_len_of(&self.types, t), 10, false) { .Ok => {}, .Err(error) => {} }
|
||
ByteBuffer.append(b, "]");
|
||
append_type_name(self, b, LangType.elem_of(&self.types, t));
|
||
},
|
||
.Fn => {
|
||
ByteBuffer.append(b, "fn(");
|
||
var i: usize = 0;
|
||
while i < LangType.fn_param_count(&self.types, t) {
|
||
if i != 0 { ByteBuffer.append(b, ", "); }
|
||
append_type_name(self, b, LangType.fn_param_of(&self.types, t, i));
|
||
i = i + 1;
|
||
}
|
||
ByteBuffer.append(b, ") -> ");
|
||
append_type_name(self, b, LangType.fn_ret_of(&self.types, t));
|
||
},
|
||
.Assoc => {
|
||
if name.len != 0 { ByteBuffer.append(b, name); }
|
||
else { ByteBuffer.append(b, "<assoc>"); }
|
||
},
|
||
.Struct, .Union, .Enum, .Shape => {
|
||
if name.len != 0 {
|
||
ByteBuffer.append(b, name);
|
||
ret;
|
||
}
|
||
match k {
|
||
.Enum => {
|
||
ByteBuffer.append(b, "enum{");
|
||
var mi: usize = 0;
|
||
while mi < LangType.member_count(&self.types, t) {
|
||
if mi != 0 { ByteBuffer.append(b, ", "); }
|
||
ByteBuffer.append(b, LangType.member_name(&self.types, t, mi));
|
||
var ml = LangType.member_lang(&self.types, t, mi);
|
||
if ml != 0 && @enum2int(LangType.kind_of(&self.types, ml)) != @enum2int(LangKind.Unit) {
|
||
ByteBuffer.append(b, ": ");
|
||
append_type_name(self, b, ml);
|
||
}
|
||
mi = mi + 1;
|
||
}
|
||
ByteBuffer.append(b, "}");
|
||
},
|
||
.Struct => { ByteBuffer.append(b, "struct"); },
|
||
.Union => { ByteBuffer.append(b, "union"); },
|
||
.Shape => { ByteBuffer.append(b, "shape"); },
|
||
_ => {},
|
||
}
|
||
},
|
||
}
|
||
}
|
||
|
||
// 只检查,不插入隐式转换;需要显式 `as` 的地方必须让用户写出来。
|
||
fn require_type(self: *Sema, ref: Ref, expected: usize, what: []u8): bool {
|
||
if expected == 0 { ret true; }
|
||
var got = tid_of(self, ref);
|
||
if got == expected || LangType.is_subtype(&self.types, got, expected) { ret true; }
|
||
// shape 原函数体里关联类型还是占位;实例化克隆后会代成具体类型再检查。
|
||
if LangType.is_assoc(&self.types, got) || LangType.is_assoc(&self.types, expected) { ret true; }
|
||
var b = ByteBuffer.new();
|
||
defer { ByteBuffer.drop(&b); }
|
||
ByteBuffer.append(&b, "type mismatch");
|
||
if what.len != 0 {
|
||
ByteBuffer.append(&b, ": ");
|
||
ByteBuffer.append(&b, what);
|
||
}
|
||
ByteBuffer.append(&b, ": expected ");
|
||
append_type_name(self, &b, expected);
|
||
ByteBuffer.append(&b, ", got ");
|
||
append_type_name(self, &b, got);
|
||
sema_error_code_at(self, ref, ErrorCode.TypeMismatch, ByteBuffer.as_slice(b), "");
|
||
ret false;
|
||
}
|
||
|
||
// shape 形参由 prepare_call 负责契约;这里不能用普通参数类型提前拒绝 allocator。
|
||
fn is_shape_param_at(self: *Sema, fn_ref: Ref, index: usize): bool {
|
||
var i: usize = 0;
|
||
while i < self.shape_state.params.len {
|
||
var p = (self.shape_state.params.data as *ShapeParam) + i;
|
||
if p.fn_ref == fn_ref && p.param_index == index { ret true; }
|
||
i = i + 1;
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
// 宏体返回的是代码/声明句柄,不是普通 runtime 返回值。
|
||
fn current_fn_is_macro(self: *Sema): bool {
|
||
if self.cur_fn == 0 || self.cur_fn > self.fn_syms.len { ret false; }
|
||
ret has_tag(self, sym_at(self, self.cur_fn - 1).ref, "macro");
|
||
}
|
||
|
||
fn current_fn_is_lambda(self: *Sema): bool {
|
||
if self.cur_fn == 0 || self.cur_fn > self.fn_syms.len { ret false; }
|
||
ret has_tag(self, sym_at(self, self.cur_fn - 1).ref, "__lambda");
|
||
}
|
||
|
||
fn current_ret_lang(self: *Sema): usize {
|
||
if self.cur_fn == 0 || self.cur_fn > self.fn_syms.len { ret 0; }
|
||
ret sym_at(self, self.cur_fn - 1).ret_tid;
|
||
}
|
||
|
||
// 源结果里的每个 #error variant 必须在目标结果里有同名同载荷 variant。
|
||
fn enum_errors_compatible(self: *Sema, src: usize, dst: usize): bool {
|
||
if src == 0 || dst == 0 { ret false; }
|
||
if !LangType.is_enum(&self.types, src) || !LangType.is_enum(&self.types, dst) { ret false; }
|
||
var i: usize = 0;
|
||
while i < LangType.member_count(&self.types, src) {
|
||
if LangType.enum_is_error(&self.types, src, i) {
|
||
var name = LangType.member_name(&self.types, src, i);
|
||
var di = LangType.member_idx(&self.types, dst, name);
|
||
if di < 0 || !LangType.enum_is_error(&self.types, dst, di as usize) {
|
||
ret false;
|
||
}
|
||
if LangType.member_lang(&self.types, src, i) !=
|
||
LangType.member_lang(&self.types, dst, di as usize) {
|
||
ret false;
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
ret true;
|
||
}
|
||
|
||
fn pattern_error_index(self: *Sema, pat: Ref, lang: usize): isize {
|
||
if pat == 0 { ret -1; }
|
||
var n = Bld.node_ptr(&self.hir, pat);
|
||
if n == null { ret -1; }
|
||
match n.kind {
|
||
.PatTag(x) => {
|
||
var idx = LangType.member_idx(&self.types, lang, x.name);
|
||
if idx >= 0 && LangType.enum_is_error(&self.types, lang, idx as usize) {
|
||
ret idx;
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
ret -1;
|
||
}
|
||
|
||
// 宏侧错误码入口:编译器输出固定为 error[E####],测试按码匹配。
|
||
fn sema_error_num(self: *Sema, code: usize, msg: []u8, detail: []u8) {
|
||
var cb = ByteBuffer.new();
|
||
defer { ByteBuffer.drop(&cb); }
|
||
ByteBuffer.append(&cb, "E");
|
||
var s = itext(self, code);
|
||
var pad = 4 - s.len;
|
||
while pad > 0 {
|
||
ByteBuffer.append(&cb, "0");
|
||
pad = pad - 1;
|
||
}
|
||
ByteBuffer.append(&cb, s);
|
||
emit_error(self, 0, ByteBuffer.as_slice(cb), msg, detail);
|
||
}
|
||
|
||
fn eq(a: []u8, b: []u8): bool {
|
||
if a.len != b.len { ret false; }
|
||
var i: usize = 0;
|
||
while i < a.len {
|
||
if a[i] != b[i] { ret false; }
|
||
i = i + 1;
|
||
}
|
||
ret true;
|
||
}
|
||
|
||
fn op_is(op: Node.Kind.Op, want: Node.Kind.Op): bool {
|
||
ret @enum2int(op) == @enum2int(want);
|
||
}
|
||
|
||
fn agg_is_enum(k: Node.Kind.Agg): bool {
|
||
match k {
|
||
.Enum => { ret true; },
|
||
_ => {},
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
// #ok / #error 只存在结果枚举上;普通 enum 两个输出都是 0。
|
||
fn enum_markers(self: *Sema, fields: []Ref, ok: *usize, errors: *usize) {
|
||
ok.* = 0;
|
||
errors.* = 0;
|
||
var i: usize = 0;
|
||
while i < fields.len {
|
||
var fref = fields[i];
|
||
var fn2 = Bld.node_ptr(&self.hir, fref);
|
||
if fn2 != null {
|
||
if list_has_tag(self, fn2.tags, "ok") {
|
||
if ok.* != 0 {
|
||
sema_error_code_at(self, fref, ErrorCode.DuplicateSuccess,
|
||
"duplicate #ok variant", "");
|
||
} else {
|
||
ok.* = i + 1;
|
||
}
|
||
}
|
||
if list_has_tag(self, fn2.tags, "error") {
|
||
errors.* = errors.* | ((1 as usize) << i);
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
fn agg_is_union(k: Node.Kind.Agg): bool {
|
||
match k {
|
||
.Union => { ret true; },
|
||
_ => {},
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
fn agg_is_shape(k: Node.Kind.Agg): bool {
|
||
match k {
|
||
.Shape => { ret true; },
|
||
_ => {},
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
fn is_cmp(op: Node.Kind.Op): bool {
|
||
match op {
|
||
.Equal, .NotEqual, .LessThan, .LessEqual, .GreaterThan, .GreaterEqual => { ret true; },
|
||
_ => { ret false; },
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
// `@sizeof` 一类查询是语言内置例外;其余 comptime 内建仍必须用 `@` 入口。
|
||
fn builtin_requires_at(bid: Builtin): bool {
|
||
match bid {
|
||
.Sizeof, .BitSizeof, .AlignOf, .FieldCount, .TypeOf, .Enum2Int, .Assert => { ret false; },
|
||
_ => {},
|
||
}
|
||
ret true;
|
||
}
|
||
|
||
fn is_logic(op: Node.Kind.Op): bool {
|
||
match op {
|
||
.LogicAnd, .LogicOr => { ret true; },
|
||
_ => { ret false; },
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
fn copy_refs(xs: []Ref): Vec {
|
||
var v = Vec.blank(@sizeof(Ref), @alignof(Ref));
|
||
var i: usize = 0;
|
||
while i < xs.len {
|
||
var r = xs[i];
|
||
match Vec.push(&v, &r as *u8) { .Ok => {}, .Err(error) => {} }
|
||
i = i + 1;
|
||
}
|
||
ret v;
|
||
}
|
||
|
||
// 诊断路径按节点源模块走;生成/克隆节点没有归属时才回退当前模块。
|
||
fn record_ref_mods(self: *Sema, m: usize, refs: []Ref) {
|
||
var i: usize = 0;
|
||
while i < refs.len {
|
||
var ri = ref_info(self, refs[i]);
|
||
if ri != null { ri.mod = m + 1; }
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
// 克隆产生的新节点继承生成器声明模块,避免分析期 cur_mod 切换后路径漂移。
|
||
fn record_new_ref_mods(self: *Sema, m: usize, start: usize) {
|
||
var i = start + 1;
|
||
var n = Bld.count(&self.hir);
|
||
while i <= n {
|
||
var ri = ref_info(self, i);
|
||
if ri != null { ri.mod = m + 1; }
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
fn new(hir: Bld.HirNodeContainer, path: ByteView, root: Ref, refs: []Ref): Sema {
|
||
// 模块路径由 Sema 自己持有,driver 的临时 ModEntry 可以提前释放。
|
||
var stable_path = ByteView.from_slice(Bld.dup_bytes(&hir, ByteView.as_slice(path)));
|
||
var tt: LangType = undefined;
|
||
match LangType.new() { .Ok(v) => { tt = v; }, .Err(error) => {} }
|
||
var ly: Layout = undefined;
|
||
match LayoutM.new() { .Ok(v) => { ly = v; }, .Err(error) => {} }
|
||
var s = Sema {
|
||
.hir = hir,
|
||
.mods = Vec.blank(@sizeof(Module), @alignof(Module)),
|
||
.cur_mod = 0,
|
||
.refs = Vec.blank(@sizeof(RefInfo), @alignof(RefInfo)),
|
||
.reflect_stack = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||
.reflect_srcs = Vec.blank(@sizeof(ReflectSrc), @alignof(ReflectSrc)),
|
||
.types = tt,
|
||
.defs = Vec.blank(@sizeof(Def), @alignof(Def)),
|
||
.fn_syms = Vec.blank(@sizeof(FnSym), @alignof(FnSym)),
|
||
.fn_nodes = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||
.decls = Vec.blank(@sizeof(Decl), @alignof(Decl)),
|
||
.namespaces = Vec.blank(@sizeof(Namespace), @alignof(Namespace)),
|
||
.def_global = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||
.loop_results = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||
.current_agg = 0,
|
||
.self_lang = 0,
|
||
.ret_override = 0,
|
||
.lang_attach_depth = 0,
|
||
.current_container = 0,
|
||
.in_callee = false,
|
||
.container_langs = Vec.blank(@sizeof(ContainerLang), @alignof(ContainerLang)),
|
||
.lang_conforms = Vec.blank(@sizeof(LangConform), @alignof(LangConform)),
|
||
.roles = RoleMap { .builtins = 0, .ops = 0, .lang = 0 },
|
||
.op_shapes = Vec.blank(@sizeof(OpShapeEntry), @alignof(OpShapeEntry)),
|
||
.op_registry_built = false,
|
||
.gen_reprs = Vec.blank(@sizeof(GenReprEntry), @alignof(GenReprEntry)),
|
||
.gen_repr_built = false,
|
||
.nglobals = 0,
|
||
.ct = CtState {
|
||
.types = Vec.blank(@sizeof(usize), @alignof(usize)),
|
||
.fn_refs = Vec.blank(@sizeof(CtFnRef), @alignof(CtFnRef)),
|
||
.eval = null,
|
||
.building = false,
|
||
},
|
||
.warn = null,
|
||
.warn_user = null,
|
||
.err = null,
|
||
.err_user = null,
|
||
.had_error = false,
|
||
.call_edges = Vec.blank(@sizeof(CallEdge), @alignof(CallEdge)),
|
||
.bindings = Vec.blank(@sizeof(Binding), @alignof(Binding)),
|
||
.name_pool = Vec.blank(@sizeof(u8), @alignof(u8)),
|
||
.name_infos = Vec.blank(@sizeof(Interned), @alignof(Interned)),
|
||
.gen_binds = Vec.blank(@sizeof(GenBind), @alignof(GenBind)),
|
||
.gen_method_binds = Vec.blank(@sizeof(GenBindRec), @alignof(GenBindRec)),
|
||
.gen_instances = Vec.blank(@sizeof(GenInstance), @alignof(GenInstance)),
|
||
.iter_infos = Vec.blank(@sizeof(IterInfo), @alignof(IterInfo)),
|
||
.expr_hooks = ExprHooks { .sema_expr = null, .sema_body = null, .patch_body = null, .iter_capture_members = null },
|
||
.decl_hooks = DeclHooks { .ensure_decl = null, .ensure_module = null, .analyze_fn_body = null },
|
||
.lang_hooks = LangHooks { .record_conforms = null, .copy_conforms = null, .conform_binds = null, .method_sym = null },
|
||
.type_hooks = TypeHooks {
|
||
.resolve_type = null, .resolve_param = null, .type_gen = null,
|
||
.type_is_generator = null,
|
||
.type_arg_lang = null, .type_value_lang = null, .type_handle_arg = null,
|
||
.type_arg_handle = null, .type_query_int = null,
|
||
.type_builtin = null, .emit_intrinsic = null, .builtin_tid = null,
|
||
.lit_u64 = null, .const_eval_usize = null, .gen_spec_activate = null,
|
||
},
|
||
.binding_hooks = BindingHooks {
|
||
.find_binding = null, .builtin_tag_id = null, .has_tag = null,
|
||
.has_builtin_tag = null,
|
||
.collect_builtins = null, .list_has_tag = null, .raw_tag = null, .tag_arg = null,
|
||
},
|
||
.dump_hooks = DumpHooks { .dump = null },
|
||
.shape_state = ShapeState {
|
||
.params = Vec.blank(@sizeof(ShapeParam), @alignof(ShapeParam)),
|
||
.instances = Vec.blank(@sizeof(ShapeInstance), @alignof(ShapeInstance)),
|
||
.seq = 0,
|
||
.type_binds = Vec.blank(@sizeof(ShapeTypeBind), @alignof(ShapeTypeBind)),
|
||
.assoc_types = Vec.blank(@sizeof(ShapeAssocType), @alignof(ShapeAssocType)),
|
||
.composed = Vec.blank(@sizeof(ShapeComp), @alignof(ShapeComp)),
|
||
.patch_roots = Vec.blank(@sizeof(Ref), @alignof(Ref)),
|
||
.patching = false,
|
||
.patch_decl = 0,
|
||
},
|
||
.shape_hooks = ShapeHooks {
|
||
.bind_params = null,
|
||
.type_bind_of = null,
|
||
.prepare_call = null,
|
||
.check_conforms = null,
|
||
.op_method = null,
|
||
.has_conformance = null,
|
||
.is_shape_lang = null,
|
||
.bind_type_arg = null,
|
||
.apply_expr = null,
|
||
},
|
||
.shape_for_item = null,
|
||
.hook_hooks = HookHooks {
|
||
.apply_decorators = null,
|
||
.prepare_params = null,
|
||
.is_impl = null,
|
||
},
|
||
.cur_fn = 0,
|
||
.cur_fn_ref = 0,
|
||
.ct_call_ref = 0,
|
||
.ct_call_mod = 0,
|
||
.macro_call_ref = 0,
|
||
.macro_call_mod = 0,
|
||
.layout = ly,
|
||
};
|
||
var main_mod = Module { .path = stable_path, .root = root, .refs = copy_refs(refs),
|
||
.imports = Vec.blank(@sizeof(ImportEdge), @alignof(ImportEdge)),
|
||
.root_lang = 0, .built = false };
|
||
match Vec.push(&s.mods, &main_mod as *u8) { .Ok => {}, .Err(error) => {} }
|
||
init_tables(&s);
|
||
record_ref_mods(&s, 0, refs);
|
||
ret s;
|
||
}
|
||
|
||
fn init_tables(self: *Sema) {
|
||
grow_tables(self);
|
||
}
|
||
|
||
fn drop(self: *Sema) {
|
||
Bld.drop(&self.hir);
|
||
LangType.drop(&self.types);
|
||
LayoutM.drop(&self.layout);
|
||
Vec.drop(&self.defs);
|
||
Vec.drop(&self.fn_syms);
|
||
Vec.drop(&self.fn_nodes);
|
||
drop_decls(self);
|
||
Vec.drop(&self.decls);
|
||
Vec.drop(&self.namespaces);
|
||
Vec.drop(&self.def_global);
|
||
Vec.drop(&self.loop_results);
|
||
Vec.drop(&self.container_langs);
|
||
var lci: usize = 0;
|
||
while lci < self.lang_conforms.len {
|
||
Vec.drop(&((self.lang_conforms.data as *LangConform) + lci).binds);
|
||
lci = lci + 1;
|
||
}
|
||
Vec.drop(&self.lang_conforms);
|
||
Vec.drop(&self.op_shapes);
|
||
var gri: usize = 0;
|
||
while gri < self.gen_reprs.len {
|
||
Vec.drop(&((self.gen_reprs.data as *GenReprEntry) + gri).fields);
|
||
gri = gri + 1;
|
||
}
|
||
Vec.drop(&self.gen_reprs);
|
||
Vec.drop(&self.call_edges);
|
||
Vec.drop(&self.bindings);
|
||
Vec.drop(&self.name_pool);
|
||
Vec.drop(&self.name_infos);
|
||
Vec.drop(&self.ct.types);
|
||
Vec.drop(&self.gen_binds);
|
||
Vec.drop(&self.gen_method_binds);
|
||
var gi: usize = 0;
|
||
while gi < self.gen_instances.len {
|
||
Vec.drop(&((self.gen_instances.data as *GenInstance) + gi).args);
|
||
gi = gi + 1;
|
||
}
|
||
Vec.drop(&self.gen_instances);
|
||
Vec.drop(&self.iter_infos);
|
||
Vec.drop(&self.shape_state.params);
|
||
var si: usize = 0;
|
||
while si < self.shape_state.instances.len {
|
||
Vec.drop(&((self.shape_state.instances.data as *ShapeInstance) + si).args);
|
||
si = si + 1;
|
||
}
|
||
Vec.drop(&self.shape_state.instances);
|
||
Vec.drop(&self.shape_state.type_binds);
|
||
Vec.drop(&self.shape_state.assoc_types);
|
||
var ci: usize = 0;
|
||
while ci < self.shape_state.composed.len {
|
||
Vec.drop(&((self.shape_state.composed.data as *ShapeComp) + ci).parts);
|
||
ci = ci + 1;
|
||
}
|
||
Vec.drop(&self.shape_state.composed);
|
||
Vec.drop(&self.shape_state.patch_roots);
|
||
Vec.drop(&self.ct.fn_refs);
|
||
Vec.drop(&self.refs);
|
||
Vec.drop(&self.reflect_stack);
|
||
Vec.drop(&self.reflect_srcs);
|
||
var mi: usize = 0;
|
||
while mi < self.mods.len {
|
||
var mm = (self.mods.data as *Module) + mi;
|
||
Vec.drop(&mm.refs);
|
||
Vec.drop(&mm.imports);
|
||
mi = mi + 1;
|
||
}
|
||
Vec.drop(&self.mods);
|
||
}
|
||
|
||
fn ref_info(self: *Sema, ref: Ref): *RefInfo {
|
||
if ref >= self.refs.len { grow_tables(self); }
|
||
if ref >= self.refs.len { ret null; }
|
||
ret (self.refs.data as *RefInfo) + ref;
|
||
}
|
||
|
||
fn tid_of(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].tid;
|
||
}
|
||
|
||
fn set_tid(self: *Sema, ref: Ref, t: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.tid = t; }
|
||
}
|
||
|
||
fn contained_at(self: *Sema, ref: Ref): bool {
|
||
if ref >= self.refs.len { ret false; }
|
||
ret (self.refs.data as *RefInfo)[ref].contained;
|
||
}
|
||
|
||
fn set_contained(self: *Sema, ref: Ref, v: bool) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.contained = v; }
|
||
}
|
||
|
||
// lang id -> IR type id。
|
||
fn ir_of(self: *Sema, lang: usize): usize {
|
||
ret LangType.ir_of(&self.types, lang);
|
||
}
|
||
|
||
fn ir_of_ref(self: *Sema, ref: Ref): usize {
|
||
ret LangType.ir_of(&self.types, tid_of(self, ref));
|
||
}
|
||
|
||
fn bool_ir(self: *Sema): usize {
|
||
ret LangType.ir_of(&self.types, LangType.make_bool(&self.types));
|
||
}
|
||
|
||
fn usize_ir(self: *Sema): usize {
|
||
ret LangType.ir_of(&self.types, LangType.usize_lang(&self.types));
|
||
}
|
||
|
||
fn node_def(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].def;
|
||
}
|
||
|
||
fn set_def(self: *Sema, ref: Ref, d: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.def = d; }
|
||
}
|
||
|
||
fn set_def_tid(self: *Sema, d: usize, t: usize) {
|
||
if d == 0 || d > self.defs.len { ret; }
|
||
var e = (self.defs.data as *Def) + (d - 1);
|
||
e.tid = t;
|
||
}
|
||
|
||
fn set_iter_info(self: *Sema, info: IterInfo) {
|
||
var i: usize = 0;
|
||
while i < self.iter_infos.len {
|
||
var e = (self.iter_infos.data as *IterInfo) + i;
|
||
if e.ref == info.ref { e.* = info; ret; }
|
||
i = i + 1;
|
||
}
|
||
var copy = info;
|
||
match Vec.push(&self.iter_infos, © as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
fn iter_info_of(self: *Sema, ref: Ref, out: *IterInfo): bool {
|
||
var i: usize = 0;
|
||
while i < self.iter_infos.len {
|
||
var e = (self.iter_infos.data as *IterInfo) + i;
|
||
if e.ref == ref { out.* = e.*; ret true; }
|
||
i = i + 1;
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
fn node_fn(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].fn_sym;
|
||
}
|
||
|
||
fn set_node_fn(self: *Sema, ref: Ref, s: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.fn_sym = s; }
|
||
}
|
||
|
||
fn node_builtin(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].builtin;
|
||
}
|
||
|
||
fn set_node_builtin(self: *Sema, ref: Ref, b: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.builtin = b; }
|
||
}
|
||
|
||
fn node_variant(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].variant;
|
||
}
|
||
|
||
fn set_node_variant(self: *Sema, ref: Ref, v: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.variant = v; }
|
||
}
|
||
|
||
fn node_initref(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].initref;
|
||
}
|
||
|
||
fn set_node_initref(self: *Sema, ref: Ref, v: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.initref = v; }
|
||
}
|
||
|
||
fn node_aggval(self: *Sema, ref: Ref): usize {
|
||
if ref >= self.refs.len { ret 0; }
|
||
ret (self.refs.data as *RefInfo)[ref].aggval;
|
||
}
|
||
|
||
fn set_node_aggval(self: *Sema, ref: Ref, v: usize) {
|
||
var r = ref_info(self, ref);
|
||
if r != null { r.aggval = v; }
|
||
}
|
||
|
||
fn sym_at(self: *Sema, i: usize): *FnSym {
|
||
ret (self.fn_syms.data as *FnSym) + i;
|
||
}
|
||
|
||
fn sym_builtin(self: *Sema, i: usize): Builtin {
|
||
if i >= self.fn_syms.len { ret .None; }
|
||
ret sym_at(self, i).builtin;
|
||
}
|
||
|
||
fn sym_op(self: *Sema, i: usize): usize {
|
||
if i >= self.fn_syms.len { ret 0; }
|
||
ret sym_at(self, i).op;
|
||
}
|
||
|
||
fn def_tid(self: *Sema, d: usize): usize {
|
||
if d == 0 || d > self.defs.len { ret 0; }
|
||
ret (self.defs.data as *Def)[d - 1].tid;
|
||
}
|
||
|
||
fn def_init(self: *Sema, d: usize): usize {
|
||
if d == 0 || d > self.defs.len { ret 0; }
|
||
ret (self.defs.data as *Def)[d - 1].init;
|
||
}
|
||
|
||
fn def_assigned(self: *Sema, d: usize): bool {
|
||
if d == 0 || d > self.defs.len { ret true; }
|
||
ret (self.defs.data as *Def)[d - 1].assigned;
|
||
}
|
||
|
||
fn set_def_init(self: *Sema, d: usize, init: Ref) {
|
||
if d == 0 || d > self.defs.len { ret; }
|
||
var e = (self.defs.data as *Def) + (d - 1);
|
||
e.init = init;
|
||
}
|
||
|
||
fn mark_assigned(self: *Sema, ref: Ref) {
|
||
var d = node_def(self, ref);
|
||
if d == 0 || d > self.defs.len { ret; }
|
||
var e = (self.defs.data as *Def) + (d - 1);
|
||
e.assigned = true;
|
||
}
|
||
|
||
// 局部名已在 lowering 解析;sema 只为声明分配 def id。
|
||
fn new_def(self: *Sema, tid: usize): usize {
|
||
var d = self.defs.len + 1;
|
||
var e = Def { .tid = tid, .init = 0, .assigned = false };
|
||
match Vec.push(&self.defs, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
var z: usize = 0;
|
||
match Vec.push(&self.def_global, &z as *u8) { .Ok => {}, .Err(error) => {} }
|
||
ret d;
|
||
}
|
||
|
||
fn new_global_def(self: *Sema, tid: usize, gidx: usize): usize {
|
||
var d = self.defs.len + 1;
|
||
var e = Def { .tid = tid, .init = 0, .assigned = false };
|
||
match Vec.push(&self.defs, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
var gv = gidx + 1;
|
||
match Vec.push(&self.def_global, &gv as *u8) { .Ok => {}, .Err(error) => {} }
|
||
ret d;
|
||
}
|
||
|
||
fn def_global_id(self: *Sema, d: usize): usize {
|
||
if d == 0 || d > self.def_global.len { ret 0; }
|
||
ret (self.def_global.data as *usize)[d - 1];
|
||
}
|
||
|
||
fn decl_at(self: *Sema, i: usize): *Decl {
|
||
ret (self.decls.data as *Decl) + i;
|
||
}
|
||
|
||
fn ns_at(self: *Sema, i: usize): *Namespace {
|
||
ret (self.namespaces.data as *Namespace) + (i - 1);
|
||
}
|
||
|
||
fn add_decl(self: *Sema, ns_i: usize, name: []u8, kind: DeclKind, node: Ref, container: Ref) {
|
||
var prior = lookup_decl_in(self, ns_i, name);
|
||
if prior != 0 && decl_at(self, prior - 1).node != node {
|
||
sema_error_code_at(self, node, ErrorCode.DuplicateDecl, "duplicate declaration", name);
|
||
ret;
|
||
}
|
||
var d = Decl { .name = name, .kind = kind, .node = node, .container = container,
|
||
.mod = self.cur_mod,
|
||
.state = .Unresolved, .lang = 0, .ret_lang = 0, .sym = 0, .gidx = 0,
|
||
.def = 0, .body_done = false };
|
||
match Vec.push(&self.decls, &d as *u8) { .Ok => {}, .Err(error) => {} }
|
||
var di = self.decls.len;
|
||
var e = NameEntry { .name = name, .decl = di };
|
||
match Vec.push(&ns_at(self, ns_i).entries, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
fn module_at(self: *Sema, i: usize): *Module {
|
||
ret (self.mods.data as *Module) + i;
|
||
}
|
||
|
||
fn set_module_path(self: *Sema, m: usize, path: ByteView) {
|
||
if m < self.mods.len { module_at(self, m).path = path; }
|
||
}
|
||
|
||
|
||
|
||
fn add_module(self: *Sema, path: ByteView, root: Ref, refs: []Ref): usize {
|
||
var stable_path = ByteView.from_slice(Bld.dup_bytes(&self.hir, ByteView.as_slice(path)));
|
||
var m = Module { .path = stable_path, .root = root, .refs = copy_refs(refs),
|
||
.imports = Vec.blank(@sizeof(ImportEdge), @alignof(ImportEdge)),
|
||
.root_lang = 0, .built = false };
|
||
match Vec.push(&self.mods, &m as *u8) { .Ok => {}, .Err(error) => {} }
|
||
record_ref_mods(self, self.mods.len - 1, refs);
|
||
ret self.mods.len - 1;
|
||
}
|
||
|
||
fn switch_mod(self: *Sema, m: usize) {
|
||
self.cur_mod = m;
|
||
}
|
||
|
||
fn scan_namespace_into(self: *Sema, ns_i: usize) {
|
||
var container = ns_at(self, ns_i).container;
|
||
if container == 0 {
|
||
var md = module_at(self, self.cur_mod);
|
||
var ri: usize = 0;
|
||
while ri < md.refs.len {
|
||
var i = (md.refs.data as *Ref)[ri];
|
||
var n = Bld.node_ptr(&self.hir, i);
|
||
if n != null && !contained_at(self, i) {
|
||
match n.kind {
|
||
.FnDecl(x) => { add_decl(self, ns_i, x.name, .Fn, i, 0); },
|
||
.TypeDecl(x) => { add_decl(self, ns_i, x.name, .Type, i, 0); },
|
||
.VarDecl(x) => { add_decl(self, ns_i, x.name, .Var, i, 0); },
|
||
.ConstDecl(x) => { add_decl(self, ns_i, x.name, .Const, i, 0); },
|
||
_ => {},
|
||
}
|
||
}
|
||
ri = ri + 1;
|
||
}
|
||
ret;
|
||
}
|
||
var an = Bld.node_ptr(&self.hir, container);
|
||
if an == null { ret; }
|
||
match an.kind {
|
||
.AggType(x) => {
|
||
var i: usize = 0;
|
||
while i < x.decls.len {
|
||
var dref = x.decls[i];
|
||
var dn = Bld.node_ptr(&self.hir, dref);
|
||
if dn != null {
|
||
match dn.kind {
|
||
.FnDecl(y) => { add_decl(self, ns_i, y.name, .Fn, dref, container); },
|
||
.TypeDecl(y) => { add_decl(self, ns_i, y.name, .Type, dref, container); },
|
||
.VarDecl(y) => { add_decl(self, ns_i, y.name, .Var, dref, container); },
|
||
.ConstDecl(y) => { add_decl(self, ns_i, y.name, .Const, dref, container); },
|
||
_ => {},
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
|
||
fn ensure_namespace(self: *Sema, container: Ref): usize {
|
||
// 容器名空间属于容器声明的模块,不能跟着调用方 cur_mod 走;否则同一个
|
||
// 生成 struct 会在调用方模块里重复注册同名 Decl,签名里的模块私有类型
|
||
// 也会在错误模块里解析。
|
||
var ns_mod = self.cur_mod;
|
||
if container != 0 {
|
||
var ci = ref_info(self, container);
|
||
if ci != null && ci.mod != 0 { ns_mod = ci.mod - 1; }
|
||
}
|
||
var i: usize = 0;
|
||
while i < self.namespaces.len {
|
||
var ns0 = (self.namespaces.data as *Namespace)[i];
|
||
if ns0.container == container && ns0.mod == ns_mod { ret i + 1; }
|
||
i = i + 1;
|
||
}
|
||
var saved = self.cur_mod;
|
||
if ns_mod != saved { switch_mod(self, ns_mod); }
|
||
var ns = Namespace { .mod = ns_mod, .container = container,
|
||
.entries = Vec.blank(@sizeof(NameEntry), @alignof(NameEntry)) };
|
||
match Vec.push(&self.namespaces, &ns as *u8) { .Ok => {}, .Err(error) => {} }
|
||
var idx = self.namespaces.len - 1;
|
||
scan_namespace_into(self, idx + 1);
|
||
if ns_mod != saved { switch_mod(self, saved); }
|
||
ret idx + 1;
|
||
}
|
||
|
||
fn lookup_decl_in(self: *Sema, ns_i: usize, name: []u8): usize {
|
||
var ns = ns_at(self, ns_i);
|
||
var i: usize = 0;
|
||
while i < ns.entries.len {
|
||
var e = (ns.entries.data as *NameEntry)[i];
|
||
if sid_same(e.name, name) { ret e.decl; }
|
||
i = i + 1;
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
// 名称解析:先当前容器,再文件根(0)。
|
||
fn lookup_decl(self: *Sema, container: Ref, name: []u8): usize {
|
||
var ni = ensure_namespace(self, container);
|
||
var d = lookup_decl_in(self, ni, name);
|
||
if d != 0 { ret d; }
|
||
if container != 0 {
|
||
var rni = ensure_namespace(self, 0);
|
||
ret lookup_decl_in(self, rni, name);
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
// 在指定模块的容器名空间按名查找。
|
||
fn lookup_decl_in_mod(self: *Sema, cmod: usize, container: Ref, name: []u8): usize {
|
||
var saved = self.cur_mod;
|
||
if cmod != saved { switch_mod(self, cmod); }
|
||
var ni = ensure_namespace(self, container);
|
||
var d = lookup_decl_in(self, ni, name);
|
||
if cmod != saved { switch_mod(self, saved); }
|
||
ret d;
|
||
}
|
||
|
||
// conformance 记录与方法查找由 sema_lang.spl 提供。
|
||
fn method_sym(self: *Sema, sl: usize, name: []u8): usize {
|
||
if self.lang_hooks.method_sym == null { ret 0; }
|
||
ret self.lang_hooks.method_sym(self as *any, sl, name);
|
||
}
|
||
|
||
fn iter_shape(self: *Sema): usize {
|
||
if self.lang_hooks.iter_shape == null { ret 0; }
|
||
ret self.lang_hooks.iter_shape(self as *any);
|
||
}
|
||
|
||
fn record_lang_conforms(self: *Sema, lang: usize) {
|
||
if self.lang_hooks.record_conforms != null { self.lang_hooks.record_conforms(self as *any, lang); }
|
||
}
|
||
|
||
fn copy_lang_conforms(self: *Sema, from: usize, to: usize) {
|
||
if self.lang_hooks.copy_conforms != null { self.lang_hooks.copy_conforms(self as *any, from, to); }
|
||
}
|
||
|
||
fn lang_conform_binds(self: *Sema, value_lang: usize, shape_lang: usize, binds: *Vec): bool {
|
||
if self.lang_hooks.conform_binds == null { ret false; }
|
||
ret self.lang_hooks.conform_binds(self as *any, value_lang, shape_lang, binds);
|
||
}
|
||
|
||
|
||
fn sid_same(a: []u8, b: []u8): bool { ret eq(a, b); }
|
||
|
||
// comptime 根的调用边 id(与函数 sym id 空间隔离)。
|
||
fn ct_root_of(ref: Ref): usize { ret 1099511627776 + ref; }
|
||
|
||
fn sid_none(): []u8 { ret ""; }
|
||
|
||
// ---- 全局名字 intern + 内置绑定表 ----
|
||
|
||
// 内置名/tag 绑定由 sema_bindings.spl 提供。
|
||
fn find_binding(self: *Sema, s: []u8): usize { if self.binding_hooks.find_binding == null { ret 0; } ret self.binding_hooks.find_binding(self as *any, s); }
|
||
fn builtin_tag_id(self: *Sema, fnref: Ref): Builtin { if self.binding_hooks.builtin_tag_id == null { ret .None; } ret self.binding_hooks.builtin_tag_id(self as *any, fnref); }
|
||
fn has_tag(self: *Sema, fnref: Ref, name: []u8): bool { if self.binding_hooks.has_tag == null { ret false; } ret self.binding_hooks.has_tag(self as *any, fnref, name); }
|
||
fn has_builtin_tag(self: *Sema, fnref: Ref): bool { if self.binding_hooks.has_builtin_tag == null { ret false; } ret self.binding_hooks.has_builtin_tag(self as *any, fnref); }
|
||
fn collect_builtins(self: *Sema) { if self.binding_hooks.collect_builtins != null { self.binding_hooks.collect_builtins(self as *any); } }
|
||
fn list_has_tag(self: *Sema, l: []Ref, tag: []u8): bool { if self.binding_hooks.list_has_tag == null { ret false; } ret self.binding_hooks.list_has_tag(self as *any, l, tag); }
|
||
fn builtin_tag_raw(self: *Sema, fnref: Ref): []u8 {
|
||
if self.binding_hooks.raw_tag == null { ret ""; }
|
||
ret self.binding_hooks.raw_tag(self as *any, fnref);
|
||
}
|
||
|
||
fn tag_arg(self: *Sema, fnref: Ref, name: []u8): []u8 {
|
||
if self.binding_hooks.tag_arg == null { ret ""; }
|
||
ret self.binding_hooks.tag_arg(self as *any, fnref, name);
|
||
}
|
||
|
||
fn is_impl_hook(self: *Sema, node: Ref): bool { if self.hook_hooks.is_impl == null { ret false; } ret self.hook_hooks.is_impl(self as *any, node); }
|
||
|
||
|
||
fn ct_handle(self: *Sema, lang: usize): tid {
|
||
var i: usize = 0;
|
||
while i < self.ct.types.len {
|
||
if (self.ct.types.data as *usize)[i] == lang { ret i + 1; }
|
||
i = i + 1;
|
||
}
|
||
match Vec.push(&self.ct.types, &lang as *u8) { .Ok => {}, .Err(error) => {} }
|
||
ret self.ct.types.len;
|
||
}
|
||
|
||
fn ct_lang(self: *Sema, h: tid): usize {
|
||
if h == 0 || h > self.ct.types.len { ret 0; }
|
||
ret (self.ct.types.data as *usize)[h - 1];
|
||
}
|
||
|
||
// ---- comptime 类型生成器实例化 ----
|
||
|
||
// 类型解析与生成器由 sema_type.spl 提供。
|
||
fn resolve_type(self: *Sema, ref: Ref): usize { if self.type_hooks.resolve_type == null { ret 0; } ret self.type_hooks.resolve_type(self as *any, ref); }
|
||
fn resolve_param(self: *Sema, ref: Ref): usize { if self.type_hooks.resolve_param == null { ret 0; } ret self.type_hooks.resolve_param(self as *any, ref); }
|
||
fn type_gen_lang(self: *Sema, ref: Ref, x: Node.Kind.Call, self_lang: usize): usize { if self.type_hooks.type_gen == null { ret 0; } ret self.type_hooks.type_gen(self as *any, ref, x, self_lang); }
|
||
fn type_is_generator(self: *Sema, fn_ref: Ref): bool { if self.type_hooks.type_is_generator == null { ret false; } ret self.type_hooks.type_is_generator(self as *any, fn_ref); }
|
||
fn type_arg_lang(self: *Sema, ref: Ref): usize { if self.type_hooks.type_arg_lang == null { ret 0; } ret self.type_hooks.type_arg_lang(self as *any, ref); }
|
||
fn type_value_lang(self: *Sema, ref: Ref): usize { if self.type_hooks.type_value_lang == null { ret 0; } ret self.type_hooks.type_value_lang(self as *any, ref); }
|
||
fn type_handle_arg(self: *Sema, a0: usize): usize { if self.type_hooks.type_handle_arg == null { ret 0; } ret self.type_hooks.type_handle_arg(self as *any, a0); }
|
||
fn type_arg_handle(self: *Sema, a0: usize): usize { if self.type_hooks.type_arg_handle == null { ret 0; } ret self.type_hooks.type_arg_handle(self as *any, a0); }
|
||
fn type_query_int(self: *Sema, lang: usize, bid: Builtin): usize { if self.type_hooks.type_query_int == null { ret 0; } ret self.type_hooks.type_query_int(self as *any, lang, bid); }
|
||
fn is_type_query(bid: Builtin): bool {
|
||
match bid {
|
||
.Sizeof, .BitSizeof, .AlignOf, .FieldCount => { ret true; },
|
||
_ => {},
|
||
}
|
||
ret false;
|
||
}
|
||
fn type_builtin(self: *Sema, ref: Ref, args: []Ref, bid: Builtin): usize { if self.type_hooks.type_builtin == null { ret 0; } ret self.type_hooks.type_builtin(self as *any, ref, args, bid); }
|
||
fn emit_intrinsic(self: *Sema, ref: Ref, args: []Ref, bid: Builtin) { if self.type_hooks.emit_intrinsic != null { self.type_hooks.emit_intrinsic(self as *any, ref, args, bid); } }
|
||
fn builtin_tid(self: *Sema, name: []u8): usize { if self.type_hooks.builtin_tid == null { ret 0; } ret self.type_hooks.builtin_tid(self as *any, name); }
|
||
fn lit_u64(self: *Sema, ref: Ref): u64 { if self.type_hooks.lit_u64 == null { ret 0; } ret self.type_hooks.lit_u64(self as *any, ref); }
|
||
fn const_eval_usize(self: *Sema, ref: Ref): usize { if self.type_hooks.const_eval_usize == null { ret 0; } ret self.type_hooks.const_eval_usize(self as *any, ref); }
|
||
fn gen_spec_activate(self: *Sema, fnref: Ref) { if self.type_hooks.gen_spec_activate != null { self.type_hooks.gen_spec_activate(self as *any, fnref); } }
|
||
|
||
fn find_lang_decl(self: *Sema, name: []u8): usize {
|
||
var lm = lang_mod(self);
|
||
if lm < 0 { ret 0; }
|
||
var saved = self.cur_mod;
|
||
if (lm as usize) != saved { switch_mod(self, lm as usize); }
|
||
ensure_namespace(self, 0);
|
||
var i: usize = 0;
|
||
while i < self.decls.len {
|
||
var d = decl_at(self, i);
|
||
if d.mod == (lm as usize) && eq(d.name, name) {
|
||
if (lm as usize) != saved { switch_mod(self, saved); }
|
||
ret i + 1;
|
||
}
|
||
i = i + 1;
|
||
}
|
||
if (lm as usize) != saved { switch_mod(self, saved); }
|
||
ret 0;
|
||
}
|
||
|
||
// 把生成器声明体挂到内建布局类型上。调用方按 `#repr(...)` 查到 decl,
|
||
// 不再按 "Slice"/"Array"/"Int" 之类的名字分支。
|
||
fn attach_generator_lang(self: *Sema, t: usize, di: usize, args: []Ref, named: bool) {
|
||
if t == 0 || LangType.container_of(&self.types, t) != 0 { ret; }
|
||
if di >= self.decls.len { ret; }
|
||
ensure_decl(self, di);
|
||
var d = decl_at(self, di);
|
||
var name = d.name;
|
||
var callee = Bld.construct(&self.hir, .{ .kind = .Global(name) });
|
||
grow_tables(self);
|
||
var call_ref = Bld.construct(&self.hir, .{ .kind = .Call(.{
|
||
.is_comptime = true,
|
||
.callee = callee,
|
||
.args = Bld.dup_refs(&self.hir, args),
|
||
}) });
|
||
grow_tables(self);
|
||
var cn = Bld.node_ptr(&self.hir, call_ref);
|
||
if cn == null { ret; }
|
||
var lang: usize = 0;
|
||
match cn.kind {
|
||
.Call(c) => { lang = instantiate_generator(self, 0, c, di, t); },
|
||
_ => {},
|
||
}
|
||
if lang == 0 { ret; }
|
||
var cont = LangType.container_of(&self.types, lang);
|
||
var cmod = LangType.container_mod_of(&self.types, lang);
|
||
if cont == 0 { ret; }
|
||
LangType.set_container(&self.types, t, cmod, cont);
|
||
LangType.set_decl_lang(&self.types, t, lang);
|
||
if named { LangType.set_name(&self.types, t, name); }
|
||
copy_lang_conforms(self, lang, t);
|
||
// 生成声明体里的 `@self` 必须指向内建布局类型,而不是声明用的 Struct。
|
||
set_container_lang(self, cont, t, true);
|
||
}
|
||
|
||
fn instantiate_generator(self: *Sema, ref: Ref, x: Node.Kind.Call, di: usize, self_lang: usize): usize {
|
||
if self.type_hooks.instantiate_generator == null { ret 0; }
|
||
ret self.type_hooks.instantiate_generator(self as *any, ref, x, di, self_lang);
|
||
}
|
||
|
||
fn ensure_lang_of(self: *Sema, t: usize) {
|
||
if self.lang_hooks.ensure_lang_of == null { ret; }
|
||
self.lang_hooks.ensure_lang_of(self as *any, t);
|
||
}
|
||
|
||
fn call_usize_arg(self: *Sema, ref: Ref): usize {
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n != null {
|
||
match n.kind {
|
||
.LitInt(_) => { ret lit_u64(self, ref) as usize; },
|
||
_ => {},
|
||
}
|
||
}
|
||
ret const_eval_usize(self, ref);
|
||
}
|
||
|
||
fn record_ct_fn_ref(self: *Sema, ref: Ref, sym: usize) {
|
||
var e = CtFnRef { .ref = ref, .sym = sym };
|
||
match Vec.push(&self.ct.fn_refs, &e as *u8) { .Ok => {}, .Err(err) => {} }
|
||
}
|
||
|
||
fn grow_tables(self: *Sema) {
|
||
var n = Bld.count(&self.hir);
|
||
while self.refs.len <= n {
|
||
var z = RefInfo { .tid = 0, .def = 0, .fn_sym = 0, .builtin = 0,
|
||
.variant = 0, .initref = 0, .aggval = 0, .mod = 0,
|
||
.contained = false };
|
||
match Vec.push(&self.refs, &z as *u8) { .Ok => {}, .Err(e) => {} }
|
||
}
|
||
}
|
||
|
||
fn fn_sym_for_ref(self: *Sema, fn_ref: Ref): usize {
|
||
var i: usize = 0;
|
||
while i < self.fn_syms.len {
|
||
if sym_at(self, i).ref == fn_ref { ret i + 1; }
|
||
i = i + 1;
|
||
}
|
||
// @meta.reflect 产出的是方法签名副本;反查原始 FnDecl 的符号。
|
||
var src = reflect_src_of(self, fn_ref);
|
||
if src != 0 && src != fn_ref { ret fn_sym_for_ref(self, src); }
|
||
ret 0;
|
||
}
|
||
|
||
fn record_reflect_src(self: *Sema, copy: Ref, src: Ref) {
|
||
if copy == 0 || src == 0 { ret; }
|
||
var e = ReflectSrc { .copy = copy, .src = src };
|
||
match Vec.push(&self.reflect_srcs, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
fn reflect_src_of(self: *Sema, r: Ref): Ref {
|
||
var i: usize = 0;
|
||
while i < self.reflect_srcs.len {
|
||
var e = (self.reflect_srcs.data as *ReflectSrc)[i];
|
||
if e.copy == r { ret e.src; }
|
||
i = i + 1;
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
fn empty_list(self: *Sema): []Ref {
|
||
ret (self.mods.data as *Ref)[0..0];
|
||
}
|
||
|
||
fn parse_u(s: []u8): usize {
|
||
var r: usize = 0;
|
||
var i: usize = 0;
|
||
while i < s.len {
|
||
var c = s[i];
|
||
if c >= '0' && c <= '9' { r = r * 10 + (c - '0') as usize; }
|
||
i = i + 1;
|
||
}
|
||
ret r;
|
||
}
|
||
|
||
fn itext(self: *Sema, v: usize): []u8 {
|
||
var buf = ByteBuffer.new();
|
||
match append_usize(&buf, v, 10, false) { .Ok => {}, .Err(error) => {} }
|
||
var t = Bld.dup_bytes(&self.hir, ByteBuffer.as_slice(buf));
|
||
ByteBuffer.drop(&buf);
|
||
ret t;
|
||
}
|
||
|
||
fn set_ct_int(self: *Sema, ref: Ref, v: usize) {
|
||
var t = itext(self, v);
|
||
var p = Bld.node_ptr(&self.hir, ref);
|
||
if p != null { p.kind = .LitInt(t); }
|
||
set_tid(self, ref, LangType.usize_lang(&self.types));
|
||
}
|
||
|
||
fn find_decl_by_node(self: *Sema, m: usize, node: Ref): usize {
|
||
var i: usize = 0;
|
||
while i < self.decls.len {
|
||
var d = decl_at(self, i);
|
||
if d.mod == m && d.node == node { ret i + 1; }
|
||
i = i + 1;
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
// 把全部 `#!builtin` 原型(extern 形态)并入 comptime 发射集:它们的调用经桥。
|
||
|
||
fn record_call_from(self: *Sema, from: usize, to: usize) { var e = CallEdge { .from = from, .to = to };
|
||
match Vec.push(&self.call_edges, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
fn record_call(self: *Sema, to: usize) {
|
||
if self.cur_fn == 0 { ret; }
|
||
var e = CallEdge { .from = self.cur_fn, .to = to };
|
||
match Vec.push(&self.call_edges, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
// 从 root 出发的可达函数 sym(1 起)集合(去重)。
|
||
fn reachable_from(self: *Sema, root: usize): Vec {
|
||
var out = Vec.blank(@sizeof(usize), @alignof(usize));
|
||
var queue = Vec.blank(@sizeof(usize), @alignof(usize));
|
||
var i: usize = 0;
|
||
while i < self.call_edges.len {
|
||
var e = (self.call_edges.data as *CallEdge)[i];
|
||
if e.from == root { add_uniq(&out, &queue, e.to); }
|
||
i = i + 1;
|
||
}
|
||
while queue.len > 0 {
|
||
var s = (queue.data as *usize)[queue.len - 1];
|
||
queue.len = queue.len - 1;
|
||
var j: usize = 0;
|
||
while j < self.call_edges.len {
|
||
var e = (self.call_edges.data as *CallEdge)[j];
|
||
if e.from == s { add_uniq(&out, &queue, e.to); }
|
||
j = j + 1;
|
||
}
|
||
}
|
||
Vec.drop(&queue);
|
||
ret out;
|
||
}
|
||
|
||
fn add_uniq(out: *Vec, queue: *Vec, v: usize) {
|
||
var i: usize = 0;
|
||
while i < out.len {
|
||
if (out.data as *usize)[i] == v { ret; }
|
||
i = i + 1;
|
||
}
|
||
match Vec.push(out, &v as *u8) { .Ok => {}, .Err(error) => {} }
|
||
match Vec.push(queue, &v as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
// 类型的“解包”:`type .m = C` 形式别名,若目标容器名空间内有同名成员 m,优先用该成员。
|
||
fn resolve_unpack_member(self: *Sema, d: usize, name: []u8): usize {
|
||
if d == 0 { ret 0; }
|
||
var di = d - 1;
|
||
if @enum2int(decl_at(self, di).kind) != @enum2int(DeclKind.Type) { ret 0; }
|
||
ensure_decl(self, di);
|
||
var tlang = decl_at(self, di).lang;
|
||
var cmod = LangType.container_mod_of(&self.types, tlang);
|
||
var cont = LangType.container_of(&self.types, tlang);
|
||
if cont == 0 && cmod == 0 { ret 0; }
|
||
ret lookup_decl_in_mod(self, cmod, cont, name);
|
||
}
|
||
|
||
// 按名查找有效声明(应用解包),返回 decl+1。
|
||
fn find_decl(self: *Sema, name: []u8): usize {
|
||
var d = lookup_decl(self, self.current_container, name);
|
||
if d == 0 { d = lookup_decl(self, 0, name); }
|
||
if d == 0 {
|
||
var bm = builtins_mod(self);
|
||
if bm >= 0 { d = lookup_decl_in_mod(self, bm as usize, 0, name); }
|
||
}
|
||
if d == 0 {
|
||
var lm = lang_mod(self);
|
||
if lm >= 0 { d = lookup_decl_in_mod(self, lm as usize, 0, name); }
|
||
}
|
||
if d == 0 { ret 0; }
|
||
var m = resolve_unpack_member(self, d, name);
|
||
if m != 0 { ret m; }
|
||
ret d;
|
||
}
|
||
|
||
fn build_roles(self: *Sema) {
|
||
if self.lang_hooks.build_roles == null { ret; }
|
||
self.lang_hooks.build_roles(self as *any);
|
||
}
|
||
|
||
fn role_mod(self: *Sema, role: usize): isize {
|
||
if self.lang_hooks.role_mod == null { ret -1; }
|
||
ret self.lang_hooks.role_mod(self as *any, role);
|
||
}
|
||
|
||
fn builtins_mod(self: *Sema): isize { ret role_mod(self, @enum2int(ModuleRole.Builtins) as usize); }
|
||
fn lang_mod(self: *Sema): isize { ret role_mod(self, @enum2int(ModuleRole.Lang) as usize); }
|
||
fn ops_mod(self: *Sema): isize { ret role_mod(self, @enum2int(ModuleRole.Ops) as usize); }
|
||
|
||
// 该表达式是否为「类型名引用」(如 `Color`),用于区分 `Color.Red` 与 `v.field`。
|
||
fn is_type_ref(self: *Sema, ref: Ref): bool {
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret false; }
|
||
match n.kind {
|
||
.Global(x) => {
|
||
var d = find_decl(self, x);
|
||
if d == 0 { ret false; }
|
||
if @enum2int(decl_at(self, d - 1).kind) == @enum2int(DeclKind.Type) { ret true; }
|
||
},
|
||
_ => {},
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
// 判断表达式是否承载类型值。类型值比较只应在这类表达式上尝试;
|
||
// 对普通值调用调用 resolve_type 会把 comptime 函数当成类型生成器预执行。
|
||
fn is_type_value_expr(self: *Sema, ref: Ref): bool {
|
||
if ref == 0 { ret false; }
|
||
if is_type_ref(self, ref) { ret true; }
|
||
var t = tid_of(self, ref);
|
||
if t != 0 { ret LangType.is_type(&self.types, t); }
|
||
var n = Bld.node_ptr(&self.hir, ref);
|
||
if n == null { ret false; }
|
||
match n.kind {
|
||
.IntType(_), .FloatType(_), .Bool, .Unit, .Any, .Type, .Never,
|
||
.PtrType(_), .SliceType(_), .ArrayType(_), .FnType(_), .TupleType(_),
|
||
.NamedType(_), .AggType(_), .ShapeApply(_), .ShapeUse(_), .ShapeNoDefault(_) => { ret true; },
|
||
.Global(x) => {
|
||
var d = find_decl(self, x);
|
||
ret d != 0 && @enum2int(decl_at(self, d - 1).kind) == @enum2int(DeclKind.Type);
|
||
},
|
||
_ => {},
|
||
}
|
||
ret false;
|
||
}
|
||
|
||
fn lookup_type(self: *Sema, name: []u8): usize {
|
||
var d = find_decl(self, name);
|
||
if d == 0 { ret 0; }
|
||
var di = d - 1;
|
||
if @enum2int(decl_at(self, di).kind) != @enum2int(DeclKind.Type) { ret 0; }
|
||
ensure_decl(self, di);
|
||
ret decl_at(self, di).lang;
|
||
}
|
||
|
||
|
||
// ---- (A) Zig Sema 风格:按 Decl 惰性分析 ----
|
||
|
||
fn container_decl_lang(self: *Sema, container: Ref): usize {
|
||
if container == 0 { ret 0; }
|
||
var i: usize = 0;
|
||
while i < self.decls.len {
|
||
var d = decl_at(self, i);
|
||
if @enum2int(d.kind) == @enum2int(DeclKind.Type) {
|
||
var n = Bld.node_ptr(&self.hir, d.node);
|
||
if n != null {
|
||
match n.kind {
|
||
.TypeDecl(x) => {
|
||
if x.ty == container {
|
||
ensure_decl(self, i);
|
||
ret decl_at(self, i).lang;
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
fn container_to_lang(self: *Sema, container: Ref): usize {
|
||
if container == 0 { ret module_at(self, self.cur_mod).root_lang; }
|
||
var cl = container_lang_find(self, container);
|
||
if cl != 0 { ret cl; }
|
||
var dl = container_decl_lang(self, container);
|
||
if dl != 0 { ret dl; }
|
||
ret tid_of(self, container);
|
||
}
|
||
|
||
fn container_lang_find(self: *Sema, container: Ref): usize {
|
||
var i: usize = 0;
|
||
while i < self.container_langs.len {
|
||
var e = (self.container_langs.data as *ContainerLang) + i;
|
||
if e.ref == container { ret e.lang; }
|
||
i = i + 1;
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
fn set_container_lang(self: *Sema, container: Ref, lang: usize, named: bool) {
|
||
if container == 0 || lang == 0 { ret; }
|
||
var i: usize = 0;
|
||
while i < self.container_langs.len {
|
||
var e = (self.container_langs.data as *ContainerLang) + i;
|
||
if e.ref == container {
|
||
if named { e.lang = lang; }
|
||
ret;
|
||
}
|
||
i = i + 1;
|
||
}
|
||
var e = ContainerLang { .ref = container, .lang = lang };
|
||
match Vec.push(&self.container_langs, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
fn add_import(self: *Sema, m: usize, ref: Ref, target: usize) {
|
||
var e = ImportEdge { .ref = ref, .mod = target };
|
||
match Vec.push(&module_at(self, m).imports, &e as *u8) { .Ok => {}, .Err(error) => {} }
|
||
}
|
||
|
||
fn import_target(self: *Sema, m: usize, ref: Ref): usize {
|
||
var im = &module_at(self, m).imports;
|
||
var i: usize = 0;
|
||
while i < im.len {
|
||
var e = (im.data as *ImportEdge)[i];
|
||
if e.ref == ref { ret e.mod + 1; }
|
||
i = i + 1;
|
||
}
|
||
ret 0;
|
||
}
|
||
|
||
// 确保模块 m 已建根 + 分析其顶层声明(不分析函数体)。
|
||
// 声明分析由 sema_decl.spl 提供。
|
||
fn ensure_decl(self: *Sema, di: usize) {
|
||
if self.decl_hooks.ensure_decl != null { self.decl_hooks.ensure_decl(self as *any, di); }
|
||
}
|
||
|
||
fn ensure_module(self: *Sema, m: usize) {
|
||
if self.decl_hooks.ensure_module != null { self.decl_hooks.ensure_module(self as *any, m); }
|
||
}
|
||
|
||
fn analyze_fn_body(self: *Sema, di: usize) {
|
||
if self.decl_hooks.analyze_fn_body != null { self.decl_hooks.analyze_fn_body(self as *any, di); }
|
||
}
|
||
|
||
|
||
fn drop_decls(self: *Sema) {
|
||
var i: usize = 0;
|
||
while i < self.namespaces.len {
|
||
Vec.drop(&(self.namespaces.data as *Namespace)[i].entries);
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
|
||
// 标记所有“位于聚合成员内”的 HirRef(这些不是模块级声明)。
|
||
fn mark_contained(self: *Sema) {
|
||
var nrefs = Bld.count(&self.hir);
|
||
while self.refs.len <= nrefs { grow_tables(self); }
|
||
var i: usize = 0;
|
||
while i < self.refs.len {
|
||
(self.refs.data as *RefInfo)[i].contained = false;
|
||
i = i + 1;
|
||
}
|
||
i = 1;
|
||
while i < Bld.count(&self.hir) {
|
||
var n = Bld.node_ptr(&self.hir, i);
|
||
if n != null {
|
||
match n.kind {
|
||
.AggType(x) => {
|
||
var d: usize = 0;
|
||
while d < x.decls.len {
|
||
set_contained(self, x.decls[d], true);
|
||
d = d + 1;
|
||
}
|
||
var k: usize = 0;
|
||
while k < x.fields.len {
|
||
set_contained(self, x.fields[k], true);
|
||
k = k + 1;
|
||
}
|
||
var m: usize = 0;
|
||
while m < x.injected.len {
|
||
set_contained(self, x.injected[m], true);
|
||
m = m + 1;
|
||
}
|
||
},
|
||
.Iter(x) => {
|
||
var c: usize = 0;
|
||
while c < x.captures.len {
|
||
set_contained(self, x.captures[c], true);
|
||
c = c + 1;
|
||
}
|
||
},
|
||
_ => {},
|
||
}
|
||
}
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
fn macro_arg_kind(self: *Sema, ref: Ref): []u8 {
|
||
var l = tid_of(self, ref);
|
||
if l == 0 { ret "other"; }
|
||
match LangType.kind_of(&self.types, l) {
|
||
.Int => { ret "int"; },
|
||
.Bool => { ret "bool"; },
|
||
.Slice => { ret "bytes"; },
|
||
_ => {},
|
||
}
|
||
ret "other";
|
||
}
|
||
|
||
// 表达式分析由 sema_expr.spl 提供,driver 注入 hook。
|
||
fn sema_expr(self: *Sema, ref: Ref, want: usize) {
|
||
if self.expr_hooks.sema_expr != null { self.expr_hooks.sema_expr(self as *any, ref, want); }
|
||
}
|
||
|
||
fn sema_body(self: *Sema, body: usize, ret_tid: usize) {
|
||
if self.expr_hooks.sema_body != null { self.expr_hooks.sema_body(self as *any, body, ret_tid); }
|
||
}
|
||
|
||
fn patch_body(self: *Sema, body: usize, ret_tid: usize) {
|
||
if self.expr_hooks.patch_body != null { self.expr_hooks.patch_body(self as *any, body, ret_tid); }
|
||
}
|
||
|
||
fn iter_capture_members(self: *Sema, ref: Ref): Vec {
|
||
if self.expr_hooks.iter_capture_members == null { ret Vec.blank(@sizeof(usize), @alignof(usize)); }
|
||
ret self.expr_hooks.iter_capture_members(self as *any, ref);
|
||
}
|
||
|
||
|
||
|
||
// 逐模块建根 + 惰性分析全部声明,再逐函数体。
|
||
// comptime fn 的体不在顶层提前解析:交给 eval 建 chunk 时首次解析(带 ct_building),
|
||
// 否则体内调用会被提前折成常量、破坏 chunk 的调用图。body_done 保证只解析一次。
|
||
fn decl_is_comptime_fn(self: *Sema, di: usize): bool {
|
||
var saved = self.cur_mod;
|
||
var dmod = decl_at(self, di).mod;
|
||
if dmod != saved { switch_mod(self, dmod); }
|
||
var n = Bld.node_ptr(&self.hir, decl_at(self, di).node);
|
||
var r = false;
|
||
if n != null {
|
||
match n.kind {
|
||
.FnDecl(x) => { r = x.is_comptime; },
|
||
_ => {},
|
||
}
|
||
}
|
||
if dmod != saved { switch_mod(self, saved); }
|
||
ret r;
|
||
}
|
||
|
||
// State dump is provided by sema_dump.spl.
|
||
fn dump(self: *Sema, out: *ByteBuffer): bool {
|
||
if self.dump_hooks.dump == null { ret false; }
|
||
ret self.dump_hooks.dump(self as *any, out);
|
||
}
|
||
|
||
|
||
fn run(self: *Sema) {
|
||
mark_contained(self);
|
||
build_roles(self);
|
||
collect_builtins(self);
|
||
var it = iter_shape(self);
|
||
if it != 0 { method_sym(self, it, "next"); }
|
||
var m: usize = 0;
|
||
while m < self.mods.len {
|
||
ensure_module(self, m);
|
||
m = m + 1;
|
||
}
|
||
var i: usize = 0;
|
||
while i < self.decls.len {
|
||
if @enum2int(decl_at(self, i).kind) == @enum2int(DeclKind.Fn) {
|
||
if !decl_is_comptime_fn(self, i) { analyze_fn_body(self, i); }
|
||
}
|
||
i = i + 1;
|
||
}
|
||
}
|
||
|
||
fn main() {} |