#! @file composite.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the checking of values built from others: struct and union #! literals, array literals, and anonymous functions, which may take their #! types from the type expected of them. module sakura::analyzer; #! A struct or union literal, as `vec2{1, 2}`, `vec2{ y = 8 }`, or #! `res::{ error = 5 }`. A generic struct written without its type #! arguments takes them from the type expected, or from its fields' values. #! @param this The checker. #! @param node The `construct` node. #! @param expected The type expected, or the error type where none is. #! @returns The literal's type, or the error type where it is not valid. def checker::construct(&mut this, node: u32, expected: u32): u32 { val named := this.left_of(node); val inits := this.right_of(node); val decl := this.type_name_decl(named); if decl == 0 { this.check_inits(inits); return bt::error; } var kind: u32 = bt::error; val wanted := this.unalias(expected); val written_args := this.segment_args(named, this.segment_count(named) - 1); if this.decls.has(decl, df::generic) && written_args == 0 && this.segment_count(named) == 1 && !this.scope_gives_args(this.segment_name(named, 0), decl) { if this.types.kind_of(wanted) == ty::named && this.types.first_of(wanted) == decl { kind = wanted; } else { kind = this.infer_struct(decl, inits, named); } } else { kind = this.resolve_type(named); } if kind == bt::error { this.check_inits(inits); return bt::error; } # `void {}` is the one value of `void`, which holds nothing. val plain := this.unalias(kind); if plain == bt::void_type { if this.run_length(inits) != 0 { this.report(this.run_at(inits, 0), ec::invalid, "`void` holds nothing, so `void {}` is given no fields."); this.check_inits(inits); } return kind; } match this.named_kind(plain) { dk::struct_type => this.construct_struct(node, plain, inits); dk::union_type => this.construct_union(node, plain, inits); else => { this.report(named, ec::not_type, "Only a struct or a union is made with `{}`."); this.check_inits(inits); return bt::error; } } return kind; } #! The declaration a type name names, without looking at its type arguments, #! or 0 where it names none. #! @param this The checker. #! @param node The `type_name` node. #! @returns The declaration, or 0. def checker::type_name_decl(&mut this, node: u32): u32 { if this.segment_count(node) == 1 { val name := this.segment_name(node, 0); val bound := this.bound(name); if (bound & ref_type) != 0 { this.set_ref(node, bound); return this.type_decl(bound & ref_mask); } val decl := this.lookup_type(name, node); if decl == 0 || !this.is_type_decl(decl) { var text := string::from("No struct or union `", nothing {}); text.append(this.names.text(name)); text.append("` is declared where it is written."); this.report_text(node, ec::undefined, &text); return 0; } this.set_ref(node, decl); return decl; } val kind := this.resolve_type(node); return kind == bt::error ? 0 : this.type_decl(this.unalias(kind)); } #! Whether a type around a name gives the arguments of the generic type it #! names, as `list` gives `node` its `T` within it, which then takes no more. #! @param this The checker. #! @param name The ID of the name. #! @param decl The type's declaration. #! @returns Whether it does. def checker::scope_gives_args(&mut this, name: u32, decl: u32): bool { return false if this.bound(name) != 0 || this.own_param_count(decl) != 0; val found := this.in_scope(name, true); return found.decl == decl && this.types.kind_of(found.owner) == ty::named && this.types.part_count(found.owner) != 0; } #! Checks the values of a literal whose type went wrong, so what is wrong #! within them is still reported. #! @param this The checker. #! @param run The run of `field_init` nodes. def checker::check_inits(&mut this, run: u32): void { for &each in this.run_view(run) { this.infer_expr(this.left_of(each)); } } #! The type of a generic struct whose type arguments its fields' values give. #! @param this The checker. #! @param decl The struct's declaration. #! @param inits The run of `field_init` nodes. #! @param node The node notes are placed at. #! @returns The struct's type, or the error type where its fields do not give #! its arguments. def checker::infer_struct(&mut this, decl: u32, inits: u32, node: u32): u32 { var inferred := this.begin_inference(decl, bt::error, span::::empty()); val fields: u32 = this.decls.children.p[decl]; for i in 0..this.run_length(inits) { val init := this.run_at(inits, i as u32); val field := this.init_field(decl, init, i as u32); continue if field == 0; this.resolve(field); val value := this.left_of(init); if !this.deferred(value) { this.unify(this.decls.types.p[field], this.infer_expr(value), &mut inferred, 0); } } val missing := this.unbound(&inferred); if missing != 0 { var text := string::from("Nothing gives the type of `", nothing {}); text.append(this.decl_text(missing)); text.append("`, so give the type arguments, as `"); text.append(this.decl_text(decl)); text.append("::<...>{...}`."); this.report_text(node, ec::generic, &text); return bt::error; } return this.named_of(decl, inferred.binds.view(), node, true); } #! The field a value of a literal is given for: the one it names, or the one at #! its place, or 0. #! @param this The checker. #! @param decl The struct's or union's declaration. #! @param init The `field_init` node. #! @param index Its position in the literal. #! @returns The field's declaration, or 0. def checker::init_field(&mut this, decl: u32, init: u32, index: u32): u32 { if this.node_has(init, nf::named) { val found := this.find_member(this.decls.types.p[decl], this.name_of(init)); return found.decl != 0 && this.decls.kinds.p[found.decl] == dk::field ? found.decl : 0; } val run: u32 = this.decls.children.p[decl]; var at: u32 = 0; for &field in this.decls.list_view(run) { continue if this.decls.kinds.p[field] != dk::field; return field if at == index; at++; } return 0; } #! The fields of a struct literal: each value given converts to its field, #! and each field left out takes its default, or its zero value. #! @param this The checker. #! @param node The `construct` node. #! @param kind The struct's type. #! @param inits The run of `field_init` nodes. def checker::construct_struct(&mut this, node: u32, kind: u32, inits: u32): void { val decl := this.types.first_of(kind); var given := vector::::new(); for i in 0..this.run_length(inits) { val init := this.run_at(inits, i as u32); val value := this.left_of(init); val field := this.init_field(decl, init, i as u32); if field == 0 { if this.node_has(init, nf::named) { this.report_no_member(init, kind, this.name_of(init)); } else { this.report(init, ec::arguments, "The literal gives more values than the struct has fields."); } this.infer_expr(value); continue; } var twice := false; for &each in given { twice = twice || each == field; } if twice { this.report(init, ec::duplicate, "The field is given a value twice."); } given.append(field); this.check_reaches(init, kind, field); this.resolve(field); val owner: u32 = this.decls.owners.p[field]; val held := owner == decl ? this.instantiated(this.decls.types.p[field], decl, kind) : this.decls.types.p[field]; this.check_expr(value, held); this.check_owned(value, held); this.set_ref(init, field); } # A field left out takes its default, or its zero value, which a reference # or a function does not have. val run: u32 = this.decls.children.p[decl]; for i in 0..this.decls.list_length(run) { val field := this.decls.list_at(run, i as u32); continue if this.decls.kinds.p[field] != dk::field; var was_given := false; for &each in given { was_given = was_given || each == field; } continue if was_given || this.field_default(field) != 0; this.resolve(field); val held := this.instantiated(this.decls.types.p[field], decl, kind); if !this.has_zero(held, 0) { var text := string::from("`", nothing {}); text.append(this.decl_text(field)); text.append("` has no zero value, so the literal gives it a value."); this.report_text(node, ec::zero_value, &text); } } } #! The default value a field is declared with, or 0. #! @param this The checker. #! @param field The field's declaration. #! @returns The default's node, or 0. pub def checker::field_default(&this, field: u32): u32 { return this.record_in(field, variable_slot::value); } #! A union literal, which names the one field it holds, or none for its zero #! value. #! @param this The checker. #! @param node The `construct` node. #! @param kind The union's type. #! @param inits The run of `field_init` nodes. def checker::construct_union(&mut this, node: u32, kind: u32, inits: u32): void { # Naming none, it is the union's zero value. return if this.run_length(inits) == 0; if this.run_length(inits) != 1 { this.report(node, ec::arguments, "A union literal names the one field it holds, as `res{ error = 5 }`."); this.check_inits(inits); return; } val decl := this.types.first_of(kind); val init := this.run_at(inits, 0); val field := this.init_field(decl, init, 0); if field == 0 || !this.node_has(init, nf::named) { this.report(init, ec::member, "A union literal names the field it holds, as `res{ error = 5 }`."); this.infer_expr(this.left_of(init)); return; } this.check_reaches(init, kind, field); this.resolve(field); this.check_expr(this.left_of(init), this.instantiated(this.decls.types.p[field], decl, kind)); this.set_ref(init, field); } #! Whether a type has a zero value, which a variable without a value starts at: #! numbers, `bool`, pointers, and enums with a member of value 0 do, and so do #! arrays, structs, and unions of what does, but references and functions do #! not, and a type parameter only where its bounds say so. #! @param this The checker. #! @param kind The type. #! @param depth How deeply the search has gone, which stops a type nesting #! without end. #! @returns Whether it has. pub def checker::has_zero(&mut this, kind: u32, depth: u32): bool { return true if depth > 8 || kind == bt::error; val plain := this.unalias(kind); match this.types.kind_of(plain) { ty::reference, ty::function => return false; ty::param, ty::pack => return this.param_zeroable(plain); ty::fixed => return this.has_zero(this.types.first_of(plain), depth + 1); ty::named => { val decl := this.types.first_of(plain); match this.decls.kinds.p[decl] { dk::enum_type => { this.resolve(decl); val run: u32 = this.decls.children.p[decl]; for &member in this.decls.list_view(run) { var value := constant {}; if this.decl_value(member, &mut value) && value.magnitude == 0 { return true; } } return false; } dk::struct_type => { val run: u32 = this.decls.children.p[decl]; for i in 0..this.decls.list_length(run) { val field := this.decls.list_at(run, i as u32); continue if this.decls.kinds.p[field] != dk::field || this.field_default(field) != 0; this.resolve(field); return false if !this.has_zero(this.instantiated(this.decls.types.p[field], decl, plain), depth + 1); } return true; } dk::union_type => { val fields := this.field_types(plain); return fields.length == 0 || this.has_zero(fields[0], depth + 1); } else => return true; } } else => return true; } return true; } #! Whether a type parameter's bounds give it a zero value: `zeroable` does, and #! so do `integer`, `real`, and `pointer`, since every number and pointer has one. #! @param this The checker. #! @param kind The type parameter's type. #! @returns Whether they do. def checker::param_zeroable(&mut this, kind: u32): bool { val zeroable := this.std_decl(word::zeroable); val integer := this.std_decl(word::integer); val real := this.std_decl(word::real); val pointer := this.std_decl(word::pointer); return (zeroable != 0 && this.param_bounded(kind, zeroable)) || (integer != 0 && this.param_bounded(kind, integer)) || (real != 0 && this.param_bounded(kind, real)) || (pointer != 0 && this.param_bounded(kind, pointer)); } #! Says why a type has no zero value, for a report that it is not `zeroable`: #! the type itself, or the field within it that has none, followed through #! fields and the elements of arrays as `has_zero` follows them, as "its field #! `inner.r` holds `&i64`, which has no zero value". #! @param this The checker. #! @param kind The type. #! @returns Why, as a note's detail. pub def checker::zero_lacking(&mut this, kind: u32): string { var fields := string::new(nothing {}); var at := this.unalias(kind); for _ in 0..8 { if this.types.kind_of(at) == ty::fixed { at = this.unalias(this.types.first_of(at)); continue; } val is_union := this.named_kind(at) == dk::union_type; break if this.named_kind(at) != dk::struct_type && !is_union; # A union is zeroed as its first field, and a struct as each without a # default. val decl := this.types.first_of(at); val run: u32 = this.decls.children.p[decl]; var lacking: u32 = 0; var held: u32 = 0; for i in 0..this.decls.list_length(run) { val field := this.decls.list_at(run, i as u32); continue if this.decls.kinds.p[field] != dk::field; this.resolve(field); val inner := this.instantiated(this.decls.types.p[field], decl, at); if (is_union || this.field_default(field) == 0) && !this.has_zero(inner, 0) { lacking = field; held = inner; } break if lacking != 0 || is_union; } break if lacking == 0; if fields.length > 0 { fields.append("."); } fields.append(this.decl_text(lacking)); at = this.unalias(held); } val described := this.describe(at); val is_enum := this.named_kind(at) == dk::enum_type; val is_param := this.types.kind_of(at) == ty::param; var text := string::new(nothing {}); if fields.length > 0 { text.append("its field `"); text.append(fields.view()); text.append("` holds `"); text.append(described.view()); text.append(is_enum ? "`, no member of which has the value 0" : is_param ? "`, which has a zero value only where its bounds say it is `zeroable`" : "`, which has no zero value"); return text; } if is_enum { text.append("no member of `"); text.append(described.view()); text.append("` has the value 0"); return text; } if is_param { text.append("a type parameter has a zero value only where its bounds say it is `zeroable`"); return text; } if this.types.kind_of(at) == ty::reference || this.types.kind_of(at) == ty::function { text.append(this.types.kind_of(at) == ty::reference ? "a reference has no zero value" : "a function has no zero value"); return text; } text.append("`"); text.append(described.view()); text.append("` has no zero value"); return text; } #! An array literal: a fixed array of the type its elements share, or of the #! type expected, or where a `span` is expected, a view of itself. #! @param this The checker. #! @param node The `array` node. #! @param expected The type expected, or the error type where none is. #! @returns The array's type, or the error type where its elements share none. def checker::array(&mut this, node: u32, expected: u32): u32 { val run := this.left_of(node); val count := this.run_length(run); val wanted := this.unalias(expected); if this.types.kind_of(wanted) == ty::fixed { val element := this.types.first_of(wanted); for i in 0..count { this.check_expr(this.run_at(run, i as u32), element); } # Within a template, a length only each instance knows is theirs to check. if count != this.types.second_of(wanted) && this.types.part_count(wanted) == 0 { val words := $"The array holds {this.types.second_of(wanted)} elements, but {count} are written."; var text := string::from(words.view(), nothing {}); this.report_text(node, ec::mismatch, &text); } return expected; } if this.is_std(wanted, word::span) { val element := this.types.part_at(wanted, 0); for i in 0..count { this.check_expr(this.run_at(run, i as u32), element); } return expected; } if count == 0 { this.report(node, ec::mismatch, "An empty array takes its type from where it is written, and nothing gives it here."); return bt::error; } var shared: u32 = 0; for i in 0..count { val kind := this.infer_expr(this.run_at(run, i as u32)); if kind == bt::error { shared = bt::error; continue; } if shared == 0 { shared = kind; continue; } if shared != bt::error { val joined := this.common(shared, kind); if joined == bt::error { this.report_operands(this.run_at(run, i as u32), shared, kind, "are elements of one array, which share no type"); } shared = joined; } } return bt::error if shared == bt::error; val element := this.settle(shared); for i in 0..count { this.convert(this.run_at(run, i as u32), element); } return this.types.fixed(element, count); } #! An anonymous function, whose parameter types and result may be left out #! where the function type expected gives them. It shares the locals of the #! function around it, so it is checked within that function's bindings. #! @param this The checker. #! @param node The `lambda` node. #! @param expected The function type expected, or the error type where none #! is. #! @returns The function type. def checker::lambda(&mut this, node: u32, expected: u32): u32 { # C calls back what it is given with nothing more, so it can't reach what a # lambda refers to. val c_wanted := this.unalias(expected); if this.is_std(c_wanted, word::cfn) && this.types.part_count(c_wanted) == 1 { this.report(node, ec::invalid, "C calls back a function of the program named where its address is expected, not a lambda, whose locals C gives it no way to reach."); this.lambda(node, this.types.part_at(c_wanted, 0)); return bt::error; } val params := this.left_of(node); val result := this.record(node, lambda_slot::result); val body := this.record(node, lambda_slot::body); val wanted := this.unalias(expected); val gives := this.types.kind_of(wanted) == ty::function; var kinds := vector::::new(); for i in 0..this.run_length(params) { val param := this.run_at(params, i as u32); val written := this.left_of(param); if written != 0 { kinds.append(this.resolve_type(written)); } else if gives && i < this.types.part_count(wanted) { kinds.append(this.types.part_at(wanted, i as u32)); } else { this.report(param, ec::mismatch, gives ? "The function type expected here has fewer parameters, so give this one a type." : "No function type is expected here, so give the parameter a type."); kinds.append(bt::error); } } # A lambda whose result infers a type argument takes it from its body. val free := this.free_result; this.free_result = false; var returns: u32 = 0; if result != 0 { returns = this.resolve_type(result); } else if gives && !free { returns = this.types.first_of(wanted); } val saved := this.at; this.at.result = returns != 0 ? returns : bt::void_type; this.at.loops = 0; this.at.body = body; val mark := this.scope_mark(); for i in 0..this.run_length(params) { val param := this.run_at(params, i as u32); this.set_type(param, kinds[i]); this.set_flag(param, rf::local); this.set_flag(param, rf::mutable); this.bind(this.name_of(param), ref_local | param); } if this.kind_of(body) == nk::block { val leaves := this.check_block(body) != flow::goes_on; if returns == 0 { returns = bt::void_type; } else if returns != bt::void_type && !leaves { this.report(node, ec::control, "The function can end without returning a value."); } } else if returns == 0 { # What the body gives is taken as a binding takes it: what a reference # reaches, unless `&` is written. returns = this.settle_expr(body); if this.takes_value(body, returns) { returns = this.types.first_of(this.unalias(returns)); this.convert(body, returns); this.check_owned(body, returns); } } else if returns == bt::void_type { this.settle_expr(body); } else { this.check_expr(body, returns); } this.unbind_to(mark); this.at.result = saved.result; this.at.loops = saved.loops; this.at.body = saved.body; # One whose parameter has no type was reported, and is nothing a call can take. for &kind in kinds.view() { return bt::error if kind == bt::error; } return this.types.function(kinds.view(), returns, false); }