#! @file resolve.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the resolving of declarations: what each is, as a function's #! parameters and result, a variable's type, an enum's members, or what an #! alias stands for, worked out where it is first needed. A declaration being #! resolved that is needed again depends on itself, which is reported rather #! than followed. Also contains the resolving of types as they are written. module sakura::analyzer; #! Gives each type declaration its type, which naming it needs nothing else to #! know: the type of its declaration, given its own type parameters. #! @param this The checker. pub def checker::name_types(&mut this): void { for id in 1..this.decls.count { val kind: dk = this.decls.kinds.p[id]; if kind == dk::struct_type || kind == dk::union_type || kind == dk::enum_type || kind == dk::trait_type || kind == dk::alias_type { val own := this.param_types(id as u32); this.decls.types.p[id] = this.types.named(id as u32, own.span); } else if kind == dk::assoc_type { this.decls.types.p[id] = this.types.param(id as u32); } else if kind == dk::type_param { this.decls.types.p[id] = this.decls.has(id as u32, df::packed) ? this.types.pack(span::::blank(), id as u32) : this.types.param(id as u32); } } } #! The types of the declaration's type parameters, as its own body sees them. #! @param this The checker. #! @param id The declaration. #! @returns The type of each of its type parameters, its owner's first. pub def checker::param_types(&mut this, id: u32): vector { var made := vector::::new(); val run: u32 = this.decls.params.p[id]; for ¶m in this.decls.list_view(run) { made.append(this.decls.has(param, df::packed) ? this.types.pack(span::::blank(), param) : this.types.param(param)); } return made; } #! Begins checking within the declaration: in its file and module, with its #! type parameters bound to the types of its arguments, where `args` gives #! them, or else to themselves. Within a trait, `self` is bound too. #! @param this The checker. #! @param id The declaration. #! @param args The pack of its type arguments, or 0 where it is checked as #! itself. #! @remarks The caller keeps `at` beforehand, and gives it to `leave`. pub def checker::enter_decl(&mut this, id: u32, args: u32): void { this.use_table(this.units[this.decls.files.p[id]].table); this.at.home = this.decls.modules.p[id]; this.at.decl = id; this.at.scope = this.scope_of(id, args); this.at.instance = 0; this.at.given = false; this.at.loops = 0; this.at.result = bt::void_type; this.at.marks = this.scope_mark(); this.at.opened = this.at.marks; # Within a trait, `self` is what implements it, and each type it asks for is # named as what that gives. In an instance of a default compiled for each type, # the trait's parameters are its arguments, `self` is the type, and the # default's own parameters follow. In an instance of any other generic trait's # default, the trait's parameters are its arguments, and `self` is the trait # given them. val holder := this.enclosing_trait(id); var skip: u32 = 0; if holder != 0 && holder != id && args != 0 && (this.typed_default(id) || this.constant_default(id)) { skip = this.decls.list_length(this.decls.params.p[holder]); val self_kind := skip < this.types.part_count(args) ? this.types.part_at(args, skip) : bt::error; this.bind(names::self_id, ref_type | self_kind); this.bind_params(holder, args); skip++; } else if holder != 0 && holder != id && args != 0 && this.decls.list_length(this.decls.params.p[holder]) != 0 && !this.receiver_declared(id) { skip = this.decls.list_length(this.decls.params.p[holder]); var given := vector::::new(); for i in 0..skip { given.append(i < this.types.part_count(args) ? this.types.part_at(args, i as u32) : bt::error); } this.bind(names::self_id, ref_type | this.types.named(holder, given.span)); this.bind_params(holder, args); } else if holder != 0 { # Nothing implements a sealed trait, so within it, `self` is the trait, # given the arguments it is checked with, where it is. A function declared # through a trait takes the trait's arguments first, as its own. var self_kind := this.types.param(this.self_decl(holder)); if this.is_sealed(holder) { self_kind = this.decls.types.p[holder]; val count := this.decls.list_length(this.decls.params.p[holder]); if args != 0 && this.types.part_count(args) >= count && (holder == id || this.receiver_declared(id)) { var given := vector::::new(); for i in 0..count { given.append(this.types.part_at(args, i as u32)); } self_kind = this.types.named(holder, given.span); } } this.bind(names::self_id, ref_type | self_kind); if holder != id { this.bind_params(holder, 0); } } # A sealed trait's types and constants are found through it where they are # named, as the members of the type checking is within, each the one it is # given, as the trait's arguments make it. if holder != 0 && !this.is_sealed(holder) { val asked: u32 = this.decls.children.p[holder]; for &member in this.decls.list_view(asked) { if this.decls.kinds.p[member] == dk::assoc_type { this.bind(this.decls.names.p[member], ref_type | this.decls.types.p[member]); } } } # A member declared within its type names the type's parameters, and a type # parameter's bounds name those of what declares it. val kind: dk = this.decls.kinds.p[id]; val owner: u32 = this.decls.owners.p[id]; if owner != 0 && owner != holder && (kind == dk::field || kind == dk::enum_member || kind == dk::assoc_type || kind == dk::type_param) { this.bind_params(owner, 0); } this.bind_params_from(id, args, skip); } #! Ends checking within a declaration, putting back where checking was. #! @param this The checker. #! @param saved Where checking was, as `at` held it before `enter_decl`. pub def checker::leave(&mut this, saved: checker::frame): void { this.unbind_to(this.at.marks); this.at = saved; } #! Binds the names of the declaration's type parameters to the types of the #! arguments `args` gives, or else to the parameters themselves. #! @param this The checker. #! @param id The declaration. #! @param args The pack of its type arguments, or 0 to bind each parameter to #! itself. pub def checker::bind_params(&mut this, id: u32, args: u32): void { this.bind_params_from(id, args, 0); } #! Binds the names of the declaration's type parameters to the types of the #! arguments `args` gives from one on, as an instance of a generic trait's #! default gives its own after the trait's. #! @param this The checker. #! @param id The declaration. #! @param args The pack of type arguments, or 0 to bind each parameter to #! itself. #! @param from The argument its first parameter is given. def checker::bind_params_from(&mut this, id: u32, args: u32, from: u32): void { val run: u32 = this.decls.params.p[id]; for i in 0..this.decls.list_length(run) { val param := this.decls.list_at(run, i as u32); val at := from + i as u32; val kind := args != 0 && at < this.types.part_count(args) ? this.types.part_at(args, at) : this.decls.types.p[param]; this.bind(this.decls.names.p[param], ref_type | kind); } } #! The trait the declaration is, or is a member of, or 0. #! @param this The checker. #! @param id The declaration. #! @returns The trait's declaration, or 0. pub def checker::enclosing_trait(&this, id: u32): u32 { var at := id; var steps := 0; while at != 0 && steps < 4 { return at if this.decls.kinds.p[at] == dk::trait_type; at = this.decls.owners.p[at]; steps++; } return 0; } #! The declaration `self` is within the trait: a type parameter standing for #! whatever implements it. #! @param this The checker. #! @param holder The trait's declaration. #! @returns The declaration of its `self`, made the first time it is asked for. pub def checker::self_decl(&mut this, holder: u32): u32 { if this.selves.get(&holder) is some(got) { return got; } val id := this.decls.add(dk::type_param, names::self_id, this.decls.files.p[holder], this.decls.nodes.p[holder], this.decls.modules.p[holder]); this.decls.own(id, holder); this.decls.types.p[id] = this.types.param(id); this.decls.states.p[id] = ds::resolved; this.selves.set(holder, id); return id; } #! Resolves what the declaration is, where it is not yet resolved. #! @param this The checker. #! @param id The declaration. pub def checker::resolve(&mut this, id: u32): void { this.depend(id); val state: ds = this.decls.states.p[id]; return if state == ds::resolved; # A type parameter is its own type while its bounds are resolved, so a bound # naming it, as `T: ceiling`, is no cycle. return if state == ds::resolving && this.decls.kinds.p[id] == dk::type_param && !this.decls.has(id, df::valued); if state == ds::resolving { this.report_cycle(id); return; } this.decls.states.p[id] = ds::resolving; val saved := this.at; # What is wrong with a declaration is its own, noted wherever it is first # resolved, even where what reached it was being tried quietly. val quiet := this.quiet; this.quiet = 0; this.enter_decl(id, 0); match this.decls.kinds.p[id] { dk::function => this.resolve_function(id); dk::variable => this.resolve_variable(id); dk::field => this.resolve_variable(id); dk::property => this.resolve_property(id); dk::enum_type => this.resolve_enum(id); dk::enum_member => { this.leave(saved); this.decls.states.p[id] = ds::unresolved; this.resolve(this.decls.owners.p[id]); return; } dk::alias_type => { val target := this.resolve_type(this.syn().node_rights[this.decls.nodes.p[id]]); this.decls.targets.p[id] = target; # An alias of an alias is followed now, so one that comes back to itself is # reported where it is declared. if this.is_alias(target) { this.resolve(this.types.first_of(target)); if this.decls.has(id, df::failed) { this.decls.targets.p[id] = bt::error; } } } dk::type_param => { if this.decls.has(id, df::valued) { this.resolve_constant(id); } else { this.resolve_bounds(id, this.left_of(this.decls.nodes.p[id])); this.inherit_bounds(id); } } dk::assoc_type => { val node: u32 = this.decls.nodes.p[id]; this.resolve_bounds(id, this.record(node, assoc_slot::bounds)); # Resolving a type may declare more, which moves the declaration's # vectors, so what is resolved is stored once it is made. val fallback := this.right_of(node); val target: u32 = fallback == 0 ? 0 : this.resolve_type(fallback); this.decls.targets.p[id] = target; } else => {} } this.leave(saved); this.quiet = quiet; this.decls.states.p[id] = ds::resolved; } #! Reports a declaration depending on itself. #! @param this The checker. #! @param id The declaration. def checker::report_cycle(&mut this, id: u32): void { return if this.decls.has(id, df::failed); this.decls.mark(id, df::failed); var text := string::from("`", nothing {}); text.append(this.decl_text(id)); text.append("` depends on itself, so what it is cannot be worked out."); this.report_decl(id, ec::cycle, text.span); } #! Resolves a function's parameters and result into its function type. #! @param this The checker. #! @param id The function's declaration. def checker::resolve_function(&mut this, id: u32): void { val node: u32 = this.decls.nodes.p[id]; if this.decls.has(id, df::accessor) { this.resolve_accessor(id, node); return; } # What a trait's method asks of the trait's parameters and `self` holds within # its signature, as it does within its body. val proved := this.proved_params.length; this.prove_outer(id); # A variadic last parameter may take a pack, as `args: Ts...` does. var params := vector::::new(); val run := this.record(node, function_slot::params); val count := this.run_length(run); for i in 0..count { val param := this.run_at(run, i as u32); if i == 0 && this.decls.has(id, df::method) { params.append(this.types.reference(this.self_type(id), this.decls.has(id, df::mut_this))); # A type written for `this`, as `this: &rect`, is what `this` is, so it is # resolved only for what its names name, which an editor shows. if this.left_of(param) != 0 { this.resolve_quietly(this.left_of(param)); } continue; } val written := this.left_of(param); val kind := this.resolve_type(written); val packs := i as u32 + 1 == count && this.decls.has(id, df::variadic) && this.types.kind_of(kind) == ty::pack; params.append(packs || this.valid_value(written, kind) ? kind : bt::error); } val written := this.record(node, function_slot::result); val result := written == 0 ? bt::void_type : this.result_type(written); this.decls.types.p[id] = this.types.function(params.span, result, this.decls.has(id, df::variadic)); this.unprove_to(proved); } #! The bounds a trait's method puts on the trait's type parameters and `self`, #! as `where self: backward_iterator`, which only that method asks of them: #! a list of each parameter's declaration followed by its bound, worked out the #! first time it is asked for. #! @param this The checker. #! @param method The method's declaration. #! @returns The list, or 0 where it puts none. pub def checker::outer_bounds(&mut this, method: u32): u32 { return 0 if this.decls.kinds.p[method] != dk::function || !this.writes_outer(method); val holder := this.enclosing_trait(method); return 0 if holder == 0 || holder == method; if this.outers.get(&method) is some(got) { return got; } this.outers.set(method, 0); val syntax := &this.units[this.decls.files.p[method]].syntax; val node: u32 = this.decls.nodes.p[method]; val run := syntax.record(node, function_slot::outer); var made := vector::::new(); val saved := this.at; this.enter_decl(method, 0); for &each in this.run_view(run) { val name := this.name_of(each); var param: u32 = name == names::self_id ? this.self_decl(holder) : 0; for &own in this.decls.list_view(this.decls.params.p[holder]) { param = this.decls.names.p[own] == name ? own : param; } continue if param == 0; for &written in this.run_view(this.left_of(each)) { # A trait may be named through an alias of it, as an old name kept. val kind := this.trait_of_alias(this.resolve_type(written)); if kind != bt::error && this.named_kind(kind) != dk::trait_type { this.report(written, ec::not_type, "A bound is a trait."); continue; } continue if kind == bt::error || this.refuse_sealed(written, kind, "nothing satisfies it, and it bounds nothing"); made.append(param); made.append(kind); } } this.leave(saved); val list := this.decls.make_list(made.span); this.outers.set(method, list); return list; } #! Gives the trait's parameters and `self` the bounds a trait's method puts on #! them, where its signature and its own body are checked, as an `if const` #! gives the branch it guards those it proves. #! @param this The checker. #! @param method The method's declaration. pub def checker::prove_outer(&mut this, method: u32): void { val list := this.outer_bounds(method); var i: u32 = 0; while i + 1 < this.decls.list_length(list) { this.proved_params.append(this.decls.list_at(list, i)); this.proved_bounds.append(this.decls.list_at(list, i + 1)); i += 2; } } #! Resolves a type for what its names name, noting nothing of it. #! @param this The checker. #! @param node The type's node. def checker::resolve_quietly(&mut this, node: u32): void { this.quiet++; this.resolve_type(node); this.quiet--; } #! Resolves an accessor of a property: a getter takes `this` and gives the #! property's type, and a setter takes `this` and a value of it. A `get mut` #! takes `&mut this`, and gives a reference the property's type is as one that #! writes, so `first: &T` gives a `&mut T` where the value can be written. #! @param this The checker. #! @param id The accessor's declaration. #! @param node The `accessor` node. def checker::resolve_accessor(&mut this, id: u32, node: u32): void { val property: u32 = this.decls.owners.p[id]; this.resolve(property); val kind: u32 = this.decls.types.p[property]; var params := vector::::new(); params.append(this.types.reference(this.self_type(id), this.decls.has(id, df::mut_this))); val run := this.left_of(node); for i in 1..this.run_length(run) { val param := this.run_at(run, i as u32); val written := this.left_of(param); params.append(written == 0 ? kind : this.resolve_type(written)); } var result: u32 = this.decls.has(id, df::setter) ? bt::void_type : kind; if !this.decls.has(id, df::setter) && this.decls.has(id, df::mut_this) { result = this.writing_reference(kind); } this.decls.types.p[id] = this.types.function(params.span, result, false); } #! The trait an alias stands for, where a bound or a type's traits name one #! through an alias, as a trait renamed keeps its old name as one, or the type #! itself. #! @param this The checker. #! @param kind The type named. #! @returns The trait, or the type. pub def checker::trait_of_alias(&mut this, kind: u32): u32 { return kind if kind == bt::error || this.named_kind(kind) != dk::alias_type; val plain := this.unalias(kind); return this.named_kind(plain) == dk::trait_type ? plain : kind; } #! The type a reference is as one that writes, as `&T` is `&mut T`, or the type #! itself where it is no reference. #! @param this The checker. #! @param kind The type. #! @returns The type, writing. pub def checker::writing_reference(&mut this, kind: u32): u32 { val plain := this.unalias(kind); return kind if this.types.kind_of(plain) != ty::reference || this.types.second_of(plain) != 0; return this.types.reference(this.types.first_of(plain), true); } #! The type `this` refers to within a method: its type, given the method's #! first type parameters, which its receiver named, or `self` within a trait. #! @param this The checker. #! @param id The method's declaration. #! @returns The type `this` refers to. pub def checker::self_type(&mut this, id: u32): u32 { var owner: u32 = this.decls.owners.p[id]; if owner != 0 && this.decls.kinds.p[owner] == dk::property { owner = this.decls.owners.p[owner]; } return bt::error if owner == 0; return this.types.param(this.self_decl(owner)) if this.decls.kinds.p[owner] == dk::trait_type; # The owner's parameters are the first of the method's, from its receiver. val from_property := this.decls.kinds.p[this.decls.owners.p[id]] == dk::property; return this.own_scope(owner, from_property ? this.decls.owners.p[id] : id, 0); } #! Resolves a variable, constant, or field: its type as written, or as its #! value gives it. A constant's value is worked out here, since a type may need #! it, as the length of a fixed array. #! @param this The checker. #! @param id The declaration. def checker::resolve_variable(&mut this, id: u32): void { val node: u32 = this.decls.nodes.p[id]; val written := this.record(node, variable_slot::type_expr); val value := this.record(node, variable_slot::value); var kind: u32 = bt::error; if written != 0 && this.kind_of(written) == nk::spread { kind = this.resolve_type(this.left_of(written)); } else if written != 0 && this.node_has(node, nf::variadic) { kind = this.pack_field_type(id, written); } else if written != 0 { kind = this.value_type(written); } else if value != 0 { # A constant declared through a type is of that type where it fits it, so # `val open_flags::read := 1;` is an `open_flags`. val own := this.infer_expr(value); val owner: u32 = this.decls.owners.p[id]; kind = this.settle(own); if this.types.is_untyped(own) && owner != 0 && this.is_type_decl(owner) && this.decls.kinds.p[owner] != dk::trait_type { val held: u32 = this.decls.types.p[owner]; if this.fits_node(value, held) == fit::constant { kind = held; this.set_convert(value, held); } } else if kind != own && kind != bt::error { this.set_convert(value, kind); } this.set_flag(value, rf::checked); } this.decls.types.p[id] = kind; if this.decls.has(id, df::constant) && value != 0 { if written != 0 { this.check_expr(value, kind); this.set_flag(value, rf::checked); } # A constant's value that folding does not give, as one calling a # function, is run where something asks for it. if this.is_known(value) { this.set_decl_value(id, this.value_at(value)); } } } #! The note for a pack held where only a variadic parameter or a struct's field #! holds one. pub const checker::pack_held: strview = "Only a variadic parameter or a struct's field holds a pack, written `Ts...`, as `items: Ts...`: a pack's values are reached with `for const` or `args[i]`, and passed on with `...`."; #! The type of a field written `name: Ts...`, which holds a value of each of #! the pack's types: a pack, held only by a struct's field. #! @param this The checker. #! @param id The field's or variable's declaration. #! @param written The type's node. #! @returns The pack, or the error type where it is wrongly written. def checker::pack_field_type(&mut this, id: u32, written: u32): u32 { val owner: u32 = this.decls.owners.p[id]; val kind := this.resolve_type(written); return bt::error if kind == bt::error; if this.decls.kinds.p[id] != dk::field || owner == 0 || this.decls.kinds.p[owner] != dk::struct_type { this.report(written, ec::not_type, checker::pack_held); return bt::error; } if this.types.kind_of(this.unalias(kind)) != ty::pack { this.report(written, ec::not_type, "`...` follows the type of a field only where it names a pack, as `items: Ts...`, which holds a value of each of its types."); return bt::error; } val value := this.record(this.decls.nodes.p[id], variable_slot::value); if value != 0 { this.report(value, ec::invalid, "A field holding a pack takes no default: it is given a pack whole, or is zeroed."); return bt::error; } return kind; } #! Resolves a property's type, which is never `void`. #! @param this The checker. #! @param id The property's declaration. def checker::resolve_property(&mut this, id: u32): void { val written := this.record(this.decls.nodes.p[id], property_slot::type_expr); val kind := this.resolve_type(written); if kind == bt::void_type { this.report(written, ec::invalid, "A property holds a value, so its type is not `void`."); } this.decls.types.p[id] = kind; } #! Records the constant a declaration holds. #! @param this The checker. #! @param id The declaration. #! @param value The constant. pub def checker::set_decl_value(&mut this, id: u32, value: constant): void { this.decl_values.put(id, this.value_known(value)); } #! The constant the declaration holds, where it holds one known while compiling. #! @param this The checker. #! @param id The declaration. #! @param out Where the constant is written, where it holds one. #! @returns Whether it holds one. pub def checker::decl_value(&this, id: u32, out: &mut constant): bool { val held := this.decl_values.at_or_zero(id); return false if held == 0; out = this.knowns.p[held].value; return true; } #! Resolves an enum: the integer type holding it, and each member's value, one #! more than the member before unless given one. A member given an earlier #! member's value is another name for it, but one counting up onto it is a #! mistake. #! @param this The checker. #! @param id The enum's declaration. def checker::resolve_enum(&mut this, id: u32): void { val node: u32 = this.decls.nodes.p[id]; val written := this.record(node, enumeration_slot::integer); var integer: u32 = bt::int32; if written != 0 { integer = this.resolve_type(written); if !this.types.is_integer(integer) && integer != bt::error { this.report(written, ec::not_type, "An enum is held as an integer type."); integer = bt::int32; } } this.decls.targets.p[id] = integer; var seen := vector::::new(); var next := constant {}; val run: u32 = this.decls.children.p[id]; for i in 0..this.decls.list_length(run) { val member := this.decls.list_at(run, i as u32); val at: u32 = this.decls.nodes.p[member]; val given := this.right_of(at); var value := next; if given != 0 { this.check_expr(given, integer); if this.is_known(given) { value = this.value_at(given); } else if this.type_at(given) != bt::error { this.report(given, ec::constant, "A member's value is known while compiling: a literal, a constant, or another member."); } } else { for &earlier in seen { if this.compare_constants(earlier, &value) == 0 { this.report(at, ec::duplicate, "The member counts up onto the value of an earlier one; give it a value of its own."); break; } } if !this.in_range(integer, &value) { var text := string::from("The member's value is outside the range of `", nothing {}); val named := this.describe(integer); text.append(named.span); text.append("`."); this.report_text(at, ec::overflow, &text); } } seen.append(value); this.set_decl_value(member, value); this.decls.types.p[member] = this.decls.types.p[id]; this.decls.states.p[member] = ds::resolved; this.fold_add(&value, 1, false, &mut next); } } #! Resolves the bounds of a type parameter, or of a type a trait asks for, each #! a trait. #! @param this The checker. #! @param id The type parameter's declaration. #! @param run The run of the nodes of its bounds. def checker::resolve_bounds(&mut this, id: u32, run: u32): void { var bounds := vector::::new(); for &written in this.run_view(run) { val kind := this.trait_of_alias(this.resolve_type(written)); if kind != bt::error && this.named_kind(kind) != dk::trait_type { this.report(written, ec::not_type, "A bound is a trait."); continue; } continue if this.refuse_sealed(written, kind, "nothing satisfies it, and it bounds nothing"); bounds.append(kind); } this.decls.children.p[id] = this.decls.make_list(bounds.span); } #! Resolves the type of a constant parameter's value: the type written for it, or #! where a receiver names one of its type's, the type that declares it. A #! constant is of its type, so it takes no bounds, and of a type that closes #! nothing, since instances are told apart by it and it is copied wherever it is #! read. #! @param this The checker. #! @param id The constant parameter's declaration. def checker::resolve_constant(&mut this, id: u32): void { val node: u32 = this.decls.nodes.p[id]; if this.run_length(this.left_of(node)) != 0 { this.report(this.run_at(this.left_of(node), 0), ec::generic, "A constant parameter is a value of the type written for it, so it takes no bounds."); } # The length a pattern, `L[N]`, names is a `u64`, as the lengths of arrays are. val written: u32 = this.kind_of(node) == nk::const_param ? this.right_of(node) : 0; var kind: u32 = written != 0 ? this.value_type(written) : this.kind_of(node) == nk::const_param ? bt::uint64 : bt::error; if this.mirrors.at_or_zero(id) != 0 { val source: u32 = this.mirrors.at_or_zero(id); this.resolve(source); # The type's parameters are the parameters of the declaration standing for them. var from := vector::::new(); var to := vector::::new(); val holder: u32 = this.decls.owners.p[id]; val run: u32 = this.decls.params.p[holder]; for ¶m in this.decls.list_view(run) { if this.mirrors.at_or_zero(param) != 0 { from.append(this.mirrors.at_or_zero(param)); to.append(this.decls.types.p[param]); } } val given := this.substitute(this.decls.targets.p[source], from.span, to.span); if written != 0 && kind != given && kind != bt::error && given != bt::error { var text := string::from("The constant is of `", nothing {}); val named := this.describe(given); text.append(named.span); text.append("` where its type declares it, so it is written that way here, or by its name alone."); this.report_text(written, ec::generic, &text); } this.decls.targets.p[id] = given; return; } this.decls.targets.p[id] = kind; if written != 0 && kind != bt::error && !this.types.is_open(kind) { this.check_constant_type(written, kind); } } #! Reports a type no constant can be of: `void`, a trait, the address of a C #! function, and what closes what it holds. #! @param this The checker. #! @param node The node a note is placed at. #! @param kind The type. #! @returns Whether a constant can be of it. pub def checker::check_constant_type(&mut this, node: u32, kind: u32): bool { val plain := this.unalias(kind); if this.is_std(plain, word::cfn) { this.report(node, ec::generic, "The address of a C function is only known once the program runs, so it is no constant."); return false; } if this.types.kind_of(plain) == ty::pack { this.report(node, ec::generic, "A pack is many values, so it is no constant."); return false; } if this.closes(kind) { var text := string::from("`", nothing {}); val named := this.describe(kind); text.append(named.span); text.append("` closes what it holds, so it is no constant: a constant parameter is given a value that closes nothing, such as a number, text, or a struct of them."); this.report_text(node, ec::generic, &text); return false; } return true; } #! Gives a type parameter a receiver names, as `T` of `box::get`, the #! bounds of the type's parameter it stands for, in terms of the declaration's #! own parameters. #! @param this The checker. #! @param id The type parameter's declaration. def checker::inherit_bounds(&mut this, id: u32): void { val source: u32 = this.mirrors.at_or_zero(id); return if source == 0; this.resolve(source); val inherited: u32 = this.decls.children.p[source as u32]; return if this.decls.list_length(inherited) == 0; # The type's parameters become the parameters of the declaration standing for them. val holder: u32 = this.decls.owners.p[id]; var from := vector::::new(); var to := vector::::new(); val run: u32 = this.decls.params.p[holder]; for ¶m in this.decls.list_view(run) { if this.mirrors.at_or_zero(param) != 0 { from.append(this.mirrors.at_or_zero(param)); to.append(this.decls.types.p[param]); } } var bounds := this.decls.list_of(this.decls.children.p[id]); for i in 0..this.decls.list_length(inherited) { bounds.append(this.substitute(this.decls.list_at(inherited, i as u32), from.span, to.span)); } this.decls.children.p[id] = this.decls.make_list(bounds.span); } #! The type a type is written as. #! @param this The checker. #! @param node The type's node. #! @returns The type, or the error type where it is wrongly written. pub def checker::resolve_type(&mut this, node: u32): u32 { return bt::error if node == 0; match this.kind_of(node) { nk::type_name => { # An alias that works out to a mistake, as a `match const` no arm of which # fits, was reported where it is worked out, and is one here too. val made := this.resolve_type_name(node); return made if made == bt::error || this.types.is_open(made) || !this.aliases(made); return this.unalias(made) == bt::error ? bt::error : made; } nk::type_ref, nk::type_ptr => { val inner := this.resolve_type(this.left_of(node)); val mutable := this.node_has(node, nf::mutable); # A reference to a trait dispatches through what the trait asks for, # which a trait only a bound cannot do, and a sealed one asks for nothing. val plain := this.unalias(inner); if this.refuse_sealed(this.left_of(node), plain, "nothing implements it for a reference or pointer to refer to") { return bt::error; } if this.named_kind(plain) == dk::trait_type && this.bound_only(this.types.first_of(plain)) { var text := string::from("`", nothing {}); text.append(this.decl_text(this.types.first_of(plain))); text.append("` names `self`, asks for types, or has generic methods, or composes a trait that does, which only what implements it gives, so it is only a bound, written in a `where` clause, and never the type of a value or reference."); this.report_text(this.left_of(node), ec::not_type, &text); return bt::error; } return this.kind_of(node) == nk::type_ref ? this.types.reference(inner, mutable) : this.types.pointer(inner, mutable); } nk::type_match => return this.resolve_type_match(node); nk::type_cond => return this.resolve_type_cond(node); nk::type_fixed => { return this.pack_element(node) if this.token_kind(node) == tk::spread; val element := this.value_type(this.left_of(node)); var length: u32 = 0; val count := this.const_length(this.right_of(node), &mut length); if length != 0 { return element == bt::error ? bt::error : this.types.fixed_of(element, length); } return element == bt::error || count == checker::no_length ? bt::error : this.types.fixed(element, count); } nk::type_func => { # The last parameter written `T...` takes any number of arguments, or where # `T` is a pack, one of each of its types. var params := vector::::new(); var variadic := false; val run := this.left_of(node); val count := this.run_length(run); for i in 0..count { val each := this.run_at(run, i as u32); if this.kind_of(each) == nk::type_spread { if i as u32 + 1 != count { this.report(this.left_of(each), ec::invalid, "Only the last parameter type is written `T...`, since it takes every argument after those before it."); } # A pack whose types are known, as an instance's `Ts`, gives a parameter # of each of its types, as substituting it into the type would, and the # function is variadic only where the pack is a variadic function's. val spread := this.resolve_type(this.left_of(each)); if this.types.is_known_pack(spread) && i as u32 + 1 == count { params.extend(this.types.parts_view(spread)); variadic = this.types.second_of(spread) != 0; continue; } params.append(spread); variadic = i as u32 + 1 == count; continue; } val made := this.resolve_type(each); if this.types.kind_of(made) == ty::pack { this.report(each, ec::invalid, "A pack is the last parameter type, written `Ts...`."); params.append(bt::error); continue; } params.append(made); } return this.types.function(params.span, this.resolve_type(this.right_of(node)), variadic); } nk::type_spread => { this.report(node, ec::invalid, "`...` follows only the last parameter type of a function type, or a pack passed on among type arguments."); return bt::error; } nk::type_of => { # The type of what a description known while compiling describes. val named := this.left_of(node); this.infer_expr(named); val one := this.reflected_one(named); if one == checker::no_reflection { if !this.waits_for_instance() { this.unsupported(node); } return bt::error; } return this.reflected_type(one); } else => return bt::error; } return bt::error; } #! What `const_length` gives for a length that is not one. pub const checker::no_length: u32 = 0xFFFFFFFF; #! The type a value is written as, which is no bare trait, and not `void`. #! @param this The checker. #! @param node The type's node. #! @returns The type, or the error type where it is wrong or no value can be of #! it. pub def checker::value_type(&mut this, node: u32): u32 { val kind := this.resolve_type(node); return this.valid_value(node, kind) ? kind : bt::error; } #! `Ts...[i]`: a pack's type at an index known while compiling, as #! `T::args...[0]` of a `generic`. Within a generic's own body, where the pack's #! types are not known, it stands for the one each instance gives, as `args[i]` #! does for a pack of values, bounded as each of the pack's types is. #! @param this The checker. #! @param node The `type_fixed` node whose token is the `...`. #! @returns The type, or the error type where it names none. def checker::pack_element(&mut this, node: u32): u32 { val pack := this.resolve_type(this.left_of(node)); return bt::error if pack == bt::error; val plain := this.unalias(pack); if this.types.kind_of(plain) != ty::pack { this.report(this.left_of(node), ec::not_type, "Only a pack's types are reached with `...[i]`, as `Ts...[0]`: an array's length is written without `...`."); return bt::error; } val at := this.right_of(node); val kind := this.infer_expr(at); return bt::error if kind == bt::error; if !this.types.is_integer(this.unalias(this.read_type(kind))) { this.report_operand(at, kind, "A pack's types are reached at an integer index."); return bt::error; } # An index worked out from constant parameters, as `I` of `Ts...[I]`, stands # for the type each instance has there. One each instance works out # otherwise, as from `T::length`, waits for the instance, which checks it. if !this.is_known(at) { val worked := this.known_of(at, this.settle(kind), 0); if worked != 0 && this.known_depends(worked) { return this.element_at(plain, this.known_type(worked)); } return bt::error if this.waits_for_instance(); this.report(at, ec::constant, "A pack's types are reached at an index known while compiling."); return bt::error; } val index := this.value_at(at); if this.types.is_open_pack(plain) && !index.negative { return this.element_param(plain, index.magnitude as u32); } if index.negative || index.magnitude >= this.types.part_count(plain) as u64 { this.report(at, ec::overflow, "The index is past the last of the pack's types."); return bt::error; } val part := this.types.part_at(plain, index.magnitude as u32); if this.is_constant_arg(part) { this.report(node, ec::not_type, "The pack holds a constant at the index, not a type."); return bt::error; } return part; } #! The type a function's result is written as, which may be `void`. #! @param this The checker. #! @param node The type's node, or 0 for `void`. #! @returns The type, or the error type where it is wrongly written. pub def checker::result_type(&mut this, node: u32): u32 { val kind := this.resolve_type(node); return kind if kind == bt::void_type; return this.valid_value(node, kind) ? kind : bt::error; } #! Whether a value can be of the type, reporting a trait used as the type of a #! value rather than through a reference or as a bound. `void` is a value's type #! too, of the one value holding nothing, which a function giving nothing gives. #! @param this The checker. #! @param node The node a note is placed at. #! @param kind The type. #! @returns Whether it can be. pub def checker::valid_value(&mut this, node: u32, kind: u32): bool { if this.named_kind(this.unalias(kind)) == dk::trait_type { this.report(node, ec::not_type, "A trait is no value's type: refer to a value implementing it with `&`, or bound a type parameter by it."); return false; } if this.types.kind_of(kind) == ty::pack { this.report(node, ec::not_type, "A pack is the type only of a variadic parameter or a struct's field, written `Ts...`, as `args: Ts...`, and is passed on with `...`."); return false; } return true; } #! Whether a trait's method writes bounds on the trait's type parameters or #! `self`, which only a type implementing it meets. #! @param this The checker. #! @param method The method's declaration. #! @returns Whether it does. pub def checker::writes_outer(&this, method: u32): bool { return false if this.decls.has(method, df::accessor); val syntax := &this.units[this.decls.files.p[method]].syntax; val node: u32 = this.decls.nodes.p[method]; return syntax.node_lefts[node] != 0 && syntax.record(node, function_slot::outer) != 0; } #! Whether a trait is only a bound: it names `self`, asks for types, or has #! generic methods or ones bounding its parameters, or composes a trait that #! does, which only what implements it gives, and no table holds. #! @param this The checker. #! @param holder The trait's declaration. #! @returns Whether it is only a bound. pub def checker::bound_only(&mut this, holder: u32): bool { if this.only_bounds.get(&holder) is some(got) { return got; } this.only_bounds.set(holder, false); var only := false; var skipped := false; val self_kind := this.types.param(this.self_decl(holder)); val run: u32 = this.decls.children.p[holder]; for i in 0..this.decls.list_length(run) { val member := this.decls.list_at(run, i as u32); if this.decls.kinds.p[member] == dk::assoc_type { only = true; break; } # A method being resolved, as one taking a reference to its own trait, is # passed over, and what is found is not kept, to be worked out again. if this.decls.kinds.p[member] == dk::function && this.decls.states.p[member] == ds::resolving { skipped = true; continue; } # A method's `this` refers to what implements the trait through the trait's # reference, so only `self` elsewhere makes the trait only a bound. if this.decls.kinds.p[member] == dk::function { if (this.decls.list_length(this.decls.params.p[member]) != 0 || this.writes_outer(member)) && this.trait_asks(member) { only = true; break; } this.resolve(member); val kind: u32 = this.decls.types.p[member]; val from: u32 = this.decls.has(member, df::method) ? 1 : 0; for p in from..this.types.part_count(kind) { only = only || this.mentions(this.types.part_at(kind, p as u32), self_kind, 0); } only = only || this.mentions(this.types.first_of(kind), self_kind, 0); break if only; } } val composed := this.composed_of(holder); for i in 0..this.decls.list_length(composed) { only = only || this.bound_only(this.types.first_of(this.decls.list_at(composed, i as u32))); } if skipped && !only { this.only_bounds.remove(&holder); } else { this.only_bounds.set(holder, only); } return only; } #! Whether the type holds the type `target` anywhere within it. #! @param this The checker. #! @param kind The type to look within. #! @param target The type to look for. #! @param depth How deeply the search has gone, which stops a type nesting #! without end. #! @returns Whether it holds it. pub def checker::mentions(&this, kind: u32, target: u32, depth: u32): bool { return true if kind == target; return false if depth > 16 || !this.types.is_open(kind); match this.types.kind_of(kind) { ty::reference, ty::pointer => return this.mentions(this.types.first_of(kind), target, depth + 1); ty::fixed => { return true if this.types.part_count(kind) != 0 && this.mentions(this.types.part_at(kind, 0), target, depth + 1); return this.mentions(this.types.first_of(kind), target, depth + 1); } else => {} } for &part in this.types.parts_view(kind) { return true if this.mentions(part, target, depth + 1); } return this.types.kind_of(kind) == ty::function && this.mentions(this.types.first_of(kind), target, depth + 1); } #! The length of a fixed array, an integer known while compiling, or #! `no_length` where it is not one. Within a template, one worked out from its #! constant parameters is known only to each instance, and is given as the #! constant standing for it instead. #! @param this The checker. #! @param node The length's node. #! @param dependent Where such a constant is written, or 0 where there is none. #! @returns The length, or `no_length`. def checker::const_length(&mut this, node: u32, dependent: &mut u32): u32 { val kind := this.infer_expr(node); return checker::no_length if kind == bt::error; if !this.types.is_integer(this.unalias(this.read_type(kind))) && this.named_kind(this.unalias(kind)) != dk::enum_type { this.report_operand(node, kind, "The length of a fixed array is an integer."); return checker::no_length; } if !this.is_known(node) { val worked := this.known_of(node, this.settle(kind), 0); if worked != 0 && this.known_depends(worked) { dependent = this.known_type(worked); return checker::no_length; } } # A constant a type parameter's bound asks for, as `T::length` of an `array`, # is known in each instance, which checks the length. if !this.is_known(node) && this.waits_for_instance() && this.reads_param_constant(node) { return checker::no_length; } if !this.is_known(node) { var answer := evaluation_answer {}; if !this.run_value(node, &mut answer) { # Without an evaluator, or with an error that keeps the program from # running, it is not known, which says nothing more. if !answer.noted && !this.failed && this.evaluator is some(_) { this.report(node, ec::constant, "The length of a fixed array is known while compiling: a literal, a constant, or what a function gives of them."); } return checker::no_length; } } val value := this.value_at(node); if value.negative || value.magnitude > 0x1000000 { this.report(node, ec::limit, "A fixed array holds from 0 to 16777216 elements."); return checker::no_length; } return value.magnitude as u32; } #! Whether an expression reads a constant through a type parameter, as #! `T::length`. #! @param this The checker. #! @param node The expression. #! @returns Whether it does. def checker::reads_param_constant(&mut this, node: u32): bool { var todo := vector::::new(); todo.append(node); while todo.length != 0 { val at: u32 = todo.last; todo.truncate(); continue if at == 0; if this.kind_of(at) == nk::path && this.segment_count(at) > 1 && this.param_named(this.segment_name(at, 0)) != 0 { return true; } this.syn().children(at, &mut todo); } return false; } #! The type a named type is written as, as `std::list` or `T`. #! @param this The checker. #! @param node The `type_name` node. #! @returns The type, or the error type where it names none. pub def checker::resolve_type_name(&mut this, node: u32): u32 { var decl: u32 = 0; var home: u32 = 0; var in_module := false; var args := vector::::new(); 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 { val kind := bound & ref_mask; if this.is_constant_arg(kind) { var text := string::from("`", nothing {}); text.append(this.names.text(name)); text.append("` is a constant, not a type."); this.report_text(segment, ec::not_type, &text); return bt::error; } this.set_ref(segment, bound); if count == 1 { this.no_type_args(node, 0); return kind; } return this.projection(kind, node, 1, this.param_named(name)); } # A module the first of several segments names comes before a declaration of # the name, as `serial::options` names the module's type where `serial` is # also the attribute it declares, unless the module declares nothing of the # next name and a type of the first is seen, as `shape::corner` of a module # `shape` declaring the type `shape`. if count > 1 { home = this.lookup_module(name); in_module = home != 0 && !this.type_over_module(home, node); } # A type a type around it gives takes that type's arguments first, as # `node` within `list` is `list::node`. if !in_module { val found := this.lookup_named(name, segment, true); decl = found.decl; if found.owner != 0 && this.is_type_decl(decl) && this.types.kind_of(found.owner) == ty::named { args.extend(this.types.parts_view(found.owner)); } } if decl == 0 && !in_module { home = this.lookup_module(name); in_module = home != 0; if !in_module { var text := string::from("No type `", nothing {}); text.append(this.names.text(name)); text.append("` is declared where it is written."); this.report_text(segment, ec::undefined, &text); return bt::error; } } } else if in_module { decl = this.modules.decl(home, name); if 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 bt::error; } continue; } in_module = false; } else { # A type the program declares through `core`'s, of a name `core` gives it # too, is the program's outside `core`. val added := this.in_core(this.at.home) || this.cores_own != 0 ? 0 : this.decls.added_member(decl, name); val member := added != 0 && this.is_type_decl(added) ? added : this.decls.path_member(decl, name); if member == 0 || !this.is_type_decl(member) { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); text.append("` declares no type `"); text.append(this.names.text(name)); text.append("`."); this.report_text(segment, ec::undefined, &text); return bt::error; } decl = member; } # What each segment names is kept for the editor, which shows it and finds # where it is declared. this.set_ref(segment, decl); val run := this.segment_args(node, i as u32); for &arg in this.run_view(run) { args.append(this.type_arg_of(arg, decl, args.length as u32, args.span)); } } if in_module { this.report(node, ec::not_type, "A module is not a type."); return bt::error; } if !this.is_type_decl(decl) { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); text.append("` is a "); text.append(this.decl_word(decl)); text.append(", not a type."); this.report_text(node, ec::not_type, &text); return bt::error; } if !this.visible(decl) { this.report_hidden(node, decl); } # A sealed trait's type is its own, as the trait given its arguments makes it, # as `remref::result`. if this.decls.kinds.p[decl] == dk::assoc_type && this.is_sealed(this.decls.owners.p[decl]) { val holder: u32 = this.decls.owners.p[decl]; val owner := this.named_of(holder, args.span, node); return owner == bt::error ? bt::error : this.sealed_type(decl, owner, 0); } return this.named_of(decl, args.span, node); } #! The type of the declaration given the type arguments, reporting arguments #! that are wrong in number or do not satisfy their bounds. #! @param this The checker. #! @param decl The declaration of the type. #! @param args Its type arguments. #! @param node The node notes are placed at. #! @returns The type, or the error type where the arguments are wrong. pub def checker::named_of(&mut this, decl: u32, args: span, node: u32, given: bool = false): u32 { match this.decls.kinds.p[decl] { dk::builtin, dk::type_param => { if args.length != 0 { this.report(node, ec::generic, "The type takes no type arguments."); } return this.decls.types.p[decl]; } dk::assoc_type => return this.decls.types.p[decl]; else => {} } # What names a type depends on it, and reaches its fields' defaults. this.depend(decl); val params: u32 = this.decls.params.p[decl]; val wanted := this.decls.list_length(params); for &arg in args { return bt::error if arg == bt::error; } # What is written gives each type parameter its argument: one each, the rest to # a pack, and a pattern's the parts of its function type. What inference gives # already has one for each. var grouped := vector::::new(); if given { grouped.extend(args); if grouped.length != wanted as u64 { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); val words := $"` takes {wanted} type arguments, but is given {args.length}."; text.append(words.span); this.report_text(node, ec::generic, &text); return bt::error; } } else { return bt::error if !this.bind_written(decl, args, node, &mut grouped); } # A pack is given only for a pack parameter, as the pack it is. for i in 0..wanted { val is_pack := this.types.kind_of(grouped[i]) == ty::pack; if is_pack != this.decls.has(this.decls.list_at(params, i as u32), df::packed) { this.report(node, ec::generic, is_pack ? "A pack is passed on with `...` only for a pack parameter, as `Ts` of `box`." : "A pack parameter is given types, or a pack passed on as `Ts...`."); return bt::error; } } val made := this.types.named(decl, grouped.span); if grouped.length != 0 { this.check_bounds(decl, made, node); } # An alias of `match const` stands for the type it chooses. return this.chosen_type(made, 0); } #! The pack a pack parameter is given by the type arguments from `from` on: the #! types, with each pack passed on as `Ts...` giving its types in their place. A #! pack whose types are not known is only passed on alone, since where the types #! after it go is not known until it is. #! @param this The checker. #! @param args The type arguments. #! @param from The first the pack takes. #! @param node The node notes are placed at. #! @returns The pack, or the error type. def checker::gather_pack(&mut this, args: span, from: u64, node: u32): u32 { var items := vector::::new(); for i in from..args.length { val each := args[i]; if this.types.kind_of(each) != ty::pack { items.append(each); continue; } if this.types.is_open_pack(each) { if args.length - from != 1 { if node != 0 { this.report(node, ec::generic, "A pack whose types are not known yet is passed on alone, as the whole of what a pack parameter is given."); } return bt::error; } return each; } items.extend(this.types.parts_view(each)); } return this.types.pack(items.span, 0); } #! Reports type arguments given to what takes none. #! @param this The checker. #! @param node The `type_name` node. #! @param index The segment that names what takes none. def checker::no_type_args(&mut this, node: u32, index: u32): void { if this.segment_args(node, index) != 0 { this.report(node, ec::generic, "A type parameter takes no type arguments."); } } #! The type the segments of a type name from `from` name within the type: a #! type a bound asks of a type parameter, as `C::iterator` where #! `C: iterable`, or a nested type, as `iterator` of `vector::iterator`. #! In an instance, the type parameter is the type it was given, which gives the #! type its bound asks for, or else the bound's default, as `storage` of #! `type storage = self`. #! @param this The checker. #! @param kind The type the name is within. #! @param node The `type_name` node. #! @param from The first segment to resolve within the type. #! @param param The type parameter the first segment names, or 0. #! @returns The type named, or the error type where the type has none of a name. def checker::projection(&mut this, kind: u32, node: u32, from: u32, param: u32): u32 { var at := kind; for i in from..this.segment_count(node) { val name := this.segment_name(node, i as u32); val segment := this.segment_node(node, i as u32); var made := this.member_type(at, name); if made == bt::error && i == from as u64 && param != 0 && this.types.kind_of(this.unalias(at)) != ty::param { made = this.asked_of_bounds(at, param, name); } if made == bt::error { var text := string::from("`", nothing {}); val named := this.describe(at); text.append(named.span); text.append("` has no type `"); text.append(this.names.text(name)); text.append("`."); this.report_text(segment, ec::undefined, &text); return bt::error; } at = made; } return at; } #! The type parameter of the name that the declaration being checked, or one #! it is declared within, declares. #! @param this The checker. #! @param name The ID of the name. #! @returns The type parameter's declaration, or 0. def checker::param_named(&this, name: u32): u32 { var at := this.at.decl; var steps := 0; while at != 0 && steps < 4 { val run: u32 = this.decls.params.p[at]; for ¶m in this.decls.list_view(run) { return param if this.decls.names.p[param] == name; } at = this.decls.owners.p[at]; steps++; } return 0; } #! The type a type given for a type parameter gives for one the parameter's #! bounds ask for, or those an `if const` the instance decided gave it: its own #! of the name, or the bound's default. #! @param this The checker. #! @param kind The type given. #! @param param The type parameter's declaration. #! @param name The ID of the name. #! @returns The type, or the error type where no bound asks for one of the name. def checker::asked_of_bounds(&mut this, kind: u32, param: u32, name: u32): u32 { this.resolve(param); val run: u32 = this.decls.children.p[param]; for i in 0..this.decls.list_length(run) { val made := this.asked_of_bound(kind, this.decls.list_at(run, i as u32), name); return made if made != 0; } for i in 0..this.proved_params.length { continue if this.proved_params[i] != param; val made := this.asked_of_bound(kind, this.proved_bounds[i], name); return made if made != 0; } return bt::error; } #! The type a type gives for one of the name a bound asks for. #! @param this The checker. #! @param kind The type. #! @param bound The bound. #! @param name The ID of the name. #! @returns The type, or 0 where the bound asks for none of the name. def checker::asked_of_bound(&mut this, kind: u32, bound: u32, name: u32): u32 { return 0 if this.types.kind_of(bound) != ty::named; val member := this.trait_member(this.types.first_of(bound), name); return 0 if member == 0 || this.decls.kinds.p[member] != dk::assoc_type; return this.asked_type(kind, member, this.declaring_bound(bound, member)); } #! The type of the name a type has: what a bound asks of a type parameter, or #! a type nested in it, given its arguments, or the error type. #! @param this The checker. #! @param kind The type. #! @param name The ID of the name. #! @returns The type, or the error type where it has none of the name. pub def checker::member_type(&mut this, kind: u32, name: u32): u32 { val plain := this.unalias(kind); if this.types.kind_of(plain) == ty::param { val found := this.find_in_bounds(plain, name); return bt::error if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::assoc_type; return this.projected(plain, found.decl, found.owner); } val found := this.find_path_member(kind, name); return bt::error if found.decl == 0 || !this.is_type_decl(found.decl); # A sealed trait's type is its own, as `self::result` within it. if this.decls.kinds.p[found.decl] == dk::assoc_type && this.is_sealed(this.decls.owners.p[found.decl]) && this.named_kind(found.owner) == dk::trait_type { return this.sealed_type(found.decl, found.owner, 0); } this.resolve(found.decl); return this.instantiated(this.decls.types.p[found.decl], found.decl, found.owner); } #! A type of a sealed trait, given the trait's arguments: the one it is given, #! as they make it, as `remref<&u8>::result` is `u8` where it is given #! `type result = T is reference ? T::target : T;`. #! @param this The checker. #! @param member The type's declaration. #! @param owner The trait, given its arguments. #! @param depth How many of the trait's types it is worked out within. #! @returns The type, or the error type. pub def checker::sealed_type(&mut this, member: u32, owner: u32, depth: u32): u32 { if depth > 16 { this.report_decl(member, ec::limit, "The sealed trait's types name each other without end."); return bt::error; } this.resolve(member); val target: u32 = this.decls.targets.p[member]; return bt::error if target == 0; return this.sealed_given(target, owner, depth); } #! A type written within a sealed trait, as the trait given its arguments sees #! it: its parameters those arguments, and each of its types named in it the #! one it is given. #! @param this The checker. #! @param kind The type. #! @param owner The trait, given its arguments. #! @param depth How many of the trait's types it is worked out within. #! @returns The type, or the error type. pub def checker::sealed_given(&mut this, kind: u32, owner: u32, depth: u32): u32 { return kind if kind == bt::error || !this.types.is_open(kind); return bt::error if owner == bt::error || this.types.kind_of(owner) != ty::named; val holder := this.types.first_of(owner); var from := this.decls.list_of(this.decls.params.p[holder]); var to := this.types.parts_of(owner); return bt::error if from.length != to.length; val members := this.decls.list_of(this.decls.children.p[holder]); for &member in members.span { continue if this.decls.kinds.p[member] != dk::assoc_type || !this.mentions(kind, this.decls.types.p[member], 0); val given := this.sealed_type(member, owner, depth + 1); from.append(member); to.append(given); } return this.substitute(kind, from.span, to.span); } #! The type parameter standing for the type a bound asks of a type parameter, #! as `C::iterator`, whose bounds are those the trait gives the type it asks #! for. #! @param this The checker. #! @param kind The type parameter's type. #! @param asked The declaration of the type the bound asks for. #! @param bound The bound asking for it. #! @returns The type parameter's type. pub def checker::projected(&mut this, kind: u32, asked: u32, bound: u32): u32 { val key := pair_key(kind, asked); if this.projections.get(&key) is some(got) { val made: u32 = got; return this.decls.types.p[made]; } # A pack asked for, as `F::params`, is stood in for by a pack whose types are # not known. val id := this.decls.add(dk::type_param, this.decls.names.p[asked], this.decls.files.p[asked], this.decls.nodes.p[asked], this.decls.modules.p[asked]); this.decls.own(id, this.decls.owners.p[asked]); if this.decls.has(asked, df::packed) { this.decls.mark(id, df::packed); this.decls.types.p[id] = this.types.pack(span::::blank(), id); } else { this.decls.types.p[id] = this.types.param(id); } this.decls.states.p[id] = ds::resolved; this.projections.set(key, id); this.stand_ins.append(checker::stand_in { base = kind, asked = asked, param = id, bound = bound }); # Its bounds are the trait's for the type it asks for, with the trait's # parameters given the bound's arguments and `self` the type parameter. this.resolve(asked); val holder: u32 = this.decls.owners.p[asked]; var from := vector::::new(); var to := vector::::new(); from.append(this.self_decl(holder)); to.append(kind); val params: u32 = this.decls.params.p[holder]; for i in 0..this.decls.list_length(params) { from.append(this.decls.list_at(params, i as u32)); to.append(i < this.types.part_count(bound) ? this.types.part_at(bound, i as u32) : bt::error); } var bounds := vector::::new(); val run: u32 = this.decls.children.p[asked]; for i in 0..this.decls.list_length(run) { bounds.append(this.substitute(this.decls.list_at(run, i as u32), from.span, to.span)); } this.decls.children.p[id] = this.decls.make_list(bounds.span); return this.decls.types.p[id]; } #! The constant parameter standing for the constant a bound asks of a type #! parameter, as `T::bytes` where `T: sized`, while the type is not known, so #! what is written with it, as `u8[T::bytes]` or `buffer`, is given #! the constant of the type each instance gives, as a constant parameter is #! given its argument. #! @param this The checker. #! @param kind The type parameter's type. #! @param asked The declaration of the constant the bound asks for. #! @param bound The bound asking for it. #! @returns The constant parameter's type. pub def checker::constant_projected(&mut this, kind: u32, asked: u32, bound: u32): u32 { val key := pair_key(kind, asked); if this.projections.get(&key) is some(got) { val made: u32 = got; return this.decls.types.p[made]; } # It is named as it is written, as `T::bytes`. val param := this.types.first_of(kind); var text := string::new(nothing {}); text.append(this.names.text(this.decls.names.p[param])); text.append("::"); text.append(this.names.text(this.decls.names.p[asked])); val id := this.decls.add(dk::type_param, this.names.intern(&text.span), this.decls.files.p[param], this.decls.nodes.p[param], this.decls.modules.p[param]); this.decls.own(id, this.decls.owners.p[param]); this.decls.mark(id, df::valued); # Its value's type is the constant's, with the trait's parameters given the # bound's arguments and `self` the type parameter. this.resolve(asked); val holder: u32 = this.decls.owners.p[asked]; var from := vector::::new(); var to := vector::::new(); from.append(this.self_decl(holder)); to.append(kind); val params: u32 = this.decls.params.p[holder]; for i in 0..this.decls.list_length(params) { from.append(this.decls.list_at(params, i as u32)); to.append(i < this.types.part_count(bound) ? this.types.part_at(bound, i as u32) : bt::error); } this.decls.targets.p[id] = this.substitute(this.decls.types.p[asked], from.span, to.span); this.decls.types.p[id] = this.types.param(id); this.decls.states.p[id] = ds::resolved; this.projections.set(key, id); this.stand_ins.append(checker::stand_in { base = kind, asked = asked, param = id, bound = bound }); return this.decls.types.p[id]; } #! The constant a type gives for one a bound asks of it, as a constant type #! argument: for a type parameter, the constant parameter standing for it, for a #! type the compiler decides it of, as `length` of an `array`, that, and else #! the type's own constant of the name, or the trait's default. #! @param this The checker. #! @param kind The type. #! @param asked The declaration of the constant the bound asks for. #! @param bound The bound asking for it. #! @returns The constant type argument, or the error type where it gives none. pub def checker::asked_constant(&mut this, kind: u32, asked: u32, bound: u32): u32 { return bt::error if kind == bt::error; val plain := this.unalias(kind); if this.types.kind_of(plain) == ty::param { return this.constant_projected(plain, asked, bound); } this.resolve(asked); var value := constant {}; if this.decided_value(plain, asked, &mut value) { return this.known_type(this.known_number(value, this.decls.types.p[asked])); } val found := this.find_path_member(kind, this.decls.names.p[asked]); val decl := found.decl != 0 ? found.decl : asked; # A sealed trait's constant is the one of the arguments it is read through, # which its template, given none, never gives. if this.decls.kinds.p[decl] == dk::variable && this.trait_asks(decl) && this.is_sealed(this.decls.owners.p[decl]) && !this.sealed_alone(decl) { val instance := this.sealed_instance(decl, kind, 0); return bt::error if instance == 0; this.check_instance_now(instance); val value := this.instances[instance].value; return value != 0 ? this.known_type(value) : bt::error; } # A default the type takes is worked out for it. if this.constant_default(decl) && found.decl != 0 { val instance := this.default_instance(decl, found.owner, kind, span::::blank()); return bt::error if instance == 0; this.check_instance_now(instance); val value := this.instances[instance].value; return value != 0 ? this.known_type(value) : bt::error; } return bt::error if this.decls.kinds.p[decl] != dk::variable || !this.decls.has(decl, df::constant); this.resolve(decl); val written := this.record_in(decl, variable_slot::value); return bt::error if written == 0; this.evaluate_constant(decl); val id := this.known_in(this.decls.files.p[decl], written, this.decls.types.p[decl], 0); return id != 0 ? this.known_type(id) : bt::error; } #! The constant type argument a path reading a constant a bound asks of a type #! names, as `T::bytes` where `T: sized`, or `T::length` where `T: array`. #! @param this The checker. #! @param node The `path` or `type_name` node. #! @param ref The declaration of the constant the bound asks for. #! @returns The constant type argument, or the error type where it names none. pub def checker::path_constant(&mut this, node: u32, ref: u32): u32 { val kind := this.kind_of(node); return bt::error if (kind != nk::path && kind != nk::type_name) || this.segment_count(node) != 2; val bound := this.bound(this.segment_name(node, 0)); return bt::error if (bound & ref_type) == 0; val base := bound & ref_mask; val plain := this.unalias(base); var asking: u32 = bt::error; if this.types.kind_of(plain) == ty::param { val found := this.find_in_bounds(plain, this.decls.names.p[ref]); return bt::error if found.decl != ref; asking = found.owner; } return this.asked_constant(base, ref, asking); } #! The type parameter standing for a pack's type at an index, as `Ts...[0]` or #! `T::args...[0]`, while the pack's types are not known, whose bounds are those #! the pack's bounds give each of its types. Substituting the pack gives it the #! type at the index. #! @param this The checker. #! @param pack The pack's type, whose types are not known. #! @param index The index. #! @returns The type parameter's type. pub def checker::element_param(&mut this, pack: u32, index: u32): u32 { val key := pair_key(pack, 0x80000000 | index); if this.projections.get(&key) is some(got) { val made: u32 = got; return this.decls.types.p[made]; } # It is named as it is written, as `Ts...[0]` or `T::args...[0]`. val of := this.types.first_of(pack); var text := string::new(nothing {}); for &each in this.stand_ins { if each.param == of { text.append(this.describe(each.base).span); text.append("::"); break; } } text.append(this.names.text(this.decls.names.p[of])); val written := $"...[{index}]"; text.append(written.span); val id := this.decls.add(dk::type_param, this.names.intern(&text.span), this.decls.files.p[of], this.decls.nodes.p[of], this.decls.modules.p[of]); this.decls.own(id, this.decls.owners.p[of]); this.decls.types.p[id] = this.types.param(id); this.decls.states.p[id] = ds::resolved; this.resolve(of); this.decls.children.p[id] = this.decls.children.p[of]; this.projections.set(key, id); this.add_element_in(checker::element_in { pack = pack, index = index, param = id }); return this.decls.types.p[id]; } #! Keeps a parameter standing for a pack's type at an index, and the #! parameters substituting which resolves it: the pack's, the type parameter a #! pack a bound asks for is asked of, as `T` of `T::args`, and each an index #! worked out from constant parameters reads, as `I` of `Ts...[I]`. #! @param this The checker. #! @param made The parameter and what it stands for. def checker::add_element_in(&mut this, made: checker::element_in): void { this.element_ins.append(made); val pack := this.types.first_of(made.pack); if pack != 0 { this.indexed.set(pack as u64, 1); for &asked in this.stand_ins { if asked.param == pack { this.indexed.set(this.types.first_of(asked.base) as u64, 1); } } } return if made.at == 0; if this.types.kind_of(made.at) == ty::param { this.indexed.set(this.types.first_of(made.at) as u64, 1); } for i in 0..this.types.part_count(made.at) { val part := this.types.part_at(made.at, i as u32); if this.types.kind_of(part) == ty::param { this.indexed.set(this.types.first_of(part) as u64, 1); } } } #! The type parameter standing for a pack's type at an index only each #! instance knows, worked out from constant parameters, as `Ts...[I]`, which #! substituting them resolves. It is bounded as each of the pack's types is. #! @param this The checker. #! @param pack The pack's type. #! @param at The constant type argument the index is. #! @returns The type parameter's type. pub def checker::element_at(&mut this, pack: u32, at: u32): u32 { for &each in this.element_ins { return this.decls.types.p[each.param] if each.at == at && each.pack == pack; } # It is named as it is written, as `Ts...[I]`, and placed where the pack is # declared, or the index's parameter where the pack is known. val of := this.types.is_open_pack(pack) ? this.types.first_of(pack) : 0u; var text := string::new(nothing {}); text.append(of != 0 ? this.names.text(this.decls.names.p[of]) : this.describe(pack).span); text.append("...["); text.append(this.describe(at).span); text.append("]"); var placed := of; if placed == 0 { var params := vector::::new(); this.params_in(at, &mut params, 0); placed = params.length != 0 ? params[0] : 0u; } val id := this.decls.add(dk::type_param, this.names.intern(&text.span), this.decls.files.p[placed], this.decls.nodes.p[placed], this.decls.modules.p[placed]); this.decls.own(id, this.decls.owners.p[placed]); this.decls.types.p[id] = this.types.param(id); this.decls.states.p[id] = ds::resolved; if of != 0 { this.resolve(of); this.decls.children.p[id] = this.decls.children.p[of]; } this.add_element_in(checker::element_in { pack = pack, param = id, at = at }); return this.decls.types.p[id]; } #! The type parameter standing, in the shape a type parameter has where it is #! known to be an instance of a generic, for the generic's parameter at an #! index: the type of `T::args` there, as `T::args...[0]` of `vector`, a #! constant parameter standing for the constant there, as `N` of `buffer`, or #! for the generic's pack, a pack of the types from there on, as #! `T::args...[1:]` of `tagged`. Its bounds, which `generic_shape` #! gives it, are the generic's of that parameter. #! @param this The checker. #! @param pack The pack of the type arguments, `T::args`. #! @param generic The generic declaration. #! @param index The index of its parameter. #! @returns The type parameter's, or pack's, type. pub def checker::shape_param(&mut this, pack: u32, generic: u32, index: u32): u32 { val key := pair_key(pack, (generic << 8) | index); if this.shape_params.get(&key) is some(got) { val made: u32 = got; return this.decls.types.p[made]; } # It is named as it is written, as `T::args...[0]`, or `T::args...[1:]`. val param := this.decls.list_at(this.decls.params.p[generic], index); val rest := this.decls.has(param, df::packed); val of := this.types.first_of(pack); var text := string::new(nothing {}); for &each in this.stand_ins { if each.param == of { text.append(this.describe(each.base).span); text.append("::"); break; } } text.append(this.names.text(this.decls.names.p[of])); val written := rest ? $"...[{index}:]" : $"...[{index}]"; text.append(written.span); val id := this.decls.add(dk::type_param, this.names.intern(&text.span), this.decls.files.p[of], this.decls.nodes.p[of], this.decls.modules.p[of]); this.decls.own(id, this.decls.owners.p[of]); this.resolve(param); if rest { this.decls.mark(id, df::packed); this.decls.types.p[id] = this.types.pack(span::::blank(), id); } else { if this.decls.has(param, df::valued) { this.decls.mark(id, df::valued); this.decls.targets.p[id] = this.decls.targets.p[param]; } this.decls.types.p[id] = this.types.param(id); } this.decls.states.p[id] = ds::resolved; this.shape_params.set(key, id); this.add_element_in(checker::element_in { pack = pack, index = index, param = id, generic = generic, rest = rest }); return this.decls.types.p[id]; } #! Reports a declaration named from where it is not visible. #! @param this The checker. #! @param node The node naming it. #! @param decl The declaration. pub def checker::report_hidden(&mut this, node: u32, decl: u32): void { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); text.append("` is not `pub`, so only its own module and those within it see it."); this.report_text(node, ec::hidden, &text); }