Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+650
View File
@@ -0,0 +1,650 @@
#! @file incremental.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains checking again after an edit. While a declaration is checked, what
#! it depends on is recorded: each declaration it resolves, and each name it
#! looks up outside any scope, found or not. Once a file is parsed again, which
#! keeps the nodes of the top-level declarations an edit did not touch, the
#! checker keeps what it worked out of those, declares what is new, lets go of
#! what is gone, and checks again only what depends on what changed. A changed
#! declaration of the same kind, module, and name keeps its ID, so what other
#! files worked out of it stays.
import std::containers;
module sakura::analyzer;
#! Records that the declaration being checked depends on the declaration.
#! @param this The checker.
#! @param id The declaration depended on.
pub def checker::depend(&mut this, id: u32): void {
val from := this.top_decl(this.at.decl);
return if from == 0 || id == 0 || from == id;
this.reach(id);
return if !this.tracks;
this.add_dependency(from, id);
}
#! Records that checking reached a declaration, which is checked in its turn,
#! where it was not reached before.
#! @param this The checker.
#! @param id The declaration.
pub def checker::reach(&mut this, id: u32): void {
if id as u64 >= this.depended.length {
this.depended.resize(this.decls.count > id as u64 ? this.decls.count : id as u64 + 1);
}
return if this.depended[id];
this.depended[id] = true;
this.queued.append(id);
}
#! Whether any declaration depends on a declaration, as resolving it.
#! @param this The checker.
#! @param id The declaration.
#! @returns Whether one does.
pub def checker::is_depended(&this, id: u32): bool {
return (id as u64) < this.depended.length && this.depended[id];
}
#! Records that the declaration being checked looked up the name outside any
#! scope, so a declaration of the name made later may be what it means.
#! @param this The checker.
#! @param name The ID of the name.
pub def checker::depend_name(&mut this, name: u32): void {
return if !this.tracks;
val from := this.top_decl(this.at.decl);
return if from == 0;
this.add_dependency(from, name | checker::named_dependency);
}
#! What a dependency on a name, rather than a declaration, has set.
pub const checker::named_dependency: u32 = 0x80000000;
def checker::add_dependency(&mut this, from: u32, to: u32): void {
val key := pair_key(from, to);
return if !this.dep_seen.insert(key, 1);
this.dep_from.append(from);
this.dep_to.append(to);
}
#! The top-level declaration a declaration was declared with: itself, or the
#! type a field or member is of, or the declaration a type parameter is of.
#! @param this The checker.
#! @param id The declaration.
#! @returns The top-level declaration.
pub def checker::top_decl(&this, id: u32): u32 {
var at := id;
var steps := 0;
while at != 0 && steps < 8 {
val node: u32 = this.decls.nodes.p[at];
val top: u32 = this.decls.tops.p[at];
return at if top == 0 || node == top;
at = this.decls.owners.p[at];
steps++;
}
return id;
}
#! A top-level declaration of a file, and the module it is declared in.
struct checker::placed {
var node: u32; #! The declaration's node.
var home: u32; #! The module it is declared in.
var external: bool; #! Whether it is in an `extern module`.
}
#! The top-level declarations the file declares, in the branches of `when` the
#! target takes, each with the module it is declared in, noting the libraries
#! its `extern import`s name anew.
#! @param this The checker.
#! @param file The file.
#! @returns Each declaration's node and module, in order.
def checker::placements(&mut this, file: u32): vector<checker::placed> {
var kept := vector::<file_node>::new();
for &each in this.libraries {
if each.file != file {
kept.append(each);
}
}
this.libraries = move(&mut kept);
var made := vector::<checker::placed>::new();
val decls := this.left_of(this.units[file].root);
var home: u32 = 0;
var active := true;
var external := false;
for &node in this.run_view(decls) {
match this.kind_of(node) {
nk::import_decl => {}
nk::extern_import => {
if this.right_of(node) == 0 || this.target_holds(this.right_of(node)) {
this.add_library(file, node);
}
}
nk::module_decl, nk::extern_module => {
home = this.module_path(this.left_of(node), true);
active = this.right_of(node) == 0 || this.target_holds(this.right_of(node));
external = this.kind_of(node) == nk::extern_module;
if external {
this.check_section(node);
}
}
else => {
if active {
this.place_top(node, home, external, &mut made);
}
}
}
}
return made;
}
def checker::place_top(&mut this, node: u32, home: u32, external: bool, into: &mut vector<checker::placed>): void {
match this.kind_of(node) {
nk::when_block => {
val chosen := this.when_branch(node);
return if chosen == 0;
val decls := this.left_of(chosen);
for &node in this.run_view(decls) {
this.place_top(node, home, external, into);
}
}
nk::error => {}
else => into.append(checker::placed { node = node, home = home, external = external });
}
}
#! The key a top-level declaration is matched by across an edit: its kind,
#! module, and name, each whole, so no two declarations differing in any share
#! one.
struct checker::match_key : hashable, equatable {
var kind: dk; #! Its kind.
var home: u32; #! The module it is declared in.
var name: u32; #! The ID of its name.
}
#! Hashes the key, as the dictionary of keys asks.
#! @param this The key.
#! @returns Its hash code.
pub def checker::match_key::hash(&this): u64 {
return mix(pair_key(this.home, this.name), this.kind as u64);
}
#! Whether two keys are of the same kind, module, and name.
#! @param this The key.
#! @param other The other key.
#! @returns Whether they are.
pub def checker::match_key::equals(&this, other: &checker::match_key): bool {
return this.kind == other.kind && this.home == other.home && this.name == other.name;
}
#! Checks the file again once it is parsed again, as `syntax::reparse` does,
#! checking again only what the edit affects.
#! @param this The checker.
#! @param file The file.
#! @param source The file's text as it was edited, which its tokens are of
#! now.
#! @param root The file's `document` node, as parsing it again gave it.
#! @returns How many top-level declarations were checked again.
pub def checker::update(&mut this, file: u32, source: &source, root: u32): u32 {
val saved := this.at;
this.units[file].source = &source;
this.units[file].root = root;
# Parsing the file whole again made every node anew, so none of the old ones
# is kept, however their indexes fall.
val whole := this.units[file].syntax.parses != this.units[file].parses;
this.units[file].parses = this.units[file].syntax.parses;
# The nodes kept keep their results where they are. Those an edit added, past
# the file's last, have a table of their own, and a file parsed whole again a
# table of all of them.
val count := this.units[file].syntax.node_count as u32;
val last: rtable = this.tables.p[this.units[file].table];
if whole {
val fresh := this.add_table(file, 0, count, 0);
this.units[file].table = fresh;
this.units[file].segments.clear();
this.units[file].segments.append(fresh);
} else if count > last.base + last.count {
val fresh := this.add_table(file, last.base + last.count, count - last.base - last.count, 0);
this.units[file].table = fresh;
this.units[file].segments.append(fresh);
}
this.use_table(this.units[file].table);
# The top-level declarations the file has now, and those it had.
val placed := this.placements(file);
var now := dict::<u32, u32>::new(); # Each top-level node now, with its module.
for &each in placed {
now.set(each.node, each.home);
}
var kept := dict::<u32, u32>::new(); # The top-level nodes kept, and their declarations.
var gone := vector::<u32>::new(); # The top-level declarations whose nodes are gone.
for &decl in this.decls.by_file.of(file) {
continue if this.decls.kinds.p[decl] == dk::none || this.decls.kinds.p[decl] == dk::builtin;
val top: u32 = this.decls.tops.p[decl];
continue if top == 0 || this.decls.nodes.p[decl] != top;
if !whole && now.contains(&top) && this.decl_kind(top) == this.decls.kinds.p[decl] {
if !kept.contains(&top) {
kept.set(top, decl);
}
} else {
gone.append(decl);
}
}
# A top-level declaration gone whose kind, module, and name a new one has
# keeps its ID, so what other files worked out of it stays.
var reusable := dict::<checker::match_key, u32>::new();
for &decl in gone {
val key := checker::match_key {
kind = this.decls.kinds.p[decl as u32],
home = this.decls.modules.p[decl as u32],
name = this.decls.names.p[decl as u32]
};
if !reusable.contains(&key) {
reusable.set(key, decl);
}
}
var changed := vector::<u32>::new();
var olds := dict::<u32, u32>::new(); # The type each reused declaration had.
for &decl in gone {
this.forget_members(decl, file);
}
this.pending.clear();
this.at.scope = 0;
var made := vector::<u32>::new();
for &each in placed {
continue if kept.contains(&each.node);
this.at.home = each.home;
val kind := this.decl_kind(each.node);
val name := this.name_of(each.node);
val key := checker::match_key { kind = kind, home = each.home, name = name };
if reusable.contains(&key) {
val decl: u32 = reusable[key];
reusable.remove(&key);
olds.set(decl, this.decls.types.p[decl]);
this.external = each.external;
this.redeclare(decl, each.node, each.home);
this.external = false;
made.append(decl);
continue;
}
this.decls.top = each.node;
this.external = each.external;
val decl := this.declare_decl(each.node, 0);
this.external = false;
this.decls.top = 0;
if decl != 0 {
made.append(decl);
changed.append(decl);
}
}
# What is gone and not reused leaves every scope it was in.
for &each in gone {
val decl: u32 = each;
continue if olds.contains(&decl);
this.forget(decl);
changed.append(decl);
}
this.relink(file);
this.forget_caches();
# Checking again: what is new or reused, then whatever depends on what
# changed, and on what that changes in turn.
var checked := hashset::<u32>::new();
var queue := vector::<u32>::new();
queue.extend(made.span);
var at: u64 = 0;
var rounds := 0;
while rounds < 64 {
rounds++;
while at < queue.length {
val decl: u32 = queue[at];
at++;
continue if checked.contains(&decl) || this.decls.kinds.p[decl] == dk::none;
checked.insert(decl);
val before := olds.get_or(&decl, this.decls.types.p[decl]);
this.check_again(decl);
val after: u32 = this.decls.types.p[decl];
if !olds.contains(&decl) || before != after || this.is_type_decl(decl) {
changed.append(decl);
}
}
# Whatever depends on what changed is checked again.
val queued := queue.length;
var names := hashset::<u32>::new();
var ids := hashset::<u32>::new();
for &decl in changed {
ids.insert(decl);
names.insert(this.decls.names.p[decl as u32]);
}
changed.clear();
for i in 0..this.dep_from.length {
val to: u32 = this.dep_to[i];
val from: u32 = this.dep_from[i];
val hit := (to & checker::named_dependency) != 0
? names.contains(&(to & ~checker::named_dependency))
: ids.contains(&to);
if hit && !checked.contains(&from) {
queue.append(from);
}
}
break if queue.length == queued;
}
this.check_reached();
this.recount_notes();
this.at = saved;
return checked.length as u32;
}
#! Lets go of what a top-level declaration declared within it: its members,
#! its type parameters, and what the checker made of it, which declaring it
#! again makes anew.
#! @param this The checker.
#! @param decl The top-level declaration.
#! @param file The file it is declared in.
def checker::forget_members(&mut this, decl: u32, file: u32): void {
val top: u32 = this.decls.tops.p[decl];
for &each in this.decls.by_file.of(file) {
continue if each == decl || this.decls.tops.p[each] != top;
this.forget(each);
}
this.decls.children.p[decl] = 0;
}
#! Lets go of a declaration: it leaves the module or type declaring it, and is
#! no longer anything.
#! @param this The checker.
#! @param decl The declaration.
def checker::forget(&mut this, decl: u32): void {
val kind: dk = this.decls.kinds.p[decl];
return if kind == dk::none;
val owner: u32 = this.decls.owners.p[decl];
val name: u32 = this.decls.names.p[decl];
if owner != 0 && (kind != dk::type_param) {
this.leave_type(owner, decl);
} else if kind != dk::type_param {
val home: u32 = this.decls.modules.p[decl];
val key := pair_key(home, name);
this.unchain(this.modules.decl(home, name), decl, key, chained::modules);
}
this.decls.kinds.p[decl] = dk::none;
this.decls.overloads.p[decl] = 0;
this.decls.tops.p[decl] = 0;
}
#! Takes a member out of its type's members, or out of the types declared
#! through its type.
#! @param this The checker.
#! @param owner The type's declaration.
#! @param decl The member's declaration.
def checker::leave_type(&mut this, owner: u32, decl: u32): void {
val name: u32 = this.decls.names.p[decl];
val key := pair_key(owner, name);
val added := this.decls.added_member(owner, name);
if added != 0 && !this.in_core(this.decls.modules.p[decl]) {
this.unchain(added, decl, key, chained::added);
return;
}
if this.decls.nests(decl) {
if this.decls.member_in(owner, name, true) == decl {
this.decls.nested.remove(&key);
}
return;
}
this.unchain(this.decls.member_in(owner, name, false), decl, key, chained::members);
}
#! Which dictionary holds the first of a chain of overloads by its key.
enum chained : u8 {
members, #! A type's members, `decls.members`.
added, #! Those declared outside `core` on `core`'s type, `decls.added`.
modules, #! A module's, `modules.decls`.
}
#! Takes the declaration out of the chain of overloads beginning with `first`,
#! which the dictionary holds by the key.
#! @param this The checker.
#! @param first The first of the overloads, or 0.
#! @param decl The declaration to take out.
#! @param key The key the dictionary holds the first by.
#! @param held Which dictionary that is.
def checker::unchain(&mut this, first: u32, decl: u32, key: u64, held: chained): void {
return if first == 0;
if first == decl {
val next: u32 = this.decls.overloads.p[decl];
var dict := match held {
chained::members => &mut this.decls.members,
chained::added => &mut this.decls.added,
chained::modules => &mut this.modules.decls
};
if next == 0 { dict.remove(&key); } else { dict.set(key, next); }
return;
}
var walk := this.decls.walk_overloads(first, 4096);
while true {
val at := walk.next();
break if at == 0;
if this.decls.overloads.p[at] == decl {
val after: u32 = this.decls.overloads.p[decl];
this.decls.overloads.p[at] = after;
return;
}
}
}
#! Declares a top-level declaration again, at its new node, keeping its ID: it
#! leaves where it was declared and is declared there again, with its members
#! and type parameters made anew.
#! @param this The checker.
#! @param decl The declaration.
#! @param node Its new node.
#! @param home The module it is declared in.
def checker::redeclare(&mut this, decl: u32, node: u32, home: u32): void {
val kind: dk = this.decls.kinds.p[decl];
val owner: u32 = this.decls.owners.p[decl];
val name: u32 = this.decls.names.p[decl];
if owner != 0 {
this.leave_type(owner, decl);
} else {
val key := pair_key(this.decls.modules.p[decl], name);
this.unchain(this.modules.decl(this.decls.modules.p[decl], name), decl, key, chained::modules);
}
this.decls.renew(decl, node, home);
this.decl_bases.put(decl, 0);
this.decl_values.put(decl, 0);
val flags := this.node_flags(node);
if (flags & nf::exported as u32) != 0 { this.decls.mark(decl, df::exported); }
if (flags & nf::mutable as u32) != 0 { this.decls.mark(decl, df::mutable); }
if (flags & nf::constant as u32) != 0 { this.decls.mark(decl, df::constant); }
this.mark_attributes(decl, node);
if this.external {
this.mark_external(decl, kind, node);
}
if kind == dk::function {
this.mark_signature(decl, node);
}
this.decls.top = node;
if this.receiver_of(node) != 0 {
this.pending.append(decl);
} else {
this.add_global(decl);
}
val own := this.declare_params(this.own_params_of(node), decl);
this.record_shape(decl, this.own_params_of(node));
if this.receiver_of(node) == 0 {
this.set_params(decl, own.span);
} else {
this.decls.params.p[decl] = this.decls.make_list(own.span);
}
this.declare_members(decl, node);
this.decls.top = 0;
}
#! Links what was declared anew: the file's imports, the declarations made
#! through types, and the types of new type declarations.
#! @param this The checker.
#! @param file The file.
def checker::relink(&mut this, file: u32): void {
var imports := vector::<u32>::new();
val decls := this.left_of(this.units[file].root);
for &node in this.run_view(decls) {
continue if this.kind_of(node) != nk::import_decl;
val found := this.module_path(this.left_of(node), false);
if found == 0 {
this.report(node, ec::undefined, "No file declares this module.");
continue;
}
imports.append(found);
}
val run := this.decls.make_list(imports.span);
this.units[file].imports = run;
this.attach_pending();
this.pending.clear();
this.give_accessors_params();
this.name_types();
}
#! Lets go of what was worked out of types and traits, which what changed may
#! have changed, to be worked out again as it is needed.
#! @param this The checker.
def checker::forget_caches(&mut this): void {
this.satisfied.clear();
this.types.forget();
this.only_bounds.clear();
this.outers.clear();
this.composed.resize(0);
for id in 1..this.decls.count {
this.decls.flags.p[id] &= ~(df::composed as u32);
}
this.known.clear();
this.worked_again.clear();
this.forget_paths();
}
#! Forgets what the paths an argument's checking looked at name, which checking
#! again finds anew.
#! @param this The checker.
def checker::forget_paths(&mut this): void {
this.named_ids.clear();
this.named_paths.resize(1);
this.chosen_arms.clear();
}
#! Checks a top-level declaration again: its notes are let go of, what it and
#! what it declares worked out is let go of, and it is resolved and checked as
#! though for the first time, with the instances of it the program uses.
#! @param this The checker.
#! @param decl The top-level declaration.
def checker::check_again(&mut this, decl: u32): void {
val top: u32 = this.decls.tops.p[decl];
val file: u32 = this.decls.files.p[decl];
this.forget_notes(decl);
var family := vector::<u32>::new();
for &each in this.decls.by_file.of(file) {
if each == decl || (top != 0 && this.decls.tops.p[each] == top && this.decls.kinds.p[each] != dk::none) {
family.append(each);
}
}
for &each in family {
this.decls.states.p[each as u32] = ds::unresolved;
this.decls.flags.p[each as u32] &= ~(df::checked as u32 | df::failed as u32);
}
this.clear_results(decl);
this.forget_paths();
for &each in family {
this.resolve(each);
}
for &each in family {
this.check_decl(each);
}
# The instances of what is checked again are checked again with it.
for i in 1..this.instances.length {
val held: instance = this.instances[i];
continue if !family.span.contains(&held.decl);
var from := this.decls.list_of(this.decls.params.p[held.decl]);
var args := this.types.parts_of(held.args);
val kind := this.substitute(this.decls.types.p[held.decl], from.span, args.span);
this.instances[i].kind = kind;
this.instances[i].table = 0;
this.instances[i].value = 0;
this.wants.append(i as u32);
}
}
#! Lets go of what was worked out of a declaration's nodes in its file's tables.
#! @param this The checker.
#! @param decl The top-level declaration.
def checker::clear_results(&mut this, decl: u32) : void {
val file: u32 = this.decls.files.p[decl];
val first := this.first_node(decl);
val last: u32 = this.decls.nodes.p[decl];
for node in first..(last + 1) {
val n := node as u32;
val table := this.file_table_of(file, n);
continue if table == checker::no_table;
val held: rtable = this.tables.p[table];
this.drop_results(held.offset as u64 + (n - held.base) as u64);
val key := table_node { table = table, node = n };
this.reflections.remove(&key);
this.lengths.remove(&key);
this.called.remove(&key);
}
}
#! Lets go of the notes the checker made checking the declaration, or what it
#! declares.
#! @param this The checker.
#! @param decl The top-level declaration.
def checker::forget_notes(&mut this, decl: u32): void {
# Those kept are moved down over those let go of, which are closed as they
# are written over, and those left past the last kept are closed with them.
var kept: u64 = 0;
for i in 0..this.sink.notes.length {
val origin := this.sink.notes[i].origin;
continue if origin != 0 && this.top_decl(origin) == decl;
if kept != i {
this.sink.notes[kept] = move(&mut this.sink.notes[i]);
}
kept++;
}
while this.sink.notes.length > kept {
this.sink.notes.truncate();
}
}
#! Counts the notes the checker made, which the most it makes bounds, and
#! whether the program is valid, once some are made again.
#! @param this The checker.
def checker::recount_notes(&mut this): void {
var count: u32 = 0;
for i in 0..this.sink.notes.length {
if this.sink.notes[i].origin != 0 {
count++;
}
}
this.sink.count = count;
this.failed = count != 0;
}