Files
sakurac/analyzer/checker/resolve.sakura
T

1353 lines
53 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file resolve.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the resolving of declarations: what each is, as a function's
#! parameters and result, a variable's type, an enum's members, or what an
#! alias stands for, worked out where it is first needed. A declaration being
#! resolved that is needed again depends on itself, which is reported rather
#! than followed. Also contains the resolving of types as they are written.
module sakura::analyzer;
#! Gives each type declaration its type, which naming it needs nothing else to
#! know: the type of its declaration, given its own type parameters.
#! @param this The checker.
pub def checker::name_types(&mut this): void {
for id in 1..this.decls.count {
val kind: dk = this.decls.kinds.p[id];
if kind == dk::struct_type || kind == dk::union_type || kind == dk::enum_type
|| kind == dk::trait_type || kind == dk::alias_type {
val own := this.param_types(id as u32);
this.decls.types.p[id] = this.types.named(id as u32, own.view());
} else if kind == dk::assoc_type {
this.decls.types.p[id] = this.types.param(id as u32);
} else if kind == dk::type_param {
this.decls.types.p[id] = this.decls.has(id as u32, df::packed)
? this.types.pack(span::<u32>::empty(), id as u32)
: this.types.param(id as u32);
}
}
}
#! The types of the declaration's type parameters, as its own body sees them.
#! @param this The checker.
#! @param id The declaration.
#! @returns The type of each of its type parameters, its owner's first.
pub def checker::param_types(&mut this, id: u32): vector<u32> {
var made := vector::<u32>::new();
val run: u32 = this.decls.params.p[id];
for &param in this.decls.list_view(run) {
made.append(this.decls.has(param, df::packed)
? this.types.pack(span::<u32>::empty(), param)
: this.types.param(param));
}
return made;
}
#! Begins checking within the declaration: in its file and module, with its
#! type parameters bound to the types of its arguments, where `args` gives
#! them, or else to themselves. Within a trait, `self` is bound too.
#! @param this The checker.
#! @param id The declaration.
#! @param args The pack of its type arguments, or 0 where it is checked as
#! itself.
#! @remarks The caller keeps `at` beforehand, and gives it to `leave`.
pub def checker::enter_decl(&mut this, id: u32, args: u32): void {
this.use_table(this.units[this.decls.files.p[id]].table);
this.at.home = this.decls.modules.p[id];
this.at.decl = id;
this.at.scope = this.scope_of(id, args);
this.at.instance = 0;
this.at.loops = 0;
this.at.result = bt::void_type;
this.at.marks = this.scope_mark();
this.at.opened = this.at.marks;
# Within a trait, `self` is what implements it, and each type it asks for is
# named as what that gives. In an instance of a default compiled for each type,
# the trait's parameters are its arguments, `self` is the type, and the
# default's own parameters follow. In an instance of any other generic trait's
# default, the trait's parameters are its arguments, and `self` is the trait
# given them.
val holder := this.enclosing_trait(id);
var skip: u32 = 0;
if holder != 0 && holder != id && args != 0 && this.typed_default(id) {
skip = this.decls.list_length(this.decls.params.p[holder]);
val self_kind := skip < this.types.part_count(args) ? this.types.part_at(args, skip) : bt::error;
this.bind(names::self_id, ref_type | self_kind);
this.bind_params(holder, args);
skip++;
} else if holder != 0 && holder != id && args != 0 && this.decls.list_length(this.decls.params.p[holder]) != 0 {
skip = this.decls.list_length(this.decls.params.p[holder]);
var given := vector::<u32>::new();
for i in 0..skip {
given.append(i < this.types.part_count(args) ? this.types.part_at(args, i as u32) : bt::error);
}
this.bind(names::self_id, ref_type | this.types.named(holder, given.view()));
this.bind_params(holder, args);
} else if holder != 0 {
this.bind(names::self_id, ref_type | this.types.param(this.self_decl(holder)));
if holder != id {
this.bind_params(holder, 0);
}
}
if holder != 0 {
val asked: u32 = this.decls.children.p[holder];
for &member in this.decls.list_view(asked) {
if this.decls.kinds.p[member] == dk::assoc_type {
this.bind(this.decls.names.p[member], ref_type | this.decls.types.p[member]);
}
}
}
# A member declared within its type names the type's parameters, and a type
# parameter's bounds name those of what declares it.
val kind: dk = this.decls.kinds.p[id];
val owner: u32 = this.decls.owners.p[id];
if owner != 0 && owner != holder && (kind == dk::field || kind == dk::enum_member || kind == dk::assoc_type
|| kind == dk::type_param) {
this.bind_params(owner, 0);
}
this.bind_params_from(id, args, skip);
}
#! Ends checking within a declaration, putting back where checking was.
#! @param this The checker.
#! @param saved Where checking was, as `at` held it before `enter_decl`.
pub def checker::leave(&mut this, saved: checker::frame): void {
this.unbind_to(this.at.marks);
this.at = saved;
}
#! Binds the names of the declaration's type parameters to the types of the
#! arguments `args` gives, or else to the parameters themselves.
#! @param this The checker.
#! @param id The declaration.
#! @param args The pack of its type arguments, or 0 to bind each parameter to
#! itself.
pub def checker::bind_params(&mut this, id: u32, args: u32): void {
this.bind_params_from(id, args, 0);
}
#! Binds the names of the declaration's type parameters to the types of the
#! arguments `args` gives from one on, as an instance of a generic trait's
#! default gives its own after the trait's.
#! @param this The checker.
#! @param id The declaration.
#! @param args The pack of type arguments, or 0 to bind each parameter to
#! itself.
#! @param from The argument its first parameter is given.
def checker::bind_params_from(&mut this, id: u32, args: u32, from: u32): void {
val run: u32 = this.decls.params.p[id];
for i in 0..this.decls.list_length(run) {
val param := this.decls.list_at(run, i as u32);
val at := from + i as u32;
val kind := args != 0 && at < this.types.part_count(args)
? this.types.part_at(args, at)
: this.decls.types.p[param];
this.bind(this.decls.names.p[param], ref_type | kind);
}
}
#! The trait the declaration is, or is a member of, or 0.
#! @param this The checker.
#! @param id The declaration.
#! @returns The trait's declaration, or 0.
pub def checker::enclosing_trait(&this, id: u32): u32 {
var at := id;
var steps := 0;
while at != 0 && steps < 4 {
return at if this.decls.kinds.p[at] == dk::trait_type;
at = this.decls.owners.p[at];
steps++;
}
return 0;
}
#! The declaration `self` is within the trait: a type parameter standing for
#! whatever implements it.
#! @param this The checker.
#! @param holder The trait's declaration.
#! @returns The declaration of its `self`, made the first time it is asked for.
pub def checker::self_decl(&mut this, holder: u32): u32 {
if this.selves.get(&holder) is some(got) {
return got;
}
val id := this.decls.add(dk::type_param, names::self_id, this.decls.files.p[holder],
this.decls.nodes.p[holder], this.decls.modules.p[holder]);
this.decls.own(id, holder);
this.decls.types.p[id] = this.types.param(id);
this.decls.states.p[id] = ds::resolved;
this.selves.set(holder, id);
return id;
}
#! Resolves what the declaration is, where it is not yet resolved.
#! @param this The checker.
#! @param id The declaration.
pub def checker::resolve(&mut this, id: u32): void {
this.depend(id);
val state: ds = this.decls.states.p[id];
return if state == ds::resolved;
# A type parameter is its own type while its bounds are resolved, so a bound
# naming it, as `T: ceiling<T>`, is no cycle.
return if state == ds::resolving && this.decls.kinds.p[id] == dk::type_param && !this.decls.has(id, df::valued);
if state == ds::resolving {
this.report_cycle(id);
return;
}
this.decls.states.p[id] = ds::resolving;
val saved := this.at;
# What is wrong with a declaration is its own, noted wherever it is first
# resolved, even where what reached it was being tried quietly.
val mute := this.mute;
this.mute = 0;
this.enter_decl(id, 0);
match this.decls.kinds.p[id] {
dk::function => this.resolve_function(id);
dk::variable => this.resolve_variable(id);
dk::field => this.resolve_variable(id);
dk::property => this.resolve_property(id);
dk::enum_type => this.resolve_enum(id);
dk::enum_member => {
this.leave(saved);
this.decls.states.p[id] = ds::unresolved;
this.resolve(this.decls.owners.p[id]);
return;
}
dk::alias_type => {
val target := this.resolve_type(this.syn().node_rights[this.decls.nodes.p[id]]);
this.decls.targets.p[id] = target;
# An alias of an alias is followed now, so one that comes back to itself is
# reported where it is declared.
if this.is_alias(target) {
this.resolve(this.types.first_of(target));
if this.decls.has(id, df::failed) {
this.decls.targets.p[id] = bt::error;
}
}
}
dk::type_param => {
if this.decls.has(id, df::valued) {
this.resolve_constant(id);
} else {
this.resolve_bounds(id, this.left_of(this.decls.nodes.p[id]));
this.inherit_bounds(id);
}
}
dk::assoc_type => {
val node: u32 = this.decls.nodes.p[id];
this.resolve_bounds(id, this.record(node, assoc_slot::bounds));
# Resolving a type may declare more, which moves the declaration's
# vectors, so what is resolved is stored once it is made.
val fallback := this.right_of(node);
val target: u32 = fallback == 0 ? 0 : this.resolve_type(fallback);
this.decls.targets.p[id] = target;
}
else => {}
}
this.leave(saved);
this.mute = mute;
this.decls.states.p[id] = ds::resolved;
}
#! Reports a declaration depending on itself.
#! @param this The checker.
#! @param id The declaration.
def checker::report_cycle(&mut this, id: u32): void {
return if this.decls.has(id, df::failed);
this.decls.mark(id, df::failed);
var text := string::from("`", nothing {});
text.append(this.decl_text(id));
text.append("` depends on itself, so what it is cannot be worked out.");
this.report_decl(id, ec::cycle, text.view());
}
#! Resolves a function's parameters and result into its function type.
#! @param this The checker.
#! @param id The function's declaration.
def checker::resolve_function(&mut this, id: u32): void {
val node: u32 = this.decls.nodes.p[id];
if this.decls.has(id, df::accessor) {
this.resolve_accessor(id, node);
return;
}
# What a trait's method asks of the trait's parameters and `self` holds within
# its signature, as it does within its body.
val proved := this.proved_params.length;
this.prove_outer(id);
# A variadic last parameter may take a pack, as `args: Ts...` does.
var params := vector::<u32>::new();
val run := this.record(node, function_slot::params);
val count := this.run_length(run);
for i in 0..count {
val param := this.run_at(run, i as u32);
if i == 0 && this.decls.has(id, df::method) {
params.append(this.types.reference(this.self_type(id), this.decls.has(id, df::mut_this)));
# A type written for `this`, as `this: &rect`, is what `this` is, so it is
# resolved only for what its names name, which an editor shows.
if this.left_of(param) != 0 {
this.resolve_quietly(this.left_of(param));
}
continue;
}
val written := this.left_of(param);
val kind := this.resolve_type(written);
val packs := i as u32 + 1 == count && this.decls.has(id, df::variadic) && this.types.kind_of(kind) == ty::pack;
params.append(packs || this.valid_value(written, kind) ? kind : bt::error);
}
val written := this.record(node, function_slot::result);
val result := written == 0 ? bt::void_type : this.result_type(written);
this.decls.types.p[id] = this.types.function(params.view(), result, this.decls.has(id, df::variadic));
this.unprove_to(proved);
}
#! The bounds a trait's method puts on the trait's type parameters and `self`,
#! as `where self: backward_iterator<T>`, which only that method asks of them:
#! a list of each parameter's declaration followed by its bound, worked out the
#! first time it is asked for.
#! @param this The checker.
#! @param method The method's declaration.
#! @returns The list, or 0 where it puts none.
pub def checker::outer_bounds(&mut this, method: u32): u32 {
return 0 if this.decls.kinds.p[method] != dk::function || !this.writes_outer(method);
val holder := this.enclosing_trait(method);
return 0 if holder == 0 || holder == method;
if this.outers.get(&method) is some(got) {
return got;
}
this.outers.set(method, 0);
val syntax := &this.units[this.decls.files.p[method]].syntax;
val node: u32 = this.decls.nodes.p[method];
val run := syntax.extras[syntax.node_lefts[node] as u64 + function_slot::outer as u64];
var made := vector::<u32>::new();
val saved := this.at;
this.enter_decl(method, 0);
for &each in this.run_view(run) {
val name := this.name_of(each);
var param: u32 = name == names::self_id ? this.self_decl(holder) : 0;
for &own in this.decls.list_view(this.decls.params.p[holder]) {
param = this.decls.names.p[own] == name ? own : param;
}
continue if param == 0;
for &written in this.run_view(this.left_of(each)) {
val kind := this.resolve_type(written);
if kind != bt::error && this.named_kind(kind) != dk::trait_type {
this.report(written, ec::not_type, "A bound is a trait.");
continue;
}
continue if kind == bt::error;
made.append(param);
made.append(kind);
}
}
this.leave(saved);
val list := this.decls.make_list(made.view());
this.outers.set(method, list);
return list;
}
#! Gives the trait's parameters and `self` the bounds a trait's method puts on
#! them, where its signature and its own body are checked, as an `if const`
#! gives the branch it guards those it proves.
#! @param this The checker.
#! @param method The method's declaration.
pub def checker::prove_outer(&mut this, method: u32): void {
val list := this.outer_bounds(method);
var i: u32 = 0;
while i + 1 < this.decls.list_length(list) {
this.proved_params.append(this.decls.list_at(list, i));
this.proved_bounds.append(this.decls.list_at(list, i + 1));
i += 2;
}
}
#! Resolves a type for what its names name, noting nothing of it.
#! @param this The checker.
#! @param node The type's node.
def checker::resolve_quietly(&mut this, node: u32): void {
val dropped := this.dropped;
val uninferred := this.uninferred;
this.mute++;
this.resolve_type(node);
this.mute--;
this.dropped = dropped;
this.uninferred = uninferred;
}
#! Resolves an accessor of a property: a getter takes `this` and gives the
#! property's type, and a setter takes `this` and a value of it.
#! @param this The checker.
#! @param id The accessor's declaration.
#! @param node The `accessor` node.
def checker::resolve_accessor(&mut this, id: u32, node: u32): void {
val property: u32 = this.decls.owners.p[id];
this.resolve(property);
val kind: u32 = this.decls.types.p[property];
var params := vector::<u32>::new();
params.append(this.types.reference(this.self_type(id), this.decls.has(id, df::mut_this)));
val run := this.left_of(node);
for i in 1..this.run_length(run) {
val param := this.run_at(run, i as u32);
val written := this.left_of(param);
params.append(written == 0 ? kind : this.resolve_type(written));
}
val result: u32 = this.decls.has(id, df::setter) ? bt::void_type : kind;
this.decls.types.p[id] = this.types.function(params.view(), result, false);
}
#! The type `this` refers to within a method: its type, given the method's
#! first type parameters, which its receiver named, or `self` within a trait.
#! @param this The checker.
#! @param id The method's declaration.
#! @returns The type `this` refers to.
pub def checker::self_type(&mut this, id: u32): u32 {
var owner: u32 = this.decls.owners.p[id];
if owner != 0 && this.decls.kinds.p[owner] == dk::property {
owner = this.decls.owners.p[owner];
}
return bt::error if owner == 0;
return this.types.param(this.self_decl(owner)) if this.decls.kinds.p[owner] == dk::trait_type;
# The owner's parameters are the first of the method's, from its receiver.
val from_property := this.decls.kinds.p[this.decls.owners.p[id]] == dk::property;
return this.own_scope(owner, from_property ? this.decls.owners.p[id] : id, 0);
}
#! Resolves a variable, constant, or field: its type as written, or as its
#! value gives it. A constant's value is worked out here, since a type may need
#! it, as the length of a fixed array.
#! @param this The checker.
#! @param id The declaration.
def checker::resolve_variable(&mut this, id: u32): void {
val node: u32 = this.decls.nodes.p[id];
val written := this.record(node, variable_slot::type_expr);
val value := this.record(node, variable_slot::value);
var kind: u32 = bt::error;
if written != 0 && this.kind_of(written) == nk::spread {
kind = this.resolve_type(this.left_of(written));
} else if written != 0 {
kind = this.value_type(written);
} else if value != 0 {
# A constant declared through a type is of that type where it fits it, so
# `val open_flags::read := 1;` is an `open_flags`.
val own := this.infer_expr(value);
val owner: u32 = this.decls.owners.p[id];
kind = this.settle(own);
if this.types.is_untyped(own) && owner != 0 && this.is_type_decl(owner) && this.decls.kinds.p[owner] != dk::trait_type {
val held: u32 = this.decls.types.p[owner];
if this.fits_node(value, held) == fit::constant {
kind = held;
this.set_convert(value, held);
}
} else if kind != own && kind != bt::error {
this.set_convert(value, kind);
}
this.set_flag(value, rf::checked);
}
this.decls.types.p[id] = kind;
if this.decls.has(id, df::constant) && value != 0 {
if written != 0 {
this.check_expr(value, kind);
this.set_flag(value, rf::checked);
}
# A constant's value that folding does not give, as one calling a
# function, is run while checking.
if !this.is_known(value) && this.type_at(value) != bt::error {
var made := ev {};
if this.evaluate(value, this.decls.nodes.p[id], &mut made) {
if made.array {
this.ev_arrays.set(id, made);
} else {
this.set_value(value, made.scalar);
}
}
}
if this.is_known(value) {
this.set_decl_value(id, this.value_at(value));
}
}
}
#! Resolves a property's type, which is never `void`.
#! @param this The checker.
#! @param id The property's declaration.
def checker::resolve_property(&mut this, id: u32): void {
val written := this.record(this.decls.nodes.p[id], property_slot::type_expr);
val kind := this.resolve_type(written);
if kind == bt::void_type {
this.report(written, ec::invalid, "A property holds a value, so its type is not `void`.");
}
this.decls.types.p[id] = kind;
}
#! Records the constant a declaration holds.
#! @param this The checker.
#! @param id The declaration.
#! @param value The constant.
pub def checker::set_decl_value(&mut this, id: u32, value: constant): void {
this.decl_values.put(id, this.intern_constant(value) + 1);
}
#! The constant the declaration holds, where it holds one known while compiling.
#! @param this The checker.
#! @param id The declaration.
#! @param out Where the constant is written, where it holds one.
#! @returns Whether it holds one.
pub def checker::decl_value(&this, id: u32, out: &mut constant): bool {
val held := this.decl_values.at_or_zero(id);
return false if held == 0;
out = this.constants.p[held - 1];
return true;
}
#! Resolves an enum: the integer type holding it, and each member's value, one
#! more than the member before unless given one. A member given an earlier
#! member's value is another name for it, but one counting up onto it is a
#! mistake.
#! @param this The checker.
#! @param id The enum's declaration.
def checker::resolve_enum(&mut this, id: u32): void {
val node: u32 = this.decls.nodes.p[id];
val written := this.record(node, enumeration_slot::integer);
var integer: u32 = bt::int32;
if written != 0 {
integer = this.resolve_type(written);
if !this.types.is_integer(integer) && integer != bt::error {
this.report(written, ec::not_type, "An enum is held as an integer type.");
integer = bt::int32;
}
}
this.decls.targets.p[id] = integer;
var seen := vector::<constant>::new();
var next := constant {};
val run: u32 = this.decls.children.p[id];
for i in 0..this.decls.list_length(run) {
val member := this.decls.list_at(run, i as u32);
val at: u32 = this.decls.nodes.p[member];
val given := this.right_of(at);
var value := next;
if given != 0 {
this.check_expr(given, integer);
if this.is_known(given) {
value = this.value_at(given);
} else if this.type_at(given) != bt::error {
this.report(given, ec::constant, "A member's value is known while compiling: a literal, a constant, or another member.");
}
} else {
for &earlier in seen {
if this.compare_constants(earlier, &value) == 0 {
this.report(at, ec::duplicate, "The member counts up onto the value of an earlier one; give it a value of its own.");
break;
}
}
if !this.in_range(integer, &value) {
var text := string::from("The member's value is outside the range of `", nothing {});
val named := this.describe(integer);
text.append(named.view());
text.append("`.");
this.report_text(at, ec::overflow, &text);
}
}
seen.append(value);
this.set_decl_value(member, value);
this.decls.types.p[member] = this.decls.types.p[id];
this.decls.states.p[member] = ds::resolved;
this.fold_add(&value, 1, false, &mut next);
}
}
#! Resolves the bounds of a type parameter, or of a type a trait asks for, each
#! a trait.
#! @param this The checker.
#! @param id The type parameter's declaration.
#! @param run The run of the nodes of its bounds.
def checker::resolve_bounds(&mut this, id: u32, run: u32): void {
var bounds := vector::<u32>::new();
for &written in this.run_view(run) {
val kind := this.resolve_type(written);
if kind != bt::error && this.named_kind(kind) != dk::trait_type {
this.report(written, ec::not_type, "A bound is a trait.");
continue;
}
bounds.append(kind);
}
this.decls.children.p[id] = this.decls.make_list(bounds.view());
}
#! Resolves the type of a constant parameter's value: the type written for it, or
#! where a receiver names one of its type's, the type that declares it. A
#! constant is of its type, so it takes no bounds, and of a type that closes
#! nothing, since instances are told apart by it and it is copied wherever it is
#! read.
#! @param this The checker.
#! @param id The constant parameter's declaration.
def checker::resolve_constant(&mut this, id: u32): void {
val node: u32 = this.decls.nodes.p[id];
if this.run_length(this.left_of(node)) != 0 {
this.report(this.run_at(this.left_of(node), 0), ec::generic,
"A constant parameter is a value of the type written for it, so it takes no bounds.");
}
# The length a pattern, `L[N]`, names is a `u64`, as the lengths of arrays are.
val written: u32 = this.kind_of(node) == nk::const_param ? this.right_of(node) : 0;
var kind: u32 = written != 0 ? this.value_type(written) : this.kind_of(node) == nk::const_param ? bt::uint64 : bt::error;
if this.mirrors.at_or_zero(id) != 0 {
val source: u32 = this.mirrors.at_or_zero(id);
this.resolve(source);
# The type's parameters are the parameters of the declaration standing for them.
var from := vector::<u32>::new();
var to := vector::<u32>::new();
val holder: u32 = this.decls.owners.p[id];
val run: u32 = this.decls.params.p[holder];
for &param in this.decls.list_view(run) {
if this.mirrors.at_or_zero(param) != 0 {
from.append(this.mirrors.at_or_zero(param));
to.append(this.decls.types.p[param]);
}
}
val given := this.substitute(this.decls.targets.p[source], from.view(), to.view());
if written != 0 && kind != given && kind != bt::error && given != bt::error {
var text := string::from("The constant is of `", nothing {});
val named := this.describe(given);
text.append(named.view());
text.append("` where its type declares it, so it is written that way here, or by its name alone.");
this.report_text(written, ec::generic, &text);
}
this.decls.targets.p[id] = given;
return;
}
this.decls.targets.p[id] = kind;
if written != 0 && kind != bt::error && !this.types.is_open(kind) {
this.check_constant_type(written, kind);
}
}
#! Reports a type no constant can be of: `void`, a trait, the address of a C
#! function, and what closes what it holds.
#! @param this The checker.
#! @param node The node a note is placed at.
#! @param kind The type.
#! @returns Whether a constant can be of it.
pub def checker::check_constant_type(&mut this, node: u32, kind: u32): bool {
val plain := this.unalias(kind);
if this.is_std(plain, word::cfn) {
this.report(node, ec::generic, "The address of a C function is only known once the program runs, so it is no constant.");
return false;
}
if this.types.kind_of(plain) == ty::pack {
this.report(node, ec::generic, "A pack is many values, so it is no constant.");
return false;
}
if this.closes(kind) {
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.view());
text.append("` closes what it holds, so it is no constant: a constant parameter is given a value that closes nothing, such as a number, text, or a struct of them.");
this.report_text(node, ec::generic, &text);
return false;
}
return true;
}
#! Gives a type parameter a receiver names, as `T` of `box<T>::get`, the
#! bounds of the type's parameter it stands for, in terms of the declaration's
#! own parameters.
#! @param this The checker.
#! @param id The type parameter's declaration.
def checker::inherit_bounds(&mut this, id: u32): void {
val source: u32 = this.mirrors.at_or_zero(id);
return if source == 0;
this.resolve(source);
val inherited: u32 = this.decls.children.p[source as u32];
return if this.decls.list_length(inherited) == 0;
# The type's parameters become the parameters of the declaration standing for them.
val holder: u32 = this.decls.owners.p[id];
var from := vector::<u32>::new();
var to := vector::<u32>::new();
val run: u32 = this.decls.params.p[holder];
for &param in this.decls.list_view(run) {
if this.mirrors.at_or_zero(param) != 0 {
from.append(this.mirrors.at_or_zero(param));
to.append(this.decls.types.p[param]);
}
}
var bounds := this.decls.list_of(this.decls.children.p[id]);
for i in 0..this.decls.list_length(inherited) {
bounds.append(this.substitute(this.decls.list_at(inherited, i as u32), from.view(), to.view()));
}
this.decls.children.p[id] = this.decls.make_list(bounds.view());
}
#! The type a type is written as.
#! @param this The checker.
#! @param node The type's node.
#! @returns The type, or the error type where it is wrongly written.
pub def checker::resolve_type(&mut this, node: u32): u32 {
return bt::error if node == 0;
match this.kind_of(node) {
nk::type_name => return this.resolve_type_name(node);
nk::type_ref, nk::type_ptr => {
val inner := this.resolve_type(this.left_of(node));
val mutable := this.node_has(node, nf::mutable);
# A reference to a trait dispatches through what the trait asks for,
# which a trait only a bound cannot do.
val plain := this.unalias(inner);
if this.named_kind(plain) == dk::trait_type && this.bound_only(this.types.first_of(plain)) {
var text := string::from("`", nothing {});
text.append(this.decl_text(this.types.first_of(plain)));
text.append("` names `self`, asks for types, or has generic methods, or composes a trait that does, which only what implements it gives, so it is only a bound, written in a `where` clause, and never the type of a value or reference.");
this.report_text(this.left_of(node), ec::not_type, &text);
return bt::error;
}
return this.kind_of(node) == nk::type_ref
? this.types.reference(inner, mutable)
: this.types.pointer(inner, mutable);
}
nk::type_fixed => {
val element := this.value_type(this.left_of(node));
var length: u32 = 0;
val count := this.const_length(this.right_of(node), &mut length);
if length != 0 {
return element == bt::error ? bt::error : this.types.fixed_of(element, length);
}
return element == bt::error || count == checker::no_length ? bt::error : this.types.fixed(element, count);
}
nk::type_func => {
# The last parameter written `T...` takes any number of arguments, or where
# `T` is a pack, one of each of its types.
var params := vector::<u32>::new();
var variadic := false;
val run := this.left_of(node);
val count := this.run_length(run);
for i in 0..count {
val each := this.run_at(run, i as u32);
if this.kind_of(each) == nk::type_spread {
if i as u32 + 1 != count {
this.report(this.left_of(each), ec::invalid, "Only the last parameter type is written `T...`, since it takes every argument after those before it.");
}
# A pack whose types are known, as an instance's `Ts`, gives a parameter
# of each of its types, as substituting it into the type would, and the
# function is variadic only where the pack is a variadic function's.
val spread := this.resolve_type(this.left_of(each));
if this.types.is_known_pack(spread) && i as u32 + 1 == count {
params.extend(this.types.parts_view(spread));
variadic = this.types.second_of(spread) != 0;
continue;
}
params.append(spread);
variadic = i as u32 + 1 == count;
continue;
}
val made := this.resolve_type(each);
if this.types.kind_of(made) == ty::pack {
this.report(each, ec::invalid, "A pack is the last parameter type, written `Ts...`.");
params.append(bt::error);
continue;
}
params.append(made);
}
return this.types.function(params.view(), this.resolve_type(this.right_of(node)), variadic);
}
nk::type_spread => {
this.report(node, ec::invalid, "`...` follows only the last parameter type of a function type, or a pack passed on among type arguments.");
return bt::error;
}
nk::type_of => {
# The type of what a description known while compiling describes.
val named := this.left_of(node);
this.infer_expr(named);
val one := this.reflected_one(named);
if one == checker::no_reflection {
if !this.waits_for_instance() {
this.unsupported(node);
}
return bt::error;
}
return this.reflected_type(one);
}
else => return bt::error;
}
return bt::error;
}
#! What `const_length` gives for a length that is not one.
pub const checker::no_length: u32 = 0xFFFFFFFF;
#! The type a value is written as, which is no bare trait, and not `void`.
#! @param this The checker.
#! @param node The type's node.
#! @returns The type, or the error type where it is wrong or no value can be of
#! it.
pub def checker::value_type(&mut this, node: u32): u32 {
val kind := this.resolve_type(node);
return this.valid_value(node, kind) ? kind : bt::error;
}
#! The type a function's result is written as, which may be `void`.
#! @param this The checker.
#! @param node The type's node, or 0 for `void`.
#! @returns The type, or the error type where it is wrongly written.
pub def checker::result_type(&mut this, node: u32): u32 {
val kind := this.resolve_type(node);
return kind if kind == bt::void_type;
return this.valid_value(node, kind) ? kind : bt::error;
}
#! Whether a value can be of the type, reporting a trait used as the type of a
#! value rather than through a reference or as a bound. `void` is a value's type
#! too, of the one value holding nothing, which a function giving nothing gives.
#! @param this The checker.
#! @param node The node a note is placed at.
#! @param kind The type.
#! @returns Whether it can be.
pub def checker::valid_value(&mut this, node: u32, kind: u32): bool {
if this.named_kind(this.unalias(kind)) == dk::trait_type {
this.report(node, ec::not_type,
"A trait is no value's type: refer to a value implementing it with `&`, or bound a type parameter by it.");
return false;
}
if this.types.kind_of(kind) == ty::pack {
this.report(node, ec::not_type, "A pack is the type only of a variadic parameter, as `args: Ts...`, and is passed on with `...`.");
return false;
}
return true;
}
#! Whether a trait's method writes bounds on the trait's type parameters or
#! `self`, which only a type implementing it meets.
#! @param this The checker.
#! @param method The method's declaration.
#! @returns Whether it does.
pub def checker::writes_outer(&this, method: u32): bool {
return false if this.decls.has(method, df::accessor);
val syntax := &this.units[this.decls.files.p[method]].syntax;
val node: u32 = this.decls.nodes.p[method];
val record := syntax.node_lefts[node];
return record != 0 && syntax.extras[record as u64 + function_slot::outer as u64] != 0;
}
#! Whether a trait is only a bound: it names `self`, asks for types, or has
#! generic methods or ones bounding its parameters, or composes a trait that
#! does, which only what implements it gives, and no table holds.
#! @param this The checker.
#! @param holder The trait's declaration.
#! @returns Whether it is only a bound.
pub def checker::bound_only(&mut this, holder: u32): bool {
if this.only_bounds.get(&holder) is some(got) {
return got;
}
this.only_bounds.set(holder, false);
var only := false;
var skipped := false;
val self_kind := this.types.param(this.self_decl(holder));
val run: u32 = this.decls.children.p[holder];
for i in 0..this.decls.list_length(run) {
val member := this.decls.list_at(run, i as u32);
if this.decls.kinds.p[member] == dk::assoc_type {
only = true;
break;
}
# A method being resolved, as one taking a reference to its own trait, is
# passed over, and what is found is not kept, to be worked out again.
if this.decls.kinds.p[member] == dk::function && this.decls.states.p[member] == ds::resolving {
skipped = true;
continue;
}
# A method's `this` refers to what implements the trait through the trait's
# reference, so only `self` elsewhere makes the trait only a bound.
if this.decls.kinds.p[member] == dk::function {
if (this.decls.list_length(this.decls.params.p[member]) != 0 || this.writes_outer(member)) && this.trait_asks(member) {
only = true;
break;
}
this.resolve(member);
val kind: u32 = this.decls.types.p[member];
val from: u32 = this.decls.has(member, df::method) ? 1 : 0;
for p in from..this.types.part_count(kind) {
only = only || this.mentions(this.types.part_at(kind, p as u32), self_kind, 0);
}
only = only || this.mentions(this.types.first_of(kind), self_kind, 0);
break if only;
}
}
val composed := this.composed_of(holder);
for i in 0..this.decls.list_length(composed) {
only = only || this.bound_only(this.types.first_of(this.decls.list_at(composed, i as u32)));
}
if skipped && !only {
this.only_bounds.remove(&holder);
} else {
this.only_bounds.set(holder, only);
}
return only;
}
#! Whether the type holds the type `target` anywhere within it.
#! @param this The checker.
#! @param kind The type to look within.
#! @param target The type to look for.
#! @param depth How deeply the search has gone, which stops a type nesting
#! without end.
#! @returns Whether it holds it.
pub def checker::mentions(&this, kind: u32, target: u32, depth: u32): bool {
return true if kind == target;
return false if depth > 16 || !this.types.is_open(kind);
match this.types.kind_of(kind) {
ty::reference, ty::pointer => return this.mentions(this.types.first_of(kind), target, depth + 1);
ty::fixed => {
return true if this.types.part_count(kind) != 0 && this.mentions(this.types.part_at(kind, 0), target, depth + 1);
return this.mentions(this.types.first_of(kind), target, depth + 1);
}
else => {}
}
for &part in this.types.parts_view(kind) {
return true if this.mentions(part, target, depth + 1);
}
return this.types.kind_of(kind) == ty::function && this.mentions(this.types.first_of(kind), target, depth + 1);
}
#! The length of a fixed array, an integer known while compiling, or
#! `no_length` where it is not one. Within a template, one worked out from its
#! constant parameters is known only to each instance, and is given as the
#! constant standing for it instead.
#! @param this The checker.
#! @param node The length's node.
#! @param dependent Where such a constant is written, or 0 where there is none.
#! @returns The length, or `no_length`.
def checker::const_length(&mut this, node: u32, dependent: &mut u32): u32 {
val kind := this.infer_expr(node);
return checker::no_length if kind == bt::error;
if !this.types.is_integer(this.unalias(this.read_type(kind))) && this.named_kind(this.unalias(kind)) != dk::enum_type {
this.report_operand(node, kind, "The length of a fixed array is an integer.");
return checker::no_length;
}
if !this.is_known(node) {
val worked := this.known_of(node, this.settle(kind), 0);
if worked != 0 && this.known_depends(worked) {
dependent = this.known_type(worked);
return checker::no_length;
}
}
if !this.is_known(node) {
var made := ev {};
if this.evaluate(node, node, &mut made) && !made.array {
this.set_value(node, made.scalar);
} else if !this.ev_failed {
this.report(node, ec::constant, "The length of a fixed array is known while compiling: a literal, a constant, or what a function gives of them.");
return checker::no_length;
} else {
return checker::no_length;
}
}
val value := this.value_at(node);
if value.negative || value.magnitude > 0x1000000 {
this.report(node, ec::limit, "A fixed array holds from 0 to 16777216 elements.");
return checker::no_length;
}
return value.magnitude as u32;
}
#! The type a named type is written as, as `std::list<T>` or `T`.
#! @param this The checker.
#! @param node The `type_name` node.
#! @returns The type, or the error type where it names none.
pub def checker::resolve_type_name(&mut this, node: u32): u32 {
var decl: u32 = 0;
var home: u32 = 0;
var in_module := false;
var args := vector::<u32>::new();
val count := this.segment_count(node);
for i in 0..count {
val name := this.segment_name(node, i as u32);
val segment := this.segment_node(node, i as u32);
if i == 0 {
val bound := this.bound(name);
if (bound & ref_type) != 0 {
val kind := bound & ref_mask;
if this.is_constant_arg(kind) {
var text := string::from("`", nothing {});
text.append(this.names.text(name));
text.append("` is a constant, not a type.");
this.report_text(segment, ec::not_type, &text);
return bt::error;
}
this.set_ref(segment, bound);
if count == 1 {
this.no_type_args(node, 0);
return kind;
}
return this.projection(kind, node, 1, this.param_named(name));
}
# A module the first of several segments names comes before a declaration of
# the name, as `serial::options` names the module's type where `serial` is
# also the attribute it declares.
if count > 1 {
home = this.lookup_module(name);
in_module = home != 0;
}
# A type a type around it gives takes that type's arguments first, as
# `node` within `list<T>` is `list<T>::node`.
if !in_module {
val found := this.lookup_named(name, segment, true);
decl = found.decl;
if found.owner != 0 && this.is_type_decl(decl) && this.types.kind_of(found.owner) == ty::named {
args.extend(this.types.parts_view(found.owner));
}
}
if decl == 0 && !in_module {
home = this.lookup_module(name);
in_module = home != 0;
if !in_module {
var text := string::from("No type `", nothing {});
text.append(this.names.text(name));
text.append("` is declared where it is written.");
this.report_text(segment, ec::undefined, &text);
return bt::error;
}
}
} else if in_module {
decl = this.modules.decl(home, name);
if decl == 0 {
home = this.modules.find(home, name);
if home == 0 {
var text := string::from("The module declares no `", nothing {});
text.append(this.names.text(name));
text.append("`.");
this.report_text(segment, ec::undefined, &text);
return bt::error;
}
continue;
}
in_module = false;
} else {
# A type the program declares through `core`'s, of a name `core` gives it
# too, is the program's outside `core`.
val added := this.in_core(this.at.home) || this.cores_own != 0 ? 0 : this.decls.added_member(decl, name);
val member := added != 0 && this.is_type_decl(added) ? added : this.decls.path_member(decl, name);
if member == 0 || !this.is_type_decl(member) {
var text := string::from("`", nothing {});
text.append(this.decl_text(decl));
text.append("` declares no type `");
text.append(this.names.text(name));
text.append("`.");
this.report_text(segment, ec::undefined, &text);
return bt::error;
}
decl = member;
}
# What each segment names is kept for the editor, which shows it and finds
# where it is declared.
this.set_ref(segment, decl);
val run := this.segment_args(node, i as u32);
for &arg in this.run_view(run) {
args.append(this.type_arg_of(arg, decl, args.length as u32, args.view()));
}
}
if in_module {
this.report(node, ec::not_type, "A module is not a type.");
return bt::error;
}
if !this.is_type_decl(decl) {
var text := string::from("`", nothing {});
text.append(this.decl_text(decl));
text.append("` is a ");
text.append(this.decl_word(decl));
text.append(", not a type.");
this.report_text(node, ec::not_type, &text);
return bt::error;
}
if !this.visible(decl) {
this.report_hidden(node, decl);
}
return this.named_of(decl, args.view(), node);
}
#! The type of the declaration given the type arguments, reporting arguments
#! that are wrong in number or do not satisfy their bounds.
#! @param this The checker.
#! @param decl The declaration of the type.
#! @param args Its type arguments.
#! @param node The node notes are placed at.
#! @returns The type, or the error type where the arguments are wrong.
pub def checker::named_of(&mut this, decl: u32, args: span<u32>, node: u32, given: bool = false): u32 {
match this.decls.kinds.p[decl] {
dk::builtin, dk::type_param => {
if args.length != 0 {
this.report(node, ec::generic, "The type takes no type arguments.");
}
return this.decls.types.p[decl];
}
dk::assoc_type => return this.decls.types.p[decl];
else => {}
}
val params: u32 = this.decls.params.p[decl];
val wanted := this.decls.list_length(params);
for &arg in args {
return bt::error if arg == bt::error;
}
# What is written gives each type parameter its argument: one each, the rest to
# a pack, and a pattern's the parts of its function type. What inference gives
# already has one for each.
var grouped := vector::<u32>::new();
if given {
grouped.extend(args);
if grouped.length != wanted as u64 {
var text := string::from("`", nothing {});
text.append(this.decl_text(decl));
val words := $"` takes {wanted} type arguments, but is given {args.length}.";
text.append(words.view());
this.report_text(node, ec::generic, &text);
return bt::error;
}
} else {
return bt::error if !this.bind_written(decl, args, node, &mut grouped);
}
# A pack is given only for a pack parameter, as the pack it is.
for i in 0..wanted {
val is_pack := this.types.kind_of(grouped[i]) == ty::pack;
if is_pack != this.decls.has(this.decls.list_at(params, i as u32), df::packed) {
this.report(node, ec::generic, is_pack
? "A pack is passed on with `...` only for a pack parameter, as `Ts` of `box<Ts...>`."
: "A pack parameter is given types, or a pack passed on as `Ts...`.");
return bt::error;
}
}
val made := this.types.named(decl, grouped.view());
if grouped.length != 0 {
this.check_bounds(decl, made, node);
}
return made;
}
#! The pack a pack parameter is given by the type arguments from `from` on: the
#! types, with each pack passed on as `Ts...` giving its types in their place. A
#! pack whose types are not known is only passed on alone, since where the types
#! after it go is not known until it is.
#! @param this The checker.
#! @param args The type arguments.
#! @param from The first the pack takes.
#! @param node The node notes are placed at.
#! @returns The pack, or the error type.
def checker::gather_pack(&mut this, args: span<u32>, from: u64, node: u32): u32 {
var items := vector::<u32>::new();
for i in from..args.length {
val each := args[i];
if this.types.kind_of(each) != ty::pack {
items.append(each);
continue;
}
if this.types.is_open_pack(each) {
if args.length - from != 1 {
if node != 0 {
this.report(node, ec::generic, "A pack whose types are not known yet is passed on alone, as the whole of what a pack parameter is given.");
}
return bt::error;
}
return each;
}
items.extend(this.types.parts_view(each));
}
return this.types.pack(items.view(), 0);
}
#! Reports type arguments given to what takes none.
#! @param this The checker.
#! @param node The `type_name` node.
#! @param index The segment that names what takes none.
def checker::no_type_args(&mut this, node: u32, index: u32): void {
if this.segment_args(node, index) != 0 {
this.report(node, ec::generic, "A type parameter takes no type arguments.");
}
}
#! The type the segments of a type name from `from` name within the type: a
#! type a bound asks of a type parameter, as `C::cursor` where `C: iterable<T>`,
#! or a nested type, as `cursor` of `vector<T>::cursor`. In an instance, the
#! type parameter is the type it was given, which gives the type its bound asks
#! for, or else the bound's default, as `storage` of `type storage = self`.
#! @param this The checker.
#! @param kind The type the name is within.
#! @param node The `type_name` node.
#! @param from The first segment to resolve within the type.
#! @param param The type parameter the first segment names, or 0.
#! @returns The type named, or the error type where the type has none of a name.
def checker::projection(&mut this, kind: u32, node: u32, from: u32, param: u32): u32 {
var at := kind;
for i in from..this.segment_count(node) {
val name := this.segment_name(node, i as u32);
val segment := this.segment_node(node, i as u32);
var made := this.member_type(at, name);
if made == bt::error && i == from as u64 && param != 0 && this.types.kind_of(this.unalias(at)) != ty::param {
made = this.asked_of_bounds(at, param, name);
}
if made == bt::error {
var text := string::from("`", nothing {});
val named := this.describe(at);
text.append(named.view());
text.append("` has no type `");
text.append(this.names.text(name));
text.append("`.");
this.report_text(segment, ec::undefined, &text);
return bt::error;
}
at = made;
}
return at;
}
#! The type parameter of the name that the declaration being checked, or one
#! it is declared within, declares.
#! @param this The checker.
#! @param name The ID of the name.
#! @returns The type parameter's declaration, or 0.
def checker::param_named(&this, name: u32): u32 {
var at := this.at.decl;
var steps := 0;
while at != 0 && steps < 4 {
val run: u32 = this.decls.params.p[at];
for &param in this.decls.list_view(run) {
return param if this.decls.names.p[param] == name;
}
at = this.decls.owners.p[at];
steps++;
}
return 0;
}
#! The type a type given for a type parameter gives for one the parameter's
#! bounds ask for: its own of the name, or the bound's default.
#! @param this The checker.
#! @param kind The type given.
#! @param param The type parameter's declaration.
#! @param name The ID of the name.
#! @returns The type, or the error type where no bound asks for one of the name.
def checker::asked_of_bounds(&mut this, kind: u32, param: u32, name: u32): u32 {
this.resolve(param);
val run: u32 = this.decls.children.p[param];
for i in 0..this.decls.list_length(run) {
val bound := this.decls.list_at(run, i as u32);
continue if this.types.kind_of(bound) != ty::named;
val member := this.trait_member(this.types.first_of(bound), name);
continue if member == 0 || this.decls.kinds.p[member] != dk::assoc_type;
return this.asked_type(kind, member, this.declaring_bound(bound, member));
}
return bt::error;
}
#! The type of the name a type has: what a bound asks of a type parameter, or
#! a type nested in it, given its arguments, or the error type.
#! @param this The checker.
#! @param kind The type.
#! @param name The ID of the name.
#! @returns The type, or the error type where it has none of the name.
pub def checker::member_type(&mut this, kind: u32, name: u32): u32 {
val plain := this.unalias(kind);
if this.types.kind_of(plain) == ty::param {
val found := this.find_in_bounds(plain, name);
return bt::error if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::assoc_type;
return this.projected(plain, found.decl, found.owner);
}
val found := this.find_path_member(kind, name);
return bt::error if found.decl == 0 || !this.is_type_decl(found.decl);
this.resolve(found.decl);
return this.instantiated(this.decls.types.p[found.decl], found.decl, found.owner);
}
#! The type parameter standing for the type a bound asks of a type parameter,
#! as `C::cursor`, whose bounds are those the trait gives the type it asks for.
#! @param this The checker.
#! @param kind The type parameter's type.
#! @param asked The declaration of the type the bound asks for.
#! @param bound The bound asking for it.
#! @returns The type parameter's type.
pub def checker::projected(&mut this, kind: u32, asked: u32, bound: u32): u32 {
val key := pair_key(kind, asked);
if this.projections.get(&key) is some(got) {
val made: u32 = got;
return this.decls.types.p[made];
}
# A pack asked for, as `F::params`, is stood in for by a pack whose types are
# not known.
val id := this.decls.add(dk::type_param, this.decls.names.p[asked], this.decls.files.p[asked],
this.decls.nodes.p[asked], this.decls.modules.p[asked]);
this.decls.own(id, this.decls.owners.p[asked]);
if this.decls.has(asked, df::packed) {
this.decls.mark(id, df::packed);
this.decls.types.p[id] = this.types.pack(span::<u32>::empty(), id);
} else {
this.decls.types.p[id] = this.types.param(id);
}
this.decls.states.p[id] = ds::resolved;
this.projections.set(key, id);
this.stand_ins.append(checker::stand_in { base = kind, asked = asked, param = id, bound = bound });
# Its bounds are the trait's for the type it asks for, with the trait's
# parameters given the bound's arguments and `self` the type parameter.
this.resolve(asked);
val holder: u32 = this.decls.owners.p[asked];
var from := vector::<u32>::new();
var to := vector::<u32>::new();
from.append(this.self_decl(holder));
to.append(kind);
val params: u32 = this.decls.params.p[holder];
for i in 0..this.decls.list_length(params) {
from.append(this.decls.list_at(params, i as u32));
to.append(i < this.types.part_count(bound) ? this.types.part_at(bound, i as u32) : bt::error);
}
var bounds := vector::<u32>::new();
val run: u32 = this.decls.children.p[asked];
for i in 0..this.decls.list_length(run) {
bounds.append(this.substitute(this.decls.list_at(run, i as u32), from.view(), to.view()));
}
this.decls.children.p[id] = this.decls.make_list(bounds.view());
return this.decls.types.p[id];
}
#! Reports a declaration named from where it is not visible.
#! @param this The checker.
#! @param node The node naming it.
#! @param decl The declaration.
pub def checker::report_hidden(&mut this, node: u32, decl: u32): void {
var text := string::from("`", nothing {});
text.append(this.decl_text(decl));
text.append("` is not `pub`, so only its own module and those within it see it.");
this.report_text(node, ec::hidden, &text);
}