Files
sakurac/compiler/dispatch.sakura
T

442 lines
18 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file dispatch.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how calls reach the functions they call: the instance of a method
#! a value of a generic type is given, the order a trait's vtables hold its
#! methods in, each type's vtable for each trait it is referred to through, the
#! maps a reference to one trait becomes one to another by, and the C functions
#! the program calls by name.
import sakura::analyzer;
module sakura::compiler;
#! What a method called on a value of a type reaches: a function of the
#! program, or a trait's method, called through a vtable.
pub struct target {
var function: u32; #! The function called directly, or `no_function`.
var holder: u32; #! The trait whose vtable is called through, or 0.
var kind: u32; #! The method's function type, given the type's arguments.
}
#! What a method called on a value of a type reaches: the instance of a generic
#! type's method, given the type's arguments, a function of its own, or a
#! trait's method, which a value of a type parameter or a reference to a trait
#! calls through a vtable.
#! @param this The compiler.
#! @param decl The method's declaration.
#! @param owner The type it is called on, read through its references.
#! @returns What it reaches.
pub def lowerer::method_target(&mut this, decl: u32, owner: u32): target {
this.checker.resolve(decl);
var holder: u32 = this.checker.decls.owners.p[decl];
if holder != 0 && this.checker.decls.kinds.p[holder] == dk::property {
holder = this.checker.decls.owners.p[holder];
}
val plain := this.checker.unalias(owner);
if this.checker.trait_asks(decl) {
val kind := this.checker.trait_method_type(decl, plain);
val dynamic := this.checker.named_kind(plain) == dk::trait_type || this.checker.types.kind_of(plain) == ty::param;
if dynamic || !this.checker.has_body(decl) {
return target { function = no_function, holder = holder, kind = kind };
}
val own := span::<u32>::blank();
return target { function = this.default_of(decl, this.checker.implemented(plain, holder), plain, own), kind = kind };
}
if !this.checker.decls.has(decl, df::generic) {
return target { function = this.function_of(decl, 0), kind = this.checker.decls.types.p[decl] };
}
# A generic type's method is the instance its arguments give.
var args := vector::<u32>::new();
if this.checker.types.kind_of(plain) == ty::named {
for i in 0..this.checker.types.part_count(plain) {
args.append(this.checker.types.part_at(plain, i as u32));
}
}
val instance := this.checker.instance_of(decl, args.span, 0);
if instance == 0 {
return target { function = this.function_of(decl, 0), kind = this.checker.method_type(decl, plain) };
}
return target {
function = this.function_of(decl, instance),
kind = this.checker.instances[instance].kind
};
}
#! Checks the bodies of the instances asked for since the checker last did,
#! which lowering asks for where it calls a method the checker only looked up,
#! as a loop's `next` or a value's `close`.
#! @param this The compiler.
pub def lowerer::check_instances(&mut this): void {
val notes := this.checker.notes_made;
val globals := this.checker.checked_globals;
this.checker.check_reached();
if this.checker.notes_made != notes {
this.problems.append("Checking an instance lowering asked for made notes.");
}
if this.globals_lowered && this.checker.checked_globals != globals {
this.problems.append("Lowering reached a global nothing checked had reached, after the globals were given their values.");
}
}
#! Checks the body of an instance lowering asks for that the checker did not
#! check yet, alone.
#! @param this The compiler.
#! @param instance The instance.
pub def lowerer::check_instance(&mut this, instance: u32): void {
val notes := this.checker.notes_made;
this.checker.check_instance(instance);
if this.checker.notes_made != notes {
this.problems.append("Checking an instance lowering asked for made notes.");
}
if this.options.evaluating && this.checker.instances[instance].flawed {
this.problems.append("A function it calls had an error checking it.");
}
}
#! The methods of a trait in the order its vtables hold them: what it takes on
#! from the traits it composes, in order, and then what it declares itself, the
#! accessors of a property among them. A member taken on is numbered by each
#! trait taking it in its own order.
#! @param this The compiler.
#! @param holder The trait's declaration.
#! @returns The run of the methods in `lists`.
pub def lowerer::dispatch_of(&mut this, holder: u32): u32 {
if this.dispatches.get(&holder) is some(got) {
return got;
}
var methods := vector::<u32>::new();
this.collect_dispatch(holder, &mut methods, 0);
val run := this.lists.add(methods.span);
this.dispatches.set(holder, run);
return run;
}
#! Collects the methods a trait's vtables hold, those of the traits it composes
#! first, each once.
#! @param this The compiler.
#! @param holder The trait's declaration.
#! @param methods Where they are collected.
#! @param depth How deeply composition has been followed.
def lowerer::collect_dispatch(&mut this, holder: u32, methods: &mut vector<u32>, depth: u32): void {
return if depth > 16;
val composed := this.checker.composed_of(holder);
for i in 0..this.checker.decls.list_length(composed) {
val inner := this.checker.types.first_of(this.checker.decls.list_at(composed, i as u32));
this.collect_dispatch(inner, methods, depth + 1);
}
val run: u32 = this.checker.decls.children.p[holder];
for i in 0..this.checker.decls.list_length(run) {
val member := this.checker.decls.list_at(run, i as u32);
match this.checker.decls.kinds.p[member] {
dk::function => this.add_method(member, methods);
dk::property => {
val accessors: u32 = this.checker.decls.children.p[member];
for j in 0..this.checker.decls.list_length(accessors) {
this.add_method(this.checker.decls.list_at(accessors, j as u32), methods);
}
}
else => {}
}
}
}
#! Adds a method to those a vtable holds, where it is not there already.
#! @param this The compiler.
#! @param method The method's declaration.
#! @param methods The methods so far.
def lowerer::add_method(&mut this, method: u32, methods: &mut vector<u32>): void {
return if methods.span.contains(&method);
methods.append(method);
}
#! The slot of a method among those a trait's vtables hold.
#! @param this The compiler.
#! @param holder The trait's declaration.
#! @param method The method's declaration.
#! @returns The slot.
pub def lowerer::dispatch_index(&mut this, holder: u32, method: u32): u32 {
val run := this.dispatch_of(holder);
if this.lists.view(run).index_of(&method) is some(slot) {
return slot as u32;
}
return 0;
}
#! The signature a call through a trait's vtable passes arguments by: the
#! method's, taking `this` as a reference to the trait, and giving the type the
#! call gives, which a generic trait's method gives in terms of the trait's
#! arguments.
#! @param this The compiler.
#! @param method The method's declaration.
#! @param holder The trait referred to.
#! @param result The type the call gives.
#! @returns The signature's index.
pub def lowerer::trait_signature(&mut this, method: u32, holder: u32, result: u32, trait_kind: u32 = 0): u32 {
this.checker.resolve(method);
# A trait's method in terms of the arguments of the trait referred to, as a
# pack it takes is the parameters of its types.
val kind: u32 = trait_kind != 0
? this.checker.requirement_type(method, this.checker.bound_for(trait_kind, this.trait_of_method(method)), trait_kind)
: this.checker.decls.types.p[method];
var params := this.checker.types.parts_of(kind);
if params.length != 0 {
params[0] = this.checker.types.reference(this.checker.decls.types.p[holder], this.checker.decls.has(method, df::mut_this));
}
val made := this.checker.types.function(params.span, result, this.checker.types.second_of(kind) != 0);
return this.signature_of(this.call_type(made));
}
#! The vtable of a type for a trait it implements, made where it is new, which
#! names the function giving each of the trait's methods, or the trait's default
#! where the type gives none.
#! @param this The compiler.
#! @param kind The type referred to.
#! @param trait_kind The trait's type, given its arguments.
#! @returns The vtable's index.
pub def lowerer::vtable_of(&mut this, kind: u32, trait_kind: u32): u32 {
# An alias's methods come before those of what it stands for, so its vtable
# is its own.
val plain := this.checker.unalias(kind);
val key := pair_key(kind, trait_kind);
if this.vtable_ids.get(&key) is some(got) {
return got;
}
# Placed before its slots are filled, which may make more vtables.
val holder := this.checker.types.first_of(trait_kind);
val index := this.program.vtables.length as u32;
this.program.vtables.append(vtable {
type_id = kind,
trait_id = trait_kind,
layout = this.layout_of(plain),
functions = vector::<u32>::new(),
owners = vector::<u32>::new()
});
this.vtable_ids.set(key, index);
# Read by place, since making the vtables its slots name may add runs.
val run := this.dispatch_of(holder);
for i in 0..this.lists.length(run) {
val method: u32 = this.lists.at(run, i);
var function := no_function;
var owner := index;
val found := this.implementation(kind, method, trait_kind);
if found != 0 && this.trait_of_method(found) == 0 {
function = this.method_target(found, kind).function;
} else if this.checker.has_body(method) {
# The default addresses the slots of the trait declaring it.
val declarer := this.trait_of_method(method);
val declaring := this.checker.bound_for(trait_kind, declarer);
function = this.default_of(method, declaring, plain, span::<u32>::blank());
if declarer != holder {
owner = this.vtable_of(kind, declaring);
}
} else if found != 0 && this.checker.has_body(found) {
function = this.default_of(found, this.checker.bound_for(trait_kind, this.trait_of_method(found)), plain, span::<u32>::blank());
} else if found == 0 {
# A built-in type's method is a function the compiler makes.
function = this.builtin_function(plain, this.checker.decls.names.p[method]);
}
this.program.vtables[index].functions.append(function);
this.program.vtables[index].owners.append(owner);
}
return index;
}
#! The function a trait's default method is, for the trait given its
#! arguments: an instance of it where the trait is generic, so its body is
#! lowered with the types the arguments give, or for the type taking it, where
#! the trait is only a bound.
#! @param this The compiler.
#! @param method The default's declaration.
#! @param trait_kind The trait declaring it, given its arguments.
#! @param self_kind The type taking it.
#! @param own The default's own type arguments.
#! @returns The function's index.
pub def lowerer::default_of(&mut this, method: u32, trait_kind: u32, self_kind: u32, own: span<u32>): u32 {
return this.function_of(method, this.checker.default_instance(method, trait_kind, self_kind, own));
}
#! The trait a method is a member of, directly or as a property's accessor, or
#! 0.
#! @param this The compiler.
#! @param method The method's declaration.
#! @returns The trait's declaration, or 0.
pub def lowerer::trait_of_method(&this, method: u32): u32 {
var owner: u32 = this.checker.decls.owners.p[method];
if owner != 0 && this.checker.decls.kinds.p[owner] == dk::property {
owner = this.checker.decls.owners.p[owner];
}
return this.checker.trait_asks(method) ? owner : 0;
}
#! The method of a type implementing a trait's: of its overloads, the one taking
#! what the trait's does, or the trait's default where the type has none.
#! @param this The compiler.
#! @param kind The type.
#! @param method The trait's method.
#! @returns The type's method, or 0.
def lowerer::implementation(&mut this, kind: u32, method: u32, trait_kind: u32): u32 {
# A property's accessor is found through the property of its name.
var name: u32 = this.checker.decls.names.p[method];
val setter := this.checker.decls.has(method, df::setter);
val accessor := this.checker.decls.has(method, df::accessor);
val found := this.checker.find_member(kind, name);
return 0 if found.decl == 0;
# A trait asks for a `get` or a `set`, never the implementer's `get mut`.
if accessor {
return 0 if this.checker.decls.kinds.p[found.decl] != dk::property;
return setter ? this.checker.property_setter(found.decl) : this.checker.property_reader(found.decl, false);
}
# Of overloads, the one with the method's shape, in terms of the trait's
# arguments, as a pack it takes is the parameters of its types: as many
# parameters, and `&mut this` where it takes it, or else `&this`, which meets
# it too.
this.checker.resolve(method);
val asked := this.checker.requirement_type(method, this.checker.bound_for(trait_kind, this.trait_of_method(method)), kind);
val wanted := this.checker.types.part_count(asked);
var first: u32 = 0;
var counted: u32 = 0;
var walk := this.checker.decls.walk_overloads(found.decl);
while true {
val at := walk.next();
break if at == 0;
if this.checker.decls.kinds.p[at] == dk::function && this.checker.decls.has(at, df::method) {
this.checker.resolve(at);
val count := this.checker.types.part_count(this.checker.decls.types.p[at]);
if first == 0 {
first = at;
}
if count == wanted && counted == 0 {
counted = at;
}
if count == wanted && this.checker.decls.has(at, df::mut_this) == this.checker.decls.has(method, df::mut_this) {
return at;
}
}
}
return counted != 0 ? counted : first;
}
#! The map a reference to one trait becomes one to another by, made where it is
#! new, and filled once every vtable is made.
#! @param this The compiler.
#! @param from The trait's type converted from.
#! @param to The trait's type converted to.
#! @returns The map's index.
pub def lowerer::retrait_of(&mut this, from: u32, to: u32): u32 {
val key := pair_key(from, to);
if this.retrait_ids.get(&key) is some(got) {
return got;
}
val index := this.program.retraits.length as u32;
this.program.retraits.append(retrait { from = from, to = to, vtables = vector::<u32>::new() });
this.retrait_ids.set(key, index);
return index;
}
#! Fills each map between traits with the vtable each vtable of its trait
#! becomes, once the code is lowered. Making a vtable of a trait converted to
#! may make more, so every map is settled before any is filled.
#! @param this The compiler.
pub def lowerer::fill_retraits(&mut this): void {
return if this.program.retraits.length == 0;
var done: u64 = 0;
while done < this.program.vtables.length {
val count := this.program.vtables.length;
for v in done..count {
for i in 0..this.program.retraits.length {
val from: u32 = this.program.retraits[i].from;
val to: u32 = this.program.retraits[i].to;
if this.program.vtables[v].trait_id == from {
this.vtable_of(this.program.vtables[v].type_id, to);
}
}
}
done = count;
}
for i in 0..this.program.retraits.length {
var map := vector::<u32>::new();
for v in 0..this.program.vtables.length {
val held_trait: u32 = this.program.vtables[v].trait_id;
if held_trait == this.program.retraits[i].from {
map.append(this.vtable_of(this.program.vtables[v].type_id, this.program.retraits[i].to));
} else {
map.append(0);
}
}
this.program.retraits[i].vtables = move(&mut map);
}
}
#! The C function a declaration of an `extern module` without a body calls,
#! among those the program links, found by its name in its file's libraries.
#! @param this The compiler.
#! @param decl The declaration.
#! @returns The C function's index.
pub def lowerer::linked_of(&mut this, decl: u32): u32 {
if this.linked_ids.get(&decl) is some(got) {
return got;
}
val index := this.program.linked.length as u32;
var libraries := this.libraries_of(decl);
var first := libraries.length != 0 ? string::copy_of(&libraries[0]) : string::new(nothing {});
this.program.linked.append(linked {
name = this.checker.decls.names.p[decl],
library = move(&mut first),
libraries = move(&mut libraries),
decl = decl
});
this.linked_ids.set(decl, index);
return index;
}
#! The libraries a C function may be found in: those its file's `extern import`s
#! name for the target, or none for one the running program has.
#! @param this The lowerer.
#! @param decl The function's declaration.
#! @returns The libraries.
def lowerer::libraries_of(&mut this, decl: u32): vector<string> {
var libraries := vector::<string>::new();
val file: u32 = this.checker.decls.files.p[decl];
val saved := this.checker.at;
this.checker.use_table(this.checker.units[file].table);
for &each in this.checker.libraries {
continue if each.file != file;
val written := this.checker.text_of(this.checker.left_of(each.node));
var text := string::new(nothing {});
this.checker.decode_text(written.substr(1, written.length < 2 ? 0 : written.length - 2), &mut text);
libraries.append(move(&mut text));
}
this.checker.at = saved;
return libraries;
}
#! The function type as the code of a function takes its parameters: a variadic
#! one's last as a view of what it is given.
#! @param this The compiler.
#! @param kind The function type.
#! @returns The type.
pub def lowerer::call_type(&mut this, kind: u32): u32 {
val plain := this.checker.unalias(kind);
val count := this.checker.types.part_count(plain);
return plain if this.checker.types.kind_of(plain) != ty::function || this.checker.types.second_of(plain) == 0 || count == 0;
var params := this.checker.types.parts_of(plain);
val last: u32 = params.last;
if this.checker.types.kind_of(last) != ty::pack {
params.last = this.checker.span_of(last);
}
return this.checker.types.function(params.span, this.checker.types.first_of(plain), false);
}