Files
sakurac/analyzer/checker/knowns.sakura
T
2026-12-06 13:52:57 -06:00

1735 lines
71 KiB
Plaintext

#! @file knowns.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the values the checker works out while checking, which are what a
#! constant type argument is, as `4` of `buffer<u8, 4>`: numbers, text,
#! structs, unions and arrays of them, functions, and the addresses of
#! constants. Each is interned, so two equal values are one, and one type
#! argument, however each is written. Within a template, a value worked out
#! from its own constant parameters is kept as the expression over them, which
#! each instance works out, and one only its type arguments decide, as
#! `small(N)`, `T is pointer`, or `size_of::<T>()`, is kept as what is written
#! with the types its type parameters are given, which is checked and worked out
#! again once none of those holds a type parameter. What a node or a
#! declaration is known to be is one of these too: a number or float of no type
#! in particular.
module sakura::analyzer;
#! The kinds of value the checker works out.
pub enum kk : u8 {
invalid, #! Known value 0, which stands for "not known".
number, #! An integer, `bool`, character, or member of an enum: `value`.
real, #! A float, rounded to its type: `value`.
text, #! Text: the bytes `text` holds.
fields, #! A struct's fields in order, or the elements of a fixed array or a view: `parts`.
held, #! A union holding its field `value.magnitude`, which holds its one part.
null_lit, #! `null`.
function, #! A function: `decl`.
address, #! The address of a constant: `decl`.
param, #! Within a template, a constant parameter: `decl`, its declaration.
unary, #! Within a template, `op` applied to its one part.
binary, #! Within a template, `op` between its two parts.
cast, #! Within a template, its one part converted to its type.
opaque, #! Within a template, anything else, the same only where written once: `node` of `file`, within `decl` given `given`.
}
#! A value the checker worked out, by its ID in `knowns`.
pub struct known {
pub var kind: kk; #! What it is.
var of: u32; #! The type it is a value of.
pub var value: constant; #! A number's or float's value, or a union's field.
var text: u32; #! Text's bytes, by index in `known_texts`.
var parts: u32; #! Its parts' run in `known_parts`, 0 where it has none.
pub var decl: u32; #! A function, a constant whose address it is, a constant parameter, or the declaration an `opaque` is written within.
var op: tk; #! The operator of a value over constant parameters.
var node: u32; #! Of `opaque`, its expression.
var file: u32; #! Of `opaque`, the file of its expression.
var given: u32; #! Of `opaque`, the pack of the types its declaration's type parameters are given, or 0 where it is never worked out again.
}
#! The value of the ID, as a copy.
#! @param this The checker.
#! @param id The value's ID.
#! @returns The value.
pub def checker::known_at(&this, id: u32): known {
return this.knowns.p[id];
}
#! How many parts a value has.
#! @param this The checker.
#! @param id The value's ID.
#! @returns How many.
pub def checker::known_count(&this, id: u32): u32 {
return this.known_parts.length(this.knowns.p[id].parts) as u32;
}
#! A part of a value.
#! @param this The checker.
#! @param id The value's ID.
#! @param index The part's position.
#! @returns The part's ID.
pub def checker::known_part(&this, id: u32, index: u32): u32 {
return this.known_parts.at(this.knowns.p[id].parts, index as u64);
}
#! The parts of a value, where they are.
#! @param this The checker.
#! @param id The value's ID.
#! @returns The IDs of its parts, in order, a view only good until a value with
#! parts is interned, so a loop that may intern one walks them by
#! `known_part`.
pub def checker::known_view(&this, id: u32): span<u32> {
return this.known_parts.view(this.knowns.p[id].parts);
}
#! The ID of a value, interned, so an equal value made before is the one given.
#! @param this The checker.
#! @param made The value, whose `parts` and `text` are given apart.
#! @param parts The IDs of its parts, in order.
#! @param text The bytes of text.
#! @returns Its ID.
pub def checker::intern_known(&mut this, made: known, parts: span<u32>, text: strview): u32 {
val hash := known_hash(&made, parts, text);
var walk := this.known_index.walk(hash);
var at := this.known_index.next(&mut walk);
while at != 0 {
val id := at - 1;
return id if this.same_known(id, &made, parts, text);
at = this.known_index.next(&mut walk);
}
var held := made;
held.parts = this.known_parts.add(parts);
if made.kind == kk::text {
held.text = this.known_texts.length as u32;
this.known_texts.append(string::from(text, nothing {}));
}
val id := this.knowns.length as u32;
this.knowns.append(held);
this.known_index.add(hash, id);
return id;
}
#! Whether a value interned is the one given: the same in each field but its
#! parts and text, its float the same to the bit, and with the same parts, by
#! their own IDs, interned already, and the same text.
#! @param this The checker.
#! @param id The interned value's ID.
#! @param made The value, whose `parts` and `text` are given apart.
#! @param parts The IDs of its parts, in order.
#! @param text The bytes of text, which only text holds.
#! @returns Whether it is.
def checker::same_known(&this, id: u32, made: &known, parts: span<u32>, text: strview): bool {
val held: known = this.knowns.p[id];
return false if held.kind != made.kind || held.of != made.of || held.decl != made.decl || held.op != made.op
|| held.node != made.node || held.file != made.file || held.given != made.given
|| held.value.negative != made.value.negative || held.value.magnitude != made.value.magnitude;
return false if made.kind == kk::real && real_bits(held.value.real) != real_bits(made.value.real);
return false if !this.known_parts.view(held.parts).equals(&parts);
return made.kind != kk::text || this.known_texts[held.text].span.equals(&text);
}
#! The hash a value is interned by, of what `same_known` compares.
#! @param made The value, whose `parts` and `text` are given apart.
#! @param parts The IDs of its parts, in order.
#! @param text The bytes of text.
#! @returns The hash.
def known_hash(made: &known, parts: span<u32>, text: strview): u64 {
var hash := mix(mix(parts.length, made.kind as u64), made.of as u64);
hash = mix(hash, made.value.magnitude);
hash = mix(hash, pair_key(made.decl, made.value.negative ? 1 : 0));
hash = mix(hash, pair_key(made.node, made.file));
hash = mix(hash, pair_key(made.given, made.op as u32));
if made.kind == kk::real {
hash = mix(hash, real_bits(made.value.real));
}
for &each in parts {
hash = mix(hash, each as u64);
}
return mix(hash, text.hash());
}
#! The bits of a float, which tell apart what `==` does not, as `-0.0` from
#! `0.0`.
#! @param value The float.
#! @returns Its bits.
def real_bits(value: f64): u64 {
var held := value;
return ((&mut held) as *mut u64)[0];
}
#! The text a value holds.
#! @param this The checker.
#! @param id The value's ID.
#! @returns The bytes it holds.
pub def checker::known_text(&this, id: u32): strview {
return this.known_texts[this.knowns.p[id].text].span;
}
#! A number or float of the type.
#! @param this The checker.
#! @param value The number.
#! @param of Its type.
#! @returns Its ID.
pub def checker::known_number(&mut this, value: constant, of: u32): u32 {
var made := value;
val plain := this.unalias(of);
if this.types.kind_of(plain) == ty::floating && !made.floating {
made = constant::real_of(made.as_real());
}
if made.floating && this.types.kind_of(plain) == ty::floating && this.types.first_of(plain) == 32 {
made = constant::real_of(made.real as f32 as f64);
}
return this.intern_known(known { kind = made.floating ? kk::real : kk::number, of = of, value = made },
span::<u32>::blank(), "");
}
#! The value as one of the type it is wanted as, where the checker already let it
#! convert: an integer constant as `u64`, or as a float. A value already of the
#! type, or wanted as no type, is given back as it is.
#! @param this The checker.
#! @param id The value's ID.
#! @param of The type, or 0.
#! @returns The value's ID.
pub def checker::known_as(&mut this, id: u32, of: u32): u32 {
return id if id == 0 || of == 0 || of == bt::error;
val held: known = this.knowns.p[id];
return id if held.of == of;
if held.kind == kk::number || held.kind == kk::real {
return this.known_number(held.value, of);
}
var parts := vector::<u32>::new();
parts.extend(this.known_view(id));
var made := held;
made.of = of;
val text := held.kind == kk::text ? this.known_text(id) : "";
return this.intern_known(made, parts.span, text);
}
#! Whether a value depends on constant parameters only each instance knows.
#! @param this The checker.
#! @param id The value's ID.
#! @returns Whether it does.
pub def checker::known_depends(&this, id: u32): bool {
return false if id == 0;
val kind: kk = this.knowns.p[id].kind;
return true if kind == kk::param || kind == kk::unary || kind == kk::binary || kind == kk::cast || kind == kk::opaque;
for &part in this.known_view(id) {
return true if this.known_depends(part);
}
return false;
}
#! The type argument a value is, as a value of the type: the constant parameter
#! itself where it is one alone of that type, and otherwise the type holding the
#! value.
#! @param this The checker.
#! @param id The value's ID.
#! @returns The type.
pub def checker::known_type(&mut this, id: u32): u32 {
val held: known = this.knowns.p[id];
if held.kind == kk::param && this.decls.targets.p[held.decl] == held.of {
return this.decls.types.p[held.decl];
}
# One worked out from constant parameters holds them as its parts, so it stands
# for other values once they are given.
var params := vector::<u32>::new();
this.known_params(id, &mut params, 0);
return this.types.intern(ty::value, held.of, id, params.span);
}
#! Gathers the types of the constant parameters a value is worked out from.
#! @param this The checker.
#! @param id The value's ID.
#! @param into Where each is added, once.
#! @param depth How deeply the search has gone.
def checker::known_params(&mut this, id: u32, into: &mut vector<u32>, depth: u32): void {
return if id == 0 || depth > 32;
val held: known = this.knowns.p[id];
if held.kind == kk::param {
this.add_known_param(into, this.decls.types.p[held.decl]);
return;
}
# One kept as what is written depends on the types its type parameters are
# given that still hold one.
if held.kind == kk::opaque && held.given != 0 {
for i in 0..this.types.part_count(held.given) {
val part := this.types.part_at(held.given, i as u32);
if this.types.is_open(part) {
this.add_known_param(into, part);
}
}
return;
}
for &part in this.known_view(id) {
this.known_params(part, into, depth + 1);
}
}
#! Adds a type to those a value depends on, once.
#! @param this The checker.
#! @param into Where it is added.
#! @param kind The type.
def checker::add_known_param(&this, into: &mut vector<u32>, kind: u32): void {
for &each in into.span {
return if each == kind;
}
into.append(kind);
}
#! The type with each type parameter `from` names replaced by the type `to`
#! gives at the same index, with each constant worked out from them once they
#! are known, as `N + 1` is `5` where `N` is given `4`, a fixed array of a
#! length that is now a number holding that many, and an alias of `match const`
#! that now knows the type it chooses by that type.
#! @param this The checker.
#! @param kind The type.
#! @param from The type parameters to replace.
#! @param to The types to replace each with.
#! @returns The type made.
pub def checker::substitute(&mut this, kind: u32, from: span<u32>, to: span<u32>): u32 {
return kind if !this.types.is_open(kind) || from.length == 0;
if (this.stand_ins.length != 0 && this.projects(from)) || (this.element_ins.length != 0 && this.indexes(from)) {
return this.chosen_type(this.substitute_projected(kind, from, to), 0);
}
val made := this.types.substitute(kind, from, to);
val settled := this.types.holds_value(kind) ? this.settle_values(made, from, to, 0) : made;
# An alias of `match const` given the types it chooses by is the type it
# chooses.
return this.chosen_type(settled, 0);
}
#! Whether a type parameter substitution replaces has types its bounds ask for
#! standing in for them, as `C` of `C::iterator`.
#! @param this The checker.
#! @param from The type parameters replaced.
#! @returns Whether one has.
def checker::projects(&this, from: span<u32>): bool {
for &each in this.stand_ins {
val base := this.types.first_of(each.base);
for &param in from {
return true if param == base;
}
}
return false;
}
#! Whether a pack substitution replaces, or one a bound asks of a type parameter
#! it replaces, as `T::args`, has a pack's type at an index standing in for it,
#! or whether it replaces a constant parameter such an index is worked out from,
#! as `add_element_in` keeps them.
#! @param this The checker.
#! @param from The type parameters replaced.
#! @returns Whether one has.
def checker::indexes(&this, from: span<u32>): bool {
for &param in from {
val key := param as u64;
return true if this.indexed.contains(&key);
}
return false;
}
#! Substitutes the type parameters, and the parameters standing for the types
#! their bounds ask for with the types the types given for them give, as
#! `C::cursor` is `ctr` where `C` is given `one` and `type one::cursor = ctr`,
#! and those standing for a pack's type at an index with the type there.
#! @param this The checker.
#! @param kind The type.
#! @param from The type parameters to replace.
#! @param to The types to replace each with.
#! @returns The type made.
def checker::substitute_projected(&mut this, kind: u32, from: span<u32>, to: span<u32>): u32 {
var wider := vector::<u32>::new();
var given := vector::<u32>::new();
for i in 0..from.length {
wider.append(from[i]);
given.append(i < to.length ? to[i] : bt::error);
}
# Asking may make more, which this substitution leaves.
val count := this.stand_ins.length;
for p in 0..count {
val each: checker::stand_in = this.stand_ins[p];
for i in 0..from.length {
continue if from[i] != this.types.first_of(each.base) || i >= to.length;
val bound := this.types.substitute(each.bound, from, to);
wider.append(each.param);
given.append(this.asked_type(to[i], each.asked, bound));
}
}
# A pack's type at an index is the type at the index of the pack substituted,
# or stands for it still where its types are still not known.
val indexed := this.element_ins.length;
for e in 0..indexed {
val each: checker::element_in = this.element_ins[e];
val pack := this.types.substitute(each.pack, wider.span, given.span);
# An index worked out from constant parameters is worked out again from
# those given, and stands for the type there still where it depends on some.
var index := each.index;
var at: u32 = 0;
if each.at != 0 {
# Working it out substitutes the types it reads, as `Ts` of
# `Ts.length - 1`, which leaves such indexes as they are.
continue if this.settling_index != 0;
this.settling_index++;
at = this.types.substitute(each.at, wider.span, given.span);
if this.types.holds_value(at) {
at = this.settle_values(at, wider.span, given.span, 0);
}
this.settling_index--;
}
continue if pack == each.pack && at == each.at;
wider.append(each.param);
if at != 0 {
val count := this.index_known(at);
if count < 0 {
given.append(this.types.is_open(at) ? this.element_at(pack, at) : bt::error);
continue;
}
index = count as u32;
}
if this.types.is_open_pack(pack) && each.at != 0 {
given.append(this.element_param(pack, index));
} else if this.types.is_open_pack(pack) {
given.append(each.generic != 0 ? this.shape_param(pack, each.generic, each.index) : this.element_param(pack, each.index));
} else if each.rest && this.types.kind_of(pack) == ty::pack {
# A shape's pack is the types from its index on.
var items := vector::<u32>::new();
for k in each.index..this.types.part_count(pack) {
items.append(this.types.part_at(pack, k as u32));
}
given.append(this.types.pack(items.span, 0));
} else if this.types.kind_of(pack) == ty::pack && index < this.types.part_count(pack) {
given.append(this.types.part_at(pack, index));
} else {
given.append(bt::error);
}
}
val made := this.types.substitute(kind, wider.span, given.span);
return this.types.holds_value(kind) ? this.settle_values(made, wider.span, given.span, 0) : made;
}
#! The index a constant type argument is, where it is known and one.
#! @param this The checker.
#! @param at The constant type argument.
#! @returns The index, or -1 where it is not known or is no index.
def checker::index_known(&this, at: u32): i64 {
return -1 if this.types.kind_of(at) != ty::value || this.known_depends(this.types.second_of(at));
val held: known = this.knowns.p[this.types.second_of(at)];
return -1 if held.kind != kk::number || held.value.negative || held.value.magnitude > 0x1000000;
return held.value.magnitude as i64;
}
#! Works out the constants within a type substitution made, as `substitute`
#! says.
#! @param this The checker.
#! @param kind The type substitution made.
#! @param from The type parameters it replaced.
#! @param to The types it replaced each with.
#! @param depth How deeply the search has gone.
#! @returns The type with its constants worked out.
def checker::settle_values(&mut this, kind: u32, from: span<u32>, to: span<u32>, depth: u32): u32 {
return kind if depth > 16;
val tkd := this.types.kind_of(kind);
if tkd == ty::value {
val id := this.types.second_of(kind);
val of := this.substitute(this.types.first_of(kind), from, to);
val made := this.known_as(this.known_subst(id, from, to), of);
# One worked out again that has no value is no type.
return made != 0 ? this.known_type(made) : bt::error;
}
if tkd == ty::fixed && this.types.part_count(kind) != 0 {
val element := this.settle_values(this.types.first_of(kind), from, to, depth + 1);
val length := this.settle_values(this.types.part_at(kind, 0), from, to, depth + 1);
if this.types.kind_of(length) == ty::value && !this.known_depends(this.types.second_of(length)) {
val count: known = this.knowns.p[this.types.second_of(length)];
return bt::error if count.kind != kk::number || count.value.negative || count.value.magnitude > 0x1000000;
return this.types.fixed(element, count.value.magnitude as u32);
}
return this.types.fixed_of(element, length);
}
match tkd {
ty::reference, ty::pointer, ty::fixed => {
val inner := this.settle_values(this.types.first_of(kind), from, to, depth + 1);
return inner == this.types.first_of(kind) ? kind
: this.types.intern(tkd, inner, this.types.second_of(kind), span::<u32>::blank());
}
ty::function, ty::named, ty::pack, ty::matched => {
var parts := vector::<u32>::new();
var changed := false;
for i in 0..this.types.part_count(kind) {
val part := this.types.part_at(kind, i as u32);
val made := this.settle_values(part, from, to, depth + 1);
changed = changed || made != part;
parts.append(made);
}
var first := this.types.first_of(kind);
if tkd == ty::function {
val result := this.settle_values(first, from, to, depth + 1);
changed = changed || result != first;
first = result;
}
return changed ? this.types.intern(tkd, first, this.types.second_of(kind), parts.span) : kind;
}
else => return kind;
}
return kind;
}
#! Whether a type argument is a constant: a value, or within a template the
#! parameter standing for one.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
pub def checker::is_constant_arg(&this, kind: u32): bool {
val tkd := this.types.kind_of(kind);
return tkd == ty::value || (tkd == ty::param && this.decls.has(this.types.first_of(kind), df::valued));
}
#! The type of the value a constant type argument stands for.
#! @param this The checker.
#! @param kind The constant type argument.
#! @returns The type of its value.
pub def checker::constant_type(&mut this, kind: u32): u32 {
if this.types.kind_of(kind) == ty::value {
return this.types.first_of(kind);
}
val decl := this.types.first_of(kind);
this.resolve(decl);
val of: u32 = this.decls.targets.p[decl];
return of == 0 ? bt::error : of;
}
#! The value a constant type argument stands for.
#! @param this The checker.
#! @param kind The constant type argument.
#! @returns The value's ID.
pub def checker::constant_known(&mut this, kind: u32): u32 {
return this.types.second_of(kind) if this.types.kind_of(kind) == ty::value;
return this.intern_known(known { kind = kk::param, of = this.constant_type(kind), decl = this.types.first_of(kind) },
span::<u32>::blank(), "");
}
#! Decodes the escapes of text written between quotes, as the program holds it.
#! @param this The checker.
#! @param written The text between the quotes.
#! @param out Where the bytes are written.
pub def checker::decode_text(&this, written: strview, out: &mut string): void {
var i := 0u;
while i < written.length {
val c := written[i];
if c != '\\' || i + 1 >= written.length {
out.push(c);
i++;
continue;
}
val e := written[i + 1];
i += 2;
if e == 'n' { out.push('\n'); }
else if e == 't' { out.push('\t'); }
else if e == 'r' { out.push('\r'); }
else if e == '0' { out.push('\0'); }
else if e == 'x' && i + 1 < written.length {
val high := written.substr(i, 2);
out.push(this.read_hex(high) as u8);
i += 2;
} else {
out.push(e);
}
}
}
#! The value of the field of a struct type at a position, or 0.
#! @param this The checker.
#! @param kind The struct or union type.
#! @param name The ID of the field's name.
#! @returns The field's position, or `no_length` where it has none of the name.
def checker::field_index(&mut this, kind: u32, name: u32): u32 {
val plain := this.unalias(kind);
return checker::no_length if this.types.kind_of(plain) != ty::named;
val decl := this.types.first_of(plain);
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 at if this.decls.names.p[field] == name;
at++;
}
return checker::no_length;
}
#! The value a type is zeroed as, which a struct literal leaving out a field gives
#! it: 0, `false`, `null`, empty text, the member of value 0, a union's first
#! field, and each field's default or zero value, or 0 where it has none.
#! @param this The checker.
#! @param kind The type.
#! @param depth How deeply the search has gone.
#! @returns The value's ID, or 0.
def checker::known_zero(&mut this, kind: u32, depth: u32): u32 {
return 0 if depth > 16;
val plain := this.unalias(kind);
match this.types.kind_of(plain) {
ty::boolean, ty::signed, ty::unsigned, ty::floating => return this.known_number(constant {}, kind);
ty::pointer => return this.intern_known(known { kind = kk::null_lit, of = kind }, span::<u32>::blank(), "");
ty::fixed => {
return 0 if this.types.part_count(plain) != 0;
var parts := vector::<u32>::new();
val element := this.types.first_of(plain);
for _ in 0..this.types.second_of(plain) {
val zero := this.known_zero(element, depth + 1);
return 0 if zero == 0;
parts.append(zero);
}
return this.intern_known(known { kind = kk::fields, of = kind }, parts.span, "");
}
ty::named => {}
else => return 0;
}
val decl := this.types.first_of(plain);
if this.decls.kinds.p[decl] == dk::enum_type {
return this.known_number(constant {}, kind);
}
if this.is_std(plain, word::span) || this.is_std(plain, word::slice) {
val element := this.types.part_count(plain) != 0 ? this.unalias(this.types.part_at(plain, 0)) : bt::error;
if element == bt::uint8 || element == bt::int8 {
return this.intern_known(known { kind = kk::text, of = kind }, span::<u32>::blank(), "");
}
return this.intern_known(known { kind = kk::fields, of = kind }, span::<u32>::blank(), "");
}
val kinds := this.field_types(plain);
if this.decls.kinds.p[decl] == dk::union_type {
return 0 if kinds.length == 0;
val zero := this.known_zero(kinds[0], depth + 1);
return 0 if zero == 0;
var parts := vector::<u32>::new();
parts.append(zero);
return this.intern_known(known { kind = kk::held, of = kind }, parts.span, "");
}
if this.decls.kinds.p[decl] != dk::struct_type {
return 0;
}
var parts := vector::<u32>::new();
val run: u32 = this.decls.children.p[decl];
var at: u64 = 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;
val default := this.field_default(field);
val made := default != 0
? this.known_in(this.decls.files.p[field], default, kinds[at], depth + 1)
: this.known_zero(kinds[at], depth + 1);
return 0 if made == 0;
parts.append(made);
at++;
}
return this.intern_known(known { kind = kk::fields, of = kind }, parts.span, "");
}
#! The value of an expression of another file, worked out over that file's
#! results, as a constant's value or a field's default.
#! @param this The checker.
#! @param file The file.
#! @param node The expression.
#! @param of The type it is wanted as, or 0.
#! @param depth How deeply the search has gone.
#! @returns The value's ID, or 0.
def checker::known_in(&mut this, file: u32, node: u32, of: u32, depth: u32): u32 {
return this.known_of(node, of, depth) if file == this.at.file;
val saved := this.at;
this.use_table(this.units[file].table);
val made := this.known_of(node, of, depth);
this.at = saved;
return made;
}
#! The value of an integer or float once converted to a type, as `as` converts
#! it: wrapped to an integer's bits, as a float, or as the member of an enum it is
#! the value of. Anything else is only renamed, as a pointer cast to another.
#! @param this The checker.
#! @param id The value's ID.
#! @param to The type.
#! @returns The value's ID, or 0 where none converts.
def checker::known_convert(&mut this, id: u32, to: u32): u32 {
val held: known = this.knowns.p[id];
val plain := this.unalias(to);
val tkd := this.types.kind_of(plain);
if tkd == ty::signed || tkd == ty::unsigned {
return 0 if held.kind != kk::number && held.kind != kk::real;
var value := held.value;
if value.floating {
# Toward zero, saturating at the ends of the range, with NaN as 0.
val bits := this.types.first_of(plain);
val half := (1u << (bits - 1) as u64) as f64;
val low := tkd == ty::signed ? -half : 0.0;
val high := tkd == ty::signed ? half : 2.0 * half;
val real := value.real;
if real != real {
value = constant {};
} else if real <= low {
value = tkd == ty::signed ? constant::integer(1u << (bits - 1) as u64, true) : constant {};
} else if real >= high {
value = tkd == ty::signed ? constant::integer((1u << (bits - 1) as u64) - 1, false)
: constant::from_bits(bits == 64 ? 0xFFFFFFFFFFFFFFFFu : (1u << bits as u64) - 1, false);
} else {
value = real < 0.0 ? constant::integer((-real) as u64, true) : constant::integer(real as u64, false);
}
}
return this.known_number(this.wrap_constant(value, plain), to);
}
if tkd == ty::floating {
return 0 if held.kind != kk::number && held.kind != kk::real;
return this.known_number(constant::real_of(held.value.as_real()), to);
}
if this.named_kind(plain) == dk::enum_type && held.kind == kk::number {
return this.known_number(held.value, to);
}
return this.known_as(id, to);
}
#! The value of a binary operator between two values worked out, or 0 where it
#! has none, as dividing by zero or overflowing has none.
#! @param this The checker.
#! @param operator The operator.
#! @param a One value.
#! @param b The other.
#! @param of The type it gives.
#! @returns The value's ID, or 0.
def checker::known_compute(&mut this, operator: tk, a: u32, b: u32, of: u32): u32 {
val x: known = this.knowns.p[a];
val y: known = this.knowns.p[b];
val comparison := operator.compares();
if (x.kind == kk::number || x.kind == kk::real) && (y.kind == kk::number || y.kind == kk::real) {
var out := constant {};
if operator == tk::modulo && (x.value.floating || y.value.floating) {
out = constant::real_of(x.value.as_real() % y.value.as_real());
} else if !this.fold_binary(operator, &x.value, &y.value, x.value.negative || y.value.negative || this.types.is_signed(this.unalias(of)), &mut out) {
return 0;
}
return this.known_number(out, comparison || operator == tk::logand || operator == tk::logor ? bt::boolean : of);
}
# Text is equal where it holds the same bytes.
if x.kind == kk::text && y.kind == kk::text && (operator == tk::equals || operator == tk::noteq) {
val same := this.known_text(a) == this.known_text(b);
return this.known_number(constant::truth(operator == tk::equals ? same : !same), bt::boolean);
}
return 0;
}
#! A value worked out from constant parameters, kept as the expression over them.
#! @param this The checker.
#! @param kind The kind of expression.
#! @param node The expression's node.
#! @param parts The values it is worked out from.
#! @returns The value's ID.
def checker::known_over(&mut this, kind: kk, node: u32, parts: span<u32>): u32 {
var made := known { kind = kind, of = this.type_at(node), op = this.token_kind(node) };
if kind == kk::opaque {
made.node = node;
made.file = this.at.file;
made.decl = this.template_holding(node);
made.given = made.decl != 0 ? this.given_params(made.decl) : 0;
}
return this.intern_known(made, parts, "");
}
#! The template an expression kept as what is written is worked out again
#! within: the declaration being checked, or for a field, member, or type
#! parameter, the type declaring it, where the expression is written within
#! it, reads no local, and no trait's default holds it, whose instances are
#! made otherwise.
#! @param this The checker.
#! @param node The expression.
#! @returns The declaration, or 0.
def checker::template_holding(&mut this, node: u32): u32 {
var decl := this.at.decl;
return 0 if decl == 0;
val kind: dk = this.decls.kinds.p[decl];
# A sealed trait's type is worked out as the trait given its arguments, as a
# generic type's field is, though nothing within any other trait is.
if kind == dk::field || kind == dk::enum_member || kind == dk::type_param
|| (kind == dk::assoc_type && this.is_sealed(this.decls.owners.p[decl])) {
decl = this.decls.owners.p[decl];
}
return 0 if decl == 0 || this.decls.files.p[decl] != this.at.file || (this.enclosing_trait(decl) != 0 && !this.is_sealed(decl));
return 0 if this.decls.list_length(this.decls.params.p[decl]) == 0;
return 0 if node > this.decls.nodes.p[decl] || node < this.first_node(decl);
return 0 if this.reads_local(node);
return decl;
}
#! The pack of the types the names of a declaration's type parameters are
#! bound to where checking is: in its own body, the parameters themselves.
#! @param this The checker.
#! @param decl The declaration.
#! @returns The pack.
def checker::given_params(&mut this, decl: u32): u32 {
var given := vector::<u32>::new();
for &param in this.decls.list_view(this.decls.params.p[decl]) {
val bound := this.bound(this.decls.names.p[param]);
given.append((bound & ref_type) != 0 ? bound & ref_mask : this.decls.types.p[param]);
}
return this.types.pack(given.span, 0);
}
#! Whether an expression reads a local, constant or not.
#! @param this The checker.
#! @param node The expression.
#! @returns Whether it does.
def checker::reads_local(&mut this, node: u32): bool {
var todo := vector::<u32>::new();
todo.append(node);
while todo.length != 0 {
val at: u32 = todo.last;
todo.truncate();
continue if at == 0;
return true if (this.kind_of(at) == nk::name || this.kind_of(at) == nk::path) && (this.ref_at(at) & ref_local) != 0;
this.syn().children(at, &mut todo);
}
return false;
}
#! Whether an expression is decided by the type arguments of the template it is
#! checked in: it names a type parameter, as a type or as a constant, or holds
#! a value of a type holding one.
#! @param this The checker.
#! @param node The expression.
#! @returns Whether it is.
def checker::reads_params(&mut this, node: u32): bool {
var todo := vector::<u32>::new();
todo.append(node);
while todo.length != 0 {
val at: u32 = todo.last;
todo.truncate();
continue if at == 0;
# A name an `is` test reads as a type keeps no reference, so what it is bound
# to is asked.
var ref := this.ref_at(at);
if ref == 0 && this.kind_of(at) == nk::name {
ref = this.bound(this.name_of(at));
}
return true if (ref & ref_type) != 0 && (ref & ref_local) == 0 && this.types.is_open(ref & ref_mask);
# A sealed trait's constant read within it is the one of the arguments the
# trait is given.
if ref != 0 && (ref & (ref_type | ref_local)) == 0 && this.decls.kinds.p[ref] == dk::variable
&& this.trait_asks(ref) && this.is_sealed(this.decls.owners.p[ref]) && !this.sealed_alone(ref) {
return true;
}
val kind := this.type_at(at);
return true if kind != 0 && kind != bt::error && this.types.is_open(kind);
this.syn().children(at, &mut todo);
}
return false;
}
#! The value a constant kept as what is written is, given types for its
#! template's type parameters that hold none: what is written, checked again
#! with them, and worked out by folding or the evaluator, once for each types
#! given. Where it has none, that is reported where it is written.
#! @param this The checker.
#! @param id The value's ID.
#! @param given The pack of the types.
#! @returns The value's ID, or 0 where it has none.
def checker::known_again(&mut this, id: u32, given: u32): u32 {
val key := pair_key(id, given);
if this.worked_again.get(&key) is some(got) {
return got == 0 ? 0 : got - 1;
}
this.worked_again.set(key, 0);
val held: known = this.knowns.p[id];
# It is checked as an instance's body is, in a table of its own, and reported
# once, whatever asked for it first.
val outside := this.step_outside();
val repeating := this.repeating;
this.repeating = 0;
this.enter_decl(held.decl, given);
this.at.given = true;
this.use_table(this.units[held.file].table);
val low := this.lowest_node(held.node);
this.use_table(this.add_table(held.file, low, held.node - low + 1, 0));
val of := this.types.is_open(held.of) ? 0u : held.of;
val errors := this.errors;
val kind := of != 0 ? this.check_expr(held.node, of) : this.infer_expr(held.node);
var made: u32 = 0;
if kind != bt::error {
made = this.known_or_run(held.node, of);
}
# Where the program has errors, or nothing runs it, the evaluator says
# nothing, and neither is this reported.
if made == 0 && this.errors == errors && !this.failed && this.evaluator is some(_) {
var text := string::from("This is not known while compiling where `", nothing {});
text.append(this.decl_text(held.decl));
text.append("` is given `<");
this.describe_args(this.types.parts_view(given), &mut text, 1);
text.append(">`, as a type argument is.");
this.report_text(held.node, ec::constant, &text);
}
this.unbind_to(this.at.marks);
this.repeating = repeating;
this.step_back(&outside);
this.worked_again.set(key, made + 1);
return made;
}
#! The value an expression checked gives, worked out while checking: by
#! folding, or else, outside a template, by the evaluator running it, where it
#! reads no local only the program running gives.
#! @param this The checker.
#! @param node The expression.
#! @param of The type it is wanted as, or 0.
#! @returns The value's ID, or 0 where it is not worked out.
pub def checker::known_or_run(&mut this, node: u32, of: u32): u32 {
val id := this.known_of(node, of, 0);
return id if id != 0 || this.waits_for_instance() || this.reads_params(node) || this.runtime_local_read(node) != 0;
var answer := evaluation_answer {};
return 0 if !this.run_value(node, &mut answer);
return this.known_of(node, of, 0);
}
#! Works out the value a checked expression gives, which is what a constant
#! argument is: a literal, a `const`, a member of an enum, a function, `null`,
#! the address of a constant, a struct, union, or array literal of those, a
#! field or element of one, and arithmetic, comparisons, and conversions over
#! them. Within a template, what depends on its constant parameters is kept as
#! the expression over them.
#! @param this The checker.
#! @param node The expression.
#! @param of The type it is wanted as, or 0.
#! @param depth How deeply the search has gone.
#! @returns The value's ID, or 0 where it cannot be worked out.
pub def checker::known_of(&mut this, node: u32, of: u32, depth: u32): u32 {
val made := this.known_worked(node, of, depth);
return made if made != 0 || node == 0 || depth > 32;
# Within a template, what only its type arguments decide is kept as what is
# written, which each instance works out again.
return 0 if !this.in_template() || this.type_at(node) == bt::error || !this.reads_params(node);
val kept := this.known_over(kk::opaque, node, span::<u32>::blank());
return 0 if this.knowns.p[kept].given == 0;
return this.known_as(kept, of);
}
#! Whether checking is within a template: a generic's own body or signature,
#! or a field, member, or type parameter of a generic type.
#! @param this The checker.
#! @returns Whether it is.
def checker::in_template(&mut this): bool {
return true if this.waits_for_instance();
val decl := this.at.decl;
return false if this.at.instance != 0 || decl == 0 || this.at.given;
val kind: dk = this.decls.kinds.p[decl];
val owner: u32 = this.decls.owners.p[decl];
return (kind == dk::field || kind == dk::enum_member || kind == dk::type_param
|| (kind == dk::assoc_type && this.is_sealed(owner))) && owner != 0
&& this.decls.has(owner, df::generic);
}
#! The value of a checked expression, as `known_of` says, but for what it keeps
#! as what is written.
#! @param this The checker.
#! @param node The expression.
#! @param of The type it is wanted as, or 0.
#! @param depth How deeply the search has gone.
#! @returns The value's ID, or 0 where it cannot be worked out.
def checker::known_worked(&mut this, node: u32, of: u32, depth: u32): u32 {
return 0 if node == 0 || depth > 32;
# A value converts where the checker said it does: a union holds it, or a view
# is of an array's elements.
val converts := this.converted(node);
if converts != 0 && converts != this.type_at(node) && converts != bt::error {
val raw := this.known_raw(node, 0, depth);
return 0 if raw == 0;
val plain := this.unalias(converts);
if this.named_kind(plain) == dk::union_type && !this.known_depends(raw) {
val kinds := this.field_types(plain);
val have: u32 = this.knowns.p[raw].of;
var index: u32 = 0;
for i in 0..kinds.length {
if this.unalias(kinds[i]) == this.unalias(this.settle(have)) {
index = i as u32;
break;
}
}
var parts := vector::<u32>::new();
parts.append(this.known_as(raw, kinds.length != 0 ? kinds[index] : 0));
val held := this.intern_known(known { kind = kk::held, of = converts, value = constant::integer(index as u64, false) },
parts.span, "");
return this.known_as(held, of);
}
return this.known_as(this.known_as(raw, converts), of);
}
return this.known_raw(node, of, depth);
}
#! The value of an expression as it is, before any conversion the checker made.
#! @param this The checker.
#! @param node The expression.
#! @param of The type it is wanted as, or 0.
#! @param depth How deeply the search has gone.
#! @returns The value's ID, or 0.
def checker::known_raw(&mut this, node: u32, of: u32, depth: u32): u32 {
val gives := of != 0 ? of : this.settle(this.type_at(node));
if this.is_known(node) {
return this.known_number(this.value_at(node), gives);
}
match this.kind_of(node) {
nk::number => {
val text := this.text_of(node);
var value := constant {};
if this.token_kind(node) == tk::float {
value = constant::real_of(this.read_real(text));
} else if !this.read_integer(text, &mut value) {
return 0;
}
return this.known_number(value, gives);
}
nk::boolean => return this.known_number(constant::truth(this.token_kind(node) == tk::kw_true), gives);
nk::character => return this.known_number(constant::integer(this.read_character(this.text_of(node)), false), gives);
nk::string => {
val written := this.text_of(node);
return 0 if written.length < 2;
var bytes := string::new(nothing {});
if written.length >= 6 && written.starts_with("\"\"\"") {
this.literal_text(written, &mut bytes);
} else {
this.decode_text(written.substr(1, written.length - 2), &mut bytes);
}
return this.intern_known(known { kind = kk::text, of = gives }, span::<u32>::blank(), bytes.span);
}
nk::null_lit => return this.intern_known(known { kind = kk::null_lit, of = gives }, span::<u32>::blank(), "");
nk::name, nk::path, nk::type_name => {
val ref := this.ref_at(node);
return 0 if ref == 0;
# A local constant is its value, which within a template may be kept as
# the expression over constant parameters, as `N + 1` or `T::bytes * 2`.
if (ref & ref_local) != 0 {
val local := ref & ref_mask;
return 0 if this.kind_of(local) != nk::variable || !this.node_has(local, nf::constant);
val value := this.record(local, variable_slot::value);
return value != 0 ? this.known_of(value, of, depth + 1) : 0;
}
if (ref & ref_type) != 0 {
val kind := ref & ref_mask;
return 0 if !this.is_constant_arg(kind);
return this.known_as(this.constant_known(kind), of);
}
if this.decls.kinds.p[ref] == dk::function {
return this.intern_known(known { kind = kk::function, of = gives, decl = ref }, span::<u32>::blank(), "");
}
# A sealed trait's constant, or a default a type takes, read through an
# instance, is the value the instance worked out.
if this.decls.kinds.p[ref] == dk::variable && this.trait_asks(ref) && !this.sealed_alone(ref) {
val key := this.table_node_of(node);
val instance := this.called.get_or(&key, 0);
return this.known_as(this.instances[instance].value, of) if instance != 0;
# Its template gives none, its parameters given nothing.
return 0 if this.is_sealed(this.decls.owners.p[ref]);
}
# A constant a bound asks of a type parameter, as `T::bytes`, is the
# constant parameter standing for it, which each instance gives.
if this.decls.kinds.p[ref] == dk::variable && this.trait_asks(ref) {
val asked := this.path_constant(node, ref);
return asked != bt::error ? this.known_as(this.constant_known(asked), of) : 0;
}
# A constant of a generic type is the value its instance worked out.
if this.decls.kinds.p[ref] == dk::variable && this.decls.has(ref, df::constant) && this.decls.has(ref, df::generic) {
val key := this.table_node_of(node);
val instance := this.called.get_or(&key, 0);
return instance != 0 ? this.known_as(this.instances[instance].value, of) : 0;
}
if this.decls.kinds.p[ref] == dk::variable && this.decls.has(ref, df::constant) {
this.resolve(ref);
val value := this.record_in(ref, variable_slot::value);
return 0 if value == 0;
this.evaluate_constant(ref);
return this.known_as(this.known_in(this.decls.files.p[ref], value, this.decls.types.p[ref], depth + 1), of);
}
return 0;
}
nk::unary => {
val operand := this.known_of(this.left_of(node), 0, depth + 1);
return 0 if operand == 0;
var parts := vector::<u32>::new();
parts.append(operand);
return this.known_over(kk::unary, node, parts.span) if this.known_depends(operand);
val held: known = this.knowns.p[operand];
if held.kind == kk::real && this.token_kind(node) == tk::minus {
return this.known_number(constant::real_of(-held.value.real), gives);
}
return 0;
}
nk::binary => {
val left := this.known_of(this.left_of(node), 0, depth + 1);
val right := this.known_of(this.right_of(node), 0, depth + 1);
return 0 if left == 0 || right == 0;
if this.known_depends(left) || this.known_depends(right) {
var parts := vector::<u32>::new();
parts.append(left);
parts.append(right);
return this.known_over(kk::binary, node, parts.span);
}
val own := this.type_at(node);
return this.known_as(this.known_compute(this.token_kind(node), left, right,
this.types.is_untyped(own) ? gives : own), of);
}
nk::cast => {
val operand := this.known_of(this.left_of(node), 0, depth + 1);
return 0 if operand == 0;
if this.known_depends(operand) {
var parts := vector::<u32>::new();
parts.append(operand);
return this.known_over(kk::cast, node, parts.span);
}
return this.known_as(this.known_convert(operand, this.type_at(node)), of);
}
nk::ternary => {
val condition := this.known_of(this.left_of(node), 0, depth + 1);
return 0 if condition == 0;
return this.known_over(kk::opaque, node, span::<u32>::blank()) if this.known_depends(condition);
return this.known_of(this.record(node, condition_holds(this.knowns.p[condition]) ? if_slot::then : if_slot::other), of, depth + 1);
}
nk::construct => return this.known_as(this.known_literal(node, depth), of);
nk::array => {
val made := this.type_at(node);
val plain := this.unalias(made);
val element := this.types.kind_of(plain) == ty::fixed ? this.types.first_of(plain) : 0;
var parts := vector::<u32>::new();
val run := this.left_of(node);
for &item in this.run_view(run) {
val each := this.known_of(item, element, depth + 1);
return 0 if each == 0;
parts.append(each);
}
return this.known_as(this.intern_known(known { kind = kk::fields, of = made }, parts.span, ""), of);
}
nk::reference, nk::type_ref => {
val operand := this.left_of(node);
return 0 if this.kind_of(operand) != nk::name && this.kind_of(operand) != nk::path
&& this.kind_of(operand) != nk::type_name;
val ref := this.ref_at(operand);
return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0;
return 0 if this.decls.kinds.p[ref] != dk::variable || !this.decls.has(ref, df::constant);
return this.intern_known(known { kind = kk::address, of = gives, decl = ref }, span::<u32>::blank(), "");
}
nk::member => {
val object := this.left_of(node);
val held := this.type_at(object);
val plain := this.unalias(this.reached(held));
# The length of a fixed array only each instance knows is its constant.
if this.types.kind_of(plain) == ty::fixed && this.types.part_count(plain) != 0
&& this.name_of(node) == word::length {
return this.known_as(this.constant_known(this.types.part_at(plain, 0)), of);
}
val whole := this.known_of(object, 0, depth + 1);
return 0 if whole == 0;
return this.known_over(kk::opaque, node, span::<u32>::blank()) if this.known_depends(whole);
val got: known = this.knowns.p[whole];
if got.kind == kk::text && this.name_of(node) == word::length {
return this.known_number(constant::integer(this.known_text(whole).length, false), gives);
}
return 0 if got.kind != kk::fields;
val index := this.field_index(got.of, this.name_of(node));
return 0 if index == checker::no_length || index >= this.known_count(whole);
return this.known_as(this.known_part(whole, index), of);
}
nk::index => {
val whole := this.known_of(this.left_of(node), 0, depth + 1);
val at := this.known_of(this.right_of(node), 0, depth + 1);
return 0 if whole == 0 || at == 0;
return this.known_over(kk::opaque, node, span::<u32>::blank())
if this.known_depends(whole) || this.known_depends(at);
val index: known = this.knowns.p[at];
return 0 if index.kind != kk::number || index.value.negative;
val got: known = this.knowns.p[whole];
if got.kind == kk::fields && index.value.magnitude < this.known_count(whole) as u64 {
return this.known_as(this.known_part(whole, index.value.magnitude as u32), of);
}
if got.kind == kk::text && index.value.magnitude < this.known_text(whole).length {
val byte := this.known_text(whole)[index.value.magnitude];
return this.known_number(constant::integer(byte as u64, false), gives);
}
return 0;
}
else => return 0;
}
return 0;
}
#! Works out a global constant's value where folding did not and running does,
#! as one a function gives, so what reads it knows it.
#! @param this The checker.
#! @param decl The constant's declaration.
def checker::evaluate_constant(&mut this, decl: u32): void {
var held := constant {};
return if this.decl_value(decl, &mut held) || !this.is_scalar(this.decls.types.p[decl]);
val value := this.record_in(decl, variable_slot::value);
val saved := this.at;
this.use_table(this.units[this.decls.files.p[decl]].table);
var answer := evaluation_answer {};
if this.run_value(value, &mut answer) {
this.set_decl_value(decl, this.value_at(value));
}
this.at = saved;
}
#! Whether a type's values are ones a constant holds: numbers, `bool`,
#! characters, and members of an enum.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether they are.
def checker::is_scalar(&mut this, kind: u32): bool {
val plain := this.unalias(kind);
return this.types.is_numeric(plain) || this.types.is_boolean(plain) || this.named_kind(plain) == dk::enum_type;
}
#! Whether a condition worked out holds.
#! @param value The condition's value.
#! @returns Whether it holds.
def condition_holds(value: known): bool {
return value.value.magnitude != 0;
}
#! A node of a declaration's record, as its value, in the file declaring it.
#! @param this The checker.
#! @param decl The declaration.
#! @param index The record's slot.
#! @returns The node, or 0.
def checker::record_in(&this, decl: u32, index: u32): u32 {
val syntax := &this.units[this.decls.files.p[decl]].syntax;
return syntax.record(this.decls.nodes.p[decl], index);
}
#! The value of a struct or union literal: each field it is given, and each it
#! is not its default or zero value, or the one field a union holds.
#! @param this The checker.
#! @param node The `construct` node.
#! @param depth How deeply the search has gone.
#! @returns The value's ID, or 0.
def checker::known_literal(&mut this, node: u32, depth: u32): u32 {
val made := this.type_at(node);
val plain := this.unalias(made);
return 0 if this.types.kind_of(plain) != ty::named;
val decl := this.types.first_of(plain);
val kinds := this.field_types(plain);
val inits := this.right_of(node);
if this.decls.kinds.p[decl] == dk::union_type {
return this.known_zero(made, depth + 1) if this.run_length(inits) == 0;
val init := this.run_at(inits, 0);
val index := this.field_index(plain, this.name_of(init));
return 0 if index == checker::no_length;
val value := this.known_of(this.left_of(init), kinds[index], depth + 1);
return 0 if value == 0;
var parts := vector::<u32>::new();
parts.append(value);
return this.intern_known(known { kind = kk::held, of = made, value = constant::integer(index as u64, false) },
parts.span, "");
}
return 0 if this.decls.kinds.p[decl] != dk::struct_type;
var parts := vector::<u32>::new();
parts.resize(kinds.length);
var position: u32 = 0;
for &init in this.run_view(inits) {
var index := position;
if this.node_has(init, nf::named) {
index = this.field_index(plain, this.name_of(init));
} else {
position++;
}
return 0 if index == checker::no_length || index as u64 >= kinds.length;
val value := this.known_of(this.left_of(init), kinds[index], depth + 1);
return 0 if value == 0;
parts[index] = value;
}
# Each field left out is its default, or its zero value.
val run: u32 = this.decls.children.p[decl];
var at: u64 = 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;
if parts[at] == 0 {
val default := this.field_default(field);
val value := default != 0
? this.known_in(this.decls.files.p[field], default, kinds[at], depth + 1)
: this.known_zero(kinds[at], depth + 1);
return 0 if value == 0;
parts[at] = value;
}
at++;
}
return this.intern_known(known { kind = kk::fields, of = made }, parts.span, "");
}
#! What a value depending on constant parameters is once they are given types,
#! worked out where every one it depends on is known.
#! @param this The checker.
#! @param id The value's ID.
#! @param from The type parameters given types.
#! @param to The type given each.
#! @returns The value's ID.
pub def checker::known_subst(&mut this, id: u32, from: span<u32>, to: span<u32>): u32 {
return id if id == 0 || !this.known_depends(id);
val held: known = this.knowns.p[id];
if held.kind == kk::param {
for i in 0..from.length {
continue if from[i] != held.decl || i >= to.length;
val given := to[i];
return this.known_as(this.constant_known(given), held.of) if this.is_constant_arg(given);
return id;
}
return id;
}
# One kept as what is written is worked out again once the types its
# template's type parameters are given hold none.
if held.kind == kk::opaque {
return id if held.given == 0;
val given := this.substitute(held.given, from, to);
return id if given == held.given;
return this.known_again(id, given) if !this.types.is_open(given);
var made := held;
made.given = given;
return this.intern_known(made, span::<u32>::blank(), "");
}
var parts := vector::<u32>::new();
var changed := false;
var depends := false;
for i in 0..this.known_count(id) {
val part := this.known_part(id, i as u32);
val made := this.known_subst(part, from, to);
changed = changed || made != part;
depends = depends || this.known_depends(made);
parts.append(made);
}
return id if !changed;
if !depends {
if held.kind == kk::binary {
val made := this.known_compute(held.op, parts[0], parts[1], held.of);
return made if made != 0;
} else if held.kind == kk::cast {
val made := this.known_convert(parts[0], held.of);
return made if made != 0;
} else if held.kind == kk::unary {
val operand: known = this.knowns.p[parts[0]];
if held.op == tk::minus && operand.kind == kk::real {
return this.known_number(constant::real_of(-operand.value.real), held.of);
}
if held.op == tk::minus && operand.kind == kk::number {
return this.known_number(constant::integer(operand.value.magnitude, !operand.value.negative), held.of);
}
if held.op == tk::negate && operand.kind == kk::number {
return this.known_number(constant::truth(operand.value.magnitude == 0), held.of);
}
if held.op == tk::bitnot && operand.kind == kk::number {
return this.known_number(this.wrap_constant(constant::from_bits(~operand.value.bits(), true), this.unalias(held.of)), held.of);
}
}
}
return this.intern_known(held, parts.span, "");
}
#! Writes how a value is spelled, as it is written: `4`, `true`, `color::red`,
#! `"text"`, `point{ x = 1, y = 2 }`, or `N + 1`.
#! @param this The checker.
#! @param id The value's ID.
#! @param out Where it is written.
#! @param depth How deeply the writing has gone.
pub def checker::describe_known(&this, id: u32, out: &mut string, depth: u32): void {
if depth > 8 || id == 0 {
out.append("...");
return;
}
val held: known = this.knowns.p[id];
val plain := this.unaliased(held.of);
match held.kind {
kk::number => {
if this.types.kind_of(plain) == ty::boolean || this.types.kind_of(plain) == ty::untyped_bool {
out.append(held.value.magnitude != 0 ? "true" : "false");
return;
}
if this.named_kind(plain) == dk::enum_type {
val decl := this.types.first_of(plain);
val run: u32 = this.decls.children.p[decl];
for &member in this.decls.list_view(run) {
var value := constant {};
continue if !this.decl_value(member, &mut value);
if value.magnitude == held.value.magnitude && value.negative == held.value.negative {
out.append(this.decl_text(decl));
out.append("::");
out.append(this.decl_text(member));
return;
}
}
}
val text := $"{held.value.negative ? "-" : ""}{held.value.magnitude}";
out.append(text.span);
}
kk::real => {
val text := $"{held.value.real}";
out.append(text.span);
if !text.span.contains('.') && !text.span.contains('e') && !text.span.contains('n') {
out.append(".0");
}
}
kk::text => {
out.append("\"");
for &byte in this.known_text(id) {
if byte == '"' || byte == '\\' {
out.push('\\');
out.push(byte);
} else if byte == '\n' {
out.append("\\n");
} else if byte == '\t' {
out.append("\\t");
} else if byte < 0x20 || byte == 0x7F {
val hex := "0123456789ABCDEF";
out.append("\\x");
out.push(hex[(byte >> 4) as u64]);
out.push(hex[(byte & 0xF) as u64]);
} else {
out.push(byte);
}
}
out.append("\"");
}
kk::fields => {
# A struct names its fields, and an array or a view lists its elements.
val decl := this.types.kind_of(plain) == ty::named ? this.types.first_of(plain) : 0;
val named := decl != 0 && this.decls.kinds.p[decl] == dk::struct_type
&& this.decls.names.p[decl] != word::span && this.decls.names.p[decl] != word::slice;
if named {
this.describe_into(held.of, out, depth + 1);
out.append("{");
} else {
out.append("[");
}
var field_at: u32 = 0;
val run: u32 = named ? this.decls.children.p[this.types.first_of(plain)] : 0;
for i in 0..this.known_count(id) {
out.append(i == 0 ? (named ? " " : "") : ", ");
if named {
while field_at < this.decls.list_length(run) && this.decls.kinds.p[this.decls.list_at(run, field_at)] != dk::field {
field_at++;
}
if field_at < this.decls.list_length(run) {
out.append(this.decl_text(this.decls.list_at(run, field_at)));
out.append(" = ");
field_at++;
}
}
this.describe_known(this.known_part(id, i as u32), out, depth + 1);
}
out.append(named ? (this.known_count(id) != 0 ? " }" : "}") : "]");
}
kk::held => {
this.describe_into(held.of, out, depth + 1);
out.append("{ ");
if this.types.kind_of(plain) == ty::named {
val run: u32 = this.decls.children.p[this.types.first_of(plain)];
var at: u64 = 0;
for &field in this.decls.list_view(run) {
continue if this.decls.kinds.p[field] != dk::field;
if at == held.value.magnitude {
out.append(this.decl_text(field));
out.append(" = ");
}
at++;
}
}
this.describe_known(this.known_part(id, 0), out, depth + 1);
out.append(" }");
}
kk::null_lit => out.append("null");
kk::function => out.append(this.decl_text(held.decl));
kk::address => {
out.append("&");
out.append(this.decl_text(held.decl));
}
kk::param => out.append(this.decl_text(held.decl));
kk::unary => {
out.append(operator_text(held.op));
this.describe_known(this.known_part(id, 0), out, depth + 1);
}
kk::binary => {
for i in 0..2 {
val part := this.known_part(id, i as u32);
val grouped := this.knowns.p[part].kind == kk::binary;
if i != 0 {
out.append(" ");
out.append(operator_text(held.op));
out.append(" ");
}
out.append(grouped ? "(" : "");
this.describe_known(part, out, depth + 1);
out.append(grouped ? ")" : "");
}
}
kk::cast => {
this.describe_known(this.known_part(id, 0), out, depth + 1);
out.append(" as ");
this.describe_into(held.of, out, depth + 1);
}
kk::opaque => out.append("...");
kk::invalid => out.append("?");
}
}
#! The type at the end of its aliases, without resolving anything.
#! @param this The checker.
#! @param kind The type.
#! @returns The type it stands for.
def checker::unaliased(&this, kind: u32): u32 {
var at := kind;
var steps := 0;
while this.is_alias(at) && steps < 32 {
val target: u32 = this.decls.targets.p[this.types.first_of(at)];
break if target == 0;
at = target;
steps++;
}
return at;
}
#! The spelling of an operator a value over constant parameters is worked out
#! with.
#! @param operator The operator.
#! @returns Its spelling.
def operator_text(operator: tk): strview {
return match operator {
tk::plus => "+", tk::minus => "-", tk::star => "*", tk::slash => "/",
tk::modulo => "%", tk::bitand => "&", tk::bitor => "|", tk::bitxor => "^",
tk::bitnot => "~", tk::lshift => "<<", tk::rshift => ">>", tk::equals => "==",
tk::noteq => "!=", tk::less => "<", tk::lesseq => "<=", tk::more => ">",
tk::moreeq => ">=", tk::logand => "&&", tk::logor => "||", tk::negate => "!",
else => "?"
};
}
#! The value a constant parameter's name gives: the constant its instance is
#! given, known while checking where it is a number, or within the template, a
#! value of its type only each instance knows.
#! @param this The checker.
#! @param node The node naming it.
#! @param kind The constant type argument it names.
#! @returns The type of its value.
pub def checker::constant_ref(&mut this, node: u32, kind: u32): u32 {
if this.types.kind_of(kind) == ty::value {
val held: known = this.knowns.p[this.types.second_of(kind)];
if held.kind == kk::number || held.kind == kk::real {
this.set_value(node, held.value);
}
}
return this.constant_type(kind);
}
#! Whether a node is a type as written, rather than a value.
#! @param this The checker.
#! @param node The node.
#! @returns Whether it is.
def checker::is_type_node(&this, node: u32): bool {
return match this.kind_of(node) {
nk::type_name, nk::type_ref, nk::type_ptr, nk::type_fixed, nk::type_func, nk::type_spread, nk::type_of, nk::type_match, nk::type_cond,
nk::error => true,
else => false
};
}
#! The type argument a generic's type parameter is given by what is written for
#! it: a type, or for a constant parameter, the value written, worked out while
#! checking.
#! @param this The checker.
#! @param node What is written.
#! @param decl The generic declaration.
#! @param index The parameter's position among the declaration's, its owner's
#! first.
#! @param given The type arguments given the parameters before it.
#! @returns The type argument, or the error type.
pub def checker::type_arg_of(&mut this, node: u32, decl: u32, index: u32, given: span<u32>): u32 {
# With patterns among its type parameters, what is written at a place is given
# to the type parameter written there, and to a pattern's the parts of it.
val params: u32 = decl != 0 ? this.decls.params.p[decl] : 0;
val written: u32 = decl != 0 && this.has_patterns(decl)
? this.param_written_at(decl, index)
: index < this.decls.list_length(params) ? this.decls.list_at(params, index) : 0;
if written != 0 {
val param := written;
if this.decls.has(param, df::valued) {
this.resolve(param);
var from := vector::<u32>::new();
for i in 0..given.length {
break if i >= this.decls.list_length(params) as u64;
from.append(this.decls.list_at(params, i as u32));
}
val wanted := this.substitute(this.decls.targets.p[param], from.span, given);
return this.constant_arg(node, wanted, param, decl);
}
if !this.is_type_node(node) && !this.decls.has(param, df::packed) {
var text := string::from("`", nothing {});
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` takes a type, so it is not given a value.");
this.report_text(node, ec::generic, &text);
this.infer_expr(node);
return bt::error;
}
}
if !this.is_type_node(node) {
this.report(node, ec::generic, "A value is given only to a constant parameter, as `N` of `struct buffer<const N: u64>`.");
this.infer_expr(node);
return bt::error;
}
# `Ts...` passes a pack on, which gives its types where it is written.
if this.kind_of(node) == nk::type_spread {
val made := this.resolve_type(this.left_of(node));
if made != bt::error && this.types.kind_of(made) != ty::pack {
this.report(node, ec::generic, "Only a pack is passed on with `...`, as `Ts...`.");
return bt::error;
}
return made;
}
val made := this.resolve_type(node);
if this.types.kind_of(made) == ty::pack {
this.report(node, ec::generic, "A pack is passed on among type arguments written `Ts...`.");
return bt::error;
}
return made;
}
#! The type argument a constant parameter is given by what is written for it:
#! the value of the expression, worked out while checking, as a value of the
#! parameter's type where that is known. A name written alone was read as a
#! type, and is the constant it names, and `&table` the address of one.
#! @param this The checker.
#! @param node What is written.
#! @param wanted The parameter's type, given the arguments before it.
#! @param param The constant parameter.
#! @param decl The generic declaration.
#! @returns The type argument, or the error type.
def checker::constant_arg(&mut this, node: u32, wanted: u32, param: u32, decl: u32): u32 {
val known_type := wanted != bt::error && !this.types.is_open(wanted);
var kind: u32 = bt::error;
val written := this.kind_of(node);
if written == nk::type_name || (written == nk::type_ref && this.kind_of(this.left_of(node)) == nk::type_name) {
# A name read as a type is the constant it names, and `&table` the address of
# one. A name of a type is a type given where a value is wanted.
val named := written == nk::type_ref ? this.left_of(node) : node;
if this.names_type(named) {
var text := string::from("`", nothing {});
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` is a constant, so it is given a value, not the type `");
text.append(this.names.text(this.segment_name(named, 0)));
text.append("`.");
this.report_text(named, ec::generic, &text);
return bt::error;
}
kind = this.constant_name(named, written == nk::type_ref ? bt::error : wanted);
return bt::error if kind == bt::error;
if written == nk::type_ref {
kind = this.types.reference(kind, this.node_has(node, nf::mutable));
this.set_type(node, kind);
if known_type && !this.convert(node, wanted) {
return bt::error;
}
}
} else if this.is_type_node(node) && written != nk::error {
var text := string::from("`", nothing {});
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` is a constant, so it is given a value rather than a type.");
this.report_text(node, ec::generic, &text);
return bt::error;
} else {
kind = known_type ? this.check_expr(node, wanted) : this.infer_expr(node);
}
return bt::error if kind == bt::error;
val of := known_type ? wanted : this.settle(kind);
val errors := this.errors;
val id := this.known_or_run(node, of);
return bt::error if id == 0 && this.errors != errors;
if id == 0 {
var text := string::from("`", nothing {});
text.append(this.decl_text(param));
text.append("` of `");
text.append(this.decl_text(decl));
text.append("` is given a constant, which is worked out while checking: a literal, a `const`, a member of an enum, a function, `null`, the address of a constant, a struct, union, or array literal of those, arithmetic over them, or what a function gives of them.");
this.report_text(node, ec::constant, &text);
return bt::error;
}
val held: known = this.knowns.p[id];
val plain := this.unalias(of);
if held.kind == kk::number && (this.types.kind_of(plain) == ty::signed || this.types.kind_of(plain) == ty::unsigned)
&& !this.in_range(plain, &held.value) {
var text := string::from("The constant is outside the range of `", nothing {});
val shown := this.describe(of);
text.append(shown.span);
text.append("`.");
this.report_text(node, ec::overflow, &text);
return bt::error;
}
return this.known_type(id);
}
#! Whether a type name of one segment names a type, rather than a constant.
#! @param this The checker.
#! @param node The `type_name` node.
#! @returns Whether it does.
def checker::names_type(&mut this, node: u32): bool {
return false if this.segment_count(node) != 1;
val bound := this.bound(this.segment_name(node, 0));
return !this.is_constant_arg(bound & ref_mask) if (bound & ref_type) != 0;
return false if bound != 0;
val decl := this.lookup_type(this.segment_name(node, 0), 0);
return decl != 0 && this.is_type_decl(decl);
}
#! Checks a name or path written as a type as the value it names, recording
#! what it refers to and its type as an expression's.
#! @param this The checker.
#! @param node The `type_name` node.
#! @param wanted The type expected, or the error type.
#! @returns Its type, or the error type.
def checker::constant_name(&mut this, node: u32, wanted: u32): u32 {
var kind: u32 = bt::error;
if this.segment_count(node) == 1 {
val name := this.segment_name(node, 0);
val ref := this.lookup(name, node);
if ref == 0 {
this.report_undeclared(node, name);
return bt::error;
}
kind = this.ref_value(node, ref, wanted);
} else {
val target := this.path_target(node);
return bt::error if target.decl == 0;
kind = this.decl_value_of(node, target.decl, wanted);
}
this.set_type(node, kind);
this.set_flag(node, rf::checked);
return bt::error if kind == bt::error;
if wanted != bt::error && !this.types.is_open(wanted) && !this.convert(node, wanted) {
return bt::error;
}
return kind;
}