Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-12-06 13:52:57 -06:00
commit 528ccf1523
782 files changed
+145156

No files matched your search

+303
View File
@@ -0,0 +1,303 @@
#! @file link.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the linking of what every file declared: each file's imports are
#! found among the modules declared, and each declaration made through a type,
#! as `vec2::length` or `list<T>::iter`, becomes a member of its type, with the
#! type parameters its receiver names. A nested type may itself wait for its
#! type, so those whose types are not yet members wait for another pass.
module sakura::analyzer;
#! Links the imports and the declarations made through types.
#! @param this The checker.
pub def checker::link(&mut this): void {
var imports := vector::<vector<u32>>::new();
for _ in 0..this.units.length {
imports.append(vector::<u32>::new());
}
for i in 0..this.pending_imports.length {
val each := this.pending_imports[i];
val file := each.file;
val node := each.node;
this.use_table(this.units[file].table);
val found := this.module_path(this.left_of(node), false);
if found == 0 {
this.report_unfound_import(node);
continue;
}
imports[file].append(found);
}
for file in 0..this.units.length {
val run := this.decls.make_list(imports[file].span);
this.units[file].imports = run;
}
this.attach_pending();
this.give_accessors_params();
}
#! Reports an import of a module no file declares, saying where `system` is
#! found where it begins with `system` and no file of it was read.
#! @param this The checker.
#! @param node The `import` node.
pub def checker::report_unfound_import(&mut this, node: u32): void {
val first := this.segment_name(this.left_of(node), 0);
if first == word::system && this.lacks_system() {
var text := string::from("No file declares this module. ", nothing {});
text.append(system_package);
this.report_text(node, ec::undefined, &text);
return;
}
this.report(node, ec::undefined, "No file declares this module.");
}
#! Makes each declaration made through a type a member of it. Each pass attaches
#! what it can. One whose type is a nested type not yet attached waits for the
#! next, and what is left when a pass attaches nothing names no type.
#! @param this The checker.
def checker::attach_pending(&mut this): void {
var waiting := vector::<u32>::new();
waiting.extend(this.pending.span);
var progress := true;
while progress && waiting.length != 0 {
progress = false;
var later := vector::<u32>::new();
for &id in waiting {
if this.attach(id, false) {
progress = true;
} else {
later.append(id);
}
}
waiting = move(&mut later);
}
for &id in waiting {
this.attach(id, true);
}
}
#! Gives each accessor its property's type parameters, which its receiver named.
#! @param this The checker.
def checker::give_accessors_params(&mut this): void {
for id in 1..this.decls.count {
continue if !this.decls.has(id as u32, df::accessor) || this.decls.kinds.p[id] == dk::none;
val params: u32 = this.decls.params.p[this.decls.owners.p[id]];
this.decls.params.p[id] = params;
if params != 0 {
this.decls.mark(id as u32, df::generic);
}
}
}
#! Makes the declaration a member of the type its receiver names.
#! @param this The checker.
#! @param id The declaration.
#! @param last Whether this is the last chance, which reports a receiver
#! naming no type rather than waiting.
#! @returns Whether it was attached, or reported.
def checker::attach(&mut this, id: u32, last: bool): bool {
val saved := this.at;
this.use_table(this.units[this.decls.files.p[id]].table);
this.at.home = this.decls.modules.p[id];
# A receiver is a path from the module, which no type around it shortens.
this.at.scope = 0;
val node: u32 = this.decls.nodes.p[id];
val receiver := this.receiver_of(node);
this.decls.top = this.decls.tops.p[id];
val owner := this.receiver_decl(receiver, last);
if owner == 0 {
this.at = saved;
this.decls.top = 0;
if last {
this.decls.mark(id, df::failed);
}
return last;
}
# What a trait asks for is declared within it. Through it, outside it, only
# functions of its own are, which take no `this`.
if this.decls.kinds.p[owner] == dk::trait_type
&& (this.decls.kinds.p[id] != dk::function || this.decls.has(id, df::method)) {
this.report(receiver, ec::invalid,
"A trait has no methods, associated variables, or nested types declared outside it. What it asks for is declared within it, and outside it only functions of its own, which take no `this`, as `def real::parse<T>(text: strview)`.");
this.decls.mark(id, df::failed);
this.at = saved;
this.decls.top = 0;
return true;
}
# A function of the trait's own takes a name it does not ask for.
if this.decls.kinds.p[owner] == dk::trait_type {
val asked := this.decls.member(owner, this.decls.names.p[id]);
if asked != 0 && this.decls.owners.p[asked] == owner && !this.receiver_declared(asked) {
this.report(node, ec::duplicate, "The trait already asks for a member of this name.");
this.decls.mark(id, df::failed);
this.at = saved;
this.decls.top = 0;
return true;
}
}
# Each segment of the receiver names the type parameters of the type it
# names, as `T` of `box<T>::get`, which the declaration takes first.
var params := vector::<u32>::new();
var level := owner;
for step in 0..this.segment_count(receiver) {
val index := this.segment_count(receiver) - 1 - step as u32;
val named := this.segment_args(receiver, index);
val made := this.declare_params(named, id);
# A `where` bounding the type's parameters further makes the method one only
# the types meeting it have.
for &written in this.run_view(named) {
if this.kind_of(written) == nk::type_param && this.left_of(written) != 0 {
this.decls.mark(id, df::bounding);
}
}
for k in 0..made.length {
params.insert_at(k, made[k]);
}
# Each stands for a parameter of the type, whose bounds it takes.
val wanted := this.own_param_count(level);
val owned: u32 = this.decls.params.p[level];
val total := this.decls.list_length(owned);
for k in 0..made.length {
if k < wanted as u64 && total >= wanted {
val source := this.decls.list_at(owned, total - wanted + k as u32);
val param: u32 = made[k];
this.mirrors.put(param, source);
# A constant parameter of the type is named a constant here too.
if this.decls.has(source, df::valued) {
this.decls.mark(param, df::valued);
} else if this.kind_of(this.decls.nodes.p[param]) == nk::const_param {
this.report(this.decls.nodes.p[param], ec::generic,
"A type parameter of the type is named here without `const`.");
}
}
}
if made.length as u32 != wanted && level != 0 {
var text := string::from("`", nothing {});
text.append(this.decl_text(level));
val words := $"` takes {wanted} type parameters, which a declaration through it names, as `box<T>::get`.";
text.append(words.span);
this.report_text(this.segment_node(receiver, index), ec::generic, &text);
}
level = this.decls.owners.p[level];
break if level == 0 || this.decls.kinds.p[level] == dk::none;
}
val own: u32 = this.decls.params.p[id];
params.extend(this.decls.list_view(own));
this.decls.params.p[id] = this.decls.make_list(params.span);
if params.length != 0 {
this.decls.mark(id, df::generic);
}
this.add_member(owner, id);
this.decls.top = 0;
this.at = saved;
return true;
}
#! How many type parameters a type declares of its own, not counting those of
#! the type it is nested in.
#! @param this The checker.
#! @param id The type's declaration.
#! @returns How many.
pub def checker::own_param_count(&this, id: u32): u32 {
return 0 if id == 0 || this.decls.kinds.p[id] == dk::builtin;
val saved_file: u32 = this.decls.files.p[id];
val syntax := &this.units[saved_file].syntax;
val node: u32 = this.decls.nodes.p[id];
val kind := syntax.node_kinds[node];
return 0 if kind != nk::struct_decl && kind != nk::union_decl && kind != nk::trait_decl
&& kind != nk::alias && kind != nk::function;
# A pattern, `def(Ts...): R` or `L[N]`, declares one for each type parameter it
# names.
var made: u32 = 0;
for &each in syntax.run_view(syntax.record(node, decl_slot::type_params)) {
if syntax.node_kinds[each] == nk::array_pattern {
made += syntax.node_rights[each] != 0 ? 2 : 1;
continue;
}
if syntax.node_kinds[each] != nk::type_pattern {
made++;
continue;
}
val inner := syntax.node_lefts[each];
made += syntax.run_length(inner) + (syntax.node_rights[each] != 0 ? 1 : 0);
}
return made;
}
#! The type declaration a receiver names, or 0 where it names none yet.
#! @param this The checker.
#! @param receiver The receiver's `type_name` node.
#! @param last Whether this is the last chance, which reports a receiver
#! naming no type.
#! @returns The declaration, or 0.
def checker::receiver_decl(&mut this, receiver: u32, last: bool): u32 {
var at: u32 = 0;
var home: u32 = 0;
var in_module := false;
for i in 0..this.segment_count(receiver) {
val name := this.segment_name(receiver, i as u32);
if i == 0 {
at = this.lookup_global(name, 0);
if at == 0 {
home = this.lookup_module(name);
in_module = home != 0;
}
} else if in_module {
at = this.modules.decl(home, name);
if at == 0 {
home = this.modules.find(home, name);
in_module = home != 0;
} else {
in_module = false;
}
} else {
at = this.decls.path_member(at, name);
}
if at == 0 && !in_module {
if last {
this.report(this.segment_node(receiver, i as u32), ec::not_type,
"A declaration is made through a type, and this names none.");
}
return 0;
}
}
return 0 if in_module;
if !this.is_type_decl(at) {
if last {
this.report(receiver, ec::not_type, "A declaration is made through a type, and this names none.");
}
return 0;
}
return at;
}
#! Whether the declaration declares a type.
#! @param this The checker.
#! @param id The declaration.
#! @returns Whether it does.
pub def checker::is_type_decl(&this, id: u32): bool {
return match this.decls.kinds.p[id] {
dk::builtin, dk::struct_type, dk::union_type, dk::enum_type, dk::trait_type,
dk::alias_type, dk::type_param, dk::assoc_type => true,
else => false
};
}