738 lines
31 KiB
Plaintext
738 lines
31 KiB
Plaintext
#! @file generics.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains generics: inferring the type arguments of a call from what it is
|
||
|
|
#! given, and the instances of generic functions. A generic's body is checked
|
||
|
|
#! once against its bounds, then again for each instance the program uses,
|
||
|
|
#! over the template's own nodes with a result table of the instance's, so the
|
||
|
|
#! template is never copied. An instance is made where a call gives a generic
|
||
|
|
#! types holding no type parameter, and its body is checked once every
|
||
|
|
#! declaration has been.
|
||
|
|
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! What inferring type arguments works out: the type of each parameter of the
|
||
|
|
#! declaration, its owner's first, and whether each was only suggested by a
|
||
|
|
#! constant, which a typed value given for it overrules.
|
||
|
|
pub struct checker::inferred {
|
||
|
|
var params: vector<u32>; #! The declarations of the type parameters.
|
||
|
|
var binds: vector<u32>; #! The type of each, or `unknown_type` where nothing gave it yet.
|
||
|
|
var soft: vector<bool>; #! Whether each was only suggested by a constant.
|
||
|
|
var outer: vector<u32>; #! The parameters a trait's default names beside its own: the trait's, and `self`.
|
||
|
|
var given: vector<u32>; #! The type each of those is given by the value it is called on.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Begins inferring the declaration's type arguments, with those of its owner
|
||
|
|
#! and those written given.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The generic declaration.
|
||
|
|
#! @param owner The type it is a member of, given its arguments, or the
|
||
|
|
#! error type.
|
||
|
|
#! @param written The type arguments written for its own parameters.
|
||
|
|
#! @returns What is known so far.
|
||
|
|
pub def checker::begin_inference(&mut this, decl: u32, owner: u32, written: span<u32>): checker::inferred {
|
||
|
|
var made := checker::inferred {
|
||
|
|
params = vector::<u32>::new(),
|
||
|
|
binds = vector::<u32>::new(),
|
||
|
|
soft = vector::<bool>::new(),
|
||
|
|
outer = vector::<u32>::new(),
|
||
|
|
given = vector::<u32>::new()
|
||
|
|
};
|
||
|
|
|
||
|
|
val params := this.decls.list_view(this.decls.params.p[decl]);
|
||
|
|
made.params.extend(params);
|
||
|
|
made.binds.resize(params.length, checker::unknown_type);
|
||
|
|
made.soft.resize(params.length, false);
|
||
|
|
|
||
|
|
var at: u64 = 0;
|
||
|
|
if owner != bt::error && this.types.kind_of(owner) == ty::named {
|
||
|
|
for &part in this.types.parts_view(owner) {
|
||
|
|
break if at >= made.binds.length;
|
||
|
|
made.binds[at] = part;
|
||
|
|
at++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# Written type arguments fill a pack with whatever the parameters before it
|
||
|
|
# leave, as `show_all::<i64, bool>`.
|
||
|
|
for i in 0..written.length {
|
||
|
|
break if at >= made.binds.length;
|
||
|
|
val param := made.params[at];
|
||
|
|
if this.decls.has(param, df::packed) {
|
||
|
|
made.binds[at] = this.gather_pack(written, i, 0);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
made.binds[at] = written[i];
|
||
|
|
at++;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Binds a pack parameter to the types of the arguments a call gives it from
|
||
|
|
#! `from` on, each spread pack already its own types.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param pack The pack parameter's type.
|
||
|
|
#! @param slots The types of the call's arguments.
|
||
|
|
#! @param from The first argument the pack takes.
|
||
|
|
#! @param into What is inferred, which the pack's types are written to.
|
||
|
|
pub def checker::bind_pack(&mut this, pack: u32, slots: span<u32>, from: u64, into: &mut checker::inferred): void {
|
||
|
|
var types := vector::<u32>::new();
|
||
|
|
for i in from..slots.length {
|
||
|
|
types.append(slots[i] == 0 ? bt::error : this.settle(slots[i]));
|
||
|
|
}
|
||
|
|
|
||
|
|
val decl := this.types.first_of(pack);
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
if into.params[i] == decl && (into.binds[i] == checker::unknown_type || into.soft[i]) {
|
||
|
|
into.binds[i] = this.types.pack(types.view(), 0);
|
||
|
|
into.soft[i] = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Infers type parameters a type holds from the type given where it is
|
||
|
|
#! expected, as `T` of `&T` from a `&i64`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param pattern The type written, holding the parameters.
|
||
|
|
#! @param actual The type given.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @param depth How deeply the search has gone, which stops a type nesting
|
||
|
|
#! without end.
|
||
|
|
pub def checker::unify(&mut this, pattern: u32, actual: u32, into: &mut checker::inferred, depth: u32): void {
|
||
|
|
return if depth > 16 || !this.types.is_open(pattern) || actual == bt::error;
|
||
|
|
|
||
|
|
# A reference is read through to what it refers to wherever a type that is no
|
||
|
|
# reference is wanted, so `through(r)` takes the pointer `r` refers to.
|
||
|
|
val shape := this.types.kind_of(pattern);
|
||
|
|
if shape != ty::param && shape != ty::pack && shape != ty::reference {
|
||
|
|
actual = this.reached(actual);
|
||
|
|
}
|
||
|
|
|
||
|
|
match shape {
|
||
|
|
ty::param => {
|
||
|
|
val decl := this.types.first_of(pattern);
|
||
|
|
# A constant parameter is given a constant, and a type parameter a type.
|
||
|
|
val given := this.types.kind_of(actual);
|
||
|
|
val constant := given == ty::value || (given == ty::param && this.decls.has(this.types.first_of(actual), df::valued));
|
||
|
|
return if this.decls.has(decl, df::valued) != constant;
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
continue if into.params[i] != decl;
|
||
|
|
|
||
|
|
if this.types.is_untyped(actual) {
|
||
|
|
if into.binds[i] == checker::unknown_type {
|
||
|
|
into.binds[i] = this.settle(actual);
|
||
|
|
into.soft[i] = true;
|
||
|
|
} else if into.soft[i] && this.types.is_float(actual) && !this.types.is_float(into.binds[i]) {
|
||
|
|
# An integer constant and a float constant share the float.
|
||
|
|
into.binds[i] = this.settle(actual);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if into.binds[i] == checker::unknown_type || into.soft[i] {
|
||
|
|
into.binds[i] = actual;
|
||
|
|
into.soft[i] = false;
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ty::reference, ty::pointer => {
|
||
|
|
val plain := this.unalias(actual);
|
||
|
|
if this.types.kind_of(plain) == this.types.kind_of(pattern) {
|
||
|
|
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||
|
|
} else if this.types.kind_of(pattern) == ty::reference {
|
||
|
|
this.unify(this.types.first_of(pattern), actual, into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ty::fixed => {
|
||
|
|
val plain := this.unalias(this.reached_value(actual));
|
||
|
|
if this.types.kind_of(plain) == ty::fixed {
|
||
|
|
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||
|
|
# A length that is a constant parameter is given the length of the array.
|
||
|
|
if this.types.part_count(pattern) != 0 {
|
||
|
|
val length := this.types.part_at(pattern, 0);
|
||
|
|
if this.types.part_count(plain) != 0 {
|
||
|
|
this.unify(length, this.types.part_at(plain, 0), into, depth + 1);
|
||
|
|
} else if this.types.kind_of(length) == ty::param && this.decls.has(this.types.first_of(length), df::valued) {
|
||
|
|
val of := this.constant_type(length);
|
||
|
|
val tkd := this.types.kind_of(this.unalias(of));
|
||
|
|
if tkd == ty::signed || tkd == ty::unsigned {
|
||
|
|
val count := this.known_number(constant::integer(this.types.second_of(plain) as u64, false), of);
|
||
|
|
this.unify(length, this.known_type(count), into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ty::function => {
|
||
|
|
val plain := this.unalias(actual);
|
||
|
|
return if this.types.kind_of(plain) != ty::function;
|
||
|
|
# A pack among the parameters, as `Ts` of `def(Ts...): R`, takes the types of
|
||
|
|
# those after the ones before it, and is variadic where the function is.
|
||
|
|
val count := this.types.part_count(pattern);
|
||
|
|
val given := this.types.part_count(plain);
|
||
|
|
if this.types.second_of(pattern) != 0 && count != 0 && this.types.is_open_pack(this.types.part_at(pattern, count - 1)) {
|
||
|
|
return if given + 1 < count;
|
||
|
|
for i in 0..count - 1 {
|
||
|
|
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(plain, i as u32), into, depth + 1);
|
||
|
|
}
|
||
|
|
this.bind_param(this.types.first_of(this.types.part_at(pattern, count - 1)), this.types.params_pack(plain, count - 1), into);
|
||
|
|
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if given == count {
|
||
|
|
for i in 0..count {
|
||
|
|
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(plain, i as u32), into, depth + 1);
|
||
|
|
}
|
||
|
|
this.unify(this.types.first_of(pattern), this.types.first_of(plain), into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# A pack whose types are not known takes those of the pack given.
|
||
|
|
ty::pack => {
|
||
|
|
if this.types.is_open_pack(pattern) && this.types.kind_of(actual) == ty::pack {
|
||
|
|
this.bind_param(this.types.first_of(pattern), actual, into);
|
||
|
|
} else if !this.types.is_open_pack(pattern) && this.types.kind_of(actual) == ty::pack
|
||
|
|
&& this.types.part_count(actual) == this.types.part_count(pattern) {
|
||
|
|
for i in 0..this.types.part_count(pattern) {
|
||
|
|
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(actual, i as u32), into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ty::named => {
|
||
|
|
var plain := this.reached_value(actual);
|
||
|
|
if this.types.kind_of(plain) == ty::named && this.types.first_of(plain) != this.types.first_of(pattern) {
|
||
|
|
plain = this.unalias(plain);
|
||
|
|
}
|
||
|
|
# A value of a type implementing a generic trait gives its parameters
|
||
|
|
# through what implements the trait's members.
|
||
|
|
val wanted := this.types.first_of(pattern);
|
||
|
|
if this.decls.kinds.p[wanted] == dk::trait_type && this.named_kind(plain) != dk::trait_type
|
||
|
|
&& this.types.kind_of(plain) != ty::param {
|
||
|
|
this.unify_named(pattern, plain, into, depth);
|
||
|
|
this.unify_members(pattern, plain, into, depth);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.types.kind_of(plain) == ty::named && this.types.first_of(plain) == this.types.first_of(pattern) {
|
||
|
|
for i in 0..this.types.part_count(pattern) {
|
||
|
|
break if i >= this.types.part_count(plain);
|
||
|
|
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(plain, i as u32), into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else => {}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives a type parameter, or a pack, the type inferred for it, where nothing
|
||
|
|
#! but a constant has given it one yet.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The parameter's declaration.
|
||
|
|
#! @param given The type it is given.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
def checker::bind_param(&mut this, decl: u32, given: u32, into: &mut checker::inferred): void {
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
continue if into.params[i] != decl;
|
||
|
|
if into.binds[i] == checker::unknown_type || into.soft[i] {
|
||
|
|
into.binds[i] = given;
|
||
|
|
into.soft[i] = false;
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Infers type parameters a generic trait holds from a type naming it, as the
|
||
|
|
#! type names it: `x : into<int>` gives the `T` of `into<T>` as `int`. A
|
||
|
|
#! member's types may be what the trait's arguments ask for, as `F::arg`, which
|
||
|
|
#! say nothing of the argument itself, so what the type names is read first.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param pattern The trait's type, holding the parameters.
|
||
|
|
#! @param kind The type given.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @param depth How deeply the search has gone.
|
||
|
|
def checker::unify_named(&mut this, pattern: u32, kind: u32, into: &mut checker::inferred, depth: u32): void {
|
||
|
|
return if depth > 16;
|
||
|
|
val holder := this.types.first_of(pattern);
|
||
|
|
val named := this.implemented(this.reached_value(kind), holder);
|
||
|
|
return if named == this.decls.types.p[holder] || this.types.first_of(named) != holder;
|
||
|
|
for i in 0..this.types.part_count(pattern) {
|
||
|
|
break if i >= this.types.part_count(named);
|
||
|
|
this.unify(this.types.part_at(pattern, i as u32), this.types.part_at(named, i as u32), into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Infers type parameters a generic trait holds from a type implementing it,
|
||
|
|
#! through what implements each of the trait's members, as `count::into(&this):
|
||
|
|
#! int` gives the `T` of `into<T>`, and through what implements those of the
|
||
|
|
#! traits it composes.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param pattern The trait's type, holding the parameters.
|
||
|
|
#! @param kind The type given.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @param depth How deeply the search has gone.
|
||
|
|
def checker::unify_members(&mut this, pattern: u32, kind: u32, into: &mut checker::inferred, depth: u32): void {
|
||
|
|
return if depth > 16;
|
||
|
|
val holder := this.types.first_of(pattern);
|
||
|
|
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);
|
||
|
|
val kind_of: dk = this.decls.kinds.p[wanted];
|
||
|
|
continue if kind_of != dk::function && kind_of != dk::variable;
|
||
|
|
val found := this.find_member(kind, this.decls.names.p[wanted]);
|
||
|
|
# A method the compiler gives a built-in type, as a function's `invoke`, says
|
||
|
|
# what it takes and gives as a declared one does.
|
||
|
|
if found.decl == 0 && kind_of == dk::function && this.decls.has(wanted, df::method) {
|
||
|
|
val given := this.builtin_method(kind, this.decls.names.p[wanted]);
|
||
|
|
if given != 0 {
|
||
|
|
this.resolve(wanted);
|
||
|
|
val asked := this.trait_in_terms(this.decls.types.p[wanted], holder, pattern, kind);
|
||
|
|
this.unify(this.past_receiver(asked), this.past_receiver(given), into, depth + 1);
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
continue if found.decl == 0 || this.enclosing_trait(found.decl) != 0;
|
||
|
|
this.resolve(wanted);
|
||
|
|
val asked := this.trait_in_terms(this.decls.types.p[wanted], holder, pattern, kind);
|
||
|
|
if kind_of == dk::variable {
|
||
|
|
continue if this.decls.kinds.p[found.decl] != dk::variable;
|
||
|
|
this.resolve(found.decl);
|
||
|
|
this.unify(asked, this.instantiated(this.decls.types.p[found.decl], this.type_decl(found.owner), found.owner), into, depth + 1);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Of overloads, one of the shape asked for.
|
||
|
|
val count := this.types.part_count(asked);
|
||
|
|
var walk := this.decls.walk_overloads(found.decl);
|
||
|
|
while true {
|
||
|
|
val at := walk.next();
|
||
|
|
break if at == 0;
|
||
|
|
if this.decls.kinds.p[at] == dk::function && this.decls.has(at, df::method) == this.decls.has(wanted, df::method)
|
||
|
|
&& this.member_seen(at) {
|
||
|
|
val given := this.method_type(at, found.owner);
|
||
|
|
if this.types.kind_of(given) == ty::function && this.types.part_count(given) == count {
|
||
|
|
val skip: u64 = this.decls.has(wanted, df::method) ? 1 : 0;
|
||
|
|
for p in skip..count as u64 {
|
||
|
|
this.unify(this.types.part_at(asked, p as u32), this.types.part_at(given, p as u32), into, depth + 1);
|
||
|
|
}
|
||
|
|
this.unify(this.types.first_of(asked), this.types.first_of(given), into, depth + 1);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
val composed := this.composed_of(holder);
|
||
|
|
for i in 0..this.decls.list_length(composed) {
|
||
|
|
val inner := this.trait_in_terms(this.decls.list_at(composed, i as u32), holder, pattern, kind);
|
||
|
|
this.unify_members(inner, kind, into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A method's type without its receiver: the function its other parameters and
|
||
|
|
#! its result make, variadic where the method is.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param method The method's type.
|
||
|
|
#! @returns The function's type.
|
||
|
|
def checker::past_receiver(&mut this, method: u32): u32 {
|
||
|
|
var parts := vector::<u32>::new();
|
||
|
|
for i in 1..this.types.part_count(method) {
|
||
|
|
parts.append(this.types.part_at(method, i as u32));
|
||
|
|
}
|
||
|
|
return this.types.function(parts.view(), this.types.first_of(method), this.types.second_of(method) != 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What the type arguments inferred so far give those their bounds name, as `C`,
|
||
|
|
#! where `C: iterable<T>`, gives `T` the type of the elements it lends. A bound
|
||
|
|
#! may name a parameter another bound then gives more of, so the bounds are read
|
||
|
|
#! again until they give nothing new.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @param depth How many times the bounds have been read.
|
||
|
|
pub def checker::unify_bounds(&mut this, into: &mut checker::inferred, depth: u32): void {
|
||
|
|
return if depth > 4 || this.unbound(into) == 0;
|
||
|
|
val before := this.unbound_count(into);
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
val param: u32 = into.params[i];
|
||
|
|
val given: u32 = into.binds[i];
|
||
|
|
continue if given == checker::unknown_type || into.soft[i] || this.decls.has(param, df::valued);
|
||
|
|
this.resolve(param);
|
||
|
|
val bounds: u32 = this.decls.children.p[param];
|
||
|
|
for j in 0..this.decls.list_length(bounds) {
|
||
|
|
val bound := this.outer_terms(this.decls.list_at(bounds, j as u32), into);
|
||
|
|
if this.types.is_open(bound) {
|
||
|
|
this.unify_trait(bound, given, into, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if this.unbound_count(into) != before {
|
||
|
|
this.unify_bounds(into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A type a trait's default names, with the trait's parameters and `self` given
|
||
|
|
#! the types the value it is called on gives them, leaving its own parameters.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @returns The type in those terms.
|
||
|
|
pub def checker::outer_terms(&mut this, kind: u32, into: &checker::inferred): u32 {
|
||
|
|
return kind if into.outer.length == 0;
|
||
|
|
return this.substitute(kind, into.outer.view(), into.given.view());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives what is inferred for a trait's default compiled for each type the
|
||
|
|
#! terms its bounds are read in: the trait's parameters, given the trait the
|
||
|
|
#! value it is called on implements, and `self`, given the value's type.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param method The default's declaration.
|
||
|
|
#! @param through The type of the value it is called on, read through its
|
||
|
|
#! references.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
pub def checker::give_outer(&mut this, method: u32, through: u32, into: &mut checker::inferred): void {
|
||
|
|
val holder := this.enclosing_trait(method);
|
||
|
|
return if holder == 0;
|
||
|
|
val plain := this.unalias(through);
|
||
|
|
val trait_kind := this.trait_reached(method, plain);
|
||
|
|
val params: u32 = this.decls.params.p[holder];
|
||
|
|
for i in 0..this.decls.list_length(params) {
|
||
|
|
into.outer.append(this.decls.list_at(params, i as u32));
|
||
|
|
into.given.append(i < this.types.part_count(trait_kind) ? this.types.part_at(trait_kind, i as u32) : bt::error);
|
||
|
|
}
|
||
|
|
into.outer.append(this.self_decl(holder));
|
||
|
|
into.given.append(plain);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many type parameters nothing has given a type yet.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param inferred What was inferred.
|
||
|
|
#! @returns How many.
|
||
|
|
def checker::unbound_count(&this, inferred: &checker::inferred): u32 {
|
||
|
|
var count: u32 = 0;
|
||
|
|
for i in 0..inferred.binds.length {
|
||
|
|
count += inferred.binds[i] == checker::unknown_type ? 1 : 0;
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Infers the arguments of a trait a type implements from how it implements it:
|
||
|
|
#! each type the trait asks for is the one the type gives, which the bounds it is
|
||
|
|
#! asked with say more of, and each method's parameters and result are those of
|
||
|
|
#! the type's method.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param pattern The trait, as the template writes it.
|
||
|
|
#! @param actual The type implementing it.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @param depth How deeply the search has gone.
|
||
|
|
def checker::unify_trait(&mut this, pattern: u32, actual: u32, into: &mut checker::inferred, depth: u32): void {
|
||
|
|
return if depth > 8 || actual == bt::error;
|
||
|
|
val plain := this.unalias(pattern);
|
||
|
|
return if this.named_kind(plain) != dk::trait_type;
|
||
|
|
val asked := this.types.first_of(plain);
|
||
|
|
this.resolve(asked);
|
||
|
|
this.unify_named(plain, actual, into, depth);
|
||
|
|
|
||
|
|
# The trait's own parameters are the pattern's arguments, and `self` the type.
|
||
|
|
var from := vector::<u32>::new();
|
||
|
|
var to := vector::<u32>::new();
|
||
|
|
val params: u32 = this.decls.params.p[asked];
|
||
|
|
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(plain) ? this.types.part_at(plain, i as u32) : bt::error);
|
||
|
|
}
|
||
|
|
from.append(this.self_decl(asked));
|
||
|
|
to.append(actual);
|
||
|
|
|
||
|
|
val members: u32 = this.decls.children.p[asked];
|
||
|
|
for i in 0..this.decls.list_length(members) {
|
||
|
|
val member := this.decls.list_at(members, i as u32);
|
||
|
|
if this.decls.kinds.p[member] == dk::assoc_type {
|
||
|
|
val given := this.member_type(actual, this.decls.names.p[member]);
|
||
|
|
continue if given == bt::error;
|
||
|
|
this.resolve(member);
|
||
|
|
val bounds: u32 = this.decls.children.p[member];
|
||
|
|
for j in 0..this.decls.list_length(bounds) {
|
||
|
|
val bound := this.substitute(this.decls.list_at(bounds, j as u32), from.view(), to.view());
|
||
|
|
this.unify_trait(bound, given, into, depth + 1);
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
continue if this.decls.kinds.p[member] != dk::function;
|
||
|
|
val found := this.find_member(this.reached_value(actual), this.decls.names.p[member]);
|
||
|
|
# A method the compiler gives a built-in type, as a function's `invoke`.
|
||
|
|
if found.decl == 0 && this.decls.has(member, df::method) {
|
||
|
|
val given := this.builtin_method(this.reached_value(actual), this.decls.names.p[member]);
|
||
|
|
if given != 0 {
|
||
|
|
this.resolve(member);
|
||
|
|
val want := this.substitute(this.decls.types.p[member], from.view(), to.view());
|
||
|
|
this.unify(this.past_receiver(want), this.past_receiver(given), into, depth + 1);
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
continue if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function;
|
||
|
|
this.resolve(member);
|
||
|
|
val want := this.substitute(this.decls.types.p[member], from.view(), to.view());
|
||
|
|
val have := this.member_function_type(found.decl, found.owner);
|
||
|
|
this.unify(want, have, into, depth + 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a value of the type is, read through a reference.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @returns What it reaches, or the type itself.
|
||
|
|
def checker::reached_value(&mut this, kind: u32): u32 {
|
||
|
|
return this.types.kind_of(kind) == ty::reference ? this.types.first_of(kind) : kind;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The first type parameter nothing gave a type, or 0 where every one has one.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param inferred What was inferred.
|
||
|
|
#! @returns The type parameter's declaration, or 0.
|
||
|
|
pub def checker::unbound(&this, inferred: &checker::inferred): u32 {
|
||
|
|
for i in 0..inferred.params.length {
|
||
|
|
return inferred.params[i] if inferred.binds[i] == checker::unknown_type;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a type parameter is inferred as while nothing has given it a type.
|
||
|
|
pub const checker::unknown_type: u32 = 0xFFFFFFFF;
|
||
|
|
|
||
|
|
#! The type with the inferred type parameters replaced by their types.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @param inferred What was inferred.
|
||
|
|
#! @returns The type with them replaced.
|
||
|
|
pub def checker::apply(&mut this, kind: u32, inferred: &checker::inferred): u32 {
|
||
|
|
return this.substitute(kind, inferred.params.view(), inferred.binds.view());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The type with the type parameters inferred so far replaced by their types,
|
||
|
|
#! and those nothing has given a type yet left as they are.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @param inferred What was inferred.
|
||
|
|
#! @returns The type with the known ones replaced.
|
||
|
|
pub def checker::apply_known(&mut this, kind: u32, inferred: &checker::inferred): u32 {
|
||
|
|
var from := vector::<u32>::new();
|
||
|
|
var to := vector::<u32>::new();
|
||
|
|
for i in 0..inferred.params.length {
|
||
|
|
continue if inferred.binds[i] == checker::unknown_type;
|
||
|
|
from.append(inferred.params[i]);
|
||
|
|
to.append(inferred.binds[i]);
|
||
|
|
}
|
||
|
|
return this.substitute(kind, from.view(), to.view());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a type holds a type parameter, so it belongs to a template rather
|
||
|
|
#! than an instance.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kinds The types.
|
||
|
|
#! @returns Whether any does.
|
||
|
|
pub def checker::any_open(&this, kinds: span<u32>): bool {
|
||
|
|
for &each in kinds {
|
||
|
|
return true if this.types.is_open(each);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How deeply type arguments nest within the type.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @param depth How deeply it already nests.
|
||
|
|
#! @returns How deeply its arguments nest.
|
||
|
|
def checker::nesting(&this, kind: u32, depth: u32): u32 {
|
||
|
|
return depth if depth > checker::max_nesting;
|
||
|
|
|
||
|
|
var deepest := depth;
|
||
|
|
match this.types.kind_of(kind) {
|
||
|
|
ty::reference, ty::pointer, ty::fixed => {
|
||
|
|
return this.nesting(this.types.first_of(kind), depth + 1);
|
||
|
|
}
|
||
|
|
ty::function, ty::named, ty::pack => {
|
||
|
|
for &part in this.types.parts_view(kind) {
|
||
|
|
val inner := this.nesting(part, depth + 1);
|
||
|
|
deepest = inner > deepest ? inner : deepest;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else => {}
|
||
|
|
}
|
||
|
|
return deepest;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The instance of the generic function given the type arguments, made where it
|
||
|
|
#! is new and waiting for its body to be checked.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The generic function.
|
||
|
|
#! @param args Its type arguments, its owner's first.
|
||
|
|
#! @param node The node a note is placed at, where too many instances are
|
||
|
|
#! made.
|
||
|
|
#! @returns One more than its index, or 0 where the arguments hold type
|
||
|
|
#! parameters, or the program has made as many instances as it may.
|
||
|
|
pub def checker::instance_of(&mut this, decl: u32, args: span<u32>, node: u32): u32 {
|
||
|
|
return 0 if this.any_open(args) || args.length == 0;
|
||
|
|
for &each in args {
|
||
|
|
return 0 if each == bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
val pack := this.types.pack(args, 0);
|
||
|
|
val key := pair_key(decl, pack);
|
||
|
|
if this.instanced.get(&key) is some(got) {
|
||
|
|
return got;
|
||
|
|
}
|
||
|
|
|
||
|
|
if this.instances.length as u32 >= checker::max_instances {
|
||
|
|
this.report(node, ec::limit, "The program makes more instances of generics than the checker allows.");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
if this.nesting(pack, 0) > checker::max_nesting {
|
||
|
|
this.report(node, ec::limit, "The type arguments nest too deeply, as a generic instantiating itself with ever larger types does.");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.resolve(decl);
|
||
|
|
val params: u32 = this.decls.params.p[decl];
|
||
|
|
var from := this.decls.list_of(params);
|
||
|
|
|
||
|
|
val id := this.instances.length as u32;
|
||
|
|
this.instances.append(instance {
|
||
|
|
decl = decl,
|
||
|
|
args = pack,
|
||
|
|
kind = this.substitute(this.decls.types.p[decl], from.view(), args)
|
||
|
|
});
|
||
|
|
this.instanced.set(key, id + 1);
|
||
|
|
this.wants.append(id);
|
||
|
|
return id + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The instance of a trait's default method a type takes. A default of a trait
|
||
|
|
#! that is only a bound is compiled for each type taking it, with the trait's
|
||
|
|
#! parameters bound to its arguments, `self` to the type, and its own type
|
||
|
|
#! parameters to those the call gives. Any other is compiled for the trait,
|
||
|
|
#! given its arguments, with `self` the trait itself, called through as a
|
||
|
|
#! reference to the trait is, and a default of a trait without parameters is
|
||
|
|
#! then its own template.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @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 method's own type arguments, which a default
|
||
|
|
#! compiled for each type takes after `self`.
|
||
|
|
#! @returns One more than the instance, or 0 where it is its own template or
|
||
|
|
#! its arguments are not known.
|
||
|
|
pub def checker::default_instance(&mut this, method: u32, trait_kind: u32, self_kind: u32, own: span<u32>): u32 {
|
||
|
|
val holder := this.enclosing_trait(method);
|
||
|
|
return 0 if holder == 0 || holder == method || this.types.kind_of(trait_kind) != ty::named;
|
||
|
|
val params: u32 = this.decls.params.p[holder];
|
||
|
|
val typed := this.typed_default(method);
|
||
|
|
return 0 if !typed && this.decls.list_length(params) == 0;
|
||
|
|
var args := this.types.parts_of(trait_kind);
|
||
|
|
if typed {
|
||
|
|
return 0 if own.length != this.decls.list_length(this.decls.params.p[method]);
|
||
|
|
args.append(self_kind);
|
||
|
|
args.extend(own);
|
||
|
|
}
|
||
|
|
return 0 if args.length == 0 || this.any_open(args.view());
|
||
|
|
for &each in args {
|
||
|
|
return 0 if each == bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
val pack := this.types.pack(args.view(), 0);
|
||
|
|
val key := pair_key(method, pack);
|
||
|
|
if this.instanced.get(&key) is some(got) {
|
||
|
|
return got;
|
||
|
|
}
|
||
|
|
if this.instances.length as u32 >= checker::max_instances {
|
||
|
|
this.report(this.decls.nodes.p[method], ec::limit, "The program makes more instances of generics than the checker allows.");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
if this.nesting(pack, 0) > checker::max_nesting {
|
||
|
|
this.report(this.decls.nodes.p[method], ec::limit, "The type arguments nest too deeply, as a generic instantiating itself with ever larger types does.");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.resolve(method);
|
||
|
|
var from := vector::<u32>::new();
|
||
|
|
var to := vector::<u32>::new();
|
||
|
|
val trait_args := this.types.part_count(trait_kind);
|
||
|
|
for i in 0..this.decls.list_length(params) {
|
||
|
|
from.append(this.decls.list_at(params, i as u32));
|
||
|
|
to.append(i < trait_args ? this.types.part_at(trait_kind, i as u32) : bt::error);
|
||
|
|
}
|
||
|
|
from.append(this.self_decl(holder));
|
||
|
|
to.append(typed ? self_kind : trait_kind);
|
||
|
|
if typed {
|
||
|
|
from.extend(this.decls.list_view(this.decls.params.p[method]));
|
||
|
|
to.extend(own);
|
||
|
|
}
|
||
|
|
|
||
|
|
val id := this.instances.length as u32;
|
||
|
|
this.instances.append(instance {
|
||
|
|
decl = method,
|
||
|
|
args = pack,
|
||
|
|
kind = this.substitute(this.decls.types.p[method], from.view(), to.view())
|
||
|
|
});
|
||
|
|
this.instanced.set(key, id + 1);
|
||
|
|
this.wants.append(id);
|
||
|
|
return id + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The first node of a declaration's nodes, which are one run of indexes, the
|
||
|
|
#! parser having made them as it went, the declaration's own last.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The declaration.
|
||
|
|
#! @returns The node.
|
||
|
|
pub def checker::first_node(&mut this, decl: u32): u32 {
|
||
|
|
val held := this.decl_bases.at_or_zero(decl);
|
||
|
|
return held if held != 0;
|
||
|
|
|
||
|
|
val saved := this.at;
|
||
|
|
this.use_table(this.units[this.decls.files.p[decl]].table);
|
||
|
|
val node: u32 = this.decls.nodes.p[decl];
|
||
|
|
var lowest := node;
|
||
|
|
var todo := vector::<u32>::new();
|
||
|
|
todo.append(node);
|
||
|
|
while todo.length != 0 {
|
||
|
|
val at: u32 = todo.last();
|
||
|
|
todo.truncate();
|
||
|
|
lowest = at < lowest ? at : lowest;
|
||
|
|
this.syn().children(at, &mut todo);
|
||
|
|
}
|
||
|
|
this.at = saved;
|
||
|
|
|
||
|
|
this.decl_bases.put(decl, lowest);
|
||
|
|
return lowest;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks the bodies of the instances the program uses, including those the
|
||
|
|
#! bodies checked here use in turn.
|
||
|
|
#! @param this The checker.
|
||
|
|
pub def checker::check_wanted(&mut this): void {
|
||
|
|
while this.wants_checked < this.wants.length {
|
||
|
|
val id: u32 = this.wants[this.wants_checked];
|
||
|
|
this.wants_checked++;
|
||
|
|
this.check_instance(id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks an instance's body over its template's nodes, with a result table of
|
||
|
|
#! its own and the type parameters bound to its arguments.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param id The instance's index.
|
||
|
|
def checker::check_instance(&mut this, id: u32): void {
|
||
|
|
val held: instance = this.instances[id];
|
||
|
|
val decl := held.decl;
|
||
|
|
return if this.decls.has(decl, df::failed);
|
||
|
|
|
||
|
|
val first := this.first_node(decl);
|
||
|
|
val last: u32 = this.decls.nodes.p[decl];
|
||
|
|
val table := this.add_table(this.decls.files.p[decl], first, last - first + 1, id + 1);
|
||
|
|
this.instances[id].table = table + 1;
|
||
|
|
|
||
|
|
val saved := this.at;
|
||
|
|
this.enter_decl(decl, held.args);
|
||
|
|
this.use_table(table);
|
||
|
|
this.at.instance = id + 1;
|
||
|
|
this.check_function_body(decl, held.kind);
|
||
|
|
this.leave(saved);
|
||
|
|
}
|