Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+895
View File
@@ -0,0 +1,895 @@
#! @file convert.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains when a value of one type is taken where another is expected, and
#! how well it fits there, which overloads are chosen by: a type exactly, then
#! a constant taking the type expected, then any other conversion, then
#! becoming a union. Also contains the type two values share, as both sides of
#! an operator or both branches of a ternary must.
module sakura::analyzer;
#! How a value fits where a type is expected, best first.
pub enum fit : u8 {
exact, #! It is the type.
read, #! It is a reference, read through to what it refers to, which is the type.
constant, #! It is a constant, which takes the type.
convert, #! It converts: widening, an alias, a view lent, or reading through a reference.
unite, #! It becomes a union holding it.
none, #! It does not fit.
overflow, #! It is a constant outside the range of the type.
}
#! The declaration of `core`'s root of the name, which the language relies on,
#! or 0 where it declares none.
#! @param this The checker.
#! @param word The ID of the name.
#! @returns The declaration, or 0.
pub def checker::std_decl(&mut this, word: u32): u32 {
if this.known.get(&word) is some(got) {
return got;
}
val core := this.modules.find(0, word::core);
val found := core == 0 ? 0 : this.modules.decl(core, word);
this.known.set(word, found);
return found;
}
#! The type of `core`'s root of the name given the arguments, or the error type
#! where it declares none.
#! @param this The checker.
#! @param word The ID of the name.
#! @param args The type arguments, or none.
#! @returns The type, or the error type.
pub def checker::std_type(&mut this, word: u32, args: span<u32>): u32 {
val decl := this.std_decl(word);
return bt::error if decl == 0;
this.resolve(decl);
return this.types.named(decl, args) if args.length != 0;
return this.decls.types.p[decl];
}
#! A `span<T>` of the element.
#! @param this The checker.
#! @param element The element's type.
#! @returns The span's type, or the error type where `core` declares no `span`.
pub def checker::span_of(&mut this, element: u32): u32 {
var args := vector::<u32>::new();
args.append(element);
return this.std_type(word::span, args.span);
}
#! The declaration a type is of: a named type's, a built-in type's, or a type
#! parameter's, or 0.
#! @param this The checker.
#! @param kind The type.
#! @returns The declaration, or 0.
pub def checker::type_decl(&this, kind: u32): u32 {
return match this.types.kind_of(kind) {
ty::named, ty::param => this.types.first_of(kind),
else => kind < bt::count as u32 ? this.builtin_of[kind] : 0
};
}
#! Whether the type is the named type of `core`'s root of the name, of any
#! arguments.
#! @param this The checker.
#! @param kind The type.
#! @param word The ID of the name.
#! @returns Whether it is.
pub def checker::is_std(&mut this, kind: u32, word: u32): bool {
return this.types.kind_of(kind) == ty::named && this.types.first_of(kind) == this.std_decl(word)
&& this.std_decl(word) != 0;
}
#! Whether a type is `core`'s vector or mask, `simd<L[N]>` or `mask<L[N]>`, which an
#! executable holds in vector registers.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
pub def checker::is_lanes_type(&mut this, kind: u32): bool {
val plain := this.unalias(kind);
return this.is_std(plain, word::simd) || this.is_std(plain, word::mask);
}
#! The type an alias stands for, given its arguments.
#! @param this The checker.
#! @param kind The alias's type.
#! @returns The type it stands for.
pub def checker::alias_target(&mut this, kind: u32): u32 {
if this.types.kind_of(kind) == ty::matched {
return this.reduce_match(kind) if this.choosable(kind);
val chosen := this.proved_choice(kind);
return chosen != 0 ? chosen : kind;
}
val decl := this.types.first_of(kind);
this.resolve(decl);
return this.instantiated(this.decls.targets.p[decl], decl, kind);
}
#! The type with the type parameters of the declaration replaced by the type
#! arguments of `kind`, a named type of it.
#! @param this The checker.
#! @param inner The type written within the declaration, as a field's.
#! @param decl The generic declaration.
#! @param kind The named type of it, whose arguments replace its parameters.
#! @returns The type with them replaced.
pub def checker::instantiated(&mut this, inner: u32, decl: u32, kind: u32): u32 {
val params: u32 = this.decls.params.p[decl];
return inner if params == 0 || !this.types.is_open(inner);
val from := this.decls.list_of(params);
val args := this.types.parts_of(kind);
return this.substitute(inner, from.span, args.span);
}
#! Whether the type is an alias.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
pub def checker::is_alias(&this, kind: u32): bool {
return this.types.kind_of(kind) == ty::named
&& this.decls.kinds.p[this.types.first_of(kind)] == dk::alias_type;
}
#! The type with every alias it is followed to what it stands for, and every
#! `match const` choosing by a type it knows to the type it chooses.
#! @param this The checker.
#! @param kind The type.
#! @returns The type at the end of its aliases, or the type itself where it is
#! none, or the error type where they do not end.
pub def checker::unalias(&mut this, kind: u32): u32 {
# Nearly every type given is no alias, which its flags say once asked, so
# that is told first, and the rest are followed apart.
return kind if (this.types.flags.p[kind] & (tf::asked as u8 | tf::alias as u8)) == tf::asked as u8;
val follows := this.follows;
return follows(this, kind);
}
# The type at the end of the aliases a type is followed through, as `unalias`
# gives it, for a type not yet asked of or that is an alias. Those not ended in
# as many steps as `max_aliases` says never end, as a `match const` choosing
# by itself does, and are reported.
def checker::follow_aliases(&mut this, kind: u32): u32 {
var at := kind;
var steps := 0u;
while this.aliases(at) {
if steps == checker::max_aliases {
this.report_endless(kind);
return bt::error;
}
at = this.alias_target(at);
steps++;
}
return at;
}
#! The most aliases, and types `match const` chooses, a type is followed through.
pub const checker::max_aliases: u64 = 32;
#! Whether the type is an alias, as `is_alias` says, or a type `match const`
#! chooses once it knows the type it chooses by, or within a generic's own body
#! where what is proved decides it, worked out once and kept in its flags, as
#! nearly every type `unalias` is given is neither.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
def checker::aliases(&mut this, kind: u32): bool {
val flags := this.types.flags.p[kind];
return (flags & tf::alias as u8) != 0 if (flags & tf::asked as u8) != 0;
# One within a generic's own body is chosen where what is proved of the type
# parameter it chooses by decides it, which changes as checking goes, so what
# it is is never kept.
if this.types.kind_of(kind) == ty::matched && !this.choosable(kind) {
return this.proved_choice(kind) != 0;
}
val alias := this.is_alias(kind) || this.choosable(kind);
this.types.flags.p[kind] = flags | tf::asked as u8 | (alias ? tf::alias as u8 : 0);
return alias;
}
#! The kind of declaration a named type is of, or `none`.
#! @param this The checker.
#! @param kind The type.
#! @returns The kind of its declaration, or `none` where it is not named.
pub def checker::named_kind(&this, kind: u32): dk {
return this.types.kind_of(kind) == ty::named
? this.decls.kinds.p[this.types.first_of(kind)]
: dk::none;
}
#! The integer type an enum is held as.
#! @param this The checker.
#! @param kind The enum's type.
#! @returns The integer type.
pub def checker::enum_integer(&mut this, kind: u32): u32 {
# An enum's members may name earlier ones while it resolves, and its integer
# type is known before them.
val decl := this.types.first_of(kind);
if this.decls.states.p[decl] != ds::resolving {
this.resolve(decl);
}
val integer: u32 = this.decls.targets.p[decl];
return integer == 0 ? bt::int32 : integer;
}
#! What the values of an enum's members are flipped by to be ordered as words
#! without a sign: the sign bit where the enum is held signed, and nothing
#! otherwise.
#! @param this The checker.
#! @param kind The enum's type.
#! @returns The bits to flip.
pub def checker::member_flip(&mut this, kind: u32): u64 {
return this.types.is_signed(this.unalias(this.enum_integer(this.unalias(kind)))) ? 0x8000000000000000u : 0;
}
#! The values of an enum's members, each once, from the least, as a range of
#! its members counts through them: each a word flipped by `member_flip`, so
#! the words order as the values do when compared without a sign.
#! @param this The checker.
#! @param kind The enum's type.
#! @returns The words.
pub def checker::member_order(&mut this, kind: u32): vector<u64> {
val plain := this.unalias(kind);
val flip := this.member_flip(plain);
val run: u32 = this.decls.children.p[this.types.first_of(plain)];
var values := vector::<u64>::new();
for i in 0..this.decls.list_length(run) {
val member := this.decls.list_at(run, i as u32);
var held := constant {};
continue if this.decls.kinds.p[member] != dk::enum_member || !this.decl_value(member, &mut held);
values.append(held.bits() ^ flip);
}
values.sort();
# A member given an earlier one's value is another name for it.
var made := vector::<u64>::new();
for each in values {
if made.length == 0 || made[made.length - 1] != each {
made.append(each);
}
}
return made;
}
#! The types of the fields of a struct or union type, given its arguments.
#! @param this The checker.
#! @param kind The struct or union type.
#! @returns The type of each field, in order.
pub def checker::field_types(&mut this, kind: u32): vector<u32> {
var made := vector::<u32>::new();
val decl := this.types.first_of(kind);
val run: u32 = this.decls.children.p[decl];
for i in 0..this.decls.list_length(run) {
val field := this.decls.list_at(run, i as u32);
continue if this.decls.kinds.p[field] != dk::field;
this.resolve(field);
made.append(this.instantiated(this.decls.types.p[field], decl, kind));
}
return made;
}
#! How the value of the node fits where the type is expected.
#! @param this The checker.
#! @param node The value's node.
#! @param to The type expected.
#! @returns How it fits.
pub def checker::fits_node(&mut this, node: u32, to: u32): fit {
val value := this.value_at(node);
val from := this.type_at(node);
val how := this.fits(from, to, &value, this.is_known(node), 0);
return how if how != fit::none || this.choices == 0;
return this.fits_unchosen(from, to, &value, this.is_known(node));
}
#! How a value of the type fits where `to` is expected, where `value` is its
#! value when `known`.
#! @param this The checker.
#! @param from The value's type.
#! @param to The type expected.
#! @param value The value, where it is known while compiling.
#! @param known Whether it is.
#! @param depth How deeply the search has gone, which stops a type nesting
#! without end.
#! @returns How it fits.
pub def checker::fits(&mut this, from: u32, to: u32, value: &constant, known: bool, depth: u32): fit {
return fit::exact if from == to || from == bt::error || to == bt::error;
return fit::none if depth > 8;
# Within a template, a pack's types fit each other, as its values did before
# they were reached at an index: each instance checks the types it gives.
val pack := this.pack_of_param(from);
return fit::exact if pack != 0 && pack == this.pack_of_param(to);
val target := this.unalias(to);
val source := this.unalias(from);
# A type parameter known to have a shape, as `T: array` or `T is vector` says,
# is one with that shape, and converts as it does.
if this.types.kind_of(source) == ty::param || this.types.kind_of(target) == ty::param {
val from_shape := this.param_shape(source);
return fit::exact if from_shape != 0 && this.unalias(from_shape) == target;
if from_shape != 0 && this.types.kind_of(this.unalias(from_shape)) != ty::param {
val how := this.fits(from_shape, to, value, known, depth + 1);
return how if how != fit::none;
}
val to_shape := this.param_shape(target);
return fit::exact if to_shape != 0 && this.unalias(to_shape) == source;
}
val goal := this.types.kind_of(target);
match this.types.kind_of(source) {
ty::untyped_int, ty::untyped_char => {
if goal == ty::signed || goal == ty::unsigned {
return known && !this.in_range(target, value) ? fit::overflow : fit::constant;
}
if goal == ty::untyped_uint {
return known && value.negative ? fit::overflow : fit::constant;
}
return fit::constant if goal == ty::floating || goal == ty::untyped_int
|| goal == ty::untyped_float || this.numeric_param(target);
return this.unites(source, target, value, known, depth);
}
ty::untyped_uint => {
if goal == ty::unsigned {
return known && !this.in_range(target, value) ? fit::overflow : fit::constant;
}
return fit::constant if this.numeric_param(target);
return this.unites(source, target, value, known, depth);
}
ty::untyped_float, ty::untyped_float32 => {
return fit::constant if goal == ty::floating || this.numeric_param(target);
return this.unites(source, target, value, known, depth);
}
ty::untyped_bool => return goal == ty::boolean ? fit::constant : this.unites(source, target, value, known, depth);
ty::untyped_text => {
return fit::constant if this.is_std(target, word::string) || this.is_text_view(target);
return this.unites(source, target, value, known, depth);
}
ty::null_type => {
return fit::constant if goal == ty::pointer || this.is_std(target, word::cfn);
return this.unites(source, target, value, known, depth);
}
ty::empty => return fit::constant;
else => {}
}
# What only differs by aliases converts, anywhere within the types.
return fit::convert if source == target || this.strip(source, 0) == this.strip(target, 0);
val skind := this.types.kind_of(source);
if this.types.is_numeric(source) && this.types.is_numeric(target) {
val sbits := this.types.first_of(source);
val tbits := this.types.first_of(target);
return fit::convert if skind == goal && tbits > sbits;
return fit::convert if skind == ty::unsigned && goal == ty::signed && tbits > sbits;
return fit::none;
}
# An enum is its integer type wherever one is expected.
if this.named_kind(source) == dk::enum_type && this.types.is_integer(target) {
val integer := this.enum_integer(source);
return integer == target || this.fits(integer, target, value, false, depth + 1) == fit::convert
? fit::convert
: fit::none;
}
if (skind == ty::reference || skind == ty::pointer) && skind == goal {
val inner_from := this.types.first_of(source);
val inner_to := this.types.first_of(target);
val writes := this.types.second_of(source) != 0;
val wants := this.types.second_of(target) != 0;
if !wants || writes {
return fit::convert if this.unalias(inner_from) == this.unalias(inner_to);
# A reference to a constant, as `&200`, refers to a copy of it of the type
# referred to.
if this.types.is_untyped(inner_from) {
val how := this.fits(inner_from, inner_to, value, false, depth + 1);
return how == fit::none || how == fit::overflow ? how : fit::constant;
}
# A reference to a value converts to a reference to a trait it satisfies.
if skind == ty::reference && this.named_kind(inner_to) == dk::trait_type
&& this.satisfies(inner_from, inner_to) {
return fit::convert;
}
}
# A reference to a reference is otherwise read through to the one it holds,
# where that converts, as a generic given a reference for `T` reads a `T`
# through a `&mut T`.
if skind == ty::reference && this.types.kind_of(this.unalias(inner_from)) == ty::reference {
val read := this.fits(inner_from, target, value, false, depth + 1);
return read == fit::none || read == fit::overflow ? read : fit::convert;
}
return fit::none;
}
# A union holds a reference as it holds anything else, before it is read.
if this.named_kind(target) == dk::union_type {
val united := this.unites(source, target, value, known, depth);
return united if united != fit::none;
}
# A plain reference is read through where its value is expected.
if skind == ty::reference && goal != ty::reference {
val read := this.fits(this.types.first_of(source), target, value, false, depth + 1);
return read == fit::none || read == fit::overflow ? read : fit::convert;
}
# A view lends a read-only one, and a string lends a view of its text.
if this.is_std(target, word::span) {
if this.is_std(source, word::slice) && this.types.part_at(source, 0) == this.types.part_at(target, 0) {
return fit::convert;
}
if this.is_std(source, word::string) && this.is_text_view(target) {
return fit::convert;
}
}
# Bytes become a string by being copied into memory the string owns, so it
# outlives whatever they were read from.
if this.is_std(target, word::string) && this.is_byte_view(source) {
return fit::convert;
}
if skind == ty::function && goal == ty::function {
return this.same_function(source, target) ? fit::convert : fit::none;
}
return this.unites(source, target, value, known, depth);
}
#! Whether the type is a view of bytes, read-only or writable.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
pub def checker::is_byte_view(&mut this, kind: u32): bool {
val plain := this.unalias(kind);
return (this.is_std(plain, word::span) || this.is_std(plain, word::slice))
&& this.unalias(this.types.part_at(plain, 0)) == bt::uint8;
}
#! The type with every alias within it followed to what it stands for, so
#! types differing only by aliases are the same once stripped.
#! @param this The checker.
#! @param kind The type.
#! @param depth How deeply the search has gone, which stops a type nesting
#! without end.
#! @returns The type stripped.
pub def checker::strip(&mut this, kind: u32, depth: u32): u32 {
return kind if depth > 16 || this.types.is_untyped(kind);
val plain := this.unalias(kind);
match this.types.kind_of(plain) {
ty::reference, ty::pointer, ty::fixed => {
val inner := this.strip(this.types.first_of(plain), depth + 1);
if this.types.part_count(plain) == 0 {
return this.types.intern(this.types.kind_of(plain), inner, this.types.second_of(plain), span::<u32>::blank());
}
val length := this.types.parts_of(plain);
return this.types.intern(this.types.kind_of(plain), inner, this.types.second_of(plain), length.span);
}
ty::named, ty::function => {
return plain if this.types.part_count(plain) == 0 && this.types.kind_of(plain) == ty::named;
var parts := vector::<u32>::new();
for i in 0..this.types.part_count(plain) {
parts.append(this.strip(this.types.part_at(plain, i as u32), depth + 1));
}
val first := this.types.kind_of(plain) == ty::function
? this.strip(this.types.first_of(plain), depth + 1)
: this.types.first_of(plain);
return this.types.intern(this.types.kind_of(plain), first, this.types.second_of(plain), parts.span);
}
else => return plain;
}
return plain;
}
#! Whether the type is a view of bytes, which text read in place is: a
#! `span<u8>`, or a `strview`.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
pub def checker::is_text_view(&mut this, kind: u32): bool {
val target := this.unalias(kind);
return this.is_std(target, word::span) && this.unalias(this.types.part_at(target, 0)) == bt::uint8;
}
#! Whether the type is a type parameter that numbers may stand in for.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is one.
def checker::numeric_param(&mut this, kind: u32): bool {
return this.types.kind_of(kind) == ty::param && this.param_has_numeric(this.types.first_of(kind));
}
#! Whether function types only differ by aliases.
#! @param this The checker.
#! @param a One function type.
#! @param b The other.
#! @returns Whether they do.
def checker::same_function(&mut this, a: u32, b: u32): bool {
return false if this.types.part_count(a) != this.types.part_count(b);
return false if this.strip(this.types.first_of(a), 0) != this.strip(this.types.first_of(b), 0);
for i in 0..this.types.part_count(a) {
# Aliases are looked through wherever they are, so `&strview` is `&span<u8>`,
# and a variadic parameter is the view it takes, so `def(i64...)` is
# `def(span<i64>)`.
val x := this.strip(this.param_taken(a, i as u32), 0);
val y := this.strip(this.param_taken(b, i as u32), 0);
return false if x != y;
}
return true;
}
#! The type a function takes a parameter as: its own, or of a variadic one, a
#! view of what it gathers.
#! @param this The checker.
#! @param kind The function type.
#! @param index The parameter's position.
#! @returns The type.
def checker::param_taken(&mut this, kind: u32, index: u32): u32 {
val part := this.types.part_at(kind, index);
return part if this.types.second_of(kind) == 0 || index + 1 != this.types.part_count(kind);
return this.span_of(part);
}
#! How a value fits a union: as the field of its own type, or else the one
#! field it converts to.
#! @param this The checker.
#! @param from The value's type.
#! @param to The union's type.
#! @param value The value, where it is known while compiling.
#! @param known Whether it is.
#! @param depth How deeply the search has gone.
#! @returns How it fits: `unite`, or `none`.
def checker::unites(&mut this, from: u32, to: u32, value: &constant, known: bool, depth: u32): fit {
# A type a generic's own body has not chosen yet, which every other way of
# fitting leaves to this, fits where each way it can be chosen does, and is
# fitted by what fits each.
if this.choices != 0 && (this.types.kind_of(from) == ty::matched || this.types.kind_of(to) == ty::matched) && from != to {
return this.fits_ways(from, to, value, known, depth);
}
return fit::none if this.named_kind(to) != dk::union_type;
# A constant goes to the field of the type it would be where nothing else is
# expected, before any other it fits.
val fields := this.field_types(to);
val settled := this.types.is_untyped(from) ? this.settle(from) : from;
var exact := 0;
var fitting := 0;
for &each in fields {
if each == from || each == settled {
exact++;
continue;
}
val how := this.fits(from, each, value, known, depth + 1);
if how != fit::none && how != fit::overflow && how != fit::unite {
fitting++;
}
}
return fit::unite if exact == 1 || (exact == 0 && fitting == 1);
return fit::none;
}
#! The field of a union a value of the type becomes, or 0.
#! @param this The checker.
#! @param from The value's type.
#! @param to The union's type.
#! @returns The field's declaration, or 0.
pub def checker::united_field(&mut this, from: u32, to: u32): u32 {
val decl := this.types.first_of(to);
val run: u32 = this.decls.children.p[decl];
val settled := this.types.is_untyped(from) ? this.settle(from) : from;
var found: u32 = 0;
var none_value := constant {};
for i in 0..this.decls.list_length(run) {
val field := this.decls.list_at(run, i as u32);
continue if this.decls.kinds.p[field] != dk::field;
this.resolve(field);
val kind := this.instantiated(this.decls.types.p[field], decl, to);
return field if kind == from || kind == settled;
val how := this.fits(from, kind, &none_value, false, 1);
if how != fit::none && how != fit::overflow && how != fit::unite && found == 0 {
found = field;
}
}
return found;
}
#! Converts the node's value to the type, reporting where it does not fit.
#! @param this The checker.
#! @param node The value's node.
#! @param to The type expected.
#! @returns Whether it fits.
pub def checker::convert(&mut this, node: u32, to: u32): bool {
return true if to == bt::error;
val from := this.type_at(node);
val how := this.fits_node(node, to);
match how {
fit::exact => return true;
fit::none => {
# A reference written as one is a pointer at what it refers to where one
# is expected, which keeps nothing alive.
if this.lends_pointer(node, from, to) {
this.refuse_lent_temporary(node);
this.set_convert(node, to);
return true;
}
# A function named where another function type is expected is called as
# that type asks, by a function made for it.
if this.adapts(node, from, to) {
this.set_convert(node, to);
this.set_flag(node, rf::adapted);
return true;
}
this.report_mismatch(node, from, to);
return false;
}
fit::overflow => {
var text := string::from("The constant is outside the range of `", nothing {});
val named := this.describe(to);
text.append(named.span);
text.append("`.");
this.report_text(node, ec::overflow, &text);
return false;
}
else => {
this.set_convert(node, to);
return true;
}
}
return true;
}
#! Whether a function named as a value, of the type `from`, is called as the
#! function type `to` asks, as a function of `to`'s type made for it calls it:
#! each of `to`'s parameters given as a call gives an argument, those after them
#! given their defaults, and what it gives converted to what `to` gives. A
#! function type matching it exactly takes it as it is.
#! @param this The checker.
#! @param node The node naming it.
#! @param from Its type.
#! @param to The function type expected.
#! @returns Whether it is.
pub def checker::adapts(&mut this, node: u32, from: u32, to: u32): bool {
return false if this.kind_of(node) != nk::name && this.kind_of(node) != nk::path;
val source := this.unalias(from);
val target := this.unalias(to);
return false if this.types.kind_of(source) != ty::function || this.types.kind_of(target) != ty::function;
return false if this.types.second_of(source) != 0 || this.types.second_of(target) != 0;
return false if source == target || this.strip(source, 0) == this.strip(target, 0);
val ref := this.ref_at(node);
return false if ref == 0 || (ref & (ref_local | ref_type)) != 0;
return this.adapt_cost(ref, source, target) >= 0;
}
#! How far a function of the type `from` is from being called as the function
#! type `to` asks, as `adapts` says it is: 0 for each parameter given as it is,
#! 1 for one lent or read through, and 2 for one converted, or -1 where it is not
#! called so. A call chooses between overloads by it.
#! @param this The checker.
#! @param decl The function's declaration, whose defaults it is given.
#! @param from Its type.
#! @param to The function type expected.
#! @returns How far, or -1.
pub def checker::adapt_cost(&mut this, decl: u32, from: u32, to: u32): i32 {
val source := this.unalias(from);
val target := this.unalias(to);
return -1 if this.types.kind_of(source) != ty::function || this.types.kind_of(target) != ty::function;
return -1 if this.types.second_of(source) != 0 || this.types.second_of(target) != 0;
# Only a function of the program is called so, with the defaults it declares.
val kind_of: dk = this.decls.kinds.p[decl];
return -1 if kind_of != dk::function && kind_of != dk::property;
return -1 if kind_of == dk::function && (this.decls.has(decl, df::foreign) || this.decls.has(decl, df::intrinsic)
|| this.decls.has(decl, df::variadic));
val given := this.types.part_count(target);
val takes := this.types.part_count(source);
val required: u32 = kind_of == dk::function ? this.required_params(decl) : takes;
return -1 if given > takes || given < required;
var cost: i32 = 0;
for i in 0..given {
val wanted := this.types.part_at(target, i as u32);
val param := this.types.part_at(source, i as u32);
return -1 if !this.passes(wanted, param);
if this.unalias(wanted) != this.unalias(param) {
cost += this.unalias(this.reached(wanted)) == this.unalias(this.reached(param)) ? 1 : 2;
}
}
# What a call around it still infers from what it gives, as `U` of `map`,
# takes whatever it gives.
return cost if this.free_result;
val none := constant {};
val gives := this.types.first_of(source);
val wants := this.types.first_of(target);
if this.types.kind_of(this.unalias(gives)) == ty::reference && this.types.kind_of(this.unalias(wants)) != ty::reference
&& this.closes(this.reached(gives)) {
return -1;
}
val how := this.fits(gives, wants, &none, false, 0);
return -1 if how == fit::none || how == fit::overflow;
return cost + (how == fit::exact ? 0 : 1);
}
#! Whether a value of the type `given` is given for a parameter of the type
#! `param` as a call gives it: a value lent where the parameter is a reference,
#! a reference read through to one of the type asked, and what a reference
#! reaches copied where the parameter takes a value, where that closes nothing.
#! @param this The checker.
#! @param given The type given.
#! @param param The parameter's type.
#! @returns Whether it is.
def checker::passes(&mut this, given: u32, param: u32): bool {
val none := constant {};
val wanted := this.unalias(param);
val held := this.unalias(given);
if this.types.kind_of(wanted) == ty::reference && this.types.kind_of(held) != ty::reference {
val how := this.fits(held, this.types.first_of(wanted), &none, false, 0);
return how == fit::exact || how == fit::convert;
}
if this.types.kind_of(wanted) != ty::reference && this.types.kind_of(held) == ty::reference
&& this.closes(this.reached(held)) {
return false;
}
val how := this.fits(held, wanted, &none, false, 0);
return how == fit::exact || how == fit::convert;
}
#! Whether a value is a reference written as one, `&x` or `&mut x`, given where
#! a pointer at what it refers to is expected, which it becomes: `&T` a `*T`, and
#! `&mut T` a `*mut T` or a `*T`. Nothing else becomes a pointer without `as`,
#! so taking an address is always written.
#! @param this The checker.
#! @param node The value's node.
#! @param from Its type.
#! @param to The type expected.
#! @returns Whether it is.
pub def checker::lends_pointer(&mut this, node: u32, from: u32, to: u32): bool {
return false if !this.lent(node);
val source := this.unalias(from);
val target := this.unalias(to);
return false if this.types.kind_of(source) != ty::reference || this.types.kind_of(target) != ty::pointer;
return false if this.types.second_of(target) != 0 && this.types.second_of(source) == 0;
val inner_from := this.types.first_of(source);
val inner_to := this.types.first_of(target);
# A reference to a constant, as `&10`, refers to a copy of it of the type
# pointed at.
if this.types.is_untyped(inner_from) {
val none := constant {};
val how := this.fits(inner_from, inner_to, &none, false, 0);
return how != fit::none && how != fit::overflow;
}
return this.unalias(inner_from) == this.unalias(inner_to);
}
#! Whether a reference written as one refers to a value that is no place, as
#! `&10` or `&make()`, which lives to the end of its statement, or as long as a
#! name a reference binds.
#! @param this The checker.
#! @param node The value's node.
#! @returns Whether it does.
pub def checker::lends_temporary(&mut this, node: u32): bool {
if this.kind_of(node) == nk::ternary {
val chosen := this.chosen_arm(node);
return this.lends_temporary(chosen) if chosen != 0;
return this.lends_temporary(this.record(node, if_slot::then)) || this.lends_temporary(this.record(node, if_slot::other));
}
return false if this.kind_of(node) != nk::reference;
val operand := this.left_of(node);
return !this.is_place(operand) && this.types.kind_of(this.unalias(this.type_at(operand))) != ty::reference;
}
#! Reports a pointer kept past its statement at a value that ends with it: a
#! pointer keeps nothing alive, as a reference bound to a name does.
#! @param this The checker.
#! @param node The value's node: a reference written as one, or one cast to a
#! pointer.
pub def checker::refuse_lent_temporary(&mut this, node: u32): void {
val lent := this.kind_of(node) == nk::cast ? this.left_of(node) : node;
return if !this.lends_temporary(lent);
this.report(node, ec::invalid,
"A pointer keeps nothing alive, and this one points at a value that ends with its statement: keep the value in a variable first, or refer to it, as a reference bound to a name keeps it.");
}
#! Reports a value of one type where another is expected.
#! @param this The checker.
#! @param node The value's node.
#! @param from Its type.
#! @param to The type expected.
pub def checker::report_mismatch(&mut this, node: u32, from: u32, to: u32): void {
var text := string::from("Expected `", nothing {});
val want := this.describe(to);
val given := this.describe(from);
text.append(want.span);
text.append("`, but the value is `");
text.append(given.span);
text.append("`.");
this.report_text(node, ec::mismatch, &text);
}
#! The type a value of the type becomes where nothing is expected of it: a
#! number constant its default type, and text a `strview`, or a `string` where
#! std declares no `strview`.
#! @param this The checker.
#! @param kind The type.
#! @returns The type it settles as.
pub def checker::settle(&mut this, kind: u32): u32 {
if this.types.kind_of(kind) == ty::reference && this.types.is_untyped(this.types.first_of(kind)) {
return this.types.reference(this.settle(this.types.first_of(kind)), this.types.second_of(kind) != 0);
}
# A literal is a view of the bytes the program was written with, or a `string`
# where std declares no `strview`.
if this.types.kind_of(kind) == ty::untyped_text {
val view := this.std_type(word::strview, span::<u32>::blank());
return view != bt::error ? view : this.std_type(word::string, span::<u32>::blank());
}
if this.types.kind_of(kind) == ty::null_type || this.types.kind_of(kind) == ty::empty {
return kind;
}
return this.types.settled(kind);
}
#! The type two values share, as the operands of an operator or the branches
#! of a ternary, or the error type where they share none.
#! @param this The checker.
#! @param a One value's type.
#! @param b The other's.
#! @returns The type they share, or the error type.
pub def checker::common(&mut this, a: u32, b: u32): u32 {
return a if a == b;
return b if a == bt::error;
return a if b == bt::error;
val none := constant {};
val a_untyped := this.types.is_untyped(a);
val b_untyped := this.types.is_untyped(b);
if a_untyped && b_untyped {
val a_float := this.types.is_float(a);
val b_float := this.types.is_float(b);
return a if a_float && !b_float;
return b if b_float && !a_float;
return a if this.types.kind_of(a) == ty::untyped_uint;
return b if this.types.kind_of(b) == ty::untyped_uint;
return a if this.types.is_integer(a) && this.types.is_integer(b);
return bt::error;
}
if a_untyped {
val how := this.fits(a, b, &none, false, 0);
return how == fit::none ? bt::error : b;
}
if b_untyped {
val how := this.fits(b, a, &none, false, 0);
return how == fit::none ? bt::error : a;
}
val forward := this.fits(a, b, &none, false, 0);
val backward := this.fits(b, a, &none, false, 0);
return b if forward == fit::convert && backward == fit::none;
return a if backward == fit::convert && forward == fit::none;
return a if forward != fit::none && backward != fit::none;
return bt::error;
}
#! The pack a type parameter stands for, or for one of whose types it stands,
#! as `Ts` or `Ts...[0]`.
#! @param this The checker.
#! @param kind The type.
#! @returns The pack parameter's declaration, or 0 where the type is neither.
def checker::pack_of_param(&mut this, kind: u32): u32 {
return 0 if this.types.kind_of(kind) != ty::param || this.element_ins.length == 0;
val decl := this.types.first_of(kind);
for &each in this.element_ins {
return this.types.first_of(each.pack) if each.param == decl;
return decl if this.types.first_of(each.pack) == decl;
}
return 0;
}