Files
sakurac/analyzer/checker/traits.sakura
T
2026-10-28 22:13:49 -05:00

896 lines
38 KiB
Plaintext

#! @file traits.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains traits: what a trait asks for, and whether a type satisfies it. A
#! type satisfies a trait by its shape, having each method the trait asks for
#! with the signature it asks, where `self` is the type, whether or not the
#! type names the trait. A type parameter satisfies what its bounds do. What
#! was worked out of a type and a trait is kept, so it is worked out once.
module sakura::analyzer;
#! Resolves the traits a declaration names after its name: those a struct or
#! union implements, or a trait composes.
#! @param this The checker.
#! @param id The declaration.
pub def checker::resolve_composed(&mut this, id: u32): void {
return if this.composed.at_or_zero(id) != 0;
this.composed.put(id, 1);
val kind: dk = this.decls.kinds.p[id];
return if kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type;
val saved := this.at;
this.enter_decl(id, 0);
var traits := vector::<u32>::new();
val run := this.record(this.decls.nodes.p[id], aggregate_slot::traits);
for &written in this.run_view(run) {
val made := this.resolve_type(written);
if made != bt::error && this.named_kind(made) != dk::trait_type {
this.report(written, ec::not_type, kind == dk::trait_type
? "A trait composes traits."
: "A struct or union names the traits it implements.");
continue;
}
if made != bt::error {
traits.append(made);
}
}
this.leave(saved);
this.composed.put(id, this.decls.make_list(traits.view()) + 1);
}
#! The traits a declaration names after its name, resolved.
#! @param this The checker.
#! @param id The declaration.
#! @returns The run of the traits' types, in `lists`.
pub def checker::composed_of(&mut this, id: u32): u32 {
this.resolve_composed(id);
return this.composed.at_or_zero(id) - 1;
}
#! Whether a value of the type satisfies the trait, given its arguments.
#! @param this The checker.
#! @param kind The type.
#! @param trait_kind The trait's type, given its arguments.
#! @returns Whether it does.
pub def checker::satisfies(&mut this, kind: u32, trait_kind: u32): bool {
return true if kind == bt::error || trait_kind == bt::error;
# What a type parameter satisfies changes as `if const` proves bounds of it,
# so only what holds no parameter is kept.
if this.types.is_open(kind) {
return this.works_out(kind, trait_kind);
}
val key := pair_key(kind, trait_kind);
if this.satisfied.get(&key) is some(got) {
return got != 0;
}
# A type asked of again while it is being worked out satisfies the trait so
# far, which lets a trait ask for methods taking the trait itself.
this.satisfied.set(key, 1);
val holds := this.works_out(kind, trait_kind);
this.satisfied.set(key, holds ? 1 : 0);
return holds;
}
def checker::works_out(&mut this, kind: u32, trait_kind: u32): bool {
val holder := this.types.first_of(trait_kind);
val name: u32 = this.decls.names.p[holder];
val is_std := this.std_decl(name) == holder;
val plain := this.unalias(kind);
# The bounds the compiler decides.
if is_std && name == word::integer {
return this.types.is_integer(plain) && !this.types.is_untyped(plain) || this.param_bounded(plain, holder);
}
if is_std && name == word::real {
return this.types.is_float(plain) && !this.types.is_untyped(plain) || this.param_bounded(plain, holder);
}
# Every integer is ordinal, so a type parameter bounded by `integer` is too.
if is_std && name == word::ordinal {
val integer := this.std_decl(word::integer);
return (this.types.is_integer(plain) && !this.types.is_untyped(plain)) || this.named_kind(plain) == dk::enum_type
|| this.param_bounded(plain, holder) || (integer != 0 && this.param_bounded(plain, integer));
}
if is_std && name == word::pointer {
return this.types.kind_of(plain) == ty::pointer || this.param_bounded(plain, holder);
}
# A function type, a variadic one too, and the address of a C function are
# signatures, and nothing else is.
if is_std && name == word::signature {
return this.signature_function(plain) != 0 || this.param_bounded(plain, holder);
}
# A type is `closeable` where its values close, and a type parameter where its
# bounds say so, or say it is `owning`.
if is_std && name == word::closeable {
if this.types.kind_of(plain) == ty::param || this.types.kind_of(plain) == ty::pack {
val owning := this.std_decl(word::owning);
return this.param_bounded(plain, holder) || (owning != 0 && this.param_bounded(plain, owning));
}
return this.closes(plain);
}
# A type is `zeroable` where it has a zero value, and a type parameter where
# its bounds say so.
if is_std && name == word::zeroable {
return this.has_zero(plain, 0);
}
# A type is `lockfree` where it is a word or less that every processor reads and
# writes whole without a lock: an integer, `bool`, enum, or pointer.
if is_std && name == word::lockfree {
val of := this.types.kind_of(plain);
return (this.types.is_integer(plain) && !this.types.is_untyped(plain)) || plain == bt::boolean
|| this.named_kind(plain) == dk::enum_type || of == ty::pointer || this.param_bounded(plain, holder);
}
# A type parameter satisfies what one of its bounds is, or composes.
if this.types.kind_of(plain) == ty::param || this.types.kind_of(plain) == ty::pack {
return this.param_bounded(plain, holder);
}
# A reference satisfies what its referent does, where the trait asks only for what
# can be called through it.
if this.types.kind_of(plain) == ty::reference && this.lends(plain, trait_kind, holder) {
return true;
}
# A trait reference satisfies the trait it is, and those it composes.
if this.named_kind(plain) == dk::trait_type && this.composes(plain, holder, 0) {
return true;
}
# A type is `owning` only by naming it, so a `close` meant to be called by hand
# never makes its values closed by accident.
if is_std && name == word::owning && !this.names_trait(plain, holder) {
return false;
}
# Only a struct naming it implements `attribute`.
if is_std && name == word::attribute {
return this.names_trait(plain, holder);
}
return this.has_requirements(kind, trait_kind, holder, 0);
}
#! Whether a reference satisfies a trait through what it refers to, as `&mut
#! file` satisfies what `file` does, so a generic can be given either. Its
#! methods are its referent's, called through it, so the trait asks only for
#! methods taking `this`, and only for ones taking `&this` where it only reads.
#! It is compared, ordered, and hashed as what it refers to, so it takes a trait
#! naming `self` where `self` is only taken by reference, but none giving or
#! taking a `self` by value, since what is made from the referent is no
#! reference. It owns nothing, so it is never `owning`. A pointer is an address,
#! and takes nothing from what it points at.
#! @param this The checker.
#! @param kind The reference's type.
#! @param trait_kind The trait's type, given its arguments.
#! @param holder The trait's declaration.
#! @returns Whether it does.
def checker::lends(&mut this, kind: u32, trait_kind: u32, holder: u32): bool {
return false if this.about_values(holder);
return false if this.unreached(holder, this.types.second_of(kind) == 1, 0) != 0;
return this.satisfies(this.types.first_of(kind), trait_kind);
}
# Whether a trait of std is one the compiler gives a meaning, which says what a value
# itself is.
def checker::about_values(&mut this, holder: u32): bool {
val name: u32 = this.decls.names.p[holder];
return this.std_decl(name) == holder
&& (name == word::owning || name == word::attribute
|| name == word::integer || name == word::ordinal || name == word::real || name == word::closeable
|| name == word::zeroable || name == word::pointer
|| name == word::lockfree);
}
# The first member a trait, or a trait it composes, asks for that a reference cannot
# reach through it: one not taking `this`, taking `&mut this` where it only reads, or
# not reading through to `self`. A type it asks for is the referent's, an `@optional`
# method taking `&mut this` is only not there to call through a reference that reads,
# and a default compiled for each type is the reference's own, as it is any type's.
# Gives 0 where there is none.
def checker::unreached(&mut this, holder: u32, writes: bool, depth: u32): u32 {
return holder if depth > 16;
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);
this.resolve(member);
val kind: dk = this.decls.kinds.p[member];
if (kind == dk::property && writes) || kind == dk::assoc_type {
continue;
}
if kind == dk::function && !writes && this.decls.has(member, df::mut_this) && this.decls.has(member, df::optional) {
continue;
}
if kind == dk::function && this.typed_default(member) {
continue;
}
return member if kind != dk::function || !this.decls.has(member, df::method);
return member if !writes && this.decls.has(member, df::mut_this);
return member if !this.reads_through(member, self_kind);
}
val composed := this.composed_of(holder);
for i in 0..this.decls.list_length(composed) {
val inner := this.unreached(this.types.first_of(this.decls.list_at(composed, i as u32)), writes, depth + 1);
return inner if inner != 0;
}
return 0;
}
# Whether a method of a trait is called through a reference to what implements it as
# it is on what that refers to: `self` is taken only by reference, which a reference
# to the reference reads through to, and is never given, since what is made from the
# referent is no reference.
def checker::reads_through(&mut this, method: u32, self_kind: u32): bool {
val kind: u32 = this.decls.types.p[method];
return false if this.mentions(this.types.first_of(kind), self_kind, 0);
for p in 1..this.types.part_count(kind) {
val taken := this.types.part_at(kind, p as u32);
continue if !this.mentions(taken, self_kind, 0);
return false if this.types.kind_of(taken) != ty::reference || this.types.first_of(taken) != self_kind;
}
return true;
}
#! Why a reference or a pointer does not satisfy a trait that what it refers to
#! does, or empty text where its referent does not either.
#! @param this The checker.
#! @param kind The reference's or pointer's type.
#! @param trait_kind The trait's type.
#! @returns Why, as a note's detail, or empty text.
def checker::unlent(&mut this, kind: u32, trait_kind: u32): string {
val plain := this.unalias(kind);
val held := this.types.kind_of(plain);
val holder := this.types.first_of(trait_kind);
var why := string::new(nothing {});
return move(why) if held != ty::reference && held != ty::pointer;
return move(why) if !this.satisfies(this.types.first_of(plain), trait_kind);
val name: u32 = this.decls.names.p[holder];
if held == ty::pointer {
why.append("a pointer is an address, so what it points at is reached through `*p`");
return move(why);
}
if this.std_decl(name) == holder && name == word::owning {
why.append("it owns nothing it refers to, though `close` can be called through it");
return move(why);
}
if this.about_values(holder) {
why.append("the trait says what a value itself is, not what it refers to");
return move(why);
}
val writes := this.types.second_of(plain) == 1;
val member := this.unreached(holder, writes, 0);
return move(why) if member == 0;
val self_kind := this.types.param(this.self_decl(this.decls.owners.p[member]));
why.append("`");
why.append(this.decl_text(member));
if this.decls.kinds.p[member] != dk::function || !this.decls.has(member, df::method) {
why.append("` takes no `this`, so it is not reached through a reference");
} else if !writes && this.decls.has(member, df::mut_this) {
why.append("` takes `&mut this`, and the reference only reads");
} else if this.mentions(this.types.first_of(this.decls.types.p[member]), self_kind, 0) {
why.append("` gives `self`, and what it makes is not a reference");
} else {
why.append("` takes `self` by value, and a reference reaches only what takes it by reference");
}
return move(why);
}
#! Whether a type parameter's bounds include the trait, or a trait composing it.
#! @param this The checker.
#! @param kind The type parameter's type.
#! @param holder The trait's declaration.
#! @returns Whether they do.
def checker::param_bounded(&mut this, kind: u32, holder: u32): bool {
return false if this.types.kind_of(kind) != ty::param && this.types.kind_of(kind) != ty::pack;
val param := this.types.first_of(kind);
return false if param == 0;
if this.decls.names.p[param] == names::self_id
&& (this.decls.owners.p[param] == holder || this.composes(this.decls.types.p[this.decls.owners.p[param]], holder, 0)) {
return true;
}
return this.bounded_by(param, holder, 0, 0);
}
#! Whether a type parameter is bounded by any of up to three traits, through a
#! bound it declares, or one an `if const` proved of it in the branch it guards.
#! @param this The checker.
#! @param param The parameter's declaration.
#! @param first A trait's declaration, or 0.
#! @param second Another, or 0.
#! @param third Another, or 0.
#! @returns Whether it is.
def checker::bounded_by(&mut this, param: u32, first: u32, second: u32, third: u32): bool {
return false if first == 0 && second == 0 && third == 0;
this.resolve(param);
val run: u32 = this.decls.children.p[param];
for i in 0..this.decls.list_length(run) {
return true if this.composes_any(this.decls.list_at(run, i as u32), first, second, third);
}
for i in 0..this.proved_params.length {
return true if this.proved_params[i] == param && this.composes_any(this.proved_bounds[i], first, second, third);
}
return false;
}
#! Whether a bound composes any of up to three traits.
#! @param this The checker.
#! @param bound The bound.
#! @param first A trait's declaration, or 0.
#! @param second Another, or 0.
#! @param third Another, or 0.
#! @returns Whether it does.
def checker::composes_any(&mut this, bound: u32, first: u32, second: u32, third: u32): bool {
return (first != 0 && this.composes(bound, first, 0)) || (second != 0 && this.composes(bound, second, 0))
|| (third != 0 && this.composes(bound, third, 0));
}
#! Whether the trait type is the trait, or composes it.
#! @param this The checker.
#! @param trait_kind The trait type asked about.
#! @param holder The declaration of the trait looked for.
#! @param depth How deeply composition has been followed, which stops one
#! composing itself without end.
#! @returns Whether it is, or does.
pub def checker::composes(&mut this, trait_kind: u32, holder: u32, depth: u32): bool {
return false if depth > 16 || this.types.kind_of(trait_kind) != ty::named;
val decl := this.types.first_of(trait_kind);
return true if decl == holder;
val run := this.composed_of(decl);
for i in 0..this.decls.list_length(run) {
return true if this.composes(this.decls.list_at(run, i as u32), holder, depth + 1);
}
return false;
}
#! Whether a struct or union names the trait after its name.
#! @param this The checker.
#! @param kind The struct's or union's type.
#! @param holder The trait's declaration.
#! @returns Whether it does.
def checker::names_trait(&mut this, kind: u32, holder: u32): bool {
return false if this.types.kind_of(kind) != ty::named;
val run := this.composed_of(this.types.first_of(kind));
for i in 0..this.decls.list_length(run) {
return true if this.composes(this.decls.list_at(run, i as u32), holder, 0);
}
return false;
}
#! Whether the type has what the trait asks for, and what each trait it
#! composes asks for.
#! @param this The checker.
#! @param kind The type.
#! @param trait_kind The trait's type, given its arguments.
#! @param holder The trait's declaration.
#! @param depth How deeply composition has been followed.
#! @returns Whether it has.
def checker::has_requirements(&mut this, kind: u32, trait_kind: u32, holder: u32, depth: u32): bool {
return false if depth > 16;
val run: u32 = this.decls.children.p[holder];
for i in 0..this.decls.list_length(run) {
val wanted := this.decls.list_at(run, i as u32);
continue if this.decls.has(wanted, df::optional);
# A method the trait writes a body for is its default, which the type may leave out.
continue if this.decls.kinds.p[wanted] == dk::function && this.has_body(wanted);
return false if !this.meets(kind, trait_kind, wanted);
}
val composed := this.composed_of(holder);
for i in 0..this.decls.list_length(composed) {
val inner := this.decls.list_at(composed, i as u32);
val given := this.trait_in_terms(inner, holder, trait_kind, kind);
return false if !this.satisfies(kind, given);
}
return true;
}
#! A type a trait names, given the trait's arguments, `self`, and each type it
#! asks for as what implements it gives.
#! @param this The checker.
#! @param inner The type the trait names.
#! @param holder The trait's declaration.
#! @param trait_kind The trait's type, given its arguments.
#! @param self_kind The type standing for `self`.
#! @returns The type in those terms.
def checker::trait_in_terms(&mut this, inner: u32, holder: u32, trait_kind: u32, self_kind: u32): u32 {
return inner if !this.types.is_open(inner);
var from := vector::<u32>::new();
var to := vector::<u32>::new();
from.append(this.self_decl(holder));
to.append(self_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(trait_kind) ? this.types.part_at(trait_kind, i as u32) : bt::error);
}
val asked: u32 = this.decls.children.p[holder];
for i in 0..this.decls.list_length(asked) {
val member := this.decls.list_at(asked, i as u32);
continue if this.decls.kinds.p[member] != dk::assoc_type;
from.append(member);
to.append(this.asked_type(self_kind, member, trait_kind));
}
return this.substitute(inner, from.view(), to.view());
}
#! The type a type gives for one a trait asks for: its own of the name, or the
#! trait's default, or for a type parameter, the parameter standing for it. A
#! trait of `core`'s is given what `core` declares through its own types.
#! @param this The checker.
#! @param self_kind The type implementing the trait.
#! @param asked The declaration of the type the trait asks for.
#! @param trait_kind The trait's type, given its arguments.
#! @returns The type, or the error type where it gives none.
def checker::asked_type(&mut this, self_kind: u32, asked: u32, trait_kind: u32): u32 {
val cores := this.in_core(this.decls.modules.p[this.decls.owners.p[asked]]);
if cores { this.cores_own++; }
val made := this.asked_one(self_kind, asked, trait_kind);
if cores { this.cores_own--; }
return made;
}
#! The type a type gives for one a trait asks for, as `asked_type` says.
#! @param this The checker.
#! @param self_kind The type implementing the trait.
#! @param asked The declaration of the type the trait asks for.
#! @param trait_kind The trait's type, given its arguments.
#! @returns The type, or the error type where it gives none.
def checker::asked_one(&mut this, self_kind: u32, asked: u32, trait_kind: u32): u32 {
val plain := this.unalias(self_kind);
if this.types.kind_of(plain) == ty::param || self_kind == bt::error {
return self_kind == bt::error ? bt::error : this.projected(plain, asked, trait_kind);
}
val part := this.signature_part(plain, asked);
return part if part != 0;
# A reference gives the types what it refers to gives.
val found := this.find_path_member(this.reached(self_kind), this.decls.names.p[asked]);
if found.decl != 0 && this.is_type_decl(found.decl) && found.decl != asked {
this.resolve(found.decl);
return this.instantiated(this.decls.types.p[found.decl], found.decl, found.owner);
}
this.resolve(asked);
val fallback: u32 = this.decls.targets.p[asked];
return bt::error if fallback == 0;
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(self_kind);
return this.substitute(fallback, from.view(), to.view());
}
#! Whether the type meets one thing a trait asks for: a method of its name and
#! signature, a property, or a type. A trait of `core`'s is met by what `core`
#! gives its own types, whatever a program declares through them beside it.
#! @param this The checker.
#! @param kind The type.
#! @param trait_kind The trait's type, given its arguments.
#! @param wanted The declaration of what the trait asks for.
#! @returns Whether it does.
def checker::meets(&mut this, kind: u32, trait_kind: u32, wanted: u32): bool {
val cores := this.in_core(this.decls.modules.p[this.decls.owners.p[wanted]]);
if cores { this.cores_own++; }
val made := this.meets_one(kind, trait_kind, wanted);
if cores { this.cores_own--; }
return made;
}
#! Whether the type meets one thing a trait asks for, as `meets` says.
#! @param this The checker.
#! @param kind The type.
#! @param trait_kind The trait's type, given its arguments.
#! @param wanted The declaration of what the trait asks for.
#! @returns Whether it does.
def checker::meets_one(&mut this, kind: u32, trait_kind: u32, wanted: u32): bool {
val name: u32 = this.decls.names.p[wanted];
match this.decls.kinds.p[wanted] {
dk::function => {
this.resolve(wanted);
val signature := this.requirement_type(wanted, trait_kind, kind);
val found := this.find_member(kind, name);
if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function {
val given := this.builtin_method(kind, name);
return given != 0 && this.same_signature(this.as_writing(given, wanted), signature);
}
# A number's methods from the compiler are asked first, so a number meets a
# trait without the program's methods beside them being worked out. One of
# those may be what asked, as `def f32::mul(&this, v: &vec3<f32>)` asks
# whether `f32` is `numeric` for `vec3<f32>`.
if this.types.is_numeric(this.unalias(kind)) {
val builtin := this.builtin_method(kind, name);
return true if builtin != 0 && this.same_signature(this.as_writing(builtin, wanted), signature);
}
# A default of a trait the type names is its own, taking it as `self`. A
# method taking `&this` meets one asked for taking `&mut this`, since what
# can be written can be read. `core`'s methods behind a program's on its
# type meet what they did.
var chain := found.decl;
while chain != 0 {
var walk := this.decls.walk_overloads(chain);
while true {
val at := walk.next();
break if at == 0;
continue if !this.member_seen(at);
this.resolve(at);
val declarer := this.enclosing_trait(at);
var given := declarer != 0
? this.requirement_type(at, this.implemented(kind, declarer), kind)
: this.method_type(at, found.owner);
if this.decls.has(at, df::method) && !this.decls.has(at, df::mut_this) {
given = this.as_writing(given, wanted);
}
if this.decls.has(at, df::method) == this.decls.has(wanted, df::method)
&& this.same_signature(given, signature) && this.bounds_met(at, found.owner) {
return true;
}
}
chain = this.core_behind(chain);
}
# A method a program declares on a built-in type is beside those the
# compiler gives it, which still meet what they did.
val given := this.builtin_method(kind, name);
return given != 0 && this.same_signature(this.as_writing(given, wanted), signature);
}
dk::property => {
val found := this.find_member(kind, name);
return found.decl != 0 && this.decls.kinds.p[found.decl] == dk::property;
}
dk::assoc_type => {
val found := this.find_member(kind, name);
return (found.decl != 0 && this.is_type_decl(found.decl)) || this.decls.targets.p[wanted] != 0;
}
dk::variable => {
val found := this.find_member(kind, name);
return found.decl != 0 && this.decls.kinds.p[found.decl] == dk::variable;
}
else => return true;
}
return true;
}
#! Whether a type has a method whose `where` bounds the type's parameters
#! further: whether its arguments meet those bounds. Any other method it has
#! always.
#! @param this The checker.
#! @param method The method's declaration.
#! @param owner The type it is a member of, given its arguments.
#! @returns Whether they do.
pub def checker::bounds_met(&mut this, method: u32, owner: u32): bool {
return true if !this.decls.has(method, df::bounding);
val plain := this.unalias(owner);
return true if this.types.kind_of(plain) != ty::named || this.types.is_open(plain);
var params := this.decls.list_of(this.decls.params.p[method]);
var args := this.types.parts_of(plain);
if params.length > args.length {
params.resize(args.length);
}
return true if params.length == 0;
val dropped := this.dropped;
val uninferred := this.uninferred;
this.mute++;
val met := this.check_bounds_of(method, params.view(), args.view(), 0);
this.mute--;
this.dropped = dropped;
this.uninferred = uninferred;
return met;
}
#! A method's type as one asked for taking `&mut this` sees it, where the method
#! takes `&this`: taking `&mut this` too, since what can be written can be read.
#! @param this The checker.
#! @param given The method's type, `this` first.
#! @param wanted The declaration of the method asked for.
#! @returns The type, as it is where the method asked for takes `&this`.
def checker::as_writing(&mut this, given: u32, wanted: u32): u32 {
return given if !this.decls.has(wanted, df::mut_this) || this.types.kind_of(given) != ty::function
|| this.types.part_count(given) == 0 || this.types.kind_of(this.types.part_at(given, 0)) != ty::reference;
var parts := this.types.parts_of(given);
parts[0] = this.types.reference(this.types.first_of(parts[0]), true);
return this.types.function(parts.view(), this.types.first_of(given), this.types.second_of(given) != 0);
}
#! Whether two function types are the same but for aliases.
#! @param this The checker.
#! @param a One function type.
#! @param b The other.
#! @returns Whether they are.
def checker::same_signature(&mut this, a: u32, b: u32): bool {
return a == b || (this.types.kind_of(a) == ty::function && this.types.kind_of(b) == ty::function
&& this.types.second_of(a) == this.types.second_of(b) && this.same_function(a, b));
}
#! The type of a method a trait asks for, given the trait's arguments and the
#! type standing for `self`.
#! @param this The checker.
#! @param method The method's declaration.
#! @param trait_kind The trait's type, given its arguments.
#! @param self_kind The type standing for `self`.
#! @returns The method's type.
pub def checker::requirement_type(&mut this, method: u32, trait_kind: u32, self_kind: u32): u32 {
this.resolve(method);
val holder: u32 = this.decls.owners.p[method];
return this.trait_in_terms(this.decls.types.p[method], holder, trait_kind, self_kind);
}
#! The type of a trait's method called on a value of the type: through a bound
#! of a type parameter, or through a trait reference.
#! @param this The checker.
#! @param method The method's declaration.
#! @param kind The type of the value it is called on.
#! @returns The method's type, or the error type where the value's type gives no
#! trait declaring it.
pub def checker::trait_method_type(&mut this, method: u32, kind: u32): u32 {
val plain := this.unalias(kind);
return this.requirement_type(method, this.trait_reached(method, plain), plain);
}
#! The trait declaring a method, given its arguments, as a value of the type it
#! is called on reaches it: through a bound of a type parameter, through a
#! trait reference, or as the trait the type implements.
#! @param this The checker.
#! @param method The method's declaration.
#! @param kind The type of the value it is called on.
#! @returns The trait's type.
pub def checker::trait_reached(&mut this, method: u32, kind: u32): u32 {
val holder: u32 = this.decls.owners.p[method];
val plain := this.unalias(kind);
# A trait reference has the methods of the traits it composes, in terms of
# its own arguments.
if this.named_kind(plain) == dk::trait_type {
return this.bound_for(plain, this.enclosing_trait(method));
}
if this.types.kind_of(plain) == ty::param {
val param := this.types.first_of(plain);
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);
if this.composes(bound, holder, 0) {
return this.bound_for(bound, holder);
}
}
# What an `if const` proved of it, in the branch it guards, given its arguments.
for i in 0..this.proved_params.length {
continue if this.proved_params[i] != param;
val bound: u32 = this.proved_bounds[i];
if this.composes(bound, holder, 0) {
return this.bound_for(bound, holder);
}
}
# Within a trait, `self` has the members of the traits it composes, in terms
# of its own arguments.
if this.decls.names.p[param] == names::self_id {
val declarer: u32 = this.decls.owners.p[param];
if declarer != 0 && declarer != holder && this.decls.kinds.p[declarer] == dk::trait_type {
val own: u32 = this.decls.types.p[declarer];
if this.composes(own, holder, 0) {
return this.bound_for(own, holder);
}
}
return this.decls.types.p[holder];
}
}
return this.implemented(plain, this.enclosing_trait(method));
}
#! The trait, given its arguments, that a type implements where it has a
#! trait's member: the trait the type names that is the one declaring it, or
#! composes it, in terms of the type's arguments, as `pair<i64>` for `one:
#! pair<i64>`, or the trait's own type where it names none.
#! @param this The checker.
#! @param kind The type.
#! @param holder The declaration of the trait declaring the member.
#! @returns The trait's type.
pub def checker::implemented(&mut this, kind: u32, holder: u32): u32 {
val plain := this.unalias(kind);
if this.types.kind_of(plain) == ty::named {
val decl := this.types.first_of(plain);
val run := this.composed_of(decl);
for i in 0..this.decls.list_length(run) {
val named := this.instantiated(this.decls.list_at(run, i as u32), decl, plain);
return this.bound_for(named, holder) if this.composes(named, holder, 0);
}
}
return this.decls.types.p[holder];
}
#! The trait a bound is, or the trait it composes that declares a member, given
#! the bound's arguments.
#! @param this The checker.
#! @param bound The bound's type.
#! @param holder The declaration of the trait declaring the member.
#! @returns That trait's type, given the bound's arguments, or the bound itself
#! where no trait it composes declares the member.
pub def checker::bound_for(&mut this, bound: u32, holder: u32): u32 {
return bound if this.types.first_of(bound) == holder;
val decl := this.types.first_of(bound);
val run := this.composed_of(decl);
for i in 0..this.decls.list_length(run) {
val inner := this.trait_in_terms(this.decls.list_at(run, i as u32), decl, bound, bt::error);
return this.bound_for(inner, holder) if this.composes(inner, holder, 0);
}
return bound;
}
#! Whether a type parameter is bounded by `ordinal`, `integer`, or `real`, which
#! only numbers and enums satisfy, so its values are ordered as theirs are.
#! @param this The checker.
#! @param param The parameter's declaration.
#! @returns Whether it is.
pub def checker::param_ordinal(&mut this, param: u32): bool {
return this.bounded_by(param, this.std_decl(word::ordinal), this.std_decl(word::integer), this.std_decl(word::real));
}
#! Whether the type parameter's bounds let numbers stand in for it.
#! @param this The checker.
#! @param param The type parameter's type.
#! @returns Whether they do.
pub def checker::param_has_numeric(&mut this, param: u32): bool {
return this.bounded_by(param, this.std_decl(word::numeric), this.std_decl(word::integer), this.std_decl(word::real));
}
#! Checks that each type argument of a named type satisfies the bounds of its
#! parameter.
#! @param this The checker.
#! @param decl The declaration of the generic type.
#! @param kind The named type, given its arguments.
#! @param node The node notes are placed at.
pub def checker::check_bounds(&mut this, decl: u32, kind: u32, node: u32): void {
val params: u32 = this.decls.params.p[decl];
var args := this.types.parts_of(kind);
this.check_args_bounds(decl, params, args.view(), node);
if this.is_std(kind, word::simd) || this.is_std(kind, word::mask) {
this.check_lanes(decl, kind, node);
}
}
#! Checks that a vector, or a mask of one, holds 16 or 32 bytes of numbers, or
#! 64 bytes of at most 32, as the processor's vector instructions take them: a
#! mask's bits are a `u32`.
#! @param this The checker.
#! @param decl `simd` or `mask`.
#! @param kind The vector's type, given its lanes.
#! @param node The node notes are placed at.
def checker::check_lanes(&mut this, decl: u32, kind: u32, node: u32): void {
return if this.types.part_count(kind) < 2;
val lane := this.unalias(this.types.part_at(kind, 0));
val count := this.types.part_at(kind, 1);
# Within a template, what it is given is known only in each instance.
return if this.types.is_open(lane) || this.types.kind_of(count) != ty::value;
val held: known = this.knowns.p[this.types.second_of(count)];
val number := this.types.kind_of(lane) == ty::signed || this.types.kind_of(lane) == ty::unsigned
|| this.types.kind_of(lane) == ty::floating;
val bytes := number && held.kind == kk::number ? held.value.magnitude * (this.types.first_of(lane) as u64 / 8) : 0;
return if bytes == 16 || bytes == 32 || (bytes == 64 && held.value.magnitude <= 32);
var text := string::from("`", nothing {});
text.append(this.decl_text(decl));
text.append("` holds 16 or 32 bytes of numbers, as `f32[4]` or `u8[32]`, or 64 of at most 32, as `f32[16]`, and is given `");
val named := this.describe(this.types.part_at(kind, 0));
text.append(named.view());
text.append("[");
val length := this.describe(count);
text.append(length.view());
text.append("]`.");
this.report_text(node, ec::generic, &text);
}
#! Checks that each type argument satisfies the bounds of its parameter, each
#! bound given the arguments for the parameters it names.
#! @param this The checker.
#! @param decl The generic declaration.
#! @param params The run of its type parameters.
#! @param args The type arguments.
#! @param node The node notes are placed at.
#! @returns Whether each satisfies them.
pub def checker::check_args_bounds(&mut this, decl: u32, params: u32, args: span<u32>, node: u32): bool {
val from := this.decls.list_of(params);
return this.check_bounds_of(decl, from.view(), args, node);
}
#! Checks that each type argument satisfies the bounds of its parameter, as
#! `check_args_bounds` does, given the parameters themselves.
#! @param this The checker.
#! @param decl The generic declaration.
#! @param from Its type parameters.
#! @param args The type arguments, one for each.
#! @param node The node a note is placed at.
#! @returns Whether every argument satisfies its bounds.
pub def checker::check_bounds_of(&mut this, decl: u32, from: span<u32>, args: span<u32>, node: u32): bool {
var valid := true;
for i in 0..from.length {
break if i >= args.length;
val param: u32 = from[i];
val given := args[i];
continue if given == bt::error;
# A pack's bounds are each of its types'.
var each := vector::<u32>::new();
if this.types.is_known_pack(given) {
each.extend(this.types.parts_view(given));
} else {
each.append(given);
}
this.resolve(param);
val run: u32 = this.decls.children.p[param];
for j in 0..this.decls.list_length(run) {
val bound := this.substitute(this.decls.list_at(run, j as u32), from, args);
for &arg in each {
continue if this.satisfies(arg, bound);
valid = false;
var text := string::from("`", nothing {});
val named_arg := this.describe(arg);
text.append(named_arg.view());
# What has no zero value is named, since the type may be one holding it.
if this.types.first_of(bound) == this.std_decl(word::zeroable) {
val lacking := this.zero_lacking(arg);
text.append("` is not `zeroable`, which `");
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` requires: ");
text.append(lacking.view());
text.append(".");
this.report_text(node, ec::bound, &text);
continue;
}
val named := this.describe(bound);
text.append("` does not implement `");
text.append(named.view());
# A reference whose referent satisfies the trait says why it does not.
val why := this.unlent(arg, bound);
if why.length > 0 {
text.append("`, though what it refers to does: ");
text.append(why.view());
text.append(". `");
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` requires it.");
this.report_text(node, ec::bound, &text);
continue;
}
text.append("`, which `");
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` requires.");
this.report_text(node, ec::bound, &text);
}
}
}
return valid;
}