#! @file patterns.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains how the type arguments written for a generic type are given to its #! type parameters: one each, a pack the arguments after those before it, and a #! pattern parameter, `def(Ts...): R`, the parts of the function type it is #! given, as `invokable` gives `Ts` the pack `(i64, bool)` #! and `R` the type `u8`. A declaration's type parameters are those its #! patterns name, so everything past writing its arguments sees only them. module sakura::analyzer; #! What marks a pattern among the entries of a written shape, as `record_shape` #! makes them: 0 for a type parameter written plainly, 1 for a pack, and for a #! pattern this mark, with how many parameter types it names shifted left by 2, #! 2 where the last of them is a pack, and 1 where it names its result. pub const checker::pattern_mark: u32 = 0x80000000; #! What marks a pattern of a fixed array, `L[N]`, beside `pattern_mark`, which #! names its element's type and its length. pub const checker::array_mark: u32 = 0x40000000; #! Records the written shape of a declaration's own type parameters where a #! pattern is among them, while the file declaring it is the one checked, so #! its type arguments are given their parameters from any file. #! @param this The checker. #! @param decl The declaration. #! @param run The run of its own type parameters as written. pub def checker::record_shape(&mut this, decl: u32, run: u32): void { var entries := vector::::new(); var any := false; for &each in this.run_view(run) { if this.kind_of(each) == nk::type_pattern { val params := this.left_of(each); val count := this.run_length(params); val packed := count != 0 && this.node_has(this.run_at(params, count - 1), nf::packed); entries.append(checker::pattern_mark | (count << 2) | (packed ? 2 : 0) | (this.right_of(each) != 0 ? 1 : 0)); any = true; } else if this.kind_of(each) == nk::array_pattern { entries.append(checker::pattern_mark | checker::array_mark); any = true; } else { entries.append(this.kind_of(each) == nk::type_param && this.node_has(each, nf::packed) ? 1 : 0); } } if any { this.shapes.set(decl, this.decls.make_list(entries.view())); } } #! The written shape of a declaration's own type parameters: as recorded where #! a pattern is among them, or else one entry for each, a pack's 1. #! @param this The checker. #! @param decl The declaration. #! @returns The entries, as `pattern_mark` says. def checker::shape_of(&this, decl: u32): vector { if this.shapes.get(&decl) is some(got) { return this.decls.list_of(got); } var made := vector::::new(); val params: u32 = this.decls.params.p[decl]; val wanted := this.decls.list_length(params); val own := this.own_param_count(decl); for k in (wanted >= own ? wanted - own : 0)..wanted { made.append(this.decls.has(this.decls.list_at(params, k as u32), df::packed) ? 1 : 0); } return made; } #! How many type parameters an entry of a written shape names: a pattern one #! for each of its parameter types and one for its result, or of a fixed array #! its element's and its length's, and anything else itself. #! @param this The checker. #! @param entry The entry. #! @returns How many. def checker::named_count(&this, entry: u32): u32 { return 1 if (entry & checker::pattern_mark) == 0; return 2 if (entry & checker::array_mark) != 0; return ((entry & ~checker::pattern_mark) >> 2) + (entry & 1); } #! Whether a declaration is written with a pattern among its own type #! parameters. #! @param this The checker. #! @param decl The declaration. #! @returns Whether it is. pub def checker::has_patterns(&this, decl: u32): bool { return this.shapes.contains(&decl); } #! The type parameter a type argument written at a place is given to: one of a #! type the declaration is within, one of its own, or the pack taking the rest, #! or 0 for a pattern, whose type parameters are given the parts of what is #! written. #! @param this The checker. #! @param decl The generic declaration. #! @param index The place of the type argument among those written. #! @returns The type parameter's declaration, or 0. pub def checker::param_written_at(&this, decl: u32, index: u32): u32 { val params: u32 = this.decls.params.p[decl]; val wanted := this.decls.list_length(params); var shape := this.shape_of(decl); var own: u32 = 0; for &each in shape { own += this.named_count(each); } val lead: u32 = wanted >= own ? wanted - own : 0; return index < wanted ? this.decls.list_at(params, index) : 0 if index < lead; var at := lead; for i in 0..shape.length { val each := shape[i]; val packs := each == 1; if i as u32 == index - lead || (packs && index - lead > i as u32) { return (each & checker::pattern_mark) != 0 || at >= wanted ? 0 : this.decls.list_at(params, at); } at += this.named_count(each); } return 0; } #! Gives the type arguments written for a generic type to its type parameters, #! after those of any type it is declared within, which are given one each: one #! each to those written plainly, the rest to a pack, and to a pattern the parts #! of the function type it is given. #! @param this The checker. #! @param decl The generic type's declaration. #! @param args The type arguments as written, those of the types it is #! declared within first. #! @param node The node notes are placed at. #! @param into What each of its type parameters is given, in order. #! @returns Whether they were given, which notes say why not where not. pub def checker::bind_written(&mut this, decl: u32, args: span, node: u32, into: &mut vector): bool { val params: u32 = this.decls.params.p[decl]; val wanted := this.decls.list_length(params); var shape := this.shape_of(decl); var own: u32 = 0; for &each in shape { own += this.named_count(each); } # A type declared within a generic one takes that one's parameters first. val lead: u32 = wanted >= own ? wanted - own : 0; val count := shape.length as u32; val last_packs := count != 0 && shape[count as u64 - 1] == 1; val written := lead + count; var at: u64 = 0; for i in 0..lead { break if at >= args.length; into.append(args[at]); at++; } for i in 0..count { val each := shape[i]; if each == 1 { # A pack takes the type arguments after those before it. val pack := this.gather_pack(args, at, node); return false if pack == bt::error; into.append(pack); at = args.length; continue; } break if at >= args.length; if (each & checker::array_mark) != 0 { return false if !this.destructure_array(decl, args[at], node, into); } else if (each & checker::pattern_mark) != 0 { return false if !this.destructure(each, decl, args[at], node, into); } else { if this.types.kind_of(args[at]) == ty::pack { this.report(node, ec::generic, "A pack is passed on with `...` only for a pack parameter, as `Ts` of `box`."); return false; } into.append(args[at]); } at++; } if at != args.length || into.length != wanted as u64 { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); val words := last_packs ? $"` takes at least {written - 1} type arguments, but is given {args.length}." : $"` takes {written} type arguments, but is given {args.length}."; text.append(words.view()); this.report_text(node, ec::generic, &text); return false; } return true; } #! Gives a pattern parameter's type parameters the parts of the function type it #! is given: those of its parameters before a pack each take one of the #! function's parameters, the pack the rest, as a variadic function's are where #! it is one, and its result the function's. #! @param this The checker. #! @param pattern The pattern's entry in its declaration's written shape. #! @param decl The generic type's declaration. #! @param arg The type the pattern is given. #! @param node The node notes are placed at. #! @param into What the pattern's type parameters are given, in order. #! @returns Whether the type fits the pattern, which a note says why not where #! not. def checker::destructure(&mut this, pattern: u32, decl: u32, arg: u32, node: u32, into: &mut vector): bool { val count := (pattern & ~checker::pattern_mark) >> 2; val packed := (pattern & 2) != 0; val result := (pattern & 1) != 0; val fixed := packed ? count - 1 : count; # The address of a C function is given as the function type it is of, and a # type parameter that is a `signature`, as `F`, its `F::params` and `F::result`. val plain := this.signature_function(arg); val bare := this.unalias(arg); if plain == 0 && this.types.kind_of(bare) == ty::param { val sig := this.std_decl(word::signature); if sig != 0 && this.satisfies(bare, this.decls.types.p[sig]) { if fixed != 0 || !packed { var text := string::from("`", nothing {}); val named := this.describe(arg); text.append(named.view()); text.append("` is a signature whose parameters are not known yet, so the pattern of `"); text.append(this.decl_text(decl)); text.append("` takes it only where it names them as one pack, as `def(Ts...): R`."); this.report_text(node, ec::generic, &text); return false; } into.append(this.projected(bare, this.trait_member(sig, word::params), this.decls.types.p[sig])); if result { into.append(this.projected(bare, this.trait_member(sig, word::result), this.decls.types.p[sig])); } return true; } } if plain == 0 { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); text.append("` is given a function type where it takes `def(...)`, as `def(i64): i64`, and this is `"); val named := this.describe(arg); text.append(named.view()); text.append("`."); this.report_text(node, ec::generic, &text); return false; } # The parameters taking one value each, before any taking the rest. val parts := this.types.part_count(plain); val variadic := this.types.second_of(plain) != 0 && parts != 0; val singles := variadic ? parts - 1 : parts; val open := variadic && this.types.is_open_pack(this.types.part_at(plain, parts - 1)); if (packed && singles < fixed) || (!packed && (variadic || singles != fixed)) || (packed && open && singles != fixed) { var text := string::from("`", nothing {}); val named := this.describe(arg); text.append(named.view()); text.append(open && singles > fixed ? "` ends in a pack whose types are not known yet, which a pattern's pack takes only alone." : "` does not have the parameters the pattern of `"); if !(open && singles > fixed) { text.append(this.decl_text(decl)); text.append(packed ? "` asks for: at least as many before any taking the rest as it names before its pack." : "` asks for: as many as it names, none taking the rest."); } this.report_text(node, ec::generic, &text); return false; } for k in 0..fixed { into.append(this.types.part_at(plain, k as u32)); } if packed { into.append(open ? this.types.part_at(plain, parts - 1) : this.types.params_pack(plain, fixed)); } if result { into.append(this.types.first_of(plain)); } return true; } #! Gives a pattern parameter, `L[N]`, the parts of the fixed array it is given: #! its element's type to `L`, and its length to `N`, as a `u64`. #! @param this The checker. #! @param decl The generic type's declaration. #! @param arg The type the pattern is given. #! @param node The node notes are placed at. #! @param into What the pattern's type parameters are given, in order. #! @returns Whether the type fits the pattern, which a note says why not where #! not. def checker::destructure_array(&mut this, decl: u32, arg: u32, node: u32, into: &mut vector): bool { val plain := this.unalias(arg); if this.types.kind_of(plain) != ty::fixed { var text := string::from("`", nothing {}); text.append(this.decl_text(decl)); text.append("` is given a fixed array where it takes `L[N]`, as `f32[4]`, and this is `"); val named := this.describe(arg); text.append(named.view()); text.append("`."); this.report_text(node, ec::generic, &text); return false; } into.append(this.types.first_of(plain)); # A length only each instance knows is given as it is. if this.types.part_count(plain) != 0 { into.append(this.types.part_at(plain, 0)); } else { into.append(this.known_type(this.known_number(constant::integer(this.types.second_of(plain) as u64, false), bt::uint64))); } return true; } #! Writes the type arguments of a type declared with patterns as they are written, #! a function type for each pattern, as `invokable`. #! @param this The checker. #! @param kind The named type. #! @param out What it is written into. #! @param depth How deeply the writing has gone. def checker::describe_written(&this, kind: u32, out: &mut string, depth: u32): void { val decl := this.types.first_of(kind); var shape := this.shape_of(decl); val total := this.types.part_count(kind); var own: u32 = 0; for &each in shape { own += this.named_count(each); } val lead: u32 = total >= own ? total - own : 0; var at: u32 = 0; var written := false; for i in 0..lead { this.describe_arg(this.types.part_at(kind, at), out, depth + 1, &mut written); at++; } for i in 0..shape.length { val each := shape[i]; break if at >= total; if (each & checker::pattern_mark) == 0 { this.describe_arg(this.types.part_at(kind, at), out, depth + 1, &mut written); at++; continue; } out.append(written ? ", " : ""); written = true; # A fixed array's pattern, as the array it was given the parts of. if (each & checker::array_mark) != 0 { this.describe_into(this.types.part_at(kind, at), out, depth + 1); out.append("["); if at + 1 < total { this.describe_into(this.types.part_at(kind, at + 1), out, depth + 1); } out.append("]"); at += 2; continue; } # A signature `F` given whole is written as itself. val count := (each & ~checker::pattern_mark) >> 2; val packed := (each & 2) != 0; if count == 1 && packed && (each & 1) != 0 && at + 1 < total { val given := this.signature_given(this.types.part_at(kind, at), this.types.part_at(kind, at + 1)); if given != 0 { this.describe_into(given, out, depth + 1); at += 2; continue; } } # The function type the pattern's type parameters were given the parts of. out.append("def("); for k in 0..count { break if at >= total; out.append(k != 0 ? ", " : ""); val part := this.types.part_at(kind, at); if packed && k + 1 == count && this.types.is_known_pack(part) { for j in 0..this.types.part_count(part) { out.append(j != 0 ? ", " : ""); this.describe_into(this.types.part_at(part, j as u32), out, depth + 1); } if this.types.second_of(part) != 0 { out.append("..."); } } else { this.describe_into(part, out, depth + 1); } at++; } out.append("): "); if (each & 1) != 0 && at < total { this.describe_into(this.types.part_at(kind, at), out, depth + 1); at++; } } } #! Writes type arguments as notes spell them, each after a comma but the #! first: a pack whose types are known as the types it gives, as `i64, bool` #! of a pack given for `Ts...`, and an empty one as none. #! @param this The checker. #! @param args The type arguments. #! @param out What they are written into. #! @param depth How deeply the writing has gone, at the arguments. pub def checker::describe_args(&this, args: span, out: &mut string, depth: u32 = 0): void { var written := false; for &arg in args { this.describe_arg(arg, out, depth, &mut written); } } #! Writes a type argument after those already written: a pack whose types are #! known as the types it gives, or another type as it is. #! @param this The checker. #! @param arg The type argument. #! @param out What it is written into. #! @param depth How deeply the writing has gone, at the argument. #! @param written Whether a type argument is written before it, which it sets. pub def checker::describe_arg(&this, arg: u32, out: &mut string, depth: u32, written: &mut bool): void { if !this.types.is_known_pack(arg) { out.append(written ? ", " : ""); this.describe_into(arg, out, depth); written = true; return; } for &part in this.types.parts_view(arg) { out.append(written ? ", " : ""); this.describe_into(part, out, depth); written = true; } } #! The type of a value a pack holds: its type, or for the last of a variadic #! function's pack, the view of the rest it holds. #! @param this The checker. #! @param pack The pack's type. #! @param index The value's place. #! @returns The type. pub def checker::pack_value_type(&mut this, pack: u32, index: u32): u32 { val part := this.types.part_at(pack, index); return this.types.second_of(pack) != 0 && index + 1 == this.types.part_count(pack) ? this.span_of(part) : part; } #! The function type a signature is: a function type itself, or the one the #! address of a C function, `cfn`, is of. #! @param this The checker. #! @param kind The type. #! @returns The function type, or 0 where the type is no signature. pub def checker::signature_function(&mut this, kind: u32): u32 { val plain := this.unalias(kind); return plain if this.types.kind_of(plain) == ty::function; if this.is_std(plain, word::cfn) && this.types.part_count(plain) != 0 { val inner := this.unalias(this.types.part_at(plain, 0)); return inner if this.types.kind_of(inner) == ty::function; } return 0; } #! What `signature` asks of a type it decides: a function's `result`, and its #! `params`, the pack of its parameters' types, variadic where it is. #! @param this The checker. #! @param kind The type. #! @param asked The declaration of the type `signature` asks for. #! @returns The type, or 0 where the type is no signature or the trait asking #! is not `signature`. pub def checker::signature_part(&mut this, kind: u32, asked: u32): u32 { val holder: u32 = this.decls.owners.p[asked]; return 0 if holder == 0 || this.decls.names.p[holder] != word::signature || this.std_decl(word::signature) != holder; val function := this.signature_function(kind); return 0 if function == 0; return this.types.first_of(function) if this.decls.names.p[asked] == word::result; return this.parameters_of(function, 0) if this.decls.names.p[asked] == word::params; return 0; } #! The pack of a function type's parameter types from `from` on, variadic where #! the function is, or where they are only a pack whose types are not known, as #! those of `def(Ts...): R`, that pack. #! @param this The checker. #! @param function The function type. #! @param from The first parameter the pack holds. #! @returns The pack. pub def checker::parameters_of(&mut this, function: u32, from: u32): u32 { val count := this.types.part_count(function); if this.types.second_of(function) != 0 && count == from + 1 && this.types.is_open_pack(this.types.part_at(function, from)) { return this.types.part_at(function, from); } return this.types.params_pack(function, from); } #! The type `F` whose `params` and `result` stand in for what a pattern's type #! parameters were given, where they do, so a type it was given `F` is written #! with it, as `invokable`. #! @param this The checker. #! @param pack What the pattern's pack was given. #! @param result What its result was given. #! @returns `F`, or 0. def checker::signature_given(&this, pack: u32, result: u32): u32 { return 0 if !this.types.is_open_pack(pack) || this.types.kind_of(result) != ty::param; var base: u32 = 0; for &each in this.stand_ins { if each.param == this.types.first_of(pack) && this.decls.names.p[each.asked] == word::params { base = each.base; } } return 0 if base == 0; for &each in this.stand_ins { return base if each.param == this.types.first_of(result) && each.base == base && this.decls.names.p[each.asked] == word::result; } return 0; }