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

416 lines
15 KiB
Plaintext

#! @file results.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the result tables, which hold what the checker works out of each
#! node: its type, what it refers to, and its flags, with the constant it is
#! and the type it converts to where it has them. A file has a table of all of
#! its nodes, and an instance of a generic a table of its template's nodes, so
#! the same node has the results of each instance it is checked in.
module sakura::analyzer;
#! What a node's flags hold.
pub enum rf : u16 {
place = 0x0001, #! It names a place: a variable, parameter, field, element, or what a reference or pointer reaches.
writable = 0x0002, #! The place can be written.
known = 0x0004, #! Its value is known while compiling, and is in `r_values`.
local = 0x0008, #! It declares a local, whose type is the node's type.
mutable = 0x0010, #! The local it declares can be written.
checked = 0x0020, #! It has been checked, which a node reached twice is checked once for.
borrowed = 0x0040, #! A reference to it was taken for a parameter, as `&` before it would.
leaves = 0x0080, #! A statement after which the block never goes on: it returns, breaks, or continues.
method = 0x0100, #! A member that is a method, which a call gives `this`.
narrowed = 0x0200, #! A binding of what an `is` test or match arm names.
reflected = 0x0400, #! It gives descriptions of reflection, which are in `reflections`.
invokes = 0x0800, #! A call of a value of no function type, through its `invoke`, whose `this` the callee is.
valued = 0x1000, #! A reference taken as the value it reaches, as a binding, a pattern, `for`, or inference takes one not written with `&`.
relent = 0x2000, #! `&` of a reference, or of a field holding one, which is that reference, since references never stack.
adapted = 0x4000, #! A function named as a value of another function type, which a function made for it calls it as, as a call would.
}
#! What a node's reference holds: a declaration, or with one of these bits, the
#! node declaring a local, or a type a type parameter is bound to.
pub const ref_local: u32 = 0x80000000; #! The node declaring a local, in the bits of `ref_mask`.
pub const ref_type: u32 = 0x40000000; #! A type a type parameter is bound to, in the bits of `ref_mask`.
pub const ref_mask: u32 = 0x3FFFFFFF; #! The bits holding the declaration, node, or type.
#! Adds a result table of `count` nodes from `base` of the file.
#! @param this The checker.
#! @param file The file whose nodes it holds results of.
#! @param base The first node it holds.
#! @param count How many nodes it holds.
#! @param instance The instance it is of, or 0 for the file's own.
#! @returns Its index.
pub def checker::add_table(&mut this, file: u32, base: u32, count: u32, instance: u32): u32 {
val index := this.tables.length as u32;
this.tables.append(rtable {
file = file,
base = base,
count = count,
offset = this.r_types.length as u32,
instance = instance
});
this.resize_results(this.r_types.length + count as u64);
return index;
}
#! Makes each result column hold a number of nodes' results, the nodes added
#! holding none.
#! @param this The checker.
#! @param total How many nodes' results each holds after.
def checker::resize_results(&mut this, total: u64): void {
this.r_types.resize(total);
this.r_refs.resize(total);
this.r_flags.resize(total);
}
#! Copies a node's results, at one row of the results, to another.
#! @param this The checker.
#! @param from The row to copy from.
#! @param to The row to copy to.
def checker::copy_results(&mut this, from: u64, to: u64): void {
this.r_types.p[to] = this.r_types.p[from];
this.r_refs.p[to] = this.r_refs.p[from];
this.r_flags.p[to] = this.r_flags.p[from];
copy_row(&mut this.r_converts, from, to);
copy_row(&mut this.r_values, from, to);
}
# Copies what a map holds of one row to another, or takes what it holds of the
# other where it holds nothing of the one.
def copy_row(rows: &mut keymap, from: u64, to: u64): void {
if rows.get(&from) is some(held) {
rows.set(to, held);
} else {
rows.remove(&to);
}
}
#! Lets go of a node's results, at a row of the results.
#! @param this The checker.
#! @param at The row.
def checker::drop_results(&mut this, at: u64): void {
this.r_types.p[at] = 0;
this.r_refs.p[at] = 0;
this.r_flags.p[at] = 0;
this.r_converts.remove(&at);
this.r_values.remove(&at);
}
#! Adds a result table of the nodes from `base`, within the one being written,
#! whose nodes it holds but for its own: what a body checked once for each of
#! several things, as `for const`, writes for each.
#! @param this The checker.
#! @param base The first node it holds.
#! @param count How many nodes it holds.
#! @returns Its index.
pub def checker::add_child_table(&mut this, base: u32, count: u32): u32 {
val index := this.add_table(this.at.file, base, count, this.tables.p[this.at.table].instance);
this.tables.p[index].parent = this.at.table;
return index;
}
#! Makes the table the one results are read from and written to.
#! @param this The checker.
#! @param table The table's index.
pub def checker::use_table(&mut this, table: u32): void {
val held: rtable = this.tables.p[table];
this.at.table = table;
this.at.file = held.file;
this.at.base = held.base;
this.at.offset = held.offset;
this.at.count = held.count;
val syntax := &this.units[held.file].syntax;
this.at.kinds = syntax.node_kinds.span.data;
this.at.tokens = syntax.node_tokens.span.data;
this.at.lefts = syntax.node_lefts.span.data;
this.at.rights = syntax.node_rights.span.data;
this.at.extras = syntax.extras.span.data;
this.at.names = syntax.token_names.span.data;
this.at.tkinds = syntax.token_kinds.span.data;
}
#! The table holding the node's results: the one being written, or the one it
#! was made within that holds the node, or, outside an instance, the one of
#! the file's own that does, or `no_table`.
#! @param this The checker.
#! @param node The node.
#! @returns The table's index, or `no_table`.
def checker::holding(&this, node: u32): u32 {
return this.at.table if node >= this.at.base && node - this.at.base < this.at.count;
var table: u32 = this.tables.p[this.at.table].parent;
var steps := 0;
while table != 0 && steps < 32 {
val held: rtable = this.tables.p[table];
return table if node >= held.base && node - held.base < held.count;
table = held.parent;
steps++;
}
return checker::no_table if this.tables.p[this.at.table].instance != 0;
return this.file_table_of(this.at.file, node);
}
#! The table of a file's own holding a node's results: of the nodes it was
#! parsed with, or of those an edit added.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @returns The table's index, or `no_table` where the node is past the file's.
pub def checker::file_table_of(&this, file: u32, node: u32): u32 {
val segments := &this.units[file].segments;
var low: u64 = 0;
var high := segments.length;
while low < high {
val middle := (low + high) / 2;
val held: rtable = this.tables.p[segments[middle]];
if node < held.base {
high = middle;
} else if node - held.base >= held.count {
low = middle + 1;
} else {
return segments[middle];
}
}
return checker::no_table;
}
#! Where a node's results are among its file's own, in the result vectors.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @returns The index of its results, or past their end where no table of the
#! file holds it.
pub def checker::file_row(&this, file: u32, node: u32): u64 {
val table := this.file_table_of(file, node);
return this.r_types.length if table == checker::no_table;
val held: rtable = this.tables.p[table];
return held.offset as u64 + (node - held.base) as u64;
}
#! What says no table holds a node.
pub const checker::no_table: u32 = 0xFFFFFFFF;
#! Where the node's results are in the result vectors, or past their end where
#! no table being written holds the node. Every read and write of a result
#! finds it here, so it is copied where it is called.
#! @param this The checker.
#! @param node The node.
#! @returns The index of its results.
@inline
def checker::slot(&this, node: u32): u64 {
if node >= this.at.base && node - this.at.base < this.at.count {
return this.at.offset as u64 + (node - this.at.base) as u64;
}
return this.slot_elsewhere(node);
}
#! Where the results are of a node the table being written does not hold, as
#! `slot` says, which is apart so `slot` stays small where it is copied.
#! @param this The checker.
#! @param node The node.
#! @returns The index of its results.
def checker::slot_elsewhere(&this, node: u32): u64 {
val table := this.holding(node);
return this.r_types.length if table == checker::no_table;
val held: rtable = this.tables.p[table];
return held.offset as u64 + (node - held.base) as u64;
}
#! The type checking gave a node.
#! @param this The checker.
#! @param node The node.
#! @returns Its type, or 0 where it has none, or no table being written holds
#! it.
@inline
pub def checker::type_at(&this, node: u32): u32 {
val at := this.slot(node);
return at < this.r_types.length ? this.r_types.p[at] : 0;
}
#! Records the type of a node.
#! @param this The checker.
#! @param node The node, whose type is not recorded where no table being
#! written holds it.
#! @param kind The type.
@inline
pub def checker::set_type(&mut this, node: u32, kind: u32): void {
val at := this.slot(node);
if at < this.r_types.length {
this.r_types.p[at] = kind;
}
}
#! What a node refers to.
#! @param this The checker.
#! @param node The node.
#! @returns A declaration, a local or a type with its bit of `ref_local` or
#! `ref_type`, or 0.
@inline
pub def checker::ref_at(&this, node: u32): u32 {
val at := this.slot(node);
return at < this.r_refs.length ? this.r_refs.p[at] : 0;
}
#! Records what a node refers to.
#! @param this The checker.
#! @param node The node.
#! @param ref A declaration, or a local or a type with its bit.
@inline
pub def checker::set_ref(&mut this, node: u32, ref: u32): void {
val at := this.slot(node);
if at < this.r_refs.length {
this.r_refs.p[at] = ref;
}
}
#! The flags of a node, as `rf` says.
#! @param this The checker.
#! @param node The node.
#! @returns Its flags, or 0.
@inline
pub def checker::flags_at(&this, node: u32): u32 {
val at := this.slot(node);
return at < this.r_flags.length ? this.r_flags.p[at] as u32 : 0;
}
#! Whether a node has a flag.
#! @param this The checker.
#! @param node The node.
#! @param flag The flag.
#! @returns Whether it has it.
@inline
pub def checker::has_flag(&this, node: u32, flag: rf): bool {
return (this.flags_at(node) & flag as u32) != 0;
}
#! Gives a node a flag.
#! @param this The checker.
#! @param node The node.
#! @param flag The flag.
@inline
pub def checker::set_flag(&mut this, node: u32, flag: rf): void {
val at := this.slot(node);
if at < this.r_flags.length {
this.r_flags.p[at] |= flag as u16;
}
}
#! Takes a flag from a node.
#! @param this The checker.
#! @param node The node.
#! @param flag The flag.
@inline
pub def checker::clear_flag(&mut this, node: u32, flag: rf): void {
val at := this.slot(node);
if at < this.r_flags.length {
this.r_flags.p[at] &= ~(flag as u16);
}
}
#! The node as the table holding its results holds it, for the results kept
#! apart from the tables: the one being written, or one around it.
#! @param this The checker.
#! @param node The node.
#! @returns Its table and node.
pub def checker::table_node_of(&this, node: u32): table_node {
val table := this.holding(node);
return table_node { table = table == checker::no_table ? this.at.table : table, node = node };
}
#! Records that the node's value converts to the type, which is not its own.
#! @param this The checker.
#! @param node The node.
#! @param kind The type it converts to.
pub def checker::set_convert(&mut this, node: u32, kind: u32): void {
val at := this.slot(node);
if at < this.r_types.length {
if kind != 0 {
this.r_converts.set(at, kind);
} else {
this.r_converts.remove(&at);
}
return;
}
this.converts.set(this.table_node_of(node), kind);
}
#! The type the node's value becomes where it is used: what it converts to, or
#! its own type.
#! @param this The checker.
#! @param node The node.
#! @returns The type.
pub def checker::converted(&this, node: u32): u32 {
val at := this.slot(node);
if at < this.r_types.length {
return this.r_converts.get_or(&at, this.r_types.p[at]);
}
val key := this.table_node_of(node);
return this.converts.get_or(&key, this.type_at(node));
}
#! Records that the node's value is known while compiling.
#! @param this The checker.
#! @param node The node.
#! @param value Its value.
pub def checker::set_value(&mut this, node: u32, value: constant): void {
val id := this.value_known(value);
val at := this.slot(node);
if at < this.r_types.length {
this.r_values.set(at, id);
this.r_flags.p[at] |= rf::known as u16;
return;
}
this.values.set(this.table_node_of(node), id);
this.set_flag(node, rf::known);
}
#! The ID among `knowns` of a number, `bool`, character, member of an enum, or
#! float, of no type in particular, as the values of nodes and declarations
#! are kept, interned, so each value is held once however many nodes are it:
#! most are 0, 1, or a small number.
#! @param this The checker.
#! @param value The value.
#! @returns Its ID.
pub def checker::value_known(&mut this, value: constant): u32 {
val hash := mix(mix(value.word, value.negative ? 1 : 0), value.floating ? 1 : 0);
var walk := this.value_index.walk(hash);
var at := this.value_index.next(&mut walk);
while at != 0 {
return at - 1 if this.knowns.p[at - 1].value.same(&value);
at = this.value_index.next(&mut walk);
}
val id := this.intern_known(known { kind = value.floating ? kk::real : kk::number, value = value }, span::<u32>::blank(), "");
this.value_index.add(hash, id);
return id;
}
#! Whether the node's value is known while compiling.
#! @param this The checker.
#! @param node The node.
#! @returns Whether it is.
@inline
pub def checker::is_known(&this, node: u32): bool {
return this.has_flag(node, rf::known);
}
#! The node's value, where it is known, or a zero constant: constant 0, which
#! stands for none.
#! @param this The checker.
#! @param node The node.
#! @returns Its value.
pub def checker::value_at(&this, node: u32): constant {
# A node a table holds says whether it has a value, which most have none of.
val at := this.slot(node);
if at < this.r_types.length {
return (this.r_flags.p[at] & rf::known as u16) != 0 ? this.knowns.p[this.r_values.get_or(&at, 0)].value : constant {};
}
val key := this.table_node_of(node);
return this.knowns.p[this.values.get_or(&key, 0)].value;
}