1373 lines
55 KiB
Plaintext
1373 lines
55 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.
|
|
|
|
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`.
|
|
}
|
|
|
|
#! 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, or a constant parameter.
|
|
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.
|
|
}
|
|
|
|
#! 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.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].view().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, made.op as u64);
|
|
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].view();
|
|
}
|
|
|
|
#! 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>::empty(), "");
|
|
}
|
|
|
|
#! 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.view(), 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.view());
|
|
}
|
|
|
|
#! 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 {
|
|
val kind: u32 = this.decls.types.p[held.decl];
|
|
for &each in into.view() {
|
|
return if each == kind;
|
|
}
|
|
into.append(kind);
|
|
return;
|
|
}
|
|
for &part in this.known_view(id) {
|
|
this.known_params(part, into, depth + 1);
|
|
}
|
|
}
|
|
|
|
#! 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`, and a fixed array of a
|
|
#! length that is now a number holding that many.
|
|
#! @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) {
|
|
return this.substitute_projected(kind, from, to);
|
|
}
|
|
val made := this.types.substitute(kind, from, to);
|
|
return this.types.holds_value(kind) ? this.settle_values(made, from, to, 0) : made;
|
|
}
|
|
|
|
#! Whether a type parameter substitution replaces has types its bounds ask for
|
|
#! standing in for them, as `C` of `C::cursor`.
|
|
#! @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 ¶m in from {
|
|
return true if param == base;
|
|
}
|
|
}
|
|
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`.
|
|
#! @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));
|
|
}
|
|
}
|
|
val made := this.types.substitute(kind, wider.view(), given.view());
|
|
return this.types.holds_value(kind) ? this.settle_values(made, wider.view(), given.view(), 0) : made;
|
|
}
|
|
|
|
#! 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);
|
|
return this.known_type(made);
|
|
}
|
|
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>::empty());
|
|
}
|
|
ty::function, ty::named, ty::pack => {
|
|
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.view()) : 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>::empty(), "");
|
|
}
|
|
|
|
#! 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>::empty(), "");
|
|
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.view(), "");
|
|
}
|
|
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>::empty(), "");
|
|
}
|
|
return this.intern_known(known { kind = kk::fields, of = kind }, span::<u32>::empty(), "");
|
|
}
|
|
|
|
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.view(), "");
|
|
}
|
|
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.view(), "");
|
|
}
|
|
|
|
#! 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.ev_wrap(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;
|
|
}
|
|
return this.intern_known(made, parts, "");
|
|
}
|
|
|
|
#! 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 {
|
|
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.view(), "");
|
|
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>::empty(), bytes.view());
|
|
}
|
|
nk::null_lit => return this.intern_known(known { kind = kk::null_lit, of = gives }, span::<u32>::empty(), "");
|
|
nk::name, nk::path, nk::type_name => {
|
|
val ref := this.ref_at(node);
|
|
return 0 if ref == 0 || (ref & ref_local) != 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>::empty(), "");
|
|
}
|
|
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;
|
|
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.view()) 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.view());
|
|
}
|
|
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.view());
|
|
}
|
|
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>::empty()) 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.view(), ""), 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>::empty(), "");
|
|
}
|
|
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>::empty()) 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>::empty())
|
|
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;
|
|
}
|
|
|
|
#! 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;
|
|
val record := syntax.node_lefts[this.decls.nodes.p[decl]];
|
|
return syntax.extras[record as u64 + index as u64];
|
|
}
|
|
|
|
#! 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.view(), "");
|
|
}
|
|
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.view(), "");
|
|
}
|
|
|
|
#! 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;
|
|
}
|
|
return id if held.kind == kk::opaque;
|
|
|
|
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.ev_wrap(constant::from_bits(~operand.value.bits(), true), this.unalias(held.of)), held.of);
|
|
}
|
|
}
|
|
}
|
|
return this.intern_known(held, parts.view(), "");
|
|
}
|
|
|
|
#! 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.view());
|
|
}
|
|
kk::real => {
|
|
val text := $"{held.value.real}";
|
|
out.append(text.view());
|
|
if !text.view().contains('.') && !text.view().contains('e') && !text.view().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::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.view(), 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 id := this.known_of(node, of, 0);
|
|
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, or arithmetic over 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.view());
|
|
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;
|
|
}
|