1833 lines
75 KiB
Plaintext
1833 lines
75 KiB
Plaintext
#! @file calls.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the checking of calls: of functions named or reached through a
|
||
|
|
#! type, of methods of a value, and of function values. A call of an
|
||
|
|
#! overloaded function chooses the overload the arguments fit best. Each
|
||
|
|
#! argument is checked once, before the overloads are compared, but for an
|
||
|
|
#! anonymous function or an array, which take their types from the parameter
|
||
|
|
#! they are given for, and are checked once it is chosen.
|
||
|
|
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! An overload a call may call, and how well the arguments fit it.
|
||
|
|
struct checker::candidate {
|
||
|
|
var decl: u32;
|
||
|
|
var kind: u32; #! Its function type, given the type arguments.
|
||
|
|
var binds: u32; #! The pack of its type arguments, where it is generic.
|
||
|
|
var worst: fit;
|
||
|
|
var total: u32;
|
||
|
|
var generic: bool;
|
||
|
|
var arity: bool; #! Whether it takes as many arguments as the call gives.
|
||
|
|
var infers: u32; #! A type parameter nothing gave a type, which the call cannot infer.
|
||
|
|
var later: bool; #! Whether it passes on a pack whose types are not known, so only each instance checks it.
|
||
|
|
var writes: u32; #! How many arguments it lends to `&mut` parameters, which what can be written is better given to.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A call.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param expected The type expected of what it gives, or the error type where
|
||
|
|
#! none is.
|
||
|
|
#! @returns The type it gives, or the error type where it is not valid.
|
||
|
|
def checker::call(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
val callee := this.left_of(node);
|
||
|
|
val run := this.right_of(node);
|
||
|
|
# The arguments are the run's nodes where the syntax holds them, which nothing
|
||
|
|
# changes while the call is checked.
|
||
|
|
val args := this.run_view(run);
|
||
|
|
|
||
|
|
match this.kind_of(callee) {
|
||
|
|
nk::name => {
|
||
|
|
val name := this.name_of(callee);
|
||
|
|
val ref := this.lookup(name, callee);
|
||
|
|
if ref == 0 {
|
||
|
|
this.report_undeclared(callee, name);
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
if (ref & (ref_local | ref_type)) == 0 && this.decls.kinds.p[ref] == dk::function
|
||
|
|
&& this.decls.has(ref, df::intrinsic) && name == this.names.intern(&"compile_error") {
|
||
|
|
# Recorded, since it ends the branch it is in, as `return` does.
|
||
|
|
this.set_ref(node, ref);
|
||
|
|
return this.compile_error(node, args);
|
||
|
|
}
|
||
|
|
if (ref & (ref_local | ref_type)) == 0 && this.decls.kinds.p[ref] == dk::function && this.is_reflection(ref) {
|
||
|
|
return this.reflect_call(node, callee, ref, 0, args, expected);
|
||
|
|
}
|
||
|
|
if (ref & (ref_local | ref_type)) == 0 && this.decls.kinds.p[ref] == dk::function {
|
||
|
|
if !this.visible(ref) {
|
||
|
|
this.report_hidden(callee, ref);
|
||
|
|
}
|
||
|
|
return this.call_decl(node, callee, ref, 0, bt::error, bt::error, 0, args, expected);
|
||
|
|
}
|
||
|
|
return this.call_value(node, callee, args, expected);
|
||
|
|
}
|
||
|
|
nk::path => {
|
||
|
|
val target := this.path_target(callee);
|
||
|
|
if target.decl == 0 {
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
if this.decls.kinds.p[target.decl] == dk::function && this.is_reflection(target.decl) {
|
||
|
|
val written := this.segment_args(callee, this.segment_count(callee) - 1);
|
||
|
|
return this.reflect_call(node, callee, target.decl, written, args, expected);
|
||
|
|
}
|
||
|
|
if this.decls.kinds.p[target.decl] == dk::function {
|
||
|
|
val written := this.segment_args(callee, this.segment_count(callee) - 1);
|
||
|
|
val first := this.overloads_for(target.decl, 0, target.owner, target.owner, args);
|
||
|
|
return this.call_decl(node, callee, first, 0, target.owner, target.owner, written, args, expected);
|
||
|
|
}
|
||
|
|
return this.call_value(node, callee, args, expected);
|
||
|
|
}
|
||
|
|
nk::member => {
|
||
|
|
# A constant settles as it would alone, so `"text".trim()` is a `strview`'s.
|
||
|
|
val object := this.left_of(callee);
|
||
|
|
val name := this.name_of(callee);
|
||
|
|
var kind := this.infer_expr(object);
|
||
|
|
if kind == bt::error {
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
if this.types.is_untyped(kind) {
|
||
|
|
kind = this.settle_expr(object);
|
||
|
|
}
|
||
|
|
|
||
|
|
val through := this.reached(kind);
|
||
|
|
val found := this.find_member(through, name);
|
||
|
|
if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::function {
|
||
|
|
# A method a program declares on a built-in type is an overload beside the
|
||
|
|
# one the compiler gives it, which is called where the arguments fit none
|
||
|
|
# of the program's. A struct, a union, or a trait is given none.
|
||
|
|
val plain := this.unalias(through);
|
||
|
|
val shaped := this.types.kind_of(plain) == ty::named && this.named_kind(plain) != dk::enum_type;
|
||
|
|
val builtin := shaped || this.types.kind_of(plain) == ty::param ? 0 : this.builtin_method(through, name);
|
||
|
|
if builtin != 0 && this.right_of(callee) == 0 && !this.fits_overload(found.decl, object, found.owner, through, args) {
|
||
|
|
return this.call_signature(node, builtin, args, 1);
|
||
|
|
}
|
||
|
|
val first := this.overloads_for(found.decl, object, found.owner, through, args);
|
||
|
|
if !this.visible(first) {
|
||
|
|
this.report_hidden(callee, first);
|
||
|
|
}
|
||
|
|
return this.call_decl(node, callee, first, object, found.owner, through, this.right_of(callee), args, expected);
|
||
|
|
}
|
||
|
|
if found.decl == 0 {
|
||
|
|
val builtin := this.builtin_method(through, name);
|
||
|
|
if builtin != 0 {
|
||
|
|
return this.call_signature(node, builtin, args, 1);
|
||
|
|
}
|
||
|
|
this.report_no_member(callee, through, name);
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
return this.call_value(node, callee, args, expected);
|
||
|
|
}
|
||
|
|
nk::member_of => {
|
||
|
|
# `x.(d)()` calls the method a description names, as though its name
|
||
|
|
# were written there.
|
||
|
|
val object := this.left_of(callee);
|
||
|
|
val kind := this.infer_expr(object);
|
||
|
|
val found := this.described_member(callee, kind);
|
||
|
|
if found.decl == 0 {
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
if this.decls.kinds.p[found.decl] == dk::function {
|
||
|
|
return this.call_decl(node, callee, found.decl, object, found.owner, this.reached(kind), 0, args, expected);
|
||
|
|
}
|
||
|
|
return this.call_value(node, callee, args, expected);
|
||
|
|
}
|
||
|
|
else => return this.call_value(node, callee, args, expected);
|
||
|
|
}
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks arguments of a call that went wrong, so what is wrong within them is
|
||
|
|
#! still reported.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
def checker::infer_args(&mut this, args: span<u32>): void {
|
||
|
|
for &each in args {
|
||
|
|
this.infer_expr(each);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument takes its type from its parameter, so it is checked once
|
||
|
|
#! the overload is chosen: an anonymous function, an array, or a range.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::deferred(&this, node: u32): bool {
|
||
|
|
val kind := this.kind_of(node);
|
||
|
|
return kind == nk::lambda || kind == nk::array || kind == nk::range;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument is an anonymous function whose parameters' types are
|
||
|
|
#! all written, which has a type of its own without its parameter's.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def checker::typed_lambda(&this, node: u32): bool {
|
||
|
|
return false if this.kind_of(node) != nk::lambda;
|
||
|
|
val params := this.left_of(node);
|
||
|
|
for i in 0..this.run_length(params) {
|
||
|
|
return false if this.left_of(this.run_at(params, i as u32)) == 0;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument is `...xs`, spreading a view into the arguments.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def checker::is_spread_arg(&this, node: u32): bool {
|
||
|
|
return this.kind_of(node) == nk::unary && this.token_kind(node) == tk::spread;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A call of a function declared by name, or of one of its overloads.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param callee The node of what is called.
|
||
|
|
#! @param first The first of the function's overloads.
|
||
|
|
#! @param object The value a method is called on, or 0.
|
||
|
|
#! @param owner The type a member is of, given its arguments, or the error
|
||
|
|
#! type.
|
||
|
|
#! @param through The type the method is reached through, for a trait's
|
||
|
|
#! method.
|
||
|
|
#! @param written The run of type arguments written, as `max::<u8>`, or 0.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param expected The type expected of what it gives, or the error type.
|
||
|
|
#! @returns The type the chosen overload gives, or the error type where none
|
||
|
|
#! fits.
|
||
|
|
def checker::call_decl(
|
||
|
|
&mut this,
|
||
|
|
node: u32,
|
||
|
|
callee: u32,
|
||
|
|
first: u32,
|
||
|
|
object: u32,
|
||
|
|
owner: u32,
|
||
|
|
through: u32,
|
||
|
|
written: u32,
|
||
|
|
args: span<u32>,
|
||
|
|
expected: u32
|
||
|
|
): u32 {
|
||
|
|
# A trait's method takes no arguments of the trait's own: its type arguments
|
||
|
|
# are its own.
|
||
|
|
var given := vector::<u32>::new();
|
||
|
|
if written != 0 {
|
||
|
|
given = this.written_args(first, this.trait_asks(first) ? bt::error : owner, written);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A generic function without overloads infers its type arguments from the
|
||
|
|
# arguments and the type expected, and checks those that need their parameter's
|
||
|
|
# type once the rest give it: anonymous functions, arrays, generic functions
|
||
|
|
# named as values, and calls that cannot infer their own without it, which are
|
||
|
|
# tried quietly first.
|
||
|
|
val alone := this.decls.overloads.p[first] == 0 && this.decls.has(first, df::generic);
|
||
|
|
val kinds := this.arg_kinds(args, true, alone);
|
||
|
|
|
||
|
|
# Between `&this` and `&mut this`, a receiver that can be written calls the
|
||
|
|
# one taking `&mut this`, and one that cannot the one taking `&this`.
|
||
|
|
val writes := object != 0 && this.receiver_writable(object);
|
||
|
|
# An `@optional` method may be missing from whatever type a bound or a trait
|
||
|
|
# reference stands for, so a call through them never reaches one.
|
||
|
|
val reached := object != 0 ? this.unalias(this.reached(this.type_at(object))) : 0;
|
||
|
|
val bounded := reached != 0 && (this.types.kind_of(reached) == ty::param || this.named_kind(reached) == dk::trait_type);
|
||
|
|
var best := checker::candidate { worst = fit::none };
|
||
|
|
var tied := false;
|
||
|
|
var tried: u32 = 0;
|
||
|
|
var single := checker::candidate { worst = fit::none };
|
||
|
|
var walk := this.decls.walk_overloads(first);
|
||
|
|
while true {
|
||
|
|
val at := walk.next();
|
||
|
|
break if at == 0;
|
||
|
|
continue if (object != 0 && !this.decls.has(at, df::method)) || (bounded && this.decls.has(at, df::optional))
|
||
|
|
|| !this.member_seen(at);
|
||
|
|
|
||
|
|
tried++;
|
||
|
|
val made := this.try_candidate(at, object, owner, through, given.view(), args, kinds.view(), expected, alone);
|
||
|
|
if made.later {
|
||
|
|
# Passing on a pack whose types are not known is only checked in each
|
||
|
|
# instance, where they are.
|
||
|
|
for i in 0..args.length {
|
||
|
|
if kinds[i] == 0 { this.infer_expr(args[i]); }
|
||
|
|
}
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
single = made;
|
||
|
|
if made.worst != fit::none && made.worst != fit::overflow {
|
||
|
|
this.keep_better(&made, &mut best, &mut tied, object != 0, writes);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if best.decl == 0 {
|
||
|
|
return this.report_no_fit(node, callee, first, &single, tried, object, args, kinds.view());
|
||
|
|
}
|
||
|
|
if tied {
|
||
|
|
var text := string::from("The call fits more than one overload of `", nothing {});
|
||
|
|
text.append(this.decl_text(first));
|
||
|
|
text.append("` as well, so convert an argument to the type of the one meant.");
|
||
|
|
this.report_text(callee, ec::ambiguous, &text);
|
||
|
|
}
|
||
|
|
|
||
|
|
return this.apply_candidate(node, callee, &best, object, args, kinds.view());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The types of a call's arguments, worked out before the function is chosen:
|
||
|
|
#! of what a spread spreads, and 0 for those that need their parameter's type
|
||
|
|
#! to be checked.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param by_name Whether a generic or overloaded function named waits for its
|
||
|
|
#! parameter's type too.
|
||
|
|
#! @param alone Whether a call is tried quietly, as one that may need its
|
||
|
|
#! parameter's type to infer its own.
|
||
|
|
#! @returns Their types.
|
||
|
|
def checker::arg_kinds(&mut this, args: span<u32>, by_name: bool, alone: bool): vector<u32> {
|
||
|
|
var kinds := vector::<u32>::new();
|
||
|
|
for &each in args {
|
||
|
|
if this.deferred(each) || (by_name && this.kind_of(each) == nk::name && (this.names_generic(each) || this.names_overloaded(each)))
|
||
|
|
|| (by_name && this.kind_of(each) == nk::path && this.path_waits(each)) {
|
||
|
|
kinds.append(0);
|
||
|
|
} else if this.is_spread_arg(each) {
|
||
|
|
kinds.append(this.infer_expr(this.left_of(each)));
|
||
|
|
} else if alone && this.kind_of(each) == nk::call {
|
||
|
|
kinds.append(this.tried(each));
|
||
|
|
} else {
|
||
|
|
kinds.append(this.infer_expr(each));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return kinds;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Keeps the better of an overload that fits and the best so far, noting where
|
||
|
|
#! neither is. Between `&this` and `&mut this`, a receiver that can be written
|
||
|
|
#! is better given to the one taking `&mut this`, and one that cannot to the
|
||
|
|
#! other.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param made The overload.
|
||
|
|
#! @param best The best so far, with no declaration where there is none.
|
||
|
|
#! @param tied Whether the best so far is tied.
|
||
|
|
#! @param receiving Whether it is a method's call on a receiver.
|
||
|
|
#! @param writes Whether the receiver can be written.
|
||
|
|
def checker::keep_better(&this, made: &checker::candidate, best: &mut checker::candidate, tied: &mut bool, receiving: bool, writes: bool): void {
|
||
|
|
var better := this.better(made, best);
|
||
|
|
if better == 0 && receiving && this.decls.has(made.decl, df::mut_this) != this.decls.has(best.decl, df::mut_this) {
|
||
|
|
better = this.decls.has(made.decl, df::mut_this) == writes ? 1 : -1;
|
||
|
|
}
|
||
|
|
# So is an argument that can be written better lent to a `&mut` parameter
|
||
|
|
# than to a `&` one, as `address(x)` of a `var x` gives a `*mut`.
|
||
|
|
if better == 0 && best.decl != 0 && made.writes != best.writes {
|
||
|
|
better = made.writes > best.writes ? 1 : -1;
|
||
|
|
}
|
||
|
|
if best.decl == 0 || better > 0 {
|
||
|
|
best = made;
|
||
|
|
tied = false;
|
||
|
|
} else if better == 0 {
|
||
|
|
tied = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether the arguments of a call fit an overload of a method, or of a
|
||
|
|
#! function where nothing is called on, without choosing one or reporting
|
||
|
|
#! anything.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param first The first of the method's overloads.
|
||
|
|
#! @param object The node of the value it is called on, or 0.
|
||
|
|
#! @param owner The type the method is of, given its arguments.
|
||
|
|
#! @param through The type it is reached through.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @returns Whether they fit one.
|
||
|
|
pub def checker::fits_overload(&mut this, first: u32, object: u32, owner: u32, through: u32, args: span<u32>): bool {
|
||
|
|
val kinds := this.arg_kinds(args, true, false);
|
||
|
|
var walk := this.decls.walk_overloads(first);
|
||
|
|
while true {
|
||
|
|
val at := walk.next();
|
||
|
|
break if at == 0;
|
||
|
|
if (object == 0 || this.decls.has(at, df::method)) && this.member_seen(at) {
|
||
|
|
val made := this.try_candidate(at, object, owner, through, span::<u32>::empty(), args, kinds.view(), bt::error, false);
|
||
|
|
return true if made.later || (made.worst != fit::none && made.worst != fit::overflow);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The type arguments written for a call, each given to the parameter at its
|
||
|
|
#! place after those of the owner, a constant parameter's worked out while
|
||
|
|
#! checking.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param first The first of the function's overloads.
|
||
|
|
#! @param owner The type a member is of, given its arguments, or the error
|
||
|
|
#! type.
|
||
|
|
#! @param written The run of type arguments written.
|
||
|
|
#! @returns The type arguments.
|
||
|
|
def checker::written_args(&mut this, first: u32, owner: u32, written: u32): vector<u32> {
|
||
|
|
var given := vector::<u32>::new();
|
||
|
|
var leading := vector::<u32>::new();
|
||
|
|
if owner != bt::error && this.types.kind_of(owner) == ty::named {
|
||
|
|
leading.extend(this.types.parts_view(owner));
|
||
|
|
}
|
||
|
|
for &arg in this.run_view(written) {
|
||
|
|
val made := this.type_arg_of(arg, first, leading.length as u32, leading.view());
|
||
|
|
given.append(made);
|
||
|
|
leading.append(made);
|
||
|
|
}
|
||
|
|
return given;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How well the call fits the overload.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The overload.
|
||
|
|
#! @param object The value a method is called on, or 0.
|
||
|
|
#! @param owner The type a member is of, given its arguments, or the error
|
||
|
|
#! type.
|
||
|
|
#! @param through The type the method is reached through, for a trait's
|
||
|
|
#! method.
|
||
|
|
#! @param given The type arguments written.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param kinds The arguments' types, 0 for one checked once the overload is
|
||
|
|
#! chosen.
|
||
|
|
#! @returns How well it fits, and what it fits as.
|
||
|
|
def checker::try_candidate(
|
||
|
|
&mut this,
|
||
|
|
decl: u32,
|
||
|
|
object: u32,
|
||
|
|
owner: u32,
|
||
|
|
through: u32,
|
||
|
|
given: span<u32>,
|
||
|
|
args: span<u32>,
|
||
|
|
kinds: span<u32>,
|
||
|
|
expected: u32,
|
||
|
|
alone: bool
|
||
|
|
): checker::candidate {
|
||
|
|
this.resolve(decl);
|
||
|
|
var made := checker::candidate { decl = decl, worst = fit::exact, arity = true };
|
||
|
|
if this.decls.has(decl, df::failed) {
|
||
|
|
made.worst = fit::none;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
val in_trait := this.trait_asks(decl);
|
||
|
|
# A default compiled for each type infers its own type arguments, in the terms
|
||
|
|
# of the trait the value implements and the value's type.
|
||
|
|
val typed := in_trait && this.decls.has(decl, df::generic) && this.typed_default(decl);
|
||
|
|
var kind := in_trait ? this.trait_method_type(decl, through) : this.decls.types.p[decl];
|
||
|
|
val skip: u32 = object != 0 && this.decls.has(decl, df::method) ? 1 : 0;
|
||
|
|
|
||
|
|
# Passing on a pack whose types are not known is checked in each instance,
|
||
|
|
# where they are, whatever is called.
|
||
|
|
for i in 0..args.length {
|
||
|
|
if this.is_spread_arg(args[i]) && kinds[i] != 0 && this.types.is_open_pack(this.reached(kinds[i])) {
|
||
|
|
made.later = true;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A generic's type arguments are its owner's, those written, and those the
|
||
|
|
# arguments give.
|
||
|
|
if this.decls.has(decl, df::generic) && (!in_trait || typed) {
|
||
|
|
# A pack spread into the arguments gives one for each of its types.
|
||
|
|
var slots := vector::<u32>::new();
|
||
|
|
for i in 0..args.length {
|
||
|
|
if this.is_spread_arg(args[i]) && kinds[i] != 0 && this.types.kind_of(kinds[i]) == ty::pack {
|
||
|
|
slots.extend(this.types.parts_view(kinds[i]));
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
slots.append(kinds[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
var inferred := this.begin_inference(decl, in_trait ? bt::error : owner, given);
|
||
|
|
if typed {
|
||
|
|
this.give_outer(decl, this.reached(through), &mut inferred);
|
||
|
|
this.unify_outer(decl, &mut inferred);
|
||
|
|
}
|
||
|
|
val count := this.types.part_count(kind);
|
||
|
|
|
||
|
|
# A pack parameter takes a type for each argument after those before it.
|
||
|
|
val last: u32 = count == 0 ? 0 : this.types.part_at(kind, count - 1);
|
||
|
|
val packed := this.types.second_of(kind) != 0 && count != 0 && this.types.is_open_pack(last);
|
||
|
|
val stop: u32 = packed ? count - 1 : count;
|
||
|
|
|
||
|
|
# A type parameter taking an argument whole, as `T` of `x: T` or of a pack,
|
||
|
|
# takes the value it is: what a reference reaches, unless `&` is written.
|
||
|
|
if slots.length == args.length {
|
||
|
|
for i in 0..slots.length {
|
||
|
|
val p := i as u32 + skip;
|
||
|
|
continue if slots[i] == 0 || p >= count;
|
||
|
|
val whole := p >= stop || this.types.kind_of(this.types.part_at(kind, p)) == ty::param;
|
||
|
|
if whole && this.takes_value(args[i], slots[i]) {
|
||
|
|
slots[i] = this.types.first_of(this.unalias(slots[i]));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for i in 0..slots.length {
|
||
|
|
val p := i as u32 + skip;
|
||
|
|
break if p >= stop;
|
||
|
|
continue if slots[i] == 0;
|
||
|
|
val pattern := this.types.part_at(kind, p);
|
||
|
|
if slots.length == args.length && this.types.kind_of(pattern) == ty::reference
|
||
|
|
&& this.referred_in_template(args[i]) {
|
||
|
|
this.unify(this.types.first_of(pattern), slots[i], &mut inferred, 0);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
this.unify(pattern, slots[i], &mut inferred, 0);
|
||
|
|
}
|
||
|
|
if packed {
|
||
|
|
this.bind_pack(last, slots.view(), (stop - skip) as u64, &mut inferred);
|
||
|
|
}
|
||
|
|
# An array literal given where an overload is chosen gives its elements'
|
||
|
|
# type, settled, as `[1, 2]` gives `T[2]` its `int`. So does an anonymous
|
||
|
|
# function whose parameters' types are written give a type parameter it's
|
||
|
|
# given for its own type, as `F` of `f: F` bounded by `invokable<...>`.
|
||
|
|
if !alone {
|
||
|
|
for i in 0..slots.length {
|
||
|
|
val p := i as u32 + skip;
|
||
|
|
break if p >= stop;
|
||
|
|
continue if slots[i] != 0;
|
||
|
|
val pattern := this.types.part_at(kind, p);
|
||
|
|
if this.kind_of(args[i]) == nk::array {
|
||
|
|
val quiet := this.tried(args[i]);
|
||
|
|
if quiet != 0 && quiet != bt::error {
|
||
|
|
this.unify(pattern, this.settle(quiet), &mut inferred, 0);
|
||
|
|
}
|
||
|
|
} else if this.typed_lambda(args[i]) && this.types.kind_of(this.unalias(pattern)) == ty::param {
|
||
|
|
val quiet := this.tried(args[i]);
|
||
|
|
if quiet != 0 && quiet != bt::error {
|
||
|
|
this.unify(pattern, quiet, &mut inferred, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if skip == 1 && count != 0 {
|
||
|
|
this.unify(this.types.part_at(kind, 0), this.types.reference(this.unalias(through), false), &mut inferred, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
# What the arguments leave, the type expected of the result gives, and then the
|
||
|
|
# bounds of what is known.
|
||
|
|
if expected != bt::error {
|
||
|
|
this.unify(this.types.first_of(kind), expected, &mut inferred, 0);
|
||
|
|
}
|
||
|
|
this.unify_bounds(&mut inferred, 0);
|
||
|
|
|
||
|
|
# The arguments checked once their parameter's type is known, in the order
|
||
|
|
# they are written, each giving what it can to the next.
|
||
|
|
if alone && slots.length == args.length {
|
||
|
|
for i in 0..slots.length {
|
||
|
|
val p := i as u32 + skip;
|
||
|
|
break if p >= stop;
|
||
|
|
continue if slots[i] != 0;
|
||
|
|
val pattern := this.types.part_at(kind, p);
|
||
|
|
this.infer_later(args[i], pattern, &mut inferred);
|
||
|
|
this.unify_bounds(&mut inferred, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A function named for a type parameter bounded by `invokable<S>`, which a
|
||
|
|
# call as `S` asks would take though its type is not `S`, gives the
|
||
|
|
# parameter `S`, and is called as `S` asks by a function made for it.
|
||
|
|
if slots.length == args.length {
|
||
|
|
for i in 0..slots.length {
|
||
|
|
val p := i as u32 + skip;
|
||
|
|
break if p >= stop;
|
||
|
|
continue if slots[i] == 0;
|
||
|
|
this.adapt_bound(args[i], slots[i], this.types.part_at(kind, p), &mut inferred);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
made.infers = this.unbound(&inferred);
|
||
|
|
if made.infers != 0 {
|
||
|
|
made.worst = fit::none;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
kind = this.apply(kind, &inferred);
|
||
|
|
made.binds = this.types.pack(inferred.binds.view(), 0);
|
||
|
|
made.generic = true;
|
||
|
|
}
|
||
|
|
made.kind = kind;
|
||
|
|
|
||
|
|
# How many arguments it takes: those before its first default, up to all of
|
||
|
|
# its parameters, or more where its last is variadic. A pack spread with
|
||
|
|
# `...` gives an argument for each of its types.
|
||
|
|
val count := this.types.part_count(kind);
|
||
|
|
val variadic := this.types.second_of(kind) != 0;
|
||
|
|
# A variadic parameter, or a pack, takes any number of arguments, so it is
|
||
|
|
# not among those a call must give.
|
||
|
|
val takes_many: u32 = this.decls.has(decl, df::variadic) ? 1 : 0;
|
||
|
|
val written_required := this.required_params(decl);
|
||
|
|
val required: u32 = written_required >= skip + takes_many ? written_required - skip - takes_many : 0;
|
||
|
|
val most := count - skip;
|
||
|
|
val argc := this.given_count(args, kinds);
|
||
|
|
if argc < required || (!variadic && argc > most) {
|
||
|
|
made.arity = false;
|
||
|
|
made.worst = fit::none;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The receiver: `&mut this` is called on what can be written.
|
||
|
|
if skip == 1 && this.decls.has(decl, df::mut_this) && !this.receiver_writable(object) {
|
||
|
|
made.worst = fit::none;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
var p := skip;
|
||
|
|
for i in 0..args.length {
|
||
|
|
if this.spreads_pack(args[i], kinds[i]) {
|
||
|
|
val none := constant {};
|
||
|
|
for j in 0..this.types.part_count(kinds[i]) {
|
||
|
|
val param := this.types.part_at(kind, p >= count ? count - 1 : p);
|
||
|
|
val how := this.spread_fits(kinds[i], j as u32, kind, p)
|
||
|
|
? this.fits(this.types.part_at(kinds[i], j as u32), param, &none, false, 0)
|
||
|
|
: fit::none;
|
||
|
|
made.worst = (how as u8) > (made.worst as u8) ? how : made.worst;
|
||
|
|
made.total += how as u32;
|
||
|
|
p++;
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
val param := this.types.part_at(kind, p >= count ? count - 1 : p);
|
||
|
|
val how := this.arg_fit(args[i], kinds[i], param, variadic && p >= count - 1);
|
||
|
|
made.worst = (how as u8) > (made.worst as u8) ? how : made.worst;
|
||
|
|
made.total += how as u32;
|
||
|
|
if this.types.kind_of(param) == ty::reference && this.types.second_of(param) != 0 {
|
||
|
|
made.writes++;
|
||
|
|
}
|
||
|
|
p++;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a value of a pack spread into a call goes at a parameter's place: any
|
||
|
|
#! does, but for the last of a variadic function's pack, which within it is a
|
||
|
|
#! view of the rest it was given, so only the parameter taking the rest takes it.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param pack The pack spread.
|
||
|
|
#! @param j The value's place in the pack.
|
||
|
|
#! @param kind The function type called.
|
||
|
|
#! @param p The parameter's place.
|
||
|
|
#! @returns Whether it goes there.
|
||
|
|
def checker::spread_fits(&this, pack: u32, j: u32, kind: u32, p: u32): bool {
|
||
|
|
return true if this.types.second_of(pack) == 0 || j + 1 != this.types.part_count(pack);
|
||
|
|
return this.types.second_of(kind) != 0 && p + 1 == this.types.part_count(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument spreads a pack whose types are known into the call.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::spreads_pack(&this, node: u32, kind: u32): bool {
|
||
|
|
return kind != 0 && this.is_spread_arg(node) && this.types.kind_of(kind) == ty::pack
|
||
|
|
&& !this.types.is_open_pack(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many arguments a call gives, each of a pack spread into it counted.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param kinds Their types.
|
||
|
|
#! @returns How many values they give.
|
||
|
|
def checker::given_count(&this, args: span<u32>, kinds: span<u32>): u32 {
|
||
|
|
var count: u32 = 0;
|
||
|
|
for i in 0..args.length {
|
||
|
|
count += this.spreads_pack(args[i], kinds[i]) ? this.types.part_count(kinds[i]) : 1;
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many parameters a call must give arguments for: those before the first
|
||
|
|
#! with a default value.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The function's declaration.
|
||
|
|
#! @returns How many.
|
||
|
|
def checker::required_params(&this, decl: u32): u32 {
|
||
|
|
val syntax := &this.units[this.decls.files.p[decl]].syntax;
|
||
|
|
val node: u32 = this.decls.nodes.p[decl];
|
||
|
|
return 1 if this.decls.has(decl, df::accessor);
|
||
|
|
|
||
|
|
val record := syntax.node_lefts[node];
|
||
|
|
val params := syntax.run_view(syntax.extras[record as u64 + function_slot::params as u64]);
|
||
|
|
for i in 0..params.length {
|
||
|
|
val values := syntax.node_rights[params[i]];
|
||
|
|
if syntax.extras[values] != 0 {
|
||
|
|
return i as u32;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return params.length as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a receiver can be written, for a method taking `&mut this`: what can
|
||
|
|
#! be written, a `&mut` reference, or a new value.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param object The receiver's node.
|
||
|
|
#! @returns Whether it can be.
|
||
|
|
def checker::receiver_writable(&mut this, object: u32): bool {
|
||
|
|
val kind := this.type_at(object);
|
||
|
|
val plain := this.unalias(kind);
|
||
|
|
if this.types.kind_of(plain) == ty::reference || this.types.kind_of(plain) == ty::pointer {
|
||
|
|
return this.types.second_of(plain) != 0;
|
||
|
|
}
|
||
|
|
return this.is_writable(object) || !this.is_place(object);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How an argument fits its parameter, taking a reference to it for a `&`
|
||
|
|
#! parameter where it is no reference itself.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @param param The parameter's type.
|
||
|
|
#! @param variadic Whether the parameter takes any number of values, each of
|
||
|
|
#! its element's type.
|
||
|
|
#! @returns How it fits.
|
||
|
|
def checker::arg_fit(&mut this, node: u32, kind: u32, param: u32, variadic: bool): fit {
|
||
|
|
if kind == 0 {
|
||
|
|
val plain := this.unalias(param);
|
||
|
|
val tkd := this.types.kind_of(plain);
|
||
|
|
# An array literal is a fixed array, which it is better given as than viewed.
|
||
|
|
return fit::exact if tkd == ty::fixed && this.kind_of(node) == nk::array;
|
||
|
|
# A range is std's `range`, of the integers its ends are given as.
|
||
|
|
if this.kind_of(node) == nk::range {
|
||
|
|
return this.is_std(plain, word::range) || tkd == ty::param || param == bt::error ? fit::convert : fit::none;
|
||
|
|
}
|
||
|
|
return fit::convert if tkd == ty::function || tkd == ty::fixed || this.is_std(plain, word::span)
|
||
|
|
|| tkd == ty::param || param == bt::error
|
||
|
|
|| this.kind_of(node) == nk::lambda && this.is_std(plain, word::cfn);
|
||
|
|
return fit::none;
|
||
|
|
}
|
||
|
|
|
||
|
|
if this.is_spread_arg(node) {
|
||
|
|
return variadic ? fit::convert : fit::none;
|
||
|
|
}
|
||
|
|
|
||
|
|
val value := this.value_at(node);
|
||
|
|
val known := this.is_known(node);
|
||
|
|
if this.types.kind_of(param) == ty::reference && this.lent_whole(node, kind, param) {
|
||
|
|
# A value given for a reference to a trait is referred to where it satisfies it.
|
||
|
|
val referent := this.types.first_of(param);
|
||
|
|
if this.named_kind(this.unalias(referent)) == dk::trait_type {
|
||
|
|
return this.satisfies(this.settle(kind), this.unalias(referent)) ? fit::convert : fit::none;
|
||
|
|
}
|
||
|
|
val how := this.fits(kind, referent, &value, known, 0);
|
||
|
|
return how if how == fit::none || how == fit::overflow;
|
||
|
|
if this.types.second_of(param) != 0 && this.is_place(node) && !this.is_writable(node) {
|
||
|
|
return fit::none;
|
||
|
|
}
|
||
|
|
# A constant fits a reference to the type it is alone best, as it fits that
|
||
|
|
# type best, so `2` is given to a `&i64` before a `&f64`.
|
||
|
|
return fit::exact if this.types.is_untyped(kind) && this.settle(kind) == referent;
|
||
|
|
return how == fit::exact ? fit::exact : fit::convert;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A constant fits the type it is where nothing else is expected best.
|
||
|
|
if this.types.is_untyped(kind) && this.settle(kind) == param {
|
||
|
|
return fit::exact;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A reference is read through to its value wherever no reference is wanted, so one
|
||
|
|
# referring to the very type fits better than one whose value converts further.
|
||
|
|
if this.types.kind_of(kind) == ty::reference && this.types.kind_of(param) != ty::reference
|
||
|
|
&& this.unalias(this.reached(kind)) == this.unalias(param) {
|
||
|
|
return fit::read;
|
||
|
|
}
|
||
|
|
return fit::convert if this.calls_back(node, kind, param) || this.lends_pointer(node, kind, param);
|
||
|
|
val how := this.fits(kind, param, &value, known, 0);
|
||
|
|
return how if how != fit::none;
|
||
|
|
return this.adapts(node, kind, param) ? fit::convert : fit::none;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument names a function of the program where C's address of one
|
||
|
|
#! of its type is expected, which it is given as the entry C calls it back
|
||
|
|
#! through.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @param param The parameter's type.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::calls_back(&mut this, node: u32, kind: u32, param: u32): bool {
|
||
|
|
val plain := this.unalias(param);
|
||
|
|
return false if !this.is_std(plain, word::cfn) || this.types.part_count(plain) != 1;
|
||
|
|
return false if this.kind_of(node) != nk::name && this.kind_of(node) != nk::path;
|
||
|
|
val ref := this.ref_at(node);
|
||
|
|
return false if ref == 0 || (ref & (ref_local | ref_type)) != 0;
|
||
|
|
return this.decls.kinds.p[ref] == dk::function && !this.decls.has(ref, df::foreign)
|
||
|
|
&& this.unalias(kind) == this.unalias(this.types.part_at(plain, 0));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Below zero where `a` fits worse than `b`, zero where they fit as well, and
|
||
|
|
#! above zero where `a` fits better: by its worst argument, then all of them,
|
||
|
|
#! and then a non-generic overload before a generic one.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param a One overload.
|
||
|
|
#! @param b Another.
|
||
|
|
#! @returns Their order.
|
||
|
|
def checker::better(&this, a: &checker::candidate, b: &checker::candidate): i32 {
|
||
|
|
return ((a.worst as u8) < (b.worst as u8)) ? 1 : -1 if a.worst != b.worst;
|
||
|
|
return (a.total < b.total) ? 1 : -1 if a.total != b.total;
|
||
|
|
return (b.generic ? 1 : -1) if a.generic != b.generic;
|
||
|
|
# One taking each argument as its own parameter is better than one gathering
|
||
|
|
# them.
|
||
|
|
val a_many := a.decl != 0 && this.decls.has(a.decl, df::variadic);
|
||
|
|
val b_many := b.decl != 0 && this.decls.has(b.decl, df::variadic);
|
||
|
|
return (b_many ? 1 : -1) if a_many != b_many;
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reports a call no overload takes, checking its arguments as the only
|
||
|
|
#! overload's, where there is one, so each argument that does not fit is
|
||
|
|
#! reported where it is.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param callee The node of what is called.
|
||
|
|
#! @param first The first of the function's overloads.
|
||
|
|
#! @param single The only overload where there is one, which the arguments are
|
||
|
|
#! checked against.
|
||
|
|
#! @param tried How many overloads were tried.
|
||
|
|
#! @param object The value a method is called on, or 0.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param kinds Their types.
|
||
|
|
#! @returns The error type, or what the only overload gives, once its arguments
|
||
|
|
#! are checked as its own.
|
||
|
|
def checker::report_no_fit(
|
||
|
|
&mut this,
|
||
|
|
node: u32,
|
||
|
|
callee: u32,
|
||
|
|
first: u32,
|
||
|
|
single: &checker::candidate,
|
||
|
|
tried: u32,
|
||
|
|
object: u32,
|
||
|
|
args: span<u32>,
|
||
|
|
kinds: span<u32>
|
||
|
|
): u32 {
|
||
|
|
for i in 0..args.length {
|
||
|
|
if kinds[i] == 0 {
|
||
|
|
this.infer_expr(args[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return bt::error if this.decls.has(first, df::failed);
|
||
|
|
|
||
|
|
if tried != 1 {
|
||
|
|
var text := string::from("No overload of `", nothing {});
|
||
|
|
text.append(this.decl_text(first));
|
||
|
|
text.append("` takes these arguments.");
|
||
|
|
this.report_text(callee, ec::arguments, &text);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
# An argument already in error gives nothing to infer from, which it was reported for.
|
||
|
|
for &each in args {
|
||
|
|
return bt::error if this.type_at(this.is_spread_arg(each) ? this.left_of(each) : each) == bt::error;
|
||
|
|
}
|
||
|
|
if single.infers != 0 {
|
||
|
|
var text := string::from(this.decls.has(single.infers, df::valued) ? "Nothing gives the value of `" : "Nothing gives the type of `", nothing {});
|
||
|
|
text.append(this.decl_text(single.infers));
|
||
|
|
text.append("`, so give the type arguments, as `");
|
||
|
|
text.append(this.decl_text(first));
|
||
|
|
text.append("::<...>`.");
|
||
|
|
this.report_text(callee, ec::generic, &text);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
if !single.arity {
|
||
|
|
# A view spread where no variadic parameter takes it gives no arguments.
|
||
|
|
if this.reports_spread(single.kind, object != 0 && this.decls.has(single.decl, df::method) ? 1 : 0, args, kinds) {
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
val skip: u32 = object != 0 && this.decls.has(single.decl, df::method) ? 1 : 0;
|
||
|
|
var text := string::from("`", nothing {});
|
||
|
|
text.append(this.decl_text(first));
|
||
|
|
val words := $"` takes {this.types.part_count(single.kind) - skip} arguments, but is given {args.length}.";
|
||
|
|
text.append(words.view());
|
||
|
|
this.report_text(callee, ec::arguments, &text);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
if object != 0 && this.decls.has(single.decl, df::mut_this) && !this.receiver_writable(object) {
|
||
|
|
this.report(object, ec::assignable,
|
||
|
|
"The method takes `&mut this`, so it is called on what can be written: a `var`, or what a `&mut` reaches.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The only overload: each argument is converted, reporting what does not fit.
|
||
|
|
return this.apply_candidate(node, callee, single, object, args, kinds);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Converts each argument of a call to its parameter, taking references where
|
||
|
|
#! the parameter is one, and records what the call calls.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param callee The node of what is called.
|
||
|
|
#! @param chosen The overload chosen.
|
||
|
|
#! @param object The value a method is called on, or 0.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param kinds Their types, 0 for one checked now.
|
||
|
|
#! @returns The type the overload gives.
|
||
|
|
def checker::apply_candidate(
|
||
|
|
&mut this,
|
||
|
|
node: u32,
|
||
|
|
callee: u32,
|
||
|
|
chosen: &checker::candidate,
|
||
|
|
object: u32,
|
||
|
|
args: span<u32>,
|
||
|
|
kinds: span<u32>
|
||
|
|
): u32 {
|
||
|
|
val kind := chosen.kind;
|
||
|
|
val count := this.types.part_count(kind);
|
||
|
|
val variadic := this.types.second_of(kind) != 0;
|
||
|
|
val skip: u32 = object != 0 && this.decls.has(chosen.decl, df::method) ? 1 : 0;
|
||
|
|
return bt::error if this.reports_spread(kind, skip, args, kinds);
|
||
|
|
this.pass_args(kind, args, kinds, skip, variadic);
|
||
|
|
|
||
|
|
if skip == 1 && count != 0 {
|
||
|
|
this.pass_receiver(object, this.types.part_at(kind, 0));
|
||
|
|
}
|
||
|
|
|
||
|
|
this.set_ref(callee, chosen.decl);
|
||
|
|
this.set_ref(node, chosen.decl);
|
||
|
|
if this.decls.has(chosen.decl, df::intrinsic) {
|
||
|
|
this.know_switch(node, chosen.decl);
|
||
|
|
}
|
||
|
|
# A default compiled for each type, called on a value of a type that is known,
|
||
|
|
# is that type's instance of it.
|
||
|
|
if object != 0 && this.typed_default(chosen.decl) {
|
||
|
|
this.call_default(node, callee, chosen, object);
|
||
|
|
return this.types.first_of(kind);
|
||
|
|
}
|
||
|
|
# An instance whose arguments break its bounds is never checked, since what
|
||
|
|
# is wrong is reported here.
|
||
|
|
if chosen.generic && chosen.binds != 0 {
|
||
|
|
var binds := this.types.parts_of(chosen.binds);
|
||
|
|
if this.check_args_bounds(chosen.decl, this.decls.params.p[chosen.decl], binds.view(), callee) {
|
||
|
|
val made := this.instance_of(chosen.decl, binds.view(), node);
|
||
|
|
if made != 0 {
|
||
|
|
this.called.set(this.result_key(node), made);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return this.types.first_of(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Records the value of a call of `checking` or `contracts`, which the options
|
||
|
|
#! the program is checked with decide, so an `if const` of one is decided while
|
||
|
|
#! checking, and only the branch it takes is checked.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param decl The intrinsic called.
|
||
|
|
def checker::know_switch(&mut this, node: u32, decl: u32): void {
|
||
|
|
val name := this.names.text(this.decls.names.p[decl]);
|
||
|
|
if name == "checking" {
|
||
|
|
this.set_value(node, constant::truth(!this.checks_off));
|
||
|
|
} else if name == "contracts" {
|
||
|
|
this.set_value(node, constant::truth(!this.contracts_off));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Records the instance a call of a default compiled for each type calls: the
|
||
|
|
#! one the type of the value it is called on takes, given the type arguments
|
||
|
|
#! the call infers, where they hold no type parameter. Its own parameters'
|
||
|
|
#! bounds are checked first, in the terms of the trait the value implements.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param callee The callee's node, where a bound it breaks is reported.
|
||
|
|
#! @param chosen The default, as the call fits it.
|
||
|
|
#! @param object The node of the value it is called on.
|
||
|
|
def checker::call_default(&mut this, node: u32, callee: u32, chosen: &checker::candidate, object: u32): void {
|
||
|
|
val decl := chosen.decl;
|
||
|
|
val holder := this.enclosing_trait(decl);
|
||
|
|
val plain := this.unalias(this.reached(this.type_at(object)));
|
||
|
|
return if this.types.is_open(plain) || this.named_kind(plain) == dk::trait_type;
|
||
|
|
val trait_kind := this.trait_reached(decl, plain);
|
||
|
|
|
||
|
|
var own := vector::<u32>::new();
|
||
|
|
if chosen.binds != 0 {
|
||
|
|
own = this.types.parts_of(chosen.binds);
|
||
|
|
}
|
||
|
|
# Each parameter, the trait's, `self`, and its own, is checked against its
|
||
|
|
# bounds with the others given.
|
||
|
|
var params := this.decls.list_of(this.decls.params.p[holder]);
|
||
|
|
var args := this.types.parts_of(trait_kind);
|
||
|
|
args.resize(params.length, bt::error);
|
||
|
|
params.append(this.self_decl(holder));
|
||
|
|
args.append(plain);
|
||
|
|
params.extend(this.decls.list_view(this.decls.params.p[decl]));
|
||
|
|
args.extend(own.view());
|
||
|
|
return if !this.check_bounds_of(decl, params.view(), args.view(), callee);
|
||
|
|
return if !this.meets_outer(decl, params.view(), args.view(), callee);
|
||
|
|
|
||
|
|
val made := this.default_instance(decl, trait_kind, plain, own.view());
|
||
|
|
if made != 0 {
|
||
|
|
this.called.set(this.result_key(node), made);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks that the trait's parameters and `self` meet what a trait's method
|
||
|
|
#! asks of them, as `where self: backward_iterator<T>`, given the arguments the
|
||
|
|
#! call gives them and its own parameters.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The method's declaration.
|
||
|
|
#! @param params The parameters the bounds may name: the trait's, `self`, and
|
||
|
|
#! the method's own.
|
||
|
|
#! @param args The type each is given.
|
||
|
|
#! @param node The node a note is placed at.
|
||
|
|
#! @returns Whether they meet them.
|
||
|
|
def checker::meets_outer(&mut this, decl: u32, params: span<u32>, args: span<u32>, node: u32): bool {
|
||
|
|
val list := this.outer_bounds(decl);
|
||
|
|
var i: u32 = 0;
|
||
|
|
while i + 1 < this.decls.list_length(list) {
|
||
|
|
val param := this.decls.list_at(list, i);
|
||
|
|
val bound := this.substitute(this.decls.list_at(list, i + 1), params, args);
|
||
|
|
i += 2;
|
||
|
|
var given: u32 = bt::error;
|
||
|
|
for k in 0..params.length {
|
||
|
|
given = params[k] == param && k < args.length ? args[k] : given;
|
||
|
|
}
|
||
|
|
continue if given == bt::error || this.satisfies(given, bound);
|
||
|
|
|
||
|
|
var text := string::from("`", nothing {});
|
||
|
|
val named_arg := this.describe(given);
|
||
|
|
text.append(named_arg.view());
|
||
|
|
text.append("` does not implement `");
|
||
|
|
val named := this.describe(bound);
|
||
|
|
text.append(named.view());
|
||
|
|
text.append("`, which `");
|
||
|
|
text.append(this.decl_text(decl));
|
||
|
|
text.append("` asks of `");
|
||
|
|
text.append(this.decl_text(param));
|
||
|
|
text.append("`.");
|
||
|
|
this.report_text(node, ec::bound, &text);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Infers a trait's default's own type parameters from what it asks of the
|
||
|
|
#! trait's parameters and `self`, as `X` of `where self: iterator<&X>` from the
|
||
|
|
#! iterator it is called on.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param method The default's declaration.
|
||
|
|
#! @param into What is inferred, whose outer terms are given.
|
||
|
|
def checker::unify_outer(&mut this, method: u32, into: &mut checker::inferred): void {
|
||
|
|
val list := this.outer_bounds(method);
|
||
|
|
var i: u32 = 0;
|
||
|
|
while i + 1 < this.decls.list_length(list) {
|
||
|
|
val param := this.decls.list_at(list, i);
|
||
|
|
val bound := this.outer_terms(this.decls.list_at(list, i + 1), into);
|
||
|
|
i += 2;
|
||
|
|
var given: u32 = bt::error;
|
||
|
|
for k in 0..into.outer.length {
|
||
|
|
given = into.outer[k] == param ? into.given[k] : given;
|
||
|
|
}
|
||
|
|
if given != bt::error && this.types.is_open(bound) {
|
||
|
|
this.unify_trait(bound, given, into, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reports a view spread with `...` where no variadic parameter takes it, since
|
||
|
|
#! only one takes a view of the arguments it gathers.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The function type called.
|
||
|
|
#! @param skip How many parameters come before the first argument.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param kinds Their types, 0 for one checked later.
|
||
|
|
#! @returns Whether one was reported.
|
||
|
|
def checker::reports_spread(&mut this, kind: u32, skip: u32, args: span<u32>, kinds: span<u32>): bool {
|
||
|
|
val count := this.types.part_count(kind);
|
||
|
|
val variadic := this.types.second_of(kind) != 0 && count != 0;
|
||
|
|
var p := skip;
|
||
|
|
for i in 0..args.length {
|
||
|
|
if this.spreads_pack(args[i], kinds[i]) {
|
||
|
|
p += this.types.part_count(kinds[i]);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
# What follows a pack whose types are not known is at no place known yet.
|
||
|
|
return false if this.is_spread_arg(args[i]) && kinds[i] != 0 && this.types.is_open_pack(this.reached(kinds[i]));
|
||
|
|
if this.is_spread_arg(args[i]) && !(variadic && p + 1 >= count) {
|
||
|
|
this.report(args[i], ec::arguments, "`...` gives a view only for a variadic parameter, or a pack for the parameters it fills.");
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
p++;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Converts an argument to its parameter.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @param param The parameter's type.
|
||
|
|
#! @param variadic Whether the parameter takes any number of values.
|
||
|
|
def checker::pass_arg(&mut this, node: u32, kind: u32, param: u32, variadic: bool): void {
|
||
|
|
if kind == 0 {
|
||
|
|
this.check_expr(node, param);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# `...xs` gives a view or a fixed array of the variadic parameter's elements.
|
||
|
|
if this.is_spread_arg(node) {
|
||
|
|
val operand := this.left_of(node);
|
||
|
|
val plain := this.unalias(this.reached(kind));
|
||
|
|
if this.types.kind_of(plain) == ty::fixed && this.types.first_of(plain) == param {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.convert(operand, this.span_of(param));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if this.types.kind_of(param) == ty::reference && this.lent_whole(node, kind, param) {
|
||
|
|
val referent := this.types.first_of(param);
|
||
|
|
if this.types.second_of(param) != 0 {
|
||
|
|
this.check_unlocked(node);
|
||
|
|
}
|
||
|
|
if this.named_kind(this.unalias(referent)) == dk::trait_type {
|
||
|
|
if !this.satisfies(this.settle(kind), this.unalias(referent)) {
|
||
|
|
this.report_mismatch(node, kind, param);
|
||
|
|
}
|
||
|
|
this.set_flag(node, rf::borrowed);
|
||
|
|
this.set_convert(node, param);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.convert(node, referent);
|
||
|
|
if this.types.second_of(param) != 0 && this.is_place(node) && !this.is_writable(node) {
|
||
|
|
this.report(node, ec::assignable,
|
||
|
|
"The parameter is `&mut`, so it takes what can be written: a `var`, a mutable field or element, or a new value.");
|
||
|
|
}
|
||
|
|
this.set_flag(node, rf::borrowed);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.calls_back(node, kind, param) {
|
||
|
|
this.clear_flag(node, rf::checked);
|
||
|
|
this.check_expr(node, param);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
# A reference written as one is a pointer for a pointer parameter, at a value
|
||
|
|
# that lasts as long as the call, whether a place or a new value.
|
||
|
|
if this.lends_pointer(node, kind, param) {
|
||
|
|
# A reference to a constant is checked again as one to the type pointed at.
|
||
|
|
if this.types.is_untyped(this.types.first_of(this.unalias(kind))) {
|
||
|
|
this.clear_flag(node, rf::checked);
|
||
|
|
this.hinted_expr(node, param);
|
||
|
|
}
|
||
|
|
this.set_convert(node, param);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.convert(node, param);
|
||
|
|
this.check_owned(node, param);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument is lent whole for a reference parameter, a reference to
|
||
|
|
#! it taken: one that is no reference, one that is a reference where the
|
||
|
|
#! parameter refers to a reference of its type, as `contains` of a
|
||
|
|
#! `vector<&mark>` takes a `&&mark`, and one whose template lent it so.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @param param The parameter's type, a reference.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def checker::lent_whole(&mut this, node: u32, kind: u32, param: u32): bool {
|
||
|
|
return true if this.types.kind_of(kind) != ty::reference || this.referred_in_template(node);
|
||
|
|
return this.unalias(kind) == this.unalias(this.types.first_of(param));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument checked for an instance was of a type parameter's type in
|
||
|
|
#! its template, which referred to it for a reference parameter. The instance
|
||
|
|
#! refers to it the same way, even where the type it gives is a reference, so a
|
||
|
|
#! call infers what the template's did: `move(value)` moves the reference
|
||
|
|
#! itself. Where the template's parameter was the type parameter itself, as `T`
|
||
|
|
#! of `invokable<def(T): bool>`, the template gave the value, and so does the
|
||
|
|
#! instance, though `T` is a reference there.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @returns Whether it was.
|
||
|
|
def checker::referred_in_template(&mut this, node: u32): bool {
|
||
|
|
return false if this.at.instance == 0;
|
||
|
|
|
||
|
|
val held: rtable = this.tables.p[this.units[this.at.file].table];
|
||
|
|
return false if node < held.base || node - held.base >= held.count;
|
||
|
|
val at := held.offset as u64 + (node - held.base) as u64;
|
||
|
|
val written: u32 = this.r_types.p[at];
|
||
|
|
return written != 0 && this.types.kind_of(this.unalias(written)) == ty::param
|
||
|
|
&& (this.r_flags.p[at] & rf::borrowed as u16) != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a value whose type is a reference is taken as what it reaches, as a
|
||
|
|
#! binding, a lambda's result, and inference take one: unless `&` is written.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The value's node, which notes the decision.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
pub def checker::takes_value(&mut this, node: u32, kind: u32): bool {
|
||
|
|
return this.decide(node, rf::valued, !this.lent(node) && this.has_value(kind));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a type is a reference whose referent has a value to take: anything
|
||
|
|
#! but a trait, which no value is of, so a reference to one is always taken as
|
||
|
|
#! the reference it is.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
pub def checker::has_value(&mut this, kind: u32): bool {
|
||
|
|
val plain := this.unalias(kind);
|
||
|
|
return this.types.kind_of(plain) == ty::reference && this.named_kind(this.unalias(this.types.first_of(plain))) != dk::trait_type;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Decides how a node takes a reference, noting it on the node with the flag:
|
||
|
|
#! within an instance, as the template's node did, so an instance means what
|
||
|
|
#! its template does even where a type parameter is a reference there, and
|
||
|
|
#! otherwise as `holds` says.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node.
|
||
|
|
#! @param flag The flag noting it.
|
||
|
|
#! @param holds What the types say.
|
||
|
|
#! @returns What is decided.
|
||
|
|
pub def checker::decide(&mut this, node: u32, flag: rf, holds: bool): bool {
|
||
|
|
val earlier := this.template_flag(node, flag);
|
||
|
|
val made := earlier >= 0 ? earlier == 1 : holds;
|
||
|
|
if made {
|
||
|
|
this.set_flag(node, flag);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a node had a flag where its template was checked, within an
|
||
|
|
#! instance: 1 where it had, 0 where it had not, and -1 outside an instance.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node.
|
||
|
|
#! @param flag The flag.
|
||
|
|
#! @returns Whether it had.
|
||
|
|
def checker::template_flag(&mut this, node: u32, flag: rf): i32 {
|
||
|
|
return -1 if this.at.instance == 0;
|
||
|
|
val held: rtable = this.tables.p[this.units[this.at.file].table];
|
||
|
|
return -1 if node < held.base || node - held.base >= held.count;
|
||
|
|
val at := held.offset as u64 + (node - held.base) as u64;
|
||
|
|
return (this.r_flags.p[at] & flag as u16) != 0 ? 1 : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Takes a reference to the value a method is called on, where it is not one.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param object The receiver's node.
|
||
|
|
#! @param param The type of `this`.
|
||
|
|
def checker::pass_receiver(&mut this, object: u32, param: u32): void {
|
||
|
|
if this.types.second_of(param) != 0 {
|
||
|
|
this.check_unlocked(object);
|
||
|
|
}
|
||
|
|
val kind := this.type_at(object);
|
||
|
|
val plain := this.unalias(kind);
|
||
|
|
if this.types.kind_of(plain) == ty::reference || this.types.kind_of(plain) == ty::pointer {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.set_flag(object, rf::borrowed);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A call of a function value: a local, a field, or anything giving a function,
|
||
|
|
#! or of a value of another type through its `invoke`, as `f(x)` is `f.invoke(x)`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param callee The node of the function value.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param expected The type expected of what it gives, or the error type.
|
||
|
|
#! @returns The type the function gives, or the error type.
|
||
|
|
def checker::call_value(&mut this, node: u32, callee: u32, args: span<u32>, expected: u32): u32 {
|
||
|
|
val kind := this.infer_expr(callee);
|
||
|
|
if kind == bt::error {
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
var plain := this.unalias(this.read_type(kind));
|
||
|
|
if this.is_std(plain, word::cfn) {
|
||
|
|
plain = this.unalias(this.types.part_at(plain, 0));
|
||
|
|
}
|
||
|
|
# A value of a type parameter that is a `signature` is called as the function
|
||
|
|
# it is in each instance, `def(F::params...): F::result`.
|
||
|
|
val sig := this.std_decl(word::signature);
|
||
|
|
if this.types.kind_of(plain) == ty::param && sig != 0 && this.satisfies(plain, this.decls.types.p[sig]) {
|
||
|
|
var parts := vector::<u32>::new();
|
||
|
|
parts.append(this.projected(plain, this.trait_member(sig, word::params), this.decls.types.p[sig]));
|
||
|
|
plain = this.types.function(parts.view(), this.projected(plain, this.trait_member(sig, word::result), this.decls.types.p[sig]), true);
|
||
|
|
}
|
||
|
|
if this.types.kind_of(plain) != ty::function {
|
||
|
|
# A value of a type with an `invoke` is called through it, by the rules of a
|
||
|
|
# method's call, the value as `this`. The call names the method itself, since
|
||
|
|
# the callee still names the value.
|
||
|
|
val through := this.reached(kind);
|
||
|
|
val found := this.find_member(through, word::invoke);
|
||
|
|
if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::function {
|
||
|
|
if !this.visible(found.decl) {
|
||
|
|
this.report_hidden(callee, found.decl);
|
||
|
|
}
|
||
|
|
this.set_flag(node, rf::invokes);
|
||
|
|
return this.call_decl(node, node, found.decl, callee, found.owner, through, 0, args, expected);
|
||
|
|
}
|
||
|
|
# A property is read as a field is, as `m.is_none`, which `()` would call.
|
||
|
|
if this.kind_of(callee) == nk::member {
|
||
|
|
val owner := this.reached(this.type_at(this.left_of(callee)));
|
||
|
|
val member := this.find_member(owner, this.name_of(callee));
|
||
|
|
if member.decl != 0 && this.decls.kinds.p[member.decl] == dk::property {
|
||
|
|
var text := string::from("`", nothing {});
|
||
|
|
text.append(this.names.text(this.name_of(callee)));
|
||
|
|
text.append("` is a property, read as a field is, without `()`.");
|
||
|
|
this.report_text(callee, ec::operands, &text);
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.report_operand(callee, kind, "Only a function is called, or a value of a type with an `invoke`, as `invokable` asks for.");
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
return this.call_signature(node, plain, args, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks a call's arguments against a function type, from its parameter at
|
||
|
|
#! `skip`, as a method's after `this`. A variadic last parameter takes every
|
||
|
|
#! argument after those before it, or a view `...xs` of them, and a pack spread
|
||
|
|
#! with `...args` gives an argument for each of its types.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param kind The function type.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param skip How many parameters come before the first argument.
|
||
|
|
#! @returns The type the function gives, or the error type.
|
||
|
|
def checker::call_signature(&mut this, node: u32, kind: u32, args: span<u32>, skip: u32): u32 {
|
||
|
|
val count := this.types.part_count(kind);
|
||
|
|
val variadic := this.types.second_of(kind) != 0 && count != 0;
|
||
|
|
val kinds := this.arg_kinds(args, false, false);
|
||
|
|
|
||
|
|
# Passing on a pack whose types are not known, or calling a function whose
|
||
|
|
# parameters are one, is checked in each instance, where they are known.
|
||
|
|
var later := variadic && this.types.is_open_pack(this.types.part_at(kind, count - 1));
|
||
|
|
for i in 0..args.length {
|
||
|
|
later = later || (this.is_spread_arg(args[i]) && kinds[i] != 0 && this.types.is_open_pack(this.reached(kinds[i])));
|
||
|
|
}
|
||
|
|
if later {
|
||
|
|
for i in 0..args.length {
|
||
|
|
if kinds[i] == 0 { this.infer_expr(args[i]); }
|
||
|
|
}
|
||
|
|
return this.types.first_of(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
val takes := count - skip;
|
||
|
|
val given := this.given_count(args, kinds.view());
|
||
|
|
val spread := this.reports_spread(kind, skip, args, kinds.view());
|
||
|
|
if spread || (variadic ? given + 1 < takes : given != takes) {
|
||
|
|
if !spread {
|
||
|
|
val words := variadic
|
||
|
|
? $"The function takes at least {takes - 1} arguments, but is given {given}."
|
||
|
|
: $"The function takes {takes} arguments, but is given {given}.";
|
||
|
|
var text := string::from(words.view(), nothing {});
|
||
|
|
this.report_text(node, ec::arguments, &text);
|
||
|
|
}
|
||
|
|
for i in 0..args.length {
|
||
|
|
if kinds[i] == 0 { this.infer_expr(args[i]); }
|
||
|
|
}
|
||
|
|
return this.types.first_of(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
this.pass_args(kind, args, kinds.view(), skip, variadic);
|
||
|
|
return this.types.first_of(kind);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks each argument of a call against the parameter it is given for, from
|
||
|
|
#! the one at `skip`, a pack spread with `...` giving an argument for each of
|
||
|
|
#! its types.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The function type called.
|
||
|
|
#! @param args The arguments' nodes.
|
||
|
|
#! @param kinds Their types, as `arg_kinds` gives them.
|
||
|
|
#! @param skip How many parameters come before the first argument.
|
||
|
|
#! @param variadic Whether the last parameter takes the rest.
|
||
|
|
def checker::pass_args(&mut this, kind: u32, args: span<u32>, kinds: span<u32>, skip: u32, variadic: bool): void {
|
||
|
|
val count := this.types.part_count(kind);
|
||
|
|
return if count == 0;
|
||
|
|
var p := skip;
|
||
|
|
for i in 0..args.length {
|
||
|
|
if this.spreads_pack(args[i], kinds[i]) {
|
||
|
|
val none := constant {};
|
||
|
|
for j in 0..this.types.part_count(kinds[i]) {
|
||
|
|
val param := this.types.part_at(kind, p >= count ? count - 1 : p);
|
||
|
|
val each := this.types.part_at(kinds[i], j as u32);
|
||
|
|
if !this.spread_fits(kinds[i], j as u32, kind, p) {
|
||
|
|
this.report(args[i], ec::arguments, "`...` gives the view of the rest that a variadic function's pack holds only for a variadic parameter.");
|
||
|
|
p++;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
val how := this.fits(each, param, &none, false, 0);
|
||
|
|
if how == fit::none || how == fit::overflow {
|
||
|
|
this.report_mismatch(args[i], each, param);
|
||
|
|
}
|
||
|
|
p++;
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
val param := this.types.part_at(kind, p >= count ? count - 1 : p);
|
||
|
|
this.pass_arg(args[i], kinds[i], param, variadic && p + 1 >= count);
|
||
|
|
p++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `compile_error(text)`, which reports its text where checking reaches it: in
|
||
|
|
#! code that is not generic, or in each instance of what is, since a generic's
|
||
|
|
#! own body is checked before its type arguments are known. Within branches of
|
||
|
|
#! `if const` only evaluating decides, it is reported once evaluating chooses
|
||
|
|
#! each, and text that is not written, as a constant's or interpolated, is the
|
||
|
|
#! text the checker knows, or else what evaluating works out.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @param args The arguments' nodes: the text, and where the error is.
|
||
|
|
#! @returns `void`.
|
||
|
|
def checker::compile_error(&mut this, node: u32, args: span<u32>): u32 {
|
||
|
|
this.infer_args(args);
|
||
|
|
return bt::void_type if this.decls.has(this.at.decl, df::generic) && this.at.instance == 0;
|
||
|
|
# Within what is undecided that evaluating does not decide, or within a
|
||
|
|
# branch checked quietly, it never reports.
|
||
|
|
return bt::void_type if this.undecided != 0 && (this.undecided != this.branches.length || this.mute != 0);
|
||
|
|
|
||
|
|
var text := string::new(nothing {});
|
||
|
|
var computed := false;
|
||
|
|
if args.length != 0 && this.kind_of(args[0]) == nk::string {
|
||
|
|
this.literal_text(this.text_of(args[0]), &mut text);
|
||
|
|
} else if args.length != 0 {
|
||
|
|
val known := this.known_of(args[0], 0, 0);
|
||
|
|
val read := known == 0 ? this.runtime_local_read(args[0]) : 0;
|
||
|
|
if known != 0 && this.knowns.p[known].kind == kk::text {
|
||
|
|
text.append(this.known_text(known));
|
||
|
|
} else if read != 0 {
|
||
|
|
this.report_runtime_read(read);
|
||
|
|
return bt::void_type;
|
||
|
|
} else {
|
||
|
|
computed = true;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
text.append("The program reports an error here.");
|
||
|
|
}
|
||
|
|
|
||
|
|
# Where `at` is a description's, as `d.at`, the error is where it declares.
|
||
|
|
var file := this.at.file;
|
||
|
|
var place := node;
|
||
|
|
if args.length > 1 && this.kind_of(args[1]) == nk::member {
|
||
|
|
val one := this.reflected_one(this.left_of(args[1]));
|
||
|
|
if one != checker::no_reflection {
|
||
|
|
val held: desc = this.descs[one];
|
||
|
|
file = this.decls.files.p[held.decl];
|
||
|
|
place = this.decls.nodes.p[held.decl];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# What is checked quietly is only dropped, so nothing of it waits.
|
||
|
|
if (computed || this.branches.length != 0) && this.mute == 0 {
|
||
|
|
if computed {
|
||
|
|
this.to_evaluate.append(pair_key(this.at.table, node));
|
||
|
|
}
|
||
|
|
this.held_errors.append(checker::held_error {
|
||
|
|
table = this.at.table,
|
||
|
|
node = node,
|
||
|
|
file = file,
|
||
|
|
place = place,
|
||
|
|
decl = this.at.decl,
|
||
|
|
instance = this.at.instance,
|
||
|
|
detail = move(&mut text),
|
||
|
|
first = this.held_branches.length as u32,
|
||
|
|
count = this.branches.length as u32,
|
||
|
|
computed = computed
|
||
|
|
});
|
||
|
|
this.held_branches.extend(this.branches.view());
|
||
|
|
return bt::void_type;
|
||
|
|
}
|
||
|
|
this.report_in(file, place, ec::invalid, text.view());
|
||
|
|
return bt::void_type;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reports a `compile_error` left to evaluating, once evaluating chose each
|
||
|
|
#! branch around it, as checking would have reported it there.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param index The held error.
|
||
|
|
#! @param text What evaluating worked out it says, where it did.
|
||
|
|
pub def checker::report_held(&mut this, index: u32, text: &mut string): void {
|
||
|
|
val held := &this.held_errors[index];
|
||
|
|
val saved := this.at;
|
||
|
|
this.at.decl = held.decl;
|
||
|
|
this.at.instance = held.instance;
|
||
|
|
if held.computed && text.length == 0 {
|
||
|
|
text.append("The program reports an error here.");
|
||
|
|
}
|
||
|
|
val detail := held.computed ? text.view() : held.detail.view();
|
||
|
|
this.report_in(held.file, held.place, ec::invalid, detail);
|
||
|
|
this.at = saved;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The text a string literal writes, without its quotes, and of text spanning
|
||
|
|
#! lines, without the indentation of its closing quotes or the line breaks
|
||
|
|
#! around it.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param written The literal as written, with its quotes.
|
||
|
|
#! @param out Where the text is written.
|
||
|
|
def checker::literal_text(&this, written: strview, out: &mut string): void {
|
||
|
|
if written.length < 6 || written[0] != '"' || written[1] != '"' || written[2] != '"' {
|
||
|
|
out.append(written.substr(1, written.length - 2));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var inner := written.substr(3, written.length - 6);
|
||
|
|
var indent := 0u;
|
||
|
|
var at := inner.length;
|
||
|
|
while at > 0 && (inner[at - 1] == ' ' || inner[at - 1] == '\t') {
|
||
|
|
at--;
|
||
|
|
indent++;
|
||
|
|
}
|
||
|
|
var line_start := true;
|
||
|
|
var skipped := 0u;
|
||
|
|
var first := true;
|
||
|
|
for i in 0..at {
|
||
|
|
val c := inner[i];
|
||
|
|
if first && c == '\n' {
|
||
|
|
first = false;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
first = false;
|
||
|
|
if line_start && skipped < indent && (c == ' ' || c == '\t') {
|
||
|
|
skipped++;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
line_start = c == '\n';
|
||
|
|
skipped = 0;
|
||
|
|
if c == '\n' && i + 1 >= at {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
out.push(c);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether an argument names a generic function, which is a value once the type
|
||
|
|
#! of its parameter gives its type arguments.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::names_generic(&mut this, node: u32): bool {
|
||
|
|
return false if this.kind_of(node) != nk::name;
|
||
|
|
val name := this.name_of(node);
|
||
|
|
return false if name as u64 >= this.generic_names.length || !this.generic_names[name];
|
||
|
|
val ref := this.bound(name) != 0 ? 0 : this.lookup_global(name, 0);
|
||
|
|
return ref != 0 && this.decls.kinds.p[ref] == dk::function && this.decls.overloads.p[ref] == 0
|
||
|
|
&& this.decls.has(ref, df::generic) && this.own_param_count(ref) != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path names what is a value only once the function type expected of
|
||
|
|
#! it is known: a generic function whose type arguments it leaves out, as
|
||
|
|
#! `integer::parse`, or a method or property of a generic type named without
|
||
|
|
#! its type arguments, as `result::is_ok`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `path` node.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::path_waits(&mut this, node: u32): bool {
|
||
|
|
return false if this.segment_args(node, this.segment_count(node) - 1) != 0;
|
||
|
|
# What it names is found quietly, and again where it is checked.
|
||
|
|
val dropped := this.dropped;
|
||
|
|
val uninferred := this.uninferred;
|
||
|
|
this.mute++;
|
||
|
|
val target := this.path_target(node);
|
||
|
|
this.mute--;
|
||
|
|
this.dropped = dropped;
|
||
|
|
this.uninferred = uninferred;
|
||
|
|
var decl := target.decl;
|
||
|
|
return false if decl == 0;
|
||
|
|
|
||
|
|
# Of a member, an owner of the error type is a generic type named without its
|
||
|
|
# arguments.
|
||
|
|
val open := this.decls.owners.p[decl] != 0 && target.owner == bt::error;
|
||
|
|
if this.decls.kinds.p[decl] == dk::property {
|
||
|
|
return false if !open;
|
||
|
|
decl = this.property_getter(decl);
|
||
|
|
return false if decl == 0;
|
||
|
|
}
|
||
|
|
return false if this.decls.kinds.p[decl] != dk::function;
|
||
|
|
# An overloaded function waits to be chosen by its parameter's type, as a name
|
||
|
|
# of one does.
|
||
|
|
return true if this.decls.overloads.p[decl] != 0;
|
||
|
|
this.resolve(decl);
|
||
|
|
return this.decls.has(decl, df::generic) && (this.own_param_count(decl) != 0 || open);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a name names an overloaded function, which is a value only where the
|
||
|
|
#! type of one overload is expected, as a parameter's.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `name` node.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::names_overloaded(&mut this, node: u32): bool {
|
||
|
|
val name := this.name_of(node);
|
||
|
|
return false if this.bound(name) != 0;
|
||
|
|
val ref := this.lookup_global(name, 0);
|
||
|
|
return ref != 0 && this.decls.kinds.p[ref] == dk::function && this.decls.overloads.p[ref] != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An argument that is a call, checked quietly, or 0 where it cannot infer its own
|
||
|
|
#! type arguments without its parameter's type, so it waits for the others to give
|
||
|
|
#! it. One that is wrong for another reason is checked again where its notes are
|
||
|
|
#! made.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @returns Its type, or 0 where it waits.
|
||
|
|
def checker::tried(&mut this, node: u32): u32 {
|
||
|
|
val dropped := this.dropped;
|
||
|
|
val uninferred := this.uninferred;
|
||
|
|
this.mute++;
|
||
|
|
val kind := this.infer_expr(node);
|
||
|
|
this.mute--;
|
||
|
|
return kind if this.dropped == dropped;
|
||
|
|
|
||
|
|
this.clear_flag(node, rf::checked);
|
||
|
|
return 0 if this.uninferred != uninferred;
|
||
|
|
return this.infer_expr(node);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks an argument that waited for its parameter's type, with what is known of
|
||
|
|
#! it, giving the type arguments what it gives.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param pattern The parameter's type, as the template writes it.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
def checker::infer_later(&mut this, node: u32, pattern: u32, into: &mut checker::inferred): void {
|
||
|
|
val applied := this.apply_known(pattern, into);
|
||
|
|
val plain := this.unalias(applied);
|
||
|
|
|
||
|
|
# An array given for a view gives the type of what it views by its elements.
|
||
|
|
if this.kind_of(node) == nk::array {
|
||
|
|
val wanted := this.unalias(pattern);
|
||
|
|
if this.is_std(wanted, word::span) && this.types.part_count(wanted) != 0 && this.types.is_open(plain) {
|
||
|
|
val run := this.left_of(node);
|
||
|
|
for &each in this.run_view(run) {
|
||
|
|
this.unify(this.types.part_at(wanted, 0), this.infer_expr(each), into, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# An anonymous function given for a type parameter is of that parameter's type.
|
||
|
|
if this.kind_of(node) == nk::lambda && this.types.kind_of(plain) == ty::param {
|
||
|
|
this.infer_lambda(node, this.types.first_of(plain), into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# So is a generic function named as a value.
|
||
|
|
val named := this.kind_of(node) == nk::name || this.kind_of(node) == nk::path;
|
||
|
|
if named && this.types.kind_of(plain) == ty::param {
|
||
|
|
this.infer_named(node, this.types.first_of(plain), into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# An anonymous function, or a generic function named as a value, is given its
|
||
|
|
# parameters' types, and gives its result's.
|
||
|
|
if this.kind_of(node) == nk::lambda || named {
|
||
|
|
return if this.types.kind_of(plain) != ty::function;
|
||
|
|
var unknown := false;
|
||
|
|
for &part in this.types.parts_view(plain) {
|
||
|
|
unknown = unknown || this.unknown_in(part, into);
|
||
|
|
}
|
||
|
|
# Where the parameter's type leaves the function's parameters unknown, as
|
||
|
|
# `def(&T): bool`, one whose types are written gives them.
|
||
|
|
if unknown {
|
||
|
|
if this.typed_lambda(node) {
|
||
|
|
val own := this.infer_expr(node);
|
||
|
|
if own != bt::error {
|
||
|
|
this.unify(pattern, own, into, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.free_result = this.unknown_in(this.types.first_of(plain), into);
|
||
|
|
val made := this.expr(node, applied);
|
||
|
|
this.free_result = false;
|
||
|
|
this.unify(pattern, made, into, 0);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A range is std's `range` of the type the parameter gives its ends, or of
|
||
|
|
# what they are where it gives none. One given for a type parameter bounded by
|
||
|
|
# `iterator<E>` counts by `E`, where that is known.
|
||
|
|
if this.kind_of(node) == nk::range {
|
||
|
|
var wanted := applied;
|
||
|
|
if this.types.kind_of(plain) == ty::param {
|
||
|
|
val element := this.iterated_bound(this.types.first_of(plain), into);
|
||
|
|
val decl := this.std_decl(word::range);
|
||
|
|
if element != 0 && decl != 0 {
|
||
|
|
val args: u32[1] = [element];
|
||
|
|
wanted = this.types.named(decl, args[0:1]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val made := this.types.is_open(wanted) ? this.infer_expr(node) : this.check_expr(node, wanted);
|
||
|
|
this.unify(pattern, made, into, 0);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A call that waited is checked once its parameter's type is known.
|
||
|
|
if this.kind_of(node) == nk::call && !this.types.is_open(applied) {
|
||
|
|
this.check_expr(node, applied);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks an anonymous function given for a type parameter, which is then of its
|
||
|
|
#! type. A parameter bounded by `invokable<def(A...): R>` gives the function the
|
||
|
|
#! types it leaves out, those inferred so far put in, and takes what the bound
|
||
|
|
#! names from it, as `R` from what the function gives. One bounded otherwise
|
||
|
|
#! takes a function whose types are written.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `lambda` node.
|
||
|
|
#! @param param The type parameter's declaration.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
def checker::infer_lambda(&mut this, node: u32, param: u32, into: &mut checker::inferred): void {
|
||
|
|
var unbound := false;
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
unbound = unbound || (into.params[i] == param && into.binds[i] == checker::unknown_type);
|
||
|
|
}
|
||
|
|
return if !unbound;
|
||
|
|
|
||
|
|
val shape := this.invoked_bound(param, into);
|
||
|
|
var expected: u32 = bt::error;
|
||
|
|
if shape != 0 {
|
||
|
|
val known := this.apply_known(shape, into);
|
||
|
|
var closed := true;
|
||
|
|
for &part in this.types.parts_view(known) {
|
||
|
|
closed = closed && !this.unknown_in(part, into);
|
||
|
|
}
|
||
|
|
expected = closed ? known : bt::error;
|
||
|
|
}
|
||
|
|
this.free_result = expected != bt::error && this.unknown_in(this.types.first_of(expected), into);
|
||
|
|
val made := this.expr(node, expected);
|
||
|
|
this.free_result = false;
|
||
|
|
return if made == bt::error;
|
||
|
|
this.bind_param(param, made, into);
|
||
|
|
if shape != 0 {
|
||
|
|
this.unify(shape, made, into, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks a generic function named as a value given for a type parameter, which
|
||
|
|
#! is then of its type. A parameter bounded by `invokable<def(A...): R>` gives it
|
||
|
|
#! the types of its parameters, those inferred so far put in, from which it is
|
||
|
|
#! the instance they make, and takes what the bound names from it, as `R` from
|
||
|
|
#! what it gives. One bounded otherwise takes it as it is named.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `name` or `path` node.
|
||
|
|
#! @param param The type parameter's declaration.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
def checker::infer_named(&mut this, node: u32, param: u32, into: &mut checker::inferred): void {
|
||
|
|
var unbound := false;
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
unbound = unbound || (into.params[i] == param && into.binds[i] == checker::unknown_type);
|
||
|
|
}
|
||
|
|
return if !unbound;
|
||
|
|
|
||
|
|
val shape := this.invoked_bound(param, into);
|
||
|
|
var expected: u32 = bt::error;
|
||
|
|
if shape != 0 {
|
||
|
|
val known := this.apply_known(shape, into);
|
||
|
|
var closed := true;
|
||
|
|
for &part in this.types.parts_view(known) {
|
||
|
|
closed = closed && !this.unknown_in(part, into);
|
||
|
|
}
|
||
|
|
expected = closed ? known : bt::error;
|
||
|
|
}
|
||
|
|
this.free_result = expected != bt::error && this.unknown_in(this.types.first_of(expected), into);
|
||
|
|
val made := this.expr(node, expected);
|
||
|
|
this.free_result = false;
|
||
|
|
return if made == bt::error;
|
||
|
|
this.bind_param(param, made, into);
|
||
|
|
if shape != 0 {
|
||
|
|
this.unify(shape, made, into, 0);
|
||
|
|
}
|
||
|
|
this.adapt_bound(node, made, this.decls.types.p[param], into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives a type parameter bounded by `invokable<S>` the function type `S`, where
|
||
|
|
#! the argument given for it names a function of another type that a call as
|
||
|
|
#! `S` asks would take, as `filter(big)` gives `def(&&i64): bool` for a `big`
|
||
|
|
#! taking `&i64`. The argument is then made a function of `S`'s type calling it.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The argument's node.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @param pattern The parameter's type, as the template writes it.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
def checker::adapt_bound(&mut this, node: u32, kind: u32, pattern: u32, into: &mut checker::inferred): void {
|
||
|
|
return if this.types.kind_of(pattern) != ty::param;
|
||
|
|
return if this.kind_of(node) != nk::name && this.kind_of(node) != nk::path;
|
||
|
|
return if this.types.kind_of(this.unalias(kind)) != ty::function;
|
||
|
|
val param := this.types.first_of(pattern);
|
||
|
|
val shape := this.invoked_bound(param, into);
|
||
|
|
return if shape == 0;
|
||
|
|
var wanted := this.apply_known(shape, into);
|
||
|
|
for &part in this.types.parts_view(wanted) {
|
||
|
|
return if this.unknown_in(part, into);
|
||
|
|
}
|
||
|
|
# What the bound leaves to what the function gives, as `U` of `map`, it gives.
|
||
|
|
if this.unknown_in(this.types.first_of(wanted), into) {
|
||
|
|
this.unify(this.types.first_of(shape), this.types.first_of(this.unalias(kind)), into, 0);
|
||
|
|
wanted = this.apply_known(shape, into);
|
||
|
|
return if this.unknown_in(this.types.first_of(wanted), into);
|
||
|
|
}
|
||
|
|
return if !this.adapts(node, kind, wanted);
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
if into.params[i] == param {
|
||
|
|
into.binds[i] = wanted;
|
||
|
|
into.soft[i] = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a type names a type parameter the call has yet to infer. One of the
|
||
|
|
#! generic around the call is known there, as `T` is within `def f<T>`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def checker::unknown_in(&this, kind: u32, into: &checker::inferred): bool {
|
||
|
|
return false if !this.types.is_open(kind);
|
||
|
|
for i in 0..into.params.length {
|
||
|
|
continue if into.binds[i] != checker::unknown_type;
|
||
|
|
return true if this.mentions(kind, this.decls.types.p[into.params[i]], 0);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The function type a type parameter's `invokable` bound names, as `def(&T):
|
||
|
|
#! bool` of `F: invokable<def(&T): bool>`, in the terms of a trait's default
|
||
|
|
#! where it is one's, or 0 where it has no such bound.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param param The type parameter's declaration.
|
||
|
|
#! @param into What is inferred, whose outer terms are put in.
|
||
|
|
#! @returns The function type, or 0.
|
||
|
|
def checker::invoked_bound(&mut this, param: u32, into: &checker::inferred): u32 {
|
||
|
|
this.resolve(param);
|
||
|
|
val invokable := this.std_decl(word::invokable);
|
||
|
|
return 0 if invokable == 0;
|
||
|
|
val run: u32 = this.decls.children.p[param];
|
||
|
|
for i in 0..this.decls.list_length(run) {
|
||
|
|
val bound := this.unalias(this.outer_terms(this.decls.list_at(run, i as u32), into));
|
||
|
|
continue if this.types.kind_of(bound) != ty::named || this.types.first_of(bound) != invokable
|
||
|
|
|| this.types.part_count(bound) != 2;
|
||
|
|
val pack := this.types.part_at(bound, 0);
|
||
|
|
continue if this.types.kind_of(pack) != ty::pack || this.types.is_open_pack(pack);
|
||
|
|
return this.types.function(this.types.parts_view(pack), this.types.part_at(bound, 1), this.types.second_of(pack) != 0);
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The integer type a type parameter's `iterator<E>` bound gives its elements,
|
||
|
|
#! with the type parameters inferred so far put in.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param param The type parameter's declaration.
|
||
|
|
#! @param into What is inferred.
|
||
|
|
#! @returns The element's type, or 0 where no such bound gives a known integer.
|
||
|
|
def checker::iterated_bound(&mut this, param: u32, into: &checker::inferred): u32 {
|
||
|
|
this.resolve(param);
|
||
|
|
val iterator := this.std_decl(word::iterator);
|
||
|
|
return 0 if iterator == 0;
|
||
|
|
val run: u32 = this.decls.children.p[param];
|
||
|
|
for i in 0..this.decls.list_length(run) {
|
||
|
|
val bound := this.unalias(this.outer_terms(this.decls.list_at(run, i as u32), into));
|
||
|
|
continue if this.types.kind_of(bound) != ty::named || this.types.first_of(bound) != iterator
|
||
|
|
|| this.types.part_count(bound) != 1;
|
||
|
|
val element := this.apply_known(this.types.part_at(bound, 0), into);
|
||
|
|
return element if !this.types.is_open(element) && this.types.is_integer(this.unalias(element));
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|