Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+540
View File
@@ -0,0 +1,540 @@
#! @file names.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what the name at a position refers to, for an editor to go to where
#! that is declared, and everywhere it is written, for an editor to find its
#! references and rename it, with why some names cannot be renamed.
module sakura::analyzer;
#! What the name at a position refers to.
pub struct referent {
pub var at: spot; #! The name at the position.
pub var declaration: spot; #! Where what it refers to is declared, where `declared` says it is.
pub var declared: bool; #! Whether it has a declaration to go to, which a built-in type has not.
pub var refusal: strview; #! Why it cannot be renamed, as that it is built into the language, or empty where it can.
pub var local: bool; #! Whether it is a local, a parameter, a type parameter, or a name a pattern binds, which is only written within the declaration holding it.
pub var member: bool; #! Whether it is a member of a type, as a field, method, property, member of an enum, or associated variable, which is written after `.` or `::` wherever it is not declared.
}
#! What the name at the offset of the file refers to, used or declared there. A
#! name the offset is just past is the one asked about, as an editor puts the
#! cursor after the name it asks about.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns What it refers to, or nothing where no name the checker resolved is
#! there.
pub def checker::name_at(&mut this, file: u32, offset: u32): maybe<referent> {
var meant := checker::meant {};
val node := this.seek_name(file, offset, &mut meant);
return nothing {} if node == 0;
var made := referent {
at = this.spot_of(file, this.units[file].syntax.node_tokens[node]),
refusal = ""
};
var declared := spot {};
made.declared = this.declared_at(&meant, &mut declared);
made.declaration = declared;
made.refusal = this.refusal_of(&meant);
made.local = meant.local != 0 || (meant.decl != 0 && this.decls.kinds.p[meant.decl] == dk::type_param);
made.member = meant.decl != 0 && this.decls.owners.p[meant.decl] != 0
&& this.decls.kinds.p[meant.decl] != dk::type_param;
return made;
}
#! Finds the node naming something at the offset of the file, or at the byte
#! before it.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @param meant Where what it refers to is written.
#! @returns The node, or 0.
def checker::seek_name(&mut this, file: u32, offset: u32, meant: &mut checker::meant): u32 {
val syntax := &this.units[file].syntax;
val asked := this.begin_query(file);
var found: u32 = 0;
for back in 0..2 {
break if found != 0 || back as u32 > offset;
val token := this.token_at(file, offset - back as u32);
continue if token as u64 >= syntax.token_count || syntax.token_kinds[token] != tk::name;
for &node in asked.walked.order {
continue if this.token_of(node) != token;
val each := this.meaning_of(node, &asked.walked, &asked.decls);
if each.found() {
meant = each;
found = node;
break;
}
}
}
return found;
}
#! Where what a name refers to is declared: the name of its declaration, of the
#! node declaring a local, or of the first module declaration naming a module.
#! @param this The checker.
#! @param meant What the name refers to.
#! @param out Where it is written.
#! @returns Whether it has a declaration to go to.
def checker::declared_at(&mut this, meant: &checker::meant, out: &mut spot): bool {
if meant.local != 0 {
out = this.spot_of(meant.file, this.units[meant.file].syntax.node_tokens[meant.local]);
return true;
}
if meant.is_module {
return this.module_declared(meant.named_module, out);
}
val decl := meant.decl;
return false if decl == 0 || this.decls.kinds.p[decl] == dk::builtin;
val file: u32 = this.decls.files.p[decl];
val node: u32 = this.decls.nodes.p[decl];
return false if node == 0;
val syntax := &this.units[file].syntax;
val token: u32 = syntax.node_tokens[node];
return false if syntax.token_names[token] != this.decls.names.p[decl];
out = this.spot_of(file, token);
return true;
}
#! Where a module is declared: the last segment of the first `module` naming it,
#! in the order the files were given.
#! @param this The checker.
#! @param id The module.
#! @param out Where it is written.
#! @returns Whether a file declares it.
def checker::module_declared(&mut this, id: u32, out: &mut spot): bool {
for file in 0..this.units.length {
val syntax := &this.units[file].syntax;
val decls := syntax.node_lefts[this.units[file].root];
for &node in syntax.run_view(decls) {
continue if syntax.node_kinds[node] != nk::module_decl && syntax.node_kinds[node] != nk::extern_module;
val path: u32 = syntax.node_lefts[node];
continue if this.module_named(file as u32, path, 0xFFFFFFFF) != id;
val run: u32 = syntax.node_lefts[path];
val last := run == 0 ? path : syntax.run_at(run, syntax.run_length(run) - 1);
out = this.spot_of(file as u32, syntax.node_tokens[last]);
return true;
}
}
return false;
}
#! Why renaming what a name refers to would lose it, or empty text where it
#! would not: what is built in, a module, `this`, and what the compiler finds by
#! its name.
#! @param this The checker.
#! @param meant What the name refers to.
#! @returns Why it cannot be renamed, or empty text.
def checker::refusal_of(&mut this, meant: &checker::meant): strview {
if meant.is_module {
return "A module is named by every file that declares or imports it, which renaming does not change.";
}
if meant.local != 0 {
val name := this.local_text(meant.file, meant.local);
if name == "this" {
return "`this` is what every method calls its receiver.";
}
val syntax := &this.units[meant.file].syntax;
if syntax.node_kinds[meant.local] == nk::param && (syntax.flags_at(meant.local) & nf::implied as u32) != 0 {
return "`value` is what every `set` calls what it is given.";
}
return "";
}
val decl := meant.decl;
return "" if decl == 0;
if this.decls.kinds.p[decl] == dk::builtin || this.decls.nodes.p[decl] == 0 {
return "It is built into the language.";
}
if this.decls.kinds.p[decl] == dk::type_param && this.decls.names.p[decl] == names::self_id {
return "`self` is what implements the trait it is written within.";
}
if this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::intrinsic) {
return "An `@intrinsic` function is bound to what implements it by its name.";
}
if this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::foreign) && !this.has_body(decl) {
return "A function without a body in an `extern module` is the C function of its name.";
}
if (this.decls.kinds.p[decl] == dk::struct_type || this.decls.kinds.p[decl] == dk::union_type) && this.decls.has(decl, df::foreign) {
return this.decls.kinds.p[decl] == dk::union_type
? "A union of an `extern module` is C's: every field begins where it does, and nothing says which it holds."
: "A struct of an `extern module` is laid out as C lays it out.";
}
return this.compiler_names(decl);
}
#! Why renaming a declaration would lose it to the compiler, which finds some by
#! their names: the declarations of std it knows, as the types it makes values
#! of and the traits whose methods it calls, the methods those traits ask for,
#! the methods its arithmetic operators call, where a program begins, and the
#! runtime an executable calls into.
#! @param this The checker.
#! @param decl The declaration.
#! @returns Why it cannot be renamed, or empty text.
def checker::compiler_names(&mut this, decl: u32): strview {
for &each in this.known {
if each.value == decl && decl != 0 {
return match this.decls.kinds.p[decl] {
dk::trait_type => "The compiler finds it by its name, as a trait whose methods it calls or whose meaning it decides.",
dk::struct_type, dk::union_type, dk::enum_type, dk::alias_type => "The compiler finds it by its name, as a type it makes values of.",
else => "The compiler finds it by its name."
};
}
}
if this.in_runtime(decl) && this.decls.kinds.p[decl] != dk::field {
return "The compiler calls into `runtime` by name.";
}
if this.decls.kinds.p[decl] == dk::function && this.decls.owners.p[decl] == 0 && this.decls.modules.p[decl] == 0
&& this.decls.names.p[decl] == word::main {
return "A program begins at the function named `main`.";
}
# A method the compiler calls for every type implementing one of its traits.
if this.decls.kinds.p[decl] == dk::function && this.decls.owners.p[decl] != 0 {
var traits := this.traits_asking(decl);
for &trait_decl in traits {
for &each in this.known {
if each.value == trait_decl {
return "The compiler calls it by its name, for every type implementing the trait asking for it.";
}
}
}
}
# A method an operator calls, which a type has the operator by declaring: `add`
# for `+`, and the rest, taking the other operand, and `negate` for `-x` and
# `bit_not` for `~x`.
if this.decls.kinds.p[decl] == dk::function && this.decls.has(decl, df::method) {
val name: u32 = this.decls.names.p[decl];
val taken := this.types.part_count(this.decls.types.p[decl]);
if name == word::negate && taken == 1 {
return "The compiler calls it by its name, as the method `-x` is.";
}
if name == word::bit_not && taken == 1 {
return "The compiler calls it by its name, as the method `~x` is.";
}
if taken == 2 && (name == word::add || name == word::sub || name == word::mul
|| name == word::div || name == word::rem || name == word::bit_and
|| name == word::bit_or || name == word::bit_xor || name == word::shift_left
|| name == word::shift_right) {
return "The compiler calls it by its name, as the method an operator is, as `+` is `add`.";
}
}
return "";
}
#! Whether a declaration is of the module `runtime`, which only an executable
#! holds.
#! @param this The checker.
#! @param decl The declaration.
#! @returns Whether it is.
def checker::in_runtime(&this, decl: u32): bool {
val home: u32 = this.decls.modules.p[decl];
return home != 0 && this.modules.parents[home] == 0 && this.names.text(this.modules.names[home]) == "runtime";
}
#! The traits that ask for a method: its own type's, where that is a trait, and
#! those its type implements, or those compose, with a member of its name.
#! @param this The checker.
#! @param method The method.
#! @returns The traits' declarations.
def checker::traits_asking(&mut this, method: u32): vector<u32> {
var made := vector::<u32>::new();
val owner: u32 = this.decls.owners.p[method];
return made if owner == 0;
if this.decls.kinds.p[owner] == dk::trait_type {
made.append(owner);
}
this.add_asking(owner, this.decls.names.p[method], &mut made, 0);
return made;
}
#! Adds the traits a type implements, or a trait composes, and those they
#! compose, that have a member of the name.
#! @param this The checker.
#! @param decl The type or trait.
#! @param name The ID of the name.
#! @param into The traits found so far.
#! @param depth How deeply composition has been followed.
def checker::add_asking(&mut this, decl: u32, name: u32, into: &mut vector<u32>, depth: u32): void {
return if depth > 16;
val kind: dk = this.decls.kinds.p[decl];
return if kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type;
val run := this.composed_of(decl);
for i in 0..this.decls.list_length(run) {
val trait_decl := this.types.first_of(this.decls.list_at(run, i as u32));
continue if trait_decl == 0 || this.decls.kinds.p[trait_decl] != dk::trait_type;
if this.decls.member(trait_decl, name) != 0 {
var known := false;
for &each in into {
known = known || each == trait_decl;
}
if !known {
into.append(trait_decl);
}
}
this.add_asking(trait_decl, name, into, depth + 1);
}
}
#! Whether a trait is another, or composes it.
#! @param this The checker.
#! @param trait_decl The trait.
#! @param target The trait it may compose.
#! @param depth How deeply composition has been followed.
#! @returns Whether it does.
def checker::trait_composes(&mut this, trait_decl: u32, target: u32, depth: u32): bool {
return true if trait_decl == target;
return false if depth > 16 || this.decls.kinds.p[trait_decl] != dk::trait_type;
val run := this.composed_of(trait_decl);
for i in 0..this.decls.list_length(run) {
return true if this.trait_composes(this.types.first_of(this.decls.list_at(run, i as u32)), target, depth + 1);
}
return false;
}
#! Everywhere the name at the offset of the file refers to is written, its
#! declaration among them, in every file of the program, ordered by file and
#! then by where in it they are. Each place is written with the very text of
#! the name, so an editor renames it by replacing each.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns The places, which are none where no name the checker resolved is
#! there.
pub def checker::references(&mut this, file: u32, offset: u32): vector<spot> {
var found := vector::<spot>::new();
var strays := vector::<spot>::new();
this.refer_all(file, offset, &mut found, &mut strays);
return found;
}
#! The places the name at the offset of the file is written but the checker left
#! unresolved, as in a branch of `when` for another target, which may refer to
#! the same thing. A local is sought only within the declaration holding it.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @returns The places, ordered as `references` orders them.
pub def checker::unresolved(&mut this, file: u32, offset: u32): vector<spot> {
var found := vector::<spot>::new();
var strays := vector::<spot>::new();
this.refer_all(file, offset, &mut found, &mut strays);
return strays;
}
#! Everywhere what the name at a place refers to is written, and where its text
#! is written but unresolved, as `references` and `unresolved` give them.
#! @param this The checker.
#! @param file The file.
#! @param offset The byte of its text.
#! @param found Where the places it is written are put.
#! @param strays Where the places it is written unresolved are put.
pub def checker::refer_all(&mut this, file: u32, offset: u32, found: &mut vector<spot>, strays: &mut vector<spot>): void {
var meant := checker::meant {};
val named := this.seek_name(file, offset, &mut meant);
return if named == 0;
var declared := spot {};
return if !this.declared_at(&meant, &mut declared);
val name: u32 = this.units[declared.file].syntax.token_names[this.token_at(declared.file, declared.offset)];
var sought := vector::<checker::meant>::new();
sought.append(meant);
this.widen(&meant, &mut sought);
# The top-level declaration holding a local, which alone may name it.
var home_top: u32 = 0;
if meant.local != 0 {
home_top = this.top_holding(meant.file, meant.local);
}
for each in 0..this.units.length {
val at := each as u32;
continue if meant.local != 0 && at != meant.file;
val syntax := &this.units[at].syntax;
val asked := this.begin_query(at);
val walked := &asked.walked;
for &node in walked.order {
val token: u32 = syntax.node_tokens[node];
continue if syntax.token_names[token] != name || syntax.token_kinds[token] != tk::name;
continue if home_top != 0 && this.top_of(walked, node) != home_top;
val held := this.meaning_of(node, walked, &asked.decls);
var hit := false;
for &want in sought {
hit = hit || held.same(&want);
}
if hit {
this.add_place(found, this.spot_of(at, token));
if syntax.node_kinds[node] == nk::type_param || syntax.node_kinds[node] == nk::const_param {
this.refer_bounds(at, node, found);
}
} else if !held.found() {
val kind := syntax.node_kinds[node];
val unnamed_type := (kind == nk::type_name && syntax.node_lefts[node] == 0) || kind == nk::segment;
if kind == nk::name || kind == nk::member || unnamed_type
|| (kind == nk::field_init && (syntax.flags_at(node) & nf::named as u32) != 0) {
this.add_place(strays, this.spot_of(at, token));
}
}
}
}
this.sort_places(found);
this.sort_places(strays);
}
#! Adds a place, once.
#! @param this The checker.
#! @param into The places.
#! @param place The place.
def checker::add_place(&this, into: &mut vector<spot>, place: spot): void {
for &each in into {
return if each.file == place.file && each.offset == place.offset;
}
into.append(place);
}
#! Orders places by the path of their file, then where in it they are, so the
#! places of a name come in the same order however the files were given.
#! @param this The checker.
#! @param places The places.
def checker::sort_places(&this, places: &mut vector<spot>): void {
places.sort_by(def(a: &spot, b: &spot): bool { return this.place_before(a, b); });
}
#! Whether one place comes before another.
#! @param this The checker.
#! @param a The one place.
#! @param b The other.
#! @returns Whether `a` comes first.
def checker::place_before(&this, a: &spot, b: &spot): bool {
if a.file != b.file {
return this.units[a.file].source.path.compare(this.units[b.file].source.path) < 0;
}
return a.offset < b.offset;
}
#! The top-level declaration holding a node of a file.
#! @param this The checker.
#! @param file The file.
#! @param node The node.
#! @returns The top-level declaration's node.
def checker::top_holding(&this, file: u32, node: u32): u32 {
val walked := this.tree_of(file);
return this.top_of(&walked, node);
}
#! The top-level node holding a node, which is the node itself where the
#! document holds it.
#! @param this The checker.
#! @param walked The file's tree.
#! @param node The node.
#! @returns The top-level node.
def checker::top_of(&this, walked: &tree_walk, node: u32): u32 {
var at := node;
var steps := 0;
while steps < 4096 {
val parent: u32 = walked.parents[at];
return at if parent == 0 || walked.parents[parent] == 0;
at = parent;
steps++;
}
return at;
}
#! A `where` clause names the type parameters it bounds, as `T` of
#! `where T: show`, though no node holds the name there, since the bounds are
#! given to the parameter. So the tokens after the parameter are read up to the
#! body, or the `;` ending the declaration, for each name that follows `where`
#! or `,` and is followed by `:`.
#! @param this The checker.
#! @param file The file.
#! @param param The `type_param` node.
#! @param into Where the places are put.
def checker::refer_bounds(&this, file: u32, param: u32, into: &mut vector<spot>): void {
val syntax := &this.units[file].syntax;
val name: u32 = syntax.token_names[syntax.node_tokens[param]];
var depth := 0;
var clause := false;
var at: u32 = syntax.node_tokens[param] + 1;
while at as u64 + 1 < syntax.token_count {
val kind := syntax.token_kinds[at];
if kind == tk::lparen || kind == tk::lbracket || kind == tk::less {
depth++;
} else if (kind == tk::rparen || kind == tk::rbracket || kind == tk::more) && depth > 0 {
depth--;
}
break if depth == 0 && (kind == tk::lbrace || kind == tk::semicolon);
if depth == 0 && kind == tk::kw_where {
clause = true;
} else if clause && kind == tk::name && syntax.token_names[at] == name && syntax.token_kinds[at + 1] == tk::colon {
val prior := syntax.token_kinds[at - 1];
if prior == tk::kw_where || prior == tk::comma {
this.add_place(into, this.spot_of(file, at));
}
}
at++;
}
}
#! A method a trait asks for is one name with each method implementing it, since
#! a type implements a trait by the names of its methods: renaming one alone
#! would leave a type no longer implementing a trait it declares. So a trait's
#! method is sought with the method of the name of every type declaring the
#! trait, directly or through one composing it, and a method of a type with the
#! members of the traits its type declares that ask for it, and so with theirs.
#! @param this The checker.
#! @param meant What the name refers to.
#! @param sought What is sought, which the rest are added to.
def checker::widen(&mut this, meant: &checker::meant, sought: &mut vector<checker::meant>): void {
val decl := meant.decl;
return if decl == 0 || this.decls.kinds.p[decl] != dk::function || this.decls.owners.p[decl] == 0;
var traits := this.traits_asking(decl);
return if traits.length == 0;
val name: u32 = this.decls.names.p[decl];
this.add_family(this.decls.owners.p[decl], name, sought);
for &trait_decl in traits {
this.add_family(trait_decl, name, sought);
for id in 1..this.decls.count {
val type_decl := id as u32;
val kind: dk = this.decls.kinds.p[type_decl];
continue if kind != dk::struct_type && kind != dk::union_type && kind != dk::trait_type;
val run := this.composed_of(type_decl);
for i in 0..this.decls.list_length(run) {
val named := this.types.first_of(this.decls.list_at(run, i as u32));
if this.trait_composes(named, trait_decl, 0) {
this.add_family(type_decl, name, sought);
break;
}
}
}
}
}
#! Adds each member of the name a type has, which overloads of a trait's method
#! are renamed together, as `at(&this)` and `at(&mut this)`.
#! @param this The checker.
#! @param owner The type.
#! @param name The ID of the name.
#! @param sought What is sought.
def checker::add_family(&mut this, owner: u32, name: u32, sought: &mut vector<checker::meant>): void {
var walk := this.decls.walk_overloads(this.decls.member(owner, name), 256);
while true {
val at := walk.next();
break if at == 0;
val each := checker::meant { decl = this.standing_for(at) };
var known := false;
for &held in sought {
known = known || held.same(&each);
}
if !known {
sought.append(each);
}
}
}