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