847 lines
35 KiB
Plaintext
847 lines
35 KiB
Plaintext
#! @file expressions.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the checking of expressions. Each is checked once, with the type
|
||
|
|
#! expected of it where one is, which a constant, an array, or an anonymous
|
||
|
|
#! function takes its type from, and is then converted to that type. What an
|
||
|
|
#! expression is checked as is passed down rather than kept on the checker,
|
||
|
|
#! so nothing has to put it back.
|
||
|
|
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! Checks the expression, converting it to the type expected, where one is.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The expression.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns The type it is used as: the type expected, or its own where none is.
|
||
|
|
pub def checker::check_expr(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
return bt::error if node == 0;
|
||
|
|
|
||
|
|
val own := this.has_flag(node, rf::checked)
|
||
|
|
? this.type_at(node)
|
||
|
|
: this.expr(node, expected);
|
||
|
|
return own if expected == bt::error;
|
||
|
|
return this.convert(node, expected) ? expected : bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks the expression where nothing is expected of it.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The expression.
|
||
|
|
#! @returns Its own type, which may be a constant's.
|
||
|
|
pub def checker::infer_expr(&mut this, node: u32): u32 {
|
||
|
|
return bt::error if node == 0;
|
||
|
|
return this.type_at(node) if this.has_flag(node, rf::checked);
|
||
|
|
return this.expr(node, bt::error);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks the expression with a type in mind, which gives what it leaves open
|
||
|
|
#! but is not what it is converted to, as the right of `+=` is checked before
|
||
|
|
#! what `+` takes there is known.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The expression.
|
||
|
|
#! @param expected The type in mind.
|
||
|
|
#! @returns Its own type, which may be a constant's.
|
||
|
|
pub def checker::hinted_expr(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
return bt::error if node == 0;
|
||
|
|
return this.type_at(node) if this.has_flag(node, rf::checked);
|
||
|
|
return this.expr(node, expected);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks the expression as a condition, which is a `bool`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The expression.
|
||
|
|
pub def checker::check_condition(&mut this, node: u32): void {
|
||
|
|
this.check_expr(node, bt::boolean);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks the expression, recording and giving its own type.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The expression.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns Its own type, or the error type where it is not valid.
|
||
|
|
def checker::expr(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
this.set_flag(node, rf::checked);
|
||
|
|
val kind := this.expr_kind(node, expected);
|
||
|
|
this.set_type(node, kind);
|
||
|
|
return kind;
|
||
|
|
}
|
||
|
|
|
||
|
|
def checker::expr_kind(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
match this.kind_of(node) {
|
||
|
|
nk::number => return this.number(node);
|
||
|
|
nk::character => {
|
||
|
|
this.set_value(node, constant::integer(this.read_character(this.text_of(node)), false));
|
||
|
|
return bt::untyped_char;
|
||
|
|
}
|
||
|
|
nk::string => return bt::untyped_text;
|
||
|
|
nk::istring => return this.interpolated(node);
|
||
|
|
nk::boolean => {
|
||
|
|
this.set_value(node, constant::truth(this.token_kind(node) == tk::kw_true));
|
||
|
|
return bt::untyped_bool;
|
||
|
|
}
|
||
|
|
nk::null_lit => return bt::null_type;
|
||
|
|
nk::name => return this.name_value(node, expected);
|
||
|
|
nk::path => return this.path_value(node, expected);
|
||
|
|
nk::unary => return this.unary(node, expected);
|
||
|
|
nk::postfix => return this.step(node, this.left_of(node));
|
||
|
|
nk::reference => return this.reference(node, expected);
|
||
|
|
nk::binary => return this.binary(node, expected);
|
||
|
|
nk::assign => return this.assign(node);
|
||
|
|
nk::ternary => return this.ternary(node, expected);
|
||
|
|
nk::fallback => return this.fallback(node, expected);
|
||
|
|
nk::call => return this.call(node, expected);
|
||
|
|
nk::index => return this.index(node);
|
||
|
|
nk::slice => return this.slice(node);
|
||
|
|
nk::member => return this.member(node);
|
||
|
|
nk::member_of => return this.member_of(node);
|
||
|
|
nk::type_args => return this.type_args_value(node);
|
||
|
|
nk::cast => return this.cast(node);
|
||
|
|
nk::is_test => return this.is_test(node);
|
||
|
|
nk::construct => return this.construct(node, expected);
|
||
|
|
nk::array => return this.array(node, expected);
|
||
|
|
nk::lambda => return this.lambda(node, expected);
|
||
|
|
nk::match_value => return this.match_value(node, expected);
|
||
|
|
nk::range => return this.range_value(node, expected);
|
||
|
|
nk::old_value => return this.old_value(node);
|
||
|
|
nk::undefined => {
|
||
|
|
this.report(node, ec::invalid, "`undefined` is only a local `var`'s value, which leaves its memory as it is.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
nk::error => return bt::error;
|
||
|
|
else => {
|
||
|
|
this.unsupported(node);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Marks the node a place, which can be written where `writable` says.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node.
|
||
|
|
#! @param writable Whether the place can be written.
|
||
|
|
pub def checker::mark_place(&mut this, node: u32, writable: bool): void {
|
||
|
|
this.set_flag(node, rf::place);
|
||
|
|
if writable {
|
||
|
|
this.set_flag(node, rf::writable);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a node is a place: a variable, parameter, field, element, or what a
|
||
|
|
#! reference or pointer reaches.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
pub def checker::is_place(&this, node: u32): bool { return this.has_flag(node, rf::place); }
|
||
|
|
|
||
|
|
#! Whether a node is a place that can be written.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
pub def checker::is_writable(&this, node: u32): bool { return this.has_flag(node, rf::writable); }
|
||
|
|
|
||
|
|
#! A number literal: an integer, an integer written with `u`, or a float.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `number` node.
|
||
|
|
#! @returns The constant's type: an untyped integer, unsigned integer, or float.
|
||
|
|
def checker::number(&mut this, node: u32): u32 {
|
||
|
|
val text := this.text_of(node);
|
||
|
|
if this.token_kind(node) == tk::float {
|
||
|
|
this.set_value(node, constant::real_of(this.read_real(text)));
|
||
|
|
return text[text.length - 1] == 'f' ? bt::untyped_float32 : bt::untyped_float;
|
||
|
|
}
|
||
|
|
|
||
|
|
var value := constant {};
|
||
|
|
if !this.read_integer(text, &mut value) {
|
||
|
|
this.report(node, ec::overflow, "Integer constants fit in 64 bits.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
this.set_value(node, value);
|
||
|
|
return this.token_kind(node) == tk::unsigned ? bt::untyped_uint : bt::untyped_int;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An interpolated string, whose holes may hold anything, which builds a
|
||
|
|
#! `string`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `istring` node.
|
||
|
|
#! @returns The `string` type.
|
||
|
|
def checker::interpolated(&mut this, node: u32): u32 {
|
||
|
|
val run := this.left_of(node);
|
||
|
|
for &piece in this.run_view(run) {
|
||
|
|
continue if this.kind_of(piece) == nk::string;
|
||
|
|
this.settle_expr(piece);
|
||
|
|
}
|
||
|
|
return this.std_type(word::string, span::<u32>::empty());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Checks the expression where nothing is expected of it, then settles a
|
||
|
|
#! constant to its default type.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The expression.
|
||
|
|
#! @returns The type it settles as.
|
||
|
|
pub def checker::settle_expr(&mut this, node: u32): u32 {
|
||
|
|
val own := this.infer_expr(node);
|
||
|
|
var settled := this.settle(own);
|
||
|
|
# An integer constant past what an `i64` holds is a `u64`, as `1 << 63` is.
|
||
|
|
if this.types.kind_of(own) == ty::untyped_int && this.is_known(node) {
|
||
|
|
val held := this.value_at(node);
|
||
|
|
if !held.floating && !held.negative && held.magnitude > 0x7FFFFFFFFFFFFFFF {
|
||
|
|
settled = bt::uint64;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if settled != own && settled != bt::error {
|
||
|
|
this.set_convert(node, settled);
|
||
|
|
}
|
||
|
|
return settled;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A name written as a value: a local, or a declaration.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `name` node.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns Its type, or the error type where it names nothing.
|
||
|
|
def checker::name_value(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
val name := this.name_of(node);
|
||
|
|
val bound := this.bound(name);
|
||
|
|
val found := bound != 0 ? checker::found { decl = bound } : this.lookup_named(name, node, false);
|
||
|
|
if found.decl == 0 {
|
||
|
|
this.report_undeclared(node, name);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
return this.scoped_value(node, found, expected) if found.owner != 0;
|
||
|
|
return this.ref_value(node, found.decl, expected);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The value of what a type around a name gives it, as though it were named
|
||
|
|
#! through the type, as `zero` within `vec2` is `vec2::zero`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `name` node.
|
||
|
|
#! @param found The member and the type giving it.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns Its type.
|
||
|
|
def checker::scoped_value(&mut this, node: u32, found: checker::found, expected: u32): u32 {
|
||
|
|
val made := this.decl_value_of(node, found.decl, expected);
|
||
|
|
# A member of a generic enum is of the enum given its arguments.
|
||
|
|
val owner := found.owner;
|
||
|
|
if this.decls.kinds.p[found.decl] == dk::enum_member && made != bt::error
|
||
|
|
&& this.types.kind_of(owner) == ty::named && this.types.part_count(owner) != 0
|
||
|
|
&& this.types.first_of(owner) == this.decls.owners.p[found.decl] {
|
||
|
|
return owner;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The value of what a name refers to.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node naming it.
|
||
|
|
#! @param ref What it refers to: a local, with `ref_local`, or a
|
||
|
|
#! declaration.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns Its type.
|
||
|
|
def checker::ref_value(&mut this, node: u32, ref: u32, expected: u32): u32 {
|
||
|
|
this.set_ref(node, ref);
|
||
|
|
if (ref & ref_local) != 0 {
|
||
|
|
val local := ref & ref_mask;
|
||
|
|
this.mark_place(node, this.has_flag(local, rf::mutable));
|
||
|
|
if this.is_known(local) {
|
||
|
|
this.set_value(node, this.value_at(local));
|
||
|
|
}
|
||
|
|
val described := this.reflected_at(local);
|
||
|
|
if described != checker::no_reflection {
|
||
|
|
this.reflections.set(this.result_key(node), described);
|
||
|
|
this.set_flag(node, rf::reflected);
|
||
|
|
}
|
||
|
|
return this.type_at(local);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (ref & ref_type) != 0 {
|
||
|
|
val kind := ref & ref_mask;
|
||
|
|
return this.constant_ref(node, kind) if this.is_constant_arg(kind);
|
||
|
|
this.report(node, ec::not_value, "A type is not a value.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
return this.decl_value_of(node, ref, expected);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The value of a declaration named as one: a function, a variable, an enum
|
||
|
|
#! member, or a property, whose value is its `get`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node naming it.
|
||
|
|
#! @param decl The declaration.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns Its type, or the error type where it is no value.
|
||
|
|
pub def checker::decl_value_of(&mut this, node: u32, decl: u32, expected: u32): u32 {
|
||
|
|
# A property named as a value is its `get`, a function of `&this`, as
|
||
|
|
# `rect::area` is a `def(&rect): f32`.
|
||
|
|
if this.decls.kinds.p[decl] == dk::property {
|
||
|
|
val getter := this.property_getter(decl);
|
||
|
|
return this.decl_value_of(node, getter, expected) if getter != 0;
|
||
|
|
}
|
||
|
|
if !this.visible(decl) && decl != this.consent {
|
||
|
|
this.report_hidden(node, decl);
|
||
|
|
}
|
||
|
|
|
||
|
|
this.set_ref(node, decl);
|
||
|
|
match this.decls.kinds.p[decl] {
|
||
|
|
dk::function => {
|
||
|
|
# A generic function named as a value is the instance the function type
|
||
|
|
# expected of it gives the type arguments of, as is a method of a generic
|
||
|
|
# type named without them, as `result::is_ok` given for a
|
||
|
|
# `def(&result<i64, failure>): bool`.
|
||
|
|
if this.decls.overloads.p[decl] == 0 && decl != this.consent {
|
||
|
|
this.resolve(decl);
|
||
|
|
if this.decls.has(decl, df::generic) && (this.own_param_count(decl) != 0 || this.decls.owners.p[decl] != 0) {
|
||
|
|
val made := this.function_instance(node, decl, expected, bt::error);
|
||
|
|
return made if made != bt::error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# Where C's address of a function is expected, the function is chosen by
|
||
|
|
# the type C calls it with.
|
||
|
|
val plain_expected := this.unalias(expected);
|
||
|
|
val c_expected := this.is_std(plain_expected, word::cfn) && this.types.part_count(plain_expected) == 1
|
||
|
|
? this.types.part_at(plain_expected, 0)
|
||
|
|
: 0;
|
||
|
|
val chosen := this.function_value(node, decl, c_expected != 0 ? c_expected : expected);
|
||
|
|
this.set_ref(node, chosen);
|
||
|
|
return bt::error if chosen == 0;
|
||
|
|
# An overload of a generic type's member is the instance chosen.
|
||
|
|
if chosen != decl || this.decls.overloads.p[decl] != 0 {
|
||
|
|
if this.decls.has(chosen, df::generic) && this.own_param_count(chosen) == 0 && this.decls.owners.p[chosen] != 0 {
|
||
|
|
val made := this.function_instance(node, chosen, expected, bt::error);
|
||
|
|
return made if made != bt::error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# A C function is taken as its address, which is what C calls, and a
|
||
|
|
# function of the program given where C's address is expected gets an
|
||
|
|
# entry C calls it back through.
|
||
|
|
val c_given := this.decls.has(chosen, df::foreign);
|
||
|
|
if (c_given || c_expected != 0) && this.std_decl(word::cfn) != 0 {
|
||
|
|
if !c_given {
|
||
|
|
this.check_callback(node, chosen);
|
||
|
|
this.decls.mark(chosen, df::callback);
|
||
|
|
}
|
||
|
|
var parts: u32[1] = [this.decls.types.p[chosen]];
|
||
|
|
return this.types.named(this.std_decl(word::cfn), parts[:]);
|
||
|
|
}
|
||
|
|
return this.decls.types.p[chosen];
|
||
|
|
}
|
||
|
|
dk::variable => {
|
||
|
|
this.resolve(decl);
|
||
|
|
val writes := this.decls.has(decl, df::mutable) && !this.decls.has(decl, df::constant);
|
||
|
|
this.mark_place(node, writes);
|
||
|
|
var value := constant {};
|
||
|
|
if this.decl_value(decl, &mut value) {
|
||
|
|
this.set_value(node, value);
|
||
|
|
}
|
||
|
|
return this.decls.types.p[decl];
|
||
|
|
}
|
||
|
|
dk::enum_member => {
|
||
|
|
this.resolve(decl);
|
||
|
|
var value := constant {};
|
||
|
|
if this.decl_value(decl, &mut value) {
|
||
|
|
this.set_value(node, value);
|
||
|
|
}
|
||
|
|
return this.decls.types.p[decl];
|
||
|
|
}
|
||
|
|
dk::attribute => return this.decls.types.p[decl];
|
||
|
|
else => {
|
||
|
|
var text := string::from("`", nothing {});
|
||
|
|
text.append(this.decl_text(decl));
|
||
|
|
val word := this.decl_word(decl);
|
||
|
|
text.append(word[0] == 'e' ? "` is an " : "` is a ");
|
||
|
|
text.append(word);
|
||
|
|
text.append(", not a value.");
|
||
|
|
this.report_text(node, ec::not_value, &text);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The type of the instance of a generic function the function type expected of
|
||
|
|
#! it gives the type arguments of, as `val f: def(i64): i64 = id;` makes
|
||
|
|
#! `id<i64>`, or the error type where it gives them not all.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node naming it.
|
||
|
|
#! @param decl The generic function.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @param owner The type it is a member of, given its arguments, which are
|
||
|
|
#! its first, or the error type.
|
||
|
|
#! @returns The instance's function type, or the error type.
|
||
|
|
def checker::function_instance(&mut this, node: u32, decl: u32, expected: u32, owner: u32): u32 {
|
||
|
|
var inferred := this.begin_inference(decl, owner, span::<u32>::empty());
|
||
|
|
val made := this.instance_type(decl, expected, &mut inferred);
|
||
|
|
return bt::error if made == bt::error;
|
||
|
|
val instance := this.instance_of(decl, inferred.binds.view(), node);
|
||
|
|
if instance != 0 {
|
||
|
|
this.called.set(this.result_key(node), instance);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The function type of the instance of a generic function the function type
|
||
|
|
#! expected of it gives the type arguments of, without making the instance, or
|
||
|
|
#! the error type. Its parameters are given those of the type expected, as many
|
||
|
|
#! as it has, so those it leaves to their defaults give nothing.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The generic function.
|
||
|
|
#! @param expected The type expected.
|
||
|
|
#! @param inferred What is inferred, which this adds to.
|
||
|
|
#! @returns The instance's function type, or the error type.
|
||
|
|
def checker::instance_type(&mut this, decl: u32, expected: u32, inferred: &mut checker::inferred): u32 {
|
||
|
|
val wanted := this.unalias(expected);
|
||
|
|
return bt::error if this.types.kind_of(wanted) != ty::function;
|
||
|
|
val kind: u32 = this.decls.types.p[decl];
|
||
|
|
val count := this.types.part_count(kind) < this.types.part_count(wanted)
|
||
|
|
? this.types.part_count(kind) : this.types.part_count(wanted);
|
||
|
|
for i in 0..count {
|
||
|
|
this.unify(this.types.part_at(kind, i as u32), this.types.part_at(wanted, i as u32), inferred, 0);
|
||
|
|
}
|
||
|
|
# A result the call around it is still inferring, as `U` of `map`'s, is what
|
||
|
|
# the function gives once its parameters' types give its arguments.
|
||
|
|
if !this.free_result {
|
||
|
|
this.unify(this.types.first_of(kind), this.types.first_of(wanted), inferred, 0);
|
||
|
|
}
|
||
|
|
return bt::error if this.unbound(inferred) != 0;
|
||
|
|
return this.apply(kind, inferred);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The overload a function named as a value is: the only one, or the one whose
|
||
|
|
#! type is the function type expected.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The node naming it.
|
||
|
|
#! @param first The first of its overloads.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns The overload's function type, or the error type where none is
|
||
|
|
#! chosen.
|
||
|
|
def checker::function_value(&mut this, node: u32, first: u32, expected: u32): u32 {
|
||
|
|
if this.decls.overloads.p[first] == 0 {
|
||
|
|
this.resolve(first);
|
||
|
|
if this.decls.has(first, df::generic) && this.own_param_count(first) != 0 {
|
||
|
|
# A generic function is a value once given its type arguments, which
|
||
|
|
# reflection, reaching it as it is, reports in its own words.
|
||
|
|
if first != this.consent {
|
||
|
|
this.report(node, ec::generic, "A generic function is a value once given its type arguments, as `max::<i64>`.");
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return first;
|
||
|
|
}
|
||
|
|
|
||
|
|
var walk := this.decls.walk_overloads(first);
|
||
|
|
while true {
|
||
|
|
val at := walk.next();
|
||
|
|
break if at == 0;
|
||
|
|
this.resolve(at);
|
||
|
|
return at if this.decls.types.p[at] == expected;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Where none is of that type, the one a call with its parameters would choose
|
||
|
|
# is, called as the type asks: a member of a generic type as the instance the
|
||
|
|
# type gives its arguments.
|
||
|
|
if this.types.kind_of(this.unalias(expected)) == ty::function {
|
||
|
|
var best: u32 = 0;
|
||
|
|
var least: i32 = -1;
|
||
|
|
var tied := false;
|
||
|
|
var each := this.decls.walk_overloads(first);
|
||
|
|
while true {
|
||
|
|
val at := each.next();
|
||
|
|
break if at == 0;
|
||
|
|
continue if this.decls.has(at, df::generic) && this.own_param_count(at) != 0;
|
||
|
|
var kind: u32 = this.decls.types.p[at];
|
||
|
|
if this.decls.has(at, df::generic) {
|
||
|
|
var inferred := this.begin_inference(at, bt::error, span::<u32>::empty());
|
||
|
|
kind = this.instance_type(at, expected, &mut inferred);
|
||
|
|
continue if kind == bt::error;
|
||
|
|
if kind == expected {
|
||
|
|
return at;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val cost := this.adapt_cost(at, kind, expected);
|
||
|
|
continue if cost < 0;
|
||
|
|
if least < 0 || cost < least {
|
||
|
|
best = at;
|
||
|
|
least = cost;
|
||
|
|
tied = false;
|
||
|
|
} else if cost == least {
|
||
|
|
tied = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return best if best != 0 && !tied;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.report(node, ec::ambiguous, "The function is overloaded, so it is a value only where the type of one of them is expected, or one a call as that type asks chooses.");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a path names: a module, a type, or a declaration, in order of its
|
||
|
|
#! segments.
|
||
|
|
pub struct checker::named {
|
||
|
|
var decl: u32; #! The declaration named, or 0.
|
||
|
|
var kind: u32; #! The type named, where it names one.
|
||
|
|
pub var owner: u32; #! The type a member is of, as `box<i64>` of `box::<i64>::new`.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What the segments of a path, from the first, name. A local or parameter
|
||
|
|
#! never hides a type or module there.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `path` or `type_name` node.
|
||
|
|
#! @returns The declaration or type named, and the type a member is of.
|
||
|
|
pub def checker::path_target(&mut this, node: u32): checker::named {
|
||
|
|
var made := checker::named {};
|
||
|
|
var home: u32 = 0;
|
||
|
|
var in_module := false;
|
||
|
|
# A generic type named without type arguments before a function lets the call
|
||
|
|
# infer them, as `box::new(5)` does.
|
||
|
|
var open := false;
|
||
|
|
val count := this.segment_count(node);
|
||
|
|
for i in 0..count {
|
||
|
|
val name := this.segment_name(node, i as u32);
|
||
|
|
val segment := this.segment_node(node, i as u32);
|
||
|
|
if i == 0 {
|
||
|
|
val bound := this.bound(name);
|
||
|
|
if (bound & ref_type) != 0 {
|
||
|
|
made.kind = bound & ref_mask;
|
||
|
|
made.decl = this.type_decl(made.kind);
|
||
|
|
if segment != node {
|
||
|
|
this.set_ref(segment, bound);
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A module the first of several segments names comes before a declaration of
|
||
|
|
# the name, as `serial::encode` names the module where `serial` is also the
|
||
|
|
# attribute it declares.
|
||
|
|
if count > 1 {
|
||
|
|
home = this.lookup_module(name);
|
||
|
|
if home != 0 {
|
||
|
|
in_module = true;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# What a type around it gives is reached through that type, as `kind::word`
|
||
|
|
# within `token` is `token::kind::word`.
|
||
|
|
val found := this.lookup_named(name, segment, count > 1);
|
||
|
|
made.decl = found.decl;
|
||
|
|
if found.owner != 0 {
|
||
|
|
made.owner = found.owner;
|
||
|
|
}
|
||
|
|
if made.decl == 0 {
|
||
|
|
home = this.lookup_module(name);
|
||
|
|
in_module = home != 0;
|
||
|
|
if !in_module {
|
||
|
|
this.report_undeclared(segment, name);
|
||
|
|
return checker::named {};
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
} else if in_module {
|
||
|
|
made.decl = this.modules.decl(home, name);
|
||
|
|
if made.decl == 0 {
|
||
|
|
home = this.modules.find(home, name);
|
||
|
|
if home == 0 {
|
||
|
|
var text := string::from("The module declares no `", nothing {});
|
||
|
|
text.append(this.names.text(name));
|
||
|
|
text.append("`.");
|
||
|
|
this.report_text(segment, ec::undefined, &text);
|
||
|
|
return checker::named {};
|
||
|
|
}
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
in_module = false;
|
||
|
|
} else {
|
||
|
|
# A type a bound asks of a type parameter given a type, as `F::result` where
|
||
|
|
# `F: signature` is given a function type, is the one the type gives.
|
||
|
|
if i == 1 && made.kind != bt::error {
|
||
|
|
val param := this.param_named(this.segment_name(node, 0));
|
||
|
|
val asked := param != 0 ? this.asked_of_bounds(made.kind, param, name) : bt::error;
|
||
|
|
if asked != bt::error {
|
||
|
|
made.kind = asked;
|
||
|
|
made.decl = this.type_decl(asked);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
val owner := made.decl;
|
||
|
|
if !this.is_type_decl(owner) {
|
||
|
|
this.report(segment, ec::member, "Only a module or a type has members named with `::`.");
|
||
|
|
return checker::named {};
|
||
|
|
}
|
||
|
|
|
||
|
|
made.owner = made.kind != bt::error ? made.kind : this.decls.types.p[owner];
|
||
|
|
val found := this.find_path_member(made.owner, name);
|
||
|
|
# A trait's static methods and constants belong to the types implementing
|
||
|
|
# it, which the trait's own name does not say.
|
||
|
|
if found.decl != 0 && this.trait_asks(found.decl) && this.named_kind(this.unalias(made.owner)) == dk::trait_type {
|
||
|
|
val kind_of: dk = this.decls.kinds.p[found.decl];
|
||
|
|
if kind_of == dk::variable || (kind_of == dk::function && !this.decls.has(found.decl, df::method)) {
|
||
|
|
this.report(segment, ec::invalid, kind_of == dk::variable
|
||
|
|
? "A constant of a trait belongs to the types implementing it, so read it through a type parameter the trait bounds, as `T::name`."
|
||
|
|
: "A static method of a trait belongs to the types implementing it, so call it through a type parameter the trait bounds, as `T::name()`.");
|
||
|
|
return checker::named {};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
made.decl = found.decl;
|
||
|
|
if found.decl != 0 && this.types.kind_of(found.owner) == ty::named
|
||
|
|
&& this.decls.kinds.p[this.types.first_of(found.owner)] != dk::trait_type {
|
||
|
|
made.owner = found.owner;
|
||
|
|
}
|
||
|
|
made.kind = bt::error;
|
||
|
|
if open && made.decl != 0 && (this.decls.kinds.p[made.decl] == dk::function || this.decls.kinds.p[made.decl] == dk::property) {
|
||
|
|
made.owner = bt::error;
|
||
|
|
}
|
||
|
|
if made.decl == 0 {
|
||
|
|
var text := string::from("`", nothing {});
|
||
|
|
val owner_text := this.describe(made.owner);
|
||
|
|
text.append(owner_text.view());
|
||
|
|
text.append("` has no member `");
|
||
|
|
text.append(this.names.text(name));
|
||
|
|
text.append("`.");
|
||
|
|
this.report_text(segment, ec::member, &text);
|
||
|
|
return checker::named {};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if !this.visible(made.decl) {
|
||
|
|
this.report_hidden(segment, made.decl);
|
||
|
|
}
|
||
|
|
# What each segment names is kept for the editor, which shows it and finds
|
||
|
|
# where it is declared.
|
||
|
|
if segment != node {
|
||
|
|
this.set_ref(segment, made.decl);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A type named along the path takes the type arguments its segment gives.
|
||
|
|
if this.is_type_decl(made.decl) && i + 1 < count {
|
||
|
|
var args := vector::<u32>::new();
|
||
|
|
if made.owner != bt::error && this.types.kind_of(made.owner) == ty::named {
|
||
|
|
args.extend(this.types.parts_view(made.owner));
|
||
|
|
}
|
||
|
|
val run := this.segment_args(node, i as u32);
|
||
|
|
for &arg in this.run_view(run) {
|
||
|
|
args.append(this.type_arg_of(arg, made.decl, args.length as u32, args.view()));
|
||
|
|
}
|
||
|
|
open = this.decls.kinds.p[made.decl] != dk::builtin && args.length == 0 && this.decls.params.p[made.decl] != 0
|
||
|
|
&& this.at.decl != made.decl;
|
||
|
|
made.kind = this.decls.kinds.p[made.decl] == dk::builtin || args.length != 0 || this.decls.params.p[made.decl] == 0
|
||
|
|
? this.named_of(made.decl, args.view(), segment)
|
||
|
|
: this.decls.types.p[made.decl];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if in_module {
|
||
|
|
this.report(node, ec::not_value, "A module is not a value.");
|
||
|
|
return checker::named {};
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A path written as a value, as `color::red` or `vec2::zero`.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `path` node.
|
||
|
|
#! @param expected The type expected, or the error type where none is.
|
||
|
|
#! @returns Its type, or the error type where it names nothing.
|
||
|
|
def checker::path_value(&mut this, node: u32, expected: u32): u32 {
|
||
|
|
val target := this.path_target(node);
|
||
|
|
return bt::error if target.decl == 0;
|
||
|
|
|
||
|
|
# A generic function given its type arguments, as `max::<f32>`, is the
|
||
|
|
# instance they make.
|
||
|
|
if this.decls.kinds.p[target.decl] == dk::function && this.decls.overloads.p[target.decl] == 0 {
|
||
|
|
this.resolve(target.decl);
|
||
|
|
val written := this.segment_args(node, this.segment_count(node) - 1);
|
||
|
|
if written != 0 && this.decls.has(target.decl, df::generic) && this.own_param_count(target.decl) != 0 {
|
||
|
|
# A method of a generic type takes the type's arguments before its own, as
|
||
|
|
# `pair::<K, V>::make::<U>` does.
|
||
|
|
var args := vector::<u32>::new();
|
||
|
|
if target.owner != bt::error && this.types.kind_of(target.owner) == ty::named {
|
||
|
|
args.extend(this.types.parts_view(target.owner));
|
||
|
|
}
|
||
|
|
args.extend(this.written_args(target.decl, target.owner, written).view());
|
||
|
|
val instance := this.instance_of(target.decl, args.view(), node);
|
||
|
|
if instance != 0 {
|
||
|
|
this.set_ref(node, target.decl);
|
||
|
|
this.called.set(this.result_key(node), instance);
|
||
|
|
return this.instances[instance - 1].kind;
|
||
|
|
}
|
||
|
|
# Within a generic, type arguments naming its own parameters make an
|
||
|
|
# instance only once it is compiled for its own arguments.
|
||
|
|
var from := this.decls.list_of(this.decls.params.p[target.decl]);
|
||
|
|
if this.any_open(args.view()) && args.length == from.length {
|
||
|
|
this.set_ref(node, target.decl);
|
||
|
|
return this.substitute(this.decls.types.p[target.decl], from.view(), args.view());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# A property of a type given its arguments is its `get`, as a method is.
|
||
|
|
var member := target.decl;
|
||
|
|
if this.decls.kinds.p[member] == dk::property && target.owner != bt::error {
|
||
|
|
member = this.property_getter(member);
|
||
|
|
if member != 0 && !this.visible(member) {
|
||
|
|
this.report_hidden(node, member);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if member != 0 && this.decls.kinds.p[member] == dk::function && target.owner != bt::error {
|
||
|
|
this.set_ref(node, member);
|
||
|
|
this.resolve(member);
|
||
|
|
# A generic function of a type, named without its own type arguments, as
|
||
|
|
# `integer::parse`, is the instance the function type expected gives them.
|
||
|
|
if this.decls.overloads.p[member] == 0 && this.own_param_count(member) != 0 {
|
||
|
|
val made := this.function_instance(node, member, expected, target.owner);
|
||
|
|
return made if made != bt::error;
|
||
|
|
this.report(node, ec::generic,
|
||
|
|
"A generic function is a value once given its type arguments, and the function type expected doesn't give them all: write them, as `max::<i64>`.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
return this.member_function_type(member, target.owner);
|
||
|
|
}
|
||
|
|
val made := this.decl_value_of(node, target.decl, expected);
|
||
|
|
# A member of a generic enum is of the enum given the arguments its path
|
||
|
|
# gives, as `bag::<T>::state::full` is a `bag<T>::state`.
|
||
|
|
if this.decls.kinds.p[target.decl] == dk::enum_member && made != bt::error
|
||
|
|
&& this.types.kind_of(target.owner) == ty::named && this.types.part_count(target.owner) != 0
|
||
|
|
&& this.types.first_of(target.owner) == this.decls.owners.p[target.decl] {
|
||
|
|
return target.owner;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The type of a function declared through a type, given the type's arguments.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param decl The function's declaration.
|
||
|
|
#! @param owner The type, given its arguments.
|
||
|
|
#! @returns The function's type.
|
||
|
|
pub def checker::member_function_type(&mut this, decl: u32, owner: u32): u32 {
|
||
|
|
this.resolve(decl);
|
||
|
|
val kind: u32 = this.decls.types.p[decl];
|
||
|
|
return kind if this.types.kind_of(owner) != ty::named || this.types.part_count(owner) == 0;
|
||
|
|
|
||
|
|
var from := vector::<u32>::new();
|
||
|
|
val run: u32 = this.decls.params.p[decl];
|
||
|
|
val count := this.types.part_count(owner);
|
||
|
|
for i in 0..count {
|
||
|
|
break if i >= this.decls.list_length(run);
|
||
|
|
from.append(this.decls.list_at(run, i as u32));
|
||
|
|
}
|
||
|
|
val args := this.types.parts_of(owner);
|
||
|
|
return this.substitute(kind, from.view(), args.view());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A value given type arguments after it, as `value_of(d)::<i64>`: the instance
|
||
|
|
#! of the generic function a description names that they make.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `type_args` node.
|
||
|
|
#! @returns The instance's function type, or the error type.
|
||
|
|
def checker::type_args_value(&mut this, node: u32): u32 {
|
||
|
|
val callee := this.left_of(node);
|
||
|
|
var decl: u32 = 0;
|
||
|
|
if this.kind_of(callee) == nk::call && this.kind_of(this.left_of(callee)) == nk::name {
|
||
|
|
val named := this.lookup(this.name_of(this.left_of(callee)), this.left_of(callee));
|
||
|
|
val run := this.right_of(callee);
|
||
|
|
if named != 0 && (named & (ref_local | ref_type)) == 0 && this.decls.kinds.p[named] == dk::function
|
||
|
|
&& this.is_reflection(named) && this.names.text(this.decls.names.p[named]) == "value_of"
|
||
|
|
&& this.run_length(run) == 1 {
|
||
|
|
val arg := this.run_at(run, 0);
|
||
|
|
this.infer_expr(arg);
|
||
|
|
val one := this.described_arg(arg, "`value_of`");
|
||
|
|
return bt::error if one == checker::no_reflection;
|
||
|
|
val held: desc = this.descs[one];
|
||
|
|
decl = held.param == 0 ? held.decl : 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if decl == 0 || this.decls.kinds.p[decl] != dk::function || this.decls.overloads.p[decl] != 0 {
|
||
|
|
this.report(node, ec::generic, "Type arguments are given after a value only to `value_of` of a generic function, as `value_of(d)::<i64>`.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.resolve(decl);
|
||
|
|
if !this.decls.has(decl, df::generic) || this.own_param_count(decl) == 0 {
|
||
|
|
this.report(node, ec::generic, "The function takes no type arguments.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
var args := this.written_args(decl, bt::error, this.right_of(node));
|
||
|
|
val instance := this.instance_of(decl, args.view(), node);
|
||
|
|
return bt::error if instance == 0;
|
||
|
|
this.set_ref(node, decl);
|
||
|
|
this.called.set(this.result_key(node), instance);
|
||
|
|
return this.instances[instance - 1].kind;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `x.(d)`, the member of `x` a description known while compiling describes, as
|
||
|
|
#! though its name were written there: a field, or a property. A method it
|
||
|
|
#! names is called, which the call checks.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `member_of` node.
|
||
|
|
#! @returns The member's type, or the error type.
|
||
|
|
def checker::member_of(&mut this, node: u32): u32 {
|
||
|
|
val object := this.left_of(node);
|
||
|
|
val kind := this.infer_expr(object);
|
||
|
|
val found := this.described_member(node, kind);
|
||
|
|
return bt::error if found.decl == 0;
|
||
|
|
|
||
|
|
this.set_ref(node, found.decl);
|
||
|
|
this.resolve(found.decl);
|
||
|
|
match this.decls.kinds.p[found.decl] {
|
||
|
|
dk::field => {
|
||
|
|
this.mark_place(node, this.reaches_writable(kind, object) && this.decls.has(found.decl, df::mutable));
|
||
|
|
return this.instantiated(this.decls.types.p[found.decl], this.types.first_of(found.owner), found.owner);
|
||
|
|
}
|
||
|
|
dk::property => {
|
||
|
|
val property: u32 = this.decls.types.p[found.decl];
|
||
|
|
val held := this.types.kind_of(found.owner) == ty::named
|
||
|
|
? this.instantiated(property, found.decl, found.owner)
|
||
|
|
: property;
|
||
|
|
if this.property_setter(found.decl) != 0 && this.reaches_writable(kind, object) {
|
||
|
|
this.set_flag(node, rf::writable);
|
||
|
|
}
|
||
|
|
this.want_accessor(this.property_getter(found.decl), found.owner, node);
|
||
|
|
return held;
|
||
|
|
}
|
||
|
|
dk::function => {
|
||
|
|
this.report(node, ec::not_value, "A method is called, as `x.(d)()`; take it through its type for its value.");
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
else => {
|
||
|
|
this.unsupported(node);
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return bt::error;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The member of a value that `x.(d)` names: the declaration `d` describes,
|
||
|
|
#! where it is a member of the value's type.
|
||
|
|
#! @param this The checker.
|
||
|
|
#! @param node The `member_of` node.
|
||
|
|
#! @param kind The type of the value.
|
||
|
|
#! @returns The member and the type it is of, or no member where it names none.
|
||
|
|
def checker::described_member(&mut this, node: u32, kind: u32): checker::found {
|
||
|
|
val named := this.right_of(node);
|
||
|
|
this.infer_expr(named);
|
||
|
|
return checker::found {} if kind == bt::error || this.type_at(named) == bt::error;
|
||
|
|
val one := this.reflected_one(named);
|
||
|
|
if one == checker::no_reflection {
|
||
|
|
if !this.waits_for_instance() {
|
||
|
|
this.report(named, ec::constant, "A member written `x.(d)` takes a declaration known while compiling, as the variable of `for const d in declarations()` is.");
|
||
|
|
}
|
||
|
|
return checker::found {};
|
||
|
|
}
|
||
|
|
|
||
|
|
# Any overload of the name is the member it names.
|
||
|
|
val held: desc = this.descs[one];
|
||
|
|
val found := held.param != 0 ? checker::found {} : this.find_member(this.reached(kind), this.decls.names.p[held.decl]);
|
||
|
|
var walk := this.decls.walk_overloads(found.decl);
|
||
|
|
var at := walk.next();
|
||
|
|
while at != 0 && at != held.decl {
|
||
|
|
at = this.decls.kinds.p[at] == dk::function ? walk.next() : 0;
|
||
|
|
}
|
||
|
|
if at == 0 {
|
||
|
|
this.report(node, ec::member, "`x.(d)` names a member of `x`, and `d` describes none of it.");
|
||
|
|
return checker::found {};
|
||
|
|
}
|
||
|
|
return checker::found { decl = held.decl, owner = found.owner };
|
||
|
|
}
|