Files
sakurac/analyzer/checker/access.sakura
T

918 lines
37 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file access.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the checking of what reaches into a value: its members, as a field
#! or a property, and its elements, by an index or a slice. Each reaches
#! through references and pointers, so `p.field` and `r[i]` work where `p` and
#! `r` point at what has them.
module sakura::analyzer;
#! What a value of the type is once read through its references, however many
#! there are. A pointer is an address, so nothing is reached through it.
#! @param this The checker.
#! @param kind The type.
#! @returns What it reaches, or the type itself where it is no reference.
pub def checker::reached(&mut this, kind: u32): u32 {
var at := kind;
var steps := 0;
while this.types.kind_of(this.unalias(at)) == ty::reference && steps < 8 {
at = this.types.first_of(this.unalias(at));
steps++;
}
return at;
}
#! Whether what a value of the type reaches through can be written: a `&mut`
#! reference or a `*mut` pointer, where it reaches through one.
#! @param this The checker.
#! @param kind The type.
#! @param object The node of the value, which says whether it can be written
#! where it is no reference.
#! @returns Whether it can be.
def checker::reaches_writable(&mut this, kind: u32, object: u32): bool {
var at := this.unalias(kind);
var steps := 0;
while this.types.kind_of(at) == ty::reference && steps < 8 {
val inner := this.unalias(this.types.first_of(at));
if this.types.kind_of(inner) != ty::reference {
return this.types.second_of(at) != 0;
}
at = inner;
steps++;
}
return this.types.second_of(at) != 0 if this.types.kind_of(at) == ty::pointer;
return this.is_writable(object);
}
#! What a member of a type is: the declaration, and the struct a spread field
#! is reached through.
pub struct checker::found {
pub var decl: u32; #! The member, or 0.
var owner: u32; #! The type it is a member of, given its arguments.
}
#! The member of the name of a value of the type: a field, a property, or a
#! method, found on an alias before what it stands for, on a struct's spread
#! fields after its own, and on a type parameter among its bounds.
#! @param this The checker.
#! @param kind The type.
#! @param name The ID of the name.
#! @returns The member and the type it is of, or no member.
pub def checker::find_member(&mut this, kind: u32, name: u32): checker::found {
return this.find_member_of(kind, name, false);
}
#! The member of the name of the type `::` reaches, as `token::kind`: a type
#! declared through it before a field of the same name, which only a value
#! reaches, and otherwise as `find_member` finds it.
#! @param this The checker.
#! @param kind The type.
#! @param name The ID of the name.
#! @returns The member and the type it is of, or no member.
pub def checker::find_path_member(&mut this, kind: u32, name: u32): checker::found {
return this.find_member_of(kind, name, true);
}
#! The member of the name of the type, with a type declared through it before a
#! field of the same name where `types` says.
#! @param this The checker.
#! @param kind The type.
#! @param name The ID of the name.
#! @param types Whether a type declared through it comes first.
#! @returns The member and the type it is of, or no member.
def checker::find_member_of(&mut this, kind: u32, name: u32, types: bool): checker::found {
this.depend(this.type_decl(this.unalias(kind)));
var at := kind;
var steps := 0;
while steps < 16 {
steps++;
val decl := this.type_decl(at);
if decl != 0 {
val member := this.member_here(decl, name, types);
return checker::found { decl = member, owner = at } if member != 0;
}
match this.types.kind_of(at) {
ty::named => {
val kind_of: dk = this.decls.kinds.p[this.types.first_of(at)];
if kind_of == dk::alias_type {
at = this.alias_target(at);
continue;
}
if kind_of == dk::struct_type {
val spread := this.find_spread(at, name);
return spread if spread.decl != 0;
}
if kind_of == dk::struct_type || kind_of == dk::union_type {
return this.find_default(this.types.first_of(at), name, 0);
}
# A trait has the members of the traits it composes.
if kind_of == dk::trait_type {
val member := this.trait_member(this.types.first_of(at), name);
return checker::found { decl = member, owner = this.declaring_bound(at, member) } if member != 0;
}
return checker::found {};
}
ty::param => return this.find_in_bounds(at, name);
else => return checker::found {};
}
}
return checker::found {};
}
#! The default of the name a trait a struct or union names gives it, or one of
#! those a trait composes gives that trait: a method the trait writes a body
#! for, which the type has as its own where it does not write one itself.
#! @param this The checker.
#! @param decl The struct's, union's, or trait's declaration.
#! @param name The ID of the name.
#! @param depth How deeply composition has been followed.
#! @returns The default and the trait it is of, or no member.
def checker::find_default(&mut this, decl: u32, name: u32, depth: u32): checker::found {
return checker::found {} if depth > 16;
val run := this.composed_of(decl);
for i in 0..this.decls.list_length(run) {
val trait_kind := this.decls.list_at(run, i as u32);
val holder := this.types.first_of(trait_kind);
val member := this.decls.member(holder, name);
if member != 0 && this.decls.kinds.p[member] == dk::function && this.has_body(member)
&& this.trait_asks(member) {
return checker::found { decl = member, owner = trait_kind };
}
val inner := this.find_default(holder, name, depth + 1);
return inner if inner.decl != 0;
}
return checker::found {};
}
#! The field of the name among the fields a struct's spread fields bring in.
#! @param this The checker.
#! @param kind The struct's type.
#! @param name The ID of the name.
#! @returns The field and the struct it is of, or no member.
def checker::find_spread(&mut this, kind: u32, name: u32): checker::found {
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.is_spread(field);
this.resolve(field);
val spread := this.instantiated(this.decls.types.p[field], decl, kind);
val inner := this.renamed(field, name);
val found := this.find_member(spread, inner);
if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::field {
return found;
}
}
return checker::found {};
}
#! Whether a member is seen where checking is. One a module declares on a type
#! of another module, or on a built-in type, is seen within that module and
#! where a file imports it, as `std::math`'s `sqrt` of a `simd` is, and `core`'s
#! everywhere; so what a file means never hangs on what another file of the
#! program imports. A type's own members are seen wherever it is.
#! @param this The checker.
#! @param id The member.
#! @returns Whether it is.
pub def checker::member_seen(&this, id: u32): bool {
var at := id;
var owner: u32 = this.decls.owners.p[id];
# An accessor is seen as its property is.
if owner != 0 && this.decls.kinds.p[owner] == dk::property {
at = owner;
owner = this.decls.owners.p[owner];
}
return true if owner == 0;
val home: u32 = this.decls.modules.p[at];
return true if this.decls.kinds.p[owner] != dk::builtin && this.decls.modules.p[owner] == home;
return true if this.modules.within(this.at.home, home) || this.in_core(home);
val imports: u32 = this.units[this.at.file].imports;
for &imported in this.decls.list_view(imports) {
return true if imported == home;
}
return false;
}
#! Whether a module is `core` or one within it.
#! @param this The checker.
#! @param id The module.
#! @returns Whether it is.
pub def checker::in_core(&this, id: u32): bool {
var at := id;
var steps := 0;
while at != 0 && this.modules.parents[at] != 0 && steps < 64 {
at = this.modules.parents[at];
steps++;
}
return at != 0 && this.modules.names[at] == word::core;
}
#! The first of a member's overloads seen where checking is, or 0.
#! @param this The checker.
#! @param first The first of them, or 0.
#! @returns The overload, or 0.
pub def checker::first_seen(&this, first: u32): u32 {
return first if first == 0 || this.member_seen(first);
var walk := this.decls.walk_overloads(first);
while true {
val at := walk.next();
return 0 if at == 0;
return at if this.member_seen(at);
}
return 0;
}
#! The first of the type's members of the name seen where checking is, with a
#! type declared through it before a field of the same name where `types` says,
#! or 0. One declared outside `core` on `core`'s type is chosen over `core`'s
#! member or type of its name, where it is seen, and never within `core` or for
#! a trait of `core`'s.
#! @param this The checker.
#! @param decl The type's declaration.
#! @param name The ID of the name.
#! @param types Whether a type declared through it comes first.
#! @returns The member, whose overloads follow it, or 0.
pub def checker::member_here(&this, decl: u32, name: u32, types: bool): u32 {
if !this.in_core(this.at.home) && this.cores_own == 0 {
val added := this.first_seen(this.decls.added_member(decl, name));
return added if added != 0;
}
return this.first_seen(types ? this.decls.path_member(decl, name) : this.decls.member(decl, name));
}
#! The overloads a call chooses from: those beginning with `first`, or `core`'s
#! of their name where they were declared outside `core` on `core`'s type and
#! none of them takes the arguments, so a program's overload of `core`'s
#! method is chosen where it fits, and `core`'s elsewhere.
#! @param this The checker.
#! @param first The first of the overloads `member_here` gave.
#! @param object The value a method 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 The first of the overloads chosen from.
pub def checker::overloads_for(&mut this, first: u32, object: u32, owner: u32, through: u32, args: span<u32>): u32 {
val behind := this.core_behind(first);
return first if behind == 0 || this.fits_overload(first, object, owner, through, args);
return behind;
}
#! The first of the overloads a member of a type is among: those declared
#! outside `core` on `core`'s type are kept apart from `core`'s of their name.
#! @param this The checker.
#! @param id The member.
#! @returns The first of them, or 0 where it is no type's member.
pub def checker::first_member(&this, id: u32): u32 {
val owner: u32 = this.decls.owners.p[id];
return 0 if owner == 0;
val name: u32 = this.decls.names.p[id];
val added := this.decls.added_member(owner, name);
return added if added != 0 && !this.in_core(this.decls.modules.p[id]);
return this.decls.member(owner, name);
}
#! `core`'s members of the name seen where checking is, behind those a module
#! outside `core` declared on `core`'s type, or 0 where these are not those.
#! @param this The checker.
#! @param first The first of the members `member_here` gave.
#! @returns The first of `core`'s, or 0.
pub def checker::core_behind(&this, first: u32): u32 {
val holder: u32 = this.decls.owners.p[first];
return 0 if first == 0 || holder == 0 || this.in_core(this.decls.modules.p[first]);
val name: u32 = this.decls.names.p[first];
return 0 if this.decls.added_member(holder, name) == 0;
return this.first_seen(this.decls.member_in(holder, name, false));
}
#! Reports a member named where it is not visible. A field a spread brings in
#! is as visible as the spread field and its own.
#! @param this The checker.
#! @param node The node naming it.
#! @param kind The type it is a member of.
#! @param member The member.
pub def checker::check_reaches(&mut this, node: u32, kind: u32, member: u32): void {
if !this.visible(member) {
this.report_hidden(node, member);
return;
}
return if this.decls.kinds.p[member] != dk::field;
val spread := this.spread_of(kind, this.decls.names.p[member]);
if spread != 0 && !this.visible(spread) {
this.report_hidden(node, spread);
}
}
#! The spread field of a struct a field of the name is brought in by, or 0
#! where the struct declares it itself or has none of the name.
#! @param this The checker.
#! @param kind The struct's type.
#! @param name The ID of the name.
#! @returns The spread field, or 0.
def checker::spread_of(&mut this, kind: u32, name: u32): u32 {
val plain := this.unalias(kind);
return 0 if this.named_kind(plain) != dk::struct_type;
val decl := this.types.first_of(plain);
return 0 if this.decls.member(decl, name) != 0;
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.is_spread(field);
this.resolve(field);
val spread := this.instantiated(this.decls.types.p[field], decl, plain);
val found := this.find_member(spread, this.renamed(field, name));
return field if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::field;
}
return 0;
}
#! Whether a field spreads another struct's fields into its own.
#! @param this The checker.
#! @param field The field's declaration.
#! @returns Whether it does.
pub def checker::is_spread(&this, field: u32): bool {
val syntax := &this.units[this.decls.files.p[field]].syntax;
val record := syntax.node_lefts[this.decls.nodes.p[field]];
val written := syntax.extras[record as u64 + variable_slot::type_expr as u64];
return written != 0 && syntax.node_kinds[written] == nk::spread;
}
#! The name a spread field's struct gives the field a rename calls `name`, as
#! `x` where `x as left` renames it `left`, or `name` itself.
#! @param this The checker.
#! @param field The spread field's declaration.
#! @param name The ID of the name it is reached by.
#! @returns The ID of the name its struct gives it.
def checker::renamed(&this, field: u32, name: u32): u32 {
val syntax := &this.units[this.decls.files.p[field]].syntax;
val record := syntax.node_lefts[this.decls.nodes.p[field]];
val written := syntax.extras[record as u64 + variable_slot::type_expr as u64];
for &rename in syntax.run_view(syntax.node_rights[written]) {
val target := syntax.node_lefts[rename];
if syntax.token_names[syntax.node_tokens[target]] == name {
return syntax.token_names[syntax.node_tokens[rename]];
}
}
return name;
}
#! The member of the name a type parameter has through the traits bounding it.
#! @param this The checker.
#! @param kind The type parameter's type.
#! @param name The ID of the name.
#! @returns The member and the trait it is of, or no member.
def checker::find_in_bounds(&mut this, kind: u32, name: u32): checker::found {
val param := this.types.first_of(kind);
this.resolve(param);
val run: u32 = this.decls.children.p[param];
for i in 0..this.decls.list_length(run) {
val bound := this.decls.list_at(run, i as u32);
val member := this.trait_member(this.types.first_of(bound), name);
return checker::found { decl = member, owner = this.declaring_bound(bound, member) } if member != 0;
}
# What an `if const` proved of it, in the branch it guards.
for i in 0..this.proved_params.length {
continue if this.proved_params[i] != param;
val bound: u32 = this.proved_bounds[i];
val member := this.trait_member(this.types.first_of(bound), name);
return checker::found { decl = member, owner = this.declaring_bound(bound, member) } if member != 0;
}
# Within a trait, `self` has the trait's members.
if this.decls.names.p[param] == names::self_id && this.decls.owners.p[param] != 0 {
val holder: u32 = this.decls.owners.p[param];
val member := this.trait_member(holder, name);
return checker::found { decl = member, owner = this.declaring_bound(this.decls.types.p[holder], member) } if member != 0;
}
return checker::found {};
}
#! The trait, given its arguments, that declares a member a bound has: the
#! bound itself, or a trait it composes, in terms of the bound's arguments, as
#! `into<T>` of `pair<T>: into<T>`.
#! @param this The checker.
#! @param bound The bound's type.
#! @param member The member.
#! @returns The trait's type.
def checker::declaring_bound(&mut this, bound: u32, member: u32): u32 {
val declarer := this.enclosing_trait(member);
return bound if declarer == 0 || this.types.kind_of(this.unalias(bound)) != ty::named;
return this.bound_for(this.unalias(bound), declarer);
}
#! The member of the name a trait has, its own or a trait's it composes.
#! @param this The checker.
#! @param trait_decl The trait's declaration.
#! @param name The ID of the name.
#! @returns The member, or 0.
pub def checker::trait_member(&mut this, trait_decl: u32, name: u32): u32 {
val own := this.decls.member(trait_decl, name);
return own if own != 0 && this.trait_asks(own);
this.resolve_composed(trait_decl);
val held := this.composed.at_or_zero(trait_decl);
val run: u32 = held != 0 ? held - 1 : 0;
for i in 0..this.decls.list_length(run) {
val inner := this.decls.list_at(run, i as u32);
val found := this.trait_member(this.types.first_of(inner), name);
return found if found != 0;
}
return 0;
}
#! Whether a value of the type has a method of the name: its own, through a
#! bound, or one the compiler gives a built-in type.
#! @param this The checker.
#! @param kind The type.
#! @param name The ID of the name.
#! @returns Whether it has.
pub def checker::has_method(&mut this, kind: u32, name: u32): bool {
val found := this.find_member(kind, name);
if found.decl != 0 {
return this.decls.kinds.p[found.decl] == dk::function;
}
return this.builtin_method(kind, name) != 0;
}
#! The type of the method the compiler gives a built-in type, as `equals` of a
#! number, or 0: every number has `equals`, `hash`, `compare`, and the methods
#! of `numeric`, and `bool`, enums, and pointers have `equals` and `hash`, and
#! enums and pointers `compare`. A fixed array has `equals` and `hash` where
#! its elements do.
#! @param this The checker.
#! @param kind The built-in type.
#! @param name The ID of the method's name.
#! @returns The method's type, or 0.
pub def checker::builtin_method(&mut this, kind: u32, name: u32): u32 {
val plain := this.unalias(kind);
# A function, and the address of a C function, are called by an `invoke`
# taking what they take, which makes each `invokable` of its own type.
if name == word::invoke {
val function := this.signature_function(plain);
return 0 if function == 0;
var taken := vector::<u32>::new();
taken.append(this.types.reference(kind, false));
taken.extend(this.types.parts_view(function));
return this.types.function(taken.view(), this.types.first_of(function), this.types.second_of(function) != 0);
}
# A fixed array has `equals` and `hash` where its elements do.
if this.types.kind_of(plain) == ty::fixed {
return 0 if this.types.part_count(plain) != 0 || (name != word::equals && name != word::hash);
return 0 if !this.has_method(this.types.first_of(plain), name);
var taken := vector::<u32>::new();
taken.append(this.types.reference(kind, false));
if name == word::equals {
taken.append(this.types.reference(kind, false));
return this.types.function(taken.view(), bt::boolean, false);
}
return this.types.function(taken.view(), bt::uint64, false);
}
val number := this.types.is_numeric(plain) && !this.types.is_untyped(plain);
val ordered := number || this.named_kind(plain) == dk::enum_type || this.types.kind_of(plain) == ty::pointer;
val equal := ordered || this.types.kind_of(plain) == ty::boolean;
val this_ref := this.types.reference(kind, false);
var params := vector::<u32>::new();
params.append(this_ref);
if name == word::equals && equal {
params.append(this_ref);
return this.types.function(params.view(), bt::boolean, false);
}
if name == word::hash && equal {
return this.types.function(params.view(), bt::uint64, false);
}
if name == word::compare && ordered {
params.append(this_ref);
return this.types.function(params.view(), bt::int64, false);
}
if number && (name == word::add || name == word::sub || name == word::mul
|| name == word::div || name == word::rem) {
params.append(this_ref);
return this.types.function(params.view(), kind, false);
}
if number && name == word::negate {
return this.types.function(params.view(), kind, false);
}
# Integers give their bitwise operators and shifts as methods too, a shift
# taking how far as a `u32`.
val integer := number && this.types.is_integer(plain);
if integer && (name == word::bit_and || name == word::bit_or || name == word::bit_xor) {
params.append(this_ref);
return this.types.function(params.view(), kind, false);
}
if integer && (name == word::shift_left || name == word::shift_right) {
params.append(this.types.reference(bt::uint32, false));
return this.types.function(params.view(), kind, false);
}
if integer && name == word::bit_not {
return this.types.function(params.view(), kind, false);
}
return 0;
}
#! `x.name`: a field, a property, or the length of a fixed array.
#! @param this The checker.
#! @param node The `member` node.
#! @returns The member's type, or the error type where the value has none of the
#! name.
def checker::member(&mut this, node: u32): u32 {
val object := this.left_of(node);
val name := this.name_of(node);
# A pack of types has a length as a pack of values does, as `Ts.length` of
# `def f<Ts...>()`, known in each instance.
if name == word::length && this.kind_of(object) == nk::name {
val named := this.tested_type(object);
if named != 0 && named != bt::error && this.types.kind_of(this.unalias(named)) == ty::pack {
if !this.types.is_open_pack(this.unalias(named)) {
this.set_value(node, constant::integer(this.types.part_count(this.unalias(named)) as u64, false));
}
return bt::uint64;
}
}
# A constant settles as it would alone, so `"text".length` is a `strview`'s.
var kind := this.infer_expr(object);
return bt::error if kind == bt::error;
if this.types.is_untyped(kind) {
kind = this.settle_expr(object);
}
val through := this.reached(kind);
val plain := this.unalias(through);
# Within a template, a length only each instance knows is no constant there.
if this.types.kind_of(plain) == ty::fixed && name == word::length {
if this.types.part_count(plain) == 0 {
this.set_value(node, constant::integer(this.types.second_of(plain) as u64, false));
}
return bt::uint64;
}
# A pack's length is how many types it has, known in each instance.
if this.types.kind_of(plain) == ty::pack {
if name != word::length {
this.report(node, ec::member, "A pack has only `length`; each of its values is reached with `for const` or `args[i]`.");
return bt::error;
}
if !this.types.is_open_pack(plain) {
this.set_value(node, constant::integer(this.types.part_count(plain) as u64, false));
}
return bt::uint64;
}
val found := this.find_member(through, name);
if found.decl == 0 {
this.report_no_member(node, through, name);
return bt::error;
}
this.check_reaches(node, through, found.decl);
# What reflection gives says some of itself while checking.
val one := this.reflected_one(object);
if one != checker::no_reflection {
this.reflected_field(node, one, name);
}
if name == word::length {
val key := this.result_key(object);
if this.lengths.get(&key) is some(got) {
this.set_value(node, constant::integer(got as u64, false));
} else {
val list := this.reflected_at(object);
if list != checker::no_reflection && this.reflected_kind(list) == rk::list as u32 {
this.set_value(node, constant::integer(this.reflected_count(list) as u64, false));
}
}
}
this.set_ref(node, found.decl);
match this.decls.kinds.p[found.decl] {
dk::field => {
this.resolve(found.decl);
val owner := this.types.first_of(found.owner);
val held := this.instantiated(this.decls.types.p[found.decl], owner, found.owner);
val writes := this.reaches_writable(kind, object) && this.decls.has(found.decl, df::mutable);
this.mark_place(node, writes || (this.named_kind(found.owner) == dk::union_type && this.reaches_writable(kind, object)));
if this.types.kind_of(this.unalias(kind)) != ty::reference && this.types.kind_of(this.unalias(kind)) != ty::pointer
&& !this.is_place(object) {
this.clear_flag(node, rf::place);
this.clear_flag(node, rf::writable);
}
return held;
}
dk::property => {
# A property's type names the parameters its receiver did.
this.resolve(found.decl);
val property: u32 = this.decls.types.p[found.decl];
val named := this.types.kind_of(found.owner) == ty::named;
val held := 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.name()`; take it through its type, as `T::name`, for its value.");
return bt::error;
}
else => {
this.report(node, ec::member, "Only fields, properties, and methods are reached with `.`; reach this through its type with `::`.");
return bt::error;
}
}
return bt::error;
}
#! The getter of a property, or 0 where it has none.
#! @param this The checker.
#! @param property The property's declaration.
#! @returns The getter's declaration, or 0.
pub def checker::property_getter(&this, property: u32): u32 {
val run: u32 = this.decls.children.p[property];
for &accessor in this.decls.list_view(run) {
return accessor if !this.decls.has(accessor, df::setter);
}
return 0;
}
#! The setter of a property, or 0 where it has none.
#! @param this The checker.
#! @param property The property's declaration.
#! @returns The setter's declaration, or 0.
pub def checker::property_setter(&this, property: u32): u32 {
val run: u32 = this.decls.children.p[property];
for &accessor in this.decls.list_view(run) {
return accessor if this.decls.has(accessor, df::setter);
}
return 0;
}
#! Asks for the instance of a generic type's property accessor that a value of
#! the type given its arguments reaches, so its body is checked, as a method's
#! called is, rather than only once lowering asks for it.
#! @param this The checker.
#! @param accessor The accessor's declaration, or 0.
#! @param owner The type the property was found on.
#! @param node The node a note is placed at, where too many instances
#! are made.
pub def checker::want_accessor(&mut this, accessor: u32, owner: u32, node: u32): void {
return if accessor == 0;
this.resolve(accessor);
val plain := this.unalias(owner);
return if !this.decls.has(accessor, df::generic) || this.trait_asks(accessor) || this.types.kind_of(plain) != ty::named;
val args := this.types.parts_of(plain);
this.instance_of(accessor, args.view(), node);
}
#! Reports a member a type does not have.
#! @param this The checker.
#! @param node The `member` node.
#! @param kind The type.
#! @param name The ID of the name.
def checker::report_no_member(&mut this, node: u32, kind: u32, name: u32): void {
if this.types.kind_of(this.unalias(kind)) == ty::pointer {
var said := string::from("A pointer is an address, whose only members are `equals`, `compare`, and `hash`. Reach what it points at with `(*p).", nothing {});
val member := this.names.text(name);
said.append(member);
said.append("`, `p[0].");
said.append(member);
said.append("`, or `through(p).");
said.append(member);
said.append("`.");
this.report_text(node, ec::member, &said);
return;
}
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.view());
# A module may declare one on the type that this file does not see.
val decl := this.type_decl(this.unalias(kind));
val unseen := decl == 0 ? 0 : this.decls.member(decl, name);
if unseen != 0 && !this.member_seen(unseen) {
var holder := string::new(nothing {});
this.module_text(this.decls.modules.p[unseen], &mut holder);
text.append("` has `");
text.append(this.names.text(name));
text.append("` where `");
text.append(holder.view());
text.append("` is imported, as `import ");
text.append(holder.view());
text.append(";`, since that module declares it.");
this.report_text(node, ec::member, &text);
return;
}
text.append("` has no field, property, or method `");
text.append(this.names.text(name));
text.append("`.");
this.report_text(node, ec::member, &text);
}
#! `x[i]`: an element of a fixed array, of what a pointer points at, or of what
#! implements `indexable` through its `at`, which is the reference `at` gives.
#! @param this The checker.
#! @param node The `index` node.
#! @returns The element's type, the reference to it `at` gives, or the error
#! type where the value cannot be indexed.
def checker::index(&mut this, node: u32): u32 {
val object := this.left_of(node);
val at := this.right_of(node);
# A constant settles as it would alone, so `"text"[0]` is a `strview`'s.
var kind := this.infer_expr(object);
if kind == bt::error {
this.infer_expr(at);
return bt::error;
}
if this.types.is_untyped(kind) {
kind = this.settle_expr(object);
}
# A reference is indexed through, but a pointer is indexed itself.
val through := this.reached_value(kind);
val plain := this.unalias(through);
match this.types.kind_of(plain) {
ty::fixed => {
this.check_index(at);
# Within a template, a length only each instance knows is theirs to check.
if this.is_known(at) && this.types.part_count(plain) == 0
&& this.value_at(at).magnitude >= this.types.second_of(plain) as u64 {
this.report(at, ec::overflow, "The index is past the end of the array.");
}
this.mark_place(node, this.reaches_writable(kind, object));
# An element of a constant array, at an index known, is known.
val held := this.ref_at(object);
if held != 0 && (held & (ref_local | ref_type)) == 0 && this.ev_arrays.contains(&held) && this.is_known(at) {
val array: ev = this.ev_arrays[held];
val index := this.value_at(at).magnitude;
if index < array.count as u64 {
this.set_value(node, this.ev_heap[array.start as u64 + index]);
}
}
return this.types.first_of(plain);
}
ty::pointer => {
this.check_index(at);
this.mark_place(node, this.types.second_of(plain) != 0);
return this.types.first_of(plain);
}
ty::pack => {
# `args[i]` is one of a pack's values, at an index known while compiling,
# of a type known in each instance.
this.check_index(at);
this.mark_place(node, this.reaches_writable(kind, object));
if this.types.is_open_pack(plain) {
return this.types.param(this.types.first_of(plain));
}
if !this.is_known(at) {
this.report(at, ec::constant, "A pack's values are reached at an index known while compiling.");
return bt::error;
}
val index := this.value_at(at).magnitude;
if index >= this.types.part_count(plain) as u64 {
this.report(at, ec::overflow, "The index is past the last of the pack's values.");
return bt::error;
}
return this.pack_value_type(plain, index as u32);
}
else => {}
}
# What implements `indexable` gives the place its `at` refers to, and the one
# taking `&mut this` where the value can be written. A slice writes what it
# views however it is held, as a `*mut` pointer does.
val writes := this.reaches_writable(kind, object) || this.is_std(this.unalias(through), word::slice);
val method := this.pick_at(through, writes);
if method == 0 {
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.view());
text.append("` is not indexed: a type is by implementing `indexable`.");
this.report_text(node, ec::member, &text);
this.infer_expr(at);
return bt::error;
}
val signature := this.method_type(method, through);
if this.types.part_count(signature) >= 2 {
this.check_expr(at, this.types.part_at(signature, 1));
}
this.set_ref(node, method);
# An element of a list of reflection, at an index known, describes one.
val list := this.reflected_at(object);
if list != checker::no_reflection && this.reflected_kind(list) == rk::list as u32 && this.is_known(at) {
val index := this.value_at(at).magnitude;
if index < this.reflected_count(list) as u64 {
var items := vector::<u32>::new();
items.append(this.reflected_item(list, index as u32));
this.set_reflected(node, rk::one, items.view());
}
}
# `x[k]` is `x.at(k)`: the reference it gives, `&V` or `&mut V`, which is
# read, written, and bound as any reference is, so it copies nothing.
return this.types.first_of(signature);
}
#! Checks an index of an array or a pointer, which is an integer.
#! @param this The checker.
#! @param node The index's node.
def checker::check_index(&mut this, node: u32): void {
val kind := this.infer_expr(node);
return if kind == bt::error;
if this.types.is_untyped(kind) {
this.check_expr(node, bt::uint64);
} else if !this.types.is_integer(this.read_type(kind)) {
this.report_operand(node, kind, "An index is an integer.");
}
}
#! The `at` of a type indexing it: the one taking `&mut this` where `writes`
#! says and there is one, or else the one taking `&this`, or 0.
#! @param this The checker.
#! @param kind The type.
#! @param writes Whether the element is written.
#! @returns The method's declaration, or 0.
pub def checker::pick_at(&mut this, kind: u32, writes: bool): u32 {
val found := this.find_member(kind, word::at);
return 0 if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function;
# Through a type parameter's bound, the accessor that writes is `@optional`, and
# no bound promises it.
val bounded := this.types.kind_of(this.unalias(kind)) == ty::param;
var reads: u32 = 0;
var walk := this.decls.walk_overloads(found.decl);
while true {
val at := walk.next();
break if at == 0;
if this.decls.has(at, df::method) && !(bounded && this.decls.has(at, df::optional)) && this.member_seen(at) {
if this.decls.has(at, df::mut_this) {
return at if writes;
} else if reads == 0 {
reads = at;
}
}
}
return reads != 0 ? reads : (writes ? 0 : found.decl);
}
#! The type of a method of a value of the type, given the type's arguments.
#! @param this The checker.
#! @param method The method's declaration.
#! @param kind The type of the value.
#! @returns The method's type.
pub def checker::method_type(&mut this, method: u32, kind: u32): u32 {
this.resolve(method);
if this.trait_asks(method) {
return this.trait_method_type(method, kind);
}
val plain := this.unalias(kind);
return this.member_function_type(method, this.types.kind_of(plain) == ty::named ? plain : kind);
}
#! `x[start:count]`: a view of the elements of a fixed array, of what a pointer
#! points at, or of another view, which writes where they can be written.
#! @param this The checker.
#! @param node The `slice` node.
#! @returns The view's type, or the error type where the value cannot be sliced.
def checker::slice(&mut this, node: u32): u32 {
val object := this.left_of(node);
val kind := this.infer_expr(object);
val start := this.record(node, slice_slot::start);
val count := this.record(node, slice_slot::count);
if start != 0 { this.check_index(start); }
if count != 0 { this.check_index(count); }
return bt::error if kind == bt::error;
val through := this.reached_value(kind);
val plain := this.unalias(through);
var element: u32 = bt::error;
var writes := false;
match this.types.kind_of(plain) {
ty::fixed => {
element = this.types.first_of(plain);
writes = this.reaches_writable(kind, object);
}
ty::pointer => {
element = this.types.first_of(plain);
writes = this.types.second_of(plain) != 0;
}
else => {
if this.is_std(plain, word::span) || this.is_std(plain, word::slice) {
return plain;
}
this.report_operand(node, kind, "A slice is taken of a fixed array, a pointer, or a view.");
return bt::error;
}
}
var args := vector::<u32>::new();
args.append(element);
return this.std_type(writes ? word::slice : word::span, args.view());
}